ledtrig-transient.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * LED Kernel Transient Trigger
  3. *
  4. * Copyright (C) 2012 Shuah Khan <shuahkhan@gmail.com>
  5. *
  6. * Based on Richard Purdie's ledtrig-timer.c and Atsushi Nemoto's
  7. * ledtrig-heartbeat.c
  8. * Design and use-case input from Jonas Bonn <jonas@southpole.se> and
  9. * Neil Brown <neilb@suse.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. */
  16. /*
  17. * Transient trigger allows one shot timer activation. Please refer to
  18. * Documentation/leds/ledtrig-transient.txt for details
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/device.h>
  24. #include <linux/slab.h>
  25. #include <linux/timer.h>
  26. #include <linux/leds.h>
  27. #include "../leds.h"
  28. struct transient_trig_data {
  29. int activate;
  30. int state;
  31. int restore_state;
  32. unsigned long duration;
  33. struct timer_list timer;
  34. };
  35. static void transient_timer_function(unsigned long data)
  36. {
  37. struct led_classdev *led_cdev = (struct led_classdev *) data;
  38. struct transient_trig_data *transient_data = led_cdev->trigger_data;
  39. transient_data->activate = 0;
  40. led_set_brightness_async(led_cdev, transient_data->restore_state);
  41. }
  42. static ssize_t transient_activate_show(struct device *dev,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  46. struct transient_trig_data *transient_data = led_cdev->trigger_data;
  47. return sprintf(buf, "%d\n", transient_data->activate);
  48. }
  49. static ssize_t transient_activate_store(struct device *dev,
  50. struct device_attribute *attr, const char *buf, size_t size)
  51. {
  52. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  53. struct transient_trig_data *transient_data = led_cdev->trigger_data;
  54. unsigned long state;
  55. ssize_t ret;
  56. ret = kstrtoul(buf, 10, &state);
  57. if (ret)
  58. return ret;
  59. if (state != 1 && state != 0)
  60. return -EINVAL;
  61. /* cancel the running timer */
  62. if (state == 0 && transient_data->activate == 1) {
  63. del_timer(&transient_data->timer);
  64. transient_data->activate = state;
  65. led_set_brightness_async(led_cdev,
  66. transient_data->restore_state);
  67. return size;
  68. }
  69. /* start timer if there is no active timer */
  70. if (state == 1 && transient_data->activate == 0 &&
  71. transient_data->duration != 0) {
  72. transient_data->activate = state;
  73. led_set_brightness_async(led_cdev, transient_data->state);
  74. transient_data->restore_state =
  75. (transient_data->state == LED_FULL) ? LED_OFF : LED_FULL;
  76. mod_timer(&transient_data->timer,
  77. jiffies + transient_data->duration);
  78. }
  79. /* state == 0 && transient_data->activate == 0
  80. timer is not active - just return */
  81. /* state == 1 && transient_data->activate == 1
  82. timer is already active - just return */
  83. return size;
  84. }
  85. static ssize_t transient_duration_show(struct device *dev,
  86. struct device_attribute *attr, char *buf)
  87. {
  88. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  89. struct transient_trig_data *transient_data = led_cdev->trigger_data;
  90. return sprintf(buf, "%lu\n", transient_data->duration);
  91. }
  92. static ssize_t transient_duration_store(struct device *dev,
  93. struct device_attribute *attr, const char *buf, size_t size)
  94. {
  95. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  96. struct transient_trig_data *transient_data = led_cdev->trigger_data;
  97. unsigned long state;
  98. ssize_t ret;
  99. ret = kstrtoul(buf, 10, &state);
  100. if (ret)
  101. return ret;
  102. transient_data->duration = state;
  103. return size;
  104. }
  105. static ssize_t transient_state_show(struct device *dev,
  106. struct device_attribute *attr, char *buf)
  107. {
  108. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  109. struct transient_trig_data *transient_data = led_cdev->trigger_data;
  110. int state;
  111. state = (transient_data->state == LED_FULL) ? 1 : 0;
  112. return sprintf(buf, "%d\n", state);
  113. }
  114. static ssize_t transient_state_store(struct device *dev,
  115. struct device_attribute *attr, const char *buf, size_t size)
  116. {
  117. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  118. struct transient_trig_data *transient_data = led_cdev->trigger_data;
  119. unsigned long state;
  120. ssize_t ret;
  121. ret = kstrtoul(buf, 10, &state);
  122. if (ret)
  123. return ret;
  124. if (state != 1 && state != 0)
  125. return -EINVAL;
  126. transient_data->state = (state == 1) ? LED_FULL : LED_OFF;
  127. return size;
  128. }
  129. static DEVICE_ATTR(activate, 0644, transient_activate_show,
  130. transient_activate_store);
  131. static DEVICE_ATTR(duration, 0644, transient_duration_show,
  132. transient_duration_store);
  133. static DEVICE_ATTR(state, 0644, transient_state_show, transient_state_store);
  134. static void transient_trig_activate(struct led_classdev *led_cdev)
  135. {
  136. int rc;
  137. struct transient_trig_data *tdata;
  138. tdata = kzalloc(sizeof(struct transient_trig_data), GFP_KERNEL);
  139. if (!tdata) {
  140. dev_err(led_cdev->dev,
  141. "unable to allocate transient trigger\n");
  142. return;
  143. }
  144. led_cdev->trigger_data = tdata;
  145. rc = device_create_file(led_cdev->dev, &dev_attr_activate);
  146. if (rc)
  147. goto err_out;
  148. rc = device_create_file(led_cdev->dev, &dev_attr_duration);
  149. if (rc)
  150. goto err_out_duration;
  151. rc = device_create_file(led_cdev->dev, &dev_attr_state);
  152. if (rc)
  153. goto err_out_state;
  154. setup_timer(&tdata->timer, transient_timer_function,
  155. (unsigned long) led_cdev);
  156. led_cdev->activated = true;
  157. return;
  158. err_out_state:
  159. device_remove_file(led_cdev->dev, &dev_attr_duration);
  160. err_out_duration:
  161. device_remove_file(led_cdev->dev, &dev_attr_activate);
  162. err_out:
  163. dev_err(led_cdev->dev, "unable to register transient trigger\n");
  164. led_cdev->trigger_data = NULL;
  165. kfree(tdata);
  166. }
  167. static void transient_trig_deactivate(struct led_classdev *led_cdev)
  168. {
  169. struct transient_trig_data *transient_data = led_cdev->trigger_data;
  170. if (led_cdev->activated) {
  171. del_timer_sync(&transient_data->timer);
  172. led_set_brightness_async(led_cdev,
  173. transient_data->restore_state);
  174. device_remove_file(led_cdev->dev, &dev_attr_activate);
  175. device_remove_file(led_cdev->dev, &dev_attr_duration);
  176. device_remove_file(led_cdev->dev, &dev_attr_state);
  177. led_cdev->trigger_data = NULL;
  178. led_cdev->activated = false;
  179. kfree(transient_data);
  180. }
  181. }
  182. static struct led_trigger transient_trigger = {
  183. .name = "transient",
  184. .activate = transient_trig_activate,
  185. .deactivate = transient_trig_deactivate,
  186. };
  187. static int __init transient_trig_init(void)
  188. {
  189. return led_trigger_register(&transient_trigger);
  190. }
  191. static void __exit transient_trig_exit(void)
  192. {
  193. led_trigger_unregister(&transient_trigger);
  194. }
  195. module_init(transient_trig_init);
  196. module_exit(transient_trig_exit);
  197. MODULE_AUTHOR("Shuah Khan <shuahkhan@gmail.com>");
  198. MODULE_DESCRIPTION("Transient LED trigger");
  199. MODULE_LICENSE("GPL");