11da177e4SLinus Torvalds #ifndef _LINUX_MODULE_PARAMS_H 21da177e4SLinus Torvalds #define _LINUX_MODULE_PARAMS_H 31da177e4SLinus Torvalds /* (C) Copyright 2001, 2002 Rusty Russell IBM Corporation */ 41da177e4SLinus Torvalds #include <linux/init.h> 51da177e4SLinus Torvalds #include <linux/stringify.h> 61da177e4SLinus Torvalds #include <linux/kernel.h> 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* You can override this manually, but generally this should match the 91da177e4SLinus Torvalds module name. */ 101da177e4SLinus Torvalds #ifdef MODULE 111da177e4SLinus Torvalds #define MODULE_PARAM_PREFIX /* empty */ 121da177e4SLinus Torvalds #else 13367cb704SSam Ravnborg #define MODULE_PARAM_PREFIX KBUILD_MODNAME "." 141da177e4SLinus Torvalds #endif 151da177e4SLinus Torvalds 16730b69d2SRusty Russell /* Chosen so that structs with an unsigned long line up. */ 17730b69d2SRusty Russell #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long)) 18730b69d2SRusty Russell 191da177e4SLinus Torvalds #ifdef MODULE 201da177e4SLinus Torvalds #define ___module_cat(a,b) __mod_ ## a ## b 211da177e4SLinus Torvalds #define __module_cat(a,b) ___module_cat(a,b) 221da177e4SLinus Torvalds #define __MODULE_INFO(tag, name, info) \ 231da177e4SLinus Torvalds static const char __module_cat(name,__LINE__)[] \ 243ff6eeccSAdrian Bunk __used \ 251da177e4SLinus Torvalds __attribute__((section(".modinfo"),unused)) = __stringify(tag) "=" info 261da177e4SLinus Torvalds #else /* !MODULE */ 271da177e4SLinus Torvalds #define __MODULE_INFO(tag, name, info) 281da177e4SLinus Torvalds #endif 291da177e4SLinus Torvalds #define __MODULE_PARM_TYPE(name, _type) \ 301da177e4SLinus Torvalds __MODULE_INFO(parmtype, name##type, #name ":" _type) 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds struct kernel_param; 331da177e4SLinus Torvalds 349bbb9e5aSRusty Russell struct kernel_param_ops { 351da177e4SLinus Torvalds /* Returns 0, or -errno. arg is in kp->arg. */ 369bbb9e5aSRusty Russell int (*set)(const char *val, const struct kernel_param *kp); 371da177e4SLinus Torvalds /* Returns length written or -errno. Buffer is 4k (ie. be short!) */ 389bbb9e5aSRusty Russell int (*get)(char *buffer, const struct kernel_param *kp); 39e6df34a4SRusty Russell /* Optional function to free kp->arg when module unloaded. */ 40e6df34a4SRusty Russell void (*free)(void *arg); 419bbb9e5aSRusty Russell }; 421da177e4SLinus Torvalds 4345fcc70cSRusty Russell /* Flag bits for kernel_param.flags */ 44fddd5201SRusty Russell #define KPARAM_ISBOOL 2 4545fcc70cSRusty Russell 461da177e4SLinus Torvalds struct kernel_param { 471da177e4SLinus Torvalds const char *name; 489bbb9e5aSRusty Russell const struct kernel_param_ops *ops; 4945fcc70cSRusty Russell u16 perm; 5045fcc70cSRusty Russell u16 flags; 5122e48eafSJan Beulich union { 521da177e4SLinus Torvalds void *arg; 5322e48eafSJan Beulich const struct kparam_string *str; 5422e48eafSJan Beulich const struct kparam_array *arr; 5522e48eafSJan Beulich }; 561da177e4SLinus Torvalds }; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* Special one for strings we want to copy into */ 591da177e4SLinus Torvalds struct kparam_string { 601da177e4SLinus Torvalds unsigned int maxlen; 611da177e4SLinus Torvalds char *string; 621da177e4SLinus Torvalds }; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* Special one for arrays */ 651da177e4SLinus Torvalds struct kparam_array 661da177e4SLinus Torvalds { 671da177e4SLinus Torvalds unsigned int max; 681da177e4SLinus Torvalds unsigned int *num; 699bbb9e5aSRusty Russell const struct kernel_param_ops *ops; 701da177e4SLinus Torvalds unsigned int elemsize; 711da177e4SLinus Torvalds void *elem; 721da177e4SLinus Torvalds }; 731da177e4SLinus Torvalds 7491d35dd9SIvan Kokshaysky /* On alpha, ia64 and ppc64 relocations to global data cannot go into 7591d35dd9SIvan Kokshaysky read-only sections (which is part of respective UNIX ABI on these 7691d35dd9SIvan Kokshaysky platforms). So 'const' makes no sense and even causes compile failures 7791d35dd9SIvan Kokshaysky with some compilers. */ 7891d35dd9SIvan Kokshaysky #if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64) 7991d35dd9SIvan Kokshaysky #define __moduleparam_const 8091d35dd9SIvan Kokshaysky #else 8191d35dd9SIvan Kokshaysky #define __moduleparam_const const 8291d35dd9SIvan Kokshaysky #endif 8391d35dd9SIvan Kokshaysky 841da177e4SLinus Torvalds /* This is the fundamental function for registering boot/module 85405ae7d3SRobert P. J. Day parameters. perm sets the visibility in sysfs: 000 means it's 861da177e4SLinus Torvalds not there, read bits mean it's readable, write bits mean it's 871da177e4SLinus Torvalds writable. */ 889bbb9e5aSRusty Russell #define __module_param_call(prefix, name, ops, arg, isbool, perm) \ 899774a1f5SAlexey Dobriyan /* Default value instead of permissions? */ \ 909774a1f5SAlexey Dobriyan static int __param_perm_check_##name __attribute__((unused)) = \ 91730b69d2SRusty Russell BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)) \ 92730b69d2SRusty Russell + BUILD_BUG_ON_ZERO(sizeof(""prefix) > MAX_PARAM_PREFIX_LEN); \ 9322e48eafSJan Beulich static const char __param_str_##name[] = prefix #name; \ 9491d35dd9SIvan Kokshaysky static struct kernel_param __moduleparam_const __param_##name \ 953ff6eeccSAdrian Bunk __used \ 961da177e4SLinus Torvalds __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \ 979bbb9e5aSRusty Russell = { __param_str_##name, ops, perm, isbool ? KPARAM_ISBOOL : 0, \ 989bbb9e5aSRusty Russell { arg } } 991da177e4SLinus Torvalds 1009bbb9e5aSRusty Russell /* Obsolete - use module_param_cb() */ 1011da177e4SLinus Torvalds #define module_param_call(name, set, get, arg, perm) \ 1029bbb9e5aSRusty Russell static struct kernel_param_ops __param_ops_##name = \ 1039bbb9e5aSRusty Russell { (void *)set, (void *)get }; \ 104fddd5201SRusty Russell __module_param_call(MODULE_PARAM_PREFIX, \ 1059bbb9e5aSRusty Russell name, &__param_ops_##name, arg, \ 1069bbb9e5aSRusty Russell __same_type(*(arg), bool), \ 1079bbb9e5aSRusty Russell (perm) + sizeof(__check_old_set_param(set))*0) 1081da177e4SLinus Torvalds 1099bbb9e5aSRusty Russell /* We don't get oldget: it's often a new-style param_get_uint, etc. */ 1109bbb9e5aSRusty Russell static inline int 1119bbb9e5aSRusty Russell __check_old_set_param(int (*oldset)(const char *, struct kernel_param *)) 1129bbb9e5aSRusty Russell { 1139bbb9e5aSRusty Russell return 0; 1149bbb9e5aSRusty Russell } 1159bbb9e5aSRusty Russell 1169bbb9e5aSRusty Russell #define module_param_cb(name, ops, arg, perm) \ 1179bbb9e5aSRusty Russell __module_param_call(MODULE_PARAM_PREFIX, \ 1189bbb9e5aSRusty Russell name, ops, arg, __same_type(*(arg), bool), perm) 1199bbb9e5aSRusty Russell 1209bbb9e5aSRusty Russell /* 1219bbb9e5aSRusty Russell * Helper functions: type is byte, short, ushort, int, uint, long, 1229bbb9e5aSRusty Russell * ulong, charp, bool or invbool, or XXX if you define param_ops_XXX 1239bbb9e5aSRusty Russell * and param_check_XXX. 1249bbb9e5aSRusty Russell */ 1251da177e4SLinus Torvalds #define module_param_named(name, value, type, perm) \ 1261da177e4SLinus Torvalds param_check_##type(name, &(value)); \ 1279bbb9e5aSRusty Russell module_param_cb(name, ¶m_ops_##type, &value, perm); \ 1281da177e4SLinus Torvalds __MODULE_PARM_TYPE(name, #type) 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds #define module_param(name, type, perm) \ 1311da177e4SLinus Torvalds module_param_named(name, name, type, perm) 1321da177e4SLinus Torvalds 133*907b29ebSRusty Russell /** 134*907b29ebSRusty Russell * kparam_block_sysfs_write - make sure a parameter isn't written via sysfs. 135*907b29ebSRusty Russell * @name: the name of the parameter 136*907b29ebSRusty Russell * 137*907b29ebSRusty Russell * There's no point blocking write on a paramter that isn't writable via sysfs! 138*907b29ebSRusty Russell */ 139*907b29ebSRusty Russell #define kparam_block_sysfs_write(name) \ 140*907b29ebSRusty Russell do { \ 141*907b29ebSRusty Russell BUG_ON(!(__param_##name.perm & 0222)); \ 142*907b29ebSRusty Russell __kernel_param_lock(); \ 143*907b29ebSRusty Russell } while (0) 144*907b29ebSRusty Russell 145*907b29ebSRusty Russell /** 146*907b29ebSRusty Russell * kparam_unblock_sysfs_write - allows sysfs to write to a parameter again. 147*907b29ebSRusty Russell * @name: the name of the parameter 148*907b29ebSRusty Russell */ 149*907b29ebSRusty Russell #define kparam_unblock_sysfs_write(name) \ 150*907b29ebSRusty Russell do { \ 151*907b29ebSRusty Russell BUG_ON(!(__param_##name.perm & 0222)); \ 152*907b29ebSRusty Russell __kernel_param_unlock(); \ 153*907b29ebSRusty Russell } while (0) 154*907b29ebSRusty Russell 155*907b29ebSRusty Russell /** 156*907b29ebSRusty Russell * kparam_block_sysfs_read - make sure a parameter isn't read via sysfs. 157*907b29ebSRusty Russell * @name: the name of the parameter 158*907b29ebSRusty Russell * 159*907b29ebSRusty Russell * This also blocks sysfs writes. 160*907b29ebSRusty Russell */ 161*907b29ebSRusty Russell #define kparam_block_sysfs_read(name) \ 162*907b29ebSRusty Russell do { \ 163*907b29ebSRusty Russell BUG_ON(!(__param_##name.perm & 0444)); \ 164*907b29ebSRusty Russell __kernel_param_lock(); \ 165*907b29ebSRusty Russell } while (0) 166*907b29ebSRusty Russell 167*907b29ebSRusty Russell /** 168*907b29ebSRusty Russell * kparam_unblock_sysfs_read - allows sysfs to read a parameter again. 169*907b29ebSRusty Russell * @name: the name of the parameter 170*907b29ebSRusty Russell */ 171*907b29ebSRusty Russell #define kparam_unblock_sysfs_read(name) \ 172*907b29ebSRusty Russell do { \ 173*907b29ebSRusty Russell BUG_ON(!(__param_##name.perm & 0444)); \ 174*907b29ebSRusty Russell __kernel_param_unlock(); \ 175*907b29ebSRusty Russell } while (0) 176*907b29ebSRusty Russell 177*907b29ebSRusty Russell #ifdef CONFIG_SYSFS 178*907b29ebSRusty Russell extern void __kernel_param_lock(void); 179*907b29ebSRusty Russell extern void __kernel_param_unlock(void); 180*907b29ebSRusty Russell #else 181*907b29ebSRusty Russell static inline void __kernel_param_lock(void) 182*907b29ebSRusty Russell { 183*907b29ebSRusty Russell } 184*907b29ebSRusty Russell static inline void __kernel_param_unlock(void) 185*907b29ebSRusty Russell { 186*907b29ebSRusty Russell } 187*907b29ebSRusty Russell #endif 188*907b29ebSRusty Russell 18967e67ceaSRusty Russell #ifndef MODULE 19067e67ceaSRusty Russell /** 19167e67ceaSRusty Russell * core_param - define a historical core kernel parameter. 19267e67ceaSRusty Russell * @name: the name of the cmdline and sysfs parameter (often the same as var) 19367e67ceaSRusty Russell * @var: the variable 19467e67ceaSRusty Russell * @type: the type (for param_set_##type and param_get_##type) 19567e67ceaSRusty Russell * @perm: visibility in sysfs 19667e67ceaSRusty Russell * 19767e67ceaSRusty Russell * core_param is just like module_param(), but cannot be modular and 19867e67ceaSRusty Russell * doesn't add a prefix (such as "printk."). This is for compatibility 19967e67ceaSRusty Russell * with __setup(), and it makes sense as truly core parameters aren't 20067e67ceaSRusty Russell * tied to the particular file they're in. 20167e67ceaSRusty Russell */ 20267e67ceaSRusty Russell #define core_param(name, var, type, perm) \ 20367e67ceaSRusty Russell param_check_##type(name, &(var)); \ 2049bbb9e5aSRusty Russell __module_param_call("", name, ¶m_ops_##type, \ 205fddd5201SRusty Russell &var, __same_type(var, bool), perm) 20667e67ceaSRusty Russell #endif /* !MODULE */ 20767e67ceaSRusty Russell 2081da177e4SLinus Torvalds /* Actually copy string: maxlen param is usually sizeof(string). */ 2091da177e4SLinus Torvalds #define module_param_string(name, string, len, perm) \ 21022e48eafSJan Beulich static const struct kparam_string __param_string_##name \ 2111da177e4SLinus Torvalds = { len, string }; \ 212fddd5201SRusty Russell __module_param_call(MODULE_PARAM_PREFIX, name, \ 2139bbb9e5aSRusty Russell ¶m_ops_string, \ 214fddd5201SRusty Russell .str = &__param_string_##name, 0, perm); \ 2151da177e4SLinus Torvalds __MODULE_PARM_TYPE(name, "string") 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds /* Called on module insert or kernel boot */ 2181da177e4SLinus Torvalds extern int parse_args(const char *name, 2191da177e4SLinus Torvalds char *args, 220914dcaa8SRusty Russell const struct kernel_param *params, 2211da177e4SLinus Torvalds unsigned num, 2221da177e4SLinus Torvalds int (*unknown)(char *param, char *val)); 2231da177e4SLinus Torvalds 224e180a6b7SRusty Russell /* Called by module remove. */ 225e180a6b7SRusty Russell #ifdef CONFIG_SYSFS 226e180a6b7SRusty Russell extern void destroy_params(const struct kernel_param *params, unsigned num); 227e180a6b7SRusty Russell #else 228e180a6b7SRusty Russell static inline void destroy_params(const struct kernel_param *params, 229e180a6b7SRusty Russell unsigned num) 230e180a6b7SRusty Russell { 231e180a6b7SRusty Russell } 232e180a6b7SRusty Russell #endif /* !CONFIG_SYSFS */ 233e180a6b7SRusty Russell 2341da177e4SLinus Torvalds /* All the helper functions */ 2351da177e4SLinus Torvalds /* The macros to do compile-time type checking stolen from Jakub 2361da177e4SLinus Torvalds Jelinek, who IIRC came up with this idea for the 2.4 module init code. */ 2371da177e4SLinus Torvalds #define __param_check(name, p, type) \ 2381da177e4SLinus Torvalds static inline type *__check_##name(void) { return(p); } 2391da177e4SLinus Torvalds 2409bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_byte; 2419bbb9e5aSRusty Russell extern int param_set_byte(const char *val, const struct kernel_param *kp); 2429bbb9e5aSRusty Russell extern int param_get_byte(char *buffer, const struct kernel_param *kp); 2431da177e4SLinus Torvalds #define param_check_byte(name, p) __param_check(name, p, unsigned char) 2441da177e4SLinus Torvalds 2459bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_short; 2469bbb9e5aSRusty Russell extern int param_set_short(const char *val, const struct kernel_param *kp); 2479bbb9e5aSRusty Russell extern int param_get_short(char *buffer, const struct kernel_param *kp); 2481da177e4SLinus Torvalds #define param_check_short(name, p) __param_check(name, p, short) 2491da177e4SLinus Torvalds 2509bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_ushort; 2519bbb9e5aSRusty Russell extern int param_set_ushort(const char *val, const struct kernel_param *kp); 2529bbb9e5aSRusty Russell extern int param_get_ushort(char *buffer, const struct kernel_param *kp); 2531da177e4SLinus Torvalds #define param_check_ushort(name, p) __param_check(name, p, unsigned short) 2541da177e4SLinus Torvalds 2559bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_int; 2569bbb9e5aSRusty Russell extern int param_set_int(const char *val, const struct kernel_param *kp); 2579bbb9e5aSRusty Russell extern int param_get_int(char *buffer, const struct kernel_param *kp); 2581da177e4SLinus Torvalds #define param_check_int(name, p) __param_check(name, p, int) 2591da177e4SLinus Torvalds 2609bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_uint; 2619bbb9e5aSRusty Russell extern int param_set_uint(const char *val, const struct kernel_param *kp); 2629bbb9e5aSRusty Russell extern int param_get_uint(char *buffer, const struct kernel_param *kp); 2631da177e4SLinus Torvalds #define param_check_uint(name, p) __param_check(name, p, unsigned int) 2641da177e4SLinus Torvalds 2659bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_long; 2669bbb9e5aSRusty Russell extern int param_set_long(const char *val, const struct kernel_param *kp); 2679bbb9e5aSRusty Russell extern int param_get_long(char *buffer, const struct kernel_param *kp); 2681da177e4SLinus Torvalds #define param_check_long(name, p) __param_check(name, p, long) 2691da177e4SLinus Torvalds 2709bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_ulong; 2719bbb9e5aSRusty Russell extern int param_set_ulong(const char *val, const struct kernel_param *kp); 2729bbb9e5aSRusty Russell extern int param_get_ulong(char *buffer, const struct kernel_param *kp); 2731da177e4SLinus Torvalds #define param_check_ulong(name, p) __param_check(name, p, unsigned long) 2741da177e4SLinus Torvalds 2759bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_charp; 2769bbb9e5aSRusty Russell extern int param_set_charp(const char *val, const struct kernel_param *kp); 2779bbb9e5aSRusty Russell extern int param_get_charp(char *buffer, const struct kernel_param *kp); 2781da177e4SLinus Torvalds #define param_check_charp(name, p) __param_check(name, p, char *) 2791da177e4SLinus Torvalds 280fddd5201SRusty Russell /* For historical reasons "bool" parameters can be (unsigned) "int". */ 2819bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_bool; 2829bbb9e5aSRusty Russell extern int param_set_bool(const char *val, const struct kernel_param *kp); 2839bbb9e5aSRusty Russell extern int param_get_bool(char *buffer, const struct kernel_param *kp); 284fddd5201SRusty Russell #define param_check_bool(name, p) \ 285fddd5201SRusty Russell static inline void __check_##name(void) \ 286fddd5201SRusty Russell { \ 287fddd5201SRusty Russell BUILD_BUG_ON(!__same_type(*(p), bool) && \ 288fddd5201SRusty Russell !__same_type(*(p), unsigned int) && \ 289fddd5201SRusty Russell !__same_type(*(p), int)); \ 290fddd5201SRusty Russell } 2911da177e4SLinus Torvalds 2929bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_invbool; 2939bbb9e5aSRusty Russell extern int param_set_invbool(const char *val, const struct kernel_param *kp); 2949bbb9e5aSRusty Russell extern int param_get_invbool(char *buffer, const struct kernel_param *kp); 2959a71af2cSRusty Russell #define param_check_invbool(name, p) __param_check(name, p, bool) 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* Comma-separated array: *nump is set to number they actually specified. */ 2981da177e4SLinus Torvalds #define module_param_array_named(name, array, type, nump, perm) \ 29922e48eafSJan Beulich static const struct kparam_array __param_arr_##name \ 3009bbb9e5aSRusty Russell = { ARRAY_SIZE(array), nump, ¶m_ops_##type, \ 3011da177e4SLinus Torvalds sizeof(array[0]), array }; \ 302fddd5201SRusty Russell __module_param_call(MODULE_PARAM_PREFIX, name, \ 3039bbb9e5aSRusty Russell ¶m_array_ops, \ 304fddd5201SRusty Russell .arr = &__param_arr_##name, \ 305fddd5201SRusty Russell __same_type(array[0], bool), perm); \ 3061da177e4SLinus Torvalds __MODULE_PARM_TYPE(name, "array of " #type) 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds #define module_param_array(name, type, nump, perm) \ 3091da177e4SLinus Torvalds module_param_array_named(name, name, type, nump, perm) 3101da177e4SLinus Torvalds 3119bbb9e5aSRusty Russell extern struct kernel_param_ops param_array_ops; 3121da177e4SLinus Torvalds 3139bbb9e5aSRusty Russell extern struct kernel_param_ops param_ops_string; 3149bbb9e5aSRusty Russell extern int param_set_copystring(const char *val, const struct kernel_param *); 3159bbb9e5aSRusty Russell extern int param_get_string(char *buffer, const struct kernel_param *kp); 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* for exporting parameters in /sys/parameters */ 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds struct module; 3201da177e4SLinus Torvalds 321ef665c1aSRandy Dunlap #if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES) 3221da177e4SLinus Torvalds extern int module_param_sysfs_setup(struct module *mod, 3239bbb9e5aSRusty Russell const struct kernel_param *kparam, 3241da177e4SLinus Torvalds unsigned int num_params); 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds extern void module_param_sysfs_remove(struct module *mod); 327ef665c1aSRandy Dunlap #else 328ef665c1aSRandy Dunlap static inline int module_param_sysfs_setup(struct module *mod, 3299bbb9e5aSRusty Russell const struct kernel_param *kparam, 330ef665c1aSRandy Dunlap unsigned int num_params) 331ef665c1aSRandy Dunlap { 332ef665c1aSRandy Dunlap return 0; 333ef665c1aSRandy Dunlap } 334ef665c1aSRandy Dunlap 335ef665c1aSRandy Dunlap static inline void module_param_sysfs_remove(struct module *mod) 336ef665c1aSRandy Dunlap { } 337ef665c1aSRandy Dunlap #endif 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds #endif /* _LINUX_MODULE_PARAMS_H */ 340