x86/PCI: use host bridge _CRS info on MSI MS-7253
[linux-flexiantxendom0.git] / kernel / params.c
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 */
18 #include <linux/module.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>
25 #include <linux/slab.h>
26 #include <linux/ctype.h>
27
28 #if 0
29 #define DEBUGP printk
30 #else
31 #define DEBUGP(fmt, a...)
32 #endif
33
34 /* Protects all parameters, and incidentally kmalloced_param list. */
35 static DEFINE_MUTEX(param_lock);
36
37 /* This just allows us to keep track of which parameters are kmalloced. */
38 struct kmalloced_param {
39         struct list_head list;
40         char val[];
41 };
42 static LIST_HEAD(kmalloced_params);
43
44 static void *kmalloc_parameter(unsigned int size)
45 {
46         struct kmalloced_param *p;
47
48         p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
49         if (!p)
50                 return NULL;
51
52         list_add(&p->list, &kmalloced_params);
53         return p->val;
54 }
55
56 /* Does nothing if parameter wasn't kmalloced above. */
57 static void maybe_kfree_parameter(void *param)
58 {
59         struct kmalloced_param *p;
60
61         list_for_each_entry(p, &kmalloced_params, list) {
62                 if (p->val == param) {
63                         list_del(&p->list);
64                         kfree(p);
65                         break;
66                 }
67         }
68 }
69
70 static char dash2underscore(char c)
71 {
72         if (c == '-')
73                 return '_';
74         return c;
75 }
76
77 bool parameqn(const char *a, const char *b, size_t n)
78 {
79         size_t i;
80
81         for (i = 0; i < n; i++) {
82                 if (dash2underscore(a[i]) != dash2underscore(b[i]))
83                         return false;
84         }
85         return true;
86 }
87
88 bool parameq(const char *a, const char *b)
89 {
90         return parameqn(a, b, strlen(a)+1);
91 }
92
93 static int parse_one(char *param,
94                      char *val,
95                      const struct kernel_param *params,
96                      unsigned num_params,
97                      int (*handle_arg)(char *param, char *val, int known))
98 {
99         unsigned int i;
100         int err;
101
102         /* Find parameter */
103         for (i = 0; i < num_params; i++) {
104                 if (parameq(param, params[i].name)) {
105                         /* No one handled NULL, so do it here. */
106                         if (!val && params[i].ops->set != param_set_bool)
107                                 return -EINVAL;
108                         if (handle_arg) {
109                                 int ret;
110                                 DEBUGP("Valid argument: calling %p\n",
111                                        handle_arg);
112                                 ret = handle_arg(param, val, 1);
113                                 if (ret)
114                                         return ret;
115                         }
116                         DEBUGP("They are equal!  Calling %p\n",
117                                params[i].ops->set);
118                         mutex_lock(&param_lock);
119                         err = params[i].ops->set(val, &params[i]);
120                         mutex_unlock(&param_lock);
121                         return err;
122                 }
123         }
124
125         if (handle_arg) {
126                 DEBUGP("Unknown argument: calling %p\n", handle_arg);
127                 return handle_arg(param, val, 0);
128         }
129
130         DEBUGP("Unknown argument `%s'\n", param);
131         return -ENOENT;
132 }
133
134 /* You can use " around spaces, but can't escape ". */
135 /* Hyphens and underscores equivalent in parameter names. */
136 static char *next_arg(char *args, char **param, char **val)
137 {
138         unsigned int i, equals = 0;
139         int in_quote = 0, quoted = 0;
140         char *next;
141
142         if (*args == '"') {
143                 args++;
144                 in_quote = 1;
145                 quoted = 1;
146         }
147
148         for (i = 0; args[i]; i++) {
149                 if (isspace(args[i]) && !in_quote)
150                         break;
151                 if (equals == 0) {
152                         if (args[i] == '=')
153                                 equals = i;
154                 }
155                 if (args[i] == '"')
156                         in_quote = !in_quote;
157         }
158
159         *param = args;
160         if (!equals)
161                 *val = NULL;
162         else {
163                 args[equals] = '\0';
164                 *val = args + equals + 1;
165
166                 /* Don't include quotes in value. */
167                 if (**val == '"') {
168                         (*val)++;
169                         if (args[i-1] == '"')
170                                 args[i-1] = '\0';
171                 }
172                 if (quoted && args[i-1] == '"')
173                         args[i-1] = '\0';
174         }
175
176         if (args[i]) {
177                 args[i] = '\0';
178                 next = args + i + 1;
179         } else
180                 next = args + i;
181
182         /* Chew up trailing spaces. */
183         return skip_spaces(next);
184 }
185
186 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
187 int parse_args(const char *name,
188                char *args,
189                const struct kernel_param *params,
190                unsigned num,
191                int (*handle_arg)(char *param, char *val, int arg))
192 {
193         char *param, *val;
194
195         DEBUGP("Parsing ARGS: %s\n", args);
196
197         /* Chew leading spaces */
198         args = skip_spaces(args);
199
200         while (*args) {
201                 int ret;
202                 int irq_was_disabled;
203
204                 args = next_arg(args, &param, &val);
205                 irq_was_disabled = irqs_disabled();
206                 ret = parse_one(param, val, params, num, handle_arg);
207                 if (irq_was_disabled && !irqs_disabled()) {
208                         printk(KERN_WARNING "parse_args(): option '%s' enabled "
209                                         "irq's!\n", param);
210                 }
211                 switch (ret) {
212                 case -ENOENT:
213                         printk(KERN_ERR "%s: Unknown parameter `%s'\n",
214                                name, param);
215                         return ret;
216                 case -ENOSPC:
217                         printk(KERN_ERR
218                                "%s: `%s' too large for parameter `%s'\n",
219                                name, val ?: "", param);
220                         return ret;
221                 case 0:
222                         break;
223                 default:
224                         printk(KERN_ERR
225                                "%s: `%s' invalid for parameter `%s'\n",
226                                name, val ?: "", param);
227                         return ret;
228                 }
229         }
230
231         /* All parsed OK. */
232         return 0;
233 }
234
235 /* Lazy bastard, eh? */
236 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn)       \
237         int param_set_##name(const char *val, const struct kernel_param *kp) \
238         {                                                               \
239                 tmptype l;                                              \
240                 int ret;                                                \
241                                                                         \
242                 ret = strtolfn(val, 0, &l);                             \
243                 if (ret < 0 || ((type)l != l))                          \
244                         return ret < 0 ? ret : -EINVAL;                 \
245                 *((type *)kp->arg) = l;                                 \
246                 return 0;                                               \
247         }                                                               \
248         int param_get_##name(char *buffer, const struct kernel_param *kp) \
249         {                                                               \
250                 return sprintf(buffer, format, *((type *)kp->arg));     \
251         }                                                               \
252         struct kernel_param_ops param_ops_##name = {                    \
253                 .set = param_set_##name,                                \
254                 .get = param_get_##name,                                \
255         };                                                              \
256         EXPORT_SYMBOL(param_set_##name);                                \
257         EXPORT_SYMBOL(param_get_##name);                                \
258         EXPORT_SYMBOL(param_ops_##name)
259
260
261 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
262 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
263 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
264 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
265 STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul);
266 STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
267 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
268
269 int param_set_charp(const char *val, const struct kernel_param *kp)
270 {
271         if (strlen(val) > 1024) {
272                 printk(KERN_ERR "%s: string parameter too long\n",
273                        kp->name);
274                 return -ENOSPC;
275         }
276
277         maybe_kfree_parameter(*(char **)kp->arg);
278
279         /* This is a hack.  We can't kmalloc in early boot, and we
280          * don't need to; this mangled commandline is preserved. */
281         if (slab_is_available()) {
282                 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
283                 if (!*(char **)kp->arg)
284                         return -ENOMEM;
285                 strcpy(*(char **)kp->arg, val);
286         } else
287                 *(const char **)kp->arg = val;
288
289         return 0;
290 }
291 EXPORT_SYMBOL(param_set_charp);
292
293 int param_get_charp(char *buffer, const struct kernel_param *kp)
294 {
295         return sprintf(buffer, "%s", *((char **)kp->arg));
296 }
297 EXPORT_SYMBOL(param_get_charp);
298
299 static void param_free_charp(void *arg)
300 {
301         maybe_kfree_parameter(*((char **)arg));
302 }
303
304 struct kernel_param_ops param_ops_charp = {
305         .set = param_set_charp,
306         .get = param_get_charp,
307         .free = param_free_charp,
308 };
309 EXPORT_SYMBOL(param_ops_charp);
310
311 /* Actually could be a bool or an int, for historical reasons. */
312 int param_set_bool(const char *val, const struct kernel_param *kp)
313 {
314         bool v;
315         int ret;
316
317         /* No equals means "set"... */
318         if (!val) val = "1";
319
320         /* One of =[yYnN01] */
321         ret = strtobool(val, &v);
322         if (ret)
323                 return ret;
324
325         if (kp->flags & KPARAM_ISBOOL)
326                 *(bool *)kp->arg = v;
327         else
328                 *(int *)kp->arg = v;
329         return 0;
330 }
331 EXPORT_SYMBOL(param_set_bool);
332
333 int param_get_bool(char *buffer, const struct kernel_param *kp)
334 {
335         bool val;
336         if (kp->flags & KPARAM_ISBOOL)
337                 val = *(bool *)kp->arg;
338         else
339                 val = *(int *)kp->arg;
340
341         /* Y and N chosen as being relatively non-coder friendly */
342         return sprintf(buffer, "%c", val ? 'Y' : 'N');
343 }
344 EXPORT_SYMBOL(param_get_bool);
345
346 struct kernel_param_ops param_ops_bool = {
347         .set = param_set_bool,
348         .get = param_get_bool,
349 };
350 EXPORT_SYMBOL(param_ops_bool);
351
352 /* This one must be bool. */
353 int param_set_invbool(const char *val, const struct kernel_param *kp)
354 {
355         int ret;
356         bool boolval;
357         struct kernel_param dummy;
358
359         dummy.arg = &boolval;
360         dummy.flags = KPARAM_ISBOOL;
361         ret = param_set_bool(val, &dummy);
362         if (ret == 0)
363                 *(bool *)kp->arg = !boolval;
364         return ret;
365 }
366 EXPORT_SYMBOL(param_set_invbool);
367
368 int param_get_invbool(char *buffer, const struct kernel_param *kp)
369 {
370         return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
371 }
372 EXPORT_SYMBOL(param_get_invbool);
373
374 struct kernel_param_ops param_ops_invbool = {
375         .set = param_set_invbool,
376         .get = param_get_invbool,
377 };
378 EXPORT_SYMBOL(param_ops_invbool);
379
380 /* We break the rule and mangle the string. */
381 static int param_array(const char *name,
382                        const char *val,
383                        unsigned int min, unsigned int max,
384                        void *elem, int elemsize,
385                        int (*set)(const char *, const struct kernel_param *kp),
386                        u16 flags,
387                        unsigned int *num)
388 {
389         int ret;
390         struct kernel_param kp;
391         char save;
392
393         /* Get the name right for errors. */
394         kp.name = name;
395         kp.arg = elem;
396         kp.flags = flags;
397
398         *num = 0;
399         /* We expect a comma-separated list of values. */
400         do {
401                 int len;
402
403                 if (*num == max) {
404                         printk(KERN_ERR "%s: can only take %i arguments\n",
405                                name, max);
406                         return -EINVAL;
407                 }
408                 len = strcspn(val, ",");
409
410                 /* nul-terminate and parse */
411                 save = val[len];
412                 ((char *)val)[len] = '\0';
413                 BUG_ON(!mutex_is_locked(&param_lock));
414                 ret = set(val, &kp);
415
416                 if (ret != 0)
417                         return ret;
418                 kp.arg += elemsize;
419                 val += len+1;
420                 (*num)++;
421         } while (save == ',');
422
423         if (*num < min) {
424                 printk(KERN_ERR "%s: needs at least %i arguments\n",
425                        name, min);
426                 return -EINVAL;
427         }
428         return 0;
429 }
430
431 static int param_array_set(const char *val, const struct kernel_param *kp)
432 {
433         const struct kparam_array *arr = kp->arr;
434         unsigned int temp_num;
435
436         return param_array(kp->name, val, 1, arr->max, arr->elem,
437                            arr->elemsize, arr->ops->set, kp->flags,
438                            arr->num ?: &temp_num);
439 }
440
441 static int param_array_get(char *buffer, const struct kernel_param *kp)
442 {
443         int i, off, ret;
444         const struct kparam_array *arr = kp->arr;
445         struct kernel_param p;
446
447         p = *kp;
448         for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
449                 if (i)
450                         buffer[off++] = ',';
451                 p.arg = arr->elem + arr->elemsize * i;
452                 BUG_ON(!mutex_is_locked(&param_lock));
453                 ret = arr->ops->get(buffer + off, &p);
454                 if (ret < 0)
455                         return ret;
456                 off += ret;
457         }
458         buffer[off] = '\0';
459         return off;
460 }
461
462 static void param_array_free(void *arg)
463 {
464         unsigned int i;
465         const struct kparam_array *arr = arg;
466
467         if (arr->ops->free)
468                 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
469                         arr->ops->free(arr->elem + arr->elemsize * i);
470 }
471
472 struct kernel_param_ops param_array_ops = {
473         .set = param_array_set,
474         .get = param_array_get,
475         .free = param_array_free,
476 };
477 EXPORT_SYMBOL(param_array_ops);
478
479 int param_set_copystring(const char *val, const struct kernel_param *kp)
480 {
481         const struct kparam_string *kps = kp->str;
482
483         if (strlen(val)+1 > kps->maxlen) {
484                 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
485                        kp->name, kps->maxlen-1);
486                 return -ENOSPC;
487         }
488         strcpy(kps->string, val);
489         return 0;
490 }
491 EXPORT_SYMBOL(param_set_copystring);
492
493 int param_get_string(char *buffer, const struct kernel_param *kp)
494 {
495         const struct kparam_string *kps = kp->str;
496         return strlcpy(buffer, kps->string, kps->maxlen);
497 }
498 EXPORT_SYMBOL(param_get_string);
499
500 struct kernel_param_ops param_ops_string = {
501         .set = param_set_copystring,
502         .get = param_get_string,
503 };
504 EXPORT_SYMBOL(param_ops_string);
505
506 /* sysfs output in /sys/modules/XYZ/parameters/ */
507 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
508 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
509
510 extern struct kernel_param __start___param[], __stop___param[];
511
512 struct param_attribute
513 {
514         struct module_attribute mattr;
515         const struct kernel_param *param;
516 };
517
518 struct module_param_attrs
519 {
520         unsigned int num;
521         struct attribute_group grp;
522         struct param_attribute attrs[0];
523 };
524
525 #ifdef CONFIG_SYSFS
526 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
527
528 static ssize_t param_attr_show(struct module_attribute *mattr,
529                                struct module_kobject *mk, char *buf)
530 {
531         int count;
532         struct param_attribute *attribute = to_param_attr(mattr);
533
534         if (!attribute->param->ops->get)
535                 return -EPERM;
536
537         mutex_lock(&param_lock);
538         count = attribute->param->ops->get(buf, attribute->param);
539         mutex_unlock(&param_lock);
540         if (count > 0) {
541                 strcat(buf, "\n");
542                 ++count;
543         }
544         return count;
545 }
546
547 /* sysfs always hands a nul-terminated string in buf.  We rely on that. */
548 static ssize_t param_attr_store(struct module_attribute *mattr,
549                                 struct module_kobject *km,
550                                 const char *buf, size_t len)
551 {
552         int err;
553         struct param_attribute *attribute = to_param_attr(mattr);
554
555         if (!attribute->param->ops->set)
556                 return -EPERM;
557
558         mutex_lock(&param_lock);
559         err = attribute->param->ops->set(buf, attribute->param);
560         mutex_unlock(&param_lock);
561         if (!err)
562                 return len;
563         return err;
564 }
565 #endif
566
567 #ifdef CONFIG_MODULES
568 #define __modinit
569 #else
570 #define __modinit __init
571 #endif
572
573 #ifdef CONFIG_SYSFS
574 void __kernel_param_lock(void)
575 {
576         mutex_lock(&param_lock);
577 }
578 EXPORT_SYMBOL(__kernel_param_lock);
579
580 void __kernel_param_unlock(void)
581 {
582         mutex_unlock(&param_lock);
583 }
584 EXPORT_SYMBOL(__kernel_param_unlock);
585
586 /*
587  * add_sysfs_param - add a parameter to sysfs
588  * @mk: struct module_kobject
589  * @kparam: the actual parameter definition to add to sysfs
590  * @name: name of parameter
591  *
592  * Create a kobject if for a (per-module) parameter if mp NULL, and
593  * create file in sysfs.  Returns an error on out of memory.  Always cleans up
594  * if there's an error.
595  */
596 static __modinit int add_sysfs_param(struct module_kobject *mk,
597                                      const struct kernel_param *kp,
598                                      const char *name)
599 {
600         struct module_param_attrs *new;
601         struct attribute **attrs;
602         int err, num;
603
604         /* We don't bother calling this with invisible parameters. */
605         BUG_ON(!kp->perm);
606
607         if (!mk->mp) {
608                 num = 0;
609                 attrs = NULL;
610         } else {
611                 num = mk->mp->num;
612                 attrs = mk->mp->grp.attrs;
613         }
614
615         /* Enlarge. */
616         new = krealloc(mk->mp,
617                        sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
618                        GFP_KERNEL);
619         if (!new) {
620                 kfree(mk->mp);
621                 err = -ENOMEM;
622                 goto fail;
623         }
624         attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
625         if (!attrs) {
626                 err = -ENOMEM;
627                 goto fail_free_new;
628         }
629
630         /* Sysfs wants everything zeroed. */
631         memset(new, 0, sizeof(*new));
632         memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
633         memset(&attrs[num], 0, sizeof(attrs[num]));
634         new->grp.name = "parameters";
635         new->grp.attrs = attrs;
636
637         /* Tack new one on the end. */
638         sysfs_attr_init(&new->attrs[num].mattr.attr);
639         new->attrs[num].param = kp;
640         new->attrs[num].mattr.show = param_attr_show;
641         new->attrs[num].mattr.store = param_attr_store;
642         new->attrs[num].mattr.attr.name = (char *)name;
643         new->attrs[num].mattr.attr.mode = kp->perm;
644         new->num = num+1;
645
646         /* Fix up all the pointers, since krealloc can move us */
647         for (num = 0; num < new->num; num++)
648                 new->grp.attrs[num] = &new->attrs[num].mattr.attr;
649         new->grp.attrs[num] = NULL;
650
651         mk->mp = new;
652         return 0;
653
654 fail_free_new:
655         kfree(new);
656 fail:
657         mk->mp = NULL;
658         return err;
659 }
660
661 #ifdef CONFIG_MODULES
662 static void free_module_param_attrs(struct module_kobject *mk)
663 {
664         kfree(mk->mp->grp.attrs);
665         kfree(mk->mp);
666         mk->mp = NULL;
667 }
668
669 /*
670  * module_param_sysfs_setup - setup sysfs support for one module
671  * @mod: module
672  * @kparam: module parameters (array)
673  * @num_params: number of module parameters
674  *
675  * Adds sysfs entries for module parameters under
676  * /sys/module/[mod->name]/parameters/
677  */
678 int module_param_sysfs_setup(struct module *mod,
679                              const struct kernel_param *kparam,
680                              unsigned int num_params)
681 {
682         int i, err;
683         bool params = false;
684
685         for (i = 0; i < num_params; i++) {
686                 if (kparam[i].perm == 0)
687                         continue;
688                 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
689                 if (err)
690                         return err;
691                 params = true;
692         }
693
694         if (!params)
695                 return 0;
696
697         /* Create the param group. */
698         err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
699         if (err)
700                 free_module_param_attrs(&mod->mkobj);
701         return err;
702 }
703
704 /*
705  * module_param_sysfs_remove - remove sysfs support for one module
706  * @mod: module
707  *
708  * Remove sysfs entries for module parameters and the corresponding
709  * kobject.
710  */
711 void module_param_sysfs_remove(struct module *mod)
712 {
713         if (mod->mkobj.mp) {
714                 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
715                 /* We are positive that no one is using any param
716                  * attrs at this point.  Deallocate immediately. */
717                 free_module_param_attrs(&mod->mkobj);
718         }
719 }
720 #endif
721
722 void destroy_params(const struct kernel_param *params, unsigned num)
723 {
724         unsigned int i;
725
726         for (i = 0; i < num; i++)
727                 if (params[i].ops->free)
728                         params[i].ops->free(params[i].arg);
729 }
730
731 static struct module_kobject * __init locate_module_kobject(const char *name)
732 {
733         struct module_kobject *mk;
734         struct kobject *kobj;
735         int err;
736
737         kobj = kset_find_obj(module_kset, name);
738         if (kobj) {
739                 mk = to_module_kobject(kobj);
740         } else {
741                 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
742                 BUG_ON(!mk);
743
744                 mk->mod = THIS_MODULE;
745                 mk->kobj.kset = module_kset;
746                 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
747                                            "%s", name);
748 #ifdef CONFIG_MODULES
749                 if (!err)
750                         err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
751 #endif
752                 if (err) {
753                         kobject_put(&mk->kobj);
754                         printk(KERN_ERR
755                                 "Module '%s' failed add to sysfs, error number %d\n",
756                                 name, err);
757                         printk(KERN_ERR
758                                 "The system will be unstable now.\n");
759                         return NULL;
760                 }
761
762                 /* So that we hold reference in both cases. */
763                 kobject_get(&mk->kobj);
764         }
765
766         return mk;
767 }
768
769 static void __init kernel_add_sysfs_param(const char *name,
770                                           struct kernel_param *kparam,
771                                           unsigned int name_skip)
772 {
773         struct module_kobject *mk;
774         int err;
775
776         mk = locate_module_kobject(name);
777         if (!mk)
778                 return;
779
780         /* We need to remove old parameters before adding more. */
781         if (mk->mp)
782                 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
783
784         /* These should not fail at boot. */
785         err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
786         BUG_ON(err);
787         err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
788         BUG_ON(err);
789         kobject_uevent(&mk->kobj, KOBJ_ADD);
790         kobject_put(&mk->kobj);
791 }
792
793 /*
794  * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
795  *
796  * Add module_parameters to sysfs for "modules" built into the kernel.
797  *
798  * The "module" name (KBUILD_MODNAME) is stored before a dot, the
799  * "parameter" name is stored behind a dot in kernel_param->name. So,
800  * extract the "module" name for all built-in kernel_param-eters,
801  * and for all who have the same, call kernel_add_sysfs_param.
802  */
803 static void __init param_sysfs_builtin(void)
804 {
805         struct kernel_param *kp;
806         unsigned int name_len;
807         char modname[MODULE_NAME_LEN];
808
809         for (kp = __start___param; kp < __stop___param; kp++) {
810                 char *dot;
811
812                 if (kp->perm == 0)
813                         continue;
814
815                 dot = strchr(kp->name, '.');
816                 if (!dot) {
817                         /* This happens for core_param() */
818                         strcpy(modname, "kernel");
819                         name_len = 0;
820                 } else {
821                         name_len = dot - kp->name + 1;
822                         strlcpy(modname, kp->name, name_len);
823                 }
824                 kernel_add_sysfs_param(modname, kp, name_len);
825         }
826 }
827
828 ssize_t __modver_version_show(struct module_attribute *mattr,
829                               struct module_kobject *mk, char *buf)
830 {
831         struct module_version_attribute *vattr =
832                 container_of(mattr, struct module_version_attribute, mattr);
833
834         return sprintf(buf, "%s\n", vattr->version);
835 }
836
837 extern const struct module_version_attribute *__start___modver[];
838 extern const struct module_version_attribute *__stop___modver[];
839
840 static void __init version_sysfs_builtin(void)
841 {
842         const struct module_version_attribute **p;
843         struct module_kobject *mk;
844         int err;
845
846         for (p = __start___modver; p < __stop___modver; p++) {
847                 const struct module_version_attribute *vattr = *p;
848
849                 mk = locate_module_kobject(vattr->module_name);
850                 if (mk) {
851                         err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
852                         kobject_uevent(&mk->kobj, KOBJ_ADD);
853                         kobject_put(&mk->kobj);
854                 }
855         }
856 }
857
858 /* module-related sysfs stuff */
859
860 static ssize_t module_attr_show(struct kobject *kobj,
861                                 struct attribute *attr,
862                                 char *buf)
863 {
864         struct module_attribute *attribute;
865         struct module_kobject *mk;
866         int ret;
867
868         attribute = to_module_attr(attr);
869         mk = to_module_kobject(kobj);
870
871         if (!attribute->show)
872                 return -EIO;
873
874         ret = attribute->show(attribute, mk, buf);
875
876         return ret;
877 }
878
879 static ssize_t module_attr_store(struct kobject *kobj,
880                                 struct attribute *attr,
881                                 const char *buf, size_t len)
882 {
883         struct module_attribute *attribute;
884         struct module_kobject *mk;
885         int ret;
886
887         attribute = to_module_attr(attr);
888         mk = to_module_kobject(kobj);
889
890         if (!attribute->store)
891                 return -EIO;
892
893         ret = attribute->store(attribute, mk, buf, len);
894
895         return ret;
896 }
897
898 static const struct sysfs_ops module_sysfs_ops = {
899         .show = module_attr_show,
900         .store = module_attr_store,
901 };
902
903 static int uevent_filter(struct kset *kset, struct kobject *kobj)
904 {
905         struct kobj_type *ktype = get_ktype(kobj);
906
907         if (ktype == &module_ktype)
908                 return 1;
909         return 0;
910 }
911
912 static const struct kset_uevent_ops module_uevent_ops = {
913         .filter = uevent_filter,
914 };
915
916 struct kset *module_kset;
917 int module_sysfs_initialized;
918
919 struct kobj_type module_ktype = {
920         .sysfs_ops =    &module_sysfs_ops,
921 };
922
923 /*
924  * param_sysfs_init - wrapper for built-in params support
925  */
926 static int __init param_sysfs_init(void)
927 {
928         module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
929         if (!module_kset) {
930                 printk(KERN_WARNING "%s (%d): error creating kset\n",
931                         __FILE__, __LINE__);
932                 return -ENOMEM;
933         }
934         module_sysfs_initialized = 1;
935
936         version_sysfs_builtin();
937         param_sysfs_builtin();
938
939         return 0;
940 }
941 subsys_initcall(param_sysfs_init);
942
943 #endif /* CONFIG_SYSFS */