Linux驱动学习—驱动模块传参数

1、什么是驱动传参

驱动传参就是传参数给我们的驱动。举例:

insmod beep.ko a=1

2、驱动传参有什么作用

①设置驱动的相关参数,比如设置缓冲区的大小

②设置安全校验,防止我们班写的驱动被人调用

3、怎么给驱动传参数?

①传递普通的参数

如char,int类型的,其函数宏定义如下:在includelinux/moduleparam.h

module_param(name, type, perm);
name:要传递进去的参数的名称
type:类型
perm:参数读写的权限

②传递数组

函数:

module_param_array(name, type, nump, perm)
​
name:要传递进去的参数的名称
type:类型
nump:实际要传入进去的参数的个数
perm:参数读写的权限

4、 实验代码:

#include <linux/init.h>
#include <linux/module.h>//最基本的文件,支持动态添加和卸载模块
​
static int a;
static int b[5];
static int count;
module_param(a,int,S_IRUSR);//S_IRUSR在include/linux/stat.h
module_param_array(b,int,&count,S_IRUSR);
​
static int hello_init(void)
{
    int i;
    for(i=0;i<count;i++)
    {
        printk("b[%d] = %d\n",i,b[i]);
    }
    printk("count = %d\n",count);
    printk("a = %d\n",a);
    return 0;
}
​
static void  hello_exit(void)
{
    printk("hello_exit \n");
}
​
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

编译前设置环境变量:

export ARCH=arm
export CROSS_COMPILE=arm-buildroot-linux-uclibcgnueabihf-

加载驱动,可以看到在/sys/module/parameter/paramters/下会生成参数变量名称:

4、如果多传递参数进去驱动,会报错

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值