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)
35 for (dev = all_devices; dev; dev = dev->next) {
36 if ((dev->bus->secondary == bus) &&
37 (dev->path.u.pci.devfn == devfn)) {
44 /** Find a device of a given vendor and type
45 * @param vendor Vendor ID (e.g. 0x8086 for Intel)
46 * @param device Device ID
47 * @param from Pointer to the device structure, used as a starting point
48 * in the linked list of all_devices, which can be 0 to start at the
49 * head of the list (i.e. all_devices)
50 * @return Pointer to the device struct
52 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
58 while (from && (from->vendor != vendor || from->device != device))
63 /** Find a device of a given class
64 * @param class Class of the device
65 * @param from Pointer to the device structure, used as a starting point
66 * in the linked list of all_devices, which can be 0 to start at the
67 * head of the list (i.e. all_devices)
68 * @return Pointer to the device struct
70 struct device *dev_find_class(unsigned int class, struct device *from)
76 while (from && from->class != class)
82 const char *dev_path(device_t dev)
84 static char buffer[DEVICE_PATH_MAX];
87 memcpy(buffer, "<null>", 7);
90 switch(dev->path.type) {
92 sprintf(buffer, "PCI: %02x:%02x.%01x",
94 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
97 sprintf(buffer, "PNP: %04x.%01x",
98 dev->path.u.pnp.port, dev->path.u.pnp.device);
100 case DEVICE_PATH_I2C:
101 sprintf(buffer, "I2C: %02x",
102 dev->path.u.i2c.device);
105 printk_err("Unknown device path type: %d\n", dev->path.type);
112 int path_eq(struct device_path *path1, struct device_path *path2)
115 if (path1->type == path2->type) {
116 switch(path1->type) {
117 case DEVICE_PATH_NONE:
119 case DEVICE_PATH_PCI:
120 equal = path1->u.pci.devfn == path2->u.pci.devfn;
122 case DEVICE_PATH_PNP:
123 equal = (path1->u.pnp.port == path2->u.pnp.port) &&
124 (path1->u.pnp.device == path2->u.pnp.device);
126 case DEVICE_PATH_I2C:
127 equal = (path1->u.i2c.device == path2->u.i2c.device);
130 printk_err("Uknown device type: %d\n", path1->type);