2 * The PCI Library -- ID to Name Translation
4 * Copyright (c) 1997--2014 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
18 static char *id_lookup(struct pci_access *a, int flags, int cat, int id1, int id2, int id3, int id4)
23 while (!(name = pci_id_lookup(a, flags, cat, id1, id2, id3, id4)))
25 if ((flags & PCI_LOOKUP_CACHE) && !a->id_cache_status)
27 if (pci_id_cache_load(a, flags))
30 if (!tried_hwdb && !(flags & (PCI_LOOKUP_SKIP_LOCAL | PCI_LOOKUP_NO_HWDB)))
33 if (name = pci_id_hwdb_lookup(a, cat, id1, id2, id3, id4))
35 pci_id_insert(a, cat, id1, id2, id3, id4, name, SRC_HWDB);
40 if (flags & PCI_LOOKUP_NETWORK)
42 if (name = pci_id_net_lookup(a, cat, id1, id2, id3, id4))
44 pci_id_insert(a, cat, id1, id2, id3, id4, name, SRC_NET);
46 pci_id_cache_dirty(a);
49 pci_id_insert(a, cat, id1, id2, id3, id4, "", SRC_NET);
50 /* We want to iterate the lookup to get the allocated ID entry from the hash */
55 return (name[0] ? name : NULL);
59 id_lookup_subsys(struct pci_access *a, int flags, int iv, int id, int isv, int isd)
62 if (iv > 0 && id > 0) /* Per-device lookup */
63 d = id_lookup(a, flags, ID_SUBSYSTEM, iv, id, isv, isd);
64 if (!d) /* Generic lookup */
65 d = id_lookup(a, flags, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
66 if (!d && iv == isv && id == isd) /* Check for subsystem == device */
67 d = id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0);
72 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
75 if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
77 else if (flags & PCI_LOOKUP_NUMERIC)
78 res = snprintf(buf, size, "%s", num);
80 res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
81 else if (!(flags & PCI_LOOKUP_MIXED))
82 res = snprintf(buf, size, "%s", name);
84 res = snprintf(buf, size, "%s [%s]", name, num);
85 if (res >= size && size >= 4)
86 buf[size-2] = buf[size-3] = buf[size-4] = '.';
87 else if (res < 0 || res >= size)
88 return "<pci_lookup_name: buffer too small>";
93 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
96 if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
98 if (flags & PCI_LOOKUP_NUMERIC)
99 res = snprintf(buf, size, "%s", num);
100 else if (flags & PCI_LOOKUP_MIXED)
103 res = snprintf(buf, size, "%s %s [%s]", v, d, num);
105 res = snprintf(buf, size, "Device [%s]", num);
107 res = snprintf(buf, size, "%s Device [%s]", v, num);
112 res = snprintf(buf, size, "%s %s", v, d);
114 res = snprintf(buf, size, "Device %s", num);
116 res = snprintf(buf, size, "%s Device %s", v, num+5);
118 if (res >= size && size >= 4)
119 buf[size-2] = buf[size-3] = buf[size-4] = '.';
120 else if (res < 0 || res >= size)
121 return "<pci_lookup_name: buffer too small>";
126 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
129 char *v, *d, *cls, *pif;
130 int iv, id, isv, isd, icls, ipif;
131 char numbuf[16], pifbuf[32];
133 va_start(args, flags);
135 flags |= a->id_lookup_mode;
136 if (!(flags & PCI_LOOKUP_NO_NUMBERS))
138 if (a->numeric_ids > 1)
139 flags |= PCI_LOOKUP_MIXED;
140 else if (a->numeric_ids)
141 flags |= PCI_LOOKUP_NUMERIC;
143 if (flags & PCI_LOOKUP_MIXED)
144 flags &= ~PCI_LOOKUP_NUMERIC;
146 if (!a->id_load_attempted && !(flags & (PCI_LOOKUP_NUMERIC | PCI_LOOKUP_SKIP_LOCAL)))
147 pci_load_name_list(a);
149 switch (flags & 0xffff)
151 case PCI_LOOKUP_VENDOR:
152 iv = va_arg(args, int);
153 sprintf(numbuf, "%04x", iv);
155 return format_name(buf, size, flags, id_lookup(a, flags, ID_VENDOR, iv, 0, 0, 0), numbuf, "Vendor");
156 case PCI_LOOKUP_DEVICE:
157 iv = va_arg(args, int);
158 id = va_arg(args, int);
159 sprintf(numbuf, "%04x", id);
161 return format_name(buf, size, flags, id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0), numbuf, "Device");
162 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
163 iv = va_arg(args, int);
164 id = va_arg(args, int);
165 sprintf(numbuf, "%04x:%04x", iv, id);
166 v = id_lookup(a, flags, ID_VENDOR, iv, 0, 0, 0);
167 d = id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0);
169 return format_name_pair(buf, size, flags, v, d, numbuf);
170 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
171 isv = va_arg(args, int);
172 sprintf(numbuf, "%04x", isv);
173 v = id_lookup(a, flags, ID_VENDOR, isv, 0, 0, 0);
175 return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
176 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
177 iv = va_arg(args, int);
178 id = va_arg(args, int);
179 isv = va_arg(args, int);
180 isd = va_arg(args, int);
181 sprintf(numbuf, "%04x", isd);
183 return format_name(buf, size, flags, id_lookup_subsys(a, flags, iv, id, isv, isd), numbuf, "Device");
184 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
185 iv = va_arg(args, int);
186 id = va_arg(args, int);
187 isv = va_arg(args, int);
188 isd = va_arg(args, int);
189 v = id_lookup(a, flags, ID_VENDOR, isv, 0, 0, 0);
190 d = id_lookup_subsys(a, flags, iv, id, isv, isd);
191 sprintf(numbuf, "%04x:%04x", isv, isd);
193 return format_name_pair(buf, size, flags, v, d, numbuf);
194 case PCI_LOOKUP_CLASS:
195 icls = va_arg(args, int);
196 sprintf(numbuf, "%04x", icls);
197 cls = id_lookup(a, flags, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
198 if (!cls && (cls = id_lookup(a, flags, ID_CLASS, icls >> 8, 0, 0, 0)))
200 if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
201 flags |= PCI_LOOKUP_MIXED;
204 return format_name(buf, size, flags, cls, numbuf, "Class");
205 case PCI_LOOKUP_PROGIF:
206 icls = va_arg(args, int);
207 ipif = va_arg(args, int);
208 sprintf(numbuf, "%02x", ipif);
209 pif = id_lookup(a, flags, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
210 if (!pif && icls == 0x0101 && !(ipif & 0x70))
212 /* IDE controllers have complex prog-if semantics */
213 sprintf(pifbuf, "%s%s%s%s%s",
214 (ipif & 0x80) ? " Master" : "",
215 (ipif & 0x08) ? " SecP" : "",
216 (ipif & 0x04) ? " SecO" : "",
217 (ipif & 0x02) ? " PriP" : "",
218 (ipif & 0x01) ? " PriO" : "");
224 return format_name(buf, size, flags, pif, numbuf, "ProgIf");
227 return "<pci_lookup_name: invalid request>";