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