op_power.sh: preliminary support for kernel-based suspend
[openpandora.oe.git] / recipes / pandora-system / pandora-scripts / op_power.sh
index 25ca48a..dd0101d 100644 (file)
@@ -22,32 +22,48 @@ fi
 
 debug && echo "powerstate=$powerstate"
 
-lowPowerOn(){ #switch from normal to lowpower mode
-       cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
-       cat $SYSFS_BACKLIGHT_BRIGHTNESS > /tmp/oldbright
-       pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
-       for PID in $pidlist
-       do
-               kill -19 $PID #send SIGSTOP
-       done
-       test -f /tmp/hcistate && rm /tmp/hcistate
+suspend_net() {
        hcistate=$(hciconfig hci0 | grep DOWN)
        if [ $hcistate ]; then
                echo "down" > /tmp/hcistate
        else
+               echo "up" > /tmp/hcistate
                hciconfig hci0 down
        fi
-       test -f /tmp/wlstate && rm /tmp/wlstate
        wlstate=$(lsmod | grep -m1 wl1251)
        if [ -z "$wlstate" ]; then
                echo "down" > /tmp/wlstate
        else
+               echo "up" > /tmp/wlstate
                ifconfig wlan0 down
-               rmmod board_omap3pandora_wifi wl1251_sdio wl1251
+               rmmod board_omap3pandora_wifi 2> /dev/null
+               rmmod wl1251_sdio wl1251
        fi
+}
+
+resume_net() {
+       hcistate=$(cat /tmp/hcistate)
+       if [ "$hcistate" = "up" ]; then
+               hciconfig hci0 up pscan
+       fi
+       wlstate=$(cat /tmp/wlstate)
+       if [ "$wlstate" = "up" ]; then
+               /etc/init.d/wl1251-init start
+       fi
+       rm -f /tmp/hcistate /tmp/wlstate
+}
+
+lowPowerOn(){ #switch from normal to lowpower mode
+       cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
+       cat $SYSFS_BACKLIGHT_BRIGHTNESS > /tmp/oldbright
+       pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
+       for PID in $pidlist
+       do
+               kill -19 $PID #send SIGSTOP
+       done
+       suspend_net
        echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
        echo 1 > /sys/devices/platform/omapfb/graphics/fb0/blank
-       echo 16 > /sys/class/leds/pandora\:\:power/brightness #dim power LED
        /usr/pandora/scripts/op_cpuspeed.sh 125
 }
 
@@ -64,14 +80,7 @@ lowPowerOff(){ # switch from lowpower to normal mode
        else
                /usr/pandora/scripts/op_bright.sh $maxbright
        fi
-       hcistate=$(cat /tmp/hcistate)
-       if [ ! $hcistate ]; then
-               hciconfig hci0 up pscan
-       fi
-       wlstate=$(cat /tmp/wlstate)
-       if [ -z "$wlstate" ]; then
-               /etc/init.d/wl1251-init start
-       fi
+       resume_net
        pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
        for PID in $pidlist
        do
@@ -80,6 +89,72 @@ lowPowerOff(){ # switch from lowpower to normal mode
        echo 255 > /sys/class/leds/pandora\:\:power/brightness #power LED bright
 }
 
+suspend_real() {
+       delay=0
+
+       # FIXME: fix the kernel and get rid of this
+       suspend_net
+
+       # get rid of modules that prevent suspend due to bugs
+       modules="$(lsmod | awk '{print $1}' | xargs echo)"
+       blacklist="ehci_hcd g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
+               g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms"
+       restore_list=""
+       for mod in $modules; do
+               if echo $blacklist | grep -q "\<$mod\>"; then
+                       restore_list="$restore_list $mod"
+                       rmmod $mod
+                       delay=1 # enough?
+               fi
+       done
+
+       # must unmount cards because they will be "ejected" on suspend
+       # (some filesystems may even deadlock if we don't do this due to bugs)
+       grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs umount -r
+
+       sleep $delay
+       echo mem > /sys/power/state
+
+       # if we are here, either we already resumed or the suspend failed
+       if [ -n "$restore_list" ]; then
+               modprobe $restore_list
+       fi
+
+       resume_net
+       echo 255 > /sys/class/leds/pandora\:\:power/brightness
+
+       # wait here a bit to prevent this script from running again (keep op_power.lock)
+       # in case user did resume using the power switch.
+       sleep 2
+}
+
+suspend_() {
+       # dim power LED
+       echo 16 > /sys/class/leds/pandora\:\:power/brightness
+
+       if [ -e /sys/power/state ]; then
+               suspend_real
+       else
+               lowPowerOn
+       fi
+}
+
+resume() {
+       if [ -e /sys/power/state ]; then
+               # nothing to do
+               echo "resume called unexpectedly" >&2
+       else
+               lowPowerOff
+       fi
+}
+
+suspend_check() {
+       if [ -e /sys/power/state ]; then
+               # in case of real suspend we've already resumed
+               powerstate="on"
+       fi
+}
+
 shutdown(){ # warns the user and shuts the pandora down
        xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
        time=5
@@ -129,11 +204,12 @@ displayOff(){ # turns the display off
 if [[ "$2" == "" ]]; then
        if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
                if [[ "$powerstate" == "buttonlowpower" ]]; then
-                       (debug && echo "lowPowerOff") || lowPowerOff
+                       (debug && echo "resume") || resume
                        powerstate="on"
                elif [[ "$powerstate" == "on" ]]; then
-                       (debug && echo "lowPowerOn") || lowPowerOn
+                       (debug && echo "suspend") || suspend_
                        powerstate="buttonlowpower"
+                       suspend_check
                fi
        elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
                if [[ "$powerstate" == "on" ]]; then
@@ -145,7 +221,7 @@ elif [[ "$2" == "lid" ]]; then
                if [[ "$powerstate" == lid* ]]; then
                        case "$lidconfig" in
                                "lowpower")
-                                       (debug && echo "lowPowerOff") || lowPowerOff
+                                       (debug && echo "resume") || resume
                                        powerstate="on"
                                ;;
                                *)
@@ -161,8 +237,9 @@ elif [[ "$2" == "lid" ]]; then
                                        (debug && echo "shutdown") || shutdown
                                ;;
                                "lowpower")
-                                       (debug && echo "lowPowerOn") || lowPowerOn
+                                       (debug && echo "suspend") || suspend_
                                        powerstate="lidlowpower"
+                                       suspend_check
                                ;;
                                *)
                                        (debug && echo "displayOff") || displayOff