main.c 18.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * drivers/base/power/main.c - Where the driver meets power management.
 *
 * Copyright (c) 2003 Patrick Mochel
 * Copyright (c) 2003 Open Source Development Lab
 *
 * This file is released under the GPLv2
 *
 *
 * The driver model core calls device_pm_add() when a device is registered.
 * This will intialize the embedded device_pm_info object in the device
 * and add it to the list of power-controlled devices. sysfs entries for
 * controlling device power management will also be added.
 *
15 16 17
 * A separate list is used for keeping track of power info, because the power
 * domain dependencies may differ from the ancestral dependencies that the
 * subsystem list maintains.
L
Linus Torvalds 已提交
18 19 20
 */

#include <linux/device.h>
21
#include <linux/kallsyms.h>
22
#include <linux/mutex.h>
23 24
#include <linux/pm.h>
#include <linux/resume-trace.h>
25
#include <linux/rwsem.h>
26

27
#include "../base.h"
L
Linus Torvalds 已提交
28 29
#include "power.h"

30
/*
31
 * The entries in the dpm_list list are in a depth first order, simply
32 33 34 35 36 37 38 39
 * because children are guaranteed to be discovered after parents, and
 * are inserted at the back of the list on discovery.
 *
 * Since device_pm_add() may be called with a device semaphore held,
 * we must never try to acquire a device semaphore while holding
 * dpm_list_mutex.
 */

40
LIST_HEAD(dpm_list);
L
Linus Torvalds 已提交
41

42
static DEFINE_MUTEX(dpm_list_mtx);
L
Linus Torvalds 已提交
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*
 * Set once the preparation of devices for a PM transition has started, reset
 * before starting to resume devices.  Protected by dpm_list_mtx.
 */
static bool transition_started;

/**
 *	device_pm_lock - lock the list of active devices used by the PM core
 */
void device_pm_lock(void)
{
	mutex_lock(&dpm_list_mtx);
}

/**
 *	device_pm_unlock - unlock the list of active devices used by the PM core
 */
void device_pm_unlock(void)
{
	mutex_unlock(&dpm_list_mtx);
}
65

66 67 68 69
/**
 *	device_pm_add - add a device to the list of active devices
 *	@dev:	Device to be added to the list
 */
70
int device_pm_add(struct device *dev)
L
Linus Torvalds 已提交
71
{
72
	int error;
73

L
Linus Torvalds 已提交
74
	pr_debug("PM: Adding info for %s:%s\n",
75 76
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
77
	mutex_lock(&dpm_list_mtx);
78 79 80
	if (dev->parent) {
		if (dev->parent->power.status >= DPM_SUSPENDING) {
			dev_warn(dev, "parent %s is sleeping, will not add\n",
81
				dev->parent->bus_id);
82 83 84 85 86 87 88 89
			WARN_ON(true);
		}
	} else if (transition_started) {
		/*
		 * We refuse to register parentless devices while a PM
		 * transition is in progress in order to avoid leaving them
		 * unhandled down the road
		 */
90 91
		WARN_ON(true);
	}
92
	error = dpm_sysfs_add(dev);
93 94 95 96
	if (!error) {
		dev->power.status = DPM_ON;
		list_add_tail(&dev->power.entry, &dpm_list);
	}
97
	mutex_unlock(&dpm_list_mtx);
98
	return error;
L
Linus Torvalds 已提交
99 100
}

101 102 103 104 105 106
/**
 *	device_pm_remove - remove a device from the list of active devices
 *	@dev:	Device to be removed from the list
 *
 *	This function also removes the device's PM-related sysfs attributes.
 */
107
void device_pm_remove(struct device *dev)
L
Linus Torvalds 已提交
108 109
{
	pr_debug("PM: Removing info for %s:%s\n",
110 111
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
112
	mutex_lock(&dpm_list_mtx);
L
Linus Torvalds 已提交
113 114
	dpm_sysfs_remove(dev);
	list_del_init(&dev->power.entry);
115
	mutex_unlock(&dpm_list_mtx);
116 117
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/**
 *	pm_op - execute the PM operation appropiate for given PM event
 *	@dev:	Device.
 *	@ops:	PM operations to choose from.
 *	@state:	PM transition of the system being carried out.
 */
static int pm_op(struct device *dev, struct pm_ops *ops, pm_message_t state)
{
	int error = 0;

	switch (state.event) {
#ifdef CONFIG_SUSPEND
	case PM_EVENT_SUSPEND:
		if (ops->suspend) {
			error = ops->suspend(dev);
			suspend_report_result(ops->suspend, error);
		}
		break;
	case PM_EVENT_RESUME:
		if (ops->resume) {
			error = ops->resume(dev);
			suspend_report_result(ops->resume, error);
		}
		break;
#endif /* CONFIG_SUSPEND */
#ifdef CONFIG_HIBERNATION
	case PM_EVENT_FREEZE:
	case PM_EVENT_QUIESCE:
		if (ops->freeze) {
			error = ops->freeze(dev);
			suspend_report_result(ops->freeze, error);
		}
		break;
	case PM_EVENT_HIBERNATE:
		if (ops->poweroff) {
			error = ops->poweroff(dev);
			suspend_report_result(ops->poweroff, error);
		}
		break;
	case PM_EVENT_THAW:
	case PM_EVENT_RECOVER:
		if (ops->thaw) {
			error = ops->thaw(dev);
			suspend_report_result(ops->thaw, error);
		}
		break;
	case PM_EVENT_RESTORE:
		if (ops->restore) {
			error = ops->restore(dev);
			suspend_report_result(ops->restore, error);
		}
		break;
#endif /* CONFIG_HIBERNATION */
	default:
		error = -EINVAL;
	}
	return error;
}

/**
 *	pm_noirq_op - execute the PM operation appropiate for given PM event
 *	@dev:	Device.
 *	@ops:	PM operations to choose from.
 *	@state: PM transition of the system being carried out.
 *
 *	The operation is executed with interrupts disabled by the only remaining
 *	functional CPU in the system.
 */
static int pm_noirq_op(struct device *dev, struct pm_ext_ops *ops,
			pm_message_t state)
{
	int error = 0;

	switch (state.event) {
#ifdef CONFIG_SUSPEND
	case PM_EVENT_SUSPEND:
		if (ops->suspend_noirq) {
			error = ops->suspend_noirq(dev);
			suspend_report_result(ops->suspend_noirq, error);
		}
		break;
	case PM_EVENT_RESUME:
		if (ops->resume_noirq) {
			error = ops->resume_noirq(dev);
			suspend_report_result(ops->resume_noirq, error);
		}
		break;
#endif /* CONFIG_SUSPEND */
#ifdef CONFIG_HIBERNATION
	case PM_EVENT_FREEZE:
	case PM_EVENT_QUIESCE:
		if (ops->freeze_noirq) {
			error = ops->freeze_noirq(dev);
			suspend_report_result(ops->freeze_noirq, error);
		}
		break;
	case PM_EVENT_HIBERNATE:
		if (ops->poweroff_noirq) {
			error = ops->poweroff_noirq(dev);
			suspend_report_result(ops->poweroff_noirq, error);
		}
		break;
	case PM_EVENT_THAW:
	case PM_EVENT_RECOVER:
		if (ops->thaw_noirq) {
			error = ops->thaw_noirq(dev);
			suspend_report_result(ops->thaw_noirq, error);
		}
		break;
	case PM_EVENT_RESTORE:
		if (ops->restore_noirq) {
			error = ops->restore_noirq(dev);
			suspend_report_result(ops->restore_noirq, error);
		}
		break;
#endif /* CONFIG_HIBERNATION */
	default:
		error = -EINVAL;
	}
	return error;
}

static char *pm_verb(int event)
{
	switch (event) {
	case PM_EVENT_SUSPEND:
		return "suspend";
	case PM_EVENT_RESUME:
		return "resume";
	case PM_EVENT_FREEZE:
		return "freeze";
	case PM_EVENT_QUIESCE:
		return "quiesce";
	case PM_EVENT_HIBERNATE:
		return "hibernate";
	case PM_EVENT_THAW:
		return "thaw";
	case PM_EVENT_RESTORE:
		return "restore";
	case PM_EVENT_RECOVER:
		return "recover";
	default:
		return "(unknown PM event)";
	}
}

static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
{
	dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
		((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
		", may wakeup" : "");
}

static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
			int error)
{
	printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
		kobject_name(&dev->kobj), pm_verb(state.event), info, error);
}

278 279 280
/*------------------------- Resume routines -------------------------*/

/**
281
 *	resume_device_noirq - Power on one device (early resume).
282
 *	@dev:	Device.
283
 *	@state: PM transition of the system being carried out.
284
 *
285
 *	Must be called with interrupts disabled.
286
 */
287
static int resume_device_noirq(struct device *dev, pm_message_t state)
288 289 290 291 292 293
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);

294 295 296 297 298 299 300 301
	if (!dev->bus)
		goto End;

	if (dev->bus->pm) {
		pm_dev_dbg(dev, state, "EARLY ");
		error = pm_noirq_op(dev, dev->bus->pm, state);
	} else if (dev->bus->resume_early) {
		pm_dev_dbg(dev, state, "legacy EARLY ");
302 303
		error = dev->bus->resume_early(dev);
	}
304
 End:
305 306 307 308 309 310
	TRACE_RESUME(error);
	return error;
}

/**
 *	dpm_power_up - Power on all regular (non-sysdev) devices.
311
 *	@state: PM transition of the system being carried out.
312
 *
313 314
 *	Execute the appropriate "noirq resume" callback for all devices marked
 *	as DPM_OFF_IRQ.
315 316 317
 *
 *	Must be called with interrupts disabled and only one CPU running.
 */
318
static void dpm_power_up(pm_message_t state)
319
{
320
	struct device *dev;
321

322 323 324
	list_for_each_entry(dev, &dpm_list, power.entry)
		if (dev->power.status > DPM_OFF) {
			int error;
325

326 327 328 329 330
			dev->power.status = DPM_OFF;
			error = resume_device_noirq(dev, state);
			if (error)
				pm_dev_err(dev, state, " early", error);
		}
331 332 333 334
}

/**
 *	device_power_up - Turn on all devices that need special attention.
335
 *	@state: PM transition of the system being carried out.
336 337 338 339 340 341
 *
 *	Power on system devices, then devices that required we shut them down
 *	with interrupts disabled.
 *
 *	Must be called with interrupts disabled.
 */
342
void device_power_up(pm_message_t state)
343 344
{
	sysdev_resume();
345
	dpm_power_up(state);
346 347 348 349 350 351
}
EXPORT_SYMBOL_GPL(device_power_up);

/**
 *	resume_device - Restore state for one device.
 *	@dev:	Device.
352
 *	@state: PM transition of the system being carried out.
353
 */
354
static int resume_device(struct device *dev, pm_message_t state)
355 356 357 358 359
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);
360

361 362
	down(&dev->sem);

363 364 365 366 367 368 369 370 371 372
	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
			error = pm_op(dev, &dev->bus->pm->base, state);
		} else if (dev->bus->resume) {
			pm_dev_dbg(dev, state, "legacy ");
			error = dev->bus->resume(dev);
		}
		if (error)
			goto End;
373 374
	}

375 376 377 378 379 380 381 382 383 384
	if (dev->type) {
		if (dev->type->pm) {
			pm_dev_dbg(dev, state, "type ");
			error = pm_op(dev, dev->type->pm, state);
		} else if (dev->type->resume) {
			pm_dev_dbg(dev, state, "legacy type ");
			error = dev->type->resume(dev);
		}
		if (error)
			goto End;
385 386
	}

387 388 389 390 391 392 393 394
	if (dev->class) {
		if (dev->class->pm) {
			pm_dev_dbg(dev, state, "class ");
			error = pm_op(dev, dev->class->pm, state);
		} else if (dev->class->resume) {
			pm_dev_dbg(dev, state, "legacy class ");
			error = dev->class->resume(dev);
		}
395
	}
396
 End:
397 398
	up(&dev->sem);

399 400 401 402
	TRACE_RESUME(error);
	return error;
}

403 404
/**
 *	dpm_resume - Resume every device.
405
 *	@state: PM transition of the system being carried out.
406
 *
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
 *	Execute the appropriate "resume" callback for all devices the status of
 *	which indicates that they are inactive.
 */
static void dpm_resume(pm_message_t state)
{
	struct list_head list;

	INIT_LIST_HEAD(&list);
	mutex_lock(&dpm_list_mtx);
	transition_started = false;
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.next);

		get_device(dev);
		if (dev->power.status >= DPM_OFF) {
			int error;

			dev->power.status = DPM_RESUMING;
			mutex_unlock(&dpm_list_mtx);

			error = resume_device(dev, state);

			mutex_lock(&dpm_list_mtx);
			if (error)
				pm_dev_err(dev, state, "", error);
		} else if (dev->power.status == DPM_SUSPENDING) {
			/* Allow new children of the device to be registered */
			dev->power.status = DPM_RESUMING;
		}
		if (!list_empty(&dev->power.entry))
			list_move_tail(&dev->power.entry, &list);
		put_device(dev);
	}
	list_splice(&list, &dpm_list);
	mutex_unlock(&dpm_list_mtx);
}

/**
 *	complete_device - Complete a PM transition for given device
 *	@dev:	Device.
 *	@state: PM transition of the system being carried out.
 */
static void complete_device(struct device *dev, pm_message_t state)
{
	down(&dev->sem);

	if (dev->class && dev->class->pm && dev->class->pm->complete) {
		pm_dev_dbg(dev, state, "completing class ");
		dev->class->pm->complete(dev);
	}

	if (dev->type && dev->type->pm && dev->type->pm->complete) {
		pm_dev_dbg(dev, state, "completing type ");
		dev->type->pm->complete(dev);
	}

	if (dev->bus && dev->bus->pm && dev->bus->pm->base.complete) {
		pm_dev_dbg(dev, state, "completing ");
		dev->bus->pm->base.complete(dev);
	}

	up(&dev->sem);
}

/**
 *	dpm_complete - Complete a PM transition for all devices.
 *	@state: PM transition of the system being carried out.
474
 *
475 476
 *	Execute the ->complete() callbacks for all devices that are not marked
 *	as DPM_ON.
477
 */
478
static void dpm_complete(pm_message_t state)
479
{
480 481 482
	struct list_head list;

	INIT_LIST_HEAD(&list);
483
	mutex_lock(&dpm_list_mtx);
484 485
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.prev);
486

487 488 489 490 491 492 493 494 495 496 497 498
		get_device(dev);
		if (dev->power.status > DPM_ON) {
			dev->power.status = DPM_ON;
			mutex_unlock(&dpm_list_mtx);

			complete_device(dev, state);

			mutex_lock(&dpm_list_mtx);
		}
		if (!list_empty(&dev->power.entry))
			list_move(&dev->power.entry, &list);
		put_device(dev);
499
	}
500
	list_splice(&list, &dpm_list);
501 502 503 504
	mutex_unlock(&dpm_list_mtx);
}

/**
505
 *	device_resume - Restore state of each device in system.
506
 *	@state: PM transition of the system being carried out.
507
 *
508 509
 *	Resume all the devices, unlock them all, and allow new
 *	devices to be registered once again.
510
 */
511
void device_resume(pm_message_t state)
512
{
513
	might_sleep();
514 515
	dpm_resume(state);
	dpm_complete(state);
516
}
517
EXPORT_SYMBOL_GPL(device_resume);
518 519 520 521


/*------------------------- Suspend routines -------------------------*/

522 523 524 525 526 527
/**
 *	resume_event - return a PM message representing the resume event
 *	               corresponding to given sleep state.
 *	@sleep_state: PM message representing a sleep state.
 */
static pm_message_t resume_event(pm_message_t sleep_state)
528
{
529 530 531 532 533 534 535 536
	switch (sleep_state.event) {
	case PM_EVENT_SUSPEND:
		return PMSG_RESUME;
	case PM_EVENT_FREEZE:
	case PM_EVENT_QUIESCE:
		return PMSG_RECOVER;
	case PM_EVENT_HIBERNATE:
		return PMSG_RESTORE;
537
	}
538
	return PMSG_ON;
539 540 541
}

/**
542
 *	suspend_device_noirq - Shut down one device (late suspend).
543
 *	@dev:	Device.
544
 *	@state: PM transition of the system being carried out.
545 546
 *
 *	This is called with interrupts off and only a single CPU running.
547
 */
548
static int suspend_device_noirq(struct device *dev, pm_message_t state)
549 550
{
	int error = 0;
551

552 553 554 555 556 557 558 559
	if (!dev->bus)
		return 0;

	if (dev->bus->pm) {
		pm_dev_dbg(dev, state, "LATE ");
		error = pm_noirq_op(dev, dev->bus->pm, state);
	} else if (dev->bus->suspend_late) {
		pm_dev_dbg(dev, state, "legacy LATE ");
560 561 562 563 564 565 566 567
		error = dev->bus->suspend_late(dev, state);
		suspend_report_result(dev->bus->suspend_late, error);
	}
	return error;
}

/**
 *	device_power_down - Shut down special devices.
568
 *	@state: PM transition of the system being carried out.
569
 *
570
 *	Power down devices that require interrupts to be disabled.
571 572 573 574 575 576
 *	Then power down system devices.
 *
 *	Must be called with interrupts disabled and only one CPU running.
 */
int device_power_down(pm_message_t state)
{
577
	struct device *dev;
578 579
	int error = 0;

580 581
	list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
		error = suspend_device_noirq(dev, state);
582
		if (error) {
583
			pm_dev_err(dev, state, " late", error);
584 585
			break;
		}
586
		dev->power.status = DPM_OFF_IRQ;
587 588 589 590
	}
	if (!error)
		error = sysdev_suspend(state);
	if (error)
591
		dpm_power_up(resume_event(state));
592 593 594 595 596 597 598
	return error;
}
EXPORT_SYMBOL_GPL(device_power_down);

/**
 *	suspend_device - Save state of one device.
 *	@dev:	Device.
599
 *	@state: PM transition of the system being carried out.
600
 */
A
Adrian Bunk 已提交
601
static int suspend_device(struct device *dev, pm_message_t state)
602 603 604
{
	int error = 0;

605 606
	down(&dev->sem);

607 608 609 610 611 612 613 614 615 616 617
	if (dev->class) {
		if (dev->class->pm) {
			pm_dev_dbg(dev, state, "class ");
			error = pm_op(dev, dev->class->pm, state);
		} else if (dev->class->suspend) {
			pm_dev_dbg(dev, state, "legacy class ");
			error = dev->class->suspend(dev, state);
			suspend_report_result(dev->class->suspend, error);
		}
		if (error)
			goto End;
618 619
	}

620 621 622 623 624 625 626 627 628 629 630
	if (dev->type) {
		if (dev->type->pm) {
			pm_dev_dbg(dev, state, "type ");
			error = pm_op(dev, dev->type->pm, state);
		} else if (dev->type->suspend) {
			pm_dev_dbg(dev, state, "legacy type ");
			error = dev->type->suspend(dev, state);
			suspend_report_result(dev->type->suspend, error);
		}
		if (error)
			goto End;
631 632
	}

633 634 635 636 637 638 639 640 641
	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
			error = pm_op(dev, &dev->bus->pm->base, state);
		} else if (dev->bus->suspend) {
			pm_dev_dbg(dev, state, "legacy ");
			error = dev->bus->suspend(dev, state);
			suspend_report_result(dev->bus->suspend, error);
		}
642
	}
643
 End:
644 645
	up(&dev->sem);

646 647 648 649
	return error;
}

/**
650
 *	dpm_suspend - Suspend every device.
651
 *	@state: PM transition of the system being carried out.
652
 *
653
 *	Execute the appropriate "suspend" callbacks for all devices.
654
 */
655
static int dpm_suspend(pm_message_t state)
656
{
657
	struct list_head list;
658 659
	int error = 0;

660
	INIT_LIST_HEAD(&list);
661
	mutex_lock(&dpm_list_mtx);
662 663
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.prev);
664

665
		get_device(dev);
666
		mutex_unlock(&dpm_list_mtx);
667

668
		error = suspend_device(dev, state);
669

670
		mutex_lock(&dpm_list_mtx);
671
		if (error) {
672 673
			pm_dev_err(dev, state, "", error);
			put_device(dev);
674 675
			break;
		}
676
		dev->power.status = DPM_OFF;
677
		if (!list_empty(&dev->power.entry))
678 679
			list_move(&dev->power.entry, &list);
		put_device(dev);
680
	}
681
	list_splice(&list, dpm_list.prev);
682
	mutex_unlock(&dpm_list_mtx);
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	return error;
}

/**
 *	prepare_device - Execute the ->prepare() callback(s) for given device.
 *	@dev:	Device.
 *	@state: PM transition of the system being carried out.
 */
static int prepare_device(struct device *dev, pm_message_t state)
{
	int error = 0;

	down(&dev->sem);

	if (dev->bus && dev->bus->pm && dev->bus->pm->base.prepare) {
		pm_dev_dbg(dev, state, "preparing ");
		error = dev->bus->pm->base.prepare(dev);
		suspend_report_result(dev->bus->pm->base.prepare, error);
		if (error)
			goto End;
	}

	if (dev->type && dev->type->pm && dev->type->pm->prepare) {
		pm_dev_dbg(dev, state, "preparing type ");
		error = dev->type->pm->prepare(dev);
		suspend_report_result(dev->type->pm->prepare, error);
		if (error)
			goto End;
	}

	if (dev->class && dev->class->pm && dev->class->pm->prepare) {
		pm_dev_dbg(dev, state, "preparing class ");
		error = dev->class->pm->prepare(dev);
		suspend_report_result(dev->class->pm->prepare, error);
	}
 End:
	up(&dev->sem);

	return error;
}
723

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
/**
 *	dpm_prepare - Prepare all devices for a PM transition.
 *	@state: PM transition of the system being carried out.
 *
 *	Execute the ->prepare() callback for all devices.
 */
static int dpm_prepare(pm_message_t state)
{
	struct list_head list;
	int error = 0;

	INIT_LIST_HEAD(&list);
	mutex_lock(&dpm_list_mtx);
	transition_started = true;
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.next);

		get_device(dev);
		dev->power.status = DPM_PREPARING;
		mutex_unlock(&dpm_list_mtx);

		error = prepare_device(dev, state);

		mutex_lock(&dpm_list_mtx);
		if (error) {
			dev->power.status = DPM_ON;
			if (error == -EAGAIN) {
				put_device(dev);
				continue;
			}
			printk(KERN_ERR "PM: Failed to prepare device %s "
				"for power transition: error %d\n",
				kobject_name(&dev->kobj), error);
			put_device(dev);
			break;
		}
		dev->power.status = DPM_SUSPENDING;
		if (!list_empty(&dev->power.entry))
			list_move_tail(&dev->power.entry, &list);
		put_device(dev);
	}
	list_splice(&list, &dpm_list);
	mutex_unlock(&dpm_list_mtx);
767 768 769
	return error;
}

770 771
/**
 *	device_suspend - Save state and stop all devices in system.
772
 *	@state: PM transition of the system being carried out.
773
 *
774
 *	Prepare and suspend all devices.
775 776 777 778
 */
int device_suspend(pm_message_t state)
{
	int error;
779

780
	might_sleep();
781 782 783
	error = dpm_prepare(state);
	if (!error)
		error = dpm_suspend(state);
784 785
	return error;
}
786
EXPORT_SYMBOL_GPL(device_suspend);
787 788 789 790 791

void __suspend_report_result(const char *function, void *fn, int ret)
{
	if (ret) {
		printk(KERN_ERR "%s(): ", function);
792
		print_fn_descriptor_symbol("%s returns ", fn);
793 794 795 796
		printk("%d\n", ret);
	}
}
EXPORT_SYMBOL_GPL(__suspend_report_result);