18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
24 #include <linux/slab.h>
25 #include <linux/ctype.h>
37 static void *kmalloc_parameter(
unsigned int size)
45 list_add(&p->
list, &kmalloced_params);
50 static void maybe_kfree_parameter(
void *
param)
55 if (p->
val == param) {
63 static char dash2underscore(
char c)
74 for (i = 0; i <
n; i++) {
75 if (dash2underscore(a[i]) != dash2underscore(b[i]))
86 static int parse_one(
char *param,
93 int (*handle_unknown)(
char *param,
char *val,
100 for (i = 0; i < num_params; i++) {
102 if (params[i].
level < min_level
103 || params[i].
level > max_level)
109 pr_debug(
"handling %s with %p\n", param,
112 err = params[
i].
ops->set(val, ¶ms[i]);
118 if (handle_unknown) {
119 pr_debug(
"doing %s: %s='%s'\n", doing, param, val);
120 return handle_unknown(param, val, doing);
123 pr_debug(
"Unknown argument '%s'\n", param);
129 static char *next_arg(
char *args,
char **param,
char **val)
131 unsigned int i, equals = 0;
132 int in_quote = 0, quoted = 0;
141 for (i = 0; args[
i]; i++) {
142 if (
isspace(args[i]) && !in_quote)
149 in_quote = !in_quote;
157 *val = args + equals + 1;
162 if (args[i-1] ==
'"')
165 if (quoted && args[i-1] ==
'"')
186 int (*
unknown)(
char *param,
char *val,
const char *doing))
194 pr_debug(
"doing %s, parsing ARGS: '%s'\n", doing, args);
198 int irq_was_disabled;
200 args = next_arg(args, ¶m, &val);
202 ret = parse_one(param, val, doing, params, num,
203 min_level, max_level,
unknown);
205 pr_warn(
"%s: option '%s' enabled irq's!\n",
210 pr_err(
"%s: Unknown parameter `%s'\n", doing, param);
213 pr_err(
"%s: `%s' too large for parameter `%s'\n",
214 doing, val ?:
"", param);
219 pr_err(
"%s: `%s' invalid for parameter `%s'\n",
220 doing, val ?:
"", param);
230 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
231 int param_set_##name(const char *val, const struct kernel_param *kp) \
236 ret = strtolfn(val, 0, &l); \
237 if (ret < 0 || ((type)l != l)) \
238 return ret < 0 ? ret : -EINVAL; \
239 *((type *)kp->arg) = l; \
242 int param_get_##name(char *buffer, const struct kernel_param *kp) \
244 return sprintf(buffer, format, *((type *)kp->arg)); \
246 struct kernel_param_ops param_ops_##name = { \
247 .set = param_set_##name, \
248 .get = param_get_##name, \
250 EXPORT_SYMBOL(param_set_##name); \
251 EXPORT_SYMBOL(param_get_##name); \
252 EXPORT_SYMBOL(param_ops_##name)
266 pr_err(
"%s: string parameter too long\n", kp->
name);
270 maybe_kfree_parameter(*(
char **)kp->
arg);
275 *(
char **)kp->
arg = kmalloc_parameter(
strlen(val)+1);
276 if (!*(
char **)kp->
arg)
280 *(
const char **)kp->
arg = val;
288 return sprintf(buffer,
"%s", *((
char **)kp->
arg));
292 static void param_free_charp(
void *
arg)
294 maybe_kfree_parameter(*((
char **)arg));
300 .free = param_free_charp,
318 return sprintf(buffer,
"%c", *(
bool *)kp->
arg ?
'Y' :
'N');
335 dummy.
arg = &boolval;
338 *(
bool *)kp->
arg = !boolval;
345 return sprintf(buffer,
"%c", (*(
bool *)kp->
arg) ?
'N' :
'Y');
379 static int param_array(
const char *name,
381 unsigned int min,
unsigned int max,
382 void *
elem,
int elemsize,
383 int (*
set)(
const char *,
const struct kernel_param *kp),
402 pr_err(
"%s: can only take %i arguments\n", name, max);
409 ((
char *)val)[len] =
'\0';
410 BUG_ON(!mutex_is_locked(¶m_lock));
418 }
while (save ==
',');
421 pr_err(
"%s: needs at least %i arguments\n", name, min);
427 static int param_array_set(
const char *val,
const struct kernel_param *kp)
430 unsigned int temp_num;
432 return param_array(kp->
name, val, 1, arr->
max, arr->
elem,
434 arr->
num ?: &temp_num);
444 for (i = off = 0; i < (arr->
num ? *arr->
num : arr->
max); i++) {
448 BUG_ON(!mutex_is_locked(¶m_lock));
449 ret = arr->
ops->get(buffer + off, &p);
458 static void param_array_free(
void *arg)
464 for (i = 0; i < (arr->
num ? *arr->
num : arr->
max); i++)
469 .set = param_array_set,
470 .get = param_array_get,
471 .free = param_array_free,
480 pr_err(
"%s: string doesn't fit in %u chars.\n",
503 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
504 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
522 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
530 if (!attribute->
param->ops->get)
534 count = attribute->
param->ops->get(buf, attribute->
param);
546 const char *buf,
size_t len)
551 if (!attribute->
param->ops->set)
555 err = attribute->
param->ops->set(buf, attribute->
param);
563 #ifdef CONFIG_MODULES
566 #define __modinit __init
570 void __kernel_param_lock(
void)
576 void __kernel_param_unlock(
void)
597 struct attribute **attrs;
608 attrs = mk->
mp->grp.attrs;
613 sizeof(*mk->
mp) +
sizeof(mk->
mp->attrs[0]) * (num+1),
627 memset(
new, 0,
sizeof(*
new));
628 memset(&new->attrs[num], 0,
sizeof(new->attrs[num]));
629 memset(&attrs[num], 0,
sizeof(attrs[num]));
630 new->grp.name =
"parameters";
631 new->grp.attrs = attrs;
635 new->attrs[num].param = kp;
636 new->attrs[num].mattr.show = param_attr_show;
637 new->attrs[num].mattr.store = param_attr_store;
638 new->attrs[num].mattr.attr.
name = (
char *)name;
639 new->attrs[num].mattr.attr.mode = kp->
perm;
643 for (num = 0; num <
new->num; num++)
644 new->grp.attrs[num] = &new->attrs[num].mattr.attr;
645 new->grp.attrs[num] =
NULL;
657 #ifdef CONFIG_MODULES
674 int module_param_sysfs_setup(
struct module *
mod,
676 unsigned int num_params)
681 for (i = 0; i < num_params; i++) {
682 if (kparam[i].
perm == 0)
684 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].
name);
696 free_module_param_attrs(&mod->mkobj);
707 void module_param_sysfs_remove(
struct module *mod)
713 free_module_param_attrs(&mod->mkobj);
718 void destroy_params(
const struct kernel_param *params,
unsigned num)
722 for (i = 0; i < num; i++)
723 if (params[i].
ops->free)
724 params[
i].
ops->free(params[i].arg);
741 mk->
kobj.kset = module_kset;
744 #ifdef CONFIG_MODULES
750 pr_crit(
"Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
762 static void __init kernel_add_sysfs_param(
const char *name,
764 unsigned int name_skip)
769 mk = locate_module_kobject(name);
778 err = add_sysfs_param(mk, kparam, kparam->
name + name_skip);
796 static void __init param_sysfs_builtin(
void)
811 strcpy(modname,
"kernel");
814 name_len = dot - kp->
name + 1;
817 kernel_add_sysfs_param(modname, kp, name_len);
833 static void __init version_sysfs_builtin(
void)
839 for (p = __start___modver; p < __stop___modver; p++) {
854 struct attribute *
attr,
864 if (!attribute->
show)
867 ret = attribute->
show(attribute, mk, buf);
873 struct attribute *attr,
874 const char *buf,
size_t len)
883 if (!attribute->
store)
886 ret = attribute->
store(attribute, mk, buf, len);
891 static const struct sysfs_ops module_sysfs_ops = {
892 .
show = module_attr_show,
893 .store = module_attr_store,
900 if (ktype == &module_ktype)
909 struct kset *module_kset;
910 int module_sysfs_initialized;
919 static int __init param_sysfs_init(
void)
927 module_sysfs_initialized = 1;
929 version_sysfs_builtin();
930 param_sysfs_builtin();