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