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