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.
17 #ifdef PCI_COMPRESSED_IDS
19 typedef gzFile pci_file;
20 #define pci_gets(f, l, s) gzgets(f, l, s)
21 #define pci_eof(f) gzeof(f)
23 static pci_file pci_open(struct pci_access *a)
29 result = gzopen(a->id_file_name, "rb");
32 len = strlen(a->id_file_name);
33 if (len >= 3 && memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
35 new_name = malloc(len - 2);
36 memcpy(new_name, a->id_file_name, len - 3);
37 new_name[len - 3] = 0;
38 pci_set_name_list_path(a, new_name, 1);
39 return gzopen(a->id_file_name, "rb");
42 #define pci_close(f) gzclose(f)
43 #define PCI_ERROR(f, err) \
46 gzerror(f, &errnum); \
47 if (errnum >= 0) err = NULL; \
48 else if (errnum == Z_ERRNO) err = "I/O error"; \
49 else err = zError(errnum); \
52 typedef FILE * pci_file;
53 #define pci_gets(f, l, s) fgets(l, s, f)
54 #define pci_eof(f) feof(f)
55 #define pci_open(a) fopen(a->id_file_name, "r")
56 #define pci_close(f) fclose(f)
57 #define PCI_ERROR(f, err) if (!err && ferror(f)) err = "I/O error";
61 struct id_entry *next;
79 struct id_bucket *next;
84 #define BUCKET_SIZE 8192
85 #define HASH_SIZE 4099
88 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
91 struct id_bucket *next;
94 #define BUCKET_ALIGNMENT sizeof(union id_align)
96 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
98 static void *id_alloc(struct pci_access *a, unsigned int size)
100 struct id_bucket *buck = a->current_id_bucket;
102 if (!buck || buck->full + size > BUCKET_SIZE)
104 buck = pci_malloc(a, BUCKET_SIZE);
105 buck->next = a->current_id_bucket;
106 a->current_id_bucket = buck;
107 buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
110 buck->full = BUCKET_ALIGN(buck->full + size);
111 return (byte *)buck + pos;
114 static inline u32 id_pair(unsigned int x, unsigned int y)
116 return ((x << 16) | y);
119 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
123 h = id12 ^ (id34 << 3) ^ (cat << 5);
124 return h % HASH_SIZE;
127 static char *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
130 u32 id12 = id_pair(id1, id2);
131 u32 id34 = id_pair(id3, id4);
135 n = a->id_hash[id_hash(cat, id12, id34)];
136 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
138 return n ? n->name : NULL;
141 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text)
143 u32 id12 = id_pair(id1, id2);
144 u32 id34 = id_pair(id3, id4);
145 unsigned int h = id_hash(cat, id12, id34);
146 struct id_entry *n = a->id_hash[h];
147 int len = strlen(text);
149 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
153 n = id_alloc(a, sizeof(struct id_entry) + len);
157 memcpy(n->name, text, len+1);
158 n->next = a->id_hash[h];
163 static int id_hex(char *p, int cnt)
169 if (*p >= '0' && *p <= '9')
171 else if (*p >= 'a' && *p <= 'f')
172 x += (*p - 'a' + 10);
173 else if (*p >= 'A' && *p <= 'F')
174 x += (*p - 'A' + 10);
182 static inline int id_white_p(int c)
184 return (c == ' ') || (c == '\t');
187 static const char *id_parse_list(struct pci_access *a, pci_file f, int *lino)
191 int id1=0, id2=0, id3=0, id4=0;
194 static const char parse_error[] = "Parse error";
197 while (pci_gets(f, line, sizeof(line)))
201 while (*p && *p != '\n' && *p != '\r')
203 if (!*p && !pci_eof(f))
204 return "Line too long";
206 if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
210 while (id_white_p(*p))
212 if (!*p || *p == '#')
220 if (!nest) /* Top-level entries */
222 if (p[0] == 'C' && p[1] == ' ') /* Class block */
224 if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
229 else if (p[0] == 'S' && p[1] == ' ')
230 { /* Generic subsystem block */
231 if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
233 if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
234 return "Vendor does not exist";
235 cat = ID_GEN_SUBSYSTEM;
238 else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
239 { /* Unrecognized block (RFU) */
245 if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
252 else if (cat == ID_UNKNOWN) /* Nested entries in RFU blocks are skipped */
254 else if (nest == 1) /* Nesting level 1 */
260 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
266 case ID_GEN_SUBSYSTEM:
267 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
275 if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
284 else if (nest == 2) /* Nesting level 2 */
289 if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
297 if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
306 else /* Nesting level 3 or more */
308 while (id_white_p(*p))
312 if (id_insert(a, cat, id1, id2, id3, id4, p))
313 return "Duplicate entry";
319 pci_load_name_list(struct pci_access *a)
325 pci_free_name_list(a);
326 a->hash_load_failed = 1;
327 if (!(f = pci_open(a)))
329 a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
330 memset(a->id_hash, 0, sizeof(struct id_entry *) * HASH_SIZE);
331 err = id_parse_list(a, f, &lino);
335 a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
336 a->hash_load_failed = 0;
341 pci_free_name_list(struct pci_access *a)
343 pci_mfree(a->id_hash);
345 while (a->current_id_bucket)
347 struct id_bucket *buck = a->current_id_bucket;
348 a->current_id_bucket = buck->next;
354 id_lookup_subsys(struct pci_access *a, int iv, int id, int isv, int isd)
357 if (iv > 0 && id > 0) /* Per-device lookup */
358 d = id_lookup(a, ID_SUBSYSTEM, iv, id, isv, isd);
359 if (!d) /* Generic lookup */
360 d = id_lookup(a, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
361 if (!d && iv == isv && id == isd) /* Check for subsystem == device */
362 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
367 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
370 if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
372 else if (flags & PCI_LOOKUP_NUMERIC)
373 res = snprintf(buf, size, "%s", num);
375 res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
376 else if (!(flags & PCI_LOOKUP_MIXED))
377 res = snprintf(buf, size, "%s", name);
379 res = snprintf(buf, size, "%s [%s]", name, num);
380 if (res < 0 || res >= size)
381 return "<pci_lookup_name: buffer too small>";
387 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
390 if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
392 if (flags & PCI_LOOKUP_NUMERIC)
393 res = snprintf(buf, size, "%s", num);
394 else if (flags & PCI_LOOKUP_MIXED)
397 res = snprintf(buf, size, "%s %s [%s]", v, d, num);
399 res = snprintf(buf, size, "Unknown device [%s]", num);
401 res = snprintf(buf, size, "%s Unknown device [%s]", v, num);
406 res = snprintf(buf, size, "%s %s", v, d);
408 res = snprintf(buf, size, "Unknown device %s", num);
410 res = snprintf(buf, size, "%s Unknown device %s", v, num+5);
412 if (res < 0 || res >= size)
413 return "<pci_lookup_name: buffer too small>";
419 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
422 char *v, *d, *cls, *pif;
423 int iv, id, isv, isd, icls, ipif;
424 char numbuf[16], pifbuf[32];
426 va_start(args, flags);
428 if (!(flags & PCI_LOOKUP_NO_NUMBERS))
430 if (a->numeric_ids > 1)
431 flags |= PCI_LOOKUP_MIXED;
432 else if (a->numeric_ids)
433 flags |= PCI_LOOKUP_NUMERIC;
435 if (flags & PCI_LOOKUP_MIXED)
436 flags &= ~PCI_LOOKUP_NUMERIC;
438 if (!a->id_hash && !(flags & PCI_LOOKUP_NUMERIC) && !a->hash_load_failed)
439 pci_load_name_list(a);
441 switch (flags & 0xffff)
443 case PCI_LOOKUP_VENDOR:
444 iv = va_arg(args, int);
445 sprintf(numbuf, "%04x", iv);
446 return format_name(buf, size, flags, id_lookup(a, ID_VENDOR, iv, 0, 0, 0), numbuf, "Unknown vendor");
447 case PCI_LOOKUP_DEVICE:
448 iv = va_arg(args, int);
449 id = va_arg(args, int);
450 sprintf(numbuf, "%04x", id);
451 return format_name(buf, size, flags, id_lookup(a, ID_DEVICE, iv, id, 0, 0), numbuf, "Unknown device");
452 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
453 iv = va_arg(args, int);
454 id = va_arg(args, int);
455 sprintf(numbuf, "%04x:%04x", iv, id);
456 v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
457 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
458 return format_name_pair(buf, size, flags, v, d, numbuf);
459 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
460 isv = va_arg(args, int);
461 sprintf(numbuf, "%04x", isv);
462 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
463 return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
464 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
465 iv = va_arg(args, int);
466 id = va_arg(args, int);
467 isv = va_arg(args, int);
468 isd = va_arg(args, int);
469 sprintf(numbuf, "%04x", isd);
470 return format_name(buf, size, flags, id_lookup_subsys(a, iv, id, isv, isd), numbuf, "Unknown device");
471 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
472 iv = va_arg(args, int);
473 id = va_arg(args, int);
474 isv = va_arg(args, int);
475 isd = va_arg(args, int);
476 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
477 d = id_lookup_subsys(a, iv, id, isv, isd);
478 sprintf(numbuf, "%04x:%04x", isv, isd);
479 return format_name_pair(buf, size, flags, v, d, numbuf);
480 case PCI_LOOKUP_CLASS:
481 icls = va_arg(args, int);
482 sprintf(numbuf, "%04x", icls);
483 cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
484 if (!cls && (cls = id_lookup(a, ID_CLASS, icls >> 8, 0, 0, 0)))
486 if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
487 flags |= PCI_LOOKUP_MIXED;
489 return format_name(buf, size, flags, cls, numbuf, ((flags & PCI_LOOKUP_MIXED) ? "Unknown class" : "Class"));
490 case PCI_LOOKUP_PROGIF:
491 icls = va_arg(args, int);
492 ipif = va_arg(args, int);
493 sprintf(numbuf, "%02x", ipif);
494 pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
495 if (!pif && icls == 0x0101 && !(ipif & 0x70))
497 /* IDE controllers have complex prog-if semantics */
498 sprintf(pifbuf, "%s%s%s%s%s",
499 (ipif & 0x80) ? " Master" : "",
500 (ipif & 0x08) ? " SecP" : "",
501 (ipif & 0x04) ? " SecO" : "",
502 (ipif & 0x02) ? " PriP" : "",
503 (ipif & 0x01) ? " PriO" : "");
508 return format_name(buf, size, flags, pif, numbuf, "ProgIf");
510 return "<pci_lookup_name: invalid request>";
514 void pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed)
517 free(a->id_file_name);
518 a->id_file_name = name;
519 a->free_id_name = to_be_freed;