[Pm-utils] [PATCH 1/2] Enable autodetection and stacking of sleep methods.

Victor Lowther victor.lowther at gmail.com
Sat Sep 6 10:51:26 PDT 2008


Instead of using just one sleep module, we can load all of them and
the first one that meets all the prerequisites for any given sleep
method will be used for that sleep method.

Now, modules do not have to implement all possible sleep methods.  Since
we can load more than one module, it is possible for one method to be provided
by one module and another method to be provided by a different module.

I also trimmed s2ram from uswsusp -- we just fall back to kernel suspend 
instead.  This way, we only have to handle video quirks in one place, and we
control that place.
---
 pm/module.d/kernel   |   64 +++++++++++---------------
 pm/module.d/tuxonice |   46 +++++++------------
 pm/module.d/uswsusp  |  123 +++++++++++++-------------------------------------
 pm/pm-functions.in   |   28 +++++++++--
 4 files changed, 98 insertions(+), 163 deletions(-)

diff --git a/pm/module.d/kernel b/pm/module.d/kernel
dissimilarity index 67%
index 88a9e5d..eb9626c 100755
--- a/pm/module.d/kernel
+++ b/pm/module.d/kernel
@@ -1,38 +1,26 @@
-#!/bin/sh
-
-check_suspend()
-{
-	[ -c /dev/pmu ] || grep -q mem /sys/power/state
-}
-
-do_suspend()
-{
-	if [ -c /dev/pmu ]; then
-		pm-pmu --suspend
-	else
-		echo -n "mem" > /sys/power/state
-	fi
-}
-
-check_hibernate()
-{
-	[ -f /sys/power/disk ] && grep -q disk /sys/power/state
-}
-
-do_hibernate()
-{
-	[ -n "${HIBERNATE_MODE}" ] && \
-	grep -qw "${HIBERNATE_MODE}" /sys/power/disk && \
-	echo -n "${HIBERNATE_MODE}" > /sys/power/disk
-	echo -n "disk" > /sys/power/state
-}
-
-check_suspend_hybrid()
-{
-	return 1
-}
-
-do_suspend_hybrid()
-{
-	return 1
-}
+#!/bin/sh
+
+if [ "$SUSPEND_METHOD" = "none" ] && \
+    { [ -c /dev/pmu ] || grep -q mem /sys/power/state; } then
+    SUSPEND_METHOD=kernel
+    do_suspend()
+    {
+	if [ -c /dev/pmu ]; then
+		pm-pmu --suspend
+	else
+		echo -n "mem" > /sys/power/state
+	fi
+    }
+fi
+
+if [ "$HIBERNATE_METHOD" = "none" ] && \
+    [ -f /sys/power/disk ] && grep -q disk /sys/power/state; then
+    HIBERNATE_METHOD=kernel
+    do_hibernate()
+    {
+	[ -n "${HIBERNATE_MODE}" ] && \
+	    grep -qw "${HIBERNATE_MODE}" /sys/power/disk && \
+	    echo -n "${HIBERNATE_MODE}" > /sys/power/disk
+	echo -n "disk" > /sys/power/state
+    }
+fi
diff --git a/pm/module.d/tuxonice b/pm/module.d/tuxonice
index 57b7bb8..7207d16 100755
--- a/pm/module.d/tuxonice
+++ b/pm/module.d/tuxonice
@@ -7,45 +7,33 @@ export TUXONICE_LOC
 # set the appropriate values.
 
 # more locations might be nice
+
 for loc in "/sys/power/tuxonice" "/sys/power/suspend2"; do
 	[ -d "${loc}" ] && { TUXONICE_LOC="${loc}"; break; }
 done 
 
-check_suspend()
-{
-	[ -c /dev/pmu ] || grep -q mem /sys/power/state
-}
-
-do_suspend()
-{
-	if [ -c /dev/pmu ]; then
-		pm-pmu --suspend
-	else
-		echo -n "mem" > /sys/power/state
-	fi
-}
+if [ "$HIBERNATE_METHOD" = "none" ] && \
+    [ -f "${TUXONICE_LOC}/do_hibernate" ]; then
+    HIBERNATE_METHOD="tuxonice"
 
-check_hibernate()
-{
-	[ -f "${TUXONICE_LOC}/do_hibernate" ]
-}
-
-do_hibernate()
-{
+    do_hibernate()
+    {
 	echo 5 > "${TUXONICE_LOC}/powerdown_method"
 	echo anything > "${TUXONICE_LOC}/do_hibernate"
-}
+    }
+fi
+
+if [ "$SUSPEND_HYBRID_METHOD" = "none" ] && \
+    grep -q mem /sys/power/state && \
+    [ -f "${TUXONICE_LOC}/do_hibernate" ]; then
+    SUSPEND_HYBRID_METHOD="tuxonice"
 
-check_suspend_hybrid()
-{
-	grep -q mem /sys/power/state && \
-	[ -f "${TUXONICE_LOC}/do_hibernate" ]
-}
-do_suspend_hybrid()
-{
+    do_suspend_hybrid()
+    {
 	echo 3 >"${TUXONICE_LOC}/powerdown_method"
 	echo anything >"${TUXONICE_LOC}/do_hibernate"
 	[ -f /sys/power/tuxonice/did_suspend_to_both ] && \
 	    [ "$(cat /sys/power/tuxonice/did_suspend_to_both)" != "1" ] && \
 	    REVERSE="thaw"
-}
+    }
+fi
diff --git a/pm/module.d/uswsusp b/pm/module.d/uswsusp
dissimilarity index 98%
index 4647824..55cdbcb 100755
--- a/pm/module.d/uswsusp
+++ b/pm/module.d/uswsusp
@@ -1,91 +1,32 @@
-#!/bin/sh
-
-# disable processing of 99video
-before_hooks()
-{
-	disablehook 90chvt "disabled by uswsusp"
-	disablehook 99video "disabled by uswsusp"
-}
-
-get_quirks()
-{
-	OPTS=""
-	ACPI_SLEEP=0
-	for opt in $PM_CMDLINE; do
-		case "${opt##--quirk-}" in # just quirks, please
-			dpms-on) 	   ;; # no-op
-			dpms-suspend) 	   ;; # no-op
-			radeon-off) 	   OPTS="$OPTS --radeontool" ;;
-			reset-brightness)  ;; # no-op
-			s3-bios) 	   ACPI_SLEEP=$(($ACPI_SLEEP + 1)) ;;
-			s3-mode) 	   ACPI_SLEEP=$(($ACPI_SLEEP + 2)) ;;
-			vbe-post) 	   OPTS="$OPTS --vbe_post" ;;
-			vbemode-restore)   OPTS="$OPTS --vbe_mode" ;;
-			vbestate-restore)  OPTS="$OPTS --vbe_save" ;;
-			vga-mode3) 	   ;; # no-op
-			save-pci)          OPTS="$OPTS --pci_save" ;;
-			none) 		   QUIRK_NONE="true" ;;
-			*) continue ;;
-		esac
-	done
-	[ $ACPI_SLEEP -ne 0 ] && OPTS="$OPTS --acpi_sleep $ACPI_SLEEP"
-	# if we were told to ignore quirks, do so.
-	[ "$QUIRK_NONE" = "true" ] && OPTS=""
-}
-
-check_suspend()
-{
-	command_exists s2ram || return 1
-	[ -c /dev/pmu ] || grep -q mem /sys/power/state
-}
-
-do_suspend()
-{
-	get_quirks
-	s2ram --force $OPTS
-}
-
-check_hibernate()
-{
-	[ -f /sys/power/disk ] && \
-	grep -q disk /sys/power/state && \
-	[ -c /dev/snapshot ] &&
-	command_exists s2disk	
-}
-
-do_hibernate()
-{
-	s2disk
-}
-
-check_suspend_hybrid()
-{
-	grep -q mem /sys/power/state && \
-	command_exists s2both && \
-	check_hibernate
-}
-
-do_suspend_hybrid()
-{
-	get_quirks
-	s2both --force $OPTS 
-}
-
-sleep_method_help()
-{
-	echo  # first echo makes it look nicer.
-	echo "Video quirk handler options:"
-	echo
-	echo "  --quirk-dpms-on"
-	echo "  --quirk-dpms-suspend"
-	echo "  --quirk-radeon-off"
-	echo "  --quirk-reset-brightness"
-	echo "  --quirk-s3-bios"
-	echo "  --quirk-s3-mode"
-	echo "  --quirk-vbe-post"
-	echo "  --quirk-vbemode-restore"
-	echo "  --quirk-vbestate-restore"
-	echo "  --quirk-vga-mode3"
-	echo "  --quirk-save-pci"
-	echo "  --quirk-none"
-}
+#!/bin/sh
+
+# Note the lack of s2ram -- we just use kernel suspend.
+# We were just duplicating the code done in 99video for no good reason.
+# If we did use s2ram, we would override its internal processing with all
+# our stuff anyways, and duplicate option processing code sucks.
+
+if [ "$HIBERNATE_METHOD" = "none" ] && \
+    [ -f /sys/power/disk ] && \
+    grep -q disk /sys/power/state && \
+    [ -c /dev/snapshot ] &&
+    command_exists s2disk; then
+    HIBERNATE_METHOD="uswsusp"
+
+    do_hibernate()
+    {
+	s2disk
+    }
+fi
+
+
+if [ "$SUSPEND_HYBRID_METHOD" = "none" ] && \
+    grep -q mem /sys/power/state && \
+    command_exists s2both && \
+    [ "$HIBERNATE_METHOD" = "uswsusp" ]; then
+    SUSPEND_HYBRID_METHOD=uswsusp
+
+    do_suspend_hybrid()
+    {
+	s2both --force
+    }
+fi
\ No newline at end of file
diff --git a/pm/pm-functions.in b/pm/pm-functions.in
index ce79619..9b11a71 100644
--- a/pm/pm-functions.in
+++ b/pm/pm-functions.in
@@ -44,6 +44,9 @@ DROP_PARAMETERS=""
 PARAMETERS="${STORAGEDIR}/parameters"
 INHIBIT="${STORAGEDIR}/inhibit"
 PM_CMDLINE="$*"
+SUSPEND_METHOD="none"
+HIBERNATE_METHOD="none"
+SUSPEND_HYBRID_METHOD="none"
 
 # when loading configuration files, allow stash-specific ones
 # to override the pm-utils global ones.
@@ -203,12 +206,27 @@ init_logfile()
 	exec > "$1" 2>&1
 }
 
+check_suspend() {
+    [ "$SUSPEND_METHOD" != "none" ]
+}
 
-SLEEP_FUNCTIONS="${PM_UTILS_LIBDIR}/module.d/${SLEEP_MODULE}"
-[ -f "${SLEEP_FUNCTIONS}" ] || { 
-	echo "Requested sleep module $SLEEP_MODULE not available."
-	exit 1
+check_hibernate() {
+    [ "$HIBERNATE_METHOD" != "none" ]
 }
 
-. "${SLEEP_FUNCTIONS}"
+check_suspend_hybrid() {
+    [ "$SUSPEND_HYBRID_METHOD" != "none" ]
+}
+
+if [ "$SLEEP_MODULE" = "auto" ]; then 
+    SLEEP_MODULE="tuxonice uswsusp"
+fi
+
+SLEEP_FUNCTIONS="${PM_UTILS_LIBDIR}/module.d/${SLEEP_MODULE}"
+
+for sleep_method in $sleep_methods; do
+    . "${PM_UTILS_LIBDIR}/module.d/${sleep_method}"
+done
 
+# always load in-kernel methods last
+. "${PM_UTILS_LIBDIR}/module.d/kernel"
\ No newline at end of file
-- 
1.5.6.5



More information about the Pm-utils mailing list