blob: c5356ef7e54ec622f5afda41b7a43795c48e248e [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>
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. Wysocki2ed8d2b32009-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>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070029
Alan Sterncd59abf2007-09-21 15:36:56 -040030#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "power.h"
32
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010033/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020034 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010035 * because children are guaranteed to be discovered after parents, and
36 * are inserted at the back of the list on discovery.
37 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080038 * Since device_pm_add() may be called with a device lock held,
39 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010040 * dpm_list_mutex.
41 */
42
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020043LIST_HEAD(dpm_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Alan Sterncd59abf2007-09-21 15:36:56 -040045static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010046static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020048/*
49 * Set once the preparation of devices for a PM transition has started, reset
50 * before starting to resume devices. Protected by dpm_list_mtx.
51 */
52static bool transition_started;
53
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020054static int async_error;
55
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020056/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020057 * device_pm_init - Initialize the PM-related part of a device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020058 * @dev: Device object being initialized.
59 */
60void device_pm_init(struct device *dev)
61{
62 dev->power.status = DPM_ON;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010063 init_completion(&dev->power.completion);
Colin Cross152e1d52010-09-03 01:24:07 +020064 complete_all(&dev->power.completion);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020065 dev->power.wakeup = NULL;
66 spin_lock_init(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020067 pm_runtime_init(dev);
68}
69
70/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020071 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020072 */
73void device_pm_lock(void)
74{
75 mutex_lock(&dpm_list_mtx);
76}
77
78/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020079 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020080 */
81void device_pm_unlock(void)
82{
83 mutex_unlock(&dpm_list_mtx);
84}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010085
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010086/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020087 * device_pm_add - Add a device to the PM core's list of active devices.
88 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010089 */
Alan Stern3b98aea2008-08-07 13:06:12 -040090void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 pr_debug("PM: Adding info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040093 dev->bus ? dev->bus->name : "No Bus",
94 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070095 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020096 if (dev->parent) {
Rafael J. Wysockif5a6d952008-08-09 01:05:13 +020097 if (dev->parent->power.status >= DPM_SUSPENDING)
98 dev_warn(dev, "parent %s should not be sleeping\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010099 dev_name(dev->parent));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200100 } else if (transition_started) {
101 /*
102 * We refuse to register parentless devices while a PM
103 * transition is in progress in order to avoid leaving them
104 * unhandled down the road
105 */
Arjan van de Ven728f0892008-09-20 19:09:00 -0700106 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100107 }
Alan Stern3b98aea2008-08-07 13:06:12 -0400108
109 list_add_tail(&dev->power.entry, &dpm_list);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700110 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100113/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200114 * device_pm_remove - Remove a device from the PM core's list of active devices.
115 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100116 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200117void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 pr_debug("PM: Removing info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -0400120 dev->bus ? dev->bus->name : "No Bus",
121 kobject_name(&dev->kobj));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100122 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700123 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700125 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200126 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200127 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100128}
129
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200130/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200131 * device_pm_move_before - Move device in the PM core's list of active devices.
132 * @deva: Device to move in dpm_list.
133 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100134 */
135void device_pm_move_before(struct device *deva, struct device *devb)
136{
137 pr_debug("PM: Moving %s:%s before %s:%s\n",
138 deva->bus ? deva->bus->name : "No Bus",
139 kobject_name(&deva->kobj),
140 devb->bus ? devb->bus->name : "No Bus",
141 kobject_name(&devb->kobj));
142 /* Delete deva from dpm_list and reinsert before devb. */
143 list_move_tail(&deva->power.entry, &devb->power.entry);
144}
145
146/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200147 * device_pm_move_after - Move device in the PM core's list of active devices.
148 * @deva: Device to move in dpm_list.
149 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100150 */
151void device_pm_move_after(struct device *deva, struct device *devb)
152{
153 pr_debug("PM: Moving %s:%s after %s:%s\n",
154 deva->bus ? deva->bus->name : "No Bus",
155 kobject_name(&deva->kobj),
156 devb->bus ? devb->bus->name : "No Bus",
157 kobject_name(&devb->kobj));
158 /* Delete deva from dpm_list and reinsert after devb. */
159 list_move(&deva->power.entry, &devb->power.entry);
160}
161
162/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200163 * device_pm_move_last - Move device to end of the PM core's list of devices.
164 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100165 */
166void device_pm_move_last(struct device *dev)
167{
168 pr_debug("PM: Moving %s:%s to end of list\n",
169 dev->bus ? dev->bus->name : "No Bus",
170 kobject_name(&dev->kobj));
171 list_move_tail(&dev->power.entry, &dpm_list);
172}
173
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100174static ktime_t initcall_debug_start(struct device *dev)
175{
176 ktime_t calltime = ktime_set(0, 0);
177
178 if (initcall_debug) {
179 pr_info("calling %s+ @ %i\n",
180 dev_name(dev), task_pid_nr(current));
181 calltime = ktime_get();
182 }
183
184 return calltime;
185}
186
187static void initcall_debug_report(struct device *dev, ktime_t calltime,
188 int error)
189{
190 ktime_t delta, rettime;
191
192 if (initcall_debug) {
193 rettime = ktime_get();
194 delta = ktime_sub(rettime, calltime);
195 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
196 error, (unsigned long long)ktime_to_ns(delta) >> 10);
197 }
198}
199
Cornelia Huckffa6a702009-03-04 12:44:00 +0100200/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100201 * dpm_wait - Wait for a PM operation to complete.
202 * @dev: Device to wait for.
203 * @async: If unset, wait only if the device's power.async_suspend flag is set.
204 */
205static void dpm_wait(struct device *dev, bool async)
206{
207 if (!dev)
208 return;
209
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100210 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100211 wait_for_completion(&dev->power.completion);
212}
213
214static int dpm_wait_fn(struct device *dev, void *async_ptr)
215{
216 dpm_wait(dev, *((bool *)async_ptr));
217 return 0;
218}
219
220static void dpm_wait_for_children(struct device *dev, bool async)
221{
222 device_for_each_child(dev, &async, dpm_wait_fn);
223}
224
225/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200226 * pm_op - Execute the PM operation appropriate for given PM event.
227 * @dev: Device to handle.
228 * @ops: PM operations to choose from.
229 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200230 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200231static int pm_op(struct device *dev,
232 const struct dev_pm_ops *ops,
233 pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200234{
235 int error = 0;
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100236 ktime_t calltime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100237
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100238 calltime = initcall_debug_start(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200239
240 switch (state.event) {
241#ifdef CONFIG_SUSPEND
242 case PM_EVENT_SUSPEND:
243 if (ops->suspend) {
244 error = ops->suspend(dev);
245 suspend_report_result(ops->suspend, error);
246 }
247 break;
248 case PM_EVENT_RESUME:
249 if (ops->resume) {
250 error = ops->resume(dev);
251 suspend_report_result(ops->resume, error);
252 }
253 break;
254#endif /* CONFIG_SUSPEND */
255#ifdef CONFIG_HIBERNATION
256 case PM_EVENT_FREEZE:
257 case PM_EVENT_QUIESCE:
258 if (ops->freeze) {
259 error = ops->freeze(dev);
260 suspend_report_result(ops->freeze, error);
261 }
262 break;
263 case PM_EVENT_HIBERNATE:
264 if (ops->poweroff) {
265 error = ops->poweroff(dev);
266 suspend_report_result(ops->poweroff, error);
267 }
268 break;
269 case PM_EVENT_THAW:
270 case PM_EVENT_RECOVER:
271 if (ops->thaw) {
272 error = ops->thaw(dev);
273 suspend_report_result(ops->thaw, error);
274 }
275 break;
276 case PM_EVENT_RESTORE:
277 if (ops->restore) {
278 error = ops->restore(dev);
279 suspend_report_result(ops->restore, error);
280 }
281 break;
282#endif /* CONFIG_HIBERNATION */
283 default:
284 error = -EINVAL;
285 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100286
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100287 initcall_debug_report(dev, calltime, error);
Arjan van de Venf2511772009-12-13 20:29:01 +0100288
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200289 return error;
290}
291
292/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200293 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
294 * @dev: Device to handle.
295 * @ops: PM operations to choose from.
296 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200297 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200298 * The driver of @dev will not receive interrupts while this function is being
299 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200300 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200301static int pm_noirq_op(struct device *dev,
302 const struct dev_pm_ops *ops,
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200303 pm_message_t state)
304{
305 int error = 0;
Arjan van de Venf2511772009-12-13 20:29:01 +0100306 ktime_t calltime, delta, rettime;
307
308 if (initcall_debug) {
Rafael J. Wysocki8cc6b392010-01-23 22:03:29 +0100309 pr_info("calling %s+ @ %i, parent: %s\n",
310 dev_name(dev), task_pid_nr(current),
311 dev->parent ? dev_name(dev->parent) : "none");
Arjan van de Venf2511772009-12-13 20:29:01 +0100312 calltime = ktime_get();
313 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200314
315 switch (state.event) {
316#ifdef CONFIG_SUSPEND
317 case PM_EVENT_SUSPEND:
318 if (ops->suspend_noirq) {
319 error = ops->suspend_noirq(dev);
320 suspend_report_result(ops->suspend_noirq, error);
321 }
322 break;
323 case PM_EVENT_RESUME:
324 if (ops->resume_noirq) {
325 error = ops->resume_noirq(dev);
326 suspend_report_result(ops->resume_noirq, error);
327 }
328 break;
329#endif /* CONFIG_SUSPEND */
330#ifdef CONFIG_HIBERNATION
331 case PM_EVENT_FREEZE:
332 case PM_EVENT_QUIESCE:
333 if (ops->freeze_noirq) {
334 error = ops->freeze_noirq(dev);
335 suspend_report_result(ops->freeze_noirq, error);
336 }
337 break;
338 case PM_EVENT_HIBERNATE:
339 if (ops->poweroff_noirq) {
340 error = ops->poweroff_noirq(dev);
341 suspend_report_result(ops->poweroff_noirq, error);
342 }
343 break;
344 case PM_EVENT_THAW:
345 case PM_EVENT_RECOVER:
346 if (ops->thaw_noirq) {
347 error = ops->thaw_noirq(dev);
348 suspend_report_result(ops->thaw_noirq, error);
349 }
350 break;
351 case PM_EVENT_RESTORE:
352 if (ops->restore_noirq) {
353 error = ops->restore_noirq(dev);
354 suspend_report_result(ops->restore_noirq, error);
355 }
356 break;
357#endif /* CONFIG_HIBERNATION */
358 default:
359 error = -EINVAL;
360 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100361
362 if (initcall_debug) {
363 rettime = ktime_get();
364 delta = ktime_sub(rettime, calltime);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100365 printk("initcall %s_i+ returned %d after %Ld usecs\n",
366 dev_name(dev), error,
367 (unsigned long long)ktime_to_ns(delta) >> 10);
Arjan van de Venf2511772009-12-13 20:29:01 +0100368 }
369
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200370 return error;
371}
372
373static char *pm_verb(int event)
374{
375 switch (event) {
376 case PM_EVENT_SUSPEND:
377 return "suspend";
378 case PM_EVENT_RESUME:
379 return "resume";
380 case PM_EVENT_FREEZE:
381 return "freeze";
382 case PM_EVENT_QUIESCE:
383 return "quiesce";
384 case PM_EVENT_HIBERNATE:
385 return "hibernate";
386 case PM_EVENT_THAW:
387 return "thaw";
388 case PM_EVENT_RESTORE:
389 return "restore";
390 case PM_EVENT_RECOVER:
391 return "recover";
392 default:
393 return "(unknown PM event)";
394 }
395}
396
397static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
398{
399 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
400 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
401 ", may wakeup" : "");
402}
403
404static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
405 int error)
406{
407 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
408 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
409}
410
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100411static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
412{
413 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200414 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100415 int usecs;
416
417 calltime = ktime_get();
418 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
419 do_div(usecs64, NSEC_PER_USEC);
420 usecs = usecs64;
421 if (usecs == 0)
422 usecs = 1;
423 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
424 info ?: "", info ? " " : "", pm_verb(state.event),
425 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
426}
427
Alan Sterncd59abf2007-09-21 15:36:56 -0400428/*------------------------- Resume routines -------------------------*/
429
430/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200431 * device_resume_noirq - Execute an "early resume" callback for given device.
432 * @dev: Device to handle.
433 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400434 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200435 * The driver of @dev will not receive interrupts while this function is being
436 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400437 */
Alan Sternd1616302009-05-24 22:05:42 +0200438static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400439{
440 int error = 0;
441
442 TRACE_DEVICE(dev);
443 TRACE_RESUME(0);
444
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100445 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200446 pm_dev_dbg(dev, state, "EARLY ");
447 error = pm_noirq_op(dev, dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100448 if (error)
449 goto End;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100450 }
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100451
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100452 if (dev->type && dev->type->pm) {
453 pm_dev_dbg(dev, state, "EARLY type ");
454 error = pm_noirq_op(dev, dev->type->pm, state);
455 if (error)
456 goto End;
457 }
458
459 if (dev->class && dev->class->pm) {
460 pm_dev_dbg(dev, state, "EARLY class ");
461 error = pm_noirq_op(dev, dev->class->pm, state);
462 }
463
464End:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100465 TRACE_RESUME(error);
466 return error;
467}
468
469/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200470 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
471 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100472 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200473 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
474 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100475 */
Alan Sternd1616302009-05-24 22:05:42 +0200476void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100477{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200478 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100479 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100480
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200481 mutex_lock(&dpm_list_mtx);
Xiaotian Feng3eb132c2009-08-20 20:28:33 +0200482 transition_started = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200483 list_for_each_entry(dev, &dpm_list, power.entry)
484 if (dev->power.status > DPM_OFF) {
485 int error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100486
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200487 dev->power.status = DPM_OFF;
Alan Sternd1616302009-05-24 22:05:42 +0200488 error = device_resume_noirq(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200489 if (error)
490 pm_dev_err(dev, state, " early", error);
491 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200492 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100493 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +0100494 resume_device_irqs();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100495}
Alan Sternd1616302009-05-24 22:05:42 +0200496EXPORT_SYMBOL_GPL(dpm_resume_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100497
498/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100499 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800500 * @dev: Device to resume.
501 * @cb: Resume callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100502 */
503static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
504{
505 int error;
506 ktime_t calltime;
507
508 calltime = initcall_debug_start(dev);
509
510 error = cb(dev);
511 suspend_report_result(cb, error);
512
513 initcall_debug_report(dev, calltime, error);
514
515 return error;
516}
517
518/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100519 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200520 * @dev: Device to handle.
521 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100522 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100523 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100524static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100525{
526 int error = 0;
527
528 TRACE_DEVICE(dev);
529 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400530
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100531 dpm_wait(dev->parent, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800532 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100533
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100534 dev->power.status = DPM_RESUMING;
535
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200536 if (dev->bus) {
537 if (dev->bus->pm) {
538 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200539 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200540 } else if (dev->bus->resume) {
541 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100542 error = legacy_resume(dev, dev->bus->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200543 }
544 if (error)
545 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400546 }
547
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200548 if (dev->type) {
549 if (dev->type->pm) {
550 pm_dev_dbg(dev, state, "type ");
551 error = pm_op(dev, dev->type->pm, state);
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->class) {
558 if (dev->class->pm) {
559 pm_dev_dbg(dev, state, "class ");
560 error = pm_op(dev, dev->class->pm, state);
561 } else if (dev->class->resume) {
562 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100563 error = legacy_resume(dev, dev->class->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200564 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400565 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200566 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800567 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100568 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100569
Alan Sterncd59abf2007-09-21 15:36:56 -0400570 TRACE_RESUME(error);
571 return error;
572}
573
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100574static void async_resume(void *data, async_cookie_t cookie)
575{
576 struct device *dev = (struct device *)data;
577 int error;
578
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100579 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100580 if (error)
581 pm_dev_err(dev, pm_transition, " async", error);
582 put_device(dev);
583}
584
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100585static bool is_async(struct device *dev)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100586{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100587 return dev->power.async_suspend && pm_async_enabled
588 && !pm_trace_is_enabled();
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100589}
590
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100591/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200592 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
593 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100594 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200595 * Execute the appropriate "resume" callback for all devices whose status
596 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400597 */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200598static void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400599{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200600 struct list_head list;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100601 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100602 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400603
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200604 INIT_LIST_HEAD(&list);
605 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100606 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200607 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200608
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100609 list_for_each_entry(dev, &dpm_list, power.entry) {
610 if (dev->power.status < DPM_OFF)
611 continue;
612
613 INIT_COMPLETION(dev->power.completion);
614 if (is_async(dev)) {
615 get_device(dev);
616 async_schedule(async_resume, dev);
617 }
618 }
619
620 while (!list_empty(&dpm_list)) {
621 dev = to_device(dpm_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200622 get_device(dev);
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100623 if (dev->power.status >= DPM_OFF && !is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200624 int error;
625
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200626 mutex_unlock(&dpm_list_mtx);
627
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100628 error = device_resume(dev, state, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200629
630 mutex_lock(&dpm_list_mtx);
631 if (error)
632 pm_dev_err(dev, state, "", error);
633 } else if (dev->power.status == DPM_SUSPENDING) {
634 /* Allow new children of the device to be registered */
635 dev->power.status = DPM_RESUMING;
636 }
637 if (!list_empty(&dev->power.entry))
638 list_move_tail(&dev->power.entry, &list);
639 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400640 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200641 list_splice(&list, &dpm_list);
642 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. Wysocki1eede072008-05-20 23:00:01 +0200688 while (!list_empty(&dpm_list)) {
689 struct device *dev = to_device(dpm_list.prev);
690
691 get_device(dev);
692 if (dev->power.status > DPM_ON) {
693 dev->power.status = DPM_ON;
694 mutex_unlock(&dpm_list_mtx);
695
Alan Sternd1616302009-05-24 22:05:42 +0200696 device_complete(dev, state);
Alan Sternaa0baae2009-12-21 02:46:11 +0100697 pm_runtime_put_sync(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200698
699 mutex_lock(&dpm_list_mtx);
700 }
701 if (!list_empty(&dev->power.entry))
702 list_move(&dev->power.entry, &list);
703 put_device(dev);
704 }
705 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400706 mutex_unlock(&dpm_list_mtx);
707}
708
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100709/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200710 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
711 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400712 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200713 * Execute "resume" callbacks for all devices and complete the PM transition of
714 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400715 */
Alan Sternd1616302009-05-24 22:05:42 +0200716void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400717{
718 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200719 dpm_resume(state);
720 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400721}
Alan Sternd1616302009-05-24 22:05:42 +0200722EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400723
724
Alan Sterncd59abf2007-09-21 15:36:56 -0400725/*------------------------- Suspend routines -------------------------*/
726
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200727/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200728 * resume_event - Return a "resume" message for given "suspend" sleep state.
729 * @sleep_state: PM message representing a sleep state.
730 *
731 * Return a PM message representing the resume event corresponding to given
732 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200733 */
734static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400735{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200736 switch (sleep_state.event) {
737 case PM_EVENT_SUSPEND:
738 return PMSG_RESUME;
739 case PM_EVENT_FREEZE:
740 case PM_EVENT_QUIESCE:
741 return PMSG_RECOVER;
742 case PM_EVENT_HIBERNATE:
743 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400744 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200745 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400746}
747
748/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200749 * device_suspend_noirq - Execute a "late suspend" callback for given device.
750 * @dev: Device to handle.
751 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100752 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200753 * The driver of @dev will not receive interrupts while this function is being
754 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100755 */
Alan Sternd1616302009-05-24 22:05:42 +0200756static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100757{
758 int error = 0;
759
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100760 if (dev->class && dev->class->pm) {
761 pm_dev_dbg(dev, state, "LATE class ");
762 error = pm_noirq_op(dev, dev->class->pm, state);
763 if (error)
764 goto End;
765 }
766
767 if (dev->type && dev->type->pm) {
768 pm_dev_dbg(dev, state, "LATE type ");
769 error = pm_noirq_op(dev, dev->type->pm, state);
770 if (error)
771 goto End;
772 }
773
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100774 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200775 pm_dev_dbg(dev, state, "LATE ");
776 error = pm_noirq_op(dev, dev->bus->pm, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100777 }
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100778
779End:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100780 return error;
781}
782
783/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200784 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
785 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100786 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200787 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
788 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100789 */
Alan Sternd1616302009-05-24 22:05:42 +0200790int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100791{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200792 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100793 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100794 int error = 0;
795
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +0100796 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200797 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200798 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
Alan Sternd1616302009-05-24 22:05:42 +0200799 error = device_suspend_noirq(dev, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100800 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200801 pm_dev_err(dev, state, " late", error);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100802 break;
803 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200804 dev->power.status = DPM_OFF_IRQ;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100805 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200806 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100807 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200808 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100809 else
810 dpm_show_time(starttime, state, "late");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100811 return error;
812}
Alan Sternd1616302009-05-24 22:05:42 +0200813EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100814
815/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100816 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800817 * @dev: Device to suspend.
818 * @state: PM transition of the system being carried out.
819 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100820 */
821static int legacy_suspend(struct device *dev, pm_message_t state,
822 int (*cb)(struct device *dev, pm_message_t state))
823{
824 int error;
825 ktime_t calltime;
826
827 calltime = initcall_debug_start(dev);
828
829 error = cb(dev, state);
830 suspend_report_result(cb, error);
831
832 initcall_debug_report(dev, calltime, error);
833
834 return error;
835}
836
837/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200838 * device_suspend - Execute "suspend" callbacks for given device.
839 * @dev: Device to handle.
840 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100841 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400842 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100843static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400844{
845 int error = 0;
846
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100847 dpm_wait_for_children(dev, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800848 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100849
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100850 if (async_error)
851 goto End;
852
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200853 if (dev->class) {
854 if (dev->class->pm) {
855 pm_dev_dbg(dev, state, "class ");
856 error = pm_op(dev, dev->class->pm, state);
857 } else if (dev->class->suspend) {
858 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100859 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200860 }
861 if (error)
862 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400863 }
864
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200865 if (dev->type) {
866 if (dev->type->pm) {
867 pm_dev_dbg(dev, state, "type ");
868 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200869 }
870 if (error)
871 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400872 }
873
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200874 if (dev->bus) {
875 if (dev->bus->pm) {
876 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200877 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200878 } else if (dev->bus->suspend) {
879 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100880 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200881 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400882 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100883
884 if (!error)
885 dev->power.status = DPM_OFF;
886
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200887 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800888 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100889 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100890
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200891 if (error)
892 async_error = error;
893
Alan Sterncd59abf2007-09-21 15:36:56 -0400894 return error;
895}
896
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100897static void async_suspend(void *data, async_cookie_t cookie)
898{
899 struct device *dev = (struct device *)data;
900 int error;
901
902 error = __device_suspend(dev, pm_transition, true);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200903 if (error)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100904 pm_dev_err(dev, pm_transition, " async", error);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100905
906 put_device(dev);
907}
908
909static int device_suspend(struct device *dev)
910{
911 INIT_COMPLETION(dev->power.completion);
912
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100913 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100914 get_device(dev);
915 async_schedule(async_suspend, dev);
916 return 0;
917 }
918
919 return __device_suspend(dev, pm_transition, false);
920}
921
Alan Sterncd59abf2007-09-21 15:36:56 -0400922/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200923 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
924 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400925 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100926static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400927{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200928 struct list_head list;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100929 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400930 int error = 0;
931
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200932 INIT_LIST_HEAD(&list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400933 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100934 pm_transition = state;
935 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200936 while (!list_empty(&dpm_list)) {
937 struct device *dev = to_device(dpm_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400938
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200939 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100940 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200941
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100942 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200943
Alan Stern1b3cbec2008-02-29 11:50:22 -0500944 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100945 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200946 pm_dev_err(dev, state, "", error);
947 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100948 break;
949 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100950 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200951 list_move(&dev->power.entry, &list);
952 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100953 if (async_error)
954 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100955 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200956 list_splice(&list, dpm_list.prev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100957 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100958 async_synchronize_full();
959 if (!error)
960 error = async_error;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100961 if (!error)
962 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200963 return error;
964}
965
966/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200967 * device_prepare - Prepare a device for system power transition.
968 * @dev: Device to handle.
969 * @state: PM transition of the system being carried out.
970 *
971 * Execute the ->prepare() callback(s) for given device. No new children of the
972 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200973 */
Alan Sternd1616302009-05-24 22:05:42 +0200974static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200975{
976 int error = 0;
977
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800978 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200979
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200980 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200981 pm_dev_dbg(dev, state, "preparing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200982 error = dev->bus->pm->prepare(dev);
983 suspend_report_result(dev->bus->pm->prepare, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200984 if (error)
985 goto End;
986 }
987
988 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
989 pm_dev_dbg(dev, state, "preparing type ");
990 error = dev->type->pm->prepare(dev);
991 suspend_report_result(dev->type->pm->prepare, error);
992 if (error)
993 goto End;
994 }
995
996 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
997 pm_dev_dbg(dev, state, "preparing class ");
998 error = dev->class->pm->prepare(dev);
999 suspend_report_result(dev->class->pm->prepare, error);
1000 }
1001 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001002 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001003
1004 return error;
1005}
1006
1007/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001008 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1009 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001010 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001011 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001012 */
1013static int dpm_prepare(pm_message_t state)
1014{
1015 struct list_head list;
1016 int error = 0;
1017
1018 INIT_LIST_HEAD(&list);
1019 mutex_lock(&dpm_list_mtx);
1020 transition_started = true;
1021 while (!list_empty(&dpm_list)) {
1022 struct device *dev = to_device(dpm_list.next);
1023
1024 get_device(dev);
1025 dev->power.status = DPM_PREPARING;
1026 mutex_unlock(&dpm_list_mtx);
1027
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001028 pm_runtime_get_noresume(dev);
1029 if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
1030 /* Wake-up requested during system sleep transition. */
Alan Sternaa0baae2009-12-21 02:46:11 +01001031 pm_runtime_put_sync(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001032 error = -EBUSY;
1033 } else {
1034 error = device_prepare(dev, state);
1035 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001036
1037 mutex_lock(&dpm_list_mtx);
1038 if (error) {
1039 dev->power.status = DPM_ON;
1040 if (error == -EAGAIN) {
1041 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001042 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001043 continue;
1044 }
1045 printk(KERN_ERR "PM: Failed to prepare device %s "
1046 "for power transition: error %d\n",
1047 kobject_name(&dev->kobj), error);
1048 put_device(dev);
1049 break;
1050 }
1051 dev->power.status = DPM_SUSPENDING;
1052 if (!list_empty(&dev->power.entry))
1053 list_move_tail(&dev->power.entry, &list);
1054 put_device(dev);
1055 }
1056 list_splice(&list, &dpm_list);
1057 mutex_unlock(&dpm_list_mtx);
1058 return error;
1059}
1060
1061/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001062 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1063 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001064 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001065 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1066 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001067 */
Alan Sternd1616302009-05-24 22:05:42 +02001068int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001069{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001070 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001071
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001072 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001073 error = dpm_prepare(state);
1074 if (!error)
1075 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001076 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001077}
Alan Sternd1616302009-05-24 22:05:42 +02001078EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001079
1080void __suspend_report_result(const char *function, void *fn, int ret)
1081{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001082 if (ret)
1083 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001084}
1085EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001086
1087/**
1088 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1089 * @dev: Device to wait for.
1090 * @subordinate: Device that needs to wait for @dev.
1091 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001092int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001093{
1094 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001095 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001096}
1097EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);