使用module_param来向内核模块传递参数

来源:互联网 发布:水印制作软件下载 编辑:程序博客网 时间:2024/04/29 13:44

http://blog.csdn.net/sttypxx520/article/details/2579679


如果希望给一个内核模块传递参数,则可以通过module_param()。

参数用moudle_param宏定义来声明,它定义在moduleparam.h。

下面是module_param宏的定义:

/* This is the fundamental function for registering boot/module   

parameters. perm sets the visibility in sysfs: 000 means it's   

not there, read bits mean it's readable, write bits mean it's   

writable. */

#define __module_param_call(prefix, name, set, get, arg, perm)        /

     /* Default value instead of permissions? */            /

     static int __param_perm_check_##name __attribute__((unused)) =    /

     BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2));    /

     static char __param_str_##name[] = prefix #name;        /

     static struct kernel_param const __param_##name            /

     __attribute_used__                        /

     __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) /

     = { __param_str_##name, perm, set, get, arg }

 

#define module_param_call(name, set, get, arg, perm)                  /

     __module_param_call(MODULE_PARAM_PREFIX, name, set, get, arg, perm)

/* Helper functions: type is byte, short, ushort, int, uint, long,    ulong, charp, bool or invbool, or XXX if you define param_get_XXX,    param_set_XXX and param_check_XXX. */

#define module_param_named(name, value, type, perm)               /

     param_check_##type(name, &(value));                   /

     module_param_call(name, param_set_##type, param_get_##type, &value, perm); /

     __MODULE_PARM_TYPE(name, #type)

#define module_param(name, type, perm)                /

     module_param_named(name, name, type, perm)

module_param使用了3个参数: 变量名,它的类型,以及一个权限掩码。这个宏定义应当放在任何函数之外,典型的是出现在源文件的前面,定义如:

static int swap_xy = 0;

static int rotate = 0;

module_param(swap_xy, int, S_IRUGO);

module_param(rotate, int, S_IRUGO);

模块参数支持许多类型:

bool

invbool 一个布尔型(true或者false)值。

charp 一个字符指针值。内存为用户提供的字串分配,指针因此设置。

int

long

short

uint

ulong

ushort 基本的变长整型值。以 u 开头的是无符号值。

最后的 module_param 字段是一个权限值,应当使用<linux/stat.h>中定义的值。这个值控制谁可以存取这些模块参数在sysfs中的表示。如果perm 被设为0,就没有sysfs项。否则,出现在/sys/module下面,并带有给定的权限。使用S_IRUGO作为参数可以被所有人读取,但是不能改变。S_IRUGO|S_IWUSR允许root来改变参数。注意,如果一个参数被sysfs修改,你的模块看到的参数值也改变了,但是你的模块没有任何 其他的通知。因此你应当不要使模块参数可写。


原创粉丝点击