2 * The PCI Library -- Capabilities
4 * Copyright (c) 2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
14 pci_add_cap(struct pci_dev *d, unsigned int addr, unsigned int id, unsigned int type)
16 struct pci_cap *cap = pci_malloc(d->access, sizeof(*cap));
19 d->last_cap->next = cap;
27 d->access->debug("%04x:%02x:%02x.%d: Found capability %04x of type %d at %04x\n",
28 d->domain, d->bus, d->dev, d->func, id, type, addr);
32 pci_scan_trad_caps(struct pci_dev *d)
34 word status = pci_read_word(d, PCI_STATUS);
38 if (!(status & PCI_STATUS_CAP_LIST))
41 memset(been_there, 0, 256);
42 where = pci_read_byte(d, PCI_CAPABILITY_LIST) & ~3;
45 byte id = pci_read_byte(d, where + PCI_CAP_LIST_ID);
46 byte next = pci_read_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
47 if (been_there[where]++)
51 pci_add_cap(d, where, id, PCI_CAP_NORMAL);
57 pci_scan_ext_caps(struct pci_dev *d)
59 byte been_there[0x1000];
62 if (!pci_find_cap(d, PCI_CAP_ID_EXP, PCI_CAP_NORMAL))
65 memset(been_there, 0, 0x1000);
71 header = pci_read_long(d, where);
72 if (!header || header == 0xffffffff)
75 if (been_there[where]++)
77 pci_add_cap(d, where, id, PCI_CAP_EXTENDED);
78 where = (header >> 20) & ~3;
84 pci_scan_caps(struct pci_dev *d, unsigned int want_fields)
86 if ((want_fields & PCI_FILL_EXT_CAPS) && !(d->known_fields & PCI_FILL_CAPS))
87 want_fields |= PCI_FILL_CAPS;
89 if (want_fields & PCI_FILL_CAPS)
90 pci_scan_trad_caps(d);
91 if (want_fields & PCI_FILL_EXT_CAPS)
97 pci_free_caps(struct pci_dev *d)
101 while (cap = d->first_cap)
103 d->first_cap = cap->next;
109 pci_find_cap(struct pci_dev *d, unsigned int id, unsigned int type)
111 return pci_find_cap_nr(d, id, type, NULL);
115 * Finds a particular capability of a device
117 * To select one capability if there are more than one with the same id, you
118 * can provide a pointer to an unsigned int that contains the index which you
119 * want as cap_number. If you don't care and are fine with the first one you
120 * can supply NULL. The cap_number will be replaced by the acutal number
121 * of capablities with that id.
124 pci_find_cap_nr(struct pci_dev *d, unsigned int id, unsigned int type,
125 unsigned int *cap_number)
128 struct pci_cap *found = NULL;
129 unsigned int target = (cap_number ? *cap_number : 0);
130 unsigned int index = 0;
132 pci_fill_info_v35(d, ((type == PCI_CAP_NORMAL) ? PCI_FILL_CAPS : PCI_FILL_EXT_CAPS));
134 for (c=d->first_cap; c; c=c->next)
136 if (c->type == type && c->id == id)