Introduction
我们今天编写第一个Linux Kernel Module的驱动程序:一个字符型设备驱动。通过简单的open(), release(), read(), write(),你将理解驱动程序的编程方法。
file_operations结构
在Linux中,一切都是文件,设备驱动也毫不例外。文件的定义在linux/fs.h中,目前我们只需要关注file_operations结构。
1 2 3 4 5 6 7 8 |
struct file_operations { ... ssize_t(*read) (struct file *, char __user *, size_t, loff_t *); ssize_t(*write) (struct file *, const char __user *, size_t, loff_t *); int (*open) (struct inode *, struct file *); int (*release) (struct inode *, struct file *); ... }; |
上面四个回调函数,是我们目前需要关心的,后面的例子也只会使用这四个回调函数。
编写我们自己的设备驱动程序,需要根据需求实现file_operations结构中的回调函数,然后将驱动通过register_chrdev()注册到内核中即可。
完整的字符驱动code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
int init_module(void); void cleanup_module(void); static ssize_t device_read(struct file *, char *, size_t, loff_t *); static ssize_t device_write(struct file *, const char *, size_t, loff_t *); static int device_open(struct inode *, struct file *); static int device_release(struct inode *, struct file *); // 我们的设备名字 static int major_version; static int device_is_open = 0; static char msg[1024]; static char *pmsg; // 定义设备允许的操作,这里只允许read(), write(), open(), release() static struct file_operations fops = { .read = device_read, .write = device_write, .open = device_open, .release = device_release }; // 模块注册函数 int init_module() { // 将我们定义的设备操作的结构体fops注册到内核中 major_version = register_chrdev(0, DEVICE_NAME, &fops); if (major_version < 0) { printk(KERN_ALERT "Register failed, error %d.\n", major_version); return major_version; } // 打印设备名称和版本号,稍后我们会利用这个来挂在模块到/dev/csprojectedu printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, major_version); return 0; } // 模块退出函数 void cleanup_module() { unregister_chrdev(major_version, DEVICE_NAME); } // 处理读模块的逻辑,可以通过cat /dev/csprojectedu 来简单的读取模块 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t *offset) { int bytes = 0; if (*pmsg == 0) { return 0; } while (length && *pmsg) { put_user(*(pmsg++), buffer++); length--; bytes++; } return bytes; } // 处理写模块的逻辑,我们目前不支持 static ssize_t device_write(struct file *filp, const char *buff, size_t length, loff_t *offset) { return -EINVAL; } // 处理打开模块的逻辑 static int device_open(struct inode *inode, struct file *file) { static int counter = 0; if (device_is_open) { return -EBUSY; } device_is_open = 1; sprintf(msg, "Device open for %d times.\n", ++counter); pmsg = msg; try_module_get(THIS_MODULE); return 0; } // 处理释放/关闭模块的逻辑 static int device_release(struct inode *inode, struct file *file) { device_is_open = 0; module_put(THIS_MODULE); return 0; } |
编译运行
Makefile同之前的一样就好
$ make
$ sudo insmod csprojectedu.ko
$ dmesg
[115465.083271] 'mknod /dev/csprojectedu c 250 0'.
$ sudo mknod /dev/csprojectedu c 250 0
$ cat /dev/csprojectedu
Device open for 1 times.
$ cat /dev/csprojectedu
Device open for 2 times.
$ sudo rm /dev/csprojectedu
$ sudo rmmod csprojectedu.ko
Summary
通过编写一个简单的字符型设备驱动,我们了解了Linux Kernel Module在驱动程序方面的应用。