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