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