op_power.sh: fix accidentally dropped cpu clock save
[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 suspend_real() {
115         delay=0
116
117         if ! [ -e /sys/power/state ]; then
118                 # no kernel suspend support
119                 return 1
120         fi
121
122         # can't suspend while SGX is in use due to bugs
123         # (prevents low power states and potential lockup)
124         if lsof -t /dev/pvrsrvkm > /dev/null; then
125                 return 1
126         fi
127
128         # TODO: we probably want to NOT do real suspend if:
129         # - cards don't unmount (running PNDs will break)
130         # - while charging too, since it stops on suspend?
131
132         # FIXME: fix the kernel and get rid of this
133         suspend_net
134
135         # get rid of modules that prevent suspend due to bugs
136         modules="$(lsmod | awk '{print $1}' | xargs echo)"
137         blacklist="ehci_hcd g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
138                 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms"
139         restore_list=""
140         for mod in $modules; do
141                 if echo $blacklist | grep -q "\<$mod\>"; then
142                         restore_list="$restore_list $mod"
143                         rmmod $mod
144                         delay=1 # enough?
145                 fi
146         done
147
148         # must unmount cards because they will be "ejected" on suspend
149         # (some filesystems may even deadlock if we don't do this due to bugs)
150         grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs umount -r
151
152         sleep $delay
153         echo mem > /sys/power/state
154
155         # if we are here, either we already resumed or the suspend failed
156         if [ -n "$restore_list" ]; then
157                 modprobe $restore_list
158         fi
159
160         resume_net
161         echo 255 > /sys/class/leds/pandora\:\:power/brightness
162
163         # wait here a bit to prevent this script from running again (keep op_power.lock)
164         # in case user did resume using the power switch.
165         sleep 2
166
167         return 0
168 }
169
170 suspend_() {
171         # dim power LED
172         echo 16 > /sys/class/leds/pandora\:\:power/brightness
173
174         if suspend_real; then
175                 # resumed already
176                 powerstate="on"
177         else
178                 lowPowerOn
179         fi
180 }
181
182 resume() {
183         if [ "$powerstate" = "on" ]; then
184                 # nothing to do
185                 echo "resume called unexpectedly" >&2
186         else
187                 lowPowerOff
188         fi
189 }
190
191 shutdown(){ # warns the user and shuts the pandora down
192         xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
193         time=5
194         countdown () {
195                 for i in $(seq $time); do
196                         precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
197                         echo $precentage
198                         echo "# Shutdown in $(($time-$i))"
199                         sleep 1
200                 done
201         }
202         countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
203         if [ $? -eq 0 ]; then
204         /sbin/shutdown -h now
205         else
206         su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
207         fi
208 }
209
210 if [[ "$2" == "" ]]; then
211         if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
212                 if [[ "$powerstate" == "buttonlowpower" ]]; then
213                         (debug && echo "resume") || resume
214                         powerstate="on"
215                 elif [[ "$powerstate" == "on" ]]; then
216                         powerstate="buttonlowpower"
217                         (debug && echo "suspend") || suspend_
218                 fi
219         elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
220                 if [[ "$powerstate" == "on" ]]; then
221                         (debug && echo "shutdown") || shutdown
222                 fi
223         fi
224 elif [[ "$2" == "lid" ]]; then
225         if [[ "$1" == 0 ]]; then # lid was opened
226                 if [[ "$powerstate" == lid* ]]; then
227                         case "$lidconfig" in
228                                 "lowpower")
229                                         (debug && echo "resume") || resume
230                                         powerstate="on"
231                                 ;;
232                                 *)
233                                         (debug && echo "display_on") || display_on
234                                         powerstate="on"
235                                 ;;
236                         esac
237                 fi
238         elif [[ "$1" == 1 ]]; then # lid was closed
239                 if [[ "$powerstate" == "on" ]]; then
240                         case "$lidconfig" in
241                                 "shutdown")
242                                         (debug && echo "shutdown") || shutdown
243                                 ;;
244                                 "lowpower")
245                                         powerstate="lidlowpower"
246                                         (debug && echo "suspend") || suspend_
247                                 ;;
248                                 *)
249                                         (debug && echo "display_off") || display_off
250                                         powerstate="liddisplayoff"
251                                 ;;
252                         esac
253                 fi
254         fi
255  fi
256 debug && echo "powerstate=$powerstate"
257 echo "$powerstate" > /tmp/powerstate
258
259 rm -f /tmp/op_power.lock