blob: 13410b20600f4a33284da6252cc7beeb99c9f105 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_MODULE_PARAMS_H
2#define _LINUX_MODULE_PARAMS_H
3/* (C) Copyright 2001, 2002 Rusty Russell IBM Corporation */
4#include <linux/init.h>
5#include <linux/stringify.h>
6#include <linux/kernel.h>
7
8/* You can override this manually, but generally this should match the
9 module name. */
10#ifdef MODULE
11#define MODULE_PARAM_PREFIX /* empty */
12#else
Sam Ravnborg367cb702006-01-06 21:17:50 +010013#define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#endif
15
16#ifdef MODULE
17#define ___module_cat(a,b) __mod_ ## a ## b
18#define __module_cat(a,b) ___module_cat(a,b)
19#define __MODULE_INFO(tag, name, info) \
20static const char __module_cat(name,__LINE__)[] \
21 __attribute_used__ \
22 __attribute__((section(".modinfo"),unused)) = __stringify(tag) "=" info
23#else /* !MODULE */
24#define __MODULE_INFO(tag, name, info)
25#endif
26#define __MODULE_PARM_TYPE(name, _type) \
27 __MODULE_INFO(parmtype, name##type, #name ":" _type)
28
29struct kernel_param;
30
31/* Returns 0, or -errno. arg is in kp->arg. */
32typedef int (*param_set_fn)(const char *val, struct kernel_param *kp);
33/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
34typedef int (*param_get_fn)(char *buffer, struct kernel_param *kp);
35
36struct kernel_param {
37 const char *name;
38 unsigned int perm;
39 param_set_fn set;
40 param_get_fn get;
Jan Beulich22e48ea2007-10-16 23:29:34 -070041 union {
42 void *arg;
43 const struct kparam_string *str;
44 const struct kparam_array *arr;
45 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
48/* Special one for strings we want to copy into */
49struct kparam_string {
50 unsigned int maxlen;
51 char *string;
52};
53
54/* Special one for arrays */
55struct kparam_array
56{
57 unsigned int max;
58 unsigned int *num;
59 param_set_fn set;
60 param_get_fn get;
61 unsigned int elemsize;
62 void *elem;
63};
64
65/* This is the fundamental function for registering boot/module
Robert P. J. Day405ae7d2007-02-17 19:13:42 +010066 parameters. perm sets the visibility in sysfs: 000 means it's
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 not there, read bits mean it's readable, write bits mean it's
68 writable. */
69#define __module_param_call(prefix, name, set, get, arg, perm) \
Alexey Dobriyan9774a1f2006-12-06 20:36:56 -080070 /* Default value instead of permissions? */ \
71 static int __param_perm_check_##name __attribute__((unused)) = \
72 BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)); \
Jan Beulich22e48ea2007-10-16 23:29:34 -070073 static const char __param_str_##name[] = prefix #name; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 static struct kernel_param const __param_##name \
75 __attribute_used__ \
76 __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
Jan Beulich22e48ea2007-10-16 23:29:34 -070077 = { __param_str_##name, perm, set, get, { arg } }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#define module_param_call(name, set, get, arg, perm) \
80 __module_param_call(MODULE_PARAM_PREFIX, name, set, get, arg, perm)
81
82/* Helper functions: type is byte, short, ushort, int, uint, long,
83 ulong, charp, bool or invbool, or XXX if you define param_get_XXX,
84 param_set_XXX and param_check_XXX. */
85#define module_param_named(name, value, type, perm) \
86 param_check_##type(name, &(value)); \
87 module_param_call(name, param_set_##type, param_get_##type, &value, perm); \
88 __MODULE_PARM_TYPE(name, #type)
89
90#define module_param(name, type, perm) \
91 module_param_named(name, name, type, perm)
92
93/* Actually copy string: maxlen param is usually sizeof(string). */
94#define module_param_string(name, string, len, perm) \
Jan Beulich22e48ea2007-10-16 23:29:34 -070095 static const struct kparam_string __param_string_##name \
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 = { len, string }; \
97 module_param_call(name, param_set_copystring, param_get_string, \
Jan Beulich22e48ea2007-10-16 23:29:34 -070098 .str = &__param_string_##name, perm); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 __MODULE_PARM_TYPE(name, "string")
100
101/* Called on module insert or kernel boot */
102extern int parse_args(const char *name,
103 char *args,
104 struct kernel_param *params,
105 unsigned num,
106 int (*unknown)(char *param, char *val));
107
108/* All the helper functions */
109/* The macros to do compile-time type checking stolen from Jakub
110 Jelinek, who IIRC came up with this idea for the 2.4 module init code. */
111#define __param_check(name, p, type) \
112 static inline type *__check_##name(void) { return(p); }
113
114extern int param_set_byte(const char *val, struct kernel_param *kp);
115extern int param_get_byte(char *buffer, struct kernel_param *kp);
116#define param_check_byte(name, p) __param_check(name, p, unsigned char)
117
118extern int param_set_short(const char *val, struct kernel_param *kp);
119extern int param_get_short(char *buffer, struct kernel_param *kp);
120#define param_check_short(name, p) __param_check(name, p, short)
121
122extern int param_set_ushort(const char *val, struct kernel_param *kp);
123extern int param_get_ushort(char *buffer, struct kernel_param *kp);
124#define param_check_ushort(name, p) __param_check(name, p, unsigned short)
125
126extern int param_set_int(const char *val, struct kernel_param *kp);
127extern int param_get_int(char *buffer, struct kernel_param *kp);
128#define param_check_int(name, p) __param_check(name, p, int)
129
130extern int param_set_uint(const char *val, struct kernel_param *kp);
131extern int param_get_uint(char *buffer, struct kernel_param *kp);
132#define param_check_uint(name, p) __param_check(name, p, unsigned int)
133
134extern int param_set_long(const char *val, struct kernel_param *kp);
135extern int param_get_long(char *buffer, struct kernel_param *kp);
136#define param_check_long(name, p) __param_check(name, p, long)
137
138extern int param_set_ulong(const char *val, struct kernel_param *kp);
139extern int param_get_ulong(char *buffer, struct kernel_param *kp);
140#define param_check_ulong(name, p) __param_check(name, p, unsigned long)
141
142extern int param_set_charp(const char *val, struct kernel_param *kp);
143extern int param_get_charp(char *buffer, struct kernel_param *kp);
144#define param_check_charp(name, p) __param_check(name, p, char *)
145
146extern int param_set_bool(const char *val, struct kernel_param *kp);
147extern int param_get_bool(char *buffer, struct kernel_param *kp);
148#define param_check_bool(name, p) __param_check(name, p, int)
149
150extern int param_set_invbool(const char *val, struct kernel_param *kp);
151extern int param_get_invbool(char *buffer, struct kernel_param *kp);
152#define param_check_invbool(name, p) __param_check(name, p, int)
153
154/* Comma-separated array: *nump is set to number they actually specified. */
155#define module_param_array_named(name, array, type, nump, perm) \
Jan Beulich22e48ea2007-10-16 23:29:34 -0700156 static const struct kparam_array __param_arr_##name \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 = { ARRAY_SIZE(array), nump, param_set_##type, param_get_##type,\
158 sizeof(array[0]), array }; \
159 module_param_call(name, param_array_set, param_array_get, \
Jan Beulich22e48ea2007-10-16 23:29:34 -0700160 .arr = &__param_arr_##name, perm); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 __MODULE_PARM_TYPE(name, "array of " #type)
162
163#define module_param_array(name, type, nump, perm) \
164 module_param_array_named(name, name, type, nump, perm)
165
166extern int param_array_set(const char *val, struct kernel_param *kp);
167extern int param_array_get(char *buffer, struct kernel_param *kp);
168
169extern int param_set_copystring(const char *val, struct kernel_param *kp);
170extern int param_get_string(char *buffer, struct kernel_param *kp);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/* for exporting parameters in /sys/parameters */
173
174struct module;
175
Randy Dunlapef665c12007-02-13 15:19:06 -0800176#if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177extern int module_param_sysfs_setup(struct module *mod,
178 struct kernel_param *kparam,
179 unsigned int num_params);
180
181extern void module_param_sysfs_remove(struct module *mod);
Randy Dunlapef665c12007-02-13 15:19:06 -0800182#else
183static inline int module_param_sysfs_setup(struct module *mod,
184 struct kernel_param *kparam,
185 unsigned int num_params)
186{
187 return 0;
188}
189
190static inline void module_param_sysfs_remove(struct module *mod)
191{ }
192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194#endif /* _LINUX_MODULE_PARAMS_H */