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> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 23 | #include <linux/pm.h> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 24 | #include <linux/pm_runtime.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 25 | #include <linux/resume-trace.h> |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 27 | #include <linux/sched.h> |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 28 | #include <linux/async.h> |
Rafael J. Wysocki | 1e75227 | 2010-12-03 22:58:05 +0100 | [diff] [blame] | 29 | #include <linux/suspend.h> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 30 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 31 | #include "../base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "power.h" |
| 33 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 34 | /* |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 35 | * 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] | 36 | * because children are guaranteed to be discovered after parents, and |
| 37 | * are inserted at the back of the list on discovery. |
| 38 | * |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 39 | * Since device_pm_add() may be called with a device lock held, |
| 40 | * we must never try to acquire a device lock while holding |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 41 | * dpm_list_mutex. |
| 42 | */ |
| 43 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 44 | LIST_HEAD(dpm_list); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 45 | LIST_HEAD(dpm_prepared_list); |
| 46 | LIST_HEAD(dpm_suspended_list); |
| 47 | LIST_HEAD(dpm_noirq_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 49 | struct suspend_stats suspend_stats; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 50 | static DEFINE_MUTEX(dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 51 | static pm_message_t pm_transition; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 53 | static int async_error; |
| 54 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 55 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 56 | * device_pm_init - Initialize the PM-related part of a device object. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 57 | * @dev: Device object being initialized. |
| 58 | */ |
| 59 | void device_pm_init(struct device *dev) |
| 60 | { |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 61 | dev->power.is_prepared = false; |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 62 | dev->power.is_suspended = false; |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 63 | init_completion(&dev->power.completion); |
Colin Cross | 152e1d5 | 2010-09-03 01:24:07 +0200 | [diff] [blame] | 64 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 65 | dev->power.wakeup = NULL; |
| 66 | spin_lock_init(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 67 | pm_runtime_init(dev); |
Rafael J. Wysocki | 22110fa | 2011-04-26 11:33:09 +0200 | [diff] [blame] | 68 | INIT_LIST_HEAD(&dev->power.entry); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 69 | dev->power.power_state = PMSG_INVALID; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 73 | * 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] | 74 | */ |
| 75 | void device_pm_lock(void) |
| 76 | { |
| 77 | mutex_lock(&dpm_list_mtx); |
| 78 | } |
| 79 | |
| 80 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 81 | * 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] | 82 | */ |
| 83 | void device_pm_unlock(void) |
| 84 | { |
| 85 | mutex_unlock(&dpm_list_mtx); |
| 86 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 87 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 88 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 89 | * device_pm_add - Add a device to the PM core's list of active devices. |
| 90 | * @dev: Device to add to the list. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 91 | */ |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 92 | void device_pm_add(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | pr_debug("PM: Adding info for %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 95 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 96 | mutex_lock(&dpm_list_mtx); |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 97 | if (dev->parent && dev->parent->power.is_prepared) |
Rafael J. Wysocki | b64959e | 2010-12-16 17:11:45 +0100 | [diff] [blame] | 98 | dev_warn(dev, "parent %s should not be sleeping\n", |
| 99 | dev_name(dev->parent)); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 100 | list_add_tail(&dev->power.entry, &dpm_list); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 101 | dev_pm_qos_constraints_init(dev); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 102 | mutex_unlock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 105 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 106 | * device_pm_remove - Remove a device from the PM core's list of active devices. |
| 107 | * @dev: Device to be removed from the list. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 108 | */ |
Rafael J. Wysocki | 9cddad7 | 2007-06-13 15:53:34 +0200 | [diff] [blame] | 109 | void device_pm_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | pr_debug("PM: Removing info for %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 112 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 113 | complete_all(&dev->power.completion); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 114 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 115 | dev_pm_qos_constraints_destroy(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | list_del_init(&dev->power.entry); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 117 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 118 | device_wakeup_disable(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 119 | pm_runtime_remove(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 122 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 123 | * device_pm_move_before - Move device in the PM core's list of active devices. |
| 124 | * @deva: Device to move in dpm_list. |
| 125 | * @devb: Device @deva should come before. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 126 | */ |
| 127 | void device_pm_move_before(struct device *deva, struct device *devb) |
| 128 | { |
| 129 | pr_debug("PM: Moving %s:%s before %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 130 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), |
| 131 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 132 | /* Delete deva from dpm_list and reinsert before devb. */ |
| 133 | list_move_tail(&deva->power.entry, &devb->power.entry); |
| 134 | } |
| 135 | |
| 136 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 137 | * device_pm_move_after - Move device in the PM core's list of active devices. |
| 138 | * @deva: Device to move in dpm_list. |
| 139 | * @devb: Device @deva should come after. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 140 | */ |
| 141 | void device_pm_move_after(struct device *deva, struct device *devb) |
| 142 | { |
| 143 | pr_debug("PM: Moving %s:%s after %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 144 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), |
| 145 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 146 | /* Delete deva from dpm_list and reinsert after devb. */ |
| 147 | list_move(&deva->power.entry, &devb->power.entry); |
| 148 | } |
| 149 | |
| 150 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 151 | * device_pm_move_last - Move device to end of the PM core's list of devices. |
| 152 | * @dev: Device to move in dpm_list. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 153 | */ |
| 154 | void device_pm_move_last(struct device *dev) |
| 155 | { |
| 156 | pr_debug("PM: Moving %s:%s to end of list\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 157 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 158 | list_move_tail(&dev->power.entry, &dpm_list); |
| 159 | } |
| 160 | |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 161 | static ktime_t initcall_debug_start(struct device *dev) |
| 162 | { |
| 163 | ktime_t calltime = ktime_set(0, 0); |
| 164 | |
| 165 | if (initcall_debug) { |
| 166 | pr_info("calling %s+ @ %i\n", |
| 167 | dev_name(dev), task_pid_nr(current)); |
| 168 | calltime = ktime_get(); |
| 169 | } |
| 170 | |
| 171 | return calltime; |
| 172 | } |
| 173 | |
| 174 | static void initcall_debug_report(struct device *dev, ktime_t calltime, |
| 175 | int error) |
| 176 | { |
| 177 | ktime_t delta, rettime; |
| 178 | |
| 179 | if (initcall_debug) { |
| 180 | rettime = ktime_get(); |
| 181 | delta = ktime_sub(rettime, calltime); |
| 182 | pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev), |
| 183 | error, (unsigned long long)ktime_to_ns(delta) >> 10); |
| 184 | } |
| 185 | } |
| 186 | |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 187 | /** |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 188 | * dpm_wait - Wait for a PM operation to complete. |
| 189 | * @dev: Device to wait for. |
| 190 | * @async: If unset, wait only if the device's power.async_suspend flag is set. |
| 191 | */ |
| 192 | static void dpm_wait(struct device *dev, bool async) |
| 193 | { |
| 194 | if (!dev) |
| 195 | return; |
| 196 | |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 197 | if (async || (pm_async_enabled && dev->power.async_suspend)) |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 198 | wait_for_completion(&dev->power.completion); |
| 199 | } |
| 200 | |
| 201 | static int dpm_wait_fn(struct device *dev, void *async_ptr) |
| 202 | { |
| 203 | dpm_wait(dev, *((bool *)async_ptr)); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static void dpm_wait_for_children(struct device *dev, bool async) |
| 208 | { |
| 209 | device_for_each_child(dev, &async, dpm_wait_fn); |
| 210 | } |
| 211 | |
| 212 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 213 | * pm_op - Execute the PM operation appropriate for given PM event. |
| 214 | * @dev: Device to handle. |
| 215 | * @ops: PM operations to choose from. |
| 216 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 217 | */ |
Dmitry Torokhov | d9ab771 | 2009-07-22 00:37:25 +0200 | [diff] [blame] | 218 | static int pm_op(struct device *dev, |
| 219 | const struct dev_pm_ops *ops, |
| 220 | pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 221 | { |
| 222 | int error = 0; |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 223 | ktime_t calltime; |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 224 | |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 225 | calltime = initcall_debug_start(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 226 | |
| 227 | switch (state.event) { |
| 228 | #ifdef CONFIG_SUSPEND |
| 229 | case PM_EVENT_SUSPEND: |
| 230 | if (ops->suspend) { |
| 231 | error = ops->suspend(dev); |
| 232 | suspend_report_result(ops->suspend, error); |
| 233 | } |
| 234 | break; |
| 235 | case PM_EVENT_RESUME: |
| 236 | if (ops->resume) { |
| 237 | error = ops->resume(dev); |
| 238 | suspend_report_result(ops->resume, error); |
| 239 | } |
| 240 | break; |
| 241 | #endif /* CONFIG_SUSPEND */ |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 242 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 243 | case PM_EVENT_FREEZE: |
| 244 | case PM_EVENT_QUIESCE: |
| 245 | if (ops->freeze) { |
| 246 | error = ops->freeze(dev); |
| 247 | suspend_report_result(ops->freeze, error); |
| 248 | } |
| 249 | break; |
| 250 | case PM_EVENT_HIBERNATE: |
| 251 | if (ops->poweroff) { |
| 252 | error = ops->poweroff(dev); |
| 253 | suspend_report_result(ops->poweroff, error); |
| 254 | } |
| 255 | break; |
| 256 | case PM_EVENT_THAW: |
| 257 | case PM_EVENT_RECOVER: |
| 258 | if (ops->thaw) { |
| 259 | error = ops->thaw(dev); |
| 260 | suspend_report_result(ops->thaw, error); |
| 261 | } |
| 262 | break; |
| 263 | case PM_EVENT_RESTORE: |
| 264 | if (ops->restore) { |
| 265 | error = ops->restore(dev); |
| 266 | suspend_report_result(ops->restore, error); |
| 267 | } |
| 268 | break; |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 269 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 270 | default: |
| 271 | error = -EINVAL; |
| 272 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 273 | |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 274 | initcall_debug_report(dev, calltime, error); |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 275 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 276 | return error; |
| 277 | } |
| 278 | |
| 279 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 280 | * pm_noirq_op - Execute the PM operation appropriate for given PM event. |
| 281 | * @dev: Device to handle. |
| 282 | * @ops: PM operations to choose from. |
| 283 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 284 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 285 | * The driver of @dev will not receive interrupts while this function is being |
| 286 | * executed. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 287 | */ |
Dmitry Torokhov | d9ab771 | 2009-07-22 00:37:25 +0200 | [diff] [blame] | 288 | static int pm_noirq_op(struct device *dev, |
| 289 | const struct dev_pm_ops *ops, |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 290 | pm_message_t state) |
| 291 | { |
| 292 | int error = 0; |
Rafael J. Wysocki | 2cbb3ce | 2010-12-15 00:17:29 +0100 | [diff] [blame] | 293 | ktime_t calltime = ktime_set(0, 0), delta, rettime; |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 294 | |
| 295 | if (initcall_debug) { |
Rafael J. Wysocki | 8cc6b39 | 2010-01-23 22:03:29 +0100 | [diff] [blame] | 296 | pr_info("calling %s+ @ %i, parent: %s\n", |
| 297 | dev_name(dev), task_pid_nr(current), |
| 298 | dev->parent ? dev_name(dev->parent) : "none"); |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 299 | calltime = ktime_get(); |
| 300 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 301 | |
| 302 | switch (state.event) { |
| 303 | #ifdef CONFIG_SUSPEND |
| 304 | case PM_EVENT_SUSPEND: |
| 305 | if (ops->suspend_noirq) { |
| 306 | error = ops->suspend_noirq(dev); |
| 307 | suspend_report_result(ops->suspend_noirq, error); |
| 308 | } |
| 309 | break; |
| 310 | case PM_EVENT_RESUME: |
| 311 | if (ops->resume_noirq) { |
| 312 | error = ops->resume_noirq(dev); |
| 313 | suspend_report_result(ops->resume_noirq, error); |
| 314 | } |
| 315 | break; |
| 316 | #endif /* CONFIG_SUSPEND */ |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 317 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 318 | case PM_EVENT_FREEZE: |
| 319 | case PM_EVENT_QUIESCE: |
| 320 | if (ops->freeze_noirq) { |
| 321 | error = ops->freeze_noirq(dev); |
| 322 | suspend_report_result(ops->freeze_noirq, error); |
| 323 | } |
| 324 | break; |
| 325 | case PM_EVENT_HIBERNATE: |
| 326 | if (ops->poweroff_noirq) { |
| 327 | error = ops->poweroff_noirq(dev); |
| 328 | suspend_report_result(ops->poweroff_noirq, error); |
| 329 | } |
| 330 | break; |
| 331 | case PM_EVENT_THAW: |
| 332 | case PM_EVENT_RECOVER: |
| 333 | if (ops->thaw_noirq) { |
| 334 | error = ops->thaw_noirq(dev); |
| 335 | suspend_report_result(ops->thaw_noirq, error); |
| 336 | } |
| 337 | break; |
| 338 | case PM_EVENT_RESTORE: |
| 339 | if (ops->restore_noirq) { |
| 340 | error = ops->restore_noirq(dev); |
| 341 | suspend_report_result(ops->restore_noirq, error); |
| 342 | } |
| 343 | break; |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 344 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 345 | default: |
| 346 | error = -EINVAL; |
| 347 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 348 | |
| 349 | if (initcall_debug) { |
| 350 | rettime = ktime_get(); |
| 351 | delta = ktime_sub(rettime, calltime); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 352 | printk("initcall %s_i+ returned %d after %Ld usecs\n", |
| 353 | dev_name(dev), error, |
| 354 | (unsigned long long)ktime_to_ns(delta) >> 10); |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 357 | return error; |
| 358 | } |
| 359 | |
| 360 | static char *pm_verb(int event) |
| 361 | { |
| 362 | switch (event) { |
| 363 | case PM_EVENT_SUSPEND: |
| 364 | return "suspend"; |
| 365 | case PM_EVENT_RESUME: |
| 366 | return "resume"; |
| 367 | case PM_EVENT_FREEZE: |
| 368 | return "freeze"; |
| 369 | case PM_EVENT_QUIESCE: |
| 370 | return "quiesce"; |
| 371 | case PM_EVENT_HIBERNATE: |
| 372 | return "hibernate"; |
| 373 | case PM_EVENT_THAW: |
| 374 | return "thaw"; |
| 375 | case PM_EVENT_RESTORE: |
| 376 | return "restore"; |
| 377 | case PM_EVENT_RECOVER: |
| 378 | return "recover"; |
| 379 | default: |
| 380 | return "(unknown PM event)"; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info) |
| 385 | { |
| 386 | dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event), |
| 387 | ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ? |
| 388 | ", may wakeup" : ""); |
| 389 | } |
| 390 | |
| 391 | static void pm_dev_err(struct device *dev, pm_message_t state, char *info, |
| 392 | int error) |
| 393 | { |
| 394 | 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] | 395 | dev_name(dev), pm_verb(state.event), info, error); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 398 | static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info) |
| 399 | { |
| 400 | ktime_t calltime; |
Kevin Cernekee | 0702d9e | 2010-09-20 22:32:10 +0200 | [diff] [blame] | 401 | u64 usecs64; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 402 | int usecs; |
| 403 | |
| 404 | calltime = ktime_get(); |
| 405 | usecs64 = ktime_to_ns(ktime_sub(calltime, starttime)); |
| 406 | do_div(usecs64, NSEC_PER_USEC); |
| 407 | usecs = usecs64; |
| 408 | if (usecs == 0) |
| 409 | usecs = 1; |
| 410 | pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n", |
| 411 | info ?: "", info ? " " : "", pm_verb(state.event), |
| 412 | usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC); |
| 413 | } |
| 414 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 415 | /*------------------------- Resume routines -------------------------*/ |
| 416 | |
| 417 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 418 | * device_resume_noirq - Execute an "early resume" callback for given device. |
| 419 | * @dev: Device to handle. |
| 420 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 421 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 422 | * The driver of @dev will not receive interrupts while this function is being |
| 423 | * executed. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 424 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 425 | static int device_resume_noirq(struct device *dev, pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 426 | { |
| 427 | int error = 0; |
| 428 | |
| 429 | TRACE_DEVICE(dev); |
| 430 | TRACE_RESUME(0); |
| 431 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 432 | if (dev->pm_domain) { |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 433 | pm_dev_dbg(dev, state, "EARLY power domain "); |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 434 | error = pm_noirq_op(dev, &dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 435 | } else if (dev->type && dev->type->pm) { |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 436 | pm_dev_dbg(dev, state, "EARLY type "); |
| 437 | error = pm_noirq_op(dev, dev->type->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 438 | } else if (dev->class && dev->class->pm) { |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 439 | pm_dev_dbg(dev, state, "EARLY class "); |
| 440 | error = pm_noirq_op(dev, dev->class->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 441 | } else if (dev->bus && dev->bus->pm) { |
| 442 | pm_dev_dbg(dev, state, "EARLY "); |
| 443 | error = pm_noirq_op(dev, dev->bus->pm, state); |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 446 | TRACE_RESUME(error); |
| 447 | return error; |
| 448 | } |
| 449 | |
| 450 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 451 | * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices. |
| 452 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 453 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 454 | * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and |
| 455 | * enable device drivers to receive interrupts. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 456 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 457 | void dpm_resume_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 458 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 459 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 460 | |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 461 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 462 | while (!list_empty(&dpm_noirq_list)) { |
| 463 | struct device *dev = to_device(dpm_noirq_list.next); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 464 | int error; |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 465 | |
| 466 | get_device(dev); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 467 | list_move_tail(&dev->power.entry, &dpm_suspended_list); |
| 468 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 469 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 470 | error = device_resume_noirq(dev, state); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 471 | if (error) { |
| 472 | suspend_stats.failed_resume_noirq++; |
| 473 | dpm_save_failed_step(SUSPEND_RESUME_NOIRQ); |
| 474 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 475 | pm_dev_err(dev, state, " early", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 476 | } |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 477 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 478 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 479 | put_device(dev); |
| 480 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 481 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 482 | dpm_show_time(starttime, state, "early"); |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 483 | resume_device_irqs(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 484 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 485 | EXPORT_SYMBOL_GPL(dpm_resume_noirq); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 486 | |
| 487 | /** |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 488 | * legacy_resume - Execute a legacy (bus or class) resume callback for device. |
Randy Dunlap | 0a88422 | 2010-01-08 14:42:57 -0800 | [diff] [blame] | 489 | * @dev: Device to resume. |
| 490 | * @cb: Resume callback to execute. |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 491 | */ |
| 492 | static int legacy_resume(struct device *dev, int (*cb)(struct device *dev)) |
| 493 | { |
| 494 | int error; |
| 495 | ktime_t calltime; |
| 496 | |
| 497 | calltime = initcall_debug_start(dev); |
| 498 | |
| 499 | error = cb(dev); |
| 500 | suspend_report_result(cb, error); |
| 501 | |
| 502 | initcall_debug_report(dev, calltime, error); |
| 503 | |
| 504 | return error; |
| 505 | } |
| 506 | |
| 507 | /** |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 508 | * device_resume - Execute "resume" callbacks for given device. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 509 | * @dev: Device to handle. |
| 510 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 511 | * @async: If true, the device is being resumed asynchronously. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 512 | */ |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 513 | 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] | 514 | { |
| 515 | int error = 0; |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 516 | bool put = false; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 517 | |
| 518 | TRACE_DEVICE(dev); |
| 519 | TRACE_RESUME(0); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 520 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 521 | dpm_wait(dev->parent, async); |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 522 | device_lock(dev); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 523 | |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 524 | /* |
| 525 | * This is a fib. But we'll allow new children to be added below |
| 526 | * a resumed device, even if the device hasn't been completed yet. |
| 527 | */ |
| 528 | dev->power.is_prepared = false; |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 529 | |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 530 | if (!dev->power.is_suspended) |
| 531 | goto Unlock; |
| 532 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 533 | pm_runtime_enable(dev); |
| 534 | put = true; |
| 535 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 536 | if (dev->pm_domain) { |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 537 | pm_dev_dbg(dev, state, "power domain "); |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 538 | error = pm_op(dev, &dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 539 | goto End; |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 542 | if (dev->type && dev->type->pm) { |
| 543 | pm_dev_dbg(dev, state, "type "); |
| 544 | error = pm_op(dev, dev->type->pm, state); |
| 545 | goto End; |
| 546 | } |
| 547 | |
| 548 | if (dev->class) { |
| 549 | if (dev->class->pm) { |
| 550 | pm_dev_dbg(dev, state, "class "); |
| 551 | error = pm_op(dev, dev->class->pm, state); |
| 552 | goto End; |
| 553 | } else if (dev->class->resume) { |
| 554 | pm_dev_dbg(dev, state, "legacy class "); |
| 555 | error = legacy_resume(dev, dev->class->resume); |
| 556 | goto End; |
| 557 | } |
| 558 | } |
| 559 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 560 | if (dev->bus) { |
| 561 | if (dev->bus->pm) { |
| 562 | pm_dev_dbg(dev, state, ""); |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 563 | error = pm_op(dev, dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 564 | } else if (dev->bus->resume) { |
| 565 | pm_dev_dbg(dev, state, "legacy "); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 566 | error = legacy_resume(dev, dev->bus->resume); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 567 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 568 | } |
| 569 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 570 | End: |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 571 | dev->power.is_suspended = false; |
| 572 | |
| 573 | Unlock: |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 574 | device_unlock(dev); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 575 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 576 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 577 | TRACE_RESUME(error); |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 578 | |
| 579 | if (put) |
| 580 | pm_runtime_put_sync(dev); |
| 581 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 582 | return error; |
| 583 | } |
| 584 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 585 | static void async_resume(void *data, async_cookie_t cookie) |
| 586 | { |
| 587 | struct device *dev = (struct device *)data; |
| 588 | int error; |
| 589 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 590 | error = device_resume(dev, pm_transition, true); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 591 | if (error) |
| 592 | pm_dev_err(dev, pm_transition, " async", error); |
| 593 | put_device(dev); |
| 594 | } |
| 595 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 596 | static bool is_async(struct device *dev) |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 597 | { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 598 | return dev->power.async_suspend && pm_async_enabled |
| 599 | && !pm_trace_is_enabled(); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 602 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 603 | * dpm_resume - Execute "resume" callbacks for non-sysdev devices. |
| 604 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 605 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 606 | * Execute the appropriate "resume" callback for all devices whose status |
| 607 | * indicates that they are suspended. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 608 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 609 | void dpm_resume(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 610 | { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 611 | struct device *dev; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 612 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 613 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 614 | might_sleep(); |
| 615 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 616 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 617 | pm_transition = state; |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 618 | async_error = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 619 | |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 620 | list_for_each_entry(dev, &dpm_suspended_list, power.entry) { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 621 | INIT_COMPLETION(dev->power.completion); |
| 622 | if (is_async(dev)) { |
| 623 | get_device(dev); |
| 624 | async_schedule(async_resume, dev); |
| 625 | } |
| 626 | } |
| 627 | |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 628 | while (!list_empty(&dpm_suspended_list)) { |
| 629 | dev = to_device(dpm_suspended_list.next); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 630 | get_device(dev); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 631 | if (!is_async(dev)) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 632 | int error; |
| 633 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 634 | mutex_unlock(&dpm_list_mtx); |
| 635 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 636 | error = device_resume(dev, state, false); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 637 | if (error) { |
| 638 | suspend_stats.failed_resume++; |
| 639 | dpm_save_failed_step(SUSPEND_RESUME); |
| 640 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 641 | pm_dev_err(dev, state, "", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 642 | } |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 643 | |
| 644 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 645 | } |
| 646 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 647 | list_move_tail(&dev->power.entry, &dpm_prepared_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 648 | put_device(dev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 649 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 650 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 651 | async_synchronize_full(); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 652 | dpm_show_time(starttime, state, NULL); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 656 | * device_complete - Complete a PM transition for given device. |
| 657 | * @dev: Device to handle. |
| 658 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 659 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 660 | static void device_complete(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 661 | { |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 662 | device_lock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 663 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 664 | if (dev->pm_domain) { |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 665 | pm_dev_dbg(dev, state, "completing power domain "); |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 666 | if (dev->pm_domain->ops.complete) |
| 667 | dev->pm_domain->ops.complete(dev); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 668 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 669 | pm_dev_dbg(dev, state, "completing type "); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 670 | if (dev->type->pm->complete) |
| 671 | dev->type->pm->complete(dev); |
| 672 | } else if (dev->class && dev->class->pm) { |
| 673 | pm_dev_dbg(dev, state, "completing class "); |
| 674 | if (dev->class->pm->complete) |
| 675 | dev->class->pm->complete(dev); |
| 676 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 677 | pm_dev_dbg(dev, state, "completing "); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 678 | if (dev->bus->pm->complete) |
| 679 | dev->bus->pm->complete(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 682 | device_unlock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 686 | * dpm_complete - Complete a PM transition for all non-sysdev devices. |
| 687 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 688 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 689 | * Execute the ->complete() callbacks for all devices whose PM status is not |
| 690 | * DPM_ON (this allows new devices to be registered). |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 691 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 692 | void dpm_complete(pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 693 | { |
| 694 | struct list_head list; |
| 695 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 696 | might_sleep(); |
| 697 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 698 | INIT_LIST_HEAD(&list); |
| 699 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 700 | while (!list_empty(&dpm_prepared_list)) { |
| 701 | struct device *dev = to_device(dpm_prepared_list.prev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 702 | |
| 703 | get_device(dev); |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 704 | dev->power.is_prepared = false; |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 705 | list_move(&dev->power.entry, &list); |
| 706 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 707 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 708 | device_complete(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 709 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 710 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 711 | put_device(dev); |
| 712 | } |
| 713 | list_splice(&list, &dpm_list); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 714 | mutex_unlock(&dpm_list_mtx); |
| 715 | } |
| 716 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 717 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 718 | * dpm_resume_end - Execute "resume" callbacks and complete system transition. |
| 719 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 720 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 721 | * Execute "resume" callbacks for all devices and complete the PM transition of |
| 722 | * the system. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 723 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 724 | void dpm_resume_end(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 725 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 726 | dpm_resume(state); |
| 727 | dpm_complete(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 728 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 729 | EXPORT_SYMBOL_GPL(dpm_resume_end); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 730 | |
| 731 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 732 | /*------------------------- Suspend routines -------------------------*/ |
| 733 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 734 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 735 | * resume_event - Return a "resume" message for given "suspend" sleep state. |
| 736 | * @sleep_state: PM message representing a sleep state. |
| 737 | * |
| 738 | * Return a PM message representing the resume event corresponding to given |
| 739 | * sleep state. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 740 | */ |
| 741 | static pm_message_t resume_event(pm_message_t sleep_state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 742 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 743 | switch (sleep_state.event) { |
| 744 | case PM_EVENT_SUSPEND: |
| 745 | return PMSG_RESUME; |
| 746 | case PM_EVENT_FREEZE: |
| 747 | case PM_EVENT_QUIESCE: |
| 748 | return PMSG_RECOVER; |
| 749 | case PM_EVENT_HIBERNATE: |
| 750 | return PMSG_RESTORE; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 751 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 752 | return PMSG_ON; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 756 | * device_suspend_noirq - Execute a "late suspend" callback for given device. |
| 757 | * @dev: Device to handle. |
| 758 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 759 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 760 | * The driver of @dev will not receive interrupts while this function is being |
| 761 | * executed. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 762 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 763 | 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] | 764 | { |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 765 | int error; |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 766 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 767 | if (dev->pm_domain) { |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 768 | pm_dev_dbg(dev, state, "LATE power domain "); |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 769 | error = pm_noirq_op(dev, &dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 770 | if (error) |
| 771 | return error; |
| 772 | } else if (dev->type && dev->type->pm) { |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 773 | pm_dev_dbg(dev, state, "LATE type "); |
| 774 | error = pm_noirq_op(dev, dev->type->pm, state); |
| 775 | if (error) |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 776 | return error; |
| 777 | } else if (dev->class && dev->class->pm) { |
| 778 | pm_dev_dbg(dev, state, "LATE class "); |
| 779 | error = pm_noirq_op(dev, dev->class->pm, state); |
| 780 | if (error) |
| 781 | return error; |
| 782 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 783 | pm_dev_dbg(dev, state, "LATE "); |
| 784 | error = pm_noirq_op(dev, dev->bus->pm, state); |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 785 | if (error) |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 786 | return error; |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 787 | } |
| 788 | |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 789 | return 0; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 793 | * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices. |
| 794 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 795 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 796 | * Prevent device drivers from receiving interrupts and call the "noirq" suspend |
| 797 | * handlers for all non-sysdev devices. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 798 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 799 | int dpm_suspend_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 800 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 801 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 802 | int error = 0; |
| 803 | |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 804 | suspend_device_irqs(); |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 805 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 806 | while (!list_empty(&dpm_suspended_list)) { |
| 807 | struct device *dev = to_device(dpm_suspended_list.prev); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 808 | |
| 809 | get_device(dev); |
| 810 | mutex_unlock(&dpm_list_mtx); |
| 811 | |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 812 | error = device_suspend_noirq(dev, state); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 813 | |
| 814 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 815 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 816 | pm_dev_err(dev, state, " late", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 817 | suspend_stats.failed_suspend_noirq++; |
| 818 | dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ); |
| 819 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 820 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 821 | break; |
| 822 | } |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 823 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 824 | list_move(&dev->power.entry, &dpm_noirq_list); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 825 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 826 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 827 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 828 | if (error) |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 829 | dpm_resume_noirq(resume_event(state)); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 830 | else |
| 831 | dpm_show_time(starttime, state, "late"); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 832 | return error; |
| 833 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 834 | EXPORT_SYMBOL_GPL(dpm_suspend_noirq); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 835 | |
| 836 | /** |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 837 | * legacy_suspend - Execute a legacy (bus or class) suspend callback for device. |
Randy Dunlap | 0a88422 | 2010-01-08 14:42:57 -0800 | [diff] [blame] | 838 | * @dev: Device to suspend. |
| 839 | * @state: PM transition of the system being carried out. |
| 840 | * @cb: Suspend callback to execute. |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 841 | */ |
| 842 | static int legacy_suspend(struct device *dev, pm_message_t state, |
| 843 | int (*cb)(struct device *dev, pm_message_t state)) |
| 844 | { |
| 845 | int error; |
| 846 | ktime_t calltime; |
| 847 | |
| 848 | calltime = initcall_debug_start(dev); |
| 849 | |
| 850 | error = cb(dev, state); |
| 851 | suspend_report_result(cb, error); |
| 852 | |
| 853 | initcall_debug_report(dev, calltime, error); |
| 854 | |
| 855 | return error; |
| 856 | } |
| 857 | |
| 858 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 859 | * device_suspend - Execute "suspend" callbacks for given device. |
| 860 | * @dev: Device to handle. |
| 861 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 862 | * @async: If true, the device is being suspended asynchronously. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 863 | */ |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 864 | 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] | 865 | { |
| 866 | int error = 0; |
| 867 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 868 | dpm_wait_for_children(dev, async); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 869 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 870 | if (async_error) |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 871 | return 0; |
| 872 | |
| 873 | pm_runtime_get_noresume(dev); |
| 874 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) |
| 875 | pm_wakeup_event(dev, 0); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 876 | |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 877 | if (pm_wakeup_pending()) { |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 878 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 879 | async_error = -EBUSY; |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 880 | return 0; |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 881 | } |
| 882 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 883 | device_lock(dev); |
| 884 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 885 | if (dev->pm_domain) { |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 886 | pm_dev_dbg(dev, state, "power domain "); |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 887 | error = pm_op(dev, &dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 888 | goto End; |
| 889 | } |
| 890 | |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 891 | if (dev->type && dev->type->pm) { |
| 892 | pm_dev_dbg(dev, state, "type "); |
| 893 | error = pm_op(dev, dev->type->pm, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 894 | goto End; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 895 | } |
| 896 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 897 | if (dev->class) { |
| 898 | if (dev->class->pm) { |
| 899 | pm_dev_dbg(dev, state, "class "); |
| 900 | error = pm_op(dev, dev->class->pm, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 901 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 902 | } else if (dev->class->suspend) { |
| 903 | pm_dev_dbg(dev, state, "legacy class "); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 904 | error = legacy_suspend(dev, state, dev->class->suspend); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 905 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 906 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 907 | } |
| 908 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 909 | if (dev->bus) { |
| 910 | if (dev->bus->pm) { |
| 911 | pm_dev_dbg(dev, state, ""); |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 912 | error = pm_op(dev, dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 913 | } else if (dev->bus->suspend) { |
| 914 | pm_dev_dbg(dev, state, "legacy "); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 915 | error = legacy_suspend(dev, state, dev->bus->suspend); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 916 | } |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 917 | } |
| 918 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 919 | End: |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 920 | dev->power.is_suspended = !error; |
| 921 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 922 | device_unlock(dev); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 923 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 924 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 925 | if (error) { |
| 926 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 927 | async_error = error; |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 928 | } else if (dev->power.is_suspended) { |
| 929 | __pm_runtime_disable(dev, false); |
| 930 | } |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 931 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 932 | return error; |
| 933 | } |
| 934 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 935 | static void async_suspend(void *data, async_cookie_t cookie) |
| 936 | { |
| 937 | struct device *dev = (struct device *)data; |
| 938 | int error; |
| 939 | |
| 940 | error = __device_suspend(dev, pm_transition, true); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 941 | if (error) { |
| 942 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 943 | pm_dev_err(dev, pm_transition, " async", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 944 | } |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 945 | |
| 946 | put_device(dev); |
| 947 | } |
| 948 | |
| 949 | static int device_suspend(struct device *dev) |
| 950 | { |
| 951 | INIT_COMPLETION(dev->power.completion); |
| 952 | |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 953 | if (pm_async_enabled && dev->power.async_suspend) { |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 954 | get_device(dev); |
| 955 | async_schedule(async_suspend, dev); |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | return __device_suspend(dev, pm_transition, false); |
| 960 | } |
| 961 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 962 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 963 | * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices. |
| 964 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 965 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 966 | int dpm_suspend(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 967 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 968 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 969 | int error = 0; |
| 970 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 971 | might_sleep(); |
| 972 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 973 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 974 | pm_transition = state; |
| 975 | async_error = 0; |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 976 | while (!list_empty(&dpm_prepared_list)) { |
| 977 | struct device *dev = to_device(dpm_prepared_list.prev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 978 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 979 | get_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 980 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 981 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 982 | error = device_suspend(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 983 | |
Alan Stern | 1b3cbec | 2008-02-29 11:50:22 -0500 | [diff] [blame] | 984 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 985 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 986 | pm_dev_err(dev, state, "", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 987 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 988 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 989 | break; |
| 990 | } |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 991 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 992 | list_move(&dev->power.entry, &dpm_suspended_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 993 | put_device(dev); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 994 | if (async_error) |
| 995 | break; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 996 | } |
| 997 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 998 | async_synchronize_full(); |
| 999 | if (!error) |
| 1000 | error = async_error; |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 1001 | if (error) { |
| 1002 | suspend_stats.failed_suspend++; |
| 1003 | dpm_save_failed_step(SUSPEND_SUSPEND); |
| 1004 | } else |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 1005 | dpm_show_time(starttime, state, NULL); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1006 | return error; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1010 | * device_prepare - Prepare a device for system power transition. |
| 1011 | * @dev: Device to handle. |
| 1012 | * @state: PM transition of the system being carried out. |
| 1013 | * |
| 1014 | * Execute the ->prepare() callback(s) for given device. No new children of the |
| 1015 | * device may be registered after this function has returned. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1016 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1017 | static int device_prepare(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1018 | { |
| 1019 | int error = 0; |
| 1020 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1021 | device_lock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1022 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 1023 | if (dev->pm_domain) { |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1024 | pm_dev_dbg(dev, state, "preparing power domain "); |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 1025 | if (dev->pm_domain->ops.prepare) |
| 1026 | error = dev->pm_domain->ops.prepare(dev); |
| 1027 | suspend_report_result(dev->pm_domain->ops.prepare, error); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1028 | if (error) |
| 1029 | goto End; |
| 1030 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1031 | pm_dev_dbg(dev, state, "preparing type "); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1032 | if (dev->type->pm->prepare) |
| 1033 | error = dev->type->pm->prepare(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1034 | suspend_report_result(dev->type->pm->prepare, error); |
| 1035 | if (error) |
| 1036 | goto End; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1037 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1038 | pm_dev_dbg(dev, state, "preparing class "); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1039 | if (dev->class->pm->prepare) |
| 1040 | error = dev->class->pm->prepare(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1041 | suspend_report_result(dev->class->pm->prepare, error); |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 1042 | if (error) |
| 1043 | goto End; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1044 | } else if (dev->bus && dev->bus->pm) { |
| 1045 | pm_dev_dbg(dev, state, "preparing "); |
| 1046 | if (dev->bus->pm->prepare) |
| 1047 | error = dev->bus->pm->prepare(dev); |
| 1048 | suspend_report_result(dev->bus->pm->prepare, error); |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1051 | End: |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1052 | device_unlock(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1053 | |
| 1054 | return error; |
| 1055 | } |
| 1056 | |
| 1057 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1058 | * dpm_prepare - Prepare all non-sysdev devices for a system PM transition. |
| 1059 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1060 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1061 | * Execute the ->prepare() callback(s) for all devices. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1062 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1063 | int dpm_prepare(pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1064 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1065 | int error = 0; |
| 1066 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1067 | might_sleep(); |
| 1068 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1069 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1070 | while (!list_empty(&dpm_list)) { |
| 1071 | struct device *dev = to_device(dpm_list.next); |
| 1072 | |
| 1073 | get_device(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1074 | mutex_unlock(&dpm_list_mtx); |
| 1075 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1076 | error = device_prepare(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1077 | |
| 1078 | mutex_lock(&dpm_list_mtx); |
| 1079 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1080 | if (error == -EAGAIN) { |
| 1081 | put_device(dev); |
Sebastian Ott | 886a7a3 | 2009-07-08 13:26:05 +0200 | [diff] [blame] | 1082 | error = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1083 | continue; |
| 1084 | } |
Rafael J. Wysocki | 1e75227 | 2010-12-03 22:58:05 +0100 | [diff] [blame] | 1085 | printk(KERN_INFO "PM: Device %s not prepared " |
| 1086 | "for power transition: code %d\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 1087 | dev_name(dev), error); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1088 | put_device(dev); |
| 1089 | break; |
| 1090 | } |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 1091 | dev->power.is_prepared = true; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1092 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1093 | list_move_tail(&dev->power.entry, &dpm_prepared_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1094 | put_device(dev); |
| 1095 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1096 | mutex_unlock(&dpm_list_mtx); |
| 1097 | return error; |
| 1098 | } |
| 1099 | |
| 1100 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1101 | * dpm_suspend_start - Prepare devices for PM transition and suspend them. |
| 1102 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1103 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1104 | * Prepare all non-sysdev devices for system PM transition and execute "suspend" |
| 1105 | * callbacks for them. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1106 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1107 | int dpm_suspend_start(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1108 | { |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1109 | int error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1110 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1111 | error = dpm_prepare(state); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame^] | 1112 | if (error) { |
| 1113 | suspend_stats.failed_prepare++; |
| 1114 | dpm_save_failed_step(SUSPEND_PREPARE); |
| 1115 | } else |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1116 | error = dpm_suspend(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1117 | return error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1118 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1119 | EXPORT_SYMBOL_GPL(dpm_suspend_start); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1120 | |
| 1121 | void __suspend_report_result(const char *function, void *fn, int ret) |
| 1122 | { |
Bjorn Helgaas | c80cfb0 | 2008-10-15 22:01:35 -0700 | [diff] [blame] | 1123 | if (ret) |
| 1124 | printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1125 | } |
| 1126 | EXPORT_SYMBOL_GPL(__suspend_report_result); |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1127 | |
| 1128 | /** |
| 1129 | * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete. |
| 1130 | * @dev: Device to wait for. |
| 1131 | * @subordinate: Device that needs to wait for @dev. |
| 1132 | */ |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1133 | 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] | 1134 | { |
| 1135 | dpm_wait(dev, subordinate->power.async_suspend); |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1136 | return async_error; |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1137 | } |
| 1138 | EXPORT_SYMBOL_GPL(device_pm_wait_for_dev); |