led.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>
  6. #include <linux/device.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/slab.h>
  9. #include <linux/pci.h>
  10. #include <linux/i2c.h>
  11. #include <linux/acpi.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/wait.h>
  14. #include <linux/sched.h>
  15. #include <linux/poll.h>
  16. #include <linux/mutex.h>
  17. #include <linux/delay.h>
  18. #include "light_ring.h"
  19. extern struct kobject *vfiec_kobj;
  20. static struct kobject *led_kobj = NULL;
  21. /* ==================== color ==================== */
  22. static ssize_t color_show(struct kobject *kobj, struct kobj_attribute *attr,
  23. char *buf)
  24. {
  25. static int count = 0;
  26. count++;
  27. return sprintf(buf, "color_show count=%d\n", count);
  28. }
  29. static ssize_t color_store(struct kobject *kobj, struct kobj_attribute *attr,
  30. const char *buf, size_t count)
  31. {
  32. printk("color_store kernel rev:%s\n", buf);
  33. return count;
  34. }
  35. static struct kobj_attribute color_attr =
  36. __ATTR(color, 0644, color_show, color_store);
  37. /* ==================== mode ==================== */
  38. static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr,
  39. char *buf)
  40. {
  41. static int count = 0;
  42. count++;
  43. return sprintf(buf, "mode_show count=%d\n", count);
  44. }
  45. static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
  46. const char *buf, size_t count)
  47. {
  48. printk("mode_store kernel rev:%s\n", buf);
  49. return count;
  50. }
  51. static struct kobj_attribute mode_attr =
  52. __ATTR(mode, 0644, mode_show, mode_store);
  53. /* ==================== 属性组 ==================== */
  54. static struct attribute *led_attrs[] = {
  55. &color_attr.attr,
  56. &mode_attr.attr,
  57. NULL,
  58. };
  59. static struct attribute_group led_attr_group = {
  60. .attrs = led_attrs,
  61. };
  62. int led_init(void)
  63. {
  64. int ret;
  65. /* 创建 /sys/kernel/vfiec/lightring */
  66. led_kobj = kobject_create_and_add("led", vfiec_kobj);
  67. if (!led_kobj)
  68. {
  69. ret = -ENOMEM;
  70. }
  71. /* 创建属性文件 */
  72. ret = sysfs_create_group(led_kobj, &led_attr_group);
  73. if (ret)
  74. {
  75. pr_err("Failed to create sysfs group: %d\n", ret);
  76. }
  77. return ret;
  78. }
  79. void led_exit(void)
  80. {
  81. sysfs_remove_group(led_kobj, &led_attr_group);
  82. kobject_put(led_kobj);
  83. }