打通linux的tty驅(qū)動(dòng)的數(shù)據(jù)鏈路
{
……
while((head = tty->buf.head) != NULL) {
………
count= head->commit – head->read;
………
char_buf= head->char_buf_ptr + head->read;
flag_buf= head->flag_buf_ptr + head->read;
head->read+= count;
disc->ops->receive_buf(tty,char_buf,
flag_buf,count);
………
}
……
}
這個(gè)函數(shù)主要的功能是,從tty_buffer中找到數(shù)據(jù)緩沖區(qū)char_buf_ptr,并將這個(gè)緩沖區(qū)指針傳遞給線路規(guī)程的操作函數(shù)receive_buf.再來(lái)看receive_buf:
static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char*cp,
char *fp, int count)
{
……
if(tty->real_raw) {
………
memcpy(tty->read_buf+ tty->read_head, cp, i);
………
}else{
………
switch(flags) {
caseTTY_NORMAL:
n_tty_receive_char(tty,*p);
break;
……
}
if(tty->ops->flush_chars)
tty->ops->flush_chars(tty);
………
}
………
}
從上面這段代碼可以看到,if條件成立,明顯地是拷貝數(shù)據(jù)進(jìn)tty的read_buf;進(jìn)入else,在正常的狀態(tài)下會(huì)調(diào)用n_tty_receive_char,然后會(huì)調(diào)用put_tty_queue,在這個(gè)函數(shù)里最終還是把數(shù)據(jù)拷貝到tty的read_buf中。
到此,tty驅(qū)動(dòng)的讀操作數(shù)據(jù)鏈路基本上連通了。
uart_write:
static int uart_write(struct tty_struct *tty,
const unsigned char *buf, int count)
{
……
port= state->uart_port;
circ= state->xmit;
……
while(1){
c= CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
………
memcpy(circ->buf+ circ->head, buf, c);
………
}
……
uart_start(tty);
return ret;
}
上面代碼的意思是把要寫(xiě)的數(shù)據(jù)拷貝到state的緩沖區(qū)里。然后調(diào)用uart_start.
static void __uart_start(struct tty_struct *tty)
{
struct uart_state *state = tty->driver_data;
struct uart_port *port = state->uart_port;
if(!uart_circ_empty(state->xmit) state->xmit.buf
!tty->stopped !tty->hw_stopped)
port->ops->start_tx(port);
}
調(diào)用了uart_port的操作函數(shù)集的start_tx.
static void serial8250_start_tx(struct uart_port *port)
{
struct uart_8250_port *up = container_of(port, struct uart_8250_port, port);
……
transmit_chars(up);
………
}
在transmit_chars中會(huì)把state->xmit緩沖區(qū)的數(shù)據(jù)寫(xiě)進(jìn)串口發(fā)送數(shù)據(jù)寄存器,也就是數(shù)據(jù)到達(dá)硬件層。到此,寫(xiě)操作的數(shù)據(jù)鏈路也連通。
評(píng)論