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