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