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