1 #include <console/console.h>
2 #include <device/device.h>
3 #include <device/path.h>
4 #include <device/pci.h>
9 * @brief See if a device structure already exists and if not allocate it
11 * @param bus The bus to find the device on
12 * @param path The relative path from the bus to the appropriate device
13 * @return pointer to a device structure for the device on bus at path
15 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
18 for (child = parent->children; child; child = child->sibling) {
19 if (path_eq(path, &child->path)) {
23 return alloc_dev(parent, path);
27 * Given a bus and a devfn number, find the device structure
28 * @param bus The bus number
29 * @param devfn a device/function number
30 * @return pointer to the device structure
32 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
34 struct device *dev, *result;
37 for (dev = all_devices; dev; dev = dev->next) {
38 if ((dev->path.type == DEVICE_PATH_PCI) &&
39 (dev->bus->secondary == bus) &&
40 (dev->path.u.pci.devfn == devfn)) {
48 /** Find a device of a given vendor and type
49 * @param vendor Vendor ID (e.g. 0x8086 for Intel)
50 * @param device Device ID
51 * @param from Pointer to the device structure, used as a starting point
52 * in the linked list of all_devices, which can be 0 to start at the
53 * head of the list (i.e. all_devices)
54 * @return Pointer to the device struct
56 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
62 while (from && (from->vendor != vendor || from->device != device)) {
68 /** Find a device of a given class
69 * @param class Class of the device
70 * @param from Pointer to the device structure, used as a starting point
71 * in the linked list of all_devices, which can be 0 to start at the
72 * head of the list (i.e. all_devices)
73 * @return Pointer to the device struct
75 struct device *dev_find_class(unsigned int class, struct device *from)
81 while (from && (from->class & 0xffffff00) != class)
87 const char *dev_path(device_t dev)
89 static char buffer[DEVICE_PATH_MAX];
92 memcpy(buffer, "<null>", 7);
95 switch(dev->path.type) {
96 case DEVICE_PATH_ROOT:
97 memcpy(buffer, "Root Device", 12);
100 sprintf(buffer, "PCI: %02x:%02x.%01x",
102 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
104 case DEVICE_PATH_PNP:
105 sprintf(buffer, "PNP: %04x.%01x",
106 dev->path.u.pnp.port, dev->path.u.pnp.device);
108 case DEVICE_PATH_I2C:
109 sprintf(buffer, "I2C: %02x",
110 dev->path.u.i2c.device);
113 printk_err("Unknown device path type: %d\n", dev->path.type);
120 int path_eq(struct device_path *path1, struct device_path *path2)
123 if (path1->type == path2->type) {
124 switch(path1->type) {
125 case DEVICE_PATH_NONE:
127 case DEVICE_PATH_ROOT:
130 case DEVICE_PATH_PCI:
131 equal = (path1->u.pci.bus == path2->u.pci.bus) &&
132 (path1->u.pci.devfn == path2->u.pci.devfn);
134 case DEVICE_PATH_PNP:
135 equal = (path1->u.pnp.port == path2->u.pnp.port) &&
136 (path1->u.pnp.device == path2->u.pnp.device);
138 case DEVICE_PATH_I2C:
139 equal = (path1->u.i2c.device == path2->u.i2c.device);
142 printk_err("Uknown device type: %d\n", path1->type);
150 * See if we have unused but allocated resource structures.
151 * If so remove the allocation.
152 * @param dev The device to find the resource on
154 void compact_resources(device_t dev)
156 struct resource *resource;
158 /* Move all of the free resources to the end */
159 for(i = 0; i < dev->resources;) {
160 resource = &dev->resource[i];
161 if (!resource->flags) {
162 memmove(resource, resource + 1, dev->resources - i);
164 memset(&dev->resource[dev->resources], 0, sizeof(*resource));
172 * See if a resource structure already exists for a given index and if
174 * @param dev The device to find the resource on
175 * @param index The index of the resource on the device.
177 struct resource *get_resource(device_t dev, unsigned index)
179 struct resource *resource;
182 /* First move all of the free resources to the end */
183 compact_resources(dev);
185 /* See if there is a resource with the appropriate index */
187 for(i = 0; i < dev->resources; i++) {
188 if (dev->resource[i].index == index) {
189 resource = &dev->resource[i];
194 if (dev->resources == MAX_RESOURCES) {
195 die("MAX_RESOURCES exceeded.");
197 resource = &dev->resource[dev->resources];
198 memset(resource, 0, sizeof(*resource));
201 /* Initialize the resource values */
202 if (!(resource->flags & IORESOURCE_FIXED)) {
208 resource->index = index;