Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/power/main.c - Where the driver meets power management. |
| 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Open Source Development Lab |
| 6 | * |
| 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | * |
| 10 | * The driver model core calls device_pm_add() when a device is registered. |
| 11 | * This will intialize the embedded device_pm_info object in the device |
| 12 | * and add it to the list of power-controlled devices. sysfs entries for |
| 13 | * controlling device power management will also be added. |
| 14 | * |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 15 | * A separate list is used for keeping track of power info, because the power |
| 16 | * domain dependencies may differ from the ancestral dependencies that the |
| 17 | * subsystem list maintains. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/device.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 21 | #include <linux/kallsyms.h> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 23 | #include <linux/pm.h> |
| 24 | #include <linux/resume-trace.h> |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 25 | #include <linux/rwsem.h> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 26 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 27 | #include "../base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "power.h" |
| 29 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 30 | /* |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 31 | * 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] | 32 | * because children are guaranteed to be discovered after parents, and |
| 33 | * are inserted at the back of the list on discovery. |
| 34 | * |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 35 | * Since device_pm_add() may be called with a device semaphore held, |
| 36 | * we must never try to acquire a device semaphore while holding |
| 37 | * dpm_list_mutex. |
| 38 | */ |
| 39 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 40 | LIST_HEAD(dpm_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 42 | static DEFINE_MUTEX(dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 44 | /* |
| 45 | * Set once the preparation of devices for a PM transition has started, reset |
| 46 | * before starting to resume devices. Protected by dpm_list_mtx. |
| 47 | */ |
| 48 | static bool transition_started; |
| 49 | |
| 50 | /** |
| 51 | * device_pm_lock - lock the list of active devices used by the PM core |
| 52 | */ |
| 53 | void device_pm_lock(void) |
| 54 | { |
| 55 | mutex_lock(&dpm_list_mtx); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * device_pm_unlock - unlock the list of active devices used by the PM core |
| 60 | */ |
| 61 | void device_pm_unlock(void) |
| 62 | { |
| 63 | mutex_unlock(&dpm_list_mtx); |
| 64 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 65 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 66 | /** |
| 67 | * device_pm_add - add a device to the list of active devices |
| 68 | * @dev: Device to be added to the list |
| 69 | */ |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 70 | void device_pm_add(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | pr_debug("PM: Adding info for %s:%s\n", |
Dmitry Torokhov | c48ea60 | 2007-04-11 01:37:18 -0400 | [diff] [blame] | 73 | dev->bus ? dev->bus->name : "No Bus", |
| 74 | kobject_name(&dev->kobj)); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 75 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 76 | if (dev->parent) { |
Rafael J. Wysocki | f5a6d95 | 2008-08-09 01:05:13 +0200 | [diff] [blame] | 77 | if (dev->parent->power.status >= DPM_SUSPENDING) |
| 78 | dev_warn(dev, "parent %s should not be sleeping\n", |
Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 79 | dev->parent->bus_id); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 80 | } else if (transition_started) { |
| 81 | /* |
| 82 | * We refuse to register parentless devices while a PM |
| 83 | * transition is in progress in order to avoid leaving them |
| 84 | * unhandled down the road |
| 85 | */ |
Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 86 | WARN_ON(true); |
Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 87 | } |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 88 | |
| 89 | list_add_tail(&dev->power.entry, &dpm_list); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 90 | mutex_unlock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 93 | /** |
| 94 | * device_pm_remove - remove a device from the list of active devices |
| 95 | * @dev: Device to be removed from the list |
| 96 | * |
| 97 | * This function also removes the device's PM-related sysfs attributes. |
| 98 | */ |
Rafael J. Wysocki | 9cddad7 | 2007-06-13 15:53:34 +0200 | [diff] [blame] | 99 | void device_pm_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | pr_debug("PM: Removing info for %s:%s\n", |
Dmitry Torokhov | c48ea60 | 2007-04-11 01:37:18 -0400 | [diff] [blame] | 102 | dev->bus ? dev->bus->name : "No Bus", |
| 103 | kobject_name(&dev->kobj)); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 104 | mutex_lock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | list_del_init(&dev->power.entry); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 106 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 109 | /** |
| 110 | * pm_op - execute the PM operation appropiate for given PM event |
| 111 | * @dev: Device. |
| 112 | * @ops: PM operations to choose from. |
| 113 | * @state: PM transition of the system being carried out. |
| 114 | */ |
| 115 | static int pm_op(struct device *dev, struct pm_ops *ops, pm_message_t state) |
| 116 | { |
| 117 | int error = 0; |
| 118 | |
| 119 | switch (state.event) { |
| 120 | #ifdef CONFIG_SUSPEND |
| 121 | case PM_EVENT_SUSPEND: |
| 122 | if (ops->suspend) { |
| 123 | error = ops->suspend(dev); |
| 124 | suspend_report_result(ops->suspend, error); |
| 125 | } |
| 126 | break; |
| 127 | case PM_EVENT_RESUME: |
| 128 | if (ops->resume) { |
| 129 | error = ops->resume(dev); |
| 130 | suspend_report_result(ops->resume, error); |
| 131 | } |
| 132 | break; |
| 133 | #endif /* CONFIG_SUSPEND */ |
| 134 | #ifdef CONFIG_HIBERNATION |
| 135 | case PM_EVENT_FREEZE: |
| 136 | case PM_EVENT_QUIESCE: |
| 137 | if (ops->freeze) { |
| 138 | error = ops->freeze(dev); |
| 139 | suspend_report_result(ops->freeze, error); |
| 140 | } |
| 141 | break; |
| 142 | case PM_EVENT_HIBERNATE: |
| 143 | if (ops->poweroff) { |
| 144 | error = ops->poweroff(dev); |
| 145 | suspend_report_result(ops->poweroff, error); |
| 146 | } |
| 147 | break; |
| 148 | case PM_EVENT_THAW: |
| 149 | case PM_EVENT_RECOVER: |
| 150 | if (ops->thaw) { |
| 151 | error = ops->thaw(dev); |
| 152 | suspend_report_result(ops->thaw, error); |
| 153 | } |
| 154 | break; |
| 155 | case PM_EVENT_RESTORE: |
| 156 | if (ops->restore) { |
| 157 | error = ops->restore(dev); |
| 158 | suspend_report_result(ops->restore, error); |
| 159 | } |
| 160 | break; |
| 161 | #endif /* CONFIG_HIBERNATION */ |
| 162 | default: |
| 163 | error = -EINVAL; |
| 164 | } |
| 165 | return error; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * pm_noirq_op - execute the PM operation appropiate for given PM event |
| 170 | * @dev: Device. |
| 171 | * @ops: PM operations to choose from. |
| 172 | * @state: PM transition of the system being carried out. |
| 173 | * |
| 174 | * The operation is executed with interrupts disabled by the only remaining |
| 175 | * functional CPU in the system. |
| 176 | */ |
| 177 | static int pm_noirq_op(struct device *dev, struct pm_ext_ops *ops, |
| 178 | pm_message_t state) |
| 179 | { |
| 180 | int error = 0; |
| 181 | |
| 182 | switch (state.event) { |
| 183 | #ifdef CONFIG_SUSPEND |
| 184 | case PM_EVENT_SUSPEND: |
| 185 | if (ops->suspend_noirq) { |
| 186 | error = ops->suspend_noirq(dev); |
| 187 | suspend_report_result(ops->suspend_noirq, error); |
| 188 | } |
| 189 | break; |
| 190 | case PM_EVENT_RESUME: |
| 191 | if (ops->resume_noirq) { |
| 192 | error = ops->resume_noirq(dev); |
| 193 | suspend_report_result(ops->resume_noirq, error); |
| 194 | } |
| 195 | break; |
| 196 | #endif /* CONFIG_SUSPEND */ |
| 197 | #ifdef CONFIG_HIBERNATION |
| 198 | case PM_EVENT_FREEZE: |
| 199 | case PM_EVENT_QUIESCE: |
| 200 | if (ops->freeze_noirq) { |
| 201 | error = ops->freeze_noirq(dev); |
| 202 | suspend_report_result(ops->freeze_noirq, error); |
| 203 | } |
| 204 | break; |
| 205 | case PM_EVENT_HIBERNATE: |
| 206 | if (ops->poweroff_noirq) { |
| 207 | error = ops->poweroff_noirq(dev); |
| 208 | suspend_report_result(ops->poweroff_noirq, error); |
| 209 | } |
| 210 | break; |
| 211 | case PM_EVENT_THAW: |
| 212 | case PM_EVENT_RECOVER: |
| 213 | if (ops->thaw_noirq) { |
| 214 | error = ops->thaw_noirq(dev); |
| 215 | suspend_report_result(ops->thaw_noirq, error); |
| 216 | } |
| 217 | break; |
| 218 | case PM_EVENT_RESTORE: |
| 219 | if (ops->restore_noirq) { |
| 220 | error = ops->restore_noirq(dev); |
| 221 | suspend_report_result(ops->restore_noirq, error); |
| 222 | } |
| 223 | break; |
| 224 | #endif /* CONFIG_HIBERNATION */ |
| 225 | default: |
| 226 | error = -EINVAL; |
| 227 | } |
| 228 | return error; |
| 229 | } |
| 230 | |
| 231 | static char *pm_verb(int event) |
| 232 | { |
| 233 | switch (event) { |
| 234 | case PM_EVENT_SUSPEND: |
| 235 | return "suspend"; |
| 236 | case PM_EVENT_RESUME: |
| 237 | return "resume"; |
| 238 | case PM_EVENT_FREEZE: |
| 239 | return "freeze"; |
| 240 | case PM_EVENT_QUIESCE: |
| 241 | return "quiesce"; |
| 242 | case PM_EVENT_HIBERNATE: |
| 243 | return "hibernate"; |
| 244 | case PM_EVENT_THAW: |
| 245 | return "thaw"; |
| 246 | case PM_EVENT_RESTORE: |
| 247 | return "restore"; |
| 248 | case PM_EVENT_RECOVER: |
| 249 | return "recover"; |
| 250 | default: |
| 251 | return "(unknown PM event)"; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info) |
| 256 | { |
| 257 | dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event), |
| 258 | ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ? |
| 259 | ", may wakeup" : ""); |
| 260 | } |
| 261 | |
| 262 | static void pm_dev_err(struct device *dev, pm_message_t state, char *info, |
| 263 | int error) |
| 264 | { |
| 265 | printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n", |
| 266 | kobject_name(&dev->kobj), pm_verb(state.event), info, error); |
| 267 | } |
| 268 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 269 | /*------------------------- Resume routines -------------------------*/ |
| 270 | |
| 271 | /** |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 272 | * resume_device_noirq - Power on one device (early resume). |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 273 | * @dev: Device. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 274 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 275 | * |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 276 | * Must be called with interrupts disabled. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 277 | */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 278 | static int resume_device_noirq(struct device *dev, pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 279 | { |
| 280 | int error = 0; |
| 281 | |
| 282 | TRACE_DEVICE(dev); |
| 283 | TRACE_RESUME(0); |
| 284 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 285 | if (!dev->bus) |
| 286 | goto End; |
| 287 | |
| 288 | if (dev->bus->pm) { |
| 289 | pm_dev_dbg(dev, state, "EARLY "); |
| 290 | error = pm_noirq_op(dev, dev->bus->pm, state); |
| 291 | } else if (dev->bus->resume_early) { |
| 292 | pm_dev_dbg(dev, state, "legacy EARLY "); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 293 | error = dev->bus->resume_early(dev); |
| 294 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 295 | End: |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 296 | TRACE_RESUME(error); |
| 297 | return error; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * dpm_power_up - Power on all regular (non-sysdev) devices. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 302 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 303 | * |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 304 | * Execute the appropriate "noirq resume" callback for all devices marked |
| 305 | * as DPM_OFF_IRQ. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 306 | * |
| 307 | * Must be called with interrupts disabled and only one CPU running. |
| 308 | */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 309 | static void dpm_power_up(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 310 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 311 | struct device *dev; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 312 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 313 | list_for_each_entry(dev, &dpm_list, power.entry) |
| 314 | if (dev->power.status > DPM_OFF) { |
| 315 | int error; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 316 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 317 | dev->power.status = DPM_OFF; |
| 318 | error = resume_device_noirq(dev, state); |
| 319 | if (error) |
| 320 | pm_dev_err(dev, state, " early", error); |
| 321 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
| 325 | * device_power_up - Turn on all devices that need special attention. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 326 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 327 | * |
| 328 | * Power on system devices, then devices that required we shut them down |
| 329 | * with interrupts disabled. |
| 330 | * |
| 331 | * Must be called with interrupts disabled. |
| 332 | */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 333 | void device_power_up(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 334 | { |
| 335 | sysdev_resume(); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 336 | dpm_power_up(state); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 337 | } |
| 338 | EXPORT_SYMBOL_GPL(device_power_up); |
| 339 | |
| 340 | /** |
| 341 | * resume_device - Restore state for one device. |
| 342 | * @dev: Device. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 343 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 344 | */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 345 | static int resume_device(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 346 | { |
| 347 | int error = 0; |
| 348 | |
| 349 | TRACE_DEVICE(dev); |
| 350 | TRACE_RESUME(0); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 351 | |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 352 | down(&dev->sem); |
| 353 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 354 | if (dev->bus) { |
| 355 | if (dev->bus->pm) { |
| 356 | pm_dev_dbg(dev, state, ""); |
| 357 | error = pm_op(dev, &dev->bus->pm->base, state); |
| 358 | } else if (dev->bus->resume) { |
| 359 | pm_dev_dbg(dev, state, "legacy "); |
| 360 | error = dev->bus->resume(dev); |
| 361 | } |
| 362 | if (error) |
| 363 | goto End; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 364 | } |
| 365 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 366 | if (dev->type) { |
| 367 | if (dev->type->pm) { |
| 368 | pm_dev_dbg(dev, state, "type "); |
| 369 | error = pm_op(dev, dev->type->pm, state); |
| 370 | } else if (dev->type->resume) { |
| 371 | pm_dev_dbg(dev, state, "legacy type "); |
| 372 | error = dev->type->resume(dev); |
| 373 | } |
| 374 | if (error) |
| 375 | goto End; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 378 | if (dev->class) { |
| 379 | if (dev->class->pm) { |
| 380 | pm_dev_dbg(dev, state, "class "); |
| 381 | error = pm_op(dev, dev->class->pm, state); |
| 382 | } else if (dev->class->resume) { |
| 383 | pm_dev_dbg(dev, state, "legacy class "); |
| 384 | error = dev->class->resume(dev); |
| 385 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 386 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 387 | End: |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 388 | up(&dev->sem); |
| 389 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 390 | TRACE_RESUME(error); |
| 391 | return error; |
| 392 | } |
| 393 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 394 | /** |
| 395 | * dpm_resume - Resume every device. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 396 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 397 | * |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 398 | * Execute the appropriate "resume" callback for all devices the status of |
| 399 | * which indicates that they are inactive. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 400 | */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 401 | static void dpm_resume(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 402 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 403 | struct list_head list; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 404 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 405 | INIT_LIST_HEAD(&list); |
| 406 | mutex_lock(&dpm_list_mtx); |
| 407 | transition_started = false; |
| 408 | while (!list_empty(&dpm_list)) { |
| 409 | struct device *dev = to_device(dpm_list.next); |
| 410 | |
| 411 | get_device(dev); |
| 412 | if (dev->power.status >= DPM_OFF) { |
| 413 | int error; |
| 414 | |
| 415 | dev->power.status = DPM_RESUMING; |
| 416 | mutex_unlock(&dpm_list_mtx); |
| 417 | |
| 418 | error = resume_device(dev, state); |
| 419 | |
| 420 | mutex_lock(&dpm_list_mtx); |
| 421 | if (error) |
| 422 | pm_dev_err(dev, state, "", error); |
| 423 | } else if (dev->power.status == DPM_SUSPENDING) { |
| 424 | /* Allow new children of the device to be registered */ |
| 425 | dev->power.status = DPM_RESUMING; |
| 426 | } |
| 427 | if (!list_empty(&dev->power.entry)) |
| 428 | list_move_tail(&dev->power.entry, &list); |
| 429 | put_device(dev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 430 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 431 | list_splice(&list, &dpm_list); |
| 432 | mutex_unlock(&dpm_list_mtx); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * complete_device - Complete a PM transition for given device |
| 437 | * @dev: Device. |
| 438 | * @state: PM transition of the system being carried out. |
| 439 | */ |
| 440 | static void complete_device(struct device *dev, pm_message_t state) |
| 441 | { |
| 442 | down(&dev->sem); |
| 443 | |
| 444 | if (dev->class && dev->class->pm && dev->class->pm->complete) { |
| 445 | pm_dev_dbg(dev, state, "completing class "); |
| 446 | dev->class->pm->complete(dev); |
| 447 | } |
| 448 | |
| 449 | if (dev->type && dev->type->pm && dev->type->pm->complete) { |
| 450 | pm_dev_dbg(dev, state, "completing type "); |
| 451 | dev->type->pm->complete(dev); |
| 452 | } |
| 453 | |
| 454 | if (dev->bus && dev->bus->pm && dev->bus->pm->base.complete) { |
| 455 | pm_dev_dbg(dev, state, "completing "); |
| 456 | dev->bus->pm->base.complete(dev); |
| 457 | } |
| 458 | |
| 459 | up(&dev->sem); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * dpm_complete - Complete a PM transition for all devices. |
| 464 | * @state: PM transition of the system being carried out. |
| 465 | * |
| 466 | * Execute the ->complete() callbacks for all devices that are not marked |
| 467 | * as DPM_ON. |
| 468 | */ |
| 469 | static void dpm_complete(pm_message_t state) |
| 470 | { |
| 471 | struct list_head list; |
| 472 | |
| 473 | INIT_LIST_HEAD(&list); |
| 474 | mutex_lock(&dpm_list_mtx); |
| 475 | while (!list_empty(&dpm_list)) { |
| 476 | struct device *dev = to_device(dpm_list.prev); |
| 477 | |
| 478 | get_device(dev); |
| 479 | if (dev->power.status > DPM_ON) { |
| 480 | dev->power.status = DPM_ON; |
| 481 | mutex_unlock(&dpm_list_mtx); |
| 482 | |
| 483 | complete_device(dev, state); |
| 484 | |
| 485 | mutex_lock(&dpm_list_mtx); |
| 486 | } |
| 487 | if (!list_empty(&dev->power.entry)) |
| 488 | list_move(&dev->power.entry, &list); |
| 489 | put_device(dev); |
| 490 | } |
| 491 | list_splice(&list, &dpm_list); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 492 | mutex_unlock(&dpm_list_mtx); |
| 493 | } |
| 494 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 495 | /** |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 496 | * device_resume - Restore state of each device in system. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 497 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 498 | * |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 499 | * Resume all the devices, unlock them all, and allow new |
| 500 | * devices to be registered once again. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 501 | */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 502 | void device_resume(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 503 | { |
| 504 | might_sleep(); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 505 | dpm_resume(state); |
| 506 | dpm_complete(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 507 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 508 | EXPORT_SYMBOL_GPL(device_resume); |
| 509 | |
| 510 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 511 | /*------------------------- Suspend routines -------------------------*/ |
| 512 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 513 | /** |
| 514 | * resume_event - return a PM message representing the resume event |
| 515 | * corresponding to given sleep state. |
| 516 | * @sleep_state: PM message representing a sleep state. |
| 517 | */ |
| 518 | static pm_message_t resume_event(pm_message_t sleep_state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 519 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 520 | switch (sleep_state.event) { |
| 521 | case PM_EVENT_SUSPEND: |
| 522 | return PMSG_RESUME; |
| 523 | case PM_EVENT_FREEZE: |
| 524 | case PM_EVENT_QUIESCE: |
| 525 | return PMSG_RECOVER; |
| 526 | case PM_EVENT_HIBERNATE: |
| 527 | return PMSG_RESTORE; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 528 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 529 | return PMSG_ON; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /** |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 533 | * suspend_device_noirq - Shut down one device (late suspend). |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 534 | * @dev: Device. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 535 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 536 | * |
| 537 | * This is called with interrupts off and only a single CPU running. |
| 538 | */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 539 | static int suspend_device_noirq(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 540 | { |
| 541 | int error = 0; |
| 542 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 543 | if (!dev->bus) |
| 544 | return 0; |
| 545 | |
| 546 | if (dev->bus->pm) { |
| 547 | pm_dev_dbg(dev, state, "LATE "); |
| 548 | error = pm_noirq_op(dev, dev->bus->pm, state); |
| 549 | } else if (dev->bus->suspend_late) { |
| 550 | pm_dev_dbg(dev, state, "legacy LATE "); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 551 | error = dev->bus->suspend_late(dev, state); |
| 552 | suspend_report_result(dev->bus->suspend_late, error); |
| 553 | } |
| 554 | return error; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * device_power_down - Shut down special devices. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 559 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 560 | * |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 561 | * Power down devices that require interrupts to be disabled. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 562 | * Then power down system devices. |
| 563 | * |
| 564 | * Must be called with interrupts disabled and only one CPU running. |
| 565 | */ |
| 566 | int device_power_down(pm_message_t state) |
| 567 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 568 | struct device *dev; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 569 | int error = 0; |
| 570 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 571 | list_for_each_entry_reverse(dev, &dpm_list, power.entry) { |
| 572 | error = suspend_device_noirq(dev, state); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 573 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 574 | pm_dev_err(dev, state, " late", error); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 575 | break; |
| 576 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 577 | dev->power.status = DPM_OFF_IRQ; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 578 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 579 | if (!error) |
| 580 | error = sysdev_suspend(state); |
| 581 | if (error) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 582 | dpm_power_up(resume_event(state)); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 583 | return error; |
| 584 | } |
| 585 | EXPORT_SYMBOL_GPL(device_power_down); |
| 586 | |
| 587 | /** |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 588 | * suspend_device - Save state of one device. |
| 589 | * @dev: Device. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 590 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 591 | */ |
Adrian Bunk | 19e20c9 | 2008-02-03 22:55:18 +0100 | [diff] [blame] | 592 | static int suspend_device(struct device *dev, pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 593 | { |
| 594 | int error = 0; |
| 595 | |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 596 | down(&dev->sem); |
| 597 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 598 | if (dev->class) { |
| 599 | if (dev->class->pm) { |
| 600 | pm_dev_dbg(dev, state, "class "); |
| 601 | error = pm_op(dev, dev->class->pm, state); |
| 602 | } else if (dev->class->suspend) { |
| 603 | pm_dev_dbg(dev, state, "legacy class "); |
| 604 | error = dev->class->suspend(dev, state); |
| 605 | suspend_report_result(dev->class->suspend, error); |
| 606 | } |
| 607 | if (error) |
| 608 | goto End; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 609 | } |
| 610 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 611 | if (dev->type) { |
| 612 | if (dev->type->pm) { |
| 613 | pm_dev_dbg(dev, state, "type "); |
| 614 | error = pm_op(dev, dev->type->pm, state); |
| 615 | } else if (dev->type->suspend) { |
| 616 | pm_dev_dbg(dev, state, "legacy type "); |
| 617 | error = dev->type->suspend(dev, state); |
| 618 | suspend_report_result(dev->type->suspend, error); |
| 619 | } |
| 620 | if (error) |
| 621 | goto End; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 622 | } |
| 623 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 624 | if (dev->bus) { |
| 625 | if (dev->bus->pm) { |
| 626 | pm_dev_dbg(dev, state, ""); |
| 627 | error = pm_op(dev, &dev->bus->pm->base, state); |
| 628 | } else if (dev->bus->suspend) { |
| 629 | pm_dev_dbg(dev, state, "legacy "); |
| 630 | error = dev->bus->suspend(dev, state); |
| 631 | suspend_report_result(dev->bus->suspend, error); |
| 632 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 633 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 634 | End: |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 635 | up(&dev->sem); |
| 636 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 637 | return error; |
| 638 | } |
| 639 | |
| 640 | /** |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 641 | * dpm_suspend - Suspend every device. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 642 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 643 | * |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 644 | * Execute the appropriate "suspend" callbacks for all devices. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 645 | */ |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 646 | static int dpm_suspend(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 647 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 648 | struct list_head list; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 649 | int error = 0; |
| 650 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 651 | INIT_LIST_HEAD(&list); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 652 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 653 | while (!list_empty(&dpm_list)) { |
| 654 | struct device *dev = to_device(dpm_list.prev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 655 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 656 | get_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 657 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 658 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 659 | error = suspend_device(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 660 | |
Alan Stern | 1b3cbec | 2008-02-29 11:50:22 -0500 | [diff] [blame] | 661 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 662 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 663 | pm_dev_err(dev, state, "", error); |
| 664 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 665 | break; |
| 666 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 667 | dev->power.status = DPM_OFF; |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 668 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 669 | list_move(&dev->power.entry, &list); |
| 670 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 671 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 672 | list_splice(&list, dpm_list.prev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 673 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 674 | return error; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * prepare_device - Execute the ->prepare() callback(s) for given device. |
| 679 | * @dev: Device. |
| 680 | * @state: PM transition of the system being carried out. |
| 681 | */ |
| 682 | static int prepare_device(struct device *dev, pm_message_t state) |
| 683 | { |
| 684 | int error = 0; |
| 685 | |
| 686 | down(&dev->sem); |
| 687 | |
| 688 | if (dev->bus && dev->bus->pm && dev->bus->pm->base.prepare) { |
| 689 | pm_dev_dbg(dev, state, "preparing "); |
| 690 | error = dev->bus->pm->base.prepare(dev); |
| 691 | suspend_report_result(dev->bus->pm->base.prepare, error); |
| 692 | if (error) |
| 693 | goto End; |
| 694 | } |
| 695 | |
| 696 | if (dev->type && dev->type->pm && dev->type->pm->prepare) { |
| 697 | pm_dev_dbg(dev, state, "preparing type "); |
| 698 | error = dev->type->pm->prepare(dev); |
| 699 | suspend_report_result(dev->type->pm->prepare, error); |
| 700 | if (error) |
| 701 | goto End; |
| 702 | } |
| 703 | |
| 704 | if (dev->class && dev->class->pm && dev->class->pm->prepare) { |
| 705 | pm_dev_dbg(dev, state, "preparing class "); |
| 706 | error = dev->class->pm->prepare(dev); |
| 707 | suspend_report_result(dev->class->pm->prepare, error); |
| 708 | } |
| 709 | End: |
| 710 | up(&dev->sem); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 711 | |
| 712 | return error; |
| 713 | } |
| 714 | |
| 715 | /** |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 716 | * dpm_prepare - Prepare all devices for a PM transition. |
| 717 | * @state: PM transition of the system being carried out. |
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 | * Execute the ->prepare() callback for all devices. |
| 720 | */ |
| 721 | static int dpm_prepare(pm_message_t state) |
| 722 | { |
| 723 | struct list_head list; |
| 724 | int error = 0; |
| 725 | |
| 726 | INIT_LIST_HEAD(&list); |
| 727 | mutex_lock(&dpm_list_mtx); |
| 728 | transition_started = true; |
| 729 | while (!list_empty(&dpm_list)) { |
| 730 | struct device *dev = to_device(dpm_list.next); |
| 731 | |
| 732 | get_device(dev); |
| 733 | dev->power.status = DPM_PREPARING; |
| 734 | mutex_unlock(&dpm_list_mtx); |
| 735 | |
| 736 | error = prepare_device(dev, state); |
| 737 | |
| 738 | mutex_lock(&dpm_list_mtx); |
| 739 | if (error) { |
| 740 | dev->power.status = DPM_ON; |
| 741 | if (error == -EAGAIN) { |
| 742 | put_device(dev); |
| 743 | continue; |
| 744 | } |
| 745 | printk(KERN_ERR "PM: Failed to prepare device %s " |
| 746 | "for power transition: error %d\n", |
| 747 | kobject_name(&dev->kobj), error); |
| 748 | put_device(dev); |
| 749 | break; |
| 750 | } |
| 751 | dev->power.status = DPM_SUSPENDING; |
| 752 | if (!list_empty(&dev->power.entry)) |
| 753 | list_move_tail(&dev->power.entry, &list); |
| 754 | put_device(dev); |
| 755 | } |
| 756 | list_splice(&list, &dpm_list); |
| 757 | mutex_unlock(&dpm_list_mtx); |
| 758 | return error; |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * device_suspend - Save state and stop all devices in system. |
| 763 | * @state: PM transition of the system being carried out. |
| 764 | * |
| 765 | * Prepare and suspend all devices. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 766 | */ |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 767 | int device_suspend(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 768 | { |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 769 | int error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 770 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 771 | might_sleep(); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 772 | error = dpm_prepare(state); |
| 773 | if (!error) |
| 774 | error = dpm_suspend(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 775 | return error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 776 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 777 | EXPORT_SYMBOL_GPL(device_suspend); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 778 | |
| 779 | void __suspend_report_result(const char *function, void *fn, int ret) |
| 780 | { |
Bjorn Helgaas | c80cfb0 | 2008-10-15 22:01:35 -0700 | [diff] [blame^] | 781 | if (ret) |
| 782 | printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 783 | } |
| 784 | EXPORT_SYMBOL_GPL(__suspend_report_result); |