1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * vim: noexpandtab ts=8 sts=0 sw=8:
5 * configfs_example_macros.c - This file is a demonstration module
6 * containing a number of configfs subsystems. It uses the helper
7 * macros defined by configfs.h
10 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
12 * configfs Copyright (C) 2005 Oracle. All rights reserved.
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/configfs.h>
24 * This first example is a childless subsystem. It cannot create
25 * any config_items. It just has attributes.
27 * Note that we are enclosing the configfs_subsystem inside a container.
28 * This is not necessary if a subsystem has no attributes directly
29 * on the subsystem. See the next example, 02-simple-children, for
34 struct configfs_subsystem subsys;
39 static inline struct childless *to_childless(struct config_item *item)
41 return container_of(to_configfs_subsystem(to_config_group(item)),
42 struct childless, subsys);
45 static ssize_t childless_showme_show(struct config_item *item, char *page)
47 struct childless *childless = to_childless(item);
50 pos = sprintf(page, "%d\n", childless->showme);
56 static ssize_t childless_storeme_show(struct config_item *item, char *page)
58 return sprintf(page, "%d\n", to_childless(item)->storeme);
61 static ssize_t childless_storeme_store(struct config_item *item,
62 const char *page, size_t count)
64 struct childless *childless = to_childless(item);
67 ret = kstrtoint(page, 10, &childless->storeme);
74 static ssize_t childless_description_show(struct config_item *item, char *page)
79 "The childless subsystem is the simplest possible subsystem in\n"
80 "configfs. It does not support the creation of child config_items.\n"
81 "It only has a few attributes. In fact, it isn't much different\n"
82 "than a directory in /proc.\n");
85 CONFIGFS_ATTR_RO(childless_, showme);
86 CONFIGFS_ATTR(childless_, storeme);
87 CONFIGFS_ATTR_RO(childless_, description);
89 static struct configfs_attribute *childless_attrs[] = {
90 &childless_attr_showme,
91 &childless_attr_storeme,
92 &childless_attr_description,
96 static const struct config_item_type childless_type = {
97 .ct_attrs = childless_attrs,
98 .ct_owner = THIS_MODULE,
101 static struct childless childless_subsys = {
105 .ci_namebuf = "01-childless",
106 .ci_type = &childless_type,
112 /* ----------------------------------------------------------------- */
117 * This example merely has a simple one-attribute child. Note that
118 * there is no extra attribute structure, as the child's attribute is
119 * known from the get-go. Also, there is no container for the
120 * subsystem, as it has no attributes of its own.
123 struct simple_child {
124 struct config_item item;
128 static inline struct simple_child *to_simple_child(struct config_item *item)
130 return container_of(item, struct simple_child, item);
133 static ssize_t simple_child_storeme_show(struct config_item *item, char *page)
135 return sprintf(page, "%d\n", to_simple_child(item)->storeme);
138 static ssize_t simple_child_storeme_store(struct config_item *item,
139 const char *page, size_t count)
141 struct simple_child *simple_child = to_simple_child(item);
144 ret = kstrtoint(page, 10, &simple_child->storeme);
151 CONFIGFS_ATTR(simple_child_, storeme);
153 static struct configfs_attribute *simple_child_attrs[] = {
154 &simple_child_attr_storeme,
158 static void simple_child_release(struct config_item *item)
160 kfree(to_simple_child(item));
163 static struct configfs_item_operations simple_child_item_ops = {
164 .release = simple_child_release,
167 static const struct config_item_type simple_child_type = {
168 .ct_item_ops = &simple_child_item_ops,
169 .ct_attrs = simple_child_attrs,
170 .ct_owner = THIS_MODULE,
173 struct simple_children {
174 struct config_group group;
177 static inline struct simple_children *to_simple_children(struct config_item *item)
179 return container_of(to_config_group(item),
180 struct simple_children, group);
183 static struct config_item *simple_children_make_item(struct config_group *group,
186 struct simple_child *simple_child;
188 simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
190 return ERR_PTR(-ENOMEM);
192 config_item_init_type_name(&simple_child->item, name,
195 return &simple_child->item;
198 static ssize_t simple_children_description_show(struct config_item *item,
202 "[02-simple-children]\n"
204 "This subsystem allows the creation of child config_items. These\n"
205 "items have only one attribute that is readable and writeable.\n");
208 CONFIGFS_ATTR_RO(simple_children_, description);
210 static struct configfs_attribute *simple_children_attrs[] = {
211 &simple_children_attr_description,
215 static void simple_children_release(struct config_item *item)
217 kfree(to_simple_children(item));
220 static struct configfs_item_operations simple_children_item_ops = {
221 .release = simple_children_release,
225 * Note that, since no extra work is required on ->drop_item(),
226 * no ->drop_item() is provided.
228 static struct configfs_group_operations simple_children_group_ops = {
229 .make_item = simple_children_make_item,
232 static const struct config_item_type simple_children_type = {
233 .ct_item_ops = &simple_children_item_ops,
234 .ct_group_ops = &simple_children_group_ops,
235 .ct_attrs = simple_children_attrs,
236 .ct_owner = THIS_MODULE,
239 static struct configfs_subsystem simple_children_subsys = {
242 .ci_namebuf = "02-simple-children",
243 .ci_type = &simple_children_type,
248 /* ----------------------------------------------------------------- */
253 * This example reuses the simple_children group from above. However,
254 * the simple_children group is not the subsystem itself, it is a
255 * child of the subsystem. Creation of a group in the subsystem creates
256 * a new simple_children group. That group can then have simple_child
257 * children of its own.
260 static struct config_group *group_children_make_group(
261 struct config_group *group, const char *name)
263 struct simple_children *simple_children;
265 simple_children = kzalloc(sizeof(struct simple_children),
267 if (!simple_children)
268 return ERR_PTR(-ENOMEM);
270 config_group_init_type_name(&simple_children->group, name,
271 &simple_children_type);
273 return &simple_children->group;
276 static ssize_t group_children_description_show(struct config_item *item,
280 "[03-group-children]\n"
282 "This subsystem allows the creation of child config_groups. These\n"
283 "groups are like the subsystem simple-children.\n");
286 CONFIGFS_ATTR_RO(group_children_, description);
288 static struct configfs_attribute *group_children_attrs[] = {
289 &group_children_attr_description,
294 * Note that, since no extra work is required on ->drop_item(),
295 * no ->drop_item() is provided.
297 static struct configfs_group_operations group_children_group_ops = {
298 .make_group = group_children_make_group,
301 static const struct config_item_type group_children_type = {
302 .ct_group_ops = &group_children_group_ops,
303 .ct_attrs = group_children_attrs,
304 .ct_owner = THIS_MODULE,
307 static struct configfs_subsystem group_children_subsys = {
310 .ci_namebuf = "03-group-children",
311 .ci_type = &group_children_type,
316 /* ----------------------------------------------------------------- */
319 * We're now done with our subsystem definitions.
320 * For convenience in this module, here's a list of them all. It
321 * allows the init function to easily register them. Most modules
322 * will only have one subsystem, and will only call register_subsystem
325 static struct configfs_subsystem *example_subsys[] = {
326 &childless_subsys.subsys,
327 &simple_children_subsys,
328 &group_children_subsys,
332 static int __init configfs_example_init(void)
334 struct configfs_subsystem *subsys;
337 for (i = 0; example_subsys[i]; i++) {
338 subsys = example_subsys[i];
340 config_group_init(&subsys->su_group);
341 mutex_init(&subsys->su_mutex);
342 ret = configfs_register_subsystem(subsys);
344 pr_err("Error %d while registering subsystem %s\n",
345 ret, subsys->su_group.cg_item.ci_namebuf);
353 for (i--; i >= 0; i--)
354 configfs_unregister_subsystem(example_subsys[i]);
359 static void __exit configfs_example_exit(void)
363 for (i = 0; example_subsys[i]; i++)
364 configfs_unregister_subsystem(example_subsys[i]);
367 module_init(configfs_example_init);
368 module_exit(configfs_example_exit);
369 MODULE_LICENSE("GPL");