linux設(shè)備模型之uart驅(qū)動(dòng)架構(gòu)分析
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)了。
評論