Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
menuconfig LEDS_TRIGGERS
bool "LED Trigger support"
depends on LEDS_CLASS
help
This option enables trigger support for the leds class.
These triggers allow kernel events to drive the LEDs and can
be configured via sysfs. If unsure, say Y.
if LEDS_TRIGGERS
config LEDS_TRIGGER_TIMER
tristate "LED Timer Trigger"
help
This allows LEDs to be controlled by a programmable timer
via sysfs. Some LED hardware can be programmed to start
blinking the LED without any further software interaction.
For more details read Documentation/leds/leds-class.txt.
If unsure, say Y.
config LEDS_TRIGGER_ONESHOT
tristate "LED One-shot Trigger"
help
This allows LEDs to blink in one-shot pulses with parameters
controlled via sysfs. It's useful to notify the user on
sporadic events, when there are no clear begin and end trap points,
or on dense events, where this blinks the LED at constant rate if
rearmed continuously.
It also shows how to use the led_blink_set_oneshot() function.
If unsure, say Y.
config LEDS_TRIGGER_DISK
bool "LED Disk Trigger"
depends on IDE_GD_ATA || ATA
help
This allows LEDs to be controlled by disk activity.
If unsure, say Y.
config LEDS_TRIGGER_MTD
bool "LED MTD (NAND/NOR) Trigger"
depends on MTD
help
This allows LEDs to be controlled by MTD activity.
If unsure, say N.
config LEDS_TRIGGER_HEARTBEAT
tristate "LED Heartbeat Trigger"
help
This allows LEDs to be controlled by a CPU load average.
The flash frequency is a hyperbolic function of the 1-minute
load average.
If unsure, say Y.
config LEDS_TRIGGER_BACKLIGHT
tristate "LED backlight Trigger"
help
This allows LEDs to be controlled as a backlight device: they
turn off and on when the display is blanked and unblanked.
If unsure, say N.
config LEDS_TRIGGER_CPU
bool "LED CPU Trigger"
help
This allows LEDs to be controlled by active CPUs. This shows
the active CPUs across an array of LEDs so you can see which
CPUs are active on the system at any given moment.
If unsure, say N.
config LEDS_TRIGGER_ACTIVITY
tristate "LED activity Trigger"
help
This allows LEDs to be controlled by an immediate CPU usage.
The flash frequency and duty cycle varies from faint flashes to
intense brightness depending on the instant CPU load.
If unsure, say N.
config LEDS_TRIGGER_GPIO
tristate "LED GPIO Trigger"
depends on GPIOLIB || COMPILE_TEST
help
This allows LEDs to be controlled by gpio events. It's good
when using gpios as switches and triggering the needed LEDs
from there. One use case is n810's keypad LEDs that could
be triggered by this trigger when user slides up to show
keypad.
If unsure, say N.
config LEDS_TRIGGER_DEFAULT_ON
tristate "LED Default ON Trigger"
help
This allows LEDs to be initialised in the ON state.
If unsure, say Y.
comment "iptables trigger is under Netfilter config (LED target)"
depends on LEDS_TRIGGERS
config LEDS_TRIGGER_TRANSIENT
tristate "LED Transient Trigger"
help
This allows one time activation of a transient state on
GPIO/PWM based hardware.
If unsure, say Y.
config LEDS_TRIGGER_CAMERA
tristate "LED Camera Flash/Torch Trigger"
help
This allows LEDs to be controlled as a camera flash/torch device.
This enables direct flash/torch on/off by the driver, kernel space.
If unsure, say Y.
config LEDS_TRIGGER_PANIC
bool "LED Panic Trigger"
help
This allows LEDs to be configured to blink on a kernel panic.
Enabling this option will allow to mark certain LEDs as panic indicators,
allowing to blink them on a kernel panic, even if they are set to
a different trigger.
If unsure, say Y.
config LEDS_TRIGGER_NETDEV
tristate "LED Netdev Trigger"
depends on NET
help
This allows LEDs to be controlled by network device activity.
If unsure, say Y.
endif # LEDS_TRIGGERS
+15
View File
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_LEDS_TRIGGER_TIMER) += ledtrig-timer.o
obj-$(CONFIG_LEDS_TRIGGER_ONESHOT) += ledtrig-oneshot.o
obj-$(CONFIG_LEDS_TRIGGER_DISK) += ledtrig-disk.o
obj-$(CONFIG_LEDS_TRIGGER_MTD) += ledtrig-mtd.o
obj-$(CONFIG_LEDS_TRIGGER_HEARTBEAT) += ledtrig-heartbeat.o
obj-$(CONFIG_LEDS_TRIGGER_BACKLIGHT) += ledtrig-backlight.o
obj-$(CONFIG_LEDS_TRIGGER_GPIO) += ledtrig-gpio.o
obj-$(CONFIG_LEDS_TRIGGER_CPU) += ledtrig-cpu.o
obj-$(CONFIG_LEDS_TRIGGER_ACTIVITY) += ledtrig-activity.o
obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) += ledtrig-default-on.o
obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
obj-$(CONFIG_LEDS_TRIGGER_PANIC) += ledtrig-panic.o
obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o
+268
View File
@@ -0,0 +1,268 @@
/*
* Activity LED trigger
*
* Copyright (C) 2017 Willy Tarreau <w@1wt.eu>
* Partially based on Atsushi Nemoto's ledtrig-heartbeat.c.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kernel_stat.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/reboot.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include "../leds.h"
static int panic_detected;
struct activity_data {
struct timer_list timer;
struct led_classdev *led_cdev;
u64 last_used;
u64 last_boot;
int time_left;
int state;
int invert;
};
static void led_activity_function(struct timer_list *t)
{
struct activity_data *activity_data = from_timer(activity_data, t,
timer);
struct led_classdev *led_cdev = activity_data->led_cdev;
unsigned int target;
unsigned int usage;
int delay;
u64 curr_used;
u64 curr_boot;
s32 diff_used;
s32 diff_boot;
int cpus;
int i;
if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags))
led_cdev->blink_brightness = led_cdev->new_blink_brightness;
if (unlikely(panic_detected)) {
/* full brightness in case of panic */
led_set_brightness_nosleep(led_cdev, led_cdev->blink_brightness);
return;
}
cpus = 0;
curr_used = 0;
for_each_possible_cpu(i) {
curr_used += kcpustat_cpu(i).cpustat[CPUTIME_USER]
+ kcpustat_cpu(i).cpustat[CPUTIME_NICE]
+ kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]
+ kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]
+ kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
cpus++;
}
/* We come here every 100ms in the worst case, so that's 100M ns of
* cumulated time. By dividing by 2^16, we get the time resolution
* down to 16us, ensuring we won't overflow 32-bit computations below
* even up to 3k CPUs, while keeping divides cheap on smaller systems.
*/
curr_boot = ktime_get_boot_ns() * cpus;
diff_boot = (curr_boot - activity_data->last_boot) >> 16;
diff_used = (curr_used - activity_data->last_used) >> 16;
activity_data->last_boot = curr_boot;
activity_data->last_used = curr_used;
if (diff_boot <= 0 || diff_used < 0)
usage = 0;
else if (diff_used >= diff_boot)
usage = 100;
else
usage = 100 * diff_used / diff_boot;
/*
* Now we know the total boot_time multiplied by the number of CPUs, and
* the total idle+wait time for all CPUs. We'll compare how they evolved
* since last call. The % of overall CPU usage is :
*
* 1 - delta_idle / delta_boot
*
* What we want is that when the CPU usage is zero, the LED must blink
* slowly with very faint flashes that are detectable but not disturbing
* (typically 10ms every second, or 10ms ON, 990ms OFF). Then we want
* blinking frequency to increase up to the point where the load is
* enough to saturate one core in multi-core systems or 50% in single
* core systems. At this point it should reach 10 Hz with a 10/90 duty
* cycle (10ms ON, 90ms OFF). After this point, the blinking frequency
* remains stable (10 Hz) and only the duty cycle increases to report
* the activity, up to the point where we have 90ms ON, 10ms OFF when
* all cores are saturated. It's important that the LED never stays in
* a steady state so that it's easy to distinguish an idle or saturated
* machine from a hung one.
*
* This gives us :
* - a target CPU usage of min(50%, 100%/#CPU) for a 10% duty cycle
* (10ms ON, 90ms OFF)
* - below target :
* ON_ms = 10
* OFF_ms = 90 + (1 - usage/target) * 900
* - above target :
* ON_ms = 10 + (usage-target)/(100%-target) * 80
* OFF_ms = 90 - (usage-target)/(100%-target) * 80
*
* In order to keep a good responsiveness, we cap the sleep time to
* 100 ms and keep track of the sleep time left. This allows us to
* quickly change it if needed.
*/
activity_data->time_left -= 100;
if (activity_data->time_left <= 0) {
activity_data->time_left = 0;
activity_data->state = !activity_data->state;
led_set_brightness_nosleep(led_cdev,
(activity_data->state ^ activity_data->invert) ?
led_cdev->blink_brightness : LED_OFF);
}
target = (cpus > 1) ? (100 / cpus) : 50;
if (usage < target)
delay = activity_data->state ?
10 : /* ON */
990 - 900 * usage / target; /* OFF */
else
delay = activity_data->state ?
10 + 80 * (usage - target) / (100 - target) : /* ON */
90 - 80 * (usage - target) / (100 - target); /* OFF */
if (!activity_data->time_left || delay <= activity_data->time_left)
activity_data->time_left = delay;
delay = min_t(int, activity_data->time_left, 100);
mod_timer(&activity_data->timer, jiffies + msecs_to_jiffies(delay));
}
static ssize_t led_invert_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct activity_data *activity_data = led_trigger_get_drvdata(dev);
return sprintf(buf, "%u\n", activity_data->invert);
}
static ssize_t led_invert_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct activity_data *activity_data = led_trigger_get_drvdata(dev);
unsigned long state;
int ret;
ret = kstrtoul(buf, 0, &state);
if (ret)
return ret;
activity_data->invert = !!state;
return size;
}
static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
static struct attribute *activity_led_attrs[] = {
&dev_attr_invert.attr,
NULL
};
ATTRIBUTE_GROUPS(activity_led);
static int activity_activate(struct led_classdev *led_cdev)
{
struct activity_data *activity_data;
activity_data = kzalloc(sizeof(*activity_data), GFP_KERNEL);
if (!activity_data)
return -ENOMEM;
led_set_trigger_data(led_cdev, activity_data);
activity_data->led_cdev = led_cdev;
timer_setup(&activity_data->timer, led_activity_function, 0);
if (!led_cdev->blink_brightness)
led_cdev->blink_brightness = led_cdev->max_brightness;
led_activity_function(&activity_data->timer);
set_bit(LED_BLINK_SW, &led_cdev->work_flags);
return 0;
}
static void activity_deactivate(struct led_classdev *led_cdev)
{
struct activity_data *activity_data = led_get_trigger_data(led_cdev);
del_timer_sync(&activity_data->timer);
kfree(activity_data);
clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
}
static struct led_trigger activity_led_trigger = {
.name = "activity",
.activate = activity_activate,
.deactivate = activity_deactivate,
.groups = activity_led_groups,
};
static int activity_reboot_notifier(struct notifier_block *nb,
unsigned long code, void *unused)
{
led_trigger_unregister(&activity_led_trigger);
return NOTIFY_DONE;
}
static int activity_panic_notifier(struct notifier_block *nb,
unsigned long code, void *unused)
{
panic_detected = 1;
return NOTIFY_DONE;
}
static struct notifier_block activity_reboot_nb = {
.notifier_call = activity_reboot_notifier,
};
static struct notifier_block activity_panic_nb = {
.notifier_call = activity_panic_notifier,
};
static int __init activity_init(void)
{
int rc = led_trigger_register(&activity_led_trigger);
if (!rc) {
atomic_notifier_chain_register(&panic_notifier_list,
&activity_panic_nb);
register_reboot_notifier(&activity_reboot_nb);
}
return rc;
}
static void __exit activity_exit(void)
{
unregister_reboot_notifier(&activity_reboot_nb);
atomic_notifier_chain_unregister(&panic_notifier_list,
&activity_panic_nb);
led_trigger_unregister(&activity_led_trigger);
}
module_init(activity_init);
module_exit(activity_exit);
MODULE_AUTHOR("Willy Tarreau <w@1wt.eu>");
MODULE_DESCRIPTION("Activity LED trigger");
MODULE_LICENSE("GPL v2");
+146
View File
@@ -0,0 +1,146 @@
/*
* Backlight emulation LED trigger
*
* Copyright 2008 (C) Rodolfo Giometti <giometti@linux.it>
* Copyright 2008 (C) Eurotech S.p.A. <info@eurotech.it>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fb.h>
#include <linux/leds.h>
#include "../leds.h"
#define BLANK 1
#define UNBLANK 0
struct bl_trig_notifier {
struct led_classdev *led;
int brightness;
int old_status;
struct notifier_block notifier;
unsigned invert;
};
static int fb_notifier_callback(struct notifier_block *p,
unsigned long event, void *data)
{
struct bl_trig_notifier *n = container_of(p,
struct bl_trig_notifier, notifier);
struct led_classdev *led = n->led;
struct fb_event *fb_event = data;
int *blank;
int new_status;
/* If we aren't interested in this event, skip it immediately ... */
if (event != FB_EVENT_BLANK)
return 0;
blank = fb_event->data;
new_status = *blank ? BLANK : UNBLANK;
if (new_status == n->old_status)
return 0;
if ((n->old_status == UNBLANK) ^ n->invert) {
n->brightness = led->brightness;
led_set_brightness_nosleep(led, LED_OFF);
} else {
led_set_brightness_nosleep(led, n->brightness);
}
n->old_status = new_status;
return 0;
}
static ssize_t bl_trig_invert_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
return sprintf(buf, "%u\n", n->invert);
}
static ssize_t bl_trig_invert_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t num)
{
struct led_classdev *led = led_trigger_get_led(dev);
struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
unsigned long invert;
int ret;
ret = kstrtoul(buf, 10, &invert);
if (ret < 0)
return ret;
if (invert > 1)
return -EINVAL;
n->invert = invert;
/* After inverting, we need to update the LED. */
if ((n->old_status == BLANK) ^ n->invert)
led_set_brightness_nosleep(led, LED_OFF);
else
led_set_brightness_nosleep(led, n->brightness);
return num;
}
static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
static struct attribute *bl_trig_attrs[] = {
&dev_attr_inverted.attr,
NULL,
};
ATTRIBUTE_GROUPS(bl_trig);
static int bl_trig_activate(struct led_classdev *led)
{
int ret;
struct bl_trig_notifier *n;
n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
if (!n)
return -ENOMEM;
led_set_trigger_data(led, n);
n->led = led;
n->brightness = led->brightness;
n->old_status = UNBLANK;
n->notifier.notifier_call = fb_notifier_callback;
ret = fb_register_client(&n->notifier);
if (ret)
dev_err(led->dev, "unable to register backlight trigger\n");
return 0;
}
static void bl_trig_deactivate(struct led_classdev *led)
{
struct bl_trig_notifier *n = led_get_trigger_data(led);
fb_unregister_client(&n->notifier);
kfree(n);
}
static struct led_trigger bl_led_trigger = {
.name = "backlight",
.activate = bl_trig_activate,
.deactivate = bl_trig_deactivate,
.groups = bl_trig_groups,
};
module_led_trigger(bl_led_trigger);
MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
MODULE_DESCRIPTION("Backlight emulation LED trigger");
MODULE_LICENSE("GPL v2");
+56
View File
@@ -0,0 +1,56 @@
/*
* Camera Flash and Torch On/Off Trigger
*
* based on ledtrig-ide-disk.c
*
* Copyright 2013 Texas Instruments
*
* Author: Milo(Woogyom) Kim <milo.kim@ti.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/leds.h>
DEFINE_LED_TRIGGER(ledtrig_flash);
DEFINE_LED_TRIGGER(ledtrig_torch);
void ledtrig_flash_ctrl(bool on)
{
enum led_brightness brt = on ? LED_FULL : LED_OFF;
led_trigger_event(ledtrig_flash, brt);
}
EXPORT_SYMBOL_GPL(ledtrig_flash_ctrl);
void ledtrig_torch_ctrl(bool on)
{
enum led_brightness brt = on ? LED_FULL : LED_OFF;
led_trigger_event(ledtrig_torch, brt);
}
EXPORT_SYMBOL_GPL(ledtrig_torch_ctrl);
static int __init ledtrig_camera_init(void)
{
led_trigger_register_simple("flash", &ledtrig_flash);
led_trigger_register_simple("torch", &ledtrig_torch);
return 0;
}
module_init(ledtrig_camera_init);
static void __exit ledtrig_camera_exit(void)
{
led_trigger_unregister_simple(ledtrig_torch);
led_trigger_unregister_simple(ledtrig_flash);
}
module_exit(ledtrig_camera_exit);
MODULE_DESCRIPTION("LED Trigger for Camera Flash/Torch Control");
MODULE_AUTHOR("Milo Kim");
MODULE_LICENSE("GPL v2");
+169
View File
@@ -0,0 +1,169 @@
/*
* ledtrig-cpu.c - LED trigger based on CPU activity
*
* This LED trigger will be registered for each possible CPU and named as
* cpu0, cpu1, cpu2, cpu3, etc.
*
* It can be bound to any LED just like other triggers using either a
* board file or via sysfs interface.
*
* An API named ledtrig_cpu is exported for any user, who want to add CPU
* activity indication in their code
*
* Copyright 2011 Linus Walleij <linus.walleij@linaro.org>
* Copyright 2011 - 2012 Bryan Wu <bryan.wu@canonical.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/percpu.h>
#include <linux/syscore_ops.h>
#include <linux/rwsem.h>
#include <linux/cpu.h>
#include "../leds.h"
#define MAX_NAME_LEN 8
struct led_trigger_cpu {
bool is_active;
char name[MAX_NAME_LEN];
struct led_trigger *_trig;
};
static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
static struct led_trigger *trig_cpu_all;
static atomic_t num_active_cpus = ATOMIC_INIT(0);
/**
* ledtrig_cpu - emit a CPU event as a trigger
* @evt: CPU event to be emitted
*
* Emit a CPU event on a CPU core, which will trigger a
* bound LED to turn on or turn off.
*/
void ledtrig_cpu(enum cpu_led_event ledevt)
{
struct led_trigger_cpu *trig = this_cpu_ptr(&cpu_trig);
bool is_active = trig->is_active;
/* Locate the correct CPU LED */
switch (ledevt) {
case CPU_LED_IDLE_END:
case CPU_LED_START:
/* Will turn the LED on, max brightness */
is_active = true;
break;
case CPU_LED_IDLE_START:
case CPU_LED_STOP:
case CPU_LED_HALTED:
/* Will turn the LED off */
is_active = false;
break;
default:
/* Will leave the LED as it is */
break;
}
if (is_active != trig->is_active) {
unsigned int active_cpus;
unsigned int total_cpus;
/* Update trigger state */
trig->is_active = is_active;
atomic_add(is_active ? 1 : -1, &num_active_cpus);
active_cpus = atomic_read(&num_active_cpus);
total_cpus = num_present_cpus();
led_trigger_event(trig->_trig,
is_active ? LED_FULL : LED_OFF);
led_trigger_event(trig_cpu_all,
DIV_ROUND_UP(LED_FULL * active_cpus, total_cpus));
}
}
EXPORT_SYMBOL(ledtrig_cpu);
static int ledtrig_cpu_syscore_suspend(void)
{
ledtrig_cpu(CPU_LED_STOP);
return 0;
}
static void ledtrig_cpu_syscore_resume(void)
{
ledtrig_cpu(CPU_LED_START);
}
static void ledtrig_cpu_syscore_shutdown(void)
{
ledtrig_cpu(CPU_LED_HALTED);
}
static struct syscore_ops ledtrig_cpu_syscore_ops = {
.shutdown = ledtrig_cpu_syscore_shutdown,
.suspend = ledtrig_cpu_syscore_suspend,
.resume = ledtrig_cpu_syscore_resume,
};
static int ledtrig_online_cpu(unsigned int cpu)
{
ledtrig_cpu(CPU_LED_START);
return 0;
}
static int ledtrig_prepare_down_cpu(unsigned int cpu)
{
ledtrig_cpu(CPU_LED_STOP);
return 0;
}
static int __init ledtrig_cpu_init(void)
{
int cpu;
int ret;
/* Supports up to 9999 cpu cores */
BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
/*
* Registering a trigger for all CPUs.
*/
led_trigger_register_simple("cpu", &trig_cpu_all);
/*
* Registering CPU led trigger for each CPU core here
* ignores CPU hotplug, but after this CPU hotplug works
* fine with this trigger.
*/
for_each_possible_cpu(cpu) {
struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
led_trigger_register_simple(trig->name, &trig->_trig);
}
register_syscore_ops(&ledtrig_cpu_syscore_ops);
ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "leds/trigger:starting",
ledtrig_online_cpu, ledtrig_prepare_down_cpu);
if (ret < 0)
pr_err("CPU hotplug notifier for ledtrig-cpu could not be registered: %d\n",
ret);
pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
return 0;
}
device_initcall(ledtrig_cpu_init);
+33
View File
@@ -0,0 +1,33 @@
/*
* LED Kernel Default ON Trigger
*
* Copyright 2008 Nick Forbes <nick.forbes@incepta.com>
*
* Based on Richard Purdie's ledtrig-timer.c.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/leds.h>
#include "../leds.h"
static int defon_trig_activate(struct led_classdev *led_cdev)
{
led_set_brightness_nosleep(led_cdev, led_cdev->max_brightness);
return 0;
}
static struct led_trigger defon_led_trigger = {
.name = "default-on",
.activate = defon_trig_activate,
};
module_led_trigger(defon_led_trigger);
MODULE_AUTHOR("Nick Forbes <nick.forbes@incepta.com>");
MODULE_DESCRIPTION("Default-ON LED trigger");
MODULE_LICENSE("GPL v2");
+51
View File
@@ -0,0 +1,51 @@
/*
* LED Disk Activity Trigger
*
* Copyright 2006 Openedhand Ltd.
*
* Author: Richard Purdie <rpurdie@openedhand.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/leds.h>
#define BLINK_DELAY 30
DEFINE_LED_TRIGGER(ledtrig_disk);
DEFINE_LED_TRIGGER(ledtrig_disk_read);
DEFINE_LED_TRIGGER(ledtrig_disk_write);
DEFINE_LED_TRIGGER(ledtrig_ide);
void ledtrig_disk_activity(bool write)
{
unsigned long blink_delay = BLINK_DELAY;
led_trigger_blink_oneshot(ledtrig_disk,
&blink_delay, &blink_delay, 0);
led_trigger_blink_oneshot(ledtrig_ide,
&blink_delay, &blink_delay, 0);
if (write)
led_trigger_blink_oneshot(ledtrig_disk_write,
&blink_delay, &blink_delay, 0);
else
led_trigger_blink_oneshot(ledtrig_disk_read,
&blink_delay, &blink_delay, 0);
}
EXPORT_SYMBOL(ledtrig_disk_activity);
static int __init ledtrig_disk_init(void)
{
led_trigger_register_simple("disk-activity", &ledtrig_disk);
led_trigger_register_simple("disk-read", &ledtrig_disk_read);
led_trigger_register_simple("disk-write", &ledtrig_disk_write);
led_trigger_register_simple("ide-disk", &ledtrig_ide);
return 0;
}
device_initcall(ledtrig_disk_init);
+202
View File
@@ -0,0 +1,202 @@
/*
* ledtrig-gio.c - LED Trigger Based on GPIO events
*
* Copyright 2009 Felipe Balbi <me@felipebalbi.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/leds.h>
#include <linux/slab.h>
#include "../leds.h"
struct gpio_trig_data {
struct led_classdev *led;
unsigned desired_brightness; /* desired brightness when led is on */
unsigned inverted; /* true when gpio is inverted */
unsigned gpio; /* gpio that triggers the leds */
};
static irqreturn_t gpio_trig_irq(int irq, void *_led)
{
struct led_classdev *led = _led;
struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
int tmp;
tmp = gpio_get_value_cansleep(gpio_data->gpio);
if (gpio_data->inverted)
tmp = !tmp;
if (tmp) {
if (gpio_data->desired_brightness)
led_set_brightness_nosleep(gpio_data->led,
gpio_data->desired_brightness);
else
led_set_brightness_nosleep(gpio_data->led, LED_FULL);
} else {
led_set_brightness_nosleep(gpio_data->led, LED_OFF);
}
return IRQ_HANDLED;
}
static ssize_t gpio_trig_brightness_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
return sprintf(buf, "%u\n", gpio_data->desired_brightness);
}
static ssize_t gpio_trig_brightness_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t n)
{
struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
unsigned desired_brightness;
int ret;
ret = sscanf(buf, "%u", &desired_brightness);
if (ret < 1 || desired_brightness > 255) {
dev_err(dev, "invalid value\n");
return -EINVAL;
}
gpio_data->desired_brightness = desired_brightness;
return n;
}
static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
gpio_trig_brightness_store);
static ssize_t gpio_trig_inverted_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
return sprintf(buf, "%u\n", gpio_data->inverted);
}
static ssize_t gpio_trig_inverted_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t n)
{
struct led_classdev *led = led_trigger_get_led(dev);
struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
unsigned long inverted;
int ret;
ret = kstrtoul(buf, 10, &inverted);
if (ret < 0)
return ret;
if (inverted > 1)
return -EINVAL;
gpio_data->inverted = inverted;
/* After inverting, we need to update the LED. */
gpio_trig_irq(0, led);
return n;
}
static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
gpio_trig_inverted_store);
static ssize_t gpio_trig_gpio_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
return sprintf(buf, "%u\n", gpio_data->gpio);
}
static ssize_t gpio_trig_gpio_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t n)
{
struct led_classdev *led = led_trigger_get_led(dev);
struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
unsigned gpio;
int ret;
ret = sscanf(buf, "%u", &gpio);
if (ret < 1) {
dev_err(dev, "couldn't read gpio number\n");
return -EINVAL;
}
if (gpio_data->gpio == gpio)
return n;
if (!gpio) {
if (gpio_data->gpio != 0)
free_irq(gpio_to_irq(gpio_data->gpio), led);
gpio_data->gpio = 0;
return n;
}
ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq,
IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
if (ret) {
dev_err(dev, "request_irq failed with error %d\n", ret);
} else {
if (gpio_data->gpio != 0)
free_irq(gpio_to_irq(gpio_data->gpio), led);
gpio_data->gpio = gpio;
/* After changing the GPIO, we need to update the LED. */
gpio_trig_irq(0, led);
}
return ret ? ret : n;
}
static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
static struct attribute *gpio_trig_attrs[] = {
&dev_attr_desired_brightness.attr,
&dev_attr_inverted.attr,
&dev_attr_gpio.attr,
NULL
};
ATTRIBUTE_GROUPS(gpio_trig);
static int gpio_trig_activate(struct led_classdev *led)
{
struct gpio_trig_data *gpio_data;
gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
if (!gpio_data)
return -ENOMEM;
gpio_data->led = led;
led_set_trigger_data(led, gpio_data);
return 0;
}
static void gpio_trig_deactivate(struct led_classdev *led)
{
struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
if (gpio_data->gpio != 0)
free_irq(gpio_to_irq(gpio_data->gpio), led);
kfree(gpio_data);
}
static struct led_trigger gpio_led_trigger = {
.name = "gpio",
.activate = gpio_trig_activate,
.deactivate = gpio_trig_deactivate,
.groups = gpio_trig_groups,
};
module_led_trigger(gpio_led_trigger);
MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
MODULE_DESCRIPTION("GPIO LED trigger");
MODULE_LICENSE("GPL v2");
+215
View File
@@ -0,0 +1,215 @@
/*
* LED Heartbeat Trigger
*
* Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
*
* Based on Richard Purdie's ledtrig-timer.c and some arch's
* CONFIG_HEARTBEAT code.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/sched.h>
#include <linux/sched/loadavg.h>
#include <linux/leds.h>
#include <linux/reboot.h>
#include "../leds.h"
static int panic_heartbeats;
struct heartbeat_trig_data {
struct led_classdev *led_cdev;
unsigned int phase;
unsigned int period;
struct timer_list timer;
unsigned int invert;
};
static void led_heartbeat_function(struct timer_list *t)
{
struct heartbeat_trig_data *heartbeat_data =
from_timer(heartbeat_data, t, timer);
struct led_classdev *led_cdev;
unsigned long brightness = LED_OFF;
unsigned long delay = 0;
led_cdev = heartbeat_data->led_cdev;
if (unlikely(panic_heartbeats)) {
led_set_brightness_nosleep(led_cdev, LED_OFF);
return;
}
if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags))
led_cdev->blink_brightness = led_cdev->new_blink_brightness;
/* acts like an actual heart beat -- ie thump-thump-pause... */
switch (heartbeat_data->phase) {
case 0:
/*
* The hyperbolic function below modifies the
* heartbeat period length in dependency of the
* current (1min) load. It goes through the points
* f(0)=1260, f(1)=860, f(5)=510, f(inf)->300.
*/
heartbeat_data->period = 300 +
(6720 << FSHIFT) / (5 * avenrun[0] + (7 << FSHIFT));
heartbeat_data->period =
msecs_to_jiffies(heartbeat_data->period);
delay = msecs_to_jiffies(70);
heartbeat_data->phase++;
if (!heartbeat_data->invert)
brightness = led_cdev->blink_brightness;
break;
case 1:
delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
heartbeat_data->phase++;
if (heartbeat_data->invert)
brightness = led_cdev->blink_brightness;
break;
case 2:
delay = msecs_to_jiffies(70);
heartbeat_data->phase++;
if (!heartbeat_data->invert)
brightness = led_cdev->blink_brightness;
break;
default:
delay = heartbeat_data->period - heartbeat_data->period / 4 -
msecs_to_jiffies(70);
heartbeat_data->phase = 0;
if (heartbeat_data->invert)
brightness = led_cdev->blink_brightness;
break;
}
led_set_brightness_nosleep(led_cdev, brightness);
mod_timer(&heartbeat_data->timer, jiffies + delay);
}
static ssize_t led_invert_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct heartbeat_trig_data *heartbeat_data =
led_trigger_get_drvdata(dev);
return sprintf(buf, "%u\n", heartbeat_data->invert);
}
static ssize_t led_invert_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct heartbeat_trig_data *heartbeat_data =
led_trigger_get_drvdata(dev);
unsigned long state;
int ret;
ret = kstrtoul(buf, 0, &state);
if (ret)
return ret;
heartbeat_data->invert = !!state;
return size;
}
static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
static struct attribute *heartbeat_trig_attrs[] = {
&dev_attr_invert.attr,
NULL
};
ATTRIBUTE_GROUPS(heartbeat_trig);
static int heartbeat_trig_activate(struct led_classdev *led_cdev)
{
struct heartbeat_trig_data *heartbeat_data;
heartbeat_data = kzalloc(sizeof(*heartbeat_data), GFP_KERNEL);
if (!heartbeat_data)
return -ENOMEM;
led_set_trigger_data(led_cdev, heartbeat_data);
heartbeat_data->led_cdev = led_cdev;
timer_setup(&heartbeat_data->timer, led_heartbeat_function, 0);
heartbeat_data->phase = 0;
if (!led_cdev->blink_brightness)
led_cdev->blink_brightness = led_cdev->max_brightness;
led_heartbeat_function(&heartbeat_data->timer);
set_bit(LED_BLINK_SW, &led_cdev->work_flags);
return 0;
}
static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
{
struct heartbeat_trig_data *heartbeat_data =
led_get_trigger_data(led_cdev);
del_timer_sync(&heartbeat_data->timer);
kfree(heartbeat_data);
clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
}
static struct led_trigger heartbeat_led_trigger = {
.name = "heartbeat",
.activate = heartbeat_trig_activate,
.deactivate = heartbeat_trig_deactivate,
.groups = heartbeat_trig_groups,
};
static int heartbeat_reboot_notifier(struct notifier_block *nb,
unsigned long code, void *unused)
{
led_trigger_unregister(&heartbeat_led_trigger);
return NOTIFY_DONE;
}
static int heartbeat_panic_notifier(struct notifier_block *nb,
unsigned long code, void *unused)
{
panic_heartbeats = 1;
return NOTIFY_DONE;
}
static struct notifier_block heartbeat_reboot_nb = {
.notifier_call = heartbeat_reboot_notifier,
};
static struct notifier_block heartbeat_panic_nb = {
.notifier_call = heartbeat_panic_notifier,
};
static int __init heartbeat_trig_init(void)
{
int rc = led_trigger_register(&heartbeat_led_trigger);
if (!rc) {
atomic_notifier_chain_register(&panic_notifier_list,
&heartbeat_panic_nb);
register_reboot_notifier(&heartbeat_reboot_nb);
}
return rc;
}
static void __exit heartbeat_trig_exit(void)
{
unregister_reboot_notifier(&heartbeat_reboot_nb);
atomic_notifier_chain_unregister(&panic_notifier_list,
&heartbeat_panic_nb);
led_trigger_unregister(&heartbeat_led_trigger);
}
module_init(heartbeat_trig_init);
module_exit(heartbeat_trig_exit);
MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
MODULE_DESCRIPTION("Heartbeat LED trigger");
MODULE_LICENSE("GPL v2");
+45
View File
@@ -0,0 +1,45 @@
/*
* LED MTD trigger
*
* Copyright 2016 Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
*
* Based on LED IDE-Disk Activity Trigger
*
* Copyright 2006 Openedhand Ltd.
*
* Author: Richard Purdie <rpurdie@openedhand.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/leds.h>
#define BLINK_DELAY 30
DEFINE_LED_TRIGGER(ledtrig_mtd);
DEFINE_LED_TRIGGER(ledtrig_nand);
void ledtrig_mtd_activity(void)
{
unsigned long blink_delay = BLINK_DELAY;
led_trigger_blink_oneshot(ledtrig_mtd,
&blink_delay, &blink_delay, 0);
led_trigger_blink_oneshot(ledtrig_nand,
&blink_delay, &blink_delay, 0);
}
EXPORT_SYMBOL(ledtrig_mtd_activity);
static int __init ledtrig_mtd_init(void)
{
led_trigger_register_simple("mtd", &ledtrig_mtd);
led_trigger_register_simple("nand-disk", &ledtrig_nand);
return 0;
}
device_initcall(ledtrig_mtd_init);
+462
View File
@@ -0,0 +1,462 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright 2017 Ben Whitten <ben.whitten@gmail.com>
// Copyright 2007 Oliver Jowett <oliver@opencloud.com>
//
// LED Kernel Netdev Trigger
//
// Toggles the LED to reflect the link and traffic state of a named net device
//
// Derived from ledtrig-timer.c which is:
// Copyright 2005-2006 Openedhand Ltd.
// Author: Richard Purdie <rpurdie@openedhand.com>
#include <linux/atomic.h>
#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/spinlock.h>
#include <linux/timer.h>
#include "../leds.h"
/*
* Configurable sysfs attributes:
*
* device_name - network device name to monitor
* interval - duration of LED blink, in milliseconds
* link - LED's normal state reflects whether the link is up
* (has carrier) or not
* tx - LED blinks on transmitted data
* rx - LED blinks on receive data
*
*/
struct led_netdev_data {
spinlock_t lock;
struct delayed_work work;
struct notifier_block notifier;
struct led_classdev *led_cdev;
struct net_device *net_dev;
char device_name[IFNAMSIZ];
atomic_t interval;
unsigned int last_activity;
unsigned long mode;
#define NETDEV_LED_LINK 0
#define NETDEV_LED_TX 1
#define NETDEV_LED_RX 2
#define NETDEV_LED_MODE_LINKUP 3
};
enum netdev_led_attr {
NETDEV_ATTR_LINK,
NETDEV_ATTR_TX,
NETDEV_ATTR_RX
};
static void set_baseline_state(struct led_netdev_data *trigger_data)
{
int current_brightness;
struct led_classdev *led_cdev = trigger_data->led_cdev;
current_brightness = led_cdev->brightness;
if (current_brightness)
led_cdev->blink_brightness = current_brightness;
if (!led_cdev->blink_brightness)
led_cdev->blink_brightness = led_cdev->max_brightness;
if (!test_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode))
led_set_brightness(led_cdev, LED_OFF);
else {
if (test_bit(NETDEV_LED_LINK, &trigger_data->mode))
led_set_brightness(led_cdev,
led_cdev->blink_brightness);
else
led_set_brightness(led_cdev, LED_OFF);
/* If we are looking for RX/TX start periodically
* checking stats
*/
if (test_bit(NETDEV_LED_TX, &trigger_data->mode) ||
test_bit(NETDEV_LED_RX, &trigger_data->mode))
schedule_delayed_work(&trigger_data->work, 0);
}
}
static ssize_t device_name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
ssize_t len;
spin_lock_bh(&trigger_data->lock);
len = sprintf(buf, "%s\n", trigger_data->device_name);
spin_unlock_bh(&trigger_data->lock);
return len;
}
static ssize_t device_name_store(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t size)
{
struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
if (size >= IFNAMSIZ)
return -EINVAL;
cancel_delayed_work_sync(&trigger_data->work);
spin_lock_bh(&trigger_data->lock);
if (trigger_data->net_dev) {
dev_put(trigger_data->net_dev);
trigger_data->net_dev = NULL;
}
memcpy(trigger_data->device_name, buf, size);
trigger_data->device_name[size] = 0;
if (size > 0 && trigger_data->device_name[size - 1] == '\n')
trigger_data->device_name[size - 1] = 0;
if (trigger_data->device_name[0] != 0)
trigger_data->net_dev =
dev_get_by_name(&init_net, trigger_data->device_name);
clear_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
if (trigger_data->net_dev != NULL)
if (netif_carrier_ok(trigger_data->net_dev))
set_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
trigger_data->last_activity = 0;
set_baseline_state(trigger_data);
spin_unlock_bh(&trigger_data->lock);
return size;
}
static DEVICE_ATTR_RW(device_name);
static ssize_t netdev_led_attr_show(struct device *dev, char *buf,
enum netdev_led_attr attr)
{
struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
int bit;
switch (attr) {
case NETDEV_ATTR_LINK:
bit = NETDEV_LED_LINK;
break;
case NETDEV_ATTR_TX:
bit = NETDEV_LED_TX;
break;
case NETDEV_ATTR_RX:
bit = NETDEV_LED_RX;
break;
default:
return -EINVAL;
}
return sprintf(buf, "%u\n", test_bit(bit, &trigger_data->mode));
}
static ssize_t netdev_led_attr_store(struct device *dev, const char *buf,
size_t size, enum netdev_led_attr attr)
{
struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
unsigned long state;
int ret;
int bit;
ret = kstrtoul(buf, 0, &state);
if (ret)
return ret;
switch (attr) {
case NETDEV_ATTR_LINK:
bit = NETDEV_LED_LINK;
break;
case NETDEV_ATTR_TX:
bit = NETDEV_LED_TX;
break;
case NETDEV_ATTR_RX:
bit = NETDEV_LED_RX;
break;
default:
return -EINVAL;
}
cancel_delayed_work_sync(&trigger_data->work);
if (state)
set_bit(bit, &trigger_data->mode);
else
clear_bit(bit, &trigger_data->mode);
set_baseline_state(trigger_data);
return size;
}
static ssize_t link_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return netdev_led_attr_show(dev, buf, NETDEV_ATTR_LINK);
}
static ssize_t link_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
return netdev_led_attr_store(dev, buf, size, NETDEV_ATTR_LINK);
}
static DEVICE_ATTR_RW(link);
static ssize_t tx_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return netdev_led_attr_show(dev, buf, NETDEV_ATTR_TX);
}
static ssize_t tx_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
return netdev_led_attr_store(dev, buf, size, NETDEV_ATTR_TX);
}
static DEVICE_ATTR_RW(tx);
static ssize_t rx_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return netdev_led_attr_show(dev, buf, NETDEV_ATTR_RX);
}
static ssize_t rx_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
return netdev_led_attr_store(dev, buf, size, NETDEV_ATTR_RX);
}
static DEVICE_ATTR_RW(rx);
static ssize_t interval_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
return sprintf(buf, "%u\n",
jiffies_to_msecs(atomic_read(&trigger_data->interval)));
}
static ssize_t interval_store(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t size)
{
struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
unsigned long value;
int ret;
ret = kstrtoul(buf, 0, &value);
if (ret)
return ret;
/* impose some basic bounds on the timer interval */
if (value >= 5 && value <= 10000) {
cancel_delayed_work_sync(&trigger_data->work);
atomic_set(&trigger_data->interval, msecs_to_jiffies(value));
set_baseline_state(trigger_data); /* resets timer */
}
return size;
}
static DEVICE_ATTR_RW(interval);
static struct attribute *netdev_trig_attrs[] = {
&dev_attr_device_name.attr,
&dev_attr_link.attr,
&dev_attr_rx.attr,
&dev_attr_tx.attr,
&dev_attr_interval.attr,
NULL
};
ATTRIBUTE_GROUPS(netdev_trig);
static int netdev_trig_notify(struct notifier_block *nb,
unsigned long evt, void *dv)
{
struct net_device *dev =
netdev_notifier_info_to_dev((struct netdev_notifier_info *)dv);
struct led_netdev_data *trigger_data =
container_of(nb, struct led_netdev_data, notifier);
if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE
&& evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER
&& evt != NETDEV_CHANGENAME)
return NOTIFY_DONE;
if (!(dev == trigger_data->net_dev ||
(evt == NETDEV_CHANGENAME && !strcmp(dev->name, trigger_data->device_name)) ||
(evt == NETDEV_REGISTER && !strcmp(dev->name, trigger_data->device_name))))
return NOTIFY_DONE;
cancel_delayed_work_sync(&trigger_data->work);
spin_lock_bh(&trigger_data->lock);
clear_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
switch (evt) {
case NETDEV_CHANGENAME:
case NETDEV_REGISTER:
if (trigger_data->net_dev)
dev_put(trigger_data->net_dev);
dev_hold(dev);
trigger_data->net_dev = dev;
break;
case NETDEV_UNREGISTER:
dev_put(trigger_data->net_dev);
trigger_data->net_dev = NULL;
break;
case NETDEV_UP:
case NETDEV_CHANGE:
if (netif_carrier_ok(dev))
set_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
break;
}
set_baseline_state(trigger_data);
spin_unlock_bh(&trigger_data->lock);
return NOTIFY_DONE;
}
/* here's the real work! */
static void netdev_trig_work(struct work_struct *work)
{
struct led_netdev_data *trigger_data =
container_of(work, struct led_netdev_data, work.work);
struct rtnl_link_stats64 *dev_stats;
unsigned int new_activity;
struct rtnl_link_stats64 temp;
unsigned long interval;
int invert;
/* If we dont have a device, insure we are off */
if (!trigger_data->net_dev) {
led_set_brightness(trigger_data->led_cdev, LED_OFF);
return;
}
/* If we are not looking for RX/TX then return */
if (!test_bit(NETDEV_LED_TX, &trigger_data->mode) &&
!test_bit(NETDEV_LED_RX, &trigger_data->mode))
return;
dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
new_activity =
(test_bit(NETDEV_LED_TX, &trigger_data->mode) ?
dev_stats->tx_packets : 0) +
(test_bit(NETDEV_LED_RX, &trigger_data->mode) ?
dev_stats->rx_packets : 0);
if (trigger_data->last_activity != new_activity) {
led_stop_software_blink(trigger_data->led_cdev);
invert = test_bit(NETDEV_LED_LINK, &trigger_data->mode);
interval = jiffies_to_msecs(
atomic_read(&trigger_data->interval));
/* base state is ON (link present) */
led_blink_set_oneshot(trigger_data->led_cdev,
&interval,
&interval,
invert);
trigger_data->last_activity = new_activity;
}
schedule_delayed_work(&trigger_data->work,
(atomic_read(&trigger_data->interval)*2));
}
static int netdev_trig_activate(struct led_classdev *led_cdev)
{
struct led_netdev_data *trigger_data;
int rc;
trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
if (!trigger_data)
return -ENOMEM;
spin_lock_init(&trigger_data->lock);
trigger_data->notifier.notifier_call = netdev_trig_notify;
trigger_data->notifier.priority = 10;
INIT_DELAYED_WORK(&trigger_data->work, netdev_trig_work);
trigger_data->led_cdev = led_cdev;
trigger_data->net_dev = NULL;
trigger_data->device_name[0] = 0;
trigger_data->mode = 0;
atomic_set(&trigger_data->interval, msecs_to_jiffies(50));
trigger_data->last_activity = 0;
led_set_trigger_data(led_cdev, trigger_data);
rc = register_netdevice_notifier(&trigger_data->notifier);
if (rc)
kfree(trigger_data);
return rc;
}
static void netdev_trig_deactivate(struct led_classdev *led_cdev)
{
struct led_netdev_data *trigger_data = led_get_trigger_data(led_cdev);
unregister_netdevice_notifier(&trigger_data->notifier);
cancel_delayed_work_sync(&trigger_data->work);
if (trigger_data->net_dev)
dev_put(trigger_data->net_dev);
kfree(trigger_data);
}
static struct led_trigger netdev_led_trigger = {
.name = "netdev",
.activate = netdev_trig_activate,
.deactivate = netdev_trig_deactivate,
.groups = netdev_trig_groups,
};
static int __init netdev_trig_init(void)
{
return led_trigger_register(&netdev_led_trigger);
}
static void __exit netdev_trig_exit(void)
{
led_trigger_unregister(&netdev_led_trigger);
}
module_init(netdev_trig_init);
module_exit(netdev_trig_exit);
MODULE_AUTHOR("Ben Whitten <ben.whitten@gmail.com>");
MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
MODULE_DESCRIPTION("Netdev LED trigger");
MODULE_LICENSE("GPL v2");
+169
View File
@@ -0,0 +1,169 @@
/*
* One-shot LED Trigger
*
* Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
*
* Based on ledtrig-timer.c by Richard Purdie <rpurdie@openedhand.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/ctype.h>
#include <linux/slab.h>
#include <linux/leds.h>
#include "../leds.h"
#define DEFAULT_DELAY 100
struct oneshot_trig_data {
unsigned int invert;
};
static ssize_t led_shot(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
led_blink_set_oneshot(led_cdev,
&led_cdev->blink_delay_on, &led_cdev->blink_delay_off,
oneshot_data->invert);
/* content is ignored */
return size;
}
static ssize_t led_invert_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
return sprintf(buf, "%u\n", oneshot_data->invert);
}
static ssize_t led_invert_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
unsigned long state;
int ret;
ret = kstrtoul(buf, 0, &state);
if (ret)
return ret;
oneshot_data->invert = !!state;
if (oneshot_data->invert)
led_set_brightness_nosleep(led_cdev, LED_FULL);
else
led_set_brightness_nosleep(led_cdev, LED_OFF);
return size;
}
static ssize_t led_delay_on_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
}
static ssize_t led_delay_on_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
unsigned long state;
int ret;
ret = kstrtoul(buf, 0, &state);
if (ret)
return ret;
led_cdev->blink_delay_on = state;
return size;
}
static ssize_t led_delay_off_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
}
static ssize_t led_delay_off_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
unsigned long state;
int ret;
ret = kstrtoul(buf, 0, &state);
if (ret)
return ret;
led_cdev->blink_delay_off = state;
return size;
}
static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
static DEVICE_ATTR(shot, 0200, NULL, led_shot);
static struct attribute *oneshot_trig_attrs[] = {
&dev_attr_delay_on.attr,
&dev_attr_delay_off.attr,
&dev_attr_invert.attr,
&dev_attr_shot.attr,
NULL
};
ATTRIBUTE_GROUPS(oneshot_trig);
static int oneshot_trig_activate(struct led_classdev *led_cdev)
{
struct oneshot_trig_data *oneshot_data;
oneshot_data = kzalloc(sizeof(*oneshot_data), GFP_KERNEL);
if (!oneshot_data)
return -ENOMEM;
led_set_trigger_data(led_cdev, oneshot_data);
led_cdev->blink_delay_on = DEFAULT_DELAY;
led_cdev->blink_delay_off = DEFAULT_DELAY;
return 0;
}
static void oneshot_trig_deactivate(struct led_classdev *led_cdev)
{
struct oneshot_trig_data *oneshot_data = led_get_trigger_data(led_cdev);
kfree(oneshot_data);
/* Stop blinking */
led_set_brightness(led_cdev, LED_OFF);
}
static struct led_trigger oneshot_led_trigger = {
.name = "oneshot",
.activate = oneshot_trig_activate,
.deactivate = oneshot_trig_deactivate,
.groups = oneshot_trig_groups,
};
module_led_trigger(oneshot_led_trigger);
MODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>");
MODULE_DESCRIPTION("One-shot LED trigger");
MODULE_LICENSE("GPL v2");
+77
View File
@@ -0,0 +1,77 @@
/*
* Kernel Panic LED Trigger
*
* Copyright 2016 Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/notifier.h>
#include <linux/leds.h>
#include "../leds.h"
static struct led_trigger *trigger;
/*
* This is called in a special context by the atomic panic
* notifier. This means the trigger can be changed without
* worrying about locking.
*/
static void led_trigger_set_panic(struct led_classdev *led_cdev)
{
struct led_trigger *trig;
list_for_each_entry(trig, &trigger_list, next_trig) {
if (strcmp("panic", trig->name))
continue;
if (led_cdev->trigger)
list_del(&led_cdev->trig_list);
list_add_tail(&led_cdev->trig_list, &trig->led_cdevs);
/* Avoid the delayed blink path */
led_cdev->blink_delay_on = 0;
led_cdev->blink_delay_off = 0;
led_cdev->trigger = trig;
if (trig->activate)
trig->activate(led_cdev);
break;
}
}
static int led_trigger_panic_notifier(struct notifier_block *nb,
unsigned long code, void *unused)
{
struct led_classdev *led_cdev;
list_for_each_entry(led_cdev, &leds_list, node)
if (led_cdev->flags & LED_PANIC_INDICATOR)
led_trigger_set_panic(led_cdev);
return NOTIFY_DONE;
}
static struct notifier_block led_trigger_panic_nb = {
.notifier_call = led_trigger_panic_notifier,
};
static long led_panic_blink(int state)
{
led_trigger_event(trigger, state ? LED_FULL : LED_OFF);
return 0;
}
static int __init ledtrig_panic_init(void)
{
atomic_notifier_chain_register(&panic_notifier_list,
&led_trigger_panic_nb);
led_trigger_register_simple("panic", &trigger);
panic_blink = led_panic_blink;
return 0;
}
device_initcall(ledtrig_panic_init);
+104
View File
@@ -0,0 +1,104 @@
/*
* LED Kernel Timer Trigger
*
* Copyright 2005-2006 Openedhand Ltd.
*
* Author: Richard Purdie <rpurdie@openedhand.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/ctype.h>
#include <linux/leds.h>
static ssize_t led_delay_on_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
}
static ssize_t led_delay_on_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
unsigned long state;
ssize_t ret = -EINVAL;
ret = kstrtoul(buf, 10, &state);
if (ret)
return ret;
led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
led_cdev->blink_delay_on = state;
return size;
}
static ssize_t led_delay_off_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
}
static ssize_t led_delay_off_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
unsigned long state;
ssize_t ret = -EINVAL;
ret = kstrtoul(buf, 10, &state);
if (ret)
return ret;
led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
led_cdev->blink_delay_off = state;
return size;
}
static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
static struct attribute *timer_trig_attrs[] = {
&dev_attr_delay_on.attr,
&dev_attr_delay_off.attr,
NULL
};
ATTRIBUTE_GROUPS(timer_trig);
static int timer_trig_activate(struct led_classdev *led_cdev)
{
led_blink_set(led_cdev, &led_cdev->blink_delay_on,
&led_cdev->blink_delay_off);
return 0;
}
static void timer_trig_deactivate(struct led_classdev *led_cdev)
{
/* Stop blinking */
led_set_brightness(led_cdev, LED_OFF);
}
static struct led_trigger timer_led_trigger = {
.name = "timer",
.activate = timer_trig_activate,
.deactivate = timer_trig_deactivate,
.groups = timer_trig_groups,
};
module_led_trigger(timer_led_trigger);
MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
MODULE_DESCRIPTION("Timer LED trigger");
MODULE_LICENSE("GPL v2");
+198
View File
@@ -0,0 +1,198 @@
// SPDX-License-Identifier: GPL-2.0
//
// LED Kernel Transient Trigger
//
// Transient trigger allows one shot timer activation. Please refer to
// Documentation/leds/ledtrig-transient.txt for details
// Copyright (C) 2012 Shuah Khan <shuahkhan@gmail.com>
//
// Based on Richard Purdie's ledtrig-timer.c and Atsushi Nemoto's
// ledtrig-heartbeat.c
// Design and use-case input from Jonas Bonn <jonas@southpole.se> and
// Neil Brown <neilb@suse.de>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/leds.h>
#include "../leds.h"
struct transient_trig_data {
int activate;
int state;
int restore_state;
unsigned long duration;
struct timer_list timer;
struct led_classdev *led_cdev;
};
static void transient_timer_function(struct timer_list *t)
{
struct transient_trig_data *transient_data =
from_timer(transient_data, t, timer);
struct led_classdev *led_cdev = transient_data->led_cdev;
transient_data->activate = 0;
led_set_brightness_nosleep(led_cdev, transient_data->restore_state);
}
static ssize_t transient_activate_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct transient_trig_data *transient_data =
led_trigger_get_drvdata(dev);
return sprintf(buf, "%d\n", transient_data->activate);
}
static ssize_t transient_activate_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = led_trigger_get_led(dev);
struct transient_trig_data *transient_data =
led_trigger_get_drvdata(dev);
unsigned long state;
ssize_t ret;
ret = kstrtoul(buf, 10, &state);
if (ret)
return ret;
if (state != 1 && state != 0)
return -EINVAL;
/* cancel the running timer */
if (state == 0 && transient_data->activate == 1) {
del_timer(&transient_data->timer);
transient_data->activate = state;
led_set_brightness_nosleep(led_cdev,
transient_data->restore_state);
return size;
}
/* start timer if there is no active timer */
if (state == 1 && transient_data->activate == 0 &&
transient_data->duration != 0) {
transient_data->activate = state;
led_set_brightness_nosleep(led_cdev, transient_data->state);
transient_data->restore_state =
(transient_data->state == LED_FULL) ? LED_OFF : LED_FULL;
mod_timer(&transient_data->timer,
jiffies + msecs_to_jiffies(transient_data->duration));
}
/* state == 0 && transient_data->activate == 0
timer is not active - just return */
/* state == 1 && transient_data->activate == 1
timer is already active - just return */
return size;
}
static ssize_t transient_duration_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct transient_trig_data *transient_data = led_trigger_get_drvdata(dev);
return sprintf(buf, "%lu\n", transient_data->duration);
}
static ssize_t transient_duration_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct transient_trig_data *transient_data =
led_trigger_get_drvdata(dev);
unsigned long state;
ssize_t ret;
ret = kstrtoul(buf, 10, &state);
if (ret)
return ret;
transient_data->duration = state;
return size;
}
static ssize_t transient_state_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct transient_trig_data *transient_data =
led_trigger_get_drvdata(dev);
int state;
state = (transient_data->state == LED_FULL) ? 1 : 0;
return sprintf(buf, "%d\n", state);
}
static ssize_t transient_state_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct transient_trig_data *transient_data =
led_trigger_get_drvdata(dev);
unsigned long state;
ssize_t ret;
ret = kstrtoul(buf, 10, &state);
if (ret)
return ret;
if (state != 1 && state != 0)
return -EINVAL;
transient_data->state = (state == 1) ? LED_FULL : LED_OFF;
return size;
}
static DEVICE_ATTR(activate, 0644, transient_activate_show,
transient_activate_store);
static DEVICE_ATTR(duration, 0644, transient_duration_show,
transient_duration_store);
static DEVICE_ATTR(state, 0644, transient_state_show, transient_state_store);
static struct attribute *transient_trig_attrs[] = {
&dev_attr_activate.attr,
&dev_attr_duration.attr,
&dev_attr_state.attr,
NULL
};
ATTRIBUTE_GROUPS(transient_trig);
static int transient_trig_activate(struct led_classdev *led_cdev)
{
struct transient_trig_data *tdata;
tdata = kzalloc(sizeof(struct transient_trig_data), GFP_KERNEL);
if (!tdata)
return -ENOMEM;
led_set_trigger_data(led_cdev, tdata);
tdata->led_cdev = led_cdev;
timer_setup(&tdata->timer, transient_timer_function, 0);
return 0;
}
static void transient_trig_deactivate(struct led_classdev *led_cdev)
{
struct transient_trig_data *transient_data = led_get_trigger_data(led_cdev);
del_timer_sync(&transient_data->timer);
led_set_brightness_nosleep(led_cdev, transient_data->restore_state);
kfree(transient_data);
}
static struct led_trigger transient_trigger = {
.name = "transient",
.activate = transient_trig_activate,
.deactivate = transient_trig_deactivate,
.groups = transient_trig_groups,
};
module_led_trigger(transient_trigger);
MODULE_AUTHOR("Shuah Khan <shuahkhan@gmail.com>");
MODULE_DESCRIPTION("Transient LED trigger");
MODULE_LICENSE("GPL v2");