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