http://www.ruixuedz.com.cn/article/dc/155.html
---------------------------------------------------------------------
module_param(name, type, perm)是一個(gè)宏,向當(dāng)前模塊傳入?yún)?shù),對(duì)源碼分析如下
在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的關(guān)鍵字,可參考
http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html
__attibute__將參數(shù)編譯到__param段中,
module_param是一步一步展開(kāi)的,
上面幾個(gè)宏在include\linux\moduleparam.h中的順序剛好相反
module_param宏的類(lèi)似函數(shù)調(diào)用的順序
module_param->module_param_named->module_param_call->__module_param_call
展開(kāi)的順序正好相反
__module_param_call->module_param_call->module_param_named->module_param
type類(lèi)型可以是byte,short,ushort,int,
uint,long,ulong,charp(注:字符指針),bool,invbool,
perm表示此參數(shù)在sysfs文件系統(tǒng)中所對(duì)應(yīng)的文件節(jié)點(diǎn)的屬性。
權(quán)限在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
當(dāng)perm為0時(shí),表示此參數(shù)不存在 sysfs文件系統(tǒng)下對(duì)應(yīng)的文件節(jié)點(diǎn)。