kobject-example.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Sample kobject 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 simple subdirectory in sysfs called
  17. * /sys/kernel/kobject-example In that directory, 3 files are created:
  18. * "foo", "baz", and "bar". If an integer is written to these files, it can be
  19. * later read out of it.
  20. */
  21. static int foo;
  22. static int baz;
  23. static int bar;
  24. /*
  25. * The "foo" file where a static variable is read from and written to.
  26. */
  27. static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
  28. char *buf)
  29. {
  30. return sprintf(buf, "%d\n", foo);
  31. }
  32. static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
  33. const char *buf, size_t count)
  34. {
  35. int ret;
  36. ret = kstrtoint(buf, 10, &foo);
  37. if (ret < 0)
  38. return ret;
  39. return count;
  40. }
  41. /* Sysfs attributes cannot be world-writable. */
  42. static struct kobj_attribute foo_attribute =
  43. __ATTR(foo, 0664, foo_show, foo_store);
  44. /*
  45. * More complex function where we determine which variable is being accessed by
  46. * looking at the attribute for the "baz" and "bar" files.
  47. */
  48. static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
  49. char *buf)
  50. {
  51. int var;
  52. if (strcmp(attr->attr.name, "baz") == 0)
  53. var = baz;
  54. else
  55. var = bar;
  56. return sprintf(buf, "%d\n", var);
  57. }
  58. static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
  59. const char *buf, size_t count)
  60. {
  61. int var, ret;
  62. ret = kstrtoint(buf, 10, &var);
  63. if (ret < 0)
  64. return ret;
  65. if (strcmp(attr->attr.name, "baz") == 0)
  66. baz = var;
  67. else
  68. bar = var;
  69. return count;
  70. }
  71. static struct kobj_attribute baz_attribute =
  72. __ATTR(baz, 0664, b_show, b_store);
  73. static struct kobj_attribute bar_attribute =
  74. __ATTR(bar, 0664, b_show, b_store);
  75. /*
  76. * Create a group of attributes so that we can create and destroy them all
  77. * at once.
  78. */
  79. static struct attribute *attrs[] = {
  80. &foo_attribute.attr,
  81. &baz_attribute.attr,
  82. &bar_attribute.attr,
  83. NULL, /* need to NULL terminate the list of attributes */
  84. };
  85. /*
  86. * An unnamed attribute group will put all of the attributes directly in
  87. * the kobject directory. If we specify a name, a subdirectory will be
  88. * created for the attributes with the directory being the name of the
  89. * attribute group.
  90. */
  91. static struct attribute_group attr_group = {
  92. .attrs = attrs,
  93. };
  94. static struct kobject *example_kobj;
  95. static int __init example_init(void)
  96. {
  97. int retval;
  98. /*
  99. * Create a simple kobject with the name of "kobject_example",
  100. * located under /sys/kernel/
  101. *
  102. * As this is a simple directory, no uevent will be sent to
  103. * userspace. That is why this function should not be used for
  104. * any type of dynamic kobjects, where the name and number are
  105. * not known ahead of time.
  106. */
  107. example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
  108. if (!example_kobj)
  109. return -ENOMEM;
  110. /* Create the files associated with this kobject */
  111. retval = sysfs_create_group(example_kobj, &attr_group);
  112. if (retval)
  113. kobject_put(example_kobj);
  114. return retval;
  115. }
  116. static void __exit example_exit(void)
  117. {
  118. kobject_put(example_kobj);
  119. }
  120. module_init(example_init);
  121. module_exit(example_exit);
  122. MODULE_LICENSE("GPL v2");
  123. MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");