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>
17 #include <sys/types.h>
23 #ifdef PCI_COMPRESSED_IDS
25 typedef gzFile pci_file;
26 #define pci_gets(f, l, s) gzgets(f, l, s)
27 #define pci_eof(f) gzeof(f)
29 static pci_file pci_open(struct pci_access *a)
35 result = gzopen(a->id_file_name, "rb");
38 len = strlen(a->id_file_name);
39 if (len >= 3 && memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
41 new_name = malloc(len - 2);
42 memcpy(new_name, a->id_file_name, len - 3);
43 new_name[len - 3] = 0;
44 pci_set_name_list_path(a, new_name, 1);
45 return gzopen(a->id_file_name, "rb");
48 #define pci_close(f) gzclose(f)
49 #define PCI_ERROR(f, err) \
52 gzerror(f, &errnum); \
53 if (errnum >= 0) err = NULL; \
54 else if (errnum == Z_ERRNO) err = "I/O error"; \
55 else err = zError(errnum); \
58 typedef FILE * pci_file;
59 #define pci_gets(f, l, s) fgets(l, s, f)
60 #define pci_eof(f) feof(f)
61 #define pci_open(a) fopen(a->id_file_name, "r")
62 #define pci_close(f) fclose(f)
63 #define PCI_ERROR(f, err) if (!err && ferror(f)) err = "I/O error";
67 struct id_entry *next;
93 struct id_bucket *next;
98 #define BUCKET_SIZE 8192
99 #define HASH_SIZE 4099
102 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
105 struct id_bucket *next;
108 #define BUCKET_ALIGNMENT sizeof(union id_align)
110 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
112 static void *id_alloc(struct pci_access *a, unsigned int size)
114 struct id_bucket *buck = a->current_id_bucket;
119 a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
120 memset(a->id_hash, 0, sizeof(struct id_entry *) * HASH_SIZE);
123 if (!buck || buck->full + size > BUCKET_SIZE)
125 buck = pci_malloc(a, BUCKET_SIZE);
126 buck->next = a->current_id_bucket;
127 a->current_id_bucket = buck;
128 buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
131 buck->full = BUCKET_ALIGN(buck->full + size);
132 return (byte *)buck + pos;
135 static inline u32 id_pair(unsigned int x, unsigned int y)
137 return ((x << 16) | y);
140 static inline unsigned int pair_first(unsigned int x)
142 return (x >> 16) & 0xffff;
145 static inline unsigned int pair_second(unsigned int x)
150 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
154 h = id12 ^ (id34 << 3) ^ (cat << 5);
155 return h % HASH_SIZE;
158 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text, enum id_entry_src src)
160 u32 id12 = id_pair(id1, id2);
161 u32 id34 = id_pair(id3, id4);
162 unsigned int h = id_hash(cat, id12, id34);
163 struct id_entry *n = a->id_hash ? a->id_hash[h] : NULL;
164 int len = strlen(text);
166 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
170 n = id_alloc(a, sizeof(struct id_entry) + len);
175 memcpy(n->name, text, len+1);
176 n->next = a->id_hash[h];
181 static char *id_lookup_raw(struct pci_access *a, int flags, int cat, int id1, int id2, int id3, int id4)
183 struct id_entry *n, *best;
184 u32 id12 = id_pair(id1, id2);
185 u32 id34 = id_pair(id3, id4);
189 n = a->id_hash[id_hash(cat, id12, id34)];
193 if (n->id12 != id12 || n->id34 != id34 || n->cat != cat)
195 if (n->src == SRC_LOCAL && (flags & PCI_LOOKUP_SKIP_LOCAL))
197 if (n->src == SRC_NET && !(flags & PCI_LOOKUP_NETWORK))
199 if (n->src == SRC_CACHE && !(flags & PCI_LOOKUP_CACHE))
201 if (!best || best->src < n->src)
210 static const char cache_version[] = "#PCI-CACHE-1.0";
213 pci_id_cache_load(struct pci_access *a, int flags)
217 const char default_name[] = "/.pciids-cache";
221 a->id_cache_status = 1;
222 if (!a->id_cache_file)
224 /* Construct the default ID cache name */
225 uid_t uid = getuid();
226 struct passwd *pw = getpwuid(uid);
229 name = pci_malloc(a, strlen(pw->pw_dir) + sizeof(default_name));
230 sprintf(name, "%s%s", pw->pw_dir, default_name);
231 pci_set_id_cache(a, name, 1);
233 a->debug("Using cache %s\n", a->id_cache_file);
234 if (flags & PCI_LOOKUP_REFRESH_CACHE)
236 a->debug("Not loading cache, will refresh everything\n");
237 a->id_cache_status = 2;
241 f = fopen(a->id_cache_file, "rb");
244 a->debug("Cache file does not exist\n");
247 /* FIXME: Compare timestamp with the pci.ids file? */
250 while (fgets(line, sizeof(line), f))
252 char *p = strchr(line, '\n');
259 if (strcmp(line, cache_version))
261 a->debug("Unrecognized cache version %s, ignoring\n", line);
268 int cat, id1, id2, id3, id4, cnt;
269 if (sscanf(line, "%d%x%x%x%x%n", &cat, &id1, &id2, &id3, &id4, &cnt) >= 5)
272 while (*p && *p == ' ')
274 id_insert(a, cat, id1, id2, id3, id4, p, SRC_CACHE);
279 a->warning("Malformed cache file %s (line %d), ignoring", a->id_cache_file, lino);
284 a->warning("Error while reading %s", a->id_cache_file);
290 pci_id_cache_dirty(struct pci_access *a)
292 if (a->id_cache_status >= 1)
293 a->id_cache_status = 2;
297 pci_id_cache_flush(struct pci_access *a)
299 int orig_status = a->id_cache_status;
302 struct id_entry *e, *e2;
304 a->id_cache_status = 0;
307 if (!a->id_cache_file)
309 f = fopen(a->id_cache_file, "wb");
312 a->warning("Cannot write %s: %s", a->id_cache_file, strerror(errno));
315 a->debug("Writing cache to %s\n", a->id_cache_file);
316 fprintf(f, "%s\n", cache_version);
318 for (h=0; h<HASH_SIZE; h++)
319 for (e=a->id_hash[h]; e; e=e->next)
320 if (e->src == SRC_CACHE || e->src == SRC_NET)
322 /* Verify that every entry is written at most once */
323 for (e2=a->id_hash[h]; e2 != e; e2=e2->next)
324 if ((e2->src == SRC_CACHE || e2->src == SRC_NET) &&
326 e2->id12 == e->id12 && e2->id34 == e->id34)
329 fprintf(f, "%d %x %x %x %x %s\n",
331 pair_first(e->id12), pair_second(e->id12),
332 pair_first(e->id34), pair_second(e->id34),
338 a->warning("Error writing %s", a->id_cache_file);
342 static char *id_net_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
344 char name[256], dnsname[256], txt[256];
354 sprintf(name, "%04x", id1);
357 sprintf(name, "%04x.%04x", id2, id1);
360 sprintf(name, "%04x.%04x.%04x.%04x", id4, id3, id2, id1);
362 case ID_GEN_SUBSYSTEM:
363 sprintf(name, "%04x.%04x.s", id2, id1);
366 sprintf(name, "%02x.c", id1);
369 sprintf(name, "%02x.%02x.c", id2, id1);
372 sprintf(name, "%02x.%02x.%02x.c", id3, id2, id1);
377 sprintf(dnsname, "%s.%s", name, a->id_domain);
379 a->debug("Resolving %s\n", dnsname);
381 res = res_query(dnsname, ns_c_in, ns_t_txt, answer, sizeof(answer));
384 a->debug("\tfailed, h_errno=%d\n", _res.res_h_errno);
387 if (ns_initparse(answer, res, &m) < 0)
389 a->debug("\tinitparse failed\n");
392 for (i=0; ns_parserr(&m, ns_s_an, i, &rr) >= 0; i++)
394 a->debug("\tanswer %d (class %d, type %d)\n", i, ns_rr_class(rr), ns_rr_type(rr));
395 if (ns_rr_class(rr) != ns_c_in || ns_rr_type(rr) != ns_t_txt)
397 data = ns_rr_rdata(rr);
398 dlen = ns_rr_rdlen(rr);
400 while (j < dlen && j+1+data[j] <= dlen)
402 memcpy(txt, &data[j+1], data[j]);
405 a->debug("\t\t%s\n", txt);
406 if (txt[0] == 'i' && txt[1] == '=')
407 return strdup(txt+2);
414 static char *id_lookup(struct pci_access *a, int flags, int cat, int id1, int id2, int id3, int id4)
418 while (!(name = id_lookup_raw(a, flags, cat, id1, id2, id3, id4)))
420 if ((flags & PCI_LOOKUP_CACHE) && !a->id_cache_status)
422 if (pci_id_cache_load(a, flags))
425 if (flags & PCI_LOOKUP_NETWORK)
427 if (name = id_net_lookup(a, cat, id1, id2, id3, id4))
429 id_insert(a, cat, id1, id2, id3, id4, name, SRC_NET);
431 pci_id_cache_dirty(a);
434 id_insert(a, cat, id1, id2, id3, id4, "", SRC_NET); /* FIXME: Check that negative caching works */
435 /* We want to iterate the lookup to get the allocated ID entry from the hash */
440 return (name[0] ? name : NULL);
443 static int id_hex(char *p, int cnt)
449 if (*p >= '0' && *p <= '9')
451 else if (*p >= 'a' && *p <= 'f')
452 x += (*p - 'a' + 10);
453 else if (*p >= 'A' && *p <= 'F')
454 x += (*p - 'A' + 10);
462 static inline int id_white_p(int c)
464 return (c == ' ') || (c == '\t');
467 static const char *id_parse_list(struct pci_access *a, pci_file f, int *lino)
471 int id1=0, id2=0, id3=0, id4=0;
474 static const char parse_error[] = "Parse error";
477 while (pci_gets(f, line, sizeof(line)))
481 while (*p && *p != '\n' && *p != '\r')
483 if (!*p && !pci_eof(f))
484 return "Line too long";
486 if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
490 while (id_white_p(*p))
492 if (!*p || *p == '#')
500 if (!nest) /* Top-level entries */
502 if (p[0] == 'C' && p[1] == ' ') /* Class block */
504 if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
509 else if (p[0] == 'S' && p[1] == ' ')
510 { /* Generic subsystem block */
511 if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
513 if (!id_lookup(a, 0, ID_VENDOR, id1, 0, 0, 0))
514 return "Vendor does not exist";
515 cat = ID_GEN_SUBSYSTEM;
518 else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
519 { /* Unrecognized block (RFU) */
525 if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
532 else if (cat == ID_UNKNOWN) /* Nested entries in RFU blocks are skipped */
534 else if (nest == 1) /* Nesting level 1 */
540 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
546 case ID_GEN_SUBSYSTEM:
547 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
555 if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
564 else if (nest == 2) /* Nesting level 2 */
569 if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
577 if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
586 else /* Nesting level 3 or more */
588 while (id_white_p(*p))
592 if (id_insert(a, cat, id1, id2, id3, id4, p, SRC_LOCAL))
593 return "Duplicate entry";
599 pci_load_name_list(struct pci_access *a)
605 pci_free_name_list(a);
606 a->id_load_failed = 1;
607 if (!(f = pci_open(a)))
609 err = id_parse_list(a, f, &lino);
613 a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
614 a->id_load_failed = 0;
619 pci_free_name_list(struct pci_access *a)
621 pci_id_cache_flush(a);
622 pci_mfree(a->id_hash);
624 a->id_cache_status = 0;
625 while (a->current_id_bucket)
627 struct id_bucket *buck = a->current_id_bucket;
628 a->current_id_bucket = buck->next;
634 id_lookup_subsys(struct pci_access *a, int flags, int iv, int id, int isv, int isd)
637 if (iv > 0 && id > 0) /* Per-device lookup */
638 d = id_lookup(a, flags, ID_SUBSYSTEM, iv, id, isv, isd);
639 if (!d) /* Generic lookup */
640 d = id_lookup(a, flags, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
641 if (!d && iv == isv && id == isd) /* Check for subsystem == device */
642 d = id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0);
647 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
650 if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
652 else if (flags & PCI_LOOKUP_NUMERIC)
653 res = snprintf(buf, size, "%s", num);
655 res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
656 else if (!(flags & PCI_LOOKUP_MIXED))
657 res = snprintf(buf, size, "%s", name);
659 res = snprintf(buf, size, "%s [%s]", name, num);
660 if (res < 0 || res >= size)
661 return "<pci_lookup_name: buffer too small>";
667 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
670 if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
672 if (flags & PCI_LOOKUP_NUMERIC)
673 res = snprintf(buf, size, "%s", num);
674 else if (flags & PCI_LOOKUP_MIXED)
677 res = snprintf(buf, size, "%s %s [%s]", v, d, num);
679 res = snprintf(buf, size, "Unknown device [%s]", num);
681 res = snprintf(buf, size, "%s Unknown device [%s]", v, num);
686 res = snprintf(buf, size, "%s %s", v, d);
688 res = snprintf(buf, size, "Unknown device %s", num);
690 res = snprintf(buf, size, "%s Unknown device %s", v, num+5);
692 if (res < 0 || res >= size)
693 return "<pci_lookup_name: buffer too small>";
699 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
702 char *v, *d, *cls, *pif;
703 int iv, id, isv, isd, icls, ipif;
704 char numbuf[16], pifbuf[32];
706 va_start(args, flags);
708 flags |= a->id_lookup_mode;
709 if (!(flags & PCI_LOOKUP_NO_NUMBERS))
711 if (a->numeric_ids > 1)
712 flags |= PCI_LOOKUP_MIXED;
713 else if (a->numeric_ids)
714 flags |= PCI_LOOKUP_NUMERIC;
716 if (flags & PCI_LOOKUP_MIXED)
717 flags &= ~PCI_LOOKUP_NUMERIC;
719 if (!a->id_hash && !(flags & (PCI_LOOKUP_NUMERIC | PCI_LOOKUP_SKIP_LOCAL)) && !a->id_load_failed)
720 pci_load_name_list(a);
722 switch (flags & 0xffff)
724 case PCI_LOOKUP_VENDOR:
725 iv = va_arg(args, int);
726 sprintf(numbuf, "%04x", iv);
727 return format_name(buf, size, flags, id_lookup(a, flags, ID_VENDOR, iv, 0, 0, 0), numbuf, "Unknown vendor");
728 case PCI_LOOKUP_DEVICE:
729 iv = va_arg(args, int);
730 id = va_arg(args, int);
731 sprintf(numbuf, "%04x", id);
732 return format_name(buf, size, flags, id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0), numbuf, "Unknown device");
733 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
734 iv = va_arg(args, int);
735 id = va_arg(args, int);
736 sprintf(numbuf, "%04x:%04x", iv, id);
737 v = id_lookup(a, flags, ID_VENDOR, iv, 0, 0, 0);
738 d = id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0);
739 return format_name_pair(buf, size, flags, v, d, numbuf);
740 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
741 isv = va_arg(args, int);
742 sprintf(numbuf, "%04x", isv);
743 v = id_lookup(a, flags, ID_VENDOR, isv, 0, 0, 0);
744 return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
745 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
746 iv = va_arg(args, int);
747 id = va_arg(args, int);
748 isv = va_arg(args, int);
749 isd = va_arg(args, int);
750 sprintf(numbuf, "%04x", isd);
751 return format_name(buf, size, flags, id_lookup_subsys(a, flags, iv, id, isv, isd), numbuf, "Unknown device");
752 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
753 iv = va_arg(args, int);
754 id = va_arg(args, int);
755 isv = va_arg(args, int);
756 isd = va_arg(args, int);
757 v = id_lookup(a, flags, ID_VENDOR, isv, 0, 0, 0);
758 d = id_lookup_subsys(a, flags, iv, id, isv, isd);
759 sprintf(numbuf, "%04x:%04x", isv, isd);
760 return format_name_pair(buf, size, flags, v, d, numbuf);
761 case PCI_LOOKUP_CLASS:
762 icls = va_arg(args, int);
763 sprintf(numbuf, "%04x", icls);
764 cls = id_lookup(a, flags, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
765 if (!cls && (cls = id_lookup(a, flags, ID_CLASS, icls >> 8, 0, 0, 0)))
767 if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
768 flags |= PCI_LOOKUP_MIXED;
770 return format_name(buf, size, flags, cls, numbuf, ((flags & PCI_LOOKUP_MIXED) ? "Unknown class" : "Class"));
771 case PCI_LOOKUP_PROGIF:
772 icls = va_arg(args, int);
773 ipif = va_arg(args, int);
774 sprintf(numbuf, "%02x", ipif);
775 pif = id_lookup(a, flags, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
776 if (!pif && icls == 0x0101 && !(ipif & 0x70))
778 /* IDE controllers have complex prog-if semantics */
779 sprintf(pifbuf, "%s%s%s%s%s",
780 (ipif & 0x80) ? " Master" : "",
781 (ipif & 0x08) ? " SecP" : "",
782 (ipif & 0x04) ? " SecO" : "",
783 (ipif & 0x02) ? " PriP" : "",
784 (ipif & 0x01) ? " PriO" : "");
789 return format_name(buf, size, flags, pif, numbuf, "ProgIf");
791 return "<pci_lookup_name: invalid request>";
795 void pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed)
798 free(a->id_file_name);
799 a->id_file_name = name;
800 a->free_id_name = to_be_freed;
803 void pci_set_net_domain(struct pci_access *a, char *name, int to_be_freed)
805 if (a->free_id_domain)
808 a->free_id_domain = to_be_freed;
811 void pci_set_id_cache(struct pci_access *a, char *name, int to_be_freed)
813 if (a->free_id_cache_file)
814 free(a->id_cache_file);
815 a->id_cache_file = name;
816 a->free_id_cache_file = to_be_freed;