kset-example.c 6.9 KB

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