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