kset-example.c 7.5 KB

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