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 v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
16 pci_add_cap(struct pci_dev *d, unsigned int addr, unsigned int id, unsigned int type)
18 struct pci_cap *cap = pci_malloc(d->access, sizeof(*cap));
21 d->last_cap->next = cap;
29 d->access->debug("%04x:%02x:%02x.%d: Found capability %04x of type %d at %04x\n",
30 d->domain, d->bus, d->dev, d->func, id, type, addr);
34 pci_scan_trad_caps(struct pci_dev *d)
36 word status = pci_read_word(d, PCI_STATUS);
40 if (!(status & PCI_STATUS_CAP_LIST))
43 memset(been_there, 0, 256);
44 where = pci_read_byte(d, PCI_CAPABILITY_LIST) & ~3;
47 byte id = pci_read_byte(d, where + PCI_CAP_LIST_ID);
48 byte next = pci_read_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
49 if (been_there[where]++)
53 pci_add_cap(d, where, id, PCI_CAP_NORMAL);
59 pci_scan_ext_caps(struct pci_dev *d)
61 byte been_there[0x1000];
64 if (!pci_find_cap(d, PCI_CAP_ID_EXP, PCI_CAP_NORMAL))
67 memset(been_there, 0, 0x1000);
73 header = pci_read_long(d, where);
74 if (!header || header == 0xffffffff)
77 if (been_there[where]++)
79 pci_add_cap(d, where, id, PCI_CAP_EXTENDED);
80 where = (header >> 20) & ~3;
86 pci_scan_caps(struct pci_dev *d, unsigned int want_fields)
88 if (want_fields & PCI_FILL_EXT_CAPS)
89 want_fields |= PCI_FILL_CAPS;
91 if (want_fill(d, want_fields, PCI_FILL_CAPS))
92 pci_scan_trad_caps(d);
93 if (want_fill(d, want_fields, PCI_FILL_EXT_CAPS))
98 pci_free_caps(struct pci_dev *d)
102 while (cap = d->first_cap)
104 d->first_cap = cap->next;
110 pci_find_cap(struct pci_dev *d, unsigned int id, unsigned int type)
112 return pci_find_cap_nr(d, id, type, NULL);
116 * Finds a particular capability of a device
118 * To select one capability if there are more than one with the same id, you
119 * can provide a pointer to an unsigned int that contains the index which you
120 * want as cap_number. If you don't care and are fine with the first one you
121 * can supply NULL. The cap_number will be replaced by the actual number
122 * of capabilities with that id.
125 pci_find_cap_nr(struct pci_dev *d, unsigned int id, unsigned int type,
126 unsigned int *cap_number)
129 struct pci_cap *found = NULL;
130 unsigned int target = (cap_number ? *cap_number : 0);
131 unsigned int index = 0;
133 pci_fill_info_v38(d, ((type == PCI_CAP_NORMAL) ? PCI_FILL_CAPS : PCI_FILL_EXT_CAPS));
135 for (c=d->first_cap; c; c=c->next)
137 if (c->type == type && c->id == id)