linux設(shè)備模型之uart驅(qū)動(dòng)架構(gòu)分析
一:前言
接著前面的終端控制臺(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)別論了^_^)。
流程圖如下:
五: 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)了。