2 * drivers/base/power/main.c - Where the driver meets power management.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
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.
15 * 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.
20 #include <linux/device.h>
21 #include <linux/kallsyms.h>
22 #include <linux/mutex.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/resume-trace.h>
26 #include <linux/interrupt.h>
27 #include <linux/sched.h>
28 #include <linux/async.h>
29 #include <linux/suspend.h>
35 * The entries in the dpm_list list are in a depth first order, simply
36 * because children are guaranteed to be discovered after parents, and
37 * are inserted at the back of the list on discovery.
39 * Since device_pm_add() may be called with a device lock held,
40 * we must never try to acquire a device lock while holding
45 LIST_HEAD(dpm_prepared_list
);
46 LIST_HEAD(dpm_suspended_list
);
47 LIST_HEAD(dpm_noirq_list
);
49 static DEFINE_MUTEX(dpm_list_mtx
);
50 static pm_message_t pm_transition
;
52 static int async_error
;
55 * device_pm_init - Initialize the PM-related part of a device object.
56 * @dev: Device object being initialized.
58 void device_pm_init(struct device
*dev
)
60 dev
->power
.in_suspend
= false;
61 init_completion(&dev
->power
.completion
);
62 complete_all(&dev
->power
.completion
);
63 dev
->power
.wakeup
= NULL
;
64 spin_lock_init(&dev
->power
.lock
);
69 * device_pm_lock - Lock the list of active devices used by the PM core.
71 void device_pm_lock(void)
73 mutex_lock(&dpm_list_mtx
);
77 * device_pm_unlock - Unlock the list of active devices used by the PM core.
79 void device_pm_unlock(void)
81 mutex_unlock(&dpm_list_mtx
);
85 * device_pm_add - Add a device to the PM core's list of active devices.
86 * @dev: Device to add to the list.
88 void device_pm_add(struct device
*dev
)
90 pr_debug("PM: Adding info for %s:%s\n",
91 dev
->bus
? dev
->bus
->name
: "No Bus",
92 kobject_name(&dev
->kobj
));
93 mutex_lock(&dpm_list_mtx
);
94 if (dev
->parent
&& dev
->parent
->power
.in_suspend
)
95 dev_warn(dev
, "parent %s should not be sleeping\n",
96 dev_name(dev
->parent
));
97 list_add_tail(&dev
->power
.entry
, &dpm_list
);
98 mutex_unlock(&dpm_list_mtx
);
102 * device_pm_remove - Remove a device from the PM core's list of active devices.
103 * @dev: Device to be removed from the list.
105 void device_pm_remove(struct device
*dev
)
107 pr_debug("PM: Removing info for %s:%s\n",
108 dev
->bus
? dev
->bus
->name
: "No Bus",
109 kobject_name(&dev
->kobj
));
110 complete_all(&dev
->power
.completion
);
111 mutex_lock(&dpm_list_mtx
);
112 list_del_init(&dev
->power
.entry
);
113 mutex_unlock(&dpm_list_mtx
);
114 device_wakeup_disable(dev
);
115 pm_runtime_remove(dev
);
119 * device_pm_move_before - Move device in the PM core's list of active devices.
120 * @deva: Device to move in dpm_list.
121 * @devb: Device @deva should come before.
123 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
125 pr_debug("PM: Moving %s:%s before %s:%s\n",
126 deva
->bus
? deva
->bus
->name
: "No Bus",
127 kobject_name(&deva
->kobj
),
128 devb
->bus
? devb
->bus
->name
: "No Bus",
129 kobject_name(&devb
->kobj
));
130 /* Delete deva from dpm_list and reinsert before devb. */
131 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
135 * device_pm_move_after - Move device in the PM core's list of active devices.
136 * @deva: Device to move in dpm_list.
137 * @devb: Device @deva should come after.
139 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
141 pr_debug("PM: Moving %s:%s after %s:%s\n",
142 deva
->bus
? deva
->bus
->name
: "No Bus",
143 kobject_name(&deva
->kobj
),
144 devb
->bus
? devb
->bus
->name
: "No Bus",
145 kobject_name(&devb
->kobj
));
146 /* Delete deva from dpm_list and reinsert after devb. */
147 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
151 * device_pm_move_last - Move device to end of the PM core's list of devices.
152 * @dev: Device to move in dpm_list.
154 void device_pm_move_last(struct device
*dev
)
156 pr_debug("PM: Moving %s:%s to end of list\n",
157 dev
->bus
? dev
->bus
->name
: "No Bus",
158 kobject_name(&dev
->kobj
));
159 list_move_tail(&dev
->power
.entry
, &dpm_list
);
162 static ktime_t
initcall_debug_start(struct device
*dev
)
164 ktime_t calltime
= ktime_set(0, 0);
166 if (initcall_debug
) {
167 pr_info("calling %s+ @ %i\n",
168 dev_name(dev
), task_pid_nr(current
));
169 calltime
= ktime_get();
175 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
178 ktime_t delta
, rettime
;
180 if (initcall_debug
) {
181 rettime
= ktime_get();
182 delta
= ktime_sub(rettime
, calltime
);
183 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
184 error
, (unsigned long long)ktime_to_ns(delta
) >> 10);
189 * dpm_wait - Wait for a PM operation to complete.
190 * @dev: Device to wait for.
191 * @async: If unset, wait only if the device's power.async_suspend flag is set.
193 static void dpm_wait(struct device
*dev
, bool async
)
198 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
199 wait_for_completion(&dev
->power
.completion
);
202 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
204 dpm_wait(dev
, *((bool *)async_ptr
));
208 static void dpm_wait_for_children(struct device
*dev
, bool async
)
210 device_for_each_child(dev
, &async
, dpm_wait_fn
);
214 * pm_op - Execute the PM operation appropriate for given PM event.
215 * @dev: Device to handle.
216 * @ops: PM operations to choose from.
217 * @state: PM transition of the system being carried out.
219 static int pm_op(struct device
*dev
,
220 const struct dev_pm_ops
*ops
,
226 calltime
= initcall_debug_start(dev
);
228 switch (state
.event
) {
229 #ifdef CONFIG_SUSPEND
230 case PM_EVENT_SUSPEND
:
232 error
= ops
->suspend(dev
);
233 suspend_report_result(ops
->suspend
, error
);
236 case PM_EVENT_RESUME
:
238 error
= ops
->resume(dev
);
239 suspend_report_result(ops
->resume
, error
);
242 #endif /* CONFIG_SUSPEND */
243 #ifdef CONFIG_HIBERNATION
244 case PM_EVENT_FREEZE
:
245 case PM_EVENT_QUIESCE
:
247 error
= ops
->freeze(dev
);
248 suspend_report_result(ops
->freeze
, error
);
251 case PM_EVENT_HIBERNATE
:
253 error
= ops
->poweroff(dev
);
254 suspend_report_result(ops
->poweroff
, error
);
258 case PM_EVENT_RECOVER
:
260 error
= ops
->thaw(dev
);
261 suspend_report_result(ops
->thaw
, error
);
264 case PM_EVENT_RESTORE
:
266 error
= ops
->restore(dev
);
267 suspend_report_result(ops
->restore
, error
);
270 #endif /* CONFIG_HIBERNATION */
275 initcall_debug_report(dev
, calltime
, error
);
281 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
282 * @dev: Device to handle.
283 * @ops: PM operations to choose from.
284 * @state: PM transition of the system being carried out.
286 * The driver of @dev will not receive interrupts while this function is being
289 static int pm_noirq_op(struct device
*dev
,
290 const struct dev_pm_ops
*ops
,
294 ktime_t calltime
= ktime_set(0, 0), delta
, rettime
;
296 if (initcall_debug
) {
297 pr_info("calling %s+ @ %i, parent: %s\n",
298 dev_name(dev
), task_pid_nr(current
),
299 dev
->parent
? dev_name(dev
->parent
) : "none");
300 calltime
= ktime_get();
303 switch (state
.event
) {
304 #ifdef CONFIG_SUSPEND
305 case PM_EVENT_SUSPEND
:
306 if (ops
->suspend_noirq
) {
307 error
= ops
->suspend_noirq(dev
);
308 suspend_report_result(ops
->suspend_noirq
, error
);
311 case PM_EVENT_RESUME
:
312 if (ops
->resume_noirq
) {
313 error
= ops
->resume_noirq(dev
);
314 suspend_report_result(ops
->resume_noirq
, error
);
317 #endif /* CONFIG_SUSPEND */
318 #ifdef CONFIG_HIBERNATION
319 case PM_EVENT_FREEZE
:
320 case PM_EVENT_QUIESCE
:
321 if (ops
->freeze_noirq
) {
322 error
= ops
->freeze_noirq(dev
);
323 suspend_report_result(ops
->freeze_noirq
, error
);
326 case PM_EVENT_HIBERNATE
:
327 if (ops
->poweroff_noirq
) {
328 error
= ops
->poweroff_noirq(dev
);
329 suspend_report_result(ops
->poweroff_noirq
, error
);
333 case PM_EVENT_RECOVER
:
334 if (ops
->thaw_noirq
) {
335 error
= ops
->thaw_noirq(dev
);
336 suspend_report_result(ops
->thaw_noirq
, error
);
339 case PM_EVENT_RESTORE
:
340 if (ops
->restore_noirq
) {
341 error
= ops
->restore_noirq(dev
);
342 suspend_report_result(ops
->restore_noirq
, error
);
345 #endif /* CONFIG_HIBERNATION */
350 if (initcall_debug
) {
351 rettime
= ktime_get();
352 delta
= ktime_sub(rettime
, calltime
);
353 printk("initcall %s_i+ returned %d after %Ld usecs\n",
354 dev_name(dev
), error
,
355 (unsigned long long)ktime_to_ns(delta
) >> 10);
361 static char *pm_verb(int event
)
364 case PM_EVENT_SUSPEND
:
366 case PM_EVENT_RESUME
:
368 case PM_EVENT_FREEZE
:
370 case PM_EVENT_QUIESCE
:
372 case PM_EVENT_HIBERNATE
:
376 case PM_EVENT_RESTORE
:
378 case PM_EVENT_RECOVER
:
381 return "(unknown PM event)";
385 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
387 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
388 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
389 ", may wakeup" : "");
392 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
395 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
396 kobject_name(&dev
->kobj
), pm_verb(state
.event
), info
, error
);
399 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
405 calltime
= ktime_get();
406 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
407 do_div(usecs64
, NSEC_PER_USEC
);
411 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
412 info
?: "", info
? " " : "", pm_verb(state
.event
),
413 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
416 /*------------------------- Resume routines -------------------------*/
419 * device_resume_noirq - Execute an "early resume" callback for given device.
420 * @dev: Device to handle.
421 * @state: PM transition of the system being carried out.
423 * The driver of @dev will not receive interrupts while this function is being
426 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
433 if (dev
->bus
&& dev
->bus
->pm
) {
434 pm_dev_dbg(dev
, state
, "EARLY ");
435 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
440 if (dev
->type
&& dev
->type
->pm
) {
441 pm_dev_dbg(dev
, state
, "EARLY type ");
442 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
447 if (dev
->class && dev
->class->pm
) {
448 pm_dev_dbg(dev
, state
, "EARLY class ");
449 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
458 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
459 * @state: PM transition of the system being carried out.
461 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
462 * enable device drivers to receive interrupts.
464 void dpm_resume_noirq(pm_message_t state
)
466 ktime_t starttime
= ktime_get();
468 mutex_lock(&dpm_list_mtx
);
469 while (!list_empty(&dpm_noirq_list
)) {
470 struct device
*dev
= to_device(dpm_noirq_list
.next
);
474 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
475 mutex_unlock(&dpm_list_mtx
);
477 error
= device_resume_noirq(dev
, state
);
479 pm_dev_err(dev
, state
, " early", error
);
481 mutex_lock(&dpm_list_mtx
);
484 mutex_unlock(&dpm_list_mtx
);
485 dpm_show_time(starttime
, state
, "early");
486 resume_device_irqs();
488 EXPORT_SYMBOL_GPL(dpm_resume_noirq
);
491 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
492 * @dev: Device to resume.
493 * @cb: Resume callback to execute.
495 static int legacy_resume(struct device
*dev
, int (*cb
)(struct device
*dev
))
500 calltime
= initcall_debug_start(dev
);
503 suspend_report_result(cb
, error
);
505 initcall_debug_report(dev
, calltime
, error
);
511 * device_resume - Execute "resume" callbacks for given device.
512 * @dev: Device to handle.
513 * @state: PM transition of the system being carried out.
514 * @async: If true, the device is being resumed asynchronously.
516 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
523 dpm_wait(dev
->parent
, async
);
526 dev
->power
.in_suspend
= false;
530 pm_dev_dbg(dev
, state
, "");
531 error
= pm_op(dev
, dev
->bus
->pm
, state
);
532 } else if (dev
->bus
->resume
) {
533 pm_dev_dbg(dev
, state
, "legacy ");
534 error
= legacy_resume(dev
, dev
->bus
->resume
);
542 pm_dev_dbg(dev
, state
, "type ");
543 error
= pm_op(dev
, dev
->type
->pm
, state
);
550 if (dev
->class->pm
) {
551 pm_dev_dbg(dev
, state
, "class ");
552 error
= pm_op(dev
, dev
->class->pm
, state
);
553 } else if (dev
->class->resume
) {
554 pm_dev_dbg(dev
, state
, "legacy class ");
555 error
= legacy_resume(dev
, dev
->class->resume
);
560 complete_all(&dev
->power
.completion
);
566 static void async_resume(void *data
, async_cookie_t cookie
)
568 struct device
*dev
= (struct device
*)data
;
571 error
= device_resume(dev
, pm_transition
, true);
573 pm_dev_err(dev
, pm_transition
, " async", error
);
577 static bool is_async(struct device
*dev
)
579 return dev
->power
.async_suspend
&& pm_async_enabled
580 && !pm_trace_is_enabled();
584 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
585 * @state: PM transition of the system being carried out.
587 * Execute the appropriate "resume" callback for all devices whose status
588 * indicates that they are suspended.
590 static void dpm_resume(pm_message_t state
)
593 ktime_t starttime
= ktime_get();
595 mutex_lock(&dpm_list_mtx
);
596 pm_transition
= state
;
599 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
600 INIT_COMPLETION(dev
->power
.completion
);
603 async_schedule(async_resume
, dev
);
607 while (!list_empty(&dpm_suspended_list
)) {
608 dev
= to_device(dpm_suspended_list
.next
);
610 if (!is_async(dev
)) {
613 mutex_unlock(&dpm_list_mtx
);
615 error
= device_resume(dev
, state
, false);
617 pm_dev_err(dev
, state
, "", error
);
619 mutex_lock(&dpm_list_mtx
);
621 if (!list_empty(&dev
->power
.entry
))
622 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
625 mutex_unlock(&dpm_list_mtx
);
626 async_synchronize_full();
627 dpm_show_time(starttime
, state
, NULL
);
631 * device_complete - Complete a PM transition for given device.
632 * @dev: Device to handle.
633 * @state: PM transition of the system being carried out.
635 static void device_complete(struct device
*dev
, pm_message_t state
)
639 if (dev
->class && dev
->class->pm
&& dev
->class->pm
->complete
) {
640 pm_dev_dbg(dev
, state
, "completing class ");
641 dev
->class->pm
->complete(dev
);
644 if (dev
->type
&& dev
->type
->pm
&& dev
->type
->pm
->complete
) {
645 pm_dev_dbg(dev
, state
, "completing type ");
646 dev
->type
->pm
->complete(dev
);
649 if (dev
->bus
&& dev
->bus
->pm
&& dev
->bus
->pm
->complete
) {
650 pm_dev_dbg(dev
, state
, "completing ");
651 dev
->bus
->pm
->complete(dev
);
658 * dpm_complete - Complete a PM transition for all non-sysdev devices.
659 * @state: PM transition of the system being carried out.
661 * Execute the ->complete() callbacks for all devices whose PM status is not
662 * DPM_ON (this allows new devices to be registered).
664 static void dpm_complete(pm_message_t state
)
666 struct list_head list
;
668 INIT_LIST_HEAD(&list
);
669 mutex_lock(&dpm_list_mtx
);
670 while (!list_empty(&dpm_prepared_list
)) {
671 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
674 dev
->power
.in_suspend
= false;
675 list_move(&dev
->power
.entry
, &list
);
676 mutex_unlock(&dpm_list_mtx
);
678 device_complete(dev
, state
);
679 pm_runtime_put_sync(dev
);
681 mutex_lock(&dpm_list_mtx
);
684 list_splice(&list
, &dpm_list
);
685 mutex_unlock(&dpm_list_mtx
);
689 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
690 * @state: PM transition of the system being carried out.
692 * Execute "resume" callbacks for all devices and complete the PM transition of
695 void dpm_resume_end(pm_message_t state
)
701 EXPORT_SYMBOL_GPL(dpm_resume_end
);
704 /*------------------------- Suspend routines -------------------------*/
707 * resume_event - Return a "resume" message for given "suspend" sleep state.
708 * @sleep_state: PM message representing a sleep state.
710 * Return a PM message representing the resume event corresponding to given
713 static pm_message_t
resume_event(pm_message_t sleep_state
)
715 switch (sleep_state
.event
) {
716 case PM_EVENT_SUSPEND
:
718 case PM_EVENT_FREEZE
:
719 case PM_EVENT_QUIESCE
:
721 case PM_EVENT_HIBERNATE
:
728 * device_suspend_noirq - Execute a "late suspend" callback for given device.
729 * @dev: Device to handle.
730 * @state: PM transition of the system being carried out.
732 * The driver of @dev will not receive interrupts while this function is being
735 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
739 if (dev
->class && dev
->class->pm
) {
740 pm_dev_dbg(dev
, state
, "LATE class ");
741 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
746 if (dev
->type
&& dev
->type
->pm
) {
747 pm_dev_dbg(dev
, state
, "LATE type ");
748 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
753 if (dev
->bus
&& dev
->bus
->pm
) {
754 pm_dev_dbg(dev
, state
, "LATE ");
755 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
763 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
764 * @state: PM transition of the system being carried out.
766 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
767 * handlers for all non-sysdev devices.
769 int dpm_suspend_noirq(pm_message_t state
)
771 ktime_t starttime
= ktime_get();
774 suspend_device_irqs();
775 mutex_lock(&dpm_list_mtx
);
776 while (!list_empty(&dpm_suspended_list
)) {
777 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
780 mutex_unlock(&dpm_list_mtx
);
782 error
= device_suspend_noirq(dev
, state
);
784 mutex_lock(&dpm_list_mtx
);
786 pm_dev_err(dev
, state
, " late", error
);
790 if (!list_empty(&dev
->power
.entry
))
791 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
794 mutex_unlock(&dpm_list_mtx
);
796 dpm_resume_noirq(resume_event(state
));
798 dpm_show_time(starttime
, state
, "late");
801 EXPORT_SYMBOL_GPL(dpm_suspend_noirq
);
804 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
805 * @dev: Device to suspend.
806 * @state: PM transition of the system being carried out.
807 * @cb: Suspend callback to execute.
809 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
810 int (*cb
)(struct device
*dev
, pm_message_t state
))
815 calltime
= initcall_debug_start(dev
);
817 error
= cb(dev
, state
);
818 suspend_report_result(cb
, error
);
820 initcall_debug_report(dev
, calltime
, error
);
826 * device_suspend - Execute "suspend" callbacks for given device.
827 * @dev: Device to handle.
828 * @state: PM transition of the system being carried out.
829 * @async: If true, the device is being suspended asynchronously.
831 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
835 dpm_wait_for_children(dev
, async
);
841 if (pm_wakeup_pending()) {
842 async_error
= -EBUSY
;
847 if (dev
->class->pm
) {
848 pm_dev_dbg(dev
, state
, "class ");
849 error
= pm_op(dev
, dev
->class->pm
, state
);
850 } else if (dev
->class->suspend
) {
851 pm_dev_dbg(dev
, state
, "legacy class ");
852 error
= legacy_suspend(dev
, state
, dev
->class->suspend
);
860 pm_dev_dbg(dev
, state
, "type ");
861 error
= pm_op(dev
, dev
->type
->pm
, state
);
869 pm_dev_dbg(dev
, state
, "");
870 error
= pm_op(dev
, dev
->bus
->pm
, state
);
871 } else if (dev
->bus
->suspend
) {
872 pm_dev_dbg(dev
, state
, "legacy ");
873 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
);
879 complete_all(&dev
->power
.completion
);
887 static void async_suspend(void *data
, async_cookie_t cookie
)
889 struct device
*dev
= (struct device
*)data
;
892 error
= __device_suspend(dev
, pm_transition
, true);
894 pm_dev_err(dev
, pm_transition
, " async", error
);
899 static int device_suspend(struct device
*dev
)
901 INIT_COMPLETION(dev
->power
.completion
);
903 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
905 async_schedule(async_suspend
, dev
);
909 return __device_suspend(dev
, pm_transition
, false);
913 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
914 * @state: PM transition of the system being carried out.
916 static int dpm_suspend(pm_message_t state
)
918 ktime_t starttime
= ktime_get();
921 mutex_lock(&dpm_list_mtx
);
922 pm_transition
= state
;
924 while (!list_empty(&dpm_prepared_list
)) {
925 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
928 mutex_unlock(&dpm_list_mtx
);
930 error
= device_suspend(dev
);
932 mutex_lock(&dpm_list_mtx
);
934 pm_dev_err(dev
, state
, "", error
);
938 if (!list_empty(&dev
->power
.entry
))
939 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
944 mutex_unlock(&dpm_list_mtx
);
945 async_synchronize_full();
949 dpm_show_time(starttime
, state
, NULL
);
954 * device_prepare - Prepare a device for system power transition.
955 * @dev: Device to handle.
956 * @state: PM transition of the system being carried out.
958 * Execute the ->prepare() callback(s) for given device. No new children of the
959 * device may be registered after this function has returned.
961 static int device_prepare(struct device
*dev
, pm_message_t state
)
967 if (dev
->bus
&& dev
->bus
->pm
&& dev
->bus
->pm
->prepare
) {
968 pm_dev_dbg(dev
, state
, "preparing ");
969 error
= dev
->bus
->pm
->prepare(dev
);
970 suspend_report_result(dev
->bus
->pm
->prepare
, error
);
975 if (dev
->type
&& dev
->type
->pm
&& dev
->type
->pm
->prepare
) {
976 pm_dev_dbg(dev
, state
, "preparing type ");
977 error
= dev
->type
->pm
->prepare(dev
);
978 suspend_report_result(dev
->type
->pm
->prepare
, error
);
983 if (dev
->class && dev
->class->pm
&& dev
->class->pm
->prepare
) {
984 pm_dev_dbg(dev
, state
, "preparing class ");
985 error
= dev
->class->pm
->prepare(dev
);
986 suspend_report_result(dev
->class->pm
->prepare
, error
);
995 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
996 * @state: PM transition of the system being carried out.
998 * Execute the ->prepare() callback(s) for all devices.
1000 static int dpm_prepare(pm_message_t state
)
1004 mutex_lock(&dpm_list_mtx
);
1005 while (!list_empty(&dpm_list
)) {
1006 struct device
*dev
= to_device(dpm_list
.next
);
1009 mutex_unlock(&dpm_list_mtx
);
1011 pm_runtime_get_noresume(dev
);
1012 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1013 pm_wakeup_event(dev
, 0);
1015 if (pm_wakeup_pending()) {
1016 pm_runtime_put_sync(dev
);
1019 error
= device_prepare(dev
, state
);
1022 mutex_lock(&dpm_list_mtx
);
1024 if (error
== -EAGAIN
) {
1029 printk(KERN_INFO
"PM: Device %s not prepared "
1030 "for power transition: code %d\n",
1031 kobject_name(&dev
->kobj
), error
);
1035 dev
->power
.in_suspend
= true;
1036 if (!list_empty(&dev
->power
.entry
))
1037 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1040 mutex_unlock(&dpm_list_mtx
);
1045 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1046 * @state: PM transition of the system being carried out.
1048 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1049 * callbacks for them.
1051 int dpm_suspend_start(pm_message_t state
)
1056 error
= dpm_prepare(state
);
1058 error
= dpm_suspend(state
);
1061 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1063 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1066 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1068 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1071 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1072 * @dev: Device to wait for.
1073 * @subordinate: Device that needs to wait for @dev.
1075 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1077 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1080 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);