设备驱动(四)

来源:互联网 发布:电脑软件怎么付费 编辑:程序博客网 时间:2024/05/22 10:41
实现操作集合
实现open
统计计数,检查错误
申请资源
识别次设备号,如果必要更新f_op指针
实现realse
open逆操作
实现
实现read
          copy_to_user
返回值为未被读取的字节数
实现write
          copy_from_user
          返回值为未被读取的字节数
实现unlocked_ioctl
          capable(CAP_SYS_ADMIN)
          
copy_to_user,copy_from效率比较低,可以将驱动中的内存映射到用户空间提高访问效率,摄像头驱动使用该方式

命令字     
设备类型序列号方向数据尺寸8bit8bit2bit(无,读,写,读写)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 
  1. fd = open("/dev/hello", O_RDONLY);
  2. close(fd);
VFS
  1. 根据"/dev/hello"中包含的设备号,找cdev结构体
  2. 根据cdev创建inode, file
  3. 创建文件描述符fd
  4. 调用驱动中的open函数,如果操作集合或者open函数没有实现,清空已申请的资源,返回负的错误码
  5. 将fd与file关联
  6. 返回fd
  7. 释放申请的资源
  8. 执行驱动中的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 };


0 0
原创粉丝点击