Pull bugfix into test branch
[firefly-linux-kernel-4.4.55.git] / kernel / power / disk.c
index d79feeb45459f49ac0856e49d604255cfe768646..88fc5d7ac737402c18db0306049b5873effeed55 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/pm.h>
 #include <linux/console.h>
 #include <linux/cpu.h>
+#include <linux/freezer.h>
 
 #include "power.h"
 
@@ -29,6 +30,22 @@ char resume_file[256] = CONFIG_PM_STD_PARTITION;
 dev_t swsusp_resume_device;
 sector_t swsusp_resume_block;
 
+/**
+ *     platform_prepare - prepare the machine for hibernation using the
+ *     platform driver if so configured and return an error code if it fails
+ */
+
+static inline int platform_prepare(void)
+{
+       int error = 0;
+
+       if (pm_disk_mode == PM_DISK_PLATFORM) {
+               if (pm_ops && pm_ops->prepare)
+                       error = pm_ops->prepare(PM_SUSPEND_DISK);
+       }
+       return error;
+}
+
 /**
  *     power_down - Shut machine down for hibernate.
  *     @mode:          Suspend-to-disk mode
@@ -41,13 +58,13 @@ sector_t swsusp_resume_block;
 
 static void power_down(suspend_disk_method_t mode)
 {
-       int error = 0;
-
        switch(mode) {
        case PM_DISK_PLATFORM:
-               kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
-               error = pm_ops->enter(PM_SUSPEND_DISK);
-               break;
+               if (pm_ops && pm_ops->enter) {
+                       kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
+                       pm_ops->enter(PM_SUSPEND_DISK);
+                       break;
+               }
        case PM_DISK_SHUTDOWN:
                kernel_power_off();
                break;
@@ -91,12 +108,18 @@ static int prepare_processes(void)
                goto thaw;
        }
 
+       error = platform_prepare();
+       if (error)
+               goto thaw;
+
        /* Free memory before shutting down devices. */
        if (!(error = swsusp_shrink_memory()))
                return 0;
-thaw:
+
+       platform_finish();
+ thaw:
        thaw_processes();
-enable_cpus:
+ enable_cpus:
        enable_nonboot_cpus();
        pm_restore_console();
        return error;
@@ -128,7 +151,7 @@ int pm_suspend_disk(void)
                return error;
 
        if (pm_disk_mode == PM_DISK_TESTPROC)
-               goto Thaw;
+               return 0;
 
        suspend_console();
        error = device_suspend(PMSG_FREEZE);
@@ -190,10 +213,10 @@ static int software_resume(void)
 {
        int error;
 
-       down(&pm_sem);
+       mutex_lock(&pm_mutex);
        if (!swsusp_resume_device) {
                if (!strlen(resume_file)) {
-                       up(&pm_sem);
+                       mutex_unlock(&pm_mutex);
                        return -ENOENT;
                }
                swsusp_resume_device = name_to_dev_t(resume_file);
@@ -208,7 +231,7 @@ static int software_resume(void)
                 * FIXME: If noresume is specified, we need to find the partition
                 * and reset it back to normal swap space.
                 */
-               up(&pm_sem);
+               mutex_unlock(&pm_mutex);
                return 0;
        }
 
@@ -252,7 +275,7 @@ static int software_resume(void)
        unprepare_processes();
  Done:
        /* For success case, the suspend path will release the lock */
-       up(&pm_sem);
+       mutex_unlock(&pm_mutex);
        pr_debug("PM: Resume from disk failed.\n");
        return 0;
 }
@@ -313,7 +336,7 @@ static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
        p = memchr(buf, '\n', n);
        len = p ? p - buf : n;
 
-       down(&pm_sem);
+       mutex_lock(&pm_mutex);
        for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
                if (!strncmp(buf, pm_disk_modes[i], len)) {
                        mode = i;
@@ -337,7 +360,7 @@ static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
 
        pr_debug("PM: suspend-to-disk mode set to '%s'\n",
                 pm_disk_modes[mode]);
-       up(&pm_sem);
+       mutex_unlock(&pm_mutex);
        return error ? error : n;
 }
 
@@ -362,14 +385,14 @@ static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
        if (maj != MAJOR(res) || min != MINOR(res))
                goto out;
 
-       down(&pm_sem);
+       mutex_lock(&pm_mutex);
        swsusp_resume_device = res;
-       up(&pm_sem);
+       mutex_unlock(&pm_mutex);
        printk("Attempting manual resume\n");
        noresume = 0;
        software_resume();
        ret = n;
-out:
+ out:
        return ret;
 }