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