ledtrig-heartbeat.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/leds.h>
  21. #include <linux/reboot.h>
  22. #include "../leds.h"
  23. static int panic_heartbeats;
  24. struct heartbeat_trig_data {
  25. unsigned int phase;
  26. unsigned int period;
  27. struct timer_list timer;
  28. unsigned int invert;
  29. };
  30. static void led_heartbeat_function(unsigned long data)
  31. {
  32. struct led_classdev *led_cdev = (struct led_classdev *) data;
  33. struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
  34. unsigned long brightness = LED_OFF;
  35. unsigned long delay = 0;
  36. if (unlikely(panic_heartbeats)) {
  37. led_set_brightness(led_cdev, LED_OFF);
  38. return;
  39. }
  40. /* acts like an actual heart beat -- ie thump-thump-pause... */
  41. switch (heartbeat_data->phase) {
  42. case 0:
  43. /*
  44. * The hyperbolic function below modifies the
  45. * heartbeat period length in dependency of the
  46. * current (1min) load. It goes through the points
  47. * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
  48. */
  49. heartbeat_data->period = 300 +
  50. (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
  51. heartbeat_data->period =
  52. msecs_to_jiffies(heartbeat_data->period);
  53. delay = msecs_to_jiffies(70);
  54. heartbeat_data->phase++;
  55. if (!heartbeat_data->invert)
  56. brightness = led_cdev->max_brightness;
  57. break;
  58. case 1:
  59. delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
  60. heartbeat_data->phase++;
  61. if (heartbeat_data->invert)
  62. brightness = led_cdev->max_brightness;
  63. break;
  64. case 2:
  65. delay = msecs_to_jiffies(70);
  66. heartbeat_data->phase++;
  67. if (!heartbeat_data->invert)
  68. brightness = led_cdev->max_brightness;
  69. break;
  70. default:
  71. delay = heartbeat_data->period - heartbeat_data->period / 4 -
  72. msecs_to_jiffies(70);
  73. heartbeat_data->phase = 0;
  74. if (heartbeat_data->invert)
  75. brightness = led_cdev->max_brightness;
  76. break;
  77. }
  78. led_set_brightness_async(led_cdev, brightness);
  79. mod_timer(&heartbeat_data->timer, jiffies + delay);
  80. }
  81. static ssize_t led_invert_show(struct device *dev,
  82. struct device_attribute *attr, char *buf)
  83. {
  84. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  85. struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
  86. return sprintf(buf, "%u\n", heartbeat_data->invert);
  87. }
  88. static ssize_t led_invert_store(struct device *dev,
  89. struct device_attribute *attr, const char *buf, size_t size)
  90. {
  91. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  92. struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
  93. unsigned long state;
  94. int ret;
  95. ret = kstrtoul(buf, 0, &state);
  96. if (ret)
  97. return ret;
  98. heartbeat_data->invert = !!state;
  99. return size;
  100. }
  101. static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
  102. static void heartbeat_trig_activate(struct led_classdev *led_cdev)
  103. {
  104. struct heartbeat_trig_data *heartbeat_data;
  105. int rc;
  106. heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
  107. if (!heartbeat_data)
  108. return;
  109. led_cdev->trigger_data = heartbeat_data;
  110. rc = device_create_file(led_cdev->dev, &dev_attr_invert);
  111. if (rc) {
  112. kfree(led_cdev->trigger_data);
  113. return;
  114. }
  115. setup_timer(&heartbeat_data->timer,
  116. led_heartbeat_function, (unsigned long) led_cdev);
  117. heartbeat_data->phase = 0;
  118. led_heartbeat_function(heartbeat_data->timer.data);
  119. led_cdev->activated = true;
  120. }
  121. static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
  122. {
  123. struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
  124. if (led_cdev->activated) {
  125. del_timer_sync(&heartbeat_data->timer);
  126. device_remove_file(led_cdev->dev, &dev_attr_invert);
  127. kfree(heartbeat_data);
  128. led_cdev->activated = false;
  129. }
  130. }
  131. static struct led_trigger heartbeat_led_trigger = {
  132. .name = "heartbeat",
  133. .activate = heartbeat_trig_activate,
  134. .deactivate = heartbeat_trig_deactivate,
  135. };
  136. static int heartbeat_reboot_notifier(struct notifier_block *nb,
  137. unsigned long code, void *unused)
  138. {
  139. led_trigger_unregister(&heartbeat_led_trigger);
  140. return NOTIFY_DONE;
  141. }
  142. static int heartbeat_panic_notifier(struct notifier_block *nb,
  143. unsigned long code, void *unused)
  144. {
  145. panic_heartbeats = 1;
  146. return NOTIFY_DONE;
  147. }
  148. static struct notifier_block heartbeat_reboot_nb = {
  149. .notifier_call = heartbeat_reboot_notifier,
  150. };
  151. static struct notifier_block heartbeat_panic_nb = {
  152. .notifier_call = heartbeat_panic_notifier,
  153. };
  154. static int __init heartbeat_trig_init(void)
  155. {
  156. int rc = led_trigger_register(&heartbeat_led_trigger);
  157. if (!rc) {
  158. atomic_notifier_chain_register(&panic_notifier_list,
  159. &heartbeat_panic_nb);
  160. register_reboot_notifier(&heartbeat_reboot_nb);
  161. }
  162. return rc;
  163. }
  164. static void __exit heartbeat_trig_exit(void)
  165. {
  166. unregister_reboot_notifier(&heartbeat_reboot_nb);
  167. atomic_notifier_chain_unregister(&panic_notifier_list,
  168. &heartbeat_panic_nb);
  169. led_trigger_unregister(&heartbeat_led_trigger);
  170. }
  171. module_init(heartbeat_trig_init);
  172. module_exit(heartbeat_trig_exit);
  173. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  174. MODULE_DESCRIPTION("Heartbeat LED trigger");
  175. MODULE_LICENSE("GPL");