kset-example.c 6.7 KB

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