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