kobject-example.c 3.4 KB

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