资源预览内容
第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
第9页 / 共14页
第10页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
基于基于 MX51 的的 TTY 驱动架构驱动架构 2012 12 28 一 一 任务目标任务目标 以 uart 为实例分析整理 TTY 驱动架构 二 层次架构二 层次架构 tty core serial core uart驱动 用户空间 硬件 线路规程 图 1 在 linux 内核中 串行设备多种多样 内核开发者为了让驱动开发者尽量简 单 可复用的东西多 构建了如图 1 所示的串行设备层次架构 tty core 里是最基本的字符驱动 经过线路规程处理的数据 通过 tty core 提 供的数据交互机制与用户空间通信 图 1 中线路规程表示对上层或下层传过来数据的处理 不同的处理方式对应 不同的协议 如红外 ppp 拨号等等 serial core 是对 tty 驱动进行了封装 与硬件无关 给底层 uart 驱动开发提供 API 针对具体硬件的操作全在 uart 驱动中实现 因为 uart 同时涉及到以上三层 所以本文档以 uart 为例 阐述 TTY 三三 数据结构 数据结构设计设计 1 数据结构数据结构 uart驱动中定义了 2 个重要的数据结构 struct uart driver mxc reg 和 struct uart mxc port mxc port 8 a struct uart driver mxc reg uart driver 是 serial core 提供的重要结构体 下面是代码定义的 uart driver 实例 变量的解释见对应注释 具体结构体见数据结构关系图 static struct uart driver mxc reg driver name ttymxc 驱动名 用于与设备匹配 dev name ttymxc 设备名 用于与驱动匹配 并会在dev下显示 dev ttymxcID nr MXC UART NR 最大支持8个uart prot cons MXC CONSOLE serial core中定义的struct console 详见serial core分析 b struct uart mxr port mxc ports 8 mxc ports 数组是自定义的 uart mxc port 数组 数组大小为 8 在 mx51 中 只用到前 3 个 mxc ports 中包含的最重要的结构体是 struct uart port 它们之间的关系详 见数据结构关系图 static uart mxc port mxc ports MXC UART NR MXC UART NR 8 2 数据结构关系图数据结构关系图 uart driver const char driver name const char dev name struct console cons int nr state tty driver uart state port xmit uart port tty driver int magic const char driver name const char name struct cdev cdev struct tty struct ttys struct ktermios termios const struct tty operations ops int num struct list head tty drivers uart mxc port port struct clk clk uart port unsigned int irq unsigned int line unsigned int type unsigned int uartclk struct uart icount icount const struct uart ops ops struct device dev struct uart state state void private data tty port struct tty struct tty Const struct tty port operations ops unsigned char xmit buf tty struct int magic driver const struct tty operations ops int index struct tty ldisc ldisc struct tty bufhead buf port void driver data char write buf circ buf char buf int head int tail tty bufhead struct tty buffer head struct tty buffer tail struct tty buffer free int memory used tty buffer struct tty buffer next char char buf ptr unsigned char flag buf ptr int used int size int commit int read unsigned long data 0 tty ldisc Struct tty ldisc ops ops 四四 uart 注册及注册及初始化初始化 1 uart 注册注册 uart 驱动程序入口 module init mxcuart init static int init mxcuart init void uart register driver uart driver tty register driver tty driver cdev add 最终注册了nr nr 8 个字符设备驱动 ret uart register driver mxc reg定义见数据结构1 ret platform driver register mxcuart driver见下面定义 在mxcuart init 函数中 我们重点分析uart register driver struct uart driver 对于 platform driver register 详见平台设备注册文档分析 下面重点分析 uart register driver struct uart driver 在 uart register driver 中 构建了 tty driver 并将 uart driver 中的相关成员 赋值给 tty driver 最终调用到 tty register driver struct tty driver uart register driver struct uart driver drv drv state kzalloc sizeof struct uart state drv nr GFP KERNEL struct tty driver normal alloc tty driver drv nr 将 uart driver 中的成员变量赋值给 tty driver 例如 主次 设备号 驱动名 设备名 通信相关 init termios 设置等 tty set operations normal tty port init port 初始化上面申请的 drv nr 个 uart state 中的 tty port tty register driver normal 在 tty register driver struct tty driver 中 最终注册了 8 个字符设备驱动 其操作函数为 tty fops static const struct file operations tty fops read tty read write tty write open tty open 下面分析不同层次间几个 operations 函数的关系 如图 tty register driver struct tty driver driver dev MKDEV driver major driver minor start register chrdev region dev driver num driver name cdev init cdev add 注册了 8 个字符驱动 list add 将 tty drivers 加入一个全局的 tty drivers 链表 tty register device driver i NULL i 0 7 循环注册 8 个 device 在上面前 3 层中 每一层都给自己的下一层提供了操作函数的接口 tty core 是一个字符设备驱动 实现了字符设备提供的 struct file operations 同时给自 己的下层提供了 struct tty operations 操作接口 serial core 使用了 tty core 提供的 struct tty operations 操作接口 同时给自 己的下层提供了 struct uart ops 操作接口 在后面的打开 数据读写过程中我们将会看到具体的层层调用关系 到这里 uart 注册告一段落 2 uart 初始化初始化 uart 平台设备在 arch arm mach mx5 serial c 中 arch initcall mxc init uart 注 册 注册了 3 个平台设备 uart 平台驱动和 uart 平台设备通过名字 mxcintuart 匹配 匹配成功调用 uart 平台驱动中的 mxcuart probe 在mxcuart probe 中主要是将平台资源给mxc ports中的uart port 然后调用 uart add one port 它根据 uart port 中的 line 拿到 uart driver 中对应的 tty core serial core uart 驱动 struct file operations open read write struct tty operations open read write struct uart ops open read write 字符驱动 uart state 然后配置好这个 port 并调用 tty register device 注册该 port 匹配成功后将调用到 mxcuart probe static struct platform driver mxcuart driver driver name mxcintuart probe mxcuart probe remove mxcuart remove suspend mxcuart suspend resume mxcuart resume static struct platform device mxc uart device1 name mxcintuart id 0 num resources ARRAY SIZE mxc uart resources1 resource mxc uart resources1 dev platform data static struct resource mxc uart resources1 start UART1 BASE ADDR end UART1 BASE ADDR 0 x0B5 flags IORESOURCE MEM start MXC INT UART1 flags IORESOURCE IRQ static uart mxc port mxc ports 0 port iotype SERIAL IO MEM fifosize 32 flags ASYNC BOOT AUTOCONF line 0 mode MODE DCE ir mode NO IRDA 匹配成功匹配成功 uart driver const char driver name const char dev name
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号