如果你对内核驱动模块一无所知,请先学习内核驱动模块的基础知识。
如果你已经入门了内核驱动模块,但是仍感觉有些模糊,不能从整体来了解一个内核驱动模块的结构,请赏读一下这篇拙文。 如果你已经从事内核模块编程N年,并且道行高深,也请不吝赐教一下文中的疏漏错误。
本文中我将实现一个简单的Linux字符设备,旨在大致勾勒出linux内核模块的编写方法的轮廓。其中重点介绍ioctl的用途。 我把这个简单的Linux字符设备模块命名为hello_mod. 设备类型名为hello_class 设备名为hello
该设备是一个虚拟设备,模块加载时会在/sys/class/中创建名为hello_class的逻辑设备,在/dev/中创建hello的物理设备文件。模块名为hello_mod,可接受输入字符串数据(长度小于128),处理该输入字符串之后可向外输出字符串。并且可以接受ioctl()函数控制内部处理字符串的方式。 例如:
a.通过write函数写入 “Tom”,通过ioctl函数设置langtype=chinese,通过read函数读出的数据将会是“你好!Tom/n”
b.通过write函数写入 “Tom”,通过ioctl函数设置langtype=english,通过read函数读出的数据将会是“hello!Tom/n”
c.通过write函数写入 “Tom”,通过ioctl函数设置langtype=pinyin,通过read函数读出的数据将会是“ni hao!Tom/n”
一般的内核模块中不会负责设备类别和节点的创建,我们在编译完之后会得到.o或者.ko文件,然后insmod之后需要mknod来创建相应文件,这个简单的例子中我们让驱动模块加载时负责自动创建设备类别和设备文件。这个功能有两个步骤, 1)创建设备类别文件 class_create(); 2)创建设备文件 device_create();
关于这两个函数的使用方法请参阅其他资料。
linux设备驱动的编写相对windows编程来说更容易理解一点因为不需要处理IRP,应用层函数和内核函数的关联方式浅显易懂。 比如当应曾函数对我的设备调用了open()函数,而最终这个应用层函数会调用我的设备中的自定义open()函数,这个函数要怎么写呢,
我在我的设备中定义的函数名是hello_mod_open,注意函数名是可以随意定义,但是函数签名是要符合内核要求的,具体的定义是怎么样请看
static int hello_mod_open(struct inode *, struct file *);
这样就定义了内核中的open函数,这只是定义还需要与我们自己的模块关联起来,这就要用到一个结构
struct file_operations
这个结构里面的成员是对应于设备操作的各种函数的指针。
我在设备中用到了这些函数所以就如下定义,注意下面的写法不是标准ANSI C的语法,而是GNU扩展语法。
struct file_operations hello_mod_fops = {
.owner = THIS_MODULE, .open = hello_mod_open,
.read = hello_mod_read, .write = hello_mod_write, .ioctl = hello_mod_ioctl, .release = hello_mod_release, };
这个结构体变量定义好之后我们在模块初始化函数中就可以通过register_chrdev()或者填充cdev结构来关联所有的操作到我们的模块函数了。
和设备交互的数据我们总称为“数据”,但是大致可划分为两种 “功能数据”:我们要输入设备处理的和设备处理完之后输出的数据。 “控制数据”:我们用来控制设备特性功能的命令和参数。
open,read,write,release等函数是对一个驱动模块的使用,就是我们对“设备的功能”的使用。但是一个设备有可能有很多功能,那么我们要怎么控制设备让设备完成指定的功能呢?
据个例子来说:假如我们有一个翻译机(姑且说机吧,也可能是器)实体设备,主要功能是输入中文,然后可以输出各种语言对应的翻译结果,那这个机的功能就是翻译,我们真正用来处理的数据是我们输入的中文,我们要得到的“设备功能”就是翻译后的输出内容,而各种语言则是我们的选择控制了,我们可设定这个设备翻译成何种语言。这就要求我们要向设备发送命令,设定目标语言。请注意我们要发送的是两个“控制数据”,命令和参数。
命令:一个设备可能有很多种行为,我们的命令就是代表我们要让设备执行何种行为。“复位”,“设定目标语言”,“获得当前目标语言”等 参数:对于某一个命令,可能需要参数可能不需要参数。 比如:
“复位”命令就不需要参数。
“设定目标语言”则需要传入目标语言的类型。
“获取目标语言”则需要传入一个能够接收目标语言类型的参数。
自己画了一个设备“数据流”图,希望能加深理解。
对于我们自己的设备我们就要自己定义设备命令了,如果你要想搞清命令的格式,请参考其他资料关于ioctl的参数的介绍。 这里不打算介绍这个,只介绍ioctl实际功能。定义自己的IO控制命令需要用到宏。这里直接写出我的定义而不是介绍宏的实现
#define HELLO_MAGIC 12
#define HELLO_IOCTL_RESETLANG _IO(HELLO_MAGIC,0) //设置复位,这个命令不带参数
#define HELLO_IOCTL_GETLANG _IOR(HELLO_MAGIC,1,int) //获取当前设备的语言类型参数,参数是int型 #define HELLO_IOCTL_SETLANG _IOW(HELLO_MAGIC,2,int) //设置设备的语言类型,参数是int型
多的不说了,下面贴上完整代码,懒人没写注释。。不好意思。 hello_mod.c
[cpp] view plaincopyprint?
1./*
2. * ===================================================================================== 3. *
4. * Filename: hello.c 5. *
6. * Description: hello_mod 7. *
8. * Version: 1.0
9. * Created: 01/28/2011 05:07:55 PM 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33.
* Revision: none * Compiler: gcc *
* Author: Tishion (shion), tishion@163.com * Company: LIM *
* ===================================================================================== */
#include
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. 77. 78.
#define MAJOR_NUM 250 #define MINOR_NUM 0 #define IN_BUF_LEN 256 #define OUT_BUF_LEN 512
MODULE_AUTHOR(\
MODULE_DESCRIPTION(\ion\
static struct class * hello_class; static struct cdev hello_cdev; static dev_t devnum = 0;
static char * modname = \ static char * devicename = \ static char * classname = \
static int open_count = 0; static struct semaphore sem;
static spinlock_t spin = SPIN_LOCK_UNLOCKED; static char * inbuffer = NULL; static char * outbuffer = NULL; static lang_t langtype;
static int hello_mod_open(struct inode *, struct file *); static int hello_mod_release(struct inode *, struct file *);
static ssize_t hello_mod_read(struct file *, char *, size_t, loff_t *);
static ssize_t hello_mod_write(struct file *, const char *, size_t, loff_t *);
static int hello_mod_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
struct file_operations hello_mod_fops = {
.owner = THIS_MODULE, .open = hello_mod_open, .read = hello_mod_read, .write = hello_mod_write, .ioctl = hello_mod_ioctl, .release = hello_mod_release, };
static int hello_mod_open(struct inode *inode, struct file *pfile) {
printk(\ spin_lock(&spin); if(open_count) {
spin_unlock(&spin);
相关推荐: