led 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "0"
  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_bool inverted $1 inverted "0"
  30. # execute application led trigger
  31. [ -f "/usr/libexec/led-trigger/${trigger}" ] && {
  32. . "/usr/libexec/led-trigger/${trigger}"
  33. return 0
  34. }
  35. [ "$trigger" = "usbdev" ] && {
  36. # Backward compatibility: translate to the new trigger
  37. trigger="usbport"
  38. # Translate port of root hub, e.g. 4-1 -> usb4-port1
  39. ports=$(echo "$dev" | sed -n 's/^\([0-9]*\)-\([0-9]*\)$/usb\1-port\2/p')
  40. # Translate port of extra hub, e.g. 2-2.4 -> 2-2-port4
  41. [ -z "$ports" ] && ports=$(echo "$dev" | sed -n 's/\./-port/p')
  42. }
  43. [ -e /sys/class/leds/${sysfs}/brightness ] && {
  44. echo "setting up led ${name}"
  45. printf "%s %s %d\n" \
  46. "$sysfs" \
  47. "$(sed -ne 's/^.*\[\(.*\)\].*$/\1/p' /sys/class/leds/${sysfs}/trigger)" \
  48. "$(cat /sys/class/leds/${sysfs}/brightness)" \
  49. >> /var/run/led.state
  50. [ "$default" = 0 ] &&
  51. echo 0 >/sys/class/leds/${sysfs}/brightness
  52. echo $trigger > /sys/class/leds/${sysfs}/trigger 2> /dev/null
  53. ret="$?"
  54. [ $default = 1 ] &&
  55. cat /sys/class/leds/${sysfs}/max_brightness > /sys/class/leds/${sysfs}/brightness
  56. [ $ret = 0 ] || {
  57. echo >&2 "Skipping trigger '$trigger' for led '$name' due to missing kernel module"
  58. return 1
  59. }
  60. case "$trigger" in
  61. "heartbeat")
  62. echo "${inverted}" > "/sys/class/leds/${sysfs}/invert"
  63. ;;
  64. "netdev")
  65. [ -n "$dev" ] && {
  66. echo $dev > /sys/class/leds/${sysfs}/device_name
  67. for m in $mode; do
  68. [ -e "/sys/class/leds/${sysfs}/$m" ] && \
  69. echo 1 > /sys/class/leds/${sysfs}/$m
  70. done
  71. echo $interval > /sys/class/leds/${sysfs}/interval
  72. }
  73. ;;
  74. "timer"|"oneshot")
  75. [ -n "$delayon" ] && \
  76. echo $delayon > /sys/class/leds/${sysfs}/delay_on
  77. [ -n "$delayoff" ] && \
  78. echo $delayoff > /sys/class/leds/${sysfs}/delay_off
  79. ;;
  80. "usbport")
  81. local p
  82. for p in $ports; do
  83. echo 1 > /sys/class/leds/${sysfs}/ports/$p
  84. done
  85. ;;
  86. "port_state")
  87. [ -n "$port_state" ] && \
  88. echo $port_state > /sys/class/leds/${sysfs}/port_state
  89. ;;
  90. "gpio")
  91. echo $gpio > /sys/class/leds/${sysfs}/gpio
  92. echo $inverted > /sys/class/leds/${sysfs}/inverted
  93. ;;
  94. switch[0-9]*)
  95. local port_mask speed_mask
  96. config_get port_mask $1 port_mask
  97. [ -n "$port_mask" ] && \
  98. echo $port_mask > /sys/class/leds/${sysfs}/port_mask
  99. config_get speed_mask $1 speed_mask
  100. [ -n "$speed_mask" ] && \
  101. echo $speed_mask > /sys/class/leds/${sysfs}/speed_mask
  102. [ -n "$mode" ] && \
  103. echo "$mode" > /sys/class/leds/${sysfs}/mode
  104. ;;
  105. esac
  106. }
  107. }
  108. start() {
  109. [ -e /sys/class/leds/ ] && {
  110. [ -s /var/run/led.state ] && {
  111. local led trigger brightness
  112. while read led trigger brightness; do
  113. [ -e "/sys/class/leds/$led/trigger" ] && \
  114. echo "$trigger" > "/sys/class/leds/$led/trigger"
  115. [ -e "/sys/class/leds/$led/brightness" ] && \
  116. echo "$brightness" > "/sys/class/leds/$led/brightness"
  117. done < /var/run/led.state
  118. rm /var/run/led.state
  119. }
  120. config_load system
  121. config_foreach load_led led
  122. }
  123. }