led-triggers.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/list.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/device.h>
  18. #include <linux/timer.h>
  19. #include <linux/rwsem.h>
  20. #include <linux/leds.h>
  21. #include <linux/slab.h>
  22. #include "leds.h"
  23. /*
  24. * Nests outside led_cdev->trigger_lock
  25. */
  26. static DECLARE_RWSEM(triggers_list_lock);
  27. static LIST_HEAD(trigger_list);
  28. /* Used by LED Class */
  29. ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
  30. const char *buf, size_t count)
  31. {
  32. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  33. char trigger_name[TRIG_NAME_MAX];
  34. struct led_trigger *trig;
  35. size_t len;
  36. int ret = count;
  37. mutex_lock(&led_cdev->led_access);
  38. if (led_sysfs_is_disabled(led_cdev)) {
  39. ret = -EBUSY;
  40. goto unlock;
  41. }
  42. trigger_name[sizeof(trigger_name) - 1] = '\0';
  43. strncpy(trigger_name, buf, sizeof(trigger_name) - 1);
  44. len = strlen(trigger_name);
  45. if (len && trigger_name[len - 1] == '\n')
  46. trigger_name[len - 1] = '\0';
  47. if (!strcmp(trigger_name, "none")) {
  48. led_trigger_remove(led_cdev);
  49. goto unlock;
  50. }
  51. down_read(&triggers_list_lock);
  52. list_for_each_entry(trig, &trigger_list, next_trig) {
  53. if (!strcmp(trigger_name, trig->name)) {
  54. down_write(&led_cdev->trigger_lock);
  55. led_trigger_set(led_cdev, trig);
  56. up_write(&led_cdev->trigger_lock);
  57. up_read(&triggers_list_lock);
  58. goto unlock;
  59. }
  60. }
  61. up_read(&triggers_list_lock);
  62. unlock:
  63. mutex_unlock(&led_cdev->led_access);
  64. return ret;
  65. }
  66. EXPORT_SYMBOL_GPL(led_trigger_store);
  67. ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
  68. char *buf)
  69. {
  70. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  71. struct led_trigger *trig;
  72. int len = 0;
  73. down_read(&triggers_list_lock);
  74. down_read(&led_cdev->trigger_lock);
  75. if (!led_cdev->trigger)
  76. len += scnprintf(buf+len, PAGE_SIZE - len, "[none] ");
  77. else
  78. len += scnprintf(buf+len, PAGE_SIZE - len, "none ");
  79. list_for_each_entry(trig, &trigger_list, next_trig) {
  80. if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
  81. trig->name))
  82. len += scnprintf(buf+len, PAGE_SIZE - len, "[%s] ",
  83. trig->name);
  84. else
  85. len += scnprintf(buf+len, PAGE_SIZE - len, "%s ",
  86. trig->name);
  87. }
  88. up_read(&led_cdev->trigger_lock);
  89. up_read(&triggers_list_lock);
  90. len += scnprintf(len+buf, PAGE_SIZE - len, "\n");
  91. return len;
  92. }
  93. EXPORT_SYMBOL_GPL(led_trigger_show);
  94. /* Caller must ensure led_cdev->trigger_lock held */
  95. void led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
  96. {
  97. unsigned long flags;
  98. char *event = NULL;
  99. char *envp[2];
  100. const char *name;
  101. name = trig ? trig->name : "none";
  102. event = kasprintf(GFP_KERNEL, "TRIGGER=%s", name);
  103. /* Remove any existing trigger */
  104. if (led_cdev->trigger) {
  105. write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
  106. list_del(&led_cdev->trig_list);
  107. write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock,
  108. flags);
  109. cancel_work_sync(&led_cdev->set_brightness_work);
  110. led_stop_software_blink(led_cdev);
  111. if (led_cdev->trigger->deactivate)
  112. led_cdev->trigger->deactivate(led_cdev);
  113. led_cdev->trigger = NULL;
  114. led_set_brightness(led_cdev, LED_OFF);
  115. }
  116. if (trig) {
  117. write_lock_irqsave(&trig->leddev_list_lock, flags);
  118. list_add_tail(&led_cdev->trig_list, &trig->led_cdevs);
  119. write_unlock_irqrestore(&trig->leddev_list_lock, flags);
  120. led_cdev->trigger = trig;
  121. if (trig->activate)
  122. trig->activate(led_cdev);
  123. }
  124. if (event) {
  125. envp[0] = event;
  126. envp[1] = NULL;
  127. kobject_uevent_env(&led_cdev->dev->kobj, KOBJ_CHANGE, envp);
  128. kfree(event);
  129. }
  130. }
  131. EXPORT_SYMBOL_GPL(led_trigger_set);
  132. void led_trigger_remove(struct led_classdev *led_cdev)
  133. {
  134. down_write(&led_cdev->trigger_lock);
  135. led_trigger_set(led_cdev, NULL);
  136. up_write(&led_cdev->trigger_lock);
  137. }
  138. EXPORT_SYMBOL_GPL(led_trigger_remove);
  139. void led_trigger_set_default(struct led_classdev *led_cdev)
  140. {
  141. struct led_trigger *trig;
  142. if (!led_cdev->default_trigger)
  143. return;
  144. down_read(&triggers_list_lock);
  145. down_write(&led_cdev->trigger_lock);
  146. list_for_each_entry(trig, &trigger_list, next_trig) {
  147. if (!strcmp(led_cdev->default_trigger, trig->name))
  148. led_trigger_set(led_cdev, trig);
  149. }
  150. up_write(&led_cdev->trigger_lock);
  151. up_read(&triggers_list_lock);
  152. }
  153. EXPORT_SYMBOL_GPL(led_trigger_set_default);
  154. void led_trigger_rename_static(const char *name, struct led_trigger *trig)
  155. {
  156. /* new name must be on a temporary string to prevent races */
  157. BUG_ON(name == trig->name);
  158. down_write(&triggers_list_lock);
  159. /* this assumes that trig->name was originaly allocated to
  160. * non constant storage */
  161. strcpy((char *)trig->name, name);
  162. up_write(&triggers_list_lock);
  163. }
  164. EXPORT_SYMBOL_GPL(led_trigger_rename_static);
  165. /* LED Trigger Interface */
  166. int led_trigger_register(struct led_trigger *trig)
  167. {
  168. struct led_classdev *led_cdev;
  169. struct led_trigger *_trig;
  170. rwlock_init(&trig->leddev_list_lock);
  171. INIT_LIST_HEAD(&trig->led_cdevs);
  172. down_write(&triggers_list_lock);
  173. /* Make sure the trigger's name isn't already in use */
  174. list_for_each_entry(_trig, &trigger_list, next_trig) {
  175. if (!strcmp(_trig->name, trig->name)) {
  176. up_write(&triggers_list_lock);
  177. return -EEXIST;
  178. }
  179. }
  180. /* Add to the list of led triggers */
  181. list_add_tail(&trig->next_trig, &trigger_list);
  182. up_write(&triggers_list_lock);
  183. /* Register with any LEDs that have this as a default trigger */
  184. down_read(&leds_list_lock);
  185. list_for_each_entry(led_cdev, &leds_list, node) {
  186. down_write(&led_cdev->trigger_lock);
  187. if (!led_cdev->trigger && led_cdev->default_trigger &&
  188. !strcmp(led_cdev->default_trigger, trig->name))
  189. led_trigger_set(led_cdev, trig);
  190. up_write(&led_cdev->trigger_lock);
  191. }
  192. up_read(&leds_list_lock);
  193. return 0;
  194. }
  195. EXPORT_SYMBOL_GPL(led_trigger_register);
  196. void led_trigger_unregister(struct led_trigger *trig)
  197. {
  198. struct led_classdev *led_cdev;
  199. if (list_empty_careful(&trig->next_trig))
  200. return;
  201. /* Remove from the list of led triggers */
  202. down_write(&triggers_list_lock);
  203. list_del_init(&trig->next_trig);
  204. up_write(&triggers_list_lock);
  205. /* Remove anyone actively using this trigger */
  206. down_read(&leds_list_lock);
  207. list_for_each_entry(led_cdev, &leds_list, node) {
  208. down_write(&led_cdev->trigger_lock);
  209. if (led_cdev->trigger == trig)
  210. led_trigger_set(led_cdev, NULL);
  211. up_write(&led_cdev->trigger_lock);
  212. }
  213. up_read(&leds_list_lock);
  214. }
  215. EXPORT_SYMBOL_GPL(led_trigger_unregister);
  216. /* Simple LED Tigger Interface */
  217. void led_trigger_event(struct led_trigger *trig,
  218. enum led_brightness brightness)
  219. {
  220. struct led_classdev *led_cdev;
  221. if (!trig)
  222. return;
  223. read_lock(&trig->leddev_list_lock);
  224. list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list)
  225. led_set_brightness(led_cdev, brightness);
  226. read_unlock(&trig->leddev_list_lock);
  227. }
  228. EXPORT_SYMBOL_GPL(led_trigger_event);
  229. static void led_trigger_blink_setup(struct led_trigger *trig,
  230. unsigned long *delay_on,
  231. unsigned long *delay_off,
  232. int oneshot,
  233. int invert)
  234. {
  235. struct led_classdev *led_cdev;
  236. if (!trig)
  237. return;
  238. read_lock(&trig->leddev_list_lock);
  239. list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) {
  240. if (oneshot)
  241. led_blink_set_oneshot(led_cdev, delay_on, delay_off,
  242. invert);
  243. else
  244. led_blink_set(led_cdev, delay_on, delay_off);
  245. }
  246. read_unlock(&trig->leddev_list_lock);
  247. }
  248. void led_trigger_blink(struct led_trigger *trig,
  249. unsigned long *delay_on,
  250. unsigned long *delay_off)
  251. {
  252. led_trigger_blink_setup(trig, delay_on, delay_off, 0, 0);
  253. }
  254. EXPORT_SYMBOL_GPL(led_trigger_blink);
  255. void led_trigger_blink_oneshot(struct led_trigger *trig,
  256. unsigned long *delay_on,
  257. unsigned long *delay_off,
  258. int invert)
  259. {
  260. led_trigger_blink_setup(trig, delay_on, delay_off, 1, invert);
  261. }
  262. EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot);
  263. void led_trigger_register_simple(const char *name, struct led_trigger **tp)
  264. {
  265. struct led_trigger *trig;
  266. int err;
  267. trig = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
  268. if (trig) {
  269. trig->name = name;
  270. err = led_trigger_register(trig);
  271. if (err < 0) {
  272. kfree(trig);
  273. trig = NULL;
  274. pr_warn("LED trigger %s failed to register (%d)\n",
  275. name, err);
  276. }
  277. } else {
  278. pr_warn("LED trigger %s failed to register (no memory)\n",
  279. name);
  280. }
  281. *tp = trig;
  282. }
  283. EXPORT_SYMBOL_GPL(led_trigger_register_simple);
  284. void led_trigger_unregister_simple(struct led_trigger *trig)
  285. {
  286. if (trig)
  287. led_trigger_unregister(trig);
  288. kfree(trig);
  289. }
  290. EXPORT_SYMBOL_GPL(led_trigger_unregister_simple);
  291. MODULE_AUTHOR("Richard Purdie");
  292. MODULE_LICENSE("GPL");
  293. MODULE_DESCRIPTION("LED Triggers Core");