configfs_sample.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/configfs.h>
  19. /*
  20. * 01-childless
  21. *
  22. * This first example is a childless subsystem. It cannot create
  23. * any config_items. It just has attributes.
  24. *
  25. * Note that we are enclosing the configfs_subsystem inside a container.
  26. * This is not necessary if a subsystem has no attributes directly
  27. * on the subsystem. See the next example, 02-simple-children, for
  28. * such a subsystem.
  29. */
  30. struct childless {
  31. struct configfs_subsystem subsys;
  32. int showme;
  33. int storeme;
  34. };
  35. static inline struct childless *to_childless(struct config_item *item)
  36. {
  37. return container_of(to_configfs_subsystem(to_config_group(item)),
  38. struct childless, subsys);
  39. }
  40. static ssize_t childless_showme_show(struct config_item *item, char *page)
  41. {
  42. struct childless *childless = to_childless(item);
  43. ssize_t pos;
  44. pos = sprintf(page, "%d\n", childless->showme);
  45. childless->showme++;
  46. return pos;
  47. }
  48. static ssize_t childless_storeme_show(struct config_item *item, char *page)
  49. {
  50. return sprintf(page, "%d\n", to_childless(item)->storeme);
  51. }
  52. static ssize_t childless_storeme_store(struct config_item *item,
  53. const char *page, size_t count)
  54. {
  55. struct childless *childless = to_childless(item);
  56. int ret;
  57. ret = kstrtoint(page, 10, &childless->storeme);
  58. if (ret)
  59. return ret;
  60. return count;
  61. }
  62. static ssize_t childless_description_show(struct config_item *item, char *page)
  63. {
  64. return sprintf(page,
  65. "[01-childless]\n"
  66. "\n"
  67. "The childless subsystem is the simplest possible subsystem in\n"
  68. "configfs. It does not support the creation of child config_items.\n"
  69. "It only has a few attributes. In fact, it isn't much different\n"
  70. "than a directory in /proc.\n");
  71. }
  72. CONFIGFS_ATTR_RO(childless_, showme);
  73. CONFIGFS_ATTR(childless_, storeme);
  74. CONFIGFS_ATTR_RO(childless_, description);
  75. static struct configfs_attribute *childless_attrs[] = {
  76. &childless_attr_showme,
  77. &childless_attr_storeme,
  78. &childless_attr_description,
  79. NULL,
  80. };
  81. static const struct config_item_type childless_type = {
  82. .ct_attrs = childless_attrs,
  83. .ct_owner = THIS_MODULE,
  84. };
  85. static struct childless childless_subsys = {
  86. .subsys = {
  87. .su_group = {
  88. .cg_item = {
  89. .ci_namebuf = "01-childless",
  90. .ci_type = &childless_type,
  91. },
  92. },
  93. },
  94. };
  95. /* ----------------------------------------------------------------- */
  96. /*
  97. * 02-simple-children
  98. *
  99. * This example merely has a simple one-attribute child. Note that
  100. * there is no extra attribute structure, as the child's attribute is
  101. * known from the get-go. Also, there is no container for the
  102. * subsystem, as it has no attributes of its own.
  103. */
  104. struct simple_child {
  105. struct config_item item;
  106. int storeme;
  107. };
  108. static inline struct simple_child *to_simple_child(struct config_item *item)
  109. {
  110. return container_of(item, struct simple_child, item);
  111. }
  112. static ssize_t simple_child_storeme_show(struct config_item *item, char *page)
  113. {
  114. return sprintf(page, "%d\n", to_simple_child(item)->storeme);
  115. }
  116. static ssize_t simple_child_storeme_store(struct config_item *item,
  117. const char *page, size_t count)
  118. {
  119. struct simple_child *simple_child = to_simple_child(item);
  120. int ret;
  121. ret = kstrtoint(page, 10, &simple_child->storeme);
  122. if (ret)
  123. return ret;
  124. return count;
  125. }
  126. CONFIGFS_ATTR(simple_child_, storeme);
  127. static struct configfs_attribute *simple_child_attrs[] = {
  128. &simple_child_attr_storeme,
  129. NULL,
  130. };
  131. static void simple_child_release(struct config_item *item)
  132. {
  133. kfree(to_simple_child(item));
  134. }
  135. static struct configfs_item_operations simple_child_item_ops = {
  136. .release = simple_child_release,
  137. };
  138. static const struct config_item_type simple_child_type = {
  139. .ct_item_ops = &simple_child_item_ops,
  140. .ct_attrs = simple_child_attrs,
  141. .ct_owner = THIS_MODULE,
  142. };
  143. struct simple_children {
  144. struct config_group group;
  145. };
  146. static inline struct simple_children *to_simple_children(struct config_item *item)
  147. {
  148. return container_of(to_config_group(item),
  149. struct simple_children, group);
  150. }
  151. static struct config_item *simple_children_make_item(struct config_group *group,
  152. const char *name)
  153. {
  154. struct simple_child *simple_child;
  155. simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
  156. if (!simple_child)
  157. return ERR_PTR(-ENOMEM);
  158. config_item_init_type_name(&simple_child->item, name,
  159. &simple_child_type);
  160. return &simple_child->item;
  161. }
  162. static ssize_t simple_children_description_show(struct config_item *item,
  163. char *page)
  164. {
  165. return sprintf(page,
  166. "[02-simple-children]\n"
  167. "\n"
  168. "This subsystem allows the creation of child config_items. These\n"
  169. "items have only one attribute that is readable and writeable.\n");
  170. }
  171. CONFIGFS_ATTR_RO(simple_children_, description);
  172. static struct configfs_attribute *simple_children_attrs[] = {
  173. &simple_children_attr_description,
  174. NULL,
  175. };
  176. static void simple_children_release(struct config_item *item)
  177. {
  178. kfree(to_simple_children(item));
  179. }
  180. static struct configfs_item_operations simple_children_item_ops = {
  181. .release = simple_children_release,
  182. };
  183. /*
  184. * Note that, since no extra work is required on ->drop_item(),
  185. * no ->drop_item() is provided.
  186. */
  187. static struct configfs_group_operations simple_children_group_ops = {
  188. .make_item = simple_children_make_item,
  189. };
  190. static const struct config_item_type simple_children_type = {
  191. .ct_item_ops = &simple_children_item_ops,
  192. .ct_group_ops = &simple_children_group_ops,
  193. .ct_attrs = simple_children_attrs,
  194. .ct_owner = THIS_MODULE,
  195. };
  196. static struct configfs_subsystem simple_children_subsys = {
  197. .su_group = {
  198. .cg_item = {
  199. .ci_namebuf = "02-simple-children",
  200. .ci_type = &simple_children_type,
  201. },
  202. },
  203. };
  204. /* ----------------------------------------------------------------- */
  205. /*
  206. * 03-group-children
  207. *
  208. * This example reuses the simple_children group from above. However,
  209. * the simple_children group is not the subsystem itself, it is a
  210. * child of the subsystem. Creation of a group in the subsystem creates
  211. * a new simple_children group. That group can then have simple_child
  212. * children of its own.
  213. */
  214. static struct config_group *group_children_make_group(
  215. struct config_group *group, const char *name)
  216. {
  217. struct simple_children *simple_children;
  218. simple_children = kzalloc(sizeof(struct simple_children),
  219. GFP_KERNEL);
  220. if (!simple_children)
  221. return ERR_PTR(-ENOMEM);
  222. config_group_init_type_name(&simple_children->group, name,
  223. &simple_children_type);
  224. return &simple_children->group;
  225. }
  226. static ssize_t group_children_description_show(struct config_item *item,
  227. char *page)
  228. {
  229. return sprintf(page,
  230. "[03-group-children]\n"
  231. "\n"
  232. "This subsystem allows the creation of child config_groups. These\n"
  233. "groups are like the subsystem simple-children.\n");
  234. }
  235. CONFIGFS_ATTR_RO(group_children_, description);
  236. static struct configfs_attribute *group_children_attrs[] = {
  237. &group_children_attr_description,
  238. NULL,
  239. };
  240. /*
  241. * Note that, since no extra work is required on ->drop_item(),
  242. * no ->drop_item() is provided.
  243. */
  244. static struct configfs_group_operations group_children_group_ops = {
  245. .make_group = group_children_make_group,
  246. };
  247. static const struct config_item_type group_children_type = {
  248. .ct_group_ops = &group_children_group_ops,
  249. .ct_attrs = group_children_attrs,
  250. .ct_owner = THIS_MODULE,
  251. };
  252. static struct configfs_subsystem group_children_subsys = {
  253. .su_group = {
  254. .cg_item = {
  255. .ci_namebuf = "03-group-children",
  256. .ci_type = &group_children_type,
  257. },
  258. },
  259. };
  260. /* ----------------------------------------------------------------- */
  261. /*
  262. * We're now done with our subsystem definitions.
  263. * For convenience in this module, here's a list of them all. It
  264. * allows the init function to easily register them. Most modules
  265. * will only have one subsystem, and will only call register_subsystem
  266. * on it directly.
  267. */
  268. static struct configfs_subsystem *example_subsys[] = {
  269. &childless_subsys.subsys,
  270. &simple_children_subsys,
  271. &group_children_subsys,
  272. NULL,
  273. };
  274. static int __init configfs_example_init(void)
  275. {
  276. struct configfs_subsystem *subsys;
  277. int ret, i;
  278. for (i = 0; example_subsys[i]; i++) {
  279. subsys = example_subsys[i];
  280. config_group_init(&subsys->su_group);
  281. mutex_init(&subsys->su_mutex);
  282. ret = configfs_register_subsystem(subsys);
  283. if (ret) {
  284. pr_err("Error %d while registering subsystem %s\n",
  285. ret, subsys->su_group.cg_item.ci_namebuf);
  286. goto out_unregister;
  287. }
  288. }
  289. return 0;
  290. out_unregister:
  291. for (i--; i >= 0; i--)
  292. configfs_unregister_subsystem(example_subsys[i]);
  293. return ret;
  294. }
  295. static void __exit configfs_example_exit(void)
  296. {
  297. int i;
  298. for (i = 0; example_subsys[i]; i++)
  299. configfs_unregister_subsystem(example_subsys[i]);
  300. }
  301. module_init(configfs_example_init);
  302. module_exit(configfs_example_exit);
  303. MODULE_LICENSE("GPL");