linux驱动11:模块参数

 模块参数可以在运行insmod或modprobe命令装载模块时赋值,modprobe可以从配置文件(/etc/modprobe.conf)中读取参数值。

在insmod改变模块参数之前,模块必须让参数对insmod命令可见。参数使用  module_param(变量名,类型,访问许可值)  宏来声明,在源文件头部声明参数。

所有的模块参数都应该在源文件中给定一个默认值。

模块支持的模块参数:

bool、invbool(取反,true变为false,false变为true)

charp(字符指针)

int、long、short、unit、ulong、ushort

数组参数:module_param_array(数组名,类型,值的个数,访问许可值);

模块中的钩子可让我们自定义类型

访问许可值:

<linux/stat.h>

设置为0不会有对应的sysfs入口项,否则模块参数会在/sys/module/(如下所示)

S_IRUGO 任何人都可以读取,但不能修改

S_IRUGO | S_IWUSR 允许root用户修改

大多数情况下不应该让模块参数是可写的

例程:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

static char *hello_str = "hello";
static int hello_cnt = 2;
module_param(hello_str, charp, S_IRUGO);
module_param(hello_cnt, int, S_IRUGO);


static int hello_init(void)
{
	printk(KERN_ALERT"hello world!\n");
	printk("%s, %d\n", hello_str, hello_cnt);
	return 0;
}

static void hello_exit(void)
{
	printk(KERN_ALERT"goodbye world!\n");
}

MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值