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 | |
25 | static int panic_heartbeats; |
26 | |
27 | struct heartbeat_trig_data { |
28 | struct led_classdev *led_cdev; |
29 | unsigned int phase; |
30 | unsigned int period; |
31 | struct timer_list timer; |
32 | unsigned int invert; |
33 | }; |
34 | |
35 | static void led_heartbeat_function(struct timer_list *t) |
36 | { |
37 | struct heartbeat_trig_data *heartbeat_data = |
38 | from_timer(heartbeat_data, t, timer); |
39 | struct led_classdev *led_cdev; |
40 | unsigned long brightness = LED_OFF; |
41 | unsigned long delay = 0; |
42 | |
43 | led_cdev = heartbeat_data->led_cdev; |
44 | |
45 | if (unlikely(panic_heartbeats)) { |
46 | led_set_brightness_nosleep(led_cdev, LED_OFF); |
47 | return; |
48 | } |
49 | |
50 | if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags)) |
51 | led_cdev->blink_brightness = led_cdev->new_blink_brightness; |
52 | |
53 | /* acts like an actual heart beat -- ie thump-thump-pause... */ |
54 | switch (heartbeat_data->phase) { |
55 | case 0: |
56 | /* |
57 | * The hyperbolic function below modifies the |
58 | * heartbeat period length in dependency of the |
59 | * current (1min) load. It goes through the points |
60 | * f(0)=1260, f(1)=860, f(5)=510, f(inf)->300. |
61 | */ |
62 | heartbeat_data->period = 300 + |
63 | (6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT)); |
64 | heartbeat_data->period = |
65 | msecs_to_jiffies(heartbeat_data->period); |
66 | delay = msecs_to_jiffies(70); |
67 | heartbeat_data->phase++; |
68 | if (!heartbeat_data->invert) |
69 | brightness = led_cdev->blink_brightness; |
70 | break; |
71 | case 1: |
72 | delay = heartbeat_data->period / 4 - msecs_to_jiffies(70); |
73 | heartbeat_data->phase++; |
74 | if (heartbeat_data->invert) |
75 | brightness = led_cdev->blink_brightness; |
76 | break; |
77 | case 2: |
78 | delay = msecs_to_jiffies(70); |
79 | heartbeat_data->phase++; |
80 | if (!heartbeat_data->invert) |
81 | brightness = led_cdev->blink_brightness; |
82 | break; |
83 | default: |
84 | delay = heartbeat_data->period - heartbeat_data->period / 4 - |
85 | msecs_to_jiffies(70); |
86 | heartbeat_data->phase = 0; |
87 | if (heartbeat_data->invert) |
88 | brightness = led_cdev->blink_brightness; |
89 | break; |
90 | } |
91 | |
92 | led_set_brightness_nosleep(led_cdev, brightness); |
93 | mod_timer(&heartbeat_data->timer, jiffies + delay); |
94 | } |
95 | |
96 | static ssize_t led_invert_show(struct device *dev, |
97 | struct device_attribute *attr, char *buf) |
98 | { |
99 | struct heartbeat_trig_data *heartbeat_data = |
100 | led_trigger_get_drvdata(dev); |
101 | |
102 | return sprintf(buf, "%u\n" , heartbeat_data->invert); |
103 | } |
104 | |
105 | static ssize_t led_invert_store(struct device *dev, |
106 | struct device_attribute *attr, const char *buf, size_t size) |
107 | { |
108 | struct heartbeat_trig_data *heartbeat_data = |
109 | led_trigger_get_drvdata(dev); |
110 | unsigned long state; |
111 | int ret; |
112 | |
113 | ret = kstrtoul(buf, 0, &state); |
114 | if (ret) |
115 | return ret; |
116 | |
117 | heartbeat_data->invert = !!state; |
118 | |
119 | return size; |
120 | } |
121 | |
122 | static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store); |
123 | |
124 | static struct attribute *heartbeat_trig_attrs[] = { |
125 | &dev_attr_invert.attr, |
126 | NULL |
127 | }; |
128 | ATTRIBUTE_GROUPS(heartbeat_trig); |
129 | |
130 | static int heartbeat_trig_activate(struct led_classdev *led_cdev) |
131 | { |
132 | struct heartbeat_trig_data *heartbeat_data; |
133 | |
134 | heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL); |
135 | if (!heartbeat_data) |
136 | return -ENOMEM; |
137 | |
138 | led_set_trigger_data(led_cdev, heartbeat_data); |
139 | heartbeat_data->led_cdev = led_cdev; |
140 | |
141 | timer_setup(&heartbeat_data->timer, led_heartbeat_function, 0); |
142 | heartbeat_data->phase = 0; |
143 | if (!led_cdev->blink_brightness) |
144 | led_cdev->blink_brightness = led_cdev->max_brightness; |
145 | led_heartbeat_function(&heartbeat_data->timer); |
146 | set_bit(LED_BLINK_SW, &led_cdev->work_flags); |
147 | |
148 | return 0; |
149 | } |
150 | |
151 | static void heartbeat_trig_deactivate(struct led_classdev *led_cdev) |
152 | { |
153 | struct heartbeat_trig_data *heartbeat_data = |
154 | led_get_trigger_data(led_cdev); |
155 | |
156 | del_timer_sync(&heartbeat_data->timer); |
157 | kfree(heartbeat_data); |
158 | clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
159 | } |
160 | |
161 | static struct led_trigger heartbeat_led_trigger = { |
162 | .name = "heartbeat" , |
163 | .activate = heartbeat_trig_activate, |
164 | .deactivate = heartbeat_trig_deactivate, |
165 | .groups = heartbeat_trig_groups, |
166 | }; |
167 | |
168 | static int heartbeat_reboot_notifier(struct notifier_block *nb, |
169 | unsigned long code, void *unused) |
170 | { |
171 | led_trigger_unregister(&heartbeat_led_trigger); |
172 | return NOTIFY_DONE; |
173 | } |
174 | |
175 | static int heartbeat_panic_notifier(struct notifier_block *nb, |
176 | unsigned long code, void *unused) |
177 | { |
178 | panic_heartbeats = 1; |
179 | return NOTIFY_DONE; |
180 | } |
181 | |
182 | static struct notifier_block heartbeat_reboot_nb = { |
183 | .notifier_call = heartbeat_reboot_notifier, |
184 | }; |
185 | |
186 | static struct notifier_block heartbeat_panic_nb = { |
187 | .notifier_call = heartbeat_panic_notifier, |
188 | }; |
189 | |
190 | static int __init heartbeat_trig_init(void) |
191 | { |
192 | int rc = led_trigger_register(&heartbeat_led_trigger); |
193 | |
194 | if (!rc) { |
195 | atomic_notifier_chain_register(&panic_notifier_list, |
196 | &heartbeat_panic_nb); |
197 | register_reboot_notifier(&heartbeat_reboot_nb); |
198 | } |
199 | return rc; |
200 | } |
201 | |
202 | static void __exit heartbeat_trig_exit(void) |
203 | { |
204 | unregister_reboot_notifier(&heartbeat_reboot_nb); |
205 | atomic_notifier_chain_unregister(&panic_notifier_list, |
206 | &heartbeat_panic_nb); |
207 | led_trigger_unregister(&heartbeat_led_trigger); |
208 | } |
209 | |
210 | module_init(heartbeat_trig_init); |
211 | module_exit(heartbeat_trig_exit); |
212 | |
213 | MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>" ); |
214 | MODULE_DESCRIPTION("Heartbeat LED trigger" ); |
215 | MODULE_LICENSE("GPL v2" ); |
216 | |