2 * The PCI Library -- ID to Name Translation
4 * Copyright (c) 1997--2005 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
18 struct id_entry *next;
36 struct id_bucket *next;
41 #define BUCKET_SIZE 8192
42 #define HASH_SIZE 4099
45 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
48 struct id_bucket *next;
51 #define BUCKET_ALIGNMENT sizeof(union id_align)
53 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
55 static void *id_alloc(struct pci_access *a, unsigned int size)
57 struct id_bucket *buck = a->current_id_bucket;
59 if (!buck || buck->full + size > BUCKET_SIZE)
61 buck = pci_malloc(a, BUCKET_SIZE);
62 buck->next = a->current_id_bucket;
63 a->current_id_bucket = buck;
64 buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
67 buck->full = BUCKET_ALIGN(buck->full + size);
68 return (byte *)buck + pos;
71 static inline u32 id_pair(unsigned int x, unsigned int y)
73 return ((x << 16) | y);
76 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
80 h = id12 ^ (id34 << 3) ^ (cat << 5);
84 static struct id_entry *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
87 u32 id12 = id_pair(id1, id2);
88 u32 id34 = id_pair(id3, id4);
90 n = a->id_hash[id_hash(cat, id12, id34)];
91 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
96 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, byte *text)
98 u32 id12 = id_pair(id1, id2);
99 u32 id34 = id_pair(id3, id4);
100 unsigned int h = id_hash(cat, id12, id34);
101 struct id_entry *n = a->id_hash[h];
102 int len = strlen(text);
104 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
108 n = id_alloc(a, sizeof(struct id_entry) + len);
112 memcpy(n->name, text, len+1);
113 n->next = a->id_hash[h];
118 static int id_hex(byte *p, int cnt)
124 if (*p >= '0' && *p <= '9')
126 else if (*p >= 'a' && *p <= 'f')
127 x += (*p - 'a' + 10);
128 else if (*p >= 'A' && *p <= 'F')
129 x += (*p - 'A' + 10);
137 static inline int id_white_p(int c)
139 return (c == ' ') || (c == '\t');
142 static const char *id_parse_list(struct pci_access *a, FILE *f, int *lino)
146 int id1=0, id2=0, id3=0, id4=0;
149 static const char parse_error[] = "Parse error";
152 while (fgets(line, sizeof(line), f))
156 while (*p && *p != '\n' && *p != '\r')
159 return "Line too long";
161 if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
165 while (id_white_p(*p))
167 if (!*p || *p == '#')
175 if (!nest) /* Top-level entries */
177 if (p[0] == 'C' && p[1] == ' ') /* Class block */
179 if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
184 else if (p[0] == 'S' && p[1] == ' ')
185 { /* Generic subsystem block */
186 if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
188 if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
189 return "Vendor does not exist";
190 cat = ID_GEN_SUBSYSTEM;
193 else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
194 { /* Unrecognized block (RFU) */
200 if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
207 else if (cat == ID_UNKNOWN) /* Nested entries in RFU blocks are skipped */
209 else if (nest == 1) /* Nesting level 1 */
215 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
221 case ID_GEN_SUBSYSTEM:
222 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
230 if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
239 else if (nest == 2) /* Nesting level 2 */
244 if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
252 if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
261 else /* Nesting level 3 or more */
263 while (id_white_p(*p))
267 if (id_insert(a, cat, id1, id2, id3, id4, p))
268 return "Duplicate entry";
274 pci_load_name_list(struct pci_access *a)
280 pci_free_name_list(a);
281 if (!(f = fopen(a->id_file_name, "r")))
283 a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
284 bzero(a->id_hash, sizeof(struct id_entry *) * HASH_SIZE);
285 err = id_parse_list(a, f, &lino);
286 if (!err && ferror(f))
290 a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
295 pci_free_name_list(struct pci_access *a)
297 pci_mfree(a->id_hash);
299 while (a->current_id_bucket)
301 struct id_bucket *buck = a->current_id_bucket;
302 a->current_id_bucket = buck->next;
307 static struct id_entry *id_lookup_subsys(struct pci_access *a, int iv, int id, int isv, int isd)
309 struct id_entry *d = NULL;
310 if (iv > 0 && id > 0) /* Per-device lookup */
311 d = id_lookup(a, ID_SUBSYSTEM, iv, id, isv, isd);
312 if (!d) /* Generic lookup */
313 d = id_lookup(a, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
314 if (!d && iv == isv && id == isd) /* Check for subsystem == device */
315 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
320 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
324 struct id_entry *v, *d, *cls, *pif;
325 int iv, id, isv, isd, icls, ipif;
327 va_start(args, flags);
330 if ((flags & PCI_LOOKUP_NUMERIC) || a->numeric_ids)
332 flags &= ~PCI_LOOKUP_NUMERIC;
335 else if (!a->id_hash)
337 if (!pci_load_name_list(a))
338 num = a->numeric_ids = 1;
341 if (flags & PCI_LOOKUP_NO_NUMBERS)
343 flags &= ~PCI_LOOKUP_NO_NUMBERS;
353 case PCI_LOOKUP_VENDOR:
354 iv = va_arg(args, int);
356 res = snprintf(buf, size, "%04x", iv);
357 else if (v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0))
360 res = snprintf(buf, size, "Unknown vendor %04x", iv);
362 case PCI_LOOKUP_DEVICE:
363 iv = va_arg(args, int);
364 id = va_arg(args, int);
366 res = snprintf(buf, size, "%04x", id);
367 else if (d = id_lookup(a, ID_DEVICE, iv, id, 0, 0))
370 res = snprintf(buf, size, "Unknown device %04x", id);
374 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
375 iv = va_arg(args, int);
376 id = va_arg(args, int);
378 res = snprintf(buf, size, "%04x:%04x", iv, id);
381 v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
382 d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
384 res = snprintf(buf, size, "%s %s", v->name, d->name);
388 res = snprintf(buf, size, "Unknown device %04x:%04x", iv, id);
390 res = snprintf(buf, size, "%s Unknown device %04x", v->name, id);
393 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
394 isv = va_arg(args, int);
396 res = snprintf(buf, size, "%04x", isv);
397 else if (v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0))
400 res = snprintf(buf, size, "Unknown vendor %04x", isv);
404 case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
405 iv = va_arg(args, int);
406 id = va_arg(args, int);
407 isv = va_arg(args, int);
408 isd = va_arg(args, int);
410 res = snprintf(buf, size, "%04x", isd);
411 else if (d = id_lookup_subsys(a, iv, id, isv, isd))
414 res = snprintf(buf, size, "Unknown device %04x", isd);
418 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
419 iv = va_arg(args, int);
420 id = va_arg(args, int);
421 isv = va_arg(args, int);
422 isd = va_arg(args, int);
424 res = snprintf(buf, size, "%04x:%04x", isv, isd);
427 v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
428 d = id_lookup_subsys(a, iv, id, isv, isd);
430 res = snprintf(buf, size, "%s %s", v->name, d->name);
434 res = snprintf(buf, size, "Unknown device %04x:%04x", isv, isd);
436 res = snprintf(buf, size, "%s Unknown device %04x", v->name, isd);
439 case PCI_LOOKUP_CLASS:
440 icls = va_arg(args, int);
442 res = snprintf(buf, size, "%04x", icls);
443 else if (cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0))
445 else if (cls = id_lookup(a, ID_CLASS, icls, 0, 0, 0))
446 res = snprintf(buf, size, "%s [%04x]", cls->name, icls);
448 res = snprintf(buf, size, "Class %04x", icls);
452 case PCI_LOOKUP_PROGIF:
453 icls = va_arg(args, int);
454 ipif = va_arg(args, int);
456 res = snprintf(buf, size, "%02x", ipif);
457 else if (pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0))
459 else if (icls == 0x0101 && !(ipif & 0x70))
461 /* IDE controllers have complex prog-if semantics */
462 res = snprintf(buf, size, "%s%s%s%s%s",
463 (ipif & 0x80) ? "Master " : "",
464 (ipif & 0x08) ? "SecP " : "",
465 (ipif & 0x04) ? "SecO " : "",
466 (ipif & 0x02) ? "PriP " : "",
467 (ipif & 0x01) ? "PriO " : "");
468 if (res > 0 && res < size)
472 res = snprintf(buf, size, "ProgIf %02x", ipif);
477 return "<pci_lookup_name: invalid request>";
479 if (res < 0 || res >= size)
480 return "<pci_lookup_name: buffer too small>";