11.2字符设备驱动编程
1.字符设备驱动编写流程
设备驱动程序可以使用模块的方式动态加载到内核中去。加载模块的方式与以往的应用程序开发有很大的不同。以往在开发应用程序时都有一个main()函数作为程序的入口点,而在驱动开发时却没有main()函数,模块在调用insmod命令时被加载,此时的入口点是init_module()函数,通常在该函数中完成设备的注册。同样,模块在调用rmmod命令时被卸载,此时的入口点是cleanup_module()函数,在该函数中完成设备的卸载。在设备完成注册加载之后,用户的应用程序就可以对该设备进行一定的操作,如open()、read()、write()等,而驱动程序就是用于实现这些操作,在用户应用程序调用相应入口函数时执行相关的操作,init_module()入口点函数则不需要完成其他如read()、write()之类功能。
上述函数之间的关系如图11.3所示。
图11.3设备驱动程序流程图
2.重要数据结构
用户应用程序调用设备的一些功能是在设备驱动程序中定义的,也就是设备驱动程序的入口点,它是一个在linux/fs.h>中定义的structfile_operations结构,这是一个内核结构,不会出现在用户空间的程序中,它定义了常见文件I/O函数的入口,如下所示:
structfile_operations
{
loff_t(*llseek)(structfile*,loff_t,int);
ssize_t(*read)(structfile*filp,
char*buff,size_tcount,loff_t*offp);
ssize_t(*write)(structfile*filp,
constchar*buff,size_tcount,loff_t*offp);
int(*readdir)(structfile*,void*,filldir_t);
unsignedint(*poll)(structfile*,structpoll_table_struct*);
int(*ioctl)(structinode*,
structfile*,unsignedint,unsignedlong);
int(*mmap)(structfile*,structvm_area_struct*);
int(*open)(structinode*,structfile*);
int(*flush)(structfile*);
int(*release)(structinode*,structfile*);
int(*fsync)(structfile*,structdentry*);
int(*fasync)(int,structfile*,int);
int(*check_media_change)(kdev_tdev);
int(*revalidate)(kdev_tdev);
int(*lock)(structfile*,int,structfile_lock*);
};
这里定义的很多函数是否跟第6章中的文件I/O系统调用类似?其实当时的系统调用函数通过内核,最终调用对应的structfile_operations结构的接口函数(例如,open()文件操作是通过调用对应文件的file_operations结构的open函数接口而被实现)。当然,每个设备的驱动程序不一定要实现其中所有的函数操作,若不需要定义实现时,则只需将其设为NULL即可。
structinode结构提供了关于设备文件/dev/driver(假设此设备名为driver)的信息,structfile结构提供关于被打开的文件信息,主要用于与文件系统对应的设备驱动程序使用。structfile结构较为重要,这里列出了它的定义:
structfile
{
mode_tf_mode;/*标识文件是否可读或可写,FMODE_READ或FMODE_WRITE*/
dev_tf_rdev;/*用于/dev/tty*/
off_tf_pos;/*当前文件位移*/
unsignedshortf_flags;/*文件标志,如O_RDONLY、O_NONBLOCK和O_SYNC*/
unsignedshortf_count;/*打开的文件数目*/
unsignedshortf_reada;
structinode*f_inode;/*指向inode的结构指针*/
structfile_operations*f_op;/*文件索引指针*/
};