3.2 Linux 驱动模块的编写之模块传参(须内核支持模块命令行传参)
1.vim Makefile
1 obj-m :=demo.o 2 3 KERNEL :=/linux-3.5 4 all: 5 make -C $(KERNEL) M=`pwd` 6 clean: 7 make -C $(KERNEL) M=`pwd` clean
vim demo.c
1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/moduleparam.h> 4 5 #define CNT 8 6 static int cnt = CNT; 7 8 static int buzzer_freq = 500;//整形变量 9 module_param(buzzer_freq,int,0);//仅能改变全局变量 10 11 static char*str = "heheda";//字符串 12 module_param(str,charp,0); 13 14 static unsigned short array[CNT] = {[3] = 6,[6] = 12};//数组 15 module_param_array(array,ushort,&cnt,0); 16 17 18 static int __init demo_init(void)//__init表示执行完内核会自动回收模块入口占用的空间和相关临时数据占用的空间 19 { 20 printk("<0>hello,kernel!\n"); 21 int i = 0; 22 printk("buzzer_freq:%dHz",buzzer_freq); 23 printk("str:%s\n",str); 24 for(i = 0;i<cnt;i++) { 25 printk("array[%d] = %d\n",i,array[i]); 26 } 27 28 return 0; 29 } 30 31 static void __exit demo_exit(void) 32 { 33 printk("<0>bye,kernel!\n"); 34 } 35 36 37 module_init(demo_init); 38 module_exit(demo_exit); 39 40 MODULE_LICENSE("GPL"); 41 MODULE_AUTHOR("crmn"); 42 MODULE_VERSION("crmn1.0"); 43 MODULE_DESCRIPTION("this is a demo!");



 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号