blob: a90480baa850e54524288a7ef789c6c648b08749 [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.
11 * This will intialize the embedded device_pm_info object in the device
12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
14 *
Rafael J. 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>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070022#include <linux/mutex.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040023#include <linux/pm.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020024#include <linux/pm_runtime.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040025#include <linux/resume-trace.h>
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +010026#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010027#include <linux/sched.h>
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010028#include <linux/async.h>
Rafael J. Wysocki1e752272010-12-03 22:58:05 +010029#include <linux/suspend.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070030
Alan Sterncd59abf2007-09-21 15:36:56 -040031#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "power.h"
33
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010034/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020035 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010036 * because children are guaranteed to be discovered after parents, and
37 * are inserted at the back of the list on discovery.
38 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080039 * Since device_pm_add() may be called with a device lock held,
40 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010041 * dpm_list_mutex.
42 */
43
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020044LIST_HEAD(dpm_list);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +010045LIST_HEAD(dpm_prepared_list);
46LIST_HEAD(dpm_suspended_list);
47LIST_HEAD(dpm_noirq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Alan Sterncd59abf2007-09-21 15:36:56 -040049static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010050static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020052/*
53 * Set once the preparation of devices for a PM transition has started, reset
54 * before starting to resume devices. Protected by dpm_list_mtx.
55 */
56static bool transition_started;
57
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020058static int async_error;
59
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020060/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020061 * device_pm_init - Initialize the PM-related part of a device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020062 * @dev: Device object being initialized.
63 */
64void device_pm_init(struct device *dev)
65{
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +010066 dev->power.in_suspend = 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);
72}
73
74/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020075 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020076 */
77void device_pm_lock(void)
78{
79 mutex_lock(&dpm_list_mtx);
80}
81
82/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020083 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020084 */
85void device_pm_unlock(void)
86{
87 mutex_unlock(&dpm_list_mtx);
88}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010089
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010090/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020091 * device_pm_add - Add a device to the PM core's list of active devices.
92 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010093 */
Alan Stern3b98aea2008-08-07 13:06:12 -040094void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 pr_debug("PM: Adding info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040097 dev->bus ? dev->bus->name : "No Bus",
98 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070099 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200100 if (dev->parent) {
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +0100101 if (dev->parent->power.in_suspend)
Rafael J. Wysockif5a6d952008-08-09 01:05:13 +0200102 dev_warn(dev, "parent %s should not be sleeping\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100103 dev_name(dev->parent));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200104 } else if (transition_started) {
105 /*
106 * We refuse to register parentless devices while a PM
107 * transition is in progress in order to avoid leaving them
108 * unhandled down the road
109 */
Arjan van de Ven728f0892008-09-20 19:09:00 -0700110 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100111 }
Alan Stern3b98aea2008-08-07 13:06:12 -0400112
113 list_add_tail(&dev->power.entry, &dpm_list);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700114 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100117/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200118 * device_pm_remove - Remove a device from the PM core's list of active devices.
119 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100120 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200121void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 pr_debug("PM: Removing info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -0400124 dev->bus ? dev->bus->name : "No Bus",
125 kobject_name(&dev->kobj));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100126 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700127 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700129 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200130 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200131 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100132}
133
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200134/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200135 * device_pm_move_before - Move device in the PM core's list of active devices.
136 * @deva: Device to move in dpm_list.
137 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100138 */
139void device_pm_move_before(struct device *deva, struct device *devb)
140{
141 pr_debug("PM: Moving %s:%s before %s:%s\n",
142 deva->bus ? deva->bus->name : "No Bus",
143 kobject_name(&deva->kobj),
144 devb->bus ? devb->bus->name : "No Bus",
145 kobject_name(&devb->kobj));
146 /* Delete deva from dpm_list and reinsert before devb. */
147 list_move_tail(&deva->power.entry, &devb->power.entry);
148}
149
150/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200151 * device_pm_move_after - Move device in the PM core's list of active devices.
152 * @deva: Device to move in dpm_list.
153 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100154 */
155void device_pm_move_after(struct device *deva, struct device *devb)
156{
157 pr_debug("PM: Moving %s:%s after %s:%s\n",
158 deva->bus ? deva->bus->name : "No Bus",
159 kobject_name(&deva->kobj),
160 devb->bus ? devb->bus->name : "No Bus",
161 kobject_name(&devb->kobj));
162 /* Delete deva from dpm_list and reinsert after devb. */
163 list_move(&deva->power.entry, &devb->power.entry);
164}
165
166/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200167 * device_pm_move_last - Move device to end of the PM core's list of devices.
168 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100169 */
170void device_pm_move_last(struct device *dev)
171{
172 pr_debug("PM: Moving %s:%s to end of list\n",
173 dev->bus ? dev->bus->name : "No Bus",
174 kobject_name(&dev->kobj));
175 list_move_tail(&dev->power.entry, &dpm_list);
176}
177
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100178static ktime_t initcall_debug_start(struct device *dev)
179{
180 ktime_t calltime = ktime_set(0, 0);
181
182 if (initcall_debug) {
183 pr_info("calling %s+ @ %i\n",
184 dev_name(dev), task_pid_nr(current));
185 calltime = ktime_get();
186 }
187
188 return calltime;
189}
190
191static void initcall_debug_report(struct device *dev, ktime_t calltime,
192 int error)
193{
194 ktime_t delta, rettime;
195
196 if (initcall_debug) {
197 rettime = ktime_get();
198 delta = ktime_sub(rettime, calltime);
199 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
200 error, (unsigned long long)ktime_to_ns(delta) >> 10);
201 }
202}
203
Cornelia Huckffa6a702009-03-04 12:44:00 +0100204/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100205 * dpm_wait - Wait for a PM operation to complete.
206 * @dev: Device to wait for.
207 * @async: If unset, wait only if the device's power.async_suspend flag is set.
208 */
209static void dpm_wait(struct device *dev, bool async)
210{
211 if (!dev)
212 return;
213
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100214 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100215 wait_for_completion(&dev->power.completion);
216}
217
218static int dpm_wait_fn(struct device *dev, void *async_ptr)
219{
220 dpm_wait(dev, *((bool *)async_ptr));
221 return 0;
222}
223
224static void dpm_wait_for_children(struct device *dev, bool async)
225{
226 device_for_each_child(dev, &async, dpm_wait_fn);
227}
228
229/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200230 * pm_op - Execute the PM operation appropriate for given PM event.
231 * @dev: Device to handle.
232 * @ops: PM operations to choose from.
233 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200234 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200235static int pm_op(struct device *dev,
236 const struct dev_pm_ops *ops,
237 pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200238{
239 int error = 0;
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100240 ktime_t calltime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100241
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100242 calltime = initcall_debug_start(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200243
244 switch (state.event) {
245#ifdef CONFIG_SUSPEND
246 case PM_EVENT_SUSPEND:
247 if (ops->suspend) {
248 error = ops->suspend(dev);
249 suspend_report_result(ops->suspend, error);
250 }
251 break;
252 case PM_EVENT_RESUME:
253 if (ops->resume) {
254 error = ops->resume(dev);
255 suspend_report_result(ops->resume, error);
256 }
257 break;
258#endif /* CONFIG_SUSPEND */
259#ifdef CONFIG_HIBERNATION
260 case PM_EVENT_FREEZE:
261 case PM_EVENT_QUIESCE:
262 if (ops->freeze) {
263 error = ops->freeze(dev);
264 suspend_report_result(ops->freeze, error);
265 }
266 break;
267 case PM_EVENT_HIBERNATE:
268 if (ops->poweroff) {
269 error = ops->poweroff(dev);
270 suspend_report_result(ops->poweroff, error);
271 }
272 break;
273 case PM_EVENT_THAW:
274 case PM_EVENT_RECOVER:
275 if (ops->thaw) {
276 error = ops->thaw(dev);
277 suspend_report_result(ops->thaw, error);
278 }
279 break;
280 case PM_EVENT_RESTORE:
281 if (ops->restore) {
282 error = ops->restore(dev);
283 suspend_report_result(ops->restore, error);
284 }
285 break;
286#endif /* CONFIG_HIBERNATION */
287 default:
288 error = -EINVAL;
289 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100290
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100291 initcall_debug_report(dev, calltime, error);
Arjan van de Venf2511772009-12-13 20:29:01 +0100292
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200293 return error;
294}
295
296/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200297 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
298 * @dev: Device to handle.
299 * @ops: PM operations to choose from.
300 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200301 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200302 * The driver of @dev will not receive interrupts while this function is being
303 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200304 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200305static int pm_noirq_op(struct device *dev,
306 const struct dev_pm_ops *ops,
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200307 pm_message_t state)
308{
309 int error = 0;
Rafael J. Wysocki2cbb3ce2010-12-15 00:17:29 +0100310 ktime_t calltime = ktime_set(0, 0), delta, rettime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100311
312 if (initcall_debug) {
Rafael J. Wysocki8cc6b392010-01-23 22:03:29 +0100313 pr_info("calling %s+ @ %i, parent: %s\n",
314 dev_name(dev), task_pid_nr(current),
315 dev->parent ? dev_name(dev->parent) : "none");
Arjan van de Venf2511772009-12-13 20:29:01 +0100316 calltime = ktime_get();
317 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200318
319 switch (state.event) {
320#ifdef CONFIG_SUSPEND
321 case PM_EVENT_SUSPEND:
322 if (ops->suspend_noirq) {
323 error = ops->suspend_noirq(dev);
324 suspend_report_result(ops->suspend_noirq, error);
325 }
326 break;
327 case PM_EVENT_RESUME:
328 if (ops->resume_noirq) {
329 error = ops->resume_noirq(dev);
330 suspend_report_result(ops->resume_noirq, error);
331 }
332 break;
333#endif /* CONFIG_SUSPEND */
334#ifdef CONFIG_HIBERNATION
335 case PM_EVENT_FREEZE:
336 case PM_EVENT_QUIESCE:
337 if (ops->freeze_noirq) {
338 error = ops->freeze_noirq(dev);
339 suspend_report_result(ops->freeze_noirq, error);
340 }
341 break;
342 case PM_EVENT_HIBERNATE:
343 if (ops->poweroff_noirq) {
344 error = ops->poweroff_noirq(dev);
345 suspend_report_result(ops->poweroff_noirq, error);
346 }
347 break;
348 case PM_EVENT_THAW:
349 case PM_EVENT_RECOVER:
350 if (ops->thaw_noirq) {
351 error = ops->thaw_noirq(dev);
352 suspend_report_result(ops->thaw_noirq, error);
353 }
354 break;
355 case PM_EVENT_RESTORE:
356 if (ops->restore_noirq) {
357 error = ops->restore_noirq(dev);
358 suspend_report_result(ops->restore_noirq, error);
359 }
360 break;
361#endif /* CONFIG_HIBERNATION */
362 default:
363 error = -EINVAL;
364 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100365
366 if (initcall_debug) {
367 rettime = ktime_get();
368 delta = ktime_sub(rettime, calltime);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100369 printk("initcall %s_i+ returned %d after %Ld usecs\n",
370 dev_name(dev), error,
371 (unsigned long long)ktime_to_ns(delta) >> 10);
Arjan van de Venf2511772009-12-13 20:29:01 +0100372 }
373
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200374 return error;
375}
376
377static char *pm_verb(int event)
378{
379 switch (event) {
380 case PM_EVENT_SUSPEND:
381 return "suspend";
382 case PM_EVENT_RESUME:
383 return "resume";
384 case PM_EVENT_FREEZE:
385 return "freeze";
386 case PM_EVENT_QUIESCE:
387 return "quiesce";
388 case PM_EVENT_HIBERNATE:
389 return "hibernate";
390 case PM_EVENT_THAW:
391 return "thaw";
392 case PM_EVENT_RESTORE:
393 return "restore";
394 case PM_EVENT_RECOVER:
395 return "recover";
396 default:
397 return "(unknown PM event)";
398 }
399}
400
401static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
402{
403 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
404 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
405 ", may wakeup" : "");
406}
407
408static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
409 int error)
410{
411 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
412 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
413}
414
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100415static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
416{
417 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200418 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100419 int usecs;
420
421 calltime = ktime_get();
422 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
423 do_div(usecs64, NSEC_PER_USEC);
424 usecs = usecs64;
425 if (usecs == 0)
426 usecs = 1;
427 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
428 info ?: "", info ? " " : "", pm_verb(state.event),
429 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
430}
431
Alan Sterncd59abf2007-09-21 15:36:56 -0400432/*------------------------- Resume routines -------------------------*/
433
434/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200435 * device_resume_noirq - Execute an "early resume" callback for given device.
436 * @dev: Device to handle.
437 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400438 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200439 * The driver of @dev will not receive interrupts while this function is being
440 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400441 */
Alan Sternd1616302009-05-24 22:05:42 +0200442static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400443{
444 int error = 0;
445
446 TRACE_DEVICE(dev);
447 TRACE_RESUME(0);
448
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100449 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200450 pm_dev_dbg(dev, state, "EARLY ");
451 error = pm_noirq_op(dev, dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100452 if (error)
453 goto End;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100454 }
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100455
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100456 if (dev->type && dev->type->pm) {
457 pm_dev_dbg(dev, state, "EARLY type ");
458 error = pm_noirq_op(dev, dev->type->pm, state);
459 if (error)
460 goto End;
461 }
462
463 if (dev->class && dev->class->pm) {
464 pm_dev_dbg(dev, state, "EARLY class ");
465 error = pm_noirq_op(dev, dev->class->pm, state);
466 }
467
468End:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100469 TRACE_RESUME(error);
470 return error;
471}
472
473/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200474 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
475 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100476 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200477 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
478 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100479 */
Alan Sternd1616302009-05-24 22:05:42 +0200480void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100481{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100482 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100483
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200484 mutex_lock(&dpm_list_mtx);
Xiaotian Feng3eb132c2009-08-20 20:28:33 +0200485 transition_started = false;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100486 while (!list_empty(&dpm_noirq_list)) {
487 struct device *dev = to_device(dpm_noirq_list.next);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100488 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100489
490 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100491 list_move_tail(&dev->power.entry, &dpm_suspended_list);
492 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100493
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100494 error = device_resume_noirq(dev, state);
495 if (error)
496 pm_dev_err(dev, state, " early", error);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100497
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100498 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100499 put_device(dev);
500 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200501 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100502 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100503 resume_device_irqs();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100504}
Alan Sternd1616302009-05-24 22:05:42 +0200505EXPORT_SYMBOL_GPL(dpm_resume_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100506
507/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100508 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800509 * @dev: Device to resume.
510 * @cb: Resume callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100511 */
512static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
513{
514 int error;
515 ktime_t calltime;
516
517 calltime = initcall_debug_start(dev);
518
519 error = cb(dev);
520 suspend_report_result(cb, error);
521
522 initcall_debug_report(dev, calltime, error);
523
524 return error;
525}
526
527/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100528 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200529 * @dev: Device to handle.
530 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100531 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100532 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100533static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100534{
535 int error = 0;
536
537 TRACE_DEVICE(dev);
538 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400539
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100540 dpm_wait(dev->parent, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800541 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100542
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +0100543 dev->power.in_suspend = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100544
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200545 if (dev->bus) {
546 if (dev->bus->pm) {
547 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200548 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200549 } else if (dev->bus->resume) {
550 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100551 error = legacy_resume(dev, dev->bus->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200552 }
553 if (error)
554 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400555 }
556
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200557 if (dev->type) {
558 if (dev->type->pm) {
559 pm_dev_dbg(dev, state, "type ");
560 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200561 }
562 if (error)
563 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400564 }
565
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200566 if (dev->class) {
567 if (dev->class->pm) {
568 pm_dev_dbg(dev, state, "class ");
569 error = pm_op(dev, dev->class->pm, state);
570 } else if (dev->class->resume) {
571 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100572 error = legacy_resume(dev, dev->class->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200573 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400574 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200575 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800576 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100577 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100578
Alan Sterncd59abf2007-09-21 15:36:56 -0400579 TRACE_RESUME(error);
580 return error;
581}
582
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100583static void async_resume(void *data, async_cookie_t cookie)
584{
585 struct device *dev = (struct device *)data;
586 int error;
587
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100588 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100589 if (error)
590 pm_dev_err(dev, pm_transition, " async", error);
591 put_device(dev);
592}
593
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100594static bool is_async(struct device *dev)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100595{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100596 return dev->power.async_suspend && pm_async_enabled
597 && !pm_trace_is_enabled();
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100598}
599
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100600/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200601 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
602 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100603 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200604 * Execute the appropriate "resume" callback for all devices whose status
605 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400606 */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200607static void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400608{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100609 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100610 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400611
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200612 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100613 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200614 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200615
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100616 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100617 INIT_COMPLETION(dev->power.completion);
618 if (is_async(dev)) {
619 get_device(dev);
620 async_schedule(async_resume, dev);
621 }
622 }
623
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100624 while (!list_empty(&dpm_suspended_list)) {
625 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200626 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100627 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200628 int error;
629
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200630 mutex_unlock(&dpm_list_mtx);
631
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100632 error = device_resume(dev, state, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200633 if (error)
634 pm_dev_err(dev, state, "", error);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100635
636 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200637 }
638 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100639 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200640 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400641 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200642 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100643 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100644 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200645}
646
647/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200648 * device_complete - Complete a PM transition for given device.
649 * @dev: Device to handle.
650 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200651 */
Alan Sternd1616302009-05-24 22:05:42 +0200652static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200653{
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800654 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200655
656 if (dev->class && dev->class->pm && dev->class->pm->complete) {
657 pm_dev_dbg(dev, state, "completing class ");
658 dev->class->pm->complete(dev);
659 }
660
661 if (dev->type && dev->type->pm && dev->type->pm->complete) {
662 pm_dev_dbg(dev, state, "completing type ");
663 dev->type->pm->complete(dev);
664 }
665
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200666 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200667 pm_dev_dbg(dev, state, "completing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200668 dev->bus->pm->complete(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200669 }
670
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800671 device_unlock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200672}
673
674/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200675 * dpm_complete - Complete a PM transition for all non-sysdev devices.
676 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200677 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200678 * Execute the ->complete() callbacks for all devices whose PM status is not
679 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200680 */
681static void dpm_complete(pm_message_t state)
682{
683 struct list_head list;
684
685 INIT_LIST_HEAD(&list);
686 mutex_lock(&dpm_list_mtx);
Romit Dasguptae528e872009-10-28 22:56:02 +0100687 transition_started = false;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100688 while (!list_empty(&dpm_prepared_list)) {
689 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200690
691 get_device(dev);
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +0100692 dev->power.in_suspend = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100693 list_move(&dev->power.entry, &list);
694 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200695
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100696 device_complete(dev, state);
697 pm_runtime_put_sync(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200698
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100699 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200700 put_device(dev);
701 }
702 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400703 mutex_unlock(&dpm_list_mtx);
704}
705
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100706/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200707 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
708 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400709 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200710 * Execute "resume" callbacks for all devices and complete the PM transition of
711 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400712 */
Alan Sternd1616302009-05-24 22:05:42 +0200713void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400714{
715 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200716 dpm_resume(state);
717 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400718}
Alan Sternd1616302009-05-24 22:05:42 +0200719EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400720
721
Alan Sterncd59abf2007-09-21 15:36:56 -0400722/*------------------------- Suspend routines -------------------------*/
723
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200724/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200725 * resume_event - Return a "resume" message for given "suspend" sleep state.
726 * @sleep_state: PM message representing a sleep state.
727 *
728 * Return a PM message representing the resume event corresponding to given
729 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200730 */
731static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400732{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200733 switch (sleep_state.event) {
734 case PM_EVENT_SUSPEND:
735 return PMSG_RESUME;
736 case PM_EVENT_FREEZE:
737 case PM_EVENT_QUIESCE:
738 return PMSG_RECOVER;
739 case PM_EVENT_HIBERNATE:
740 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400741 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200742 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400743}
744
745/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200746 * device_suspend_noirq - Execute a "late suspend" callback for given device.
747 * @dev: Device to handle.
748 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100749 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200750 * The driver of @dev will not receive interrupts while this function is being
751 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100752 */
Alan Sternd1616302009-05-24 22:05:42 +0200753static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100754{
755 int error = 0;
756
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100757 if (dev->class && dev->class->pm) {
758 pm_dev_dbg(dev, state, "LATE class ");
759 error = pm_noirq_op(dev, dev->class->pm, state);
760 if (error)
761 goto End;
762 }
763
764 if (dev->type && dev->type->pm) {
765 pm_dev_dbg(dev, state, "LATE type ");
766 error = pm_noirq_op(dev, dev->type->pm, state);
767 if (error)
768 goto End;
769 }
770
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100771 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200772 pm_dev_dbg(dev, state, "LATE ");
773 error = pm_noirq_op(dev, dev->bus->pm, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100774 }
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100775
776End:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100777 return error;
778}
779
780/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200781 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
782 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100783 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200784 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
785 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100786 */
Alan Sternd1616302009-05-24 22:05:42 +0200787int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100788{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100789 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100790 int error = 0;
791
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100792 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200793 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100794 while (!list_empty(&dpm_suspended_list)) {
795 struct device *dev = to_device(dpm_suspended_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100796
797 get_device(dev);
798 mutex_unlock(&dpm_list_mtx);
799
Alan Sternd1616302009-05-24 22:05:42 +0200800 error = device_suspend_noirq(dev, state);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100801
802 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100803 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200804 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100805 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100806 break;
807 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100808 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100809 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100810 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100811 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200812 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100813 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200814 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100815 else
816 dpm_show_time(starttime, state, "late");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100817 return error;
818}
Alan Sternd1616302009-05-24 22:05:42 +0200819EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100820
821/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100822 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800823 * @dev: Device to suspend.
824 * @state: PM transition of the system being carried out.
825 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100826 */
827static int legacy_suspend(struct device *dev, pm_message_t state,
828 int (*cb)(struct device *dev, pm_message_t state))
829{
830 int error;
831 ktime_t calltime;
832
833 calltime = initcall_debug_start(dev);
834
835 error = cb(dev, state);
836 suspend_report_result(cb, error);
837
838 initcall_debug_report(dev, calltime, error);
839
840 return error;
841}
842
843/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200844 * device_suspend - Execute "suspend" callbacks for given device.
845 * @dev: Device to handle.
846 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100847 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400848 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100849static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400850{
851 int error = 0;
852
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100853 dpm_wait_for_children(dev, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800854 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100855
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100856 if (async_error)
857 goto End;
858
Rafael J. Wysockid83f9052010-12-03 23:14:26 +0100859 if (pm_wakeup_pending()) {
860 async_error = -EBUSY;
861 goto End;
862 }
863
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200864 if (dev->class) {
865 if (dev->class->pm) {
866 pm_dev_dbg(dev, state, "class ");
867 error = pm_op(dev, dev->class->pm, state);
868 } else if (dev->class->suspend) {
869 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100870 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200871 }
872 if (error)
873 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400874 }
875
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200876 if (dev->type) {
877 if (dev->type->pm) {
878 pm_dev_dbg(dev, state, "type ");
879 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200880 }
881 if (error)
882 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400883 }
884
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200885 if (dev->bus) {
886 if (dev->bus->pm) {
887 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200888 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200889 } else if (dev->bus->suspend) {
890 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100891 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200892 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400893 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100894
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200895 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800896 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100897 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100898
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200899 if (error)
900 async_error = error;
901
Alan Sterncd59abf2007-09-21 15:36:56 -0400902 return error;
903}
904
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100905static void async_suspend(void *data, async_cookie_t cookie)
906{
907 struct device *dev = (struct device *)data;
908 int error;
909
910 error = __device_suspend(dev, pm_transition, true);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200911 if (error)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100912 pm_dev_err(dev, pm_transition, " async", error);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100913
914 put_device(dev);
915}
916
917static int device_suspend(struct device *dev)
918{
919 INIT_COMPLETION(dev->power.completion);
920
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100921 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100922 get_device(dev);
923 async_schedule(async_suspend, dev);
924 return 0;
925 }
926
927 return __device_suspend(dev, pm_transition, false);
928}
929
Alan Sterncd59abf2007-09-21 15:36:56 -0400930/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200931 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
932 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400933 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100934static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400935{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100936 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400937 int error = 0;
938
Alan Sterncd59abf2007-09-21 15:36:56 -0400939 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100940 pm_transition = state;
941 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100942 while (!list_empty(&dpm_prepared_list)) {
943 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400944
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200945 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100946 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200947
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100948 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200949
Alan Stern1b3cbec2008-02-29 11:50:22 -0500950 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100951 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200952 pm_dev_err(dev, state, "", error);
953 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100954 break;
955 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100956 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100957 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200958 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100959 if (async_error)
960 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100961 }
962 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100963 async_synchronize_full();
964 if (!error)
965 error = async_error;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100966 if (!error)
967 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200968 return error;
969}
970
971/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200972 * device_prepare - Prepare a device for system power transition.
973 * @dev: Device to handle.
974 * @state: PM transition of the system being carried out.
975 *
976 * Execute the ->prepare() callback(s) for given device. No new children of the
977 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200978 */
Alan Sternd1616302009-05-24 22:05:42 +0200979static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200980{
981 int error = 0;
982
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800983 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200984
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200985 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200986 pm_dev_dbg(dev, state, "preparing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200987 error = dev->bus->pm->prepare(dev);
988 suspend_report_result(dev->bus->pm->prepare, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200989 if (error)
990 goto End;
991 }
992
993 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
994 pm_dev_dbg(dev, state, "preparing type ");
995 error = dev->type->pm->prepare(dev);
996 suspend_report_result(dev->type->pm->prepare, error);
997 if (error)
998 goto End;
999 }
1000
1001 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
1002 pm_dev_dbg(dev, state, "preparing class ");
1003 error = dev->class->pm->prepare(dev);
1004 suspend_report_result(dev->class->pm->prepare, error);
1005 }
1006 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001007 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001008
1009 return error;
1010}
1011
1012/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001013 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1014 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001015 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001016 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001017 */
1018static int dpm_prepare(pm_message_t state)
1019{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001020 int error = 0;
1021
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001022 mutex_lock(&dpm_list_mtx);
1023 transition_started = true;
1024 while (!list_empty(&dpm_list)) {
1025 struct device *dev = to_device(dpm_list.next);
1026
1027 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001028 mutex_unlock(&dpm_list_mtx);
1029
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001030 pm_runtime_get_noresume(dev);
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001031 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1032 pm_wakeup_event(dev, 0);
1033
Rafael J. Wysockia2867e02010-12-03 22:58:31 +01001034 if (pm_wakeup_pending()) {
Alan Sternaa0baae2009-12-21 02:46:11 +01001035 pm_runtime_put_sync(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001036 error = -EBUSY;
1037 } else {
1038 error = device_prepare(dev, state);
1039 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001040
1041 mutex_lock(&dpm_list_mtx);
1042 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001043 if (error == -EAGAIN) {
1044 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001045 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001046 continue;
1047 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001048 printk(KERN_INFO "PM: Device %s not prepared "
1049 "for power transition: code %d\n",
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001050 kobject_name(&dev->kobj), error);
1051 put_device(dev);
1052 break;
1053 }
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +01001054 dev->power.in_suspend = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001055 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001056 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001057 put_device(dev);
1058 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001059 mutex_unlock(&dpm_list_mtx);
1060 return error;
1061}
1062
1063/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001064 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1065 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001066 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001067 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1068 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001069 */
Alan Sternd1616302009-05-24 22:05:42 +02001070int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001071{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001072 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001073
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001074 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001075 error = dpm_prepare(state);
1076 if (!error)
1077 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001078 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001079}
Alan Sternd1616302009-05-24 22:05:42 +02001080EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001081
1082void __suspend_report_result(const char *function, void *fn, int ret)
1083{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001084 if (ret)
1085 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001086}
1087EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001088
1089/**
1090 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1091 * @dev: Device to wait for.
1092 * @subordinate: Device that needs to wait for @dev.
1093 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001094int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001095{
1096 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001097 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001098}
1099EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);