1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * vim: noexpandtab ts=8 sts=0 sw=8: 4 * 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 8 * 9 * Based on sysfs: 10 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel 11 * 12 * configfs Copyright (C) 2005 Oracle. All rights reserved. 13 */ 14 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> 20 21 /* 22 * 01-childless 23 * 24 * This first example is a childless subsystem. It cannot create 25 * any config_items. It just has attributes. 26 * 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 30 * such a subsystem. 31 */ 32 33 struct childless { 34 struct configfs_subsystem subsys; 35 int showme; 36 int storeme; 37 }; 38 39 static inline struct childless *to_childless(struct config_item *item) 40 { 41 return container_of(to_configfs_subsystem(to_config_group(item)), 42 struct childless, subsys); 43 } 44 45 static ssize_t childless_showme_show(struct config_item *item, char *page) 46 { 47 struct childless *childless = to_childless(item); 48 ssize_t pos; 49 50 pos = sprintf(page, "%d\n", childless->showme); 51 childless->showme++; 52 53 return pos; 54 } 55 56 static ssize_t childless_storeme_show(struct config_item *item, char *page) 57 { 58 return sprintf(page, "%d\n", to_childless(item)->storeme); 59 } 60 61 static ssize_t childless_storeme_store(struct config_item *item, 62 const char *page, size_t count) 63 { 64 struct childless *childless = to_childless(item); 65 int ret; 66 67 ret = kstrtoint(page, 10, &childless->storeme); 68 if (ret) 69 return ret; 70 71 return count; 72 } 73 74 static ssize_t childless_description_show(struct config_item *item, char *page) 75 { 76 return sprintf(page, 77 "[01-childless]\n" 78 "\n" 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"); 83 } 84 85 CONFIGFS_ATTR_RO(childless_, showme); 86 CONFIGFS_ATTR(childless_, storeme); 87 CONFIGFS_ATTR_RO(childless_, description); 88 89 static struct configfs_attribute *childless_attrs[] = { 90 &childless_attr_showme, 91 &childless_attr_storeme, 92 &childless_attr_description, 93 NULL, 94 }; 95 96 static const struct config_item_type childless_type = { 97 .ct_attrs = childless_attrs, 98 .ct_owner = THIS_MODULE, 99 }; 100 101 static struct childless childless_subsys = { 102 .subsys = { 103 .su_group = { 104 .cg_item = { 105 .ci_namebuf = "01-childless", 106 .ci_type = &childless_type, 107 }, 108 }, 109 }, 110 }; 111 112 /* ----------------------------------------------------------------- */ 113 114 /* 115 * 02-simple-children 116 * 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. 121 */ 122 123 struct simple_child { 124 struct config_item item; 125 int storeme; 126 }; 127 128 static inline struct simple_child *to_simple_child(struct config_item *item) 129 { 130 return container_of(item, struct simple_child, item); 131 } 132 133 static ssize_t simple_child_storeme_show(struct config_item *item, char *page) 134 { 135 return sprintf(page, "%d\n", to_simple_child(item)->storeme); 136 } 137 138 static ssize_t simple_child_storeme_store(struct config_item *item, 139 const char *page, size_t count) 140 { 141 struct simple_child *simple_child = to_simple_child(item); 142 int ret; 143 144 ret = kstrtoint(page, 10, &simple_child->storeme); 145 if (ret) 146 return ret; 147 148 return count; 149 } 150 151 CONFIGFS_ATTR(simple_child_, storeme); 152 153 static struct configfs_attribute *simple_child_attrs[] = { 154 &simple_child_attr_storeme, 155 NULL, 156 }; 157 158 static void simple_child_release(struct config_item *item) 159 { 160 kfree(to_simple_child(item)); 161 } 162 163 static struct configfs_item_operations simple_child_item_ops = { 164 .release = simple_child_release, 165 }; 166 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, 171 }; 172 173 struct simple_children { 174 struct config_group group; 175 }; 176 177 static inline struct simple_children *to_simple_children(struct config_item *item) 178 { 179 return container_of(to_config_group(item), 180 struct simple_children, group); 181 } 182 183 static struct config_item *simple_children_make_item(struct config_group *group, 184 const char *name) 185 { 186 struct simple_child *simple_child; 187 188 simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL); 189 if (!simple_child) 190 return ERR_PTR(-ENOMEM); 191 192 config_item_init_type_name(&simple_child->item, name, 193 &simple_child_type); 194 195 simple_child->storeme = 0; 196 197 return &simple_child->item; 198 } 199 200 static ssize_t simple_children_description_show(struct config_item *item, 201 char *page) 202 { 203 return sprintf(page, 204 "[02-simple-children]\n" 205 "\n" 206 "This subsystem allows the creation of child config_items. These\n" 207 "items have only one attribute that is readable and writeable.\n"); 208 } 209 210 CONFIGFS_ATTR_RO(simple_children_, description); 211 212 static struct configfs_attribute *simple_children_attrs[] = { 213 &simple_children_attr_description, 214 NULL, 215 }; 216 217 static void simple_children_release(struct config_item *item) 218 { 219 kfree(to_simple_children(item)); 220 } 221 222 static struct configfs_item_operations simple_children_item_ops = { 223 .release = simple_children_release, 224 }; 225 226 /* 227 * Note that, since no extra work is required on ->drop_item(), 228 * no ->drop_item() is provided. 229 */ 230 static struct configfs_group_operations simple_children_group_ops = { 231 .make_item = simple_children_make_item, 232 }; 233 234 static const struct config_item_type simple_children_type = { 235 .ct_item_ops = &simple_children_item_ops, 236 .ct_group_ops = &simple_children_group_ops, 237 .ct_attrs = simple_children_attrs, 238 .ct_owner = THIS_MODULE, 239 }; 240 241 static struct configfs_subsystem simple_children_subsys = { 242 .su_group = { 243 .cg_item = { 244 .ci_namebuf = "02-simple-children", 245 .ci_type = &simple_children_type, 246 }, 247 }, 248 }; 249 250 /* ----------------------------------------------------------------- */ 251 252 /* 253 * 03-group-children 254 * 255 * This example reuses the simple_children group from above. However, 256 * the simple_children group is not the subsystem itself, it is a 257 * child of the subsystem. Creation of a group in the subsystem creates 258 * a new simple_children group. That group can then have simple_child 259 * children of its own. 260 */ 261 262 static struct config_group *group_children_make_group( 263 struct config_group *group, const char *name) 264 { 265 struct simple_children *simple_children; 266 267 simple_children = kzalloc(sizeof(struct simple_children), 268 GFP_KERNEL); 269 if (!simple_children) 270 return ERR_PTR(-ENOMEM); 271 272 config_group_init_type_name(&simple_children->group, name, 273 &simple_children_type); 274 275 return &simple_children->group; 276 } 277 278 static ssize_t group_children_description_show(struct config_item *item, 279 char *page) 280 { 281 return sprintf(page, 282 "[03-group-children]\n" 283 "\n" 284 "This subsystem allows the creation of child config_groups. These\n" 285 "groups are like the subsystem simple-children.\n"); 286 } 287 288 CONFIGFS_ATTR_RO(group_children_, description); 289 290 static struct configfs_attribute *group_children_attrs[] = { 291 &group_children_attr_description, 292 NULL, 293 }; 294 295 /* 296 * Note that, since no extra work is required on ->drop_item(), 297 * no ->drop_item() is provided. 298 */ 299 static struct configfs_group_operations group_children_group_ops = { 300 .make_group = group_children_make_group, 301 }; 302 303 static const struct config_item_type group_children_type = { 304 .ct_group_ops = &group_children_group_ops, 305 .ct_attrs = group_children_attrs, 306 .ct_owner = THIS_MODULE, 307 }; 308 309 static struct configfs_subsystem group_children_subsys = { 310 .su_group = { 311 .cg_item = { 312 .ci_namebuf = "03-group-children", 313 .ci_type = &group_children_type, 314 }, 315 }, 316 }; 317 318 /* ----------------------------------------------------------------- */ 319 320 /* 321 * We're now done with our subsystem definitions. 322 * For convenience in this module, here's a list of them all. It 323 * allows the init function to easily register them. Most modules 324 * will only have one subsystem, and will only call register_subsystem 325 * on it directly. 326 */ 327 static struct configfs_subsystem *example_subsys[] = { 328 &childless_subsys.subsys, 329 &simple_children_subsys, 330 &group_children_subsys, 331 NULL, 332 }; 333 334 static int __init configfs_example_init(void) 335 { 336 int ret; 337 int i; 338 struct configfs_subsystem *subsys; 339 340 for (i = 0; example_subsys[i]; i++) { 341 subsys = example_subsys[i]; 342 343 config_group_init(&subsys->su_group); 344 mutex_init(&subsys->su_mutex); 345 ret = configfs_register_subsystem(subsys); 346 if (ret) { 347 printk(KERN_ERR "Error %d while registering subsystem %s\n", 348 ret, 349 subsys->su_group.cg_item.ci_namebuf); 350 goto out_unregister; 351 } 352 } 353 354 return 0; 355 356 out_unregister: 357 for (i--; i >= 0; i--) 358 configfs_unregister_subsystem(example_subsys[i]); 359 360 return ret; 361 } 362 363 static void __exit configfs_example_exit(void) 364 { 365 int i; 366 367 for (i = 0; example_subsys[i]; i++) 368 configfs_unregister_subsystem(example_subsys[i]); 369 } 370 371 module_init(configfs_example_init); 372 module_exit(configfs_example_exit); 373 MODULE_LICENSE("GPL"); 374