本文参考了大量牛人的博客,对大神的分享表示由衷的感谢。
主要参考:
tty驱动分析 :http://www.wowotech.net/linux_kenrel/183.html
Linux TTY驱动--Uart_driver底层:http://blog.csdn.net/sharecode/article/details/9196591
Linux TTY驱动--Serial Core层 :http://blog.csdn.net/sharecode/article/details/9197567
前面学习过了 i2c、spi,这俩都是基于设备总线驱动模型,分析起来相对比较简单,今天打算迎难而上学习一下 Uart 驱动程序,因为它涉及到 tty 、线路规程,确实有些难度,幸好有万能的互联网让我可以学习大神们的博客。一天下来总算有些收获,下面总结一下(主要是框架)。
整个 uart 框架大概的样子如上图所示,简单来分的话可以说成两层,一层是下层我们的串口驱动层,它直接与硬件相接触,我们需要填充一个 struct uart_ops 的结构体,另一层是上层 tty 层,包括 tty 核心以及线路规程,它们各自都有一个 Ops 结构,用户空通过间是 tty 注册的字符设备节点来访问,这么说来如上图所示涉及到了4个 ops 结构了,层层跳转。下面,就来分析分析它们的层次结构。
在 s3c2440 平台,它是这样来注册串口驱动的,分配一个struct uart_driver 简单填充,并调用uart_register_driver 注册到内核中去。
- static struct uart_driver s3c24xx_uart_drv = {
- .owner = THIS_MODULE,
- .dev_name = "s3c2410_serial",
- .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
- .cons = S3C24XX_SERIAL_CONSOLE,
- .driver_name = S3C24XX_SERIAL_NAME,
- .major = S3C24XX_SERIAL_MAJOR,
- .minor = S3C24XX_SERIAL_MINOR,
- };
- static int __init s3c24xx_serial_modinit(void)
- {
- int ret;
-
- ret = uart_register_driver(&s3c24xx_uart_drv);
- if (ret < 0) {
- printk(KERN_ERR "failed to register UART driver\n");
- return -1;
- }
-
- return 0;
- }
uart_driver 中,我们只是填充了一些名字、设备号等信息,这些都是不涉及底层硬件访问的,那是怎么回事呢?来看一下完整的 uart_driver 结构或许就明白了。
- struct uart_driver {
- struct module *owner;
- const char *driver_name;
- const char *dev_name;
- int major;
- int minor;
- int nr;
- struct console *cons;
-
-
- struct uart_state *state; <span style="white-space:pre"> </span>
- struct tty_driver *tty_driver;
- };
在我们上边填充的结构体中,有两个成员未被赋值,对于tty_driver 代表的是上层,它会在 register_uart_driver 中的过程中赋值,而uart_state 则代表下层,uart_state 也会在register_uart_driver 的过程中分配空间,但是它里面真正设置硬件相关的东西是 uart_state->uart_port ,这个uart_port 是需要我们从其它地方调用 uart_add_one_port 来添加的。
1、下层(串口驱动层)
首先,我们需要认识这几个结构体
- struct uart_state {
- struct tty_port port;
-
- int pm_state;
- struct circ_buf xmit;
-
- struct tasklet_struct tlet;
- struct uart_port *uart_port;
- };
在注册 driver 时,会根据 uart_driver->nr 来申请 nr 个 uart_state 空间,用来存放驱动所支持的串口(端口)的物理信息。
- struct uart_port {
- spinlock_t lock;
- unsigned long iobase;
- unsigned char __iomem *membase;
- unsigned int (*serial_in)(struct uart_port *, int);
- void (*serial_out)(struct uart_port *, int, int);
- unsigned int irq;
- unsigned long irqflags;
- unsigned int uartclk;
- unsigned int fifosize;
- unsigned char x_char;
- unsigned char regshift;
- unsigned char iotype;
- unsigned char unused1;
-
- unsigned int read_status_mask;
- unsigned int ignore_status_mask;
- struct uart_state *state;
- struct uart_icount icount;
-
- struct console *cons;
- #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(SUPPORT_SYSRQ)
- unsigned long sysrq;
- #endif
-
- upf_t flags;
-
- unsigned int mctrl;
- unsigned int timeout;
- unsigned int type;
- const struct uart_ops *ops;
- unsigned int custom_divisor;
- unsigned int line;
- resource_size_t mapbase;
- struct device *dev;
- unsigned char hub6;
- unsigned char suspended;
- unsigned char unused[2];
- void *private_data;
- };
这个结构体,是需要我们自己来填充的,比如我们 s3c2440 有3个串口,那么就需要填充3个 uart_port ,并且通过 uart_add_one_port 添加到 uart_driver->uart_state->uart_port 中去。当然 uart_driver 有多个 uart_state ,每个 uart_state 有一个 uart_port 。在 uart_port 里还有一个非常重要的成员 struct uart_ops *ops ,这个也是需要我们自己来实现的,一般芯片厂家都写好了吧?或者只需要稍作修改。
- struct uart_ops {
- unsigned int (*tx_empty)(struct uart_port *);
- void (*set_mctrl)(struct uart_port *, unsigned int mctrl);
- unsigned int (*get_mctrl)(struct uart_port *);
- void (*stop_tx)(struct uart_port *);
- void (*start_tx)(struct uart_port *);
- void (*send_xchar)(struct uart_port *, char ch);
- void (*stop_rx)(struct uart_port *);
- void (*enable_ms)(struct uart_port *);
- void (*break_ctl)(struct uart_port *, int ctl);
- int (*startup)(struct uart_port *);
- void (*shutdown)(struct uart_port *);
- void (*flush_buffer)(struct uart_port *);
- void (*set_termios)(struct uart_port *, struct ktermios *new,
- struct ktermios *old);
- void (*set_ldisc)(struct uart_port *);
- void (*pm)(struct uart_port *, unsigned int state,
- unsigned int oldstate);
- int (*set_wake)(struct uart_port *, unsigned int state);
-
-
-
-
- const char *(*type)(struct uart_port *);
-
-
-
-
-
- void (*release_port)(struct uart_port *);
-
-
-
-
-
- int (*request_port)(struct uart_port *);
- void (*config_port)(struct uart_port *, int);
- int (*verify_port)(struct uart_port *, struct serial_struct *);
- int (*ioctl)(struct uart_port *, unsigned int, unsigned long);
- #ifdef CONFIG_CONSOLE_POLL
- void (*poll_put_char)(struct uart_port *, unsigned char);
- int (*poll_get_char)(struct uart_port *);
- #endif
- };
实在是太复杂了。。。但这一层就跟裸机程序一样,用来操作硬件寄存器,只不过内核把“格式”给我们规定死了。
2、上层(tty 核心层)
tty 层要从 register_uart_driver 来看起了,因为 tty_driver 是在注册过程中构建的,我们也就顺便了解了注册过程~。
- int uart_register_driver(struct uart_driver *drv)
- {
- struct tty_driver *normal = NULL;
- int i, retval;
-
-
-
- drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
-
-
- normal = alloc_tty_driver(drv->nr);
- 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);
-
-
-
-
- for (i = 0; i < drv->nr; i++) {
- struct uart_state *state = drv->state + i;
- struct tty_port *port = &state->port;
-
- tty_port_init(port);
- port->close_delay = 500;
- port->closing_wait = 30000;
-
- tasklet_init(&state->tlet, uart_tasklet_action,
- (unsigned long)state);
- }
-
-
- retval = tty_register_driver(normal);
-
- }
注册过程干了哪些事:
1、根据driver支持的最大设备数,申请n个 uart_state 空间,每一个 uart_state 都有一个 uart_port 。
2、分配一个 tty_driver ,并将drv->tty_driver 指向它。
3、对 tty_driver 进行设置,其中包括默认波特率、校验方式等,还有一个重要的 Ops ,uart_ops ,它是tty核心与我们串口驱动通信的接口。
4、初始化每一个 uart_state 的 tasklet 。
5、注册 tty_driver 。
注册 uart_driver 实际上是注册 tty_driver,因此与用户空间打交道的工作完全交给了 tty_driver ,而且这一部分都是内核实现好的,我们不需要修改,了解一下工作原理即可。
- 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,
- .set_ldisc = uart_set_ldisc,
- .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
- .proc_fops = &uart_proc_fops,
- #endif
- .tiocmget = uart_tiocmget,
- .tiocmset = uart_tiocmset,
- #ifdef CONFIG_CONSOLE_POLL
- .poll_init = uart_poll_init,
- .poll_get_char = uart_poll_get_char,
- .poll_put_char = uart_poll_put_char,
- #endif
- };
这个是 tty 核心的 Ops ,简单一看,后面分析调用关系时,我们在来看具体的里边的函数,下面来看 tty_driver 的注册。
- int tty_register_driver(struct tty_driver *driver)
- {
- int error;
- int i;
- dev_t dev;
- void **p = NULL;
-
- if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
- p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
- }
-
-
- if (!driver->major) {
- error = alloc_chrdev_region(&dev, driver->minor_start,
- driver->num, driver->name);
- } else {
- dev = MKDEV(driver->major, driver->minor_start);
- error = register_chrdev_region(dev, driver->num, driver->name);
- }
-
- if (p) {
- driver->ttys = (struct tty_struct **)p;
- driver->termios = (struct ktermios **)(p + driver->num);
- } else {
- driver->ttys = NULL;
- driver->termios = NULL;
- }
-
-
- cdev_init(&driver->cdev, &tty_fops);
- driver->cdev.owner = driver->owner;
- error = cdev_add(&driver->cdev, dev, driver->num);
-
- mutex_lock(&tty_mutex);
-
-
- list_add(&driver->tty_drivers, &tty_drivers);
- mutex_unlock(&tty_mutex);
-
- if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
- for (i = 0; i < driver->num; i++)
- tty_register_device(driver, i, NULL);
- }
-
-
- proc_tty_register_driver(driver);
- driver->flags |= TTY_DRIVER_INSTALLED;
- return 0;
- }
tty_driver 注册过程干了哪些事:
1、为线路规程和termios分配空间,并使 tty_driver 相应的成员指向它们。
2、注册字符设备,名字是 uart_driver->name 我们这里是“ttySAC”,文件操作函数集是 tty_fops。
3、将该 uart_driver->tty_drivers 添加到全局链表 tty_drivers 。
4、向 proc 文件系统添加 driver ,这个暂时不了解。
至此,文章起初的结构图中的4个ops已经出现了3个,另一个关于线路规程的在哪?继续看吧。
3、调用关系分析
tty_driver 不是注册了一个字符设备么,那我们就以它的 tty_fops 入手,以 open、read、write 为例,看看用户空间是如何访问到最底层的硬件操作函数的。
3.1 tty_open
- static int tty_open(struct inode *inode, struct file *filp)
- {
- int ret;
-
- lock_kernel();
- ret = __tty_open(inode, filp);
- unlock_kernel();
- return ret;
- }
为了方便分析,我把看不懂的代码都删掉了- -!!!
- static int __tty_open(struct inode *inode, struct file *filp)
- {
- struct tty_struct *tty = NULL;
- int noctty, retval;
- struct tty_driver *driver;
- int index;
- dev_t device = inode->i_rdev;
- unsigned saved_flags = filp->f_flags;
- ...
-
- driver = get_tty_driver(device, &index);
-
- tty = tty_init_dev(driver, index, 0);
-
- filp->private_data = tty;
-
- if (tty->ops->open)
-
- retval = tty->ops->open(tty, filp);
-
- return 0;
- }
从 tty_drivers 全局链表获取到前边我们注册进去的 tty_driver ,然后分配设置一个 struct tty_struct 的东西,最后调用 tty_struct->ops->open 函数,其实 tty_struct->ops == tty_driver->ops 。
- struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx, int first_ok)
- {
- struct tty_struct *tty;
- int retval;
-
- tty = alloc_tty_struct();
-
-
- initialize_tty_struct(tty, driver, idx);
-
-
- retval = tty_ldisc_setup(tty, tty->link);
-
- return tty;
- }
- void initialize_tty_struct(struct tty_struct *tty,
- struct tty_driver *driver, int idx)
- {
- memset(tty, 0, sizeof(struct tty_struct));
-
-
- tty_ldisc_init(tty);
-
- ...
- tty_buffer_init(tty);
- tty->driver = driver;
-
-
- init_waitqueue_head(&tty->write_wait);
- init_waitqueue_head(&tty->read_wait);
-
-
- tty->ops = driver->ops;
- tty->index = idx;
- }
- void tty_buffer_init(struct tty_struct *tty)
- {
- spin_lock_init(&tty->buf.lock);
- tty->buf.head = NULL;
- tty->buf.tail = NULL;
- tty->buf.free = NULL;
- tty->buf.memory_used = 0;
-
-
- INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
- }
整个 tty_open 的工作:
1、获取到 tty_driver
2、根据 tty_driver 初始化一个 tty_struct
2.1 设置 tty_struct 的线路规程为 N_TTY (不同类型的线路规程有不同的 ops)
2.2 初始化一个延时工作队列,唤醒时调用flush_to_ldisc ,读函数时我们需要分析它。
2.3 初始化 tty_struct 里的两个等待队列头。
2.4 设置 tty_struct->ops == tty_driver->ops 。
3、在 tty_ldisc_setup 函数中调用到线路规程的open函数,对于 N_TTY 来说是 n_tty_open 。
4、如果 tty_struct->ops 也就是 tty_driver->ops 定义了 open 函数则调用,显然是有的 uart_open 。
对于 n_tty_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;
- struct tty_port *port;
- int retval, line = tty->index;
-
- state = uart_get(drv, line);
- port = &state->port;
- tty->driver_data = state;
- state->uart_port->state = state;
-
-
- retval = uart_startup(state, 0);
-
- }
根据 tty_struct 获取到 uart_driver ,再由 uart_driver 获取到里面 uart_state->uart_port->ops->startup 调用它。至此,open函数分析完毕,它不是简单的 “打开”,还有大量的初始化工作,最终调用到最底层的 startup 函数。
3.2 tty_write
- static ssize_t tty_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
- {
- struct tty_struct *tty;
- struct inode *inode = file->f_path.dentry->d_inode;
- ssize_t ret;
- struct tty_ldisc *ld;
-
- tty = (struct tty_struct *)file->private_data;
-
- ld = tty_ldisc_ref_wait(tty);
- if (!ld->ops->write)
- ret = -EIO;
- else
-
- ret = do_tty_write(ld->ops->write, tty, file, buf, count);
- tty_ldisc_deref(ld);
- return ret;
- }
- 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;
-
- add_wait_queue(&tty->write_wait, &wait);
- while (1) {
-
- set_current_state(TASK_INTERRUPTIBLE);
- if (signal_pending(current)) {
- retval = -ERESTARTSYS;
- break;
- }
- if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
- retval = -EIO;
- break;
- }
-
- if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
- ....
- } else {
- while (nr > 0) {
-
- c = tty->ops->write(tty, b, nr);
- if (c < 0) {
- retval = c;
- goto break_out;
- }
- if (!c)
- break;
- b += c;
- nr -= c;
- }
- }
- if (!nr)
- break;
- if (file->f_flags & O_NONBLOCK) {
- retval = -EAGAIN;
- break;
- }
-
- schedule();
- }
- }
n_tty_write 调用 tty->ops->write 也就是 uart_write .
- static int uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
- {
- uart_start(tty);
- return ret;
- }
-
- static void uart_start(struct tty_struct *tty)
- {
- __uart_start(tty);
- }
-
- 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);
- }
uart_write 又调用到了最底层的 uart_port->ops->start_tx 函数。
猜测一下,大概“写”的思路:
1、将当前进程加入到等待队列
2、设置当前进程为可打断的
3、层层调用最终调用到底层的 start_tx 函数,将要发送的数据存入 DATA 寄存器,由硬件自动发送。
4、进程调度,当前进程进入休眠。
5、硬件发送完成,进入中断处理函数,唤醒对面队列。
当然这只是我自己意淫的,到底是不是这样,具体分析底层操作函数的时候应该会明白。
3.2 tty_read
- static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
- loff_t *ppos)
- {
- int i;
- struct tty_struct *tty;
- struct inode *inode;
- struct tty_ldisc *ld;
-
- tty = (struct tty_struct *)file->private_data;
- inode = file->f_path.dentry->d_inode;
-
-
-
- ld = tty_ldisc_ref_wait(tty);
-
- if (ld->ops->read)
- i = (ld->ops->read)(tty, file, buf, count);
- else
- i = -EIO;
- tty_ldisc_deref(ld);
- if (i > 0)
- inode->i_atime = current_fs_time(inode->i_sb);
- return i;
- }
调用线路规程的 read 函数,对于 N_TTY 来说是 n_tty_read ,删掉了一堆看不懂的代码,还是有很多
- static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
- unsigned char __user *buf, size_t nr)
- {
- unsigned char __user *b = buf;
- DECLARE_WAITQUEUE(wait, current);
- int c;
- int minimum, time;
- ssize_t retval = 0;
- ssize_t size;
- long timeout;
- unsigned long flags;
- int packet;
-
- do_it_again:
-
- BUG_ON(!tty->read_buf);
-
- c = job_control(tty, file);
-
- minimum = time = 0;
- timeout = MAX_SCHEDULE_TIMEOUT;
-
- if (!tty->icanon) {
- ...
- }
-
- packet = tty->packet;
-
- add_wait_queue(&tty->read_wait, &wait);
- while (nr) {
-
- if (packet && tty->link->ctrl_status) {
-
- }
-
-
-
- set_current_state(TASK_INTERRUPTIBLE);
-
- if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
- ((minimum - (b - buf)) >= 1))
- tty->minimum_to_wake = (minimum - (b - buf));
-
- if (!input_available_p(tty, 0)) {
-
-
-
- n_tty_set_room(tty);
-
- timeout = schedule_timeout(timeout);
- continue;
- }
- __set_current_state(TASK_RUNNING);
-
-
- if (packet && b == buf) {
-
- }
-
-
- if (tty->icanon) {
-
- while (nr && tty->read_cnt) {
- int eol;
-
- eol = test_and_clear_bit(tty->read_tail,
- tty->read_flags);
-
-
- c = tty->read_buf[tty->read_tail];
- spin_lock_irqsave(&tty->read_lock, flags);
- tty->read_tail = ((tty->read_tail+1) &
- (N_TTY_BUF_SIZE-1));
- tty->read_cnt--;
- if (eol) {
-
-
-
-
- if (--tty->canon_data < 0)
- tty->canon_data = 0;
- }
- spin_unlock_irqrestore(&tty->read_lock, flags);
-
- if (!eol || (c != __DISABLED_CHAR)) {
-
- if (tty_put_user(tty, c, b++)) {
- retval = -EFAULT;
- b--;
- break;
- }
- nr--;
- }
- if (eol) {
- tty_audit_push(tty);
- break;
- }
- }
- if (retval)
- break;
- } else {
-
- }
- ....
- }
- mutex_unlock(&tty->atomic_read_lock);
- remove_wait_queue(&tty->read_wait, &wait);
-
- if (!waitqueue_active(&tty->read_wait))
- tty->minimum_to_wake = minimum;
-
- __set_current_state(TASK_RUNNING);
- ...
- n_tty_set_room(tty);
- return retval;
- }
“读”过程干了哪些事:
1、将当前进程加入等待队列
2、设置当前进程可中断
3、进程调度,当前进程进入休眠
4、在某处被唤醒
5、从 tty->read_buf 取出数据,通过 tty_put_user 拷贝到用户空间。
那么,在何处唤醒,猜测应该是在中断处理函数中,当DATA寄存器满,触发中断,中断处理函数中调用 tty_flip_buffer_push 。
- void tty_flip_buffer_push(struct tty_struct *tty)
- {
- unsigned long flags;
- spin_lock_irqsave(&tty->buf.lock, flags);
- if (tty->buf.tail != NULL)
- tty->buf.tail->commit = tty->buf.tail->used;
- spin_unlock_irqrestore(&tty->buf.lock, flags);
-
- if (tty->low_latency)
- flush_to_ldisc(&tty->buf.work.work);
- else
- schedule_delayed_work(&tty->buf.work, 1);
- }
tty_flip_buffer_push 有两种方式调用到 flush_to_ldisc ,一种直接调用,另一种使用延时工作队列,在很久很久以前,我们初始化了这么一个工作队列~(tty_open 初始化 tty_struct 时前面有提到)。
在 flush_to_ldisc 会调用到 disc->ops->receive_buf ,对于 N_TTY 来说是 n_tty_receive_buf ,在 n_tty_receive_buf 中,将数据拷贝到 tty->read_buf ,然后 wake_up_interruptible(&tty->read_wait) 唤醒休眠队列。然后就是前面提到的,在n_tty_read 函数中 从 tty->read_buf 里取出数据 拷贝到用户空间了。
至此,关于 uart 的框架分析基本就结束了~对于 tty 以及线路规程是什么东西,大概了解是个什么东西。虽然大部分东西都不需要我们自己实现,但是了解它们有益无害~
下一篇文章,以 s3c2440 为例,分析底层的操作函数,以及 s3c2440 是如何初始化 uart_port 结构的~,这些是在移植驱动过程中需要做的工作~