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