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. |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 11 | * This will initialize the embedded device_pm_info object in the device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Paul Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame] | 22 | #include <linux/export.h> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 24 | #include <linux/pm.h> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 26 | #include <linux/resume-trace.h> |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 28 | #include <linux/sched.h> |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 29 | #include <linux/async.h> |
Rafael J. Wysocki | 1e75227 | 2010-12-03 22:58:05 +0100 | [diff] [blame] | 30 | #include <linux/suspend.h> |
Benoit Goby | 9109a22 | 2009-08-13 09:40:42 -0700 | [diff] [blame^] | 31 | #include <linux/timer.h> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 32 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 33 | #include "../base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "power.h" |
| 35 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 36 | typedef int (*pm_callback_t)(struct device *); |
| 37 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 38 | /* |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 39 | * 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] | 40 | * because children are guaranteed to be discovered after parents, and |
| 41 | * are inserted at the back of the list on discovery. |
| 42 | * |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 43 | * Since device_pm_add() may be called with a device lock held, |
| 44 | * we must never try to acquire a device lock while holding |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 45 | * dpm_list_mutex. |
| 46 | */ |
| 47 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 48 | LIST_HEAD(dpm_list); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 49 | LIST_HEAD(dpm_prepared_list); |
| 50 | LIST_HEAD(dpm_suspended_list); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 51 | LIST_HEAD(dpm_late_early_list); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 52 | LIST_HEAD(dpm_noirq_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 54 | struct suspend_stats suspend_stats; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 55 | static DEFINE_MUTEX(dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 56 | static pm_message_t pm_transition; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Benoit Goby | 9109a22 | 2009-08-13 09:40:42 -0700 | [diff] [blame^] | 58 | static void dpm_drv_timeout(unsigned long data); |
| 59 | struct dpm_drv_wd_data { |
| 60 | struct device *dev; |
| 61 | struct task_struct *tsk; |
| 62 | }; |
| 63 | |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 64 | static int async_error; |
| 65 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 66 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 67 | * 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] | 68 | * @dev: Device object being initialized. |
| 69 | */ |
| 70 | void device_pm_init(struct device *dev) |
| 71 | { |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 72 | dev->power.is_prepared = false; |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 73 | dev->power.is_suspended = false; |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 74 | init_completion(&dev->power.completion); |
Colin Cross | 152e1d5 | 2010-09-03 01:24:07 +0200 | [diff] [blame] | 75 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 76 | dev->power.wakeup = NULL; |
| 77 | spin_lock_init(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 78 | pm_runtime_init(dev); |
Rafael J. Wysocki | 22110fa | 2011-04-26 11:33:09 +0200 | [diff] [blame] | 79 | INIT_LIST_HEAD(&dev->power.entry); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 80 | dev->power.power_state = PMSG_INVALID; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 84 | * 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] | 85 | */ |
| 86 | void device_pm_lock(void) |
| 87 | { |
| 88 | mutex_lock(&dpm_list_mtx); |
| 89 | } |
| 90 | |
| 91 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 92 | * 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] | 93 | */ |
| 94 | void device_pm_unlock(void) |
| 95 | { |
| 96 | mutex_unlock(&dpm_list_mtx); |
| 97 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 98 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 99 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 100 | * device_pm_add - Add a device to the PM core's list of active devices. |
| 101 | * @dev: Device to add to the list. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 102 | */ |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 103 | void device_pm_add(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | pr_debug("PM: Adding info for %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 106 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 107 | mutex_lock(&dpm_list_mtx); |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 108 | if (dev->parent && dev->parent->power.is_prepared) |
Rafael J. Wysocki | b64959e | 2010-12-16 17:11:45 +0100 | [diff] [blame] | 109 | dev_warn(dev, "parent %s should not be sleeping\n", |
| 110 | dev_name(dev->parent)); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 111 | list_add_tail(&dev->power.entry, &dpm_list); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 112 | dev_pm_qos_constraints_init(dev); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 113 | mutex_unlock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 116 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 117 | * device_pm_remove - Remove a device from the PM core's list of active devices. |
| 118 | * @dev: Device to be removed from the list. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 119 | */ |
Rafael J. Wysocki | 9cddad7 | 2007-06-13 15:53:34 +0200 | [diff] [blame] | 120 | void device_pm_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | pr_debug("PM: Removing info for %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 123 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 124 | complete_all(&dev->power.completion); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 125 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 126 | dev_pm_qos_constraints_destroy(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | list_del_init(&dev->power.entry); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 128 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 129 | device_wakeup_disable(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 130 | pm_runtime_remove(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 133 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 134 | * device_pm_move_before - Move device in the PM core's list of active devices. |
| 135 | * @deva: Device to move in dpm_list. |
| 136 | * @devb: Device @deva should come before. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 137 | */ |
| 138 | void device_pm_move_before(struct device *deva, struct device *devb) |
| 139 | { |
| 140 | pr_debug("PM: Moving %s:%s before %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 141 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), |
| 142 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 143 | /* Delete deva from dpm_list and reinsert before devb. */ |
| 144 | list_move_tail(&deva->power.entry, &devb->power.entry); |
| 145 | } |
| 146 | |
| 147 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 148 | * device_pm_move_after - Move device in the PM core's list of active devices. |
| 149 | * @deva: Device to move in dpm_list. |
| 150 | * @devb: Device @deva should come after. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 151 | */ |
| 152 | void device_pm_move_after(struct device *deva, struct device *devb) |
| 153 | { |
| 154 | pr_debug("PM: Moving %s:%s after %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 155 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), |
| 156 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 157 | /* Delete deva from dpm_list and reinsert after devb. */ |
| 158 | list_move(&deva->power.entry, &devb->power.entry); |
| 159 | } |
| 160 | |
| 161 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 162 | * device_pm_move_last - Move device to end of the PM core's list of devices. |
| 163 | * @dev: Device to move in dpm_list. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 164 | */ |
| 165 | void device_pm_move_last(struct device *dev) |
| 166 | { |
| 167 | pr_debug("PM: Moving %s:%s to end of list\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 168 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 169 | list_move_tail(&dev->power.entry, &dpm_list); |
| 170 | } |
| 171 | |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 172 | static ktime_t initcall_debug_start(struct device *dev) |
| 173 | { |
| 174 | ktime_t calltime = ktime_set(0, 0); |
| 175 | |
| 176 | if (initcall_debug) { |
Rafael J. Wysocki | 0c6aebe | 2011-12-03 00:23:43 +0100 | [diff] [blame] | 177 | pr_info("calling %s+ @ %i, parent: %s\n", |
| 178 | dev_name(dev), task_pid_nr(current), |
| 179 | dev->parent ? dev_name(dev->parent) : "none"); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 180 | calltime = ktime_get(); |
| 181 | } |
| 182 | |
| 183 | return calltime; |
| 184 | } |
| 185 | |
| 186 | static void initcall_debug_report(struct device *dev, ktime_t calltime, |
| 187 | int error) |
| 188 | { |
| 189 | ktime_t delta, rettime; |
| 190 | |
| 191 | if (initcall_debug) { |
| 192 | rettime = ktime_get(); |
| 193 | delta = ktime_sub(rettime, calltime); |
| 194 | pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev), |
| 195 | error, (unsigned long long)ktime_to_ns(delta) >> 10); |
| 196 | } |
| 197 | } |
| 198 | |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 199 | /** |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 200 | * dpm_wait - Wait for a PM operation to complete. |
| 201 | * @dev: Device to wait for. |
| 202 | * @async: If unset, wait only if the device's power.async_suspend flag is set. |
| 203 | */ |
| 204 | static void dpm_wait(struct device *dev, bool async) |
| 205 | { |
| 206 | if (!dev) |
| 207 | return; |
| 208 | |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 209 | if (async || (pm_async_enabled && dev->power.async_suspend)) |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 210 | wait_for_completion(&dev->power.completion); |
| 211 | } |
| 212 | |
| 213 | static int dpm_wait_fn(struct device *dev, void *async_ptr) |
| 214 | { |
| 215 | dpm_wait(dev, *((bool *)async_ptr)); |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static void dpm_wait_for_children(struct device *dev, bool async) |
| 220 | { |
| 221 | device_for_each_child(dev, &async, dpm_wait_fn); |
| 222 | } |
| 223 | |
| 224 | /** |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 225 | * pm_op - Return the PM operation appropriate for given PM event. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 226 | * @ops: PM operations to choose from. |
| 227 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 228 | */ |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 229 | static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 230 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 231 | switch (state.event) { |
| 232 | #ifdef CONFIG_SUSPEND |
| 233 | case PM_EVENT_SUSPEND: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 234 | return ops->suspend; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 235 | case PM_EVENT_RESUME: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 236 | return ops->resume; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 237 | #endif /* CONFIG_SUSPEND */ |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 238 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 239 | case PM_EVENT_FREEZE: |
| 240 | case PM_EVENT_QUIESCE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 241 | return ops->freeze; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 242 | case PM_EVENT_HIBERNATE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 243 | return ops->poweroff; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 244 | case PM_EVENT_THAW: |
| 245 | case PM_EVENT_RECOVER: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 246 | return ops->thaw; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 247 | break; |
| 248 | case PM_EVENT_RESTORE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 249 | return ops->restore; |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 250 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 251 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 252 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 253 | return NULL; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 257 | * pm_late_early_op - Return the PM operation appropriate for given PM event. |
| 258 | * @ops: PM operations to choose from. |
| 259 | * @state: PM transition of the system being carried out. |
| 260 | * |
| 261 | * Runtime PM is disabled for @dev while this function is being executed. |
| 262 | */ |
| 263 | static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops, |
| 264 | pm_message_t state) |
| 265 | { |
| 266 | switch (state.event) { |
| 267 | #ifdef CONFIG_SUSPEND |
| 268 | case PM_EVENT_SUSPEND: |
| 269 | return ops->suspend_late; |
| 270 | case PM_EVENT_RESUME: |
| 271 | return ops->resume_early; |
| 272 | #endif /* CONFIG_SUSPEND */ |
| 273 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
| 274 | case PM_EVENT_FREEZE: |
| 275 | case PM_EVENT_QUIESCE: |
| 276 | return ops->freeze_late; |
| 277 | case PM_EVENT_HIBERNATE: |
| 278 | return ops->poweroff_late; |
| 279 | case PM_EVENT_THAW: |
| 280 | case PM_EVENT_RECOVER: |
| 281 | return ops->thaw_early; |
| 282 | case PM_EVENT_RESTORE: |
| 283 | return ops->restore_early; |
| 284 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
| 285 | } |
| 286 | |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | /** |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 291 | * pm_noirq_op - Return the PM operation appropriate for given PM event. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 292 | * @ops: PM operations to choose from. |
| 293 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 294 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 295 | * The driver of @dev will not receive interrupts while this function is being |
| 296 | * executed. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 297 | */ |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 298 | static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 299 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 300 | switch (state.event) { |
| 301 | #ifdef CONFIG_SUSPEND |
| 302 | case PM_EVENT_SUSPEND: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 303 | return ops->suspend_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 304 | case PM_EVENT_RESUME: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 305 | return ops->resume_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 306 | #endif /* CONFIG_SUSPEND */ |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 307 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 308 | case PM_EVENT_FREEZE: |
| 309 | case PM_EVENT_QUIESCE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 310 | return ops->freeze_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 311 | case PM_EVENT_HIBERNATE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 312 | return ops->poweroff_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 313 | case PM_EVENT_THAW: |
| 314 | case PM_EVENT_RECOVER: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 315 | return ops->thaw_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 316 | case PM_EVENT_RESTORE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 317 | return ops->restore_noirq; |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 318 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 319 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 320 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 321 | return NULL; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static char *pm_verb(int event) |
| 325 | { |
| 326 | switch (event) { |
| 327 | case PM_EVENT_SUSPEND: |
| 328 | return "suspend"; |
| 329 | case PM_EVENT_RESUME: |
| 330 | return "resume"; |
| 331 | case PM_EVENT_FREEZE: |
| 332 | return "freeze"; |
| 333 | case PM_EVENT_QUIESCE: |
| 334 | return "quiesce"; |
| 335 | case PM_EVENT_HIBERNATE: |
| 336 | return "hibernate"; |
| 337 | case PM_EVENT_THAW: |
| 338 | return "thaw"; |
| 339 | case PM_EVENT_RESTORE: |
| 340 | return "restore"; |
| 341 | case PM_EVENT_RECOVER: |
| 342 | return "recover"; |
| 343 | default: |
| 344 | return "(unknown PM event)"; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info) |
| 349 | { |
| 350 | dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event), |
| 351 | ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ? |
| 352 | ", may wakeup" : ""); |
| 353 | } |
| 354 | |
| 355 | static void pm_dev_err(struct device *dev, pm_message_t state, char *info, |
| 356 | int error) |
| 357 | { |
| 358 | printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 359 | dev_name(dev), pm_verb(state.event), info, error); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 360 | } |
| 361 | |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 362 | static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info) |
| 363 | { |
| 364 | ktime_t calltime; |
Kevin Cernekee | 0702d9e | 2010-09-20 22:32:10 +0200 | [diff] [blame] | 365 | u64 usecs64; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 366 | int usecs; |
| 367 | |
| 368 | calltime = ktime_get(); |
| 369 | usecs64 = ktime_to_ns(ktime_sub(calltime, starttime)); |
| 370 | do_div(usecs64, NSEC_PER_USEC); |
| 371 | usecs = usecs64; |
| 372 | if (usecs == 0) |
| 373 | usecs = 1; |
| 374 | pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n", |
| 375 | info ?: "", info ? " " : "", pm_verb(state.event), |
| 376 | usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC); |
| 377 | } |
| 378 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 379 | static int dpm_run_callback(pm_callback_t cb, struct device *dev, |
| 380 | pm_message_t state, char *info) |
| 381 | { |
| 382 | ktime_t calltime; |
| 383 | int error; |
| 384 | |
| 385 | if (!cb) |
| 386 | return 0; |
| 387 | |
| 388 | calltime = initcall_debug_start(dev); |
| 389 | |
| 390 | pm_dev_dbg(dev, state, info); |
| 391 | error = cb(dev); |
| 392 | suspend_report_result(cb, error); |
| 393 | |
| 394 | initcall_debug_report(dev, calltime, error); |
| 395 | |
| 396 | return error; |
| 397 | } |
| 398 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 399 | /*------------------------- Resume routines -------------------------*/ |
| 400 | |
| 401 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 402 | * device_resume_noirq - Execute an "early resume" callback for given device. |
| 403 | * @dev: Device to handle. |
| 404 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 405 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 406 | * The driver of @dev will not receive interrupts while this function is being |
| 407 | * executed. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 408 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 409 | static int device_resume_noirq(struct device *dev, pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 410 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 411 | pm_callback_t callback = NULL; |
| 412 | char *info = NULL; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 413 | int error = 0; |
| 414 | |
| 415 | TRACE_DEVICE(dev); |
| 416 | TRACE_RESUME(0); |
| 417 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 418 | if (dev->pm_domain) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 419 | info = "noirq power domain "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 420 | callback = pm_noirq_op(&dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 421 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 422 | info = "noirq type "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 423 | callback = pm_noirq_op(dev->type->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 424 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 425 | info = "noirq class "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 426 | callback = pm_noirq_op(dev->class->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 427 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 428 | info = "noirq bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 429 | callback = pm_noirq_op(dev->bus->pm, state); |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 432 | if (!callback && dev->driver && dev->driver->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 433 | info = "noirq driver "; |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 434 | callback = pm_noirq_op(dev->driver->pm, state); |
| 435 | } |
| 436 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 437 | error = dpm_run_callback(callback, dev, state, info); |
| 438 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 439 | TRACE_RESUME(error); |
| 440 | return error; |
| 441 | } |
| 442 | |
| 443 | /** |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 444 | * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 445 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 446 | * |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 447 | * Call the "noirq" resume handlers for all devices in dpm_noirq_list and |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 448 | * enable device drivers to receive interrupts. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 449 | */ |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 450 | static void dpm_resume_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 451 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 452 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 453 | |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 454 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 455 | while (!list_empty(&dpm_noirq_list)) { |
| 456 | struct device *dev = to_device(dpm_noirq_list.next); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 457 | int error; |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 458 | |
| 459 | get_device(dev); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 460 | list_move_tail(&dev->power.entry, &dpm_late_early_list); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 461 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 462 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 463 | error = device_resume_noirq(dev, state); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 464 | if (error) { |
| 465 | suspend_stats.failed_resume_noirq++; |
| 466 | dpm_save_failed_step(SUSPEND_RESUME_NOIRQ); |
| 467 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 468 | pm_dev_err(dev, state, " noirq", error); |
| 469 | } |
| 470 | |
| 471 | mutex_lock(&dpm_list_mtx); |
| 472 | put_device(dev); |
| 473 | } |
| 474 | mutex_unlock(&dpm_list_mtx); |
| 475 | dpm_show_time(starttime, state, "noirq"); |
| 476 | resume_device_irqs(); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * device_resume_early - Execute an "early resume" callback for given device. |
| 481 | * @dev: Device to handle. |
| 482 | * @state: PM transition of the system being carried out. |
| 483 | * |
| 484 | * Runtime PM is disabled for @dev while this function is being executed. |
| 485 | */ |
| 486 | static int device_resume_early(struct device *dev, pm_message_t state) |
| 487 | { |
| 488 | pm_callback_t callback = NULL; |
| 489 | char *info = NULL; |
| 490 | int error = 0; |
| 491 | |
| 492 | TRACE_DEVICE(dev); |
| 493 | TRACE_RESUME(0); |
| 494 | |
| 495 | if (dev->pm_domain) { |
| 496 | info = "early power domain "; |
| 497 | callback = pm_late_early_op(&dev->pm_domain->ops, state); |
| 498 | } else if (dev->type && dev->type->pm) { |
| 499 | info = "early type "; |
| 500 | callback = pm_late_early_op(dev->type->pm, state); |
| 501 | } else if (dev->class && dev->class->pm) { |
| 502 | info = "early class "; |
| 503 | callback = pm_late_early_op(dev->class->pm, state); |
| 504 | } else if (dev->bus && dev->bus->pm) { |
| 505 | info = "early bus "; |
| 506 | callback = pm_late_early_op(dev->bus->pm, state); |
| 507 | } |
| 508 | |
| 509 | if (!callback && dev->driver && dev->driver->pm) { |
| 510 | info = "early driver "; |
| 511 | callback = pm_late_early_op(dev->driver->pm, state); |
| 512 | } |
| 513 | |
| 514 | error = dpm_run_callback(callback, dev, state, info); |
| 515 | |
| 516 | TRACE_RESUME(error); |
| 517 | return error; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * dpm_resume_early - Execute "early resume" callbacks for all devices. |
| 522 | * @state: PM transition of the system being carried out. |
| 523 | */ |
| 524 | static void dpm_resume_early(pm_message_t state) |
| 525 | { |
| 526 | ktime_t starttime = ktime_get(); |
| 527 | |
| 528 | mutex_lock(&dpm_list_mtx); |
| 529 | while (!list_empty(&dpm_late_early_list)) { |
| 530 | struct device *dev = to_device(dpm_late_early_list.next); |
| 531 | int error; |
| 532 | |
| 533 | get_device(dev); |
| 534 | list_move_tail(&dev->power.entry, &dpm_suspended_list); |
| 535 | mutex_unlock(&dpm_list_mtx); |
| 536 | |
| 537 | error = device_resume_early(dev, state); |
| 538 | if (error) { |
| 539 | suspend_stats.failed_resume_early++; |
| 540 | dpm_save_failed_step(SUSPEND_RESUME_EARLY); |
| 541 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 542 | pm_dev_err(dev, state, " early", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 543 | } |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 544 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 545 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 546 | put_device(dev); |
| 547 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 548 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 549 | dpm_show_time(starttime, state, "early"); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 550 | } |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 551 | |
| 552 | /** |
| 553 | * dpm_resume_start - Execute "noirq" and "early" device callbacks. |
| 554 | * @state: PM transition of the system being carried out. |
| 555 | */ |
| 556 | void dpm_resume_start(pm_message_t state) |
| 557 | { |
| 558 | dpm_resume_noirq(state); |
| 559 | dpm_resume_early(state); |
| 560 | } |
| 561 | EXPORT_SYMBOL_GPL(dpm_resume_start); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 562 | |
| 563 | /** |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 564 | * device_resume - Execute "resume" callbacks for given device. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 565 | * @dev: Device to handle. |
| 566 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 567 | * @async: If true, the device is being resumed asynchronously. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 568 | */ |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 569 | static int device_resume(struct device *dev, pm_message_t state, bool async) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 570 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 571 | pm_callback_t callback = NULL; |
| 572 | char *info = NULL; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 573 | int error = 0; |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 574 | bool put = false; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 575 | |
| 576 | TRACE_DEVICE(dev); |
| 577 | TRACE_RESUME(0); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 578 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 579 | dpm_wait(dev->parent, async); |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 580 | device_lock(dev); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 581 | |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 582 | /* |
| 583 | * This is a fib. But we'll allow new children to be added below |
| 584 | * a resumed device, even if the device hasn't been completed yet. |
| 585 | */ |
| 586 | dev->power.is_prepared = false; |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 587 | |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 588 | if (!dev->power.is_suspended) |
| 589 | goto Unlock; |
| 590 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 591 | pm_runtime_enable(dev); |
| 592 | put = true; |
| 593 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 594 | if (dev->pm_domain) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 595 | info = "power domain "; |
| 596 | callback = pm_op(&dev->pm_domain->ops, state); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 597 | goto Driver; |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 598 | } |
| 599 | |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 600 | if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 601 | info = "type "; |
| 602 | callback = pm_op(dev->type->pm, state); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 603 | goto Driver; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | if (dev->class) { |
| 607 | if (dev->class->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 608 | info = "class "; |
| 609 | callback = pm_op(dev->class->pm, state); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 610 | goto Driver; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 611 | } else if (dev->class->resume) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 612 | info = "legacy class "; |
| 613 | callback = dev->class->resume; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 614 | goto End; |
| 615 | } |
| 616 | } |
| 617 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 618 | if (dev->bus) { |
| 619 | if (dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 620 | info = "bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 621 | callback = pm_op(dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 622 | } else if (dev->bus->resume) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 623 | info = "legacy bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 624 | callback = dev->bus->resume; |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 625 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 626 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 627 | } |
| 628 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 629 | Driver: |
| 630 | if (!callback && dev->driver && dev->driver->pm) { |
| 631 | info = "driver "; |
| 632 | callback = pm_op(dev->driver->pm, state); |
| 633 | } |
| 634 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 635 | End: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 636 | error = dpm_run_callback(callback, dev, state, info); |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 637 | dev->power.is_suspended = false; |
| 638 | |
| 639 | Unlock: |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 640 | device_unlock(dev); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 641 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 642 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 643 | TRACE_RESUME(error); |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 644 | |
| 645 | if (put) |
| 646 | pm_runtime_put_sync(dev); |
| 647 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 648 | return error; |
| 649 | } |
| 650 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 651 | static void async_resume(void *data, async_cookie_t cookie) |
| 652 | { |
| 653 | struct device *dev = (struct device *)data; |
| 654 | int error; |
| 655 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 656 | error = device_resume(dev, pm_transition, true); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 657 | if (error) |
| 658 | pm_dev_err(dev, pm_transition, " async", error); |
| 659 | put_device(dev); |
| 660 | } |
| 661 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 662 | static bool is_async(struct device *dev) |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 663 | { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 664 | return dev->power.async_suspend && pm_async_enabled |
| 665 | && !pm_trace_is_enabled(); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 668 | /** |
Benoit Goby | 9109a22 | 2009-08-13 09:40:42 -0700 | [diff] [blame^] | 669 | * dpm_drv_timeout - Driver suspend / resume watchdog handler |
| 670 | * @data: struct device which timed out |
| 671 | * |
| 672 | * Called when a driver has timed out suspending or resuming. |
| 673 | * There's not much we can do here to recover so |
| 674 | * BUG() out for a crash-dump |
| 675 | * |
| 676 | */ |
| 677 | static void dpm_drv_timeout(unsigned long data) |
| 678 | { |
| 679 | struct dpm_drv_wd_data *wd_data = (void *)data; |
| 680 | struct device *dev = wd_data->dev; |
| 681 | struct task_struct *tsk = wd_data->tsk; |
| 682 | |
| 683 | printk(KERN_EMERG "**** DPM device timeout: %s (%s)\n", dev_name(dev), |
| 684 | (dev->driver ? dev->driver->name : "no driver")); |
| 685 | |
| 686 | printk(KERN_EMERG "dpm suspend stack:\n"); |
| 687 | show_stack(tsk, NULL); |
| 688 | |
| 689 | BUG(); |
| 690 | } |
| 691 | |
| 692 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 693 | * dpm_resume - Execute "resume" callbacks for non-sysdev devices. |
| 694 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 695 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 696 | * Execute the appropriate "resume" callback for all devices whose status |
| 697 | * indicates that they are suspended. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 698 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 699 | void dpm_resume(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 700 | { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 701 | struct device *dev; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 702 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 703 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 704 | might_sleep(); |
| 705 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 706 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 707 | pm_transition = state; |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 708 | async_error = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 709 | |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 710 | list_for_each_entry(dev, &dpm_suspended_list, power.entry) { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 711 | INIT_COMPLETION(dev->power.completion); |
| 712 | if (is_async(dev)) { |
| 713 | get_device(dev); |
| 714 | async_schedule(async_resume, dev); |
| 715 | } |
| 716 | } |
| 717 | |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 718 | while (!list_empty(&dpm_suspended_list)) { |
| 719 | dev = to_device(dpm_suspended_list.next); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 720 | get_device(dev); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 721 | if (!is_async(dev)) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 722 | int error; |
| 723 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 724 | mutex_unlock(&dpm_list_mtx); |
| 725 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 726 | error = device_resume(dev, state, false); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 727 | if (error) { |
| 728 | suspend_stats.failed_resume++; |
| 729 | dpm_save_failed_step(SUSPEND_RESUME); |
| 730 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 731 | pm_dev_err(dev, state, "", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 732 | } |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 733 | |
| 734 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 735 | } |
| 736 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 737 | list_move_tail(&dev->power.entry, &dpm_prepared_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 738 | put_device(dev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 739 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 740 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 741 | async_synchronize_full(); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 742 | dpm_show_time(starttime, state, NULL); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 746 | * device_complete - Complete a PM transition for given device. |
| 747 | * @dev: Device to handle. |
| 748 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 749 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 750 | static void device_complete(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 751 | { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 752 | void (*callback)(struct device *) = NULL; |
| 753 | char *info = NULL; |
| 754 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 755 | device_lock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 756 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 757 | if (dev->pm_domain) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 758 | info = "completing power domain "; |
| 759 | callback = dev->pm_domain->ops.complete; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 760 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 761 | info = "completing type "; |
| 762 | callback = dev->type->pm->complete; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 763 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 764 | info = "completing class "; |
| 765 | callback = dev->class->pm->complete; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 766 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 767 | info = "completing bus "; |
| 768 | callback = dev->bus->pm->complete; |
| 769 | } |
| 770 | |
| 771 | if (!callback && dev->driver && dev->driver->pm) { |
| 772 | info = "completing driver "; |
| 773 | callback = dev->driver->pm->complete; |
| 774 | } |
| 775 | |
| 776 | if (callback) { |
| 777 | pm_dev_dbg(dev, state, info); |
| 778 | callback(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 779 | } |
| 780 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 781 | device_unlock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 785 | * dpm_complete - Complete a PM transition for all non-sysdev devices. |
| 786 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 787 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 788 | * Execute the ->complete() callbacks for all devices whose PM status is not |
| 789 | * DPM_ON (this allows new devices to be registered). |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 790 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 791 | void dpm_complete(pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 792 | { |
| 793 | struct list_head list; |
| 794 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 795 | might_sleep(); |
| 796 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 797 | INIT_LIST_HEAD(&list); |
| 798 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 799 | while (!list_empty(&dpm_prepared_list)) { |
| 800 | struct device *dev = to_device(dpm_prepared_list.prev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 801 | |
| 802 | get_device(dev); |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 803 | dev->power.is_prepared = false; |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 804 | list_move(&dev->power.entry, &list); |
| 805 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 806 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 807 | device_complete(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 808 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 809 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 810 | put_device(dev); |
| 811 | } |
| 812 | list_splice(&list, &dpm_list); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 813 | mutex_unlock(&dpm_list_mtx); |
| 814 | } |
| 815 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 816 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 817 | * dpm_resume_end - Execute "resume" callbacks and complete system transition. |
| 818 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 819 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 820 | * Execute "resume" callbacks for all devices and complete the PM transition of |
| 821 | * the system. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 822 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 823 | void dpm_resume_end(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 824 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 825 | dpm_resume(state); |
| 826 | dpm_complete(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 827 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 828 | EXPORT_SYMBOL_GPL(dpm_resume_end); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 829 | |
| 830 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 831 | /*------------------------- Suspend routines -------------------------*/ |
| 832 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 833 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 834 | * resume_event - Return a "resume" message for given "suspend" sleep state. |
| 835 | * @sleep_state: PM message representing a sleep state. |
| 836 | * |
| 837 | * Return a PM message representing the resume event corresponding to given |
| 838 | * sleep state. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 839 | */ |
| 840 | static pm_message_t resume_event(pm_message_t sleep_state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 841 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 842 | switch (sleep_state.event) { |
| 843 | case PM_EVENT_SUSPEND: |
| 844 | return PMSG_RESUME; |
| 845 | case PM_EVENT_FREEZE: |
| 846 | case PM_EVENT_QUIESCE: |
| 847 | return PMSG_RECOVER; |
| 848 | case PM_EVENT_HIBERNATE: |
| 849 | return PMSG_RESTORE; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 850 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 851 | return PMSG_ON; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 855 | * device_suspend_noirq - Execute a "late suspend" callback for given device. |
| 856 | * @dev: Device to handle. |
| 857 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 858 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 859 | * The driver of @dev will not receive interrupts while this function is being |
| 860 | * executed. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 861 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 862 | 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] | 863 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 864 | pm_callback_t callback = NULL; |
| 865 | char *info = NULL; |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 866 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 867 | if (dev->pm_domain) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 868 | info = "noirq power domain "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 869 | callback = pm_noirq_op(&dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 870 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 871 | info = "noirq type "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 872 | callback = pm_noirq_op(dev->type->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 873 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 874 | info = "noirq class "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 875 | callback = pm_noirq_op(dev->class->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 876 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 877 | info = "noirq bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 878 | callback = pm_noirq_op(dev->bus->pm, state); |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 881 | if (!callback && dev->driver && dev->driver->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 882 | info = "noirq driver "; |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 883 | callback = pm_noirq_op(dev->driver->pm, state); |
| 884 | } |
| 885 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 886 | return dpm_run_callback(callback, dev, state, info); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /** |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 890 | * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 891 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 892 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 893 | * Prevent device drivers from receiving interrupts and call the "noirq" suspend |
| 894 | * handlers for all non-sysdev devices. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 895 | */ |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 896 | static int dpm_suspend_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 897 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 898 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 899 | int error = 0; |
| 900 | |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 901 | suspend_device_irqs(); |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 902 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 903 | while (!list_empty(&dpm_late_early_list)) { |
| 904 | struct device *dev = to_device(dpm_late_early_list.prev); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 905 | |
| 906 | get_device(dev); |
| 907 | mutex_unlock(&dpm_list_mtx); |
| 908 | |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 909 | error = device_suspend_noirq(dev, state); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 910 | |
| 911 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 912 | if (error) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 913 | pm_dev_err(dev, state, " noirq", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 914 | suspend_stats.failed_suspend_noirq++; |
| 915 | dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ); |
| 916 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 917 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 918 | break; |
| 919 | } |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 920 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 921 | list_move(&dev->power.entry, &dpm_noirq_list); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 922 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 923 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 924 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 925 | if (error) |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 926 | dpm_resume_noirq(resume_event(state)); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 927 | else |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 928 | dpm_show_time(starttime, state, "noirq"); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 929 | return error; |
| 930 | } |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 931 | |
| 932 | /** |
| 933 | * device_suspend_late - Execute a "late suspend" callback for given device. |
| 934 | * @dev: Device to handle. |
| 935 | * @state: PM transition of the system being carried out. |
| 936 | * |
| 937 | * Runtime PM is disabled for @dev while this function is being executed. |
| 938 | */ |
| 939 | static int device_suspend_late(struct device *dev, pm_message_t state) |
| 940 | { |
| 941 | pm_callback_t callback = NULL; |
| 942 | char *info = NULL; |
| 943 | |
| 944 | if (dev->pm_domain) { |
| 945 | info = "late power domain "; |
| 946 | callback = pm_late_early_op(&dev->pm_domain->ops, state); |
| 947 | } else if (dev->type && dev->type->pm) { |
| 948 | info = "late type "; |
| 949 | callback = pm_late_early_op(dev->type->pm, state); |
| 950 | } else if (dev->class && dev->class->pm) { |
| 951 | info = "late class "; |
| 952 | callback = pm_late_early_op(dev->class->pm, state); |
| 953 | } else if (dev->bus && dev->bus->pm) { |
| 954 | info = "late bus "; |
| 955 | callback = pm_late_early_op(dev->bus->pm, state); |
| 956 | } |
| 957 | |
| 958 | if (!callback && dev->driver && dev->driver->pm) { |
| 959 | info = "late driver "; |
| 960 | callback = pm_late_early_op(dev->driver->pm, state); |
| 961 | } |
| 962 | |
| 963 | return dpm_run_callback(callback, dev, state, info); |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * dpm_suspend_late - Execute "late suspend" callbacks for all devices. |
| 968 | * @state: PM transition of the system being carried out. |
| 969 | */ |
| 970 | static int dpm_suspend_late(pm_message_t state) |
| 971 | { |
| 972 | ktime_t starttime = ktime_get(); |
| 973 | int error = 0; |
| 974 | |
| 975 | mutex_lock(&dpm_list_mtx); |
| 976 | while (!list_empty(&dpm_suspended_list)) { |
| 977 | struct device *dev = to_device(dpm_suspended_list.prev); |
| 978 | |
| 979 | get_device(dev); |
| 980 | mutex_unlock(&dpm_list_mtx); |
| 981 | |
| 982 | error = device_suspend_late(dev, state); |
| 983 | |
| 984 | mutex_lock(&dpm_list_mtx); |
| 985 | if (error) { |
| 986 | pm_dev_err(dev, state, " late", error); |
| 987 | suspend_stats.failed_suspend_late++; |
| 988 | dpm_save_failed_step(SUSPEND_SUSPEND_LATE); |
| 989 | dpm_save_failed_dev(dev_name(dev)); |
| 990 | put_device(dev); |
| 991 | break; |
| 992 | } |
| 993 | if (!list_empty(&dev->power.entry)) |
| 994 | list_move(&dev->power.entry, &dpm_late_early_list); |
| 995 | put_device(dev); |
| 996 | } |
| 997 | mutex_unlock(&dpm_list_mtx); |
| 998 | if (error) |
| 999 | dpm_resume_early(resume_event(state)); |
| 1000 | else |
| 1001 | dpm_show_time(starttime, state, "late"); |
| 1002 | |
| 1003 | return error; |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks. |
| 1008 | * @state: PM transition of the system being carried out. |
| 1009 | */ |
| 1010 | int dpm_suspend_end(pm_message_t state) |
| 1011 | { |
| 1012 | int error = dpm_suspend_late(state); |
| 1013 | |
| 1014 | return error ? : dpm_suspend_noirq(state); |
| 1015 | } |
| 1016 | EXPORT_SYMBOL_GPL(dpm_suspend_end); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1017 | |
| 1018 | /** |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1019 | * legacy_suspend - Execute a legacy (bus or class) suspend callback for device. |
Randy Dunlap | 0a88422 | 2010-01-08 14:42:57 -0800 | [diff] [blame] | 1020 | * @dev: Device to suspend. |
| 1021 | * @state: PM transition of the system being carried out. |
| 1022 | * @cb: Suspend callback to execute. |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1023 | */ |
| 1024 | static int legacy_suspend(struct device *dev, pm_message_t state, |
| 1025 | int (*cb)(struct device *dev, pm_message_t state)) |
| 1026 | { |
| 1027 | int error; |
| 1028 | ktime_t calltime; |
| 1029 | |
| 1030 | calltime = initcall_debug_start(dev); |
| 1031 | |
| 1032 | error = cb(dev, state); |
| 1033 | suspend_report_result(cb, error); |
| 1034 | |
| 1035 | initcall_debug_report(dev, calltime, error); |
| 1036 | |
| 1037 | return error; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1041 | * device_suspend - Execute "suspend" callbacks for given device. |
| 1042 | * @dev: Device to handle. |
| 1043 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1044 | * @async: If true, the device is being suspended asynchronously. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1045 | */ |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1046 | static int __device_suspend(struct device *dev, pm_message_t state, bool async) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1047 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1048 | pm_callback_t callback = NULL; |
| 1049 | char *info = NULL; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1050 | int error = 0; |
Benoit Goby | 9109a22 | 2009-08-13 09:40:42 -0700 | [diff] [blame^] | 1051 | struct timer_list timer; |
| 1052 | struct dpm_drv_wd_data data; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1053 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1054 | dpm_wait_for_children(dev, async); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 1055 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1056 | if (async_error) |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1057 | return 0; |
| 1058 | |
| 1059 | pm_runtime_get_noresume(dev); |
| 1060 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) |
| 1061 | pm_wakeup_event(dev, 0); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1062 | |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 1063 | if (pm_wakeup_pending()) { |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1064 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 1065 | async_error = -EBUSY; |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1066 | return 0; |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
Benoit Goby | 9109a22 | 2009-08-13 09:40:42 -0700 | [diff] [blame^] | 1069 | data.dev = dev; |
| 1070 | data.tsk = get_current(); |
| 1071 | init_timer_on_stack(&timer); |
| 1072 | timer.expires = jiffies + HZ * 12; |
| 1073 | timer.function = dpm_drv_timeout; |
| 1074 | timer.data = (unsigned long)&data; |
| 1075 | add_timer(&timer); |
| 1076 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1077 | device_lock(dev); |
| 1078 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 1079 | if (dev->pm_domain) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1080 | info = "power domain "; |
| 1081 | callback = pm_op(&dev->pm_domain->ops, state); |
| 1082 | goto Run; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1085 | if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1086 | info = "type "; |
| 1087 | callback = pm_op(dev->type->pm, state); |
| 1088 | goto Run; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1089 | } |
| 1090 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1091 | if (dev->class) { |
| 1092 | if (dev->class->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1093 | info = "class "; |
| 1094 | callback = pm_op(dev->class->pm, state); |
| 1095 | goto Run; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1096 | } else if (dev->class->suspend) { |
| 1097 | pm_dev_dbg(dev, state, "legacy class "); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1098 | error = legacy_suspend(dev, state, dev->class->suspend); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1099 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1100 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1103 | if (dev->bus) { |
| 1104 | if (dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1105 | info = "bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1106 | callback = pm_op(dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1107 | } else if (dev->bus->suspend) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1108 | pm_dev_dbg(dev, state, "legacy bus "); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1109 | error = legacy_suspend(dev, state, dev->bus->suspend); |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1110 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1111 | } |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1114 | Run: |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1115 | if (!callback && dev->driver && dev->driver->pm) { |
| 1116 | info = "driver "; |
| 1117 | callback = pm_op(dev->driver->pm, state); |
| 1118 | } |
| 1119 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1120 | error = dpm_run_callback(callback, dev, state, info); |
| 1121 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1122 | End: |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1123 | if (!error) { |
| 1124 | dev->power.is_suspended = true; |
Rafael J. Wysocki | 8b258cc | 2011-11-17 21:39:33 +0100 | [diff] [blame] | 1125 | if (dev->power.wakeup_path |
| 1126 | && dev->parent && !dev->parent->power.ignore_children) |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1127 | dev->parent->power.wakeup_path = true; |
| 1128 | } |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 1129 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1130 | device_unlock(dev); |
Benoit Goby | 9109a22 | 2009-08-13 09:40:42 -0700 | [diff] [blame^] | 1131 | |
| 1132 | del_timer_sync(&timer); |
| 1133 | destroy_timer_on_stack(&timer); |
| 1134 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1135 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 1136 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1137 | if (error) { |
| 1138 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1139 | async_error = error; |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1140 | } else if (dev->power.is_suspended) { |
| 1141 | __pm_runtime_disable(dev, false); |
| 1142 | } |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1143 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1144 | return error; |
| 1145 | } |
| 1146 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1147 | static void async_suspend(void *data, async_cookie_t cookie) |
| 1148 | { |
| 1149 | struct device *dev = (struct device *)data; |
| 1150 | int error; |
| 1151 | |
| 1152 | error = __device_suspend(dev, pm_transition, true); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1153 | if (error) { |
| 1154 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1155 | pm_dev_err(dev, pm_transition, " async", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1156 | } |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1157 | |
| 1158 | put_device(dev); |
| 1159 | } |
| 1160 | |
| 1161 | static int device_suspend(struct device *dev) |
| 1162 | { |
| 1163 | INIT_COMPLETION(dev->power.completion); |
| 1164 | |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 1165 | if (pm_async_enabled && dev->power.async_suspend) { |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1166 | get_device(dev); |
| 1167 | async_schedule(async_suspend, dev); |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | return __device_suspend(dev, pm_transition, false); |
| 1172 | } |
| 1173 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1174 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1175 | * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices. |
| 1176 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1177 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1178 | int dpm_suspend(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1179 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 1180 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1181 | int error = 0; |
| 1182 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1183 | might_sleep(); |
| 1184 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1185 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1186 | pm_transition = state; |
| 1187 | async_error = 0; |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1188 | while (!list_empty(&dpm_prepared_list)) { |
| 1189 | struct device *dev = to_device(dpm_prepared_list.prev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1190 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1191 | get_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1192 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1193 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1194 | error = device_suspend(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1195 | |
Alan Stern | 1b3cbec | 2008-02-29 11:50:22 -0500 | [diff] [blame] | 1196 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1197 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1198 | pm_dev_err(dev, state, "", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1199 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1200 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1201 | break; |
| 1202 | } |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 1203 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1204 | list_move(&dev->power.entry, &dpm_suspended_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1205 | put_device(dev); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1206 | if (async_error) |
| 1207 | break; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1208 | } |
| 1209 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1210 | async_synchronize_full(); |
| 1211 | if (!error) |
| 1212 | error = async_error; |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1213 | if (error) { |
| 1214 | suspend_stats.failed_suspend++; |
| 1215 | dpm_save_failed_step(SUSPEND_SUSPEND); |
| 1216 | } else |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 1217 | dpm_show_time(starttime, state, NULL); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1218 | return error; |
| 1219 | } |
| 1220 | |
| 1221 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1222 | * device_prepare - Prepare a device for system power transition. |
| 1223 | * @dev: Device to handle. |
| 1224 | * @state: PM transition of the system being carried out. |
| 1225 | * |
| 1226 | * Execute the ->prepare() callback(s) for given device. No new children of the |
| 1227 | * device may be registered after this function has returned. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1228 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1229 | static int device_prepare(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1230 | { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1231 | int (*callback)(struct device *) = NULL; |
| 1232 | char *info = NULL; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1233 | int error = 0; |
| 1234 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1235 | device_lock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1236 | |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1237 | dev->power.wakeup_path = device_may_wakeup(dev); |
| 1238 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 1239 | if (dev->pm_domain) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1240 | info = "preparing power domain "; |
| 1241 | callback = dev->pm_domain->ops.prepare; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1242 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1243 | info = "preparing type "; |
| 1244 | callback = dev->type->pm->prepare; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1245 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1246 | info = "preparing class "; |
| 1247 | callback = dev->class->pm->prepare; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1248 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1249 | info = "preparing bus "; |
| 1250 | callback = dev->bus->pm->prepare; |
| 1251 | } |
| 1252 | |
| 1253 | if (!callback && dev->driver && dev->driver->pm) { |
| 1254 | info = "preparing driver "; |
| 1255 | callback = dev->driver->pm->prepare; |
| 1256 | } |
| 1257 | |
| 1258 | if (callback) { |
| 1259 | error = callback(dev); |
| 1260 | suspend_report_result(callback, error); |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 1261 | } |
| 1262 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1263 | device_unlock(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1264 | |
| 1265 | return error; |
| 1266 | } |
| 1267 | |
| 1268 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1269 | * dpm_prepare - Prepare all non-sysdev devices for a system PM transition. |
| 1270 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1271 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1272 | * Execute the ->prepare() callback(s) for all devices. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1273 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1274 | int dpm_prepare(pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1275 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1276 | int error = 0; |
| 1277 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1278 | might_sleep(); |
| 1279 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1280 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1281 | while (!list_empty(&dpm_list)) { |
| 1282 | struct device *dev = to_device(dpm_list.next); |
| 1283 | |
| 1284 | get_device(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1285 | mutex_unlock(&dpm_list_mtx); |
| 1286 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1287 | error = device_prepare(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1288 | |
| 1289 | mutex_lock(&dpm_list_mtx); |
| 1290 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1291 | if (error == -EAGAIN) { |
| 1292 | put_device(dev); |
Sebastian Ott | 886a7a3 | 2009-07-08 13:26:05 +0200 | [diff] [blame] | 1293 | error = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1294 | continue; |
| 1295 | } |
Rafael J. Wysocki | 1e75227 | 2010-12-03 22:58:05 +0100 | [diff] [blame] | 1296 | printk(KERN_INFO "PM: Device %s not prepared " |
| 1297 | "for power transition: code %d\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 1298 | dev_name(dev), error); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1299 | put_device(dev); |
| 1300 | break; |
| 1301 | } |
Alan Stern | f76b168 | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 1302 | dev->power.is_prepared = true; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1303 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1304 | list_move_tail(&dev->power.entry, &dpm_prepared_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1305 | put_device(dev); |
| 1306 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1307 | mutex_unlock(&dpm_list_mtx); |
| 1308 | return error; |
| 1309 | } |
| 1310 | |
| 1311 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1312 | * dpm_suspend_start - Prepare devices for PM transition and suspend them. |
| 1313 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1314 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1315 | * Prepare all non-sysdev devices for system PM transition and execute "suspend" |
| 1316 | * callbacks for them. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1317 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1318 | int dpm_suspend_start(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1319 | { |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1320 | int error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1321 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1322 | error = dpm_prepare(state); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1323 | if (error) { |
| 1324 | suspend_stats.failed_prepare++; |
| 1325 | dpm_save_failed_step(SUSPEND_PREPARE); |
| 1326 | } else |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1327 | error = dpm_suspend(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1328 | return error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1329 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1330 | EXPORT_SYMBOL_GPL(dpm_suspend_start); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1331 | |
| 1332 | void __suspend_report_result(const char *function, void *fn, int ret) |
| 1333 | { |
Bjorn Helgaas | c80cfb0 | 2008-10-15 22:01:35 -0700 | [diff] [blame] | 1334 | if (ret) |
| 1335 | printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1336 | } |
| 1337 | EXPORT_SYMBOL_GPL(__suspend_report_result); |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1338 | |
| 1339 | /** |
| 1340 | * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete. |
| 1341 | * @dev: Device to wait for. |
| 1342 | * @subordinate: Device that needs to wait for @dev. |
| 1343 | */ |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1344 | int device_pm_wait_for_dev(struct device *subordinate, struct device *dev) |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1345 | { |
| 1346 | dpm_wait(dev, subordinate->power.async_suspend); |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1347 | return async_error; |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1348 | } |
| 1349 | EXPORT_SYMBOL_GPL(device_pm_wait_for_dev); |