linux驱动开发-模块参数

在我们使用模块的时候需要在装载时传递参数给模块,linux 支持在装载模块的同时传入参数 。

比如 (num 和who就是传入模块的参数):

 $> insmod hello.ko num=10 who="jack"
模块参数必须用module_parm宏声明,这个宏定义在<linux/moduleparam.h>,刚才连个参数在模块中的定义如下:

static char *who = "world";
static int num = 1;
module_param(num, int, S_IRUGO);
module_param(who, charp, S_IRUGO);
内核支持的模块参数类型有:
bool
invbool (true变为false, false变为true)
charp 字符指针
int
long
short
uint
ulong
ushort

S_IRUGO是访问许可值,参见<linux/stat.h>中的定义(S_IWUGO ,S_IXUGO 等)。

示例:

//test.c
#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");

static int count = 1;
module_param(count, int, S_IRUGO);
static char *name = "test";
module_param(name, charp, S_IRUGO);

static __init int a(void)
{
    printk("%s %d: count = %d, name = %s\n", __func__, __LINE__, count, name);

    return 0;
}

static __exit void b(void)
{
    printk("%s %d: count = %d, name = %s\n", __func__, __LINE__, count, name);
}

module_init(a);
module_exit(b);

$> insmod count=10 name=jacky

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值