led-triggers.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * LED Triggers Core
  3. *
  4. * Copyright 2005-2007 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <rpurdie@openedhand.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/list.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/device.h>
  19. #include <linux/sysdev.h>
  20. #include <linux/timer.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/leds.h>
  23. #include <linux/slab.h>
  24. #include "leds.h"
  25. /*
  26. * Nests outside led_cdev->trigger_lock
  27. */
  28. static DECLARE_RWSEM(triggers_list_lock);
  29. static LIST_HEAD(trigger_list);
  30. /* Used by LED Class */
  31. ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
  32. const char *buf, size_t count)
  33. {
  34. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  35. char trigger_name[TRIG_NAME_MAX];
  36. struct led_trigger *trig;
  37. size_t len;
  38. trigger_name[sizeof(trigger_name) - 1] = '\0';
  39. strncpy(trigger_name, buf, sizeof(trigger_name) - 1);
  40. len = strlen(trigger_name);
  41. if (len && trigger_name[len - 1] == '\n')
  42. trigger_name[len - 1] = '\0';
  43. if (!strcmp(trigger_name, "none")) {
  44. led_trigger_remove(led_cdev);
  45. return count;
  46. }
  47. down_read(&triggers_list_lock);
  48. list_for_each_entry(trig, &trigger_list, next_trig) {
  49. if (!strcmp(trigger_name, trig->name)) {
  50. down_write(&led_cdev->trigger_lock);
  51. led_trigger_set(led_cdev, trig);
  52. up_write(&led_cdev->trigger_lock);
  53. up_read(&triggers_list_lock);
  54. return count;
  55. }
  56. }
  57. up_read(&triggers_list_lock);
  58. return -EINVAL;
  59. }
  60. EXPORT_SYMBOL_GPL(led_trigger_store);
  61. ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
  62. char *buf)
  63. {
  64. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  65. struct led_trigger *trig;
  66. int len = 0;
  67. down_read(&triggers_list_lock);
  68. down_read(&led_cdev->trigger_lock);
  69. if (!led_cdev->trigger)
  70. len += sprintf(buf+len, "[none] ");
  71. else
  72. len += sprintf(buf+len, "none ");
  73. list_for_each_entry(trig, &trigger_list, next_trig) {
  74. if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
  75. trig->name))
  76. len += sprintf(buf+len, "[%s] ", trig->name);
  77. else
  78. len += sprintf(buf+len, "%s ", trig->name);
  79. }
  80. up_read(&led_cdev->trigger_lock);
  81. up_read(&triggers_list_lock);
  82. len += sprintf(len+buf, "\n");
  83. return len;
  84. }
  85. EXPORT_SYMBOL_GPL(led_trigger_show);
  86. /* Caller must ensure led_cdev->trigger_lock held */
  87. void led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trigger)
  88. {
  89. unsigned long flags;
  90. /* Remove any existing trigger */
  91. if (led_cdev->trigger) {
  92. write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
  93. list_del(&led_cdev->trig_list);
  94. write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock,
  95. flags);
  96. if (led_cdev->trigger->deactivate)
  97. led_cdev->trigger->deactivate(led_cdev);
  98. led_cdev->trigger = NULL;
  99. led_brightness_set(led_cdev, LED_OFF);
  100. }
  101. if (trigger) {
  102. write_lock_irqsave(&trigger->leddev_list_lock, flags);
  103. list_add_tail(&led_cdev->trig_list, &trigger->led_cdevs);
  104. write_unlock_irqrestore(&trigger->leddev_list_lock, flags);
  105. led_cdev->trigger = trigger;
  106. if (trigger->activate)
  107. trigger->activate(led_cdev);
  108. }
  109. }
  110. EXPORT_SYMBOL_GPL(led_trigger_set);
  111. void led_trigger_remove(struct led_classdev *led_cdev)
  112. {
  113. down_write(&led_cdev->trigger_lock);
  114. led_trigger_set(led_cdev, NULL);
  115. up_write(&led_cdev->trigger_lock);
  116. }
  117. EXPORT_SYMBOL_GPL(led_trigger_remove);
  118. void led_trigger_set_default(struct led_classdev *led_cdev)
  119. {
  120. struct led_trigger *trig;
  121. if (!led_cdev->default_trigger)
  122. return;
  123. down_read(&triggers_list_lock);
  124. down_write(&led_cdev->trigger_lock);
  125. list_for_each_entry(trig, &trigger_list, next_trig) {
  126. if (!strcmp(led_cdev->default_trigger, trig->name))
  127. led_trigger_set(led_cdev, trig);
  128. }
  129. up_write(&led_cdev->trigger_lock);
  130. up_read(&triggers_list_lock);
  131. }
  132. EXPORT_SYMBOL_GPL(led_trigger_set_default);
  133. /* LED Trigger Interface */
  134. int led_trigger_register(struct led_trigger *trigger)
  135. {
  136. struct led_classdev *led_cdev;
  137. struct led_trigger *trig;
  138. rwlock_init(&trigger->leddev_list_lock);
  139. INIT_LIST_HEAD(&trigger->led_cdevs);
  140. down_write(&triggers_list_lock);
  141. /* Make sure the trigger's name isn't already in use */
  142. list_for_each_entry(trig, &trigger_list, next_trig) {
  143. if (!strcmp(trig->name, trigger->name)) {
  144. up_write(&triggers_list_lock);
  145. return -EEXIST;
  146. }
  147. }
  148. /* Add to the list of led triggers */
  149. list_add_tail(&trigger->next_trig, &trigger_list);
  150. up_write(&triggers_list_lock);
  151. /* Register with any LEDs that have this as a default trigger */
  152. down_read(&leds_list_lock);
  153. list_for_each_entry(led_cdev, &leds_list, node) {
  154. down_write(&led_cdev->trigger_lock);
  155. if (!led_cdev->trigger && led_cdev->default_trigger &&
  156. !strcmp(led_cdev->default_trigger, trigger->name))
  157. led_trigger_set(led_cdev, trigger);
  158. up_write(&led_cdev->trigger_lock);
  159. }
  160. up_read(&leds_list_lock);
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(led_trigger_register);
  164. void led_trigger_unregister(struct led_trigger *trigger)
  165. {
  166. struct led_classdev *led_cdev;
  167. /* Remove from the list of led triggers */
  168. down_write(&triggers_list_lock);
  169. list_del(&trigger->next_trig);
  170. up_write(&triggers_list_lock);
  171. /* Remove anyone actively using this trigger */
  172. down_read(&leds_list_lock);
  173. list_for_each_entry(led_cdev, &leds_list, node) {
  174. down_write(&led_cdev->trigger_lock);
  175. if (led_cdev->trigger == trigger)
  176. led_trigger_set(led_cdev, NULL);
  177. up_write(&led_cdev->trigger_lock);
  178. }
  179. up_read(&leds_list_lock);
  180. }
  181. EXPORT_SYMBOL_GPL(led_trigger_unregister);
  182. /* Simple LED Tigger Interface */
  183. void led_trigger_event(struct led_trigger *trigger,
  184. enum led_brightness brightness)
  185. {
  186. struct list_head *entry;
  187. if (!trigger)
  188. return;
  189. read_lock(&trigger->leddev_list_lock);
  190. list_for_each(entry, &trigger->led_cdevs) {
  191. struct led_classdev *led_cdev;
  192. led_cdev = list_entry(entry, struct led_classdev, trig_list);
  193. led_set_brightness(led_cdev, brightness);
  194. }
  195. read_unlock(&trigger->leddev_list_lock);
  196. }
  197. EXPORT_SYMBOL_GPL(led_trigger_event);
  198. void led_trigger_blink(struct led_trigger *trigger,
  199. unsigned long *delay_on,
  200. unsigned long *delay_off)
  201. {
  202. struct list_head *entry;
  203. if (!trigger)
  204. return;
  205. read_lock(&trigger->leddev_list_lock);
  206. list_for_each(entry, &trigger->led_cdevs) {
  207. struct led_classdev *led_cdev;
  208. led_cdev = list_entry(entry, struct led_classdev, trig_list);
  209. led_blink_set(led_cdev, delay_on, delay_off);
  210. }
  211. read_unlock(&trigger->leddev_list_lock);
  212. }
  213. EXPORT_SYMBOL_GPL(led_trigger_blink);
  214. void led_trigger_register_simple(const char *name, struct led_trigger **tp)
  215. {
  216. struct led_trigger *trigger;
  217. int err;
  218. trigger = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
  219. if (trigger) {
  220. trigger->name = name;
  221. err = led_trigger_register(trigger);
  222. if (err < 0)
  223. printk(KERN_WARNING "LED trigger %s failed to register"
  224. " (%d)\n", name, err);
  225. } else
  226. printk(KERN_WARNING "LED trigger %s failed to register"
  227. " (no memory)\n", name);
  228. *tp = trigger;
  229. }
  230. EXPORT_SYMBOL_GPL(led_trigger_register_simple);
  231. void led_trigger_unregister_simple(struct led_trigger *trigger)
  232. {
  233. if (trigger)
  234. led_trigger_unregister(trigger);
  235. kfree(trigger);
  236. }
  237. EXPORT_SYMBOL_GPL(led_trigger_unregister_simple);
  238. MODULE_AUTHOR("Richard Purdie");
  239. MODULE_LICENSE("GPL");
  240. MODULE_DESCRIPTION("LED Triggers Core");