ledtrig-heartbeat.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. };
  29. static void led_heartbeat_function(unsigned long data)
  30. {
  31. struct led_classdev *led_cdev = (struct led_classdev *) data;
  32. struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
  33. unsigned long brightness = LED_OFF;
  34. unsigned long delay = 0;
  35. if (unlikely(panic_heartbeats)) {
  36. led_set_brightness(led_cdev, LED_OFF);
  37. return;
  38. }
  39. /* acts like an actual heart beat -- ie thump-thump-pause... */
  40. switch (heartbeat_data->phase) {
  41. case 0:
  42. /*
  43. * The hyperbolic function below modifies the
  44. * heartbeat period length in dependency of the
  45. * current (1min) load. It goes through the points
  46. * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
  47. */
  48. heartbeat_data->period = 300 +
  49. (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
  50. heartbeat_data->period =
  51. msecs_to_jiffies(heartbeat_data->period);
  52. delay = msecs_to_jiffies(70);
  53. heartbeat_data->phase++;
  54. brightness = led_cdev->max_brightness;
  55. break;
  56. case 1:
  57. delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
  58. heartbeat_data->phase++;
  59. break;
  60. case 2:
  61. delay = msecs_to_jiffies(70);
  62. heartbeat_data->phase++;
  63. brightness = led_cdev->max_brightness;
  64. break;
  65. default:
  66. delay = heartbeat_data->period - heartbeat_data->period / 4 -
  67. msecs_to_jiffies(70);
  68. heartbeat_data->phase = 0;
  69. break;
  70. }
  71. __led_set_brightness(led_cdev, brightness);
  72. mod_timer(&heartbeat_data->timer, jiffies + delay);
  73. }
  74. static void heartbeat_trig_activate(struct led_classdev *led_cdev)
  75. {
  76. struct heartbeat_trig_data *heartbeat_data;
  77. heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
  78. if (!heartbeat_data)
  79. return;
  80. led_cdev->trigger_data = heartbeat_data;
  81. setup_timer(&heartbeat_data->timer,
  82. led_heartbeat_function, (unsigned long) led_cdev);
  83. heartbeat_data->phase = 0;
  84. led_heartbeat_function(heartbeat_data->timer.data);
  85. led_cdev->activated = true;
  86. }
  87. static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
  88. {
  89. struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
  90. if (led_cdev->activated) {
  91. del_timer_sync(&heartbeat_data->timer);
  92. kfree(heartbeat_data);
  93. led_cdev->activated = false;
  94. }
  95. }
  96. static struct led_trigger heartbeat_led_trigger = {
  97. .name = "heartbeat",
  98. .activate = heartbeat_trig_activate,
  99. .deactivate = heartbeat_trig_deactivate,
  100. };
  101. static int heartbeat_reboot_notifier(struct notifier_block *nb,
  102. unsigned long code, void *unused)
  103. {
  104. led_trigger_unregister(&heartbeat_led_trigger);
  105. return NOTIFY_DONE;
  106. }
  107. static int heartbeat_panic_notifier(struct notifier_block *nb,
  108. unsigned long code, void *unused)
  109. {
  110. panic_heartbeats = 1;
  111. return NOTIFY_DONE;
  112. }
  113. static struct notifier_block heartbeat_reboot_nb = {
  114. .notifier_call = heartbeat_reboot_notifier,
  115. };
  116. static struct notifier_block heartbeat_panic_nb = {
  117. .notifier_call = heartbeat_panic_notifier,
  118. };
  119. static int __init heartbeat_trig_init(void)
  120. {
  121. int rc = led_trigger_register(&heartbeat_led_trigger);
  122. if (!rc) {
  123. atomic_notifier_chain_register(&panic_notifier_list,
  124. &heartbeat_panic_nb);
  125. register_reboot_notifier(&heartbeat_reboot_nb);
  126. }
  127. return rc;
  128. }
  129. static void __exit heartbeat_trig_exit(void)
  130. {
  131. unregister_reboot_notifier(&heartbeat_reboot_nb);
  132. atomic_notifier_chain_unregister(&panic_notifier_list,
  133. &heartbeat_panic_nb);
  134. led_trigger_unregister(&heartbeat_led_trigger);
  135. }
  136. module_init(heartbeat_trig_init);
  137. module_exit(heartbeat_trig_exit);
  138. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  139. MODULE_DESCRIPTION("Heartbeat LED trigger");
  140. MODULE_LICENSE("GPL");