]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - kernel/power/main.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6-omap-h63xx.git] / kernel / power / main.c
index f6dda685e7e2b06bd0eb27631dd84dbf33e8c0ef..32147b57c3bfb419f0950b7abafe88aa207984a5 100644 (file)
@@ -15,7 +15,6 @@
 #include <linux/delay.h>
 #include <linux/errno.h>
 #include <linux/init.h>
-#include <linux/pm.h>
 #include <linux/console.h>
 #include <linux/cpu.h>
 #include <linux/resume-trace.h>
 
 #include "power.h"
 
+BLOCKING_NOTIFIER_HEAD(pm_chain_head);
+
 /*This is just an arbitrary number */
 #define FREE_PAGE_NUMBER (100)
 
 DEFINE_MUTEX(pm_mutex);
 
 struct pm_ops *pm_ops;
-suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
 
 /**
  *     pm_set_ops - Set the global power method table. 
@@ -41,10 +41,6 @@ void pm_set_ops(struct pm_ops * ops)
 {
        mutex_lock(&pm_mutex);
        pm_ops = ops;
-       if (ops && ops->pm_disk_mode != PM_DISK_INVALID) {
-               pm_disk_mode = ops->pm_disk_mode;
-       } else
-               pm_disk_mode = PM_DISK_SHUTDOWN;
        mutex_unlock(&pm_mutex);
 }
 
@@ -69,14 +65,11 @@ static inline void pm_finish(suspend_state_t state)
 
 /**
  *     suspend_prepare - Do prep work before entering low-power state.
- *     @state:         State we're entering.
  *
- *     This is common code that is called for each state that we're 
- *     entering. Allocate a console, stop all processes, then make sure
- *     the platform can enter the requested state.
+ *     This is common code that is called for each state that we're entering.
+ *     Run suspend notifiers, allocate a console and stop all processes.
  */
-
-static int suspend_prepare(suspend_state_t state)
+static int suspend_prepare(void)
 {
        int error;
        unsigned int free_pages;
@@ -84,6 +77,10 @@ static int suspend_prepare(suspend_state_t state)
        if (!pm_ops || !pm_ops->enter)
                return -EPERM;
 
+       error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
+       if (error)
+               goto Finish;
+
        pm_prepare_console();
 
        if (freeze_processes()) {
@@ -91,40 +88,23 @@ static int suspend_prepare(suspend_state_t state)
                goto Thaw;
        }
 
-       if ((free_pages = global_page_state(NR_FREE_PAGES))
-                       < FREE_PAGE_NUMBER) {
+       free_pages = global_page_state(NR_FREE_PAGES);
+       if (free_pages < FREE_PAGE_NUMBER) {
                pr_debug("PM: free some memory\n");
                shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
                if (nr_free_pages() < FREE_PAGE_NUMBER) {
                        error = -ENOMEM;
                        printk(KERN_ERR "PM: No enough memory\n");
-                       goto Thaw;
                }
        }
-
-       if (pm_ops->prepare) {
-               if ((error = pm_ops->prepare(state)))
-                       goto Thaw;
-       }
-
-       suspend_console();
-       error = device_suspend(PMSG_SUSPEND);
-       if (error) {
-               printk(KERN_ERR "Some devices failed to suspend\n");
-               goto Resume_devices;
-       }
-       error = disable_nonboot_cpus();
        if (!error)
                return 0;
 
-       enable_nonboot_cpus();
- Resume_devices:
-       pm_finish(state);
-       device_resume();
-       resume_console();
  Thaw:
        thaw_processes();
        pm_restore_console();
+ Finish:
+       pm_notifier_call_chain(PM_POST_SUSPEND);
        return error;
 }
 
@@ -140,6 +120,12 @@ void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
        local_irq_enable();
 }
 
+/**
+ *     suspend_enter - enter the desired system sleep state.
+ *     @state:         state to enter
+ *
+ *     This function should be called after devices have been suspended.
+ */
 int suspend_enter(suspend_state_t state)
 {
        int error = 0;
@@ -159,23 +145,58 @@ int suspend_enter(suspend_state_t state)
        return error;
 }
 
+/**
+ *     suspend_devices_and_enter - suspend devices and enter the desired system sleep
+ *                       state.
+ *     @state:           state to enter
+ */
+int suspend_devices_and_enter(suspend_state_t state)
+{
+       int error;
+
+       if (!pm_ops)
+               return -ENOSYS;
+
+       if (pm_ops->set_target) {
+               error = pm_ops->set_target(state);
+               if (error)
+                       return error;
+       }
+       suspend_console();
+       error = device_suspend(PMSG_SUSPEND);
+       if (error) {
+               printk(KERN_ERR "Some devices failed to suspend\n");
+               goto Resume_console;
+       }
+       if (pm_ops->prepare) {
+               error = pm_ops->prepare(state);
+               if (error)
+                       goto Resume_devices;
+       }
+       error = disable_nonboot_cpus();
+       if (!error)
+               suspend_enter(state);
+
+       enable_nonboot_cpus();
+       pm_finish(state);
+ Resume_devices:
+       device_resume();
+ Resume_console:
+       resume_console();
+       return error;
+}
 
 /**
  *     suspend_finish - Do final work before exiting suspend sequence.
- *     @state:         State we're coming out of.
  *
  *     Call platform code to clean up, restart processes, and free the 
  *     console that we've allocated. This is not called for suspend-to-disk.
  */
-
-static void suspend_finish(suspend_state_t state)
+static void suspend_finish(void)
 {
-       enable_nonboot_cpus();
-       pm_finish(state);
-       device_resume();
-       resume_console();
        thaw_processes();
        pm_restore_console();
+       pm_notifier_call_chain(PM_POST_SUSPEND);
 }
 
 
@@ -184,24 +205,12 @@ static void suspend_finish(suspend_state_t state)
 static const char * const pm_states[PM_SUSPEND_MAX] = {
        [PM_SUSPEND_STANDBY]    = "standby",
        [PM_SUSPEND_MEM]        = "mem",
-       [PM_SUSPEND_DISK]       = "disk",
 };
 
 static inline int valid_state(suspend_state_t state)
 {
-       /* Suspend-to-disk does not really need low-level support.
-        * It can work with shutdown/reboot if needed. If it isn't
-        * configured, then it cannot be supported.
-        */
-       if (state == PM_SUSPEND_DISK)
-#ifdef CONFIG_SOFTWARE_SUSPEND
-               return 1;
-#else
-               return 0;
-#endif
-
-       /* all other states need lowlevel support and need to be
-        * valid to the lowlevel implementation, no valid callback
+       /* All states need lowlevel support and need to be valid
+        * to the lowlevel implementation, no valid callback
         * implies that none are valid. */
        if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
                return 0;
@@ -219,7 +228,6 @@ static inline int valid_state(suspend_state_t state)
  *     Then, do the setup for suspend, enter the state, and cleaup (after
  *     we've woken up).
  */
-
 static int enter_state(suspend_state_t state)
 {
        int error;
@@ -229,20 +237,15 @@ static int enter_state(suspend_state_t state)
        if (!mutex_trylock(&pm_mutex))
                return -EBUSY;
 
-       if (state == PM_SUSPEND_DISK) {
-               error = pm_suspend_disk();
-               goto Unlock;
-       }
-
        pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
-       if ((error = suspend_prepare(state)))
+       if ((error = suspend_prepare()))
                goto Unlock;
 
        pr_debug("PM: Entering %s sleep\n", pm_states[state]);
-       error = suspend_enter(state);
+       error = suspend_devices_and_enter(state);
 
        pr_debug("PM: Finishing wakeup.\n");
-       suspend_finish(state);
+       suspend_finish();
  Unlock:
        mutex_unlock(&pm_mutex);
        return error;
@@ -251,7 +254,7 @@ static int enter_state(suspend_state_t state)
 
 /**
  *     pm_suspend - Externally visible function for suspending system.
- *     @state:         Enumarted value of state to enter.
+ *     @state:         Enumerated value of state to enter.
  *
  *     Determine whether or not value is within range, get state 
  *     structure, and enter (above).
@@ -289,7 +292,13 @@ static ssize_t state_show(struct kset *kset, char *buf)
                if (pm_states[i] && valid_state(i))
                        s += sprintf(s,"%s ", pm_states[i]);
        }
-       s += sprintf(s,"\n");
+#ifdef CONFIG_SOFTWARE_SUSPEND
+       s += sprintf(s, "%s\n", "disk");
+#else
+       if (s != buf)
+               /* convert the last space to a newline */
+               *(s-1) = '\n';
+#endif
        return (s - buf);
 }
 
@@ -304,8 +313,14 @@ static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
        p = memchr(buf, '\n', n);
        len = p ? p - buf : n;
 
+       /* First, check if we are requested to hibernate */
+       if (len == 4 && !strncmp(buf, "disk", len)) {
+               error = hibernate();
+               return error ? error : n;
+       }
+
        for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
-               if (*s && !strncmp(buf, *s, len))
+               if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
                        break;
        }
        if (state < PM_SUSPEND_MAX && *s)