ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

应用是如何一步步调用到你编写的Linux底层驱动接口的

应用是如何一步步调用到你编写的Linux底层驱动接口的 目录1.概述2.调用过程3.调用分析4.相关结构体4.1 struct file4.2 struct inode4.3 struct cdev4.4 struct file_operations5.总结1.概述应用层调用字符设备的open,read,write,close时是如何调用到你所编写的linux驱动对应接口的2.调用过程应用依靠主次设备号和驱动进行关联(1)应用通过设备节点(比如/dev/ttyUSB0)使用设备驱动在创建设备节点时需要指明主次设备号设备节点也是以文件的形式保存主次设备号会保存在设备节点对应的 struct inode结构体中(2)向内核注册字符设备驱动时会将对应的struct cdev结构体注册到chrdevs全局变量中struct cdev结构体保存了主次设备号(3)open打开设备节点时先从struct inode结构体中获取主次设备号然后用这个主次设备号去chrdevs全局变量中找到对应的struct cdev结构体。3.调用分析4.相关结构体4.1 struct filestruct file { struct path f_path; const struct file_operations *f_op; /*操作函数指针*/ fmode_t f_mode; loff_t f_pos; ... };4.2 struct inodestruct inode { umode_t i_mode; uid_t i_uid; gid_t i_gid; dev_t i_rdev; /*主次设备号*/ struct timespec i_atime; struct timespec i_mtime; struct timespec i_ctime; ... };4.3 struct cdevstruct cdev { struct kobject kobj; struct module *owner; const struct file_operations *ops; /*操作函数接口*/ struct list_head list; dev_t dev; unsigned int count; };4.4 struct file_operationsstruct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *, fl_owner_t id); int (*release) (struct inode *, struct file *); ... };5.总结--应用空间open(/dev/ttyUSB0, ...) ----根据inode里的主次设备号去遍历内核cdev链表找到对应的cdev对象 ------取出cdev里面的ops --------通过ops找到注册驱动时传入的的file_oparetions ----------将file_oparetions的地址赋值给应用层打开的struct file里的f_op ------------此时应用已经找到驱动后续通过struct file里的f_op调用驱动里面的read、write等接口
返回列表