pandora-scripts: dim the LED on idle too
[openpandora.oe.git] / recipes / pandora-system / pandora-scripts / op_power.sh
1 #!/bin/bash
2
3 . /usr/pandora/scripts/op_common.sh
4 xfceuser=$(get_user)
5 xfceuser_home=$(get_user_home)
6
7 # XXX: better use lockfile (or something), but it's not in current firmware
8 test -e /tmp/op_power.lock && exit 2
9 touch /tmp/op_power.lock
10
11 led_high="$(cat /etc/pandora/conf/led.conf | grep HighPowerLED: | awk -F\: '{print $2}')"
12 led_low="$(cat /etc/pandora/conf/led.conf | grep LowPowerLED: | awk -F\: '{print $2}')"
13 led_idle="$(cat /etc/pandora/conf/led.conf | grep IdleLED: | awk -F\: '{print $2}')"
14 test -z "$led_high" && led_high=255
15 test -z "$led_low" && led_low=16
16 test -z "$led_idle" && led_idle=48
17
18 debug(){
19         return 1 # 0 when debugging, 1 when not
20 }
21
22 test -e $xfceuser_home/.lidconfig && lidconfig=$(cat $xfceuser_home/.lidconfig) # read lid conf. file if it exists
23
24 if [ -e /tmp/powerstate ]; then 
25         powerstate="$(cat /tmp/powerstate)"
26 else
27         powerstate="on"
28 fi
29
30 debug && echo "powerstate=$powerstate"
31
32 suspend_net() {
33         # This is kernel 3.x only,
34         # but ignored since 2.6 only has lowpower anyways (no suspend)
35         if (ps -A | grep -q hciattac[h]); then
36                 touch /tmp/bt-state-up
37                 /usr/pandora/scripts/op_bluetooth_work.sh 0
38         else
39                 rm -f /tmp/bt-state-up
40         fi
41
42         wlstate=$(lsmod | grep -m1 wl1251)
43         if [ -z "$wlstate" ]; then
44                 echo "down" > /tmp/wlstate
45         else
46                 echo "up" > /tmp/wlstate
47                 ifconfig wlan0 down
48                 rmmod board_omap3pandora_wifi 2> /dev/null
49                 rmmod wl1251_sdio wl1251
50         fi
51 }
52
53 resume_net() {
54         if [ -f /tmp/bt-state-up ]; then
55                 /usr/pandora/scripts/op_bluetooth_work.sh 1
56                 hciconfig hci0 up pscan
57         fi
58         wlstate=$(cat /tmp/wlstate)
59         if [ "$wlstate" = "up" ]; then
60                 /etc/init.d/wl1251-init start
61         fi
62         rm -f /tmp/hcistate /tmp/wlstate
63 }
64
65 display_on() {
66         echo 0 > /sys/class/graphics/fb0/blank
67
68         # only bother restoring brightness if it's 0
69         # (old kernel or user messed it up somehow)
70         brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
71         if [ $brightness -gt 0 ]; then
72                 return 0
73         fi
74
75         maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
76         oldbright=0
77         if [ -f /tmp/oldbright ]; then
78                 oldbright=$(cat /tmp/oldbright)
79         fi
80         if [ $oldbright -eq 0 ]; then
81                 oldbright=$(cat /etc/pandora/conf/brightness.state)
82         fi
83         if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then 
84                 /usr/pandora/scripts/op_bright.sh $oldbright 
85         else
86                 /usr/pandora/scripts/op_bright.sh $maxbright
87         fi
88 }
89
90 display_off() {
91         brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
92         if [ $brightness -gt 0 ]; then
93                 echo $brightness > /tmp/oldbright
94         fi
95         kernel_major=`uname -r | cut -c 1`
96         if [ "$kernel_major" = "2" ]; then
97                 echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
98         fi
99
100         echo 1 > /sys/class/graphics/fb0/blank
101 }
102
103 lowPowerOn(){ #switch from normal to lowpower mode
104         display_off
105
106         pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
107         for PID in $pidlist
108         do
109                 kill -STOP $PID
110         done
111
112         suspend_net
113
114         cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
115         /usr/pandora/scripts/op_cpuspeed.sh -n 125
116 }
117
118 lowPowerOff(){ # switch from lowpower to normal mode
119         oldspeed=$(cat /tmp/oldspeed)
120         /usr/pandora/scripts/op_cpuspeed.sh -n $oldspeed
121         rm -f /tmp/oldspeed
122
123         display_on
124         resume_net
125
126         pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
127         for PID in $pidlist
128         do
129                 kill -CONT $PID
130         done
131         echo $led_high > /sys/class/leds/pandora\:\:power/brightness #power LED bright
132 }
133
134 display_on_with_checks() {
135         # after turning on the display, we don't want lowpower state
136         # (which could be active because of some races)
137         if [ "$powerstate" = "buttonlowpower" -o "$powerstate" = "lidlowpower" -o \
138              -e /tmp/oldspeed ]
139         then
140                 lowPowerOff
141         else
142                 display_on
143                 echo $led_high > /sys/class/leds/pandora\:\:power/brightness
144         fi
145 }
146
147 show_message() {
148         # TODO: check if desktop is visible; maybe use layer3?
149         cmd="DISPLAY=:0.0 zenity --info --text \"$1\" --timeout 10"
150         su -c "$cmd" $xfceuser
151 }
152
153 suspend_real() {
154         delay=0
155
156         if ! [ -e /sys/power/state ]; then
157                 # no kernel suspend support
158                 return 1
159         fi
160
161         current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
162  
163         if [ $current_now -gt 0 ]; then 
164                 return 1 
165                 #don't suspend while unit is charging
166         fi
167
168         # can't suspend while SGX is in use due to bugs
169         # (prevents low power states and potential lockup)
170         if lsof -t /dev/pvrsrvkm > /dev/null; then
171                 return 1
172         fi
173
174         if ! grep -q 'mmc_core.removable=0' /proc/cmdline; then
175                 # must unmount cards because they will be "ejected" on suspend
176                 # (some filesystems may even deadlock if we don't do this due to bugs)
177                 mounts="$(grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs echo)"
178                 for mnt in $mounts; do
179                         if ! umount $mnt; then
180                                 show_message "Could not unmount $mnt, using partial suspend only"
181                                 return 1
182                         fi
183                 done
184                 swaps="$(grep "/dev/mmcblk" /proc/swaps | awk '{print $1}' | xargs echo)"
185                 for swp in $swaps; do
186                         if ! swapoff $swp; then
187                                 show_message "Could not unmount $swp, using partial suspend only"
188                                 return 1
189                         fi
190                 done
191         else
192                 if [ ! -e /etc/pandora/suspend-warned ]; then
193                         show_message "Pandora will now suspend.\n\n\
194 Please do not remove SD cards while pandora is suspended, doing so will corrupt them."
195                         touch /etc/pandora/suspend-warned
196                 fi
197         fi
198
199         # FIXME: fix the kernel and get rid of this
200         suspend_net
201
202         # get rid of modules that prevent suspend due to bugs
203         modules="$(lsmod | awk '{print $1}' | xargs echo)"
204         blacklist="g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
205                 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms
206                 ehci_hcd bridgedriver"
207         restore_list=""
208         for mod in $modules; do
209                 if echo $blacklist | grep -q "\<$mod\>"; then
210                         restore_list="$restore_list $mod"
211                         rmmod $mod
212                         delay=1 # enough?
213                 fi
214         done
215
216         sleep $delay
217         sync
218         echo mem > /sys/power/state
219
220         # if we are here, either we already resumed or the suspend failed
221         if [ -n "$restore_list" ]; then
222                 for module in $restore_list; do
223                         modprobe $module
224                 done
225         fi
226
227         # it has been reported that time sometimes drifts during suspend
228         # so we reload hwclock here (WizardStan)
229         hwclock -u -s
230
231         display_on
232         resume_net
233         echo $led_high > /sys/class/leds/pandora\:\:power/brightness
234
235         # wait here a bit to prevent this script from running again (keep op_power.lock)
236         # in case user did resume using the power switch.
237         sleep 2
238
239         return 0
240 }
241
242 suspend_() {
243         # dim power LED
244         echo $led_low > /sys/class/leds/pandora\:\:power/brightness
245
246         if suspend_real; then
247                 # resumed already
248                 powerstate="on"
249         else
250                 lowPowerOn
251         fi
252 }
253
254 resume() {
255         if [ "$powerstate" = "on" ]; then
256                 # nothing to do
257                 echo "resume called unexpectedly" >&2
258         else
259                 lowPowerOff
260         fi
261 }
262
263 shutdown(){ # warns the user and shuts the pandora down
264         time=5
265         countdown () {
266                 for i in $(seq $time); do
267                         precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
268                         echo $precentage
269                         echo "# Shutdown in $(($time-$i))"
270                         sleep 1
271                 done
272         }
273         countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
274         if [ $? -eq 0 ]; then
275         /sbin/shutdown -h now
276         else
277         su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
278         fi
279 }
280
281 if [[ "$2" == "" ]]; then
282         if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
283                 if [[ "$powerstate" == "buttonlowpower" ]]; then
284                         (debug && echo "resume") || resume
285                         powerstate="on"
286                 elif [[ "$powerstate" == "on" ]]; then
287                         powerstate="buttonlowpower"
288                         (debug && echo "suspend") || suspend_
289                 elif [[ "$powerstate" == "liddisplayoff" ]]; then
290                         powerstate="buttonlowpower"
291                         (debug && echo "suspend") || suspend_
292                 fi
293         elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
294                 if [[ "$powerstate" == "on" ]]; then
295                         (debug && echo "shutdown") || shutdown
296                 fi
297         fi
298 elif [[ "$2" == "lid" ]]; then
299         if [[ "$1" == 0 ]]; then # lid was opened
300                 if [[ "$powerstate" == lid* ]]; then
301                         case "$lidconfig" in
302                                 "lowpower")
303                                         (debug && echo "resume") || resume
304                                         powerstate="on"
305                                 ;;
306                                 *)
307                                         (debug && echo "display_on") || display_on_with_checks
308                                         powerstate="on"
309                                 ;;
310                         esac
311                 fi
312         elif [[ "$1" == 1 ]]; then # lid was closed
313                 if [[ "$powerstate" == "on" ]]; then
314                         case "$lidconfig" in
315                                 "shutdown")
316                                         (debug && echo "shutdown") || shutdown
317                                 ;;
318                                 "lowpower")
319                                         powerstate="lidlowpower"
320                                         (debug && echo "suspend") || suspend_
321                                 ;;
322                                 *)
323                                         (debug && echo "display_off") || display_off
324                                         echo $led_idle > /sys/class/leds/pandora\:\:power/brightness
325                                         powerstate="liddisplayoff"
326                                 ;;
327                         esac
328                 fi
329         fi
330 elif [[ "$2" == "screensaver" ]]; then
331         # warning: don't try to interact with X or do real suspend here -
332         # will cause various deadlocks
333         unset DISPLAY
334
335         if [[ "$1" == 0 ]]; then # deactivate screensaver
336                 display_on_with_checks
337                 powerstate="on"
338         elif [[ "$1" == 1 ]]; then # activate screensaver
339                 display_off
340                 echo $led_idle > /sys/class/leds/pandora\:\:power/brightness
341         fi
342 fi
343
344 debug && echo "powerstate=$powerstate"
345 echo "$powerstate" > /tmp/powerstate
346
347 rm -f /tmp/op_power.lock