led-triggers.c 9.0 KB

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