標籤:
一、應用程式中write函數到底層驅動曆程
和前文提到的一樣,首先先註冊串口,使用uart_register_driver函數,依次分別為tty_register_driver,cdev_init函數,找到使用的file_operations,即應用程式與tty架構的統一介面。步驟不再贅述。
static const struct file_operations tty_fops = { .llseek = no_llseek, .read = tty_read, .write = tty_write, .poll = tty_poll, .unlocked_ioctl = tty_ioctl, .compat_ioctl = tty_compat_ioctl, .open = tty_open, .release = tty_release, .fasync = tty_fasync,};
tty_write函數
static ssize_t tty_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos){ struct inode *inode = file->f_path.dentry->d_inode; struct tty_struct *tty = file_tty(file); struct tty_ldisc *ld; ssize_t ret; ... ret = do_tty_write(ld->ops->write, tty, file, buf, count); ...}
這裡通過do_tty_write函數調用到了線路規程(ldisc)中的函數,結構名為tty_ldisc_N_TTY。
struct tty_ldisc_ops tty_ldisc_N_TTY = { .magic = TTY_LDISC_MAGIC, .name = "n_tty", .open = n_tty_open, .close = n_tty_close, .flush_buffer = n_tty_flush_buffer, .chars_in_buffer = n_tty_chars_in_buffer, .read = n_tty_read, .write = n_tty_write, .ioctl = n_tty_ioctl, .set_termios = n_tty_set_termios, .poll = n_tty_poll, .receive_buf = n_tty_receive_buf, .write_wakeup = n_tty_write_wakeup};
n_tty_write函數
static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *buf, size_t nr){ const unsigned char *b = buf; DECLARE_WAITQUEUE(wait, current); int c; ssize_t retval = 0;
...
c = tty->ops->write(tty, b, nr);
...
}
ops為struct tty_operations類型,由上文可知該結構名為
static const struct tty_operations uart_ops = { ... .write = uart_write, ...};
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;
...
uart_start(tty);
...
}
uart_start函數中又調用了__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);}
這裡的port就是uart_port類型的了,終於到達底層驅動了,好累。。又是這個數組,同樣的函數操作集
static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = { [0] = { .port = { .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock), .iotype = UPIO_MEM, .irq = IRQ_S3CUART_RX0, .uartclk = 0, .fifosize = 16, .ops = &s3c24xx_serial_ops, .flags = UPF_BOOT_AUTOCONF, .line = 0, } },
...
}
所以在底層驅動與之對應的就是s3c24xx_serial_start_tx這個函數。層層追溯下來,最終與應用程式中的write函數千裡相會。。。
static void s3c24xx_serial_start_tx(struct uart_port *port){ struct s3c24xx_uart_port *ourport = to_ourport(port); if (!tx_enabled(port)) { if (port->flags & UPF_CONS_FLOW) s3c24xx_serial_rx_disable(port); enable_irq(ourport->tx_irq); tx_enabled(port) = 1; }}
但是要讓各位看官失望了。這個函數很簡單,功能上來說就是如果沒有開啟發送使能就去開啟。那麼串口驅動又是在什麼地方去完成發送資料相關的操作呢?
預知後事如何,且聽下回分析。
如果有疑問或建議,歡迎指出。
linux串口驅動分析——發送資料