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#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) \
23static const char __module_cat(name,__LINE__)[] \
24 __used __attribute__((section(".modinfo"), unused, aligned(1))) \
25 = __stringify(tag) "=" info
26#else
27
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
34struct kernel_param;
35
36struct kernel_param_ops {
37
38 int (*set)(const char *val, const struct kernel_param *kp);
39
40 int (*get)(char *buffer, const struct kernel_param *kp);
41
42 void (*free)(void *arg);
43};
44
45
46#define KPARAM_ISBOOL 2
47
48struct kernel_param {
49 const char *name;
50 const struct kernel_param_ops *ops;
51 u16 perm;
52 u16 flags;
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, \
130 name, ops, arg, __same_type((arg), bool *), perm)
131
132
133
134
135
136#if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64)
137#define __moduleparam_const
138#else
139#define __moduleparam_const const
140#endif
141
142
143
144#define __module_param_call(prefix, name, ops, arg, isbool, perm) \
145 \
146 static int __param_perm_check_##name __attribute__((unused)) = \
147 BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)) \
148 + BUILD_BUG_ON_ZERO(sizeof(""prefix) > MAX_PARAM_PREFIX_LEN); \
149 static const char __param_str_##name[] = prefix #name; \
150 static struct kernel_param __moduleparam_const __param_##name \
151 __used \
152 __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
153 = { __param_str_##name, ops, perm, isbool ? KPARAM_ISBOOL : 0, \
154 { arg } }
155
156
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 __same_type(arg, bool *), \
163 (perm) + sizeof(__check_old_set_param(set))*0)
164
165
166static inline int
167__check_old_set_param(int (*oldset)(const char *, struct kernel_param *))
168{
169 return 0;
170}
171
172
173
174
175
176
177
178#define kparam_block_sysfs_write(name) \
179 do { \
180 BUG_ON(!(__param_##name.perm & 0222)); \
181 __kernel_param_lock(); \
182 } while (0)
183
184
185
186
187
188#define kparam_unblock_sysfs_write(name) \
189 do { \
190 BUG_ON(!(__param_##name.perm & 0222)); \
191 __kernel_param_unlock(); \
192 } while (0)
193
194
195
196
197
198
199
200#define kparam_block_sysfs_read(name) \
201 do { \
202 BUG_ON(!(__param_##name.perm & 0444)); \
203 __kernel_param_lock(); \
204 } while (0)
205
206
207
208
209
210#define kparam_unblock_sysfs_read(name) \
211 do { \
212 BUG_ON(!(__param_##name.perm & 0444)); \
213 __kernel_param_unlock(); \
214 } while (0)
215
216#ifdef CONFIG_SYSFS
217extern void __kernel_param_lock(void);
218extern void __kernel_param_unlock(void);
219#else
220static inline void __kernel_param_lock(void)
221{
222}
223static inline void __kernel_param_unlock(void)
224{
225}
226#endif
227
228#ifndef MODULE
229
230
231
232
233
234
235
236
237
238
239
240
241#define core_param(name, var, type, perm) \
242 param_check_##type(name, &(var)); \
243 __module_param_call("", name, ¶m_ops_##type, \
244 &var, __same_type(var, bool), perm)
245#endif
246
247
248
249
250
251
252
253
254
255
256
257#define module_param_string(name, string, len, perm) \
258 static const struct kparam_string __param_string_##name \
259 = { len, string }; \
260 __module_param_call(MODULE_PARAM_PREFIX, name, \
261 ¶m_ops_string, \
262 .str = &__param_string_##name, 0, perm); \
263 __MODULE_PARM_TYPE(name, "string")
264
265
266extern int parse_args(const char *name,
267 char *args,
268 const struct kernel_param *params,
269 unsigned num,
270 int (*unknown)(char *param, char *val));
271
272
273#ifdef CONFIG_SYSFS
274extern void destroy_params(const struct kernel_param *params, unsigned num);
275#else
276static inline void destroy_params(const struct kernel_param *params,
277 unsigned num)
278{
279}
280#endif
281
282
283
284
285#define __param_check(name, p, type) \
286 static inline type *__check_##name(void) { return(p); }
287
288extern struct kernel_param_ops param_ops_byte;
289extern int param_set_byte(const char *val, const struct kernel_param *kp);
290extern int param_get_byte(char *buffer, const struct kernel_param *kp);
291#define param_check_byte(name, p) __param_check(name, p, unsigned char)
292
293extern struct kernel_param_ops param_ops_short;
294extern int param_set_short(const char *val, const struct kernel_param *kp);
295extern int param_get_short(char *buffer, const struct kernel_param *kp);
296#define param_check_short(name, p) __param_check(name, p, short)
297
298extern struct kernel_param_ops param_ops_ushort;
299extern int param_set_ushort(const char *val, const struct kernel_param *kp);
300extern int param_get_ushort(char *buffer, const struct kernel_param *kp);
301#define param_check_ushort(name, p) __param_check(name, p, unsigned short)
302
303extern struct kernel_param_ops param_ops_int;
304extern int param_set_int(const char *val, const struct kernel_param *kp);
305extern int param_get_int(char *buffer, const struct kernel_param *kp);
306#define param_check_int(name, p) __param_check(name, p, int)
307
308extern struct kernel_param_ops param_ops_uint;
309extern int param_set_uint(const char *val, const struct kernel_param *kp);
310extern int param_get_uint(char *buffer, const struct kernel_param *kp);
311#define param_check_uint(name, p) __param_check(name, p, unsigned int)
312
313extern struct kernel_param_ops param_ops_long;
314extern int param_set_long(const char *val, const struct kernel_param *kp);
315extern int param_get_long(char *buffer, const struct kernel_param *kp);
316#define param_check_long(name, p) __param_check(name, p, long)
317
318extern struct kernel_param_ops param_ops_ulong;
319extern int param_set_ulong(const char *val, const struct kernel_param *kp);
320extern int param_get_ulong(char *buffer, const struct kernel_param *kp);
321#define param_check_ulong(name, p) __param_check(name, p, unsigned long)
322
323extern struct kernel_param_ops param_ops_charp;
324extern int param_set_charp(const char *val, const struct kernel_param *kp);
325extern int param_get_charp(char *buffer, const struct kernel_param *kp);
326#define param_check_charp(name, p) __param_check(name, p, char *)
327
328
329extern struct kernel_param_ops param_ops_bool;
330extern int param_set_bool(const char *val, const struct kernel_param *kp);
331extern int param_get_bool(char *buffer, const struct kernel_param *kp);
332#define param_check_bool(name, p) \
333 static inline void __check_##name(void) \
334 { \
335 BUILD_BUG_ON(!__same_type((p), bool *) && \
336 !__same_type((p), unsigned int *) && \
337 !__same_type((p), int *)); \
338 }
339
340extern struct kernel_param_ops param_ops_invbool;
341extern int param_set_invbool(const char *val, const struct kernel_param *kp);
342extern int param_get_invbool(char *buffer, const struct kernel_param *kp);
343#define param_check_invbool(name, p) __param_check(name, p, bool)
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358#define module_param_array(name, type, nump, perm) \
359 module_param_array_named(name, name, type, nump, perm)
360
361
362
363
364
365
366
367
368
369
370
371
372#define module_param_array_named(name, array, type, nump, perm) \
373 static const struct kparam_array __param_arr_##name \
374 = { .max = ARRAY_SIZE(array), .num = nump, \
375 .ops = ¶m_ops_##type, \
376 .elemsize = sizeof(array[0]), .elem = array }; \
377 __module_param_call(MODULE_PARAM_PREFIX, name, \
378 ¶m_array_ops, \
379 .arr = &__param_arr_##name, \
380 __same_type(array[0], bool), perm); \
381 __MODULE_PARM_TYPE(name, "array of " #type)
382
383extern struct kernel_param_ops param_array_ops;
384
385extern struct kernel_param_ops param_ops_string;
386extern int param_set_copystring(const char *val, const struct kernel_param *);
387extern int param_get_string(char *buffer, const struct kernel_param *kp);
388
389
390
391struct module;
392
393#if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
394extern int module_param_sysfs_setup(struct module *mod,
395 const struct kernel_param *kparam,
396 unsigned int num_params);
397
398extern void module_param_sysfs_remove(struct module *mod);
399#else
400static inline int module_param_sysfs_setup(struct module *mod,
401 const struct kernel_param *kparam,
402 unsigned int num_params)
403{
404 return 0;
405}
406
407static inline void module_param_sysfs_remove(struct module *mod)
408{ }
409#endif
410
411#endif
412