ledtrig-heartbeat.c 3.9 KB

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