]> git.ipfire.org Git - thirdparty/pciutils.git/blob - lib/caps.c
Library: The list of capabilities is ordered properly
[thirdparty/pciutils.git] / lib / caps.c
1 /*
2 * The PCI Library -- Capabilities
3 *
4 * Copyright (c) 2008 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #include <string.h>
10
11 #include "internal.h"
12
13 static void
14 pci_add_cap(struct pci_dev *d, unsigned int addr, unsigned int id, unsigned int type)
15 {
16 struct pci_cap *cap = pci_malloc(d->access, sizeof(*cap));
17
18 if (d->last_cap)
19 d->last_cap->next = cap;
20 else
21 d->first_cap = cap;
22 d->last_cap = cap;
23 cap->addr = addr;
24 cap->id = id;
25 cap->type = type;
26 d->access->debug("%04x:%02x:%02x.%d: Found capability %04x of type %d at %04x\n",
27 d->domain, d->bus, d->dev, d->func, id, type, addr);
28 }
29
30 static void
31 pci_scan_trad_caps(struct pci_dev *d)
32 {
33 word status = pci_read_word(d, PCI_STATUS);
34 byte been_there[256];
35 int where;
36
37 if (!(status & PCI_STATUS_CAP_LIST))
38 return;
39
40 memset(been_there, 0, 256);
41 where = pci_read_byte(d, PCI_CAPABILITY_LIST) & ~3;
42 while (where)
43 {
44 byte id = pci_read_byte(d, where + PCI_CAP_LIST_ID);
45 byte next = pci_read_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
46 if (been_there[where]++)
47 break;
48 if (id == 0xff)
49 break;
50 pci_add_cap(d, where, id, PCI_CAP_NORMAL);
51 where = next;
52 }
53 }
54
55 static void
56 pci_scan_ext_caps(struct pci_dev *d)
57 {
58 byte been_there[0x1000];
59 int where = 0x100;
60
61 if (!pci_find_cap(d, PCI_CAP_ID_EXP, PCI_CAP_NORMAL))
62 return;
63
64 memset(been_there, 0, 0x1000);
65 do
66 {
67 u32 header;
68 int id;
69
70 header = pci_read_long(d, where);
71 if (!header || header == 0xffffffff)
72 break;
73 id = header & 0xffff;
74 if (been_there[where]++)
75 break;
76 pci_add_cap(d, where, id, PCI_CAP_EXTENDED);
77 where = (header >> 20) & ~3;
78 }
79 while (where);
80 }
81
82 unsigned int
83 pci_scan_caps(struct pci_dev *d, unsigned int want_fields)
84 {
85 if ((want_fields & PCI_FILL_EXT_CAPS) && !(d->known_fields & PCI_FILL_CAPS))
86 want_fields |= PCI_FILL_CAPS;
87
88 if (want_fields & PCI_FILL_CAPS)
89 pci_scan_trad_caps(d);
90 if (want_fields & PCI_FILL_EXT_CAPS)
91 pci_scan_ext_caps(d);
92 return want_fields;
93 }
94
95 void
96 pci_free_caps(struct pci_dev *d)
97 {
98 struct pci_cap *cap;
99
100 while (cap = d->first_cap)
101 {
102 d->first_cap = cap->next;
103 pci_mfree(cap);
104 }
105 }
106
107 struct pci_cap *
108 pci_find_cap(struct pci_dev *d, unsigned int id, unsigned int type)
109 {
110 return pci_find_cap_nr(d, id, type, NULL);
111 }
112
113 /**
114 * Finds a particular capability of a device
115 *
116 * To select one capability if there are more than one with the same id, you
117 * can provide a pointer to an unsigned int that contains the index which you
118 * want as cap_number. If you don't care and are fine with the first one you
119 * can supply NULL. The cap_number will be replaced by the acutal number
120 * of capablities with that id.
121 */
122 struct pci_cap *
123 pci_find_cap_nr(struct pci_dev *d, unsigned int id, unsigned int type,
124 unsigned int *cap_number)
125 {
126 struct pci_cap *c;
127 struct pci_cap *found = NULL;
128 unsigned int target = (cap_number ? *cap_number : 0);
129 unsigned int index = 0;
130
131 pci_fill_info_v35(d, ((type == PCI_CAP_NORMAL) ? PCI_FILL_CAPS : PCI_FILL_EXT_CAPS));
132
133 for (c=d->first_cap; c; c=c->next)
134 {
135 if (c->type == type && c->id == id)
136 {
137 if (target == index)
138 found = c;
139 index++;
140 }
141 }
142
143 if (cap_number)
144 *cap_number = index;
145 return found;
146 }