]> git.ipfire.org Git - thirdparty/pciutils.git/blame - lib/caps.c
Fixed memory initialization bug in previous commit
[thirdparty/pciutils.git] / lib / caps.c
CommitLineData
6d5e39ac
MM
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
13static void
14pci_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
9c48ed23
MM
18 if (d->last_cap)
19 d->last_cap->next = cap;
20 else
21 d->first_cap = cap;
22 d->last_cap = cap;
ae94d494 23 cap->next = NULL;
6d5e39ac
MM
24 cap->addr = addr;
25 cap->id = id;
26 cap->type = type;
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);
29}
30
31static void
32pci_scan_trad_caps(struct pci_dev *d)
33{
34 word status = pci_read_word(d, PCI_STATUS);
35 byte been_there[256];
36 int where;
37
38 if (!(status & PCI_STATUS_CAP_LIST))
39 return;
40
41 memset(been_there, 0, 256);
42 where = pci_read_byte(d, PCI_CAPABILITY_LIST) & ~3;
43 while (where)
44 {
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]++)
48 break;
49 if (id == 0xff)
50 break;
51 pci_add_cap(d, where, id, PCI_CAP_NORMAL);
52 where = next;
53 }
54}
55
56static void
57pci_scan_ext_caps(struct pci_dev *d)
58{
59 byte been_there[0x1000];
60 int where = 0x100;
61
62 if (!pci_find_cap(d, PCI_CAP_ID_EXP, PCI_CAP_NORMAL))
63 return;
64
65 memset(been_there, 0, 0x1000);
66 do
67 {
68 u32 header;
69 int id;
70
71 header = pci_read_long(d, where);
72 if (!header || header == 0xffffffff)
73 break;
74 id = header & 0xffff;
75 if (been_there[where]++)
76 break;
77 pci_add_cap(d, where, id, PCI_CAP_EXTENDED);
d61c4772 78 where = (header >> 20) & ~3;
6d5e39ac
MM
79 }
80 while (where);
81}
82
83unsigned int
84pci_scan_caps(struct pci_dev *d, unsigned int want_fields)
85{
86 if ((want_fields & PCI_FILL_EXT_CAPS) && !(d->known_fields & PCI_FILL_CAPS))
87 want_fields |= PCI_FILL_CAPS;
88
89 if (want_fields & PCI_FILL_CAPS)
90 pci_scan_trad_caps(d);
91 if (want_fields & PCI_FILL_EXT_CAPS)
92 pci_scan_ext_caps(d);
93 return want_fields;
94}
95
96void
97pci_free_caps(struct pci_dev *d)
98{
99 struct pci_cap *cap;
100
101 while (cap = d->first_cap)
102 {
adb9abcf
MM
103 d->first_cap = cap->next;
104 pci_mfree(cap);
6d5e39ac
MM
105 }
106}
107
108struct pci_cap *
109pci_find_cap(struct pci_dev *d, unsigned int id, unsigned int type)
c22c314a
DS
110{
111 return pci_find_cap_nr(d, id, type, NULL);
112}
113
114/**
115 * Finds a particular capability of a device
116 *
d7d9e305 117 * To select one capability if there are more than one with the same id, you
c22c314a
DS
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
d7d9e305
MM
120 * can supply NULL. The cap_number will be replaced by the acutal number
121 * of capablities with that id.
c22c314a
DS
122 */
123struct pci_cap *
124pci_find_cap_nr(struct pci_dev *d, unsigned int id, unsigned int type,
125 unsigned int *cap_number)
6d5e39ac
MM
126{
127 struct pci_cap *c;
d7d9e305
MM
128 struct pci_cap *found = NULL;
129 unsigned int target = (cap_number ? *cap_number : 0);
130 unsigned int index = 0;
131
132 pci_fill_info_v35(d, ((type == PCI_CAP_NORMAL) ? PCI_FILL_CAPS : PCI_FILL_EXT_CAPS));
133
134 for (c=d->first_cap; c; c=c->next)
c22c314a 135 {
d7d9e305
MM
136 if (c->type == type && c->id == id)
137 {
138 if (target == index)
139 found = c;
bc68c6cf 140 index++;
d7d9e305 141 }
c22c314a 142 }
6d5e39ac 143
d7d9e305
MM
144 if (cap_number)
145 *cap_number = index;
146 return found;
6d5e39ac 147}