标签:linux驱动
先上代码:
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/moduleparam.h> static char* whom = "World"; static int howmany = 1; module_param( howmany, int, S_IRUGO ); module_param( whom, charp, S_IRUGO ); static int hello_init(void) { int i; printk( KERN_ALERT "Hello, world! I'm Lizhixing.\n" ); for( i=0; i< howmany; ++i ) { printk( KERN_ALERT "Hello, %s !\n", whom ); } return 0; }; static void hello_exit(void) { printk( KERN_ALERT "Good bye, cruel world. Tommorow is another day! \n" ); }; module_init( hello_init ); module_exit( hello_exit );
insmod hello.ko howmany=10 whom="Daddy"
这里传入2个参数, 分别是 howmany 和 whom.
为了让module能接收这两个参数要做的事:
1. 包含头文件 moduleparam.h, 这个头文件中定义了参数需要的东西.
2. 声明参数. 支持的几种类型有:
bool/invbool, 都是boolean, invbool颠倒黑白,将true和false互换,好奇葩的设计,理解不能.....
charp, 就是字符串了.
int/long/short/uint/ulong/ushort, 这些都是熟悉的不能再熟的老面孔了.
还有就是能支持Array参数, 然后就这些了. 如果想用其它的类型,就需要用注册回调函数的方式自己去处理了.
3. 用module_param( name, type, permission ) 来注册这个参数,这样kernel就会用命令行里的参数将变量赋值.
载入module:
insmod hello.ko howmany=10 whom="Daddy"
dmesg, 有打印了:
33553.819340] Good bye, cruel world. Tommorow is another day! [383720.555138] perf samples too long (2516 > 2500), lowering kernel.perf_event_max_sample_rate to 50000 [565169.015155] Hello, world! I'm Lizhixing. [565169.015179] Hello, Daddy [565169.015187] Hello, Daddy [565169.015194] Hello, Daddy [565169.015200] Hello, Daddy [565169.015207] Hello, Daddy [565169.015213] Hello, Daddy [565169.015220] Hello, Daddy [565169.015226] Hello, Daddy [565169.015232] Hello, Daddy [565169.015239] Hello, Daddy [565181.213351] Good bye, cruel world. Tommorow is another day!
标签:linux驱动
原文地址:http://blog.csdn.net/thinkingl/article/details/38767203