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