Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [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 | /* 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 |
Sam Ravnborg | 367cb70 | 2006-01-06 21:17:50 +0100 | [diff] [blame] | 13 | #define MODULE_PARAM_PREFIX KBUILD_MODNAME "." |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #endif |
| 15 | |
| 16 | #ifdef MODULE |
| 17 | #define ___module_cat(a,b) __mod_ ## a ## b |
| 18 | #define __module_cat(a,b) ___module_cat(a,b) |
| 19 | #define __MODULE_INFO(tag, name, info) \ |
| 20 | static const char __module_cat(name,__LINE__)[] \ |
Adrian Bunk | 3ff6eec | 2008-01-24 22:16:20 +0100 | [diff] [blame] | 21 | __used \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | __attribute__((section(".modinfo"),unused)) = __stringify(tag) "=" info |
| 23 | #else /* !MODULE */ |
| 24 | #define __MODULE_INFO(tag, name, info) |
| 25 | #endif |
| 26 | #define __MODULE_PARM_TYPE(name, _type) \ |
| 27 | __MODULE_INFO(parmtype, name##type, #name ":" _type) |
| 28 | |
| 29 | struct kernel_param; |
| 30 | |
| 31 | /* Returns 0, or -errno. arg is in kp->arg. */ |
| 32 | typedef int (*param_set_fn)(const char *val, struct kernel_param *kp); |
| 33 | /* Returns length written or -errno. Buffer is 4k (ie. be short!) */ |
| 34 | typedef int (*param_get_fn)(char *buffer, struct kernel_param *kp); |
| 35 | |
| 36 | struct kernel_param { |
| 37 | const char *name; |
| 38 | unsigned int perm; |
| 39 | param_set_fn set; |
| 40 | param_get_fn get; |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 41 | union { |
| 42 | void *arg; |
| 43 | const struct kparam_string *str; |
| 44 | const struct kparam_array *arr; |
| 45 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /* Special one for strings we want to copy into */ |
| 49 | struct kparam_string { |
| 50 | unsigned int maxlen; |
| 51 | char *string; |
| 52 | }; |
| 53 | |
| 54 | /* Special one for arrays */ |
| 55 | struct kparam_array |
| 56 | { |
| 57 | unsigned int max; |
| 58 | unsigned int *num; |
| 59 | param_set_fn set; |
| 60 | param_get_fn get; |
| 61 | unsigned int elemsize; |
| 62 | void *elem; |
| 63 | }; |
| 64 | |
Ivan Kokshaysky | 91d35dd | 2008-02-13 15:03:26 -0800 | [diff] [blame] | 65 | /* On alpha, ia64 and ppc64 relocations to global data cannot go into |
| 66 | read-only sections (which is part of respective UNIX ABI on these |
| 67 | platforms). So 'const' makes no sense and even causes compile failures |
| 68 | with some compilers. */ |
| 69 | #if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64) |
| 70 | #define __moduleparam_const |
| 71 | #else |
| 72 | #define __moduleparam_const const |
| 73 | #endif |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* This is the fundamental function for registering boot/module |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 76 | parameters. perm sets the visibility in sysfs: 000 means it's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | not there, read bits mean it's readable, write bits mean it's |
| 78 | writable. */ |
| 79 | #define __module_param_call(prefix, name, set, get, arg, perm) \ |
Alexey Dobriyan | 9774a1f | 2006-12-06 20:36:56 -0800 | [diff] [blame] | 80 | /* Default value instead of permissions? */ \ |
| 81 | static int __param_perm_check_##name __attribute__((unused)) = \ |
| 82 | BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)); \ |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 83 | static const char __param_str_##name[] = prefix #name; \ |
Ivan Kokshaysky | 91d35dd | 2008-02-13 15:03:26 -0800 | [diff] [blame] | 84 | static struct kernel_param __moduleparam_const __param_##name \ |
Adrian Bunk | 3ff6eec | 2008-01-24 22:16:20 +0100 | [diff] [blame] | 85 | __used \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \ |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 87 | = { __param_str_##name, perm, set, get, { arg } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | #define module_param_call(name, set, get, arg, perm) \ |
| 90 | __module_param_call(MODULE_PARAM_PREFIX, name, set, get, arg, perm) |
| 91 | |
| 92 | /* Helper functions: type is byte, short, ushort, int, uint, long, |
| 93 | ulong, charp, bool or invbool, or XXX if you define param_get_XXX, |
| 94 | param_set_XXX and param_check_XXX. */ |
| 95 | #define module_param_named(name, value, type, perm) \ |
| 96 | param_check_##type(name, &(value)); \ |
| 97 | module_param_call(name, param_set_##type, param_get_##type, &value, perm); \ |
| 98 | __MODULE_PARM_TYPE(name, #type) |
| 99 | |
| 100 | #define module_param(name, type, perm) \ |
| 101 | module_param_named(name, name, type, perm) |
| 102 | |
| 103 | /* Actually copy string: maxlen param is usually sizeof(string). */ |
| 104 | #define module_param_string(name, string, len, perm) \ |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 105 | static const struct kparam_string __param_string_##name \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | = { len, string }; \ |
| 107 | module_param_call(name, param_set_copystring, param_get_string, \ |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 108 | .str = &__param_string_##name, perm); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | __MODULE_PARM_TYPE(name, "string") |
| 110 | |
| 111 | /* Called on module insert or kernel boot */ |
| 112 | extern int parse_args(const char *name, |
| 113 | char *args, |
| 114 | struct kernel_param *params, |
| 115 | unsigned num, |
| 116 | int (*unknown)(char *param, char *val)); |
| 117 | |
| 118 | /* All the helper functions */ |
| 119 | /* The macros to do compile-time type checking stolen from Jakub |
| 120 | Jelinek, who IIRC came up with this idea for the 2.4 module init code. */ |
| 121 | #define __param_check(name, p, type) \ |
| 122 | static inline type *__check_##name(void) { return(p); } |
| 123 | |
| 124 | extern int param_set_byte(const char *val, struct kernel_param *kp); |
| 125 | extern int param_get_byte(char *buffer, struct kernel_param *kp); |
| 126 | #define param_check_byte(name, p) __param_check(name, p, unsigned char) |
| 127 | |
| 128 | extern int param_set_short(const char *val, struct kernel_param *kp); |
| 129 | extern int param_get_short(char *buffer, struct kernel_param *kp); |
| 130 | #define param_check_short(name, p) __param_check(name, p, short) |
| 131 | |
| 132 | extern int param_set_ushort(const char *val, struct kernel_param *kp); |
| 133 | extern int param_get_ushort(char *buffer, struct kernel_param *kp); |
| 134 | #define param_check_ushort(name, p) __param_check(name, p, unsigned short) |
| 135 | |
| 136 | extern int param_set_int(const char *val, struct kernel_param *kp); |
| 137 | extern int param_get_int(char *buffer, struct kernel_param *kp); |
| 138 | #define param_check_int(name, p) __param_check(name, p, int) |
| 139 | |
| 140 | extern int param_set_uint(const char *val, struct kernel_param *kp); |
| 141 | extern int param_get_uint(char *buffer, struct kernel_param *kp); |
| 142 | #define param_check_uint(name, p) __param_check(name, p, unsigned int) |
| 143 | |
| 144 | extern int param_set_long(const char *val, struct kernel_param *kp); |
| 145 | extern int param_get_long(char *buffer, struct kernel_param *kp); |
| 146 | #define param_check_long(name, p) __param_check(name, p, long) |
| 147 | |
| 148 | extern int param_set_ulong(const char *val, struct kernel_param *kp); |
| 149 | extern int param_get_ulong(char *buffer, struct kernel_param *kp); |
| 150 | #define param_check_ulong(name, p) __param_check(name, p, unsigned long) |
| 151 | |
| 152 | extern int param_set_charp(const char *val, struct kernel_param *kp); |
| 153 | extern int param_get_charp(char *buffer, struct kernel_param *kp); |
| 154 | #define param_check_charp(name, p) __param_check(name, p, char *) |
| 155 | |
| 156 | extern int param_set_bool(const char *val, struct kernel_param *kp); |
| 157 | extern int param_get_bool(char *buffer, struct kernel_param *kp); |
| 158 | #define param_check_bool(name, p) __param_check(name, p, int) |
| 159 | |
| 160 | extern int param_set_invbool(const char *val, struct kernel_param *kp); |
| 161 | extern int param_get_invbool(char *buffer, struct kernel_param *kp); |
| 162 | #define param_check_invbool(name, p) __param_check(name, p, int) |
| 163 | |
| 164 | /* Comma-separated array: *nump is set to number they actually specified. */ |
| 165 | #define module_param_array_named(name, array, type, nump, perm) \ |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 166 | static const struct kparam_array __param_arr_##name \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | = { ARRAY_SIZE(array), nump, param_set_##type, param_get_##type,\ |
| 168 | sizeof(array[0]), array }; \ |
| 169 | module_param_call(name, param_array_set, param_array_get, \ |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 170 | .arr = &__param_arr_##name, perm); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | __MODULE_PARM_TYPE(name, "array of " #type) |
| 172 | |
| 173 | #define module_param_array(name, type, nump, perm) \ |
| 174 | module_param_array_named(name, name, type, nump, perm) |
| 175 | |
| 176 | extern int param_array_set(const char *val, struct kernel_param *kp); |
| 177 | extern int param_array_get(char *buffer, struct kernel_param *kp); |
| 178 | |
| 179 | extern int param_set_copystring(const char *val, struct kernel_param *kp); |
| 180 | extern int param_get_string(char *buffer, struct kernel_param *kp); |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* for exporting parameters in /sys/parameters */ |
| 183 | |
| 184 | struct module; |
| 185 | |
Randy Dunlap | ef665c1 | 2007-02-13 15:19:06 -0800 | [diff] [blame] | 186 | #if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | extern int module_param_sysfs_setup(struct module *mod, |
| 188 | struct kernel_param *kparam, |
| 189 | unsigned int num_params); |
| 190 | |
| 191 | extern void module_param_sysfs_remove(struct module *mod); |
Randy Dunlap | ef665c1 | 2007-02-13 15:19:06 -0800 | [diff] [blame] | 192 | #else |
| 193 | static inline int module_param_sysfs_setup(struct module *mod, |
| 194 | struct kernel_param *kparam, |
| 195 | unsigned int num_params) |
| 196 | { |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static inline void module_param_sysfs_remove(struct module *mod) |
| 201 | { } |
| 202 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | #endif /* _LINUX_MODULE_PARAMS_H */ |