led 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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
  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. for m in $mode; do
  64. [ -e "/sys/class/leds/${sysfs}/$m" ] && \
  65. echo 1 > /sys/class/leds/${sysfs}/$m
  66. done
  67. echo $interval > /sys/class/leds/${sysfs}/interval
  68. }
  69. ;;
  70. "timer"|"oneshot")
  71. [ -n "$delayon" ] && \
  72. echo $delayon > /sys/class/leds/${sysfs}/delay_on
  73. [ -n "$delayoff" ] && \
  74. echo $delayoff > /sys/class/leds/${sysfs}/delay_off
  75. ;;
  76. "usbport")
  77. local p
  78. for p in $ports; do
  79. echo 1 > /sys/class/leds/${sysfs}/ports/$p
  80. done
  81. ;;
  82. "port_state")
  83. [ -n "$port_state" ] && \
  84. echo $port_state > /sys/class/leds/${sysfs}/port_state
  85. ;;
  86. "gpio")
  87. echo $gpio > /sys/class/leds/${sysfs}/gpio
  88. echo $inverted > /sys/class/leds/${sysfs}/inverted
  89. ;;
  90. switch[0-9]*)
  91. local port_mask speed_mask
  92. config_get port_mask $1 port_mask
  93. [ -n "$port_mask" ] && \
  94. echo $port_mask > /sys/class/leds/${sysfs}/port_mask
  95. config_get speed_mask $1 speed_mask
  96. [ -n "$speed_mask" ] && \
  97. echo $speed_mask > /sys/class/leds/${sysfs}/speed_mask
  98. [ -n "$mode" ] && \
  99. echo "$mode" > /sys/class/leds/${sysfs}/mode
  100. ;;
  101. esac
  102. }
  103. }
  104. start() {
  105. [ -e /sys/class/leds/ ] && {
  106. [ -s /var/run/led.state ] && {
  107. local led trigger brightness
  108. while read led trigger brightness; do
  109. [ -e "/sys/class/leds/$led/trigger" ] && \
  110. echo "$trigger" > "/sys/class/leds/$led/trigger"
  111. [ -e "/sys/class/leds/$led/brightness" ] && \
  112. echo "$brightness" > "/sys/class/leds/$led/brightness"
  113. done < /var/run/led.state
  114. rm /var/run/led.state
  115. }
  116. config_load system
  117. config_foreach load_led led
  118. }
  119. }