1、什么是驱动传参
驱动传参就是传参数给我们的驱动。举例:
insmod beep.ko a=1
2、驱动传参有什么作用
①设置驱动的相关参数,比如设置缓冲区的大小
②设置安全校验,防止我们班写的驱动被人调用
3、怎么给驱动传参数?
①传递普通的参数
如char,int类型的,其函数宏定义如下:在includelinux/moduleparam.h
module_param(name, type, perm);
name:要传递进去的参数的名称
type:类型
perm:参数读写的权限
②传递数组
函数:
module_param_array(name, type, nump, perm)
name:要传递进去的参数的名称
type:类型
nump:实际要传入进去的参数的个数
perm:参数读写的权限
4、 实验代码:
#include <linux/init.h>
#include <linux/module.h>//最基本的文件,支持动态添加和卸载模块
static int a;
static int b[5];
static int count;
module_param(a,int,S_IRUSR);//S_IRUSR在include/linux/stat.h
module_param_array(b,int,&count,S_IRUSR);
static int hello_init(void)
{
int i;
for(i=0;i<count;i++)
{
printk("b[%d] = %d\n",i,b[i]);
}
printk("count = %d\n",count);
printk("a = %d\n",a);
return 0;
}
static void hello_exit(void)
{
printk("hello_exit \n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
编译前设置环境变量:
export ARCH=arm
export CROSS_COMPILE=arm-buildroot-linux-uclibcgnueabihf-
加载驱动,可以看到在/sys/module/parameter/paramters/下会生成参数变量名称: