1 #include <console/console.h>
2 #include <device/device.h>
3 #include <device/path.h>
4 #include <device/pci.h>
9 * See if a device structure already exists and if not allocate it
10 * @param bus The bus to find the device on
11 * @param path The relative path from the bus to the appropriate device
12 * @return pointer a device structure for the device on bus at path
14 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
17 for(child = parent->children; child; child = child->sibling) {
18 if (path_eq(path, &child->path)) {
22 return alloc_dev(parent, path);
26 * Given a bus and a devfn number, find the device structure
27 * @param bus The bus number
28 * @param devfn a device/function number
29 * @return pointer to the device structure
31 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
33 struct device *dev, *result;
36 for (dev = all_devices; dev; dev = dev->next) {
37 if ((dev->bus->secondary == bus) &&
38 (dev->path.u.pci.devfn == devfn)) {
46 /** Find a device of a given vendor and type
47 * @param vendor Vendor ID (e.g. 0x8086 for Intel)
48 * @param device Device ID
49 * @param from Pointer to the device structure, used as a starting point
50 * in the linked list of all_devices, which can be 0 to start at the
51 * head of the list (i.e. all_devices)
52 * @return Pointer to the device struct
54 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
60 while (from && (from->vendor != vendor || from->device != device))
65 /** Find a device of a given class
66 * @param class Class of the device
67 * @param from Pointer to the device structure, used as a starting point
68 * in the linked list of all_devices, which can be 0 to start at the
69 * head of the list (i.e. all_devices)
70 * @return Pointer to the device struct
72 struct device *dev_find_class(unsigned int class, struct device *from)
78 while (from && from->class != class)
84 const char *dev_path(device_t dev)
86 static char buffer[DEVICE_PATH_MAX];
89 memcpy(buffer, "<null>", 7);
92 switch(dev->path.type) {
93 case DEVICE_PATH_ROOT:
94 memcpy(buffer, "Root Device", 12);
97 sprintf(buffer, "PCI: %02x:%02x.%01x",
99 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
101 case DEVICE_PATH_PNP:
102 sprintf(buffer, "PNP: %04x.%01x",
103 dev->path.u.pnp.port, dev->path.u.pnp.device);
105 case DEVICE_PATH_I2C:
106 sprintf(buffer, "I2C: %02x",
107 dev->path.u.i2c.device);
110 printk_err("Unknown device path type: %d\n", dev->path.type);
117 int path_eq(struct device_path *path1, struct device_path *path2)
120 if (path1->type == path2->type) {
121 switch(path1->type) {
122 case DEVICE_PATH_NONE:
124 case DEVICE_PATH_ROOT:
127 case DEVICE_PATH_PCI:
128 equal = (path1->u.pci.bus == path2->u.pci.bus) &&
129 (path1->u.pci.devfn == path2->u.pci.devfn);
131 case DEVICE_PATH_PNP:
132 equal = (path1->u.pnp.port == path2->u.pnp.port) &&
133 (path1->u.pnp.device == path2->u.pnp.device);
135 case DEVICE_PATH_I2C:
136 equal = (path1->u.i2c.device == path2->u.i2c.device);
139 printk_err("Uknown device type: %d\n", path1->type);