module_param 在内核编程中的作用
在用戶態(tài)下編程可以通過(guò)main()的來(lái)傳遞命令行參數(shù),而編寫(xiě)一個(gè)內(nèi)核模塊則通過(guò)module_param()!
module_param的作用 一.module_param1.為什么引入
?在用戶態(tài)下編程可以通過(guò)main()來(lái)傳遞命令行參數(shù),而編寫(xiě)一個(gè)內(nèi)核模塊則可通過(guò)module_param()來(lái)傳遞命令行參數(shù)
| 內(nèi)核允許對(duì)驅(qū)動(dòng)程序在加載的時(shí)候傳遞參數(shù),在編寫(xiě)內(nèi)核程序的時(shí)候。要在代碼中用宏module_param來(lái)指定接受的參數(shù)名,而這個(gè)宏在<linux/moduleparam.h>中的定義如下 /* Helper functions: type is byte, short, ushort, int, uint, long, ?? ulong, charp, bool or invbool, or XXX if you define param_get_XXX, ?? param_set_XXX and param_check_XXX. */ #define module_param_named(name, value, type, perm)??? ??? ??? ?? \ ??? param_check_##type(name, &(value));??? ??? ??? ??? ?? \ ??? module_param_call(name, param_set_##type, param_get_##type, &value, perm); \ ??? __MODULE_PARM_TYPE(name, #type) #define module_param(name, type, perm)??? ??? ??? ??? \ ??? module_param_named(name, name, type, perm) 由此可知 module_param的實(shí)現(xiàn)是通過(guò)module_param_named(name, name, type, perm)的。 其中使用了 3 個(gè)參數(shù):要傳遞的參數(shù)變量名,變量的數(shù)據(jù)類型,以及訪問(wèn)參數(shù)的權(quán)限。 測(cè)試模塊,源程序hello.c內(nèi)容如下: #include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h>???????? MODULE_LICENSE("Dual BSD/GPL");???? static char *who= "world";???????????? static int times = 1;??????? module_param(times,int,S_IRUSR);???? module_param(who,charp,S_IRUSR);?? static int hello_init(void)?????? { int i; for(i=0;i<times;i++) printk(KERN_ALERT "(%d) hello, %s!\n",i,who); return 0; } static void hello_exit(void) { printk(KERN_ALERT"Goodbye, %s!\n",who); } module_init(hello_init); module_exit(hello_exit); 編譯生成可執(zhí)行文件hello 插入: # insmod hellowho="world"times=5出現(xiàn)5次"hello,world!": #(1)hello,world! #(2)hello,world! #(3)hello,world! # (4)hello,world! #(5)hello,world! 卸載: # rmmodhello出現(xiàn): #Goodbye,world! |
?
驅(qū)動(dòng)程序module的工作流程主要分為四個(gè)部分:
1、 insmod module
2、 驅(qū)動(dòng)module的初始化(初始化結(jié)束后即進(jìn)入“潛伏”狀態(tài),直到有系統(tǒng)調(diào)用)
3、 當(dāng)操作設(shè)備時(shí),即有系統(tǒng)調(diào)用時(shí),調(diào)用驅(qū)動(dòng)module提供的各個(gè)服務(wù)函數(shù)
4、 rmmod module
一、 驅(qū)動(dòng)程序的加載
Linux驅(qū)動(dòng)程序分為兩種形式:一種是直接編譯進(jìn)內(nèi)核,另一種是編譯成module,然后在需要該驅(qū)動(dòng)module時(shí)手動(dòng)加載。
在用insmod加載module時(shí),還可以給提供模塊參數(shù),如:
static char *whom=”world”;
static int howmany=10;
module_param(howmany,int,S_IRUGO);
module_param(whom,charp,S_IRUGO);
這樣,當(dāng)使用insmod scull.ko whom=”string” howmany=20這樣的命令加載驅(qū)動(dòng)時(shí),whom和howmay的值就會(huì)傳入scull驅(qū)動(dòng)模塊了。
二、 驅(qū)動(dòng)module的初始化
scull_init_module函數(shù)中主要做了以下幾件事情:
a) 分配并注冊(cè)主設(shè)備號(hào)和次設(shè)備號(hào)
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) 初始化代表設(shè)備的struct結(jié)構(gòu)體:scull_dev
c) 初始化互斥體init_MUTEX
d) 初始化在內(nèi)核中代表設(shè)備的cdev結(jié)構(gòu)體,最主要是將該設(shè)備與file_operations結(jié)構(gòu)體聯(lián)系起來(lái)。在Linux內(nèi)核中,cdev結(jié)構(gòu)體才是真正代表了某個(gè)設(shè)備。在內(nèi)核調(diào)用設(shè)備的open,read等操作之前,必須先分配并注冊(cè)一個(gè)或者多個(gè)cdev結(jié)構(gòu)。
三、設(shè)備操作
涉及open ,close ioclt,release等函數(shù)
四、卸載
scull_cleanup_module
總結(jié)
以上是生活随笔為你收集整理的module_param 在内核编程中的作用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SVN 分支主干的合并
- 下一篇: 软件开发需求文档案例_第2部分:开发软件