《Linux设备驱动开发技术及应用》 加载模块时传参数 (4.5节 实

#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/moduleparam.h>static int onevalue = 1;static char *twostring = NULL;module_param(onevalue, int, 0);module_param(twostring, charp, 0);static int hello_init(void){printk("Hello world! [onevalue = %d : twostring = %s] \n", onevalue, twostring);return 0;}static void hello_exit(void){printk("Goodbye!\n");}module_init(hello_init);module_exit(hello_exit);MODULE_AUTHOR("li_xiangping");MODULE_DESCRIPTION("Module Parameter Test Module");MODULE_LICENSE("Dual BSD/GPL");

运行结果:

# insmod test.ko onevalue=0x27 twostring="twostring"# dmesg -c[ 3193.463256] Hello world! [onevalue = 39 : twostring = twostring]# rmmod test.ko# dmesg -c[ 3198.525263] Goodbye!

说明:

(1)[test.c] 4行应包含《linux/moduleparam.h文件》

(2)[test.c] 6~10行初始化模块时设定整数变量onevalue和字符串变量twostring。用module_param()实现外部引用定义符号。module_param(变量名,变量类型、使用属性) 扩展[变量类型]short: shortushort: unsigned shortint : intlong: longulong: unsigned longcharp: char*bool: intinvbool: intintarray: int*由接受字符串char *twostring定义的字符串地址变量不分配内存,因此要特别留意

(3)static定义设备驱动程序的变量或常数时使用static关键字。编译语法中在变量或常数上定义static时,不能从外部链接,只能在相同的源代码中调试或使用。Linux内核不能检索所有的变量状态,为使源代码的变量和函数不与其他源代码冲突,只能采取上述调用方式。不会复用的函数或变量可以不使用static关键字。此时static的意义受全局变量或函数的限制。因为函数内部变量上使用static,就会起到局部变量的效果。

世上再美的风景,都不及回家的那段路。

《Linux设备驱动开发技术及应用》 加载模块时传参数 (4.5节 实

相关文章:

你感兴趣的文章:

标签云: