2008-08-04 22:56:46 +00:00
|
|
|
#!/bin/sh /etc/rc.common
|
2014-06-24 18:18:50 +00:00
|
|
|
# Copyright (C) 2008 OpenWrt.org
|
2008-08-04 22:56:46 +00:00
|
|
|
|
2010-01-06 02:34:20 +00:00
|
|
|
START=96
|
2008-08-04 22:56:46 +00:00
|
|
|
|
2024-02-01 11:51:02 +00:00
|
|
|
led_color_set() {
|
|
|
|
local cfg="$1"
|
|
|
|
local sysfs="$2"
|
|
|
|
|
|
|
|
local max_b
|
|
|
|
local colors
|
|
|
|
local color
|
|
|
|
local multi_intensity
|
|
|
|
local value
|
|
|
|
local write
|
|
|
|
|
|
|
|
[ -e /sys/class/leds/${sysfs}/multi_intensity ] || return
|
|
|
|
[ -e /sys/class/leds/${sysfs}/multi_index ] || return
|
|
|
|
|
|
|
|
max_b="$(cat /sys/class/leds/${sysfs}/max_brightness)"
|
|
|
|
colors="$(cat /sys/class/leds/${sysfs}/multi_index | tr " " "\n")"
|
|
|
|
multi_intensity=""
|
|
|
|
for color in $colors; do
|
|
|
|
config_get value $1 "color_${color}" "0"
|
|
|
|
[ "$value" -gt 0 ] && write=1
|
|
|
|
[ "$value" -gt "$max_b" ] && value="$max_b"
|
|
|
|
multi_intensity="${multi_intensity}${value} "
|
|
|
|
done
|
|
|
|
|
|
|
|
# Check if any color is configured
|
|
|
|
[ "$write" = 1 ] || return
|
|
|
|
# Remove last whitespace
|
|
|
|
multi_intensity="${multi_intensity:0:-1}"
|
|
|
|
|
|
|
|
echo "setting '${name}' led color to '${multi_intensity}'"
|
|
|
|
echo "${multi_intensity}" > /sys/class/leds/${sysfs}/multi_intensity
|
|
|
|
}
|
|
|
|
|
2008-08-04 22:56:46 +00:00
|
|
|
load_led() {
|
|
|
|
local name
|
|
|
|
local sysfs
|
|
|
|
local trigger
|
|
|
|
local dev
|
2016-10-19 10:09:45 +00:00
|
|
|
local ports
|
2008-08-04 22:56:46 +00:00
|
|
|
local mode
|
|
|
|
local default
|
2008-08-07 11:01:46 +00:00
|
|
|
local delayon
|
|
|
|
local delayoff
|
2010-12-17 17:10:14 +00:00
|
|
|
local interval
|
2008-08-04 22:56:46 +00:00
|
|
|
|
|
|
|
config_get sysfs $1 sysfs
|
2009-08-07 00:07:56 +00:00
|
|
|
config_get name $1 name "$sysfs"
|
|
|
|
config_get trigger $1 trigger "none"
|
2008-08-04 22:56:46 +00:00
|
|
|
config_get dev $1 dev
|
2016-10-19 11:05:53 +00:00
|
|
|
config_get ports $1 port
|
2018-09-21 22:38:40 +00:00
|
|
|
config_get mode $1 mode
|
2020-07-26 20:37:53 +00:00
|
|
|
config_get_bool default $1 default "0"
|
2008-08-07 11:01:46 +00:00
|
|
|
config_get delayon $1 delayon
|
|
|
|
config_get delayoff $1 delayoff
|
2010-12-17 17:10:14 +00:00
|
|
|
config_get interval $1 interval "50"
|
2011-01-27 21:49:46 +00:00
|
|
|
config_get port_state $1 port_state
|
2012-11-25 17:20:49 +00:00
|
|
|
config_get delay $1 delay "150"
|
|
|
|
config_get message $1 message ""
|
2015-07-14 07:35:59 +00:00
|
|
|
config_get gpio $1 gpio "0"
|
2022-07-31 08:19:09 +00:00
|
|
|
config_get_bool inverted $1 inverted "0"
|
2008-08-04 22:56:46 +00:00
|
|
|
|
2024-10-03 15:25:36 +00:00
|
|
|
[ "$2" ] && [ "$sysfs" != "$2" ] && return
|
|
|
|
|
2020-02-05 08:23:27 +00:00
|
|
|
# execute application led trigger
|
|
|
|
[ -f "/usr/libexec/led-trigger/${trigger}" ] && {
|
|
|
|
. "/usr/libexec/led-trigger/${trigger}"
|
|
|
|
return 0
|
|
|
|
}
|
2012-10-11 06:45:02 +00:00
|
|
|
|
2016-10-19 10:09:45 +00:00
|
|
|
[ "$trigger" = "usbdev" ] && {
|
|
|
|
# Backward compatibility: translate to the new trigger
|
|
|
|
trigger="usbport"
|
|
|
|
# Translate port of root hub, e.g. 4-1 -> usb4-port1
|
|
|
|
ports=$(echo "$dev" | sed -n 's/^\([0-9]*\)-\([0-9]*\)$/usb\1-port\2/p')
|
|
|
|
# Translate port of extra hub, e.g. 2-2.4 -> 2-2-port4
|
|
|
|
[ -z "$ports" ] && ports=$(echo "$dev" | sed -n 's/\./-port/p')
|
|
|
|
}
|
|
|
|
|
2008-09-13 00:08:39 +00:00
|
|
|
[ -e /sys/class/leds/${sysfs}/brightness ] && {
|
2009-08-07 00:07:56 +00:00
|
|
|
echo "setting up led ${name}"
|
2015-04-16 20:09:30 +00:00
|
|
|
|
2024-02-01 10:17:17 +00:00
|
|
|
printf "%s %s %d" \
|
2016-06-08 08:14:27 +00:00
|
|
|
"$sysfs" \
|
|
|
|
"$(sed -ne 's/^.*\[\(.*\)\].*$/\1/p' /sys/class/leds/${sysfs}/trigger)" \
|
|
|
|
"$(cat /sys/class/leds/${sysfs}/brightness)" \
|
|
|
|
>> /var/run/led.state
|
2024-02-01 10:17:17 +00:00
|
|
|
# Save default color if supported
|
|
|
|
[ -e /sys/class/leds/${sysfs}/multi_intensity ] && {
|
|
|
|
printf " %s" \
|
|
|
|
"$(sed 's/\ /:/g' /sys/class/leds/${sysfs}/multi_intensity)" \
|
|
|
|
>> /var/run/led.state
|
|
|
|
}
|
|
|
|
printf "\n" >> /var/run/led.state
|
2016-06-08 08:14:27 +00:00
|
|
|
|
2015-05-03 18:04:27 +00:00
|
|
|
[ "$default" = 0 ] &&
|
2015-04-16 20:09:30 +00:00
|
|
|
echo 0 >/sys/class/leds/${sysfs}/brightness
|
|
|
|
|
|
|
|
[ $default = 1 ] &&
|
2015-12-11 15:06:11 +00:00
|
|
|
cat /sys/class/leds/${sysfs}/max_brightness > /sys/class/leds/${sysfs}/brightness
|
2015-04-16 20:09:30 +00:00
|
|
|
|
2024-02-01 11:51:02 +00:00
|
|
|
led_color_set "$1" "$sysfs"
|
|
|
|
|
2024-02-01 10:31:20 +00:00
|
|
|
echo $trigger > /sys/class/leds/${sysfs}/trigger 2> /dev/null
|
|
|
|
ret="$?"
|
2015-04-16 20:09:30 +00:00
|
|
|
[ $ret = 0 ] || {
|
2015-01-23 12:33:04 +00:00
|
|
|
echo >&2 "Skipping trigger '$trigger' for led '$name' due to missing kernel module"
|
|
|
|
return 1
|
|
|
|
}
|
2011-12-31 15:02:32 +00:00
|
|
|
case "$trigger" in
|
2022-07-31 08:11:58 +00:00
|
|
|
"heartbeat")
|
|
|
|
echo "${inverted}" > "/sys/class/leds/${sysfs}/invert"
|
|
|
|
;;
|
|
|
|
|
2011-12-31 15:02:32 +00:00
|
|
|
"netdev")
|
|
|
|
[ -n "$dev" ] && {
|
|
|
|
echo $dev > /sys/class/leds/${sysfs}/device_name
|
2018-12-16 15:51:26 +00:00
|
|
|
for m in $mode; do
|
|
|
|
[ -e "/sys/class/leds/${sysfs}/$m" ] && \
|
|
|
|
echo 1 > /sys/class/leds/${sysfs}/$m
|
|
|
|
done
|
2024-10-03 15:25:36 +00:00
|
|
|
echo $interval > /sys/class/leds/${sysfs}/interval 2>/dev/null
|
2011-12-31 15:02:32 +00:00
|
|
|
}
|
|
|
|
;;
|
|
|
|
|
2016-08-26 11:17:49 +00:00
|
|
|
"timer"|"oneshot")
|
2011-12-31 15:02:32 +00:00
|
|
|
[ -n "$delayon" ] && \
|
|
|
|
echo $delayon > /sys/class/leds/${sysfs}/delay_on
|
|
|
|
[ -n "$delayoff" ] && \
|
|
|
|
echo $delayoff > /sys/class/leds/${sysfs}/delay_off
|
|
|
|
;;
|
|
|
|
|
2016-10-19 10:09:45 +00:00
|
|
|
"usbport")
|
|
|
|
local p
|
|
|
|
|
|
|
|
for p in $ports; do
|
|
|
|
echo 1 > /sys/class/leds/${sysfs}/ports/$p
|
|
|
|
done
|
2011-12-31 15:02:32 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
"port_state")
|
|
|
|
[ -n "$port_state" ] && \
|
|
|
|
echo $port_state > /sys/class/leds/${sysfs}/port_state
|
|
|
|
;;
|
2011-12-31 15:02:35 +00:00
|
|
|
|
2015-07-14 07:35:59 +00:00
|
|
|
"gpio")
|
|
|
|
echo $gpio > /sys/class/leds/${sysfs}/gpio
|
|
|
|
echo $inverted > /sys/class/leds/${sysfs}/inverted
|
|
|
|
;;
|
|
|
|
|
2011-12-31 15:02:35 +00:00
|
|
|
switch[0-9]*)
|
2016-02-25 13:31:29 +00:00
|
|
|
local port_mask speed_mask
|
2011-12-31 15:02:35 +00:00
|
|
|
|
|
|
|
config_get port_mask $1 port_mask
|
|
|
|
[ -n "$port_mask" ] && \
|
|
|
|
echo $port_mask > /sys/class/leds/${sysfs}/port_mask
|
2016-02-25 13:31:29 +00:00
|
|
|
config_get speed_mask $1 speed_mask
|
|
|
|
[ -n "$speed_mask" ] && \
|
|
|
|
echo $speed_mask > /sys/class/leds/${sysfs}/speed_mask
|
2018-09-21 22:38:40 +00:00
|
|
|
[ -n "$mode" ] && \
|
|
|
|
echo "$mode" > /sys/class/leds/${sysfs}/mode
|
2011-12-31 15:02:35 +00:00
|
|
|
;;
|
2011-12-31 15:02:32 +00:00
|
|
|
esac
|
2008-08-04 22:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
[ -e /sys/class/leds/ ] && {
|
2016-06-08 08:14:27 +00:00
|
|
|
[ -s /var/run/led.state ] && {
|
2024-02-01 10:17:17 +00:00
|
|
|
local led trigger brightness color
|
|
|
|
while read led trigger brightness color; do
|
2024-10-03 15:25:36 +00:00
|
|
|
[ "$1" ] && [ "$1" != "$led" ] && continue
|
2016-06-08 08:14:27 +00:00
|
|
|
[ -e "/sys/class/leds/$led/trigger" ] && \
|
|
|
|
echo "$trigger" > "/sys/class/leds/$led/trigger"
|
|
|
|
|
|
|
|
[ -e "/sys/class/leds/$led/brightness" ] && \
|
|
|
|
echo "$brightness" > "/sys/class/leds/$led/brightness"
|
2024-02-01 10:17:17 +00:00
|
|
|
|
|
|
|
[ -e "/sys/class/leds/$led/multi_intensity" ] && \
|
|
|
|
echo "$color" | sed 's/:/\ /g' > \
|
|
|
|
"/sys/class/leds/$led/multi_intensity"
|
2016-06-08 08:14:27 +00:00
|
|
|
done < /var/run/led.state
|
2024-10-05 17:20:35 +00:00
|
|
|
if [ "$1" ]; then
|
|
|
|
grep -v "^$1 " /var/run/led.state > /var/run/led.state.new
|
|
|
|
mv /var/run/led.state.new /var/run/led.state
|
|
|
|
else
|
|
|
|
rm /var/run/led.state
|
|
|
|
fi
|
2016-06-08 08:14:27 +00:00
|
|
|
}
|
2016-06-07 20:58:22 +00:00
|
|
|
|
2008-08-04 22:56:46 +00:00
|
|
|
config_load system
|
2024-10-03 15:25:36 +00:00
|
|
|
config_foreach load_led led "$1"
|
2008-08-04 22:56:46 +00:00
|
|
|
}
|
|
|
|
}
|