Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 18 | #include <linux/timer.h> |
| 19 | #include <linux/sched.h> |
Ingo Molnar | badaff8 | 2017-02-08 08:45:17 +0100 | [diff] [blame] | 20 | #include <linux/sched/loadavg.h> |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 21 | #include <linux/leds.h> |
Alexander Holler | 49dca5a | 2012-05-29 15:07:29 -0700 | [diff] [blame] | 22 | #include <linux/reboot.h> |
Kim, Milo | f07fb52 | 2013-02-20 00:36:01 -0800 | [diff] [blame] | 23 | #include "../leds.h" |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 24 | |
Alexander Holler | fb31fbe | 2012-07-03 14:35:47 +0800 | [diff] [blame] | 25 | static int panic_heartbeats; |
| 26 | |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 27 | struct heartbeat_trig_data { |
| 28 | unsigned int phase; |
| 29 | unsigned int period; |
| 30 | struct timer_list timer; |
Jiri Prchal | 1c7b9d0 | 2015-10-08 10:32:26 +0200 | [diff] [blame] | 31 | unsigned int invert; |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static void led_heartbeat_function(unsigned long data) |
| 35 | { |
| 36 | struct led_classdev *led_cdev = (struct led_classdev *) data; |
| 37 | struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data; |
| 38 | unsigned long brightness = LED_OFF; |
| 39 | unsigned long delay = 0; |
| 40 | |
Alexander Holler | fb31fbe | 2012-07-03 14:35:47 +0800 | [diff] [blame] | 41 | if (unlikely(panic_heartbeats)) { |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 42 | led_set_brightness_nosleep(led_cdev, LED_OFF); |
Alexander Holler | fb31fbe | 2012-07-03 14:35:47 +0800 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 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++; |
Jiri Prchal | 1c7b9d0 | 2015-10-08 10:32:26 +0200 | [diff] [blame] | 61 | if (!heartbeat_data->invert) |
| 62 | brightness = led_cdev->max_brightness; |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 63 | break; |
| 64 | case 1: |
| 65 | delay = heartbeat_data->period / 4 - msecs_to_jiffies(70); |
| 66 | heartbeat_data->phase++; |
Jiri Prchal | 1c7b9d0 | 2015-10-08 10:32:26 +0200 | [diff] [blame] | 67 | if (heartbeat_data->invert) |
| 68 | brightness = led_cdev->max_brightness; |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 69 | break; |
| 70 | case 2: |
| 71 | delay = msecs_to_jiffies(70); |
| 72 | heartbeat_data->phase++; |
Jiri Prchal | 1c7b9d0 | 2015-10-08 10:32:26 +0200 | [diff] [blame] | 73 | if (!heartbeat_data->invert) |
| 74 | brightness = led_cdev->max_brightness; |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 75 | break; |
| 76 | default: |
| 77 | delay = heartbeat_data->period - heartbeat_data->period / 4 - |
| 78 | msecs_to_jiffies(70); |
| 79 | heartbeat_data->phase = 0; |
Jiri Prchal | 1c7b9d0 | 2015-10-08 10:32:26 +0200 | [diff] [blame] | 80 | if (heartbeat_data->invert) |
| 81 | brightness = led_cdev->max_brightness; |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 82 | break; |
| 83 | } |
| 84 | |
Jacek Anaszewski | 81fe8e5 | 2015-10-07 11:10:41 +0200 | [diff] [blame] | 85 | led_set_brightness_nosleep(led_cdev, brightness); |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 86 | mod_timer(&heartbeat_data->timer, jiffies + delay); |
| 87 | } |
| 88 | |
Jiri Prchal | 1c7b9d0 | 2015-10-08 10:32:26 +0200 | [diff] [blame] | 89 | static ssize_t led_invert_show(struct device *dev, |
| 90 | struct device_attribute *attr, char *buf) |
| 91 | { |
| 92 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 93 | struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data; |
| 94 | |
| 95 | return sprintf(buf, "%u\n", heartbeat_data->invert); |
| 96 | } |
| 97 | |
| 98 | static ssize_t led_invert_store(struct device *dev, |
| 99 | struct device_attribute *attr, const char *buf, size_t size) |
| 100 | { |
| 101 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 102 | struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data; |
| 103 | unsigned long state; |
| 104 | int ret; |
| 105 | |
| 106 | ret = kstrtoul(buf, 0, &state); |
| 107 | if (ret) |
| 108 | return ret; |
| 109 | |
| 110 | heartbeat_data->invert = !!state; |
| 111 | |
| 112 | return size; |
| 113 | } |
| 114 | |
| 115 | static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store); |
| 116 | |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 117 | static void heartbeat_trig_activate(struct led_classdev *led_cdev) |
| 118 | { |
| 119 | struct heartbeat_trig_data *heartbeat_data; |
Jiri Prchal | 1c7b9d0 | 2015-10-08 10:32:26 +0200 | [diff] [blame] | 120 | int rc; |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 121 | |
| 122 | heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL); |
| 123 | if (!heartbeat_data) |
| 124 | return; |
| 125 | |
| 126 | led_cdev->trigger_data = heartbeat_data; |
Jiri Prchal | 1c7b9d0 | 2015-10-08 10:32:26 +0200 | [diff] [blame] | 127 | rc = device_create_file(led_cdev->dev, &dev_attr_invert); |
| 128 | if (rc) { |
| 129 | kfree(led_cdev->trigger_data); |
| 130 | return; |
| 131 | } |
| 132 | |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 133 | setup_timer(&heartbeat_data->timer, |
| 134 | led_heartbeat_function, (unsigned long) led_cdev); |
| 135 | heartbeat_data->phase = 0; |
| 136 | led_heartbeat_function(heartbeat_data->timer.data); |
Shuah Khan | 03c091e | 2012-05-29 15:07:28 -0700 | [diff] [blame] | 137 | led_cdev->activated = true; |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void heartbeat_trig_deactivate(struct led_classdev *led_cdev) |
| 141 | { |
| 142 | struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data; |
| 143 | |
Shuah Khan | 03c091e | 2012-05-29 15:07:28 -0700 | [diff] [blame] | 144 | if (led_cdev->activated) { |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 145 | del_timer_sync(&heartbeat_data->timer); |
Jiri Prchal | 1c7b9d0 | 2015-10-08 10:32:26 +0200 | [diff] [blame] | 146 | device_remove_file(led_cdev->dev, &dev_attr_invert); |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 147 | kfree(heartbeat_data); |
Shuah Khan | 03c091e | 2012-05-29 15:07:28 -0700 | [diff] [blame] | 148 | led_cdev->activated = false; |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | static struct led_trigger heartbeat_led_trigger = { |
| 153 | .name = "heartbeat", |
| 154 | .activate = heartbeat_trig_activate, |
| 155 | .deactivate = heartbeat_trig_deactivate, |
| 156 | }; |
| 157 | |
Alexander Holler | 49dca5a | 2012-05-29 15:07:29 -0700 | [diff] [blame] | 158 | static int heartbeat_reboot_notifier(struct notifier_block *nb, |
| 159 | unsigned long code, void *unused) |
| 160 | { |
| 161 | led_trigger_unregister(&heartbeat_led_trigger); |
| 162 | return NOTIFY_DONE; |
| 163 | } |
| 164 | |
Alexander Holler | fb31fbe | 2012-07-03 14:35:47 +0800 | [diff] [blame] | 165 | static int heartbeat_panic_notifier(struct notifier_block *nb, |
| 166 | unsigned long code, void *unused) |
| 167 | { |
| 168 | panic_heartbeats = 1; |
| 169 | return NOTIFY_DONE; |
| 170 | } |
| 171 | |
Alexander Holler | 49dca5a | 2012-05-29 15:07:29 -0700 | [diff] [blame] | 172 | static struct notifier_block heartbeat_reboot_nb = { |
| 173 | .notifier_call = heartbeat_reboot_notifier, |
| 174 | }; |
| 175 | |
| 176 | static struct notifier_block heartbeat_panic_nb = { |
Alexander Holler | fb31fbe | 2012-07-03 14:35:47 +0800 | [diff] [blame] | 177 | .notifier_call = heartbeat_panic_notifier, |
Alexander Holler | 49dca5a | 2012-05-29 15:07:29 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 180 | static int __init heartbeat_trig_init(void) |
| 181 | { |
Alexander Holler | 49dca5a | 2012-05-29 15:07:29 -0700 | [diff] [blame] | 182 | int rc = led_trigger_register(&heartbeat_led_trigger); |
| 183 | |
| 184 | if (!rc) { |
| 185 | atomic_notifier_chain_register(&panic_notifier_list, |
| 186 | &heartbeat_panic_nb); |
| 187 | register_reboot_notifier(&heartbeat_reboot_nb); |
| 188 | } |
| 189 | return rc; |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void __exit heartbeat_trig_exit(void) |
| 193 | { |
Alexander Holler | 49dca5a | 2012-05-29 15:07:29 -0700 | [diff] [blame] | 194 | unregister_reboot_notifier(&heartbeat_reboot_nb); |
| 195 | atomic_notifier_chain_unregister(&panic_notifier_list, |
| 196 | &heartbeat_panic_nb); |
Atsushi Nemoto | 891c668 | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 197 | led_trigger_unregister(&heartbeat_led_trigger); |
| 198 | } |
| 199 | |
| 200 | module_init(heartbeat_trig_init); |
| 201 | module_exit(heartbeat_trig_exit); |
| 202 | |
| 203 | MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>"); |
| 204 | MODULE_DESCRIPTION("Heartbeat LED trigger"); |
| 205 | MODULE_LICENSE("GPL"); |