2 * The PCI Library -- ID to Name Translation
4 * Copyright (c) 1997--2007 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, "r");
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, "r");
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 byte name[256], dnsname[256], answer[4096], txt[256];
146 sprintf(name, "%04x", id1);
149 sprintf(name, "%04x.%04x", id2, id1);
152 sprintf(name, "%04x.%04x.%04x.%04x", id4, id3, id2, id1);
154 case ID_GEN_SUBSYSTEM:
155 sprintf(name, "%04x.%04x.s", id2, id1);
158 sprintf(name, "%02x.c", id1);
161 sprintf(name, "%02x.%02x.c", id2, id1);
164 sprintf(name, "%02x.%02x.%02x.c", id3, id2, id1);
169 sprintf(dnsname, "%s.%s", name, a->id_domain);
171 a->debug("Resolving %s\n", dnsname);
173 res = res_query(dnsname, ns_c_in, ns_t_txt, answer, sizeof(answer));
176 a->debug("\tfailed, h_errno=%d\n", _res.res_h_errno);
179 if (ns_initparse(answer, res, &m) < 0)
181 a->debug("\tinitparse failed\n");
184 for (i=0; ns_parserr(&m, ns_s_an, i, &rr) >= 0; i++)
186 a->debug("\tanswer %d (class %d, type %d)\n", i, ns_rr_class(rr), ns_rr_type(rr));
187 if (ns_rr_class(rr) != ns_c_in || ns_rr_type(rr) != ns_t_txt)
189 data = ns_rr_rdata(rr);
190 dlen = ns_rr_rdlen(rr);
192 while (j < dlen && j+1+data[j] <= dlen)
194 memcpy(txt, &data[j+1], data[j]);
197 a->debug("\t\t%s\n", txt);
198 if (txt[0] == 'i' && txt[1] == '=')
199 return strdup(txt+2); /* FIXME */
206 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text)
208 u32 id12 = id_pair(id1, id2);
209 u32 id34 = id_pair(id3, id4);
210 unsigned int h = id_hash(cat, id12, id34);
211 struct id_entry *n = a->id_hash ? a->id_hash[h] : NULL;
212 int len = strlen(text);
214 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
218 n = id_alloc(a, sizeof(struct id_entry) + len);
222 memcpy(n->name, text, len+1);
223 n->next = a->id_hash[h];
228 static char *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
231 u32 id12 = id_pair(id1, id2);
232 u32 id34 = id_pair(id3, id4);
237 n = a->id_hash[id_hash(cat, id12, id34)];
238 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
243 if (name = id_net_lookup(a, cat, id1, id2, id3, id4))
245 id_insert(a, cat, id1, id2, id3, id4, name);
251 static int id_hex(char *p, int cnt)
257 if (*p >= '0' && *p <= '9')
259 else if (*p >= 'a' && *p <= 'f')
260 x += (*p - 'a' + 10);
261 else if (*p >= 'A' && *p <= 'F')
262 x += (*p - 'A' + 10);
270 static inline int id_white_p(int c)
272 return (c == ' ') || (c == '\t');
275 static const char *id_parse_list(struct pci_access *a, pci_file f, int *lino)
279 int id1=0, id2=0, id3=0, id4=0;
282 static const char parse_error[] = "Parse error";
285 while (pci_gets(f, line, sizeof(line)))
289 while (*p && *p != '\n' && *p != '\r')
291 if (!*p && !pci_eof(f))
292 return "Line too long";
294 if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
298 while (id_white_p(*p))
300 if (!*p || *p == '#')
308 if (!nest) /* Top-level entries */
310 if (p[0] == 'C' && p[1] == ' ') /* Class block */
312 if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
317 else if (p[0] == 'S' && p[1] == ' ')
318 { /* Generic subsystem block */
319 if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
321 if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
322 return "Vendor does not exist";
323 cat = ID_GEN_SUBSYSTEM;
326 else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
327 { /* Unrecognized block (RFU) */
333 if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
340 else if (cat == ID_UNKNOWN) /* Nested entries in RFU blocks are skipped */
342 else if (nest == 1) /* Nesting level 1 */
348 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
354 case ID_GEN_SUBSYSTEM:
355 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
363 if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
372 else if (nest == 2) /* Nesting level 2 */
377 if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
385 if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
394 else /* Nesting level 3 or more */
396 while (id_white_p(*p))
400 if (id_insert(a, cat, id1, id2, id3, id4, p))
401 return "Duplicate entry";
407 pci_load_name_list(struct pci_access *a)
413 pci_free_name_list(a);
414 a->hash_load_failed = 1;
415 if (!(f = pci_open(a)))
417 err = id_parse_list(a, f, &lino);
421 a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
422 a->hash_load_failed = 0;
427 pci_free_name_list(struct pci_access *a)
429 pci_mfree(a->id_hash);
431 while (a->current_id_bucket)
433 struct id_bucket *buck = a->current_id_bucket;
434 a->current_id_bucket = buck->next;
440 id_lookup_subsys(struct pci_access *a, int iv, int id, int isv, int isd)
443 if (iv > 0 && id > 0) /* Per-device lookup */
444 d = id_lookup(a, ID_SUBSYSTEM, iv, id, isv, isd);
445 if (!d) /* Generic lookup */
446 d = id_lookup(a, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
447 if (!d && iv == isv && id == isd) /* Check for subsystem == device */
448 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
453 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
456 if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
458 else if (flags & PCI_LOOKUP_NUMERIC)
459 res = snprintf(buf, size, "%s", num);
461 res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
462 else if (!(flags & PCI_LOOKUP_MIXED))
463 res = snprintf(buf, size, "%s", name);
465 res = snprintf(buf, size, "%s [%s]", name, num);
466 if (res < 0 || res >= size)
467 return "<pci_lookup_name: buffer too small>";
473 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
476 if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
478 if (flags & PCI_LOOKUP_NUMERIC)
479 res = snprintf(buf, size, "%s", num);
480 else if (flags & PCI_LOOKUP_MIXED)
483 res = snprintf(buf, size, "%s %s [%s]", v, d, num);
485 res = snprintf(buf, size, "Unknown device [%s]", num);
487 res = snprintf(buf, size, "%s Unknown device [%s]", v, num);
492 res = snprintf(buf, size, "%s %s", v, d);
494 res = snprintf(buf, size, "Unknown device %s", num);
496 res = snprintf(buf, size, "%s Unknown device %s", v, num+5);
498 if (res < 0 || res >= size)
499 return "<pci_lookup_name: buffer too small>";
505 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
508 char *v, *d, *cls, *pif;
509 int iv, id, isv, isd, icls, ipif;
510 char numbuf[16], pifbuf[32];
512 va_start(args, flags);
514 if (!(flags & PCI_LOOKUP_NO_NUMBERS))
516 if (a->numeric_ids > 1)
517 flags |= PCI_LOOKUP_MIXED;
518 else if (a->numeric_ids)
519 flags |= PCI_LOOKUP_NUMERIC;
521 if (flags & PCI_LOOKUP_MIXED)
522 flags &= ~PCI_LOOKUP_NUMERIC;
524 if (!a->id_hash && !(flags & PCI_LOOKUP_NUMERIC) && !a->hash_load_failed)
525 pci_load_name_list(a);
527 switch (flags & 0xffff)
529 case PCI_LOOKUP_VENDOR:
530 iv = va_arg(args, int);
531 sprintf(numbuf, "%04x", iv);
532 return format_name(buf, size, flags, id_lookup(a, ID_VENDOR, iv, 0, 0, 0), numbuf, "Unknown vendor");
533 case PCI_LOOKUP_DEVICE:
534 iv = va_arg(args, int);
535 id = va_arg(args, int);
536 sprintf(numbuf, "%04x", id);
537 return format_name(buf, size, flags, id_lookup(a, ID_DEVICE, iv, id, 0, 0), numbuf, "Unknown device");
538 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
539 iv = va_arg(args, int);
540 id = va_arg(args, int);
541 sprintf(numbuf, "%04x:%04x", iv, id);
542 v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
543 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
544 return format_name_pair(buf, size, flags, v, d, numbuf);
545 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
546 isv = va_arg(args, int);
547 sprintf(numbuf, "%04x", isv);
548 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
549 return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
550 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
551 iv = va_arg(args, int);
552 id = va_arg(args, int);
553 isv = va_arg(args, int);
554 isd = va_arg(args, int);
555 sprintf(numbuf, "%04x", isd);
556 return format_name(buf, size, flags, id_lookup_subsys(a, iv, id, isv, isd), numbuf, "Unknown device");
557 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
558 iv = va_arg(args, int);
559 id = va_arg(args, int);
560 isv = va_arg(args, int);
561 isd = va_arg(args, int);
562 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
563 d = id_lookup_subsys(a, iv, id, isv, isd);
564 sprintf(numbuf, "%04x:%04x", isv, isd);
565 return format_name_pair(buf, size, flags, v, d, numbuf);
566 case PCI_LOOKUP_CLASS:
567 icls = va_arg(args, int);
568 sprintf(numbuf, "%04x", icls);
569 cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
570 if (!cls && (cls = id_lookup(a, ID_CLASS, icls >> 8, 0, 0, 0)))
572 if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
573 flags |= PCI_LOOKUP_MIXED;
575 return format_name(buf, size, flags, cls, numbuf, ((flags & PCI_LOOKUP_MIXED) ? "Unknown class" : "Class"));
576 case PCI_LOOKUP_PROGIF:
577 icls = va_arg(args, int);
578 ipif = va_arg(args, int);
579 sprintf(numbuf, "%02x", ipif);
580 pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
581 if (!pif && icls == 0x0101 && !(ipif & 0x70))
583 /* IDE controllers have complex prog-if semantics */
584 sprintf(pifbuf, "%s%s%s%s%s",
585 (ipif & 0x80) ? " Master" : "",
586 (ipif & 0x08) ? " SecP" : "",
587 (ipif & 0x04) ? " SecO" : "",
588 (ipif & 0x02) ? " PriP" : "",
589 (ipif & 0x01) ? " PriO" : "");
594 return format_name(buf, size, flags, pif, numbuf, "ProgIf");
596 return "<pci_lookup_name: invalid request>";
600 void pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed)
603 free(a->id_file_name);
604 a->id_file_name = name;
605 a->free_id_name = to_be_freed;