main.c 19.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
#include <linux/interrupt.h>
27

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

31
/*
32
 * The entries in the dpm_list list are in a depth first order, simply
33 34 35 36 37 38 39 40
 * 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.
 */

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

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

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
 * 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);
}
66

67 68 69 70
/**
 *	device_pm_add - add a device to the list of active devices
 *	@dev:	Device to be added to the list
 */
71
void device_pm_add(struct device *dev)
L
Linus Torvalds 已提交
72 73
{
	pr_debug("PM: Adding info for %s:%s\n",
74 75
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
76
	mutex_lock(&dpm_list_mtx);
77
	if (dev->parent) {
78 79
		if (dev->parent->power.status >= DPM_SUSPENDING)
			dev_warn(dev, "parent %s should not be sleeping\n",
80
				 dev_name(dev->parent));
81 82 83 84 85 86
	} 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
		 */
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);
L
Linus Torvalds 已提交
92 93
}

94 95 96 97 98 99
/**
 *	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.
 */
100
void device_pm_remove(struct device *dev)
L
Linus Torvalds 已提交
101 102
{
	pr_debug("PM: Removing info for %s:%s\n",
103 104
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
105
	mutex_lock(&dpm_list_mtx);
L
Linus Torvalds 已提交
106
	list_del_init(&dev->power.entry);
107
	mutex_unlock(&dpm_list_mtx);
108 109
}

110 111 112 113 114 115 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
/**
 *	device_pm_move_before - move device in dpm_list
 *	@deva:  Device to move in dpm_list
 *	@devb:  Device @deva should come before
 */
void device_pm_move_before(struct device *deva, struct device *devb)
{
	pr_debug("PM: Moving %s:%s before %s:%s\n",
		 deva->bus ? deva->bus->name : "No Bus",
		 kobject_name(&deva->kobj),
		 devb->bus ? devb->bus->name : "No Bus",
		 kobject_name(&devb->kobj));
	/* Delete deva from dpm_list and reinsert before devb. */
	list_move_tail(&deva->power.entry, &devb->power.entry);
}

/**
 *	device_pm_move_after - move device in dpm_list
 *	@deva:  Device to move in dpm_list
 *	@devb:  Device @deva should come after
 */
void device_pm_move_after(struct device *deva, struct device *devb)
{
	pr_debug("PM: Moving %s:%s after %s:%s\n",
		 deva->bus ? deva->bus->name : "No Bus",
		 kobject_name(&deva->kobj),
		 devb->bus ? devb->bus->name : "No Bus",
		 kobject_name(&devb->kobj));
	/* Delete deva from dpm_list and reinsert after devb. */
	list_move(&deva->power.entry, &devb->power.entry);
}

/**
 * 	device_pm_move_last - move device to end of dpm_list
 * 	@dev:   Device to move in dpm_list
 */
void device_pm_move_last(struct device *dev)
{
	pr_debug("PM: Moving %s:%s to end of list\n",
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
	list_move_tail(&dev->power.entry, &dpm_list);
}

154 155 156 157 158 159
/**
 *	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.
 */
160 161
static int pm_op(struct device *dev, struct dev_pm_ops *ops,
			pm_message_t state)
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
{
	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.
 */
223
static int pm_noirq_op(struct device *dev, struct dev_pm_ops *ops,
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
			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);
}

315 316 317
/*------------------------- Resume routines -------------------------*/

/**
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 327 328 329 330
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);

331 332 333 334 335 336 337 338
	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 ");
339 340
		error = dev->bus->resume_early(dev);
	}
341
 End:
342 343 344 345 346
	TRACE_RESUME(error);
	return error;
}

/**
347
 *	dpm_resume_noirq - Power on all regular (non-sysdev) devices.
348
 *	@state: PM transition of the system being carried out.
349
 *
350 351
 *	Call the "noirq" resume handlers for all devices marked as
 *	DPM_OFF_IRQ and enable device drivers to receive interrupts.
352
 *
353 354
 *	Must be called under dpm_list_mtx.  Device drivers should not receive
 *	interrupts while it's being executed.
355
 */
356
void dpm_resume_noirq(pm_message_t state)
357
{
358
	struct device *dev;
359

360
	mutex_lock(&dpm_list_mtx);
361 362 363
	list_for_each_entry(dev, &dpm_list, power.entry)
		if (dev->power.status > DPM_OFF) {
			int error;
364

365
			dev->power.status = DPM_OFF;
366
			error = device_resume_noirq(dev, state);
367 368 369
			if (error)
				pm_dev_err(dev, state, " early", error);
		}
370
	mutex_unlock(&dpm_list_mtx);
371
	resume_device_irqs();
372
}
373
EXPORT_SYMBOL_GPL(dpm_resume_noirq);
374 375

/**
376
 *	device_resume - Restore state for one device.
377
 *	@dev:	Device.
378
 *	@state: PM transition of the system being carried out.
379
 */
380
static int device_resume(struct device *dev, pm_message_t state)
381 382 383 384 385
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);
386

387 388
	down(&dev->sem);

389 390 391
	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
392
			error = pm_op(dev, dev->bus->pm, state);
393 394 395 396 397 398
		} else if (dev->bus->resume) {
			pm_dev_dbg(dev, state, "legacy ");
			error = dev->bus->resume(dev);
		}
		if (error)
			goto End;
399 400
	}

401 402 403 404 405 406 407 408 409 410
	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;
411 412
	}

413 414 415 416 417 418 419 420
	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);
		}
421
	}
422
 End:
423 424
	up(&dev->sem);

425 426 427 428
	TRACE_RESUME(error);
	return error;
}

429 430
/**
 *	dpm_resume - Resume every device.
431
 *	@state: PM transition of the system being carried out.
432
 *
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
 *	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);

453
			error = device_resume(dev, state);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470

			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);
}

/**
471
 *	device_complete - Complete a PM transition for given device
472 473 474
 *	@dev:	Device.
 *	@state: PM transition of the system being carried out.
 */
475
static void device_complete(struct device *dev, pm_message_t state)
476 477 478 479 480 481 482 483 484 485 486 487 488
{
	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);
	}

489
	if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
490
		pm_dev_dbg(dev, state, "completing ");
491
		dev->bus->pm->complete(dev);
492 493 494 495 496 497 498 499
	}

	up(&dev->sem);
}

/**
 *	dpm_complete - Complete a PM transition for all devices.
 *	@state: PM transition of the system being carried out.
500
 *
501 502
 *	Execute the ->complete() callbacks for all devices that are not marked
 *	as DPM_ON.
503
 */
504
static void dpm_complete(pm_message_t state)
505
{
506 507 508
	struct list_head list;

	INIT_LIST_HEAD(&list);
509
	mutex_lock(&dpm_list_mtx);
510 511
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.prev);
512

513 514 515 516 517
		get_device(dev);
		if (dev->power.status > DPM_ON) {
			dev->power.status = DPM_ON;
			mutex_unlock(&dpm_list_mtx);

518
			device_complete(dev, state);
519 520 521 522 523 524

			mutex_lock(&dpm_list_mtx);
		}
		if (!list_empty(&dev->power.entry))
			list_move(&dev->power.entry, &list);
		put_device(dev);
525
	}
526
	list_splice(&list, &dpm_list);
527 528 529 530
	mutex_unlock(&dpm_list_mtx);
}

/**
531
 *	dpm_resume_end - Restore state of each device in system.
532
 *	@state: PM transition of the system being carried out.
533
 *
534 535
 *	Resume all the devices, unlock them all, and allow new
 *	devices to be registered once again.
536
 */
537
void dpm_resume_end(pm_message_t state)
538
{
539
	might_sleep();
540 541
	dpm_resume(state);
	dpm_complete(state);
542
}
543
EXPORT_SYMBOL_GPL(dpm_resume_end);
544 545 546 547


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

548 549 550 551 552 553
/**
 *	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)
554
{
555 556 557 558 559 560 561 562
	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;
563
	}
564
	return PMSG_ON;
565 566 567
}

/**
568
 *	device_suspend_noirq - Shut down one device (late suspend).
569
 *	@dev:	Device.
570
 *	@state: PM transition of the system being carried out.
571 572
 *
 *	This is called with interrupts off and only a single CPU running.
573
 */
574
static int device_suspend_noirq(struct device *dev, pm_message_t state)
575 576
{
	int error = 0;
577

578 579 580 581 582 583 584 585
	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 ");
586 587 588 589 590 591 592
		error = dev->bus->suspend_late(dev, state);
		suspend_report_result(dev->bus->suspend_late, error);
	}
	return error;
}

/**
593
 *	dpm_suspend_noirq - Power down all regular (non-sysdev) devices.
594
 *	@state: PM transition of the system being carried out.
595
 *
596
 *	Prevent device drivers from receiving interrupts and call the "noirq"
597
 *	suspend handlers.
598
 *
599
 *	Must be called under dpm_list_mtx.
600
 */
601
int dpm_suspend_noirq(pm_message_t state)
602
{
603
	struct device *dev;
604 605
	int error = 0;

606
	suspend_device_irqs();
607
	mutex_lock(&dpm_list_mtx);
608
	list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
609
		error = device_suspend_noirq(dev, state);
610
		if (error) {
611
			pm_dev_err(dev, state, " late", error);
612 613
			break;
		}
614
		dev->power.status = DPM_OFF_IRQ;
615
	}
616
	mutex_unlock(&dpm_list_mtx);
617
	if (error)
618
		dpm_resume_noirq(resume_event(state));
619 620
	return error;
}
621
EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
622 623

/**
624
 *	device_suspend - Save state of one device.
625
 *	@dev:	Device.
626
 *	@state: PM transition of the system being carried out.
627
 */
628
static int device_suspend(struct device *dev, pm_message_t state)
629 630 631
{
	int error = 0;

632 633
	down(&dev->sem);

634 635 636 637 638 639 640 641 642 643 644
	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;
645 646
	}

647 648 649 650 651 652 653 654 655 656 657
	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;
658 659
	}

660 661 662
	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
663
			error = pm_op(dev, dev->bus->pm, state);
664 665 666 667 668
		} else if (dev->bus->suspend) {
			pm_dev_dbg(dev, state, "legacy ");
			error = dev->bus->suspend(dev, state);
			suspend_report_result(dev->bus->suspend, error);
		}
669
	}
670
 End:
671 672
	up(&dev->sem);

673 674 675 676
	return error;
}

/**
677
 *	dpm_suspend - Suspend every device.
678
 *	@state: PM transition of the system being carried out.
679
 *
680
 *	Execute the appropriate "suspend" callbacks for all devices.
681
 */
682
static int dpm_suspend(pm_message_t state)
683
{
684
	struct list_head list;
685 686
	int error = 0;

687
	INIT_LIST_HEAD(&list);
688
	mutex_lock(&dpm_list_mtx);
689 690
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.prev);
691

692
		get_device(dev);
693
		mutex_unlock(&dpm_list_mtx);
694

695
		error = device_suspend(dev, state);
696

697
		mutex_lock(&dpm_list_mtx);
698
		if (error) {
699 700
			pm_dev_err(dev, state, "", error);
			put_device(dev);
701 702
			break;
		}
703
		dev->power.status = DPM_OFF;
704
		if (!list_empty(&dev->power.entry))
705 706
			list_move(&dev->power.entry, &list);
		put_device(dev);
707
	}
708
	list_splice(&list, dpm_list.prev);
709
	mutex_unlock(&dpm_list_mtx);
710 711 712 713
	return error;
}

/**
714
 *	device_prepare - Execute the ->prepare() callback(s) for given device.
715 716 717
 *	@dev:	Device.
 *	@state: PM transition of the system being carried out.
 */
718
static int device_prepare(struct device *dev, pm_message_t state)
719 720 721 722 723
{
	int error = 0;

	down(&dev->sem);

724
	if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
725
		pm_dev_dbg(dev, state, "preparing ");
726 727
		error = dev->bus->pm->prepare(dev);
		suspend_report_result(dev->bus->pm->prepare, error);
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
		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;
}
750

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
/**
 *	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);

772
		error = device_prepare(dev, state);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793

		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);
794 795 796
	return error;
}

797
/**
798
 *	dpm_suspend_start - Save state and stop all devices in system.
799
 *	@state: PM transition of the system being carried out.
800
 *
801
 *	Prepare and suspend all devices.
802
 */
803
int dpm_suspend_start(pm_message_t state)
804 805
{
	int error;
806

807
	might_sleep();
808 809 810
	error = dpm_prepare(state);
	if (!error)
		error = dpm_suspend(state);
811 812
	return error;
}
813
EXPORT_SYMBOL_GPL(dpm_suspend_start);
814 815 816

void __suspend_report_result(const char *function, void *fn, int ret)
{
817 818
	if (ret)
		printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
819 820
}
EXPORT_SYMBOL_GPL(__suspend_report_result);