kset-example.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sample kset and ktype implementation
  4. *
  5. * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
  6. * Copyright (C) 2007 Novell Inc.
  7. */
  8. #include <linux/kobject.h>
  9. #include <linux/string.h>
  10. #include <linux/sysfs.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. /*
  15. * This module shows how to create a kset in sysfs called
  16. * /sys/kernel/kset-example
  17. * Then tree kobjects are created and assigned to this kset, "foo", "baz",
  18. * and "bar". In those kobjects, attributes of the same name are also
  19. * created and if an integer is written to these files, it can be later
  20. * read out of it.
  21. */
  22. /*
  23. * This is our "object" that we will create a few of and register them with
  24. * sysfs.
  25. */
  26. struct foo_obj {
  27. struct kobject kobj;
  28. int foo;
  29. int baz;
  30. int bar;
  31. };
  32. #define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
  33. /* a custom attribute that works just for a struct foo_obj. */
  34. struct foo_attribute {
  35. struct attribute attr;
  36. ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
  37. ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
  38. };
  39. #define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
  40. /*
  41. * The default show function that must be passed to sysfs. This will be
  42. * called by sysfs for whenever a show function is called by the user on a
  43. * sysfs file associated with the kobjects we have registered. We need to
  44. * transpose back from a "default" kobject to our custom struct foo_obj and
  45. * then call the show function for that specific object.
  46. */
  47. static ssize_t foo_attr_show(struct kobject *kobj,
  48. struct attribute *attr,
  49. char *buf)
  50. {
  51. struct foo_attribute *attribute;
  52. struct foo_obj *foo;
  53. attribute = to_foo_attr(attr);
  54. foo = to_foo_obj(kobj);
  55. if (!attribute->show)
  56. return -EIO;
  57. return attribute->show(foo, attribute, buf);
  58. }
  59. /*
  60. * Just like the default show function above, but this one is for when the
  61. * sysfs "store" is requested (when a value is written to a file.)
  62. */
  63. static ssize_t foo_attr_store(struct kobject *kobj,
  64. struct attribute *attr,
  65. const char *buf, size_t len)
  66. {
  67. struct foo_attribute *attribute;
  68. struct foo_obj *foo;
  69. attribute = to_foo_attr(attr);
  70. foo = to_foo_obj(kobj);
  71. if (!attribute->store)
  72. return -EIO;
  73. return attribute->store(foo, attribute, buf, len);
  74. }
  75. /* Our custom sysfs_ops that we will associate with our ktype later on */
  76. static const struct sysfs_ops foo_sysfs_ops = {
  77. .show = foo_attr_show,
  78. .store = foo_attr_store,
  79. };
  80. /*
  81. * The release function for our object. This is REQUIRED by the kernel to
  82. * have. We free the memory held in our object here.
  83. *
  84. * NEVER try to get away with just a "blank" release function to try to be
  85. * smarter than the kernel. Turns out, no one ever is...
  86. */
  87. static void foo_release(struct kobject *kobj)
  88. {
  89. struct foo_obj *foo;
  90. foo = to_foo_obj(kobj);
  91. kfree(foo);
  92. }
  93. /*
  94. * The "foo" file where the .foo variable is read from and written to.
  95. */
  96. static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
  97. char *buf)
  98. {
  99. return sprintf(buf, "%d\n", foo_obj->foo);
  100. }
  101. static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
  102. const char *buf, size_t count)
  103. {
  104. int ret;
  105. ret = kstrtoint(buf, 10, &foo_obj->foo);
  106. if (ret < 0)
  107. return ret;
  108. return count;
  109. }
  110. /* Sysfs attributes cannot be world-writable. */
  111. static struct foo_attribute foo_attribute =
  112. __ATTR(foo, 0664, foo_show, foo_store);
  113. /*
  114. * More complex function where we determine which variable is being accessed by
  115. * looking at the attribute for the "baz" and "bar" files.
  116. */
  117. static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
  118. char *buf)
  119. {
  120. int var;
  121. if (strcmp(attr->attr.name, "baz") == 0)
  122. var = foo_obj->baz;
  123. else
  124. var = foo_obj->bar;
  125. return sprintf(buf, "%d\n", var);
  126. }
  127. static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
  128. const char *buf, size_t count)
  129. {
  130. int var, ret;
  131. ret = kstrtoint(buf, 10, &var);
  132. if (ret < 0)
  133. return ret;
  134. if (strcmp(attr->attr.name, "baz") == 0)
  135. foo_obj->baz = var;
  136. else
  137. foo_obj->bar = var;
  138. return count;
  139. }
  140. static struct foo_attribute baz_attribute =
  141. __ATTR(baz, 0664, b_show, b_store);
  142. static struct foo_attribute bar_attribute =
  143. __ATTR(bar, 0664, b_show, b_store);
  144. /*
  145. * Create a group of attributes so that we can create and destroy them all
  146. * at once.
  147. */
  148. static struct attribute *foo_default_attrs[] = {
  149. &foo_attribute.attr,
  150. &baz_attribute.attr,
  151. &bar_attribute.attr,
  152. NULL, /* need to NULL terminate the list of attributes */
  153. };
  154. ATTRIBUTE_GROUPS(foo_default);
  155. /*
  156. * Our own ktype for our kobjects. Here we specify our sysfs ops, the
  157. * release function, and the set of default attributes we want created
  158. * whenever a kobject of this type is registered with the kernel.
  159. */
  160. static struct kobj_type foo_ktype = {
  161. .sysfs_ops = &foo_sysfs_ops,
  162. .release = foo_release,
  163. .default_groups = foo_default_groups,
  164. };
  165. static struct kset *example_kset;
  166. static struct foo_obj *foo_obj;
  167. static struct foo_obj *bar_obj;
  168. static struct foo_obj *baz_obj;
  169. static struct foo_obj *create_foo_obj(const char *name)
  170. {
  171. struct foo_obj *foo;
  172. int retval;
  173. /* allocate the memory for the whole object */
  174. foo = kzalloc(sizeof(*foo), GFP_KERNEL);
  175. if (!foo)
  176. return NULL;
  177. /*
  178. * As we have a kset for this kobject, we need to set it before calling
  179. * the kobject core.
  180. */
  181. foo->kobj.kset = example_kset;
  182. /*
  183. * Initialize and add the kobject to the kernel. All the default files
  184. * will be created here. As we have already specified a kset for this
  185. * kobject, we don't have to set a parent for the kobject, the kobject
  186. * will be placed beneath that kset automatically.
  187. */
  188. retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
  189. if (retval) {
  190. kobject_put(&foo->kobj);
  191. return NULL;
  192. }
  193. /*
  194. * We are always responsible for sending the uevent that the kobject
  195. * was added to the system.
  196. */
  197. kobject_uevent(&foo->kobj, KOBJ_ADD);
  198. return foo;
  199. }
  200. static void destroy_foo_obj(struct foo_obj *foo)
  201. {
  202. kobject_put(&foo->kobj);
  203. }
  204. static int __init example_init(void)
  205. {
  206. /*
  207. * Create a kset with the name of "kset_example",
  208. * located under /sys/kernel/
  209. */
  210. example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
  211. if (!example_kset)
  212. return -ENOMEM;
  213. /*
  214. * Create three objects and register them with our kset
  215. */
  216. foo_obj = create_foo_obj("foo");
  217. if (!foo_obj)
  218. goto foo_error;
  219. bar_obj = create_foo_obj("bar");
  220. if (!bar_obj)
  221. goto bar_error;
  222. baz_obj = create_foo_obj("baz");
  223. if (!baz_obj)
  224. goto baz_error;
  225. return 0;
  226. baz_error:
  227. destroy_foo_obj(bar_obj);
  228. bar_error:
  229. destroy_foo_obj(foo_obj);
  230. foo_error:
  231. kset_unregister(example_kset);
  232. return -EINVAL;
  233. }
  234. static void __exit example_exit(void)
  235. {
  236. destroy_foo_obj(baz_obj);
  237. destroy_foo_obj(bar_obj);
  238. destroy_foo_obj(foo_obj);
  239. kset_unregister(example_kset);
  240. }
  241. module_init(example_init);
  242. module_exit(example_exit);
  243. MODULE_LICENSE("GPL v2");
  244. MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");