#include <linux/resume-trace.h>
 #include <linux/interrupt.h>
 #include <linux/sched.h>
+#include <linux/async.h>
 
 #include "../base.h"
 #include "power.h"
 LIST_HEAD(dpm_list);
 
 static DEFINE_MUTEX(dpm_list_mtx);
+static pm_message_t pm_transition;
 
 /*
  * Set once the preparation of devices for a PM transition has started, reset
 void device_pm_init(struct device *dev)
 {
        dev->power.status = DPM_ON;
+       init_completion(&dev->power.completion);
        pm_runtime_init(dev);
 }
 
        pr_debug("PM: Removing info for %s:%s\n",
                 dev->bus ? dev->bus->name : "No Bus",
                 kobject_name(&dev->kobj));
+       complete_all(&dev->power.completion);
        mutex_lock(&dpm_list_mtx);
        list_del_init(&dev->power.entry);
        mutex_unlock(&dpm_list_mtx);
        }
 }
 
+/**
+ * dpm_wait - Wait for a PM operation to complete.
+ * @dev: Device to wait for.
+ * @async: If unset, wait only if the device's power.async_suspend flag is set.
+ */
+static void dpm_wait(struct device *dev, bool async)
+{
+       if (!dev)
+               return;
+
+       if (async || dev->power.async_suspend)
+               wait_for_completion(&dev->power.completion);
+}
+
+static int dpm_wait_fn(struct device *dev, void *async_ptr)
+{
+       dpm_wait(dev, *((bool *)async_ptr));
+       return 0;
+}
+
+static void dpm_wait_for_children(struct device *dev, bool async)
+{
+       device_for_each_child(dev, &async, dpm_wait_fn);
+}
+
 /**
  * pm_op - Execute the PM operation appropriate for given PM event.
  * @dev: Device to handle.
 }
 
 /**
- * device_resume - Execute "resume" callbacks for given device.
+ * __device_resume - Execute "resume" callbacks for given device.
  * @dev: Device to handle.
  * @state: PM transition of the system being carried out.
+ * @async: If true, the device is being resumed asynchronously.
  */
-static int device_resume(struct device *dev, pm_message_t state)
+static int __device_resume(struct device *dev, pm_message_t state, bool async)
 {
        int error = 0;
 
        TRACE_DEVICE(dev);
        TRACE_RESUME(0);
 
+       dpm_wait(dev->parent, async);
        down(&dev->sem);
 
        if (dev->bus) {
        }
  End:
        up(&dev->sem);
+       complete_all(&dev->power.completion);
 
        TRACE_RESUME(error);
        return error;
 }
 
+static void async_resume(void *data, async_cookie_t cookie)
+{
+       struct device *dev = (struct device *)data;
+       int error;
+
+       error = __device_resume(dev, pm_transition, true);
+       if (error)
+               pm_dev_err(dev, pm_transition, " async", error);
+       put_device(dev);
+}
+
+static int device_resume(struct device *dev)
+{
+       INIT_COMPLETION(dev->power.completion);
+
+       if (dev->power.async_suspend && !pm_trace_is_enabled()) {
+               get_device(dev);
+               async_schedule(async_resume, dev);
+               return 0;
+       }
+
+       return __device_resume(dev, pm_transition, false);
+}
+
 /**
  * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  * @state: PM transition of the system being carried out.
 
        INIT_LIST_HEAD(&list);
        mutex_lock(&dpm_list_mtx);
+       pm_transition = state;
        while (!list_empty(&dpm_list)) {
                struct device *dev = to_device(dpm_list.next);
 
                        dev->power.status = DPM_RESUMING;
                        mutex_unlock(&dpm_list_mtx);
 
-                       error = device_resume(dev, state);
+                       error = device_resume(dev);
 
                        mutex_lock(&dpm_list_mtx);
                        if (error)
        }
        list_splice(&list, &dpm_list);
        mutex_unlock(&dpm_list_mtx);
+       async_synchronize_full();
        dpm_show_time(starttime, state, NULL);
 }
 
        return error;
 }
 
+static int async_error;
+
 /**
  * device_suspend - Execute "suspend" callbacks for given device.
  * @dev: Device to handle.
  * @state: PM transition of the system being carried out.
+ * @async: If true, the device is being suspended asynchronously.
  */
-static int device_suspend(struct device *dev, pm_message_t state)
+static int __device_suspend(struct device *dev, pm_message_t state, bool async)
 {
        int error = 0;
 
+       dpm_wait_for_children(dev, async);
        down(&dev->sem);
 
+       if (async_error)
+               goto End;
+
        if (dev->class) {
                if (dev->class->pm) {
                        pm_dev_dbg(dev, state, "class ");
                        error = legacy_suspend(dev, state, dev->bus->suspend);
                }
        }
+
+       if (!error)
+               dev->power.status = DPM_OFF;
+
  End:
        up(&dev->sem);
+       complete_all(&dev->power.completion);
 
        return error;
 }
 
+static void async_suspend(void *data, async_cookie_t cookie)
+{
+       struct device *dev = (struct device *)data;
+       int error;
+
+       error = __device_suspend(dev, pm_transition, true);
+       if (error) {
+               pm_dev_err(dev, pm_transition, " async", error);
+               async_error = error;
+       }
+
+       put_device(dev);
+}
+
+static int device_suspend(struct device *dev)
+{
+       INIT_COMPLETION(dev->power.completion);
+
+       if (dev->power.async_suspend) {
+               get_device(dev);
+               async_schedule(async_suspend, dev);
+               return 0;
+       }
+
+       return __device_suspend(dev, pm_transition, false);
+}
+
 /**
  * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  * @state: PM transition of the system being carried out.
 
        INIT_LIST_HEAD(&list);
        mutex_lock(&dpm_list_mtx);
+       pm_transition = state;
+       async_error = 0;
        while (!list_empty(&dpm_list)) {
                struct device *dev = to_device(dpm_list.prev);
 
                get_device(dev);
                mutex_unlock(&dpm_list_mtx);
 
-               error = device_suspend(dev, state);
+               error = device_suspend(dev);
 
                mutex_lock(&dpm_list_mtx);
                if (error) {
                        put_device(dev);
                        break;
                }
-               dev->power.status = DPM_OFF;
                if (!list_empty(&dev->power.entry))
                        list_move(&dev->power.entry, &list);
                put_device(dev);
+               if (async_error)
+                       break;
        }
        list_splice(&list, dpm_list.prev);
        mutex_unlock(&dpm_list_mtx);
+       async_synchronize_full();
+       if (!error)
+               error = async_error;
        if (!error)
                dpm_show_time(starttime, state, NULL);
        return error;