2 * vim: noexpandtab ts=8 sts=0 sw=8:
4 * configfs_example_macros.c - This file is a demonstration module
5 * containing a number of configfs subsystems. It uses the helper
6 * macros defined by configfs.h
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 021110-1307, USA.
24 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
26 * configfs Copyright (C) 2005 Oracle. All rights reserved.
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
33 #include <linux/configfs.h>
40 * This first example is a childless subsystem. It cannot create
41 * any config_items. It just has attributes.
43 * Note that we are enclosing the configfs_subsystem inside a container.
44 * This is not necessary if a subsystem has no attributes directly
45 * on the subsystem. See the next example, 02-simple-children, for
50 struct configfs_subsystem subsys
;
55 static inline struct childless
*to_childless(struct config_item
*item
)
57 return item
? container_of(to_configfs_subsystem(to_config_group(item
)), struct childless
, subsys
) : NULL
;
60 CONFIGFS_ATTR_STRUCT(childless
);
61 #define CHILDLESS_ATTR(_name, _mode, _show, _store) \
62 struct childless_attribute childless_attr_##_name = __CONFIGFS_ATTR(_name, _mode, _show, _store)
63 #define CHILDLESS_ATTR_RO(_name, _show) \
64 struct childless_attribute childless_attr_##_name = __CONFIGFS_ATTR_RO(_name, _show);
66 static ssize_t
childless_showme_read(struct childless
*childless
,
71 pos
= sprintf(page
, "%d\n", childless
->showme
);
77 static ssize_t
childless_storeme_read(struct childless
*childless
,
80 return sprintf(page
, "%d\n", childless
->storeme
);
83 static ssize_t
childless_storeme_write(struct childless
*childless
,
88 char *p
= (char *) page
;
90 tmp
= simple_strtoul(p
, &p
, 10);
91 if (!p
|| (*p
&& (*p
!= '\n')))
97 childless
->storeme
= tmp
;
102 static ssize_t
childless_description_read(struct childless
*childless
,
108 "The childless subsystem is the simplest possible subsystem in\n"
109 "configfs. It does not support the creation of child config_items.\n"
110 "It only has a few attributes. In fact, it isn't much different\n"
111 "than a directory in /proc.\n");
114 CHILDLESS_ATTR_RO(showme
, childless_showme_read
);
115 CHILDLESS_ATTR(storeme
, S_IRUGO
| S_IWUSR
, childless_storeme_read
,
116 childless_storeme_write
);
117 CHILDLESS_ATTR_RO(description
, childless_description_read
);
119 static struct configfs_attribute
*childless_attrs
[] = {
120 &childless_attr_showme
.attr
,
121 &childless_attr_storeme
.attr
,
122 &childless_attr_description
.attr
,
126 CONFIGFS_ATTR_OPS(childless
);
127 static struct configfs_item_operations childless_item_ops
= {
128 .show_attribute
= childless_attr_show
,
129 .store_attribute
= childless_attr_store
,
132 static struct config_item_type childless_type
= {
133 .ct_item_ops
= &childless_item_ops
,
134 .ct_attrs
= childless_attrs
,
135 .ct_owner
= THIS_MODULE
,
138 static struct childless childless_subsys
= {
142 .ci_namebuf
= "01-childless",
143 .ci_type
= &childless_type
,
150 /* ----------------------------------------------------------------- */
155 * This example merely has a simple one-attribute child. Note that
156 * there is no extra attribute structure, as the child's attribute is
157 * known from the get-go. Also, there is no container for the
158 * subsystem, as it has no attributes of its own.
161 struct simple_child
{
162 struct config_item item
;
166 static inline struct simple_child
*to_simple_child(struct config_item
*item
)
168 return item
? container_of(item
, struct simple_child
, item
) : NULL
;
171 static struct configfs_attribute simple_child_attr_storeme
= {
172 .ca_owner
= THIS_MODULE
,
173 .ca_name
= "storeme",
174 .ca_mode
= S_IRUGO
| S_IWUSR
,
177 static struct configfs_attribute
*simple_child_attrs
[] = {
178 &simple_child_attr_storeme
,
182 static ssize_t
simple_child_attr_show(struct config_item
*item
,
183 struct configfs_attribute
*attr
,
187 struct simple_child
*simple_child
= to_simple_child(item
);
189 count
= sprintf(page
, "%d\n", simple_child
->storeme
);
194 static ssize_t
simple_child_attr_store(struct config_item
*item
,
195 struct configfs_attribute
*attr
,
196 const char *page
, size_t count
)
198 struct simple_child
*simple_child
= to_simple_child(item
);
200 char *p
= (char *) page
;
202 tmp
= simple_strtoul(p
, &p
, 10);
203 if (!p
|| (*p
&& (*p
!= '\n')))
209 simple_child
->storeme
= tmp
;
214 static void simple_child_release(struct config_item
*item
)
216 kfree(to_simple_child(item
));
219 static struct configfs_item_operations simple_child_item_ops
= {
220 .release
= simple_child_release
,
221 .show_attribute
= simple_child_attr_show
,
222 .store_attribute
= simple_child_attr_store
,
225 static struct config_item_type simple_child_type
= {
226 .ct_item_ops
= &simple_child_item_ops
,
227 .ct_attrs
= simple_child_attrs
,
228 .ct_owner
= THIS_MODULE
,
232 struct simple_children
{
233 struct config_group group
;
236 static inline struct simple_children
*to_simple_children(struct config_item
*item
)
238 return item
? container_of(to_config_group(item
), struct simple_children
, group
) : NULL
;
241 static struct config_item
*simple_children_make_item(struct config_group
*group
, const char *name
)
243 struct simple_child
*simple_child
;
245 simple_child
= kzalloc(sizeof(struct simple_child
), GFP_KERNEL
);
247 return ERR_PTR(-ENOMEM
);
249 config_item_init_type_name(&simple_child
->item
, name
,
252 simple_child
->storeme
= 0;
254 return &simple_child
->item
;
257 static struct configfs_attribute simple_children_attr_description
= {
258 .ca_owner
= THIS_MODULE
,
259 .ca_name
= "description",
263 static struct configfs_attribute
*simple_children_attrs
[] = {
264 &simple_children_attr_description
,
268 static ssize_t
simple_children_attr_show(struct config_item
*item
,
269 struct configfs_attribute
*attr
,
273 "[02-simple-children]\n"
275 "This subsystem allows the creation of child config_items. These\n"
276 "items have only one attribute that is readable and writeable.\n");
279 static void simple_children_release(struct config_item
*item
)
281 kfree(to_simple_children(item
));
284 static struct configfs_item_operations simple_children_item_ops
= {
285 .release
= simple_children_release
,
286 .show_attribute
= simple_children_attr_show
,
290 * Note that, since no extra work is required on ->drop_item(),
291 * no ->drop_item() is provided.
293 static struct configfs_group_operations simple_children_group_ops
= {
294 .make_item
= simple_children_make_item
,
297 static struct config_item_type simple_children_type
= {
298 .ct_item_ops
= &simple_children_item_ops
,
299 .ct_group_ops
= &simple_children_group_ops
,
300 .ct_attrs
= simple_children_attrs
,
301 .ct_owner
= THIS_MODULE
,
304 static struct configfs_subsystem simple_children_subsys
= {
307 .ci_namebuf
= "02-simple-children",
308 .ci_type
= &simple_children_type
,
314 /* ----------------------------------------------------------------- */
319 * This example reuses the simple_children group from above. However,
320 * the simple_children group is not the subsystem itself, it is a
321 * child of the subsystem. Creation of a group in the subsystem creates
322 * a new simple_children group. That group can then have simple_child
323 * children of its own.
326 static struct config_group
*group_children_make_group(struct config_group
*group
, const char *name
)
328 struct simple_children
*simple_children
;
330 simple_children
= kzalloc(sizeof(struct simple_children
),
332 if (!simple_children
)
333 return ERR_PTR(-ENOMEM
);
335 config_group_init_type_name(&simple_children
->group
, name
,
336 &simple_children_type
);
338 return &simple_children
->group
;
341 static struct configfs_attribute group_children_attr_description
= {
342 .ca_owner
= THIS_MODULE
,
343 .ca_name
= "description",
347 static struct configfs_attribute
*group_children_attrs
[] = {
348 &group_children_attr_description
,
352 static ssize_t
group_children_attr_show(struct config_item
*item
,
353 struct configfs_attribute
*attr
,
357 "[03-group-children]\n"
359 "This subsystem allows the creation of child config_groups. These\n"
360 "groups are like the subsystem simple-children.\n");
363 static struct configfs_item_operations group_children_item_ops
= {
364 .show_attribute
= group_children_attr_show
,
368 * Note that, since no extra work is required on ->drop_item(),
369 * no ->drop_item() is provided.
371 static struct configfs_group_operations group_children_group_ops
= {
372 .make_group
= group_children_make_group
,
375 static struct config_item_type group_children_type
= {
376 .ct_item_ops
= &group_children_item_ops
,
377 .ct_group_ops
= &group_children_group_ops
,
378 .ct_attrs
= group_children_attrs
,
379 .ct_owner
= THIS_MODULE
,
382 static struct configfs_subsystem group_children_subsys
= {
385 .ci_namebuf
= "03-group-children",
386 .ci_type
= &group_children_type
,
391 /* ----------------------------------------------------------------- */
394 * We're now done with our subsystem definitions.
395 * For convenience in this module, here's a list of them all. It
396 * allows the init function to easily register them. Most modules
397 * will only have one subsystem, and will only call register_subsystem
400 static struct configfs_subsystem
*example_subsys
[] = {
401 &childless_subsys
.subsys
,
402 &simple_children_subsys
,
403 &group_children_subsys
,
407 static int __init
configfs_example_init(void)
411 struct configfs_subsystem
*subsys
;
413 for (i
= 0; example_subsys
[i
]; i
++) {
414 subsys
= example_subsys
[i
];
416 config_group_init(&subsys
->su_group
);
417 mutex_init(&subsys
->su_mutex
);
418 ret
= configfs_register_subsystem(subsys
);
420 printk(KERN_ERR
"Error %d while registering subsystem %s\n",
422 subsys
->su_group
.cg_item
.ci_namebuf
);
430 for (i
--; i
>= 0; i
--)
431 configfs_unregister_subsystem(example_subsys
[i
]);
436 static void __exit
configfs_example_exit(void)
440 for (i
= 0; example_subsys
[i
]; i
++)
441 configfs_unregister_subsystem(example_subsys
[i
]);
444 module_init(configfs_example_init
);
445 module_exit(configfs_example_exit
);
446 MODULE_LICENSE("GPL");