pci-driver.c 26.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * drivers/pci/pci-driver.c
 *
4 5 6 7 8
 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
 * (C) Copyright 2007 Novell Inc.
 *
 * Released under the GPL v2 only.
 *
L
Linus Torvalds 已提交
9 10 11 12 13 14
 */

#include <linux/pci.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
15
#include <linux/mempolicy.h>
T
Tim Schmielau 已提交
16 17
#include <linux/string.h>
#include <linux/slab.h>
18
#include <linux/sched.h>
19
#include <linux/cpu.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25
#include "pci.h"

/*
 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
 */

26 27 28 29
struct pci_dynid {
	struct list_head node;
	struct pci_device_id id;
};
L
Linus Torvalds 已提交
30

31 32
#ifdef CONFIG_HOTPLUG

L
Linus Torvalds 已提交
33
/**
R
Randy Dunlap 已提交
34 35 36 37
 * store_new_id - add a new PCI device ID to this driver and re-probe devices
 * @driver: target device driver
 * @buf: buffer for scanning device ID data
 * @count: input size
L
Linus Torvalds 已提交
38 39 40 41
 *
 * Adds a new dynamic pci device ID to this driver,
 * and causes the driver to probe for all devices again.
 */
42
static ssize_t
L
Linus Torvalds 已提交
43 44
store_new_id(struct device_driver *driver, const char *buf, size_t count)
{
45
	struct pci_dynid *dynid;
L
Linus Torvalds 已提交
46
	struct pci_driver *pdrv = to_pci_driver(driver);
47
	const struct pci_device_id *ids = pdrv->id_table;
48
	__u32 vendor, device, subvendor=PCI_ANY_ID,
L
Linus Torvalds 已提交
49 50 51
		subdevice=PCI_ANY_ID, class=0, class_mask=0;
	unsigned long driver_data=0;
	int fields=0;
52
	int retval=0;
L
Linus Torvalds 已提交
53

54
	fields = sscanf(buf, "%x %x %x %x %x %x %lx",
L
Linus Torvalds 已提交
55 56
			&vendor, &device, &subvendor, &subdevice,
			&class, &class_mask, &driver_data);
57
	if (fields < 2)
L
Linus Torvalds 已提交
58 59
		return -EINVAL;

60 61
	/* Only accept driver_data values that match an existing id_table
	   entry */
62 63 64 65 66 67 68 69
	if (ids) {
		retval = -EINVAL;
		while (ids->vendor || ids->subvendor || ids->class_mask) {
			if (driver_data == ids->driver_data) {
				retval = 0;
				break;
			}
			ids++;
70
		}
71 72
		if (retval)	/* No match */
			return retval;
73 74
	}

75
	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83 84
	if (!dynid)
		return -ENOMEM;

	dynid->id.vendor = vendor;
	dynid->id.device = device;
	dynid->id.subvendor = subvendor;
	dynid->id.subdevice = subdevice;
	dynid->id.class = class;
	dynid->id.class_mask = class_mask;
M
Milton Miller 已提交
85
	dynid->id.driver_data = driver_data;
L
Linus Torvalds 已提交
86 87

	spin_lock(&pdrv->dynids.lock);
88
	list_add_tail(&dynid->node, &pdrv->dynids.list);
L
Linus Torvalds 已提交
89 90
	spin_unlock(&pdrv->dynids.lock);

91
	if (get_driver(&pdrv->driver)) {
92
		retval = driver_attach(&pdrv->driver);
93
		put_driver(&pdrv->driver);
L
Linus Torvalds 已提交
94 95
	}

96 97
	if (retval)
		return retval;
L
Linus Torvalds 已提交
98 99 100 101
	return count;
}
static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);

C
Chris Wright 已提交
102 103 104 105 106 107 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
/**
 * store_remove_id - remove a PCI device ID from this driver
 * @driver: target device driver
 * @buf: buffer for scanning device ID data
 * @count: input size
 *
 * Removes a dynamic pci device ID to this driver.
 */
static ssize_t
store_remove_id(struct device_driver *driver, const char *buf, size_t count)
{
	struct pci_dynid *dynid, *n;
	struct pci_driver *pdrv = to_pci_driver(driver);
	__u32 vendor, device, subvendor = PCI_ANY_ID,
		subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
	int fields = 0;
	int retval = -ENODEV;

	fields = sscanf(buf, "%x %x %x %x %x %x",
			&vendor, &device, &subvendor, &subdevice,
			&class, &class_mask);
	if (fields < 2)
		return -EINVAL;

	spin_lock(&pdrv->dynids.lock);
	list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
		struct pci_device_id *id = &dynid->id;
		if ((id->vendor == vendor) &&
		    (id->device == device) &&
		    (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
		    (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
		    !((id->class ^ class) & class_mask)) {
			list_del(&dynid->node);
			kfree(dynid);
			retval = 0;
			break;
		}
	}
	spin_unlock(&pdrv->dynids.lock);

	if (retval)
		return retval;
	return count;
}
static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);

L
Linus Torvalds 已提交
148 149 150
static void
pci_free_dynids(struct pci_driver *drv)
{
151
	struct pci_dynid *dynid, *n;
L
Linus Torvalds 已提交
152 153

	spin_lock(&drv->dynids.lock);
154
	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163 164 165
		list_del(&dynid->node);
		kfree(dynid);
	}
	spin_unlock(&drv->dynids.lock);
}

static int
pci_create_newid_file(struct pci_driver *drv)
{
	int error = 0;
	if (drv->probe != NULL)
166
		error = driver_create_file(&drv->driver, &driver_attr_new_id);
L
Linus Torvalds 已提交
167 168 169
	return error;
}

170 171 172 173
static void pci_remove_newid_file(struct pci_driver *drv)
{
	driver_remove_file(&drv->driver, &driver_attr_new_id);
}
C
Chris Wright 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187

static int
pci_create_removeid_file(struct pci_driver *drv)
{
	int error = 0;
	if (drv->probe != NULL)
		error = driver_create_file(&drv->driver,&driver_attr_remove_id);
	return error;
}

static void pci_remove_removeid_file(struct pci_driver *drv)
{
	driver_remove_file(&drv->driver, &driver_attr_remove_id);
}
L
Linus Torvalds 已提交
188 189 190 191 192 193
#else /* !CONFIG_HOTPLUG */
static inline void pci_free_dynids(struct pci_driver *drv) {}
static inline int pci_create_newid_file(struct pci_driver *drv)
{
	return 0;
}
194
static inline void pci_remove_newid_file(struct pci_driver *drv) {}
C
Chris Wright 已提交
195 196 197 198 199
static inline int pci_create_removeid_file(struct pci_driver *drv)
{
	return 0;
}
static inline void pci_remove_removeid_file(struct pci_driver *drv) {}
L
Linus Torvalds 已提交
200 201 202
#endif

/**
203
 * pci_match_id - See if a pci device matches a given pci_id table
L
Linus Torvalds 已提交
204
 * @ids: array of PCI device id structures to search in
205 206
 * @dev: the PCI device structure to match against.
 *
L
Linus Torvalds 已提交
207
 * Used by a driver to check whether a PCI device present in the
208
 * system is in its list of supported devices.  Returns the matching
L
Linus Torvalds 已提交
209
 * pci_device_id structure or %NULL if there is no match.
210
 *
R
Randy Dunlap 已提交
211
 * Deprecated, don't use this as it will not catch any dynamic ids
212
 * that a driver might want to check for.
L
Linus Torvalds 已提交
213
 */
214 215
const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
					 struct pci_dev *dev)
L
Linus Torvalds 已提交
216
{
217 218 219 220 221 222
	if (ids) {
		while (ids->vendor || ids->subvendor || ids->class_mask) {
			if (pci_match_one_device(ids, dev))
				return ids;
			ids++;
		}
L
Linus Torvalds 已提交
223 224 225 226 227
	}
	return NULL;
}

/**
R
Randy Dunlap 已提交
228
 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
229
 * @drv: the PCI driver to match against
230
 * @dev: the PCI device structure to match against
231 232 233 234
 *
 * Used by a driver to check whether a PCI device present in the
 * system is in its list of supported devices.  Returns the matching
 * pci_device_id structure or %NULL if there is no match.
L
Linus Torvalds 已提交
235
 */
A
Adrian Bunk 已提交
236 237
static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
						    struct pci_dev *dev)
238 239
{
	struct pci_dynid *dynid;
L
Linus Torvalds 已提交
240

241
	/* Look at the dynamic ids first, before the static ones */
242 243 244 245 246 247
	spin_lock(&drv->dynids.lock);
	list_for_each_entry(dynid, &drv->dynids.list, node) {
		if (pci_match_one_device(&dynid->id, dev)) {
			spin_unlock(&drv->dynids.lock);
			return &dynid->id;
		}
L
Linus Torvalds 已提交
248
	}
249
	spin_unlock(&drv->dynids.lock);
250 251

	return pci_match_id(drv->id_table, dev);
L
Linus Torvalds 已提交
252 253
}

254 255 256 257 258 259 260 261 262 263 264 265 266
struct drv_dev_and_id {
	struct pci_driver *drv;
	struct pci_dev *dev;
	const struct pci_device_id *id;
};

static long local_pci_probe(void *_ddi)
{
	struct drv_dev_and_id *ddi = _ddi;

	return ddi->drv->probe(ddi->dev, ddi->id);
}

267 268 269
static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
			  const struct pci_device_id *id)
{
270 271 272 273 274 275 276 277
	int error, node;
	struct drv_dev_and_id ddi = { drv, dev, id };

	/* Execute driver initialization on node where the device's
	   bus is attached to.  This way the driver likely allocates
	   its local memory on the right node without any need to
	   change it. */
	node = dev_to_node(&dev->dev);
278
	if (node >= 0) {
279 280 281
		int cpu;

		get_online_cpus();
282
		cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
283 284 285 286 287 288 289
		if (cpu < nr_cpu_ids)
			error = work_on_cpu(cpu, local_pci_probe, &ddi);
		else
			error = local_pci_probe(&ddi);
		put_online_cpus();
	} else
		error = local_pci_probe(&ddi);
290 291 292
	return error;
}

L
Linus Torvalds 已提交
293 294
/**
 * __pci_device_probe()
R
Randy Dunlap 已提交
295 296
 * @drv: driver to call to check if it wants the PCI device
 * @pci_dev: PCI device being probed
L
Linus Torvalds 已提交
297
 * 
R
Randy Dunlap 已提交
298
 * returns 0 on success, else error.
L
Linus Torvalds 已提交
299 300 301 302
 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
 */
static int
__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
303 304
{
	const struct pci_device_id *id;
L
Linus Torvalds 已提交
305 306 307
	int error = 0;

	if (!pci_dev->driver && drv->probe) {
308 309 310 311
		error = -ENODEV;

		id = pci_match_device(drv, pci_dev);
		if (id)
312
			error = pci_call_probe(drv, pci_dev, id);
313 314 315 316
		if (error >= 0) {
			pci_dev->driver = drv;
			error = 0;
		}
L
Linus Torvalds 已提交
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
	}
	return error;
}

static int pci_device_probe(struct device * dev)
{
	int error = 0;
	struct pci_driver *drv;
	struct pci_dev *pci_dev;

	drv = to_pci_driver(dev->driver);
	pci_dev = to_pci_dev(dev);
	pci_dev_get(pci_dev);
	error = __pci_device_probe(drv, pci_dev);
	if (error)
		pci_dev_put(pci_dev);

	return error;
}

static int pci_device_remove(struct device * dev)
{
	struct pci_dev * pci_dev = to_pci_dev(dev);
	struct pci_driver * drv = pci_dev->driver;

	if (drv) {
		if (drv->remove)
			drv->remove(pci_dev);
		pci_dev->driver = NULL;
	}

348 349 350 351 352 353 354
	/*
	 * If the device is still on, set the power state as "unknown",
	 * since it might change by the next time we load the driver.
	 */
	if (pci_dev->current_state == PCI_D0)
		pci_dev->current_state = PCI_UNKNOWN;

L
Linus Torvalds 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367
	/*
	 * We would love to complain here if pci_dev->is_enabled is set, that
	 * the driver should have called pci_disable_device(), but the
	 * unfortunate fact is there are too many odd BIOS and bridge setups
	 * that don't like drivers doing that all of the time.  
	 * Oh well, we can dream of sane hardware when we sleep, no matter how
	 * horrible the crap we have to deal with is when we are awake...
	 */

	pci_dev_put(pci_dev);
	return 0;
}

368 369 370 371 372 373 374 375 376 377 378 379 380
static void pci_device_shutdown(struct device *dev)
{
	struct pci_dev *pci_dev = to_pci_dev(dev);
	struct pci_driver *drv = pci_dev->driver;

	if (drv && drv->shutdown)
		drv->shutdown(pci_dev);
	pci_msi_shutdown(pci_dev);
	pci_msix_shutdown(pci_dev);
}

#ifdef CONFIG_PM_SLEEP

381 382 383
/*
 * Default "suspend" method for devices that have no driver provided suspend,
 * or not even a driver at all (second part).
384
 */
385
static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
386 387 388 389 390 391 392 393 394
{
	/*
	 * mark its power state as "unknown", since we don't know if
	 * e.g. the BIOS will change its device state when we suspend.
	 */
	if (pci_dev->current_state == PCI_D0)
		pci_dev->current_state = PCI_UNKNOWN;
}

395 396 397 398
/*
 * Default "resume" method for devices that have no driver provided resume,
 * or not even a driver at all (second part).
 */
399
static int pci_pm_reenable_device(struct pci_dev *pci_dev)
400 401 402
{
	int retval;

403 404 405 406 407 408 409 410 411 412 413 414 415
	/* if the device was enabled before suspend, reenable */
	retval = pci_reenable_device(pci_dev);
	/*
	 * if the device was busmaster before the suspend, make it busmaster
	 * again
	 */
	if (pci_dev->is_busmaster)
		pci_set_master(pci_dev);

	return retval;
}

static int pci_legacy_suspend(struct device *dev, pm_message_t state)
L
Linus Torvalds 已提交
416 417 418
{
	struct pci_dev * pci_dev = to_pci_dev(dev);
	struct pci_driver * drv = pci_dev->driver;
419 420

	pci_dev->state_saved = false;
L
Linus Torvalds 已提交
421

422
	if (drv && drv->suspend) {
423
		pci_power_t prev = pci_dev->current_state;
424
		int error;
425

426 427 428 429
		error = drv->suspend(pci_dev, state);
		suspend_report_result(drv->suspend, error);
		if (error)
			return error;
430

431
		if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
432 433 434 435 436
		    && pci_dev->current_state != PCI_UNKNOWN) {
			WARN_ONCE(pci_dev->current_state != prev,
				"PCI PM: Device state not saved by %pF\n",
				drv->suspend);
		}
437
	}
438 439 440

	pci_fixup_device(pci_fixup_suspend, pci_dev);

441
	return 0;
L
Linus Torvalds 已提交
442 443
}

444
static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
L
Linus Torvalds 已提交
445 446 447 448 449
{
	struct pci_dev * pci_dev = to_pci_dev(dev);
	struct pci_driver * drv = pci_dev->driver;

	if (drv && drv->suspend_late) {
450 451 452
		pci_power_t prev = pci_dev->current_state;
		int error;

453 454
		error = drv->suspend_late(pci_dev, state);
		suspend_report_result(drv->suspend_late, error);
455 456 457 458 459 460 461 462 463 464
		if (error)
			return error;

		if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
		    && pci_dev->current_state != PCI_UNKNOWN) {
			WARN_ONCE(pci_dev->current_state != prev,
				"PCI PM: Device state not saved by %pF\n",
				drv->suspend_late);
			return 0;
		}
L
Linus Torvalds 已提交
465
	}
466 467 468 469 470 471 472

	if (!pci_dev->state_saved)
		pci_save_state(pci_dev);

	pci_pm_set_unknown_state(pci_dev);

	return 0;
L
Linus Torvalds 已提交
473
}
L
Linus Torvalds 已提交
474

475 476 477 478 479
static int pci_legacy_resume_early(struct device *dev)
{
	struct pci_dev * pci_dev = to_pci_dev(dev);
	struct pci_driver * drv = pci_dev->driver;

480 481
	return drv && drv->resume_early ?
			drv->resume_early(pci_dev) : 0;
482 483
}

484
static int pci_legacy_resume(struct device *dev)
L
Linus Torvalds 已提交
485 486 487 488
{
	struct pci_dev * pci_dev = to_pci_dev(dev);
	struct pci_driver * drv = pci_dev->driver;

489 490
	pci_fixup_device(pci_fixup_resume, pci_dev);

491 492
	return drv && drv->resume ?
			drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
L
Linus Torvalds 已提交
493 494
}

495 496
/* Auxiliary functions used by the new power management framework */

497 498 499 500 501 502 503 504 505 506 507 508 509 510
/**
 * pci_restore_standard_config - restore standard config registers of PCI device
 * @pci_dev: PCI device to handle
 */
static int pci_restore_standard_config(struct pci_dev *pci_dev)
{
	pci_update_current_state(pci_dev, PCI_UNKNOWN);

	if (pci_dev->current_state != PCI_D0) {
		int error = pci_set_power_state(pci_dev, PCI_D0);
		if (error)
			return error;
	}

A
Alek Du 已提交
511
	return pci_restore_state(pci_dev);
512 513
}

514 515
static void pci_pm_default_resume_noirq(struct pci_dev *pci_dev)
{
516
	pci_restore_standard_config(pci_dev);
517
	pci_dev->state_saved = false;
518
	pci_fixup_device(pci_fixup_resume_early, pci_dev);
519 520
}

521
static void pci_pm_default_resume(struct pci_dev *pci_dev)
522
{
523 524
	pci_fixup_device(pci_fixup_resume, pci_dev);

525 526
	if (!pci_is_bridge(pci_dev))
		pci_enable_wake(pci_dev, PCI_D0, false);
527 528
}

529
static void pci_pm_default_suspend(struct pci_dev *pci_dev)
530
{
531
	/* Disable non-bridge devices without PM support */
532 533
	if (!pci_is_bridge(pci_dev))
		pci_disable_enabled_device(pci_dev);
534 535
}

536 537 538
static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
{
	struct pci_driver *drv = pci_dev->driver;
539
	bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
540
		|| drv->resume_early);
541 542 543 544 545 546 547 548 549

	/*
	 * Legacy PM support is used by default, so warn if the new framework is
	 * supported as well.  Drivers are supposed to support either the
	 * former, or the latter, but not both at the same time.
	 */
	WARN_ON(ret && drv->driver.pm);

	return ret;
550 551
}

552 553
/* New power management framework */

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
static int pci_pm_prepare(struct device *dev)
{
	struct device_driver *drv = dev->driver;
	int error = 0;

	if (drv && drv->pm && drv->pm->prepare)
		error = drv->pm->prepare(dev);

	return error;
}

static void pci_pm_complete(struct device *dev)
{
	struct device_driver *drv = dev->driver;

	if (drv && drv->pm && drv->pm->complete)
		drv->pm->complete(dev);
}

#ifdef CONFIG_SUSPEND

static int pci_pm_suspend(struct device *dev)
{
	struct pci_dev *pci_dev = to_pci_dev(dev);
578
	struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
579

580 581
	if (pci_has_legacy_pm_support(pci_dev))
		return pci_legacy_suspend(dev, PMSG_SUSPEND);
582

583 584
	pci_dev->state_saved = false;

585 586 587 588 589 590 591 592 593
	if (!pm) {
		pci_pm_default_suspend(pci_dev);
		goto Fixup;
	}

	if (pm->suspend) {
		pci_power_t prev = pci_dev->current_state;
		int error;

594 595
		error = pm->suspend(dev);
		suspend_report_result(pm->suspend, error);
596 597 598
		if (error)
			return error;

599
		if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
600 601 602 603 604
		    && pci_dev->current_state != PCI_UNKNOWN) {
			WARN_ONCE(pci_dev->current_state != prev,
				"PCI PM: State of device not saved by %pF\n",
				pm->suspend);
		}
605
	}
606

607 608 609 610
 Fixup:
	pci_fixup_device(pci_fixup_suspend, pci_dev);

	return 0;
611 612 613
}

static int pci_pm_suspend_noirq(struct device *dev)
G
Greg KH 已提交
614
{
615
	struct pci_dev *pci_dev = to_pci_dev(dev);
616
	struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
G
Greg KH 已提交
617

618 619 620
	if (pci_has_legacy_pm_support(pci_dev))
		return pci_legacy_suspend_late(dev, PMSG_SUSPEND);

621 622
	if (!pm) {
		pci_save_state(pci_dev);
623
		return 0;
624
	}
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

	if (pm->suspend_noirq) {
		pci_power_t prev = pci_dev->current_state;
		int error;

		error = pm->suspend_noirq(dev);
		suspend_report_result(pm->suspend_noirq, error);
		if (error)
			return error;

		if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
		    && pci_dev->current_state != PCI_UNKNOWN) {
			WARN_ONCE(pci_dev->current_state != prev,
				"PCI PM: State of device not saved by %pF\n",
				pm->suspend_noirq);
			return 0;
		}
642 643
	}

644 645 646 647 648
	if (!pci_dev->state_saved) {
		pci_save_state(pci_dev);
		if (!pci_is_bridge(pci_dev))
			pci_prepare_to_sleep(pci_dev);
	}
649

650 651 652
	pci_pm_set_unknown_state(pci_dev);

	return 0;
G
Greg KH 已提交
653
}
L
Linus Torvalds 已提交
654

655
static int pci_pm_resume_noirq(struct device *dev)
656 657 658
{
	struct pci_dev *pci_dev = to_pci_dev(dev);
	struct device_driver *drv = dev->driver;
659
	int error = 0;
660

661 662
	pci_pm_default_resume_noirq(pci_dev);

663
	if (pci_has_legacy_pm_support(pci_dev))
664
		return pci_legacy_resume_early(dev);
665

666 667
	if (drv && drv->pm && drv->pm->resume_noirq)
		error = drv->pm->resume_noirq(dev);
668 669 670 671

	return error;
}

672
static int pci_pm_resume(struct device *dev)
673
{
674
	struct pci_dev *pci_dev = to_pci_dev(dev);
675
	struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
676 677
	int error = 0;

678 679 680 681 682 683 684
	/*
	 * This is necessary for the suspend error path in which resume is
	 * called without restoring the standard config registers of the device.
	 */
	if (pci_dev->state_saved)
		pci_restore_standard_config(pci_dev);

685
	if (pci_has_legacy_pm_support(pci_dev))
686
		return pci_legacy_resume(dev);
687

688
	pci_pm_default_resume(pci_dev);
689

690 691 692 693 694 695
	if (pm) {
		if (pm->resume)
			error = pm->resume(dev);
	} else {
		pci_pm_reenable_device(pci_dev);
	}
696

697
	return 0;
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
}

#else /* !CONFIG_SUSPEND */

#define pci_pm_suspend		NULL
#define pci_pm_suspend_noirq	NULL
#define pci_pm_resume		NULL
#define pci_pm_resume_noirq	NULL

#endif /* !CONFIG_SUSPEND */

#ifdef CONFIG_HIBERNATION

static int pci_pm_freeze(struct device *dev)
{
	struct pci_dev *pci_dev = to_pci_dev(dev);
714
	struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
715

716 717
	if (pci_has_legacy_pm_support(pci_dev))
		return pci_legacy_suspend(dev, PMSG_FREEZE);
718

719 720
	pci_dev->state_saved = false;

721 722 723
	if (!pm) {
		pci_pm_default_suspend(pci_dev);
		return 0;
724 725
	}

726 727 728 729 730 731 732 733 734 735
	if (pm->freeze) {
		int error;

		error = pm->freeze(dev);
		suspend_report_result(pm->freeze, error);
		if (error)
			return error;
	}

	return 0;
736 737 738 739
}

static int pci_pm_freeze_noirq(struct device *dev)
{
740
	struct pci_dev *pci_dev = to_pci_dev(dev);
741
	struct device_driver *drv = dev->driver;
742

743 744 745
	if (pci_has_legacy_pm_support(pci_dev))
		return pci_legacy_suspend_late(dev, PMSG_FREEZE);

746
	if (drv && drv->pm && drv->pm->freeze_noirq) {
747 748
		int error;

749 750
		error = drv->pm->freeze_noirq(dev);
		suspend_report_result(drv->pm->freeze_noirq, error);
751 752
		if (error)
			return error;
753 754
	}

755 756
	if (!pci_dev->state_saved)
		pci_save_state(pci_dev);
757

758 759 760
	pci_pm_set_unknown_state(pci_dev);

	return 0;
761 762
}

763
static int pci_pm_thaw_noirq(struct device *dev)
764
{
765
	struct pci_dev *pci_dev = to_pci_dev(dev);
766 767 768
	struct device_driver *drv = dev->driver;
	int error = 0;

769
	if (pci_has_legacy_pm_support(pci_dev))
770
		return pci_legacy_resume_early(dev);
771

772
	pci_update_current_state(pci_dev, PCI_D0);
773

774 775
	if (drv && drv->pm && drv->pm->thaw_noirq)
		error = drv->pm->thaw_noirq(dev);
776 777 778 779

	return error;
}

780
static int pci_pm_thaw(struct device *dev)
781
{
782
	struct pci_dev *pci_dev = to_pci_dev(dev);
783
	struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
784 785
	int error = 0;

786
	if (pci_has_legacy_pm_support(pci_dev))
787
		return pci_legacy_resume(dev);
788

789 790 791 792 793 794
	if (pm) {
		if (pm->thaw)
			error = pm->thaw(dev);
	} else {
		pci_pm_reenable_device(pci_dev);
	}
795 796 797 798 799 800

	return error;
}

static int pci_pm_poweroff(struct device *dev)
{
801
	struct pci_dev *pci_dev = to_pci_dev(dev);
802
	struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
803

804 805
	if (pci_has_legacy_pm_support(pci_dev))
		return pci_legacy_suspend(dev, PMSG_HIBERNATE);
806

807 808
	pci_dev->state_saved = false;

809 810 811 812 813 814
	if (!pm) {
		pci_pm_default_suspend(pci_dev);
		goto Fixup;
	}

	if (pm->poweroff) {
815 816
		int error;

817 818
		error = pm->poweroff(dev);
		suspend_report_result(pm->poweroff, error);
819 820
		if (error)
			return error;
821 822
	}

823 824
 Fixup:
	pci_fixup_device(pci_fixup_suspend, pci_dev);
825

826
	return 0;
827 828 829 830
}

static int pci_pm_poweroff_noirq(struct device *dev)
{
831
	struct pci_dev *pci_dev = to_pci_dev(dev);
832
	struct device_driver *drv = dev->driver;
833

834 835 836
	if (pci_has_legacy_pm_support(to_pci_dev(dev)))
		return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);

837 838 839 840 841 842
	if (!drv || !drv->pm)
		return 0;

	if (drv->pm->poweroff_noirq) {
		int error;

843 844
		error = drv->pm->poweroff_noirq(dev);
		suspend_report_result(drv->pm->poweroff_noirq, error);
845 846
		if (error)
			return error;
847 848
	}

849 850 851 852
	if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
		pci_prepare_to_sleep(pci_dev);

	return 0;
853 854
}

855
static int pci_pm_restore_noirq(struct device *dev)
856 857 858
{
	struct pci_dev *pci_dev = to_pci_dev(dev);
	struct device_driver *drv = dev->driver;
859
	int error = 0;
860

861 862
	pci_pm_default_resume_noirq(pci_dev);

863
	if (pci_has_legacy_pm_support(pci_dev))
864
		return pci_legacy_resume_early(dev);
865

866 867
	if (drv && drv->pm && drv->pm->restore_noirq)
		error = drv->pm->restore_noirq(dev);
868 869 870 871

	return error;
}

872
static int pci_pm_restore(struct device *dev)
873 874
{
	struct pci_dev *pci_dev = to_pci_dev(dev);
875
	struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
876 877
	int error = 0;

878 879 880 881 882 883 884
	/*
	 * This is necessary for the hibernation error path in which restore is
	 * called without restoring the standard config registers of the device.
	 */
	if (pci_dev->state_saved)
		pci_restore_standard_config(pci_dev);

885
	if (pci_has_legacy_pm_support(pci_dev))
886
		return pci_legacy_resume(dev);
887

888
	pci_pm_default_resume(pci_dev);
889

890 891 892 893 894 895
	if (pm) {
		if (pm->restore)
			error = pm->restore(dev);
	} else {
		pci_pm_reenable_device(pci_dev);
	}
896 897

	return error;
G
Greg KH 已提交
898
}
L
Linus Torvalds 已提交
899

900 901 902 903 904 905 906 907 908 909 910 911 912
#else /* !CONFIG_HIBERNATION */

#define pci_pm_freeze		NULL
#define pci_pm_freeze_noirq	NULL
#define pci_pm_thaw		NULL
#define pci_pm_thaw_noirq	NULL
#define pci_pm_poweroff		NULL
#define pci_pm_poweroff_noirq	NULL
#define pci_pm_restore		NULL
#define pci_pm_restore_noirq	NULL

#endif /* !CONFIG_HIBERNATION */

913 914 915 916 917 918 919 920 921
struct dev_pm_ops pci_dev_pm_ops = {
	.prepare = pci_pm_prepare,
	.complete = pci_pm_complete,
	.suspend = pci_pm_suspend,
	.resume = pci_pm_resume,
	.freeze = pci_pm_freeze,
	.thaw = pci_pm_thaw,
	.poweroff = pci_pm_poweroff,
	.restore = pci_pm_restore,
922 923 924 925 926 927 928 929
	.suspend_noirq = pci_pm_suspend_noirq,
	.resume_noirq = pci_pm_resume_noirq,
	.freeze_noirq = pci_pm_freeze_noirq,
	.thaw_noirq = pci_pm_thaw_noirq,
	.poweroff_noirq = pci_pm_poweroff_noirq,
	.restore_noirq = pci_pm_restore_noirq,
};

930
#define PCI_PM_OPS_PTR	(&pci_dev_pm_ops)
931 932 933 934 935 936 937

#else /* !CONFIG_PM_SLEEP */

#define PCI_PM_OPS_PTR	NULL

#endif /* !CONFIG_PM_SLEEP */

L
Linus Torvalds 已提交
938
/**
939
 * __pci_register_driver - register a new pci driver
L
Linus Torvalds 已提交
940
 * @drv: the driver structure to register
941
 * @owner: owner module of drv
942
 * @mod_name: module name string
L
Linus Torvalds 已提交
943 944 945
 * 
 * Adds the driver structure to the list of registered drivers.
 * Returns a negative value on error, otherwise 0. 
946
 * If no error occurred, the driver remains registered even if 
L
Linus Torvalds 已提交
947 948
 * no device was claimed during registration.
 */
949 950
int __pci_register_driver(struct pci_driver *drv, struct module *owner,
			  const char *mod_name)
L
Linus Torvalds 已提交
951 952 953 954 955 956
{
	int error;

	/* initialize common driver fields */
	drv->driver.name = drv->name;
	drv->driver.bus = &pci_bus_type;
957
	drv->driver.owner = owner;
958
	drv->driver.mod_name = mod_name;
A
Alan Cox 已提交
959

960 961
	spin_lock_init(&drv->dynids.lock);
	INIT_LIST_HEAD(&drv->dynids.list);
L
Linus Torvalds 已提交
962 963 964

	/* register with core */
	error = driver_register(&drv->driver);
965
	if (error)
C
Chris Wright 已提交
966
		goto out;
L
Linus Torvalds 已提交
967

968 969
	error = pci_create_newid_file(drv);
	if (error)
C
Chris Wright 已提交
970
		goto out_newid;
L
Linus Torvalds 已提交
971

C
Chris Wright 已提交
972 973 974 975
	error = pci_create_removeid_file(drv);
	if (error)
		goto out_removeid;
out:
L
Linus Torvalds 已提交
976
	return error;
C
Chris Wright 已提交
977 978 979 980 981 982

out_removeid:
	pci_remove_newid_file(drv);
out_newid:
	driver_unregister(&drv->driver);
	goto out;
L
Linus Torvalds 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
}

/**
 * pci_unregister_driver - unregister a pci driver
 * @drv: the driver structure to unregister
 * 
 * Deletes the driver structure from the list of registered PCI drivers,
 * gives it a chance to clean up by calling its remove() function for
 * each device it was responsible for, and marks those devices as
 * driverless.
 */

void
pci_unregister_driver(struct pci_driver *drv)
{
C
Chris Wright 已提交
998
	pci_remove_removeid_file(drv);
999
	pci_remove_newid_file(drv);
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	driver_unregister(&drv->driver);
	pci_free_dynids(drv);
}

static struct pci_driver pci_compat_driver = {
	.name = "compat"
};

/**
 * pci_dev_driver - get the pci_driver of a device
 * @dev: the device to query
 *
 * Returns the appropriate pci_driver structure or %NULL if there is no 
 * registered driver for the device.
 */
struct pci_driver *
pci_dev_driver(const struct pci_dev *dev)
{
	if (dev->driver)
		return dev->driver;
	else {
		int i;
		for(i=0; i<=PCI_ROM_RESOURCE; i++)
			if (dev->resource[i].flags & IORESOURCE_BUSY)
				return &pci_compat_driver;
	}
	return NULL;
}

/**
 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
 * @dev: the PCI device structure to match against
R
Randy Dunlap 已提交
1032
 * @drv: the device driver to search for matching PCI device id structures
L
Linus Torvalds 已提交
1033 1034
 * 
 * Used by a driver to check whether a PCI device present in the
R
Randy Dunlap 已提交
1035
 * system is in its list of supported devices. Returns the matching
L
Linus Torvalds 已提交
1036 1037
 * pci_device_id structure or %NULL if there is no match.
 */
1038
static int pci_bus_match(struct device *dev, struct device_driver *drv)
L
Linus Torvalds 已提交
1039
{
1040 1041
	struct pci_dev *pci_dev = to_pci_dev(dev);
	struct pci_driver *pci_drv = to_pci_driver(drv);
L
Linus Torvalds 已提交
1042 1043
	const struct pci_device_id *found_id;

1044
	found_id = pci_match_device(pci_drv, pci_dev);
L
Linus Torvalds 已提交
1045 1046 1047
	if (found_id)
		return 1;

1048
	return 0;
L
Linus Torvalds 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
}

/**
 * pci_dev_get - increments the reference count of the pci device structure
 * @dev: the device being referenced
 *
 * Each live reference to a device should be refcounted.
 *
 * Drivers for PCI devices should normally record such references in
 * their probe() methods, when they bind to a device, and release
 * them by calling pci_dev_put(), in their disconnect() methods.
 *
 * A pointer to the device with the incremented reference counter is returned.
 */
struct pci_dev *pci_dev_get(struct pci_dev *dev)
{
	if (dev)
		get_device(&dev->dev);
	return dev;
}

/**
 * pci_dev_put - release a use of the pci device structure
 * @dev: device that's been disconnected
 *
 * Must be called when a user of a device is finished with it.  When the last
 * user of the device calls this function, the memory of the device is freed.
 */
void pci_dev_put(struct pci_dev *dev)
{
	if (dev)
		put_device(&dev->dev);
}

#ifndef CONFIG_HOTPLUG
1084
int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
L
Linus Torvalds 已提交
1085 1086 1087 1088 1089 1090 1091 1092
{
	return -ENODEV;
}
#endif

struct bus_type pci_bus_type = {
	.name		= "pci",
	.match		= pci_bus_match,
1093
	.uevent		= pci_uevent,
1094 1095
	.probe		= pci_device_probe,
	.remove		= pci_device_remove,
L
Linus Torvalds 已提交
1096
	.shutdown	= pci_device_shutdown,
L
Linus Torvalds 已提交
1097
	.dev_attrs	= pci_dev_attrs,
A
Alex Chiang 已提交
1098
	.bus_attrs	= pci_bus_attrs,
1099
	.pm		= PCI_PM_OPS_PTR,
L
Linus Torvalds 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108
};

static int __init pci_driver_init(void)
{
	return bus_register(&pci_bus_type);
}

postcore_initcall(pci_driver_init);

1109
EXPORT_SYMBOL(pci_match_id);
1110
EXPORT_SYMBOL(__pci_register_driver);
L
Linus Torvalds 已提交
1111 1112 1113 1114 1115
EXPORT_SYMBOL(pci_unregister_driver);
EXPORT_SYMBOL(pci_dev_driver);
EXPORT_SYMBOL(pci_bus_type);
EXPORT_SYMBOL(pci_dev_get);
EXPORT_SYMBOL(pci_dev_put);