標籤:des style blog http color 使用 os io
對於如何向模組傳遞參數,Linux kernel 提供了一個簡單的架構。其允許驅動程式聲明參數,並且使用者在系統啟動或模組裝載時為參數指定相應值,在驅動程式裡,參數的用法如同全域變數。
通過宏module_param()定義一個模組參數:
module_param(name,type,perm);
name既是使用者看到的參數名,又是模組內接受參數的變數。type表示參數的資料類型。
如:
byte, short, ushort,int, uint, long, ulong, charp, bool, invbool;
perm指定了在sysfs中相應檔案的存取權限。存取權限與linux檔案愛你存取權限相同的方式管理,如0644,或使用stat.h中的宏如S_IRUGO表示。0表示完全關閉在sysfs中相對應的項。
這些宏不會聲明變數,因此在使用宏之前,必須聲明變數,典型
地用法如下:
static unsigned int int_var = 0;
module_param(int_var, uint, S_IRUGO);
這些必須寫在模組源檔案的開頭部分。即int_var是全域的。也可以使模組源檔案內部的變數名與外部的參數名有不同的名字,通過
module_param_named()定義。
module_param_named(name, variable, type, perm);其中name是外部可見的參數名,variable是源檔案內部的全域變數名,而module_param通過module_param_named實現,只不過name與variable相同。
例如:
static unsigned int max_test = 9;
module_param_name(maximum_line_test, max_test, int, 0);
如果模組參數是一個字串時,通常使用char類型定義這個模組參數。核心複製使用者提供的字串到記憶體,並且相對應的變數指向這個字串。
例如:
static char *name;
module_param(name, charp, 0);
另一種方法是通過宏module_param_string()讓核心把字串直接複製到程式中的字元數組內。
module_param_string(name, string, len, perm);
這裡,name是外部的參數名,string是內部的變數名,len是以string命名的buffer大小(可以小於buffer的大小,但是沒有意義),perm表示sysfs的存取權限(或者perm是零,表示完全關閉相對應的sysfs項)。
例如:
static char species[BUF_LEN];
module_param_string(specifies, species, BUF_LEN, 0);
如果需要傳遞多個參數可以通過宏module_param_array()實現。
module_param_array(name, type, nump, perm);
其中,name既是外部模組的參數名又是程式內部的變數名,type是資料類型,perm是sysfs的存取權限。指標nump指向一個整數,其值表示有多少個參數存放在數組name中。值得注意是name數組必須靜態分配。
例如:
static int finsh[MAX_FISH];
static int nr_fish;
module_param_array(fish, int, &nr_fish, 0444); 最終傳
遞數組元素個數存在nr_fish中通過宏module_param_array_named()使得內部的數組名與外部的參數名有不同的名字。
例如:
module_param_array_named(name, array, type, nump, perm);
通過宏MODULE_PARM_DESC()對參數進行說明:
static unsigned short size = 1;
module_param(size, ushort, 0644);
MODULE_PARM_DESC(size, “The size in inches of the fishing pole connected to this computer.” );
實驗:
下面的小模組具體操作了如何傳遞參數的方法,麻雀雖小,五髒俱全。
在pc機上也可以實驗,並且驗證效果。
1 #include<linux/module.h> 2 #include<linux/moduleparam.h> 3 #include<linux/kernel.h> 4 5 6 #define MAX_ARRAY 6 7 8 static int_var= 0; 9 static char * str_var="default";10 static int_array[6];11 int narr;12 //模組傳遞變數和描述13 module_param(int_var,int, 0644);14 MODULE_PARM_DESC(int_var,"A integer variable");15 16 //模組傳遞變數和描述17 module_param(str_var, charp, 0644);18 MODULE_PARM_DESC(str_var,"A string variable");19 //模組傳遞數組合描述20 module_param_array(int_array,int,&narr, 0644);21 MODULE_PARM_DESC(int_array,"A integer array");22 //array的傳遞時多了一個參數int類型的地址23 24 25 static int __init hello_init(void)26 {27 int i;28 printk(KERN_ALERT"Hello, my LKM.\n");29 printk(KERN_ALERT"int_var %d.\n", int_var);30 printk(KERN_ALERT"str_var %s.\n", str_var);//這個數組參數不知道怎麼使用31 for(i= 0; i< narr; i++){32 printk("int_array[%d] = %d\n", i, int_array[i]);33 }34 return 0;35 }36 37 static void __exit hello_exit(void)38 {39 printk(KERN_ALERT"Bye, my LKM.\n");40 }41 module_init(hello_init);42 module_exit(hello_exit);43 MODULE_LICENSE("GPL");44 MODULE_AUTHOR("Edward");45 MODULE_DEION("This module is a example.");View Code
insmod hello.ko int_var=1 str_var="edward" int_array=1,2,3,4,5
如果要查看printk輸出的內容可以用命令dmesg