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 类型 |
invbool | bool的反值,例如赋值为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$