Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial
[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  *      resume_device_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 resume_device_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         } else if (dev->bus->resume_early) {
338                 pm_dev_dbg(dev, state, "legacy EARLY ");
339                 error = dev->bus->resume_early(dev);
340         }
341  End:
342         TRACE_RESUME(error);
343         return error;
344 }
345
346 /**
347  *      dpm_power_up - Power on all regular (non-sysdev) devices.
348  *      @state: PM transition of the system being carried out.
349  *
350  *      Execute the appropriate "noirq resume" callback for all devices marked
351  *      as DPM_OFF_IRQ.
352  *
353  *      Must be called under dpm_list_mtx.  Device drivers should not receive
354  *      interrupts while it's being executed.
355  */
356 static void dpm_power_up(pm_message_t state)
357 {
358         struct device *dev;
359
360         list_for_each_entry(dev, &dpm_list, power.entry)
361                 if (dev->power.status > DPM_OFF) {
362                         int error;
363
364                         dev->power.status = DPM_OFF;
365                         error = resume_device_noirq(dev, state);
366                         if (error)
367                                 pm_dev_err(dev, state, " early", error);
368                 }
369 }
370
371 /**
372  *      device_power_up - Turn on all devices that need special attention.
373  *      @state: PM transition of the system being carried out.
374  *
375  *      Call the "early" resume handlers and enable device drivers to receive
376  *      interrupts.
377  */
378 void device_power_up(pm_message_t state)
379 {
380         dpm_power_up(state);
381         resume_device_irqs();
382 }
383 EXPORT_SYMBOL_GPL(device_power_up);
384
385 /**
386  *      resume_device - Restore state for one device.
387  *      @dev:   Device.
388  *      @state: PM transition of the system being carried out.
389  */
390 static int resume_device(struct device *dev, pm_message_t state)
391 {
392         int error = 0;
393
394         TRACE_DEVICE(dev);
395         TRACE_RESUME(0);
396
397         down(&dev->sem);
398
399         if (dev->bus) {
400                 if (dev->bus->pm) {
401                         pm_dev_dbg(dev, state, "");
402                         error = pm_op(dev, dev->bus->pm, state);
403                 } else if (dev->bus->resume) {
404                         pm_dev_dbg(dev, state, "legacy ");
405                         error = dev->bus->resume(dev);
406                 }
407                 if (error)
408                         goto End;
409         }
410
411         if (dev->type) {
412                 if (dev->type->pm) {
413                         pm_dev_dbg(dev, state, "type ");
414                         error = pm_op(dev, dev->type->pm, state);
415                 } else if (dev->type->resume) {
416                         pm_dev_dbg(dev, state, "legacy type ");
417                         error = dev->type->resume(dev);
418                 }
419                 if (error)
420                         goto End;
421         }
422
423         if (dev->class) {
424                 if (dev->class->pm) {
425                         pm_dev_dbg(dev, state, "class ");
426                         error = pm_op(dev, dev->class->pm, state);
427                 } else if (dev->class->resume) {
428                         pm_dev_dbg(dev, state, "legacy class ");
429                         error = dev->class->resume(dev);
430                 }
431         }
432  End:
433         up(&dev->sem);
434
435         TRACE_RESUME(error);
436         return error;
437 }
438
439 /**
440  *      dpm_resume - Resume every device.
441  *      @state: PM transition of the system being carried out.
442  *
443  *      Execute the appropriate "resume" callback for all devices the status of
444  *      which indicates that they are inactive.
445  */
446 static void dpm_resume(pm_message_t state)
447 {
448         struct list_head list;
449
450         INIT_LIST_HEAD(&list);
451         mutex_lock(&dpm_list_mtx);
452         transition_started = false;
453         while (!list_empty(&dpm_list)) {
454                 struct device *dev = to_device(dpm_list.next);
455
456                 get_device(dev);
457                 if (dev->power.status >= DPM_OFF) {
458                         int error;
459
460                         dev->power.status = DPM_RESUMING;
461                         mutex_unlock(&dpm_list_mtx);
462
463                         error = resume_device(dev, state);
464
465                         mutex_lock(&dpm_list_mtx);
466                         if (error)
467                                 pm_dev_err(dev, state, "", error);
468                 } else if (dev->power.status == DPM_SUSPENDING) {
469                         /* Allow new children of the device to be registered */
470                         dev->power.status = DPM_RESUMING;
471                 }
472                 if (!list_empty(&dev->power.entry))
473                         list_move_tail(&dev->power.entry, &list);
474                 put_device(dev);
475         }
476         list_splice(&list, &dpm_list);
477         mutex_unlock(&dpm_list_mtx);
478 }
479
480 /**
481  *      complete_device - Complete a PM transition for given device
482  *      @dev:   Device.
483  *      @state: PM transition of the system being carried out.
484  */
485 static void complete_device(struct device *dev, pm_message_t state)
486 {
487         down(&dev->sem);
488
489         if (dev->class && dev->class->pm && dev->class->pm->complete) {
490                 pm_dev_dbg(dev, state, "completing class ");
491                 dev->class->pm->complete(dev);
492         }
493
494         if (dev->type && dev->type->pm && dev->type->pm->complete) {
495                 pm_dev_dbg(dev, state, "completing type ");
496                 dev->type->pm->complete(dev);
497         }
498
499         if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
500                 pm_dev_dbg(dev, state, "completing ");
501                 dev->bus->pm->complete(dev);
502         }
503
504         up(&dev->sem);
505 }
506
507 /**
508  *      dpm_complete - Complete a PM transition for all devices.
509  *      @state: PM transition of the system being carried out.
510  *
511  *      Execute the ->complete() callbacks for all devices that are not marked
512  *      as DPM_ON.
513  */
514 static void dpm_complete(pm_message_t state)
515 {
516         struct list_head list;
517
518         INIT_LIST_HEAD(&list);
519         mutex_lock(&dpm_list_mtx);
520         while (!list_empty(&dpm_list)) {
521                 struct device *dev = to_device(dpm_list.prev);
522
523                 get_device(dev);
524                 if (dev->power.status > DPM_ON) {
525                         dev->power.status = DPM_ON;
526                         mutex_unlock(&dpm_list_mtx);
527
528                         complete_device(dev, state);
529
530                         mutex_lock(&dpm_list_mtx);
531                 }
532                 if (!list_empty(&dev->power.entry))
533                         list_move(&dev->power.entry, &list);
534                 put_device(dev);
535         }
536         list_splice(&list, &dpm_list);
537         mutex_unlock(&dpm_list_mtx);
538 }
539
540 /**
541  *      device_resume - Restore state of each device in system.
542  *      @state: PM transition of the system being carried out.
543  *
544  *      Resume all the devices, unlock them all, and allow new
545  *      devices to be registered once again.
546  */
547 void device_resume(pm_message_t state)
548 {
549         might_sleep();
550         dpm_resume(state);
551         dpm_complete(state);
552 }
553 EXPORT_SYMBOL_GPL(device_resume);
554
555
556 /*------------------------- Suspend routines -------------------------*/
557
558 /**
559  *      resume_event - return a PM message representing the resume event
560  *                     corresponding to given sleep state.
561  *      @sleep_state: PM message representing a sleep state.
562  */
563 static pm_message_t resume_event(pm_message_t sleep_state)
564 {
565         switch (sleep_state.event) {
566         case PM_EVENT_SUSPEND:
567                 return PMSG_RESUME;
568         case PM_EVENT_FREEZE:
569         case PM_EVENT_QUIESCE:
570                 return PMSG_RECOVER;
571         case PM_EVENT_HIBERNATE:
572                 return PMSG_RESTORE;
573         }
574         return PMSG_ON;
575 }
576
577 /**
578  *      suspend_device_noirq - Shut down one device (late suspend).
579  *      @dev:   Device.
580  *      @state: PM transition of the system being carried out.
581  *
582  *      This is called with interrupts off and only a single CPU running.
583  */
584 static int suspend_device_noirq(struct device *dev, pm_message_t state)
585 {
586         int error = 0;
587
588         if (!dev->bus)
589                 return 0;
590
591         if (dev->bus->pm) {
592                 pm_dev_dbg(dev, state, "LATE ");
593                 error = pm_noirq_op(dev, dev->bus->pm, state);
594         } else if (dev->bus->suspend_late) {
595                 pm_dev_dbg(dev, state, "legacy LATE ");
596                 error = dev->bus->suspend_late(dev, state);
597                 suspend_report_result(dev->bus->suspend_late, error);
598         }
599         return error;
600 }
601
602 /**
603  *      device_power_down - Shut down special devices.
604  *      @state: PM transition of the system being carried out.
605  *
606  *      Prevent device drivers from receiving interrupts and call the "late"
607  *      suspend handlers.
608  *
609  *      Must be called under dpm_list_mtx.
610  */
611 int device_power_down(pm_message_t state)
612 {
613         struct device *dev;
614         int error = 0;
615
616         suspend_device_irqs();
617         list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
618                 error = suspend_device_noirq(dev, state);
619                 if (error) {
620                         pm_dev_err(dev, state, " late", error);
621                         break;
622                 }
623                 dev->power.status = DPM_OFF_IRQ;
624         }
625         if (error)
626                 device_power_up(resume_event(state));
627         return error;
628 }
629 EXPORT_SYMBOL_GPL(device_power_down);
630
631 /**
632  *      suspend_device - Save state of one device.
633  *      @dev:   Device.
634  *      @state: PM transition of the system being carried out.
635  */
636 static int suspend_device(struct device *dev, pm_message_t state)
637 {
638         int error = 0;
639
640         down(&dev->sem);
641
642         if (dev->class) {
643                 if (dev->class->pm) {
644                         pm_dev_dbg(dev, state, "class ");
645                         error = pm_op(dev, dev->class->pm, state);
646                 } else if (dev->class->suspend) {
647                         pm_dev_dbg(dev, state, "legacy class ");
648                         error = dev->class->suspend(dev, state);
649                         suspend_report_result(dev->class->suspend, error);
650                 }
651                 if (error)
652                         goto End;
653         }
654
655         if (dev->type) {
656                 if (dev->type->pm) {
657                         pm_dev_dbg(dev, state, "type ");
658                         error = pm_op(dev, dev->type->pm, state);
659                 } else if (dev->type->suspend) {
660                         pm_dev_dbg(dev, state, "legacy type ");
661                         error = dev->type->suspend(dev, state);
662                         suspend_report_result(dev->type->suspend, error);
663                 }
664                 if (error)
665                         goto End;
666         }
667
668         if (dev->bus) {
669                 if (dev->bus->pm) {
670                         pm_dev_dbg(dev, state, "");
671                         error = pm_op(dev, dev->bus->pm, state);
672                 } else if (dev->bus->suspend) {
673                         pm_dev_dbg(dev, state, "legacy ");
674                         error = dev->bus->suspend(dev, state);
675                         suspend_report_result(dev->bus->suspend, error);
676                 }
677         }
678  End:
679         up(&dev->sem);
680
681         return error;
682 }
683
684 /**
685  *      dpm_suspend - Suspend every device.
686  *      @state: PM transition of the system being carried out.
687  *
688  *      Execute the appropriate "suspend" callbacks for all devices.
689  */
690 static int dpm_suspend(pm_message_t state)
691 {
692         struct list_head list;
693         int error = 0;
694
695         INIT_LIST_HEAD(&list);
696         mutex_lock(&dpm_list_mtx);
697         while (!list_empty(&dpm_list)) {
698                 struct device *dev = to_device(dpm_list.prev);
699
700                 get_device(dev);
701                 mutex_unlock(&dpm_list_mtx);
702
703                 error = suspend_device(dev, state);
704
705                 mutex_lock(&dpm_list_mtx);
706                 if (error) {
707                         pm_dev_err(dev, state, "", error);
708                         put_device(dev);
709                         break;
710                 }
711                 dev->power.status = DPM_OFF;
712                 if (!list_empty(&dev->power.entry))
713                         list_move(&dev->power.entry, &list);
714                 put_device(dev);
715         }
716         list_splice(&list, dpm_list.prev);
717         mutex_unlock(&dpm_list_mtx);
718         return error;
719 }
720
721 /**
722  *      prepare_device - Execute the ->prepare() callback(s) for given device.
723  *      @dev:   Device.
724  *      @state: PM transition of the system being carried out.
725  */
726 static int prepare_device(struct device *dev, pm_message_t state)
727 {
728         int error = 0;
729
730         down(&dev->sem);
731
732         if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
733                 pm_dev_dbg(dev, state, "preparing ");
734                 error = dev->bus->pm->prepare(dev);
735                 suspend_report_result(dev->bus->pm->prepare, error);
736                 if (error)
737                         goto End;
738         }
739
740         if (dev->type && dev->type->pm && dev->type->pm->prepare) {
741                 pm_dev_dbg(dev, state, "preparing type ");
742                 error = dev->type->pm->prepare(dev);
743                 suspend_report_result(dev->type->pm->prepare, error);
744                 if (error)
745                         goto End;
746         }
747
748         if (dev->class && dev->class->pm && dev->class->pm->prepare) {
749                 pm_dev_dbg(dev, state, "preparing class ");
750                 error = dev->class->pm->prepare(dev);
751                 suspend_report_result(dev->class->pm->prepare, error);
752         }
753  End:
754         up(&dev->sem);
755
756         return error;
757 }
758
759 /**
760  *      dpm_prepare - Prepare all devices for a PM transition.
761  *      @state: PM transition of the system being carried out.
762  *
763  *      Execute the ->prepare() callback for all devices.
764  */
765 static int dpm_prepare(pm_message_t state)
766 {
767         struct list_head list;
768         int error = 0;
769
770         INIT_LIST_HEAD(&list);
771         mutex_lock(&dpm_list_mtx);
772         transition_started = true;
773         while (!list_empty(&dpm_list)) {
774                 struct device *dev = to_device(dpm_list.next);
775
776                 get_device(dev);
777                 dev->power.status = DPM_PREPARING;
778                 mutex_unlock(&dpm_list_mtx);
779
780                 error = prepare_device(dev, state);
781
782                 mutex_lock(&dpm_list_mtx);
783                 if (error) {
784                         dev->power.status = DPM_ON;
785                         if (error == -EAGAIN) {
786                                 put_device(dev);
787                                 continue;
788                         }
789                         printk(KERN_ERR "PM: Failed to prepare device %s "
790                                 "for power transition: error %d\n",
791                                 kobject_name(&dev->kobj), error);
792                         put_device(dev);
793                         break;
794                 }
795                 dev->power.status = DPM_SUSPENDING;
796                 if (!list_empty(&dev->power.entry))
797                         list_move_tail(&dev->power.entry, &list);
798                 put_device(dev);
799         }
800         list_splice(&list, &dpm_list);
801         mutex_unlock(&dpm_list_mtx);
802         return error;
803 }
804
805 /**
806  *      device_suspend - Save state and stop all devices in system.
807  *      @state: PM transition of the system being carried out.
808  *
809  *      Prepare and suspend all devices.
810  */
811 int device_suspend(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(device_suspend);
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);