blob: 8052dafc0ba92e3cfc97ddf91bd4ca31ba528467 [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. Wysocki2ed8d2b32009-03-16 22:34:06 +010026#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010027#include <linux/sched.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070028
Alan Sterncd59abf2007-09-21 15:36:56 -040029#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "power.h"
31
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010032/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020033 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010034 * because children are guaranteed to be discovered after parents, and
35 * are inserted at the back of the list on discovery.
36 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010037 * Since device_pm_add() may be called with a device semaphore held,
38 * we must never try to acquire a device semaphore while holding
39 * dpm_list_mutex.
40 */
41
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020042LIST_HEAD(dpm_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Alan Sterncd59abf2007-09-21 15:36:56 -040044static DEFINE_MUTEX(dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020046/*
47 * Set once the preparation of devices for a PM transition has started, reset
48 * before starting to resume devices. Protected by dpm_list_mtx.
49 */
50static bool transition_started;
51
52/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020053 * device_pm_init - Initialize the PM-related part of a device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020054 * @dev: Device object being initialized.
55 */
56void device_pm_init(struct device *dev)
57{
58 dev->power.status = DPM_ON;
59 pm_runtime_init(dev);
60}
61
62/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020063 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020064 */
65void device_pm_lock(void)
66{
67 mutex_lock(&dpm_list_mtx);
68}
69
70/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020071 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020072 */
73void device_pm_unlock(void)
74{
75 mutex_unlock(&dpm_list_mtx);
76}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010077
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010078/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020079 * device_pm_add - Add a device to the PM core's list of active devices.
80 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010081 */
Alan Stern3b98aea2008-08-07 13:06:12 -040082void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 pr_debug("PM: Adding info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040085 dev->bus ? dev->bus->name : "No Bus",
86 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070087 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020088 if (dev->parent) {
Rafael J. Wysockif5a6d952008-08-09 01:05:13 +020089 if (dev->parent->power.status >= DPM_SUSPENDING)
90 dev_warn(dev, "parent %s should not be sleeping\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010091 dev_name(dev->parent));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020092 } else if (transition_started) {
93 /*
94 * We refuse to register parentless devices while a PM
95 * transition is in progress in order to avoid leaving them
96 * unhandled down the road
97 */
Arjan van de Ven728f0892008-09-20 19:09:00 -070098 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010099 }
Alan Stern3b98aea2008-08-07 13:06:12 -0400100
101 list_add_tail(&dev->power.entry, &dpm_list);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700102 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100105/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200106 * device_pm_remove - Remove a device from the PM core's list of active devices.
107 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100108 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200109void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 pr_debug("PM: Removing info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -0400112 dev->bus ? dev->bus->name : "No Bus",
113 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700114 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700116 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200117 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100118}
119
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200120/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200121 * device_pm_move_before - Move device in the PM core's list of active devices.
122 * @deva: Device to move in dpm_list.
123 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100124 */
125void device_pm_move_before(struct device *deva, struct device *devb)
126{
127 pr_debug("PM: Moving %s:%s before %s:%s\n",
128 deva->bus ? deva->bus->name : "No Bus",
129 kobject_name(&deva->kobj),
130 devb->bus ? devb->bus->name : "No Bus",
131 kobject_name(&devb->kobj));
132 /* Delete deva from dpm_list and reinsert before devb. */
133 list_move_tail(&deva->power.entry, &devb->power.entry);
134}
135
136/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200137 * device_pm_move_after - Move device in the PM core's list of active devices.
138 * @deva: Device to move in dpm_list.
139 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100140 */
141void device_pm_move_after(struct device *deva, struct device *devb)
142{
143 pr_debug("PM: Moving %s:%s after %s:%s\n",
144 deva->bus ? deva->bus->name : "No Bus",
145 kobject_name(&deva->kobj),
146 devb->bus ? devb->bus->name : "No Bus",
147 kobject_name(&devb->kobj));
148 /* Delete deva from dpm_list and reinsert after devb. */
149 list_move(&deva->power.entry, &devb->power.entry);
150}
151
152/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200153 * device_pm_move_last - Move device to end of the PM core's list of devices.
154 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100155 */
156void device_pm_move_last(struct device *dev)
157{
158 pr_debug("PM: Moving %s:%s to end of list\n",
159 dev->bus ? dev->bus->name : "No Bus",
160 kobject_name(&dev->kobj));
161 list_move_tail(&dev->power.entry, &dpm_list);
162}
163
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100164static ktime_t initcall_debug_start(struct device *dev)
165{
166 ktime_t calltime = ktime_set(0, 0);
167
168 if (initcall_debug) {
169 pr_info("calling %s+ @ %i\n",
170 dev_name(dev), task_pid_nr(current));
171 calltime = ktime_get();
172 }
173
174 return calltime;
175}
176
177static void initcall_debug_report(struct device *dev, ktime_t calltime,
178 int error)
179{
180 ktime_t delta, rettime;
181
182 if (initcall_debug) {
183 rettime = ktime_get();
184 delta = ktime_sub(rettime, calltime);
185 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
186 error, (unsigned long long)ktime_to_ns(delta) >> 10);
187 }
188}
189
Cornelia Huckffa6a702009-03-04 12:44:00 +0100190/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200191 * pm_op - Execute the PM operation appropriate for given PM event.
192 * @dev: Device to handle.
193 * @ops: PM operations to choose from.
194 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200195 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200196static int pm_op(struct device *dev,
197 const struct dev_pm_ops *ops,
198 pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200199{
200 int error = 0;
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100201 ktime_t calltime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100202
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100203 calltime = initcall_debug_start(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200204
205 switch (state.event) {
206#ifdef CONFIG_SUSPEND
207 case PM_EVENT_SUSPEND:
208 if (ops->suspend) {
209 error = ops->suspend(dev);
210 suspend_report_result(ops->suspend, error);
211 }
212 break;
213 case PM_EVENT_RESUME:
214 if (ops->resume) {
215 error = ops->resume(dev);
216 suspend_report_result(ops->resume, error);
217 }
218 break;
219#endif /* CONFIG_SUSPEND */
220#ifdef CONFIG_HIBERNATION
221 case PM_EVENT_FREEZE:
222 case PM_EVENT_QUIESCE:
223 if (ops->freeze) {
224 error = ops->freeze(dev);
225 suspend_report_result(ops->freeze, error);
226 }
227 break;
228 case PM_EVENT_HIBERNATE:
229 if (ops->poweroff) {
230 error = ops->poweroff(dev);
231 suspend_report_result(ops->poweroff, error);
232 }
233 break;
234 case PM_EVENT_THAW:
235 case PM_EVENT_RECOVER:
236 if (ops->thaw) {
237 error = ops->thaw(dev);
238 suspend_report_result(ops->thaw, error);
239 }
240 break;
241 case PM_EVENT_RESTORE:
242 if (ops->restore) {
243 error = ops->restore(dev);
244 suspend_report_result(ops->restore, error);
245 }
246 break;
247#endif /* CONFIG_HIBERNATION */
248 default:
249 error = -EINVAL;
250 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100251
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100252 initcall_debug_report(dev, calltime, error);
Arjan van de Venf2511772009-12-13 20:29:01 +0100253
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200254 return error;
255}
256
257/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200258 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
259 * @dev: Device to handle.
260 * @ops: PM operations to choose from.
261 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200262 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200263 * The driver of @dev will not receive interrupts while this function is being
264 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200265 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200266static int pm_noirq_op(struct device *dev,
267 const struct dev_pm_ops *ops,
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200268 pm_message_t state)
269{
270 int error = 0;
Arjan van de Venf2511772009-12-13 20:29:01 +0100271 ktime_t calltime, delta, rettime;
272
273 if (initcall_debug) {
274 pr_info("calling %s_i+ @ %i\n",
275 dev_name(dev), task_pid_nr(current));
276 calltime = ktime_get();
277 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200278
279 switch (state.event) {
280#ifdef CONFIG_SUSPEND
281 case PM_EVENT_SUSPEND:
282 if (ops->suspend_noirq) {
283 error = ops->suspend_noirq(dev);
284 suspend_report_result(ops->suspend_noirq, error);
285 }
286 break;
287 case PM_EVENT_RESUME:
288 if (ops->resume_noirq) {
289 error = ops->resume_noirq(dev);
290 suspend_report_result(ops->resume_noirq, error);
291 }
292 break;
293#endif /* CONFIG_SUSPEND */
294#ifdef CONFIG_HIBERNATION
295 case PM_EVENT_FREEZE:
296 case PM_EVENT_QUIESCE:
297 if (ops->freeze_noirq) {
298 error = ops->freeze_noirq(dev);
299 suspend_report_result(ops->freeze_noirq, error);
300 }
301 break;
302 case PM_EVENT_HIBERNATE:
303 if (ops->poweroff_noirq) {
304 error = ops->poweroff_noirq(dev);
305 suspend_report_result(ops->poweroff_noirq, error);
306 }
307 break;
308 case PM_EVENT_THAW:
309 case PM_EVENT_RECOVER:
310 if (ops->thaw_noirq) {
311 error = ops->thaw_noirq(dev);
312 suspend_report_result(ops->thaw_noirq, error);
313 }
314 break;
315 case PM_EVENT_RESTORE:
316 if (ops->restore_noirq) {
317 error = ops->restore_noirq(dev);
318 suspend_report_result(ops->restore_noirq, error);
319 }
320 break;
321#endif /* CONFIG_HIBERNATION */
322 default:
323 error = -EINVAL;
324 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100325
326 if (initcall_debug) {
327 rettime = ktime_get();
328 delta = ktime_sub(rettime, calltime);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100329 printk("initcall %s_i+ returned %d after %Ld usecs\n",
330 dev_name(dev), error,
331 (unsigned long long)ktime_to_ns(delta) >> 10);
Arjan van de Venf2511772009-12-13 20:29:01 +0100332 }
333
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200334 return error;
335}
336
337static char *pm_verb(int event)
338{
339 switch (event) {
340 case PM_EVENT_SUSPEND:
341 return "suspend";
342 case PM_EVENT_RESUME:
343 return "resume";
344 case PM_EVENT_FREEZE:
345 return "freeze";
346 case PM_EVENT_QUIESCE:
347 return "quiesce";
348 case PM_EVENT_HIBERNATE:
349 return "hibernate";
350 case PM_EVENT_THAW:
351 return "thaw";
352 case PM_EVENT_RESTORE:
353 return "restore";
354 case PM_EVENT_RECOVER:
355 return "recover";
356 default:
357 return "(unknown PM event)";
358 }
359}
360
361static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
362{
363 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
364 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
365 ", may wakeup" : "");
366}
367
368static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
369 int error)
370{
371 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
372 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
373}
374
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100375static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
376{
377 ktime_t calltime;
378 s64 usecs64;
379 int usecs;
380
381 calltime = ktime_get();
382 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
383 do_div(usecs64, NSEC_PER_USEC);
384 usecs = usecs64;
385 if (usecs == 0)
386 usecs = 1;
387 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
388 info ?: "", info ? " " : "", pm_verb(state.event),
389 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
390}
391
Alan Sterncd59abf2007-09-21 15:36:56 -0400392/*------------------------- Resume routines -------------------------*/
393
394/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200395 * device_resume_noirq - Execute an "early resume" callback for given device.
396 * @dev: Device to handle.
397 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400398 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200399 * The driver of @dev will not receive interrupts while this function is being
400 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400401 */
Alan Sternd1616302009-05-24 22:05:42 +0200402static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400403{
404 int error = 0;
405
406 TRACE_DEVICE(dev);
407 TRACE_RESUME(0);
408
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100409 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200410 pm_dev_dbg(dev, state, "EARLY ");
411 error = pm_noirq_op(dev, dev->bus->pm, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100412 }
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100413
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100414 TRACE_RESUME(error);
415 return error;
416}
417
418/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200419 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
420 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100421 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200422 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
423 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100424 */
Alan Sternd1616302009-05-24 22:05:42 +0200425void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100426{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200427 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100428 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100429
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200430 mutex_lock(&dpm_list_mtx);
Xiaotian Feng3eb132c2009-08-20 20:28:33 +0200431 transition_started = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200432 list_for_each_entry(dev, &dpm_list, power.entry)
433 if (dev->power.status > DPM_OFF) {
434 int error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100435
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200436 dev->power.status = DPM_OFF;
Alan Sternd1616302009-05-24 22:05:42 +0200437 error = device_resume_noirq(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200438 if (error)
439 pm_dev_err(dev, state, " early", error);
440 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200441 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100442 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +0100443 resume_device_irqs();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100444}
Alan Sternd1616302009-05-24 22:05:42 +0200445EXPORT_SYMBOL_GPL(dpm_resume_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100446
447/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100448 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
449 * dev: Device to resume.
450 * cb: Resume callback to execute.
451 */
452static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
453{
454 int error;
455 ktime_t calltime;
456
457 calltime = initcall_debug_start(dev);
458
459 error = cb(dev);
460 suspend_report_result(cb, error);
461
462 initcall_debug_report(dev, calltime, error);
463
464 return error;
465}
466
467/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200468 * device_resume - Execute "resume" callbacks for given device.
469 * @dev: Device to handle.
470 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100471 */
Alan Sternd1616302009-05-24 22:05:42 +0200472static int device_resume(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100473{
474 int error = 0;
475
476 TRACE_DEVICE(dev);
477 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400478
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100479 down(&dev->sem);
480
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200481 if (dev->bus) {
482 if (dev->bus->pm) {
483 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200484 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200485 } else if (dev->bus->resume) {
486 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100487 error = legacy_resume(dev, dev->bus->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200488 }
489 if (error)
490 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400491 }
492
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200493 if (dev->type) {
494 if (dev->type->pm) {
495 pm_dev_dbg(dev, state, "type ");
496 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200497 }
498 if (error)
499 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400500 }
501
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200502 if (dev->class) {
503 if (dev->class->pm) {
504 pm_dev_dbg(dev, state, "class ");
505 error = pm_op(dev, dev->class->pm, state);
506 } else if (dev->class->resume) {
507 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100508 error = legacy_resume(dev, dev->class->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200509 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400510 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200511 End:
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100512 up(&dev->sem);
513
Alan Sterncd59abf2007-09-21 15:36:56 -0400514 TRACE_RESUME(error);
515 return error;
516}
517
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100518/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200519 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
520 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100521 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200522 * Execute the appropriate "resume" callback for all devices whose status
523 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400524 */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200525static void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400526{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200527 struct list_head list;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100528 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400529
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200530 INIT_LIST_HEAD(&list);
531 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200532 while (!list_empty(&dpm_list)) {
533 struct device *dev = to_device(dpm_list.next);
534
535 get_device(dev);
536 if (dev->power.status >= DPM_OFF) {
537 int error;
538
539 dev->power.status = DPM_RESUMING;
540 mutex_unlock(&dpm_list_mtx);
541
Alan Sternd1616302009-05-24 22:05:42 +0200542 error = device_resume(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200543
544 mutex_lock(&dpm_list_mtx);
545 if (error)
546 pm_dev_err(dev, state, "", error);
547 } else if (dev->power.status == DPM_SUSPENDING) {
548 /* Allow new children of the device to be registered */
549 dev->power.status = DPM_RESUMING;
550 }
551 if (!list_empty(&dev->power.entry))
552 list_move_tail(&dev->power.entry, &list);
553 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400554 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200555 list_splice(&list, &dpm_list);
556 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100557 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200558}
559
560/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200561 * device_complete - Complete a PM transition for given device.
562 * @dev: Device to handle.
563 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200564 */
Alan Sternd1616302009-05-24 22:05:42 +0200565static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200566{
567 down(&dev->sem);
568
569 if (dev->class && dev->class->pm && dev->class->pm->complete) {
570 pm_dev_dbg(dev, state, "completing class ");
571 dev->class->pm->complete(dev);
572 }
573
574 if (dev->type && dev->type->pm && dev->type->pm->complete) {
575 pm_dev_dbg(dev, state, "completing type ");
576 dev->type->pm->complete(dev);
577 }
578
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200579 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200580 pm_dev_dbg(dev, state, "completing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200581 dev->bus->pm->complete(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200582 }
583
584 up(&dev->sem);
585}
586
587/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200588 * dpm_complete - Complete a PM transition for all non-sysdev devices.
589 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200590 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200591 * Execute the ->complete() callbacks for all devices whose PM status is not
592 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200593 */
594static void dpm_complete(pm_message_t state)
595{
596 struct list_head list;
597
598 INIT_LIST_HEAD(&list);
599 mutex_lock(&dpm_list_mtx);
Romit Dasguptae528e872009-10-28 22:56:02 +0100600 transition_started = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200601 while (!list_empty(&dpm_list)) {
602 struct device *dev = to_device(dpm_list.prev);
603
604 get_device(dev);
605 if (dev->power.status > DPM_ON) {
606 dev->power.status = DPM_ON;
607 mutex_unlock(&dpm_list_mtx);
608
Alan Sternd1616302009-05-24 22:05:42 +0200609 device_complete(dev, state);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200610 pm_runtime_put_noidle(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200611
612 mutex_lock(&dpm_list_mtx);
613 }
614 if (!list_empty(&dev->power.entry))
615 list_move(&dev->power.entry, &list);
616 put_device(dev);
617 }
618 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400619 mutex_unlock(&dpm_list_mtx);
620}
621
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100622/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200623 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
624 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400625 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200626 * Execute "resume" callbacks for all devices and complete the PM transition of
627 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400628 */
Alan Sternd1616302009-05-24 22:05:42 +0200629void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400630{
631 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200632 dpm_resume(state);
633 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400634}
Alan Sternd1616302009-05-24 22:05:42 +0200635EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400636
637
Alan Sterncd59abf2007-09-21 15:36:56 -0400638/*------------------------- Suspend routines -------------------------*/
639
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200640/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200641 * resume_event - Return a "resume" message for given "suspend" sleep state.
642 * @sleep_state: PM message representing a sleep state.
643 *
644 * Return a PM message representing the resume event corresponding to given
645 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200646 */
647static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400648{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200649 switch (sleep_state.event) {
650 case PM_EVENT_SUSPEND:
651 return PMSG_RESUME;
652 case PM_EVENT_FREEZE:
653 case PM_EVENT_QUIESCE:
654 return PMSG_RECOVER;
655 case PM_EVENT_HIBERNATE:
656 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400657 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200658 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400659}
660
661/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200662 * device_suspend_noirq - Execute a "late suspend" callback for given device.
663 * @dev: Device to handle.
664 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100665 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200666 * The driver of @dev will not receive interrupts while this function is being
667 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100668 */
Alan Sternd1616302009-05-24 22:05:42 +0200669static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100670{
671 int error = 0;
672
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100673 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200674 pm_dev_dbg(dev, state, "LATE ");
675 error = pm_noirq_op(dev, dev->bus->pm, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100676 }
677 return error;
678}
679
680/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200681 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
682 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100683 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200684 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
685 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100686 */
Alan Sternd1616302009-05-24 22:05:42 +0200687int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100688{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200689 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100690 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100691 int error = 0;
692
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +0100693 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200694 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200695 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
Alan Sternd1616302009-05-24 22:05:42 +0200696 error = device_suspend_noirq(dev, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100697 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200698 pm_dev_err(dev, state, " late", error);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100699 break;
700 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200701 dev->power.status = DPM_OFF_IRQ;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100702 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200703 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100704 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200705 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100706 else
707 dpm_show_time(starttime, state, "late");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100708 return error;
709}
Alan Sternd1616302009-05-24 22:05:42 +0200710EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100711
712/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100713 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
714 * dev: Device to suspend.
715 * cb: Suspend callback to execute.
716 */
717static int legacy_suspend(struct device *dev, pm_message_t state,
718 int (*cb)(struct device *dev, pm_message_t state))
719{
720 int error;
721 ktime_t calltime;
722
723 calltime = initcall_debug_start(dev);
724
725 error = cb(dev, state);
726 suspend_report_result(cb, error);
727
728 initcall_debug_report(dev, calltime, error);
729
730 return error;
731}
732
733/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200734 * device_suspend - Execute "suspend" callbacks for given device.
735 * @dev: Device to handle.
736 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400737 */
Alan Sternd1616302009-05-24 22:05:42 +0200738static int device_suspend(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400739{
740 int error = 0;
741
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100742 down(&dev->sem);
743
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200744 if (dev->class) {
745 if (dev->class->pm) {
746 pm_dev_dbg(dev, state, "class ");
747 error = pm_op(dev, dev->class->pm, state);
748 } else if (dev->class->suspend) {
749 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100750 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200751 }
752 if (error)
753 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400754 }
755
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200756 if (dev->type) {
757 if (dev->type->pm) {
758 pm_dev_dbg(dev, state, "type ");
759 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200760 }
761 if (error)
762 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400763 }
764
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200765 if (dev->bus) {
766 if (dev->bus->pm) {
767 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200768 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200769 } else if (dev->bus->suspend) {
770 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100771 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200772 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400773 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200774 End:
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100775 up(&dev->sem);
776
Alan Sterncd59abf2007-09-21 15:36:56 -0400777 return error;
778}
779
780/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200781 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
782 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400783 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100784static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400785{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200786 struct list_head list;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100787 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400788 int error = 0;
789
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200790 INIT_LIST_HEAD(&list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400791 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200792 while (!list_empty(&dpm_list)) {
793 struct device *dev = to_device(dpm_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400794
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200795 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100796 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200797
Alan Sternd1616302009-05-24 22:05:42 +0200798 error = device_suspend(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200799
Alan Stern1b3cbec2008-02-29 11:50:22 -0500800 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100801 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200802 pm_dev_err(dev, state, "", error);
803 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100804 break;
805 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200806 dev->power.status = DPM_OFF;
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100807 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200808 list_move(&dev->power.entry, &list);
809 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100810 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200811 list_splice(&list, dpm_list.prev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100812 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100813 if (!error)
814 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200815 return error;
816}
817
818/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200819 * device_prepare - Prepare a device for system power transition.
820 * @dev: Device to handle.
821 * @state: PM transition of the system being carried out.
822 *
823 * Execute the ->prepare() callback(s) for given device. No new children of the
824 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200825 */
Alan Sternd1616302009-05-24 22:05:42 +0200826static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200827{
828 int error = 0;
829
830 down(&dev->sem);
831
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200832 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200833 pm_dev_dbg(dev, state, "preparing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200834 error = dev->bus->pm->prepare(dev);
835 suspend_report_result(dev->bus->pm->prepare, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200836 if (error)
837 goto End;
838 }
839
840 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
841 pm_dev_dbg(dev, state, "preparing type ");
842 error = dev->type->pm->prepare(dev);
843 suspend_report_result(dev->type->pm->prepare, error);
844 if (error)
845 goto End;
846 }
847
848 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
849 pm_dev_dbg(dev, state, "preparing class ");
850 error = dev->class->pm->prepare(dev);
851 suspend_report_result(dev->class->pm->prepare, error);
852 }
853 End:
854 up(&dev->sem);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100855
856 return error;
857}
858
859/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200860 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
861 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400862 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200863 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200864 */
865static int dpm_prepare(pm_message_t state)
866{
867 struct list_head list;
868 int error = 0;
869
870 INIT_LIST_HEAD(&list);
871 mutex_lock(&dpm_list_mtx);
872 transition_started = true;
873 while (!list_empty(&dpm_list)) {
874 struct device *dev = to_device(dpm_list.next);
875
876 get_device(dev);
877 dev->power.status = DPM_PREPARING;
878 mutex_unlock(&dpm_list_mtx);
879
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200880 pm_runtime_get_noresume(dev);
881 if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
882 /* Wake-up requested during system sleep transition. */
883 pm_runtime_put_noidle(dev);
884 error = -EBUSY;
885 } else {
886 error = device_prepare(dev, state);
887 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200888
889 mutex_lock(&dpm_list_mtx);
890 if (error) {
891 dev->power.status = DPM_ON;
892 if (error == -EAGAIN) {
893 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +0200894 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200895 continue;
896 }
897 printk(KERN_ERR "PM: Failed to prepare device %s "
898 "for power transition: error %d\n",
899 kobject_name(&dev->kobj), error);
900 put_device(dev);
901 break;
902 }
903 dev->power.status = DPM_SUSPENDING;
904 if (!list_empty(&dev->power.entry))
905 list_move_tail(&dev->power.entry, &list);
906 put_device(dev);
907 }
908 list_splice(&list, &dpm_list);
909 mutex_unlock(&dpm_list_mtx);
910 return error;
911}
912
913/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200914 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
915 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200916 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200917 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
918 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -0400919 */
Alan Sternd1616302009-05-24 22:05:42 +0200920int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400921{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100922 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400923
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100924 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200925 error = dpm_prepare(state);
926 if (!error)
927 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400928 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400929}
Alan Sternd1616302009-05-24 22:05:42 +0200930EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -0400931
932void __suspend_report_result(const char *function, void *fn, int ret)
933{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -0700934 if (ret)
935 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -0400936}
937EXPORT_SYMBOL_GPL(__suspend_report_result);