hwrng: core - Don't use a stack buffer in add_early_randomness()
[linux/fpc-iii.git] / drivers / leds / trigger / ledtrig-heartbeat.c
blobc9f386213e9ef16ae006e2270e2615e4bb60253e
1 /*
2 * LED Heartbeat Trigger
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 * Based on Richard Purdie's ledtrig-timer.c and some arch's
7 * CONFIG_HEARTBEAT code.
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.
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 <linux/suspend.h>
23 #include "../leds.h"
25 static int panic_heartbeats;
27 struct heartbeat_trig_data {
28 unsigned int phase;
29 unsigned int period;
30 struct timer_list timer;
31 unsigned int invert;
34 static void led_heartbeat_function(unsigned long data)
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;
41 if (unlikely(panic_heartbeats)) {
42 led_set_brightness_nosleep(led_cdev, LED_OFF);
43 return;
46 /* acts like an actual heart beat -- ie thump-thump-pause... */
47 switch (heartbeat_data->phase) {
48 case 0:
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.
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++;
61 if (!heartbeat_data->invert)
62 brightness = led_cdev->max_brightness;
63 break;
64 case 1:
65 delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
66 heartbeat_data->phase++;
67 if (heartbeat_data->invert)
68 brightness = led_cdev->max_brightness;
69 break;
70 case 2:
71 delay = msecs_to_jiffies(70);
72 heartbeat_data->phase++;
73 if (!heartbeat_data->invert)
74 brightness = led_cdev->max_brightness;
75 break;
76 default:
77 delay = heartbeat_data->period - heartbeat_data->period / 4 -
78 msecs_to_jiffies(70);
79 heartbeat_data->phase = 0;
80 if (heartbeat_data->invert)
81 brightness = led_cdev->max_brightness;
82 break;
85 led_set_brightness_nosleep(led_cdev, brightness);
86 mod_timer(&heartbeat_data->timer, jiffies + delay);
89 static ssize_t led_invert_show(struct device *dev,
90 struct device_attribute *attr, char *buf)
92 struct led_classdev *led_cdev = dev_get_drvdata(dev);
93 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
95 return sprintf(buf, "%u\n", heartbeat_data->invert);
98 static ssize_t led_invert_store(struct device *dev,
99 struct device_attribute *attr, const char *buf, size_t size)
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;
106 ret = kstrtoul(buf, 0, &state);
107 if (ret)
108 return ret;
110 heartbeat_data->invert = !!state;
112 return size;
115 static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
117 static void heartbeat_trig_activate(struct led_classdev *led_cdev)
119 struct heartbeat_trig_data *heartbeat_data;
120 int rc;
122 heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
123 if (!heartbeat_data)
124 return;
126 led_cdev->trigger_data = heartbeat_data;
127 rc = device_create_file(led_cdev->dev, &dev_attr_invert);
128 if (rc) {
129 kfree(led_cdev->trigger_data);
130 return;
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);
137 led_cdev->activated = true;
140 static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
142 struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
144 if (led_cdev->activated) {
145 del_timer_sync(&heartbeat_data->timer);
146 device_remove_file(led_cdev->dev, &dev_attr_invert);
147 kfree(heartbeat_data);
148 led_cdev->activated = false;
152 static struct led_trigger heartbeat_led_trigger = {
153 .name = "heartbeat",
154 .activate = heartbeat_trig_activate,
155 .deactivate = heartbeat_trig_deactivate,
158 static int heartbeat_pm_notifier(struct notifier_block *nb,
159 unsigned long pm_event, void *unused)
161 int rc;
163 switch (pm_event) {
164 case PM_SUSPEND_PREPARE:
165 case PM_HIBERNATION_PREPARE:
166 case PM_RESTORE_PREPARE:
167 led_trigger_unregister(&heartbeat_led_trigger);
168 break;
169 case PM_POST_SUSPEND:
170 case PM_POST_HIBERNATION:
171 case PM_POST_RESTORE:
172 rc = led_trigger_register(&heartbeat_led_trigger);
173 if (rc)
174 pr_err("could not re-register heartbeat trigger\n");
175 break;
176 default:
177 break;
179 return NOTIFY_DONE;
182 static int heartbeat_reboot_notifier(struct notifier_block *nb,
183 unsigned long code, void *unused)
185 led_trigger_unregister(&heartbeat_led_trigger);
186 return NOTIFY_DONE;
189 static int heartbeat_panic_notifier(struct notifier_block *nb,
190 unsigned long code, void *unused)
192 panic_heartbeats = 1;
193 return NOTIFY_DONE;
196 static struct notifier_block heartbeat_pm_nb = {
197 .notifier_call = heartbeat_pm_notifier,
200 static struct notifier_block heartbeat_reboot_nb = {
201 .notifier_call = heartbeat_reboot_notifier,
204 static struct notifier_block heartbeat_panic_nb = {
205 .notifier_call = heartbeat_panic_notifier,
208 static int __init heartbeat_trig_init(void)
210 int rc = led_trigger_register(&heartbeat_led_trigger);
212 if (!rc) {
213 atomic_notifier_chain_register(&panic_notifier_list,
214 &heartbeat_panic_nb);
215 register_reboot_notifier(&heartbeat_reboot_nb);
216 register_pm_notifier(&heartbeat_pm_nb);
218 return rc;
221 static void __exit heartbeat_trig_exit(void)
223 unregister_pm_notifier(&heartbeat_pm_nb);
224 unregister_reboot_notifier(&heartbeat_reboot_nb);
225 atomic_notifier_chain_unregister(&panic_notifier_list,
226 &heartbeat_panic_nb);
227 led_trigger_unregister(&heartbeat_led_trigger);
230 module_init(heartbeat_trig_init);
231 module_exit(heartbeat_trig_exit);
233 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
234 MODULE_DESCRIPTION("Heartbeat LED trigger");
235 MODULE_LICENSE("GPL");