模块参数 --- parameters

为模块定义一个参数:
module_param(参数名,参数类型,参数读/写权限)   //可定义各种类型参数,该参数可由加载模块时,外部传入。
以下示例定义了一个整型参数和一个字符指针参数:

static char *bookname = "dissecting linux device driver";
static int num = 4000;
module_param(num, int, S_IRUGO);
module_param(book_name, charp, S_IRUGO);


装载内核模块时,用户可以向模块传递参数,如下:
insmod (或modprobe) 模块名 参数名=参数值

模块被加载后,在/sys/module/目录下,会出现以此模块名命名的目录。

当参数读/写权限为0时,表示此参数不存在sysfs文件系统下对应的文件节点。

当参数读/写权限不为0时,此模块目录下,将出现parameters目录,包含一系列以参数名命名的文件节点,这些文件的权限值,就是参数读/写权限

参数读写权限宏定义:

#define S_IRWXUGO	(S_IRWXU|S_IRWXG|S_IRWXO)
#define S_IALLUGO	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
#define S_IRUGO		(S_IRUSR|S_IRGRP|S_IROTH)
#define S_IWUGO		(S_IWUSR|S_IWGRP|S_IWOTH)
#define S_IXUGO		(S_IXUSR|S_IXGRP|S_IXOTH)
#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100

#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010

#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001

模块也可以拥有参数数组:
module_param_array(数组名,数组类型,数组长度,参数读/写权限)

示例如下:
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver$ ls
book.c  Makefile
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver$ vim book.c

1 /*======================================================================
  2 A kernel module: book
  3 This example is to introduce module params
  4 
  5 The initial developer of the original code is Baohua Song
  6 <author@linuxdriver.cn>. All Rights Reserved.
  7 ======================================================================*/
  8 #include <linux/init.h>
  9 #include <linux/module.h>
 10 MODULE_LICENSE("Dual BSD/GPL");
 11 
 12 static char *book_name = "dissecting Linux Device Driver";
 13 static int num = 4000;
 14 
 15 static int book_init(void)
 16 {
 17 printk(KERN_INFO " book name:%s\n",book_name);
 18 printk(KERN_INFO " book num:%d\n",num);
 19 return 0;
 20 }
 21 static void book_exit(void)
 22 {
 23 printk(KERN_INFO " Book module exit\n ");
 24 }
 25 module_init(book_init);
 26 module_exit(book_exit);
 27 module_param(num, int, S_IRUGO);
 28 module_param(book_name, charp, S_IRUGO);
 29 
 30 MODULE_AUTHOR("Song Baohua, author@linuxdriver.cn");
 31 MODULE_DESCRIPTION("A simple Module for testing module params");
 32 MODULE_VERSION("V1.0");


xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver$ vim Makefile
1 obj-m := book.o
  2 
  3 clean: 
  4 rm -rf *.ko *.mod.c *.o *.order *.symvers

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver$ make -C /usr/src/linux-headers-2.6.35-22-generic/ M=$(pwd) modules
make:进入目录'/usr/src/linux-headers-2.6.35-22-generic'
  CC [M]  /home/xxha/share/learning/songbaohua/4_char_device_driver/book.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/xxha/share/learning/songbaohua/4_char_device_driver/book.mod.o
  LD [M]  /home/xxha/share/learning/songbaohua/4_char_device_driver/book.ko
make:离开目录“/usr/src/linux-headers-2.6.35-22-generic”

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver$ ls
book.c  book.ko  book.mod.c  book.mod.o  book.o  Makefile  modules.order  Module.symvers

这样就编译成功了 book.ko
接着加载模块:
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver$ sudo insmod book.ko

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver$ dmesg
在最后面能看见:
[ 5232.943337]  book name:dissecting Linux Device Driver
[ 5232.943339]  book num:4000

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver$ modinfo book.ko
filename:       book.ko
version:        V1.0
description:    A simple Module for testing module params
author:         Song Baohua, author@linuxdriver.cn
license:        Dual BSD/GPL
srcversion:     84B9AF414D6F568FA2AB9A6
depends:       
vermagic:       2.6.35.4 SMP mod_unload modversions
parm:           num:int
parm:           book_name:charp

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver$ sudo rmmod book.ko
dmesg后,能看见:
[ 5492.863268]  Book module exit
[ 5492.863269]

参数的传递过程:
当运行如下:
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/module_param$sudo insmod book.ko book_name='GoodBook' num=5000

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/module_param$tail -n 2 /var/log/messages
Jan 24 11:13:15 xxha-OptiPlex-780 kernel: [ 6909.918999]  book name:GoodBook
Jan 24 11:13:15 xxha-OptiPlex-780 kernel: [ 6909.919001]  book num:5000

这样就把参数传进去了。
/var/log/messages日志文件可以看到内核的输出。
这就是book.ko驱动了,很简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值