Linux驱动之 加载模块传参module_param、module_param_named

Linux驱动–加载模块传参学习记录:

/*
*@ module_param(name, type, perm) //给模块传参函数
*@ name 接收参数变量名,既是用户看到的参数名,又是模块内接受参数的变量;
*@ type 参数数据类型
*@ perm 指定参数访问权限
*/
module_param(whom,charp,0644);

相关宏:

module_param_named(name, g_var, type, perm); //声明外部的参数名
module_param_string(name, string, len, perm); //将字符串直接复制到程序中的字符数组内
module_param_arry(name, type, num_point, perm);//传递多个参数
module_param_array_named(name, array, type, nump, perm);//声明外部的参数名
MODULE_PARM_DESC(g_val,"Boollean to enable debugging (0/1 == off/on)"); //对参数进行说明

Linux内核支持的模块参数类型

类型说明
bool布尔值(true/false),关联的变量应该是int 类型
invboolbool的反值,例如赋值为true,实际值为false
charp字符型指针,内核为用户提供的字符串分配内存,并设置此指针保存其首地址
int整型
long长整型
short短整型
uint无符号整型
ulong无符号长整型
ushort无符号短整型

vim hello.c

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

static char *whom = "helloChen \n";
static int g_var = 888;

static int drivers_init(void)
{
	printk("drivers_init %s \n",whom);
	return 0;
}
static void drivers_exit(void)
{
	printk("drivers_exit  %d\n",g_var);
	return;
}

MODULE_LICENSE("GPL"); 
module_param(whom,charp,0644);
module_param_named(g_var_out,g_var,int,0644);

module_init(drivers_init);
module_exit(drivers_exit);

编译、装载:

cxx@ubuntu16:~/driver/param$ ls
hello.c  Makefile
cxx@ubuntu16:~/driver/param$ make
make -C /lib/modules/4.15.0-142-generic/build M=/home/cxx/driver/param modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-142-generic'
  CC [M]  /home/cxx/driver/param/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/cxx/driver/param/hello.mod.o
  LD [M]  /home/cxx/driver/param/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-142-generic'
cxx@ubuntu16:~/driver/param$ lsmod | grep hello
cxx@ubuntu16:~/driver/param$ dmesg
cxx@ubuntu16:~/driver/param$ sudo insmod hello.ko 
cxx@ubuntu16:~/driver/param$ dmesg
[ 7553.610837] drivers_init helloChen 
          
cxx@ubuntu16:~/driver/param$ sudo rmmod hello
cxx@ubuntu16:~/driver/param$ dmesg 
[ 7553.610837] drivers_init helloChen 
                
[ 7576.117113] drivers_exit  888
cxx@ubuntu16:~/driver/param$ 

传入参数:

cxx@ubuntu16:~/driver/param$ sudo dmesg -c
[ 7553.610837] drivers_init helloChen        
[ 7576.117113] drivers_exit  888
cxx@ubuntu16:~/driver/param$ sudo insmod hello.ko g_var_out=666 whom="cxx"
cxx@ubuntu16:~/driver/param$ ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers
cxx@ubuntu16:~/driver/param$ 
cxx@ubuntu16:~/driver/param$ 
cxx@ubuntu16:~/driver/param$ sudo rmmod hello 
cxx@ubuntu16:~/driver/param$ dmesg 
[ 7869.074366] drivers_init cxx 
[ 8053.996123] drivers_exit  666
cxx@ubuntu16:~/driver/param$ 

查看/sys/module/文件:

cxx@ubuntu16:/sys/module/hello$ ls
coresize  initsize   notes       refcnt    srcversion  uevent
holders   initstate  parameters  sections  taint
cxx@ubuntu16:/sys/module/hello$ cd parameters/
cxx@ubuntu16:/sys/module/hello/parameters$ ls
g_var_out  whom
cxx@ubuntu16:/sys/module/hello/parameters$ cat g_var_out 
666
cxx@ubuntu16:/sys/module/hello/parameters$ cat whom 
cxx
cxx@ubuntu16:/sys/module/hello/parameters$ ls -l
total 0
-rw-r--r-- 1 root root 4096 Apr 23 17:35 g_var_out
-rw-r--r-- 1 root root 4096 Apr 23 17:35 whom
cxx@ubuntu16:/sys/module/hello/parameters$ 
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程一时爽Cxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值