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->path.type == DEVICE_PATH_PCI) &&
38 (dev->bus->secondary == bus) &&
39 (dev->path.u.pci.devfn == devfn)) {
47 /** Find a device of a given vendor and type
48 * @param vendor Vendor ID (e.g. 0x8086 for Intel)
49 * @param device Device ID
50 * @param from Pointer to the device structure, used as a starting point
51 * in the linked list of all_devices, which can be 0 to start at the
52 * head of the list (i.e. all_devices)
53 * @return Pointer to the device struct
55 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
61 while (from && (from->vendor != vendor || from->device != device)) {
67 /** Find a device of a given class
68 * @param class Class of the device
69 * @param from Pointer to the device structure, used as a starting point
70 * in the linked list of all_devices, which can be 0 to start at the
71 * head of the list (i.e. all_devices)
72 * @return Pointer to the device struct
74 struct device *dev_find_class(unsigned int class, struct device *from)
80 while (from && (from->class & 0xffffff00) != class)
86 const char *dev_path(device_t dev)
88 static char buffer[DEVICE_PATH_MAX];
91 memcpy(buffer, "<null>", 7);
94 switch(dev->path.type) {
95 case DEVICE_PATH_ROOT:
96 memcpy(buffer, "Root Device", 12);
99 sprintf(buffer, "PCI: %02x:%02x.%01x",
101 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
103 case DEVICE_PATH_PNP:
104 sprintf(buffer, "PNP: %04x.%01x",
105 dev->path.u.pnp.port, dev->path.u.pnp.device);
107 case DEVICE_PATH_I2C:
108 sprintf(buffer, "I2C: %02x",
109 dev->path.u.i2c.device);
112 printk_err("Unknown device path type: %d\n", dev->path.type);
119 int path_eq(struct device_path *path1, struct device_path *path2)
122 if (path1->type == path2->type) {
123 switch(path1->type) {
124 case DEVICE_PATH_NONE:
126 case DEVICE_PATH_ROOT:
129 case DEVICE_PATH_PCI:
130 equal = (path1->u.pci.bus == path2->u.pci.bus) &&
131 (path1->u.pci.devfn == path2->u.pci.devfn);
133 case DEVICE_PATH_PNP:
134 equal = (path1->u.pnp.port == path2->u.pnp.port) &&
135 (path1->u.pnp.device == path2->u.pnp.device);
137 case DEVICE_PATH_I2C:
138 equal = (path1->u.i2c.device == path2->u.i2c.device);
141 printk_err("Uknown device type: %d\n", path1->type);
149 * See if we have unused but allocated resource structures.
150 * If so remove the allocation.
151 * @param dev The device to find the resource on
153 void compact_resources(device_t dev)
155 struct resource *resource;
157 /* Move all of the free resources to the end */
158 for(i = 0; i < dev->resources;) {
159 resource = &dev->resource[i];
160 if (!resource->flags) {
161 memmove(resource, resource + 1, dev->resources - i);
163 memset(&dev->resource[dev->resources], 0, sizeof(*resource));
171 * See if a resource structure already exists for a given index and if
173 * @param dev The device to find the resource on
174 * @param index The index of the resource on the device.
176 struct resource *get_resource(device_t dev, unsigned index)
178 struct resource *resource;
181 /* First move all of the free resources to the end */
182 compact_resources(dev);
184 /* See if there is a resource with the appropriate index */
186 for(i = 0; i < dev->resources; i++) {
187 if (dev->resource[i].index == index) {
188 resource = &dev->resource[i];
193 if (dev->resources == MAX_RESOURCES) {
194 die("MAX_RESOURCES exceeded.");
196 resource = &dev->resource[dev->resources];
197 memset(resource, 0, sizeof(*resource));
200 /* Initialize the resource values */
201 if (!(resource->flags & IORESOURCE_FIXED)) {
207 resource->index = index;