kobject-example.c 3.3 KB

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