blob: df5f41d2ec95b3be977cbe2a531b8d4aaaa1896b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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önigb5950762010-11-01 15:38:34 -040011 * This will initialize the embedded device_pm_info object in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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. Wysocki1eede072008-05-20 23:00:01 +020015 * 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 Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/device.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040021#include <linux/kallsyms.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040022#include <linux/export.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070023#include <linux/mutex.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040024#include <linux/pm.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020025#include <linux/pm_runtime.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040026#include <linux/resume-trace.h>
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +010027#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010028#include <linux/sched.h>
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010029#include <linux/async.h>
Rafael J. Wysocki1e752272010-12-03 22:58:05 +010030#include <linux/suspend.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070031
Alan Sterncd59abf2007-09-21 15:36:56 -040032#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "power.h"
34
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +010035typedef int (*pm_callback_t)(struct device *);
36
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010037/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020038 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010039 * because children are guaranteed to be discovered after parents, and
40 * are inserted at the back of the list on discovery.
41 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080042 * Since device_pm_add() may be called with a device lock held,
43 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010044 * dpm_list_mutex.
45 */
46
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020047LIST_HEAD(dpm_list);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +010048LIST_HEAD(dpm_prepared_list);
49LIST_HEAD(dpm_suspended_list);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +010050LIST_HEAD(dpm_late_early_list);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +010051LIST_HEAD(dpm_noirq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
ShuoX Liu2a77c462011-08-10 23:01:26 +020053struct suspend_stats suspend_stats;
Alan Sterncd59abf2007-09-21 15:36:56 -040054static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010055static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020057static int async_error;
58
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020059/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020060 * device_pm_init - Initialize the PM-related part of a device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020061 * @dev: Device object being initialized.
62 */
63void device_pm_init(struct device *dev)
64{
Alan Sternf76b168b2011-06-18 20:22:23 +020065 dev->power.is_prepared = false;
Alan Stern6d0e0e82011-06-18 22:42:09 +020066 dev->power.is_suspended = false;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010067 init_completion(&dev->power.completion);
Colin Cross152e1d52010-09-03 01:24:07 +020068 complete_all(&dev->power.completion);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020069 dev->power.wakeup = NULL;
70 spin_lock_init(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020071 pm_runtime_init(dev);
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +020072 INIT_LIST_HEAD(&dev->power.entry);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +020073 dev->power.power_state = PMSG_INVALID;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020074}
75
76/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020077 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020078 */
79void device_pm_lock(void)
80{
81 mutex_lock(&dpm_list_mtx);
82}
83
84/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020085 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020086 */
87void device_pm_unlock(void)
88{
89 mutex_unlock(&dpm_list_mtx);
90}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010091
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010092/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020093 * device_pm_add - Add a device to the PM core's list of active devices.
94 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010095 */
Alan Stern3b98aea2008-08-07 13:06:12 -040096void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 pr_debug("PM: Adding info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +010099 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700100 mutex_lock(&dpm_list_mtx);
Alan Sternf76b168b2011-06-18 20:22:23 +0200101 if (dev->parent && dev->parent->power.is_prepared)
Rafael J. Wysockib64959e2010-12-16 17:11:45 +0100102 dev_warn(dev, "parent %s should not be sleeping\n",
103 dev_name(dev->parent));
Alan Stern3b98aea2008-08-07 13:06:12 -0400104 list_add_tail(&dev->power.entry, &dpm_list);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200105 dev_pm_qos_constraints_init(dev);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200106 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100109/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200110 * device_pm_remove - Remove a device from the PM core's list of active devices.
111 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100112 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200113void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100116 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100117 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700118 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200119 dev_pm_qos_constraints_destroy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700121 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200122 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200123 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100124}
125
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200126/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200127 * device_pm_move_before - Move device in the PM core's list of active devices.
128 * @deva: Device to move in dpm_list.
129 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100130 */
131void device_pm_move_before(struct device *deva, struct device *devb)
132{
133 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100134 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
135 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100136 /* Delete deva from dpm_list and reinsert before devb. */
137 list_move_tail(&deva->power.entry, &devb->power.entry);
138}
139
140/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200141 * device_pm_move_after - Move device in the PM core's list of active devices.
142 * @deva: Device to move in dpm_list.
143 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100144 */
145void device_pm_move_after(struct device *deva, struct device *devb)
146{
147 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100148 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
149 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100150 /* Delete deva from dpm_list and reinsert after devb. */
151 list_move(&deva->power.entry, &devb->power.entry);
152}
153
154/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200155 * device_pm_move_last - Move device to end of the PM core's list of devices.
156 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100157 */
158void device_pm_move_last(struct device *dev)
159{
160 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100161 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100162 list_move_tail(&dev->power.entry, &dpm_list);
163}
164
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100165static ktime_t initcall_debug_start(struct device *dev)
166{
167 ktime_t calltime = ktime_set(0, 0);
168
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200169 if (pm_print_times_enabled) {
Rafael J. Wysocki0c6aebe2011-12-03 00:23:43 +0100170 pr_info("calling %s+ @ %i, parent: %s\n",
171 dev_name(dev), task_pid_nr(current),
172 dev->parent ? dev_name(dev->parent) : "none");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100173 calltime = ktime_get();
174 }
175
176 return calltime;
177}
178
179static void initcall_debug_report(struct device *dev, ktime_t calltime,
180 int error)
181{
182 ktime_t delta, rettime;
183
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200184 if (pm_print_times_enabled) {
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100185 rettime = ktime_get();
186 delta = ktime_sub(rettime, calltime);
187 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
188 error, (unsigned long long)ktime_to_ns(delta) >> 10);
189 }
190}
191
Cornelia Huckffa6a702009-03-04 12:44:00 +0100192/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100193 * dpm_wait - Wait for a PM operation to complete.
194 * @dev: Device to wait for.
195 * @async: If unset, wait only if the device's power.async_suspend flag is set.
196 */
197static void dpm_wait(struct device *dev, bool async)
198{
199 if (!dev)
200 return;
201
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100202 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100203 wait_for_completion(&dev->power.completion);
204}
205
206static int dpm_wait_fn(struct device *dev, void *async_ptr)
207{
208 dpm_wait(dev, *((bool *)async_ptr));
209 return 0;
210}
211
212static void dpm_wait_for_children(struct device *dev, bool async)
213{
214 device_for_each_child(dev, &async, dpm_wait_fn);
215}
216
217/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100218 * pm_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200219 * @ops: PM operations to choose from.
220 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200221 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100222static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200223{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200224 switch (state.event) {
225#ifdef CONFIG_SUSPEND
226 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100227 return ops->suspend;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200228 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100229 return ops->resume;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200230#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200231#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200232 case PM_EVENT_FREEZE:
233 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100234 return ops->freeze;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200235 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100236 return ops->poweroff;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200237 case PM_EVENT_THAW:
238 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100239 return ops->thaw;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200240 break;
241 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100242 return ops->restore;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200243#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200244 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100245
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100246 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200247}
248
249/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100250 * pm_late_early_op - Return the PM operation appropriate for given PM event.
251 * @ops: PM operations to choose from.
252 * @state: PM transition of the system being carried out.
253 *
254 * Runtime PM is disabled for @dev while this function is being executed.
255 */
256static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
257 pm_message_t state)
258{
259 switch (state.event) {
260#ifdef CONFIG_SUSPEND
261 case PM_EVENT_SUSPEND:
262 return ops->suspend_late;
263 case PM_EVENT_RESUME:
264 return ops->resume_early;
265#endif /* CONFIG_SUSPEND */
266#ifdef CONFIG_HIBERNATE_CALLBACKS
267 case PM_EVENT_FREEZE:
268 case PM_EVENT_QUIESCE:
269 return ops->freeze_late;
270 case PM_EVENT_HIBERNATE:
271 return ops->poweroff_late;
272 case PM_EVENT_THAW:
273 case PM_EVENT_RECOVER:
274 return ops->thaw_early;
275 case PM_EVENT_RESTORE:
276 return ops->restore_early;
277#endif /* CONFIG_HIBERNATE_CALLBACKS */
278 }
279
280 return NULL;
281}
282
283/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100284 * pm_noirq_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200285 * @ops: PM operations to choose from.
286 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200287 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200288 * The driver of @dev will not receive interrupts while this function is being
289 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200290 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100291static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200292{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200293 switch (state.event) {
294#ifdef CONFIG_SUSPEND
295 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100296 return ops->suspend_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200297 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100298 return ops->resume_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200299#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200300#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200301 case PM_EVENT_FREEZE:
302 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100303 return ops->freeze_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200304 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100305 return ops->poweroff_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200306 case PM_EVENT_THAW:
307 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100308 return ops->thaw_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200309 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100310 return ops->restore_noirq;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200311#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200312 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100313
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100314 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200315}
316
317static char *pm_verb(int event)
318{
319 switch (event) {
320 case PM_EVENT_SUSPEND:
321 return "suspend";
322 case PM_EVENT_RESUME:
323 return "resume";
324 case PM_EVENT_FREEZE:
325 return "freeze";
326 case PM_EVENT_QUIESCE:
327 return "quiesce";
328 case PM_EVENT_HIBERNATE:
329 return "hibernate";
330 case PM_EVENT_THAW:
331 return "thaw";
332 case PM_EVENT_RESTORE:
333 return "restore";
334 case PM_EVENT_RECOVER:
335 return "recover";
336 default:
337 return "(unknown PM event)";
338 }
339}
340
341static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
342{
343 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
344 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
345 ", may wakeup" : "");
346}
347
348static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
349 int error)
350{
351 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100352 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200353}
354
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100355static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
356{
357 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200358 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100359 int usecs;
360
361 calltime = ktime_get();
362 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
363 do_div(usecs64, NSEC_PER_USEC);
364 usecs = usecs64;
365 if (usecs == 0)
366 usecs = 1;
367 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
368 info ?: "", info ? " " : "", pm_verb(state.event),
369 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
370}
371
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100372static int dpm_run_callback(pm_callback_t cb, struct device *dev,
373 pm_message_t state, char *info)
374{
375 ktime_t calltime;
376 int error;
377
378 if (!cb)
379 return 0;
380
381 calltime = initcall_debug_start(dev);
382
383 pm_dev_dbg(dev, state, info);
384 error = cb(dev);
385 suspend_report_result(cb, error);
386
387 initcall_debug_report(dev, calltime, error);
388
389 return error;
390}
391
Alan Sterncd59abf2007-09-21 15:36:56 -0400392/*------------------------- Resume routines -------------------------*/
393
394/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200395 * 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 Sterncd59abf2007-09-21 15:36:56 -0400398 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200399 * The driver of @dev will not receive interrupts while this function is being
400 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400401 */
Alan Sternd1616302009-05-24 22:05:42 +0200402static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400403{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100404 pm_callback_t callback = NULL;
405 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400406 int error = 0;
407
408 TRACE_DEVICE(dev);
409 TRACE_RESUME(0);
410
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200411 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100412 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100413 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200414 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100415 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100416 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100417 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100418 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100419 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100420 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100421 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100422 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100423 }
424
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100425 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100426 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100427 callback = pm_noirq_op(dev->driver->pm, state);
428 }
429
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100430 error = dpm_run_callback(callback, dev, state, info);
431
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100432 TRACE_RESUME(error);
433 return error;
434}
435
436/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100437 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200438 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100439 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100440 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200441 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100442 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100443static void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100444{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100445 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100446
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200447 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100448 while (!list_empty(&dpm_noirq_list)) {
449 struct device *dev = to_device(dpm_noirq_list.next);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100450 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100451
452 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100453 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100454 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100455
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100456 error = device_resume_noirq(dev, state);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200457 if (error) {
458 suspend_stats.failed_resume_noirq++;
459 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
460 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100461 pm_dev_err(dev, state, " noirq", error);
462 }
463
464 mutex_lock(&dpm_list_mtx);
465 put_device(dev);
466 }
467 mutex_unlock(&dpm_list_mtx);
468 dpm_show_time(starttime, state, "noirq");
469 resume_device_irqs();
470}
471
472/**
473 * device_resume_early - Execute an "early resume" callback for given device.
474 * @dev: Device to handle.
475 * @state: PM transition of the system being carried out.
476 *
477 * Runtime PM is disabled for @dev while this function is being executed.
478 */
479static int device_resume_early(struct device *dev, pm_message_t state)
480{
481 pm_callback_t callback = NULL;
482 char *info = NULL;
483 int error = 0;
484
485 TRACE_DEVICE(dev);
486 TRACE_RESUME(0);
487
488 if (dev->pm_domain) {
489 info = "early power domain ";
490 callback = pm_late_early_op(&dev->pm_domain->ops, state);
491 } else if (dev->type && dev->type->pm) {
492 info = "early type ";
493 callback = pm_late_early_op(dev->type->pm, state);
494 } else if (dev->class && dev->class->pm) {
495 info = "early class ";
496 callback = pm_late_early_op(dev->class->pm, state);
497 } else if (dev->bus && dev->bus->pm) {
498 info = "early bus ";
499 callback = pm_late_early_op(dev->bus->pm, state);
500 }
501
502 if (!callback && dev->driver && dev->driver->pm) {
503 info = "early driver ";
504 callback = pm_late_early_op(dev->driver->pm, state);
505 }
506
507 error = dpm_run_callback(callback, dev, state, info);
508
509 TRACE_RESUME(error);
510 return error;
511}
512
513/**
514 * dpm_resume_early - Execute "early resume" callbacks for all devices.
515 * @state: PM transition of the system being carried out.
516 */
517static void dpm_resume_early(pm_message_t state)
518{
519 ktime_t starttime = ktime_get();
520
521 mutex_lock(&dpm_list_mtx);
522 while (!list_empty(&dpm_late_early_list)) {
523 struct device *dev = to_device(dpm_late_early_list.next);
524 int error;
525
526 get_device(dev);
527 list_move_tail(&dev->power.entry, &dpm_suspended_list);
528 mutex_unlock(&dpm_list_mtx);
529
530 error = device_resume_early(dev, state);
531 if (error) {
532 suspend_stats.failed_resume_early++;
533 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
534 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100535 pm_dev_err(dev, state, " early", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200536 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100537
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100538 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100539 put_device(dev);
540 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200541 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100542 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100543}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100544
545/**
546 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
547 * @state: PM transition of the system being carried out.
548 */
549void dpm_resume_start(pm_message_t state)
550{
551 dpm_resume_noirq(state);
552 dpm_resume_early(state);
553}
554EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100555
556/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100557 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200558 * @dev: Device to handle.
559 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100560 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100561 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100562static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100563{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100564 pm_callback_t callback = NULL;
565 char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100566 int error = 0;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200567 bool put = false;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100568
569 TRACE_DEVICE(dev);
570 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400571
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100572 dpm_wait(dev->parent, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800573 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100574
Alan Sternf76b168b2011-06-18 20:22:23 +0200575 /*
576 * This is a fib. But we'll allow new children to be added below
577 * a resumed device, even if the device hasn't been completed yet.
578 */
579 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100580
Alan Stern6d0e0e82011-06-18 22:42:09 +0200581 if (!dev->power.is_suspended)
582 goto Unlock;
583
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200584 pm_runtime_enable(dev);
585 put = true;
586
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200587 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100588 info = "power domain ";
589 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100590 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100591 }
592
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100593 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100594 info = "type ";
595 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100596 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100597 }
598
599 if (dev->class) {
600 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100601 info = "class ";
602 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100603 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100604 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100605 info = "legacy class ";
606 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100607 goto End;
608 }
609 }
610
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200611 if (dev->bus) {
612 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100613 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100614 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200615 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100616 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100617 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100618 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200619 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400620 }
621
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100622 Driver:
623 if (!callback && dev->driver && dev->driver->pm) {
624 info = "driver ";
625 callback = pm_op(dev->driver->pm, state);
626 }
627
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200628 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100629 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200630 dev->power.is_suspended = false;
631
632 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800633 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100634 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100635
Alan Sterncd59abf2007-09-21 15:36:56 -0400636 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200637
638 if (put)
639 pm_runtime_put_sync(dev);
640
Alan Sterncd59abf2007-09-21 15:36:56 -0400641 return error;
642}
643
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100644static void async_resume(void *data, async_cookie_t cookie)
645{
646 struct device *dev = (struct device *)data;
647 int error;
648
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100649 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100650 if (error)
651 pm_dev_err(dev, pm_transition, " async", error);
652 put_device(dev);
653}
654
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100655static bool is_async(struct device *dev)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100656{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100657 return dev->power.async_suspend && pm_async_enabled
658 && !pm_trace_is_enabled();
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100659}
660
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100661/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200662 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
663 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100664 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200665 * Execute the appropriate "resume" callback for all devices whose status
666 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400667 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200668void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400669{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100670 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100671 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400672
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200673 might_sleep();
674
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200675 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100676 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200677 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200678
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100679 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100680 INIT_COMPLETION(dev->power.completion);
681 if (is_async(dev)) {
682 get_device(dev);
683 async_schedule(async_resume, dev);
684 }
685 }
686
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100687 while (!list_empty(&dpm_suspended_list)) {
688 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200689 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100690 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200691 int error;
692
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200693 mutex_unlock(&dpm_list_mtx);
694
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100695 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200696 if (error) {
697 suspend_stats.failed_resume++;
698 dpm_save_failed_step(SUSPEND_RESUME);
699 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200700 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200701 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100702
703 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200704 }
705 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100706 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200707 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400708 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200709 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100710 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100711 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200712}
713
714/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200715 * device_complete - Complete a PM transition for given device.
716 * @dev: Device to handle.
717 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200718 */
Alan Sternd1616302009-05-24 22:05:42 +0200719static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200720{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100721 void (*callback)(struct device *) = NULL;
722 char *info = NULL;
723
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800724 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200725
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200726 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100727 info = "completing power domain ";
728 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200729 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100730 info = "completing type ";
731 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100732 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100733 info = "completing class ";
734 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100735 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100736 info = "completing bus ";
737 callback = dev->bus->pm->complete;
738 }
739
740 if (!callback && dev->driver && dev->driver->pm) {
741 info = "completing driver ";
742 callback = dev->driver->pm->complete;
743 }
744
745 if (callback) {
746 pm_dev_dbg(dev, state, info);
747 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200748 }
749
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800750 device_unlock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200751}
752
753/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200754 * dpm_complete - Complete a PM transition for all non-sysdev devices.
755 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200756 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200757 * Execute the ->complete() callbacks for all devices whose PM status is not
758 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200759 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200760void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200761{
762 struct list_head list;
763
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200764 might_sleep();
765
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200766 INIT_LIST_HEAD(&list);
767 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100768 while (!list_empty(&dpm_prepared_list)) {
769 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200770
771 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +0200772 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100773 list_move(&dev->power.entry, &list);
774 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200775
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100776 device_complete(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200777
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100778 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200779 put_device(dev);
780 }
781 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400782 mutex_unlock(&dpm_list_mtx);
783}
784
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100785/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200786 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
787 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400788 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200789 * Execute "resume" callbacks for all devices and complete the PM transition of
790 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400791 */
Alan Sternd1616302009-05-24 22:05:42 +0200792void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400793{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200794 dpm_resume(state);
795 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400796}
Alan Sternd1616302009-05-24 22:05:42 +0200797EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400798
799
Alan Sterncd59abf2007-09-21 15:36:56 -0400800/*------------------------- Suspend routines -------------------------*/
801
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200802/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200803 * resume_event - Return a "resume" message for given "suspend" sleep state.
804 * @sleep_state: PM message representing a sleep state.
805 *
806 * Return a PM message representing the resume event corresponding to given
807 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200808 */
809static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400810{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200811 switch (sleep_state.event) {
812 case PM_EVENT_SUSPEND:
813 return PMSG_RESUME;
814 case PM_EVENT_FREEZE:
815 case PM_EVENT_QUIESCE:
816 return PMSG_RECOVER;
817 case PM_EVENT_HIBERNATE:
818 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400819 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200820 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400821}
822
823/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200824 * device_suspend_noirq - Execute a "late suspend" callback for given device.
825 * @dev: Device to handle.
826 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100827 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200828 * The driver of @dev will not receive interrupts while this function is being
829 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100830 */
Alan Sternd1616302009-05-24 22:05:42 +0200831static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100832{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100833 pm_callback_t callback = NULL;
834 char *info = NULL;
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100835
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200836 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100837 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100838 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200839 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100840 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100841 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100842 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100843 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100844 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100845 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100846 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100847 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100848 }
849
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100850 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100851 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100852 callback = pm_noirq_op(dev->driver->pm, state);
853 }
854
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100855 return dpm_run_callback(callback, dev, state, info);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100856}
857
858/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100859 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200860 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100861 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200862 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
863 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100864 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100865static int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100866{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100867 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100868 int error = 0;
869
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +0100870 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200871 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100872 while (!list_empty(&dpm_late_early_list)) {
873 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100874
875 get_device(dev);
876 mutex_unlock(&dpm_list_mtx);
877
Alan Sternd1616302009-05-24 22:05:42 +0200878 error = device_suspend_noirq(dev, state);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100879
880 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100881 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100882 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200883 suspend_stats.failed_suspend_noirq++;
884 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
885 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100886 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100887 break;
888 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100889 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100890 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100891 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +0200892
893 if (pm_wakeup_pending()) {
894 error = -EBUSY;
895 break;
896 }
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100897 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200898 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100899 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200900 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100901 else
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100902 dpm_show_time(starttime, state, "noirq");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100903 return error;
904}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100905
906/**
907 * device_suspend_late - Execute a "late suspend" callback for given device.
908 * @dev: Device to handle.
909 * @state: PM transition of the system being carried out.
910 *
911 * Runtime PM is disabled for @dev while this function is being executed.
912 */
913static int device_suspend_late(struct device *dev, pm_message_t state)
914{
915 pm_callback_t callback = NULL;
916 char *info = NULL;
917
918 if (dev->pm_domain) {
919 info = "late power domain ";
920 callback = pm_late_early_op(&dev->pm_domain->ops, state);
921 } else if (dev->type && dev->type->pm) {
922 info = "late type ";
923 callback = pm_late_early_op(dev->type->pm, state);
924 } else if (dev->class && dev->class->pm) {
925 info = "late class ";
926 callback = pm_late_early_op(dev->class->pm, state);
927 } else if (dev->bus && dev->bus->pm) {
928 info = "late bus ";
929 callback = pm_late_early_op(dev->bus->pm, state);
930 }
931
932 if (!callback && dev->driver && dev->driver->pm) {
933 info = "late driver ";
934 callback = pm_late_early_op(dev->driver->pm, state);
935 }
936
937 return dpm_run_callback(callback, dev, state, info);
938}
939
940/**
941 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
942 * @state: PM transition of the system being carried out.
943 */
944static int dpm_suspend_late(pm_message_t state)
945{
946 ktime_t starttime = ktime_get();
947 int error = 0;
948
949 mutex_lock(&dpm_list_mtx);
950 while (!list_empty(&dpm_suspended_list)) {
951 struct device *dev = to_device(dpm_suspended_list.prev);
952
953 get_device(dev);
954 mutex_unlock(&dpm_list_mtx);
955
956 error = device_suspend_late(dev, state);
957
958 mutex_lock(&dpm_list_mtx);
959 if (error) {
960 pm_dev_err(dev, state, " late", error);
961 suspend_stats.failed_suspend_late++;
962 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
963 dpm_save_failed_dev(dev_name(dev));
964 put_device(dev);
965 break;
966 }
967 if (!list_empty(&dev->power.entry))
968 list_move(&dev->power.entry, &dpm_late_early_list);
969 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +0200970
971 if (pm_wakeup_pending()) {
972 error = -EBUSY;
973 break;
974 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100975 }
976 mutex_unlock(&dpm_list_mtx);
977 if (error)
978 dpm_resume_early(resume_event(state));
979 else
980 dpm_show_time(starttime, state, "late");
981
982 return error;
983}
984
985/**
986 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
987 * @state: PM transition of the system being carried out.
988 */
989int dpm_suspend_end(pm_message_t state)
990{
991 int error = dpm_suspend_late(state);
992
993 return error ? : dpm_suspend_noirq(state);
994}
995EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100996
997/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100998 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800999 * @dev: Device to suspend.
1000 * @state: PM transition of the system being carried out.
1001 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001002 */
1003static int legacy_suspend(struct device *dev, pm_message_t state,
1004 int (*cb)(struct device *dev, pm_message_t state))
1005{
1006 int error;
1007 ktime_t calltime;
1008
1009 calltime = initcall_debug_start(dev);
1010
1011 error = cb(dev, state);
1012 suspend_report_result(cb, error);
1013
1014 initcall_debug_report(dev, calltime, error);
1015
1016 return error;
1017}
1018
1019/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001020 * device_suspend - Execute "suspend" callbacks for given device.
1021 * @dev: Device to handle.
1022 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001023 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001024 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001025static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001026{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001027 pm_callback_t callback = NULL;
1028 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001029 int error = 0;
1030
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001031 dpm_wait_for_children(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001032
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001033 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001034 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001035
1036 pm_runtime_get_noresume(dev);
1037 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1038 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001039
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001040 if (pm_wakeup_pending()) {
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001041 pm_runtime_put_sync(dev);
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001042 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001043 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001044 }
1045
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001046 device_lock(dev);
1047
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001048 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001049 info = "power domain ";
1050 callback = pm_op(&dev->pm_domain->ops, state);
1051 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001052 }
1053
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001054 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001055 info = "type ";
1056 callback = pm_op(dev->type->pm, state);
1057 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001058 }
1059
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001060 if (dev->class) {
1061 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001062 info = "class ";
1063 callback = pm_op(dev->class->pm, state);
1064 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001065 } else if (dev->class->suspend) {
1066 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001067 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001068 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001069 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001070 }
1071
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001072 if (dev->bus) {
1073 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001074 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001075 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001076 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001077 pm_dev_dbg(dev, state, "legacy bus ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001078 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001079 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001080 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001081 }
1082
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001083 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001084 if (!callback && dev->driver && dev->driver->pm) {
1085 info = "driver ";
1086 callback = pm_op(dev->driver->pm, state);
1087 }
1088
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001089 error = dpm_run_callback(callback, dev, state, info);
1090
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001091 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001092 if (!error) {
1093 dev->power.is_suspended = true;
Rafael J. Wysocki8b258cc2011-11-17 21:39:33 +01001094 if (dev->power.wakeup_path
1095 && dev->parent && !dev->parent->power.ignore_children)
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001096 dev->parent->power.wakeup_path = true;
1097 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001098
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001099 device_unlock(dev);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001100
1101 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001102 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001103
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001104 if (error) {
1105 pm_runtime_put_sync(dev);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001106 async_error = error;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001107 } else if (dev->power.is_suspended) {
1108 __pm_runtime_disable(dev, false);
1109 }
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001110
Alan Sterncd59abf2007-09-21 15:36:56 -04001111 return error;
1112}
1113
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001114static void async_suspend(void *data, async_cookie_t cookie)
1115{
1116 struct device *dev = (struct device *)data;
1117 int error;
1118
1119 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001120 if (error) {
1121 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001122 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001123 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001124
1125 put_device(dev);
1126}
1127
1128static int device_suspend(struct device *dev)
1129{
1130 INIT_COMPLETION(dev->power.completion);
1131
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +01001132 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001133 get_device(dev);
1134 async_schedule(async_suspend, dev);
1135 return 0;
1136 }
1137
1138 return __device_suspend(dev, pm_transition, false);
1139}
1140
Alan Sterncd59abf2007-09-21 15:36:56 -04001141/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001142 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1143 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001144 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001145int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001146{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001147 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001148 int error = 0;
1149
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001150 might_sleep();
1151
Alan Sterncd59abf2007-09-21 15:36:56 -04001152 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001153 pm_transition = state;
1154 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001155 while (!list_empty(&dpm_prepared_list)) {
1156 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001157
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001158 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001159 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001160
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001161 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001162
Alan Stern1b3cbec2008-02-29 11:50:22 -05001163 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001164 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001165 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001166 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001167 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001168 break;
1169 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001170 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001171 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001172 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001173 if (async_error)
1174 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001175 }
1176 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001177 async_synchronize_full();
1178 if (!error)
1179 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001180 if (error) {
1181 suspend_stats.failed_suspend++;
1182 dpm_save_failed_step(SUSPEND_SUSPEND);
1183 } else
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001184 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001185 return error;
1186}
1187
1188/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001189 * device_prepare - Prepare a device for system power transition.
1190 * @dev: Device to handle.
1191 * @state: PM transition of the system being carried out.
1192 *
1193 * Execute the ->prepare() callback(s) for given device. No new children of the
1194 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001195 */
Alan Sternd1616302009-05-24 22:05:42 +02001196static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001197{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001198 int (*callback)(struct device *) = NULL;
1199 char *info = NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001200 int error = 0;
1201
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001202 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001203
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001204 dev->power.wakeup_path = device_may_wakeup(dev);
1205
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001206 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001207 info = "preparing power domain ";
1208 callback = dev->pm_domain->ops.prepare;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001209 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001210 info = "preparing type ";
1211 callback = dev->type->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001212 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001213 info = "preparing class ";
1214 callback = dev->class->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001215 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001216 info = "preparing bus ";
1217 callback = dev->bus->pm->prepare;
1218 }
1219
1220 if (!callback && dev->driver && dev->driver->pm) {
1221 info = "preparing driver ";
1222 callback = dev->driver->pm->prepare;
1223 }
1224
1225 if (callback) {
1226 error = callback(dev);
1227 suspend_report_result(callback, error);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001228 }
1229
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001230 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001231
1232 return error;
1233}
1234
1235/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001236 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1237 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001238 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001239 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001240 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001241int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001242{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001243 int error = 0;
1244
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001245 might_sleep();
1246
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001247 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001248 while (!list_empty(&dpm_list)) {
1249 struct device *dev = to_device(dpm_list.next);
1250
1251 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001252 mutex_unlock(&dpm_list_mtx);
1253
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001254 error = device_prepare(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001255
1256 mutex_lock(&dpm_list_mtx);
1257 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001258 if (error == -EAGAIN) {
1259 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001260 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001261 continue;
1262 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001263 printk(KERN_INFO "PM: Device %s not prepared "
1264 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001265 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001266 put_device(dev);
1267 break;
1268 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001269 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001270 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001271 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001272 put_device(dev);
1273 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001274 mutex_unlock(&dpm_list_mtx);
1275 return error;
1276}
1277
1278/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001279 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1280 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001281 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001282 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1283 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001284 */
Alan Sternd1616302009-05-24 22:05:42 +02001285int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001286{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001287 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001288
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001289 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001290 if (error) {
1291 suspend_stats.failed_prepare++;
1292 dpm_save_failed_step(SUSPEND_PREPARE);
1293 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001294 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001295 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001296}
Alan Sternd1616302009-05-24 22:05:42 +02001297EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001298
1299void __suspend_report_result(const char *function, void *fn, int ret)
1300{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001301 if (ret)
1302 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001303}
1304EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001305
1306/**
1307 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1308 * @dev: Device to wait for.
1309 * @subordinate: Device that needs to wait for @dev.
1310 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001311int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001312{
1313 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001314 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001315}
1316EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);