Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
moduleparam.h
Go to the documentation of this file.
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 /* You can override this manually, but generally this should match the
9  module name. */
10 #ifdef MODULE
11 #define MODULE_PARAM_PREFIX /* empty */
12 #else
13 #define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
14 #endif
15 
16 /* Chosen so that structs with an unsigned long line up. */
17 #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
18 
19 #define ___module_cat(a,b) __mod_ ## a ## b
20 #define __module_cat(a,b) ___module_cat(a,b)
21 #ifdef MODULE
22 #define __MODULE_INFO(tag, name, info) \
23 static const char __module_cat(name,__LINE__)[] \
24  __used __attribute__((section(".modinfo"), unused, aligned(1))) \
25  = __stringify(tag) "=" info
26 #else /* !MODULE */
27 /* This struct is here for syntactic coherency, it is not used */
28 #define __MODULE_INFO(tag, name, info) \
29  struct __module_cat(name,__LINE__) {}
30 #endif
31 #define __MODULE_PARM_TYPE(name, _type) \
32  __MODULE_INFO(parmtype, name##type, #name ":" _type)
33 
34 /* One for each parameter, describing how to use it. Some files do
35  multiple of these per line, so can't just use MODULE_INFO. */
36 #define MODULE_PARM_DESC(_parm, desc) \
37  __MODULE_INFO(parm, _parm, #_parm ":" desc)
38 
39 struct kernel_param;
40 
42  /* Returns 0, or -errno. arg is in kp->arg. */
43  int (*set)(const char *val, const struct kernel_param *kp);
44  /* Returns length written or -errno. Buffer is 4k (ie. be short!) */
45  int (*get)(char *buffer, const struct kernel_param *kp);
46  /* Optional function to free kp->arg when module unloaded. */
47  void (*free)(void *arg);
48 };
49 
50 struct kernel_param {
51  const char *name;
52  const struct kernel_param_ops *ops;
55  union {
56  void *arg;
57  const struct kparam_string *str;
58  const struct kparam_array *arr;
59  };
60 };
61 
62 /* Special one for strings we want to copy into */
63 struct kparam_string {
64  unsigned int maxlen;
65  char *string;
66 };
67 
68 /* Special one for arrays */
70 {
71  unsigned int max;
72  unsigned int elemsize;
73  unsigned int *num;
74  const struct kernel_param_ops *ops;
75  void *elem;
76 };
77 
103 #define module_param(name, type, perm) \
104  module_param_named(name, name, type, perm)
105 
117 #define module_param_named(name, value, type, perm) \
118  param_check_##type(name, &(value)); \
119  module_param_cb(name, &param_ops_##type, &value, perm); \
120  __MODULE_PARM_TYPE(name, #type)
121 
130 #define module_param_cb(name, ops, arg, perm) \
131  __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1)
132 
142 #define __level_param_cb(name, ops, arg, perm, level) \
143  __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, level)
144 
145 #define core_param_cb(name, ops, arg, perm) \
146  __level_param_cb(name, ops, arg, perm, 1)
147 
148 #define postcore_param_cb(name, ops, arg, perm) \
149  __level_param_cb(name, ops, arg, perm, 2)
150 
151 #define arch_param_cb(name, ops, arg, perm) \
152  __level_param_cb(name, ops, arg, perm, 3)
153 
154 #define subsys_param_cb(name, ops, arg, perm) \
155  __level_param_cb(name, ops, arg, perm, 4)
156 
157 #define fs_param_cb(name, ops, arg, perm) \
158  __level_param_cb(name, ops, arg, perm, 5)
159 
160 #define device_param_cb(name, ops, arg, perm) \
161  __level_param_cb(name, ops, arg, perm, 6)
162 
163 #define late_param_cb(name, ops, arg, perm) \
164  __level_param_cb(name, ops, arg, perm, 7)
165 
166 /* On alpha, ia64 and ppc64 relocations to global data cannot go into
167  read-only sections (which is part of respective UNIX ABI on these
168  platforms). So 'const' makes no sense and even causes compile failures
169  with some compilers. */
170 #if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64)
171 #define __moduleparam_const
172 #else
173 #define __moduleparam_const const
174 #endif
175 
176 /* This is the fundamental function for registering boot/module
177  parameters. */
178 #define __module_param_call(prefix, name, ops, arg, perm, level) \
179  /* Default value instead of permissions? */ \
180  static int __param_perm_check_##name __attribute__((unused)) = \
181  BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)) \
182  + BUILD_BUG_ON_ZERO(sizeof(""prefix) > MAX_PARAM_PREFIX_LEN); \
183  static const char __param_str_##name[] = prefix #name; \
184  static struct kernel_param __moduleparam_const __param_##name \
185  __used \
186  __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
187  = { __param_str_##name, ops, perm, level, { arg } }
188 
189 /* Obsolete - use module_param_cb() */
190 #define module_param_call(name, set, get, arg, perm) \
191  static struct kernel_param_ops __param_ops_##name = \
192  { (void *)set, (void *)get }; \
193  __module_param_call(MODULE_PARAM_PREFIX, \
194  name, &__param_ops_##name, arg, \
195  (perm) + sizeof(__check_old_set_param(set))*0, -1)
196 
197 /* We don't get oldget: it's often a new-style param_get_uint, etc. */
198 static inline int
199 __check_old_set_param(int (*oldset)(const char *, struct kernel_param *))
200 {
201  return 0;
202 }
203 
210 #define kparam_block_sysfs_write(name) \
211  do { \
212  BUG_ON(!(__param_##name.perm & 0222)); \
213  __kernel_param_lock(); \
214  } while (0)
215 
220 #define kparam_unblock_sysfs_write(name) \
221  do { \
222  BUG_ON(!(__param_##name.perm & 0222)); \
223  __kernel_param_unlock(); \
224  } while (0)
225 
232 #define kparam_block_sysfs_read(name) \
233  do { \
234  BUG_ON(!(__param_##name.perm & 0444)); \
235  __kernel_param_lock(); \
236  } while (0)
237 
242 #define kparam_unblock_sysfs_read(name) \
243  do { \
244  BUG_ON(!(__param_##name.perm & 0444)); \
245  __kernel_param_unlock(); \
246  } while (0)
247 
248 #ifdef CONFIG_SYSFS
249 extern void __kernel_param_lock(void);
250 extern void __kernel_param_unlock(void);
251 #else
252 static inline void __kernel_param_lock(void)
253 {
254 }
255 static inline void __kernel_param_unlock(void)
256 {
257 }
258 #endif
259 
260 #ifndef MODULE
261 
273 #define core_param(name, var, type, perm) \
274  param_check_##type(name, &(var)); \
275  __module_param_call("", name, &param_ops_##type, &var, perm, -1)
276 #endif /* !MODULE */
277 
288 #define module_param_string(name, string, len, perm) \
289  static const struct kparam_string __param_string_##name \
290  = { len, string }; \
291  __module_param_call(MODULE_PARAM_PREFIX, name, \
292  &param_ops_string, \
293  .str = &__param_string_##name, perm, -1); \
294  __MODULE_PARM_TYPE(name, "string")
295 
304 extern bool parameq(const char *name1, const char *name2);
305 
314 extern bool parameqn(const char *name1, const char *name2, size_t n);
315 
316 /* Called on module insert or kernel boot */
317 extern int parse_args(const char *name,
318  char *args,
319  const struct kernel_param *params,
320  unsigned num,
321  s16 level_min,
322  s16 level_max,
323  int (*unknown)(char *param, char *val,
324  const char *doing));
325 
326 /* Called by module remove. */
327 #ifdef CONFIG_SYSFS
328 extern void destroy_params(const struct kernel_param *params, unsigned num);
329 #else
330 static inline void destroy_params(const struct kernel_param *params,
331  unsigned num)
332 {
333 }
334 #endif /* !CONFIG_SYSFS */
335 
336 /* All the helper functions */
337 /* The macros to do compile-time type checking stolen from Jakub
338  Jelinek, who IIRC came up with this idea for the 2.4 module init code. */
339 #define __param_check(name, p, type) \
340  static inline type *__check_##name(void) { return(p); }
341 
342 extern struct kernel_param_ops param_ops_byte;
343 extern int param_set_byte(const char *val, const struct kernel_param *kp);
344 extern int param_get_byte(char *buffer, const struct kernel_param *kp);
345 #define param_check_byte(name, p) __param_check(name, p, unsigned char)
346 
347 extern struct kernel_param_ops param_ops_short;
348 extern int param_set_short(const char *val, const struct kernel_param *kp);
349 extern int param_get_short(char *buffer, const struct kernel_param *kp);
350 #define param_check_short(name, p) __param_check(name, p, short)
351 
352 extern struct kernel_param_ops param_ops_ushort;
353 extern int param_set_ushort(const char *val, const struct kernel_param *kp);
354 extern int param_get_ushort(char *buffer, const struct kernel_param *kp);
355 #define param_check_ushort(name, p) __param_check(name, p, unsigned short)
356 
357 extern struct kernel_param_ops param_ops_int;
358 extern int param_set_int(const char *val, const struct kernel_param *kp);
359 extern int param_get_int(char *buffer, const struct kernel_param *kp);
360 #define param_check_int(name, p) __param_check(name, p, int)
361 
362 extern struct kernel_param_ops param_ops_uint;
363 extern int param_set_uint(const char *val, const struct kernel_param *kp);
364 extern int param_get_uint(char *buffer, const struct kernel_param *kp);
365 #define param_check_uint(name, p) __param_check(name, p, unsigned int)
366 
367 extern struct kernel_param_ops param_ops_long;
368 extern int param_set_long(const char *val, const struct kernel_param *kp);
369 extern int param_get_long(char *buffer, const struct kernel_param *kp);
370 #define param_check_long(name, p) __param_check(name, p, long)
371 
372 extern struct kernel_param_ops param_ops_ulong;
373 extern int param_set_ulong(const char *val, const struct kernel_param *kp);
374 extern int param_get_ulong(char *buffer, const struct kernel_param *kp);
375 #define param_check_ulong(name, p) __param_check(name, p, unsigned long)
376 
377 extern struct kernel_param_ops param_ops_charp;
378 extern int param_set_charp(const char *val, const struct kernel_param *kp);
379 extern int param_get_charp(char *buffer, const struct kernel_param *kp);
380 #define param_check_charp(name, p) __param_check(name, p, char *)
381 
382 /* We used to allow int as well as bool. We're taking that away! */
383 extern struct kernel_param_ops param_ops_bool;
384 extern int param_set_bool(const char *val, const struct kernel_param *kp);
385 extern int param_get_bool(char *buffer, const struct kernel_param *kp);
386 #define param_check_bool(name, p) __param_check(name, p, bool)
387 
388 extern struct kernel_param_ops param_ops_invbool;
389 extern int param_set_invbool(const char *val, const struct kernel_param *kp);
390 extern int param_get_invbool(char *buffer, const struct kernel_param *kp);
391 #define param_check_invbool(name, p) __param_check(name, p, bool)
392 
393 /* An int, which can only be set like a bool (though it shows as an int). */
394 extern struct kernel_param_ops param_ops_bint;
395 extern int param_set_bint(const char *val, const struct kernel_param *kp);
396 #define param_get_bint param_get_int
397 #define param_check_bint param_check_int
398 
412 #define module_param_array(name, type, nump, perm) \
413  module_param_array_named(name, name, type, nump, perm)
414 
426 #define module_param_array_named(name, array, type, nump, perm) \
427  param_check_##type(name, &(array)[0]); \
428  static const struct kparam_array __param_arr_##name \
429  = { .max = ARRAY_SIZE(array), .num = nump, \
430  .ops = &param_ops_##type, \
431  .elemsize = sizeof(array[0]), .elem = array }; \
432  __module_param_call(MODULE_PARAM_PREFIX, name, \
433  &param_array_ops, \
434  .arr = &__param_arr_##name, \
435  perm, -1); \
436  __MODULE_PARM_TYPE(name, "array of " #type)
437 
438 extern struct kernel_param_ops param_array_ops;
439 
440 extern struct kernel_param_ops param_ops_string;
441 extern int param_set_copystring(const char *val, const struct kernel_param *);
442 extern int param_get_string(char *buffer, const struct kernel_param *kp);
443 
444 /* for exporting parameters in /sys/parameters */
445 
446 struct module;
447 
448 #if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
449 extern int module_param_sysfs_setup(struct module *mod,
450  const struct kernel_param *kparam,
451  unsigned int num_params);
452 
453 extern void module_param_sysfs_remove(struct module *mod);
454 #else
455 static inline int module_param_sysfs_setup(struct module *mod,
456  const struct kernel_param *kparam,
457  unsigned int num_params)
458 {
459  return 0;
460 }
461 
462 static inline void module_param_sysfs_remove(struct module *mod)
463 { }
464 #endif
465 
466 #endif /* _LINUX_MODULE_PARAMS_H */