PM: Avoid compiler warning in pm_noirq_op()
[linux-2.6/libata-dev.git] / drivers / base / power / main.c
blobe6d628012654e0edbf2b2081aa8dc524e7206a52
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>
29 #include <linux/suspend.h>
31 #include "../base.h"
32 #include "power.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
41 * dpm_list_mutex.
44 LIST_HEAD(dpm_list);
46 static DEFINE_MUTEX(dpm_list_mtx);
47 static pm_message_t pm_transition;
50 * Set once the preparation of devices for a PM transition has started, reset
51 * before starting to resume devices. Protected by dpm_list_mtx.
53 static bool transition_started;
55 static int async_error;
57 /**
58 * device_pm_init - Initialize the PM-related part of a device object.
59 * @dev: Device object being initialized.
61 void device_pm_init(struct device *dev)
63 dev->power.status = DPM_ON;
64 init_completion(&dev->power.completion);
65 complete_all(&dev->power.completion);
66 dev->power.wakeup = NULL;
67 spin_lock_init(&dev->power.lock);
68 pm_runtime_init(dev);
71 /**
72 * device_pm_lock - Lock the list of active devices used by the PM core.
74 void device_pm_lock(void)
76 mutex_lock(&dpm_list_mtx);
79 /**
80 * device_pm_unlock - Unlock the list of active devices used by the PM core.
82 void device_pm_unlock(void)
84 mutex_unlock(&dpm_list_mtx);
87 /**
88 * device_pm_add - Add a device to the PM core's list of active devices.
89 * @dev: Device to add to the list.
91 void device_pm_add(struct device *dev)
93 pr_debug("PM: Adding info for %s:%s\n",
94 dev->bus ? dev->bus->name : "No Bus",
95 kobject_name(&dev->kobj));
96 mutex_lock(&dpm_list_mtx);
97 if (dev->parent) {
98 if (dev->parent->power.status >= DPM_SUSPENDING)
99 dev_warn(dev, "parent %s should not be sleeping\n",
100 dev_name(dev->parent));
101 } else if (transition_started) {
103 * We refuse to register parentless devices while a PM
104 * transition is in progress in order to avoid leaving them
105 * unhandled down the road
107 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
110 list_add_tail(&dev->power.entry, &dpm_list);
111 mutex_unlock(&dpm_list_mtx);
115 * device_pm_remove - Remove a device from the PM core's list of active devices.
116 * @dev: Device to be removed from the list.
118 void device_pm_remove(struct device *dev)
120 pr_debug("PM: Removing info for %s:%s\n",
121 dev->bus ? dev->bus->name : "No Bus",
122 kobject_name(&dev->kobj));
123 complete_all(&dev->power.completion);
124 mutex_lock(&dpm_list_mtx);
125 list_del_init(&dev->power.entry);
126 mutex_unlock(&dpm_list_mtx);
127 device_wakeup_disable(dev);
128 pm_runtime_remove(dev);
132 * device_pm_move_before - Move device in the PM core's list of active devices.
133 * @deva: Device to move in dpm_list.
134 * @devb: Device @deva should come before.
136 void device_pm_move_before(struct device *deva, struct device *devb)
138 pr_debug("PM: Moving %s:%s before %s:%s\n",
139 deva->bus ? deva->bus->name : "No Bus",
140 kobject_name(&deva->kobj),
141 devb->bus ? devb->bus->name : "No Bus",
142 kobject_name(&devb->kobj));
143 /* Delete deva from dpm_list and reinsert before devb. */
144 list_move_tail(&deva->power.entry, &devb->power.entry);
148 * device_pm_move_after - Move device in the PM core's list of active devices.
149 * @deva: Device to move in dpm_list.
150 * @devb: Device @deva should come after.
152 void device_pm_move_after(struct device *deva, struct device *devb)
154 pr_debug("PM: Moving %s:%s after %s:%s\n",
155 deva->bus ? deva->bus->name : "No Bus",
156 kobject_name(&deva->kobj),
157 devb->bus ? devb->bus->name : "No Bus",
158 kobject_name(&devb->kobj));
159 /* Delete deva from dpm_list and reinsert after devb. */
160 list_move(&deva->power.entry, &devb->power.entry);
164 * device_pm_move_last - Move device to end of the PM core's list of devices.
165 * @dev: Device to move in dpm_list.
167 void device_pm_move_last(struct device *dev)
169 pr_debug("PM: Moving %s:%s to end of list\n",
170 dev->bus ? dev->bus->name : "No Bus",
171 kobject_name(&dev->kobj));
172 list_move_tail(&dev->power.entry, &dpm_list);
175 static ktime_t initcall_debug_start(struct device *dev)
177 ktime_t calltime = ktime_set(0, 0);
179 if (initcall_debug) {
180 pr_info("calling %s+ @ %i\n",
181 dev_name(dev), task_pid_nr(current));
182 calltime = ktime_get();
185 return calltime;
188 static void initcall_debug_report(struct device *dev, ktime_t calltime,
189 int error)
191 ktime_t delta, rettime;
193 if (initcall_debug) {
194 rettime = ktime_get();
195 delta = ktime_sub(rettime, calltime);
196 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
197 error, (unsigned long long)ktime_to_ns(delta) >> 10);
202 * dpm_wait - Wait for a PM operation to complete.
203 * @dev: Device to wait for.
204 * @async: If unset, wait only if the device's power.async_suspend flag is set.
206 static void dpm_wait(struct device *dev, bool async)
208 if (!dev)
209 return;
211 if (async || (pm_async_enabled && dev->power.async_suspend))
212 wait_for_completion(&dev->power.completion);
215 static int dpm_wait_fn(struct device *dev, void *async_ptr)
217 dpm_wait(dev, *((bool *)async_ptr));
218 return 0;
221 static void dpm_wait_for_children(struct device *dev, bool async)
223 device_for_each_child(dev, &async, dpm_wait_fn);
227 * pm_op - Execute the PM operation appropriate for given PM event.
228 * @dev: Device to handle.
229 * @ops: PM operations to choose from.
230 * @state: PM transition of the system being carried out.
232 static int pm_op(struct device *dev,
233 const struct dev_pm_ops *ops,
234 pm_message_t state)
236 int error = 0;
237 ktime_t calltime;
239 calltime = initcall_debug_start(dev);
241 switch (state.event) {
242 #ifdef CONFIG_SUSPEND
243 case PM_EVENT_SUSPEND:
244 if (ops->suspend) {
245 error = ops->suspend(dev);
246 suspend_report_result(ops->suspend, error);
248 break;
249 case PM_EVENT_RESUME:
250 if (ops->resume) {
251 error = ops->resume(dev);
252 suspend_report_result(ops->resume, error);
254 break;
255 #endif /* CONFIG_SUSPEND */
256 #ifdef CONFIG_HIBERNATION
257 case PM_EVENT_FREEZE:
258 case PM_EVENT_QUIESCE:
259 if (ops->freeze) {
260 error = ops->freeze(dev);
261 suspend_report_result(ops->freeze, error);
263 break;
264 case PM_EVENT_HIBERNATE:
265 if (ops->poweroff) {
266 error = ops->poweroff(dev);
267 suspend_report_result(ops->poweroff, error);
269 break;
270 case PM_EVENT_THAW:
271 case PM_EVENT_RECOVER:
272 if (ops->thaw) {
273 error = ops->thaw(dev);
274 suspend_report_result(ops->thaw, error);
276 break;
277 case PM_EVENT_RESTORE:
278 if (ops->restore) {
279 error = ops->restore(dev);
280 suspend_report_result(ops->restore, error);
282 break;
283 #endif /* CONFIG_HIBERNATION */
284 default:
285 error = -EINVAL;
288 initcall_debug_report(dev, calltime, error);
290 return error;
294 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
295 * @dev: Device to handle.
296 * @ops: PM operations to choose from.
297 * @state: PM transition of the system being carried out.
299 * The driver of @dev will not receive interrupts while this function is being
300 * executed.
302 static int pm_noirq_op(struct device *dev,
303 const struct dev_pm_ops *ops,
304 pm_message_t state)
306 int error = 0;
307 ktime_t calltime = ktime_set(0, 0), delta, rettime;
309 if (initcall_debug) {
310 pr_info("calling %s+ @ %i, parent: %s\n",
311 dev_name(dev), task_pid_nr(current),
312 dev->parent ? dev_name(dev->parent) : "none");
313 calltime = ktime_get();
316 switch (state.event) {
317 #ifdef CONFIG_SUSPEND
318 case PM_EVENT_SUSPEND:
319 if (ops->suspend_noirq) {
320 error = ops->suspend_noirq(dev);
321 suspend_report_result(ops->suspend_noirq, error);
323 break;
324 case PM_EVENT_RESUME:
325 if (ops->resume_noirq) {
326 error = ops->resume_noirq(dev);
327 suspend_report_result(ops->resume_noirq, error);
329 break;
330 #endif /* CONFIG_SUSPEND */
331 #ifdef CONFIG_HIBERNATION
332 case PM_EVENT_FREEZE:
333 case PM_EVENT_QUIESCE:
334 if (ops->freeze_noirq) {
335 error = ops->freeze_noirq(dev);
336 suspend_report_result(ops->freeze_noirq, error);
338 break;
339 case PM_EVENT_HIBERNATE:
340 if (ops->poweroff_noirq) {
341 error = ops->poweroff_noirq(dev);
342 suspend_report_result(ops->poweroff_noirq, error);
344 break;
345 case PM_EVENT_THAW:
346 case PM_EVENT_RECOVER:
347 if (ops->thaw_noirq) {
348 error = ops->thaw_noirq(dev);
349 suspend_report_result(ops->thaw_noirq, error);
351 break;
352 case PM_EVENT_RESTORE:
353 if (ops->restore_noirq) {
354 error = ops->restore_noirq(dev);
355 suspend_report_result(ops->restore_noirq, error);
357 break;
358 #endif /* CONFIG_HIBERNATION */
359 default:
360 error = -EINVAL;
363 if (initcall_debug) {
364 rettime = ktime_get();
365 delta = ktime_sub(rettime, calltime);
366 printk("initcall %s_i+ returned %d after %Ld usecs\n",
367 dev_name(dev), error,
368 (unsigned long long)ktime_to_ns(delta) >> 10);
371 return error;
374 static char *pm_verb(int event)
376 switch (event) {
377 case PM_EVENT_SUSPEND:
378 return "suspend";
379 case PM_EVENT_RESUME:
380 return "resume";
381 case PM_EVENT_FREEZE:
382 return "freeze";
383 case PM_EVENT_QUIESCE:
384 return "quiesce";
385 case PM_EVENT_HIBERNATE:
386 return "hibernate";
387 case PM_EVENT_THAW:
388 return "thaw";
389 case PM_EVENT_RESTORE:
390 return "restore";
391 case PM_EVENT_RECOVER:
392 return "recover";
393 default:
394 return "(unknown PM event)";
398 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
400 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
401 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
402 ", may wakeup" : "");
405 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
406 int error)
408 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
409 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
412 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
414 ktime_t calltime;
415 u64 usecs64;
416 int usecs;
418 calltime = ktime_get();
419 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
420 do_div(usecs64, NSEC_PER_USEC);
421 usecs = usecs64;
422 if (usecs == 0)
423 usecs = 1;
424 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
425 info ?: "", info ? " " : "", pm_verb(state.event),
426 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
429 /*------------------------- Resume routines -------------------------*/
432 * device_resume_noirq - Execute an "early resume" callback for given device.
433 * @dev: Device to handle.
434 * @state: PM transition of the system being carried out.
436 * The driver of @dev will not receive interrupts while this function is being
437 * executed.
439 static int device_resume_noirq(struct device *dev, pm_message_t state)
441 int error = 0;
443 TRACE_DEVICE(dev);
444 TRACE_RESUME(0);
446 if (dev->bus && dev->bus->pm) {
447 pm_dev_dbg(dev, state, "EARLY ");
448 error = pm_noirq_op(dev, dev->bus->pm, state);
449 if (error)
450 goto End;
453 if (dev->type && dev->type->pm) {
454 pm_dev_dbg(dev, state, "EARLY type ");
455 error = pm_noirq_op(dev, dev->type->pm, state);
456 if (error)
457 goto End;
460 if (dev->class && dev->class->pm) {
461 pm_dev_dbg(dev, state, "EARLY class ");
462 error = pm_noirq_op(dev, dev->class->pm, state);
465 End:
466 TRACE_RESUME(error);
467 return error;
471 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
472 * @state: PM transition of the system being carried out.
474 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
475 * enable device drivers to receive interrupts.
477 void dpm_resume_noirq(pm_message_t state)
479 struct list_head list;
480 ktime_t starttime = ktime_get();
482 INIT_LIST_HEAD(&list);
483 mutex_lock(&dpm_list_mtx);
484 transition_started = false;
485 while (!list_empty(&dpm_list)) {
486 struct device *dev = to_device(dpm_list.next);
488 get_device(dev);
489 if (dev->power.status > DPM_OFF) {
490 int error;
492 dev->power.status = DPM_OFF;
493 mutex_unlock(&dpm_list_mtx);
495 error = device_resume_noirq(dev, state);
497 mutex_lock(&dpm_list_mtx);
498 if (error)
499 pm_dev_err(dev, state, " early", error);
501 if (!list_empty(&dev->power.entry))
502 list_move_tail(&dev->power.entry, &list);
503 put_device(dev);
505 list_splice(&list, &dpm_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))
519 int error;
520 ktime_t calltime;
522 calltime = initcall_debug_start(dev);
524 error = cb(dev);
525 suspend_report_result(cb, error);
527 initcall_debug_report(dev, calltime, error);
529 return 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)
540 int error = 0;
542 TRACE_DEVICE(dev);
543 TRACE_RESUME(0);
545 dpm_wait(dev->parent, async);
546 device_lock(dev);
548 dev->power.status = DPM_RESUMING;
550 if (dev->bus) {
551 if (dev->bus->pm) {
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);
558 if (error)
559 goto End;
562 if (dev->type) {
563 if (dev->type->pm) {
564 pm_dev_dbg(dev, state, "type ");
565 error = pm_op(dev, dev->type->pm, state);
567 if (error)
568 goto End;
571 if (dev->class) {
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);
580 End:
581 device_unlock(dev);
582 complete_all(&dev->power.completion);
584 TRACE_RESUME(error);
585 return error;
588 static void async_resume(void *data, async_cookie_t cookie)
590 struct device *dev = (struct device *)data;
591 int error;
593 error = device_resume(dev, pm_transition, true);
594 if (error)
595 pm_dev_err(dev, pm_transition, " async", error);
596 put_device(dev);
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)
614 struct list_head list;
615 struct device *dev;
616 ktime_t starttime = ktime_get();
618 INIT_LIST_HEAD(&list);
619 mutex_lock(&dpm_list_mtx);
620 pm_transition = state;
621 async_error = 0;
623 list_for_each_entry(dev, &dpm_list, power.entry) {
624 if (dev->power.status < DPM_OFF)
625 continue;
627 INIT_COMPLETION(dev->power.completion);
628 if (is_async(dev)) {
629 get_device(dev);
630 async_schedule(async_resume, dev);
634 while (!list_empty(&dpm_list)) {
635 dev = to_device(dpm_list.next);
636 get_device(dev);
637 if (dev->power.status >= DPM_OFF && !is_async(dev)) {
638 int error;
640 mutex_unlock(&dpm_list_mtx);
642 error = device_resume(dev, state, false);
644 mutex_lock(&dpm_list_mtx);
645 if (error)
646 pm_dev_err(dev, state, "", error);
647 } else if (dev->power.status == DPM_SUSPENDING) {
648 /* Allow new children of the device to be registered */
649 dev->power.status = DPM_RESUMING;
651 if (!list_empty(&dev->power.entry))
652 list_move_tail(&dev->power.entry, &list);
653 put_device(dev);
655 list_splice(&list, &dpm_list);
656 mutex_unlock(&dpm_list_mtx);
657 async_synchronize_full();
658 dpm_show_time(starttime, state, NULL);
662 * device_complete - Complete a PM transition for given device.
663 * @dev: Device to handle.
664 * @state: PM transition of the system being carried out.
666 static void device_complete(struct device *dev, pm_message_t state)
668 device_lock(dev);
670 if (dev->class && dev->class->pm && dev->class->pm->complete) {
671 pm_dev_dbg(dev, state, "completing class ");
672 dev->class->pm->complete(dev);
675 if (dev->type && dev->type->pm && dev->type->pm->complete) {
676 pm_dev_dbg(dev, state, "completing type ");
677 dev->type->pm->complete(dev);
680 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
681 pm_dev_dbg(dev, state, "completing ");
682 dev->bus->pm->complete(dev);
685 device_unlock(dev);
689 * dpm_complete - Complete a PM transition for all non-sysdev devices.
690 * @state: PM transition of the system being carried out.
692 * Execute the ->complete() callbacks for all devices whose PM status is not
693 * DPM_ON (this allows new devices to be registered).
695 static void dpm_complete(pm_message_t state)
697 struct list_head list;
699 INIT_LIST_HEAD(&list);
700 mutex_lock(&dpm_list_mtx);
701 transition_started = false;
702 while (!list_empty(&dpm_list)) {
703 struct device *dev = to_device(dpm_list.prev);
705 get_device(dev);
706 if (dev->power.status > DPM_ON) {
707 dev->power.status = DPM_ON;
708 mutex_unlock(&dpm_list_mtx);
710 device_complete(dev, state);
711 pm_runtime_put_sync(dev);
713 mutex_lock(&dpm_list_mtx);
715 if (!list_empty(&dev->power.entry))
716 list_move(&dev->power.entry, &list);
717 put_device(dev);
719 list_splice(&list, &dpm_list);
720 mutex_unlock(&dpm_list_mtx);
724 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
725 * @state: PM transition of the system being carried out.
727 * Execute "resume" callbacks for all devices and complete the PM transition of
728 * the system.
730 void dpm_resume_end(pm_message_t state)
732 might_sleep();
733 dpm_resume(state);
734 dpm_complete(state);
736 EXPORT_SYMBOL_GPL(dpm_resume_end);
739 /*------------------------- Suspend routines -------------------------*/
742 * resume_event - Return a "resume" message for given "suspend" sleep state.
743 * @sleep_state: PM message representing a sleep state.
745 * Return a PM message representing the resume event corresponding to given
746 * sleep state.
748 static pm_message_t resume_event(pm_message_t sleep_state)
750 switch (sleep_state.event) {
751 case PM_EVENT_SUSPEND:
752 return PMSG_RESUME;
753 case PM_EVENT_FREEZE:
754 case PM_EVENT_QUIESCE:
755 return PMSG_RECOVER;
756 case PM_EVENT_HIBERNATE:
757 return PMSG_RESTORE;
759 return PMSG_ON;
763 * device_suspend_noirq - Execute a "late suspend" callback for given device.
764 * @dev: Device to handle.
765 * @state: PM transition of the system being carried out.
767 * The driver of @dev will not receive interrupts while this function is being
768 * executed.
770 static int device_suspend_noirq(struct device *dev, pm_message_t state)
772 int error = 0;
774 if (dev->class && dev->class->pm) {
775 pm_dev_dbg(dev, state, "LATE class ");
776 error = pm_noirq_op(dev, dev->class->pm, state);
777 if (error)
778 goto End;
781 if (dev->type && dev->type->pm) {
782 pm_dev_dbg(dev, state, "LATE type ");
783 error = pm_noirq_op(dev, dev->type->pm, state);
784 if (error)
785 goto End;
788 if (dev->bus && dev->bus->pm) {
789 pm_dev_dbg(dev, state, "LATE ");
790 error = pm_noirq_op(dev, dev->bus->pm, state);
793 End:
794 return error;
798 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
799 * @state: PM transition of the system being carried out.
801 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
802 * handlers for all non-sysdev devices.
804 int dpm_suspend_noirq(pm_message_t state)
806 struct list_head list;
807 ktime_t starttime = ktime_get();
808 int error = 0;
810 INIT_LIST_HEAD(&list);
811 suspend_device_irqs();
812 mutex_lock(&dpm_list_mtx);
813 while (!list_empty(&dpm_list)) {
814 struct device *dev = to_device(dpm_list.prev);
816 get_device(dev);
817 mutex_unlock(&dpm_list_mtx);
819 error = device_suspend_noirq(dev, state);
821 mutex_lock(&dpm_list_mtx);
822 if (error) {
823 pm_dev_err(dev, state, " late", error);
824 put_device(dev);
825 break;
827 dev->power.status = DPM_OFF_IRQ;
828 if (!list_empty(&dev->power.entry))
829 list_move(&dev->power.entry, &list);
830 put_device(dev);
832 list_splice_tail(&list, &dpm_list);
833 mutex_unlock(&dpm_list_mtx);
834 if (error)
835 dpm_resume_noirq(resume_event(state));
836 else
837 dpm_show_time(starttime, state, "late");
838 return error;
840 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
843 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
844 * @dev: Device to suspend.
845 * @state: PM transition of the system being carried out.
846 * @cb: Suspend callback to execute.
848 static int legacy_suspend(struct device *dev, pm_message_t state,
849 int (*cb)(struct device *dev, pm_message_t state))
851 int error;
852 ktime_t calltime;
854 calltime = initcall_debug_start(dev);
856 error = cb(dev, state);
857 suspend_report_result(cb, error);
859 initcall_debug_report(dev, calltime, error);
861 return error;
865 * device_suspend - Execute "suspend" callbacks for given device.
866 * @dev: Device to handle.
867 * @state: PM transition of the system being carried out.
868 * @async: If true, the device is being suspended asynchronously.
870 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
872 int error = 0;
874 dpm_wait_for_children(dev, async);
875 device_lock(dev);
877 if (async_error)
878 goto End;
880 if (pm_wakeup_pending()) {
881 async_error = -EBUSY;
882 goto End;
885 if (dev->class) {
886 if (dev->class->pm) {
887 pm_dev_dbg(dev, state, "class ");
888 error = pm_op(dev, dev->class->pm, state);
889 } else if (dev->class->suspend) {
890 pm_dev_dbg(dev, state, "legacy class ");
891 error = legacy_suspend(dev, state, dev->class->suspend);
893 if (error)
894 goto End;
897 if (dev->type) {
898 if (dev->type->pm) {
899 pm_dev_dbg(dev, state, "type ");
900 error = pm_op(dev, dev->type->pm, state);
902 if (error)
903 goto End;
906 if (dev->bus) {
907 if (dev->bus->pm) {
908 pm_dev_dbg(dev, state, "");
909 error = pm_op(dev, dev->bus->pm, state);
910 } else if (dev->bus->suspend) {
911 pm_dev_dbg(dev, state, "legacy ");
912 error = legacy_suspend(dev, state, dev->bus->suspend);
916 if (!error)
917 dev->power.status = DPM_OFF;
919 End:
920 device_unlock(dev);
921 complete_all(&dev->power.completion);
923 if (error)
924 async_error = error;
926 return error;
929 static void async_suspend(void *data, async_cookie_t cookie)
931 struct device *dev = (struct device *)data;
932 int error;
934 error = __device_suspend(dev, pm_transition, true);
935 if (error)
936 pm_dev_err(dev, pm_transition, " async", error);
938 put_device(dev);
941 static int device_suspend(struct device *dev)
943 INIT_COMPLETION(dev->power.completion);
945 if (pm_async_enabled && dev->power.async_suspend) {
946 get_device(dev);
947 async_schedule(async_suspend, dev);
948 return 0;
951 return __device_suspend(dev, pm_transition, false);
955 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
956 * @state: PM transition of the system being carried out.
958 static int dpm_suspend(pm_message_t state)
960 struct list_head list;
961 ktime_t starttime = ktime_get();
962 int error = 0;
964 INIT_LIST_HEAD(&list);
965 mutex_lock(&dpm_list_mtx);
966 pm_transition = state;
967 async_error = 0;
968 while (!list_empty(&dpm_list)) {
969 struct device *dev = to_device(dpm_list.prev);
971 get_device(dev);
972 mutex_unlock(&dpm_list_mtx);
974 error = device_suspend(dev);
976 mutex_lock(&dpm_list_mtx);
977 if (error) {
978 pm_dev_err(dev, state, "", error);
979 put_device(dev);
980 break;
982 if (!list_empty(&dev->power.entry))
983 list_move(&dev->power.entry, &list);
984 put_device(dev);
985 if (async_error)
986 break;
988 list_splice(&list, dpm_list.prev);
989 mutex_unlock(&dpm_list_mtx);
990 async_synchronize_full();
991 if (!error)
992 error = async_error;
993 if (!error)
994 dpm_show_time(starttime, state, NULL);
995 return error;
999 * device_prepare - Prepare a device for system power transition.
1000 * @dev: Device to handle.
1001 * @state: PM transition of the system being carried out.
1003 * Execute the ->prepare() callback(s) for given device. No new children of the
1004 * device may be registered after this function has returned.
1006 static int device_prepare(struct device *dev, pm_message_t state)
1008 int error = 0;
1010 device_lock(dev);
1012 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
1013 pm_dev_dbg(dev, state, "preparing ");
1014 error = dev->bus->pm->prepare(dev);
1015 suspend_report_result(dev->bus->pm->prepare, error);
1016 if (error)
1017 goto End;
1020 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
1021 pm_dev_dbg(dev, state, "preparing type ");
1022 error = dev->type->pm->prepare(dev);
1023 suspend_report_result(dev->type->pm->prepare, error);
1024 if (error)
1025 goto End;
1028 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
1029 pm_dev_dbg(dev, state, "preparing class ");
1030 error = dev->class->pm->prepare(dev);
1031 suspend_report_result(dev->class->pm->prepare, error);
1033 End:
1034 device_unlock(dev);
1036 return error;
1040 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1041 * @state: PM transition of the system being carried out.
1043 * Execute the ->prepare() callback(s) for all devices.
1045 static int dpm_prepare(pm_message_t state)
1047 struct list_head list;
1048 int error = 0;
1050 INIT_LIST_HEAD(&list);
1051 mutex_lock(&dpm_list_mtx);
1052 transition_started = true;
1053 while (!list_empty(&dpm_list)) {
1054 struct device *dev = to_device(dpm_list.next);
1056 get_device(dev);
1057 dev->power.status = DPM_PREPARING;
1058 mutex_unlock(&dpm_list_mtx);
1060 pm_runtime_get_noresume(dev);
1061 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1062 pm_wakeup_event(dev, 0);
1064 if (pm_wakeup_pending()) {
1065 pm_runtime_put_sync(dev);
1066 error = -EBUSY;
1067 } else {
1068 error = device_prepare(dev, state);
1071 mutex_lock(&dpm_list_mtx);
1072 if (error) {
1073 dev->power.status = DPM_ON;
1074 if (error == -EAGAIN) {
1075 put_device(dev);
1076 error = 0;
1077 continue;
1079 printk(KERN_INFO "PM: Device %s not prepared "
1080 "for power transition: code %d\n",
1081 kobject_name(&dev->kobj), error);
1082 put_device(dev);
1083 break;
1085 dev->power.status = DPM_SUSPENDING;
1086 if (!list_empty(&dev->power.entry))
1087 list_move_tail(&dev->power.entry, &list);
1088 put_device(dev);
1090 list_splice(&list, &dpm_list);
1091 mutex_unlock(&dpm_list_mtx);
1092 return error;
1096 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1097 * @state: PM transition of the system being carried out.
1099 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1100 * callbacks for them.
1102 int dpm_suspend_start(pm_message_t state)
1104 int error;
1106 might_sleep();
1107 error = dpm_prepare(state);
1108 if (!error)
1109 error = dpm_suspend(state);
1110 return error;
1112 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1114 void __suspend_report_result(const char *function, void *fn, int ret)
1116 if (ret)
1117 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1119 EXPORT_SYMBOL_GPL(__suspend_report_result);
1122 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1123 * @dev: Device to wait for.
1124 * @subordinate: Device that needs to wait for @dev.
1126 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1128 dpm_wait(dev, subordinate->power.async_suspend);
1129 return async_error;
1131 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);