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