moduleparam.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. /* You can override this manually, but generally this should match the
  8. module name. */
  9. #ifdef MODULE
  10. #define MODULE_PARAM_PREFIX /* empty */
  11. #else
  12. #define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
  13. #endif
  14. /* Chosen so that structs with an unsigned long line up. */
  15. #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
  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 /* !MODULE */
  24. /* This struct is here for syntactic coherency, it is not used */
  25. #define __MODULE_INFO(tag, name, info) \
  26. struct __module_cat(name,__LINE__) {}
  27. #endif
  28. #define __MODULE_PARM_TYPE(name, _type) \
  29. __MODULE_INFO(parmtype, name##type, #name ":" _type)
  30. struct kernel_param;
  31. struct kernel_param_ops {
  32. /* Returns 0, or -errno. arg is in kp->arg. */
  33. int (*set)(const char *val, const struct kernel_param *kp);
  34. /* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  35. int (*get)(char *buffer, const struct kernel_param *kp);
  36. /* Optional function to free kp->arg when module unloaded. */
  37. void (*free)(void *arg);
  38. };
  39. /* Flag bits for kernel_param.flags */
  40. #define KPARAM_ISBOOL 2
  41. struct kernel_param {
  42. const char *name;
  43. const struct kernel_param_ops *ops;
  44. u16 perm;
  45. u16 flags;
  46. union {
  47. void *arg;
  48. const struct kparam_string *str;
  49. const struct kparam_array *arr;
  50. };
  51. };
  52. /* Special one for strings we want to copy into */
  53. struct kparam_string {
  54. unsigned int maxlen;
  55. char *string;
  56. };
  57. /* Special one for arrays */
  58. struct kparam_array
  59. {
  60. unsigned int max;
  61. unsigned int *num;
  62. const struct kernel_param_ops *ops;
  63. unsigned int elemsize;
  64. void *elem;
  65. };
  66. /**
  67. * module_param - typesafe helper for a module/cmdline parameter
  68. * @value: the variable to alter, and exposed parameter name.
  69. * @type: the type of the parameter
  70. * @perm: visibility in sysfs.
  71. *
  72. * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
  73. * ".") the kernel commandline parameter. Note that - is changed to _, so
  74. * the user can use "foo-bar=1" even for variable "foo_bar".
  75. *
  76. * @perm is 0 if the the variable is not to appear in sysfs, or 0444
  77. * for world-readable, 0644 for root-writable, etc. Note that if it
  78. * is writable, you may need to use kparam_block_sysfs_write() around
  79. * accesses (esp. charp, which can be kfreed when it changes).
  80. *
  81. * The @type is simply pasted to refer to a param_ops_##type and a
  82. * param_check_##type: for convenience many standard types are provided but
  83. * you can create your own by defining those variables.
  84. *
  85. * Standard types are:
  86. * byte, short, ushort, int, uint, long, ulong
  87. * charp: a character pointer
  88. * bool: a bool, values 0/1, y/n, Y/N.
  89. * invbool: the above, only sense-reversed (N = true).
  90. */
  91. #define module_param(name, type, perm) \
  92. module_param_named(name, name, type, perm)
  93. /**
  94. * module_param_named - typesafe helper for a renamed module/cmdline parameter
  95. * @name: a valid C identifier which is the parameter name.
  96. * @value: the actual lvalue to alter.
  97. * @type: the type of the parameter
  98. * @perm: visibility in sysfs.
  99. *
  100. * Usually it's a good idea to have variable names and user-exposed names the
  101. * same, but that's harder if the variable must be non-static or is inside a
  102. * structure. This allows exposure under a different name.
  103. */
  104. #define module_param_named(name, value, type, perm) \
  105. param_check_##type(name, &(value)); \
  106. module_param_cb(name, &param_ops_##type, &value, perm); \
  107. __MODULE_PARM_TYPE(name, #type)
  108. /**
  109. * module_param_cb - general callback for a module/cmdline parameter
  110. * @name: a valid C identifier which is the parameter name.
  111. * @ops: the set & get operations for this parameter.
  112. * @perm: visibility in sysfs.
  113. *
  114. * The ops can have NULL set or get functions.
  115. */
  116. #define module_param_cb(name, ops, arg, perm) \
  117. __module_param_call(MODULE_PARAM_PREFIX, \
  118. name, ops, arg, __same_type((arg), bool *), perm)
  119. /* On alpha, ia64 and ppc64 relocations to global data cannot go into
  120. read-only sections (which is part of respective UNIX ABI on these
  121. platforms). So 'const' makes no sense and even causes compile failures
  122. with some compilers. */
  123. #if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64)
  124. #define __moduleparam_const
  125. #else
  126. #define __moduleparam_const const
  127. #endif
  128. /* This is the fundamental function for registering boot/module
  129. parameters. */
  130. #define __module_param_call(prefix, name, ops, arg, isbool, perm) \
  131. /* Default value instead of permissions? */ \
  132. static int __param_perm_check_##name __attribute__((unused)) = \
  133. BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)) \
  134. + BUILD_BUG_ON_ZERO(sizeof(""prefix) > MAX_PARAM_PREFIX_LEN); \
  135. static const char __param_str_##name[] = prefix #name; \
  136. static struct kernel_param __moduleparam_const __param_##name \
  137. __used \
  138. __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
  139. = { __param_str_##name, ops, perm, isbool ? KPARAM_ISBOOL : 0, \
  140. { arg } }
  141. /* Obsolete - use module_param_cb() */
  142. #define module_param_call(name, set, get, arg, perm) \
  143. static struct kernel_param_ops __param_ops_##name = \
  144. { (void *)set, (void *)get }; \
  145. __module_param_call(MODULE_PARAM_PREFIX, \
  146. name, &__param_ops_##name, arg, \
  147. __same_type(arg, bool *), \
  148. (perm) + sizeof(__check_old_set_param(set))*0)
  149. /* We don't get oldget: it's often a new-style param_get_uint, etc. */
  150. static inline int
  151. __check_old_set_param(int (*oldset)(const char *, struct kernel_param *))
  152. {
  153. return 0;
  154. }
  155. /**
  156. * kparam_block_sysfs_write - make sure a parameter isn't written via sysfs.
  157. * @name: the name of the parameter
  158. *
  159. * There's no point blocking write on a paramter that isn't writable via sysfs!
  160. */
  161. #define kparam_block_sysfs_write(name) \
  162. do { \
  163. BUG_ON(!(__param_##name.perm & 0222)); \
  164. __kernel_param_lock(); \
  165. } while (0)
  166. /**
  167. * kparam_unblock_sysfs_write - allows sysfs to write to a parameter again.
  168. * @name: the name of the parameter
  169. */
  170. #define kparam_unblock_sysfs_write(name) \
  171. do { \
  172. BUG_ON(!(__param_##name.perm & 0222)); \
  173. __kernel_param_unlock(); \
  174. } while (0)
  175. /**
  176. * kparam_block_sysfs_read - make sure a parameter isn't read via sysfs.
  177. * @name: the name of the parameter
  178. *
  179. * This also blocks sysfs writes.
  180. */
  181. #define kparam_block_sysfs_read(name) \
  182. do { \
  183. BUG_ON(!(__param_##name.perm & 0444)); \
  184. __kernel_param_lock(); \
  185. } while (0)
  186. /**
  187. * kparam_unblock_sysfs_read - allows sysfs to read a parameter again.
  188. * @name: the name of the parameter
  189. */
  190. #define kparam_unblock_sysfs_read(name) \
  191. do { \
  192. BUG_ON(!(__param_##name.perm & 0444)); \
  193. __kernel_param_unlock(); \
  194. } while (0)
  195. #ifdef CONFIG_SYSFS
  196. extern void __kernel_param_lock(void);
  197. extern void __kernel_param_unlock(void);
  198. #else
  199. static inline void __kernel_param_lock(void)
  200. {
  201. }
  202. static inline void __kernel_param_unlock(void)
  203. {
  204. }
  205. #endif
  206. #ifndef MODULE
  207. /**
  208. * core_param - define a historical core kernel parameter.
  209. * @name: the name of the cmdline and sysfs parameter (often the same as var)
  210. * @var: the variable
  211. * @type: the type of the parameter
  212. * @perm: visibility in sysfs
  213. *
  214. * core_param is just like module_param(), but cannot be modular and
  215. * doesn't add a prefix (such as "printk."). This is for compatibility
  216. * with __setup(), and it makes sense as truly core parameters aren't
  217. * tied to the particular file they're in.
  218. */
  219. #define core_param(name, var, type, perm) \
  220. param_check_##type(name, &(var)); \
  221. __module_param_call("", name, &param_ops_##type, \
  222. &var, __same_type(var, bool), perm)
  223. #endif /* !MODULE */
  224. /**
  225. * module_param_string - a char array parameter
  226. * @name: the name of the parameter
  227. * @string: the string variable
  228. * @len: the maximum length of the string, incl. terminator
  229. * @perm: visibility in sysfs.
  230. *
  231. * This actually copies the string when it's set (unlike type charp).
  232. * @len is usually just sizeof(string).
  233. */
  234. #define module_param_string(name, string, len, perm) \
  235. static const struct kparam_string __param_string_##name \
  236. = { len, string }; \
  237. __module_param_call(MODULE_PARAM_PREFIX, name, \
  238. &param_ops_string, \
  239. .str = &__param_string_##name, 0, perm); \
  240. __MODULE_PARM_TYPE(name, "string")
  241. /* Called on module insert or kernel boot */
  242. extern int parse_args(const char *name,
  243. char *args,
  244. const struct kernel_param *params,
  245. unsigned num,
  246. int (*unknown)(char *param, char *val));
  247. /* Called by module remove. */
  248. #ifdef CONFIG_SYSFS
  249. extern void destroy_params(const struct kernel_param *params, unsigned num);
  250. #else
  251. static inline void destroy_params(const struct kernel_param *params,
  252. unsigned num)
  253. {
  254. }
  255. #endif /* !CONFIG_SYSFS */
  256. /* All the helper functions */
  257. /* The macros to do compile-time type checking stolen from Jakub
  258. Jelinek, who IIRC came up with this idea for the 2.4 module init code. */
  259. #define __param_check(name, p, type) \
  260. static inline type *__check_##name(void) { return(p); }
  261. extern struct kernel_param_ops param_ops_byte;
  262. extern int param_set_byte(const char *val, const struct kernel_param *kp);
  263. extern int param_get_byte(char *buffer, const struct kernel_param *kp);
  264. #define param_check_byte(name, p) __param_check(name, p, unsigned char)
  265. extern struct kernel_param_ops param_ops_short;
  266. extern int param_set_short(const char *val, const struct kernel_param *kp);
  267. extern int param_get_short(char *buffer, const struct kernel_param *kp);
  268. #define param_check_short(name, p) __param_check(name, p, short)
  269. extern struct kernel_param_ops param_ops_ushort;
  270. extern int param_set_ushort(const char *val, const struct kernel_param *kp);
  271. extern int param_get_ushort(char *buffer, const struct kernel_param *kp);
  272. #define param_check_ushort(name, p) __param_check(name, p, unsigned short)
  273. extern struct kernel_param_ops param_ops_int;
  274. extern int param_set_int(const char *val, const struct kernel_param *kp);
  275. extern int param_get_int(char *buffer, const struct kernel_param *kp);
  276. #define param_check_int(name, p) __param_check(name, p, int)
  277. extern struct kernel_param_ops param_ops_uint;
  278. extern int param_set_uint(const char *val, const struct kernel_param *kp);
  279. extern int param_get_uint(char *buffer, const struct kernel_param *kp);
  280. #define param_check_uint(name, p) __param_check(name, p, unsigned int)
  281. extern struct kernel_param_ops param_ops_long;
  282. extern int param_set_long(const char *val, const struct kernel_param *kp);
  283. extern int param_get_long(char *buffer, const struct kernel_param *kp);
  284. #define param_check_long(name, p) __param_check(name, p, long)
  285. extern struct kernel_param_ops param_ops_ulong;
  286. extern int param_set_ulong(const char *val, const struct kernel_param *kp);
  287. extern int param_get_ulong(char *buffer, const struct kernel_param *kp);
  288. #define param_check_ulong(name, p) __param_check(name, p, unsigned long)
  289. extern struct kernel_param_ops param_ops_charp;
  290. extern int param_set_charp(const char *val, const struct kernel_param *kp);
  291. extern int param_get_charp(char *buffer, const struct kernel_param *kp);
  292. #define param_check_charp(name, p) __param_check(name, p, char *)
  293. /* For historical reasons "bool" parameters can be (unsigned) "int". */
  294. extern struct kernel_param_ops param_ops_bool;
  295. extern int param_set_bool(const char *val, const struct kernel_param *kp);
  296. extern int param_get_bool(char *buffer, const struct kernel_param *kp);
  297. #define param_check_bool(name, p) \
  298. static inline void __check_##name(void) \
  299. { \
  300. BUILD_BUG_ON(!__same_type((p), bool *) && \
  301. !__same_type((p), unsigned int *) && \
  302. !__same_type((p), int *)); \
  303. }
  304. extern struct kernel_param_ops param_ops_invbool;
  305. extern int param_set_invbool(const char *val, const struct kernel_param *kp);
  306. extern int param_get_invbool(char *buffer, const struct kernel_param *kp);
  307. #define param_check_invbool(name, p) __param_check(name, p, bool)
  308. /**
  309. * module_param_array - a parameter which is an array of some type
  310. * @name: the name of the array variable
  311. * @type: the type, as per module_param()
  312. * @nump: optional pointer filled in with the number written
  313. * @perm: visibility in sysfs
  314. *
  315. * Input and output are as comma-separated values. Commas inside values
  316. * don't work properly (eg. an array of charp).
  317. *
  318. * ARRAY_SIZE(@name) is used to determine the number of elements in the
  319. * array, so the definition must be visible.
  320. */
  321. #define module_param_array(name, type, nump, perm) \
  322. module_param_array_named(name, name, type, nump, perm)
  323. /**
  324. * module_param_array_named - renamed parameter which is an array of some type
  325. * @name: a valid C identifier which is the parameter name
  326. * @array: the name of the array variable
  327. * @type: the type, as per module_param()
  328. * @nump: optional pointer filled in with the number written
  329. * @perm: visibility in sysfs
  330. *
  331. * This exposes a different name than the actual variable name. See
  332. * module_param_named() for why this might be necessary.
  333. */
  334. #define module_param_array_named(name, array, type, nump, perm) \
  335. static const struct kparam_array __param_arr_##name \
  336. = { ARRAY_SIZE(array), nump, &param_ops_##type, \
  337. sizeof(array[0]), array }; \
  338. __module_param_call(MODULE_PARAM_PREFIX, name, \
  339. &param_array_ops, \
  340. .arr = &__param_arr_##name, \
  341. __same_type(array[0], bool), perm); \
  342. __MODULE_PARM_TYPE(name, "array of " #type)
  343. extern struct kernel_param_ops param_array_ops;
  344. extern struct kernel_param_ops param_ops_string;
  345. extern int param_set_copystring(const char *val, const struct kernel_param *);
  346. extern int param_get_string(char *buffer, const struct kernel_param *kp);
  347. /* for exporting parameters in /sys/parameters */
  348. struct module;
  349. #if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
  350. extern int module_param_sysfs_setup(struct module *mod,
  351. const struct kernel_param *kparam,
  352. unsigned int num_params);
  353. extern void module_param_sysfs_remove(struct module *mod);
  354. #else
  355. static inline int module_param_sysfs_setup(struct module *mod,
  356. const struct kernel_param *kparam,
  357. unsigned int num_params)
  358. {
  359. return 0;
  360. }
  361. static inline void module_param_sysfs_remove(struct module *mod)
  362. { }
  363. #endif
  364. #endif /* _LINUX_MODULE_PARAMS_H */