【Linux设备驱动程序(第三版)】----模块参数
HelloWorldParam.c
#include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> MODULE_LICENSE("Dual BSD/GPL"); static char *whom = "world"; static int howmany = 1; module_param(whom, charp, S_IRUGO); module_param(howmany, int, S_IRUGO); static int __init hello_init(void) { printk(KERN_ALERT "Hello, World!\n"); int i; for( i = 0; i < howmany; i++) printk(KERN_ALERT "(%d) Hello, %s\n", i, whom); return 0; } static void __exit hello_exit(void) { printk(KERN_ALERT "Goodbye,world!\n"); } module_init(hello_init); module_exit(hello_exit);
Makefile
obj-m:= HelloWorldParam.o modules-objs:= HelloWorldParam.o KDIR:= /usr/src/linux-headers-2.6.31-14-generic/ PWD:= $(shell pwd) default: make -C $(KDIR) M=$(PWD) modules clean: rm -rf *.ko *.mod.c *.mod.o *.o *.markers *.symvers *.order
装载
insmod HelloWordParam howmany=10 whom="Tom"
查看Log(dmesg)
[89611.281829] Hello, World! [89611.281833] (0) Hello, Tom [89611.281834] (1) Hello, Tom [89611.281835] (2) Hello, Tom [89611.281836] (3) Hello, Tom [89611.281837] (4) Hello, Tom [89611.281838] (5) Hello, Tom [89611.281840] (6) Hello, Tom [89611.281841] (7) Hello, Tom [89611.281842] (8) Hello, Tom [89611.281843] (9) Hello, Tom