blob: 7e79201b09bbab19c3cbe92950e9a9eccdcb95a5 [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>
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 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010038 * Since device_pm_add() may be called with a device semaphore held,
39 * we must never try to acquire a device semaphore while holding
40 * 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
54/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020055 * device_pm_init - Initialize the PM-related part of a device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020056 * @dev: Device object being initialized.
57 */
58void device_pm_init(struct device *dev)
59{
60 dev->power.status = DPM_ON;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010061 init_completion(&dev->power.completion);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020062 pm_runtime_init(dev);
63}
64
65/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020066 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020067 */
68void device_pm_lock(void)
69{
70 mutex_lock(&dpm_list_mtx);
71}
72
73/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020074 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020075 */
76void device_pm_unlock(void)
77{
78 mutex_unlock(&dpm_list_mtx);
79}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010080
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010081/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020082 * device_pm_add - Add a device to the PM core's list of active devices.
83 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010084 */
Alan Stern3b98aea2008-08-07 13:06:12 -040085void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 pr_debug("PM: Adding info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040088 dev->bus ? dev->bus->name : "No Bus",
89 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070090 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020091 if (dev->parent) {
Rafael J. Wysockif5a6d952008-08-09 01:05:13 +020092 if (dev->parent->power.status >= DPM_SUSPENDING)
93 dev_warn(dev, "parent %s should not be sleeping\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010094 dev_name(dev->parent));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020095 } else if (transition_started) {
96 /*
97 * We refuse to register parentless devices while a PM
98 * transition is in progress in order to avoid leaving them
99 * unhandled down the road
100 */
Arjan van de Ven728f0892008-09-20 19:09:00 -0700101 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100102 }
Alan Stern3b98aea2008-08-07 13:06:12 -0400103
104 list_add_tail(&dev->power.entry, &dpm_list);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700105 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100108/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200109 * device_pm_remove - Remove a device from the PM core's list of active devices.
110 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100111 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200112void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 pr_debug("PM: Removing info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -0400115 dev->bus ? dev->bus->name : "No Bus",
116 kobject_name(&dev->kobj));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100117 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700118 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700120 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200121 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100122}
123
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200124/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200125 * device_pm_move_before - Move device in the PM core's list of active devices.
126 * @deva: Device to move in dpm_list.
127 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100128 */
129void device_pm_move_before(struct device *deva, struct device *devb)
130{
131 pr_debug("PM: Moving %s:%s before %s:%s\n",
132 deva->bus ? deva->bus->name : "No Bus",
133 kobject_name(&deva->kobj),
134 devb->bus ? devb->bus->name : "No Bus",
135 kobject_name(&devb->kobj));
136 /* Delete deva from dpm_list and reinsert before devb. */
137 list_move_tail(&deva->power.entry, &devb->power.entry);
138}
139
140/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200141 * device_pm_move_after - Move device in the PM core's list of active devices.
142 * @deva: Device to move in dpm_list.
143 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100144 */
145void device_pm_move_after(struct device *deva, struct device *devb)
146{
147 pr_debug("PM: Moving %s:%s after %s:%s\n",
148 deva->bus ? deva->bus->name : "No Bus",
149 kobject_name(&deva->kobj),
150 devb->bus ? devb->bus->name : "No Bus",
151 kobject_name(&devb->kobj));
152 /* Delete deva from dpm_list and reinsert after devb. */
153 list_move(&deva->power.entry, &devb->power.entry);
154}
155
156/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200157 * device_pm_move_last - Move device to end of the PM core's list of devices.
158 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100159 */
160void device_pm_move_last(struct device *dev)
161{
162 pr_debug("PM: Moving %s:%s to end of list\n",
163 dev->bus ? dev->bus->name : "No Bus",
164 kobject_name(&dev->kobj));
165 list_move_tail(&dev->power.entry, &dpm_list);
166}
167
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100168static ktime_t initcall_debug_start(struct device *dev)
169{
170 ktime_t calltime = ktime_set(0, 0);
171
172 if (initcall_debug) {
173 pr_info("calling %s+ @ %i\n",
174 dev_name(dev), task_pid_nr(current));
175 calltime = ktime_get();
176 }
177
178 return calltime;
179}
180
181static void initcall_debug_report(struct device *dev, ktime_t calltime,
182 int error)
183{
184 ktime_t delta, rettime;
185
186 if (initcall_debug) {
187 rettime = ktime_get();
188 delta = ktime_sub(rettime, calltime);
189 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
190 error, (unsigned long long)ktime_to_ns(delta) >> 10);
191 }
192}
193
Cornelia Huckffa6a702009-03-04 12:44:00 +0100194/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100195 * dpm_wait - Wait for a PM operation to complete.
196 * @dev: Device to wait for.
197 * @async: If unset, wait only if the device's power.async_suspend flag is set.
198 */
199static void dpm_wait(struct device *dev, bool async)
200{
201 if (!dev)
202 return;
203
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100204 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100205 wait_for_completion(&dev->power.completion);
206}
207
208static int dpm_wait_fn(struct device *dev, void *async_ptr)
209{
210 dpm_wait(dev, *((bool *)async_ptr));
211 return 0;
212}
213
214static void dpm_wait_for_children(struct device *dev, bool async)
215{
216 device_for_each_child(dev, &async, dpm_wait_fn);
217}
218
219/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200220 * pm_op - Execute the PM operation appropriate for given PM event.
221 * @dev: Device to handle.
222 * @ops: PM operations to choose from.
223 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200224 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200225static int pm_op(struct device *dev,
226 const struct dev_pm_ops *ops,
227 pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200228{
229 int error = 0;
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100230 ktime_t calltime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100231
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100232 calltime = initcall_debug_start(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200233
234 switch (state.event) {
235#ifdef CONFIG_SUSPEND
236 case PM_EVENT_SUSPEND:
237 if (ops->suspend) {
238 error = ops->suspend(dev);
239 suspend_report_result(ops->suspend, error);
240 }
241 break;
242 case PM_EVENT_RESUME:
243 if (ops->resume) {
244 error = ops->resume(dev);
245 suspend_report_result(ops->resume, error);
246 }
247 break;
248#endif /* CONFIG_SUSPEND */
249#ifdef CONFIG_HIBERNATION
250 case PM_EVENT_FREEZE:
251 case PM_EVENT_QUIESCE:
252 if (ops->freeze) {
253 error = ops->freeze(dev);
254 suspend_report_result(ops->freeze, error);
255 }
256 break;
257 case PM_EVENT_HIBERNATE:
258 if (ops->poweroff) {
259 error = ops->poweroff(dev);
260 suspend_report_result(ops->poweroff, error);
261 }
262 break;
263 case PM_EVENT_THAW:
264 case PM_EVENT_RECOVER:
265 if (ops->thaw) {
266 error = ops->thaw(dev);
267 suspend_report_result(ops->thaw, error);
268 }
269 break;
270 case PM_EVENT_RESTORE:
271 if (ops->restore) {
272 error = ops->restore(dev);
273 suspend_report_result(ops->restore, error);
274 }
275 break;
276#endif /* CONFIG_HIBERNATION */
277 default:
278 error = -EINVAL;
279 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100280
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100281 initcall_debug_report(dev, calltime, error);
Arjan van de Venf2511772009-12-13 20:29:01 +0100282
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200283 return error;
284}
285
286/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200287 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
288 * @dev: Device to handle.
289 * @ops: PM operations to choose from.
290 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200291 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200292 * The driver of @dev will not receive interrupts while this function is being
293 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200294 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200295static int pm_noirq_op(struct device *dev,
296 const struct dev_pm_ops *ops,
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200297 pm_message_t state)
298{
299 int error = 0;
Arjan van de Venf2511772009-12-13 20:29:01 +0100300 ktime_t calltime, delta, rettime;
301
302 if (initcall_debug) {
Rafael J. Wysocki8cc6b392010-01-23 22:03:29 +0100303 pr_info("calling %s+ @ %i, parent: %s\n",
304 dev_name(dev), task_pid_nr(current),
305 dev->parent ? dev_name(dev->parent) : "none");
Arjan van de Venf2511772009-12-13 20:29:01 +0100306 calltime = ktime_get();
307 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200308
309 switch (state.event) {
310#ifdef CONFIG_SUSPEND
311 case PM_EVENT_SUSPEND:
312 if (ops->suspend_noirq) {
313 error = ops->suspend_noirq(dev);
314 suspend_report_result(ops->suspend_noirq, error);
315 }
316 break;
317 case PM_EVENT_RESUME:
318 if (ops->resume_noirq) {
319 error = ops->resume_noirq(dev);
320 suspend_report_result(ops->resume_noirq, error);
321 }
322 break;
323#endif /* CONFIG_SUSPEND */
324#ifdef CONFIG_HIBERNATION
325 case PM_EVENT_FREEZE:
326 case PM_EVENT_QUIESCE:
327 if (ops->freeze_noirq) {
328 error = ops->freeze_noirq(dev);
329 suspend_report_result(ops->freeze_noirq, error);
330 }
331 break;
332 case PM_EVENT_HIBERNATE:
333 if (ops->poweroff_noirq) {
334 error = ops->poweroff_noirq(dev);
335 suspend_report_result(ops->poweroff_noirq, error);
336 }
337 break;
338 case PM_EVENT_THAW:
339 case PM_EVENT_RECOVER:
340 if (ops->thaw_noirq) {
341 error = ops->thaw_noirq(dev);
342 suspend_report_result(ops->thaw_noirq, error);
343 }
344 break;
345 case PM_EVENT_RESTORE:
346 if (ops->restore_noirq) {
347 error = ops->restore_noirq(dev);
348 suspend_report_result(ops->restore_noirq, error);
349 }
350 break;
351#endif /* CONFIG_HIBERNATION */
352 default:
353 error = -EINVAL;
354 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100355
356 if (initcall_debug) {
357 rettime = ktime_get();
358 delta = ktime_sub(rettime, calltime);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100359 printk("initcall %s_i+ returned %d after %Ld usecs\n",
360 dev_name(dev), error,
361 (unsigned long long)ktime_to_ns(delta) >> 10);
Arjan van de Venf2511772009-12-13 20:29:01 +0100362 }
363
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200364 return error;
365}
366
367static char *pm_verb(int event)
368{
369 switch (event) {
370 case PM_EVENT_SUSPEND:
371 return "suspend";
372 case PM_EVENT_RESUME:
373 return "resume";
374 case PM_EVENT_FREEZE:
375 return "freeze";
376 case PM_EVENT_QUIESCE:
377 return "quiesce";
378 case PM_EVENT_HIBERNATE:
379 return "hibernate";
380 case PM_EVENT_THAW:
381 return "thaw";
382 case PM_EVENT_RESTORE:
383 return "restore";
384 case PM_EVENT_RECOVER:
385 return "recover";
386 default:
387 return "(unknown PM event)";
388 }
389}
390
391static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
392{
393 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
394 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
395 ", may wakeup" : "");
396}
397
398static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
399 int error)
400{
401 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
402 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
403}
404
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100405static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
406{
407 ktime_t calltime;
408 s64 usecs64;
409 int usecs;
410
411 calltime = ktime_get();
412 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
413 do_div(usecs64, NSEC_PER_USEC);
414 usecs = usecs64;
415 if (usecs == 0)
416 usecs = 1;
417 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
418 info ?: "", info ? " " : "", pm_verb(state.event),
419 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
420}
421
Alan Sterncd59abf2007-09-21 15:36:56 -0400422/*------------------------- Resume routines -------------------------*/
423
424/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200425 * device_resume_noirq - Execute an "early resume" callback for given device.
426 * @dev: Device to handle.
427 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400428 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200429 * The driver of @dev will not receive interrupts while this function is being
430 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400431 */
Alan Sternd1616302009-05-24 22:05:42 +0200432static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400433{
434 int error = 0;
435
436 TRACE_DEVICE(dev);
437 TRACE_RESUME(0);
438
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100439 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200440 pm_dev_dbg(dev, state, "EARLY ");
441 error = pm_noirq_op(dev, dev->bus->pm, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100442 }
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100443
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100444 TRACE_RESUME(error);
445 return error;
446}
447
448/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200449 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
450 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100451 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200452 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
453 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100454 */
Alan Sternd1616302009-05-24 22:05:42 +0200455void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100456{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200457 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100458 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100459
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200460 mutex_lock(&dpm_list_mtx);
Xiaotian Feng3eb132c2009-08-20 20:28:33 +0200461 transition_started = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200462 list_for_each_entry(dev, &dpm_list, power.entry)
463 if (dev->power.status > DPM_OFF) {
464 int error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100465
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200466 dev->power.status = DPM_OFF;
Alan Sternd1616302009-05-24 22:05:42 +0200467 error = device_resume_noirq(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200468 if (error)
469 pm_dev_err(dev, state, " early", error);
470 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200471 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100472 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100473 resume_device_irqs();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100474}
Alan Sternd1616302009-05-24 22:05:42 +0200475EXPORT_SYMBOL_GPL(dpm_resume_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100476
477/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100478 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800479 * @dev: Device to resume.
480 * @cb: Resume callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100481 */
482static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
483{
484 int error;
485 ktime_t calltime;
486
487 calltime = initcall_debug_start(dev);
488
489 error = cb(dev);
490 suspend_report_result(cb, error);
491
492 initcall_debug_report(dev, calltime, error);
493
494 return error;
495}
496
497/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100498 * __device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200499 * @dev: Device to handle.
500 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100501 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100502 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100503static int __device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100504{
505 int error = 0;
506
507 TRACE_DEVICE(dev);
508 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400509
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100510 dpm_wait(dev->parent, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100511 down(&dev->sem);
512
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200513 if (dev->bus) {
514 if (dev->bus->pm) {
515 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200516 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200517 } else if (dev->bus->resume) {
518 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100519 error = legacy_resume(dev, dev->bus->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200520 }
521 if (error)
522 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400523 }
524
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200525 if (dev->type) {
526 if (dev->type->pm) {
527 pm_dev_dbg(dev, state, "type ");
528 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200529 }
530 if (error)
531 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400532 }
533
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200534 if (dev->class) {
535 if (dev->class->pm) {
536 pm_dev_dbg(dev, state, "class ");
537 error = pm_op(dev, dev->class->pm, state);
538 } else if (dev->class->resume) {
539 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100540 error = legacy_resume(dev, dev->class->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200541 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400542 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200543 End:
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100544 up(&dev->sem);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100545 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100546
Alan Sterncd59abf2007-09-21 15:36:56 -0400547 TRACE_RESUME(error);
548 return error;
549}
550
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100551static void async_resume(void *data, async_cookie_t cookie)
552{
553 struct device *dev = (struct device *)data;
554 int error;
555
556 error = __device_resume(dev, pm_transition, true);
557 if (error)
558 pm_dev_err(dev, pm_transition, " async", error);
559 put_device(dev);
560}
561
562static int device_resume(struct device *dev)
563{
564 INIT_COMPLETION(dev->power.completion);
565
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100566 if (pm_async_enabled && dev->power.async_suspend
567 && !pm_trace_is_enabled()) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100568 get_device(dev);
569 async_schedule(async_resume, dev);
570 return 0;
571 }
572
573 return __device_resume(dev, pm_transition, false);
574}
575
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100576/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200577 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
578 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100579 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200580 * Execute the appropriate "resume" callback for all devices whose status
581 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400582 */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200583static void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400584{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200585 struct list_head list;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100586 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400587
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200588 INIT_LIST_HEAD(&list);
589 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100590 pm_transition = state;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200591 while (!list_empty(&dpm_list)) {
592 struct device *dev = to_device(dpm_list.next);
593
594 get_device(dev);
595 if (dev->power.status >= DPM_OFF) {
596 int error;
597
598 dev->power.status = DPM_RESUMING;
599 mutex_unlock(&dpm_list_mtx);
600
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100601 error = device_resume(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200602
603 mutex_lock(&dpm_list_mtx);
604 if (error)
605 pm_dev_err(dev, state, "", error);
606 } else if (dev->power.status == DPM_SUSPENDING) {
607 /* Allow new children of the device to be registered */
608 dev->power.status = DPM_RESUMING;
609 }
610 if (!list_empty(&dev->power.entry))
611 list_move_tail(&dev->power.entry, &list);
612 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400613 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200614 list_splice(&list, &dpm_list);
615 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100616 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100617 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200618}
619
620/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200621 * device_complete - Complete a PM transition for given device.
622 * @dev: Device to handle.
623 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200624 */
Alan Sternd1616302009-05-24 22:05:42 +0200625static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200626{
627 down(&dev->sem);
628
629 if (dev->class && dev->class->pm && dev->class->pm->complete) {
630 pm_dev_dbg(dev, state, "completing class ");
631 dev->class->pm->complete(dev);
632 }
633
634 if (dev->type && dev->type->pm && dev->type->pm->complete) {
635 pm_dev_dbg(dev, state, "completing type ");
636 dev->type->pm->complete(dev);
637 }
638
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200639 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200640 pm_dev_dbg(dev, state, "completing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200641 dev->bus->pm->complete(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200642 }
643
644 up(&dev->sem);
645}
646
647/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200648 * dpm_complete - Complete a PM transition for all non-sysdev devices.
649 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200650 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200651 * Execute the ->complete() callbacks for all devices whose PM status is not
652 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200653 */
654static void dpm_complete(pm_message_t state)
655{
656 struct list_head list;
657
658 INIT_LIST_HEAD(&list);
659 mutex_lock(&dpm_list_mtx);
Romit Dasguptae528e872009-10-28 22:56:02 +0100660 transition_started = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200661 while (!list_empty(&dpm_list)) {
662 struct device *dev = to_device(dpm_list.prev);
663
664 get_device(dev);
665 if (dev->power.status > DPM_ON) {
666 dev->power.status = DPM_ON;
667 mutex_unlock(&dpm_list_mtx);
668
Alan Sternd1616302009-05-24 22:05:42 +0200669 device_complete(dev, state);
Alan Sternaa0baae2009-12-21 02:46:11 +0100670 pm_runtime_put_sync(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200671
672 mutex_lock(&dpm_list_mtx);
673 }
674 if (!list_empty(&dev->power.entry))
675 list_move(&dev->power.entry, &list);
676 put_device(dev);
677 }
678 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400679 mutex_unlock(&dpm_list_mtx);
680}
681
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100682/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200683 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
684 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400685 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200686 * Execute "resume" callbacks for all devices and complete the PM transition of
687 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400688 */
Alan Sternd1616302009-05-24 22:05:42 +0200689void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400690{
691 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200692 dpm_resume(state);
693 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400694}
Alan Sternd1616302009-05-24 22:05:42 +0200695EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400696
697
Alan Sterncd59abf2007-09-21 15:36:56 -0400698/*------------------------- Suspend routines -------------------------*/
699
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200700/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200701 * resume_event - Return a "resume" message for given "suspend" sleep state.
702 * @sleep_state: PM message representing a sleep state.
703 *
704 * Return a PM message representing the resume event corresponding to given
705 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200706 */
707static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400708{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200709 switch (sleep_state.event) {
710 case PM_EVENT_SUSPEND:
711 return PMSG_RESUME;
712 case PM_EVENT_FREEZE:
713 case PM_EVENT_QUIESCE:
714 return PMSG_RECOVER;
715 case PM_EVENT_HIBERNATE:
716 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400717 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200718 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400719}
720
721/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200722 * device_suspend_noirq - Execute a "late suspend" callback for given device.
723 * @dev: Device to handle.
724 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100725 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200726 * The driver of @dev will not receive interrupts while this function is being
727 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100728 */
Alan Sternd1616302009-05-24 22:05:42 +0200729static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100730{
731 int error = 0;
732
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100733 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200734 pm_dev_dbg(dev, state, "LATE ");
735 error = pm_noirq_op(dev, dev->bus->pm, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100736 }
737 return error;
738}
739
740/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200741 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
742 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100743 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200744 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
745 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100746 */
Alan Sternd1616302009-05-24 22:05:42 +0200747int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100748{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200749 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100750 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100751 int error = 0;
752
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100753 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200754 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200755 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
Alan Sternd1616302009-05-24 22:05:42 +0200756 error = device_suspend_noirq(dev, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100757 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200758 pm_dev_err(dev, state, " late", error);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100759 break;
760 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200761 dev->power.status = DPM_OFF_IRQ;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100762 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200763 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100764 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200765 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100766 else
767 dpm_show_time(starttime, state, "late");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100768 return error;
769}
Alan Sternd1616302009-05-24 22:05:42 +0200770EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100771
772/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100773 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800774 * @dev: Device to suspend.
775 * @state: PM transition of the system being carried out.
776 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100777 */
778static int legacy_suspend(struct device *dev, pm_message_t state,
779 int (*cb)(struct device *dev, pm_message_t state))
780{
781 int error;
782 ktime_t calltime;
783
784 calltime = initcall_debug_start(dev);
785
786 error = cb(dev, state);
787 suspend_report_result(cb, error);
788
789 initcall_debug_report(dev, calltime, error);
790
791 return error;
792}
793
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100794static int async_error;
795
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100796/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200797 * device_suspend - Execute "suspend" callbacks for given device.
798 * @dev: Device to handle.
799 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100800 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400801 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100802static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400803{
804 int error = 0;
805
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100806 dpm_wait_for_children(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100807 down(&dev->sem);
808
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100809 if (async_error)
810 goto End;
811
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200812 if (dev->class) {
813 if (dev->class->pm) {
814 pm_dev_dbg(dev, state, "class ");
815 error = pm_op(dev, dev->class->pm, state);
816 } else if (dev->class->suspend) {
817 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100818 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200819 }
820 if (error)
821 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400822 }
823
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200824 if (dev->type) {
825 if (dev->type->pm) {
826 pm_dev_dbg(dev, state, "type ");
827 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200828 }
829 if (error)
830 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400831 }
832
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200833 if (dev->bus) {
834 if (dev->bus->pm) {
835 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200836 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200837 } else if (dev->bus->suspend) {
838 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100839 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200840 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400841 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100842
843 if (!error)
844 dev->power.status = DPM_OFF;
845
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200846 End:
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100847 up(&dev->sem);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100848 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100849
Alan Sterncd59abf2007-09-21 15:36:56 -0400850 return error;
851}
852
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100853static void async_suspend(void *data, async_cookie_t cookie)
854{
855 struct device *dev = (struct device *)data;
856 int error;
857
858 error = __device_suspend(dev, pm_transition, true);
859 if (error) {
860 pm_dev_err(dev, pm_transition, " async", error);
861 async_error = error;
862 }
863
864 put_device(dev);
865}
866
867static int device_suspend(struct device *dev)
868{
869 INIT_COMPLETION(dev->power.completion);
870
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100871 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100872 get_device(dev);
873 async_schedule(async_suspend, dev);
874 return 0;
875 }
876
877 return __device_suspend(dev, pm_transition, false);
878}
879
Alan Sterncd59abf2007-09-21 15:36:56 -0400880/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200881 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
882 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400883 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100884static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400885{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200886 struct list_head list;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100887 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400888 int error = 0;
889
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200890 INIT_LIST_HEAD(&list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400891 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100892 pm_transition = state;
893 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200894 while (!list_empty(&dpm_list)) {
895 struct device *dev = to_device(dpm_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400896
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200897 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100898 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200899
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100900 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200901
Alan Stern1b3cbec2008-02-29 11:50:22 -0500902 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100903 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200904 pm_dev_err(dev, state, "", error);
905 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100906 break;
907 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100908 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200909 list_move(&dev->power.entry, &list);
910 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100911 if (async_error)
912 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100913 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200914 list_splice(&list, dpm_list.prev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100915 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100916 async_synchronize_full();
917 if (!error)
918 error = async_error;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100919 if (!error)
920 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200921 return error;
922}
923
924/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200925 * device_prepare - Prepare a device for system power transition.
926 * @dev: Device to handle.
927 * @state: PM transition of the system being carried out.
928 *
929 * Execute the ->prepare() callback(s) for given device. No new children of the
930 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200931 */
Alan Sternd1616302009-05-24 22:05:42 +0200932static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200933{
934 int error = 0;
935
936 down(&dev->sem);
937
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200938 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200939 pm_dev_dbg(dev, state, "preparing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200940 error = dev->bus->pm->prepare(dev);
941 suspend_report_result(dev->bus->pm->prepare, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200942 if (error)
943 goto End;
944 }
945
946 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
947 pm_dev_dbg(dev, state, "preparing type ");
948 error = dev->type->pm->prepare(dev);
949 suspend_report_result(dev->type->pm->prepare, error);
950 if (error)
951 goto End;
952 }
953
954 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
955 pm_dev_dbg(dev, state, "preparing class ");
956 error = dev->class->pm->prepare(dev);
957 suspend_report_result(dev->class->pm->prepare, error);
958 }
959 End:
960 up(&dev->sem);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100961
962 return error;
963}
964
965/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200966 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
967 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400968 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200969 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200970 */
971static int dpm_prepare(pm_message_t state)
972{
973 struct list_head list;
974 int error = 0;
975
976 INIT_LIST_HEAD(&list);
977 mutex_lock(&dpm_list_mtx);
978 transition_started = true;
979 while (!list_empty(&dpm_list)) {
980 struct device *dev = to_device(dpm_list.next);
981
982 get_device(dev);
983 dev->power.status = DPM_PREPARING;
984 mutex_unlock(&dpm_list_mtx);
985
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200986 pm_runtime_get_noresume(dev);
987 if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
988 /* Wake-up requested during system sleep transition. */
Alan Sternaa0baae2009-12-21 02:46:11 +0100989 pm_runtime_put_sync(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200990 error = -EBUSY;
991 } else {
992 error = device_prepare(dev, state);
993 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200994
995 mutex_lock(&dpm_list_mtx);
996 if (error) {
997 dev->power.status = DPM_ON;
998 if (error == -EAGAIN) {
999 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001000 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001001 continue;
1002 }
1003 printk(KERN_ERR "PM: Failed to prepare device %s "
1004 "for power transition: error %d\n",
1005 kobject_name(&dev->kobj), error);
1006 put_device(dev);
1007 break;
1008 }
1009 dev->power.status = DPM_SUSPENDING;
1010 if (!list_empty(&dev->power.entry))
1011 list_move_tail(&dev->power.entry, &list);
1012 put_device(dev);
1013 }
1014 list_splice(&list, &dpm_list);
1015 mutex_unlock(&dpm_list_mtx);
1016 return error;
1017}
1018
1019/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001020 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1021 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001022 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001023 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1024 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001025 */
Alan Sternd1616302009-05-24 22:05:42 +02001026int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001027{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001028 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001029
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001030 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001031 error = dpm_prepare(state);
1032 if (!error)
1033 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001034 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001035}
Alan Sternd1616302009-05-24 22:05:42 +02001036EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001037
1038void __suspend_report_result(const char *function, void *fn, int ret)
1039{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001040 if (ret)
1041 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001042}
1043EXPORT_SYMBOL_GPL(__suspend_report_result);