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