2 * The PCI Library -- ID to Name Translation
4 * Copyright (c) 1997--2005 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
18 struct id_entry *next;
36 struct id_bucket *next;
41 #define BUCKET_SIZE 8192
42 #define HASH_SIZE 4099
45 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
48 struct id_bucket *next;
51 #define BUCKET_ALIGNMENT sizeof(union id_align)
53 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
55 static void *id_alloc(struct pci_access *a, unsigned int size)
57 struct id_bucket *buck = a->current_id_bucket;
59 if (!buck || buck->full + size > BUCKET_SIZE)
61 buck = pci_malloc(a, BUCKET_SIZE);
62 buck->next = a->current_id_bucket;
63 a->current_id_bucket = buck;
64 buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
67 buck->full = BUCKET_ALIGN(buck->full + size);
68 return (byte *)buck + pos;
71 static inline u32 id_pair(unsigned int x, unsigned int y)
73 return ((x << 16) | y);
76 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
80 h = id12 ^ (id34 << 3) ^ (cat << 5);
84 static byte *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
87 u32 id12 = id_pair(id1, id2);
88 u32 id34 = id_pair(id3, id4);
92 n = a->id_hash[id_hash(cat, id12, id34)];
93 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
95 return n ? n->name : NULL;
98 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, byte *text)
100 u32 id12 = id_pair(id1, id2);
101 u32 id34 = id_pair(id3, id4);
102 unsigned int h = id_hash(cat, id12, id34);
103 struct id_entry *n = a->id_hash[h];
104 int len = strlen(text);
106 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
110 n = id_alloc(a, sizeof(struct id_entry) + len);
114 memcpy(n->name, text, len+1);
115 n->next = a->id_hash[h];
120 static int id_hex(byte *p, int cnt)
126 if (*p >= '0' && *p <= '9')
128 else if (*p >= 'a' && *p <= 'f')
129 x += (*p - 'a' + 10);
130 else if (*p >= 'A' && *p <= 'F')
131 x += (*p - 'A' + 10);
139 static inline int id_white_p(int c)
141 return (c == ' ') || (c == '\t');
144 static const char *id_parse_list(struct pci_access *a, FILE *f, int *lino)
148 int id1=0, id2=0, id3=0, id4=0;
151 static const char parse_error[] = "Parse error";
154 while (fgets(line, sizeof(line), f))
158 while (*p && *p != '\n' && *p != '\r')
161 return "Line too long";
163 if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
167 while (id_white_p(*p))
169 if (!*p || *p == '#')
177 if (!nest) /* Top-level entries */
179 if (p[0] == 'C' && p[1] == ' ') /* Class block */
181 if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
186 else if (p[0] == 'S' && p[1] == ' ')
187 { /* Generic subsystem block */
188 if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
190 if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
191 return "Vendor does not exist";
192 cat = ID_GEN_SUBSYSTEM;
195 else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
196 { /* Unrecognized block (RFU) */
202 if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
209 else if (cat == ID_UNKNOWN) /* Nested entries in RFU blocks are skipped */
211 else if (nest == 1) /* Nesting level 1 */
217 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
223 case ID_GEN_SUBSYSTEM:
224 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
232 if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
241 else if (nest == 2) /* Nesting level 2 */
246 if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
254 if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
263 else /* Nesting level 3 or more */
265 while (id_white_p(*p))
269 if (id_insert(a, cat, id1, id2, id3, id4, p))
270 return "Duplicate entry";
276 pci_load_name_list(struct pci_access *a)
282 pci_free_name_list(a);
283 a->hash_load_failed = 1;
284 if (!(f = fopen(a->id_file_name, "r")))
286 a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
287 bzero(a->id_hash, sizeof(struct id_entry *) * HASH_SIZE);
288 err = id_parse_list(a, f, &lino);
289 if (!err && ferror(f))
293 a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
294 a->hash_load_failed = 0;
299 pci_free_name_list(struct pci_access *a)
301 pci_mfree(a->id_hash);
303 while (a->current_id_bucket)
305 struct id_bucket *buck = a->current_id_bucket;
306 a->current_id_bucket = buck->next;
312 id_lookup_subsys(struct pci_access *a, int iv, int id, int isv, int isd)
315 if (iv > 0 && id > 0) /* Per-device lookup */
316 d = id_lookup(a, ID_SUBSYSTEM, iv, id, isv, isd);
317 if (!d) /* Generic lookup */
318 d = id_lookup(a, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
319 if (!d && iv == isv && id == isd) /* Check for subsystem == device */
320 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
325 format_name(char *buf, int size, int flags, byte *name, byte *num, byte *unknown)
328 if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
330 else if (flags & PCI_LOOKUP_NUMERIC)
331 res = snprintf(buf, size, "%s", num);
333 res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
334 else if (!(flags & PCI_LOOKUP_MIXED))
335 res = snprintf(buf, size, "%s", name);
337 res = snprintf(buf, size, "%s [%s]", name, num);
338 if (res < 0 || res >= size)
339 return "<pci_lookup_name: buffer too small>";
345 format_name_pair(char *buf, int size, int flags, byte *v, byte *d, byte *num)
348 if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
350 if (flags & PCI_LOOKUP_NUMERIC)
351 res = snprintf(buf, size, "%s", num);
352 else if (flags & PCI_LOOKUP_MIXED)
355 res = snprintf(buf, size, "%s %s [%s]", v, d, num);
357 res = snprintf(buf, size, "Unknown device [%s]", num);
359 res = snprintf(buf, size, "%s Unknown device [%s]", v, num);
364 res = snprintf(buf, size, "%s %s", v, d);
366 res = snprintf(buf, size, "Unknown device %s", num);
368 res = snprintf(buf, size, "%s Unknown device %s", v, num+5);
370 if (res < 0 || res >= size)
371 return "<pci_lookup_name: buffer too small>";
377 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
380 byte *v, *d, *cls, *pif;
381 int iv, id, isv, isd, icls, ipif;
382 byte numbuf[16], pifbuf[32];
384 va_start(args, flags);
386 if (a->numeric_ids > 1)
387 flags |= PCI_LOOKUP_MIXED;
388 else if (a->numeric_ids)
389 flags |= PCI_LOOKUP_NUMERIC;
390 if (flags & PCI_LOOKUP_MIXED)
391 flags &= ~PCI_LOOKUP_NUMERIC;
393 if (!a->id_hash && !(flags & PCI_LOOKUP_NUMERIC) && !a->hash_load_failed)
394 pci_load_name_list(a);
396 switch (flags & 0xffff)
398 case PCI_LOOKUP_VENDOR:
399 iv = va_arg(args, int);
400 sprintf(numbuf, "%04x", iv);
401 return format_name(buf, size, flags, id_lookup(a, ID_VENDOR, iv, 0, 0, 0), numbuf, "Unknown vendor");
402 case PCI_LOOKUP_DEVICE:
403 iv = va_arg(args, int);
404 id = va_arg(args, int);
405 sprintf(numbuf, "%04x", id);
406 return format_name(buf, size, flags, id_lookup(a, ID_DEVICE, iv, id, 0, 0), numbuf, "Unknown device");
407 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
408 iv = va_arg(args, int);
409 id = va_arg(args, int);
410 sprintf(numbuf, "%04x:%04x", iv, id);
411 v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
412 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
413 return format_name_pair(buf, size, flags, v, d, numbuf);
414 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
415 isv = va_arg(args, int);
416 sprintf(numbuf, "%04x", isv);
417 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
418 return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
419 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
420 iv = va_arg(args, int);
421 id = va_arg(args, int);
422 isv = va_arg(args, int);
423 isd = va_arg(args, int);
424 sprintf(numbuf, "%04x", isd);
425 return format_name(buf, size, flags, id_lookup_subsys(a, iv, id, isv, isd), numbuf, "Unknown device");
426 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
427 iv = va_arg(args, int);
428 id = va_arg(args, int);
429 isv = va_arg(args, int);
430 isd = va_arg(args, int);
431 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
432 d = id_lookup_subsys(a, iv, id, isv, isd);
433 sprintf(numbuf, "%04x:%04x", isv, isd);
434 return format_name_pair(buf, size, flags, v, d, numbuf);
435 case PCI_LOOKUP_CLASS:
436 icls = va_arg(args, int);
437 sprintf(numbuf, "%04x", icls);
438 cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
439 if (!cls && (cls = id_lookup(a, ID_CLASS, icls, 0, 0, 0)))
441 if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
442 flags |= PCI_LOOKUP_MIXED;
444 return format_name(buf, size, flags, cls, numbuf, ((flags & PCI_LOOKUP_MIXED) ? "Unknown class" : "Class"));
445 case PCI_LOOKUP_PROGIF:
446 icls = va_arg(args, int);
447 ipif = va_arg(args, int);
448 sprintf(numbuf, "%02x", ipif);
449 pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
450 if (!pif && icls == 0x0101 && !(ipif & 0x70))
452 /* IDE controllers have complex prog-if semantics */
453 sprintf(pifbuf, "%s%s%s%s%s",
454 (ipif & 0x80) ? "Master " : "",
455 (ipif & 0x08) ? "SecP " : "",
456 (ipif & 0x04) ? "SecO " : "",
457 (ipif & 0x02) ? "PriP " : "",
458 (ipif & 0x01) ? "PriO " : "");
461 return format_name(buf, size, flags, pif, numbuf, "ProgIf");
463 return "<pci_lookup_name: invalid request>";