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 <arpa/nameser.h>
19 #ifdef PCI_COMPRESSED_IDS
21 typedef gzFile pci_file;
22 #define pci_gets(f, l, s) gzgets(f, l, s)
23 #define pci_eof(f) gzeof(f)
25 static pci_file pci_open(struct pci_access *a)
31 result = gzopen(a->id_file_name, "rb");
34 len = strlen(a->id_file_name);
35 if (len >= 3 && memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
37 new_name = malloc(len - 2);
38 memcpy(new_name, a->id_file_name, len - 3);
39 new_name[len - 3] = 0;
40 pci_set_name_list_path(a, new_name, 1);
41 return gzopen(a->id_file_name, "rb");
44 #define pci_close(f) gzclose(f)
45 #define PCI_ERROR(f, err) \
48 err = gzerror(f, &errnum); \
49 if (errnum == Z_ERRNO) err = "I/O error"; \
50 else if (errnum >= 0) err = NULL; \
53 typedef FILE * pci_file;
54 #define pci_gets(f, l, s) fgets(l, s, f)
55 #define pci_eof(f) feof(f)
56 #define pci_open(a) fopen(a->id_file_name, "r")
57 #define pci_close(f) fclose(f)
58 #define PCI_ERROR(f, err) if (!err && ferror(f)) err = "I/O error";
62 struct id_entry *next;
80 struct id_bucket *next;
85 #define BUCKET_SIZE 8192
86 #define HASH_SIZE 4099
89 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
92 struct id_bucket *next;
95 #define BUCKET_ALIGNMENT sizeof(union id_align)
97 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
99 static void *id_alloc(struct pci_access *a, unsigned int size)
101 struct id_bucket *buck = a->current_id_bucket;
106 a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
107 memset(a->id_hash, 0, sizeof(struct id_entry *) * HASH_SIZE);
110 if (!buck || buck->full + size > BUCKET_SIZE)
112 buck = pci_malloc(a, BUCKET_SIZE);
113 buck->next = a->current_id_bucket;
114 a->current_id_bucket = buck;
115 buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
118 buck->full = BUCKET_ALIGN(buck->full + size);
119 return (byte *)buck + pos;
122 static inline u32 id_pair(unsigned int x, unsigned int y)
124 return ((x << 16) | y);
127 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
131 h = id12 ^ (id34 << 3) ^ (cat << 5);
132 return h % HASH_SIZE;
135 static char *id_net_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
137 char name[256], dnsname[256], txt[256];
147 sprintf(name, "%04x", id1);
150 sprintf(name, "%04x.%04x", id2, id1);
153 sprintf(name, "%04x.%04x.%04x.%04x", id4, id3, id2, id1);
155 case ID_GEN_SUBSYSTEM:
156 sprintf(name, "%04x.%04x.s", id2, id1);
159 sprintf(name, "%02x.c", id1);
162 sprintf(name, "%02x.%02x.c", id2, id1);
165 sprintf(name, "%02x.%02x.%02x.c", id3, id2, id1);
170 sprintf(dnsname, "%s.%s", name, a->id_domain);
172 a->debug("Resolving %s\n", dnsname);
174 res = res_query(dnsname, ns_c_in, ns_t_txt, answer, sizeof(answer));
177 a->debug("\tfailed, h_errno=%d\n", _res.res_h_errno);
180 if (ns_initparse(answer, res, &m) < 0)
182 a->debug("\tinitparse failed\n");
185 for (i=0; ns_parserr(&m, ns_s_an, i, &rr) >= 0; i++)
187 a->debug("\tanswer %d (class %d, type %d)\n", i, ns_rr_class(rr), ns_rr_type(rr));
188 if (ns_rr_class(rr) != ns_c_in || ns_rr_type(rr) != ns_t_txt)
190 data = ns_rr_rdata(rr);
191 dlen = ns_rr_rdlen(rr);
193 while (j < dlen && j+1+data[j] <= dlen)
195 memcpy(txt, &data[j+1], data[j]);
198 a->debug("\t\t%s\n", txt);
199 if (txt[0] == 'i' && txt[1] == '=')
200 return strdup(txt+2); /* FIXME */
207 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text)
209 u32 id12 = id_pair(id1, id2);
210 u32 id34 = id_pair(id3, id4);
211 unsigned int h = id_hash(cat, id12, id34);
212 struct id_entry *n = a->id_hash ? a->id_hash[h] : NULL;
213 int len = strlen(text);
215 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
219 n = id_alloc(a, sizeof(struct id_entry) + len);
223 memcpy(n->name, text, len+1);
224 n->next = a->id_hash[h];
229 static char *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
232 u32 id12 = id_pair(id1, id2);
233 u32 id34 = id_pair(id3, id4);
238 n = a->id_hash[id_hash(cat, id12, id34)];
239 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
244 if (name = id_net_lookup(a, cat, id1, id2, id3, id4))
246 id_insert(a, cat, id1, id2, id3, id4, name);
252 static int id_hex(char *p, int cnt)
258 if (*p >= '0' && *p <= '9')
260 else if (*p >= 'a' && *p <= 'f')
261 x += (*p - 'a' + 10);
262 else if (*p >= 'A' && *p <= 'F')
263 x += (*p - 'A' + 10);
271 static inline int id_white_p(int c)
273 return (c == ' ') || (c == '\t');
276 static const char *id_parse_list(struct pci_access *a, pci_file f, int *lino)
280 int id1=0, id2=0, id3=0, id4=0;
283 static const char parse_error[] = "Parse error";
286 while (pci_gets(f, line, sizeof(line)))
290 while (*p && *p != '\n' && *p != '\r')
292 if (!*p && !pci_eof(f))
293 return "Line too long";
295 if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
299 while (id_white_p(*p))
301 if (!*p || *p == '#')
309 if (!nest) /* Top-level entries */
311 if (p[0] == 'C' && p[1] == ' ') /* Class block */
313 if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
318 else if (p[0] == 'S' && p[1] == ' ')
319 { /* Generic subsystem block */
320 if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
322 if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
323 return "Vendor does not exist";
324 cat = ID_GEN_SUBSYSTEM;
327 else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
328 { /* Unrecognized block (RFU) */
334 if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
341 else if (cat == ID_UNKNOWN) /* Nested entries in RFU blocks are skipped */
343 else if (nest == 1) /* Nesting level 1 */
349 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
355 case ID_GEN_SUBSYSTEM:
356 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
364 if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
373 else if (nest == 2) /* Nesting level 2 */
378 if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
386 if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
395 else /* Nesting level 3 or more */
397 while (id_white_p(*p))
401 if (id_insert(a, cat, id1, id2, id3, id4, p))
402 return "Duplicate entry";
408 pci_load_name_list(struct pci_access *a)
414 pci_free_name_list(a);
415 a->hash_load_failed = 1;
416 if (!(f = pci_open(a)))
418 err = id_parse_list(a, f, &lino);
422 a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
423 a->hash_load_failed = 0;
428 pci_free_name_list(struct pci_access *a)
430 pci_mfree(a->id_hash);
432 while (a->current_id_bucket)
434 struct id_bucket *buck = a->current_id_bucket;
435 a->current_id_bucket = buck->next;
441 id_lookup_subsys(struct pci_access *a, int iv, int id, int isv, int isd)
444 if (iv > 0 && id > 0) /* Per-device lookup */
445 d = id_lookup(a, ID_SUBSYSTEM, iv, id, isv, isd);
446 if (!d) /* Generic lookup */
447 d = id_lookup(a, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
448 if (!d && iv == isv && id == isd) /* Check for subsystem == device */
449 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
454 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
457 if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
459 else if (flags & PCI_LOOKUP_NUMERIC)
460 res = snprintf(buf, size, "%s", num);
462 res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
463 else if (!(flags & PCI_LOOKUP_MIXED))
464 res = snprintf(buf, size, "%s", name);
466 res = snprintf(buf, size, "%s [%s]", name, num);
467 if (res < 0 || res >= size)
468 return "<pci_lookup_name: buffer too small>";
474 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
477 if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
479 if (flags & PCI_LOOKUP_NUMERIC)
480 res = snprintf(buf, size, "%s", num);
481 else if (flags & PCI_LOOKUP_MIXED)
484 res = snprintf(buf, size, "%s %s [%s]", v, d, num);
486 res = snprintf(buf, size, "Unknown device [%s]", num);
488 res = snprintf(buf, size, "%s Unknown device [%s]", v, num);
493 res = snprintf(buf, size, "%s %s", v, d);
495 res = snprintf(buf, size, "Unknown device %s", num);
497 res = snprintf(buf, size, "%s Unknown device %s", v, num+5);
499 if (res < 0 || res >= size)
500 return "<pci_lookup_name: buffer too small>";
506 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
509 char *v, *d, *cls, *pif;
510 int iv, id, isv, isd, icls, ipif;
511 char numbuf[16], pifbuf[32];
513 va_start(args, flags);
515 if (!(flags & PCI_LOOKUP_NO_NUMBERS))
517 if (a->numeric_ids > 1)
518 flags |= PCI_LOOKUP_MIXED;
519 else if (a->numeric_ids)
520 flags |= PCI_LOOKUP_NUMERIC;
522 if (flags & PCI_LOOKUP_MIXED)
523 flags &= ~PCI_LOOKUP_NUMERIC;
525 if (!a->id_hash && !(flags & PCI_LOOKUP_NUMERIC) && !a->hash_load_failed)
526 pci_load_name_list(a);
528 switch (flags & 0xffff)
530 case PCI_LOOKUP_VENDOR:
531 iv = va_arg(args, int);
532 sprintf(numbuf, "%04x", iv);
533 return format_name(buf, size, flags, id_lookup(a, ID_VENDOR, iv, 0, 0, 0), numbuf, "Unknown vendor");
534 case PCI_LOOKUP_DEVICE:
535 iv = va_arg(args, int);
536 id = va_arg(args, int);
537 sprintf(numbuf, "%04x", id);
538 return format_name(buf, size, flags, id_lookup(a, ID_DEVICE, iv, id, 0, 0), numbuf, "Unknown device");
539 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
540 iv = va_arg(args, int);
541 id = va_arg(args, int);
542 sprintf(numbuf, "%04x:%04x", iv, id);
543 v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
544 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
545 return format_name_pair(buf, size, flags, v, d, numbuf);
546 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
547 isv = va_arg(args, int);
548 sprintf(numbuf, "%04x", isv);
549 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
550 return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
551 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
552 iv = va_arg(args, int);
553 id = va_arg(args, int);
554 isv = va_arg(args, int);
555 isd = va_arg(args, int);
556 sprintf(numbuf, "%04x", isd);
557 return format_name(buf, size, flags, id_lookup_subsys(a, iv, id, isv, isd), numbuf, "Unknown device");
558 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
559 iv = va_arg(args, int);
560 id = va_arg(args, int);
561 isv = va_arg(args, int);
562 isd = va_arg(args, int);
563 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
564 d = id_lookup_subsys(a, iv, id, isv, isd);
565 sprintf(numbuf, "%04x:%04x", isv, isd);
566 return format_name_pair(buf, size, flags, v, d, numbuf);
567 case PCI_LOOKUP_CLASS:
568 icls = va_arg(args, int);
569 sprintf(numbuf, "%04x", icls);
570 cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
571 if (!cls && (cls = id_lookup(a, ID_CLASS, icls >> 8, 0, 0, 0)))
573 if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
574 flags |= PCI_LOOKUP_MIXED;
576 return format_name(buf, size, flags, cls, numbuf, ((flags & PCI_LOOKUP_MIXED) ? "Unknown class" : "Class"));
577 case PCI_LOOKUP_PROGIF:
578 icls = va_arg(args, int);
579 ipif = va_arg(args, int);
580 sprintf(numbuf, "%02x", ipif);
581 pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
582 if (!pif && icls == 0x0101 && !(ipif & 0x70))
584 /* IDE controllers have complex prog-if semantics */
585 sprintf(pifbuf, "%s%s%s%s%s",
586 (ipif & 0x80) ? " Master" : "",
587 (ipif & 0x08) ? " SecP" : "",
588 (ipif & 0x04) ? " SecO" : "",
589 (ipif & 0x02) ? " PriP" : "",
590 (ipif & 0x01) ? " PriO" : "");
595 return format_name(buf, size, flags, pif, numbuf, "ProgIf");
597 return "<pci_lookup_name: invalid request>";
601 void pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed)
604 free(a->id_file_name);
605 a->id_file_name = name;
606 a->free_id_name = to_be_freed;