共计 8590 个字符,预计需要花费 22 分钟才能阅读完成。
导读 | Linux 字符设备提供连续的数据流,应用程序可以顺序读取,通常不支持随机存取。相反,此类设备支持按字节 / 字符来读写设备。举例来说,键盘,串口,调制解调器都是典型的字符设备。 |
众所周知,字符设备是 Linux 下最基本,也是最常用到的设备,它是学习 linux 驱动入门最好的选择。计算机的很多东西都是相通的,掌握了其中一块,其它的就触类旁通了。在写驱动之前,必须先搞清楚字符设备驱动的框架大概是怎样的,一定要弄清楚流程,才开始动手,不要一开始就动手写代码。
linux 系统将设备分为 3 类:字符设备、块设备、网络设备。
- 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据。字符设备是面向流的设备,常见的字符设备有鼠标、键盘、串口、控制台和 LED 设备等。
- 块设备:是指可以从设备的任意位置读取一定长度数据的设备。块设备包括硬盘、磁盘、U 盘和 SD 卡等。
每一个字符设备或块设备都在 /dev 目录下对应一个设备文件。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 的接口函数。
1.1. 分配 cdev
在 2.6 的内核中使用 cdev 结构体来描述字符设备,在驱动中分配 cdev, 主要是分配一个 cdev 结构体与申请设备号,以按键驱动为例:
/* 分配 cdev*/ | |
struct cdev btn_cdev; | |
/*1.1 申请设备号 */ | |
if(major){ | |
// 静态 | |
dev_id = MKDEV(major, 0); | |
register_chrdev_region(dev_id, 1, "button"); | |
} | |
else { | |
// 动态 | |
alloc_chardev_region(&dev_id, 0, 1, "button"); | |
major = MAJOR(dev_id); | |
} |
从上面的代码可以看出,申请设备号有动静之分,其实设备号还有主次之分。在 Linux 中以主设备号用来标识与设备文件相连的驱动程序。次编号被驱动程序用来辨别操作的是哪个设备。cdev 结构体的 dev_t 成员定义了设备号,为 32 位,其中高 12 位为主设备号,低 20 位为次设备号。
设备号的获得与生成:
获得:
主设备号:MAJOR(dev_t dev); | |
次设备号:MINOR(dev_t dev); |
生成:MKDEV(int major,int minor)
;
设备号申请的动静之分:
静态:
int register_chrdev_region(dev_t from, unsigned count, const char *name);/* 功能:申请使用从 from 开始的 count 个设备号 (主设备号不变,次设备号增加)*/
静态申请相对较简单,但是一旦驱动被广泛使用, 这个随机选定的主设备号可能会导致设备号冲突,而使驱动程序无法注册。
动态:
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,const char *name);/* 功能:请求内核动态分配 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.4 硬件初始化 */ | |
// 申请 GPIO 资源 | |
gpio_request(S5PV210_GPH0(0), "GPH0_0"); | |
// 配置输入 | |
gpio_direction_input(S5PV210_GPH0(0)); |
2. 实现设备操作
用户空间的程序以访问文件的形式访问字符设备,通常进行 open、read、write、close 等系统调用。而这些系统调用的最终落实则是 file_operations 结构体中成员函数,它们是字符设备驱动与内核的接口。以 TQ210 的按键驱动为例:
/* 设备操作集合 */ | |
static struct file_operations btn_fops = { | |
.owner = THIS_MODULE, | |
.open = button_open, | |
.release = button_close, | |
.read = button_read | |
}; |
上面代码中的 button_open、button_close、button_read 是要在驱动中自己实现的。file_operations 结构体成员函数有很多个,下面就选几个常见的来展示:
2.1 open() 函数
原型:
int(*open)(struct inode *, struct file*); | |
/* 打开 */ |
2.2 read() 函数
原型:
ssize_t(*read)(struct file *, char __user*, size_t, loff_t*); | |
/* 用来从设备中读取数据,成功时函数返回读取的字节数,出错时返回一个负值 */ |
2.3 write() 函数
原型:
ssize_t(*write)(struct file *, const char__user *, size_t, loff_t*); | |
/* 向设备发送数据,成功时该函数返回写入的字节数。如果此函数未被实现,当用户进行 write() 系统调用时,将得到 -EINVAL 返回值 */ |
2.4 close() 函数
原型:
int(*release)(struct inode *, struct file*); | |
/* 关闭 */ |
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 参数是用户空间指针。因此, 它不能被内核代码直接引用,因为用户空间指针在内核空间时可能根本是无效的——没有那个地址的映射。因此,内核提供了专门的函数用于访问用户空间的指针:
unsigned long copy_from_user(void *to, const void __user *from, unsigned long count); | |
unsigned long copy_to_user(void __user *to, const void *from, unsigned long count); |
3.1 删除 cdev
在字符设备驱动模块卸载函数中通过 cdev_del() 函数向系统删除一个 cdev,完成字符设备的注销。
/* 原型:*/ | |
void cdev_del(struct cdev *); | |
/* 例:*/ | |
cdev_del(&btn_cdev); |
3.2 释放设备号
在调用 cdev_del() 函数从系统注销字符设备之后,unregister_chrdev_region() 应该被调用以释放原先申请的设备号。
/* 原型:*/ | |
void unregister_chrdev_region(dev_t from, unsigned count); | |
/* 例:*/ | |
unregister_chrdev_region(MKDEV(major, 0), 1); |
4. 字符设备驱动程序基础
4.1 结构体
在 Linux2.6 内核中,使用 cdev 结构体来描述一个字符设备,cdev 结构体的定义如下:
struct cdev { | |
struct kobject kobj; | |
struct module *owner; /* 通常为 THIS_MODULE*/ | |
struct file_operations *ops; /* 在 cdev_init() 这个函数里面与 cdev 结构联系起来 */ | |
struct list_head list; | |
dev_t dev; /* 设备号 */ | |
unsigned int count; | |
}; |
MAJOR(dev_t dev)cdev 结构体的 dev_t 成员定义了设备号,为 32 位,其中 12 位是主设备号,20 位是次设备号,我们只需使用二个简单的宏就可以从 dev_t 中获取主设备号和次设备号:
MINOR(dev_t dev)
相反地,可以通过主次设备号来生成 dev_t:
MKDEV(int major,int minor)
4.2 Linux2.6 内核提供一组函数用于操作 cdev 结构体
(1)void cdev_init(struct cdev*,struct file_operations *); | |
(2)struct cdev *cdev_alloc(void); | |
(3)int cdev_add(struct cdev *,dev_t,unsigned); | |
(4)void cdev_del(struct cdev *); |
其中(1)用于初始化 cdev 结构体,并建立 cdev 与 file_operations 之间的连接。(2)用于动态分配一个 cdev 结构,(3)向内核注册一个 cdev 结构,(4)向内核注销一个 cdev 结构。
4.3 Linux2.6 内核分配和释放设备号
在调用 cdev_add() 函数向系统注册字符设备之前,首先应向系统申请设备号,有二种方法申请设备号,一种是静态申请设备号:
int register_chrdev_region(dev_t from,unsigned count,const char *name)
另一种是动态申请设备号:
int alloc_chrdev_region(dev_t *dev,unsigned baseminor,unsigned count,const char *name);
其中,静态申请是已知起始设备号的情况,如先使用 cat /proc/devices 命令查得哪个设备号未事先使用(不推荐使用静态申请);动态申请是由系统自动分配,只需设置 major = 0 即可。
相反地,在调用 cdev_del() 函数从系统中注销字符设备之后,应该向系统申请释放原先申请的设备号,使用:
void unregister_chrdev_region(dev_t from,unsigned count);
4.4 cdev 结构的 file_operations 结构体
这个结构体是字符设备当中最重要的结构体之一,file_operations 结构体中的成员函数指针是字符设备驱动程序设计的主体内容,这些函数实际上在应用程序进行 Linux 的 open()、read()、write()、close()、seek()、ioctl() 等系统调用时最终被调用。
struct file_operations { | |
/* 拥有该结构的模块计数,一般为 THIS_MODULE*/ | |
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 *); | |
/* 执行设备的 I / O 命令 */ | |
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); | |
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 *, struct dentry *, 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 **); |
4.5 file 结构
file 结构代表一个打开的文件,它的特点是一个文件可以对应多个 file 结构。它由内核再 open 时创建,并传递给在该文件上操作的所有函数,直到最后 close 函数,在文件的所有实例都被关闭之后,内核才释放这个数据结构。
在内核源代码中,指向 struct file 的指针通常比称为 filp,file 结构有以下几个重要的成员:
内核用 inode 结构在内部表示文件,它是实实在在的表示物理硬件上的某一个文件,且一个文件仅有一个 inode 与之对应,同样它有二个比较重要的成员:
struct file{ | |
mode_t fmode; /* 文件模式,如 FMODE_READ,FMODE_WRITE*/ | |
... | |
loff_t f_pos; /*loff_t 是一个 64 位的数,需要时,须强制转换为 32 位 */ | |
unsigned int f_flags; /* 文件标志,如:O_NONBLOCK*/ | |
struct file_operations *f_op; | |
void *private_data; /* 非常重要,用于存放转换后的设备描述结构指针 */ | |
... | |
}; |
4.6 inode 结构
struct inode{ | |
dev_t i_rdev; /* 设备编号 */ | |
struct cdev *i_cdev; /*cdev 是表示字符设备的内核的内部结构 */ | |
}; |
可以从 inode 中获取主次设备号,使用下面二个宏:
/* 驱动工程师一般不关心这二个宏 */ | |
unsigned int imajor(struct inode *inode); | |
unsigned int iminor(struct inode *inode); |
4.7 字符设备驱动模块加载与卸载函数
在字符设备驱动模块加载函数中应该实现设备号的申请和 cdev 结构的注册,而在卸载函数中应该实现设备号的释放与 cdev 结构的注销。
我们一般习惯将 cdev 内嵌到另外一个设备相关的结构体里面,该设备包含所涉及的 cdev、私有数据及信号量等等信息。常见的设备结构体、模块加载函数、模块卸载函数形式如下:
/* 设备结构体 */ | |
struct xxx_dev{ | |
struct cdev cdev; | |
char *data; | |
struct semaphore sem; | |
... | |
}; |
/* 模块加载函数 */ | |
static int __init xxx_init(void) | |
{ | |
... | |
// 初始化 cdev 结构; | |
// 申请设备号;// 注册设备号;// 申请分配设备结构体的内存;} |
/* 模块卸载函数 */ | |
static void __exit xxx_exit(void) | |
{ | |
... | |
// 释放原先申请的设备号;// 释放原先申请的内存;// 注销 cdev 设备;} |
4.8 字符设备驱动的 file_operations 结构体成员函数
/* 读设备 */ | |
ssize_t xxx_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) | |
{ | |
/* 使用 filp->private_data 获取设备结构体指针;*/ | |
/* 分析和获取有效的长度;*/ | |
/* 内核空间到用户空间的数据传递 */ | |
copy_to_user(void __user *to, const void *from, unsigned long count); | |
} |
/* 写设备 */ | |
ssize_t xxx_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) | |
{ | |
/* 使用 filp->private_data 获取设备结构体指针;*/ | |
/* 分析和获取有效的长度;*/ | |
/* 用户空间到内核空间的数据传递 */ | |
copy_from_user(void *to, const void __user *from, unsigned long count); | |
} |
/*ioctl 函数 */ | |
static int xxx_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg) | |
{switch(cmd){ | |
case xxx_CMD1: | |
break; | |
case xxx_CMD2: | |
break; | |
default: | |
return -ENOTTY; /* 不能支持的命令 */ | |
} | |
return 0; | |
} |
4.9 字符设备驱动文件操作结构体模板
struct file_operations xxx_fops = { | |
.owner = THIS_MODULE, | |
.open = xxx_open, | |
.read = xxx_read, | |
.write = xxx_write, | |
.close = xxx_release, | |
.ioctl = xxx_ioctl, | |
.lseek = xxx_llseek, | |
}; |
