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 | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 27 | #include <linux/sched.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 | |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 164 | static ktime_t initcall_debug_start(struct device *dev) |
| 165 | { |
| 166 | ktime_t calltime = ktime_set(0, 0); |
| 167 | |
| 168 | if (initcall_debug) { |
| 169 | pr_info("calling %s+ @ %i\n", |
| 170 | dev_name(dev), task_pid_nr(current)); |
| 171 | calltime = ktime_get(); |
| 172 | } |
| 173 | |
| 174 | return calltime; |
| 175 | } |
| 176 | |
| 177 | static void initcall_debug_report(struct device *dev, ktime_t calltime, |
| 178 | int error) |
| 179 | { |
| 180 | ktime_t delta, rettime; |
| 181 | |
| 182 | if (initcall_debug) { |
| 183 | rettime = ktime_get(); |
| 184 | delta = ktime_sub(rettime, calltime); |
| 185 | pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev), |
| 186 | error, (unsigned long long)ktime_to_ns(delta) >> 10); |
| 187 | } |
| 188 | } |
| 189 | |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 190 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 191 | * pm_op - Execute the PM operation appropriate for given PM event. |
| 192 | * @dev: Device to handle. |
| 193 | * @ops: PM operations to choose from. |
| 194 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 195 | */ |
Dmitry Torokhov | d9ab771 | 2009-07-22 00:37:25 +0200 | [diff] [blame] | 196 | static int pm_op(struct device *dev, |
| 197 | const struct dev_pm_ops *ops, |
| 198 | pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 199 | { |
| 200 | int error = 0; |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 201 | ktime_t calltime; |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 202 | |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 203 | calltime = initcall_debug_start(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 204 | |
| 205 | switch (state.event) { |
| 206 | #ifdef CONFIG_SUSPEND |
| 207 | case PM_EVENT_SUSPEND: |
| 208 | if (ops->suspend) { |
| 209 | error = ops->suspend(dev); |
| 210 | suspend_report_result(ops->suspend, error); |
| 211 | } |
| 212 | break; |
| 213 | case PM_EVENT_RESUME: |
| 214 | if (ops->resume) { |
| 215 | error = ops->resume(dev); |
| 216 | suspend_report_result(ops->resume, error); |
| 217 | } |
| 218 | break; |
| 219 | #endif /* CONFIG_SUSPEND */ |
| 220 | #ifdef CONFIG_HIBERNATION |
| 221 | case PM_EVENT_FREEZE: |
| 222 | case PM_EVENT_QUIESCE: |
| 223 | if (ops->freeze) { |
| 224 | error = ops->freeze(dev); |
| 225 | suspend_report_result(ops->freeze, error); |
| 226 | } |
| 227 | break; |
| 228 | case PM_EVENT_HIBERNATE: |
| 229 | if (ops->poweroff) { |
| 230 | error = ops->poweroff(dev); |
| 231 | suspend_report_result(ops->poweroff, error); |
| 232 | } |
| 233 | break; |
| 234 | case PM_EVENT_THAW: |
| 235 | case PM_EVENT_RECOVER: |
| 236 | if (ops->thaw) { |
| 237 | error = ops->thaw(dev); |
| 238 | suspend_report_result(ops->thaw, error); |
| 239 | } |
| 240 | break; |
| 241 | case PM_EVENT_RESTORE: |
| 242 | if (ops->restore) { |
| 243 | error = ops->restore(dev); |
| 244 | suspend_report_result(ops->restore, error); |
| 245 | } |
| 246 | break; |
| 247 | #endif /* CONFIG_HIBERNATION */ |
| 248 | default: |
| 249 | error = -EINVAL; |
| 250 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 251 | |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 252 | initcall_debug_report(dev, calltime, error); |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 253 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 254 | return error; |
| 255 | } |
| 256 | |
| 257 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 258 | * pm_noirq_op - Execute the PM operation appropriate for given PM event. |
| 259 | * @dev: Device to handle. |
| 260 | * @ops: PM operations to choose from. |
| 261 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 262 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 263 | * The driver of @dev will not receive interrupts while this function is being |
| 264 | * executed. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 265 | */ |
Dmitry Torokhov | d9ab771 | 2009-07-22 00:37:25 +0200 | [diff] [blame] | 266 | static int pm_noirq_op(struct device *dev, |
| 267 | const struct dev_pm_ops *ops, |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 268 | pm_message_t state) |
| 269 | { |
| 270 | int error = 0; |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 271 | ktime_t calltime, delta, rettime; |
| 272 | |
| 273 | if (initcall_debug) { |
| 274 | pr_info("calling %s_i+ @ %i\n", |
| 275 | dev_name(dev), task_pid_nr(current)); |
| 276 | calltime = ktime_get(); |
| 277 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 278 | |
| 279 | switch (state.event) { |
| 280 | #ifdef CONFIG_SUSPEND |
| 281 | case PM_EVENT_SUSPEND: |
| 282 | if (ops->suspend_noirq) { |
| 283 | error = ops->suspend_noirq(dev); |
| 284 | suspend_report_result(ops->suspend_noirq, error); |
| 285 | } |
| 286 | break; |
| 287 | case PM_EVENT_RESUME: |
| 288 | if (ops->resume_noirq) { |
| 289 | error = ops->resume_noirq(dev); |
| 290 | suspend_report_result(ops->resume_noirq, error); |
| 291 | } |
| 292 | break; |
| 293 | #endif /* CONFIG_SUSPEND */ |
| 294 | #ifdef CONFIG_HIBERNATION |
| 295 | case PM_EVENT_FREEZE: |
| 296 | case PM_EVENT_QUIESCE: |
| 297 | if (ops->freeze_noirq) { |
| 298 | error = ops->freeze_noirq(dev); |
| 299 | suspend_report_result(ops->freeze_noirq, error); |
| 300 | } |
| 301 | break; |
| 302 | case PM_EVENT_HIBERNATE: |
| 303 | if (ops->poweroff_noirq) { |
| 304 | error = ops->poweroff_noirq(dev); |
| 305 | suspend_report_result(ops->poweroff_noirq, error); |
| 306 | } |
| 307 | break; |
| 308 | case PM_EVENT_THAW: |
| 309 | case PM_EVENT_RECOVER: |
| 310 | if (ops->thaw_noirq) { |
| 311 | error = ops->thaw_noirq(dev); |
| 312 | suspend_report_result(ops->thaw_noirq, error); |
| 313 | } |
| 314 | break; |
| 315 | case PM_EVENT_RESTORE: |
| 316 | if (ops->restore_noirq) { |
| 317 | error = ops->restore_noirq(dev); |
| 318 | suspend_report_result(ops->restore_noirq, error); |
| 319 | } |
| 320 | break; |
| 321 | #endif /* CONFIG_HIBERNATION */ |
| 322 | default: |
| 323 | error = -EINVAL; |
| 324 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 325 | |
| 326 | if (initcall_debug) { |
| 327 | rettime = ktime_get(); |
| 328 | delta = ktime_sub(rettime, calltime); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 329 | printk("initcall %s_i+ returned %d after %Ld usecs\n", |
| 330 | dev_name(dev), error, |
| 331 | (unsigned long long)ktime_to_ns(delta) >> 10); |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 334 | return error; |
| 335 | } |
| 336 | |
| 337 | static char *pm_verb(int event) |
| 338 | { |
| 339 | switch (event) { |
| 340 | case PM_EVENT_SUSPEND: |
| 341 | return "suspend"; |
| 342 | case PM_EVENT_RESUME: |
| 343 | return "resume"; |
| 344 | case PM_EVENT_FREEZE: |
| 345 | return "freeze"; |
| 346 | case PM_EVENT_QUIESCE: |
| 347 | return "quiesce"; |
| 348 | case PM_EVENT_HIBERNATE: |
| 349 | return "hibernate"; |
| 350 | case PM_EVENT_THAW: |
| 351 | return "thaw"; |
| 352 | case PM_EVENT_RESTORE: |
| 353 | return "restore"; |
| 354 | case PM_EVENT_RECOVER: |
| 355 | return "recover"; |
| 356 | default: |
| 357 | return "(unknown PM event)"; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info) |
| 362 | { |
| 363 | dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event), |
| 364 | ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ? |
| 365 | ", may wakeup" : ""); |
| 366 | } |
| 367 | |
| 368 | static void pm_dev_err(struct device *dev, pm_message_t state, char *info, |
| 369 | int error) |
| 370 | { |
| 371 | printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n", |
| 372 | kobject_name(&dev->kobj), pm_verb(state.event), info, error); |
| 373 | } |
| 374 | |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 375 | static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info) |
| 376 | { |
| 377 | ktime_t calltime; |
| 378 | s64 usecs64; |
| 379 | int usecs; |
| 380 | |
| 381 | calltime = ktime_get(); |
| 382 | usecs64 = ktime_to_ns(ktime_sub(calltime, starttime)); |
| 383 | do_div(usecs64, NSEC_PER_USEC); |
| 384 | usecs = usecs64; |
| 385 | if (usecs == 0) |
| 386 | usecs = 1; |
| 387 | pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n", |
| 388 | info ?: "", info ? " " : "", pm_verb(state.event), |
| 389 | usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC); |
| 390 | } |
| 391 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 392 | /*------------------------- Resume routines -------------------------*/ |
| 393 | |
| 394 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 395 | * device_resume_noirq - Execute an "early resume" callback for given device. |
| 396 | * @dev: Device to handle. |
| 397 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 398 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 399 | * The driver of @dev will not receive interrupts while this function is being |
| 400 | * executed. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 401 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 402 | static int device_resume_noirq(struct device *dev, pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 403 | { |
| 404 | int error = 0; |
| 405 | |
| 406 | TRACE_DEVICE(dev); |
| 407 | TRACE_RESUME(0); |
| 408 | |
Rafael J. Wysocki | 33c3374 | 2009-12-13 20:31:12 +0100 | [diff] [blame] | 409 | if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 410 | pm_dev_dbg(dev, state, "EARLY "); |
| 411 | error = pm_noirq_op(dev, dev->bus->pm, state); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 412 | } |
Rafael J. Wysocki | 33c3374 | 2009-12-13 20:31:12 +0100 | [diff] [blame] | 413 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 414 | TRACE_RESUME(error); |
| 415 | return error; |
| 416 | } |
| 417 | |
| 418 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 419 | * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices. |
| 420 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 421 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 422 | * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and |
| 423 | * enable device drivers to receive interrupts. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 424 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 425 | void dpm_resume_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 426 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 427 | struct device *dev; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 428 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 429 | |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 430 | mutex_lock(&dpm_list_mtx); |
Xiaotian Feng | 3eb132c | 2009-08-20 20:28:33 +0200 | [diff] [blame] | 431 | transition_started = false; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 432 | list_for_each_entry(dev, &dpm_list, power.entry) |
| 433 | if (dev->power.status > DPM_OFF) { |
| 434 | int error; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 435 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 436 | dev->power.status = DPM_OFF; |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 437 | error = device_resume_noirq(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 438 | if (error) |
| 439 | pm_dev_err(dev, state, " early", error); |
| 440 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 441 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 442 | dpm_show_time(starttime, state, "early"); |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 443 | resume_device_irqs(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 444 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 445 | EXPORT_SYMBOL_GPL(dpm_resume_noirq); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 446 | |
| 447 | /** |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 448 | * legacy_resume - Execute a legacy (bus or class) resume callback for device. |
| 449 | * dev: Device to resume. |
| 450 | * cb: Resume callback to execute. |
| 451 | */ |
| 452 | static int legacy_resume(struct device *dev, int (*cb)(struct device *dev)) |
| 453 | { |
| 454 | int error; |
| 455 | ktime_t calltime; |
| 456 | |
| 457 | calltime = initcall_debug_start(dev); |
| 458 | |
| 459 | error = cb(dev); |
| 460 | suspend_report_result(cb, error); |
| 461 | |
| 462 | initcall_debug_report(dev, calltime, error); |
| 463 | |
| 464 | return error; |
| 465 | } |
| 466 | |
| 467 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 468 | * device_resume - Execute "resume" callbacks for given device. |
| 469 | * @dev: Device to handle. |
| 470 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 471 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 472 | static int device_resume(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 473 | { |
| 474 | int error = 0; |
| 475 | |
| 476 | TRACE_DEVICE(dev); |
| 477 | TRACE_RESUME(0); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 478 | |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 479 | down(&dev->sem); |
| 480 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 481 | if (dev->bus) { |
| 482 | if (dev->bus->pm) { |
| 483 | pm_dev_dbg(dev, state, ""); |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 484 | error = pm_op(dev, dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 485 | } else if (dev->bus->resume) { |
| 486 | pm_dev_dbg(dev, state, "legacy "); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 487 | error = legacy_resume(dev, dev->bus->resume); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 488 | } |
| 489 | if (error) |
| 490 | goto End; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 491 | } |
| 492 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 493 | if (dev->type) { |
| 494 | if (dev->type->pm) { |
| 495 | pm_dev_dbg(dev, state, "type "); |
| 496 | error = pm_op(dev, dev->type->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 497 | } |
| 498 | if (error) |
| 499 | goto End; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 502 | if (dev->class) { |
| 503 | if (dev->class->pm) { |
| 504 | pm_dev_dbg(dev, state, "class "); |
| 505 | error = pm_op(dev, dev->class->pm, state); |
| 506 | } else if (dev->class->resume) { |
| 507 | pm_dev_dbg(dev, state, "legacy class "); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 508 | error = legacy_resume(dev, dev->class->resume); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 509 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 510 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 511 | End: |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 512 | up(&dev->sem); |
| 513 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 514 | TRACE_RESUME(error); |
| 515 | return error; |
| 516 | } |
| 517 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 518 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 519 | * dpm_resume - Execute "resume" callbacks for non-sysdev devices. |
| 520 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 521 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 522 | * Execute the appropriate "resume" callback for all devices whose status |
| 523 | * indicates that they are suspended. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 524 | */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 525 | static void dpm_resume(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 526 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 527 | struct list_head list; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 528 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 529 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 530 | INIT_LIST_HEAD(&list); |
| 531 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 532 | while (!list_empty(&dpm_list)) { |
| 533 | struct device *dev = to_device(dpm_list.next); |
| 534 | |
| 535 | get_device(dev); |
| 536 | if (dev->power.status >= DPM_OFF) { |
| 537 | int error; |
| 538 | |
| 539 | dev->power.status = DPM_RESUMING; |
| 540 | mutex_unlock(&dpm_list_mtx); |
| 541 | |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 542 | error = device_resume(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 543 | |
| 544 | mutex_lock(&dpm_list_mtx); |
| 545 | if (error) |
| 546 | pm_dev_err(dev, state, "", error); |
| 547 | } else if (dev->power.status == DPM_SUSPENDING) { |
| 548 | /* Allow new children of the device to be registered */ |
| 549 | dev->power.status = DPM_RESUMING; |
| 550 | } |
| 551 | if (!list_empty(&dev->power.entry)) |
| 552 | list_move_tail(&dev->power.entry, &list); |
| 553 | put_device(dev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 554 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 555 | list_splice(&list, &dpm_list); |
| 556 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 557 | dpm_show_time(starttime, state, NULL); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 561 | * device_complete - Complete a PM transition for given device. |
| 562 | * @dev: Device to handle. |
| 563 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 564 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 565 | static void device_complete(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 566 | { |
| 567 | down(&dev->sem); |
| 568 | |
| 569 | if (dev->class && dev->class->pm && dev->class->pm->complete) { |
| 570 | pm_dev_dbg(dev, state, "completing class "); |
| 571 | dev->class->pm->complete(dev); |
| 572 | } |
| 573 | |
| 574 | if (dev->type && dev->type->pm && dev->type->pm->complete) { |
| 575 | pm_dev_dbg(dev, state, "completing type "); |
| 576 | dev->type->pm->complete(dev); |
| 577 | } |
| 578 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 579 | if (dev->bus && dev->bus->pm && dev->bus->pm->complete) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 580 | pm_dev_dbg(dev, state, "completing "); |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 581 | dev->bus->pm->complete(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | up(&dev->sem); |
| 585 | } |
| 586 | |
| 587 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 588 | * dpm_complete - Complete a PM transition for all non-sysdev devices. |
| 589 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 590 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 591 | * Execute the ->complete() callbacks for all devices whose PM status is not |
| 592 | * DPM_ON (this allows new devices to be registered). |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 593 | */ |
| 594 | static void dpm_complete(pm_message_t state) |
| 595 | { |
| 596 | struct list_head list; |
| 597 | |
| 598 | INIT_LIST_HEAD(&list); |
| 599 | mutex_lock(&dpm_list_mtx); |
Romit Dasgupta | e528e87 | 2009-10-28 22:56:02 +0100 | [diff] [blame] | 600 | transition_started = false; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 601 | while (!list_empty(&dpm_list)) { |
| 602 | struct device *dev = to_device(dpm_list.prev); |
| 603 | |
| 604 | get_device(dev); |
| 605 | if (dev->power.status > DPM_ON) { |
| 606 | dev->power.status = DPM_ON; |
| 607 | mutex_unlock(&dpm_list_mtx); |
| 608 | |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 609 | device_complete(dev, state); |
Alan Stern | aa0baae | 2009-12-21 02:46:11 +0100 | [diff] [blame] | 610 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 611 | |
| 612 | mutex_lock(&dpm_list_mtx); |
| 613 | } |
| 614 | if (!list_empty(&dev->power.entry)) |
| 615 | list_move(&dev->power.entry, &list); |
| 616 | put_device(dev); |
| 617 | } |
| 618 | list_splice(&list, &dpm_list); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 619 | mutex_unlock(&dpm_list_mtx); |
| 620 | } |
| 621 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 622 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 623 | * dpm_resume_end - Execute "resume" callbacks and complete system transition. |
| 624 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 625 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 626 | * Execute "resume" callbacks for all devices and complete the PM transition of |
| 627 | * the system. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 628 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 629 | void dpm_resume_end(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 630 | { |
| 631 | might_sleep(); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 632 | dpm_resume(state); |
| 633 | dpm_complete(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 634 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 635 | EXPORT_SYMBOL_GPL(dpm_resume_end); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 636 | |
| 637 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 638 | /*------------------------- Suspend routines -------------------------*/ |
| 639 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 640 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 641 | * resume_event - Return a "resume" message for given "suspend" sleep state. |
| 642 | * @sleep_state: PM message representing a sleep state. |
| 643 | * |
| 644 | * Return a PM message representing the resume event corresponding to given |
| 645 | * sleep state. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 646 | */ |
| 647 | static pm_message_t resume_event(pm_message_t sleep_state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 648 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 649 | switch (sleep_state.event) { |
| 650 | case PM_EVENT_SUSPEND: |
| 651 | return PMSG_RESUME; |
| 652 | case PM_EVENT_FREEZE: |
| 653 | case PM_EVENT_QUIESCE: |
| 654 | return PMSG_RECOVER; |
| 655 | case PM_EVENT_HIBERNATE: |
| 656 | return PMSG_RESTORE; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 657 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 658 | return PMSG_ON; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 662 | * device_suspend_noirq - Execute a "late suspend" callback for given device. |
| 663 | * @dev: Device to handle. |
| 664 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 665 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 666 | * The driver of @dev will not receive interrupts while this function is being |
| 667 | * executed. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 668 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 669 | 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] | 670 | { |
| 671 | int error = 0; |
| 672 | |
Rafael J. Wysocki | 33c3374 | 2009-12-13 20:31:12 +0100 | [diff] [blame] | 673 | if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 674 | pm_dev_dbg(dev, state, "LATE "); |
| 675 | error = pm_noirq_op(dev, dev->bus->pm, state); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 676 | } |
| 677 | return error; |
| 678 | } |
| 679 | |
| 680 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 681 | * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices. |
| 682 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 683 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 684 | * Prevent device drivers from receiving interrupts and call the "noirq" suspend |
| 685 | * handlers for all non-sysdev devices. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 686 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 687 | int dpm_suspend_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 688 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 689 | struct device *dev; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 690 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 691 | int error = 0; |
| 692 | |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 693 | suspend_device_irqs(); |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 694 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 695 | list_for_each_entry_reverse(dev, &dpm_list, power.entry) { |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 696 | error = device_suspend_noirq(dev, state); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 697 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 698 | pm_dev_err(dev, state, " late", error); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 699 | break; |
| 700 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 701 | dev->power.status = DPM_OFF_IRQ; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 702 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 703 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 704 | if (error) |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 705 | dpm_resume_noirq(resume_event(state)); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 706 | else |
| 707 | dpm_show_time(starttime, state, "late"); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 708 | return error; |
| 709 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 710 | EXPORT_SYMBOL_GPL(dpm_suspend_noirq); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 711 | |
| 712 | /** |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 713 | * legacy_suspend - Execute a legacy (bus or class) suspend callback for device. |
| 714 | * dev: Device to suspend. |
| 715 | * cb: Suspend callback to execute. |
| 716 | */ |
| 717 | static int legacy_suspend(struct device *dev, pm_message_t state, |
| 718 | int (*cb)(struct device *dev, pm_message_t state)) |
| 719 | { |
| 720 | int error; |
| 721 | ktime_t calltime; |
| 722 | |
| 723 | calltime = initcall_debug_start(dev); |
| 724 | |
| 725 | error = cb(dev, state); |
| 726 | suspend_report_result(cb, error); |
| 727 | |
| 728 | initcall_debug_report(dev, calltime, error); |
| 729 | |
| 730 | return error; |
| 731 | } |
| 732 | |
| 733 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 734 | * device_suspend - Execute "suspend" callbacks for given device. |
| 735 | * @dev: Device to handle. |
| 736 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 737 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 738 | static int device_suspend(struct device *dev, pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 739 | { |
| 740 | int error = 0; |
| 741 | |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 742 | down(&dev->sem); |
| 743 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 744 | if (dev->class) { |
| 745 | if (dev->class->pm) { |
| 746 | pm_dev_dbg(dev, state, "class "); |
| 747 | error = pm_op(dev, dev->class->pm, state); |
| 748 | } else if (dev->class->suspend) { |
| 749 | pm_dev_dbg(dev, state, "legacy class "); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 750 | error = legacy_suspend(dev, state, dev->class->suspend); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 751 | } |
| 752 | if (error) |
| 753 | goto End; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 754 | } |
| 755 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 756 | if (dev->type) { |
| 757 | if (dev->type->pm) { |
| 758 | pm_dev_dbg(dev, state, "type "); |
| 759 | error = pm_op(dev, dev->type->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 760 | } |
| 761 | if (error) |
| 762 | goto End; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 763 | } |
| 764 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 765 | if (dev->bus) { |
| 766 | if (dev->bus->pm) { |
| 767 | pm_dev_dbg(dev, state, ""); |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 768 | error = pm_op(dev, dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 769 | } else if (dev->bus->suspend) { |
| 770 | pm_dev_dbg(dev, state, "legacy "); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 771 | error = legacy_suspend(dev, state, dev->bus->suspend); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 772 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 773 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 774 | End: |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 775 | up(&dev->sem); |
| 776 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 777 | return error; |
| 778 | } |
| 779 | |
| 780 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 781 | * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices. |
| 782 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 783 | */ |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 784 | static int dpm_suspend(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 785 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 786 | struct list_head list; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 787 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 788 | int error = 0; |
| 789 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 790 | INIT_LIST_HEAD(&list); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 791 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 792 | while (!list_empty(&dpm_list)) { |
| 793 | struct device *dev = to_device(dpm_list.prev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 794 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 795 | get_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 796 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 797 | |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 798 | error = device_suspend(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 799 | |
Alan Stern | 1b3cbec | 2008-02-29 11:50:22 -0500 | [diff] [blame] | 800 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 801 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 802 | pm_dev_err(dev, state, "", error); |
| 803 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 804 | break; |
| 805 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 806 | dev->power.status = DPM_OFF; |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 807 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 808 | list_move(&dev->power.entry, &list); |
| 809 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 810 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 811 | list_splice(&list, dpm_list.prev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 812 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 813 | if (!error) |
| 814 | dpm_show_time(starttime, state, NULL); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 815 | return error; |
| 816 | } |
| 817 | |
| 818 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 819 | * device_prepare - Prepare a device for system power transition. |
| 820 | * @dev: Device to handle. |
| 821 | * @state: PM transition of the system being carried out. |
| 822 | * |
| 823 | * Execute the ->prepare() callback(s) for given device. No new children of the |
| 824 | * device may be registered after this function has returned. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 825 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 826 | static int device_prepare(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 827 | { |
| 828 | int error = 0; |
| 829 | |
| 830 | down(&dev->sem); |
| 831 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 832 | if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 833 | pm_dev_dbg(dev, state, "preparing "); |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 834 | error = dev->bus->pm->prepare(dev); |
| 835 | suspend_report_result(dev->bus->pm->prepare, error); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 836 | if (error) |
| 837 | goto End; |
| 838 | } |
| 839 | |
| 840 | if (dev->type && dev->type->pm && dev->type->pm->prepare) { |
| 841 | pm_dev_dbg(dev, state, "preparing type "); |
| 842 | error = dev->type->pm->prepare(dev); |
| 843 | suspend_report_result(dev->type->pm->prepare, error); |
| 844 | if (error) |
| 845 | goto End; |
| 846 | } |
| 847 | |
| 848 | if (dev->class && dev->class->pm && dev->class->pm->prepare) { |
| 849 | pm_dev_dbg(dev, state, "preparing class "); |
| 850 | error = dev->class->pm->prepare(dev); |
| 851 | suspend_report_result(dev->class->pm->prepare, error); |
| 852 | } |
| 853 | End: |
| 854 | up(&dev->sem); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 855 | |
| 856 | return error; |
| 857 | } |
| 858 | |
| 859 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 860 | * dpm_prepare - Prepare all non-sysdev devices for a system PM transition. |
| 861 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 862 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 863 | * Execute the ->prepare() callback(s) for all devices. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 864 | */ |
| 865 | static int dpm_prepare(pm_message_t state) |
| 866 | { |
| 867 | struct list_head list; |
| 868 | int error = 0; |
| 869 | |
| 870 | INIT_LIST_HEAD(&list); |
| 871 | mutex_lock(&dpm_list_mtx); |
| 872 | transition_started = true; |
| 873 | while (!list_empty(&dpm_list)) { |
| 874 | struct device *dev = to_device(dpm_list.next); |
| 875 | |
| 876 | get_device(dev); |
| 877 | dev->power.status = DPM_PREPARING; |
| 878 | mutex_unlock(&dpm_list_mtx); |
| 879 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 880 | pm_runtime_get_noresume(dev); |
| 881 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) { |
| 882 | /* Wake-up requested during system sleep transition. */ |
Alan Stern | aa0baae | 2009-12-21 02:46:11 +0100 | [diff] [blame] | 883 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 884 | error = -EBUSY; |
| 885 | } else { |
| 886 | error = device_prepare(dev, state); |
| 887 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 888 | |
| 889 | mutex_lock(&dpm_list_mtx); |
| 890 | if (error) { |
| 891 | dev->power.status = DPM_ON; |
| 892 | if (error == -EAGAIN) { |
| 893 | put_device(dev); |
Sebastian Ott | 886a7a3 | 2009-07-08 13:26:05 +0200 | [diff] [blame] | 894 | error = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 895 | continue; |
| 896 | } |
| 897 | printk(KERN_ERR "PM: Failed to prepare device %s " |
| 898 | "for power transition: error %d\n", |
| 899 | kobject_name(&dev->kobj), error); |
| 900 | put_device(dev); |
| 901 | break; |
| 902 | } |
| 903 | dev->power.status = DPM_SUSPENDING; |
| 904 | if (!list_empty(&dev->power.entry)) |
| 905 | list_move_tail(&dev->power.entry, &list); |
| 906 | put_device(dev); |
| 907 | } |
| 908 | list_splice(&list, &dpm_list); |
| 909 | mutex_unlock(&dpm_list_mtx); |
| 910 | return error; |
| 911 | } |
| 912 | |
| 913 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 914 | * dpm_suspend_start - Prepare devices for PM transition and suspend them. |
| 915 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 916 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 917 | * Prepare all non-sysdev devices for system PM transition and execute "suspend" |
| 918 | * callbacks for them. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 919 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 920 | int dpm_suspend_start(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 921 | { |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 922 | int error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 923 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 924 | might_sleep(); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 925 | error = dpm_prepare(state); |
| 926 | if (!error) |
| 927 | error = dpm_suspend(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 928 | return error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 929 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 930 | EXPORT_SYMBOL_GPL(dpm_suspend_start); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 931 | |
| 932 | void __suspend_report_result(const char *function, void *fn, int ret) |
| 933 | { |
Bjorn Helgaas | c80cfb0 | 2008-10-15 22:01:35 -0700 | [diff] [blame] | 934 | if (ret) |
| 935 | printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 936 | } |
| 937 | EXPORT_SYMBOL_GPL(__suspend_report_result); |