ledtrig-heartbeat.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * LED Heartbeat Trigger
  3. *
  4. * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  5. *
  6. * Based on Richard Purdie's ledtrig-timer.c and some arch's
  7. * CONFIG_HEARTBEAT code.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/timer.h>
  19. #include <linux/sched.h>
  20. #include <linux/sched/loadavg.h>
  21. #include <linux/leds.h>
  22. #include <linux/reboot.h>
  23. #include "../leds.h"
  24. static int panic_heartbeats;
  25. struct heartbeat_trig_data {
  26. struct led_classdev *led_cdev;
  27. unsigned int phase;
  28. unsigned int period;
  29. struct timer_list timer;
  30. unsigned int invert;
  31. };
  32. static void led_heartbeat_function(struct timer_list *t)
  33. {
  34. struct heartbeat_trig_data *heartbeat_data =
  35. from_timer(heartbeat_data, t, timer);
  36. struct led_classdev *led_cdev;
  37. unsigned long brightness = LED_OFF;
  38. unsigned long delay = 0;
  39. led_cdev = heartbeat_data->led_cdev;
  40. if (unlikely(panic_heartbeats)) {
  41. led_set_brightness_nosleep(led_cdev, LED_OFF);
  42. return;
  43. }
  44. if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags))
  45. led_cdev->blink_brightness = led_cdev->new_blink_brightness;
  46. /* acts like an actual heart beat -- ie thump-thump-pause... */
  47. switch (heartbeat_data->phase) {
  48. case 0:
  49. /*
  50. * The hyperbolic function below modifies the
  51. * heartbeat period length in dependency of the
  52. * current (1min) load. It goes through the points
  53. * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
  54. */
  55. heartbeat_data->period = 300 +
  56. (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
  57. heartbeat_data->period =
  58. msecs_to_jiffies(heartbeat_data->period);
  59. delay = msecs_to_jiffies(70);
  60. heartbeat_data->phase++;
  61. if (!heartbeat_data->invert)
  62. brightness = led_cdev->blink_brightness;
  63. break;
  64. case 1:
  65. delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
  66. heartbeat_data->phase++;
  67. if (heartbeat_data->invert)
  68. brightness = led_cdev->blink_brightness;
  69. break;
  70. case 2:
  71. delay = msecs_to_jiffies(70);
  72. heartbeat_data->phase++;
  73. if (!heartbeat_data->invert)
  74. brightness = led_cdev->blink_brightness;
  75. break;
  76. default:
  77. delay = heartbeat_data->period - heartbeat_data->period / 4 -
  78. msecs_to_jiffies(70);
  79. heartbeat_data->phase = 0;
  80. if (heartbeat_data->invert)
  81. brightness = led_cdev->blink_brightness;
  82. break;
  83. }
  84. led_set_brightness_nosleep(led_cdev, brightness);
  85. mod_timer(&heartbeat_data->timer, jiffies + delay);
  86. }
  87. static ssize_t led_invert_show(struct device *dev,
  88. struct device_attribute *attr, char *buf)
  89. {
  90. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  91. struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
  92. return sprintf(buf, "%u\n", heartbeat_data->invert);
  93. }
  94. static ssize_t led_invert_store(struct device *dev,
  95. struct device_attribute *attr, const char *buf, size_t size)
  96. {
  97. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  98. struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
  99. unsigned long state;
  100. int ret;
  101. ret = kstrtoul(buf, 0, &state);
  102. if (ret)
  103. return ret;
  104. heartbeat_data->invert = !!state;
  105. return size;
  106. }
  107. static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
  108. static void heartbeat_trig_activate(struct led_classdev *led_cdev)
  109. {
  110. struct heartbeat_trig_data *heartbeat_data;
  111. int rc;
  112. heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
  113. if (!heartbeat_data)
  114. return;
  115. led_cdev->trigger_data = heartbeat_data;
  116. heartbeat_data->led_cdev = led_cdev;
  117. rc = device_create_file(led_cdev->dev, &dev_attr_invert);
  118. if (rc) {
  119. kfree(led_cdev->trigger_data);
  120. return;
  121. }
  122. timer_setup(&heartbeat_data->timer, led_heartbeat_function, 0);
  123. heartbeat_data->phase = 0;
  124. if (!led_cdev->blink_brightness)
  125. led_cdev->blink_brightness = led_cdev->max_brightness;
  126. led_heartbeat_function(&heartbeat_data->timer);
  127. set_bit(LED_BLINK_SW, &led_cdev->work_flags);
  128. led_cdev->activated = true;
  129. }
  130. static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
  131. {
  132. struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
  133. if (led_cdev->activated) {
  134. del_timer_sync(&heartbeat_data->timer);
  135. device_remove_file(led_cdev->dev, &dev_attr_invert);
  136. kfree(heartbeat_data);
  137. clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
  138. led_cdev->activated = false;
  139. }
  140. }
  141. static struct led_trigger heartbeat_led_trigger = {
  142. .name = "heartbeat",
  143. .activate = heartbeat_trig_activate,
  144. .deactivate = heartbeat_trig_deactivate,
  145. };
  146. static int heartbeat_reboot_notifier(struct notifier_block *nb,
  147. unsigned long code, void *unused)
  148. {
  149. led_trigger_unregister(&heartbeat_led_trigger);
  150. return NOTIFY_DONE;
  151. }
  152. static int heartbeat_panic_notifier(struct notifier_block *nb,
  153. unsigned long code, void *unused)
  154. {
  155. panic_heartbeats = 1;
  156. return NOTIFY_DONE;
  157. }
  158. static struct notifier_block heartbeat_reboot_nb = {
  159. .notifier_call = heartbeat_reboot_notifier,
  160. };
  161. static struct notifier_block heartbeat_panic_nb = {
  162. .notifier_call = heartbeat_panic_notifier,
  163. };
  164. static int __init heartbeat_trig_init(void)
  165. {
  166. int rc = led_trigger_register(&heartbeat_led_trigger);
  167. if (!rc) {
  168. atomic_notifier_chain_register(&panic_notifier_list,
  169. &heartbeat_panic_nb);
  170. register_reboot_notifier(&heartbeat_reboot_nb);
  171. }
  172. return rc;
  173. }
  174. static void __exit heartbeat_trig_exit(void)
  175. {
  176. unregister_reboot_notifier(&heartbeat_reboot_nb);
  177. atomic_notifier_chain_unregister(&panic_notifier_list,
  178. &heartbeat_panic_nb);
  179. led_trigger_unregister(&heartbeat_led_trigger);
  180. }
  181. module_init(heartbeat_trig_init);
  182. module_exit(heartbeat_trig_exit);
  183. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  184. MODULE_DESCRIPTION("Heartbeat LED trigger");
  185. MODULE_LICENSE("GPL");