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