main.c 21.0 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
#include <linux/pm.h>
24
#include <linux/pm_runtime.h>
25
#include <linux/resume-trace.h>
26
#include <linux/rwsem.h>
27
#include <linux/interrupt.h>
28
#include <linux/sched.h>
29

30
#include "../base.h"
L
Linus Torvalds 已提交
31 32
#include "power.h"

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

43
LIST_HEAD(dpm_list);
L
Linus Torvalds 已提交
44

45
static DEFINE_MUTEX(dpm_list_mtx);
L
Linus Torvalds 已提交
46

47 48 49 50 51 52
/*
 * 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;

53
/**
54
 * device_pm_init - Initialize the PM-related part of a device object.
55 56 57 58 59 60 61 62
 * @dev: Device object being initialized.
 */
void device_pm_init(struct device *dev)
{
	dev->power.status = DPM_ON;
	pm_runtime_init(dev);
}

63
/**
64
 * device_pm_lock - Lock the list of active devices used by the PM core.
65 66 67 68 69 70 71
 */
void device_pm_lock(void)
{
	mutex_lock(&dpm_list_mtx);
}

/**
72
 * device_pm_unlock - Unlock the list of active devices used by the PM core.
73 74 75 76 77
 */
void device_pm_unlock(void)
{
	mutex_unlock(&dpm_list_mtx);
}
78

79
/**
80 81
 * device_pm_add - Add a device to the PM core's list of active devices.
 * @dev: Device to add to the list.
82
 */
83
void device_pm_add(struct device *dev)
L
Linus Torvalds 已提交
84 85
{
	pr_debug("PM: Adding info for %s:%s\n",
86 87
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
88
	mutex_lock(&dpm_list_mtx);
89
	if (dev->parent) {
90 91
		if (dev->parent->power.status >= DPM_SUSPENDING)
			dev_warn(dev, "parent %s should not be sleeping\n",
92
				 dev_name(dev->parent));
93 94 95 96 97 98
	} 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
		 */
99
		dev_WARN(dev, "Parentless device registered during a PM transaction\n");
100
	}
101 102

	list_add_tail(&dev->power.entry, &dpm_list);
103
	mutex_unlock(&dpm_list_mtx);
L
Linus Torvalds 已提交
104 105
}

106
/**
107 108
 * device_pm_remove - Remove a device from the PM core's list of active devices.
 * @dev: Device to be removed from the list.
109
 */
110
void device_pm_remove(struct device *dev)
L
Linus Torvalds 已提交
111 112
{
	pr_debug("PM: Removing info for %s:%s\n",
113 114
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
115
	mutex_lock(&dpm_list_mtx);
L
Linus Torvalds 已提交
116
	list_del_init(&dev->power.entry);
117
	mutex_unlock(&dpm_list_mtx);
118
	pm_runtime_remove(dev);
119 120
}

121
/**
122 123 124
 * device_pm_move_before - Move device in the PM core's list of active devices.
 * @deva: Device to move in dpm_list.
 * @devb: Device @deva should come before.
125 126 127 128 129 130 131 132 133 134 135 136 137
 */
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);
}

/**
138 139 140
 * device_pm_move_after - Move device in the PM core's list of active devices.
 * @deva: Device to move in dpm_list.
 * @devb: Device @deva should come after.
141 142 143 144 145 146 147 148 149 150 151 152 153
 */
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);
}

/**
154 155
 * device_pm_move_last - Move device to end of the PM core's list of devices.
 * @dev: Device to move in dpm_list.
156 157 158 159 160 161 162 163 164
 */
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);
}

165
/**
166 167 168 169
 * pm_op - Execute the PM operation appropriate for given PM event.
 * @dev: Device to handle.
 * @ops: PM operations to choose from.
 * @state: PM transition of the system being carried out.
170
 */
171 172 173
static int pm_op(struct device *dev,
		 const struct dev_pm_ops *ops,
		 pm_message_t state)
174 175
{
	int error = 0;
176 177 178 179 180 181 182
	ktime_t calltime, delta, rettime;

	if (initcall_debug) {
		pr_info("calling  %s+ @ %i\n",
				dev_name(dev), task_pid_nr(current));
		calltime = ktime_get();
	}
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

	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;
	}
230 231 232 233 234 235 236 237

	if (initcall_debug) {
		rettime = ktime_get();
		delta = ktime_sub(rettime, calltime);
		pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
			error, (unsigned long long)ktime_to_ns(delta) >> 10);
	}

238 239 240 241
	return error;
}

/**
242 243 244 245
 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
 * @dev: Device to handle.
 * @ops: PM operations to choose from.
 * @state: PM transition of the system being carried out.
246
 *
247 248
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
249
 */
250 251
static int pm_noirq_op(struct device *dev,
			const struct dev_pm_ops *ops,
252 253 254
			pm_message_t state)
{
	int error = 0;
255 256 257 258 259 260 261
	ktime_t calltime, delta, rettime;

	if (initcall_debug) {
		pr_info("calling  %s_i+ @ %i\n",
				dev_name(dev), task_pid_nr(current));
		calltime = ktime_get();
	}
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

	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;
	}
309 310 311 312 313 314 315 316

	if (initcall_debug) {
		rettime = ktime_get();
		delta = ktime_sub(rettime, calltime);
		printk("initcall %s_i+ returned %d after %Ld usecs\n", dev_name(dev),
			error, (unsigned long long)ktime_to_ns(delta) >> 10);
	}

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	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);
}

358 359 360
/*------------------------- Resume routines -------------------------*/

/**
361 362 363
 * device_resume_noirq - Execute an "early resume" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
364
 *
365 366
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
367
 */
368
static int device_resume_noirq(struct device *dev, pm_message_t state)
369 370 371 372 373 374
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);

375 376 377 378 379 380
	if (!dev->bus)
		goto End;

	if (dev->bus->pm) {
		pm_dev_dbg(dev, state, "EARLY ");
		error = pm_noirq_op(dev, dev->bus->pm, state);
381
	}
382
 End:
383 384 385 386 387
	TRACE_RESUME(error);
	return error;
}

/**
388 389
 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
390
 *
391 392
 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
 * enable device drivers to receive interrupts.
393
 */
394
void dpm_resume_noirq(pm_message_t state)
395
{
396
	struct device *dev;
397

398
	mutex_lock(&dpm_list_mtx);
399
	transition_started = false;
400 401 402
	list_for_each_entry(dev, &dpm_list, power.entry)
		if (dev->power.status > DPM_OFF) {
			int error;
403

404
			dev->power.status = DPM_OFF;
405
			error = device_resume_noirq(dev, state);
406 407 408
			if (error)
				pm_dev_err(dev, state, " early", error);
		}
409
	mutex_unlock(&dpm_list_mtx);
410
	resume_device_irqs();
411
}
412
EXPORT_SYMBOL_GPL(dpm_resume_noirq);
413 414

/**
415 416 417
 * device_resume - Execute "resume" callbacks for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
418
 */
419
static int device_resume(struct device *dev, pm_message_t state)
420 421 422 423 424
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);
425

426 427
	down(&dev->sem);

428 429 430
	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
431
			error = pm_op(dev, dev->bus->pm, state);
432 433 434 435 436 437
		} else if (dev->bus->resume) {
			pm_dev_dbg(dev, state, "legacy ");
			error = dev->bus->resume(dev);
		}
		if (error)
			goto End;
438 439
	}

440 441 442 443 444 445 446
	if (dev->type) {
		if (dev->type->pm) {
			pm_dev_dbg(dev, state, "type ");
			error = pm_op(dev, dev->type->pm, state);
		}
		if (error)
			goto End;
447 448
	}

449 450 451 452 453 454 455 456
	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);
		}
457
	}
458
 End:
459 460
	up(&dev->sem);

461 462 463 464
	TRACE_RESUME(error);
	return error;
}

465
/**
466 467
 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
468
 *
469 470
 * Execute the appropriate "resume" callback for all devices whose status
 * indicates that they are suspended.
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
 */
static void dpm_resume(pm_message_t state)
{
	struct list_head list;

	INIT_LIST_HEAD(&list);
	mutex_lock(&dpm_list_mtx);
	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);

488
			error = device_resume(dev, state);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

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

/**
506 507 508
 * device_complete - Complete a PM transition for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
509
 */
510
static void device_complete(struct device *dev, pm_message_t state)
511 512 513 514 515 516 517 518 519 520 521 522 523
{
	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);
	}

524
	if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
525
		pm_dev_dbg(dev, state, "completing ");
526
		dev->bus->pm->complete(dev);
527 528 529 530 531 532
	}

	up(&dev->sem);
}

/**
533 534
 * dpm_complete - Complete a PM transition for all non-sysdev devices.
 * @state: PM transition of the system being carried out.
535
 *
536 537
 * Execute the ->complete() callbacks for all devices whose PM status is not
 * DPM_ON (this allows new devices to be registered).
538
 */
539
static void dpm_complete(pm_message_t state)
540
{
541 542 543
	struct list_head list;

	INIT_LIST_HEAD(&list);
544
	mutex_lock(&dpm_list_mtx);
R
Romit Dasgupta 已提交
545
	transition_started = false;
546 547
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.prev);
548

549 550 551 552 553
		get_device(dev);
		if (dev->power.status > DPM_ON) {
			dev->power.status = DPM_ON;
			mutex_unlock(&dpm_list_mtx);

554
			device_complete(dev, state);
555
			pm_runtime_put_noidle(dev);
556 557 558 559 560 561

			mutex_lock(&dpm_list_mtx);
		}
		if (!list_empty(&dev->power.entry))
			list_move(&dev->power.entry, &list);
		put_device(dev);
562
	}
563
	list_splice(&list, &dpm_list);
564 565 566 567
	mutex_unlock(&dpm_list_mtx);
}

/**
568 569
 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
 * @state: PM transition of the system being carried out.
570
 *
571 572
 * Execute "resume" callbacks for all devices and complete the PM transition of
 * the system.
573
 */
574
void dpm_resume_end(pm_message_t state)
575
{
576
	might_sleep();
577 578
	dpm_resume(state);
	dpm_complete(state);
579
}
580
EXPORT_SYMBOL_GPL(dpm_resume_end);
581 582 583 584


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

585
/**
586 587 588 589 590
 * resume_event - Return a "resume" message for given "suspend" sleep state.
 * @sleep_state: PM message representing a sleep state.
 *
 * Return a PM message representing the resume event corresponding to given
 * sleep state.
591 592
 */
static pm_message_t resume_event(pm_message_t sleep_state)
593
{
594 595 596 597 598 599 600 601
	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;
602
	}
603
	return PMSG_ON;
604 605 606
}

/**
607 608 609
 * device_suspend_noirq - Execute a "late suspend" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
610
 *
611 612
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
613
 */
614
static int device_suspend_noirq(struct device *dev, pm_message_t state)
615 616
{
	int error = 0;
617

618 619 620 621 622 623
	if (!dev->bus)
		return 0;

	if (dev->bus->pm) {
		pm_dev_dbg(dev, state, "LATE ");
		error = pm_noirq_op(dev, dev->bus->pm, state);
624 625 626 627 628
	}
	return error;
}

/**
629 630
 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
631
 *
632 633
 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
 * handlers for all non-sysdev devices.
634
 */
635
int dpm_suspend_noirq(pm_message_t state)
636
{
637
	struct device *dev;
638 639
	int error = 0;

640
	suspend_device_irqs();
641
	mutex_lock(&dpm_list_mtx);
642
	list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
643
		error = device_suspend_noirq(dev, state);
644
		if (error) {
645
			pm_dev_err(dev, state, " late", error);
646 647
			break;
		}
648
		dev->power.status = DPM_OFF_IRQ;
649
	}
650
	mutex_unlock(&dpm_list_mtx);
651
	if (error)
652
		dpm_resume_noirq(resume_event(state));
653 654
	return error;
}
655
EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
656 657

/**
658 659 660
 * device_suspend - Execute "suspend" callbacks for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
661
 */
662
static int device_suspend(struct device *dev, pm_message_t state)
663 664 665
{
	int error = 0;

666 667
	down(&dev->sem);

668 669 670 671 672 673 674 675 676 677 678
	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;
679 680
	}

681 682 683 684 685 686 687
	if (dev->type) {
		if (dev->type->pm) {
			pm_dev_dbg(dev, state, "type ");
			error = pm_op(dev, dev->type->pm, state);
		}
		if (error)
			goto End;
688 689
	}

690 691 692
	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
693
			error = pm_op(dev, dev->bus->pm, state);
694 695 696 697 698
		} else if (dev->bus->suspend) {
			pm_dev_dbg(dev, state, "legacy ");
			error = dev->bus->suspend(dev, state);
			suspend_report_result(dev->bus->suspend, error);
		}
699
	}
700
 End:
701 702
	up(&dev->sem);

703 704 705 706
	return error;
}

/**
707 708
 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
 * @state: PM transition of the system being carried out.
709
 */
710
static int dpm_suspend(pm_message_t state)
711
{
712
	struct list_head list;
713 714
	int error = 0;

715
	INIT_LIST_HEAD(&list);
716
	mutex_lock(&dpm_list_mtx);
717 718
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.prev);
719

720
		get_device(dev);
721
		mutex_unlock(&dpm_list_mtx);
722

723
		error = device_suspend(dev, state);
724

725
		mutex_lock(&dpm_list_mtx);
726
		if (error) {
727 728
			pm_dev_err(dev, state, "", error);
			put_device(dev);
729 730
			break;
		}
731
		dev->power.status = DPM_OFF;
732
		if (!list_empty(&dev->power.entry))
733 734
			list_move(&dev->power.entry, &list);
		put_device(dev);
735
	}
736
	list_splice(&list, dpm_list.prev);
737
	mutex_unlock(&dpm_list_mtx);
738 739 740 741
	return error;
}

/**
742 743 744 745 746 747
 * device_prepare - Prepare a device for system power transition.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
 *
 * Execute the ->prepare() callback(s) for given device.  No new children of the
 * device may be registered after this function has returned.
748
 */
749
static int device_prepare(struct device *dev, pm_message_t state)
750 751 752 753 754
{
	int error = 0;

	down(&dev->sem);

755
	if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
756
		pm_dev_dbg(dev, state, "preparing ");
757 758
		error = dev->bus->pm->prepare(dev);
		suspend_report_result(dev->bus->pm->prepare, error);
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
		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;
}
781

782
/**
783 784
 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
 * @state: PM transition of the system being carried out.
785
 *
786
 * Execute the ->prepare() callback(s) for all devices.
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
 */
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);

803 804 805 806 807 808 809 810
		pm_runtime_get_noresume(dev);
		if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
			/* Wake-up requested during system sleep transition. */
			pm_runtime_put_noidle(dev);
			error = -EBUSY;
		} else {
			error = device_prepare(dev, state);
		}
811 812 813 814 815 816

		mutex_lock(&dpm_list_mtx);
		if (error) {
			dev->power.status = DPM_ON;
			if (error == -EAGAIN) {
				put_device(dev);
S
Sebastian Ott 已提交
817
				error = 0;
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
				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);
833 834 835
	return error;
}

836
/**
837 838
 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
 * @state: PM transition of the system being carried out.
839
 *
840 841
 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
 * callbacks for them.
842
 */
843
int dpm_suspend_start(pm_message_t state)
844 845
{
	int error;
846

847
	might_sleep();
848 849 850
	error = dpm_prepare(state);
	if (!error)
		error = dpm_suspend(state);
851 852
	return error;
}
853
EXPORT_SYMBOL_GPL(dpm_suspend_start);
854 855 856

void __suspend_report_result(const char *function, void *fn, int ret)
{
857 858
	if (ret)
		printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
859 860
}
EXPORT_SYMBOL_GPL(__suspend_report_result);