kset-example.c 6.8 KB

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