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 | * |
| 15 | * A different set of lists than the global subsystem list are used to |
| 16 | * keep track of power info because we use different lists to hold |
| 17 | * devices based on what stage of the power management process they |
| 18 | * are in. The power domain dependencies may also differ from the |
| 19 | * ancestral dependencies that the subsystem list maintains. |
| 20 | */ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/device.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 23 | #include <linux/kallsyms.h> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 25 | #include <linux/pm.h> |
| 26 | #include <linux/resume-trace.h> |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 27 | #include <linux/rwsem.h> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 28 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 29 | #include "../base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "power.h" |
| 31 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 32 | /* |
| 33 | * The entries in the dpm_active list are in a depth first order, simply |
| 34 | * because children are guaranteed to be discovered after parents, and |
| 35 | * are inserted at the back of the list on discovery. |
| 36 | * |
| 37 | * All the other lists are kept in the same order, for consistency. |
| 38 | * However the lists aren't always traversed in the same order. |
| 39 | * Semaphores must be acquired from the top (i.e., front) down |
| 40 | * and released in the opposite order. Devices must be suspended |
| 41 | * from the bottom (i.e., end) up and resumed in the opposite order. |
| 42 | * That way no parent will be suspended while it still has an active |
| 43 | * child. |
| 44 | * |
| 45 | * Since device_pm_add() may be called with a device semaphore held, |
| 46 | * we must never try to acquire a device semaphore while holding |
| 47 | * dpm_list_mutex. |
| 48 | */ |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | LIST_HEAD(dpm_active); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 51 | static LIST_HEAD(dpm_off); |
| 52 | static LIST_HEAD(dpm_off_irq); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 53 | static LIST_HEAD(dpm_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 55 | static DEFINE_MUTEX(dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 57 | static DECLARE_RWSEM(pm_sleep_rwsem); |
| 58 | |
David Brownell | 075c177 | 2007-04-26 00:12:06 -0700 | [diff] [blame] | 59 | int (*platform_enable_wakeup)(struct device *dev, int is_on); |
| 60 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 61 | /** |
| 62 | * device_pm_add - add a device to the list of active devices |
| 63 | * @dev: Device to be added to the list |
| 64 | */ |
Daniel Drake | dec13c1 | 2007-11-21 14:55:18 -0800 | [diff] [blame] | 65 | void device_pm_add(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | pr_debug("PM: Adding info for %s:%s\n", |
Dmitry Torokhov | c48ea60 | 2007-04-11 01:37:18 -0400 | [diff] [blame] | 68 | dev->bus ? dev->bus->name : "No Bus", |
| 69 | kobject_name(&dev->kobj)); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 70 | mutex_lock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | list_add_tail(&dev->power.entry, &dpm_active); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 72 | mutex_unlock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 75 | /** |
| 76 | * device_pm_remove - remove a device from the list of active devices |
| 77 | * @dev: Device to be removed from the list |
| 78 | * |
| 79 | * This function also removes the device's PM-related sysfs attributes. |
| 80 | */ |
Rafael J. Wysocki | 9cddad7 | 2007-06-13 15:53:34 +0200 | [diff] [blame] | 81 | void device_pm_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | pr_debug("PM: Removing info for %s:%s\n", |
Dmitry Torokhov | c48ea60 | 2007-04-11 01:37:18 -0400 | [diff] [blame] | 84 | dev->bus ? dev->bus->name : "No Bus", |
| 85 | kobject_name(&dev->kobj)); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 86 | mutex_lock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | dpm_sysfs_remove(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | list_del_init(&dev->power.entry); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 89 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * device_pm_schedule_removal - schedule the removal of a suspended device |
| 94 | * @dev: Device to destroy |
| 95 | * |
| 96 | * Moves the device to the dpm_destroy list for further processing by |
| 97 | * unregister_dropped_devices(). |
| 98 | */ |
| 99 | void device_pm_schedule_removal(struct device *dev) |
| 100 | { |
| 101 | pr_debug("PM: Preparing for removal: %s:%s\n", |
| 102 | dev->bus ? dev->bus->name : "No Bus", |
| 103 | kobject_name(&dev->kobj)); |
| 104 | mutex_lock(&dpm_list_mtx); |
| 105 | list_move_tail(&dev->power.entry, &dpm_destroy); |
| 106 | mutex_unlock(&dpm_list_mtx); |
| 107 | } |
Rafael J. Wysocki | 9617c3e | 2008-01-25 01:30:25 +0100 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(device_pm_schedule_removal); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * pm_sleep_lock - mutual exclusion for registration and suspend |
| 112 | * |
| 113 | * Returns 0 if no suspend is underway and device registration |
| 114 | * may proceed, otherwise -EBUSY. |
| 115 | */ |
| 116 | int pm_sleep_lock(void) |
| 117 | { |
| 118 | if (down_read_trylock(&pm_sleep_rwsem)) |
| 119 | return 0; |
| 120 | |
| 121 | return -EBUSY; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * pm_sleep_unlock - mutual exclusion for registration and suspend |
| 126 | * |
| 127 | * This routine undoes the effect of device_pm_add_lock |
| 128 | * when a device's registration is complete. |
| 129 | */ |
| 130 | void pm_sleep_unlock(void) |
| 131 | { |
| 132 | up_read(&pm_sleep_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 136 | /*------------------------- Resume routines -------------------------*/ |
| 137 | |
| 138 | /** |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 139 | * resume_device_early - Power on one device (early resume). |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 140 | * @dev: Device. |
| 141 | * |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 142 | * Must be called with interrupts disabled. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 143 | */ |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 144 | static int resume_device_early(struct device *dev) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 145 | { |
| 146 | int error = 0; |
| 147 | |
| 148 | TRACE_DEVICE(dev); |
| 149 | TRACE_RESUME(0); |
| 150 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 151 | if (dev->bus && dev->bus->resume_early) { |
| 152 | dev_dbg(dev, "EARLY resume\n"); |
| 153 | error = dev->bus->resume_early(dev); |
| 154 | } |
| 155 | |
| 156 | TRACE_RESUME(error); |
| 157 | return error; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * dpm_power_up - Power on all regular (non-sysdev) devices. |
| 162 | * |
| 163 | * Walk the dpm_off_irq list and power each device up. This |
| 164 | * is used for devices that required they be powered down with |
| 165 | * interrupts disabled. As devices are powered on, they are moved |
| 166 | * to the dpm_off list. |
| 167 | * |
| 168 | * Must be called with interrupts disabled and only one CPU running. |
| 169 | */ |
| 170 | static void dpm_power_up(void) |
| 171 | { |
| 172 | |
| 173 | while (!list_empty(&dpm_off_irq)) { |
| 174 | struct list_head *entry = dpm_off_irq.next; |
| 175 | struct device *dev = to_device(entry); |
| 176 | |
| 177 | list_move_tail(entry, &dpm_off); |
| 178 | resume_device_early(dev); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * device_power_up - Turn on all devices that need special attention. |
| 184 | * |
| 185 | * Power on system devices, then devices that required we shut them down |
| 186 | * with interrupts disabled. |
| 187 | * |
| 188 | * Must be called with interrupts disabled. |
| 189 | */ |
| 190 | void device_power_up(void) |
| 191 | { |
| 192 | sysdev_resume(); |
| 193 | dpm_power_up(); |
| 194 | } |
| 195 | EXPORT_SYMBOL_GPL(device_power_up); |
| 196 | |
| 197 | /** |
| 198 | * resume_device - Restore state for one device. |
| 199 | * @dev: Device. |
| 200 | * |
| 201 | */ |
| 202 | static int resume_device(struct device *dev) |
| 203 | { |
| 204 | int error = 0; |
| 205 | |
| 206 | TRACE_DEVICE(dev); |
| 207 | TRACE_RESUME(0); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 208 | |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame^] | 209 | down(&dev->sem); |
| 210 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 211 | if (dev->bus && dev->bus->resume) { |
| 212 | dev_dbg(dev,"resuming\n"); |
| 213 | error = dev->bus->resume(dev); |
| 214 | } |
| 215 | |
| 216 | if (!error && dev->type && dev->type->resume) { |
| 217 | dev_dbg(dev,"resuming\n"); |
| 218 | error = dev->type->resume(dev); |
| 219 | } |
| 220 | |
| 221 | if (!error && dev->class && dev->class->resume) { |
| 222 | dev_dbg(dev,"class resume\n"); |
| 223 | error = dev->class->resume(dev); |
| 224 | } |
| 225 | |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame^] | 226 | up(&dev->sem); |
| 227 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 228 | TRACE_RESUME(error); |
| 229 | return error; |
| 230 | } |
| 231 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 232 | /** |
| 233 | * dpm_resume - Resume every device. |
| 234 | * |
| 235 | * Resume the devices that have either not gone through |
| 236 | * the late suspend, or that did go through it but also |
| 237 | * went through the early resume. |
| 238 | * |
| 239 | * Take devices from the dpm_off_list, resume them, |
| 240 | * and put them on the dpm_locked list. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 241 | */ |
| 242 | static void dpm_resume(void) |
| 243 | { |
| 244 | mutex_lock(&dpm_list_mtx); |
| 245 | while(!list_empty(&dpm_off)) { |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 246 | struct list_head *entry = dpm_off.next; |
| 247 | struct device *dev = to_device(entry); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 248 | |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame^] | 249 | list_move_tail(entry, &dpm_active); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 250 | mutex_unlock(&dpm_list_mtx); |
| 251 | resume_device(dev); |
| 252 | mutex_lock(&dpm_list_mtx); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 253 | } |
| 254 | mutex_unlock(&dpm_list_mtx); |
| 255 | } |
| 256 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 257 | /** |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 258 | * unregister_dropped_devices - Unregister devices scheduled for removal |
| 259 | * |
| 260 | * Unregister all devices on the dpm_destroy list. |
| 261 | */ |
| 262 | static void unregister_dropped_devices(void) |
| 263 | { |
| 264 | mutex_lock(&dpm_list_mtx); |
| 265 | while (!list_empty(&dpm_destroy)) { |
| 266 | struct list_head *entry = dpm_destroy.next; |
| 267 | struct device *dev = to_device(entry); |
| 268 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 269 | mutex_unlock(&dpm_list_mtx); |
| 270 | /* This also removes the device from the list */ |
| 271 | device_unregister(dev); |
| 272 | mutex_lock(&dpm_list_mtx); |
| 273 | } |
| 274 | mutex_unlock(&dpm_list_mtx); |
| 275 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * device_resume - Restore state of each device in system. |
| 279 | * |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 280 | * Resume all the devices, unlock them all, and allow new |
| 281 | * devices to be registered once again. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 282 | */ |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 283 | void device_resume(void) |
| 284 | { |
| 285 | might_sleep(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 286 | dpm_resume(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 287 | unregister_dropped_devices(); |
| 288 | up_write(&pm_sleep_rwsem); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 289 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 290 | EXPORT_SYMBOL_GPL(device_resume); |
| 291 | |
| 292 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 293 | /*------------------------- Suspend routines -------------------------*/ |
| 294 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 295 | static inline char *suspend_verb(u32 event) |
| 296 | { |
| 297 | switch (event) { |
| 298 | case PM_EVENT_SUSPEND: return "suspend"; |
| 299 | case PM_EVENT_FREEZE: return "freeze"; |
| 300 | case PM_EVENT_PRETHAW: return "prethaw"; |
| 301 | default: return "(unknown suspend event)"; |
| 302 | } |
| 303 | } |
| 304 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 305 | static void |
| 306 | suspend_device_dbg(struct device *dev, pm_message_t state, char *info) |
| 307 | { |
| 308 | dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event), |
| 309 | ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ? |
| 310 | ", may wakeup" : ""); |
| 311 | } |
| 312 | |
| 313 | /** |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 314 | * suspend_device_late - Shut down one device (late suspend). |
| 315 | * @dev: Device. |
| 316 | * @state: Power state device is entering. |
| 317 | * |
| 318 | * This is called with interrupts off and only a single CPU running. |
| 319 | */ |
| 320 | static int suspend_device_late(struct device *dev, pm_message_t state) |
| 321 | { |
| 322 | int error = 0; |
| 323 | |
| 324 | if (dev->bus && dev->bus->suspend_late) { |
| 325 | suspend_device_dbg(dev, state, "LATE "); |
| 326 | error = dev->bus->suspend_late(dev, state); |
| 327 | suspend_report_result(dev->bus->suspend_late, error); |
| 328 | } |
| 329 | return error; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * device_power_down - Shut down special devices. |
| 334 | * @state: Power state to enter. |
| 335 | * |
| 336 | * Power down devices that require interrupts to be disabled |
| 337 | * and move them from the dpm_off list to the dpm_off_irq list. |
| 338 | * Then power down system devices. |
| 339 | * |
| 340 | * Must be called with interrupts disabled and only one CPU running. |
| 341 | */ |
| 342 | int device_power_down(pm_message_t state) |
| 343 | { |
| 344 | int error = 0; |
| 345 | |
| 346 | while (!list_empty(&dpm_off)) { |
| 347 | struct list_head *entry = dpm_off.prev; |
| 348 | struct device *dev = to_device(entry); |
| 349 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 350 | error = suspend_device_late(dev, state); |
| 351 | if (error) { |
| 352 | printk(KERN_ERR "Could not power down device %s: " |
| 353 | "error %d\n", |
| 354 | kobject_name(&dev->kobj), error); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 355 | break; |
| 356 | } |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame^] | 357 | if (!list_empty(&dev->power.entry)) |
| 358 | list_move(&dev->power.entry, &dpm_off_irq); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | if (!error) |
| 362 | error = sysdev_suspend(state); |
| 363 | if (error) |
| 364 | dpm_power_up(); |
| 365 | return error; |
| 366 | } |
| 367 | EXPORT_SYMBOL_GPL(device_power_down); |
| 368 | |
| 369 | /** |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 370 | * suspend_device - Save state of one device. |
| 371 | * @dev: Device. |
| 372 | * @state: Power state device is entering. |
| 373 | */ |
Adrian Bunk | 19e20c9 | 2008-02-03 22:55:18 +0100 | [diff] [blame] | 374 | static int suspend_device(struct device *dev, pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 375 | { |
| 376 | int error = 0; |
| 377 | |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame^] | 378 | down(&dev->sem); |
| 379 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 380 | if (dev->power.power_state.event) { |
| 381 | dev_dbg(dev, "PM: suspend %d-->%d\n", |
| 382 | dev->power.power_state.event, state.event); |
| 383 | } |
| 384 | |
| 385 | if (dev->class && dev->class->suspend) { |
| 386 | suspend_device_dbg(dev, state, "class "); |
| 387 | error = dev->class->suspend(dev, state); |
| 388 | suspend_report_result(dev->class->suspend, error); |
| 389 | } |
| 390 | |
| 391 | if (!error && dev->type && dev->type->suspend) { |
| 392 | suspend_device_dbg(dev, state, "type "); |
| 393 | error = dev->type->suspend(dev, state); |
| 394 | suspend_report_result(dev->type->suspend, error); |
| 395 | } |
| 396 | |
| 397 | if (!error && dev->bus && dev->bus->suspend) { |
| 398 | suspend_device_dbg(dev, state, ""); |
| 399 | error = dev->bus->suspend(dev, state); |
| 400 | suspend_report_result(dev->bus->suspend, error); |
| 401 | } |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame^] | 402 | |
| 403 | up(&dev->sem); |
| 404 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 405 | return error; |
| 406 | } |
| 407 | |
| 408 | /** |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 409 | * dpm_suspend - Suspend every device. |
| 410 | * @state: Power state to put each device in. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 411 | * |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 412 | * Walk the dpm_locked list. Suspend each device and move it |
| 413 | * to the dpm_off list. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 414 | * |
| 415 | * (For historical reasons, if it returns -EAGAIN, that used to mean |
| 416 | * that the device would be called again with interrupts disabled. |
| 417 | * These days, we use the "suspend_late()" callback for that, so we |
| 418 | * print a warning and consider it an error). |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 419 | */ |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 420 | static int dpm_suspend(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 421 | { |
| 422 | int error = 0; |
| 423 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 424 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame^] | 425 | while (!list_empty(&dpm_active)) { |
| 426 | struct list_head *entry = dpm_active.prev; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 427 | struct device *dev = to_device(entry); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 428 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 429 | mutex_unlock(&dpm_list_mtx); |
| 430 | error = suspend_device(dev, state); |
| 431 | if (error) { |
| 432 | printk(KERN_ERR "Could not suspend device %s: " |
| 433 | "error %d%s\n", |
| 434 | kobject_name(&dev->kobj), |
| 435 | error, |
| 436 | (error == -EAGAIN ? |
| 437 | " (please convert to suspend_late)" : |
| 438 | "")); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame^] | 442 | if (!list_empty(&dev->power.entry)) |
| 443 | list_move(&dev->power.entry, &dpm_off); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 444 | } |
| 445 | mutex_unlock(&dpm_list_mtx); |
| 446 | |
| 447 | return error; |
| 448 | } |
| 449 | |
| 450 | /** |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 451 | * device_suspend - Save state and stop all devices in system. |
Randy Dunlap | 7199677 | 2008-02-18 13:09:03 -0800 | [diff] [blame] | 452 | * @state: new power management state |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 453 | * |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 454 | * Prevent new devices from being registered, then lock all devices |
| 455 | * and suspend them. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 456 | */ |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 457 | int device_suspend(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 458 | { |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 459 | int error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 460 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 461 | might_sleep(); |
| 462 | down_write(&pm_sleep_rwsem); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 463 | error = dpm_suspend(state); |
| 464 | if (error) |
| 465 | device_resume(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 466 | return error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 467 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 468 | EXPORT_SYMBOL_GPL(device_suspend); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 469 | |
| 470 | void __suspend_report_result(const char *function, void *fn, int ret) |
| 471 | { |
| 472 | if (ret) { |
| 473 | printk(KERN_ERR "%s(): ", function); |
| 474 | print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn); |
| 475 | printk("%d\n", ret); |
| 476 | } |
| 477 | } |
| 478 | EXPORT_SYMBOL_GPL(__suspend_report_result); |