Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / kernel / power / main.c
index d582906..a6d9ef4 100644 (file)
@@ -4,12 +4,10 @@
  * Copyright (c) 2003 Patrick Mochel
  * Copyright (c) 2003 Open Source Development Lab
  * 
- * This file is release under the GPLv2
+ * This file is released under the GPLv2
  *
  */
 
-#define DEBUG
-
 #include <linux/suspend.h>
 #include <linux/kobject.h>
 #include <linux/string.h>
 
 #include "power.h"
 
+/*This is just an arbitrary number */
+#define FREE_PAGE_NUMBER (100)
+
 DECLARE_MUTEX(pm_sem);
 
-struct pm_ops * pm_ops = NULL;
-u32 pm_disk_mode = PM_DISK_SHUTDOWN;
+struct pm_ops *pm_ops;
+suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
 
 /**
  *     pm_set_ops - Set the global power method table. 
@@ -35,8 +36,6 @@ void pm_set_ops(struct pm_ops * ops)
 {
        down(&pm_sem);
        pm_ops = ops;
-       if (ops->pm_disk_mode && ops->pm_disk_mode < PM_DISK_MAX)
-               pm_disk_mode = ops->pm_disk_mode;
        up(&pm_sem);
 }
 
@@ -50,46 +49,71 @@ void pm_set_ops(struct pm_ops * ops)
  *     the platform can enter the requested state.
  */
 
-static int suspend_prepare(u32 state)
+static int suspend_prepare(suspend_state_t state)
 {
        int error = 0;
+       unsigned int free_pages;
 
        if (!pm_ops || !pm_ops->enter)
                return -EPERM;
 
        pm_prepare_console();
 
+       disable_nonboot_cpus();
+
+       if (num_online_cpus() != 1) {
+               error = -EPERM;
+               goto Enable_cpu;
+       }
+
        if (freeze_processes()) {
                error = -EAGAIN;
                goto Thaw;
        }
 
+       if ((free_pages = nr_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;
        }
 
-       if ((error = device_suspend(state)))
+       if ((error = device_suspend(PMSG_SUSPEND))) {
+               printk(KERN_ERR "Some devices failed to suspend\n");
                goto Finish;
+       }
        return 0;
  Finish:
        if (pm_ops->finish)
                pm_ops->finish(state);
  Thaw:
        thaw_processes();
+ Enable_cpu:
+       enable_nonboot_cpus();
        pm_restore_console();
        return error;
 }
 
 
-static int suspend_enter(u32 state)
+int suspend_enter(suspend_state_t state)
 {
        int error = 0;
        unsigned long flags;
 
        local_irq_save(flags);
-       if ((error = device_power_down(state)))
+
+       if ((error = device_power_down(PMSG_SUSPEND))) {
+               printk(KERN_ERR "Some devices failed to power down\n");
                goto Done;
+       }
        error = pm_ops->enter(state);
        device_power_up();
  Done:
@@ -103,28 +127,42 @@ static int suspend_enter(u32 state)
  *     @state:         State we're coming out of.
  *
  *     Call platform code to clean up, restart processes, and free the 
- *     console that we've allocated.
+ *     console that we've allocated. This is not called for suspend-to-disk.
  */
 
-static void suspend_finish(u32 state)
+static void suspend_finish(suspend_state_t state)
 {
        device_resume();
+       thaw_processes();
+       enable_nonboot_cpus();
        if (pm_ops && pm_ops->finish)
                pm_ops->finish(state);
-       thaw_processes();
        pm_restore_console();
 }
 
 
 
 
-char * pm_states[] = {
+static char *pm_states[PM_SUSPEND_MAX] = {
        [PM_SUSPEND_STANDBY]    = "standby",
        [PM_SUSPEND_MEM]        = "mem",
+#ifdef CONFIG_SOFTWARE_SUSPEND
        [PM_SUSPEND_DISK]       = "disk",
-       NULL,
+#endif
 };
 
+static inline int valid_state(suspend_state_t state)
+{
+       /* Suspend-to-disk does not really need low-level support.
+        * It can work with reboot if needed. */
+       if (state == PM_SUSPEND_DISK)
+               return 1;
+
+       if (pm_ops && pm_ops->valid && !pm_ops->valid(state))
+               return 0;
+       return 1;
+}
+
 
 /**
  *     enter_state - Do common work of entering low-power state.
@@ -137,38 +175,43 @@ char * pm_states[] = {
  *     we've woken up).
  */
 
-static int enter_state(u32 state)
+static int enter_state(suspend_state_t state)
 {
        int error;
 
+       if (!valid_state(state))
+               return -ENODEV;
        if (down_trylock(&pm_sem))
                return -EBUSY;
 
-       /* Suspend is hard to get right on SMP. */
-       if (num_online_cpus() != 1) {
-               error = -EPERM;
-               goto Unlock;
-       }
-
        if (state == PM_SUSPEND_DISK) {
                error = pm_suspend_disk();
                goto Unlock;
        }
 
-       pr_debug("PM: Preparing system for suspend\n");
+       pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
        if ((error = suspend_prepare(state)))
                goto Unlock;
 
-       pr_debug("PM: Entering state.\n");
+       pr_debug("PM: Entering %s sleep\n", pm_states[state]);
        error = suspend_enter(state);
 
-       pr_debug("PM: Finishing up.\n");
+       pr_debug("PM: Finishing wakeup.\n");
        suspend_finish(state);
  Unlock:
        up(&pm_sem);
        return error;
 }
 
+/*
+ * This is main interface to the outside world. It needs to be
+ * called from process context.
+ */
+int software_suspend(void)
+{
+       return enter_state(PM_SUSPEND_DISK);
+}
+
 
 /**
  *     pm_suspend - Externally visible function for suspending system.
@@ -178,9 +221,9 @@ static int enter_state(u32 state)
  *     structure, and enter (above).
  */
 
-int pm_suspend(u32 state)
+int pm_suspend(suspend_state_t state)
 {
-       if (state > PM_SUSPEND_ON && state < PM_SUSPEND_MAX)
+       if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
                return enter_state(state);
        return -EINVAL;
 }
@@ -207,8 +250,8 @@ static ssize_t state_show(struct subsystem * subsys, char * buf)
        char * s = buf;
 
        for (i = 0; i < PM_SUSPEND_MAX; i++) {
-               if (pm_states[i])
-                       s += sprintf(s,"%s ",pm_states[i]);
+               if (pm_states[i] && valid_state(i))
+                       s += sprintf(s,"%s ", pm_states[i]);
        }
        s += sprintf(s,"\n");
        return (s - buf);
@@ -216,7 +259,7 @@ static ssize_t state_show(struct subsystem * subsys, char * buf)
 
 static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
 {
-       u32 state = PM_SUSPEND_STANDBY;
+       suspend_state_t state = PM_SUSPEND_STANDBY;
        char ** s;
        char *p;
        int error;
@@ -225,11 +268,11 @@ static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n
        p = memchr(buf, '\n', n);
        len = p ? p - buf : n;
 
-       for (s = &pm_states[state]; *s; s++, state++) {
-               if (!strncmp(buf, *s, len))
+       for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
+               if (*s && !strncmp(buf, *s, len))
                        break;
        }
-       if (*s)
+       if (state < PM_SUSPEND_MAX && *s)
                error = enter_state(state);
        else
                error = -EINVAL;