當(dāng)前位置:首頁 > 嵌入式 > 嵌入式軟件
[導(dǎo)讀] 一:前言接著前面的終端控制臺(tái)分析,接下來分析serial的驅(qū)動(dòng)。在linux中,serial也對應(yīng)著終端,通常被稱為串口終端。在shell上,我們看到的/dev/ttyS*就是串口終端所對應(yīng)的

 一:前言

接著前面的終端控制臺(tái)分析,接下來分析serial的驅(qū)動(dòng)。在linux中,serial也對應(yīng)著終端,通常被稱為串口終端。在shell上,我們看到的/dev/ttyS*就是串口終端所對應(yīng)的設(shè)備節(jié)點(diǎn)。

在分析具體的serial驅(qū)動(dòng)之前。有必要先分析uart驅(qū)動(dòng)架構(gòu)。uart是Universal Asynchronous Receiver and Transmitter的縮寫。翻譯成中文即為”通用異步收發(fā)器”。它是串口設(shè)備驅(qū)動(dòng)的封裝層。

二:uart驅(qū)動(dòng)架構(gòu)概貌

如下圖所示:

上圖中紅色部份標(biāo)識(shí)即為uart部份的操作。

從上圖可以看到,uart設(shè)備是繼tty_driver的又一層封裝。實(shí)際上uart_driver就是對應(yīng)tty_driver.在它的操作函數(shù)中,將操作轉(zhuǎn)入uart_port.

在寫操作的時(shí)候,先將數(shù)據(jù)放入一個(gè)叫做circ_buf的環(huán)形緩存區(qū)。然后uart_port從緩存區(qū)中取數(shù)據(jù),將其寫入到串口設(shè)備中。

當(dāng)uart_port從serial設(shè)備接收到數(shù)據(jù)時(shí),會(huì)將設(shè)備放入對應(yīng)line discipline的緩存區(qū)中。

這樣。用戶在編寫串口驅(qū)動(dòng)的時(shí)候,只先要注冊一個(gè)uart_driver.它的主要作用是定義設(shè)備節(jié)點(diǎn)號(hào)。然后將對設(shè)備的各項(xiàng)操作封裝在uart_port.驅(qū)動(dòng)工程師沒必要關(guān)心上層的流程,只需按硬件規(guī)范將uart_port中的接口函數(shù)完成就可以了。

三:uart驅(qū)動(dòng)中重要的數(shù)據(jù)結(jié)構(gòu)及其關(guān)聯(lián)

我們可以自己考慮下,基于上面的架構(gòu)代碼應(yīng)該要怎么寫。首先考慮以下幾點(diǎn):

1: 一個(gè)uart_driver通常會(huì)注冊一段設(shè)備號(hào)。即在用戶空間會(huì)看到uart_driver對應(yīng)有多個(gè)設(shè)備節(jié)點(diǎn)。例如:

/dev/ttyS0 /dev/ttyS1

每個(gè)設(shè)備節(jié)點(diǎn)是對應(yīng)一個(gè)具體硬件的,從上面的架構(gòu)來看,每個(gè)設(shè)備文件應(yīng)該對應(yīng)一個(gè)uart_port.

也就是說:uart_device怎么同多個(gè)uart_port關(guān)系起來?怎么去區(qū)分操作的是哪一個(gè)設(shè)備文件?

2:每個(gè)uart_port對應(yīng)一個(gè)circ_buf,所以u(píng)art_port必須要和這個(gè)緩存區(qū)關(guān)系起來

回憶tty驅(qū)動(dòng)架構(gòu)中。tty_driver有一個(gè)叫成員指向一個(gè)數(shù)組,即tty->ttys.每個(gè)設(shè)備文件對應(yīng)設(shè)數(shù)組中的一項(xiàng)。而這個(gè)數(shù)組所代碼的數(shù)據(jù)結(jié)構(gòu)為tty_struct. 相應(yīng)的tty_struct會(huì)將tty_driver和ldisc關(guān)聯(lián)起來。

那在uart驅(qū)動(dòng)中,是否也可用相同的方式來處理呢?

將uart驅(qū)動(dòng)常用的數(shù)據(jù)結(jié)構(gòu)表示如下:

結(jié)合上面提出的疑問??梢院芮宄目炊@些結(jié)構(gòu)的設(shè)計(jì)。

四:uart_driver的注冊操作

Uart_driver注冊對應(yīng)的函數(shù)為: uart_register_driver()代碼如下:

int uart_register_driver(struct uart_driver *drv)

{

struct tty_driver *normal = NULL;

int i, retval;

BUG_ON(drv->state);

/*

* Maybe we should be using a slab cache for this, especially if

* we have a large number of ports to handle.

*/

drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);

retval = -ENOMEM;

if (!drv->state)

goto out;

normal = alloc_tty_driver(drv->nr);

if (!normal)

goto out;

drv->tty_driver = normal;

normal->owner = drv->owner;

normal->driver_name = drv->driver_name;

normal->name = drv->dev_name;

normal->major = drv->major;

normal->minor_start = drv->minor;

normal->type = TTY_DRIVER_TYPE_SERIAL;

normal->subtype = SERIAL_TYPE_NORMAL;

normal->init_termios = tty_std_termios;

normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;

normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;

normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;

normal->driver_state = drv;

tty_set_operations(normal, &uart_ops);

/*

* Initialise the UART state(s)。

*/

for (i = 0; i < drv->nr; i++) {

struct uart_state *state = drv->state + i;

state->close_delay = 500; /* .5 seconds */

state->closing_wait = 30000; /* 30 seconds */

mutex_init(&state->mutex);

}

retval = tty_register_driver(normal);

out:

if (retval < 0) {

put_tty_driver(normal);

kfree(drv->state);

}

return retval;

}

從上面代碼可以看出。uart_driver中很多數(shù)據(jù)結(jié)構(gòu)其實(shí)就是tty_driver中的。將數(shù)據(jù)轉(zhuǎn)換為tty_driver之后,注冊tty_driver.然后初始化uart_driver->state的存儲(chǔ)空間。

這樣,就會(huì)注冊u(píng)art_driver->nr個(gè)設(shè)備節(jié)點(diǎn)。主設(shè)備號(hào)為uart_driver-> major. 開始的次設(shè)備號(hào)為uart_driver-> minor.

值得注意的是。在這里將tty_driver的操作集統(tǒng)一設(shè)為了uart_ops.其次,在tty_driver-> driver_state保存了這個(gè)uart_driver.這樣做是為了在用戶空間對設(shè)備文件的操作時(shí),很容易轉(zhuǎn)到對應(yīng)的uart_driver.

另外:tty_driver的flags成員值為: TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV.里面包含有TTY_DRIVER_DYNAMIC_DEV標(biāo)志。結(jié)合之前對tty的分析。如果包含有這個(gè)標(biāo)志,是不會(huì)在初始化的時(shí)候去注冊device.也就是說在/dev/下沒有動(dòng)態(tài)生成結(jié)點(diǎn)(如果是/dev下靜態(tài)創(chuàng)建了這個(gè)結(jié)點(diǎn)就另當(dāng)別論了^_^)。

流程圖如下:

[!--empirenews.page--]

五: uart_add_one_port()操作

在前面提到。在對uart設(shè)備文件過程中。會(huì)將操作轉(zhuǎn)換到對應(yīng)的port上,這個(gè)port跟uart_driver是怎么關(guān)聯(lián)起來的呢?這就是uart_add_ont_port()的主要工作了。

顧名思義,這個(gè)函數(shù)是在uart_driver增加一個(gè)port.代碼如下:

int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)

{

struct uart_state *state;

int ret = 0;

struct device *tty_dev;

BUG_ON(in_interrupt());

if (port->line >= drv->nr)

return -EINVAL;

state = drv->state + port->line;

mutex_lock(&port_mutex);

mutex_lock(&state->mutex);

if (state->port) {

ret = -EINVAL;

goto out;

}

state->port = port;

state->pm_state = -1;

port->cons = drv->cons;

port->info = state->info;

/*

* If this port is a console, then the spinlock is already

* initialised.

*/

if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {

spin_lock_init(&port->lock);

lockdep_set_class(&port->lock, &port_lock_key);

}

uart_configure_port(drv, state, port);

/*

* Register the port whether it‘s detected or not. This allows

* setserial to be used to alter this ports parameters.

*/

tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);

if (likely(!IS_ERR(tty_dev))) {

device_can_wakeup(tty_dev) = 1;

device_set_wakeup_enable(tty_dev, 0);

} else

printk(KERN_ERR "Cannot register tty device on line %dn",

port->line);

/*

* Ensure UPF_DEAD is not set.

*/

port->flags &= ~UPF_DEAD;

out:

mutex_unlock(&state->mutex);

mutex_unlock(&port_mutex);

return ret;

}

首先這個(gè)函數(shù)不能在中斷環(huán)境中使用。 Uart_port->line就是對uart設(shè)備文件序號(hào)。它對應(yīng)的也就是uart_driver->state數(shù)組中的uart_port->line項(xiàng)。

它主要初始化對應(yīng)uart_driver->state項(xiàng)。接著調(diào)用uart_configure_port()進(jìn)行port的自動(dòng)配置。然后注冊tty_device.如果用戶空間運(yùn)行了udev或者已經(jīng)配置好了hotplug.就會(huì)在/dev下自動(dòng)生成設(shè)備文件了。

操作流程圖如下所示:

六:設(shè)備節(jié)點(diǎn)的open操作

在用戶空間執(zhí)行open操作的時(shí)候,就會(huì)執(zhí)行uart_ops->open. Uart_ops的定義如下:

static const struct tty_operations uart_ops = {

.open = uart_open,

.close = uart_close,

.write = uart_write,

.put_char = uart_put_char,

.flush_chars = uart_flush_chars,

.write_room = uart_write_room,

.chars_in_buffer= uart_chars_in_buffer,

.flush_buffer = uart_flush_buffer,

.ioctl = uart_ioctl,

.throttle = uart_throttle,

.unthrottle = uart_unthrottle,

.send_xchar = uart_send_xchar,

.set_termios = uart_set_termios,

.stop = uart_stop,

.start = uart_start,

.hangup = uart_hangup,

.break_ctl = uart_break_ctl,

.wait_until_sent= uart_wait_until_sent,

#ifdef CONFIG_PROC_FS

.read_proc = uart_read_proc,

#endif

.tiocmget = uart_tiocmget,

.tiocmset = uart_tiocmset,

};

對應(yīng)open的操作接口為uart_open.代碼如下:

static int uart_open(struct tty_struct *tty, struct file *filp)

{

struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;

struct uart_state *state;

int retval, line = tty->index;

BUG_ON(!kernel_locked());

pr_debug("uart_open(%d) calledn", line);

/*

* tty->driver->num won‘t change, so we won‘t fail here with

* tty->driver_data set to something non-NULL (and therefore

* we won‘t get caught by uart_close())。

*/

retval = -ENODEV;

if (line >= tty->driver->num)

goto fail;

/*

* We take the semaphore inside uart_get to guarantee that we won‘t

* be re-entered while allocating the info structure, or while we

* request any IRQs that the driver may need. This also has the nice

* side-effect that it delays the action of uart_hangup, so we can

* guarantee that info->tty will always contain something reasonable.

*/

state = uart_get(drv, line);

if (IS_ERR(state)) {

retval = PTR_ERR(state);

goto fail;

}

/*

* Once we set tty->driver_data here, we are guaranteed that

* uart_close() will decrement the driver module use count.

* Any failures from here onwards should not touch the count.

*/

tty->driver_data = state;

tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;[!--empirenews.page--]

tty->alt_speed = 0;

state->info->tty = tty;

/*

* If the port is in the middle of closing, bail out now.

*/

if (tty_hung_up_p(filp)) {

retval = -EAGAIN;

state->count--;

mutex_unlock(&state->mutex);

goto fail;

}

/*

* Make sure the device is in D0 state.

*/

if (state->count == 1)

uart_change_pm(state, 0);

/*

* Start up the serial port.

*/

retval = uart_startup(state, 0);

/*

* If we succeeded, wait until the port is ready.

*/

if (retval == 0)

retval = uart_block_til_ready(filp, state);

mutex_unlock(&state->mutex);

/*

* If this is the first open to succeed, adjust things to suit.

*/

if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {

state->info->flags |= UIF_NORMAL_ACTIVE;

uart_update_termios(state);

}

fail:

return retval;

}

int ret = 0;

state = drv->state + line;

if (mutex_lock_interruptible(&state->mutex)) {

ret = -ERESTARTSYS;

goto err;

}

state->count++;

if (!state->port || state->port->flags & UPF_DEAD) {

ret = -ENXIO;

goto err_unlock;

}

if (!state->info) {

state->info = kzalloc(sizeof(struct uart_info), GFP_KERNEL);

if (state->info) {

init_waitqueue_head(&state->info->open_wait);

init_waitqueue_head(&state->info->delta_msr_wait);

/*

* Link the info into the other structures.

*/

state->port->info = state->info;

tasklet_init(&state->info->tlet, uart_tasklet_action,

(unsigned long)state);

} else {

ret = -ENOMEM;

goto err_unlock;

}

}

return state;

err_unlock:

state->count--;

mutex_unlock(&state->mutex);

err:

return ERR_PTR(ret);

}

從代碼中可以看出。這里注要是操作是初始化state->info.注意port->info就是state->info的一個(gè)副本。即port直接通過port->info可以找到它要操作的緩存區(qū)。

uart_startup()代碼如下:

static int uart_startup(struct uart_state *state, int init_hw)

{

struct uart_info *info = state->info;

struct uart_port *port = state->port;

unsigned long page;

int retval = 0;

if (info->flags & UIF_INITIALIZED)

return 0;

/*

* Set the TTY IO error marker - we will only clear this

* once we have successfully opened the port. Also set

* up the tty->alt_speed kludge

*/

set_bit(TTY_IO_ERROR, &info->tty->flags);

if (port->type == PORT_UNKNOWN)

return 0;

/*

* Initialise and allocate the transmit and temporary

* buffer.

*/

if (!info->xmit.buf) {

page = get_zeroed_page(GFP_KERNEL);

if (!page)

return -ENOMEM;

info->xmit.buf = (unsigned char *) page;

uart_circ_clear(&info->xmit);

}

retval = port->ops->startup(port);

if (retval == 0) {

if (init_hw) {

/*

* Initialise the hardware port settings.

*/

uart_change_speed(state, NULL);

/*

* Setup the RTS and DTR signals once the

* port is open and ready to respond.

*/

if (info->tty->termios->c_cflag & CBAUD)

uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);

}

if (info->flags & UIF_CTS_FLOW) {

spin_lock_irq(&port->lock);

if (!(port->ops->get_mctrl(port) & TIOCM_CTS))

info->tty->hw_stopped = 1;

spin_unlock_irq(&port->lock);

}

info->flags |= UIF_INITIALIZED;

clear_bit(TTY_IO_ERROR, &info->tty->flags);

}

if (retval && capable(CAP_SYS_ADMIN))

retval = 0;

return retval;

}

在這里,注要完成對環(huán)形緩沖,即info->xmit的初始化。然后調(diào)用port->ops->startup( )將這個(gè)port帶入到工作狀態(tài)。其它的是一個(gè)可調(diào)參數(shù)的設(shè)置,就不詳細(xì)講解了。

七:設(shè)備節(jié)點(diǎn)的write操作

Write操作對應(yīng)的操作接口為uart_write( )。代碼如下:

static int

uart_write(struct tty_struct *tty, const unsigned char *buf, int count)

{

struct uart_state *state = tty->driver_data;

struct uart_port *port;

struct circ_buf *circ;

unsigned long flags;

int c, ret = 0;

/*

* This means you called this function _after_ the port was

* closed. No cookie for you.

*/

if (!state || !state->info) {

WARN_ON(1);

return -EL3HLT;

}

port = state->port;[!--empirenews.page--]

circ = &state->info->xmit;

if (!circ->buf)

return 0;

spin_lock_irqsave(&port->lock, flags);

while (1) {

c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);

if (count < c)

c = count;

if (c <= 0)

break;

memcpy(circ->buf + circ->head, buf, c);

circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);

buf += c;

count -= c;

ret += c;

}

spin_unlock_irqrestore(&port->lock, flags);

uart_start(tty);

return ret;

}

Uart_start()代碼如下:

static void uart_start(struct tty_struct *tty)

{

struct uart_state *state = tty->driver_data;

struct uart_port *port = state->port;

unsigned long flags;

spin_lock_irqsave(&port->lock, flags);

__uart_start(tty);

spin_unlock_irqrestore(&port->lock, flags);

}

static void __uart_start(struct tty_struct *tty)

{

struct uart_state *state = tty->driver_data;

struct uart_port *port = state->port;

if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&

!tty->stopped && !tty->hw_stopped)

port->ops->start_tx(port);

}

顯然,對于write操作而言,它就是將數(shù)據(jù)copy到環(huán)形緩存區(qū)。然后調(diào)用port->ops->start_tx()將數(shù)據(jù)寫到硬件寄存器。

八:Read操作

Uart的read操作同Tty的read操作相同,即都是調(diào)用ldsic->read()讀取read_buf中的內(nèi)容。有對這部份內(nèi)容不太清楚的,參閱《 linux設(shè)備模型之tty驅(qū)動(dòng)架構(gòu)》.

九:小結(jié)

本小節(jié)是分析serial驅(qū)動(dòng)的基礎(chǔ)。在理解了tty驅(qū)動(dòng)架構(gòu)之后,再來理解uart驅(qū)動(dòng)架構(gòu)應(yīng)該不是很難。隨著我們在linux設(shè)備驅(qū)動(dòng)分析的深入,越來越深刻的體會(huì)到,linux的設(shè)備驅(qū)動(dòng)架構(gòu)很多都是相通的。只要深刻理解了一種驅(qū)動(dòng)架構(gòu)。舉一反三。也就很容易分析出其它架構(gòu)的驅(qū)動(dòng)了。

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運(yùn)營商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡稱"軟通動(dòng)力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉