7.字符设备驱动编程模型

         字符设备驱动编程模型

  在Linux系统中,设备的类型非常繁多,如:字符设备,块设备,网络接口设备,USB设备,PCI设备,平台设备,混杂设备… ,而设备类型不同,也意味着其对应的驱动程序模型不同,这样就导致了我们需要对应的驱动程序模型不同,这样就导致了我们需要去掌握众多的驱动程序模型。那么能不能从这些众多的驱动模型中提炼出一些具有共性的规则,则是我们能不能学好Linux驱动的关键。

一、设备描述结构

  在任何一种驱动模型中,设备都会用内核中的一种结构来描述。我们的字符设备在内核中使用struct cdev来描述。

1 struct cdev {
2   struct kobject kobj;
3   struct module *owner;
4   const struct file_operations *ops; //设备操作集
5   struct list_head list;
6   dev_t dev; //设备号
7   unsigned int count; //设备数
8 };

1.1设备号

1.1.1主设备号

  字符设备文件与字符驱动程序通过主设备号建立关系的桥梁

1.1.2次设备号

  驱动程序通过次设备号来区分同一种设备(比如串口1和串口2)

1.1.3设备号操作

  Linux内核中使用dev_t类型来定义设备号,dev_t这种类型其实质为32位的unsigned int,其中高12位为主设备号,低20位为次设备号.

知道主设备号,次设备号,组合成dev_t类型:

  dev_t dev = MKDEV(主设备号,次设备号)

从dev_t中分解出主设备号:

  主设备号 = MAJOR(dev_t dev)

从dev_t中分解出次设备:

  次设备号=MINOR(dev_t dev)

1.1.4设备号分配

1.静态申请

  开发者自己选择一个数字作为主设备号,然后通过函数register_chrdev_region向内核申请使用。缺点:如果申请使用的设备号已经被内核中的其他驱动使用了,则申请失败。

2.动态申请

  使用alloc_chrdev_region由内核分配一个可用的主设备号。优点:因为内核知道哪些号已经被使用了,所以不会导致分配到已经被使用的号。

1.1.5设备号注销

  不论使用何种方法分配设备号,都应该在驱动退出时,使用unregister_chrdev_region函数释放这些设备号。

1.2操作函数集

结构体原型:

 1 struct file_operations {
 2     struct module *owner;
 3     loff_t (*llseek) (struct file *, loff_t, int);
 4     ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
 5     ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
 6     ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
 7     ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
 8     int (*readdir) (struct file *, void *, filldir_t);
 9     unsigned int (*poll) (struct file *, struct poll_table_struct *);
10     long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
11     long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
12     int (*mmap) (struct file *, struct vm_area_struct *);
13     int (*open) (struct inode *, struct file *);
14     int (*flush) (struct file *, fl_owner_t id);
15     int (*release) (struct inode *, struct file *);
16     int (*fsync) (struct file *, int datasync);
17     int (*aio_fsync) (struct kiocb *, int datasync);
18     int (*fasync) (int, struct file *, int);
19     int (*lock) (struct file *, int, struct file_lock *);
20     ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
21     unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
22     int (*check_flags)(int);
23     int (*flock) (struct file *, int, struct file_lock *);
24     ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
25     ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
26     int (*setlease)(struct file *, long, struct file_lock **);
27     long (*fallocate)(struct file *file, int mode, loff_t offset,
28               loff_t len);
29 };

  Struct file_operations是一个函数指针的集合,定义能在设备上进行的操作。结构中的函数指针指向驱动中的函数, 这些函数实现一个针对设备的操作, 对于不支持的操作则设置函数指针为 NULL。例如:

struct file_operations dev_fops ={
   .llseek = NULL,
   .read = dev_read,
  .write = dev_write,
  .ioctl = dev_ioctl,
  .open = dev_open,
  .release = dev_release,
};

二、字符设备驱动模型

2.1驱动初始化

2.1.1分配

cdev变量的定义可以采用静态和动态两种办法
• 静态分配
  struct cdev mdev;
• 动态分配
  struct cdev *pdev = cdev_alloc();

2.1.2初始化

struct cdev的初始化使用cdev_init函数来完成。
cdev_init(struct cdev *cdev, const struct file_operations *fops)
参数:
  cdev: 待初始化的cdev结构
  fops: 设备对应的操作函数集

2.1.3注册

字符设备的注册使用cdev_add函数来完成。
cdev_add(struct cdev *p, dev_t dev, unsigned count)
参数:
  p: 待添加到内核的字符设备结构
  dev: 设备号
  count: 该类设备的设备个数

2.1.4硬件初始化

 

2.2分析实现设备操作

2.2.1设备操作原型

1 int (*open) (struct inode *, struct file *)
2   打开设备,响应open系统

open设备方法是驱动程序用来为以后的操作完成初始化准备工作的。在大部分驱动程序中,open完成如下工作:

  标明次设备号
  启动设备

1 int (*release) (struct inode *, struct file *);
2   关闭设备,响应close系统调用

  release方法的作用正好与open相反。这个设备方法有时也称为close,它应该:关闭设备。

1 loff_t (*llseek) (struct file *, loff_t, int)
2   重定位读写指针,响应lseek系统调用
1 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *)                                                                                   从设备读取数据,响应read系统调用    

read设备方法通常完成2件事情:
  从设备中读取数据(属于硬件访问类操作)
  将读取到的数据返回给应用程序

ssize_t (*read) (struct file *filp, char __user *buff, size_t count, loff_t *offp)
参数分析:
  filp:与符设备文件关联的file结构指针, 由内核创建。
  count: 请求传输的数据量,由read系统调用提供该参数。
  offp: 文件的读写位置,由内核从file结构中取出后,传递进来。

  buff : 从设备读取到的数据,需要保存到的位置。由read系统调用提供该参数。

  buff参数是来源于用户空间的指针,这类指针都不能被内核代码直接引用,必须使用专门的函数

   int copy_from_user(void *to, const void __user *from, int n)    //写入内核
  int copy_to_user(void __user *to, const void*from, intn)  //写到用户空间

1 ssize_t(*write) (struct file *, const char __user *,size_t,loff_t *)
2   向设备写入数据,响应write系统调用 

write设备方法通常完成2件事情:
   从应用程序提供的地址中取出数据
  将数据写入设备(属于硬件访问类操作)
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *)
其参数类似于read

2.2.2struct file

  在Linux系统中,每一个打开的文件,在内核中都会关联一个struct file,它由内核在打开文件时创建, 在文件关闭后释放。

重要成员:
  loff_t f_pos /*文件读写指针*/
  struct file_operations *f_op /*该文件所对应的操作*/

2.2.3Struct inode

  每一个存在于文件系统里面的文件都会关联一个inode 结构,该结构主要用来记录文件物理上的信息。因此, 它和代表打开文件的file结构是不同的。一个文件没有被打开时不会关联file结构,但是却会关联一个inode 结构。

重要成员:
  dev_t i_rdev:设备号

2.3驱动注销

  当我们从内核中卸载驱动程序的时候,需要使用cdev_del函数来完成字符设备的注销。

 

posted @ 2016-03-31 18:26  for_learning  阅读(356)  评论(0编辑  收藏  举报