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