summaryrefslogtreecommitdiffstats
path: root/drivers/pci/bus.c
blob: ac15623307885b58353886aec397f4c39c3bcff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <common.h>
#include <init.h>
#include <driver.h>
#include <linux/pci.h>

/**
 * pci_match_one_device - Tell if a PCI device structure has a matching
 *                        PCI device id structure
 * @id: single PCI device id structure to match
 * @dev: the PCI device structure to match against
 *
 * Returns the matching pci_device_id structure or %NULL if there is no match.
 */
static inline const struct pci_device_id *
pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
{
	if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
	    (id->device == PCI_ANY_ID || id->device == dev->device) &&
	    (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
	    (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
	    !((id->class ^ dev->class) & id->class_mask))
		return id;
	return NULL;
}

static int pci_match(struct device_d *dev, struct driver_d *drv)
{
	struct pci_dev *pdev = to_pci_dev(dev);
	struct pci_driver *pdrv = to_pci_driver(drv);
	const struct pci_device_id *id;

	for (id = pdrv->id_table; id->vendor; id++)
		if (pci_match_one_device(id, pdev)) {
			pdev->id = id;
			return 0;
		}

	return -1;
}

static int pci_probe(struct device_d *dev)
{
	struct pci_dev *pdev = to_pci_dev(dev);
	struct pci_driver *pdrv = to_pci_driver(dev->driver);

	return pdrv->probe(pdev, pdev->id);
}

static void pci_remove(struct device_d *dev)
{
	struct pci_dev *pdev = to_pci_dev(dev);
	struct pci_driver *pdrv = to_pci_driver(dev->driver);

	if (pdrv->remove)
		pdrv->remove(pdev);
}

struct bus_type pci_bus = {
	.name = "pci",
	.match = pci_match,
	.probe = pci_probe,
	.remove = pci_remove,
};

static int pci_bus_init(void)
{
	return bus_register(&pci_bus);
}
pure_initcall(pci_bus_init);

int pci_register_driver(struct pci_driver *pdrv)
{
	struct driver_d *drv = &pdrv->driver;

	if (!pdrv->id_table)
		return -EIO;

	drv->name = pdrv->name;
	drv->bus = &pci_bus;

	return register_driver(drv);
}

int pci_register_device(struct pci_dev *pdev)
{
	char str[6];
	struct device_d *dev = &pdev->dev;
	int ret;

	dev_set_name(dev, "pci-%04x:%04x.", pdev->vendor, pdev->device);
	dev->bus = &pci_bus;
	dev->id = DEVICE_ID_DYNAMIC;

	ret = register_device(dev);

	if (ret)
		return ret;

	sprintf(str, "%02x", pdev->devfn);
	dev_add_param_fixed(dev, "devfn", str);
	sprintf(str, "%04x", (pdev->class >> 8) & 0xffff);
	dev_add_param_fixed(dev, "class", str);
	sprintf(str, "%04x", pdev->vendor);
	dev_add_param_fixed(dev, "vendor", str);
	sprintf(str, "%04x", pdev->device);
	dev_add_param_fixed(dev, "device", str);
	sprintf(str, "%04x", pdev->revision);
	dev_add_param_fixed(dev, "revision", str);

	return 0;
}