blob: b60e2c74b96121982240127245f2c9127cf4c2dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/string.h>
20#include <linux/errno.h>
21#include <linux/module.h>
22#include <linux/device.h>
23#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080024#include <linux/slab.h>
Peter Oberparleiter26d052b2009-07-06 17:11:22 +020025#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Rusty Russell907b29e2010-08-11 23:04:19 -060027/* Protects all parameters, and incidentally kmalloced_param list. */
28static DEFINE_MUTEX(param_lock);
29
Rusty Russella1054322010-08-11 23:04:18 -060030/* This just allows us to keep track of which parameters are kmalloced. */
31struct kmalloced_param {
32 struct list_head list;
33 char val[];
34};
Rusty Russella1054322010-08-11 23:04:18 -060035static LIST_HEAD(kmalloced_params);
36
37static void *kmalloc_parameter(unsigned int size)
38{
39 struct kmalloced_param *p;
40
41 p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
42 if (!p)
43 return NULL;
44
Rusty Russella1054322010-08-11 23:04:18 -060045 list_add(&p->list, &kmalloced_params);
Rusty Russella1054322010-08-11 23:04:18 -060046 return p->val;
47}
48
49/* Does nothing if parameter wasn't kmalloced above. */
50static void maybe_kfree_parameter(void *param)
51{
52 struct kmalloced_param *p;
53
Rusty Russella1054322010-08-11 23:04:18 -060054 list_for_each_entry(p, &kmalloced_params, list) {
55 if (p->val == param) {
56 list_del(&p->list);
57 kfree(p);
58 break;
59 }
60 }
Rusty Russella1054322010-08-11 23:04:18 -060061}
62
Michal Schmidtb1e4d202011-10-10 00:03:37 +020063static char dash2underscore(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 if (c == '-')
66 return '_';
67 return c;
68}
69
Michal Schmidtb1e4d202011-10-10 00:03:37 +020070bool parameqn(const char *a, const char *b, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Michal Schmidtb1e4d202011-10-10 00:03:37 +020072 size_t i;
73
74 for (i = 0; i < n; i++) {
75 if (dash2underscore(a[i]) != dash2underscore(b[i]))
76 return false;
77 }
78 return true;
79}
80
81bool parameq(const char *a, const char *b)
82{
83 return parameqn(a, b, strlen(a)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static int parse_one(char *param,
87 char *val,
Jim Cromie9fb48c72012-04-27 14:30:34 -060088 const char *doing,
Rusty Russell914dcaa2010-08-11 23:04:18 -060089 const struct kernel_param *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned num_params,
Pawel Moll026cee02012-03-26 12:50:51 +103091 s16 min_level,
92 s16 max_level,
Jim Cromie9fb48c72012-04-27 14:30:34 -060093 int (*handle_unknown)(char *param, char *val,
94 const char *doing))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 unsigned int i;
Rusty Russell907b29e2010-08-11 23:04:19 -060097 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* Find parameter */
100 for (i = 0; i < num_params; i++) {
101 if (parameq(param, params[i].name)) {
Pawel Moll026cee02012-03-26 12:50:51 +1030102 if (params[i].level < min_level
103 || params[i].level > max_level)
104 return 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300105 /* No one handled NULL, so do it here. */
Dave Young10f296c2012-02-01 10:33:11 +0800106 if (!val && params[i].ops->set != param_set_bool
107 && params[i].ops->set != param_set_bint)
Rusty Russell2e9fb992010-08-11 23:04:10 -0600108 return -EINVAL;
Jim Cromie9fb48c72012-04-27 14:30:34 -0600109 pr_debug("handling %s with %p\n", param,
110 params[i].ops->set);
Rusty Russell907b29e2010-08-11 23:04:19 -0600111 mutex_lock(&param_lock);
112 err = params[i].ops->set(val, &params[i]);
113 mutex_unlock(&param_lock);
114 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116 }
117
118 if (handle_unknown) {
Jim Cromie9fb48c72012-04-27 14:30:34 -0600119 pr_debug("doing %s: %s='%s'\n", doing, param, val);
120 return handle_unknown(param, val, doing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
Jim Cromie9fb48c72012-04-27 14:30:34 -0600123 pr_debug("Unknown argument '%s'\n", param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -ENOENT;
125}
126
127/* You can use " around spaces, but can't escape ". */
128/* Hyphens and underscores equivalent in parameter names. */
129static char *next_arg(char *args, char **param, char **val)
130{
131 unsigned int i, equals = 0;
132 int in_quote = 0, quoted = 0;
133 char *next;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (*args == '"') {
136 args++;
137 in_quote = 1;
138 quoted = 1;
139 }
140
141 for (i = 0; args[i]; i++) {
Peter Oberparleiter26d052b2009-07-06 17:11:22 +0200142 if (isspace(args[i]) && !in_quote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144 if (equals == 0) {
145 if (args[i] == '=')
146 equals = i;
147 }
148 if (args[i] == '"')
149 in_quote = !in_quote;
150 }
151
152 *param = args;
153 if (!equals)
154 *val = NULL;
155 else {
156 args[equals] = '\0';
157 *val = args + equals + 1;
158
159 /* Don't include quotes in value. */
160 if (**val == '"') {
161 (*val)++;
162 if (args[i-1] == '"')
163 args[i-1] = '\0';
164 }
165 if (quoted && args[i-1] == '"')
166 args[i-1] = '\0';
167 }
168
169 if (args[i]) {
170 args[i] = '\0';
171 next = args + i + 1;
172 } else
173 next = args + i;
Rusty Russellf36462f2005-09-27 21:45:34 -0700174
175 /* Chew up trailing spaces. */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800176 return skip_spaces(next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179/* Args looks like "foo=bar,bar2 baz=fuz wiz". */
Jim Cromie9fb48c72012-04-27 14:30:34 -0600180int parse_args(const char *doing,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 char *args,
Rusty Russell914dcaa2010-08-11 23:04:18 -0600182 const struct kernel_param *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned num,
Pawel Moll026cee02012-03-26 12:50:51 +1030184 s16 min_level,
185 s16 max_level,
Jim Cromie9fb48c72012-04-27 14:30:34 -0600186 int (*unknown)(char *param, char *val, const char *doing))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 char *param, *val;
189
Rusty Russellf36462f2005-09-27 21:45:34 -0700190 /* Chew leading spaces */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800191 args = skip_spaces(args);
Rusty Russellf36462f2005-09-27 21:45:34 -0700192
Jim Cromie9fb48c72012-04-27 14:30:34 -0600193 if (args && *args)
194 pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 while (*args) {
197 int ret;
Ard van Breemena416aba2007-01-05 16:36:20 -0800198 int irq_was_disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 args = next_arg(args, &param, &val);
Ard van Breemena416aba2007-01-05 16:36:20 -0800201 irq_was_disabled = irqs_disabled();
Jim Cromie9fb48c72012-04-27 14:30:34 -0600202 ret = parse_one(param, val, doing, params, num,
Pawel Moll026cee02012-03-26 12:50:51 +1030203 min_level, max_level, unknown);
Ard van Breemena416aba2007-01-05 16:36:20 -0800204 if (irq_was_disabled && !irqs_disabled()) {
205 printk(KERN_WARNING "parse_args(): option '%s' enabled "
206 "irq's!\n", param);
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 switch (ret) {
209 case -ENOENT:
210 printk(KERN_ERR "%s: Unknown parameter `%s'\n",
Jim Cromie9fb48c72012-04-27 14:30:34 -0600211 doing, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return ret;
213 case -ENOSPC:
214 printk(KERN_ERR
215 "%s: `%s' too large for parameter `%s'\n",
Jim Cromie9fb48c72012-04-27 14:30:34 -0600216 doing, val ?: "", param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return ret;
218 case 0:
219 break;
220 default:
221 printk(KERN_ERR
222 "%s: `%s' invalid for parameter `%s'\n",
Jim Cromie9fb48c72012-04-27 14:30:34 -0600223 doing, val ?: "", param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return ret;
225 }
226 }
227
228 /* All parsed OK. */
229 return 0;
230}
231
232/* Lazy bastard, eh? */
233#define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600234 int param_set_##name(const char *val, const struct kernel_param *kp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 tmptype l; \
Yi Yang06b2a762008-02-08 04:21:57 -0800237 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 \
Yi Yang06b2a762008-02-08 04:21:57 -0800239 ret = strtolfn(val, 0, &l); \
Satoru Moriya81c74132011-05-26 19:38:04 -0400240 if (ret < 0 || ((type)l != l)) \
241 return ret < 0 ? ret : -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *((type *)kp->arg) = l; \
243 return 0; \
244 } \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600245 int param_get_##name(char *buffer, const struct kernel_param *kp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 { \
247 return sprintf(buffer, format, *((type *)kp->arg)); \
Rusty Russella14fe242010-08-11 23:04:11 -0600248 } \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600249 struct kernel_param_ops param_ops_##name = { \
250 .set = param_set_##name, \
251 .get = param_get_##name, \
252 }; \
Rusty Russella14fe242010-08-11 23:04:11 -0600253 EXPORT_SYMBOL(param_set_##name); \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600254 EXPORT_SYMBOL(param_get_##name); \
255 EXPORT_SYMBOL(param_ops_##name)
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Yi Yang06b2a762008-02-08 04:21:57 -0800258STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
259STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
260STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
261STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
262STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul);
263STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
264STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600266int param_set_charp(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (strlen(val) > 1024) {
269 printk(KERN_ERR "%s: string parameter too long\n",
270 kp->name);
271 return -ENOSPC;
272 }
273
Rusty Russella1054322010-08-11 23:04:18 -0600274 maybe_kfree_parameter(*(char **)kp->arg);
275
276 /* This is a hack. We can't kmalloc in early boot, and we
Rusty Russelle180a6b2009-03-31 13:05:29 -0600277 * don't need to; this mangled commandline is preserved. */
278 if (slab_is_available()) {
Rusty Russella1054322010-08-11 23:04:18 -0600279 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
Rusty Russelld553ad82009-10-29 08:56:17 -0600280 if (!*(char **)kp->arg)
Rusty Russelle180a6b2009-03-31 13:05:29 -0600281 return -ENOMEM;
Rusty Russella1054322010-08-11 23:04:18 -0600282 strcpy(*(char **)kp->arg, val);
Rusty Russelle180a6b2009-03-31 13:05:29 -0600283 } else
284 *(const char **)kp->arg = val;
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287}
Rusty Russella14fe242010-08-11 23:04:11 -0600288EXPORT_SYMBOL(param_set_charp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600290int param_get_charp(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 return sprintf(buffer, "%s", *((char **)kp->arg));
293}
Rusty Russella14fe242010-08-11 23:04:11 -0600294EXPORT_SYMBOL(param_get_charp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Rusty Russella1054322010-08-11 23:04:18 -0600296static void param_free_charp(void *arg)
297{
298 maybe_kfree_parameter(*((char **)arg));
299}
300
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600301struct kernel_param_ops param_ops_charp = {
302 .set = param_set_charp,
303 .get = param_get_charp,
Rusty Russella1054322010-08-11 23:04:18 -0600304 .free = param_free_charp,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600305};
306EXPORT_SYMBOL(param_ops_charp);
307
Rusty Russellfddd5202009-06-12 21:46:57 -0600308/* Actually could be a bool or an int, for historical reasons. */
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600309int param_set_bool(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 /* No equals means "set"... */
312 if (!val) val = "1";
313
314 /* One of =[yYnN01] */
Rusty Russell8b825282012-03-26 12:50:51 +1030315 return strtobool(val, kp->arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Rusty Russella14fe242010-08-11 23:04:11 -0600317EXPORT_SYMBOL(param_set_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600319int param_get_bool(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 /* Y and N chosen as being relatively non-coder friendly */
Rusty Russell8b825282012-03-26 12:50:51 +1030322 return sprintf(buffer, "%c", *(bool *)kp->arg ? 'Y' : 'N');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
Rusty Russella14fe242010-08-11 23:04:11 -0600324EXPORT_SYMBOL(param_get_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600326struct kernel_param_ops param_ops_bool = {
327 .set = param_set_bool,
328 .get = param_get_bool,
329};
330EXPORT_SYMBOL(param_ops_bool);
331
Rusty Russellfddd5202009-06-12 21:46:57 -0600332/* This one must be bool. */
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600333int param_set_invbool(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Rusty Russellfddd5202009-06-12 21:46:57 -0600335 int ret;
336 bool boolval;
Jan Beulich22e48ea2007-10-16 23:29:34 -0700337 struct kernel_param dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Jan Beulich22e48ea2007-10-16 23:29:34 -0700339 dummy.arg = &boolval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 ret = param_set_bool(val, &dummy);
341 if (ret == 0)
Rusty Russell9a71af22009-06-12 21:46:53 -0600342 *(bool *)kp->arg = !boolval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return ret;
344}
Rusty Russella14fe242010-08-11 23:04:11 -0600345EXPORT_SYMBOL(param_set_invbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600347int param_get_invbool(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Rusty Russell9a71af22009-06-12 21:46:53 -0600349 return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
Rusty Russella14fe242010-08-11 23:04:11 -0600351EXPORT_SYMBOL(param_get_invbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600353struct kernel_param_ops param_ops_invbool = {
354 .set = param_set_invbool,
355 .get = param_get_invbool,
356};
357EXPORT_SYMBOL(param_ops_invbool);
358
Rusty Russell69116f22012-01-13 09:32:17 +1030359int param_set_bint(const char *val, const struct kernel_param *kp)
360{
361 struct kernel_param boolkp;
362 bool v;
363 int ret;
364
365 /* Match bool exactly, by re-using it. */
366 boolkp = *kp;
367 boolkp.arg = &v;
Rusty Russell69116f22012-01-13 09:32:17 +1030368
369 ret = param_set_bool(val, &boolkp);
370 if (ret == 0)
371 *(int *)kp->arg = v;
372 return ret;
373}
374EXPORT_SYMBOL(param_set_bint);
375
376struct kernel_param_ops param_ops_bint = {
377 .set = param_set_bint,
378 .get = param_get_int,
379};
380EXPORT_SYMBOL(param_ops_bint);
381
Bert Wesarg9730b5b2007-05-08 00:28:50 -0700382/* We break the rule and mangle the string. */
Adrian Bunk98717282006-03-25 03:07:06 -0800383static int param_array(const char *name,
384 const char *val,
385 unsigned int min, unsigned int max,
386 void *elem, int elemsize,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600387 int (*set)(const char *, const struct kernel_param *kp),
Pawel Moll026cee02012-03-26 12:50:51 +1030388 s16 level,
Richard Knutssoneb38a992008-02-06 01:37:50 -0800389 unsigned int *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 int ret;
392 struct kernel_param kp;
393 char save;
394
395 /* Get the name right for errors. */
396 kp.name = name;
397 kp.arg = elem;
Pawel Moll026cee02012-03-26 12:50:51 +1030398 kp.level = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 *num = 0;
401 /* We expect a comma-separated list of values. */
402 do {
403 int len;
404
405 if (*num == max) {
406 printk(KERN_ERR "%s: can only take %i arguments\n",
407 name, max);
408 return -EINVAL;
409 }
410 len = strcspn(val, ",");
411
412 /* nul-terminate and parse */
413 save = val[len];
414 ((char *)val)[len] = '\0';
Rusty Russell907b29e2010-08-11 23:04:19 -0600415 BUG_ON(!mutex_is_locked(&param_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ret = set(val, &kp);
417
418 if (ret != 0)
419 return ret;
420 kp.arg += elemsize;
421 val += len+1;
422 (*num)++;
423 } while (save == ',');
424
425 if (*num < min) {
426 printk(KERN_ERR "%s: needs at least %i arguments\n",
427 name, min);
428 return -EINVAL;
429 }
430 return 0;
431}
432
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600433static int param_array_set(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Jan Beulich22e48ea2007-10-16 23:29:34 -0700435 const struct kparam_array *arr = kp->arr;
Bert Wesarg31143a12005-04-16 15:25:42 -0700436 unsigned int temp_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 return param_array(kp->name, val, 1, arr->max, arr->elem,
Pawel Moll026cee02012-03-26 12:50:51 +1030439 arr->elemsize, arr->ops->set, kp->level,
Rusty Russell3c7d76e2009-10-29 08:56:19 -0600440 arr->num ?: &temp_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600443static int param_array_get(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 int i, off, ret;
Jan Beulich22e48ea2007-10-16 23:29:34 -0700446 const struct kparam_array *arr = kp->arr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct kernel_param p;
448
449 p = *kp;
450 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
451 if (i)
452 buffer[off++] = ',';
453 p.arg = arr->elem + arr->elemsize * i;
Rusty Russell907b29e2010-08-11 23:04:19 -0600454 BUG_ON(!mutex_is_locked(&param_lock));
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600455 ret = arr->ops->get(buffer + off, &p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (ret < 0)
457 return ret;
458 off += ret;
459 }
460 buffer[off] = '\0';
461 return off;
462}
463
Rusty Russelle6df34a2010-08-11 23:04:17 -0600464static void param_array_free(void *arg)
465{
466 unsigned int i;
467 const struct kparam_array *arr = arg;
468
469 if (arr->ops->free)
470 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
471 arr->ops->free(arr->elem + arr->elemsize * i);
472}
473
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600474struct kernel_param_ops param_array_ops = {
475 .set = param_array_set,
476 .get = param_array_get,
Rusty Russelle6df34a2010-08-11 23:04:17 -0600477 .free = param_array_free,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600478};
479EXPORT_SYMBOL(param_array_ops);
480
481int param_set_copystring(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Jan Beulich22e48ea2007-10-16 23:29:34 -0700483 const struct kparam_string *kps = kp->str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (strlen(val)+1 > kps->maxlen) {
486 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
487 kp->name, kps->maxlen-1);
488 return -ENOSPC;
489 }
490 strcpy(kps->string, val);
491 return 0;
492}
Rusty Russella14fe242010-08-11 23:04:11 -0600493EXPORT_SYMBOL(param_set_copystring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600495int param_get_string(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Jan Beulich22e48ea2007-10-16 23:29:34 -0700497 const struct kparam_string *kps = kp->str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return strlcpy(buffer, kps->string, kps->maxlen);
499}
Rusty Russella14fe242010-08-11 23:04:11 -0600500EXPORT_SYMBOL(param_get_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600502struct kernel_param_ops param_ops_string = {
503 .set = param_set_copystring,
504 .get = param_get_string,
505};
506EXPORT_SYMBOL(param_ops_string);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/* sysfs output in /sys/modules/XYZ/parameters/ */
Edward Z. Yang350f8252010-02-01 18:26:59 -0500509#define to_module_attr(n) container_of(n, struct module_attribute, attr)
510#define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512extern struct kernel_param __start___param[], __stop___param[];
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514struct param_attribute
515{
516 struct module_attribute mattr;
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600517 const struct kernel_param *param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518};
519
520struct module_param_attrs
521{
Rusty Russell9b473de2008-10-22 10:00:22 -0500522 unsigned int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct attribute_group grp;
524 struct param_attribute attrs[0];
525};
526
Randy Dunlapef665c12007-02-13 15:19:06 -0800527#ifdef CONFIG_SYSFS
Edward Z. Yang350f8252010-02-01 18:26:59 -0500528#define to_param_attr(n) container_of(n, struct param_attribute, mattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530static ssize_t param_attr_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930531 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 int count;
534 struct param_attribute *attribute = to_param_attr(mattr);
535
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600536 if (!attribute->param->ops->get)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -EPERM;
538
Rusty Russell907b29e2010-08-11 23:04:19 -0600539 mutex_lock(&param_lock);
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600540 count = attribute->param->ops->get(buf, attribute->param);
Rusty Russell907b29e2010-08-11 23:04:19 -0600541 mutex_unlock(&param_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (count > 0) {
543 strcat(buf, "\n");
544 ++count;
545 }
546 return count;
547}
548
549/* sysfs always hands a nul-terminated string in buf. We rely on that. */
550static ssize_t param_attr_store(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930551 struct module_kobject *km,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 const char *buf, size_t len)
553{
554 int err;
555 struct param_attribute *attribute = to_param_attr(mattr);
556
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600557 if (!attribute->param->ops->set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EPERM;
559
Rusty Russell907b29e2010-08-11 23:04:19 -0600560 mutex_lock(&param_lock);
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600561 err = attribute->param->ops->set(buf, attribute->param);
Rusty Russell907b29e2010-08-11 23:04:19 -0600562 mutex_unlock(&param_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!err)
564 return len;
565 return err;
566}
Randy Dunlapef665c12007-02-13 15:19:06 -0800567#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569#ifdef CONFIG_MODULES
570#define __modinit
571#else
572#define __modinit __init
573#endif
574
Randy Dunlapef665c12007-02-13 15:19:06 -0800575#ifdef CONFIG_SYSFS
Rusty Russell907b29e2010-08-11 23:04:19 -0600576void __kernel_param_lock(void)
577{
578 mutex_lock(&param_lock);
579}
580EXPORT_SYMBOL(__kernel_param_lock);
581
582void __kernel_param_unlock(void)
583{
584 mutex_unlock(&param_lock);
585}
586EXPORT_SYMBOL(__kernel_param_unlock);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/*
Rusty Russell9b473de2008-10-22 10:00:22 -0500589 * add_sysfs_param - add a parameter to sysfs
590 * @mk: struct module_kobject
591 * @kparam: the actual parameter definition to add to sysfs
592 * @name: name of parameter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
Rusty Russell9b473de2008-10-22 10:00:22 -0500594 * Create a kobject if for a (per-module) parameter if mp NULL, and
595 * create file in sysfs. Returns an error on out of memory. Always cleans up
596 * if there's an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
Rusty Russell9b473de2008-10-22 10:00:22 -0500598static __modinit int add_sysfs_param(struct module_kobject *mk,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600599 const struct kernel_param *kp,
Rusty Russell9b473de2008-10-22 10:00:22 -0500600 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Rusty Russell9b473de2008-10-22 10:00:22 -0500602 struct module_param_attrs *new;
603 struct attribute **attrs;
604 int err, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Rusty Russell9b473de2008-10-22 10:00:22 -0500606 /* We don't bother calling this with invisible parameters. */
607 BUG_ON(!kp->perm);
608
609 if (!mk->mp) {
610 num = 0;
611 attrs = NULL;
612 } else {
613 num = mk->mp->num;
614 attrs = mk->mp->grp.attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Rusty Russell9b473de2008-10-22 10:00:22 -0500617 /* Enlarge. */
618 new = krealloc(mk->mp,
619 sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
620 GFP_KERNEL);
621 if (!new) {
622 kfree(mk->mp);
623 err = -ENOMEM;
624 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Rusty Russell9b473de2008-10-22 10:00:22 -0500626 attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
627 if (!attrs) {
628 err = -ENOMEM;
629 goto fail_free_new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Rusty Russell9b473de2008-10-22 10:00:22 -0500631
632 /* Sysfs wants everything zeroed. */
633 memset(new, 0, sizeof(*new));
634 memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
635 memset(&attrs[num], 0, sizeof(attrs[num]));
636 new->grp.name = "parameters";
637 new->grp.attrs = attrs;
638
639 /* Tack new one on the end. */
Eric W. Biedermana07e4152010-02-11 15:23:05 -0800640 sysfs_attr_init(&new->attrs[num].mattr.attr);
Rusty Russell9b473de2008-10-22 10:00:22 -0500641 new->attrs[num].param = kp;
642 new->attrs[num].mattr.show = param_attr_show;
643 new->attrs[num].mattr.store = param_attr_store;
644 new->attrs[num].mattr.attr.name = (char *)name;
645 new->attrs[num].mattr.attr.mode = kp->perm;
646 new->num = num+1;
647
648 /* Fix up all the pointers, since krealloc can move us */
649 for (num = 0; num < new->num; num++)
650 new->grp.attrs[num] = &new->attrs[num].mattr.attr;
651 new->grp.attrs[num] = NULL;
652
653 mk->mp = new;
654 return 0;
655
656fail_free_new:
657 kfree(new);
658fail:
659 mk->mp = NULL;
660 return err;
661}
662
Linus Torvaldsd2441182008-10-23 12:07:17 -0700663#ifdef CONFIG_MODULES
Rusty Russell9b473de2008-10-22 10:00:22 -0500664static void free_module_param_attrs(struct module_kobject *mk)
665{
666 kfree(mk->mp->grp.attrs);
667 kfree(mk->mp);
668 mk->mp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/*
672 * module_param_sysfs_setup - setup sysfs support for one module
673 * @mod: module
674 * @kparam: module parameters (array)
675 * @num_params: number of module parameters
676 *
Rusty Russell9b473de2008-10-22 10:00:22 -0500677 * Adds sysfs entries for module parameters under
678 * /sys/module/[mod->name]/parameters/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
680int module_param_sysfs_setup(struct module *mod,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600681 const struct kernel_param *kparam,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 unsigned int num_params)
683{
Rusty Russell9b473de2008-10-22 10:00:22 -0500684 int i, err;
685 bool params = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Rusty Russell9b473de2008-10-22 10:00:22 -0500687 for (i = 0; i < num_params; i++) {
688 if (kparam[i].perm == 0)
689 continue;
690 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
691 if (err)
692 return err;
693 params = true;
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Rusty Russell9b473de2008-10-22 10:00:22 -0500696 if (!params)
697 return 0;
698
699 /* Create the param group. */
700 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
701 if (err)
702 free_module_param_attrs(&mod->mkobj);
703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706/*
707 * module_param_sysfs_remove - remove sysfs support for one module
708 * @mod: module
709 *
710 * Remove sysfs entries for module parameters and the corresponding
711 * kobject.
712 */
713void module_param_sysfs_remove(struct module *mod)
714{
Rusty Russell9b473de2008-10-22 10:00:22 -0500715 if (mod->mkobj.mp) {
716 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* We are positive that no one is using any param
718 * attrs at this point. Deallocate immediately. */
Rusty Russell9b473de2008-10-22 10:00:22 -0500719 free_module_param_attrs(&mod->mkobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721}
722#endif
723
Rusty Russelle180a6b2009-03-31 13:05:29 -0600724void destroy_params(const struct kernel_param *params, unsigned num)
725{
Rusty Russelle6df34a2010-08-11 23:04:17 -0600726 unsigned int i;
727
728 for (i = 0; i < num; i++)
729 if (params[i].ops->free)
730 params[i].ops->free(params[i].arg);
Rusty Russelle180a6b2009-03-31 13:05:29 -0600731}
732
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800733static struct module_kobject * __init locate_module_kobject(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct module_kobject *mk;
Rusty Russell9b473de2008-10-22 10:00:22 -0500736 struct kobject *kobj;
737 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Rusty Russell9b473de2008-10-22 10:00:22 -0500739 kobj = kset_find_obj(module_kset, name);
740 if (kobj) {
Rusty Russell9b473de2008-10-22 10:00:22 -0500741 mk = to_module_kobject(kobj);
Rusty Russell9b473de2008-10-22 10:00:22 -0500742 } else {
743 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
744 BUG_ON(!mk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Rusty Russell9b473de2008-10-22 10:00:22 -0500746 mk->mod = THIS_MODULE;
747 mk->kobj.kset = module_kset;
748 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
749 "%s", name);
Kay Sievers88bfa322011-07-24 22:06:04 +0930750#ifdef CONFIG_MODULES
751 if (!err)
752 err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
753#endif
Rusty Russell9b473de2008-10-22 10:00:22 -0500754 if (err) {
755 kobject_put(&mk->kobj);
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800756 printk(KERN_ERR
757 "Module '%s' failed add to sysfs, error number %d\n",
758 name, err);
759 printk(KERN_ERR
760 "The system will be unstable now.\n");
761 return NULL;
Rusty Russell9b473de2008-10-22 10:00:22 -0500762 }
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800763
764 /* So that we hold reference in both cases. */
Rusty Russell9b473de2008-10-22 10:00:22 -0500765 kobject_get(&mk->kobj);
Greg Kroah-Hartman74c5b592007-07-30 11:26:38 -0700766 }
Rusty Russell9b473de2008-10-22 10:00:22 -0500767
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800768 return mk;
769}
770
771static void __init kernel_add_sysfs_param(const char *name,
772 struct kernel_param *kparam,
773 unsigned int name_skip)
774{
775 struct module_kobject *mk;
776 int err;
777
778 mk = locate_module_kobject(name);
779 if (!mk)
780 return;
781
782 /* We need to remove old parameters before adding more. */
783 if (mk->mp)
784 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
785
Rusty Russell9b473de2008-10-22 10:00:22 -0500786 /* These should not fail at boot. */
787 err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
788 BUG_ON(err);
789 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
790 BUG_ON(err);
Kay Sieversf30c53a2007-01-15 20:22:02 +0100791 kobject_uevent(&mk->kobj, KOBJ_ADD);
Rusty Russell9b473de2008-10-22 10:00:22 -0500792 kobject_put(&mk->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795/*
796 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
797 *
798 * Add module_parameters to sysfs for "modules" built into the kernel.
799 *
800 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
801 * "parameter" name is stored behind a dot in kernel_param->name. So,
802 * extract the "module" name for all built-in kernel_param-eters,
Rusty Russell9b473de2008-10-22 10:00:22 -0500803 * and for all who have the same, call kernel_add_sysfs_param.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
805static void __init param_sysfs_builtin(void)
806{
Rusty Russell9b473de2008-10-22 10:00:22 -0500807 struct kernel_param *kp;
808 unsigned int name_len;
809 char modname[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Rusty Russell9b473de2008-10-22 10:00:22 -0500811 for (kp = __start___param; kp < __stop___param; kp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 char *dot;
813
Rusty Russell9b473de2008-10-22 10:00:22 -0500814 if (kp->perm == 0)
815 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Rusty Russell730b69d2008-10-22 10:00:22 -0500817 dot = strchr(kp->name, '.');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!dot) {
Rusty Russell67e67ce2008-10-22 10:00:23 -0500819 /* This happens for core_param() */
820 strcpy(modname, "kernel");
821 name_len = 0;
822 } else {
823 name_len = dot - kp->name + 1;
824 strlcpy(modname, kp->name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Rusty Russell67e67ce2008-10-22 10:00:23 -0500826 kernel_add_sysfs_param(modname, kp, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800830ssize_t __modver_version_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930831 struct module_kobject *mk, char *buf)
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800832{
833 struct module_version_attribute *vattr =
834 container_of(mattr, struct module_version_attribute, mattr);
835
836 return sprintf(buf, "%s\n", vattr->version);
837}
838
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800839extern const struct module_version_attribute *__start___modver[];
840extern const struct module_version_attribute *__stop___modver[];
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800841
842static void __init version_sysfs_builtin(void)
843{
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800844 const struct module_version_attribute **p;
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800845 struct module_kobject *mk;
846 int err;
847
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800848 for (p = __start___modver; p < __stop___modver; p++) {
849 const struct module_version_attribute *vattr = *p;
850
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800851 mk = locate_module_kobject(vattr->module_name);
852 if (mk) {
853 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
854 kobject_uevent(&mk->kobj, KOBJ_ADD);
855 kobject_put(&mk->kobj);
856 }
857 }
858}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860/* module-related sysfs stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static ssize_t module_attr_show(struct kobject *kobj,
863 struct attribute *attr,
864 char *buf)
865{
866 struct module_attribute *attribute;
867 struct module_kobject *mk;
868 int ret;
869
870 attribute = to_module_attr(attr);
871 mk = to_module_kobject(kobj);
872
873 if (!attribute->show)
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500874 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Kay Sievers4befb022011-07-24 22:06:04 +0930876 ret = attribute->show(attribute, mk, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return ret;
879}
880
881static ssize_t module_attr_store(struct kobject *kobj,
882 struct attribute *attr,
883 const char *buf, size_t len)
884{
885 struct module_attribute *attribute;
886 struct module_kobject *mk;
887 int ret;
888
889 attribute = to_module_attr(attr);
890 mk = to_module_kobject(kobj);
891
892 if (!attribute->store)
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500893 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Kay Sievers4befb022011-07-24 22:06:04 +0930895 ret = attribute->store(attribute, mk, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return ret;
898}
899
Emese Revfy52cf25d2010-01-19 02:58:23 +0100900static const struct sysfs_ops module_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 .show = module_attr_show,
902 .store = module_attr_store,
903};
904
Kay Sievers270a6c42007-01-18 13:26:15 +0100905static int uevent_filter(struct kset *kset, struct kobject *kobj)
906{
907 struct kobj_type *ktype = get_ktype(kobj);
908
909 if (ktype == &module_ktype)
910 return 1;
911 return 0;
912}
913
Emese Revfy9cd43612009-12-31 14:52:51 +0100914static const struct kset_uevent_ops module_uevent_ops = {
Kay Sievers270a6c42007-01-18 13:26:15 +0100915 .filter = uevent_filter,
916};
917
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700918struct kset *module_kset;
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700919int module_sysfs_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700921struct kobj_type module_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 .sysfs_ops = &module_sysfs_ops,
923};
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * param_sysfs_init - wrapper for built-in params support
927 */
928static int __init param_sysfs_init(void)
929{
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700930 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
931 if (!module_kset) {
932 printk(KERN_WARNING "%s (%d): error creating kset\n",
933 __FILE__, __LINE__);
934 return -ENOMEM;
Randy Dunlapd8c76492006-09-29 01:58:55 -0700935 }
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700936 module_sysfs_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800938 version_sysfs_builtin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 param_sysfs_builtin();
940
941 return 0;
942}
Mark Huangd10be6d2006-09-29 01:59:34 -0700943subsys_initcall(param_sysfs_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700945#endif /* CONFIG_SYSFS */