Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame^] | 1 | #ifndef _LINUX_MODULE_PARAMS_H |
| 2 | #define _LINUX_MODULE_PARAMS_H |
| 3 | /* (C) Copyright 2001, 2002 Rusty Russell IBM Corporation */ |
| 4 | #include <linux/init.h> |
| 5 | #include <linux/stringify.h> |
| 6 | #include <linux/kernel.h> |
| 7 | |
| 8 | #ifdef MODULE |
| 9 | #define MODULE_PARAM_PREFIX |
| 10 | #else |
| 11 | #define MODULE_PARAM_PREFIX KBUILD_MODNAME "." |
| 12 | #endif |
| 13 | |
| 14 | #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long)) |
| 15 | |
| 16 | #define ___module_cat(a,b) __mod_ ## a ## b |
| 17 | #define __module_cat(a,b) ___module_cat(a,b) |
| 18 | #ifdef MODULE |
| 19 | #define __MODULE_INFO(tag, name, info) \ |
| 20 | static const char __module_cat(name,__LINE__)[] \ |
| 21 | __used __attribute__((section(".modinfo"), unused, aligned(1))) \ |
| 22 | = __stringify(tag) "=" info |
| 23 | #else |
| 24 | #define __MODULE_INFO(tag, name, info) \ |
| 25 | struct __module_cat(name,__LINE__) {} |
| 26 | #endif |
| 27 | #define __MODULE_PARM_TYPE(name, _type) \ |
| 28 | __MODULE_INFO(parmtype, name##type, #name ":" _type) |
| 29 | |
| 30 | #define MODULE_PARM_DESC(_parm, desc) \ |
| 31 | __MODULE_INFO(parm, _parm, #_parm ":" desc) |
| 32 | |
| 33 | struct kernel_param; |
| 34 | |
| 35 | struct kernel_param_ops { |
| 36 | |
| 37 | int (*set)(const char *val, const struct kernel_param *kp); |
| 38 | /* Returns length written or -errno. Buffer is 4k (ie. be short!) */ |
| 39 | int (*get)(char *buffer, const struct kernel_param *kp); |
| 40 | |
| 41 | void (*free)(void *arg); |
| 42 | }; |
| 43 | |
| 44 | struct kernel_param { |
| 45 | const char *name; |
| 46 | const struct kernel_param_ops *ops; |
| 47 | u16 perm; |
| 48 | s16 level; |
| 49 | union { |
| 50 | void *arg; |
| 51 | const struct kparam_string *str; |
| 52 | const struct kparam_array *arr; |
| 53 | }; |
| 54 | }; |
| 55 | |
| 56 | struct kparam_string { |
| 57 | unsigned int maxlen; |
| 58 | char *string; |
| 59 | }; |
| 60 | |
| 61 | struct kparam_array |
| 62 | { |
| 63 | unsigned int max; |
| 64 | unsigned int elemsize; |
| 65 | unsigned int *num; |
| 66 | const struct kernel_param_ops *ops; |
| 67 | void *elem; |
| 68 | }; |
| 69 | |
| 70 | #define module_param(name, type, perm) \ |
| 71 | module_param_named(name, name, type, perm) |
| 72 | |
| 73 | #define module_param_named(name, value, type, perm) \ |
| 74 | param_check_##type(name, &(value)); \ |
| 75 | module_param_cb(name, ¶m_ops_##type, &value, perm); \ |
| 76 | __MODULE_PARM_TYPE(name, #type) |
| 77 | |
| 78 | #define module_param_cb(name, ops, arg, perm) \ |
| 79 | __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1) |
| 80 | |
| 81 | #define __level_param_cb(name, ops, arg, perm, level) \ |
| 82 | __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, level) |
| 83 | |
| 84 | #define core_param_cb(name, ops, arg, perm) \ |
| 85 | __level_param_cb(name, ops, arg, perm, 1) |
| 86 | |
| 87 | #define postcore_param_cb(name, ops, arg, perm) \ |
| 88 | __level_param_cb(name, ops, arg, perm, 2) |
| 89 | |
| 90 | #define arch_param_cb(name, ops, arg, perm) \ |
| 91 | __level_param_cb(name, ops, arg, perm, 3) |
| 92 | |
| 93 | #define subsys_param_cb(name, ops, arg, perm) \ |
| 94 | __level_param_cb(name, ops, arg, perm, 4) |
| 95 | |
| 96 | #define fs_param_cb(name, ops, arg, perm) \ |
| 97 | __level_param_cb(name, ops, arg, perm, 5) |
| 98 | |
| 99 | #define device_param_cb(name, ops, arg, perm) \ |
| 100 | __level_param_cb(name, ops, arg, perm, 6) |
| 101 | |
| 102 | #define late_param_cb(name, ops, arg, perm) \ |
| 103 | __level_param_cb(name, ops, arg, perm, 7) |
| 104 | |
| 105 | #if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64) |
| 106 | #define __moduleparam_const |
| 107 | #else |
| 108 | #define __moduleparam_const const |
| 109 | #endif |
| 110 | |
| 111 | #define __module_param_call(prefix, name, ops, arg, perm, level) \ |
| 112 | \ |
| 113 | static int __param_perm_check_##name __attribute__((unused)) = \ |
| 114 | BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)) \ |
| 115 | + BUILD_BUG_ON_ZERO(sizeof(""prefix) > MAX_PARAM_PREFIX_LEN); \ |
| 116 | static const char __param_str_##name[] = prefix #name; \ |
| 117 | static struct kernel_param __moduleparam_const __param_##name \ |
| 118 | __used \ |
| 119 | __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \ |
| 120 | = { __param_str_##name, ops, perm, level, { arg } } |
| 121 | |
| 122 | #define module_param_call(name, set, get, arg, perm) \ |
| 123 | static struct kernel_param_ops __param_ops_##name = \ |
| 124 | { (void *)set, (void *)get }; \ |
| 125 | __module_param_call(MODULE_PARAM_PREFIX, \ |
| 126 | name, &__param_ops_##name, arg, \ |
| 127 | (perm) + sizeof(__check_old_set_param(set))*0, -1) |
| 128 | |
| 129 | static inline int |
| 130 | __check_old_set_param(int (*oldset)(const char *, struct kernel_param *)) |
| 131 | { |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * kparam_block_sysfs_write - make sure a parameter isn't written via sysfs. |
| 137 | * @name: the name of the parameter |
| 138 | * |
| 139 | * There's no point blocking write on a paramter that isn't writable via sysfs! |
| 140 | */ |
| 141 | #define kparam_block_sysfs_write(name) \ |
| 142 | do { \ |
| 143 | BUG_ON(!(__param_##name.perm & 0222)); \ |
| 144 | __kernel_param_lock(); \ |
| 145 | } while (0) |
| 146 | |
| 147 | #define kparam_unblock_sysfs_write(name) \ |
| 148 | do { \ |
| 149 | BUG_ON(!(__param_##name.perm & 0222)); \ |
| 150 | __kernel_param_unlock(); \ |
| 151 | } while (0) |
| 152 | |
| 153 | #define kparam_block_sysfs_read(name) \ |
| 154 | do { \ |
| 155 | BUG_ON(!(__param_##name.perm & 0444)); \ |
| 156 | __kernel_param_lock(); \ |
| 157 | } while (0) |
| 158 | |
| 159 | #define kparam_unblock_sysfs_read(name) \ |
| 160 | do { \ |
| 161 | BUG_ON(!(__param_##name.perm & 0444)); \ |
| 162 | __kernel_param_unlock(); \ |
| 163 | } while (0) |
| 164 | |
| 165 | #ifdef CONFIG_SYSFS |
| 166 | extern void __kernel_param_lock(void); |
| 167 | extern void __kernel_param_unlock(void); |
| 168 | #else |
| 169 | static inline void __kernel_param_lock(void) |
| 170 | { |
| 171 | } |
| 172 | static inline void __kernel_param_unlock(void) |
| 173 | { |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | #ifndef MODULE |
| 178 | #define core_param(name, var, type, perm) \ |
| 179 | param_check_##type(name, &(var)); \ |
| 180 | __module_param_call("", name, ¶m_ops_##type, &var, perm, -1) |
| 181 | #endif |
| 182 | |
| 183 | #define module_param_string(name, string, len, perm) \ |
| 184 | static const struct kparam_string __param_string_##name \ |
| 185 | = { len, string }; \ |
| 186 | __module_param_call(MODULE_PARAM_PREFIX, name, \ |
| 187 | ¶m_ops_string, \ |
| 188 | .str = &__param_string_##name, perm, -1); \ |
| 189 | __MODULE_PARM_TYPE(name, "string") |
| 190 | |
| 191 | extern bool parameq(const char *name1, const char *name2); |
| 192 | |
| 193 | extern bool parameqn(const char *name1, const char *name2, size_t n); |
| 194 | |
| 195 | extern int parse_args(const char *name, |
| 196 | char *args, |
| 197 | const struct kernel_param *params, |
| 198 | unsigned num, |
| 199 | s16 level_min, |
| 200 | s16 level_max, |
| 201 | int (*unknown)(char *param, char *val)); |
| 202 | |
| 203 | #ifdef CONFIG_SYSFS |
| 204 | extern void destroy_params(const struct kernel_param *params, unsigned num); |
| 205 | #else |
| 206 | static inline void destroy_params(const struct kernel_param *params, |
| 207 | unsigned num) |
| 208 | { |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | #define __param_check(name, p, type) \ |
| 213 | static inline type *__check_##name(void) { return(p); } |
| 214 | |
| 215 | extern struct kernel_param_ops param_ops_byte; |
| 216 | extern int param_set_byte(const char *val, const struct kernel_param *kp); |
| 217 | extern int param_get_byte(char *buffer, const struct kernel_param *kp); |
| 218 | #define param_check_byte(name, p) __param_check(name, p, unsigned char) |
| 219 | |
| 220 | extern struct kernel_param_ops param_ops_short; |
| 221 | extern int param_set_short(const char *val, const struct kernel_param *kp); |
| 222 | extern int param_get_short(char *buffer, const struct kernel_param *kp); |
| 223 | #define param_check_short(name, p) __param_check(name, p, short) |
| 224 | |
| 225 | extern struct kernel_param_ops param_ops_ushort; |
| 226 | extern int param_set_ushort(const char *val, const struct kernel_param *kp); |
| 227 | extern int param_get_ushort(char *buffer, const struct kernel_param *kp); |
| 228 | #define param_check_ushort(name, p) __param_check(name, p, unsigned short) |
| 229 | |
| 230 | extern struct kernel_param_ops param_ops_int; |
| 231 | extern int param_set_int(const char *val, const struct kernel_param *kp); |
| 232 | extern int param_get_int(char *buffer, const struct kernel_param *kp); |
| 233 | #define param_check_int(name, p) __param_check(name, p, int) |
| 234 | |
| 235 | extern struct kernel_param_ops param_ops_uint; |
| 236 | extern int param_set_uint(const char *val, const struct kernel_param *kp); |
| 237 | extern int param_get_uint(char *buffer, const struct kernel_param *kp); |
| 238 | #define param_check_uint(name, p) __param_check(name, p, unsigned int) |
| 239 | |
| 240 | extern struct kernel_param_ops param_ops_long; |
| 241 | extern int param_set_long(const char *val, const struct kernel_param *kp); |
| 242 | extern int param_get_long(char *buffer, const struct kernel_param *kp); |
| 243 | #define param_check_long(name, p) __param_check(name, p, long) |
| 244 | |
| 245 | extern struct kernel_param_ops param_ops_ulong; |
| 246 | extern int param_set_ulong(const char *val, const struct kernel_param *kp); |
| 247 | extern int param_get_ulong(char *buffer, const struct kernel_param *kp); |
| 248 | #define param_check_ulong(name, p) __param_check(name, p, unsigned long) |
| 249 | |
| 250 | extern struct kernel_param_ops param_ops_charp; |
| 251 | extern int param_set_charp(const char *val, const struct kernel_param *kp); |
| 252 | extern int param_get_charp(char *buffer, const struct kernel_param *kp); |
| 253 | #define param_check_charp(name, p) __param_check(name, p, char *) |
| 254 | |
| 255 | extern struct kernel_param_ops param_ops_bool; |
| 256 | extern int param_set_bool(const char *val, const struct kernel_param *kp); |
| 257 | extern int param_get_bool(char *buffer, const struct kernel_param *kp); |
| 258 | #define param_check_bool(name, p) __param_check(name, p, bool) |
| 259 | |
| 260 | extern struct kernel_param_ops param_ops_invbool; |
| 261 | extern int param_set_invbool(const char *val, const struct kernel_param *kp); |
| 262 | extern int param_get_invbool(char *buffer, const struct kernel_param *kp); |
| 263 | #define param_check_invbool(name, p) __param_check(name, p, bool) |
| 264 | |
| 265 | extern struct kernel_param_ops param_ops_bint; |
| 266 | extern int param_set_bint(const char *val, const struct kernel_param *kp); |
| 267 | #define param_get_bint param_get_int |
| 268 | #define param_check_bint param_check_int |
| 269 | |
| 270 | /** |
| 271 | * module_param_array - a parameter which is an array of some type |
| 272 | * @name: the name of the array variable |
| 273 | * @type: the type, as per module_param() |
| 274 | * @nump: optional pointer filled in with the number written |
| 275 | * @perm: visibility in sysfs |
| 276 | * |
| 277 | * Input and output are as comma-separated values. Commas inside values |
| 278 | * don't work properly (eg. an array of charp). |
| 279 | * |
| 280 | * ARRAY_SIZE(@name) is used to determine the number of elements in the |
| 281 | * array, so the definition must be visible. |
| 282 | */ |
| 283 | #define module_param_array(name, type, nump, perm) \ |
| 284 | module_param_array_named(name, name, type, nump, perm) |
| 285 | |
| 286 | /** |
| 287 | * module_param_array_named - renamed parameter which is an array of some type |
| 288 | * @name: a valid C identifier which is the parameter name |
| 289 | * @array: the name of the array variable |
| 290 | * @type: the type, as per module_param() |
| 291 | * @nump: optional pointer filled in with the number written |
| 292 | * @perm: visibility in sysfs |
| 293 | * |
| 294 | * This exposes a different name than the actual variable name. See |
| 295 | * module_param_named() for why this might be necessary. |
| 296 | */ |
| 297 | #define module_param_array_named(name, array, type, nump, perm) \ |
| 298 | param_check_##type(name, &(array)[0]); \ |
| 299 | static const struct kparam_array __param_arr_##name \ |
| 300 | = { .max = ARRAY_SIZE(array), .num = nump, \ |
| 301 | .ops = ¶m_ops_##type, \ |
| 302 | .elemsize = sizeof(array[0]), .elem = array }; \ |
| 303 | __module_param_call(MODULE_PARAM_PREFIX, name, \ |
| 304 | ¶m_array_ops, \ |
| 305 | .arr = &__param_arr_##name, \ |
| 306 | perm, -1); \ |
| 307 | __MODULE_PARM_TYPE(name, "array of " #type) |
| 308 | |
| 309 | extern struct kernel_param_ops param_array_ops; |
| 310 | |
| 311 | extern struct kernel_param_ops param_ops_string; |
| 312 | extern int param_set_copystring(const char *val, const struct kernel_param *); |
| 313 | extern int param_get_string(char *buffer, const struct kernel_param *kp); |
| 314 | |
| 315 | |
| 316 | struct module; |
| 317 | |
| 318 | #if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES) |
| 319 | extern int module_param_sysfs_setup(struct module *mod, |
| 320 | const struct kernel_param *kparam, |
| 321 | unsigned int num_params); |
| 322 | |
| 323 | extern void module_param_sysfs_remove(struct module *mod); |
| 324 | #else |
| 325 | static inline int module_param_sysfs_setup(struct module *mod, |
| 326 | const struct kernel_param *kparam, |
| 327 | unsigned int num_params) |
| 328 | { |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static inline void module_param_sysfs_remove(struct module *mod) |
| 333 | { } |
| 334 | #endif |
| 335 | |
| 336 | #endif |