summaryrefslogtreecommitdiff
path: root/recipes/slugos-init/files/leds
blob: b40d5d874e236b4fa829a2d1b5cb77e5949c2cf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/sh
# leds
#
# utilities to manipulate the settings of the system leds
#
# load the utility functions unless this script is being called
# just to load its own functions.
case "$1" in
leds)	;;
*)	. /etc/default/rcS
	. /etc/default/functions;;
esac

#
# led_set led-dir off|on|slow|fast|panic|blink|flash|*
#  set the given LED (expressed as a directory) to the 
#  given status.
#
led_set(){
	local setting
	# expect led-dir state
	if test -d "$1"
	then
		setting="$2"
		case "$setting" in
		off|on)		echo -n none

				case "$setting" in
				on)	echo -n 255;;
				off)	echo -n 0;;
				esac >"$1/brightness";;

		slow|fast|panic|blink|flash)
				echo -n timer

				case "$setting" in
				flash)	echo -n 60;;
				blink)	echo -n 540;;
				slow)	echo -n 500;;
				fast)	echo -n 1500;;
				panic)	echo -n 3000;;
				esac >"$1/delay_on"

				case "$setting" in
				flash)	echo -n 540;;
				blink)	echo -n 60;;
				slow)	echo -n 500;;
				fast)	echo -n 1500;;
				panic)	echo -n 3000;;
				esac >"$1/delay_off";;

		*)		echo -n "$setting";;
		esac >"$1/trigger"
	else
		echo "leds: $1: no such directory" >&2
		return 1
	fi
}

#
# sysled [boot] system|user|singleuser|shutdown [error|panic|*]
#  set the system LEDs to indicate the given boot state, the function
#  will temporarily mount sysfs is necessary (using /mnt)
#
# the cases for two LEDs (ready+status)
sysled_readystatus(){
	local ready status
	# expect dir [boot](system|user) [error|panic]
	case "$3" in
	error)	ready=fast;	status=off;;
	panic)	ready=fast;	status=fast;;
	*)	case "$2" in
		bootsystem)	ready=slow;	status=slow;;
		system)		ready=on;	status=on;;
		bootuser)	ready=on;	status=slow;;
		user)		ready=user;	status=off;;
		bootsingleuser)	ready=on;	status=slow;;
		singleuser)	ready=user;	status=user;;
		bootshutdown)	ready=on;	status=slow;;
		shutdown)	ready=slow;	status=on;;
		esac;;
	esac

	led_set "$1/$ready_led_name" "$ready"
	led_set "$1/$status_led_name" "$status"
}
#
# the cases for one LED (just ready)
sysled_ready(){
	local ready
	# expect dir [boot](system|user) [error|panic]
	case "$3" in
	error)	ready=fast;;
	panic)	ready=panic;;
	*)	case "$2" in
		bootsystem)	ready=flash;;
		system)		ready=blink;;
		bootuser)	ready=slow;;
		user)		ready=user;;
		bootsingleuser)	ready=flash;;
		singleuser)	ready=blink;;
		bootshutdown)	ready=slow;;
		shutdown)	ready=blink;;
		esac;;
	esac

	led_set "$1/$ready_led_name" "$ready"
}
#
# the cases for one blue flashing LED (just power)
sysled_power(){
	local power
	# expect dir [boot](system|user) [error|panic]
	case "$3" in
	error)	power=off;;
	panic)	power=off;;
	*)	case "$2" in
		bootsystem)	power=off;; # blinking
		system)		power=off;; # blinking
		bootuser)	power=on;;
		user)		power=on;;
		bootsingleuser)	power=off;; # blinking
		singleuser)	power=off;; # blinking
		bootshutdown)	power=off;; # blinking
		shutdown)	power=off;; # blinking
		esac;;
	esac

	led_set "$1/$power_led_name" "$power"
}
#
sysled(){
	local mp st boot isst
	mp=/sys
	st=1
	boot=

	# validate arguments
	if test "$1" = boot
	then
		shift
		boot=boot
	fi
	case "$1" in
	system|user|singleuser|shutdown) :;;
	*)	echo "sysled: unknown option '$1'" >&2
		echo " usage: sysled [boot] system|user|singleuser|shutdown [error|panic|*]" >&2
		return 1;;
	esac

	if test ! -d "$mp/class/leds" && mount -t sysfs sysfs /mnt
	then
		mp=/mnt
	fi
	#
	# check for the 'ready' LED - otherwise check for a 'power' LED
	if test -d "$mp/class/leds/$ready_led_name"
	then
		if test -d "$mp/class/leds/$status_led_name"
		then
			sysled_readystatus "$mp/class/leds" $boot"$@"
		else
			sysled_ready "$mp/class/leds" $boot"$@"
		fi
	else
		if test -d "$mp/class/leds/$power_led_name"
		then
			sysled_power "$mp/class/leds" $boot"$@"
		fi
	fi
	#
	# clean up
	test "$mp" = /mnt && umount /mnt
	return "$st"
}

#
# beep {arguments}
#  emit a beep
#  does nothing if there is no beep executable, is very
#  quiet in the presence of errors
beep(){
	local arg devices module
	arg=
	test "$1" = beep && shift
	if test -x /bin/beep
	then
		devices=`ls -d /sys/class/input/event* 2>/dev/null`
		for device in $devices
		do
			module=`egrep PHYSDEVDRIVER $device'/uevent' | cut -d '=' -f 2`
			if test "$module" = "ixp4xx-beeper";
			then
				devnode=`echo "$device" | cut -d '/' -f 5`
				arg="-e /dev/input/"$devnode
				break
			fi
		done
		/bin/beep $arg "$@" 2>/dev/null
	fi
	return 0
}

#
# leds_help
#  be helpful
# leds <led> off|on|slow|fast|panic|blink|flash|user|*
# leds [boot] system|user|singleuser|shutdown [error|panic|*]
leds_help(){
	echo "leds: change the setting of the LEDs" >&2
	echo " usage:" >&2
	echo "  leds [boot] system|user|singleuser|shutdown [error|panic|*]" >&2
	echo "    set leds during system boot to indicate a particular boot" >&2
	echo "    state.  'boot' means that the system is transitioning to" >&2
	echo "    the new state.  'error' or 'panic' means a (potentially)" >&2
	echo "    recoverable error or an unrecoverable error ('panic') has" >&2
	echo "    occured." >&2
	echo "  <led> off|on|slow|fast|panic|blink|flash" >&2
	echo "    set the named led to the given display." >&2
	echo "  beep {args}" >&2
	echo "    if possible cause the machine to emit a beep" >&2
}

# Define the LED names based on kernel version.
version=$(uname -r | cut -c -6)
if [ "$version" \< "2.6.25" ]; then
	status_led_name="status"
	ready_led_name="ready"
	power_led_name="power"
else
	case "$(machine)" in
	dsmg600)
		status_led_name="none"
		ready_led_name="none"
		power_led_name="dsmg600:green:power"
		;;
	fsg3)
		status_led_name="none"
		ready_led_name="fsg:blue:sync"
		power_led_name="none"
		;;
	nas100d)
		status_led_name="none"
		ready_led_name="none"
		power_led_name="nas100d:blue:power"
		;;
	nslu2)
		status_led_name="nslu2:red:status"
		ready_led_name="nslu2:green:ready"
		power_led_name="none"
		;;
	esac
fi

# the real command, if required
case "$1" in
boot|system|user|singleuser|shutdown)
	sysled "$@";;

beep)	beep "$@";;

""|-*)	leds_help;;
help)	leds_help;;

leds)	# just load the functions
	;;

*)	led_set /sys/class/leds/"$@"
esac