宏MODULE_PARAM(var,type,right) 用于向模块传递命令行参数。参数类型可以是整数、长整型、字符串等类型。
例1.2 带参数的内核模块实例
代码见光盘\src\1drivermodel\1-2module。本实例演示了如何向模块传递整型、长整型、字符串型等参数。核心代码如下所示:
- static int itype=0;
- module_param(itype, int, 0);
- static int btype = 0;
- module_param(btype, bool, 0);
- static unsigned char ctype=0;
- module_param(ctype, byte, 0);
- static char *stype=0;
- module_param(stype, charp, 0);
- //模块初始化
- static int __init demo_module_init(void)
- {
- printk("simple module init\n");
- printk("itype=%d\n",itype);
- printk("btype=%d\n",btype);
- printk("ctype=%d\n",ctype);
- printk("stype='%s'\n",stype);
- return 0;
- }
- //模块卸载
- static void __exit demo_module_exit(void)
- {
- printk("simple module exit\n");
- }
- module_init(demo_module_init);
- module_exit(demo_module_exit);
接下来编写一个makefile文件,同例1.1。执行make后生成smodule.ko,运行结果如下:
- [root@urbetter /home]# insmod smodule.ko itype=2 btype=1 ctype=0xAC stype='a'
- simple module init
- itype=2
- btype=1
- ctype=172
- stype='a'