模块传参:
1.头文件:
include/linux/moduleparam.h
传单个参数:
内核模块传单个参数
– module_param(name,type,perm)
– name:模块参数的名称
– type: 模块参数的数据类型(支持int long short uint ulong ushort类
型)
– perm: 模块参数的访问权限
传多个参数
module_param_array(name, type, nump, perm)
– name:模块参数的名称
– type: 模块参数的数据类型(支持int long short uint ulong ushort类
型)
– nump:保存参数个数的地址
– perm: 模块参数的访问权限
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h> //file_operationes
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/moduleparam.h>
#include <linux/stat.h>
#define DEVICE_NODE_NAME "device_node_name"
#define DRIVER_NAME "hello_ctl"
#define LED_PIN_VAL0 0
#define LED_PIN_VAL1 1
MODULE_LICENSE("Dual BSD/GPL");
/*单个参数传递定义*/
int name_para=0,name_para2;
/*多个参数数组定义*/
int name2[10];
/*多个参数的数量定义*/
int num;
module_param(name_para,int,S_IRUSR);
module_param(name_para2,int,S_IRUSR);
module_param_array(name2, int, &num, S_IRUSR);
static int plat_probe(struct platform_device * dev)
{
int i=0;
/*打印传递的参数*/
printk(KERN_EMERG "plat_probe over name_para =%d \n", name_para);
printk(KERN_EMERG "plat_probe over name_para2 =%d \n", name_para2);
for (i=0;i<num;i++)
{
printk(KERN_EMERG "plat_probe over name2 =%d i = %d\n", name2[i], i);
}
return 0;
}
static int plat_remov(struct platform_device * dev)
{
return 0;
}
static void plat_shutdown(struct platform_device *dev)
{
}
static int plat_suspend(struct platform_device *dev, pm_message_t state)
{
return 0;
}
static int plat_resume(struct platform_device * dev)
{
return 0;
}
/* 驱动结构初始化 */
static struct platform_driver plat_dev=
{
.probe = plat_probe,
.remove =plat_remov,
.shutdown =plat_shutdown,
.suspend=plat_suspend,
.resume = plat_resume,
.driver ={
.owner = THIS_MODULE,
.name = DRIVER_NAME,
}
};
/* 平台虚拟总线驱动注册函数 */
static int misc_cdev_init (void)
{
platform_driver_register(&plat_dev);
return 0;
}
static void misc_cdev_exit (void)
{
platform_driver_unregister(&plat_dev);
}
/*模块注册*/
module_init(misc_cdev_init);
module_exit(misc_cdev_exit);
输出结果:
insmod /mnt/usb1/module_param.ko name_para=11 name_para2=99 n
ame2=12,13,11,11
[ 464.789509] plat_probe over name_para =11
[ 464.792358] plat_probe over name_para2 =99
[ 464.804563] plat_probe over name2 =12 i = 0
[ 464.807723] plat_probe over name2 =13 i = 1
[ 464.811923] plat_probe over name2 =11 i = 2
[ 464.816218] plat_probe over name2 =11 i = 3
这里数组定义name的范围是最多存储的数量,不是一定要传递的个数;
以实际传入的数量为准,记录在nump中