]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/base/power/main.c
PM: Introduce functions for suspending and resuming device interrupts
[mirror_ubuntu-artful-kernel.git] / drivers / base / power / main.c
CommitLineData
1da177e4
LT
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 *
1eede070
RW
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.
1da177e4
LT
18 */
19
1da177e4 20#include <linux/device.h>
cd59abfc 21#include <linux/kallsyms.h>
11048dcf 22#include <linux/mutex.h>
cd59abfc
AS
23#include <linux/pm.h>
24#include <linux/resume-trace.h>
775b64d2 25#include <linux/rwsem.h>
11048dcf 26
cd59abfc 27#include "../base.h"
1da177e4
LT
28#include "power.h"
29
775b64d2 30/*
1eede070 31 * The entries in the dpm_list list are in a depth first order, simply
775b64d2
RW
32 * because children are guaranteed to be discovered after parents, and
33 * are inserted at the back of the list on discovery.
34 *
775b64d2
RW
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
1eede070 40LIST_HEAD(dpm_list);
1da177e4 41
cd59abfc 42static DEFINE_MUTEX(dpm_list_mtx);
1da177e4 43
1eede070
RW
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 */
48static bool transition_started;
49
50/**
51 * device_pm_lock - lock the list of active devices used by the PM core
52 */
53void 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 */
61void device_pm_unlock(void)
62{
63 mutex_unlock(&dpm_list_mtx);
64}
075c1771 65
775b64d2
RW
66/**
67 * device_pm_add - add a device to the list of active devices
68 * @dev: Device to be added to the list
69 */
3b98aeaf 70void device_pm_add(struct device *dev)
1da177e4 71{
1da177e4 72 pr_debug("PM: Adding info for %s:%s\n",
c48ea603
DT
73 dev->bus ? dev->bus->name : "No Bus",
74 kobject_name(&dev->kobj));
11048dcf 75 mutex_lock(&dpm_list_mtx);
1eede070 76 if (dev->parent) {
f5a6d958
RW
77 if (dev->parent->power.status >= DPM_SUSPENDING)
78 dev_warn(dev, "parent %s should not be sleeping\n",
1e0b2cf9 79 dev_name(dev->parent));
1eede070
RW
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 */
728f0893 86 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
58aca232 87 }
3b98aeaf
AS
88
89 list_add_tail(&dev->power.entry, &dpm_list);
11048dcf 90 mutex_unlock(&dpm_list_mtx);
1da177e4
LT
91}
92
775b64d2
RW
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 */
9cddad77 99void device_pm_remove(struct device *dev)
1da177e4
LT
100{
101 pr_debug("PM: Removing info for %s:%s\n",
c48ea603
DT
102 dev->bus ? dev->bus->name : "No Bus",
103 kobject_name(&dev->kobj));
11048dcf 104 mutex_lock(&dpm_list_mtx);
1da177e4 105 list_del_init(&dev->power.entry);
11048dcf 106 mutex_unlock(&dpm_list_mtx);
775b64d2
RW
107}
108
ffa6a705
CH
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 */
114void 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 */
130void 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 */
145void 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
1eede070
RW
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 */
adf09493
RW
159static int pm_op(struct device *dev, struct dev_pm_ops *ops,
160 pm_message_t state)
1eede070
RW
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 */
adf09493 222static int pm_noirq_op(struct device *dev, struct dev_pm_ops *ops,
1eede070
RW
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
276static 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
300static 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
307static 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
cd59abfc
AS
314/*------------------------- Resume routines -------------------------*/
315
316/**
1eede070 317 * resume_device_noirq - Power on one device (early resume).
cd59abfc 318 * @dev: Device.
1eede070 319 * @state: PM transition of the system being carried out.
cd59abfc 320 *
775b64d2 321 * Must be called with interrupts disabled.
cd59abfc 322 */
1eede070 323static int resume_device_noirq(struct device *dev, pm_message_t state)
cd59abfc
AS
324{
325 int error = 0;
326
327 TRACE_DEVICE(dev);
328 TRACE_RESUME(0);
329
1eede070
RW
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 ");
775b64d2
RW
338 error = dev->bus->resume_early(dev);
339 }
1eede070 340 End:
775b64d2
RW
341 TRACE_RESUME(error);
342 return error;
343}
344
345/**
346 * dpm_power_up - Power on all regular (non-sysdev) devices.
1eede070 347 * @state: PM transition of the system being carried out.
775b64d2 348 *
1eede070
RW
349 * Execute the appropriate "noirq resume" callback for all devices marked
350 * as DPM_OFF_IRQ.
775b64d2
RW
351 *
352 * Must be called with interrupts disabled and only one CPU running.
353 */
1eede070 354static void dpm_power_up(pm_message_t state)
775b64d2 355{
1eede070 356 struct device *dev;
775b64d2 357
1eede070
RW
358 list_for_each_entry(dev, &dpm_list, power.entry)
359 if (dev->power.status > DPM_OFF) {
360 int error;
775b64d2 361
1eede070
RW
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 }
775b64d2
RW
367}
368
369/**
370 * device_power_up - Turn on all devices that need special attention.
1eede070 371 * @state: PM transition of the system being carried out.
775b64d2
RW
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 */
1eede070 378void device_power_up(pm_message_t state)
775b64d2 379{
1eede070 380 dpm_power_up(state);
775b64d2
RW
381}
382EXPORT_SYMBOL_GPL(device_power_up);
383
384/**
385 * resume_device - Restore state for one device.
386 * @dev: Device.
1eede070 387 * @state: PM transition of the system being carried out.
775b64d2 388 */
1eede070 389static int resume_device(struct device *dev, pm_message_t state)
775b64d2
RW
390{
391 int error = 0;
392
393 TRACE_DEVICE(dev);
394 TRACE_RESUME(0);
cd59abfc 395
7a8d37a3
RW
396 down(&dev->sem);
397
1eede070
RW
398 if (dev->bus) {
399 if (dev->bus->pm) {
400 pm_dev_dbg(dev, state, "");
adf09493 401 error = pm_op(dev, dev->bus->pm, state);
1eede070
RW
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;
cd59abfc
AS
408 }
409
1eede070
RW
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;
cd59abfc
AS
420 }
421
1eede070
RW
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 }
cd59abfc 430 }
1eede070 431 End:
7a8d37a3
RW
432 up(&dev->sem);
433
cd59abfc
AS
434 TRACE_RESUME(error);
435 return error;
436}
437
775b64d2
RW
438/**
439 * dpm_resume - Resume every device.
1eede070 440 * @state: PM transition of the system being carried out.
775b64d2 441 *
1eede070
RW
442 * Execute the appropriate "resume" callback for all devices the status of
443 * which indicates that they are inactive.
444 */
445static 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 */
484static 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
adf09493 498 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
1eede070 499 pm_dev_dbg(dev, state, "completing ");
adf09493 500 dev->bus->pm->complete(dev);
1eede070
RW
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.
775b64d2 509 *
1eede070
RW
510 * Execute the ->complete() callbacks for all devices that are not marked
511 * as DPM_ON.
cd59abfc 512 */
1eede070 513static void dpm_complete(pm_message_t state)
cd59abfc 514{
1eede070
RW
515 struct list_head list;
516
517 INIT_LIST_HEAD(&list);
cd59abfc 518 mutex_lock(&dpm_list_mtx);
1eede070
RW
519 while (!list_empty(&dpm_list)) {
520 struct device *dev = to_device(dpm_list.prev);
cd59abfc 521
1eede070
RW
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);
cd59abfc 534 }
1eede070 535 list_splice(&list, &dpm_list);
cd59abfc
AS
536 mutex_unlock(&dpm_list_mtx);
537}
538
cd59abfc 539/**
775b64d2 540 * device_resume - Restore state of each device in system.
1eede070 541 * @state: PM transition of the system being carried out.
cd59abfc 542 *
775b64d2
RW
543 * Resume all the devices, unlock them all, and allow new
544 * devices to be registered once again.
cd59abfc 545 */
1eede070 546void device_resume(pm_message_t state)
cd59abfc 547{
775b64d2 548 might_sleep();
1eede070
RW
549 dpm_resume(state);
550 dpm_complete(state);
cd59abfc 551}
775b64d2 552EXPORT_SYMBOL_GPL(device_resume);
cd59abfc
AS
553
554
555/*------------------------- Suspend routines -------------------------*/
556
1eede070
RW
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 */
562static pm_message_t resume_event(pm_message_t sleep_state)
cd59abfc 563{
1eede070
RW
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;
cd59abfc 572 }
1eede070 573 return PMSG_ON;
cd59abfc
AS
574}
575
576/**
1eede070 577 * suspend_device_noirq - Shut down one device (late suspend).
cd59abfc 578 * @dev: Device.
1eede070 579 * @state: PM transition of the system being carried out.
775b64d2
RW
580 *
581 * This is called with interrupts off and only a single CPU running.
cd59abfc 582 */
1eede070 583static int suspend_device_noirq(struct device *dev, pm_message_t state)
775b64d2
RW
584{
585 int error = 0;
cd59abfc 586
1eede070
RW
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 ");
775b64d2
RW
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.
1eede070 603 * @state: PM transition of the system being carried out.
775b64d2 604 *
1eede070 605 * Power down devices that require interrupts to be disabled.
775b64d2
RW
606 * Then power down system devices.
607 *
608 * Must be called with interrupts disabled and only one CPU running.
609 */
610int device_power_down(pm_message_t state)
611{
1eede070 612 struct device *dev;
775b64d2
RW
613 int error = 0;
614
1eede070
RW
615 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
616 error = suspend_device_noirq(dev, state);
775b64d2 617 if (error) {
1eede070 618 pm_dev_err(dev, state, " late", error);
775b64d2
RW
619 break;
620 }
1eede070 621 dev->power.status = DPM_OFF_IRQ;
775b64d2 622 }
775b64d2 623 if (error)
1eede070 624 dpm_power_up(resume_event(state));
775b64d2
RW
625 return error;
626}
627EXPORT_SYMBOL_GPL(device_power_down);
628
629/**
630 * suspend_device - Save state of one device.
631 * @dev: Device.
1eede070 632 * @state: PM transition of the system being carried out.
775b64d2 633 */
19e20c91 634static int suspend_device(struct device *dev, pm_message_t state)
cd59abfc
AS
635{
636 int error = 0;
637
7a8d37a3
RW
638 down(&dev->sem);
639
1eede070
RW
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;
cd59abfc
AS
651 }
652
1eede070
RW
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;
cd59abfc
AS
664 }
665
1eede070
RW
666 if (dev->bus) {
667 if (dev->bus->pm) {
668 pm_dev_dbg(dev, state, "");
adf09493 669 error = pm_op(dev, dev->bus->pm, state);
1eede070
RW
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 }
cd59abfc 675 }
1eede070 676 End:
7a8d37a3
RW
677 up(&dev->sem);
678
cd59abfc
AS
679 return error;
680}
681
682/**
775b64d2 683 * dpm_suspend - Suspend every device.
1eede070 684 * @state: PM transition of the system being carried out.
cd59abfc 685 *
1eede070 686 * Execute the appropriate "suspend" callbacks for all devices.
cd59abfc 687 */
775b64d2 688static int dpm_suspend(pm_message_t state)
cd59abfc 689{
1eede070 690 struct list_head list;
cd59abfc
AS
691 int error = 0;
692
1eede070 693 INIT_LIST_HEAD(&list);
cd59abfc 694 mutex_lock(&dpm_list_mtx);
1eede070
RW
695 while (!list_empty(&dpm_list)) {
696 struct device *dev = to_device(dpm_list.prev);
58aca232 697
1eede070 698 get_device(dev);
cd59abfc 699 mutex_unlock(&dpm_list_mtx);
1eede070 700
cd59abfc 701 error = suspend_device(dev, state);
1eede070 702
1b3cbec1 703 mutex_lock(&dpm_list_mtx);
775b64d2 704 if (error) {
1eede070
RW
705 pm_dev_err(dev, state, "", error);
706 put_device(dev);
775b64d2
RW
707 break;
708 }
1eede070 709 dev->power.status = DPM_OFF;
7a8d37a3 710 if (!list_empty(&dev->power.entry))
1eede070
RW
711 list_move(&dev->power.entry, &list);
712 put_device(dev);
cd59abfc 713 }
1eede070 714 list_splice(&list, dpm_list.prev);
cd59abfc 715 mutex_unlock(&dpm_list_mtx);
1eede070
RW
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 */
724static int prepare_device(struct device *dev, pm_message_t state)
725{
726 int error = 0;
727
728 down(&dev->sem);
729
adf09493 730 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
1eede070 731 pm_dev_dbg(dev, state, "preparing ");
adf09493
RW
732 error = dev->bus->pm->prepare(dev);
733 suspend_report_result(dev->bus->pm->prepare, error);
1eede070
RW
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}
cd59abfc 756
1eede070
RW
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 */
763static 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);
cd59abfc
AS
800 return error;
801}
802
775b64d2
RW
803/**
804 * device_suspend - Save state and stop all devices in system.
1eede070 805 * @state: PM transition of the system being carried out.
775b64d2 806 *
1eede070 807 * Prepare and suspend all devices.
775b64d2
RW
808 */
809int device_suspend(pm_message_t state)
810{
811 int error;
cd59abfc 812
775b64d2 813 might_sleep();
1eede070
RW
814 error = dpm_prepare(state);
815 if (!error)
816 error = dpm_suspend(state);
cd59abfc 817 return error;
cd59abfc 818}
775b64d2 819EXPORT_SYMBOL_GPL(device_suspend);
cd59abfc
AS
820
821void __suspend_report_result(const char *function, void *fn, int ret)
822{
c80cfb04
BH
823 if (ret)
824 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
cd59abfc
AS
825}
826EXPORT_SYMBOL_GPL(__suspend_report_result);