[转]module_param(name, type, perm)的理解 //TODO
http://www.ruixuedz.com.cn/article/dc/155.html
---------------------------------------------------------------------
module_param(name, type, perm)是一个宏,向当前模块传入参数,对源码分析如下
在include\linux\moduleparam.h中
#define module_param(name, type, perm) \
module_param_named(name, name, type, perm)
#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_call(name, set, get, arg, perm) \
__module_param_call(MODULE_PARAM_PREFIX, name, set, get, arg, perm)
#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 }
__attibute__ 是gcc的关键字,可参考
http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html
__attibute__将参数编译到__param段中,
module_param是一步一步展开的,
上面几个宏在include\linux\moduleparam.h中的顺序刚好相反
module_param宏的类似函数调用的顺序
module_param->module_param_named->module_param_call->__module_param_call
展开的顺序正好相反
__module_param_call->module_param_call->module_param_named->module_param
type类型可以是byte,short,ushort,int,
uint,long,ulong,charp(注:字符指针),bool,invbool,
perm表示此参数在sysfs文件系统中所对应的文件节点的属性。
权限在include/linux/stat.h中有定义
比如:
#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100
#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010
#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001
当perm为0时,表示此参数不存在 sysfs文件系统下对应的文件节点。
---------------------------------------------------------------------
module_param(name, type, perm)是一个宏,向当前模块传入参数,对源码分析如下
在include\linux\moduleparam.h中
#define module_param(name, type, perm) \
module_param_named(name, name, type, perm)
#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_call(name, set, get, arg, perm) \
__module_param_call(MODULE_PARAM_PREFIX, name, set, get, arg, perm)
#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 }
__attibute__ 是gcc的关键字,可参考
http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html
__attibute__将参数编译到__param段中,
module_param是一步一步展开的,
上面几个宏在include\linux\moduleparam.h中的顺序刚好相反
module_param宏的类似函数调用的顺序
module_param->module_param_named->module_param_call->__module_param_call
展开的顺序正好相反
__module_param_call->module_param_call->module_param_named->module_param
type类型可以是byte,short,ushort,int,
uint,long,ulong,charp(注:字符指针),bool,invbool,
perm表示此参数在sysfs文件系统中所对应的文件节点的属性。
权限在include/linux/stat.h中有定义
比如:
#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100
#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010
#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001
当perm为0时,表示此参数不存在 sysfs文件系统下对应的文件节点。