PM: Update kerneldoc comments in drivers/base/power/main.c
[cascardo/linux.git] / drivers / base / power / main.c
1 /*
2  * drivers/base/power/main.c - Where the driver meets power management.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released under the GPLv2
8  *
9  *
10  * The driver model core calls device_pm_add() when a device is registered.
11  * This will intialize the embedded device_pm_info object in the device
12  * and add it to the list of power-controlled devices. sysfs entries for
13  * controlling device power management will also be added.
14  *
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.
18  */
19
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/rwsem.h>
27 #include <linux/interrupt.h>
28
29 #include "../base.h"
30 #include "power.h"
31
32 /*
33  * The entries in the dpm_list list are in a depth first order, simply
34  * because children are guaranteed to be discovered after parents, and
35  * are inserted at the back of the list on discovery.
36  *
37  * Since device_pm_add() may be called with a device semaphore held,
38  * we must never try to acquire a device semaphore while holding
39  * dpm_list_mutex.
40  */
41
42 LIST_HEAD(dpm_list);
43
44 static DEFINE_MUTEX(dpm_list_mtx);
45
46 /*
47  * Set once the preparation of devices for a PM transition has started, reset
48  * before starting to resume devices.  Protected by dpm_list_mtx.
49  */
50 static bool transition_started;
51
52 /**
53  * device_pm_init - Initialize the PM-related part of a device object.
54  * @dev: Device object being initialized.
55  */
56 void device_pm_init(struct device *dev)
57 {
58         dev->power.status = DPM_ON;
59         pm_runtime_init(dev);
60 }
61
62 /**
63  * device_pm_lock - Lock the list of active devices used by the PM core.
64  */
65 void device_pm_lock(void)
66 {
67         mutex_lock(&dpm_list_mtx);
68 }
69
70 /**
71  * device_pm_unlock - Unlock the list of active devices used by the PM core.
72  */
73 void device_pm_unlock(void)
74 {
75         mutex_unlock(&dpm_list_mtx);
76 }
77
78 /**
79  * device_pm_add - Add a device to the PM core's list of active devices.
80  * @dev: Device to add to the list.
81  */
82 void device_pm_add(struct device *dev)
83 {
84         pr_debug("PM: Adding info for %s:%s\n",
85                  dev->bus ? dev->bus->name : "No Bus",
86                  kobject_name(&dev->kobj));
87         mutex_lock(&dpm_list_mtx);
88         if (dev->parent) {
89                 if (dev->parent->power.status >= DPM_SUSPENDING)
90                         dev_warn(dev, "parent %s should not be sleeping\n",
91                                  dev_name(dev->parent));
92         } else if (transition_started) {
93                 /*
94                  * We refuse to register parentless devices while a PM
95                  * transition is in progress in order to avoid leaving them
96                  * unhandled down the road
97                  */
98                 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
99         }
100
101         list_add_tail(&dev->power.entry, &dpm_list);
102         mutex_unlock(&dpm_list_mtx);
103 }
104
105 /**
106  * device_pm_remove - Remove a device from the PM core's list of active devices.
107  * @dev: Device to be removed from the list.
108  */
109 void device_pm_remove(struct device *dev)
110 {
111         pr_debug("PM: Removing info for %s:%s\n",
112                  dev->bus ? dev->bus->name : "No Bus",
113                  kobject_name(&dev->kobj));
114         mutex_lock(&dpm_list_mtx);
115         list_del_init(&dev->power.entry);
116         mutex_unlock(&dpm_list_mtx);
117         pm_runtime_remove(dev);
118 }
119
120 /**
121  * device_pm_move_before - Move device in the PM core's list of active devices.
122  * @deva: Device to move in dpm_list.
123  * @devb: Device @deva should come before.
124  */
125 void device_pm_move_before(struct device *deva, struct device *devb)
126 {
127         pr_debug("PM: Moving %s:%s before %s:%s\n",
128                  deva->bus ? deva->bus->name : "No Bus",
129                  kobject_name(&deva->kobj),
130                  devb->bus ? devb->bus->name : "No Bus",
131                  kobject_name(&devb->kobj));
132         /* Delete deva from dpm_list and reinsert before devb. */
133         list_move_tail(&deva->power.entry, &devb->power.entry);
134 }
135
136 /**
137  * device_pm_move_after - Move device in the PM core's list of active devices.
138  * @deva: Device to move in dpm_list.
139  * @devb: Device @deva should come after.
140  */
141 void device_pm_move_after(struct device *deva, struct device *devb)
142 {
143         pr_debug("PM: Moving %s:%s after %s:%s\n",
144                  deva->bus ? deva->bus->name : "No Bus",
145                  kobject_name(&deva->kobj),
146                  devb->bus ? devb->bus->name : "No Bus",
147                  kobject_name(&devb->kobj));
148         /* Delete deva from dpm_list and reinsert after devb. */
149         list_move(&deva->power.entry, &devb->power.entry);
150 }
151
152 /**
153  * device_pm_move_last - Move device to end of the PM core's list of devices.
154  * @dev: Device to move in dpm_list.
155  */
156 void device_pm_move_last(struct device *dev)
157 {
158         pr_debug("PM: Moving %s:%s to end of list\n",
159                  dev->bus ? dev->bus->name : "No Bus",
160                  kobject_name(&dev->kobj));
161         list_move_tail(&dev->power.entry, &dpm_list);
162 }
163
164 /**
165  * pm_op - Execute the PM operation appropriate for given PM event.
166  * @dev: Device to handle.
167  * @ops: PM operations to choose from.
168  * @state: PM transition of the system being carried out.
169  */
170 static int pm_op(struct device *dev,
171                  const struct dev_pm_ops *ops,
172                  pm_message_t state)
173 {
174         int error = 0;
175
176         switch (state.event) {
177 #ifdef CONFIG_SUSPEND
178         case PM_EVENT_SUSPEND:
179                 if (ops->suspend) {
180                         error = ops->suspend(dev);
181                         suspend_report_result(ops->suspend, error);
182                 }
183                 break;
184         case PM_EVENT_RESUME:
185                 if (ops->resume) {
186                         error = ops->resume(dev);
187                         suspend_report_result(ops->resume, error);
188                 }
189                 break;
190 #endif /* CONFIG_SUSPEND */
191 #ifdef CONFIG_HIBERNATION
192         case PM_EVENT_FREEZE:
193         case PM_EVENT_QUIESCE:
194                 if (ops->freeze) {
195                         error = ops->freeze(dev);
196                         suspend_report_result(ops->freeze, error);
197                 }
198                 break;
199         case PM_EVENT_HIBERNATE:
200                 if (ops->poweroff) {
201                         error = ops->poweroff(dev);
202                         suspend_report_result(ops->poweroff, error);
203                 }
204                 break;
205         case PM_EVENT_THAW:
206         case PM_EVENT_RECOVER:
207                 if (ops->thaw) {
208                         error = ops->thaw(dev);
209                         suspend_report_result(ops->thaw, error);
210                 }
211                 break;
212         case PM_EVENT_RESTORE:
213                 if (ops->restore) {
214                         error = ops->restore(dev);
215                         suspend_report_result(ops->restore, error);
216                 }
217                 break;
218 #endif /* CONFIG_HIBERNATION */
219         default:
220                 error = -EINVAL;
221         }
222         return error;
223 }
224
225 /**
226  * pm_noirq_op - Execute the PM operation appropriate for given PM event.
227  * @dev: Device to handle.
228  * @ops: PM operations to choose from.
229  * @state: PM transition of the system being carried out.
230  *
231  * The driver of @dev will not receive interrupts while this function is being
232  * executed.
233  */
234 static int pm_noirq_op(struct device *dev,
235                         const struct dev_pm_ops *ops,
236                         pm_message_t state)
237 {
238         int error = 0;
239
240         switch (state.event) {
241 #ifdef CONFIG_SUSPEND
242         case PM_EVENT_SUSPEND:
243                 if (ops->suspend_noirq) {
244                         error = ops->suspend_noirq(dev);
245                         suspend_report_result(ops->suspend_noirq, error);
246                 }
247                 break;
248         case PM_EVENT_RESUME:
249                 if (ops->resume_noirq) {
250                         error = ops->resume_noirq(dev);
251                         suspend_report_result(ops->resume_noirq, error);
252                 }
253                 break;
254 #endif /* CONFIG_SUSPEND */
255 #ifdef CONFIG_HIBERNATION
256         case PM_EVENT_FREEZE:
257         case PM_EVENT_QUIESCE:
258                 if (ops->freeze_noirq) {
259                         error = ops->freeze_noirq(dev);
260                         suspend_report_result(ops->freeze_noirq, error);
261                 }
262                 break;
263         case PM_EVENT_HIBERNATE:
264                 if (ops->poweroff_noirq) {
265                         error = ops->poweroff_noirq(dev);
266                         suspend_report_result(ops->poweroff_noirq, error);
267                 }
268                 break;
269         case PM_EVENT_THAW:
270         case PM_EVENT_RECOVER:
271                 if (ops->thaw_noirq) {
272                         error = ops->thaw_noirq(dev);
273                         suspend_report_result(ops->thaw_noirq, error);
274                 }
275                 break;
276         case PM_EVENT_RESTORE:
277                 if (ops->restore_noirq) {
278                         error = ops->restore_noirq(dev);
279                         suspend_report_result(ops->restore_noirq, error);
280                 }
281                 break;
282 #endif /* CONFIG_HIBERNATION */
283         default:
284                 error = -EINVAL;
285         }
286         return error;
287 }
288
289 static char *pm_verb(int event)
290 {
291         switch (event) {
292         case PM_EVENT_SUSPEND:
293                 return "suspend";
294         case PM_EVENT_RESUME:
295                 return "resume";
296         case PM_EVENT_FREEZE:
297                 return "freeze";
298         case PM_EVENT_QUIESCE:
299                 return "quiesce";
300         case PM_EVENT_HIBERNATE:
301                 return "hibernate";
302         case PM_EVENT_THAW:
303                 return "thaw";
304         case PM_EVENT_RESTORE:
305                 return "restore";
306         case PM_EVENT_RECOVER:
307                 return "recover";
308         default:
309                 return "(unknown PM event)";
310         }
311 }
312
313 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
314 {
315         dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
316                 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
317                 ", may wakeup" : "");
318 }
319
320 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
321                         int error)
322 {
323         printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
324                 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
325 }
326
327 /*------------------------- Resume routines -------------------------*/
328
329 /**
330  * device_resume_noirq - Execute an "early resume" callback for given device.
331  * @dev: Device to handle.
332  * @state: PM transition of the system being carried out.
333  *
334  * The driver of @dev will not receive interrupts while this function is being
335  * executed.
336  */
337 static int device_resume_noirq(struct device *dev, pm_message_t state)
338 {
339         int error = 0;
340
341         TRACE_DEVICE(dev);
342         TRACE_RESUME(0);
343
344         if (!dev->bus)
345                 goto End;
346
347         if (dev->bus->pm) {
348                 pm_dev_dbg(dev, state, "EARLY ");
349                 error = pm_noirq_op(dev, dev->bus->pm, state);
350         }
351  End:
352         TRACE_RESUME(error);
353         return error;
354 }
355
356 /**
357  * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
358  * @state: PM transition of the system being carried out.
359  *
360  * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
361  * enable device drivers to receive interrupts.
362  */
363 void dpm_resume_noirq(pm_message_t state)
364 {
365         struct device *dev;
366
367         mutex_lock(&dpm_list_mtx);
368         list_for_each_entry(dev, &dpm_list, power.entry)
369                 if (dev->power.status > DPM_OFF) {
370                         int error;
371
372                         dev->power.status = DPM_OFF;
373                         error = device_resume_noirq(dev, state);
374                         if (error)
375                                 pm_dev_err(dev, state, " early", error);
376                 }
377         mutex_unlock(&dpm_list_mtx);
378         resume_device_irqs();
379 }
380 EXPORT_SYMBOL_GPL(dpm_resume_noirq);
381
382 /**
383  * device_resume - Execute "resume" callbacks for given device.
384  * @dev: Device to handle.
385  * @state: PM transition of the system being carried out.
386  */
387 static int device_resume(struct device *dev, pm_message_t state)
388 {
389         int error = 0;
390
391         TRACE_DEVICE(dev);
392         TRACE_RESUME(0);
393
394         down(&dev->sem);
395
396         if (dev->bus) {
397                 if (dev->bus->pm) {
398                         pm_dev_dbg(dev, state, "");
399                         error = pm_op(dev, dev->bus->pm, state);
400                 } else if (dev->bus->resume) {
401                         pm_dev_dbg(dev, state, "legacy ");
402                         error = dev->bus->resume(dev);
403                 }
404                 if (error)
405                         goto End;
406         }
407
408         if (dev->type) {
409                 if (dev->type->pm) {
410                         pm_dev_dbg(dev, state, "type ");
411                         error = pm_op(dev, dev->type->pm, state);
412                 }
413                 if (error)
414                         goto End;
415         }
416
417         if (dev->class) {
418                 if (dev->class->pm) {
419                         pm_dev_dbg(dev, state, "class ");
420                         error = pm_op(dev, dev->class->pm, state);
421                 } else if (dev->class->resume) {
422                         pm_dev_dbg(dev, state, "legacy class ");
423                         error = dev->class->resume(dev);
424                 }
425         }
426  End:
427         up(&dev->sem);
428
429         TRACE_RESUME(error);
430         return error;
431 }
432
433 /**
434  * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
435  * @state: PM transition of the system being carried out.
436  *
437  * Execute the appropriate "resume" callback for all devices whose status
438  * indicates that they are suspended.
439  */
440 static void dpm_resume(pm_message_t state)
441 {
442         struct list_head list;
443
444         INIT_LIST_HEAD(&list);
445         mutex_lock(&dpm_list_mtx);
446         transition_started = false;
447         while (!list_empty(&dpm_list)) {
448                 struct device *dev = to_device(dpm_list.next);
449
450                 get_device(dev);
451                 if (dev->power.status >= DPM_OFF) {
452                         int error;
453
454                         dev->power.status = DPM_RESUMING;
455                         mutex_unlock(&dpm_list_mtx);
456
457                         error = device_resume(dev, state);
458
459                         mutex_lock(&dpm_list_mtx);
460                         if (error)
461                                 pm_dev_err(dev, state, "", error);
462                 } else if (dev->power.status == DPM_SUSPENDING) {
463                         /* Allow new children of the device to be registered */
464                         dev->power.status = DPM_RESUMING;
465                 }
466                 if (!list_empty(&dev->power.entry))
467                         list_move_tail(&dev->power.entry, &list);
468                 put_device(dev);
469         }
470         list_splice(&list, &dpm_list);
471         mutex_unlock(&dpm_list_mtx);
472 }
473
474 /**
475  * device_complete - Complete a PM transition for given device.
476  * @dev: Device to handle.
477  * @state: PM transition of the system being carried out.
478  */
479 static void device_complete(struct device *dev, pm_message_t state)
480 {
481         down(&dev->sem);
482
483         if (dev->class && dev->class->pm && dev->class->pm->complete) {
484                 pm_dev_dbg(dev, state, "completing class ");
485                 dev->class->pm->complete(dev);
486         }
487
488         if (dev->type && dev->type->pm && dev->type->pm->complete) {
489                 pm_dev_dbg(dev, state, "completing type ");
490                 dev->type->pm->complete(dev);
491         }
492
493         if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
494                 pm_dev_dbg(dev, state, "completing ");
495                 dev->bus->pm->complete(dev);
496         }
497
498         up(&dev->sem);
499 }
500
501 /**
502  * dpm_complete - Complete a PM transition for all non-sysdev devices.
503  * @state: PM transition of the system being carried out.
504  *
505  * Execute the ->complete() callbacks for all devices whose PM status is not
506  * DPM_ON (this allows new devices to be registered).
507  */
508 static void dpm_complete(pm_message_t state)
509 {
510         struct list_head list;
511
512         INIT_LIST_HEAD(&list);
513         mutex_lock(&dpm_list_mtx);
514         while (!list_empty(&dpm_list)) {
515                 struct device *dev = to_device(dpm_list.prev);
516
517                 get_device(dev);
518                 if (dev->power.status > DPM_ON) {
519                         dev->power.status = DPM_ON;
520                         mutex_unlock(&dpm_list_mtx);
521
522                         device_complete(dev, state);
523                         pm_runtime_put_noidle(dev);
524
525                         mutex_lock(&dpm_list_mtx);
526                 }
527                 if (!list_empty(&dev->power.entry))
528                         list_move(&dev->power.entry, &list);
529                 put_device(dev);
530         }
531         list_splice(&list, &dpm_list);
532         mutex_unlock(&dpm_list_mtx);
533 }
534
535 /**
536  * dpm_resume_end - Execute "resume" callbacks and complete system transition.
537  * @state: PM transition of the system being carried out.
538  *
539  * Execute "resume" callbacks for all devices and complete the PM transition of
540  * the system.
541  */
542 void dpm_resume_end(pm_message_t state)
543 {
544         might_sleep();
545         dpm_resume(state);
546         dpm_complete(state);
547 }
548 EXPORT_SYMBOL_GPL(dpm_resume_end);
549
550
551 /*------------------------- Suspend routines -------------------------*/
552
553 /**
554  * resume_event - Return a "resume" message for given "suspend" sleep state.
555  * @sleep_state: PM message representing a sleep state.
556  *
557  * Return a PM message representing the resume event corresponding to given
558  * sleep state.
559  */
560 static pm_message_t resume_event(pm_message_t sleep_state)
561 {
562         switch (sleep_state.event) {
563         case PM_EVENT_SUSPEND:
564                 return PMSG_RESUME;
565         case PM_EVENT_FREEZE:
566         case PM_EVENT_QUIESCE:
567                 return PMSG_RECOVER;
568         case PM_EVENT_HIBERNATE:
569                 return PMSG_RESTORE;
570         }
571         return PMSG_ON;
572 }
573
574 /**
575  * device_suspend_noirq - Execute a "late suspend" callback for given device.
576  * @dev: Device to handle.
577  * @state: PM transition of the system being carried out.
578  *
579  * The driver of @dev will not receive interrupts while this function is being
580  * executed.
581  */
582 static int device_suspend_noirq(struct device *dev, pm_message_t state)
583 {
584         int error = 0;
585
586         if (!dev->bus)
587                 return 0;
588
589         if (dev->bus->pm) {
590                 pm_dev_dbg(dev, state, "LATE ");
591                 error = pm_noirq_op(dev, dev->bus->pm, state);
592         }
593         return error;
594 }
595
596 /**
597  * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
598  * @state: PM transition of the system being carried out.
599  *
600  * Prevent device drivers from receiving interrupts and call the "noirq" suspend
601  * handlers for all non-sysdev devices.
602  */
603 int dpm_suspend_noirq(pm_message_t state)
604 {
605         struct device *dev;
606         int error = 0;
607
608         suspend_device_irqs();
609         mutex_lock(&dpm_list_mtx);
610         list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
611                 error = device_suspend_noirq(dev, state);
612                 if (error) {
613                         pm_dev_err(dev, state, " late", error);
614                         break;
615                 }
616                 dev->power.status = DPM_OFF_IRQ;
617         }
618         mutex_unlock(&dpm_list_mtx);
619         if (error)
620                 dpm_resume_noirq(resume_event(state));
621         return error;
622 }
623 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
624
625 /**
626  * device_suspend - Execute "suspend" callbacks for given device.
627  * @dev: Device to handle.
628  * @state: PM transition of the system being carried out.
629  */
630 static int device_suspend(struct device *dev, pm_message_t state)
631 {
632         int error = 0;
633
634         down(&dev->sem);
635
636         if (dev->class) {
637                 if (dev->class->pm) {
638                         pm_dev_dbg(dev, state, "class ");
639                         error = pm_op(dev, dev->class->pm, state);
640                 } else if (dev->class->suspend) {
641                         pm_dev_dbg(dev, state, "legacy class ");
642                         error = dev->class->suspend(dev, state);
643                         suspend_report_result(dev->class->suspend, error);
644                 }
645                 if (error)
646                         goto End;
647         }
648
649         if (dev->type) {
650                 if (dev->type->pm) {
651                         pm_dev_dbg(dev, state, "type ");
652                         error = pm_op(dev, dev->type->pm, state);
653                 }
654                 if (error)
655                         goto End;
656         }
657
658         if (dev->bus) {
659                 if (dev->bus->pm) {
660                         pm_dev_dbg(dev, state, "");
661                         error = pm_op(dev, dev->bus->pm, state);
662                 } else if (dev->bus->suspend) {
663                         pm_dev_dbg(dev, state, "legacy ");
664                         error = dev->bus->suspend(dev, state);
665                         suspend_report_result(dev->bus->suspend, error);
666                 }
667         }
668  End:
669         up(&dev->sem);
670
671         return error;
672 }
673
674 /**
675  * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
676  * @state: PM transition of the system being carried out.
677  */
678 static int dpm_suspend(pm_message_t state)
679 {
680         struct list_head list;
681         int error = 0;
682
683         INIT_LIST_HEAD(&list);
684         mutex_lock(&dpm_list_mtx);
685         while (!list_empty(&dpm_list)) {
686                 struct device *dev = to_device(dpm_list.prev);
687
688                 get_device(dev);
689                 mutex_unlock(&dpm_list_mtx);
690
691                 error = device_suspend(dev, state);
692
693                 mutex_lock(&dpm_list_mtx);
694                 if (error) {
695                         pm_dev_err(dev, state, "", error);
696                         put_device(dev);
697                         break;
698                 }
699                 dev->power.status = DPM_OFF;
700                 if (!list_empty(&dev->power.entry))
701                         list_move(&dev->power.entry, &list);
702                 put_device(dev);
703         }
704         list_splice(&list, dpm_list.prev);
705         mutex_unlock(&dpm_list_mtx);
706         return error;
707 }
708
709 /**
710  * device_prepare - Prepare a device for system power transition.
711  * @dev: Device to handle.
712  * @state: PM transition of the system being carried out.
713  *
714  * Execute the ->prepare() callback(s) for given device.  No new children of the
715  * device may be registered after this function has returned.
716  */
717 static int device_prepare(struct device *dev, pm_message_t state)
718 {
719         int error = 0;
720
721         down(&dev->sem);
722
723         if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
724                 pm_dev_dbg(dev, state, "preparing ");
725                 error = dev->bus->pm->prepare(dev);
726                 suspend_report_result(dev->bus->pm->prepare, error);
727                 if (error)
728                         goto End;
729         }
730
731         if (dev->type && dev->type->pm && dev->type->pm->prepare) {
732                 pm_dev_dbg(dev, state, "preparing type ");
733                 error = dev->type->pm->prepare(dev);
734                 suspend_report_result(dev->type->pm->prepare, error);
735                 if (error)
736                         goto End;
737         }
738
739         if (dev->class && dev->class->pm && dev->class->pm->prepare) {
740                 pm_dev_dbg(dev, state, "preparing class ");
741                 error = dev->class->pm->prepare(dev);
742                 suspend_report_result(dev->class->pm->prepare, error);
743         }
744  End:
745         up(&dev->sem);
746
747         return error;
748 }
749
750 /**
751  * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
752  * @state: PM transition of the system being carried out.
753  *
754  * Execute the ->prepare() callback(s) for all devices.
755  */
756 static int dpm_prepare(pm_message_t state)
757 {
758         struct list_head list;
759         int error = 0;
760
761         INIT_LIST_HEAD(&list);
762         mutex_lock(&dpm_list_mtx);
763         transition_started = true;
764         while (!list_empty(&dpm_list)) {
765                 struct device *dev = to_device(dpm_list.next);
766
767                 get_device(dev);
768                 dev->power.status = DPM_PREPARING;
769                 mutex_unlock(&dpm_list_mtx);
770
771                 pm_runtime_get_noresume(dev);
772                 if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
773                         /* Wake-up requested during system sleep transition. */
774                         pm_runtime_put_noidle(dev);
775                         error = -EBUSY;
776                 } else {
777                         error = device_prepare(dev, state);
778                 }
779
780                 mutex_lock(&dpm_list_mtx);
781                 if (error) {
782                         dev->power.status = DPM_ON;
783                         if (error == -EAGAIN) {
784                                 put_device(dev);
785                                 error = 0;
786                                 continue;
787                         }
788                         printk(KERN_ERR "PM: Failed to prepare device %s "
789                                 "for power transition: error %d\n",
790                                 kobject_name(&dev->kobj), error);
791                         put_device(dev);
792                         break;
793                 }
794                 dev->power.status = DPM_SUSPENDING;
795                 if (!list_empty(&dev->power.entry))
796                         list_move_tail(&dev->power.entry, &list);
797                 put_device(dev);
798         }
799         list_splice(&list, &dpm_list);
800         mutex_unlock(&dpm_list_mtx);
801         return error;
802 }
803
804 /**
805  * dpm_suspend_start - Prepare devices for PM transition and suspend them.
806  * @state: PM transition of the system being carried out.
807  *
808  * Prepare all non-sysdev devices for system PM transition and execute "suspend"
809  * callbacks for them.
810  */
811 int dpm_suspend_start(pm_message_t state)
812 {
813         int error;
814
815         might_sleep();
816         error = dpm_prepare(state);
817         if (!error)
818                 error = dpm_suspend(state);
819         return error;
820 }
821 EXPORT_SYMBOL_GPL(dpm_suspend_start);
822
823 void __suspend_report_result(const char *function, void *fn, int ret)
824 {
825         if (ret)
826                 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
827 }
828 EXPORT_SYMBOL_GPL(__suspend_report_result);