2、驱动模块传参

就是驱动模块加载传递参数给我们的驱动

驱动传参的作用?

1、设置驱动的相关参数,驱动程序更灵活 ,比如 缓冲区大小

2、设置安全校验,防止驱动被别人盗用

怎么给驱动传参

驱动传递的参数类型:

基本类型:char /bool/int /long/short/byte/ushort/uint.

数组:array

字符串:string

传递普通参数

Module_param()

传递数组

Module_param_array()

传递参数

例程

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

static int a;
static int b[10];
static int array_size;

static char str1[10];

module_param(a,int,S_IRUGO);
MODULE_PARAM_DESC(a,"e.g:a=1");

module_param_array(b,int,&array_size,S_IRUGO);
MODULE_PARAM_DESC(b,"e.g:b=1,2,3");

module_param_string(str,str1,sizeof(str1),S_IRUGO);
MODULE_PARAM_DESC(str,"e.g:str=hello");


static int hello_init(void)
{
    printk("input a=%d\n",a);
    for(int i=0; i< array_size;i++)
    {
        printk("input b[%d]=%d \n",,i,b[i]);
    }
    
    printk("hello word init\n");
    return 0;
}

static void hello_exit(void)
{

    printk("hello word exit\n");
    return 0;
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("SONG");
MODULE_VERSION("v1.0");

通过命令可以查看 传参的描述信息

modinfo hello.ko

执行命令 

insmod hello.ko a=125 b=1,2,7,9 str="hhhh"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值