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