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.
14 #include <netinet/in.h>
15 #include <arpa/nameser.h>
20 #ifdef PCI_COMPRESSED_IDS
22 typedef gzFile pci_file;
23 #define pci_gets(f, l, s) gzgets(f, l, s)
24 #define pci_eof(f) gzeof(f)
26 static pci_file pci_open(struct pci_access *a)
32 result = gzopen(a->id_file_name, "rb");
35 len = strlen(a->id_file_name);
36 if (len >= 3 && memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
38 new_name = malloc(len - 2);
39 memcpy(new_name, a->id_file_name, len - 3);
40 new_name[len - 3] = 0;
41 pci_set_name_list_path(a, new_name, 1);
42 return gzopen(a->id_file_name, "rb");
45 #define pci_close(f) gzclose(f)
46 #define PCI_ERROR(f, err) \
49 gzerror(f, &errnum); \
50 if (errnum >= 0) err = NULL; \
51 else if (errnum == Z_ERRNO) err = "I/O error"; \
52 else err = zError(errnum); \
55 typedef FILE * pci_file;
56 #define pci_gets(f, l, s) fgets(l, s, f)
57 #define pci_eof(f) feof(f)
58 #define pci_open(a) fopen(a->id_file_name, "r")
59 #define pci_close(f) fclose(f)
60 #define PCI_ERROR(f, err) if (!err && ferror(f)) err = "I/O error";
64 struct id_entry *next;
82 struct id_bucket *next;
87 #define BUCKET_SIZE 8192
88 #define HASH_SIZE 4099
91 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
94 struct id_bucket *next;
97 #define BUCKET_ALIGNMENT sizeof(union id_align)
99 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
101 static void *id_alloc(struct pci_access *a, unsigned int size)
103 struct id_bucket *buck = a->current_id_bucket;
108 a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
109 memset(a->id_hash, 0, sizeof(struct id_entry *) * HASH_SIZE);
112 if (!buck || buck->full + size > BUCKET_SIZE)
114 buck = pci_malloc(a, BUCKET_SIZE);
115 buck->next = a->current_id_bucket;
116 a->current_id_bucket = buck;
117 buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
120 buck->full = BUCKET_ALIGN(buck->full + size);
121 return (byte *)buck + pos;
124 static inline u32 id_pair(unsigned int x, unsigned int y)
126 return ((x << 16) | y);
129 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
133 h = id12 ^ (id34 << 3) ^ (cat << 5);
134 return h % HASH_SIZE;
137 static char *id_net_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
139 char name[256], dnsname[256], txt[256];
149 sprintf(name, "%04x", id1);
152 sprintf(name, "%04x.%04x", id2, id1);
155 sprintf(name, "%04x.%04x.%04x.%04x", id4, id3, id2, id1);
157 case ID_GEN_SUBSYSTEM:
158 sprintf(name, "%04x.%04x.s", id2, id1);
161 sprintf(name, "%02x.c", id1);
164 sprintf(name, "%02x.%02x.c", id2, id1);
167 sprintf(name, "%02x.%02x.%02x.c", id3, id2, id1);
172 sprintf(dnsname, "%s.%s", name, a->id_domain);
174 a->debug("Resolving %s\n", dnsname);
176 res = res_query(dnsname, ns_c_in, ns_t_txt, answer, sizeof(answer));
179 a->debug("\tfailed, h_errno=%d\n", _res.res_h_errno);
182 if (ns_initparse(answer, res, &m) < 0)
184 a->debug("\tinitparse failed\n");
187 for (i=0; ns_parserr(&m, ns_s_an, i, &rr) >= 0; i++)
189 a->debug("\tanswer %d (class %d, type %d)\n", i, ns_rr_class(rr), ns_rr_type(rr));
190 if (ns_rr_class(rr) != ns_c_in || ns_rr_type(rr) != ns_t_txt)
192 data = ns_rr_rdata(rr);
193 dlen = ns_rr_rdlen(rr);
195 while (j < dlen && j+1+data[j] <= dlen)
197 memcpy(txt, &data[j+1], data[j]);
200 a->debug("\t\t%s\n", txt);
201 if (txt[0] == 'i' && txt[1] == '=')
202 return strdup(txt+2); /* FIXME */
209 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text)
211 u32 id12 = id_pair(id1, id2);
212 u32 id34 = id_pair(id3, id4);
213 unsigned int h = id_hash(cat, id12, id34);
214 struct id_entry *n = a->id_hash ? a->id_hash[h] : NULL;
215 int len = strlen(text);
217 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
221 n = id_alloc(a, sizeof(struct id_entry) + len);
225 memcpy(n->name, text, len+1);
226 n->next = a->id_hash[h];
231 static char *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
234 u32 id12 = id_pair(id1, id2);
235 u32 id34 = id_pair(id3, id4);
240 n = a->id_hash[id_hash(cat, id12, id34)];
241 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
246 if (name = id_net_lookup(a, cat, id1, id2, id3, id4))
248 id_insert(a, cat, id1, id2, id3, id4, name);
254 static int id_hex(char *p, int cnt)
260 if (*p >= '0' && *p <= '9')
262 else if (*p >= 'a' && *p <= 'f')
263 x += (*p - 'a' + 10);
264 else if (*p >= 'A' && *p <= 'F')
265 x += (*p - 'A' + 10);
273 static inline int id_white_p(int c)
275 return (c == ' ') || (c == '\t');
278 static const char *id_parse_list(struct pci_access *a, pci_file f, int *lino)
282 int id1=0, id2=0, id3=0, id4=0;
285 static const char parse_error[] = "Parse error";
288 while (pci_gets(f, line, sizeof(line)))
292 while (*p && *p != '\n' && *p != '\r')
294 if (!*p && !pci_eof(f))
295 return "Line too long";
297 if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
301 while (id_white_p(*p))
303 if (!*p || *p == '#')
311 if (!nest) /* Top-level entries */
313 if (p[0] == 'C' && p[1] == ' ') /* Class block */
315 if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
320 else if (p[0] == 'S' && p[1] == ' ')
321 { /* Generic subsystem block */
322 if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
324 if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
325 return "Vendor does not exist";
326 cat = ID_GEN_SUBSYSTEM;
329 else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
330 { /* Unrecognized block (RFU) */
336 if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
343 else if (cat == ID_UNKNOWN) /* Nested entries in RFU blocks are skipped */
345 else if (nest == 1) /* Nesting level 1 */
351 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
357 case ID_GEN_SUBSYSTEM:
358 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
366 if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
375 else if (nest == 2) /* Nesting level 2 */
380 if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
388 if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
397 else /* Nesting level 3 or more */
399 while (id_white_p(*p))
403 if (id_insert(a, cat, id1, id2, id3, id4, p))
404 return "Duplicate entry";
410 pci_load_name_list(struct pci_access *a)
416 pci_free_name_list(a);
417 a->hash_load_failed = 1;
418 if (!(f = pci_open(a)))
420 err = id_parse_list(a, f, &lino);
424 a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
425 a->hash_load_failed = 0;
430 pci_free_name_list(struct pci_access *a)
432 pci_mfree(a->id_hash);
434 while (a->current_id_bucket)
436 struct id_bucket *buck = a->current_id_bucket;
437 a->current_id_bucket = buck->next;
443 id_lookup_subsys(struct pci_access *a, int iv, int id, int isv, int isd)
446 if (iv > 0 && id > 0) /* Per-device lookup */
447 d = id_lookup(a, ID_SUBSYSTEM, iv, id, isv, isd);
448 if (!d) /* Generic lookup */
449 d = id_lookup(a, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
450 if (!d && iv == isv && id == isd) /* Check for subsystem == device */
451 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
456 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
459 if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
461 else if (flags & PCI_LOOKUP_NUMERIC)
462 res = snprintf(buf, size, "%s", num);
464 res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
465 else if (!(flags & PCI_LOOKUP_MIXED))
466 res = snprintf(buf, size, "%s", name);
468 res = snprintf(buf, size, "%s [%s]", name, num);
469 if (res < 0 || res >= size)
470 return "<pci_lookup_name: buffer too small>";
476 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
479 if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
481 if (flags & PCI_LOOKUP_NUMERIC)
482 res = snprintf(buf, size, "%s", num);
483 else if (flags & PCI_LOOKUP_MIXED)
486 res = snprintf(buf, size, "%s %s [%s]", v, d, num);
488 res = snprintf(buf, size, "Unknown device [%s]", num);
490 res = snprintf(buf, size, "%s Unknown device [%s]", v, num);
495 res = snprintf(buf, size, "%s %s", v, d);
497 res = snprintf(buf, size, "Unknown device %s", num);
499 res = snprintf(buf, size, "%s Unknown device %s", v, num+5);
501 if (res < 0 || res >= size)
502 return "<pci_lookup_name: buffer too small>";
508 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
511 char *v, *d, *cls, *pif;
512 int iv, id, isv, isd, icls, ipif;
513 char numbuf[16], pifbuf[32];
515 va_start(args, flags);
517 if (!(flags & PCI_LOOKUP_NO_NUMBERS))
519 if (a->numeric_ids > 1)
520 flags |= PCI_LOOKUP_MIXED;
521 else if (a->numeric_ids)
522 flags |= PCI_LOOKUP_NUMERIC;
524 if (flags & PCI_LOOKUP_MIXED)
525 flags &= ~PCI_LOOKUP_NUMERIC;
527 if (!a->id_hash && !(flags & PCI_LOOKUP_NUMERIC) && !a->hash_load_failed)
528 pci_load_name_list(a);
530 switch (flags & 0xffff)
532 case PCI_LOOKUP_VENDOR:
533 iv = va_arg(args, int);
534 sprintf(numbuf, "%04x", iv);
535 return format_name(buf, size, flags, id_lookup(a, ID_VENDOR, iv, 0, 0, 0), numbuf, "Unknown vendor");
536 case PCI_LOOKUP_DEVICE:
537 iv = va_arg(args, int);
538 id = va_arg(args, int);
539 sprintf(numbuf, "%04x", id);
540 return format_name(buf, size, flags, id_lookup(a, ID_DEVICE, iv, id, 0, 0), numbuf, "Unknown device");
541 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
542 iv = va_arg(args, int);
543 id = va_arg(args, int);
544 sprintf(numbuf, "%04x:%04x", iv, id);
545 v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
546 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
547 return format_name_pair(buf, size, flags, v, d, numbuf);
548 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
549 isv = va_arg(args, int);
550 sprintf(numbuf, "%04x", isv);
551 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
552 return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
553 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
554 iv = va_arg(args, int);
555 id = va_arg(args, int);
556 isv = va_arg(args, int);
557 isd = va_arg(args, int);
558 sprintf(numbuf, "%04x", isd);
559 return format_name(buf, size, flags, id_lookup_subsys(a, iv, id, isv, isd), numbuf, "Unknown device");
560 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
561 iv = va_arg(args, int);
562 id = va_arg(args, int);
563 isv = va_arg(args, int);
564 isd = va_arg(args, int);
565 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
566 d = id_lookup_subsys(a, iv, id, isv, isd);
567 sprintf(numbuf, "%04x:%04x", isv, isd);
568 return format_name_pair(buf, size, flags, v, d, numbuf);
569 case PCI_LOOKUP_CLASS:
570 icls = va_arg(args, int);
571 sprintf(numbuf, "%04x", icls);
572 cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
573 if (!cls && (cls = id_lookup(a, ID_CLASS, icls >> 8, 0, 0, 0)))
575 if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
576 flags |= PCI_LOOKUP_MIXED;
578 return format_name(buf, size, flags, cls, numbuf, ((flags & PCI_LOOKUP_MIXED) ? "Unknown class" : "Class"));
579 case PCI_LOOKUP_PROGIF:
580 icls = va_arg(args, int);
581 ipif = va_arg(args, int);
582 sprintf(numbuf, "%02x", ipif);
583 pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
584 if (!pif && icls == 0x0101 && !(ipif & 0x70))
586 /* IDE controllers have complex prog-if semantics */
587 sprintf(pifbuf, "%s%s%s%s%s",
588 (ipif & 0x80) ? " Master" : "",
589 (ipif & 0x08) ? " SecP" : "",
590 (ipif & 0x04) ? " SecO" : "",
591 (ipif & 0x02) ? " PriP" : "",
592 (ipif & 0x01) ? " PriO" : "");
597 return format_name(buf, size, flags, pif, numbuf, "ProgIf");
599 return "<pci_lookup_name: invalid request>";
603 void pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed)
606 free(a->id_file_name);
607 a->id_file_name = name;
608 a->free_id_name = to_be_freed;