kset-example.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. sscanf(buf, "%du", &foo_obj->foo);
  107. return count;
  108. }
  109. /* Sysfs attributes cannot be world-writable. */
  110. static struct foo_attribute foo_attribute =
  111. __ATTR(foo, 0664, foo_show, foo_store);
  112. /*
  113. * More complex function where we determine which variable is being accessed by
  114. * looking at the attribute for the "baz" and "bar" files.
  115. */
  116. static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
  117. char *buf)
  118. {
  119. int var;
  120. if (strcmp(attr->attr.name, "baz") == 0)
  121. var = foo_obj->baz;
  122. else
  123. var = foo_obj->bar;
  124. return sprintf(buf, "%d\n", var);
  125. }
  126. static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
  127. const char *buf, size_t count)
  128. {
  129. int var;
  130. sscanf(buf, "%du", &var);
  131. if (strcmp(attr->attr.name, "baz") == 0)
  132. foo_obj->baz = var;
  133. else
  134. foo_obj->bar = var;
  135. return count;
  136. }
  137. static struct foo_attribute baz_attribute =
  138. __ATTR(baz, 0664, b_show, b_store);
  139. static struct foo_attribute bar_attribute =
  140. __ATTR(bar, 0664, b_show, b_store);
  141. /*
  142. * Create a group of attributes so that we can create and destroy them all
  143. * at once.
  144. */
  145. static struct attribute *foo_default_attrs[] = {
  146. &foo_attribute.attr,
  147. &baz_attribute.attr,
  148. &bar_attribute.attr,
  149. NULL, /* need to NULL terminate the list of attributes */
  150. };
  151. /*
  152. * Our own ktype for our kobjects. Here we specify our sysfs ops, the
  153. * release function, and the set of default attributes we want created
  154. * whenever a kobject of this type is registered with the kernel.
  155. */
  156. static struct kobj_type foo_ktype = {
  157. .sysfs_ops = &foo_sysfs_ops,
  158. .release = foo_release,
  159. .default_attrs = foo_default_attrs,
  160. };
  161. static struct kset *example_kset;
  162. static struct foo_obj *foo_obj;
  163. static struct foo_obj *bar_obj;
  164. static struct foo_obj *baz_obj;
  165. static struct foo_obj *create_foo_obj(const char *name)
  166. {
  167. struct foo_obj *foo;
  168. int retval;
  169. /* allocate the memory for the whole object */
  170. foo = kzalloc(sizeof(*foo), GFP_KERNEL);
  171. if (!foo)
  172. return NULL;
  173. /*
  174. * As we have a kset for this kobject, we need to set it before calling
  175. * the kobject core.
  176. */
  177. foo->kobj.kset = example_kset;
  178. /*
  179. * Initialize and add the kobject to the kernel. All the default files
  180. * will be created here. As we have already specified a kset for this
  181. * kobject, we don't have to set a parent for the kobject, the kobject
  182. * will be placed beneath that kset automatically.
  183. */
  184. retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
  185. if (retval) {
  186. kobject_put(&foo->kobj);
  187. return NULL;
  188. }
  189. /*
  190. * We are always responsible for sending the uevent that the kobject
  191. * was added to the system.
  192. */
  193. kobject_uevent(&foo->kobj, KOBJ_ADD);
  194. return foo;
  195. }
  196. static void destroy_foo_obj(struct foo_obj *foo)
  197. {
  198. kobject_put(&foo->kobj);
  199. }
  200. static int __init example_init(void)
  201. {
  202. /*
  203. * Create a kset with the name of "kset_example",
  204. * located under /sys/kernel/
  205. */
  206. example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
  207. if (!example_kset)
  208. return -ENOMEM;
  209. /*
  210. * Create three objects and register them with our kset
  211. */
  212. foo_obj = create_foo_obj("foo");
  213. if (!foo_obj)
  214. goto foo_error;
  215. bar_obj = create_foo_obj("bar");
  216. if (!bar_obj)
  217. goto bar_error;
  218. baz_obj = create_foo_obj("baz");
  219. if (!baz_obj)
  220. goto baz_error;
  221. return 0;
  222. baz_error:
  223. destroy_foo_obj(bar_obj);
  224. bar_error:
  225. destroy_foo_obj(foo_obj);
  226. foo_error:
  227. kset_unregister(example_kset);
  228. return -EINVAL;
  229. }
  230. static void __exit example_exit(void)
  231. {
  232. destroy_foo_obj(baz_obj);
  233. destroy_foo_obj(bar_obj);
  234. destroy_foo_obj(foo_obj);
  235. kset_unregister(example_kset);
  236. }
  237. module_init(example_init);
  238. module_exit(example_exit);
  239. MODULE_LICENSE("GPL");
  240. MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");