module param 在核心程式設計中的作用

2021-06-02 23:11:53 字數 2762 閱讀 3664

module_param

在使用者態下程式設計可以通過main

()的來傳遞命令列引數,而編寫乙個核心模組則通過module_param()!

module_param的作用

一.module_param

1.為什麼引入

在使用者態下程式設計可以通過main()來傳遞命令列引數,而編寫乙個核心模組則可通過module_param()來傳遞命令列引數

核心允許對驅動程式在載入的時候傳遞引數,在編寫核心程式的時候。要在**中用巨集module_param來指定接受的引數名,而這個巨集在中的定義如下

/* helper functions: type is byte, short, ushort, int, uint, long,

ulong, charp, bool or invbool, or *** if you define param_get_***,

param_set_*** and param_check_***. */

#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的實現是通過

module_param_named(name, name, type, perm)的。

其中使用了 3 個引數:要傳遞的引數變數名,變數的資料型別,以及訪問引數的許可權

測試模組,源程式hello.c內容如下:

#include

#include

#include

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編譯生成可執行檔案hello

插入:# insmod hellowho="world"times=5

出現5次"hello,world!":

#(1)hello,world!

#(2)hello,world!

#(3)hello,world!

# (4)hello,world!

#(5)hello,world!

解除安裝:# rmmodhello

出現:#goodbye,world!

驅動程式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

Linux核心module param的使用

1.定義模組引數的方法 module param name,type,perm 其中,name 表示引數的名字 type 表示引數的型別 perm 表示引數的訪問許可權 2.陣列型別模組引數的定義 用逗號間隔的列表提供的值 宣告乙個陣列引數 module param array name,type,...

Linux核心module param的使用

1.定義模組引數的方法 module param name,type,perm 其中,name 表示引數的名字 type 表示引數的型別 perm 表示引數的訪問許可權 2.陣列型別模組引數的定義 用逗號間隔的列表提供的值 宣告乙個陣列引數 module param array name,type,...

module param核心模組引數傳遞

在使用者態下程式設計可以通過main 的來傳遞命令列引數,而編寫乙個核心模組則通過module param 引數用 moudle param 巨集定義來宣告,它定義在 moduleparam.h.module param name,type,perm module param 使用了 3 個引數 變...