设备驱动(四)
2014-02-18 09:19
218 查看
实现操作集合
实现open
统计计数,检查错误
申请资源
识别次设备号,如果必要更新f_op指针
实现realse
open逆操作
实现
实现read
copy_to_user
返回值为未被读取的字节数
实现write
copy_from_user
返回值为未被读取的字节数
实现unlocked_ioctl
capable(CAP_SYS_ADMIN)
copy_to_user,copy_from效率比较低,可以将驱动中的内存映射到用户空间提高访问效率,摄像头驱动使用该方式
命令字
创建命令字宏
__IO(type, num)
__IOR(type, num, count)
__IOW(type, num, count)
__IORW(type, num, count)
如果直接操作设备节点, 可以不执行open函数
cat /dev/hello
echo abc > /dev/hello
应用程序调用open,close流程
APP
fd = open("/dev/hello", O_RDONLY);
close(fd);
VFS
根据"/dev/hello"中包含的设备号,找cdev结构体
根据cdev创建inode, file
创建文件描述符fd
调用驱动中的open函数,如果操作集合或者open函数没有实现,清空已申请的资源,返回负的错误码
将fd与file关联
返回fd
释放申请的资源
执行驱动中的release函数
1583 struct file_operations {
1584 struct module *owner;
1585 loff_t (*llseek) (struct file *, loff_t, int);
1586 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1587 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1588 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1589 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1590 int (*readdir) (struct file *, void *, filldir_t);
1591 unsigned int (*poll) (struct file *, struct poll_table_struct *);
1592 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1593 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1594 int (*mmap) (struct file *, struct vm_area_struct *);
1595 int (*open) (struct inode *, struct file *);
1596 int (*flush) (struct file *, fl_owner_t id);
1597 int (*release) (struct inode *, struct file *);
1598 int (*fsync) (struct file *, loff_t, loff_t, int datasync);
1599 int (*aio_fsync) (struct kiocb *, int datasync);
1600 int (*fasync) (int, struct file *, int);
1601 int (*lock) (struct file *, int, struct file_lock *);
1602 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
1603 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1604 int (*check_flags)(int);
1605 int (*flock) (struct file *, int, struct file_lock *);
1606 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1607 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
1608 int (*setlease)(struct file *, long, struct file_lock **);
1609 long (*fallocate)(struct file *file, int mode, loff_t offset,
1610 loff_t len);
1611 };
实现open
统计计数,检查错误
申请资源
识别次设备号,如果必要更新f_op指针
实现realse
open逆操作
实现
实现read
copy_to_user
返回值为未被读取的字节数
实现write
copy_from_user
返回值为未被读取的字节数
实现unlocked_ioctl
capable(CAP_SYS_ADMIN)
copy_to_user,copy_from效率比较低,可以将驱动中的内存映射到用户空间提高访问效率,摄像头驱动使用该方式
命令字
设备类型 | 序列号 | 方向 | 数据尺寸 |
8bit | 8bit | 2bit(无,读,写,读写) | 8~14bit |
__IO(type, num)
__IOR(type, num, count)
__IOW(type, num, count)
__IORW(type, num, count)
如果直接操作设备节点, 可以不执行open函数
cat /dev/hello
echo abc > /dev/hello
应用程序调用open,close流程
APP
fd = open("/dev/hello", O_RDONLY);
close(fd);
VFS
根据"/dev/hello"中包含的设备号,找cdev结构体
根据cdev创建inode, file
创建文件描述符fd
调用驱动中的open函数,如果操作集合或者open函数没有实现,清空已申请的资源,返回负的错误码
将fd与file关联
返回fd
释放申请的资源
执行驱动中的release函数
1583 struct file_operations {
1584 struct module *owner;
1585 loff_t (*llseek) (struct file *, loff_t, int);
1586 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1587 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1588 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1589 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1590 int (*readdir) (struct file *, void *, filldir_t);
1591 unsigned int (*poll) (struct file *, struct poll_table_struct *);
1592 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1593 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1594 int (*mmap) (struct file *, struct vm_area_struct *);
1595 int (*open) (struct inode *, struct file *);
1596 int (*flush) (struct file *, fl_owner_t id);
1597 int (*release) (struct inode *, struct file *);
1598 int (*fsync) (struct file *, loff_t, loff_t, int datasync);
1599 int (*aio_fsync) (struct kiocb *, int datasync);
1600 int (*fasync) (int, struct file *, int);
1601 int (*lock) (struct file *, int, struct file_lock *);
1602 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
1603 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1604 int (*check_flags)(int);
1605 int (*flock) (struct file *, int, struct file_lock *);
1606 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1607 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
1608 int (*setlease)(struct file *, long, struct file_lock **);
1609 long (*fallocate)(struct file *file, int mode, loff_t offset,
1610 loff_t len);
1611 };
相关文章推荐
- Linux设备模型中三个很重要的概念: 总线,设备,驱动.即bus,device,driver
- PCIe驱动开发-设备打开/关闭
- Linux设备驱动3快速参考汇总--建立和运行模块
- Linux查看硬件信息以及驱动设备的命令
- 勾住有设备名字的驱动 拦截IRP(USB 光驱 无线上网卡等)
- /sys/bus一个总线设备驱动
- 高级字符设备驱动-Poll设备方法笔记
- Linux SPI总线和设备驱动架构之一:系统概述
- linux设备驱动之USB数据传输分析(续)
- Linux设备驱动程式学习(1)-字符设备驱动程式
- Linux高级字符设备驱动
- linux内核部件分析(八)——设备驱动模型之bus
- newstyle方式的i2c设备驱动
- Linux ALSA声卡驱动之四:Control设备的创建
- Linux 字符设备驱动开发基础(五)—— ioremap() 函数解析
- linuxDM900网卡设备驱动
- Linux下的硬件驱动——USB设备(上)(驱动配置部分)
- linux高级字符设备驱动之 四 poll方法(select多路监控原理与实现)
- Linux 设备驱动开发 —— platform设备驱动应用实例解析
- Linux SPI总线和设备驱动架构之四:SPI数据传输的队列化