实现操作集合
实现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 };