blob: 8a5258339ca2c45d6d097d4a14ab1cfd721694b2 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Alan Sterncd59abf2007-09-21 15:36:56 -040046static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010047static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020049/*
50 * Set once the preparation of devices for a PM transition has started, reset
51 * before starting to resume devices. Protected by dpm_list_mtx.
52 */
53static bool transition_started;
54
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020055static int async_error;
56
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020057/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020058 * device_pm_init - Initialize the PM-related part of a device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020059 * @dev: Device object being initialized.
60 */
61void device_pm_init(struct device *dev)
62{
63 dev->power.status = DPM_ON;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010064 init_completion(&dev->power.completion);
Colin Cross152e1d52010-09-03 01:24:07 +020065 complete_all(&dev->power.completion);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020066 dev->power.wakeup = NULL;
67 spin_lock_init(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020068 pm_runtime_init(dev);
69}
70
71/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020072 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020073 */
74void device_pm_lock(void)
75{
76 mutex_lock(&dpm_list_mtx);
77}
78
79/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020080 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020081 */
82void device_pm_unlock(void)
83{
84 mutex_unlock(&dpm_list_mtx);
85}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010086
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010087/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020088 * device_pm_add - Add a device to the PM core's list of active devices.
89 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010090 */
Alan Stern3b98aea2008-08-07 13:06:12 -040091void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pr_debug("PM: Adding info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040094 dev->bus ? dev->bus->name : "No Bus",
95 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070096 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020097 if (dev->parent) {
Rafael J. Wysockif5a6d952008-08-09 01:05:13 +020098 if (dev->parent->power.status >= DPM_SUSPENDING)
99 dev_warn(dev, "parent %s should not be sleeping\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100100 dev_name(dev->parent));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200101 } else if (transition_started) {
102 /*
103 * We refuse to register parentless devices while a PM
104 * transition is in progress in order to avoid leaving them
105 * unhandled down the road
106 */
Arjan van de Ven728f0892008-09-20 19:09:00 -0700107 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100108 }
Alan Stern3b98aea2008-08-07 13:06:12 -0400109
110 list_add_tail(&dev->power.entry, &dpm_list);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700111 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100114/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200115 * device_pm_remove - Remove a device from the PM core's list of active devices.
116 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100117 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200118void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 pr_debug("PM: Removing info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -0400121 dev->bus ? dev->bus->name : "No Bus",
122 kobject_name(&dev->kobj));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100123 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700124 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700126 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200127 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200128 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100129}
130
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200131/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200132 * device_pm_move_before - Move device in the PM core's list of active devices.
133 * @deva: Device to move in dpm_list.
134 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100135 */
136void device_pm_move_before(struct device *deva, struct device *devb)
137{
138 pr_debug("PM: Moving %s:%s before %s:%s\n",
139 deva->bus ? deva->bus->name : "No Bus",
140 kobject_name(&deva->kobj),
141 devb->bus ? devb->bus->name : "No Bus",
142 kobject_name(&devb->kobj));
143 /* Delete deva from dpm_list and reinsert before devb. */
144 list_move_tail(&deva->power.entry, &devb->power.entry);
145}
146
147/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200148 * device_pm_move_after - Move device in the PM core's list of active devices.
149 * @deva: Device to move in dpm_list.
150 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100151 */
152void device_pm_move_after(struct device *deva, struct device *devb)
153{
154 pr_debug("PM: Moving %s:%s after %s:%s\n",
155 deva->bus ? deva->bus->name : "No Bus",
156 kobject_name(&deva->kobj),
157 devb->bus ? devb->bus->name : "No Bus",
158 kobject_name(&devb->kobj));
159 /* Delete deva from dpm_list and reinsert after devb. */
160 list_move(&deva->power.entry, &devb->power.entry);
161}
162
163/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200164 * device_pm_move_last - Move device to end of the PM core's list of devices.
165 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100166 */
167void device_pm_move_last(struct device *dev)
168{
169 pr_debug("PM: Moving %s:%s to end of list\n",
170 dev->bus ? dev->bus->name : "No Bus",
171 kobject_name(&dev->kobj));
172 list_move_tail(&dev->power.entry, &dpm_list);
173}
174
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100175static ktime_t initcall_debug_start(struct device *dev)
176{
177 ktime_t calltime = ktime_set(0, 0);
178
179 if (initcall_debug) {
180 pr_info("calling %s+ @ %i\n",
181 dev_name(dev), task_pid_nr(current));
182 calltime = ktime_get();
183 }
184
185 return calltime;
186}
187
188static void initcall_debug_report(struct device *dev, ktime_t calltime,
189 int error)
190{
191 ktime_t delta, rettime;
192
193 if (initcall_debug) {
194 rettime = ktime_get();
195 delta = ktime_sub(rettime, calltime);
196 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
197 error, (unsigned long long)ktime_to_ns(delta) >> 10);
198 }
199}
200
Cornelia Huckffa6a702009-03-04 12:44:00 +0100201/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100202 * dpm_wait - Wait for a PM operation to complete.
203 * @dev: Device to wait for.
204 * @async: If unset, wait only if the device's power.async_suspend flag is set.
205 */
206static void dpm_wait(struct device *dev, bool async)
207{
208 if (!dev)
209 return;
210
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100211 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100212 wait_for_completion(&dev->power.completion);
213}
214
215static int dpm_wait_fn(struct device *dev, void *async_ptr)
216{
217 dpm_wait(dev, *((bool *)async_ptr));
218 return 0;
219}
220
221static void dpm_wait_for_children(struct device *dev, bool async)
222{
223 device_for_each_child(dev, &async, dpm_wait_fn);
224}
225
226/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200227 * pm_op - Execute the PM operation appropriate for given PM event.
228 * @dev: Device to handle.
229 * @ops: PM operations to choose from.
230 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200231 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200232static int pm_op(struct device *dev,
233 const struct dev_pm_ops *ops,
234 pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200235{
236 int error = 0;
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100237 ktime_t calltime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100238
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100239 calltime = initcall_debug_start(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200240
241 switch (state.event) {
242#ifdef CONFIG_SUSPEND
243 case PM_EVENT_SUSPEND:
244 if (ops->suspend) {
245 error = ops->suspend(dev);
246 suspend_report_result(ops->suspend, error);
247 }
248 break;
249 case PM_EVENT_RESUME:
250 if (ops->resume) {
251 error = ops->resume(dev);
252 suspend_report_result(ops->resume, error);
253 }
254 break;
255#endif /* CONFIG_SUSPEND */
256#ifdef CONFIG_HIBERNATION
257 case PM_EVENT_FREEZE:
258 case PM_EVENT_QUIESCE:
259 if (ops->freeze) {
260 error = ops->freeze(dev);
261 suspend_report_result(ops->freeze, error);
262 }
263 break;
264 case PM_EVENT_HIBERNATE:
265 if (ops->poweroff) {
266 error = ops->poweroff(dev);
267 suspend_report_result(ops->poweroff, error);
268 }
269 break;
270 case PM_EVENT_THAW:
271 case PM_EVENT_RECOVER:
272 if (ops->thaw) {
273 error = ops->thaw(dev);
274 suspend_report_result(ops->thaw, error);
275 }
276 break;
277 case PM_EVENT_RESTORE:
278 if (ops->restore) {
279 error = ops->restore(dev);
280 suspend_report_result(ops->restore, error);
281 }
282 break;
283#endif /* CONFIG_HIBERNATION */
284 default:
285 error = -EINVAL;
286 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100287
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100288 initcall_debug_report(dev, calltime, error);
Arjan van de Venf2511772009-12-13 20:29:01 +0100289
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200290 return error;
291}
292
293/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200294 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
295 * @dev: Device to handle.
296 * @ops: PM operations to choose from.
297 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200298 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200299 * The driver of @dev will not receive interrupts while this function is being
300 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200301 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200302static int pm_noirq_op(struct device *dev,
303 const struct dev_pm_ops *ops,
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200304 pm_message_t state)
305{
306 int error = 0;
Arjan van de Venf2511772009-12-13 20:29:01 +0100307 ktime_t calltime, delta, rettime;
308
309 if (initcall_debug) {
Rafael J. Wysocki8cc6b392010-01-23 22:03:29 +0100310 pr_info("calling %s+ @ %i, parent: %s\n",
311 dev_name(dev), task_pid_nr(current),
312 dev->parent ? dev_name(dev->parent) : "none");
Arjan van de Venf2511772009-12-13 20:29:01 +0100313 calltime = ktime_get();
314 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200315
316 switch (state.event) {
317#ifdef CONFIG_SUSPEND
318 case PM_EVENT_SUSPEND:
319 if (ops->suspend_noirq) {
320 error = ops->suspend_noirq(dev);
321 suspend_report_result(ops->suspend_noirq, error);
322 }
323 break;
324 case PM_EVENT_RESUME:
325 if (ops->resume_noirq) {
326 error = ops->resume_noirq(dev);
327 suspend_report_result(ops->resume_noirq, error);
328 }
329 break;
330#endif /* CONFIG_SUSPEND */
331#ifdef CONFIG_HIBERNATION
332 case PM_EVENT_FREEZE:
333 case PM_EVENT_QUIESCE:
334 if (ops->freeze_noirq) {
335 error = ops->freeze_noirq(dev);
336 suspend_report_result(ops->freeze_noirq, error);
337 }
338 break;
339 case PM_EVENT_HIBERNATE:
340 if (ops->poweroff_noirq) {
341 error = ops->poweroff_noirq(dev);
342 suspend_report_result(ops->poweroff_noirq, error);
343 }
344 break;
345 case PM_EVENT_THAW:
346 case PM_EVENT_RECOVER:
347 if (ops->thaw_noirq) {
348 error = ops->thaw_noirq(dev);
349 suspend_report_result(ops->thaw_noirq, error);
350 }
351 break;
352 case PM_EVENT_RESTORE:
353 if (ops->restore_noirq) {
354 error = ops->restore_noirq(dev);
355 suspend_report_result(ops->restore_noirq, error);
356 }
357 break;
358#endif /* CONFIG_HIBERNATION */
359 default:
360 error = -EINVAL;
361 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100362
363 if (initcall_debug) {
364 rettime = ktime_get();
365 delta = ktime_sub(rettime, calltime);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100366 printk("initcall %s_i+ returned %d after %Ld usecs\n",
367 dev_name(dev), error,
368 (unsigned long long)ktime_to_ns(delta) >> 10);
Arjan van de Venf2511772009-12-13 20:29:01 +0100369 }
370
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200371 return error;
372}
373
374static char *pm_verb(int event)
375{
376 switch (event) {
377 case PM_EVENT_SUSPEND:
378 return "suspend";
379 case PM_EVENT_RESUME:
380 return "resume";
381 case PM_EVENT_FREEZE:
382 return "freeze";
383 case PM_EVENT_QUIESCE:
384 return "quiesce";
385 case PM_EVENT_HIBERNATE:
386 return "hibernate";
387 case PM_EVENT_THAW:
388 return "thaw";
389 case PM_EVENT_RESTORE:
390 return "restore";
391 case PM_EVENT_RECOVER:
392 return "recover";
393 default:
394 return "(unknown PM event)";
395 }
396}
397
398static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
399{
400 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
401 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
402 ", may wakeup" : "");
403}
404
405static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
406 int error)
407{
408 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
409 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
410}
411
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100412static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
413{
414 ktime_t calltime;
Kevin Cernekee0702d9e2010-09-20 22:32:10 +0200415 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100416 int usecs;
417
418 calltime = ktime_get();
419 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
420 do_div(usecs64, NSEC_PER_USEC);
421 usecs = usecs64;
422 if (usecs == 0)
423 usecs = 1;
424 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
425 info ?: "", info ? " " : "", pm_verb(state.event),
426 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
427}
428
Alan Sterncd59abf2007-09-21 15:36:56 -0400429/*------------------------- Resume routines -------------------------*/
430
431/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200432 * device_resume_noirq - Execute an "early resume" callback for given device.
433 * @dev: Device to handle.
434 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400435 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200436 * The driver of @dev will not receive interrupts while this function is being
437 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400438 */
Alan Sternd1616302009-05-24 22:05:42 +0200439static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400440{
441 int error = 0;
442
443 TRACE_DEVICE(dev);
444 TRACE_RESUME(0);
445
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100446 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200447 pm_dev_dbg(dev, state, "EARLY ");
448 error = pm_noirq_op(dev, dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100449 if (error)
450 goto End;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100451 }
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100452
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100453 if (dev->type && dev->type->pm) {
454 pm_dev_dbg(dev, state, "EARLY type ");
455 error = pm_noirq_op(dev, dev->type->pm, state);
456 if (error)
457 goto End;
458 }
459
460 if (dev->class && dev->class->pm) {
461 pm_dev_dbg(dev, state, "EARLY class ");
462 error = pm_noirq_op(dev, dev->class->pm, state);
463 }
464
465End:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100466 TRACE_RESUME(error);
467 return error;
468}
469
470/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200471 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
472 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100473 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200474 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
475 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100476 */
Alan Sternd1616302009-05-24 22:05:42 +0200477void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100478{
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100479 struct list_head list;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100480 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100481
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100482 INIT_LIST_HEAD(&list);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200483 mutex_lock(&dpm_list_mtx);
Xiaotian Feng3eb132c2009-08-20 20:28:33 +0200484 transition_started = false;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100485 while (!list_empty(&dpm_list)) {
486 struct device *dev = to_device(dpm_list.next);
487
488 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200489 if (dev->power.status > DPM_OFF) {
490 int error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100491
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200492 dev->power.status = DPM_OFF;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100493 mutex_unlock(&dpm_list_mtx);
494
Alan Sternd1616302009-05-24 22:05:42 +0200495 error = device_resume_noirq(dev, state);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100496
497 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200498 if (error)
499 pm_dev_err(dev, state, " early", error);
500 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100501 if (!list_empty(&dev->power.entry))
502 list_move_tail(&dev->power.entry, &list);
503 put_device(dev);
504 }
505 list_splice(&list, &dpm_list);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200506 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100507 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100508 resume_device_irqs();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100509}
Alan Sternd1616302009-05-24 22:05:42 +0200510EXPORT_SYMBOL_GPL(dpm_resume_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100511
512/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100513 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800514 * @dev: Device to resume.
515 * @cb: Resume callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100516 */
517static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
518{
519 int error;
520 ktime_t calltime;
521
522 calltime = initcall_debug_start(dev);
523
524 error = cb(dev);
525 suspend_report_result(cb, error);
526
527 initcall_debug_report(dev, calltime, error);
528
529 return error;
530}
531
532/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100533 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200534 * @dev: Device to handle.
535 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100536 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100537 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100538static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100539{
540 int error = 0;
541
542 TRACE_DEVICE(dev);
543 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400544
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100545 dpm_wait(dev->parent, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800546 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100547
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100548 dev->power.status = DPM_RESUMING;
549
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200550 if (dev->bus) {
551 if (dev->bus->pm) {
552 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200553 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200554 } else if (dev->bus->resume) {
555 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100556 error = legacy_resume(dev, dev->bus->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200557 }
558 if (error)
559 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400560 }
561
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200562 if (dev->type) {
563 if (dev->type->pm) {
564 pm_dev_dbg(dev, state, "type ");
565 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200566 }
567 if (error)
568 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400569 }
570
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200571 if (dev->class) {
572 if (dev->class->pm) {
573 pm_dev_dbg(dev, state, "class ");
574 error = pm_op(dev, dev->class->pm, state);
575 } else if (dev->class->resume) {
576 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100577 error = legacy_resume(dev, dev->class->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200578 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400579 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200580 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800581 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100582 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100583
Alan Sterncd59abf2007-09-21 15:36:56 -0400584 TRACE_RESUME(error);
585 return error;
586}
587
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100588static void async_resume(void *data, async_cookie_t cookie)
589{
590 struct device *dev = (struct device *)data;
591 int error;
592
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100593 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100594 if (error)
595 pm_dev_err(dev, pm_transition, " async", error);
596 put_device(dev);
597}
598
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100599static bool is_async(struct device *dev)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100600{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100601 return dev->power.async_suspend && pm_async_enabled
602 && !pm_trace_is_enabled();
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100603}
604
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100605/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200606 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
607 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100608 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200609 * Execute the appropriate "resume" callback for all devices whose status
610 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400611 */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200612static void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400613{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200614 struct list_head list;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100615 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100616 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400617
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200618 INIT_LIST_HEAD(&list);
619 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100620 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200621 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200622
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100623 list_for_each_entry(dev, &dpm_list, power.entry) {
624 if (dev->power.status < DPM_OFF)
625 continue;
626
627 INIT_COMPLETION(dev->power.completion);
628 if (is_async(dev)) {
629 get_device(dev);
630 async_schedule(async_resume, dev);
631 }
632 }
633
634 while (!list_empty(&dpm_list)) {
635 dev = to_device(dpm_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200636 get_device(dev);
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100637 if (dev->power.status >= DPM_OFF && !is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200638 int error;
639
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200640 mutex_unlock(&dpm_list_mtx);
641
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100642 error = device_resume(dev, state, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200643
644 mutex_lock(&dpm_list_mtx);
645 if (error)
646 pm_dev_err(dev, state, "", error);
647 } else if (dev->power.status == DPM_SUSPENDING) {
648 /* Allow new children of the device to be registered */
649 dev->power.status = DPM_RESUMING;
650 }
651 if (!list_empty(&dev->power.entry))
652 list_move_tail(&dev->power.entry, &list);
653 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400654 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200655 list_splice(&list, &dpm_list);
656 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100657 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100658 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200659}
660
661/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200662 * device_complete - Complete a PM transition for given device.
663 * @dev: Device to handle.
664 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200665 */
Alan Sternd1616302009-05-24 22:05:42 +0200666static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200667{
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800668 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200669
670 if (dev->class && dev->class->pm && dev->class->pm->complete) {
671 pm_dev_dbg(dev, state, "completing class ");
672 dev->class->pm->complete(dev);
673 }
674
675 if (dev->type && dev->type->pm && dev->type->pm->complete) {
676 pm_dev_dbg(dev, state, "completing type ");
677 dev->type->pm->complete(dev);
678 }
679
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200680 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200681 pm_dev_dbg(dev, state, "completing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200682 dev->bus->pm->complete(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200683 }
684
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800685 device_unlock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200686}
687
688/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200689 * dpm_complete - Complete a PM transition for all non-sysdev devices.
690 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200691 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200692 * Execute the ->complete() callbacks for all devices whose PM status is not
693 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200694 */
695static void dpm_complete(pm_message_t state)
696{
697 struct list_head list;
698
699 INIT_LIST_HEAD(&list);
700 mutex_lock(&dpm_list_mtx);
Romit Dasguptae528e872009-10-28 22:56:02 +0100701 transition_started = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200702 while (!list_empty(&dpm_list)) {
703 struct device *dev = to_device(dpm_list.prev);
704
705 get_device(dev);
706 if (dev->power.status > DPM_ON) {
707 dev->power.status = DPM_ON;
708 mutex_unlock(&dpm_list_mtx);
709
Alan Sternd1616302009-05-24 22:05:42 +0200710 device_complete(dev, state);
Alan Sternaa0baae2009-12-21 02:46:11 +0100711 pm_runtime_put_sync(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200712
713 mutex_lock(&dpm_list_mtx);
714 }
715 if (!list_empty(&dev->power.entry))
716 list_move(&dev->power.entry, &list);
717 put_device(dev);
718 }
719 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400720 mutex_unlock(&dpm_list_mtx);
721}
722
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100723/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200724 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
725 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400726 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200727 * Execute "resume" callbacks for all devices and complete the PM transition of
728 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400729 */
Alan Sternd1616302009-05-24 22:05:42 +0200730void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400731{
732 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200733 dpm_resume(state);
734 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400735}
Alan Sternd1616302009-05-24 22:05:42 +0200736EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400737
738
Alan Sterncd59abf2007-09-21 15:36:56 -0400739/*------------------------- Suspend routines -------------------------*/
740
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200741/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200742 * resume_event - Return a "resume" message for given "suspend" sleep state.
743 * @sleep_state: PM message representing a sleep state.
744 *
745 * Return a PM message representing the resume event corresponding to given
746 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200747 */
748static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400749{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200750 switch (sleep_state.event) {
751 case PM_EVENT_SUSPEND:
752 return PMSG_RESUME;
753 case PM_EVENT_FREEZE:
754 case PM_EVENT_QUIESCE:
755 return PMSG_RECOVER;
756 case PM_EVENT_HIBERNATE:
757 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400758 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200759 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400760}
761
762/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200763 * device_suspend_noirq - Execute a "late suspend" callback for given device.
764 * @dev: Device to handle.
765 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100766 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200767 * The driver of @dev will not receive interrupts while this function is being
768 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100769 */
Alan Sternd1616302009-05-24 22:05:42 +0200770static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100771{
772 int error = 0;
773
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100774 if (dev->class && dev->class->pm) {
775 pm_dev_dbg(dev, state, "LATE class ");
776 error = pm_noirq_op(dev, dev->class->pm, state);
777 if (error)
778 goto End;
779 }
780
781 if (dev->type && dev->type->pm) {
782 pm_dev_dbg(dev, state, "LATE type ");
783 error = pm_noirq_op(dev, dev->type->pm, state);
784 if (error)
785 goto End;
786 }
787
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100788 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200789 pm_dev_dbg(dev, state, "LATE ");
790 error = pm_noirq_op(dev, dev->bus->pm, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100791 }
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100792
793End:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100794 return error;
795}
796
797/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200798 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
799 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100800 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200801 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
802 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100803 */
Alan Sternd1616302009-05-24 22:05:42 +0200804int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100805{
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100806 struct list_head list;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100807 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100808 int error = 0;
809
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100810 INIT_LIST_HEAD(&list);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100811 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200812 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100813 while (!list_empty(&dpm_list)) {
814 struct device *dev = to_device(dpm_list.prev);
815
816 get_device(dev);
817 mutex_unlock(&dpm_list_mtx);
818
Alan Sternd1616302009-05-24 22:05:42 +0200819 error = device_suspend_noirq(dev, state);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100820
821 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100822 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200823 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100824 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100825 break;
826 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200827 dev->power.status = DPM_OFF_IRQ;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100828 if (!list_empty(&dev->power.entry))
829 list_move(&dev->power.entry, &list);
830 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100831 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100832 list_splice_tail(&list, &dpm_list);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200833 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100834 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200835 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100836 else
837 dpm_show_time(starttime, state, "late");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100838 return error;
839}
Alan Sternd1616302009-05-24 22:05:42 +0200840EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100841
842/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100843 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800844 * @dev: Device to suspend.
845 * @state: PM transition of the system being carried out.
846 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100847 */
848static int legacy_suspend(struct device *dev, pm_message_t state,
849 int (*cb)(struct device *dev, pm_message_t state))
850{
851 int error;
852 ktime_t calltime;
853
854 calltime = initcall_debug_start(dev);
855
856 error = cb(dev, state);
857 suspend_report_result(cb, error);
858
859 initcall_debug_report(dev, calltime, error);
860
861 return error;
862}
863
864/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200865 * device_suspend - Execute "suspend" callbacks for given device.
866 * @dev: Device to handle.
867 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100868 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400869 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100870static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400871{
872 int error = 0;
873
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100874 dpm_wait_for_children(dev, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800875 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100876
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100877 if (async_error)
878 goto End;
879
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200880 if (dev->class) {
881 if (dev->class->pm) {
882 pm_dev_dbg(dev, state, "class ");
883 error = pm_op(dev, dev->class->pm, state);
884 } else if (dev->class->suspend) {
885 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100886 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200887 }
888 if (error)
889 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400890 }
891
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200892 if (dev->type) {
893 if (dev->type->pm) {
894 pm_dev_dbg(dev, state, "type ");
895 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200896 }
897 if (error)
898 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400899 }
900
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200901 if (dev->bus) {
902 if (dev->bus->pm) {
903 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200904 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200905 } else if (dev->bus->suspend) {
906 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100907 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200908 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400909 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100910
911 if (!error)
912 dev->power.status = DPM_OFF;
913
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200914 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800915 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100916 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100917
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200918 if (error)
919 async_error = error;
920
Alan Sterncd59abf2007-09-21 15:36:56 -0400921 return error;
922}
923
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100924static void async_suspend(void *data, async_cookie_t cookie)
925{
926 struct device *dev = (struct device *)data;
927 int error;
928
929 error = __device_suspend(dev, pm_transition, true);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200930 if (error)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100931 pm_dev_err(dev, pm_transition, " async", error);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100932
933 put_device(dev);
934}
935
936static int device_suspend(struct device *dev)
937{
938 INIT_COMPLETION(dev->power.completion);
939
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100940 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100941 get_device(dev);
942 async_schedule(async_suspend, dev);
943 return 0;
944 }
945
946 return __device_suspend(dev, pm_transition, false);
947}
948
Alan Sterncd59abf2007-09-21 15:36:56 -0400949/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200950 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
951 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400952 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100953static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400954{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200955 struct list_head list;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100956 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400957 int error = 0;
958
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200959 INIT_LIST_HEAD(&list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400960 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100961 pm_transition = state;
962 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200963 while (!list_empty(&dpm_list)) {
964 struct device *dev = to_device(dpm_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400965
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200966 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100967 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200968
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100969 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200970
Alan Stern1b3cbec2008-02-29 11:50:22 -0500971 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100972 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200973 pm_dev_err(dev, state, "", error);
974 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100975 break;
976 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100977 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200978 list_move(&dev->power.entry, &list);
979 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100980 if (async_error)
981 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100982 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200983 list_splice(&list, dpm_list.prev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100984 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100985 async_synchronize_full();
986 if (!error)
987 error = async_error;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100988 if (!error)
989 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200990 return error;
991}
992
993/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200994 * device_prepare - Prepare a device for system power transition.
995 * @dev: Device to handle.
996 * @state: PM transition of the system being carried out.
997 *
998 * Execute the ->prepare() callback(s) for given device. No new children of the
999 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001000 */
Alan Sternd1616302009-05-24 22:05:42 +02001001static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001002{
1003 int error = 0;
1004
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001005 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001006
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001007 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001008 pm_dev_dbg(dev, state, "preparing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001009 error = dev->bus->pm->prepare(dev);
1010 suspend_report_result(dev->bus->pm->prepare, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001011 if (error)
1012 goto End;
1013 }
1014
1015 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
1016 pm_dev_dbg(dev, state, "preparing type ");
1017 error = dev->type->pm->prepare(dev);
1018 suspend_report_result(dev->type->pm->prepare, error);
1019 if (error)
1020 goto End;
1021 }
1022
1023 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
1024 pm_dev_dbg(dev, state, "preparing class ");
1025 error = dev->class->pm->prepare(dev);
1026 suspend_report_result(dev->class->pm->prepare, error);
1027 }
1028 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001029 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001030
1031 return error;
1032}
1033
1034/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001035 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1036 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001037 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001038 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001039 */
1040static int dpm_prepare(pm_message_t state)
1041{
1042 struct list_head list;
1043 int error = 0;
1044
1045 INIT_LIST_HEAD(&list);
1046 mutex_lock(&dpm_list_mtx);
1047 transition_started = true;
1048 while (!list_empty(&dpm_list)) {
1049 struct device *dev = to_device(dpm_list.next);
1050
1051 get_device(dev);
1052 dev->power.status = DPM_PREPARING;
1053 mutex_unlock(&dpm_list_mtx);
1054
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001055 pm_runtime_get_noresume(dev);
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001056 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1057 pm_wakeup_event(dev, 0);
1058
Rafael J. Wysockia2867e02010-12-03 22:58:31 +01001059 if (pm_wakeup_pending()) {
Alan Sternaa0baae2009-12-21 02:46:11 +01001060 pm_runtime_put_sync(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001061 error = -EBUSY;
1062 } else {
1063 error = device_prepare(dev, state);
1064 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001065
1066 mutex_lock(&dpm_list_mtx);
1067 if (error) {
1068 dev->power.status = DPM_ON;
1069 if (error == -EAGAIN) {
1070 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001071 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001072 continue;
1073 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001074 printk(KERN_INFO "PM: Device %s not prepared "
1075 "for power transition: code %d\n",
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001076 kobject_name(&dev->kobj), error);
1077 put_device(dev);
1078 break;
1079 }
1080 dev->power.status = DPM_SUSPENDING;
1081 if (!list_empty(&dev->power.entry))
1082 list_move_tail(&dev->power.entry, &list);
1083 put_device(dev);
1084 }
1085 list_splice(&list, &dpm_list);
1086 mutex_unlock(&dpm_list_mtx);
1087 return error;
1088}
1089
1090/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001091 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1092 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001093 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001094 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1095 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001096 */
Alan Sternd1616302009-05-24 22:05:42 +02001097int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001098{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001099 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001100
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001101 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001102 error = dpm_prepare(state);
1103 if (!error)
1104 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001105 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001106}
Alan Sternd1616302009-05-24 22:05:42 +02001107EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001108
1109void __suspend_report_result(const char *function, void *fn, int ret)
1110{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001111 if (ret)
1112 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001113}
1114EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001115
1116/**
1117 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1118 * @dev: Device to wait for.
1119 * @subordinate: Device that needs to wait for @dev.
1120 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001121int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001122{
1123 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001124 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001125}
1126EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);