1、在Linux系统中,驱动程序通常采用内核模块的程序结构来进行编码。

      因此,编译/安装一个驱动程序,其实质就是编译/安装一个内核模块。

 
#include <linux/init.h> #include <linux/module.h>   static int hello_init(void) {     printk(KERN_WARNING"Hello, world !\n"); return 0; } static void hello_exit(void) {     printk(KERN_INFO "Goodbye, world\n"); } module_init(hello_init); module_exit(hello_exit);


2、设备驱动模型

    

什么是设备描述结构?

在任何一种驱动模型中,设备都会用内核中的一种结构来描述。

我们的字符设备在内核中使用struct    cdev 来描述。

 
struct cdev { struct kobject kobj; struct module *owner; const struct file_operations *ops;//设备操作集 struct list_head list; dev_t dev; //设备号 unsigned int count; //设备数 };
什么是设备号?

Linux内核中使用dev_t类型来定义设备号,dev_t这种类型其实质为32位的unsigned int,

其中高12位为主设备号,低20位为次设备号.

问1:如果知道主设备号,次设备号,怎么组合成dev_t类型

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

问2: 如何从dev_t中分解出主设备号?

答: 主设备号= MAJOR(dev_t dev)

问3: 如何从dev_t中分解出次设备号?

答: 次设备号=MINOR(dev_t dev)

字符设备文件与字符驱动程序通过主设备号建立起对应关系

驱动程序通过次设备号来区分不同的串口。


如何为设备分配一个主设备号?
1、静态申请
    开发者自己选择一个数字作为主设备号,然后通过函数register_chrdev_region向内核申请使用。

    缺点:如果申请使用的设备号已经被内核中的其他驱动使用了,则申请失败。

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

3、总结:

    静态申请:register_chrdev_region

    动态申请:alloc_chrdev_region

注销设备号!

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

3、字符设备驱动



一、驱动初始化



1、cdev变量的定义可以采用静态和动态两种办法

    静态分配:    struct cdev mdev;
    动态分配:    struct cdev *pdev = cdev_alloc();


2、struct cdev的初始化使用cdev_init函数来完成。

        cdev_init(struct cdev *cdev, const struct file_operations *fops) 

        参数:

        cdev: 待初始化的cdev结构

        fops: 设备对应的操作函数集



3、字符设备的注册使用cdev_add函数来完成。

    

    cdev_add(struct cdev     *p, dev_t     dev, unsigned count)

    参数:

    p: 待添加到内核的字符设备结构

    dev: 设备号

    count: 该类设备的设备个数

4、根据相应硬件的芯片手册,完成硬件初始化。


二、实现设备操作


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, };
在内核中的原形:

#include <linux/fs.h> //struct file_operations struct 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 *); ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); 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 *); int (*fsync) (struct file *, int datasync); int (*aio_fsync) (struct kiocb *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); int (*check_flags)(int); int (*flock) (struct file *, int, struct file_lock *); ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); int (*setlease)(struct file *, long, struct file_lock **); long (*fallocate)(struct file *file, int mode, loff_t offset, loff_t len); }; 

解释:


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

1、open

int  (*open) (struct  inode  *, struct  file  *)

/*打开设备,响应open系统

open设备方法是驱动程序用来为以后的操作完成初始化准备工作的。

在大部分驱动程序中,open完成如下工作: 标明次设备号和启动设备

在Linux系统中,每一个打开的文件,在内核中都会关联一个struct  file,

它由内核在打开文件时创建, 在文件关闭后释放。

重要成员:

loff_t     f_pos /*文件读写指针*/

struct     file_operations     *f_op /*该文件所对应的操作*/

每一个存在于文件系统里面的文件都会关联一个struct  inode 结构,该结构主要用来记录文件物理上的信息。

因此, 它和代表打开文件的file结构是不同的。

一个文件没有被打开时不会关联file结构,但是却会关联一个inode 结构。

重要成员:    dev_t     i_rdev    :设备号*/

2、release int (*release) (struct inode *, struct file *) 


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


3、llseek loff_t (*llseek) (struct file *, loff_t, int) 

//重定位读写指针,响应lseek系统调用


4、read 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结构指针, 由内核创建。buff : 从设备读取到的数据,需要保存到的位置。由read系统调用提供该参数。count: 请求传输的数据量,由read系统调用提供该参数。

offp: 文件的读写位置,由内核从file结构中取出后,传递进来

buff参数是来源于用户空间的指针,这类指针都不能被内核代码直接引用,必须使用专门的函数int copy_from_user(void *to, const void __user *from, int n)

int copy_to_user(void __user *to, const void *from, int n)*/


5、write ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *) 

/*向设备写入数据,响应write系统调用,

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

其参数类似于read */


三、驱动注销


 
void memdev_exit(void) { cdev_del(&mdev); unregister_chrdev_region(devno,2); }