dd0101d8b24b620e2ba9f704075413e8dc784b00
[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 lowPowerOn(){ #switch from normal to lowpower mode
57         cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
58         cat $SYSFS_BACKLIGHT_BRIGHTNESS > /tmp/oldbright
59         pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
60         for PID in $pidlist
61         do
62                 kill -19 $PID #send SIGSTOP
63         done
64         suspend_net
65         echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
66         echo 1 > /sys/devices/platform/omapfb/graphics/fb0/blank
67         /usr/pandora/scripts/op_cpuspeed.sh 125
68 }
69
70 lowPowerOff(){ # switch from lowpower to normal mode
71         oldspeed=$(cat /tmp/oldspeed)
72         /usr/pandora/scripts/op_cpuspeed.sh $oldspeed
73         oldbright=$(cat /tmp/oldbright)
74         maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
75         echo 0 > /sys/devices/platform/omapfb/graphics/fb0/blank
76         sleep 0.1s # looks cleaner, could flicker without
77         oldspeed=$(cat /tmp/oldspeed)
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         resume_net
84         pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
85         for PID in $pidlist
86         do
87                 kill -18 $PID #send SIGCONT
88         done
89         echo 255 > /sys/class/leds/pandora\:\:power/brightness #power LED bright
90 }
91
92 suspend_real() {
93         delay=0
94
95         # FIXME: fix the kernel and get rid of this
96         suspend_net
97
98         # get rid of modules that prevent suspend due to bugs
99         modules="$(lsmod | awk '{print $1}' | xargs echo)"
100         blacklist="ehci_hcd g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
101                 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms"
102         restore_list=""
103         for mod in $modules; do
104                 if echo $blacklist | grep -q "\<$mod\>"; then
105                         restore_list="$restore_list $mod"
106                         rmmod $mod
107                         delay=1 # enough?
108                 fi
109         done
110
111         # must unmount cards because they will be "ejected" on suspend
112         # (some filesystems may even deadlock if we don't do this due to bugs)
113         grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs umount -r
114
115         sleep $delay
116         echo mem > /sys/power/state
117
118         # if we are here, either we already resumed or the suspend failed
119         if [ -n "$restore_list" ]; then
120                 modprobe $restore_list
121         fi
122
123         resume_net
124         echo 255 > /sys/class/leds/pandora\:\:power/brightness
125
126         # wait here a bit to prevent this script from running again (keep op_power.lock)
127         # in case user did resume using the power switch.
128         sleep 2
129 }
130
131 suspend_() {
132         # dim power LED
133         echo 16 > /sys/class/leds/pandora\:\:power/brightness
134
135         if [ -e /sys/power/state ]; then
136                 suspend_real
137         else
138                 lowPowerOn
139         fi
140 }
141
142 resume() {
143         if [ -e /sys/power/state ]; then
144                 # nothing to do
145                 echo "resume called unexpectedly" >&2
146         else
147                 lowPowerOff
148         fi
149 }
150
151 suspend_check() {
152         if [ -e /sys/power/state ]; then
153                 # in case of real suspend we've already resumed
154                 powerstate="on"
155         fi
156 }
157
158 shutdown(){ # warns the user and shuts the pandora down
159         xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
160         time=5
161         countdown () {
162                 for i in $(seq $time); do
163                         precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
164                         echo $precentage
165                         echo "# Shutdown in $(($time-$i))"
166                         sleep 1
167                 done
168         }
169         countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
170         if [ $? -eq 0 ]; then
171         /sbin/shutdown -h now
172         else
173         su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
174         fi
175 }
176
177 displayOn(){ # turns the display on
178         #echo 0 > /sys/devices/platform/omapfb/graphics/fb0/blank
179         #sleep 0.1s # looks cleaner, could flicker without
180         maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
181         oldbright=0
182         if [ -f /tmp/oldbright ]; then
183                 oldbright=$(cat /tmp/oldbright)
184         fi
185         if [ $oldbright -eq 0 ]; then
186                 oldbright=$(cat /etc/pandora/conf/brightness.state)
187         fi
188         if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then 
189                 /usr/pandora/scripts/op_bright.sh $oldbright 
190         else
191                 /usr/pandora/scripts/op_bright.sh $maxbright
192         fi
193 }
194
195 displayOff(){ # turns the display off
196         brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
197         if [ $brightness -gt 0 ]; then
198                 echo $brightness > /tmp/oldbright
199         fi
200         echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
201         #echo 1 > /sys/devices/platform/omapfb/graphics/fb0/blank
202 }
203
204 if [[ "$2" == "" ]]; then
205         if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
206                 if [[ "$powerstate" == "buttonlowpower" ]]; then
207                         (debug && echo "resume") || resume
208                         powerstate="on"
209                 elif [[ "$powerstate" == "on" ]]; then
210                         (debug && echo "suspend") || suspend_
211                         powerstate="buttonlowpower"
212                         suspend_check
213                 fi
214         elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
215                 if [[ "$powerstate" == "on" ]]; then
216                         (debug && echo "shutdown") || shutdown
217                 fi
218         fi
219 elif [[ "$2" == "lid" ]]; then
220         if [[ "$1" == 0 ]]; then # lid was opened
221                 if [[ "$powerstate" == lid* ]]; then
222                         case "$lidconfig" in
223                                 "lowpower")
224                                         (debug && echo "resume") || resume
225                                         powerstate="on"
226                                 ;;
227                                 *)
228                                         (debug && echo "displayOn") || displayOn
229                                         powerstate="on"
230                                 ;;
231                         esac
232                 fi
233         elif [[ "$1" == 1 ]]; then # lid was closed
234                 if [[ "$powerstate" == "on" ]]; then
235                         case "$lidconfig" in
236                                 "shutdown")
237                                         (debug && echo "shutdown") || shutdown
238                                 ;;
239                                 "lowpower")
240                                         (debug && echo "suspend") || suspend_
241                                         powerstate="lidlowpower"
242                                         suspend_check
243                                 ;;
244                                 *)
245                                         (debug && echo "displayOff") || displayOff
246                                         powerstate="liddisplayoff"
247                                 ;;
248                         esac
249                 fi
250         fi
251  fi
252 debug && echo "powerstate=$powerstate"
253 echo "$powerstate" > /tmp/powerstate
254
255 rm -f /tmp/op_power.lock