moduleparam.h 15 KB

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