2
0

led 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2008 OpenWrt.org
  3. START=96
  4. load_led() {
  5. local name
  6. local sysfs
  7. local trigger
  8. local dev
  9. local ports
  10. local mode
  11. local default
  12. local delayon
  13. local delayoff
  14. local interval
  15. config_get sysfs $1 sysfs
  16. config_get name $1 name "$sysfs"
  17. config_get trigger $1 trigger "none"
  18. config_get dev $1 dev
  19. config_get ports $1 port
  20. config_get mode $1 mode "link"
  21. config_get_bool default $1 default "nil"
  22. config_get delayon $1 delayon
  23. config_get delayoff $1 delayoff
  24. config_get interval $1 interval "50"
  25. config_get port_state $1 port_state
  26. config_get delay $1 delay "150"
  27. config_get message $1 message ""
  28. config_get gpio $1 gpio "0"
  29. config_get inverted $1 inverted "0"
  30. if [ "$trigger" = "rssi" ]; then
  31. # handled by rssileds userspace process
  32. return
  33. fi
  34. [ "$trigger" = "usbdev" ] && {
  35. # Backward compatibility: translate to the new trigger
  36. trigger="usbport"
  37. # Translate port of root hub, e.g. 4-1 -> usb4-port1
  38. ports=$(echo "$dev" | sed -n 's/^\([0-9]*\)-\([0-9]*\)$/usb\1-port\2/p')
  39. # Translate port of extra hub, e.g. 2-2.4 -> 2-2-port4
  40. [ -z "$ports" ] && ports=$(echo "$dev" | sed -n 's/\./-port/p')
  41. }
  42. [ -e /sys/class/leds/${sysfs}/brightness ] && {
  43. echo "setting up led ${name}"
  44. printf "%s %s %d\n" \
  45. "$sysfs" \
  46. "$(sed -ne 's/^.*\[\(.*\)\].*$/\1/p' /sys/class/leds/${sysfs}/trigger)" \
  47. "$(cat /sys/class/leds/${sysfs}/brightness)" \
  48. >> /var/run/led.state
  49. [ "$default" = 0 ] &&
  50. echo 0 >/sys/class/leds/${sysfs}/brightness
  51. echo $trigger > /sys/class/leds/${sysfs}/trigger 2> /dev/null
  52. ret="$?"
  53. [ $default = 1 ] &&
  54. cat /sys/class/leds/${sysfs}/max_brightness > /sys/class/leds/${sysfs}/brightness
  55. [ $ret = 0 ] || {
  56. echo >&2 "Skipping trigger '$trigger' for led '$name' due to missing kernel module"
  57. return 1
  58. }
  59. case "$trigger" in
  60. "netdev")
  61. [ -n "$dev" ] && {
  62. echo $dev > /sys/class/leds/${sysfs}/device_name
  63. echo $mode > /sys/class/leds/${sysfs}/mode
  64. }
  65. ;;
  66. "timer"|"oneshot")
  67. [ -n "$delayon" ] && \
  68. echo $delayon > /sys/class/leds/${sysfs}/delay_on
  69. [ -n "$delayoff" ] && \
  70. echo $delayoff > /sys/class/leds/${sysfs}/delay_off
  71. ;;
  72. "usbport")
  73. local p
  74. for p in $ports; do
  75. echo 1 > /sys/class/leds/${sysfs}/ports/$p
  76. done
  77. ;;
  78. "port_state")
  79. [ -n "$port_state" ] && \
  80. echo $port_state > /sys/class/leds/${sysfs}/port_state
  81. ;;
  82. "gpio")
  83. echo $gpio > /sys/class/leds/${sysfs}/gpio
  84. echo $inverted > /sys/class/leds/${sysfs}/inverted
  85. ;;
  86. switch[0-9]*)
  87. local port_mask speed_mask
  88. config_get port_mask $1 port_mask
  89. [ -n "$port_mask" ] && \
  90. echo $port_mask > /sys/class/leds/${sysfs}/port_mask
  91. config_get speed_mask $1 speed_mask
  92. [ -n "$speed_mask" ] && \
  93. echo $speed_mask > /sys/class/leds/${sysfs}/speed_mask
  94. ;;
  95. esac
  96. }
  97. }
  98. start() {
  99. [ -e /sys/class/leds/ ] && {
  100. [ -s /var/run/led.state ] && {
  101. local led trigger brightness
  102. while read led trigger brightness; do
  103. [ -e "/sys/class/leds/$led/trigger" ] && \
  104. echo "$trigger" > "/sys/class/leds/$led/trigger"
  105. [ -e "/sys/class/leds/$led/brightness" ] && \
  106. echo "$brightness" > "/sys/class/leds/$led/brightness"
  107. done < /var/run/led.state
  108. rm /var/run/led.state
  109. }
  110. config_load system
  111. config_foreach load_led led
  112. }
  113. }