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"