2
* The PCI Library -- ID to Name Translation
4
* Copyright (c) 1997--2005 Martin Mares <mj@ucw.cz>
6
* May 8, 2006 - Modified and ported to HelenOS by Jakub Jermar.
8
* Can be freely distributed and used under the terms of the GNU GPL.
21
struct id_entry *next;
39
struct id_bucket *next;
44
#define BUCKET_SIZE 8192
45
#define HASH_SIZE 4099
48
#define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
51
struct id_bucket *next;
54
#define BUCKET_ALIGNMENT sizeof(union id_align)
56
#define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
58
static void *id_alloc(struct pci_access *a, unsigned int size)
60
struct id_bucket *buck = a->current_id_bucket;
62
if (!buck || buck->full + size > BUCKET_SIZE) {
63
buck = pci_malloc(a, BUCKET_SIZE);
64
buck->next = a->current_id_bucket;
65
a->current_id_bucket = buck;
66
buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
69
buck->full = BUCKET_ALIGN(buck->full + size);
70
return (byte *) buck + pos;
73
static inline u32 id_pair(unsigned int x, unsigned int y)
75
return ((x << 16) | y);
78
static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
82
h = id12 ^ (id34 << 3) ^ (cat << 5);
86
static struct id_entry *id_lookup(struct pci_access *a, int cat, int id1,
87
int id2, int id3, int id4)
90
u32 id12 = id_pair(id1, id2);
91
u32 id34 = id_pair(id3, id4);
93
n = a->id_hash[id_hash(cat, id12, id34)];
94
while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
99
static int id_insert(struct pci_access *a, int cat, int id1, int id2,
100
int id3, int id4, byte * text)
102
u32 id12 = id_pair(id1, id2);
103
u32 id34 = id_pair(id3, id4);
104
unsigned int h = id_hash(cat, id12, id34);
105
struct id_entry *n = a->id_hash[h];
106
int len = str_size((char *) text);
108
while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
112
n = id_alloc(a, sizeof(struct id_entry) + len);
116
memcpy(n->name, text, len + 1);
117
n->next = a->id_hash[h];
122
static int id_hex(byte * p, int cnt)
127
if (*p >= '0' && *p <= '9')
129
else if (*p >= 'a' && *p <= 'f')
130
x += (*p - 'a' + 10);
131
else if (*p >= 'A' && *p <= 'F')
132
x += (*p - 'A' + 10);
140
static inline int id_white_p(int c)
142
return (c == ' ') || (c == '\t');
145
static const char *id_parse_list(struct pci_access *a, int *lino)
149
int id1 = 0, id2 = 0, id3 = 0, id4 = 0;
152
static const char parse_error[] = "Parse error";
156
for (i = 0; i < sizeof(pci_ids) / sizeof(char *); i++) {
157
line = (byte *) pci_ids[i];
162
if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
166
while (id_white_p(*p))
168
if (!*p || *p == '#')
176
if (!nest) { /* Top-level entries */
177
if (p[0] == 'C' && p[1] == ' ') { /* Class block */
178
if ((id1 = id_hex(p + 2, 2)) < 0 || !id_white_p(p[4]))
182
} else if (p[0] == 'S' && p[1] == ' ') { /* Generic subsystem block */
183
if ((id1 = id_hex(p + 2, 4)) < 0 || p[6])
185
if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
186
return "Vendor does not exist";
187
cat = ID_GEN_SUBSYSTEM;
189
} else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ') { /* Unrecognized block (RFU) */
192
} else { /* Vendor ID */
194
if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
200
} else if (cat == ID_UNKNOWN) /* Nested entries in RFU blocks are skipped */
202
else if (nest == 1) /* Nesting level 1 */
207
if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
213
case ID_GEN_SUBSYSTEM:
214
if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
222
if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
230
} else if (nest == 2) /* Nesting level 2 */
234
if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4])
235
|| (id4 = id_hex(p + 5, 4)) < 0 || !id_white_p(p[9]))
243
if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
251
} else /* Nesting level 3 or more */
253
while (id_white_p(*p))
257
if (id_insert(a, cat, id1, id2, id3, id4, p))
258
return "Duplicate entry";
263
int pci_load_name_list(struct pci_access *a)
268
pci_free_name_list(a);
269
a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
270
bzero(a->id_hash, sizeof(struct id_entry *) * HASH_SIZE);
271
err = id_parse_list(a, &lino);
273
a->error("%s at %s, element %d\n", err, "pci_ids.h", lino);
277
void pci_free_name_list(struct pci_access *a)
279
pci_mfree(a->id_hash);
281
while (a->current_id_bucket) {
282
struct id_bucket *buck = a->current_id_bucket;
283
a->current_id_bucket = buck->next;
288
static struct id_entry *id_lookup_subsys(struct pci_access *a, int iv,
289
int id, int isv, int isd)
291
struct id_entry *d = NULL;
292
if (iv > 0 && id > 0) /* Per-device lookup */
293
d = id_lookup(a, ID_SUBSYSTEM, iv, id, isv, isd);
294
if (!d) /* Generic lookup */
295
d = id_lookup(a, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
296
if (!d && iv == isv && id == isd) /* Check for subsystem == device */
297
d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
301
char *pci_lookup_name(struct pci_access *a, char *buf, int size, int flags,
306
struct id_entry *v, *d, *cls, *pif;
307
int iv, id, isv, isd, icls, ipif;
309
va_start(args, flags);
312
if ((flags & PCI_LOOKUP_NUMERIC) || a->numeric_ids) {
313
flags &= ~PCI_LOOKUP_NUMERIC;
315
} else if (!a->id_hash) {
316
if (!pci_load_name_list(a))
317
num = a->numeric_ids = 1;
320
if (flags & PCI_LOOKUP_NO_NUMBERS) {
321
flags &= ~PCI_LOOKUP_NO_NUMBERS;
329
case PCI_LOOKUP_VENDOR:
330
iv = va_arg(args, int);
332
res = snprintf(buf, size, "%04x", iv);
333
else if ((v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0)) != 0)
334
return (char *) v->name;
336
res = snprintf(buf, size, "Unknown vendor %04x", iv);
338
case PCI_LOOKUP_DEVICE:
339
iv = va_arg(args, int);
340
id = va_arg(args, int);
342
res = snprintf(buf, size, "%04x", id);
343
else if ((d = id_lookup(a, ID_DEVICE, iv, id, 0, 0)) != 0)
344
return (char *) d->name;
346
res = snprintf(buf, size, "Unknown device %04x", id);
350
case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
351
iv = va_arg(args, int);
352
id = va_arg(args, int);
354
res = snprintf(buf, size, "%04x:%04x", iv, id);
356
v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
357
d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
359
res = snprintf(buf, size, "%s %s", v->name,
364
res = snprintf(buf, size, "Unknown device %04x:%04x", iv, id);
366
res = snprintf(buf, size, "%s Unknown device %04x", v->name, id);
369
case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
370
isv = va_arg(args, int);
372
res = snprintf(buf, size, "%04x", isv);
373
else if ((v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0)) != 0)
374
return (char *) v->name;
376
res = snprintf(buf, size, "Unknown vendor %04x", isv);
380
case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
381
iv = va_arg(args, int);
382
id = va_arg(args, int);
383
isv = va_arg(args, int);
384
isd = va_arg(args, int);
386
res = snprintf(buf, size, "%04x", isd);
387
else if ((d = id_lookup_subsys(a, iv, id, isv, isd)) != 0)
388
return (char *) d->name;
390
res = snprintf(buf, size, "Unknown device %04x", isd);
394
case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
395
iv = va_arg(args, int);
396
id = va_arg(args, int);
397
isv = va_arg(args, int);
398
isd = va_arg(args, int);
400
res = snprintf(buf, size, "%04x:%04x", isv, isd);
402
v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
403
d = id_lookup_subsys(a, iv, id, isv, isd);
405
res = snprintf(buf, size, "%s %s", v->name, d->name);
409
res = snprintf(buf, size, "Unknown device %04x:%04x", isv, isd);
411
res = snprintf(buf, size, "%s Unknown device %04x", v->name, isd);
414
case PCI_LOOKUP_CLASS:
415
icls = va_arg(args, int);
417
res = snprintf(buf, size, "%04x", icls);
418
else if ((cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0)) != 0)
419
return (char *) cls->name;
420
else if ((cls = id_lookup(a, ID_CLASS, icls, 0, 0, 0)) != 0)
421
res = snprintf(buf, size, "%s [%04x]", cls->name, icls);
423
res = snprintf(buf, size, "Class %04x", icls);
427
case PCI_LOOKUP_PROGIF:
428
icls = va_arg(args, int);
429
ipif = va_arg(args, int);
431
res = snprintf(buf, size, "%02x", ipif);
432
else if ((pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0)) != 0)
433
return (char *) pif->name;
434
else if (icls == 0x0101 && !(ipif & 0x70)) {
435
/* IDE controllers have complex prog-if semantics */
436
res = snprintf(buf, size, "%s%s%s%s%s",
437
(ipif & 0x80) ? "Master " : "",
438
(ipif & 0x08) ? "SecP " : "",
439
(ipif & 0x04) ? "SecO " : "",
440
(ipif & 0x02) ? "PriP " : "",
441
(ipif & 0x01) ? "PriO " : "");
442
if (res > 0 && res < size)
445
res = snprintf(buf, size, "ProgIf %02x", ipif);
450
return "<pci_lookup_name: invalid request>";
452
if (res < 0 || res >= size)
453
return "<pci_lookup_name: buffer too small>";