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