moduleparam.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. #ifdef MODULE
  17. #define __MODULE_INFO(tag, name, info) \
  18. static const char __UNIQUE_ID(name)[] \
  19. __used __attribute__((section(".modinfo"), unused, aligned(1))) \
  20. = __stringify(tag) "=" info
  21. #else /* !MODULE */
  22. /* This struct is here for syntactic coherency, it is not used */
  23. #define __MODULE_INFO(tag, name, info) \
  24. struct __UNIQUE_ID(name) {}
  25. #endif
  26. #define __MODULE_PARM_TYPE(name, _type) \
  27. __MODULE_INFO(parmtype, name##type, #name ":" _type)
  28. /* One for each parameter, describing how to use it. Some files do
  29. multiple of these per line, so can't just use MODULE_INFO. */
  30. #define MODULE_PARM_DESC(_parm, desc) \
  31. __MODULE_INFO(parm, _parm, #_parm ":" desc)
  32. struct kernel_param;
  33. /*
  34. * Flags available for kernel_param_ops
  35. *
  36. * NOARG - the parameter allows for no argument (foo instead of foo=1)
  37. */
  38. enum {
  39. KERNEL_PARAM_OPS_FL_NOARG = (1 << 0)
  40. };
  41. struct kernel_param_ops {
  42. /* How the ops should behave */
  43. unsigned int flags;
  44. /* Returns 0, or -errno. arg is in kp->arg. */
  45. int (*set)(const char *val, const struct kernel_param *kp);
  46. /* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  47. int (*get)(char *buffer, const struct kernel_param *kp);
  48. /* Optional function to free kp->arg when module unloaded. */
  49. void (*free)(void *arg);
  50. };
  51. /*
  52. * Flags available for kernel_param
  53. *
  54. * UNSAFE - the parameter is dangerous and setting it will taint the kernel
  55. * HWPARAM - Hardware param not permitted in lockdown mode
  56. */
  57. enum {
  58. KERNEL_PARAM_FL_UNSAFE = (1 << 0),
  59. KERNEL_PARAM_FL_HWPARAM = (1 << 1),
  60. };
  61. struct kernel_param {
  62. const char *name;
  63. struct module *mod;
  64. const struct kernel_param_ops *ops;
  65. const u16 perm;
  66. s8 level;
  67. u8 flags;
  68. union {
  69. void *arg;
  70. const struct kparam_string *str;
  71. const struct kparam_array *arr;
  72. };
  73. };
  74. extern const struct kernel_param __start___param[], __stop___param[];
  75. /* Special one for strings we want to copy into */
  76. struct kparam_string {
  77. unsigned int maxlen;
  78. char *string;
  79. };
  80. /* Special one for arrays */
  81. struct kparam_array
  82. {
  83. unsigned int max;
  84. unsigned int elemsize;
  85. unsigned int *num;
  86. const struct kernel_param_ops *ops;
  87. void *elem;
  88. };
  89. /**
  90. * module_param - typesafe helper for a module/cmdline parameter
  91. * @value: the variable to alter, and exposed parameter name.
  92. * @type: the type of the parameter
  93. * @perm: visibility in sysfs.
  94. *
  95. * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
  96. * ".") the kernel commandline parameter. Note that - is changed to _, so
  97. * the user can use "foo-bar=1" even for variable "foo_bar".
  98. *
  99. * @perm is 0 if the the variable is not to appear in sysfs, or 0444
  100. * for world-readable, 0644 for root-writable, etc. Note that if it
  101. * is writable, you may need to use kernel_param_lock() around
  102. * accesses (esp. charp, which can be kfreed when it changes).
  103. *
  104. * The @type is simply pasted to refer to a param_ops_##type and a
  105. * param_check_##type: for convenience many standard types are provided but
  106. * you can create your own by defining those variables.
  107. *
  108. * Standard types are:
  109. * byte, short, ushort, int, uint, long, ulong
  110. * charp: a character pointer
  111. * bool: a bool, values 0/1, y/n, Y/N.
  112. * invbool: the above, only sense-reversed (N = true).
  113. */
  114. #define module_param(name, type, perm) \
  115. module_param_named(name, name, type, perm)
  116. /**
  117. * module_param_unsafe - same as module_param but taints kernel
  118. */
  119. #define module_param_unsafe(name, type, perm) \
  120. module_param_named_unsafe(name, name, type, perm)
  121. /**
  122. * module_param_named - typesafe helper for a renamed module/cmdline parameter
  123. * @name: a valid C identifier which is the parameter name.
  124. * @value: the actual lvalue to alter.
  125. * @type: the type of the parameter
  126. * @perm: visibility in sysfs.
  127. *
  128. * Usually it's a good idea to have variable names and user-exposed names the
  129. * same, but that's harder if the variable must be non-static or is inside a
  130. * structure. This allows exposure under a different name.
  131. */
  132. #define module_param_named(name, value, type, perm) \
  133. param_check_##type(name, &(value)); \
  134. module_param_cb(name, &param_ops_##type, &value, perm); \
  135. __MODULE_PARM_TYPE(name, #type)
  136. /**
  137. * module_param_named_unsafe - same as module_param_named but taints kernel
  138. */
  139. #define module_param_named_unsafe(name, value, type, perm) \
  140. param_check_##type(name, &(value)); \
  141. module_param_cb_unsafe(name, &param_ops_##type, &value, perm); \
  142. __MODULE_PARM_TYPE(name, #type)
  143. /**
  144. * module_param_cb - general callback for a module/cmdline parameter
  145. * @name: a valid C identifier which is the parameter name.
  146. * @ops: the set & get operations for this parameter.
  147. * @perm: visibility in sysfs.
  148. *
  149. * The ops can have NULL set or get functions.
  150. */
  151. #define module_param_cb(name, ops, arg, perm) \
  152. __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1, 0)
  153. #define module_param_cb_unsafe(name, ops, arg, perm) \
  154. __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1, \
  155. KERNEL_PARAM_FL_UNSAFE)
  156. /**
  157. * <level>_param_cb - general callback for a module/cmdline parameter
  158. * to be evaluated before certain initcall level
  159. * @name: a valid C identifier which is the parameter name.
  160. * @ops: the set & get operations for this parameter.
  161. * @perm: visibility in sysfs.
  162. *
  163. * The ops can have NULL set or get functions.
  164. */
  165. #define __level_param_cb(name, ops, arg, perm, level) \
  166. __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, level, 0)
  167. #define core_param_cb(name, ops, arg, perm) \
  168. __level_param_cb(name, ops, arg, perm, 1)
  169. #define postcore_param_cb(name, ops, arg, perm) \
  170. __level_param_cb(name, ops, arg, perm, 2)
  171. #define arch_param_cb(name, ops, arg, perm) \
  172. __level_param_cb(name, ops, arg, perm, 3)
  173. #define subsys_param_cb(name, ops, arg, perm) \
  174. __level_param_cb(name, ops, arg, perm, 4)
  175. #define fs_param_cb(name, ops, arg, perm) \
  176. __level_param_cb(name, ops, arg, perm, 5)
  177. #define device_param_cb(name, ops, arg, perm) \
  178. __level_param_cb(name, ops, arg, perm, 6)
  179. #define late_param_cb(name, ops, arg, perm) \
  180. __level_param_cb(name, ops, arg, perm, 7)
  181. /* On alpha, ia64 and ppc64 relocations to global data cannot go into
  182. read-only sections (which is part of respective UNIX ABI on these
  183. platforms). So 'const' makes no sense and even causes compile failures
  184. with some compilers. */
  185. #if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64)
  186. #define __moduleparam_const
  187. #else
  188. #define __moduleparam_const const
  189. #endif
  190. /* This is the fundamental function for registering boot/module
  191. parameters. */
  192. #define __module_param_call(prefix, name, ops, arg, perm, level, flags) \
  193. /* Default value instead of permissions? */ \
  194. static const char __param_str_##name[] = prefix #name; \
  195. static struct kernel_param __moduleparam_const __param_##name \
  196. __used \
  197. __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
  198. = { __param_str_##name, THIS_MODULE, ops, \
  199. VERIFY_OCTAL_PERMISSIONS(perm), level, flags, { arg } }
  200. /* Obsolete - use module_param_cb() */
  201. #define module_param_call(name, set, get, arg, perm) \
  202. static const struct kernel_param_ops __param_ops_##name = \
  203. { .flags = 0, (void *)set, (void *)get }; \
  204. __module_param_call(MODULE_PARAM_PREFIX, \
  205. name, &__param_ops_##name, arg, \
  206. (perm) + sizeof(__check_old_set_param(set))*0, -1, 0)
  207. /* We don't get oldget: it's often a new-style param_get_uint, etc. */
  208. static inline int
  209. __check_old_set_param(int (*oldset)(const char *, struct kernel_param *))
  210. {
  211. return 0;
  212. }
  213. #ifdef CONFIG_SYSFS
  214. extern void kernel_param_lock(struct module *mod);
  215. extern void kernel_param_unlock(struct module *mod);
  216. #else
  217. static inline void kernel_param_lock(struct module *mod)
  218. {
  219. }
  220. static inline void kernel_param_unlock(struct module *mod)
  221. {
  222. }
  223. #endif
  224. #ifndef MODULE
  225. /**
  226. * core_param - define a historical core kernel parameter.
  227. * @name: the name of the cmdline and sysfs parameter (often the same as var)
  228. * @var: the variable
  229. * @type: the type of the parameter
  230. * @perm: visibility in sysfs
  231. *
  232. * core_param is just like module_param(), but cannot be modular and
  233. * doesn't add a prefix (such as "printk."). This is for compatibility
  234. * with __setup(), and it makes sense as truly core parameters aren't
  235. * tied to the particular file they're in.
  236. */
  237. #define core_param(name, var, type, perm) \
  238. param_check_##type(name, &(var)); \
  239. __module_param_call("", name, &param_ops_##type, &var, perm, -1, 0)
  240. /**
  241. * core_param_unsafe - same as core_param but taints kernel
  242. */
  243. #define core_param_unsafe(name, var, type, perm) \
  244. param_check_##type(name, &(var)); \
  245. __module_param_call("", name, &param_ops_##type, &var, perm, \
  246. -1, KERNEL_PARAM_FL_UNSAFE)
  247. #endif /* !MODULE */
  248. /**
  249. * module_param_string - a char array parameter
  250. * @name: the name of the parameter
  251. * @string: the string variable
  252. * @len: the maximum length of the string, incl. terminator
  253. * @perm: visibility in sysfs.
  254. *
  255. * This actually copies the string when it's set (unlike type charp).
  256. * @len is usually just sizeof(string).
  257. */
  258. #define module_param_string(name, string, len, perm) \
  259. static const struct kparam_string __param_string_##name \
  260. = { len, string }; \
  261. __module_param_call(MODULE_PARAM_PREFIX, name, \
  262. &param_ops_string, \
  263. .str = &__param_string_##name, perm, -1, 0);\
  264. __MODULE_PARM_TYPE(name, "string")
  265. /**
  266. * parameq - checks if two parameter names match
  267. * @name1: parameter name 1
  268. * @name2: parameter name 2
  269. *
  270. * Returns true if the two parameter names are equal.
  271. * Dashes (-) are considered equal to underscores (_).
  272. */
  273. extern bool parameq(const char *name1, const char *name2);
  274. /**
  275. * parameqn - checks if two parameter names match
  276. * @name1: parameter name 1
  277. * @name2: parameter name 2
  278. * @n: the length to compare
  279. *
  280. * Similar to parameq(), except it compares @n characters.
  281. */
  282. extern bool parameqn(const char *name1, const char *name2, size_t n);
  283. /* Called on module insert or kernel boot */
  284. extern char *parse_args(const char *name,
  285. char *args,
  286. const struct kernel_param *params,
  287. unsigned num,
  288. s16 level_min,
  289. s16 level_max,
  290. void *arg,
  291. int (*unknown)(char *param, char *val,
  292. const char *doing, void *arg));
  293. /* Called by module remove. */
  294. #ifdef CONFIG_SYSFS
  295. extern void destroy_params(const struct kernel_param *params, unsigned num);
  296. #else
  297. static inline void destroy_params(const struct kernel_param *params,
  298. unsigned num)
  299. {
  300. }
  301. #endif /* !CONFIG_SYSFS */
  302. /* All the helper functions */
  303. /* The macros to do compile-time type checking stolen from Jakub
  304. Jelinek, who IIRC came up with this idea for the 2.4 module init code. */
  305. #define __param_check(name, p, type) \
  306. static inline type __always_unused *__check_##name(void) { return(p); }
  307. extern const struct kernel_param_ops param_ops_byte;
  308. extern int param_set_byte(const char *val, const struct kernel_param *kp);
  309. extern int param_get_byte(char *buffer, const struct kernel_param *kp);
  310. #define param_check_byte(name, p) __param_check(name, p, unsigned char)
  311. extern const struct kernel_param_ops param_ops_short;
  312. extern int param_set_short(const char *val, const struct kernel_param *kp);
  313. extern int param_get_short(char *buffer, const struct kernel_param *kp);
  314. #define param_check_short(name, p) __param_check(name, p, short)
  315. extern const struct kernel_param_ops param_ops_ushort;
  316. extern int param_set_ushort(const char *val, const struct kernel_param *kp);
  317. extern int param_get_ushort(char *buffer, const struct kernel_param *kp);
  318. #define param_check_ushort(name, p) __param_check(name, p, unsigned short)
  319. extern const struct kernel_param_ops param_ops_int;
  320. extern int param_set_int(const char *val, const struct kernel_param *kp);
  321. extern int param_get_int(char *buffer, const struct kernel_param *kp);
  322. #define param_check_int(name, p) __param_check(name, p, int)
  323. extern const struct kernel_param_ops param_ops_uint;
  324. extern int param_set_uint(const char *val, const struct kernel_param *kp);
  325. extern int param_get_uint(char *buffer, const struct kernel_param *kp);
  326. #define param_check_uint(name, p) __param_check(name, p, unsigned int)
  327. extern const struct kernel_param_ops param_ops_long;
  328. extern int param_set_long(const char *val, const struct kernel_param *kp);
  329. extern int param_get_long(char *buffer, const struct kernel_param *kp);
  330. #define param_check_long(name, p) __param_check(name, p, long)
  331. extern const struct kernel_param_ops param_ops_ulong;
  332. extern int param_set_ulong(const char *val, const struct kernel_param *kp);
  333. extern int param_get_ulong(char *buffer, const struct kernel_param *kp);
  334. #define param_check_ulong(name, p) __param_check(name, p, unsigned long)
  335. extern const struct kernel_param_ops param_ops_ullong;
  336. extern int param_set_ullong(const char *val, const struct kernel_param *kp);
  337. extern int param_get_ullong(char *buffer, const struct kernel_param *kp);
  338. #define param_check_ullong(name, p) __param_check(name, p, unsigned long long)
  339. extern const struct kernel_param_ops param_ops_charp;
  340. extern int param_set_charp(const char *val, const struct kernel_param *kp);
  341. extern int param_get_charp(char *buffer, const struct kernel_param *kp);
  342. extern void param_free_charp(void *arg);
  343. #define param_check_charp(name, p) __param_check(name, p, char *)
  344. /* We used to allow int as well as bool. We're taking that away! */
  345. extern const struct kernel_param_ops param_ops_bool;
  346. extern int param_set_bool(const char *val, const struct kernel_param *kp);
  347. extern int param_get_bool(char *buffer, const struct kernel_param *kp);
  348. #define param_check_bool(name, p) __param_check(name, p, bool)
  349. extern const struct kernel_param_ops param_ops_bool_enable_only;
  350. extern int param_set_bool_enable_only(const char *val,
  351. const struct kernel_param *kp);
  352. /* getter is the same as for the regular bool */
  353. #define param_check_bool_enable_only param_check_bool
  354. extern const struct kernel_param_ops param_ops_invbool;
  355. extern int param_set_invbool(const char *val, const struct kernel_param *kp);
  356. extern int param_get_invbool(char *buffer, const struct kernel_param *kp);
  357. #define param_check_invbool(name, p) __param_check(name, p, bool)
  358. /* An int, which can only be set like a bool (though it shows as an int). */
  359. extern const struct kernel_param_ops param_ops_bint;
  360. extern int param_set_bint(const char *val, const struct kernel_param *kp);
  361. #define param_get_bint param_get_int
  362. #define param_check_bint param_check_int
  363. /**
  364. * module_param_array - a parameter which is an array of some type
  365. * @name: the name of the array variable
  366. * @type: the type, as per module_param()
  367. * @nump: optional pointer filled in with the number written
  368. * @perm: visibility in sysfs
  369. *
  370. * Input and output are as comma-separated values. Commas inside values
  371. * don't work properly (eg. an array of charp).
  372. *
  373. * ARRAY_SIZE(@name) is used to determine the number of elements in the
  374. * array, so the definition must be visible.
  375. */
  376. #define module_param_array(name, type, nump, perm) \
  377. module_param_array_named(name, name, type, nump, perm)
  378. /**
  379. * module_param_array_named - renamed parameter which is an array of some type
  380. * @name: a valid C identifier which is the parameter name
  381. * @array: the name of the array variable
  382. * @type: the type, as per module_param()
  383. * @nump: optional pointer filled in with the number written
  384. * @perm: visibility in sysfs
  385. *
  386. * This exposes a different name than the actual variable name. See
  387. * module_param_named() for why this might be necessary.
  388. */
  389. #define module_param_array_named(name, array, type, nump, perm) \
  390. param_check_##type(name, &(array)[0]); \
  391. static const struct kparam_array __param_arr_##name \
  392. = { .max = ARRAY_SIZE(array), .num = nump, \
  393. .ops = &param_ops_##type, \
  394. .elemsize = sizeof(array[0]), .elem = array }; \
  395. __module_param_call(MODULE_PARAM_PREFIX, name, \
  396. &param_array_ops, \
  397. .arr = &__param_arr_##name, \
  398. perm, -1, 0); \
  399. __MODULE_PARM_TYPE(name, "array of " #type)
  400. enum hwparam_type {
  401. hwparam_ioport, /* Module parameter configures an I/O port */
  402. hwparam_iomem, /* Module parameter configures an I/O mem address */
  403. hwparam_ioport_or_iomem, /* Module parameter could be either, depending on other option */
  404. hwparam_irq, /* Module parameter configures an I/O port */
  405. hwparam_dma, /* Module parameter configures a DMA channel */
  406. hwparam_dma_addr, /* Module parameter configures a DMA buffer address */
  407. hwparam_other, /* Module parameter configures some other value */
  408. };
  409. /**
  410. * module_param_hw_named - A parameter representing a hw parameters
  411. * @name: a valid C identifier which is the parameter name.
  412. * @value: the actual lvalue to alter.
  413. * @type: the type of the parameter
  414. * @hwtype: what the value represents (enum hwparam_type)
  415. * @perm: visibility in sysfs.
  416. *
  417. * Usually it's a good idea to have variable names and user-exposed names the
  418. * same, but that's harder if the variable must be non-static or is inside a
  419. * structure. This allows exposure under a different name.
  420. */
  421. #define module_param_hw_named(name, value, type, hwtype, perm) \
  422. param_check_##type(name, &(value)); \
  423. __module_param_call(MODULE_PARAM_PREFIX, name, \
  424. &param_ops_##type, &value, \
  425. perm, -1, \
  426. KERNEL_PARAM_FL_HWPARAM | (hwparam_##hwtype & 0)); \
  427. __MODULE_PARM_TYPE(name, #type)
  428. #define module_param_hw(name, type, hwtype, perm) \
  429. module_param_hw_named(name, name, type, hwtype, perm)
  430. /**
  431. * module_param_hw_array - A parameter representing an array of hw parameters
  432. * @name: the name of the array variable
  433. * @type: the type, as per module_param()
  434. * @hwtype: what the value represents (enum hwparam_type)
  435. * @nump: optional pointer filled in with the number written
  436. * @perm: visibility in sysfs
  437. *
  438. * Input and output are as comma-separated values. Commas inside values
  439. * don't work properly (eg. an array of charp).
  440. *
  441. * ARRAY_SIZE(@name) is used to determine the number of elements in the
  442. * array, so the definition must be visible.
  443. */
  444. #define module_param_hw_array(name, type, hwtype, nump, perm) \
  445. param_check_##type(name, &(name)[0]); \
  446. static const struct kparam_array __param_arr_##name \
  447. = { .max = ARRAY_SIZE(name), .num = nump, \
  448. .ops = &param_ops_##type, \
  449. .elemsize = sizeof(name[0]), .elem = name }; \
  450. __module_param_call(MODULE_PARAM_PREFIX, name, \
  451. &param_array_ops, \
  452. .arr = &__param_arr_##name, \
  453. perm, -1, \
  454. KERNEL_PARAM_FL_HWPARAM | (hwparam_##hwtype & 0)); \
  455. __MODULE_PARM_TYPE(name, "array of " #type)
  456. extern const struct kernel_param_ops param_array_ops;
  457. extern const struct kernel_param_ops param_ops_string;
  458. extern int param_set_copystring(const char *val, const struct kernel_param *);
  459. extern int param_get_string(char *buffer, const struct kernel_param *kp);
  460. /* for exporting parameters in /sys/module/.../parameters */
  461. struct module;
  462. #if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
  463. extern int module_param_sysfs_setup(struct module *mod,
  464. const struct kernel_param *kparam,
  465. unsigned int num_params);
  466. extern void module_param_sysfs_remove(struct module *mod);
  467. #else
  468. static inline int module_param_sysfs_setup(struct module *mod,
  469. const struct kernel_param *kparam,
  470. unsigned int num_params)
  471. {
  472. return 0;
  473. }
  474. static inline void module_param_sysfs_remove(struct module *mod)
  475. { }
  476. #endif
  477. #endif /* _LINUX_MODULE_PARAMS_H */