本例一共三种类型模块参数——无符号整型,字符串,字符串数组
#include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/kernel.h> #define ACCESS (0644) static uint age = 24; module_param(age, uint, ACCESS); static char *name = "netboy"; module_param(name, charp, ACCESS); static char *hobby[10] = {"basketbal", "running"}; static int hobby_num = 2; module_param_array(hobby, charp, &hobby_num, ACCESS); static int __init param_init(void){ int i = 0; printk("my name is %s, my age is %u\n", name, age); for (i = 0; i < hobby_num; i++){ printk("%d %s\n", i, hobby[i]); } return 0; } static void __exit param_exit(void){ printk("bye"); } module_init(param_init); module_exit(param_exit); MODULE_AUTHOR("ngnetboy"); MODULE_DESCRIPTION("kernel parms test!"); MODULE_LICENSE("GPL"); MODULE_VERSION("0.0.0.2");
注:
1:在使用 char * 类型的时候,一定要用 charp
2:使用参数数组的时候 module_param_array(hobby, charp, &hobby_num, ACCESS); hobby_num 会被设置为用户提供的值的个数。
3:加载驱动命令: insmod my_project.ko name="netboy" age=25 hobby="shopping","playing","sleeping"
相对应的Makefile
ifeq ($(KERNELRELEASE), ) KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) all: clean $(MAKE) -C $(KERNELDIR) M=$(PWD) modules -rm -rf *.o *~ core .depend .*.cmd *.mod.c .tmp_versions Module.* Makefile.xen modules.order clean: -rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module.* Makefile.xen modules.order else obj-m := my_project.o endif
时间: 2024-10-21 08:15:25