首页 > 代码库 > 驱动程序module的工作流程

驱动程序module的工作流程

驱动程序module的工作流程主要分为四个部分:
1、 insmod module
2、 驱动module的初始化(初始化结束后即进入“潜伏”状态,直到有系统调用)
3、 当操作设备时,即有系统调用时,调用驱动module提供的各个服务函数
4、 rmmod module
 
一、 驱动程序的加载
 
Linux驱动程序分为两种形式:一种是直接编译进内核,另一种是编译成module,然后在需要该驱动module时手动加载。
 
在用insmod加载module时,还可以给提供模块参数,如:
static char *whom=”world”;
static int  howmany=10;
module_param(howmany,int,S_IRUGO);
module_param(whom,charp,S_IRUGO);
这样,当使用insmod scull.ko  whom=”string”  howmany=20这样的命令加载驱动时,whom和howmay的值就会传入scull驱动模块了。
 
二、 驱动module的初始化
scull_init_module函数中主要做了以下几件事情:
a) 分配并注册主设备号和次设备号
int register_chrdev_region(dev_t first, unsigned int count, char *name)
int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name)


b) 初始化代表设备的struct结构体:scull_dev


c) 初始化互斥体init_MUTEX
d) 初始化在内核中代表设备的cdev结构体,最主要是将该设备与file_operations结构体联系起来。在Linux内核中,cdev结构体才是真正代表了某个设备。在内核调用设备的open,read等操作之前,必须先分配并注册一个或者多个cdev结构。
 
三、设备操作
涉及open ,close ioclt,release等函数
 
四、卸载
scull_cleanup_module

驱动程序module的工作流程