1.概述
在Linux下,编写驱动程序实际上是实现对设备文件对应操作函数的编写,而这些操作函数是结构体file_operations中函数指针所指函数的具体实现。file_operations在Linux系统下的 /usr/include/linux/fs.h 中可以找到定义,该结构体的每个域都对应着驱动内核模块用来处理某个被请求的事件的函数的地址。(ps:这其实也就是函数指针的功能)
2.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 *, loff_t, loff_t, 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);
int (*show_fdinfo)(struct seq_file *m, struct file *f);
};
其中比较常用的函数有:
第1行:owner拥有该拥有该结构体的模块指针,一般设置为 THIS_MODULE,防止模块还在被使用的时候被卸载。
第2行:lseek函数用于修改文件当前的读写位置。
第3行:read函数用于读取设备文件。
第4行:write函数用于向设备文件写入(发送 )数据。
第13行:open函数用于打开设备文件。
第15行:release函数用于释放 函数用于释放 函数用于释放 (关闭 )设备文件,与应用程序中的close()函数对应。
3.实现
具体哪些函数还看具体的驱动要求,一般最常用的是open、release、write、read等函数,这些函数也是必须要实现的。拿最简单的字符设备驱动开发举一个例子,其file_operations结构体的例化如下:
static struct file_operations test_fops = {
.owner = THIS_MODULE,
.open = chrtest_open,
.read = chrtest_read,
.write = chrtest_write,
.release = chrtest_release,
}:
剩下的就是要根据具体是需求添加进去并实现了,具体的函数实现需要结合硬件信息以及相应的接口信息。