2 * The PCI Library -- ID to Name Translation
4 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
16 static char *id_lookup(struct pci_access *a, int flags, int cat, int id1, int id2, int id3, int id4)
20 while (!(name = pci_id_lookup(a, flags, cat, id1, id2, id3, id4)))
22 if ((flags & PCI_LOOKUP_CACHE) && !a->id_cache_status)
24 if (pci_id_cache_load(a, flags))
27 if (flags & PCI_LOOKUP_NETWORK)
29 if (name = pci_id_net_lookup(a, cat, id1, id2, id3, id4))
31 pci_id_insert(a, cat, id1, id2, id3, id4, name, SRC_NET);
33 pci_id_cache_dirty(a);
36 pci_id_insert(a, cat, id1, id2, id3, id4, "", SRC_NET);
37 /* We want to iterate the lookup to get the allocated ID entry from the hash */
42 return (name[0] ? name : NULL);
46 id_lookup_subsys(struct pci_access *a, int flags, int iv, int id, int isv, int isd)
49 if (iv > 0 && id > 0) /* Per-device lookup */
50 d = id_lookup(a, flags, ID_SUBSYSTEM, iv, id, isv, isd);
51 if (!d) /* Generic lookup */
52 d = id_lookup(a, flags, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
53 if (!d && iv == isv && id == isd) /* Check for subsystem == device */
54 d = id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0);
59 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
62 if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
64 else if (flags & PCI_LOOKUP_NUMERIC)
65 res = snprintf(buf, size, "%s", num);
67 res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
68 else if (!(flags & PCI_LOOKUP_MIXED))
69 res = snprintf(buf, size, "%s", name);
71 res = snprintf(buf, size, "%s [%s]", name, num);
72 if (res < 0 || res >= size)
73 return "<pci_lookup_name: buffer too small>";
79 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
82 if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
84 if (flags & PCI_LOOKUP_NUMERIC)
85 res = snprintf(buf, size, "%s", num);
86 else if (flags & PCI_LOOKUP_MIXED)
89 res = snprintf(buf, size, "%s %s [%s]", v, d, num);
91 res = snprintf(buf, size, "Device [%s]", num);
93 res = snprintf(buf, size, "%s Device [%s]", v, num);
98 res = snprintf(buf, size, "%s %s", v, d);
100 res = snprintf(buf, size, "Device %s", num);
102 res = snprintf(buf, size, "%s Device %s", v, num+5);
104 if (res < 0 || res >= size)
105 return "<pci_lookup_name: buffer too small>";
111 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
114 char *v, *d, *cls, *pif;
115 int iv, id, isv, isd, icls, ipif;
116 char numbuf[16], pifbuf[32];
118 va_start(args, flags);
120 flags |= a->id_lookup_mode;
121 if (!(flags & PCI_LOOKUP_NO_NUMBERS))
123 if (a->numeric_ids > 1)
124 flags |= PCI_LOOKUP_MIXED;
125 else if (a->numeric_ids)
126 flags |= PCI_LOOKUP_NUMERIC;
128 if (flags & PCI_LOOKUP_MIXED)
129 flags &= ~PCI_LOOKUP_NUMERIC;
131 if (!a->id_hash && !(flags & (PCI_LOOKUP_NUMERIC | PCI_LOOKUP_SKIP_LOCAL)) && !a->id_load_failed)
132 pci_load_name_list(a);
134 switch (flags & 0xffff)
136 case PCI_LOOKUP_VENDOR:
137 iv = va_arg(args, int);
138 sprintf(numbuf, "%04x", iv);
139 return format_name(buf, size, flags, id_lookup(a, flags, ID_VENDOR, iv, 0, 0, 0), numbuf, "Vendor");
140 case PCI_LOOKUP_DEVICE:
141 iv = va_arg(args, int);
142 id = va_arg(args, int);
143 sprintf(numbuf, "%04x", id);
144 return format_name(buf, size, flags, id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0), numbuf, "Device");
145 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
146 iv = va_arg(args, int);
147 id = va_arg(args, int);
148 sprintf(numbuf, "%04x:%04x", iv, id);
149 v = id_lookup(a, flags, ID_VENDOR, iv, 0, 0, 0);
150 d = id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0);
151 return format_name_pair(buf, size, flags, v, d, numbuf);
152 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
153 isv = va_arg(args, int);
154 sprintf(numbuf, "%04x", isv);
155 v = id_lookup(a, flags, ID_VENDOR, isv, 0, 0, 0);
156 return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
157 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
158 iv = va_arg(args, int);
159 id = va_arg(args, int);
160 isv = va_arg(args, int);
161 isd = va_arg(args, int);
162 sprintf(numbuf, "%04x", isd);
163 return format_name(buf, size, flags, id_lookup_subsys(a, flags, iv, id, isv, isd), numbuf, "Device");
164 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
165 iv = va_arg(args, int);
166 id = va_arg(args, int);
167 isv = va_arg(args, int);
168 isd = va_arg(args, int);
169 v = id_lookup(a, flags, ID_VENDOR, isv, 0, 0, 0);
170 d = id_lookup_subsys(a, flags, iv, id, isv, isd);
171 sprintf(numbuf, "%04x:%04x", isv, isd);
172 return format_name_pair(buf, size, flags, v, d, numbuf);
173 case PCI_LOOKUP_CLASS:
174 icls = va_arg(args, int);
175 sprintf(numbuf, "%04x", icls);
176 cls = id_lookup(a, flags, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
177 if (!cls && (cls = id_lookup(a, flags, ID_CLASS, icls >> 8, 0, 0, 0)))
179 if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
180 flags |= PCI_LOOKUP_MIXED;
182 return format_name(buf, size, flags, cls, numbuf, "Class");
183 case PCI_LOOKUP_PROGIF:
184 icls = va_arg(args, int);
185 ipif = va_arg(args, int);
186 sprintf(numbuf, "%02x", ipif);
187 pif = id_lookup(a, flags, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
188 if (!pif && icls == 0x0101 && !(ipif & 0x70))
190 /* IDE controllers have complex prog-if semantics */
191 sprintf(pifbuf, "%s%s%s%s%s",
192 (ipif & 0x80) ? " Master" : "",
193 (ipif & 0x08) ? " SecP" : "",
194 (ipif & 0x04) ? " SecO" : "",
195 (ipif & 0x02) ? " PriP" : "",
196 (ipif & 0x01) ? " PriO" : "");
201 return format_name(buf, size, flags, pif, numbuf, "ProgIf");
203 return "<pci_lookup_name: invalid request>";