c5ce2997cc44b5a463fada4b6c969b288e6e2d4e
[vuplus_openembedded] / packages / slugos-init / files / leds
1 #!/bin/sh
2 # leds
3 #
4 # utilities to manipulate the settings of the system leds
5 #
6 # load the utility functions unless this script is being called
7 # just to load its own functions.
8 case "$1" in
9 leds)   ;;
10 *)      . /etc/default/rcS
11         . /etc/default/functions;;
12 esac
13
14 #
15 # led_set led-dir off|on|slow|fast|panic|blink|flash|*
16 #  set the given LED (expressed as a directory) to the 
17 #  given status.
18 #
19 led_set(){
20         local setting
21         # expect led-dir state
22         if test -d "$1"
23         then
24                 setting="$2"
25                 case "$setting" in
26                 off|on)         echo -n none
27
28                                 case "$setting" in
29                                 on)     echo -n 255;;
30                                 off)    echo -n 0;;
31                                 esac >"$1/brightness";;
32
33                 slow|fast|panic|blink|flash)
34                                 echo -n timer
35
36                                 case "$setting" in
37                                 flash)  echo -n 60;;
38                                 blink)  echo -n 540;;
39                                 slow)   echo -n 500;;
40                                 fast)   echo -n 1500;;
41                                 panic)  echo -n 3000;;
42                                 esac >"$1/delay_on"
43
44                                 case "$setting" in
45                                 flash)  echo -n 540;;
46                                 blink)  echo -n 60;;
47                                 slow)   echo -n 500;;
48                                 fast)   echo -n 1500;;
49                                 panic)  echo -n 3000;;
50                                 esac >"$1/delay_off";;
51
52                 *)              echo -n "$setting";;
53                 esac >"$1/trigger"
54         else
55                 echo "leds: $1: no such directory" >&2
56                 return 1
57         fi
58 }
59
60 #
61 # sysled [boot] system|user|singleuser|shutdown [error|panic|*]
62 #  set the system LEDs to indicate the given boot state, the function
63 #  will temporarily mount sysfs is necessary (using /mnt)
64 #
65 # the cases for two LEDs (ready+status)
66 sysled_readystatus(){
67         local ready status
68         # expect dir [boot](system|user) [error|panic]
69         case "$3" in
70         error)  ready=fast;     status=off;;
71         panic)  ready=fast;     status=fast;;
72         *)      case "$2" in
73                 bootsystem)     ready=slow;     status=slow;;
74                 system)         ready=on;       status=on;;
75                 bootuser)       ready=on;       status=slow;;
76                 user)           ready=user;     status=off;;
77                 bootsingleuser) ready=on;       status=slow;;
78                 singleuser)     ready=user;     status=user;;
79                 bootshutdown)   ready=on;       status=slow;;
80                 shutdown)       ready=slow;     status=on;;
81                 esac;;
82         esac
83
84         led_set "$1/$ready_led_name" "$ready"
85         led_set "$1/$status_led_name" "$status"
86 }
87 #
88 # the cases for one LED (just ready)
89 sysled_ready(){
90         local ready
91         # expect dir [boot](system|user) [error|panic]
92         case "$3" in
93         error)  ready=fast;;
94         panic)  ready=panic;;
95         *)      case "$2" in
96                 bootsystem)     ready=flash;;
97                 system)         ready=blink;;
98                 bootuser)       ready=slow;;
99                 user)           ready=user;;
100                 bootsingleuser) ready=flash;;
101                 singleuser)     ready=blink;;
102                 bootshutdown)   ready=slow;;
103                 shutdown)       ready=blink;;
104                 esac;;
105         esac
106
107         led_set "$1/$ready_led_name" "$ready"
108 }
109 #
110 # the cases for one blue flashing LED (just power)
111 sysled_power(){
112         local power
113         # expect dir [boot](system|user) [error|panic]
114         case "$3" in
115         error)  power=off;;
116         panic)  power=off;;
117         *)      case "$2" in
118                 bootsystem)     power=off;; # blinking
119                 system)         power=off;; # blinking
120                 bootuser)       power=on;;
121                 user)           power=on;;
122                 bootsingleuser) power=off;; # blinking
123                 singleuser)     power=off;; # blinking
124                 bootshutdown)   power=off;; # blinking
125                 shutdown)       power=off;; # blinking
126                 esac;;
127         esac
128
129         led_set "$1/$power_led_name" "$power"
130 }
131 #
132 sysled(){
133         local mp st boot isst
134         mp=/sys
135         st=1
136         boot=
137
138         # validate arguments
139         if test "$1" = boot
140         then
141                 shift
142                 boot=boot
143         fi
144         case "$1" in
145         system|user|singleuser|shutdown) :;;
146         *)      echo "sysled: unknown option '$1'" >&2
147                 echo " usage: sysled [boot] system|user|singleuser|shutdown [error|panic|*]" >&2
148                 return 1;;
149         esac
150
151         if test ! -d "$mp/class/leds" && mount -t sysfs sysfs /mnt
152         then
153                 mp=/mnt
154         fi
155         #
156         # check for the 'ready' LED - otherwise check for a 'power' LED
157         if test -d "$mp/class/leds/$ready_led_name"
158         then
159                 if test -d "$mp/class/leds/$status_led_name"
160                 then
161                         sysled_readystatus "$mp/class/leds" $boot"$@"
162                 else
163                         sysled_ready "$mp/class/leds" $boot"$@"
164                 fi
165         else
166                 if test -d "$mp/class/leds/$power_led_name"
167                 then
168                         sysled_power "$mp/class/leds" $boot"$@"
169                 fi
170         fi
171         #
172         # clean up
173         test "$mp" = /mnt && umount /mnt
174         return "$st"
175 }
176
177 #
178 # beep {arguments}
179 #  emit a beep
180 #  does nothing if there is no beep executable, is very
181 #  quiet in the presence of errors
182 beep(){
183         local arg devices module
184         arg=
185         test "$1" = beep && shift
186         if test -x /bin/beep
187         then
188                 devices=`ls -d /sys/class/input/event*`
189                 for device in $devices
190                 do
191                         module=`egrep PHYSDEVDRIVER $device'/uevent' | cut -d '=' -f 2`
192                         if test "$module" = "ixp4xx-beeper";
193                         then
194                                 devnode=`echo "$device" | cut -d '/' -f 5`
195                                 arg="-e /dev/input/"$devnode
196                                 break
197                         fi
198                 done
199                 /bin/beep $arg "$@" 2>/dev/null
200         fi
201         return 0
202 }
203
204 #
205 # leds_help
206 #  be helpful
207 # leds <led> off|on|slow|fast|panic|blink|flash|user|*
208 # leds [boot] system|user|singleuser|shutdown [error|panic|*]
209 leds_help(){
210         echo "leds: change the setting of the LEDs" >&2
211         echo " usage:" >&2
212         echo "  leds [boot] system|user|singleuser|shutdown [error|panic|*]" >&2
213         echo "    set leds during system boot to indicate a particular boot" >&2
214         echo "    state.  'boot' means that the system is transitioning to" >&2
215         echo "    the new state.  'error' or 'panic' means a (potentially)" >&2
216         echo "    recoverable error or an unrecoverable error ('panic') has" >&2
217         echo "    occured." >&2
218         echo "  <led> off|on|slow|fast|panic|blink|flash" >&2
219         echo "    set the named led to the given display." >&2
220         echo "  beep {args}" >&2
221         echo "    if possible cause the machine to emit a beep" >&2
222 }
223
224 # Define the LED names based on kernel version.
225 version=$(uname -r | cut -c -6)
226 if [ "$version" \< "2.6.25" ]; then
227         status_led_name="status"
228         ready_led_name="ready"
229         power_led_name="power"
230 else
231         case "$(machine)" in
232         dsmg600)
233                 status_led_name="none"
234                 ready_led_name="none"
235                 power_led_name="dsmg600:green:power"
236                 ;;
237         fsg3)
238                 status_led_name="none"
239                 ready_led_name="fsg:blue:sync"
240                 power_led_name="none"
241                 ;;
242         nas100d)
243                 status_led_name="none"
244                 ready_led_name="none"
245                 power_led_name="nas100d:blue:power"
246                 ;;
247         nslu2)
248                 status_led_name="nslu2:red:status"
249                 ready_led_name="nslu2:green:ready"
250                 power_led_name="none"
251                 ;;
252         esac
253 fi
254
255 # the real command, if required
256 case "$1" in
257 boot|system|user|singleuser|shutdown)
258         sysled "$@";;
259
260 beep)   beep "$@";;
261
262 ""|-*)  leds_help;;
263 help)   leds_help;;
264
265 leds)   # just load the functions
266         ;;
267
268 *)      led_set /sys/class/leds/"$@"
269 esac