8dc1c21c74fcbc3a115421c373137eb42c4b2f8f
[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 125
107 }
108
109 lowPowerOff(){ # switch from lowpower to normal mode
110         oldspeed=$(cat /tmp/oldspeed)
111         /usr/pandora/scripts/op_cpuspeed.sh $oldspeed
112
113         display_on
114         resume_net
115
116         pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
117         for PID in $pidlist
118         do
119                 kill -CONT $PID
120         done
121         echo 255 > /sys/class/leds/pandora\:\:power/brightness #power LED bright
122 }
123
124 show_message() {
125         # TODO: check if desktop is visible; maybe use layer3?
126         xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
127         cmd="DISPLAY=:0.0 zenity --info --text \"$1\""
128         su -c "$cmd" $xfceuser
129 }
130
131 suspend_real() {
132         delay=0
133
134         if ! [ -e /sys/power/state ]; then
135                 # no kernel suspend support
136                 return 1
137         fi
138
139         current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
140  
141         if [ $current_now -gt 0 ]; then 
142                 return 1 
143                 #don't suspend while unit is charging
144         fi
145
146         # can't suspend while SGX is in use due to bugs
147         # (prevents low power states and potential lockup)
148         if lsof -t /dev/pvrsrvkm > /dev/null; then
149                 return 1
150         fi
151
152         # TODO: we probably want to NOT do real suspend if:
153         # - cards don't unmount (running PNDs will break)
154         # - while charging too, since it stops on suspend?
155
156         if ! grep -q 'mmc_core.removable=0' /proc/cmdline; then
157                 # must unmount cards because they will be "ejected" on suspend
158                 # (some filesystems may even deadlock if we don't do this due to bugs)
159                 mounts="$(grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs echo)"
160                 for mnt in $mounts; do
161                         if ! umount $mnt; then
162                                 show_message "Could not unmount $mnt, using partial suspend only"
163                                 return 1
164                         fi
165                 done
166                 swaps="$(grep "/dev/mmcblk" /proc/swaps | awk '{print $1}' | xargs echo)"
167                 for swp in $swaps; do
168                         if ! swapoff $swp; then
169                                 show_message "Could not unmount $swp, using partial suspend only"
170                                 return 1
171                         fi
172                 done
173         else
174                 if [ ! -e /etc/pandora/suspend-warned ]; then
175                         show_message "Pandora will now suspend.\n\n\
176 Please do not remove SD cards while pandora is suspended, doing so will corrupt them."
177                         touch /etc/pandora/suspend-warned
178                 fi
179         fi
180
181         # FIXME: fix the kernel and get rid of this
182         suspend_net
183
184         # get rid of modules that prevent suspend due to bugs
185         modules="$(lsmod | awk '{print $1}' | xargs echo)"
186         blacklist="ehci_hcd g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
187                 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms"
188         restore_list=""
189         for mod in $modules; do
190                 if echo $blacklist | grep -q "\<$mod\>"; then
191                         restore_list="$restore_list $mod"
192                         rmmod $mod
193                         delay=1 # enough?
194                 fi
195         done
196
197         sleep $delay
198         sync
199         echo mem > /sys/power/state
200
201         # if we are here, either we already resumed or the suspend failed
202         if [ -n "$restore_list" ]; then
203                 modprobe $restore_list
204         fi
205
206         display_on
207         resume_net
208         echo 255 > /sys/class/leds/pandora\:\:power/brightness
209
210         # wait here a bit to prevent this script from running again (keep op_power.lock)
211         # in case user did resume using the power switch.
212         sleep 2
213
214         return 0
215 }
216
217 suspend_() {
218         # dim power LED
219         echo 16 > /sys/class/leds/pandora\:\:power/brightness
220
221         if suspend_real; then
222                 # resumed already
223                 powerstate="on"
224         else
225                 lowPowerOn
226         fi
227 }
228
229 resume() {
230         if [ "$powerstate" = "on" ]; then
231                 # nothing to do
232                 echo "resume called unexpectedly" >&2
233         else
234                 lowPowerOff
235         fi
236 }
237
238 shutdown(){ # warns the user and shuts the pandora down
239         xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
240         time=5
241         countdown () {
242                 for i in $(seq $time); do
243                         precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
244                         echo $precentage
245                         echo "# Shutdown in $(($time-$i))"
246                         sleep 1
247                 done
248         }
249         countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
250         if [ $? -eq 0 ]; then
251         /sbin/shutdown -h now
252         else
253         su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
254         fi
255 }
256
257 if [[ "$2" == "" ]]; then
258         if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
259                 if [[ "$powerstate" == "buttonlowpower" ]]; then
260                         (debug && echo "resume") || resume
261                         powerstate="on"
262                 elif [[ "$powerstate" == "on" ]]; then
263                         powerstate="buttonlowpower"
264                         (debug && echo "suspend") || suspend_
265                 elif [[ "$powerstate" == "liddisplayoff" ]]; then
266                         powerstate="buttonlowpower"
267                         (debug && echo "suspend") || suspend_
268                 fi
269         elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
270                 if [[ "$powerstate" == "on" ]]; then
271                         (debug && echo "shutdown") || shutdown
272                 fi
273         fi
274 elif [[ "$2" == "lid" ]]; then
275         if [[ "$1" == 0 ]]; then # lid was opened
276                 if [[ "$powerstate" == lid* ]]; then
277                         case "$lidconfig" in
278                                 "lowpower")
279                                         (debug && echo "resume") || resume
280                                         powerstate="on"
281                                 ;;
282                                 *)
283                                         (debug && echo "display_on") || display_on
284                                         powerstate="on"
285                                 ;;
286                         esac
287                 fi
288         elif [[ "$1" == 1 ]]; then # lid was closed
289                 if [[ "$powerstate" == "on" ]]; then
290                         case "$lidconfig" in
291                                 "shutdown")
292                                         (debug && echo "shutdown") || shutdown
293                                 ;;
294                                 "lowpower")
295                                         powerstate="lidlowpower"
296                                         (debug && echo "suspend") || suspend_
297                                 ;;
298                                 *)
299                                         (debug && echo "display_off") || display_off
300                                         powerstate="liddisplayoff"
301                                 ;;
302                         esac
303                 fi
304         fi
305  fi
306 debug && echo "powerstate=$powerstate"
307 echo "$powerstate" > /tmp/powerstate
308
309 rm -f /tmp/op_power.lock