leds.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * LED support code, ripped out of arch/arm/kernel/time.c
  3. *
  4. * Copyright (C) 1994-2001 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/notifier.h>
  13. #include <linux/cpu.h>
  14. #include <linux/sysdev.h>
  15. #include <linux/syscore_ops.h>
  16. #include <asm/leds.h>
  17. static void dummy_leds_event(led_event_t evt)
  18. {
  19. }
  20. void (*leds_event)(led_event_t) = dummy_leds_event;
  21. struct leds_evt_name {
  22. const char name[8];
  23. int on;
  24. int off;
  25. };
  26. static const struct leds_evt_name evt_names[] = {
  27. { "amber", led_amber_on, led_amber_off },
  28. { "blue", led_blue_on, led_blue_off },
  29. { "green", led_green_on, led_green_off },
  30. { "red", led_red_on, led_red_off },
  31. };
  32. static ssize_t leds_store(struct sys_device *dev,
  33. struct sysdev_attribute *attr,
  34. const char *buf, size_t size)
  35. {
  36. int ret = -EINVAL, len = strcspn(buf, " ");
  37. if (len > 0 && buf[len] == '\0')
  38. len--;
  39. if (strncmp(buf, "claim", len) == 0) {
  40. leds_event(led_claim);
  41. ret = size;
  42. } else if (strncmp(buf, "release", len) == 0) {
  43. leds_event(led_release);
  44. ret = size;
  45. } else {
  46. int i;
  47. for (i = 0; i < ARRAY_SIZE(evt_names); i++) {
  48. if (strlen(evt_names[i].name) != len ||
  49. strncmp(buf, evt_names[i].name, len) != 0)
  50. continue;
  51. if (strncmp(buf+len, " on", 3) == 0) {
  52. leds_event(evt_names[i].on);
  53. ret = size;
  54. } else if (strncmp(buf+len, " off", 4) == 0) {
  55. leds_event(evt_names[i].off);
  56. ret = size;
  57. }
  58. break;
  59. }
  60. }
  61. return ret;
  62. }
  63. static SYSDEV_ATTR(event, 0200, NULL, leds_store);
  64. static struct sysdev_class leds_sysclass = {
  65. .name = "leds",
  66. };
  67. static struct sys_device leds_device = {
  68. .id = 0,
  69. .cls = &leds_sysclass,
  70. };
  71. static int leds_suspend(void)
  72. {
  73. leds_event(led_stop);
  74. return 0;
  75. }
  76. static void leds_resume(void)
  77. {
  78. leds_event(led_start);
  79. }
  80. static void leds_shutdown(void)
  81. {
  82. leds_event(led_halted);
  83. }
  84. static struct syscore_ops leds_syscore_ops = {
  85. .shutdown = leds_shutdown,
  86. .suspend = leds_suspend,
  87. .resume = leds_resume,
  88. };
  89. static int leds_idle_notifier(struct notifier_block *nb, unsigned long val,
  90. void *data)
  91. {
  92. switch (val) {
  93. case IDLE_START:
  94. leds_event(led_idle_start);
  95. break;
  96. case IDLE_END:
  97. leds_event(led_idle_end);
  98. break;
  99. }
  100. return 0;
  101. }
  102. static struct notifier_block leds_idle_nb = {
  103. .notifier_call = leds_idle_notifier,
  104. };
  105. static int __init leds_init(void)
  106. {
  107. int ret;
  108. ret = sysdev_class_register(&leds_sysclass);
  109. if (ret == 0)
  110. ret = sysdev_register(&leds_device);
  111. if (ret == 0)
  112. ret = sysdev_create_file(&leds_device, &attr_event);
  113. if (ret == 0) {
  114. register_syscore_ops(&leds_syscore_ops);
  115. idle_notifier_register(&leds_idle_nb);
  116. }
  117. return ret;
  118. }
  119. device_initcall(leds_init);
  120. EXPORT_SYMBOL(leds_event);