Staging: netwave: delete the driver
[linux/fpc-iii.git] / drivers / base / power / main.c
blob941fcb87e52a12765df2aaa2e2ab21267693af9f
1 /*
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>
23 #include <linux/pm.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>
30 #include "../base.h"
31 #include "power.h"
34 * The entries in the dpm_list list are in a depth first order, simply
35 * because children are guaranteed to be discovered after parents, and
36 * are inserted at the back of the list on discovery.
38 * Since device_pm_add() may be called with a device lock held,
39 * we must never try to acquire a device lock while holding
40 * dpm_list_mutex.
43 LIST_HEAD(dpm_list);
45 static DEFINE_MUTEX(dpm_list_mtx);
46 static pm_message_t pm_transition;
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.
52 static bool transition_started;
54 /**
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.status = DPM_ON;
61 init_completion(&dev->power.completion);
62 pm_runtime_init(dev);
65 /**
66 * device_pm_lock - Lock the list of active devices used by the PM core.
68 void device_pm_lock(void)
70 mutex_lock(&dpm_list_mtx);
73 /**
74 * device_pm_unlock - Unlock the list of active devices used by the PM core.
76 void device_pm_unlock(void)
78 mutex_unlock(&dpm_list_mtx);
81 /**
82 * device_pm_add - Add a device to the PM core's list of active devices.
83 * @dev: Device to add to the list.
85 void device_pm_add(struct device *dev)
87 pr_debug("PM: Adding info for %s:%s\n",
88 dev->bus ? dev->bus->name : "No Bus",
89 kobject_name(&dev->kobj));
90 mutex_lock(&dpm_list_mtx);
91 if (dev->parent) {
92 if (dev->parent->power.status >= DPM_SUSPENDING)
93 dev_warn(dev, "parent %s should not be sleeping\n",
94 dev_name(dev->parent));
95 } else if (transition_started) {
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
101 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
104 list_add_tail(&dev->power.entry, &dpm_list);
105 mutex_unlock(&dpm_list_mtx);
109 * device_pm_remove - Remove a device from the PM core's list of active devices.
110 * @dev: Device to be removed from the list.
112 void device_pm_remove(struct device *dev)
114 pr_debug("PM: Removing info for %s:%s\n",
115 dev->bus ? dev->bus->name : "No Bus",
116 kobject_name(&dev->kobj));
117 complete_all(&dev->power.completion);
118 mutex_lock(&dpm_list_mtx);
119 list_del_init(&dev->power.entry);
120 mutex_unlock(&dpm_list_mtx);
121 pm_runtime_remove(dev);
125 * 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.
129 void device_pm_move_before(struct device *deva, struct device *devb)
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);
141 * 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.
145 void device_pm_move_after(struct device *deva, struct device *devb)
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);
157 * device_pm_move_last - Move device to end of the PM core's list of devices.
158 * @dev: Device to move in dpm_list.
160 void device_pm_move_last(struct device *dev)
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);
168 static ktime_t initcall_debug_start(struct device *dev)
170 ktime_t calltime = ktime_set(0, 0);
172 if (initcall_debug) {
173 pr_info("calling %s+ @ %i\n",
174 dev_name(dev), task_pid_nr(current));
175 calltime = ktime_get();
178 return calltime;
181 static void initcall_debug_report(struct device *dev, ktime_t calltime,
182 int error)
184 ktime_t delta, rettime;
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);
195 * 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.
199 static void dpm_wait(struct device *dev, bool async)
201 if (!dev)
202 return;
204 if (async || (pm_async_enabled && dev->power.async_suspend))
205 wait_for_completion(&dev->power.completion);
208 static int dpm_wait_fn(struct device *dev, void *async_ptr)
210 dpm_wait(dev, *((bool *)async_ptr));
211 return 0;
214 static void dpm_wait_for_children(struct device *dev, bool async)
216 device_for_each_child(dev, &async, dpm_wait_fn);
220 * 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.
225 static int pm_op(struct device *dev,
226 const struct dev_pm_ops *ops,
227 pm_message_t state)
229 int error = 0;
230 ktime_t calltime;
232 calltime = initcall_debug_start(dev);
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);
241 break;
242 case PM_EVENT_RESUME:
243 if (ops->resume) {
244 error = ops->resume(dev);
245 suspend_report_result(ops->resume, error);
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);
256 break;
257 case PM_EVENT_HIBERNATE:
258 if (ops->poweroff) {
259 error = ops->poweroff(dev);
260 suspend_report_result(ops->poweroff, error);
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);
269 break;
270 case PM_EVENT_RESTORE:
271 if (ops->restore) {
272 error = ops->restore(dev);
273 suspend_report_result(ops->restore, error);
275 break;
276 #endif /* CONFIG_HIBERNATION */
277 default:
278 error = -EINVAL;
281 initcall_debug_report(dev, calltime, error);
283 return error;
287 * 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.
292 * The driver of @dev will not receive interrupts while this function is being
293 * executed.
295 static int pm_noirq_op(struct device *dev,
296 const struct dev_pm_ops *ops,
297 pm_message_t state)
299 int error = 0;
300 ktime_t calltime, delta, rettime;
302 if (initcall_debug) {
303 pr_info("calling %s+ @ %i, parent: %s\n",
304 dev_name(dev), task_pid_nr(current),
305 dev->parent ? dev_name(dev->parent) : "none");
306 calltime = ktime_get();
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);
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);
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);
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);
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);
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);
350 break;
351 #endif /* CONFIG_HIBERNATION */
352 default:
353 error = -EINVAL;
356 if (initcall_debug) {
357 rettime = ktime_get();
358 delta = ktime_sub(rettime, calltime);
359 printk("initcall %s_i+ returned %d after %Ld usecs\n",
360 dev_name(dev), error,
361 (unsigned long long)ktime_to_ns(delta) >> 10);
364 return error;
367 static char *pm_verb(int event)
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)";
391 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
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" : "");
398 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
399 int error)
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);
405 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
407 ktime_t calltime;
408 s64 usecs64;
409 int usecs;
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);
422 /*------------------------- Resume routines -------------------------*/
425 * 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.
429 * The driver of @dev will not receive interrupts while this function is being
430 * executed.
432 static int device_resume_noirq(struct device *dev, pm_message_t state)
434 int error = 0;
436 TRACE_DEVICE(dev);
437 TRACE_RESUME(0);
439 if (dev->bus && dev->bus->pm) {
440 pm_dev_dbg(dev, state, "EARLY ");
441 error = pm_noirq_op(dev, dev->bus->pm, state);
442 if (error)
443 goto End;
446 if (dev->type && dev->type->pm) {
447 pm_dev_dbg(dev, state, "EARLY type ");
448 error = pm_noirq_op(dev, dev->type->pm, state);
449 if (error)
450 goto End;
453 if (dev->class && dev->class->pm) {
454 pm_dev_dbg(dev, state, "EARLY class ");
455 error = pm_noirq_op(dev, dev->class->pm, state);
458 End:
459 TRACE_RESUME(error);
460 return error;
464 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
465 * @state: PM transition of the system being carried out.
467 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
468 * enable device drivers to receive interrupts.
470 void dpm_resume_noirq(pm_message_t state)
472 struct device *dev;
473 ktime_t starttime = ktime_get();
475 mutex_lock(&dpm_list_mtx);
476 transition_started = false;
477 list_for_each_entry(dev, &dpm_list, power.entry)
478 if (dev->power.status > DPM_OFF) {
479 int error;
481 dev->power.status = DPM_OFF;
482 error = device_resume_noirq(dev, state);
483 if (error)
484 pm_dev_err(dev, state, " early", error);
486 mutex_unlock(&dpm_list_mtx);
487 dpm_show_time(starttime, state, "early");
488 resume_device_irqs();
490 EXPORT_SYMBOL_GPL(dpm_resume_noirq);
493 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
494 * @dev: Device to resume.
495 * @cb: Resume callback to execute.
497 static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
499 int error;
500 ktime_t calltime;
502 calltime = initcall_debug_start(dev);
504 error = cb(dev);
505 suspend_report_result(cb, error);
507 initcall_debug_report(dev, calltime, error);
509 return error;
513 * device_resume - Execute "resume" callbacks for given device.
514 * @dev: Device to handle.
515 * @state: PM transition of the system being carried out.
516 * @async: If true, the device is being resumed asynchronously.
518 static int device_resume(struct device *dev, pm_message_t state, bool async)
520 int error = 0;
522 TRACE_DEVICE(dev);
523 TRACE_RESUME(0);
525 dpm_wait(dev->parent, async);
526 device_lock(dev);
528 dev->power.status = DPM_RESUMING;
530 if (dev->bus) {
531 if (dev->bus->pm) {
532 pm_dev_dbg(dev, state, "");
533 error = pm_op(dev, dev->bus->pm, state);
534 } else if (dev->bus->resume) {
535 pm_dev_dbg(dev, state, "legacy ");
536 error = legacy_resume(dev, dev->bus->resume);
538 if (error)
539 goto End;
542 if (dev->type) {
543 if (dev->type->pm) {
544 pm_dev_dbg(dev, state, "type ");
545 error = pm_op(dev, dev->type->pm, state);
547 if (error)
548 goto End;
551 if (dev->class) {
552 if (dev->class->pm) {
553 pm_dev_dbg(dev, state, "class ");
554 error = pm_op(dev, dev->class->pm, state);
555 } else if (dev->class->resume) {
556 pm_dev_dbg(dev, state, "legacy class ");
557 error = legacy_resume(dev, dev->class->resume);
560 End:
561 device_unlock(dev);
562 complete_all(&dev->power.completion);
564 TRACE_RESUME(error);
565 return error;
568 static void async_resume(void *data, async_cookie_t cookie)
570 struct device *dev = (struct device *)data;
571 int error;
573 error = device_resume(dev, pm_transition, true);
574 if (error)
575 pm_dev_err(dev, pm_transition, " async", error);
576 put_device(dev);
579 static bool is_async(struct device *dev)
581 return dev->power.async_suspend && pm_async_enabled
582 && !pm_trace_is_enabled();
586 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
587 * @state: PM transition of the system being carried out.
589 * Execute the appropriate "resume" callback for all devices whose status
590 * indicates that they are suspended.
592 static void dpm_resume(pm_message_t state)
594 struct list_head list;
595 struct device *dev;
596 ktime_t starttime = ktime_get();
598 INIT_LIST_HEAD(&list);
599 mutex_lock(&dpm_list_mtx);
600 pm_transition = state;
602 list_for_each_entry(dev, &dpm_list, power.entry) {
603 if (dev->power.status < DPM_OFF)
604 continue;
606 INIT_COMPLETION(dev->power.completion);
607 if (is_async(dev)) {
608 get_device(dev);
609 async_schedule(async_resume, dev);
613 while (!list_empty(&dpm_list)) {
614 dev = to_device(dpm_list.next);
615 get_device(dev);
616 if (dev->power.status >= DPM_OFF && !is_async(dev)) {
617 int error;
619 mutex_unlock(&dpm_list_mtx);
621 error = device_resume(dev, state, false);
623 mutex_lock(&dpm_list_mtx);
624 if (error)
625 pm_dev_err(dev, state, "", error);
626 } else if (dev->power.status == DPM_SUSPENDING) {
627 /* Allow new children of the device to be registered */
628 dev->power.status = DPM_RESUMING;
630 if (!list_empty(&dev->power.entry))
631 list_move_tail(&dev->power.entry, &list);
632 put_device(dev);
634 list_splice(&list, &dpm_list);
635 mutex_unlock(&dpm_list_mtx);
636 async_synchronize_full();
637 dpm_show_time(starttime, state, NULL);
641 * device_complete - Complete a PM transition for given device.
642 * @dev: Device to handle.
643 * @state: PM transition of the system being carried out.
645 static void device_complete(struct device *dev, pm_message_t state)
647 device_lock(dev);
649 if (dev->class && dev->class->pm && dev->class->pm->complete) {
650 pm_dev_dbg(dev, state, "completing class ");
651 dev->class->pm->complete(dev);
654 if (dev->type && dev->type->pm && dev->type->pm->complete) {
655 pm_dev_dbg(dev, state, "completing type ");
656 dev->type->pm->complete(dev);
659 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
660 pm_dev_dbg(dev, state, "completing ");
661 dev->bus->pm->complete(dev);
664 device_unlock(dev);
668 * dpm_complete - Complete a PM transition for all non-sysdev devices.
669 * @state: PM transition of the system being carried out.
671 * Execute the ->complete() callbacks for all devices whose PM status is not
672 * DPM_ON (this allows new devices to be registered).
674 static void dpm_complete(pm_message_t state)
676 struct list_head list;
678 INIT_LIST_HEAD(&list);
679 mutex_lock(&dpm_list_mtx);
680 transition_started = false;
681 while (!list_empty(&dpm_list)) {
682 struct device *dev = to_device(dpm_list.prev);
684 get_device(dev);
685 if (dev->power.status > DPM_ON) {
686 dev->power.status = DPM_ON;
687 mutex_unlock(&dpm_list_mtx);
689 device_complete(dev, state);
690 pm_runtime_put_sync(dev);
692 mutex_lock(&dpm_list_mtx);
694 if (!list_empty(&dev->power.entry))
695 list_move(&dev->power.entry, &list);
696 put_device(dev);
698 list_splice(&list, &dpm_list);
699 mutex_unlock(&dpm_list_mtx);
703 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
704 * @state: PM transition of the system being carried out.
706 * Execute "resume" callbacks for all devices and complete the PM transition of
707 * the system.
709 void dpm_resume_end(pm_message_t state)
711 might_sleep();
712 dpm_resume(state);
713 dpm_complete(state);
715 EXPORT_SYMBOL_GPL(dpm_resume_end);
718 /*------------------------- Suspend routines -------------------------*/
721 * resume_event - Return a "resume" message for given "suspend" sleep state.
722 * @sleep_state: PM message representing a sleep state.
724 * Return a PM message representing the resume event corresponding to given
725 * sleep state.
727 static pm_message_t resume_event(pm_message_t sleep_state)
729 switch (sleep_state.event) {
730 case PM_EVENT_SUSPEND:
731 return PMSG_RESUME;
732 case PM_EVENT_FREEZE:
733 case PM_EVENT_QUIESCE:
734 return PMSG_RECOVER;
735 case PM_EVENT_HIBERNATE:
736 return PMSG_RESTORE;
738 return PMSG_ON;
742 * device_suspend_noirq - Execute a "late suspend" callback for given device.
743 * @dev: Device to handle.
744 * @state: PM transition of the system being carried out.
746 * The driver of @dev will not receive interrupts while this function is being
747 * executed.
749 static int device_suspend_noirq(struct device *dev, pm_message_t state)
751 int error = 0;
753 if (dev->class && dev->class->pm) {
754 pm_dev_dbg(dev, state, "LATE class ");
755 error = pm_noirq_op(dev, dev->class->pm, state);
756 if (error)
757 goto End;
760 if (dev->type && dev->type->pm) {
761 pm_dev_dbg(dev, state, "LATE type ");
762 error = pm_noirq_op(dev, dev->type->pm, state);
763 if (error)
764 goto End;
767 if (dev->bus && dev->bus->pm) {
768 pm_dev_dbg(dev, state, "LATE ");
769 error = pm_noirq_op(dev, dev->bus->pm, state);
772 End:
773 return error;
777 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
778 * @state: PM transition of the system being carried out.
780 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
781 * handlers for all non-sysdev devices.
783 int dpm_suspend_noirq(pm_message_t state)
785 struct device *dev;
786 ktime_t starttime = ktime_get();
787 int error = 0;
789 suspend_device_irqs();
790 mutex_lock(&dpm_list_mtx);
791 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
792 error = device_suspend_noirq(dev, state);
793 if (error) {
794 pm_dev_err(dev, state, " late", error);
795 break;
797 dev->power.status = DPM_OFF_IRQ;
799 mutex_unlock(&dpm_list_mtx);
800 if (error)
801 dpm_resume_noirq(resume_event(state));
802 else
803 dpm_show_time(starttime, state, "late");
804 return error;
806 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
809 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
810 * @dev: Device to suspend.
811 * @state: PM transition of the system being carried out.
812 * @cb: Suspend callback to execute.
814 static int legacy_suspend(struct device *dev, pm_message_t state,
815 int (*cb)(struct device *dev, pm_message_t state))
817 int error;
818 ktime_t calltime;
820 calltime = initcall_debug_start(dev);
822 error = cb(dev, state);
823 suspend_report_result(cb, error);
825 initcall_debug_report(dev, calltime, error);
827 return error;
830 static int async_error;
833 * device_suspend - Execute "suspend" callbacks for given device.
834 * @dev: Device to handle.
835 * @state: PM transition of the system being carried out.
836 * @async: If true, the device is being suspended asynchronously.
838 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
840 int error = 0;
842 dpm_wait_for_children(dev, async);
843 device_lock(dev);
845 if (async_error)
846 goto End;
848 if (dev->class) {
849 if (dev->class->pm) {
850 pm_dev_dbg(dev, state, "class ");
851 error = pm_op(dev, dev->class->pm, state);
852 } else if (dev->class->suspend) {
853 pm_dev_dbg(dev, state, "legacy class ");
854 error = legacy_suspend(dev, state, dev->class->suspend);
856 if (error)
857 goto End;
860 if (dev->type) {
861 if (dev->type->pm) {
862 pm_dev_dbg(dev, state, "type ");
863 error = pm_op(dev, dev->type->pm, state);
865 if (error)
866 goto End;
869 if (dev->bus) {
870 if (dev->bus->pm) {
871 pm_dev_dbg(dev, state, "");
872 error = pm_op(dev, dev->bus->pm, state);
873 } else if (dev->bus->suspend) {
874 pm_dev_dbg(dev, state, "legacy ");
875 error = legacy_suspend(dev, state, dev->bus->suspend);
879 if (!error)
880 dev->power.status = DPM_OFF;
882 End:
883 device_unlock(dev);
884 complete_all(&dev->power.completion);
886 return error;
889 static void async_suspend(void *data, async_cookie_t cookie)
891 struct device *dev = (struct device *)data;
892 int error;
894 error = __device_suspend(dev, pm_transition, true);
895 if (error) {
896 pm_dev_err(dev, pm_transition, " async", error);
897 async_error = error;
900 put_device(dev);
903 static int device_suspend(struct device *dev)
905 INIT_COMPLETION(dev->power.completion);
907 if (pm_async_enabled && dev->power.async_suspend) {
908 get_device(dev);
909 async_schedule(async_suspend, dev);
910 return 0;
913 return __device_suspend(dev, pm_transition, false);
917 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
918 * @state: PM transition of the system being carried out.
920 static int dpm_suspend(pm_message_t state)
922 struct list_head list;
923 ktime_t starttime = ktime_get();
924 int error = 0;
926 INIT_LIST_HEAD(&list);
927 mutex_lock(&dpm_list_mtx);
928 pm_transition = state;
929 async_error = 0;
930 while (!list_empty(&dpm_list)) {
931 struct device *dev = to_device(dpm_list.prev);
933 get_device(dev);
934 mutex_unlock(&dpm_list_mtx);
936 error = device_suspend(dev);
938 mutex_lock(&dpm_list_mtx);
939 if (error) {
940 pm_dev_err(dev, state, "", error);
941 put_device(dev);
942 break;
944 if (!list_empty(&dev->power.entry))
945 list_move(&dev->power.entry, &list);
946 put_device(dev);
947 if (async_error)
948 break;
950 list_splice(&list, dpm_list.prev);
951 mutex_unlock(&dpm_list_mtx);
952 async_synchronize_full();
953 if (!error)
954 error = async_error;
955 if (!error)
956 dpm_show_time(starttime, state, NULL);
957 return error;
961 * device_prepare - Prepare a device for system power transition.
962 * @dev: Device to handle.
963 * @state: PM transition of the system being carried out.
965 * Execute the ->prepare() callback(s) for given device. No new children of the
966 * device may be registered after this function has returned.
968 static int device_prepare(struct device *dev, pm_message_t state)
970 int error = 0;
972 device_lock(dev);
974 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
975 pm_dev_dbg(dev, state, "preparing ");
976 error = dev->bus->pm->prepare(dev);
977 suspend_report_result(dev->bus->pm->prepare, error);
978 if (error)
979 goto End;
982 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
983 pm_dev_dbg(dev, state, "preparing type ");
984 error = dev->type->pm->prepare(dev);
985 suspend_report_result(dev->type->pm->prepare, error);
986 if (error)
987 goto End;
990 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
991 pm_dev_dbg(dev, state, "preparing class ");
992 error = dev->class->pm->prepare(dev);
993 suspend_report_result(dev->class->pm->prepare, error);
995 End:
996 device_unlock(dev);
998 return error;
1002 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1003 * @state: PM transition of the system being carried out.
1005 * Execute the ->prepare() callback(s) for all devices.
1007 static int dpm_prepare(pm_message_t state)
1009 struct list_head list;
1010 int error = 0;
1012 INIT_LIST_HEAD(&list);
1013 mutex_lock(&dpm_list_mtx);
1014 transition_started = true;
1015 while (!list_empty(&dpm_list)) {
1016 struct device *dev = to_device(dpm_list.next);
1018 get_device(dev);
1019 dev->power.status = DPM_PREPARING;
1020 mutex_unlock(&dpm_list_mtx);
1022 pm_runtime_get_noresume(dev);
1023 if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
1024 /* Wake-up requested during system sleep transition. */
1025 pm_runtime_put_sync(dev);
1026 error = -EBUSY;
1027 } else {
1028 error = device_prepare(dev, state);
1031 mutex_lock(&dpm_list_mtx);
1032 if (error) {
1033 dev->power.status = DPM_ON;
1034 if (error == -EAGAIN) {
1035 put_device(dev);
1036 error = 0;
1037 continue;
1039 printk(KERN_ERR "PM: Failed to prepare device %s "
1040 "for power transition: error %d\n",
1041 kobject_name(&dev->kobj), error);
1042 put_device(dev);
1043 break;
1045 dev->power.status = DPM_SUSPENDING;
1046 if (!list_empty(&dev->power.entry))
1047 list_move_tail(&dev->power.entry, &list);
1048 put_device(dev);
1050 list_splice(&list, &dpm_list);
1051 mutex_unlock(&dpm_list_mtx);
1052 return error;
1056 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1057 * @state: PM transition of the system being carried out.
1059 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1060 * callbacks for them.
1062 int dpm_suspend_start(pm_message_t state)
1064 int error;
1066 might_sleep();
1067 error = dpm_prepare(state);
1068 if (!error)
1069 error = dpm_suspend(state);
1070 return error;
1072 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1074 void __suspend_report_result(const char *function, void *fn, int ret)
1076 if (ret)
1077 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1079 EXPORT_SYMBOL_GPL(__suspend_report_result);
1082 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1083 * @dev: Device to wait for.
1084 * @subordinate: Device that needs to wait for @dev.
1086 void device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1088 dpm_wait(dev, subordinate->power.async_suspend);
1090 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);