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