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
;
53 * Set once the preparation of devices for a PM transition has started, reset
54 * before starting to resume devices. Protected by dpm_list_mtx.
56 static bool transition_started
;
58 static int async_error
;
61 * device_pm_init - Initialize the PM-related part of a device object.
62 * @dev: Device object being initialized.
64 void device_pm_init(struct device
*dev
)
66 dev
->power
.status
= DPM_ON
;
67 init_completion(&dev
->power
.completion
);
68 complete_all(&dev
->power
.completion
);
69 dev
->power
.wakeup
= NULL
;
70 spin_lock_init(&dev
->power
.lock
);
75 * device_pm_lock - Lock the list of active devices used by the PM core.
77 void device_pm_lock(void)
79 mutex_lock(&dpm_list_mtx
);
83 * device_pm_unlock - Unlock the list of active devices used by the PM core.
85 void device_pm_unlock(void)
87 mutex_unlock(&dpm_list_mtx
);
91 * device_pm_add - Add a device to the PM core's list of active devices.
92 * @dev: Device to add to the list.
94 void device_pm_add(struct device
*dev
)
96 pr_debug("PM: Adding info for %s:%s\n",
97 dev
->bus
? dev
->bus
->name
: "No Bus",
98 kobject_name(&dev
->kobj
));
99 mutex_lock(&dpm_list_mtx
);
101 if (dev
->parent
->power
.status
>= DPM_SUSPENDING
)
102 dev_warn(dev
, "parent %s should not be sleeping\n",
103 dev_name(dev
->parent
));
104 } else if (transition_started
) {
106 * We refuse to register parentless devices while a PM
107 * transition is in progress in order to avoid leaving them
108 * unhandled down the road
110 dev_WARN(dev
, "Parentless device registered during a PM transaction\n");
113 list_add_tail(&dev
->power
.entry
, &dpm_list
);
114 mutex_unlock(&dpm_list_mtx
);
118 * device_pm_remove - Remove a device from the PM core's list of active devices.
119 * @dev: Device to be removed from the list.
121 void device_pm_remove(struct device
*dev
)
123 pr_debug("PM: Removing info for %s:%s\n",
124 dev
->bus
? dev
->bus
->name
: "No Bus",
125 kobject_name(&dev
->kobj
));
126 complete_all(&dev
->power
.completion
);
127 mutex_lock(&dpm_list_mtx
);
128 list_del_init(&dev
->power
.entry
);
129 mutex_unlock(&dpm_list_mtx
);
130 device_wakeup_disable(dev
);
131 pm_runtime_remove(dev
);
135 * device_pm_move_before - 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 before.
139 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
141 pr_debug("PM: Moving %s:%s before %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 before devb. */
147 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
151 * device_pm_move_after - Move device in the PM core's list of active devices.
152 * @deva: Device to move in dpm_list.
153 * @devb: Device @deva should come after.
155 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
157 pr_debug("PM: Moving %s:%s after %s:%s\n",
158 deva
->bus
? deva
->bus
->name
: "No Bus",
159 kobject_name(&deva
->kobj
),
160 devb
->bus
? devb
->bus
->name
: "No Bus",
161 kobject_name(&devb
->kobj
));
162 /* Delete deva from dpm_list and reinsert after devb. */
163 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
167 * device_pm_move_last - Move device to end of the PM core's list of devices.
168 * @dev: Device to move in dpm_list.
170 void device_pm_move_last(struct device
*dev
)
172 pr_debug("PM: Moving %s:%s to end of list\n",
173 dev
->bus
? dev
->bus
->name
: "No Bus",
174 kobject_name(&dev
->kobj
));
175 list_move_tail(&dev
->power
.entry
, &dpm_list
);
178 static ktime_t
initcall_debug_start(struct device
*dev
)
180 ktime_t calltime
= ktime_set(0, 0);
182 if (initcall_debug
) {
183 pr_info("calling %s+ @ %i\n",
184 dev_name(dev
), task_pid_nr(current
));
185 calltime
= ktime_get();
191 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
194 ktime_t delta
, rettime
;
196 if (initcall_debug
) {
197 rettime
= ktime_get();
198 delta
= ktime_sub(rettime
, calltime
);
199 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
200 error
, (unsigned long long)ktime_to_ns(delta
) >> 10);
205 * dpm_wait - Wait for a PM operation to complete.
206 * @dev: Device to wait for.
207 * @async: If unset, wait only if the device's power.async_suspend flag is set.
209 static void dpm_wait(struct device
*dev
, bool async
)
214 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
215 wait_for_completion(&dev
->power
.completion
);
218 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
220 dpm_wait(dev
, *((bool *)async_ptr
));
224 static void dpm_wait_for_children(struct device
*dev
, bool async
)
226 device_for_each_child(dev
, &async
, dpm_wait_fn
);
230 * pm_op - Execute the PM operation appropriate for given PM event.
231 * @dev: Device to handle.
232 * @ops: PM operations to choose from.
233 * @state: PM transition of the system being carried out.
235 static int pm_op(struct device
*dev
,
236 const struct dev_pm_ops
*ops
,
242 calltime
= initcall_debug_start(dev
);
244 switch (state
.event
) {
245 #ifdef CONFIG_SUSPEND
246 case PM_EVENT_SUSPEND
:
248 error
= ops
->suspend(dev
);
249 suspend_report_result(ops
->suspend
, error
);
252 case PM_EVENT_RESUME
:
254 error
= ops
->resume(dev
);
255 suspend_report_result(ops
->resume
, error
);
258 #endif /* CONFIG_SUSPEND */
259 #ifdef CONFIG_HIBERNATION
260 case PM_EVENT_FREEZE
:
261 case PM_EVENT_QUIESCE
:
263 error
= ops
->freeze(dev
);
264 suspend_report_result(ops
->freeze
, error
);
267 case PM_EVENT_HIBERNATE
:
269 error
= ops
->poweroff(dev
);
270 suspend_report_result(ops
->poweroff
, error
);
274 case PM_EVENT_RECOVER
:
276 error
= ops
->thaw(dev
);
277 suspend_report_result(ops
->thaw
, error
);
280 case PM_EVENT_RESTORE
:
282 error
= ops
->restore(dev
);
283 suspend_report_result(ops
->restore
, error
);
286 #endif /* CONFIG_HIBERNATION */
291 initcall_debug_report(dev
, calltime
, error
);
297 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
298 * @dev: Device to handle.
299 * @ops: PM operations to choose from.
300 * @state: PM transition of the system being carried out.
302 * The driver of @dev will not receive interrupts while this function is being
305 static int pm_noirq_op(struct device
*dev
,
306 const struct dev_pm_ops
*ops
,
310 ktime_t calltime
= ktime_set(0, 0), delta
, rettime
;
312 if (initcall_debug
) {
313 pr_info("calling %s+ @ %i, parent: %s\n",
314 dev_name(dev
), task_pid_nr(current
),
315 dev
->parent
? dev_name(dev
->parent
) : "none");
316 calltime
= ktime_get();
319 switch (state
.event
) {
320 #ifdef CONFIG_SUSPEND
321 case PM_EVENT_SUSPEND
:
322 if (ops
->suspend_noirq
) {
323 error
= ops
->suspend_noirq(dev
);
324 suspend_report_result(ops
->suspend_noirq
, error
);
327 case PM_EVENT_RESUME
:
328 if (ops
->resume_noirq
) {
329 error
= ops
->resume_noirq(dev
);
330 suspend_report_result(ops
->resume_noirq
, error
);
333 #endif /* CONFIG_SUSPEND */
334 #ifdef CONFIG_HIBERNATION
335 case PM_EVENT_FREEZE
:
336 case PM_EVENT_QUIESCE
:
337 if (ops
->freeze_noirq
) {
338 error
= ops
->freeze_noirq(dev
);
339 suspend_report_result(ops
->freeze_noirq
, error
);
342 case PM_EVENT_HIBERNATE
:
343 if (ops
->poweroff_noirq
) {
344 error
= ops
->poweroff_noirq(dev
);
345 suspend_report_result(ops
->poweroff_noirq
, error
);
349 case PM_EVENT_RECOVER
:
350 if (ops
->thaw_noirq
) {
351 error
= ops
->thaw_noirq(dev
);
352 suspend_report_result(ops
->thaw_noirq
, error
);
355 case PM_EVENT_RESTORE
:
356 if (ops
->restore_noirq
) {
357 error
= ops
->restore_noirq(dev
);
358 suspend_report_result(ops
->restore_noirq
, error
);
361 #endif /* CONFIG_HIBERNATION */
366 if (initcall_debug
) {
367 rettime
= ktime_get();
368 delta
= ktime_sub(rettime
, calltime
);
369 printk("initcall %s_i+ returned %d after %Ld usecs\n",
370 dev_name(dev
), error
,
371 (unsigned long long)ktime_to_ns(delta
) >> 10);
377 static char *pm_verb(int event
)
380 case PM_EVENT_SUSPEND
:
382 case PM_EVENT_RESUME
:
384 case PM_EVENT_FREEZE
:
386 case PM_EVENT_QUIESCE
:
388 case PM_EVENT_HIBERNATE
:
392 case PM_EVENT_RESTORE
:
394 case PM_EVENT_RECOVER
:
397 return "(unknown PM event)";
401 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
403 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
404 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
405 ", may wakeup" : "");
408 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
411 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
412 kobject_name(&dev
->kobj
), pm_verb(state
.event
), info
, error
);
415 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
421 calltime
= ktime_get();
422 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
423 do_div(usecs64
, NSEC_PER_USEC
);
427 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
428 info
?: "", info
? " " : "", pm_verb(state
.event
),
429 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
432 /*------------------------- Resume routines -------------------------*/
435 * device_resume_noirq - Execute an "early resume" callback for given device.
436 * @dev: Device to handle.
437 * @state: PM transition of the system being carried out.
439 * The driver of @dev will not receive interrupts while this function is being
442 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
449 if (dev
->bus
&& dev
->bus
->pm
) {
450 pm_dev_dbg(dev
, state
, "EARLY ");
451 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
456 if (dev
->type
&& dev
->type
->pm
) {
457 pm_dev_dbg(dev
, state
, "EARLY type ");
458 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
463 if (dev
->class && dev
->class->pm
) {
464 pm_dev_dbg(dev
, state
, "EARLY class ");
465 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
474 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
475 * @state: PM transition of the system being carried out.
477 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
478 * enable device drivers to receive interrupts.
480 void dpm_resume_noirq(pm_message_t state
)
482 ktime_t starttime
= ktime_get();
484 mutex_lock(&dpm_list_mtx
);
485 transition_started
= false;
486 while (!list_empty(&dpm_noirq_list
)) {
487 struct device
*dev
= to_device(dpm_noirq_list
.next
);
490 if (dev
->power
.status
> DPM_OFF
) {
493 dev
->power
.status
= DPM_OFF
;
494 mutex_unlock(&dpm_list_mtx
);
496 error
= device_resume_noirq(dev
, state
);
498 mutex_lock(&dpm_list_mtx
);
500 pm_dev_err(dev
, state
, " early", error
);
502 if (!list_empty(&dev
->power
.entry
))
503 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
506 mutex_unlock(&dpm_list_mtx
);
507 dpm_show_time(starttime
, state
, "early");
508 resume_device_irqs();
510 EXPORT_SYMBOL_GPL(dpm_resume_noirq
);
513 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
514 * @dev: Device to resume.
515 * @cb: Resume callback to execute.
517 static int legacy_resume(struct device
*dev
, int (*cb
)(struct device
*dev
))
522 calltime
= initcall_debug_start(dev
);
525 suspend_report_result(cb
, error
);
527 initcall_debug_report(dev
, calltime
, error
);
533 * device_resume - Execute "resume" callbacks for given device.
534 * @dev: Device to handle.
535 * @state: PM transition of the system being carried out.
536 * @async: If true, the device is being resumed asynchronously.
538 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
545 dpm_wait(dev
->parent
, async
);
548 dev
->power
.status
= DPM_RESUMING
;
552 pm_dev_dbg(dev
, state
, "");
553 error
= pm_op(dev
, dev
->bus
->pm
, state
);
554 } else if (dev
->bus
->resume
) {
555 pm_dev_dbg(dev
, state
, "legacy ");
556 error
= legacy_resume(dev
, dev
->bus
->resume
);
564 pm_dev_dbg(dev
, state
, "type ");
565 error
= pm_op(dev
, dev
->type
->pm
, state
);
572 if (dev
->class->pm
) {
573 pm_dev_dbg(dev
, state
, "class ");
574 error
= pm_op(dev
, dev
->class->pm
, state
);
575 } else if (dev
->class->resume
) {
576 pm_dev_dbg(dev
, state
, "legacy class ");
577 error
= legacy_resume(dev
, dev
->class->resume
);
582 complete_all(&dev
->power
.completion
);
588 static void async_resume(void *data
, async_cookie_t cookie
)
590 struct device
*dev
= (struct device
*)data
;
593 error
= device_resume(dev
, pm_transition
, true);
595 pm_dev_err(dev
, pm_transition
, " async", error
);
599 static bool is_async(struct device
*dev
)
601 return dev
->power
.async_suspend
&& pm_async_enabled
602 && !pm_trace_is_enabled();
606 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
607 * @state: PM transition of the system being carried out.
609 * Execute the appropriate "resume" callback for all devices whose status
610 * indicates that they are suspended.
612 static void dpm_resume(pm_message_t state
)
615 ktime_t starttime
= ktime_get();
617 mutex_lock(&dpm_list_mtx
);
618 pm_transition
= state
;
621 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
622 if (dev
->power
.status
< DPM_OFF
)
625 INIT_COMPLETION(dev
->power
.completion
);
628 async_schedule(async_resume
, dev
);
632 while (!list_empty(&dpm_suspended_list
)) {
633 dev
= to_device(dpm_suspended_list
.next
);
635 if (dev
->power
.status
>= DPM_OFF
&& !is_async(dev
)) {
638 mutex_unlock(&dpm_list_mtx
);
640 error
= device_resume(dev
, state
, false);
642 mutex_lock(&dpm_list_mtx
);
644 pm_dev_err(dev
, state
, "", error
);
646 if (!list_empty(&dev
->power
.entry
))
647 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
650 mutex_unlock(&dpm_list_mtx
);
651 async_synchronize_full();
652 dpm_show_time(starttime
, state
, NULL
);
656 * device_complete - Complete a PM transition for given device.
657 * @dev: Device to handle.
658 * @state: PM transition of the system being carried out.
660 static void device_complete(struct device
*dev
, pm_message_t state
)
664 if (dev
->class && dev
->class->pm
&& dev
->class->pm
->complete
) {
665 pm_dev_dbg(dev
, state
, "completing class ");
666 dev
->class->pm
->complete(dev
);
669 if (dev
->type
&& dev
->type
->pm
&& dev
->type
->pm
->complete
) {
670 pm_dev_dbg(dev
, state
, "completing type ");
671 dev
->type
->pm
->complete(dev
);
674 if (dev
->bus
&& dev
->bus
->pm
&& dev
->bus
->pm
->complete
) {
675 pm_dev_dbg(dev
, state
, "completing ");
676 dev
->bus
->pm
->complete(dev
);
683 * dpm_complete - Complete a PM transition for all non-sysdev devices.
684 * @state: PM transition of the system being carried out.
686 * Execute the ->complete() callbacks for all devices whose PM status is not
687 * DPM_ON (this allows new devices to be registered).
689 static void dpm_complete(pm_message_t state
)
691 struct list_head list
;
693 INIT_LIST_HEAD(&list
);
694 mutex_lock(&dpm_list_mtx
);
695 transition_started
= false;
696 while (!list_empty(&dpm_prepared_list
)) {
697 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
700 if (dev
->power
.status
> DPM_ON
) {
701 dev
->power
.status
= DPM_ON
;
702 mutex_unlock(&dpm_list_mtx
);
704 device_complete(dev
, state
);
705 pm_runtime_put_sync(dev
);
707 mutex_lock(&dpm_list_mtx
);
709 if (!list_empty(&dev
->power
.entry
))
710 list_move(&dev
->power
.entry
, &list
);
713 list_splice(&list
, &dpm_list
);
714 mutex_unlock(&dpm_list_mtx
);
718 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
719 * @state: PM transition of the system being carried out.
721 * Execute "resume" callbacks for all devices and complete the PM transition of
724 void dpm_resume_end(pm_message_t state
)
730 EXPORT_SYMBOL_GPL(dpm_resume_end
);
733 /*------------------------- Suspend routines -------------------------*/
736 * resume_event - Return a "resume" message for given "suspend" sleep state.
737 * @sleep_state: PM message representing a sleep state.
739 * Return a PM message representing the resume event corresponding to given
742 static pm_message_t
resume_event(pm_message_t sleep_state
)
744 switch (sleep_state
.event
) {
745 case PM_EVENT_SUSPEND
:
747 case PM_EVENT_FREEZE
:
748 case PM_EVENT_QUIESCE
:
750 case PM_EVENT_HIBERNATE
:
757 * device_suspend_noirq - Execute a "late suspend" callback for given device.
758 * @dev: Device to handle.
759 * @state: PM transition of the system being carried out.
761 * The driver of @dev will not receive interrupts while this function is being
764 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
768 if (dev
->class && dev
->class->pm
) {
769 pm_dev_dbg(dev
, state
, "LATE class ");
770 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
775 if (dev
->type
&& dev
->type
->pm
) {
776 pm_dev_dbg(dev
, state
, "LATE type ");
777 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
782 if (dev
->bus
&& dev
->bus
->pm
) {
783 pm_dev_dbg(dev
, state
, "LATE ");
784 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
792 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
793 * @state: PM transition of the system being carried out.
795 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
796 * handlers for all non-sysdev devices.
798 int dpm_suspend_noirq(pm_message_t state
)
800 ktime_t starttime
= ktime_get();
803 suspend_device_irqs();
804 mutex_lock(&dpm_list_mtx
);
805 while (!list_empty(&dpm_suspended_list
)) {
806 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
809 mutex_unlock(&dpm_list_mtx
);
811 error
= device_suspend_noirq(dev
, state
);
813 mutex_lock(&dpm_list_mtx
);
815 pm_dev_err(dev
, state
, " late", error
);
819 dev
->power
.status
= DPM_OFF_IRQ
;
820 if (!list_empty(&dev
->power
.entry
))
821 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
824 mutex_unlock(&dpm_list_mtx
);
826 dpm_resume_noirq(resume_event(state
));
828 dpm_show_time(starttime
, state
, "late");
831 EXPORT_SYMBOL_GPL(dpm_suspend_noirq
);
834 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
835 * @dev: Device to suspend.
836 * @state: PM transition of the system being carried out.
837 * @cb: Suspend callback to execute.
839 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
840 int (*cb
)(struct device
*dev
, pm_message_t state
))
845 calltime
= initcall_debug_start(dev
);
847 error
= cb(dev
, state
);
848 suspend_report_result(cb
, error
);
850 initcall_debug_report(dev
, calltime
, error
);
856 * device_suspend - Execute "suspend" callbacks for given device.
857 * @dev: Device to handle.
858 * @state: PM transition of the system being carried out.
859 * @async: If true, the device is being suspended asynchronously.
861 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
865 dpm_wait_for_children(dev
, async
);
871 if (pm_wakeup_pending()) {
872 async_error
= -EBUSY
;
877 if (dev
->class->pm
) {
878 pm_dev_dbg(dev
, state
, "class ");
879 error
= pm_op(dev
, dev
->class->pm
, state
);
880 } else if (dev
->class->suspend
) {
881 pm_dev_dbg(dev
, state
, "legacy class ");
882 error
= legacy_suspend(dev
, state
, dev
->class->suspend
);
890 pm_dev_dbg(dev
, state
, "type ");
891 error
= pm_op(dev
, dev
->type
->pm
, state
);
899 pm_dev_dbg(dev
, state
, "");
900 error
= pm_op(dev
, dev
->bus
->pm
, state
);
901 } else if (dev
->bus
->suspend
) {
902 pm_dev_dbg(dev
, state
, "legacy ");
903 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
);
908 dev
->power
.status
= DPM_OFF
;
912 complete_all(&dev
->power
.completion
);
920 static void async_suspend(void *data
, async_cookie_t cookie
)
922 struct device
*dev
= (struct device
*)data
;
925 error
= __device_suspend(dev
, pm_transition
, true);
927 pm_dev_err(dev
, pm_transition
, " async", error
);
932 static int device_suspend(struct device
*dev
)
934 INIT_COMPLETION(dev
->power
.completion
);
936 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
938 async_schedule(async_suspend
, dev
);
942 return __device_suspend(dev
, pm_transition
, false);
946 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
947 * @state: PM transition of the system being carried out.
949 static int dpm_suspend(pm_message_t state
)
951 ktime_t starttime
= ktime_get();
954 mutex_lock(&dpm_list_mtx
);
955 pm_transition
= state
;
957 while (!list_empty(&dpm_prepared_list
)) {
958 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
961 mutex_unlock(&dpm_list_mtx
);
963 error
= device_suspend(dev
);
965 mutex_lock(&dpm_list_mtx
);
967 pm_dev_err(dev
, state
, "", error
);
971 if (!list_empty(&dev
->power
.entry
))
972 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
977 mutex_unlock(&dpm_list_mtx
);
978 async_synchronize_full();
982 dpm_show_time(starttime
, state
, NULL
);
987 * device_prepare - Prepare a device for system power transition.
988 * @dev: Device to handle.
989 * @state: PM transition of the system being carried out.
991 * Execute the ->prepare() callback(s) for given device. No new children of the
992 * device may be registered after this function has returned.
994 static int device_prepare(struct device
*dev
, pm_message_t state
)
1000 if (dev
->bus
&& dev
->bus
->pm
&& dev
->bus
->pm
->prepare
) {
1001 pm_dev_dbg(dev
, state
, "preparing ");
1002 error
= dev
->bus
->pm
->prepare(dev
);
1003 suspend_report_result(dev
->bus
->pm
->prepare
, error
);
1008 if (dev
->type
&& dev
->type
->pm
&& dev
->type
->pm
->prepare
) {
1009 pm_dev_dbg(dev
, state
, "preparing type ");
1010 error
= dev
->type
->pm
->prepare(dev
);
1011 suspend_report_result(dev
->type
->pm
->prepare
, error
);
1016 if (dev
->class && dev
->class->pm
&& dev
->class->pm
->prepare
) {
1017 pm_dev_dbg(dev
, state
, "preparing class ");
1018 error
= dev
->class->pm
->prepare(dev
);
1019 suspend_report_result(dev
->class->pm
->prepare
, error
);
1028 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1029 * @state: PM transition of the system being carried out.
1031 * Execute the ->prepare() callback(s) for all devices.
1033 static int dpm_prepare(pm_message_t state
)
1037 mutex_lock(&dpm_list_mtx
);
1038 transition_started
= true;
1039 while (!list_empty(&dpm_list
)) {
1040 struct device
*dev
= to_device(dpm_list
.next
);
1043 dev
->power
.status
= DPM_PREPARING
;
1044 mutex_unlock(&dpm_list_mtx
);
1046 pm_runtime_get_noresume(dev
);
1047 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1048 pm_wakeup_event(dev
, 0);
1050 if (pm_wakeup_pending()) {
1051 pm_runtime_put_sync(dev
);
1054 error
= device_prepare(dev
, state
);
1057 mutex_lock(&dpm_list_mtx
);
1059 dev
->power
.status
= DPM_ON
;
1060 if (error
== -EAGAIN
) {
1065 printk(KERN_INFO
"PM: Device %s not prepared "
1066 "for power transition: code %d\n",
1067 kobject_name(&dev
->kobj
), error
);
1071 dev
->power
.status
= DPM_SUSPENDING
;
1072 if (!list_empty(&dev
->power
.entry
))
1073 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1076 mutex_unlock(&dpm_list_mtx
);
1081 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1082 * @state: PM transition of the system being carried out.
1084 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1085 * callbacks for them.
1087 int dpm_suspend_start(pm_message_t state
)
1092 error
= dpm_prepare(state
);
1094 error
= dpm_suspend(state
);
1097 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1099 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1102 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1104 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1107 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1108 * @dev: Device to wait for.
1109 * @subordinate: Device that needs to wait for @dev.
1111 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1113 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1116 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);