op_power: make sure powersave is off when turning on display
[openpandora.oe.git] / recipes / pandora-system / pandora-scripts / op_power.sh
index 487056f..35e01cf 100644 (file)
@@ -56,6 +56,13 @@ resume_net() {
 display_on() {
        echo 0 > /sys/class/graphics/fb0/blank
 
+       # only bother restoring brightness if it's 0
+       # (old kernel or user messed it up somehow)
+       brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
+       if [ $brightness -gt 0 ]; then
+               return 0
+       fi
+
        maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
        oldbright=0
        if [ -f /tmp/oldbright ]; then
@@ -76,7 +83,10 @@ display_off() {
        if [ $brightness -gt 0 ]; then
                echo $brightness > /tmp/oldbright
        fi
-       echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
+       kernel_major=`uname -r | cut -c 1`
+       if [ "$kernel_major" = "2" ]; then
+               echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
+       fi
 
        echo 1 > /sys/class/graphics/fb0/blank
 }
@@ -93,12 +103,13 @@ lowPowerOn(){ #switch from normal to lowpower mode
        suspend_net
 
        cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
-       /usr/pandora/scripts/op_cpuspeed.sh 125
+       /usr/pandora/scripts/op_cpuspeed.sh -n 125
 }
 
 lowPowerOff(){ # switch from lowpower to normal mode
        oldspeed=$(cat /tmp/oldspeed)
-       /usr/pandora/scripts/op_cpuspeed.sh $oldspeed
+       /usr/pandora/scripts/op_cpuspeed.sh -n $oldspeed
+       rm -f /tmp/oldspeed
 
        display_on
        resume_net
@@ -111,6 +122,25 @@ lowPowerOff(){ # switch from lowpower to normal mode
        echo 255 > /sys/class/leds/pandora\:\:power/brightness #power LED bright
 }
 
+display_on_with_checks() {
+       # after turning on the display, we don't want lowpower state
+       # (which could be active because of some races)
+       if [ "$powerstate" = "buttonlowpower" -o "$powerstate" = "lidlowpower" -o \
+            -e /tmp/oldspeed ]
+       then
+               lowPowerOff
+       else
+               display_on
+       fi
+}
+
+show_message() {
+       # TODO: check if desktop is visible; maybe use layer3?
+       xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
+       cmd="DISPLAY=:0.0 zenity --info --text \"$1\" --timeout 10"
+       su -c "$cmd" $xfceuser
+}
+
 suspend_real() {
        delay=0
 
@@ -119,23 +149,52 @@ suspend_real() {
                return 1
        fi
 
+       current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
+       if [ $current_now -gt 0 ]; then 
+               return 1 
+               #don't suspend while unit is charging
+       fi
+
        # can't suspend while SGX is in use due to bugs
        # (prevents low power states and potential lockup)
        if lsof -t /dev/pvrsrvkm > /dev/null; then
                return 1
        fi
 
-       # TODO: we probably want to NOT do real suspend if:
-       # - cards don't unmount (running PNDs will break)
-       # - while charging too, since it stops on suspend?
+       if ! grep -q 'mmc_core.removable=0' /proc/cmdline; then
+               # must unmount cards because they will be "ejected" on suspend
+               # (some filesystems may even deadlock if we don't do this due to bugs)
+               mounts="$(grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs echo)"
+               for mnt in $mounts; do
+                       if ! umount $mnt; then
+                               show_message "Could not unmount $mnt, using partial suspend only"
+                               return 1
+                       fi
+               done
+               swaps="$(grep "/dev/mmcblk" /proc/swaps | awk '{print $1}' | xargs echo)"
+               for swp in $swaps; do
+                       if ! swapoff $swp; then
+                               show_message "Could not unmount $swp, using partial suspend only"
+                               return 1
+                       fi
+               done
+       else
+               if [ ! -e /etc/pandora/suspend-warned ]; then
+                       show_message "Pandora will now suspend.\n\n\
+Please do not remove SD cards while pandora is suspended, doing so will corrupt them."
+                       touch /etc/pandora/suspend-warned
+               fi
+       fi
 
        # 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"
+       blacklist="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
+               ehci_hcd bridgedriver"
        restore_list=""
        for mod in $modules; do
                if echo $blacklist | grep -q "\<$mod\>"; then
@@ -145,11 +204,8 @@ suspend_real() {
                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
+       sync
        echo mem > /sys/power/state
 
        # if we are here, either we already resumed or the suspend failed
@@ -157,6 +213,7 @@ suspend_real() {
                modprobe $restore_list
        fi
 
+       display_on
        resume_net
        echo 255 > /sys/class/leds/pandora\:\:power/brightness
 
@@ -215,6 +272,9 @@ if [[ "$2" == "" ]]; then
                elif [[ "$powerstate" == "on" ]]; then
                        powerstate="buttonlowpower"
                        (debug && echo "suspend") || suspend_
+               elif [[ "$powerstate" == "liddisplayoff" ]]; then
+                       powerstate="buttonlowpower"
+                       (debug && echo "suspend") || suspend_
                fi
        elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
                if [[ "$powerstate" == "on" ]]; then
@@ -230,7 +290,7 @@ elif [[ "$2" == "lid" ]]; then
                                        powerstate="on"
                                ;;
                                *)
-                                       (debug && echo "display_on") || display_on
+                                       (debug && echo "display_on") || display_on_with_checks
                                        powerstate="on"
                                ;;
                        esac
@@ -252,7 +312,19 @@ elif [[ "$2" == "lid" ]]; then
                        esac
                fi
        fi
- fi
+elif [[ "$2" == "screensaver" ]]; then
+       # warning: don't try to interact with X or do real suspend here -
+       # will cause various deadlocks
+       unset DISPLAY
+
+       if [[ "$1" == 0 ]]; then # deactivate screensaver
+               display_on_with_checks
+               powerstate="on"
+       elif [[ "$1" == 1 ]]; then # activate screensaver
+               display_off
+       fi
+fi
+
 debug && echo "powerstate=$powerstate"
 echo "$powerstate" > /tmp/powerstate