]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Helpers for initial module or kernel cmdline parsing |
2 | Copyright (C) 2001 Rusty Russell. | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
1da177e4 LT |
18 | #include <linux/moduleparam.h> |
19 | #include <linux/kernel.h> | |
20 | #include <linux/string.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/device.h> | |
24 | #include <linux/err.h> | |
4e57b681 | 25 | #include <linux/slab.h> |
1da177e4 LT |
26 | |
27 | #if 0 | |
28 | #define DEBUGP printk | |
29 | #else | |
30 | #define DEBUGP(fmt, a...) | |
31 | #endif | |
32 | ||
3514faca GKH |
33 | static struct kobj_type module_ktype; |
34 | ||
b9e20a92 | 35 | static inline char dash2underscore(char c) |
1da177e4 LT |
36 | { |
37 | if (c == '-') | |
38 | return '_'; | |
39 | return c; | |
40 | } | |
41 | ||
42 | static inline int parameq(const char *input, const char *paramname) | |
43 | { | |
44 | unsigned int i; | |
45 | for (i = 0; dash2underscore(input[i]) == paramname[i]; i++) | |
46 | if (input[i] == '\0') | |
47 | return 1; | |
48 | return 0; | |
49 | } | |
50 | ||
51 | static int parse_one(char *param, | |
52 | char *val, | |
53 | struct kernel_param *params, | |
54 | unsigned num_params, | |
55 | int (*handle_unknown)(char *param, char *val)) | |
56 | { | |
57 | unsigned int i; | |
58 | ||
59 | /* Find parameter */ | |
60 | for (i = 0; i < num_params; i++) { | |
61 | if (parameq(param, params[i].name)) { | |
62 | DEBUGP("They are equal! Calling %p\n", | |
63 | params[i].set); | |
64 | return params[i].set(val, ¶ms[i]); | |
65 | } | |
66 | } | |
67 | ||
68 | if (handle_unknown) { | |
69 | DEBUGP("Unknown argument: calling %p\n", handle_unknown); | |
70 | return handle_unknown(param, val); | |
71 | } | |
72 | ||
73 | DEBUGP("Unknown argument `%s'\n", param); | |
74 | return -ENOENT; | |
75 | } | |
76 | ||
77 | /* You can use " around spaces, but can't escape ". */ | |
78 | /* Hyphens and underscores equivalent in parameter names. */ | |
79 | static char *next_arg(char *args, char **param, char **val) | |
80 | { | |
81 | unsigned int i, equals = 0; | |
82 | int in_quote = 0, quoted = 0; | |
83 | char *next; | |
84 | ||
1da177e4 LT |
85 | if (*args == '"') { |
86 | args++; | |
87 | in_quote = 1; | |
88 | quoted = 1; | |
89 | } | |
90 | ||
91 | for (i = 0; args[i]; i++) { | |
92 | if (args[i] == ' ' && !in_quote) | |
93 | break; | |
94 | if (equals == 0) { | |
95 | if (args[i] == '=') | |
96 | equals = i; | |
97 | } | |
98 | if (args[i] == '"') | |
99 | in_quote = !in_quote; | |
100 | } | |
101 | ||
102 | *param = args; | |
103 | if (!equals) | |
104 | *val = NULL; | |
105 | else { | |
106 | args[equals] = '\0'; | |
107 | *val = args + equals + 1; | |
108 | ||
109 | /* Don't include quotes in value. */ | |
110 | if (**val == '"') { | |
111 | (*val)++; | |
112 | if (args[i-1] == '"') | |
113 | args[i-1] = '\0'; | |
114 | } | |
115 | if (quoted && args[i-1] == '"') | |
116 | args[i-1] = '\0'; | |
117 | } | |
118 | ||
119 | if (args[i]) { | |
120 | args[i] = '\0'; | |
121 | next = args + i + 1; | |
122 | } else | |
123 | next = args + i; | |
f36462f0 RR |
124 | |
125 | /* Chew up trailing spaces. */ | |
126 | while (*next == ' ') | |
127 | next++; | |
1da177e4 LT |
128 | return next; |
129 | } | |
130 | ||
131 | /* Args looks like "foo=bar,bar2 baz=fuz wiz". */ | |
132 | int parse_args(const char *name, | |
133 | char *args, | |
134 | struct kernel_param *params, | |
135 | unsigned num, | |
136 | int (*unknown)(char *param, char *val)) | |
137 | { | |
138 | char *param, *val; | |
139 | ||
140 | DEBUGP("Parsing ARGS: %s\n", args); | |
141 | ||
f36462f0 RR |
142 | /* Chew leading spaces */ |
143 | while (*args == ' ') | |
144 | args++; | |
145 | ||
1da177e4 LT |
146 | while (*args) { |
147 | int ret; | |
a416aba6 | 148 | int irq_was_disabled; |
1da177e4 LT |
149 | |
150 | args = next_arg(args, ¶m, &val); | |
a416aba6 | 151 | irq_was_disabled = irqs_disabled(); |
1da177e4 | 152 | ret = parse_one(param, val, params, num, unknown); |
a416aba6 AB |
153 | if (irq_was_disabled && !irqs_disabled()) { |
154 | printk(KERN_WARNING "parse_args(): option '%s' enabled " | |
155 | "irq's!\n", param); | |
156 | } | |
1da177e4 LT |
157 | switch (ret) { |
158 | case -ENOENT: | |
159 | printk(KERN_ERR "%s: Unknown parameter `%s'\n", | |
160 | name, param); | |
161 | return ret; | |
162 | case -ENOSPC: | |
163 | printk(KERN_ERR | |
164 | "%s: `%s' too large for parameter `%s'\n", | |
165 | name, val ?: "", param); | |
166 | return ret; | |
167 | case 0: | |
168 | break; | |
169 | default: | |
170 | printk(KERN_ERR | |
171 | "%s: `%s' invalid for parameter `%s'\n", | |
172 | name, val ?: "", param); | |
173 | return ret; | |
174 | } | |
175 | } | |
176 | ||
177 | /* All parsed OK. */ | |
178 | return 0; | |
179 | } | |
180 | ||
181 | /* Lazy bastard, eh? */ | |
182 | #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \ | |
183 | int param_set_##name(const char *val, struct kernel_param *kp) \ | |
184 | { \ | |
185 | char *endp; \ | |
186 | tmptype l; \ | |
187 | \ | |
188 | if (!val) return -EINVAL; \ | |
189 | l = strtolfn(val, &endp, 0); \ | |
190 | if (endp == val || ((type)l != l)) \ | |
191 | return -EINVAL; \ | |
192 | *((type *)kp->arg) = l; \ | |
193 | return 0; \ | |
194 | } \ | |
195 | int param_get_##name(char *buffer, struct kernel_param *kp) \ | |
196 | { \ | |
197 | return sprintf(buffer, format, *((type *)kp->arg)); \ | |
198 | } | |
199 | ||
200 | STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, simple_strtoul); | |
201 | STANDARD_PARAM_DEF(short, short, "%hi", long, simple_strtol); | |
202 | STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, simple_strtoul); | |
203 | STANDARD_PARAM_DEF(int, int, "%i", long, simple_strtol); | |
204 | STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, simple_strtoul); | |
205 | STANDARD_PARAM_DEF(long, long, "%li", long, simple_strtol); | |
206 | STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, simple_strtoul); | |
207 | ||
208 | int param_set_charp(const char *val, struct kernel_param *kp) | |
209 | { | |
210 | if (!val) { | |
211 | printk(KERN_ERR "%s: string parameter expected\n", | |
212 | kp->name); | |
213 | return -EINVAL; | |
214 | } | |
215 | ||
216 | if (strlen(val) > 1024) { | |
217 | printk(KERN_ERR "%s: string parameter too long\n", | |
218 | kp->name); | |
219 | return -ENOSPC; | |
220 | } | |
221 | ||
222 | *(char **)kp->arg = (char *)val; | |
223 | return 0; | |
224 | } | |
225 | ||
226 | int param_get_charp(char *buffer, struct kernel_param *kp) | |
227 | { | |
228 | return sprintf(buffer, "%s", *((char **)kp->arg)); | |
229 | } | |
230 | ||
231 | int param_set_bool(const char *val, struct kernel_param *kp) | |
232 | { | |
233 | /* No equals means "set"... */ | |
234 | if (!val) val = "1"; | |
235 | ||
236 | /* One of =[yYnN01] */ | |
237 | switch (val[0]) { | |
238 | case 'y': case 'Y': case '1': | |
239 | *(int *)kp->arg = 1; | |
240 | return 0; | |
241 | case 'n': case 'N': case '0': | |
242 | *(int *)kp->arg = 0; | |
243 | return 0; | |
244 | } | |
245 | return -EINVAL; | |
246 | } | |
247 | ||
248 | int param_get_bool(char *buffer, struct kernel_param *kp) | |
249 | { | |
250 | /* Y and N chosen as being relatively non-coder friendly */ | |
251 | return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'Y' : 'N'); | |
252 | } | |
253 | ||
254 | int param_set_invbool(const char *val, struct kernel_param *kp) | |
255 | { | |
256 | int boolval, ret; | |
22e48eaf | 257 | struct kernel_param dummy; |
1da177e4 | 258 | |
22e48eaf | 259 | dummy.arg = &boolval; |
1da177e4 LT |
260 | ret = param_set_bool(val, &dummy); |
261 | if (ret == 0) | |
262 | *(int *)kp->arg = !boolval; | |
263 | return ret; | |
264 | } | |
265 | ||
266 | int param_get_invbool(char *buffer, struct kernel_param *kp) | |
267 | { | |
22e48eaf | 268 | return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'N' : 'Y'); |
1da177e4 LT |
269 | } |
270 | ||
9730b5b0 | 271 | /* We break the rule and mangle the string. */ |
9871728b AB |
272 | static int param_array(const char *name, |
273 | const char *val, | |
274 | unsigned int min, unsigned int max, | |
275 | void *elem, int elemsize, | |
276 | int (*set)(const char *, struct kernel_param *kp), | |
277 | int *num) | |
1da177e4 LT |
278 | { |
279 | int ret; | |
280 | struct kernel_param kp; | |
281 | char save; | |
282 | ||
283 | /* Get the name right for errors. */ | |
284 | kp.name = name; | |
285 | kp.arg = elem; | |
286 | ||
287 | /* No equals sign? */ | |
288 | if (!val) { | |
289 | printk(KERN_ERR "%s: expects arguments\n", name); | |
290 | return -EINVAL; | |
291 | } | |
292 | ||
293 | *num = 0; | |
294 | /* We expect a comma-separated list of values. */ | |
295 | do { | |
296 | int len; | |
297 | ||
298 | if (*num == max) { | |
299 | printk(KERN_ERR "%s: can only take %i arguments\n", | |
300 | name, max); | |
301 | return -EINVAL; | |
302 | } | |
303 | len = strcspn(val, ","); | |
304 | ||
305 | /* nul-terminate and parse */ | |
306 | save = val[len]; | |
307 | ((char *)val)[len] = '\0'; | |
308 | ret = set(val, &kp); | |
309 | ||
310 | if (ret != 0) | |
311 | return ret; | |
312 | kp.arg += elemsize; | |
313 | val += len+1; | |
314 | (*num)++; | |
315 | } while (save == ','); | |
316 | ||
317 | if (*num < min) { | |
318 | printk(KERN_ERR "%s: needs at least %i arguments\n", | |
319 | name, min); | |
320 | return -EINVAL; | |
321 | } | |
322 | return 0; | |
323 | } | |
324 | ||
325 | int param_array_set(const char *val, struct kernel_param *kp) | |
326 | { | |
22e48eaf | 327 | const struct kparam_array *arr = kp->arr; |
31143a12 | 328 | unsigned int temp_num; |
1da177e4 LT |
329 | |
330 | return param_array(kp->name, val, 1, arr->max, arr->elem, | |
31143a12 | 331 | arr->elemsize, arr->set, arr->num ?: &temp_num); |
1da177e4 LT |
332 | } |
333 | ||
334 | int param_array_get(char *buffer, struct kernel_param *kp) | |
335 | { | |
336 | int i, off, ret; | |
22e48eaf | 337 | const struct kparam_array *arr = kp->arr; |
1da177e4 LT |
338 | struct kernel_param p; |
339 | ||
340 | p = *kp; | |
341 | for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) { | |
342 | if (i) | |
343 | buffer[off++] = ','; | |
344 | p.arg = arr->elem + arr->elemsize * i; | |
345 | ret = arr->get(buffer + off, &p); | |
346 | if (ret < 0) | |
347 | return ret; | |
348 | off += ret; | |
349 | } | |
350 | buffer[off] = '\0'; | |
351 | return off; | |
352 | } | |
353 | ||
354 | int param_set_copystring(const char *val, struct kernel_param *kp) | |
355 | { | |
22e48eaf | 356 | const struct kparam_string *kps = kp->str; |
1da177e4 | 357 | |
fe20e581 RD |
358 | if (!val) { |
359 | printk(KERN_ERR "%s: missing param set value\n", kp->name); | |
360 | return -EINVAL; | |
361 | } | |
1da177e4 LT |
362 | if (strlen(val)+1 > kps->maxlen) { |
363 | printk(KERN_ERR "%s: string doesn't fit in %u chars.\n", | |
364 | kp->name, kps->maxlen-1); | |
365 | return -ENOSPC; | |
366 | } | |
367 | strcpy(kps->string, val); | |
368 | return 0; | |
369 | } | |
370 | ||
371 | int param_get_string(char *buffer, struct kernel_param *kp) | |
372 | { | |
22e48eaf | 373 | const struct kparam_string *kps = kp->str; |
1da177e4 LT |
374 | return strlcpy(buffer, kps->string, kps->maxlen); |
375 | } | |
376 | ||
377 | /* sysfs output in /sys/modules/XYZ/parameters/ */ | |
378 | ||
379 | extern struct kernel_param __start___param[], __stop___param[]; | |
380 | ||
381 | #define MAX_KBUILD_MODNAME KOBJ_NAME_LEN | |
382 | ||
383 | struct param_attribute | |
384 | { | |
385 | struct module_attribute mattr; | |
386 | struct kernel_param *param; | |
387 | }; | |
388 | ||
389 | struct module_param_attrs | |
390 | { | |
391 | struct attribute_group grp; | |
392 | struct param_attribute attrs[0]; | |
393 | }; | |
394 | ||
ef665c1a | 395 | #ifdef CONFIG_SYSFS |
1da177e4 LT |
396 | #define to_param_attr(n) container_of(n, struct param_attribute, mattr); |
397 | ||
398 | static ssize_t param_attr_show(struct module_attribute *mattr, | |
399 | struct module *mod, char *buf) | |
400 | { | |
401 | int count; | |
402 | struct param_attribute *attribute = to_param_attr(mattr); | |
403 | ||
404 | if (!attribute->param->get) | |
405 | return -EPERM; | |
406 | ||
407 | count = attribute->param->get(buf, attribute->param); | |
408 | if (count > 0) { | |
409 | strcat(buf, "\n"); | |
410 | ++count; | |
411 | } | |
412 | return count; | |
413 | } | |
414 | ||
415 | /* sysfs always hands a nul-terminated string in buf. We rely on that. */ | |
416 | static ssize_t param_attr_store(struct module_attribute *mattr, | |
417 | struct module *owner, | |
418 | const char *buf, size_t len) | |
419 | { | |
420 | int err; | |
421 | struct param_attribute *attribute = to_param_attr(mattr); | |
422 | ||
423 | if (!attribute->param->set) | |
424 | return -EPERM; | |
425 | ||
426 | err = attribute->param->set(buf, attribute->param); | |
427 | if (!err) | |
428 | return len; | |
429 | return err; | |
430 | } | |
ef665c1a | 431 | #endif |
1da177e4 LT |
432 | |
433 | #ifdef CONFIG_MODULES | |
434 | #define __modinit | |
435 | #else | |
436 | #define __modinit __init | |
437 | #endif | |
438 | ||
ef665c1a | 439 | #ifdef CONFIG_SYSFS |
1da177e4 LT |
440 | /* |
441 | * param_sysfs_setup - setup sysfs support for one module or KBUILD_MODNAME | |
442 | * @mk: struct module_kobject (contains parent kobject) | |
443 | * @kparam: array of struct kernel_param, the actual parameter definitions | |
444 | * @num_params: number of entries in array | |
445 | * @name_skip: offset where the parameter name start in kparam[].name. Needed for built-in "modules" | |
446 | * | |
447 | * Create a kobject for a (per-module) group of parameters, and create files | |
448 | * in sysfs. A pointer to the param_kobject is returned on success, | |
449 | * NULL if there's no parameter to export, or other ERR_PTR(err). | |
450 | */ | |
451 | static __modinit struct module_param_attrs * | |
452 | param_sysfs_setup(struct module_kobject *mk, | |
453 | struct kernel_param *kparam, | |
454 | unsigned int num_params, | |
455 | unsigned int name_skip) | |
456 | { | |
457 | struct module_param_attrs *mp; | |
458 | unsigned int valid_attrs = 0; | |
459 | unsigned int i, size[2]; | |
460 | struct param_attribute *pattr; | |
461 | struct attribute **gattr; | |
462 | int err; | |
463 | ||
464 | for (i=0; i<num_params; i++) { | |
465 | if (kparam[i].perm) | |
466 | valid_attrs++; | |
467 | } | |
468 | ||
469 | if (!valid_attrs) | |
470 | return NULL; | |
471 | ||
472 | size[0] = ALIGN(sizeof(*mp) + | |
473 | valid_attrs * sizeof(mp->attrs[0]), | |
474 | sizeof(mp->grp.attrs[0])); | |
475 | size[1] = (valid_attrs + 1) * sizeof(mp->grp.attrs[0]); | |
476 | ||
477 | mp = kmalloc(size[0] + size[1], GFP_KERNEL); | |
478 | if (!mp) | |
479 | return ERR_PTR(-ENOMEM); | |
480 | ||
481 | mp->grp.name = "parameters"; | |
482 | mp->grp.attrs = (void *)mp + size[0]; | |
483 | ||
484 | pattr = &mp->attrs[0]; | |
485 | gattr = &mp->grp.attrs[0]; | |
486 | for (i = 0; i < num_params; i++) { | |
487 | struct kernel_param *kp = &kparam[i]; | |
488 | if (kp->perm) { | |
489 | pattr->param = kp; | |
490 | pattr->mattr.show = param_attr_show; | |
491 | pattr->mattr.store = param_attr_store; | |
492 | pattr->mattr.attr.name = (char *)&kp->name[name_skip]; | |
1da177e4 LT |
493 | pattr->mattr.attr.mode = kp->perm; |
494 | *(gattr++) = &(pattr++)->mattr.attr; | |
495 | } | |
496 | } | |
497 | *gattr = NULL; | |
498 | ||
499 | if ((err = sysfs_create_group(&mk->kobj, &mp->grp))) { | |
500 | kfree(mp); | |
501 | return ERR_PTR(err); | |
502 | } | |
503 | return mp; | |
504 | } | |
505 | ||
1da177e4 | 506 | #ifdef CONFIG_MODULES |
1da177e4 LT |
507 | /* |
508 | * module_param_sysfs_setup - setup sysfs support for one module | |
509 | * @mod: module | |
510 | * @kparam: module parameters (array) | |
511 | * @num_params: number of module parameters | |
512 | * | |
513 | * Adds sysfs entries for module parameters, and creates a link from | |
514 | * /sys/module/[mod->name]/parameters to /sys/parameters/[mod->name]/ | |
515 | */ | |
516 | int module_param_sysfs_setup(struct module *mod, | |
517 | struct kernel_param *kparam, | |
518 | unsigned int num_params) | |
519 | { | |
520 | struct module_param_attrs *mp; | |
521 | ||
522 | mp = param_sysfs_setup(&mod->mkobj, kparam, num_params, 0); | |
523 | if (IS_ERR(mp)) | |
524 | return PTR_ERR(mp); | |
525 | ||
526 | mod->param_attrs = mp; | |
527 | return 0; | |
528 | } | |
529 | ||
530 | /* | |
531 | * module_param_sysfs_remove - remove sysfs support for one module | |
532 | * @mod: module | |
533 | * | |
534 | * Remove sysfs entries for module parameters and the corresponding | |
535 | * kobject. | |
536 | */ | |
537 | void module_param_sysfs_remove(struct module *mod) | |
538 | { | |
539 | if (mod->param_attrs) { | |
540 | sysfs_remove_group(&mod->mkobj.kobj, | |
541 | &mod->param_attrs->grp); | |
542 | /* We are positive that no one is using any param | |
543 | * attrs at this point. Deallocate immediately. */ | |
544 | kfree(mod->param_attrs); | |
545 | mod->param_attrs = NULL; | |
546 | } | |
547 | } | |
548 | #endif | |
549 | ||
550 | /* | |
551 | * kernel_param_sysfs_setup - wrapper for built-in params support | |
552 | */ | |
553 | static void __init kernel_param_sysfs_setup(const char *name, | |
554 | struct kernel_param *kparam, | |
555 | unsigned int num_params, | |
556 | unsigned int name_skip) | |
557 | { | |
558 | struct module_kobject *mk; | |
d8c7649e | 559 | int ret; |
1da177e4 | 560 | |
dd392710 PE |
561 | mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL); |
562 | BUG_ON(!mk); | |
1da177e4 LT |
563 | |
564 | mk->mod = THIS_MODULE; | |
3514faca GKH |
565 | mk->kobj.kset = &module_subsys; |
566 | mk->kobj.ktype = &module_ktype; | |
1da177e4 | 567 | kobject_set_name(&mk->kobj, name); |
f30c53a8 KS |
568 | kobject_init(&mk->kobj); |
569 | ret = kobject_add(&mk->kobj); | |
74c5b597 GKH |
570 | if (ret) { |
571 | printk(KERN_ERR "Module '%s' failed to be added to sysfs, " | |
572 | "error number %d\n", name, ret); | |
573 | printk(KERN_ERR "The system will be unstable now.\n"); | |
574 | return; | |
575 | } | |
f30c53a8 | 576 | param_sysfs_setup(mk, kparam, num_params, name_skip); |
f30c53a8 | 577 | kobject_uevent(&mk->kobj, KOBJ_ADD); |
1da177e4 LT |
578 | } |
579 | ||
580 | /* | |
581 | * param_sysfs_builtin - add contents in /sys/parameters for built-in modules | |
582 | * | |
583 | * Add module_parameters to sysfs for "modules" built into the kernel. | |
584 | * | |
585 | * The "module" name (KBUILD_MODNAME) is stored before a dot, the | |
586 | * "parameter" name is stored behind a dot in kernel_param->name. So, | |
587 | * extract the "module" name for all built-in kernel_param-eters, | |
588 | * and for all who have the same, call kernel_param_sysfs_setup. | |
589 | */ | |
590 | static void __init param_sysfs_builtin(void) | |
591 | { | |
592 | struct kernel_param *kp, *kp_begin = NULL; | |
593 | unsigned int i, name_len, count = 0; | |
594 | char modname[MAX_KBUILD_MODNAME + 1] = ""; | |
595 | ||
596 | for (i=0; i < __stop___param - __start___param; i++) { | |
597 | char *dot; | |
22800a28 | 598 | size_t max_name_len; |
1da177e4 LT |
599 | |
600 | kp = &__start___param[i]; | |
22800a28 JK |
601 | max_name_len = |
602 | min_t(size_t, MAX_KBUILD_MODNAME, strlen(kp->name)); | |
1da177e4 | 603 | |
22800a28 | 604 | dot = memchr(kp->name, '.', max_name_len); |
1da177e4 | 605 | if (!dot) { |
22800a28 JK |
606 | DEBUGP("couldn't find period in first %d characters " |
607 | "of %s\n", MAX_KBUILD_MODNAME, kp->name); | |
1da177e4 LT |
608 | continue; |
609 | } | |
610 | name_len = dot - kp->name; | |
611 | ||
612 | /* new kbuild_modname? */ | |
613 | if (strlen(modname) != name_len | |
614 | || strncmp(modname, kp->name, name_len) != 0) { | |
615 | /* add a new kobject for previous kernel_params. */ | |
616 | if (count) | |
617 | kernel_param_sysfs_setup(modname, | |
618 | kp_begin, | |
619 | count, | |
620 | strlen(modname)+1); | |
621 | ||
622 | strncpy(modname, kp->name, name_len); | |
623 | modname[name_len] = '\0'; | |
624 | count = 0; | |
625 | kp_begin = kp; | |
626 | } | |
627 | count++; | |
628 | } | |
629 | ||
630 | /* last kernel_params need to be registered as well */ | |
631 | if (count) | |
632 | kernel_param_sysfs_setup(modname, kp_begin, count, | |
633 | strlen(modname)+1); | |
634 | } | |
635 | ||
636 | ||
637 | /* module-related sysfs stuff */ | |
1da177e4 LT |
638 | |
639 | #define to_module_attr(n) container_of(n, struct module_attribute, attr); | |
640 | #define to_module_kobject(n) container_of(n, struct module_kobject, kobj); | |
641 | ||
642 | static ssize_t module_attr_show(struct kobject *kobj, | |
643 | struct attribute *attr, | |
644 | char *buf) | |
645 | { | |
646 | struct module_attribute *attribute; | |
647 | struct module_kobject *mk; | |
648 | int ret; | |
649 | ||
650 | attribute = to_module_attr(attr); | |
651 | mk = to_module_kobject(kobj); | |
652 | ||
653 | if (!attribute->show) | |
70f2817a | 654 | return -EIO; |
1da177e4 | 655 | |
1da177e4 LT |
656 | ret = attribute->show(attribute, mk->mod, buf); |
657 | ||
1da177e4 LT |
658 | return ret; |
659 | } | |
660 | ||
661 | static ssize_t module_attr_store(struct kobject *kobj, | |
662 | struct attribute *attr, | |
663 | const char *buf, size_t len) | |
664 | { | |
665 | struct module_attribute *attribute; | |
666 | struct module_kobject *mk; | |
667 | int ret; | |
668 | ||
669 | attribute = to_module_attr(attr); | |
670 | mk = to_module_kobject(kobj); | |
671 | ||
672 | if (!attribute->store) | |
70f2817a | 673 | return -EIO; |
1da177e4 | 674 | |
1da177e4 LT |
675 | ret = attribute->store(attribute, mk->mod, buf, len); |
676 | ||
1da177e4 LT |
677 | return ret; |
678 | } | |
679 | ||
680 | static struct sysfs_ops module_sysfs_ops = { | |
681 | .show = module_attr_show, | |
682 | .store = module_attr_store, | |
683 | }; | |
684 | ||
270a6c4c KS |
685 | static int uevent_filter(struct kset *kset, struct kobject *kobj) |
686 | { | |
687 | struct kobj_type *ktype = get_ktype(kobj); | |
688 | ||
689 | if (ktype == &module_ktype) | |
690 | return 1; | |
691 | return 0; | |
692 | } | |
693 | ||
694 | static struct kset_uevent_ops module_uevent_ops = { | |
695 | .filter = uevent_filter, | |
696 | }; | |
697 | ||
3514faca | 698 | decl_subsys(module, &module_uevent_ops); |
823bccfc | 699 | int module_sysfs_initialized; |
1da177e4 | 700 | |
d172f4ef GKH |
701 | static void module_release(struct kobject *kobj) |
702 | { | |
703 | /* | |
704 | * Stupid empty release function to allow the memory for the kobject to | |
705 | * be properly cleaned up. This will not need to be present for 2.6.25 | |
706 | * with the upcoming kobject core rework. | |
707 | */ | |
708 | } | |
709 | ||
1da177e4 LT |
710 | static struct kobj_type module_ktype = { |
711 | .sysfs_ops = &module_sysfs_ops, | |
d172f4ef | 712 | .release = module_release, |
1da177e4 LT |
713 | }; |
714 | ||
1da177e4 LT |
715 | /* |
716 | * param_sysfs_init - wrapper for built-in params support | |
717 | */ | |
718 | static int __init param_sysfs_init(void) | |
719 | { | |
d8c7649e RD |
720 | int ret; |
721 | ||
722 | ret = subsystem_register(&module_subsys); | |
723 | if (ret < 0) { | |
724 | printk(KERN_WARNING "%s (%d): subsystem_register error: %d\n", | |
725 | __FILE__, __LINE__, ret); | |
726 | return ret; | |
727 | } | |
823bccfc | 728 | module_sysfs_initialized = 1; |
1da177e4 LT |
729 | |
730 | param_sysfs_builtin(); | |
731 | ||
732 | return 0; | |
733 | } | |
d10be6d1 | 734 | subsys_initcall(param_sysfs_init); |
1da177e4 | 735 | |
ef665c1a RD |
736 | #else |
737 | #if 0 | |
738 | static struct sysfs_ops module_sysfs_ops = { | |
739 | .show = NULL, | |
740 | .store = NULL, | |
741 | }; | |
742 | #endif | |
743 | #endif | |
744 | ||
1da177e4 LT |
745 | EXPORT_SYMBOL(param_set_byte); |
746 | EXPORT_SYMBOL(param_get_byte); | |
747 | EXPORT_SYMBOL(param_set_short); | |
748 | EXPORT_SYMBOL(param_get_short); | |
749 | EXPORT_SYMBOL(param_set_ushort); | |
750 | EXPORT_SYMBOL(param_get_ushort); | |
751 | EXPORT_SYMBOL(param_set_int); | |
752 | EXPORT_SYMBOL(param_get_int); | |
753 | EXPORT_SYMBOL(param_set_uint); | |
754 | EXPORT_SYMBOL(param_get_uint); | |
755 | EXPORT_SYMBOL(param_set_long); | |
756 | EXPORT_SYMBOL(param_get_long); | |
757 | EXPORT_SYMBOL(param_set_ulong); | |
758 | EXPORT_SYMBOL(param_get_ulong); | |
759 | EXPORT_SYMBOL(param_set_charp); | |
760 | EXPORT_SYMBOL(param_get_charp); | |
761 | EXPORT_SYMBOL(param_set_bool); | |
762 | EXPORT_SYMBOL(param_get_bool); | |
763 | EXPORT_SYMBOL(param_set_invbool); | |
764 | EXPORT_SYMBOL(param_get_invbool); | |
765 | EXPORT_SYMBOL(param_array_set); | |
766 | EXPORT_SYMBOL(param_array_get); | |
767 | EXPORT_SYMBOL(param_set_copystring); | |
768 | EXPORT_SYMBOL(param_get_string); |