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