驱动 内核模块传参

1.接收命令行传递的参数

module_param(name, type, perm)

name:要传参的变量名

type:变量类型

        /* Standard types are:
         * byte, hexint, short, ushort, int, uint, long, ulong
         * charp: a character pointer
         * bool: a bool, values 0/1, y/n, Y/N.
         * invbool: the above, only sense-reversed (N = true).
         */

perm:设置文件权限(0664)

2.接收命令行传递的数组

module_param_array(name, type, nump, perm)

name:要传参的变量名

type:变量类型

        /* Standard types are:
         * byte, hexint, short, ushort, int, uint, long, ulong
         * charp: a character pointer
         * bool: a bool, values 0/1, y/n, Y/N.
         * invbool: the above, only sense-reversed (N = true).
         */

nump:传递的成员个数

perm:设置文件权限(0664)

3.对变量信息的详细描述(可以通过modinfo xxx.ko查看)

MODULE_PARM_DESC(_parm, desc)

_parm:要传参的变量名

desc:描述变量的字符串

传参示例

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

int backlight = 127;
module_param(backlight, int, 0664);
MODULE_PARM_DESC(backlight, "this is backlight var range[0-255]");

char a = 'A';
module_param(a, byte, 0664);
MODULE_PARM_DESC(a, "this is char var");

char* p = "helloworld";
module_param(p, charp, 0664);
MODULE_PARM_DESC(p, "this is char* var");

int ww[10] = {0};
int len = 10;
module_param_array(ww,int,&len,0664);
MODULE_PARM_DESC(ww, "this is int [10] var");

static int __init mycdev_init(void)
{
    int i;
    printk("backlight = %d\n", backlight);
    printk("a = %d\n", a);
    printk("p = %s\n", p);
    for(i=0;i<len;i++){
        printk("ww[%d] = %d\n",i,ww[i]);
    }
    return 0;
}
static void __exit mycdev_exit(void)
{
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

传参方式:

sudo insmod demo.ko ww=11,22,33,44,55 backlight=100 a=70 p="123_456"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值