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