Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/power/main.c - Where the driver meets power management. |
| 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Open Source Development Lab |
| 6 | * |
| 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | * |
| 10 | * The driver model core calls device_pm_add() when a device is registered. |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 11 | * This will initialize the embedded device_pm_info object in the device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * and add it to the list of power-controlled devices. sysfs entries for |
| 13 | * controlling device power management will also be added. |
| 14 | * |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 15 | * A separate list is used for keeping track of power info, because the power |
| 16 | * domain dependencies may differ from the ancestral dependencies that the |
| 17 | * subsystem list maintains. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/device.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 21 | #include <linux/kallsyms.h> |
Paul Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame] | 22 | #include <linux/export.h> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 24 | #include <linux/pm.h> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 26 | #include <linux/resume-trace.h> |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 28 | #include <linux/sched.h> |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 29 | #include <linux/async.h> |
Rafael J. Wysocki | 1e75227 | 2010-12-03 22:58:05 +0100 | [diff] [blame] | 30 | #include <linux/suspend.h> |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 31 | #include <trace/events/power.h> |
Preeti U Murthy | 8651f97 | 2012-07-09 10:12:56 +0200 | [diff] [blame] | 32 | #include <linux/cpuidle.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 33 | #include "../base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "power.h" |
| 35 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 36 | typedef int (*pm_callback_t)(struct device *); |
| 37 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 38 | /* |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 39 | * The entries in the dpm_list list are in a depth first order, simply |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 40 | * because children are guaranteed to be discovered after parents, and |
| 41 | * are inserted at the back of the list on discovery. |
| 42 | * |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 43 | * Since device_pm_add() may be called with a device lock held, |
| 44 | * we must never try to acquire a device lock while holding |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 45 | * dpm_list_mutex. |
| 46 | */ |
| 47 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 48 | LIST_HEAD(dpm_list); |
Sachin Kamat | 7664e96 | 2012-07-17 22:38:08 +0200 | [diff] [blame] | 49 | static LIST_HEAD(dpm_prepared_list); |
| 50 | static LIST_HEAD(dpm_suspended_list); |
| 51 | static LIST_HEAD(dpm_late_early_list); |
| 52 | static LIST_HEAD(dpm_noirq_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 54 | struct suspend_stats suspend_stats; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 55 | static DEFINE_MUTEX(dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 56 | static pm_message_t pm_transition; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 58 | static int async_error; |
| 59 | |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 60 | static char *pm_verb(int event) |
| 61 | { |
| 62 | switch (event) { |
| 63 | case PM_EVENT_SUSPEND: |
| 64 | return "suspend"; |
| 65 | case PM_EVENT_RESUME: |
| 66 | return "resume"; |
| 67 | case PM_EVENT_FREEZE: |
| 68 | return "freeze"; |
| 69 | case PM_EVENT_QUIESCE: |
| 70 | return "quiesce"; |
| 71 | case PM_EVENT_HIBERNATE: |
| 72 | return "hibernate"; |
| 73 | case PM_EVENT_THAW: |
| 74 | return "thaw"; |
| 75 | case PM_EVENT_RESTORE: |
| 76 | return "restore"; |
| 77 | case PM_EVENT_RECOVER: |
| 78 | return "recover"; |
| 79 | default: |
| 80 | return "(unknown PM event)"; |
| 81 | } |
| 82 | } |
| 83 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 84 | /** |
Rafael J. Wysocki | e91c11b | 2012-08-06 01:44:28 +0200 | [diff] [blame] | 85 | * device_pm_sleep_init - Initialize system suspend-related device fields. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 86 | * @dev: Device object being initialized. |
| 87 | */ |
Rafael J. Wysocki | e91c11b | 2012-08-06 01:44:28 +0200 | [diff] [blame] | 88 | void device_pm_sleep_init(struct device *dev) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 89 | { |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 90 | dev->power.is_prepared = false; |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 91 | dev->power.is_suspended = false; |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 92 | init_completion(&dev->power.completion); |
Colin Cross | 152e1d5 | 2010-09-03 01:24:07 +0200 | [diff] [blame] | 93 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 94 | dev->power.wakeup = NULL; |
Rafael J. Wysocki | 22110fa | 2011-04-26 11:33:09 +0200 | [diff] [blame] | 95 | INIT_LIST_HEAD(&dev->power.entry); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 99 | * device_pm_lock - Lock the list of active devices used by the PM core. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 100 | */ |
| 101 | void device_pm_lock(void) |
| 102 | { |
| 103 | mutex_lock(&dpm_list_mtx); |
| 104 | } |
| 105 | |
| 106 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 107 | * device_pm_unlock - Unlock the list of active devices used by the PM core. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 108 | */ |
| 109 | void device_pm_unlock(void) |
| 110 | { |
| 111 | mutex_unlock(&dpm_list_mtx); |
| 112 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 113 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 114 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 115 | * device_pm_add - Add a device to the PM core's list of active devices. |
| 116 | * @dev: Device to add to the list. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 117 | */ |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 118 | void device_pm_add(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | pr_debug("PM: Adding info for %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 121 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 122 | mutex_lock(&dpm_list_mtx); |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 123 | if (dev->parent && dev->parent->power.is_prepared) |
Rafael J. Wysocki | b64959e | 2010-12-16 17:11:45 +0100 | [diff] [blame] | 124 | dev_warn(dev, "parent %s should not be sleeping\n", |
| 125 | dev_name(dev->parent)); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 126 | list_add_tail(&dev->power.entry, &dpm_list); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 127 | mutex_unlock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 130 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 131 | * device_pm_remove - Remove a device from the PM core's list of active devices. |
| 132 | * @dev: Device to be removed from the list. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 133 | */ |
Rafael J. Wysocki | 9cddad7 | 2007-06-13 15:53:34 +0200 | [diff] [blame] | 134 | void device_pm_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
| 136 | pr_debug("PM: Removing info for %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 137 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 138 | complete_all(&dev->power.completion); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 139 | mutex_lock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | list_del_init(&dev->power.entry); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 141 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 142 | device_wakeup_disable(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 143 | pm_runtime_remove(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 146 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 147 | * device_pm_move_before - Move device in the PM core's list of active devices. |
| 148 | * @deva: Device to move in dpm_list. |
| 149 | * @devb: Device @deva should come before. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 150 | */ |
| 151 | void device_pm_move_before(struct device *deva, struct device *devb) |
| 152 | { |
| 153 | pr_debug("PM: Moving %s:%s before %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 154 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), |
| 155 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 156 | /* Delete deva from dpm_list and reinsert before devb. */ |
| 157 | list_move_tail(&deva->power.entry, &devb->power.entry); |
| 158 | } |
| 159 | |
| 160 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 161 | * device_pm_move_after - Move device in the PM core's list of active devices. |
| 162 | * @deva: Device to move in dpm_list. |
| 163 | * @devb: Device @deva should come after. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 164 | */ |
| 165 | void device_pm_move_after(struct device *deva, struct device *devb) |
| 166 | { |
| 167 | pr_debug("PM: Moving %s:%s after %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 168 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), |
| 169 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 170 | /* Delete deva from dpm_list and reinsert after devb. */ |
| 171 | list_move(&deva->power.entry, &devb->power.entry); |
| 172 | } |
| 173 | |
| 174 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 175 | * device_pm_move_last - Move device to end of the PM core's list of devices. |
| 176 | * @dev: Device to move in dpm_list. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 177 | */ |
| 178 | void device_pm_move_last(struct device *dev) |
| 179 | { |
| 180 | pr_debug("PM: Moving %s:%s to end of list\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 181 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 182 | list_move_tail(&dev->power.entry, &dpm_list); |
| 183 | } |
| 184 | |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 185 | static ktime_t initcall_debug_start(struct device *dev) |
| 186 | { |
| 187 | ktime_t calltime = ktime_set(0, 0); |
| 188 | |
Rafael J. Wysocki | b2df1d4 | 2012-06-21 00:19:33 +0200 | [diff] [blame] | 189 | if (pm_print_times_enabled) { |
Rafael J. Wysocki | 0c6aebe | 2011-12-03 00:23:43 +0100 | [diff] [blame] | 190 | pr_info("calling %s+ @ %i, parent: %s\n", |
| 191 | dev_name(dev), task_pid_nr(current), |
| 192 | dev->parent ? dev_name(dev->parent) : "none"); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 193 | calltime = ktime_get(); |
| 194 | } |
| 195 | |
| 196 | return calltime; |
| 197 | } |
| 198 | |
| 199 | static void initcall_debug_report(struct device *dev, ktime_t calltime, |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 200 | int error, pm_message_t state, char *info) |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 201 | { |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 202 | ktime_t rettime; |
| 203 | s64 nsecs; |
| 204 | |
| 205 | rettime = ktime_get(); |
| 206 | nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime)); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 207 | |
Rafael J. Wysocki | b2df1d4 | 2012-06-21 00:19:33 +0200 | [diff] [blame] | 208 | if (pm_print_times_enabled) { |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 209 | pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev), |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 210 | error, (unsigned long long)nsecs >> 10); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 211 | } |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 212 | |
| 213 | trace_device_pm_report_time(dev, info, nsecs, pm_verb(state.event), |
| 214 | error); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 217 | /** |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 218 | * dpm_wait - Wait for a PM operation to complete. |
| 219 | * @dev: Device to wait for. |
| 220 | * @async: If unset, wait only if the device's power.async_suspend flag is set. |
| 221 | */ |
| 222 | static void dpm_wait(struct device *dev, bool async) |
| 223 | { |
| 224 | if (!dev) |
| 225 | return; |
| 226 | |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 227 | if (async || (pm_async_enabled && dev->power.async_suspend)) |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 228 | wait_for_completion(&dev->power.completion); |
| 229 | } |
| 230 | |
| 231 | static int dpm_wait_fn(struct device *dev, void *async_ptr) |
| 232 | { |
| 233 | dpm_wait(dev, *((bool *)async_ptr)); |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static void dpm_wait_for_children(struct device *dev, bool async) |
| 238 | { |
| 239 | device_for_each_child(dev, &async, dpm_wait_fn); |
| 240 | } |
| 241 | |
| 242 | /** |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 243 | * pm_op - Return the PM operation appropriate for given PM event. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 244 | * @ops: PM operations to choose from. |
| 245 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 246 | */ |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 247 | static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 248 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 249 | switch (state.event) { |
| 250 | #ifdef CONFIG_SUSPEND |
| 251 | case PM_EVENT_SUSPEND: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 252 | return ops->suspend; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 253 | case PM_EVENT_RESUME: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 254 | return ops->resume; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 255 | #endif /* CONFIG_SUSPEND */ |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 256 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 257 | case PM_EVENT_FREEZE: |
| 258 | case PM_EVENT_QUIESCE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 259 | return ops->freeze; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 260 | case PM_EVENT_HIBERNATE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 261 | return ops->poweroff; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 262 | case PM_EVENT_THAW: |
| 263 | case PM_EVENT_RECOVER: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 264 | return ops->thaw; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 265 | break; |
| 266 | case PM_EVENT_RESTORE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 267 | return ops->restore; |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 268 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 269 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 270 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 271 | return NULL; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 275 | * pm_late_early_op - Return the PM operation appropriate for given PM event. |
| 276 | * @ops: PM operations to choose from. |
| 277 | * @state: PM transition of the system being carried out. |
| 278 | * |
| 279 | * Runtime PM is disabled for @dev while this function is being executed. |
| 280 | */ |
| 281 | static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops, |
| 282 | pm_message_t state) |
| 283 | { |
| 284 | switch (state.event) { |
| 285 | #ifdef CONFIG_SUSPEND |
| 286 | case PM_EVENT_SUSPEND: |
| 287 | return ops->suspend_late; |
| 288 | case PM_EVENT_RESUME: |
| 289 | return ops->resume_early; |
| 290 | #endif /* CONFIG_SUSPEND */ |
| 291 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
| 292 | case PM_EVENT_FREEZE: |
| 293 | case PM_EVENT_QUIESCE: |
| 294 | return ops->freeze_late; |
| 295 | case PM_EVENT_HIBERNATE: |
| 296 | return ops->poweroff_late; |
| 297 | case PM_EVENT_THAW: |
| 298 | case PM_EVENT_RECOVER: |
| 299 | return ops->thaw_early; |
| 300 | case PM_EVENT_RESTORE: |
| 301 | return ops->restore_early; |
| 302 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
| 303 | } |
| 304 | |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | /** |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 309 | * pm_noirq_op - Return the PM operation appropriate for given PM event. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 310 | * @ops: PM operations to choose from. |
| 311 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 312 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 313 | * The driver of @dev will not receive interrupts while this function is being |
| 314 | * executed. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 315 | */ |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 316 | static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 317 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 318 | switch (state.event) { |
| 319 | #ifdef CONFIG_SUSPEND |
| 320 | case PM_EVENT_SUSPEND: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 321 | return ops->suspend_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 322 | case PM_EVENT_RESUME: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 323 | return ops->resume_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 324 | #endif /* CONFIG_SUSPEND */ |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 325 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 326 | case PM_EVENT_FREEZE: |
| 327 | case PM_EVENT_QUIESCE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 328 | return ops->freeze_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 329 | case PM_EVENT_HIBERNATE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 330 | return ops->poweroff_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 331 | case PM_EVENT_THAW: |
| 332 | case PM_EVENT_RECOVER: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 333 | return ops->thaw_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 334 | case PM_EVENT_RESTORE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 335 | return ops->restore_noirq; |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 336 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 337 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 338 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 339 | return NULL; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 342 | static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info) |
| 343 | { |
| 344 | dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event), |
| 345 | ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ? |
| 346 | ", may wakeup" : ""); |
| 347 | } |
| 348 | |
| 349 | static void pm_dev_err(struct device *dev, pm_message_t state, char *info, |
| 350 | int error) |
| 351 | { |
| 352 | printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 353 | dev_name(dev), pm_verb(state.event), info, error); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 356 | static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info) |
| 357 | { |
| 358 | ktime_t calltime; |
Kevin Cernekee | 0702d9ee | 2010-09-20 22:32:10 +0200 | [diff] [blame] | 359 | u64 usecs64; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 360 | int usecs; |
| 361 | |
| 362 | calltime = ktime_get(); |
| 363 | usecs64 = ktime_to_ns(ktime_sub(calltime, starttime)); |
| 364 | do_div(usecs64, NSEC_PER_USEC); |
| 365 | usecs = usecs64; |
| 366 | if (usecs == 0) |
| 367 | usecs = 1; |
| 368 | pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n", |
| 369 | info ?: "", info ? " " : "", pm_verb(state.event), |
| 370 | usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC); |
| 371 | } |
| 372 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 373 | static int dpm_run_callback(pm_callback_t cb, struct device *dev, |
| 374 | pm_message_t state, char *info) |
| 375 | { |
| 376 | ktime_t calltime; |
| 377 | int error; |
| 378 | |
| 379 | if (!cb) |
| 380 | return 0; |
| 381 | |
| 382 | calltime = initcall_debug_start(dev); |
| 383 | |
| 384 | pm_dev_dbg(dev, state, info); |
| 385 | error = cb(dev); |
| 386 | suspend_report_result(cb, error); |
| 387 | |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 388 | initcall_debug_report(dev, calltime, error, state, info); |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 389 | |
| 390 | return error; |
| 391 | } |
| 392 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 393 | /*------------------------- Resume routines -------------------------*/ |
| 394 | |
| 395 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 396 | * device_resume_noirq - Execute an "early resume" callback for given device. |
| 397 | * @dev: Device to handle. |
| 398 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 399 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 400 | * The driver of @dev will not receive interrupts while this function is being |
| 401 | * executed. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 402 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 403 | static int device_resume_noirq(struct device *dev, pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 404 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 405 | pm_callback_t callback = NULL; |
| 406 | char *info = NULL; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 407 | int error = 0; |
| 408 | |
| 409 | TRACE_DEVICE(dev); |
| 410 | TRACE_RESUME(0); |
| 411 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 412 | if (dev->power.syscore) |
| 413 | goto Out; |
| 414 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 415 | if (dev->pm_domain) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 416 | info = "noirq power domain "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 417 | callback = pm_noirq_op(&dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 418 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 419 | info = "noirq type "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 420 | callback = pm_noirq_op(dev->type->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 421 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 422 | info = "noirq class "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 423 | callback = pm_noirq_op(dev->class->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 424 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 425 | info = "noirq bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 426 | callback = pm_noirq_op(dev->bus->pm, state); |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 429 | if (!callback && dev->driver && dev->driver->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 430 | info = "noirq driver "; |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 431 | callback = pm_noirq_op(dev->driver->pm, state); |
| 432 | } |
| 433 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 434 | error = dpm_run_callback(callback, dev, state, info); |
| 435 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 436 | Out: |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 437 | TRACE_RESUME(error); |
| 438 | return error; |
| 439 | } |
| 440 | |
| 441 | /** |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 442 | * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 443 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 444 | * |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 445 | * Call the "noirq" resume handlers for all devices in dpm_noirq_list and |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 446 | * enable device drivers to receive interrupts. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 447 | */ |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 448 | static void dpm_resume_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 449 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 450 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 451 | |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 452 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 453 | while (!list_empty(&dpm_noirq_list)) { |
| 454 | struct device *dev = to_device(dpm_noirq_list.next); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 455 | int error; |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 456 | |
| 457 | get_device(dev); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 458 | list_move_tail(&dev->power.entry, &dpm_late_early_list); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 459 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 460 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 461 | error = device_resume_noirq(dev, state); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 462 | if (error) { |
| 463 | suspend_stats.failed_resume_noirq++; |
| 464 | dpm_save_failed_step(SUSPEND_RESUME_NOIRQ); |
| 465 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 466 | pm_dev_err(dev, state, " noirq", error); |
| 467 | } |
| 468 | |
| 469 | mutex_lock(&dpm_list_mtx); |
| 470 | put_device(dev); |
| 471 | } |
| 472 | mutex_unlock(&dpm_list_mtx); |
| 473 | dpm_show_time(starttime, state, "noirq"); |
| 474 | resume_device_irqs(); |
Preeti U Murthy | 8651f97 | 2012-07-09 10:12:56 +0200 | [diff] [blame] | 475 | cpuidle_resume(); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /** |
| 479 | * device_resume_early - Execute an "early resume" callback for given device. |
| 480 | * @dev: Device to handle. |
| 481 | * @state: PM transition of the system being carried out. |
| 482 | * |
| 483 | * Runtime PM is disabled for @dev while this function is being executed. |
| 484 | */ |
| 485 | static int device_resume_early(struct device *dev, pm_message_t state) |
| 486 | { |
| 487 | pm_callback_t callback = NULL; |
| 488 | char *info = NULL; |
| 489 | int error = 0; |
| 490 | |
| 491 | TRACE_DEVICE(dev); |
| 492 | TRACE_RESUME(0); |
| 493 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 494 | if (dev->power.syscore) |
| 495 | goto Out; |
| 496 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 497 | if (dev->pm_domain) { |
| 498 | info = "early power domain "; |
| 499 | callback = pm_late_early_op(&dev->pm_domain->ops, state); |
| 500 | } else if (dev->type && dev->type->pm) { |
| 501 | info = "early type "; |
| 502 | callback = pm_late_early_op(dev->type->pm, state); |
| 503 | } else if (dev->class && dev->class->pm) { |
| 504 | info = "early class "; |
| 505 | callback = pm_late_early_op(dev->class->pm, state); |
| 506 | } else if (dev->bus && dev->bus->pm) { |
| 507 | info = "early bus "; |
| 508 | callback = pm_late_early_op(dev->bus->pm, state); |
| 509 | } |
| 510 | |
| 511 | if (!callback && dev->driver && dev->driver->pm) { |
| 512 | info = "early driver "; |
| 513 | callback = pm_late_early_op(dev->driver->pm, state); |
| 514 | } |
| 515 | |
| 516 | error = dpm_run_callback(callback, dev, state, info); |
| 517 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 518 | Out: |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 519 | TRACE_RESUME(error); |
Rafael J. Wysocki | 9f6d8f6 | 2012-12-22 23:59:01 +0100 | [diff] [blame] | 520 | |
| 521 | pm_runtime_enable(dev); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 522 | return error; |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * dpm_resume_early - Execute "early resume" callbacks for all devices. |
| 527 | * @state: PM transition of the system being carried out. |
| 528 | */ |
| 529 | static void dpm_resume_early(pm_message_t state) |
| 530 | { |
| 531 | ktime_t starttime = ktime_get(); |
| 532 | |
| 533 | mutex_lock(&dpm_list_mtx); |
| 534 | while (!list_empty(&dpm_late_early_list)) { |
| 535 | struct device *dev = to_device(dpm_late_early_list.next); |
| 536 | int error; |
| 537 | |
| 538 | get_device(dev); |
| 539 | list_move_tail(&dev->power.entry, &dpm_suspended_list); |
| 540 | mutex_unlock(&dpm_list_mtx); |
| 541 | |
| 542 | error = device_resume_early(dev, state); |
| 543 | if (error) { |
| 544 | suspend_stats.failed_resume_early++; |
| 545 | dpm_save_failed_step(SUSPEND_RESUME_EARLY); |
| 546 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 547 | pm_dev_err(dev, state, " early", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 548 | } |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 549 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 550 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 551 | put_device(dev); |
| 552 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 553 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 554 | dpm_show_time(starttime, state, "early"); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 555 | } |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 556 | |
| 557 | /** |
| 558 | * dpm_resume_start - Execute "noirq" and "early" device callbacks. |
| 559 | * @state: PM transition of the system being carried out. |
| 560 | */ |
| 561 | void dpm_resume_start(pm_message_t state) |
| 562 | { |
| 563 | dpm_resume_noirq(state); |
| 564 | dpm_resume_early(state); |
| 565 | } |
| 566 | EXPORT_SYMBOL_GPL(dpm_resume_start); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 567 | |
| 568 | /** |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 569 | * device_resume - Execute "resume" callbacks for given device. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 570 | * @dev: Device to handle. |
| 571 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 572 | * @async: If true, the device is being resumed asynchronously. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 573 | */ |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 574 | static int device_resume(struct device *dev, pm_message_t state, bool async) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 575 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 576 | pm_callback_t callback = NULL; |
| 577 | char *info = NULL; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 578 | int error = 0; |
| 579 | |
| 580 | TRACE_DEVICE(dev); |
| 581 | TRACE_RESUME(0); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 582 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 583 | if (dev->power.syscore) |
| 584 | goto Complete; |
| 585 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 586 | dpm_wait(dev->parent, async); |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 587 | device_lock(dev); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 588 | |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 589 | /* |
| 590 | * This is a fib. But we'll allow new children to be added below |
| 591 | * a resumed device, even if the device hasn't been completed yet. |
| 592 | */ |
| 593 | dev->power.is_prepared = false; |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 594 | |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 595 | if (!dev->power.is_suspended) |
| 596 | goto Unlock; |
| 597 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 598 | if (dev->pm_domain) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 599 | info = "power domain "; |
| 600 | callback = pm_op(&dev->pm_domain->ops, state); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 601 | goto Driver; |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 602 | } |
| 603 | |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 604 | if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 605 | info = "type "; |
| 606 | callback = pm_op(dev->type->pm, state); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 607 | goto Driver; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | if (dev->class) { |
| 611 | if (dev->class->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 612 | info = "class "; |
| 613 | callback = pm_op(dev->class->pm, state); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 614 | goto Driver; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 615 | } else if (dev->class->resume) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 616 | info = "legacy class "; |
| 617 | callback = dev->class->resume; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 618 | goto End; |
| 619 | } |
| 620 | } |
| 621 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 622 | if (dev->bus) { |
| 623 | if (dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 624 | info = "bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 625 | callback = pm_op(dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 626 | } else if (dev->bus->resume) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 627 | info = "legacy bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 628 | callback = dev->bus->resume; |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 629 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 630 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 631 | } |
| 632 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 633 | Driver: |
| 634 | if (!callback && dev->driver && dev->driver->pm) { |
| 635 | info = "driver "; |
| 636 | callback = pm_op(dev->driver->pm, state); |
| 637 | } |
| 638 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 639 | End: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 640 | error = dpm_run_callback(callback, dev, state, info); |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 641 | dev->power.is_suspended = false; |
| 642 | |
| 643 | Unlock: |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 644 | device_unlock(dev); |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 645 | |
| 646 | Complete: |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 647 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 648 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 649 | TRACE_RESUME(error); |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 650 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 651 | return error; |
| 652 | } |
| 653 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 654 | static void async_resume(void *data, async_cookie_t cookie) |
| 655 | { |
| 656 | struct device *dev = (struct device *)data; |
| 657 | int error; |
| 658 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 659 | error = device_resume(dev, pm_transition, true); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 660 | if (error) |
| 661 | pm_dev_err(dev, pm_transition, " async", error); |
| 662 | put_device(dev); |
| 663 | } |
| 664 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 665 | static bool is_async(struct device *dev) |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 666 | { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 667 | return dev->power.async_suspend && pm_async_enabled |
| 668 | && !pm_trace_is_enabled(); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 669 | } |
| 670 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 671 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 672 | * dpm_resume - Execute "resume" callbacks for non-sysdev devices. |
| 673 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 674 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 675 | * Execute the appropriate "resume" callback for all devices whose status |
| 676 | * indicates that they are suspended. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 677 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 678 | void dpm_resume(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 679 | { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 680 | struct device *dev; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 681 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 682 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 683 | might_sleep(); |
| 684 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 685 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 686 | pm_transition = state; |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 687 | async_error = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 688 | |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 689 | list_for_each_entry(dev, &dpm_suspended_list, power.entry) { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 690 | INIT_COMPLETION(dev->power.completion); |
| 691 | if (is_async(dev)) { |
| 692 | get_device(dev); |
| 693 | async_schedule(async_resume, dev); |
| 694 | } |
| 695 | } |
| 696 | |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 697 | while (!list_empty(&dpm_suspended_list)) { |
| 698 | dev = to_device(dpm_suspended_list.next); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 699 | get_device(dev); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 700 | if (!is_async(dev)) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 701 | int error; |
| 702 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 703 | mutex_unlock(&dpm_list_mtx); |
| 704 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 705 | error = device_resume(dev, state, false); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 706 | if (error) { |
| 707 | suspend_stats.failed_resume++; |
| 708 | dpm_save_failed_step(SUSPEND_RESUME); |
| 709 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 710 | pm_dev_err(dev, state, "", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 711 | } |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 712 | |
| 713 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 714 | } |
| 715 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 716 | list_move_tail(&dev->power.entry, &dpm_prepared_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 717 | put_device(dev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 718 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 719 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 720 | async_synchronize_full(); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 721 | dpm_show_time(starttime, state, NULL); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 725 | * device_complete - Complete a PM transition for given device. |
| 726 | * @dev: Device to handle. |
| 727 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 728 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 729 | static void device_complete(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 730 | { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 731 | void (*callback)(struct device *) = NULL; |
| 732 | char *info = NULL; |
| 733 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 734 | if (dev->power.syscore) |
| 735 | return; |
| 736 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 737 | device_lock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 738 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 739 | if (dev->pm_domain) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 740 | info = "completing power domain "; |
| 741 | callback = dev->pm_domain->ops.complete; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 742 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 743 | info = "completing type "; |
| 744 | callback = dev->type->pm->complete; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 745 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 746 | info = "completing class "; |
| 747 | callback = dev->class->pm->complete; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 748 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 749 | info = "completing bus "; |
| 750 | callback = dev->bus->pm->complete; |
| 751 | } |
| 752 | |
| 753 | if (!callback && dev->driver && dev->driver->pm) { |
| 754 | info = "completing driver "; |
| 755 | callback = dev->driver->pm->complete; |
| 756 | } |
| 757 | |
| 758 | if (callback) { |
| 759 | pm_dev_dbg(dev, state, info); |
| 760 | callback(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 761 | } |
| 762 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 763 | device_unlock(dev); |
Alan Stern | 88d2613 | 2012-09-19 21:59:02 +0200 | [diff] [blame] | 764 | |
Ulf Hansson | af93933 | 2013-04-12 09:41:06 +0000 | [diff] [blame] | 765 | pm_runtime_put(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 769 | * dpm_complete - Complete a PM transition for all non-sysdev devices. |
| 770 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 771 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 772 | * Execute the ->complete() callbacks for all devices whose PM status is not |
| 773 | * DPM_ON (this allows new devices to be registered). |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 774 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 775 | void dpm_complete(pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 776 | { |
| 777 | struct list_head list; |
| 778 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 779 | might_sleep(); |
| 780 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 781 | INIT_LIST_HEAD(&list); |
| 782 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 783 | while (!list_empty(&dpm_prepared_list)) { |
| 784 | struct device *dev = to_device(dpm_prepared_list.prev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 785 | |
| 786 | get_device(dev); |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 787 | dev->power.is_prepared = false; |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 788 | list_move(&dev->power.entry, &list); |
| 789 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 790 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 791 | device_complete(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 792 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 793 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 794 | put_device(dev); |
| 795 | } |
| 796 | list_splice(&list, &dpm_list); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 797 | mutex_unlock(&dpm_list_mtx); |
| 798 | } |
| 799 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 800 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 801 | * dpm_resume_end - Execute "resume" callbacks and complete system transition. |
| 802 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 803 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 804 | * Execute "resume" callbacks for all devices and complete the PM transition of |
| 805 | * the system. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 806 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 807 | void dpm_resume_end(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 808 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 809 | dpm_resume(state); |
| 810 | dpm_complete(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 811 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 812 | EXPORT_SYMBOL_GPL(dpm_resume_end); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 813 | |
| 814 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 815 | /*------------------------- Suspend routines -------------------------*/ |
| 816 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 817 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 818 | * resume_event - Return a "resume" message for given "suspend" sleep state. |
| 819 | * @sleep_state: PM message representing a sleep state. |
| 820 | * |
| 821 | * Return a PM message representing the resume event corresponding to given |
| 822 | * sleep state. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 823 | */ |
| 824 | static pm_message_t resume_event(pm_message_t sleep_state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 825 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 826 | switch (sleep_state.event) { |
| 827 | case PM_EVENT_SUSPEND: |
| 828 | return PMSG_RESUME; |
| 829 | case PM_EVENT_FREEZE: |
| 830 | case PM_EVENT_QUIESCE: |
| 831 | return PMSG_RECOVER; |
| 832 | case PM_EVENT_HIBERNATE: |
| 833 | return PMSG_RESTORE; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 834 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 835 | return PMSG_ON; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 839 | * device_suspend_noirq - Execute a "late suspend" callback for given device. |
| 840 | * @dev: Device to handle. |
| 841 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 842 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 843 | * The driver of @dev will not receive interrupts while this function is being |
| 844 | * executed. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 845 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 846 | static int device_suspend_noirq(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 847 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 848 | pm_callback_t callback = NULL; |
| 849 | char *info = NULL; |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 850 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 851 | if (dev->power.syscore) |
| 852 | return 0; |
| 853 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 854 | if (dev->pm_domain) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 855 | info = "noirq power domain "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 856 | callback = pm_noirq_op(&dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 857 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 858 | info = "noirq type "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 859 | callback = pm_noirq_op(dev->type->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 860 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 861 | info = "noirq class "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 862 | callback = pm_noirq_op(dev->class->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 863 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 864 | info = "noirq bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 865 | callback = pm_noirq_op(dev->bus->pm, state); |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 866 | } |
| 867 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 868 | if (!callback && dev->driver && dev->driver->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 869 | info = "noirq driver "; |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 870 | callback = pm_noirq_op(dev->driver->pm, state); |
| 871 | } |
| 872 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 873 | return dpm_run_callback(callback, dev, state, info); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /** |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 877 | * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 878 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 879 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 880 | * Prevent device drivers from receiving interrupts and call the "noirq" suspend |
| 881 | * handlers for all non-sysdev devices. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 882 | */ |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 883 | static int dpm_suspend_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 884 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 885 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 886 | int error = 0; |
| 887 | |
Preeti U Murthy | 8651f97 | 2012-07-09 10:12:56 +0200 | [diff] [blame] | 888 | cpuidle_pause(); |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 889 | suspend_device_irqs(); |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 890 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 891 | while (!list_empty(&dpm_late_early_list)) { |
| 892 | struct device *dev = to_device(dpm_late_early_list.prev); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 893 | |
| 894 | get_device(dev); |
| 895 | mutex_unlock(&dpm_list_mtx); |
| 896 | |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 897 | error = device_suspend_noirq(dev, state); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 898 | |
| 899 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 900 | if (error) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 901 | pm_dev_err(dev, state, " noirq", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 902 | suspend_stats.failed_suspend_noirq++; |
| 903 | dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ); |
| 904 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 905 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 906 | break; |
| 907 | } |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 908 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 909 | list_move(&dev->power.entry, &dpm_noirq_list); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 910 | put_device(dev); |
Rafael J. Wysocki | 52d136c | 2012-04-29 22:52:19 +0200 | [diff] [blame] | 911 | |
| 912 | if (pm_wakeup_pending()) { |
| 913 | error = -EBUSY; |
| 914 | break; |
| 915 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 916 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 917 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 918 | if (error) |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 919 | dpm_resume_noirq(resume_event(state)); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 920 | else |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 921 | dpm_show_time(starttime, state, "noirq"); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 922 | return error; |
| 923 | } |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 924 | |
| 925 | /** |
| 926 | * device_suspend_late - Execute a "late suspend" callback for given device. |
| 927 | * @dev: Device to handle. |
| 928 | * @state: PM transition of the system being carried out. |
| 929 | * |
| 930 | * Runtime PM is disabled for @dev while this function is being executed. |
| 931 | */ |
| 932 | static int device_suspend_late(struct device *dev, pm_message_t state) |
| 933 | { |
| 934 | pm_callback_t callback = NULL; |
| 935 | char *info = NULL; |
| 936 | |
Rafael J. Wysocki | 9f6d8f6 | 2012-12-22 23:59:01 +0100 | [diff] [blame] | 937 | __pm_runtime_disable(dev, false); |
| 938 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 939 | if (dev->power.syscore) |
| 940 | return 0; |
| 941 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 942 | if (dev->pm_domain) { |
| 943 | info = "late power domain "; |
| 944 | callback = pm_late_early_op(&dev->pm_domain->ops, state); |
| 945 | } else if (dev->type && dev->type->pm) { |
| 946 | info = "late type "; |
| 947 | callback = pm_late_early_op(dev->type->pm, state); |
| 948 | } else if (dev->class && dev->class->pm) { |
| 949 | info = "late class "; |
| 950 | callback = pm_late_early_op(dev->class->pm, state); |
| 951 | } else if (dev->bus && dev->bus->pm) { |
| 952 | info = "late bus "; |
| 953 | callback = pm_late_early_op(dev->bus->pm, state); |
| 954 | } |
| 955 | |
| 956 | if (!callback && dev->driver && dev->driver->pm) { |
| 957 | info = "late driver "; |
| 958 | callback = pm_late_early_op(dev->driver->pm, state); |
| 959 | } |
| 960 | |
| 961 | return dpm_run_callback(callback, dev, state, info); |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * dpm_suspend_late - Execute "late suspend" callbacks for all devices. |
| 966 | * @state: PM transition of the system being carried out. |
| 967 | */ |
| 968 | static int dpm_suspend_late(pm_message_t state) |
| 969 | { |
| 970 | ktime_t starttime = ktime_get(); |
| 971 | int error = 0; |
| 972 | |
| 973 | mutex_lock(&dpm_list_mtx); |
| 974 | while (!list_empty(&dpm_suspended_list)) { |
| 975 | struct device *dev = to_device(dpm_suspended_list.prev); |
| 976 | |
| 977 | get_device(dev); |
| 978 | mutex_unlock(&dpm_list_mtx); |
| 979 | |
| 980 | error = device_suspend_late(dev, state); |
| 981 | |
| 982 | mutex_lock(&dpm_list_mtx); |
| 983 | if (error) { |
| 984 | pm_dev_err(dev, state, " late", error); |
| 985 | suspend_stats.failed_suspend_late++; |
| 986 | dpm_save_failed_step(SUSPEND_SUSPEND_LATE); |
| 987 | dpm_save_failed_dev(dev_name(dev)); |
| 988 | put_device(dev); |
| 989 | break; |
| 990 | } |
| 991 | if (!list_empty(&dev->power.entry)) |
| 992 | list_move(&dev->power.entry, &dpm_late_early_list); |
| 993 | put_device(dev); |
Rafael J. Wysocki | 52d136c | 2012-04-29 22:52:19 +0200 | [diff] [blame] | 994 | |
| 995 | if (pm_wakeup_pending()) { |
| 996 | error = -EBUSY; |
| 997 | break; |
| 998 | } |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 999 | } |
| 1000 | mutex_unlock(&dpm_list_mtx); |
| 1001 | if (error) |
| 1002 | dpm_resume_early(resume_event(state)); |
| 1003 | else |
| 1004 | dpm_show_time(starttime, state, "late"); |
| 1005 | |
| 1006 | return error; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks. |
| 1011 | * @state: PM transition of the system being carried out. |
| 1012 | */ |
| 1013 | int dpm_suspend_end(pm_message_t state) |
| 1014 | { |
| 1015 | int error = dpm_suspend_late(state); |
Colin Cross | 064b021 | 2012-07-19 10:38:06 +0200 | [diff] [blame] | 1016 | if (error) |
| 1017 | return error; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1018 | |
Colin Cross | 064b021 | 2012-07-19 10:38:06 +0200 | [diff] [blame] | 1019 | error = dpm_suspend_noirq(state); |
| 1020 | if (error) { |
Feng Hong | 997a031 | 2012-09-19 14:16:00 +0200 | [diff] [blame] | 1021 | dpm_resume_early(resume_event(state)); |
Colin Cross | 064b021 | 2012-07-19 10:38:06 +0200 | [diff] [blame] | 1022 | return error; |
| 1023 | } |
| 1024 | |
| 1025 | return 0; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1026 | } |
| 1027 | EXPORT_SYMBOL_GPL(dpm_suspend_end); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1028 | |
| 1029 | /** |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1030 | * legacy_suspend - Execute a legacy (bus or class) suspend callback for device. |
Randy Dunlap | 0a88422 | 2010-01-08 14:42:57 -0800 | [diff] [blame] | 1031 | * @dev: Device to suspend. |
| 1032 | * @state: PM transition of the system being carried out. |
| 1033 | * @cb: Suspend callback to execute. |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1034 | */ |
| 1035 | static int legacy_suspend(struct device *dev, pm_message_t state, |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 1036 | int (*cb)(struct device *dev, pm_message_t state), |
| 1037 | char *info) |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1038 | { |
| 1039 | int error; |
| 1040 | ktime_t calltime; |
| 1041 | |
| 1042 | calltime = initcall_debug_start(dev); |
| 1043 | |
| 1044 | error = cb(dev, state); |
| 1045 | suspend_report_result(cb, error); |
| 1046 | |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 1047 | initcall_debug_report(dev, calltime, error, state, info); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1048 | |
| 1049 | return error; |
| 1050 | } |
| 1051 | |
| 1052 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1053 | * device_suspend - Execute "suspend" callbacks for given device. |
| 1054 | * @dev: Device to handle. |
| 1055 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1056 | * @async: If true, the device is being suspended asynchronously. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1057 | */ |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1058 | static int __device_suspend(struct device *dev, pm_message_t state, bool async) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1059 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1060 | pm_callback_t callback = NULL; |
| 1061 | char *info = NULL; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1062 | int error = 0; |
| 1063 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1064 | dpm_wait_for_children(dev, async); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 1065 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1066 | if (async_error) |
Mandeep Singh Baines | 1f758b2 | 2012-06-24 23:31:09 +0200 | [diff] [blame] | 1067 | goto Complete; |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1068 | |
Alan Stern | 88d2613 | 2012-09-19 21:59:02 +0200 | [diff] [blame] | 1069 | /* |
| 1070 | * If a device configured to wake up the system from sleep states |
| 1071 | * has been suspended at run time and there's a resume request pending |
| 1072 | * for it, this is equivalent to the device signaling wakeup, so the |
| 1073 | * system suspend operation should be aborted. |
| 1074 | */ |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1075 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) |
| 1076 | pm_wakeup_event(dev, 0); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1077 | |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 1078 | if (pm_wakeup_pending()) { |
| 1079 | async_error = -EBUSY; |
Mandeep Singh Baines | 1f758b2 | 2012-06-24 23:31:09 +0200 | [diff] [blame] | 1080 | goto Complete; |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 1083 | if (dev->power.syscore) |
| 1084 | goto Complete; |
| 1085 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1086 | device_lock(dev); |
| 1087 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 1088 | if (dev->pm_domain) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1089 | info = "power domain "; |
| 1090 | callback = pm_op(&dev->pm_domain->ops, state); |
| 1091 | goto Run; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1094 | if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1095 | info = "type "; |
| 1096 | callback = pm_op(dev->type->pm, state); |
| 1097 | goto Run; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1098 | } |
| 1099 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1100 | if (dev->class) { |
| 1101 | if (dev->class->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1102 | info = "class "; |
| 1103 | callback = pm_op(dev->class->pm, state); |
| 1104 | goto Run; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1105 | } else if (dev->class->suspend) { |
| 1106 | pm_dev_dbg(dev, state, "legacy class "); |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 1107 | error = legacy_suspend(dev, state, dev->class->suspend, |
| 1108 | "legacy class "); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1109 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1110 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1113 | if (dev->bus) { |
| 1114 | if (dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1115 | info = "bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1116 | callback = pm_op(dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1117 | } else if (dev->bus->suspend) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1118 | pm_dev_dbg(dev, state, "legacy bus "); |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 1119 | error = legacy_suspend(dev, state, dev->bus->suspend, |
| 1120 | "legacy bus "); |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1121 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1122 | } |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1125 | Run: |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1126 | if (!callback && dev->driver && dev->driver->pm) { |
| 1127 | info = "driver "; |
| 1128 | callback = pm_op(dev->driver->pm, state); |
| 1129 | } |
| 1130 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1131 | error = dpm_run_callback(callback, dev, state, info); |
| 1132 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1133 | End: |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1134 | if (!error) { |
| 1135 | dev->power.is_suspended = true; |
Rafael J. Wysocki | 8b258cc | 2011-11-17 21:39:33 +0100 | [diff] [blame] | 1136 | if (dev->power.wakeup_path |
| 1137 | && dev->parent && !dev->parent->power.ignore_children) |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1138 | dev->parent->power.wakeup_path = true; |
| 1139 | } |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 1140 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1141 | device_unlock(dev); |
Mandeep Singh Baines | 1f758b2 | 2012-06-24 23:31:09 +0200 | [diff] [blame] | 1142 | |
| 1143 | Complete: |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1144 | complete_all(&dev->power.completion); |
Alan Stern | 88d2613 | 2012-09-19 21:59:02 +0200 | [diff] [blame] | 1145 | if (error) |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1146 | async_error = error; |
| 1147 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1148 | return error; |
| 1149 | } |
| 1150 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1151 | static void async_suspend(void *data, async_cookie_t cookie) |
| 1152 | { |
| 1153 | struct device *dev = (struct device *)data; |
| 1154 | int error; |
| 1155 | |
| 1156 | error = __device_suspend(dev, pm_transition, true); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1157 | if (error) { |
| 1158 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1159 | pm_dev_err(dev, pm_transition, " async", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1160 | } |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1161 | |
| 1162 | put_device(dev); |
| 1163 | } |
| 1164 | |
| 1165 | static int device_suspend(struct device *dev) |
| 1166 | { |
| 1167 | INIT_COMPLETION(dev->power.completion); |
| 1168 | |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 1169 | if (pm_async_enabled && dev->power.async_suspend) { |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1170 | get_device(dev); |
| 1171 | async_schedule(async_suspend, dev); |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | return __device_suspend(dev, pm_transition, false); |
| 1176 | } |
| 1177 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1178 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1179 | * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices. |
| 1180 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1181 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1182 | int dpm_suspend(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1183 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 1184 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1185 | int error = 0; |
| 1186 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1187 | might_sleep(); |
| 1188 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1189 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1190 | pm_transition = state; |
| 1191 | async_error = 0; |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1192 | while (!list_empty(&dpm_prepared_list)) { |
| 1193 | struct device *dev = to_device(dpm_prepared_list.prev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1194 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1195 | get_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1196 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1197 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1198 | error = device_suspend(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1199 | |
Alan Stern | 1b3cbec | 2008-02-29 11:50:22 -0500 | [diff] [blame] | 1200 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1201 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1202 | pm_dev_err(dev, state, "", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1203 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1204 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1205 | break; |
| 1206 | } |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 1207 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1208 | list_move(&dev->power.entry, &dpm_suspended_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1209 | put_device(dev); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1210 | if (async_error) |
| 1211 | break; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1212 | } |
| 1213 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1214 | async_synchronize_full(); |
| 1215 | if (!error) |
| 1216 | error = async_error; |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1217 | if (error) { |
| 1218 | suspend_stats.failed_suspend++; |
| 1219 | dpm_save_failed_step(SUSPEND_SUSPEND); |
| 1220 | } else |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 1221 | dpm_show_time(starttime, state, NULL); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1222 | return error; |
| 1223 | } |
| 1224 | |
| 1225 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1226 | * device_prepare - Prepare a device for system power transition. |
| 1227 | * @dev: Device to handle. |
| 1228 | * @state: PM transition of the system being carried out. |
| 1229 | * |
| 1230 | * Execute the ->prepare() callback(s) for given device. No new children of the |
| 1231 | * device may be registered after this function has returned. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1232 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1233 | static int device_prepare(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1234 | { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1235 | int (*callback)(struct device *) = NULL; |
| 1236 | char *info = NULL; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1237 | int error = 0; |
| 1238 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 1239 | if (dev->power.syscore) |
| 1240 | return 0; |
| 1241 | |
Alan Stern | 88d2613 | 2012-09-19 21:59:02 +0200 | [diff] [blame] | 1242 | /* |
| 1243 | * If a device's parent goes into runtime suspend at the wrong time, |
| 1244 | * it won't be possible to resume the device. To prevent this we |
| 1245 | * block runtime suspend here, during the prepare phase, and allow |
| 1246 | * it again during the complete phase. |
| 1247 | */ |
| 1248 | pm_runtime_get_noresume(dev); |
| 1249 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1250 | device_lock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1251 | |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1252 | dev->power.wakeup_path = device_may_wakeup(dev); |
| 1253 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 1254 | if (dev->pm_domain) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1255 | info = "preparing power domain "; |
| 1256 | callback = dev->pm_domain->ops.prepare; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1257 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1258 | info = "preparing type "; |
| 1259 | callback = dev->type->pm->prepare; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1260 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1261 | info = "preparing class "; |
| 1262 | callback = dev->class->pm->prepare; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1263 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1264 | info = "preparing bus "; |
| 1265 | callback = dev->bus->pm->prepare; |
| 1266 | } |
| 1267 | |
| 1268 | if (!callback && dev->driver && dev->driver->pm) { |
| 1269 | info = "preparing driver "; |
| 1270 | callback = dev->driver->pm->prepare; |
| 1271 | } |
| 1272 | |
| 1273 | if (callback) { |
| 1274 | error = callback(dev); |
| 1275 | suspend_report_result(callback, error); |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1278 | device_unlock(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1279 | |
Ulf Hansson | aa1b9f1 | 2013-11-13 15:45:03 +0100 | [diff] [blame^] | 1280 | if (error) |
| 1281 | pm_runtime_put(dev); |
| 1282 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1283 | return error; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1287 | * dpm_prepare - Prepare all non-sysdev devices for a system PM transition. |
| 1288 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1289 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1290 | * Execute the ->prepare() callback(s) for all devices. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1291 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1292 | int dpm_prepare(pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1293 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1294 | int error = 0; |
| 1295 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1296 | might_sleep(); |
| 1297 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1298 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1299 | while (!list_empty(&dpm_list)) { |
| 1300 | struct device *dev = to_device(dpm_list.next); |
| 1301 | |
| 1302 | get_device(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1303 | mutex_unlock(&dpm_list_mtx); |
| 1304 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1305 | error = device_prepare(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1306 | |
| 1307 | mutex_lock(&dpm_list_mtx); |
| 1308 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1309 | if (error == -EAGAIN) { |
| 1310 | put_device(dev); |
Sebastian Ott | 886a7a3 | 2009-07-08 13:26:05 +0200 | [diff] [blame] | 1311 | error = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1312 | continue; |
| 1313 | } |
Rafael J. Wysocki | 1e75227 | 2010-12-03 22:58:05 +0100 | [diff] [blame] | 1314 | printk(KERN_INFO "PM: Device %s not prepared " |
| 1315 | "for power transition: code %d\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 1316 | dev_name(dev), error); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1317 | put_device(dev); |
| 1318 | break; |
| 1319 | } |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 1320 | dev->power.is_prepared = true; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1321 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1322 | list_move_tail(&dev->power.entry, &dpm_prepared_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1323 | put_device(dev); |
| 1324 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1325 | mutex_unlock(&dpm_list_mtx); |
| 1326 | return error; |
| 1327 | } |
| 1328 | |
| 1329 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1330 | * dpm_suspend_start - Prepare devices for PM transition and suspend them. |
| 1331 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1332 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1333 | * Prepare all non-sysdev devices for system PM transition and execute "suspend" |
| 1334 | * callbacks for them. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1335 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1336 | int dpm_suspend_start(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1337 | { |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1338 | int error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1339 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1340 | error = dpm_prepare(state); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1341 | if (error) { |
| 1342 | suspend_stats.failed_prepare++; |
| 1343 | dpm_save_failed_step(SUSPEND_PREPARE); |
| 1344 | } else |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1345 | error = dpm_suspend(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1346 | return error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1347 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1348 | EXPORT_SYMBOL_GPL(dpm_suspend_start); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1349 | |
| 1350 | void __suspend_report_result(const char *function, void *fn, int ret) |
| 1351 | { |
Bjorn Helgaas | c80cfb0 | 2008-10-15 22:01:35 -0700 | [diff] [blame] | 1352 | if (ret) |
| 1353 | printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1354 | } |
| 1355 | EXPORT_SYMBOL_GPL(__suspend_report_result); |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1356 | |
| 1357 | /** |
| 1358 | * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete. |
| 1359 | * @dev: Device to wait for. |
| 1360 | * @subordinate: Device that needs to wait for @dev. |
| 1361 | */ |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1362 | int device_pm_wait_for_dev(struct device *subordinate, struct device *dev) |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1363 | { |
| 1364 | dpm_wait(dev, subordinate->power.async_suspend); |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1365 | return async_error; |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1366 | } |
| 1367 | EXPORT_SYMBOL_GPL(device_pm_wait_for_dev); |
Ming Lei | dfe3212 | 2012-08-17 22:06:59 +0800 | [diff] [blame] | 1368 | |
| 1369 | /** |
| 1370 | * dpm_for_each_dev - device iterator. |
| 1371 | * @data: data for the callback. |
| 1372 | * @fn: function to be called for each device. |
| 1373 | * |
| 1374 | * Iterate over devices in dpm_list, and call @fn for each device, |
| 1375 | * passing it @data. |
| 1376 | */ |
| 1377 | void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *)) |
| 1378 | { |
| 1379 | struct device *dev; |
| 1380 | |
| 1381 | if (!fn) |
| 1382 | return; |
| 1383 | |
| 1384 | device_pm_lock(); |
| 1385 | list_for_each_entry(dev, &dpm_list, power.entry) |
| 1386 | fn(dev, data); |
| 1387 | device_pm_unlock(); |
| 1388 | } |
| 1389 | EXPORT_SYMBOL_GPL(dpm_for_each_dev); |