内核允许驱动程序在insmod的时候通过 module_param(name,type,perm) 传递参数给驱动程序
3个参数:name=要传递的参数变量名,type=变量的数据类型,perm=访问参数的权限
宏module_param在<linux/moduleparam.h>中定义
#define module_param(name, type, perm) module_param_named(name, name, type, perm)
perm:在 <linux/stat.h>中定义,其中S_IRUGO参数可以被所有人读取,但不能更改
其中module就是指驱动程序,linux驱动程序可以以两种方式加载,一种直接编译进内核中;
第二种就是编译成module,使用驱动程序的时候,通过insmod命令 加载module
param.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/moduleparam.h>
//参数及默认值
static int times = 1; //打印次数
static char *who =" world!"; //打印内容
//module_param(name,type,perm);
//功能:指定模块参数,用于在加载模块时或者模块加载以后传递参数给模块。
//name:模块参数的名称
//type: 模块参数的数据类型
//perm: 模块参数的访问权限
module_param (times, int, S_IRUGO);
MODULE_PARM_DESC (times, "A integer type for times");
module_param (who, charp, S_IRUGO);
MODULE_PARM_DESC (who, "A character string");
static int __init hello_init (void)
{
int i;
for (i=0; i<times; i++){
printk (KERN_INFO "%d hello %s\n",i, who);
}
return 0;
}
static void __exit hello_exit (void)
{
printk (KERN_INFO "googbye %s\n",who);
}
module_init (hello_init);
module_exit (hello_exit);
MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("hcl0317");
MODULE_DESCRIPTION ("a simple param driver");
MODULE_SUPPORTED_DEVICE ("param test");
带1个参数
insmod param.ko times=10
[ 1639.040000] 0 hello world!
[ 1639.040000] 1 hello world!
[ 1639.040000] 2 hello world!
[ 1639.040000] 3 hello world!
[ 1639.045000] 4 hello world!
[ 1639.045000] 5 hello world!
[ 1639.050000] 6 hello world!
[ 1639.050000] 7 hello world!
[ 1639.055000] 8 hello world!
[ 1639.060000] 9 hello world!
rmmod param
[ 1677.255000] googbye world!
带2个参数
insmod param.ko times=5 who=hgfdks
[ 2843.455000] 0 hello hgfdks
[ 2843.455000] 1 hello hgfdks
[ 2843.455000] 2 hello hgfdks
[ 2843.455000] 3 hello hgfdks
[ 2843.460000] 4 hello hgfdks
rmmod param
[ 2870.540000] googbye hgfdks
rmmod: module 'param' not found