Linux字符设备驱动框架总结
字符设备驱动简介
Linux的三大设备驱动分别是字符设备、块设备、网络设备。其中,字符设备是按照字节流进行读写操作的设备,读写数据是分先后顺序的。
Linux应用程序对驱动程序的调用流程如下:
在Linux系统中,应用程序运行在应用空间,而驱动属于内核的一部分,运行于内核空间。如果用户空间想对内核空间进行操作,需要使用“系统调用”(实际上是利用中断实现,可参考Linux系统调用详解(实现机制分析)–linux内核剖析(六))的方式来实现从用户态“陷入”内核态,才能实现对底层驱动的操作。C库提供了open/close/write/read等函数。在Linux系统中,系统调用作为C库的一部分,调用open函数时的流程如下:
每一个系统调用,在驱动中都有与之对应的驱动函数。在Linux内核文件 include/linux/fs.h 中定义了 内核驱动操作函数集合的结构体 file_operations,如下:
struct file_operations {
struct module *owner; //拥有该结构体的模块的指针,一般设置为THIS_MODULE
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 (*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
int (*iterate) (struct file *, struct dir_context *);
unsigned int (*poll) (struct file *, struct poll_table_struct *); //轮询函数,用于查询设备是否可以进行非阻塞的读写(POLL机制)
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); //提供对于设备的控制功能,与应用程序ioctl函数对应,32位系统(读写设备参数、读设备状态、控制设备)
long (*compat_ioctl) (struct file *, unsigned int, unsigned long); //同unlocked_ioctl,64位系统
/*用户将设备的内存映射到用户空间中,方便用户直接操作,一般帧缓冲设备会使用此函数,如LCD驱动的显存*/
int (*mmap) (struct file *, struct vm_area_struct *);
int (*mremap)(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 *); //关闭设备文件,对应应用程序的close函数
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync); //同fasync函数,但是是异步处理待处理数据
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 **, void **);
long (*fallocate)(struct file *file, int mode, loff_t offset,loff_t len);
void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMU
unsigned (*mmap_capabilities)(struct file *);
#endif
};
字符设备驱动框架代码
下面先以虚拟字符设备hello_drv来展示字符设备的驱动框架,然后再进行分析。
/* *************************** hello_drv.c *************************** */
/*
虚拟字符设备:简单实现对设备文件的读写操作
*/
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
//主设备号
static int major = 0; /* 主设备号, 0 表示由系统分配 */
static char kernel_buf[1024]; /* 内核数据缓存区 */
static struct class *hello_class;
#define MN(a,b) ((a) < (b) ? (a) : (b))
/*
* @description : 从设备读取数据
* @param :
* file : 打开的设备文件(文件描述符)
* buf : 返回给用户空间的数据缓冲区
* size :要读取的数据长度
* offset:相对于文件首地址的偏移
* @return : 读取的字节数,如果为负值,表示读取失败
*/
static ssize_t hello_drv_read(struct file* file, char __user *buf, size_t size, loff_t *offset) {