1,代码
1 /* 2 * a simple kernel module supporting parameters 3 * 4 * Copyright (C) 2014 Barry Song ([email protected]) 5 * 6 * Licensed under GPLv2 or later. 7 */ 8 9 #include <linux/init.h> 10 #include <linux/module.h> 11 12 static char *book_name = "dissecting Linux Device Driver"; 13 module_param(book_name, charp, S_IRUGO); 14 15 static int book_num = 4000; 16 module_param(book_num, int, S_IRUGO); 17 18 static int __init book_init(void) 19 { 20 printk(KERN_INFO "book name:%s\n", book_name); 21 printk(KERN_INFO "book num:%d\n", book_num); 22 return 0; 23 } 24 module_init(book_init); 25 26 static void __exit book_exit(void) 27 { 28 printk(KERN_INFO "book module exit\n "); 29 } 30 module_exit(book_exit); 31 32 MODULE_AUTHOR("Barry Song <[email protected]>"); 33 MODULE_LICENSE("GPL v2"); 34 MODULE_DESCRIPTION("A simple Module for testing module params"); 35 MODULE_VERSION("V1.0");
module_param 用来定义需要传递的参数,这里的参数都是有默认值的
2,makefile
1 KVERS = $(shell uname -r) 2 3 # Kernel modules 4 obj-m += book.o 5 6 # Specify flags for the module compilation. 7 #EXTRA_CFLAGS=-g -O0 8 9 build: kernel_modules 10 11 kernel_modules: 12 make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules 13 14 clean: 15 make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean
和hello驱动的makefile类似
3,安装驱动和查看参数
3.1 不带参数模式: insmod ./book.ko
查看参数如下所示:
baohua@baohua-VirtualBox:/sys/module/book/parameters$ cat book_name dissecting Linux Device Driver baohua@baohua-VirtualBox:/sys/module/book/parameters$ cat book_num 4000
3.2 带参数模式: insmod ./book.ko book_name='test123' book_num=500
参数如下所示:
baohua@baohua-VirtualBox:/sys/module/book/parameters$ cat book_name test123 baohua@baohua-VirtualBox:/sys/module/book/parameters$ cat book_num 500
5,卸载驱动
rmmod book
标签:num,name,modules,ko,module,baohua,book,paramter From: https://www.cnblogs.com/zhiminyu/p/17461768.html