1#ifndef _LINUX_MODULE_PARAMS_H
2#define _LINUX_MODULE_PARAMS_H
3
4#include <linux/init.h>
5#include <linux/stringify.h>
6#include <linux/kernel.h>
7
8
9
10#ifdef MODULE
11#define MODULE_PARAM_PREFIX
12#else
13#define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
14#endif
15
16
17#define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
18
19#ifdef MODULE
20#define __MODULE_INFO(tag, name, info) \
21static const char __UNIQUE_ID(name)[] \
22 __used __attribute__((section(".modinfo"), unused, aligned(1))) \
23 = __stringify(tag) "=" info
24#else
25
26#define __MODULE_INFO(tag, name, info) \
27 struct __UNIQUE_ID(name) {}
28#endif
29#define __MODULE_PARM_TYPE(name, _type) \
30 __MODULE_INFO(parmtype, name##type, #name ":" _type)
31
32
33
34#define MODULE_PARM_DESC(_parm, desc) \
35 __MODULE_INFO(parm, _parm, #_parm ":" desc)
36
37struct kernel_param;
38
39struct kernel_param_ops {
40
41 int (*set)(const char *val, const struct kernel_param *kp);
42
43 int (*get)(char *buffer, const struct kernel_param *kp);
44
45 void (*free)(void *arg);
46};
47
48struct kernel_param {
49 const char *name;
50 const struct kernel_param_ops *ops;
51 u16 perm;
52 s16 level;
53 union {
54 void *arg;
55 const struct kparam_string *str;
56 const struct kparam_array *arr;
57 };
58};
59
60
61struct kparam_string {
62 unsigned int maxlen;
63 char *string;
64};
65
66
67struct kparam_array
68{
69 unsigned int max;
70 unsigned int elemsize;
71 unsigned int *num;
72 const struct kernel_param_ops *ops;
73 void *elem;
74};
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101#define module_param(name, type, perm) \
102 module_param_named(name, name, type, perm)
103
104
105
106
107
108
109
110
111
112
113
114
115#define module_param_named(name, value, type, perm) \
116 param_check_##type(name, &(value)); \
117 module_param_cb(name, ¶m_ops_##type, &value, perm); \
118 __MODULE_PARM_TYPE(name, #type)
119
120
121
122
123
124
125
126
127
128#define module_param_cb(name, ops, arg, perm) \
129 __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1)
130
131
132
133
134
135
136
137
138
139
140#define __level_param_cb(name, ops, arg, perm, level) \
141 __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, level)
142
143#define core_param_cb(name, ops, arg, perm) \
144 __level_param_cb(name, ops, arg, perm, 1)
145
146#define postcore_param_cb(name, ops, arg, perm) \
147 __level_param_cb(name, ops, arg, perm, 2)
148
149#define arch_param_cb(name, ops, arg, perm) \
150 __level_param_cb(name, ops, arg, perm, 3)
151
152#define subsys_param_cb(name, ops, arg, perm) \
153 __level_param_cb(name, ops, arg, perm, 4)
154
155#define fs_param_cb(name, ops, arg, perm) \
156 __level_param_cb(name, ops, arg, perm, 5)
157
158#define device_param_cb(name, ops, arg, perm) \
159 __level_param_cb(name, ops, arg, perm, 6)
160
161#define late_param_cb(name, ops, arg, perm) \
162 __level_param_cb(name, ops, arg, perm, 7)
163
164
165
166
167
168#if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64)
169#define __moduleparam_const
170#else
171#define __moduleparam_const const
172#endif
173
174
175
176#define __module_param_call(prefix, name, ops, arg, perm, level) \
177 \
178 static int __param_perm_check_##name __attribute__((unused)) = \
179 BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)) \
180 + BUILD_BUG_ON_ZERO(sizeof(""prefix) > MAX_PARAM_PREFIX_LEN); \
181 static const char __param_str_##name[] = prefix #name; \
182 static struct kernel_param __moduleparam_const __param_##name \
183 __used \
184 __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
185 = { __param_str_##name, ops, perm, level, { arg } }
186
187
188#define module_param_call(name, set, get, arg, perm) \
189 static struct kernel_param_ops __param_ops_##name = \
190 { (void *)set, (void *)get }; \
191 __module_param_call(MODULE_PARAM_PREFIX, \
192 name, &__param_ops_##name, arg, \
193 (perm) + sizeof(__check_old_set_param(set))*0, -1)
194
195
196static inline int
197__check_old_set_param(int (*oldset)(const char *, struct kernel_param *))
198{
199 return 0;
200}
201
202
203
204
205
206
207
208#define kparam_block_sysfs_write(name) \
209 do { \
210 BUG_ON(!(__param_##name.perm & 0222)); \
211 __kernel_param_lock(); \
212 } while (0)
213
214
215
216
217
218#define kparam_unblock_sysfs_write(name) \
219 do { \
220 BUG_ON(!(__param_##name.perm & 0222)); \
221 __kernel_param_unlock(); \
222 } while (0)
223
224
225
226
227
228
229
230#define kparam_block_sysfs_read(name) \
231 do { \
232 BUG_ON(!(__param_##name.perm & 0444)); \
233 __kernel_param_lock(); \
234 } while (0)
235
236
237
238
239
240#define kparam_unblock_sysfs_read(name) \
241 do { \
242 BUG_ON(!(__param_##name.perm & 0444)); \
243 __kernel_param_unlock(); \
244 } while (0)
245
246#ifdef CONFIG_SYSFS
247extern void __kernel_param_lock(void);
248extern void __kernel_param_unlock(void);
249#else
250static inline void __kernel_param_lock(void)
251{
252}
253static inline void __kernel_param_unlock(void)
254{
255}
256#endif
257
258#ifndef MODULE
259
260
261
262
263
264
265
266
267
268
269
270
271#define core_param(name, var, type, perm) \
272 param_check_##type(name, &(var)); \
273 __module_param_call("", name, ¶m_ops_##type, &var, perm, -1)
274#endif
275
276
277
278
279
280
281
282
283
284
285
286#define module_param_string(name, string, len, perm) \
287 static const struct kparam_string __param_string_##name \
288 = { len, string }; \
289 __module_param_call(MODULE_PARAM_PREFIX, name, \
290 ¶m_ops_string, \
291 .str = &__param_string_##name, perm, -1); \
292 __MODULE_PARM_TYPE(name, "string")
293
294
295
296
297
298
299
300
301
302extern bool parameq(const char *name1, const char *name2);
303
304
305
306
307
308
309
310
311
312extern bool parameqn(const char *name1, const char *name2, size_t n);
313
314
315extern int parse_args(const char *name,
316 char *args,
317 const struct kernel_param *params,
318 unsigned num,
319 s16 level_min,
320 s16 level_max,
321 int (*unknown)(char *param, char *val,
322 const char *doing));
323
324
325#ifdef CONFIG_SYSFS
326extern void destroy_params(const struct kernel_param *params, unsigned num);
327#else
328static inline void destroy_params(const struct kernel_param *params,
329 unsigned num)
330{
331}
332#endif
333
334
335
336
337#define __param_check(name, p, type) \
338 static inline type *__check_##name(void) { return(p); }
339
340extern struct kernel_param_ops param_ops_byte;
341extern int param_set_byte(const char *val, const struct kernel_param *kp);
342extern int param_get_byte(char *buffer, const struct kernel_param *kp);
343#define param_check_byte(name, p) __param_check(name, p, unsigned char)
344
345extern struct kernel_param_ops param_ops_short;
346extern int param_set_short(const char *val, const struct kernel_param *kp);
347extern int param_get_short(char *buffer, const struct kernel_param *kp);
348#define param_check_short(name, p) __param_check(name, p, short)
349
350extern struct kernel_param_ops param_ops_ushort;
351extern int param_set_ushort(const char *val, const struct kernel_param *kp);
352extern int param_get_ushort(char *buffer, const struct kernel_param *kp);
353#define param_check_ushort(name, p) __param_check(name, p, unsigned short)
354
355extern struct kernel_param_ops param_ops_int;
356extern int param_set_int(const char *val, const struct kernel_param *kp);
357extern int param_get_int(char *buffer, const struct kernel_param *kp);
358#define param_check_int(name, p) __param_check(name, p, int)
359
360extern struct kernel_param_ops param_ops_uint;
361extern int param_set_uint(const char *val, const struct kernel_param *kp);
362extern int param_get_uint(char *buffer, const struct kernel_param *kp);
363#define param_check_uint(name, p) __param_check(name, p, unsigned int)
364
365extern struct kernel_param_ops param_ops_long;
366extern int param_set_long(const char *val, const struct kernel_param *kp);
367extern int param_get_long(char *buffer, const struct kernel_param *kp);
368#define param_check_long(name, p) __param_check(name, p, long)
369
370extern struct kernel_param_ops param_ops_ulong;
371extern int param_set_ulong(const char *val, const struct kernel_param *kp);
372extern int param_get_ulong(char *buffer, const struct kernel_param *kp);
373#define param_check_ulong(name, p) __param_check(name, p, unsigned long)
374
375extern struct kernel_param_ops param_ops_charp;
376extern int param_set_charp(const char *val, const struct kernel_param *kp);
377extern int param_get_charp(char *buffer, const struct kernel_param *kp);
378#define param_check_charp(name, p) __param_check(name, p, char *)
379
380
381extern struct kernel_param_ops param_ops_bool;
382extern int param_set_bool(const char *val, const struct kernel_param *kp);
383extern int param_get_bool(char *buffer, const struct kernel_param *kp);
384#define param_check_bool(name, p) __param_check(name, p, bool)
385
386extern struct kernel_param_ops param_ops_invbool;
387extern int param_set_invbool(const char *val, const struct kernel_param *kp);
388extern int param_get_invbool(char *buffer, const struct kernel_param *kp);
389#define param_check_invbool(name, p) __param_check(name, p, bool)
390
391
392extern struct kernel_param_ops param_ops_bint;
393extern int param_set_bint(const char *val, const struct kernel_param *kp);
394#define param_get_bint param_get_int
395#define param_check_bint param_check_int
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410#define module_param_array(name, type, nump, perm) \
411 module_param_array_named(name, name, type, nump, perm)
412
413
414
415
416
417
418
419
420
421
422
423
424#define module_param_array_named(name, array, type, nump, perm) \
425 param_check_##type(name, &(array)[0]); \
426 static const struct kparam_array __param_arr_##name \
427 = { .max = ARRAY_SIZE(array), .num = nump, \
428 .ops = ¶m_ops_##type, \
429 .elemsize = sizeof(array[0]), .elem = array }; \
430 __module_param_call(MODULE_PARAM_PREFIX, name, \
431 ¶m_array_ops, \
432 .arr = &__param_arr_##name, \
433 perm, -1); \
434 __MODULE_PARM_TYPE(name, "array of " #type)
435
436extern struct kernel_param_ops param_array_ops;
437
438extern struct kernel_param_ops param_ops_string;
439extern int param_set_copystring(const char *val, const struct kernel_param *);
440extern int param_get_string(char *buffer, const struct kernel_param *kp);
441
442
443
444struct module;
445
446#if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
447extern int module_param_sysfs_setup(struct module *mod,
448 const struct kernel_param *kparam,
449 unsigned int num_params);
450
451extern void module_param_sysfs_remove(struct module *mod);
452#else
453static inline int module_param_sysfs_setup(struct module *mod,
454 const struct kernel_param *kparam,
455 unsigned int num_params)
456{
457 return 0;
458}
459
460static inline void module_param_sysfs_remove(struct module *mod)
461{ }
462#endif
463
464#endif
465