Linux 字符设备驱动简单总结

看完宋宝华的《Linux设备驱动开发详解》及其有关博客,对字符设备驱动做一个小总结。

一、字符设备、字符设备驱动与用户空间访问该设备的程序三者之间的关系。

如图,在Linux内核中使用cdev结构体来描述字符设备,通过其成员dev_t来定义设备号(分为主、次设备号)以确定字符设备的唯一性。通过其成员file_operations来定义字符设备驱动提供给VFS的接口函数,如常见的open()、read()、write()等。

在Linux字符设备驱动中,模块加载函数通过register_chrdev_region( ) 或alloc_chrdev_region( )来静态或者动态获取设备号,通过cdev_init( )建立cdev与file_operations之间的连接,通过cdev_add( )向系统添加一个cdev以完成注册。模块卸载函数通过cdev_del( )来注销cdev,通过unregister_chrdev_region( )来释放设备号。

用户空间访问该设备的程序通过Linux系统调用,如open( )、read( )、write( ),来“调用”file_operations来定义字符设备驱动提供给VFS的接口函数。

二、字符设备驱动模型

(PS:神马情况!本地上传的图片,质量下降这么多)

1. 驱动初始化

1.1. 分配cdev

在2.6的内核中使用cdev结构体来描述字符设备,在驱动中分配cdev,主要是分配一个cdev结构体与申请设备号,以按键驱动为例:

01/*……*/ 02/*分配cdev*/ 03structcdevbtn_cdev; 04/*……*/ 05/*1.1申请设备号*/ 06if(major){ 07//静态 08dev_id=MKDEV(major,0); 09register_chrdev_region(dev_id,1,"button"); 10}else{ 11//动态 12alloc_chardev_region(&dev_id,0,1,"button"); 13major=MAJOR(dev_id); 14} 15/*……*/

从上面的代码可以看出,申请设备号有动静之分,其实设备号还有主次之分。

在Linux中以主设备号用来标识与设备文件相连的驱动程序。次编号被驱动程序用来辨别操作的是哪个设备。cdev 结构体的 dev_t 成员定义了设备号,为 32 位,其中高 12 位为主设备号,低20 位为次设备号。

设备号的获得与生成:

获得:主设备号:MAJOR(dev_t dev);

次设备号:MINOR(dev_t dev);

生成:MKDEV(int major,int minor);

设备号申请的动静之分:

静态:

1intregister_chrdev_region(dev_tfrom,unsignedcount,constchar*name); 2/*功能:申请使用从from开始的count个设备号(主设备号不变,次设备号增加)*/

静态申请相对较简单,但是一旦驱动被广泛使用,这个随机选定的主设备号可能会导致设备号冲突,而使驱动程序无法注册。

动态:

1intalloc_chrdev_region(dev_t*dev,unsignedbaseminor,unsignedcount,constchar*name); 2/*功能:请求内核动态分配count个设备号,且次设备号从baseminor开始。*/

动态申请简单,易于驱动推广,但是无法在安装驱动前创建设备文件(因为安装前还没有分配到主设备号)。

1.2. 初始化cdev

void cdev_init(struct cdev *, struct file_operations *);

cdev_init()函数用于初始化 cdev 的成员,并建立 cdev 和 file_operations 之间的连接。

1.3. 注册cdev

int cdev_add(struct cdev *, dev_t, unsigned);

cdev_add()函数向系统添加一个 cdev,完成字符设备的注册。

1.4. 硬件初始化

硬件初始化主要是硬件资源的申请与配置,以TQ210的按键驱动为例:

1/*1.4硬件初始化*/ 2//申请GPIO资源 3gpio_request(S5PV210_GPH0(0),"GPH0_0"); 4//配置输入 5gpio_direction_input(S5PV210_GPH0(0));

2.实现设备操作

用户空间的程序以访问文件的形式访问字符设备,通常进行open、read、write、close等系统调用。而这些系统调用的最终落实则是file_operations结构体中成员函数,它们是字符设备驱动与内核的接口。以TQ210的按键驱动为例:

1/*设备操作集合*/ 2staticstructfile_operationsbtn_fops={ 3.owner=THIS_MODULE, 4.open=button_open, 5.release=button_close, 6.read=button_read 7};

上面代码中的button_open、button_close、button_read是要在驱动中自己实现的。file_operations结构体成员函数有很多个,下面就选几个常见的来展示:

2.1. open()函数

原型:

1int(*open)(structinode*,structfile*); 2/*打开*/

案例:

01staticintbutton_open(structinode*inode,structfile*file){ 02unsignedlongflags; 03//获取分配好的私有数据结构的首地址 04structbutton_priv*pbtnp=container_of(inode->i_cdev, 05structbutton_priv, 06btn_cdev); 07//保存首地址到file->private_data 08file->private_data=pbtnp; 09if(down_interruptible(&pbtnp->sema)){ 10printk("ProccessisINT!\n"); 11return-EINTR; 12} 13printk("openbuttonsuccessfully!\n"); 14return0; 15}

2.2. read( )函数

原型:

1ssize_t(*read)(structfile*,char__user*,size_t,loff_t*); 2/*用来从设备中读取数据,成功时函数返回读取的字节数,出错时返回一个负值*/

案例:

01staticssize_tbutton_read(structfile*file,char__user*buf, 02size_tcount,loff_t*ppos){ 03//获取首地址 04structbutton_priv*pbtnp=file->private_data; 05//判断按键是否有操作,如果有,则读取键值并上报给用户;反之,则休眠 06wait_event_interruptible(pbtnp->btn_wq,is_press!=0); 07is_press=0; 08//上报键值 09copy_to_user(buf,&key_value,sizeof(key_value)); 10returncount; 11} 12/*参数:file是文件结构体指针,buf是用户空间内存的地址,该地址在内核空间不能直接读写, 13count是要读的字节数,ppos是读的位置相对于文件开头的偏移*/

2.3. write( )函数

原型:

1ssize_t(*write)(structfile*,constchar__user*,size_t,loff_t*); 2/*向设备发送数据,成功时该函数返回写入的字节数。如果此函数未被实现, 3当用户进行write()系统调用时,将得到-EINVAL返回值*/

案例:

01staticssize_tmem_write(structfile*filp,constchar__user*buf, 02size_tsize,loff_t*ppos){ 03unsignedlongp=*ppos; 04unsignedintcount=size; 05intret=0; 06int*register_addr=filp->private_data;/*获取设备的寄存器地址*/ 07/*分析和获取有效的写长度*/ 08if(p>=5*sizeof(int)) 09return0; 10if(count>5*sizeof(int)-p) 11count=5*sizeof(int)-p; 12/*从用户空间写入数据*/ 13if(copy_from_user(register_addr+p,buf,count)) 14ret=-EFAULT; 15else{ 16*ppos+=count; 17ret=count; 18} 19returnret; 20} 21/*参数:filp是文件结构体指针,buf是用户空间内存的地址,该地址在内核空间不能直接读写, 22count是要读的字节数,ppos是读的位置相对于文件开头的偏移*/

2.4. close( )函数

原型:

1int(*release)(structinode*,structfile*); 2/*关闭*/

案例:

1staticintbutton_close(structinode*inode,structfile*file){ 2/*1.获取首地址*/ 3structbutton_priv*pbtnp=file->private_data; 4up(&pbtnp->sema); 5return0; 6}

2.5. 补充说明

1.在Linux字符设备驱动程序设计中,有3种非常重要的数据结构:struct file、struct inode、struct file_operations。

struct file代表一个打开的文件。系统中每个打开的文件在内核空间都有一个关联的struct file。它由内核在打开文件时创建, 在文件关闭后释放。其成员loff_t f_pos 表示文件读写位置。

struct inode用来记录文件的物理上的信息。因此,它和代表打开文件的file结构是不同的。一个文件可以对应多个file结构,但只有一个inode结构。其成员dev_t i_rdev表示设备号。

struct file_operations一个函数指针的集合,定义能在设备上进行的操作。结构中的成员指向驱动中的函数,这些函数实现一个特别的操作, 对于不支持的操作保留为NULL。

2. 在read( )和write( )中的buff 参数是用户空间指针。因此,它不能被内核代码直接引用,因为用户空间指针在内核空间时可能根本是无效的——没有那个地址的映射。因此,内核提供了专门的函数用于访问用户空间的指针:

1unsignedlongcopy_from_user(void*to,constvoid__user*from,unsignedlongcount); 2unsignedlongcopy_to_user(void__user*to,constvoid*from,unsignedlongcount);

3. 驱动注销

3.1. 删除cdev

在字符设备驱动模块卸载函数中通过cdev_del()函数向系统删除一个cdev,完成字符设备的注销。

1/*原型:*/ 2voidcdev_del(structcdev*); 3/*例:*/ 4cdev_del(&btn_cdev);

3.2. 释放设备号

在调用cdev_del()函数从系统注销字符设备之后,unregister_chrdev_region()应该被调用以释放原先申请的设备号。

1/*原型:*/ 2voidunregister_chrdev_region(dev_tfrom,unsignedcount); 3/*例:*/ 4unregister_chrdev_region(MKDEV(major,0),1);

三、Linux字符设备驱动模板与案例

1.字符设备驱动模块加载与卸载函数模板

在实际开发中,通常习惯为设备定义一个设备相关的结构体,其包含该设备所涉及到的cdev、私有数据及信号量等信息。

01/*字符设备驱动模块加载与卸载函数模板*/ 02/*设备结构体 03structxxx_dev_t{ 04structcdevcdev; 05... 06}xxx_dev; 07/*设备驱动模块加载函数 08staticint__initxxx_init(void){ 09... 10cdev_init(&xxx_dev.cdev,&xxx_fops);/*初始化cdev*/ 11xxx_dev.cdev.owner=THIS_MODULE; 12/*获取字符设备号*/ 13if(xxx_major){ 14register_chrdev_region(xxx_dev_no,1,DEV_NAME); 15}else{ 16alloc_chrdev_region(&xxx_dev_no,0,1,DEV_NAME); 17} 18ret=cdev_add(&xxx_dev.cdev,xxx_dev_no,1);/*注册设备*/ 19... 20} 21 22/*设备驱动模块卸载函数*/ 23staticvoid__exitxxx_exit(void){ 24unregister_chrdev_region(xxx_dev_no,1);/*释放占用的设备号*/ 25cdev_del(&xxx_dev.cdev);/*注销设备*/ 26... 27}

2.字符设备驱动读、写、IO控制函数模板

01/*字符设备驱动读、写、IO控制函数模板*/ 02/*读设备*/ 03ssize_txxx_read(structfile*filp,char__user*buf, 04size_tcount,loff_t*f_pos){ 05... 06copy_to_user(buf,...,...); 07... 08} 09 10/*写设备*/ 11ssize_txxx_write(structfile*filp,constchar__user*buf, 12size_tcount,loff_t*f_pos){ 13... 14copy_from_user(...,buf,...); 15... 16} 17 18/*ioctl函数*/ 19intxxx_ioctl(structinode*inode,structfile*filp, 20unsignedintcmd,unsignedlongarg){ 21... 22switch(cmd){ 23caseXXX_CMD1: 24... 25break; 26caseXXX_CMD2: 27... 28break; 29default: 30/*不能支持的命令*/ 31return-ENOTTY; 32} 33return0; 34}

在设备驱动的读、写函数中,filp是文件结构体指针,buf是用户空间内存的地址,该地址在内核空间不能直接读写,count 是要读的字节数,f_pos是读的位置相对于文件开头的偏移。

3.TQ210的最简单按键驱动示例

001#include<linux/init.d> 002#include<linux/module.h> 003#include<linux/cdev> 004#include<linux/fs.h> 005#include<linux/types.h> 006#include<linux/uaccess.h> 007#include<linux/device.h> 008 009#include<plat/gpio-cfg.h> 010#include<asm/gpio.h> 011 012staticintmajor; 013/*分配cdev*/ 014structcdevbtn_cdev; 015 016/*记录按键值*/ 017staticunsignedcharkey_value; 018 019/*2.实现设备操作*/ 020/*2.1read*/ 021staticssize_tbutton_read(structfile*file,char__user*buf, 022size_tcount,loff_t*ppos){ 023intstatus=0; 024 025//1.获取GPIO的状态 026status=gpio_get_value(S5PV210_GPH0(0)); 027if(status==1) 028key_value=0x50; 029else 030key_value=0x51; 031 032//2.上报GPIO的状态 033copy_to_user(buf,&key_value,sizeof(key_value)); 034 035returncount; 036} 037 038/*2.2设备操作集合*/ 039staticstructfile_operationsbtn_fops={ 040.owner=THIS_MODULE, 041.read=button_read 042}; 043 044//设备类 045staticstructclass*btn_cls; 046 047/*1.驱动初始化*/ 048staticinitbutton_init(void){ 049dev_tdev_id; 050/*1.1申请设备号*/ 051if(major){ 052//静态 053dev_id=MKDEV(major,0); 054register_chrdev_region(dev_id,1,"button"); 055}else{ 056//动态 057alloc_chardev_region(&dev_id,0,1,"button"); 058major=MAJOR(dev_id); 059} 060 061/*1.2初始化cdev*/ 062cdev_init(&btn_cdev,&btn_fops); 063 064/*1.3注册cdev*/ 065cdev_add(&btn_cdev,dev_id,1); 066 067/*1.4自动创建设备节点*/ 068/*1.4.1创建设备类*/ 069//sys/class/button 070btn_cls=class_create(THIS_MODULE,"button"); 071/*1.4.2创建设备节点*/ 072device_create(btn_cls,NULL,dev_id,NULL,"button"); 073 074/*1.4硬件初始化*/ 075//申请GPIO资源 076gpio_request(S5PV210_GPH0(0),"GPH0_0"); 077//配置输入 078gpio_direction_input(S5PV210_GPH0(0)); 079 080return0; 081 082} 083 084 085/*3.驱动注销*/ 086staticvoidbutton_exit(void){ 087/*3.1释放GPIO资源*/ 088gpio_free(S5PV210_GPH0(0)); 089 090/*3.2删除设备节点*/ 091device_destroy(btn_cls,MKDEV(major,0)); 092class_destroy(btn_cls); 093 094/*3.3删除cdev*/ 095cdev_del(&btn_cdev); 096 097/*3.4释放设备号*/ 098unregister_chrdev_region(MKDEV(major,0),1); 099} 100 101module_init(button_init); 102module_exit(button_exit); 103MODULE_LICENSE("GPLv2");

我们摇摇头说,困难其实没什么大不了。

Linux 字符设备驱动简单总结

相关文章:

你感兴趣的文章:

标签云: