]> mj.ucw.cz Git - pciutils.git/blob - lib/names.c
0a88d62c0a91befe0caca91db065902975003bce
[pciutils.git] / lib / names.c
1 /*
2  *      The PCI Library -- ID to Name Translation
3  *
4  *      Copyright (c) 1997--2005 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #include "internal.h"
16
17 struct id_entry {
18   struct id_entry *next;
19   u32 id12, id34;
20   byte cat;
21   byte name[1];
22 };
23
24 enum id_entry_type {
25   ID_UNKNOWN,
26   ID_VENDOR,
27   ID_DEVICE,
28   ID_SUBSYSTEM,
29   ID_GEN_SUBSYSTEM,
30   ID_CLASS,
31   ID_SUBCLASS,
32   ID_PROGIF
33 };
34
35 struct id_bucket {
36   struct id_bucket *next;
37   unsigned int full;
38 };
39
40 #define MAX_LINE 1024
41 #define BUCKET_SIZE 8192
42 #define HASH_SIZE 4099
43
44 #ifdef __GNUC__FIXME
45 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
46 #else
47 union id_align {
48   struct id_bucket *next;
49   unsigned int full;
50 };
51 #define BUCKET_ALIGNMENT sizeof(union id_align)
52 #endif
53 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
54
55 static void *id_alloc(struct pci_access *a, unsigned int size)
56 {
57   struct id_bucket *buck = a->current_id_bucket;
58   unsigned int pos;
59   if (!buck || buck->full + size > BUCKET_SIZE)
60     {
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));
65     }
66   pos = buck->full;
67   buck->full = BUCKET_ALIGN(buck->full + size);
68   return (byte *)buck + pos;
69 }
70
71 static inline u32 id_pair(unsigned int x, unsigned int y)
72 {
73   return ((x << 16) | y);
74 }
75
76 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
77 {
78   unsigned int h;
79
80   h = id12 ^ (id34 << 3) ^ (cat << 5);
81   return h % HASH_SIZE;
82 }
83
84 static struct id_entry *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
85 {
86   struct id_entry *n;
87   u32 id12 = id_pair(id1, id2);
88   u32 id34 = id_pair(id3, id4);
89
90   n = a->id_hash[id_hash(cat, id12, id34)];
91   while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
92     n = n->next;
93   return n;
94 }
95
96 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, byte *text)
97 {
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);
103
104   while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
105     n = n->next;
106   if (n)
107     return 1;
108   n = id_alloc(a, sizeof(struct id_entry) + len);
109   n->id12 = id12;
110   n->id34 = id34;
111   n->cat = cat;
112   memcpy(n->name, text, len+1);
113   n->next = a->id_hash[h];
114   a->id_hash[h] = n;
115   return 0;
116 }
117
118 static int id_hex(byte *p, int cnt)
119 {
120   int x = 0;
121   while (cnt--)
122     {
123       x <<= 4;
124       if (*p >= '0' && *p <= '9')
125         x += (*p - '0');
126       else if (*p >= 'a' && *p <= 'f')
127         x += (*p - 'a' + 10);
128       else if (*p >= 'A' && *p <= 'F')
129         x += (*p - 'A' + 10);
130       else
131         return -1;
132       p++;
133     }
134   return x;
135 }
136
137 static inline int id_white_p(int c)
138 {
139   return (c == ' ') || (c == '\t');
140 }
141
142 static const char *id_parse_list(struct pci_access *a, FILE *f, int *lino)
143 {
144   byte line[MAX_LINE];
145   byte *p;
146   int id1=0, id2=0, id3=0, id4=0;
147   int cat = -1;
148   int nest;
149   static const char parse_error[] = "Parse error";
150
151   *lino = 0;
152   while (fgets(line, sizeof(line), f))
153     {
154       (*lino)++;
155       p = line;
156       while (*p && *p != '\n' && *p != '\r')
157         p++;
158       if (!*p && !feof(f))
159         return "Line too long";
160       *p = 0;
161       if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
162         *--p = 0;
163
164       p = line;
165       while (id_white_p(*p))
166         p++;
167       if (!*p || *p == '#')
168         continue;
169
170       p = line;
171       while (*p == '\t')
172         p++;
173       nest = p - line;
174
175       if (!nest)                                        /* Top-level entries */
176         {
177           if (p[0] == 'C' && p[1] == ' ')               /* Class block */
178             {
179               if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
180                 return parse_error;
181               cat = ID_CLASS;
182               p += 5;
183             }
184           else if (p[0] == 'S' && p[1] == ' ')
185             {                                           /* Generic subsystem block */
186               if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
187                 return parse_error;
188               if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
189                 return "Vendor does not exist";
190               cat = ID_GEN_SUBSYSTEM;
191               continue;
192             }
193           else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
194             {                                           /* Unrecognized block (RFU) */
195               cat = ID_UNKNOWN;
196               continue;
197             }
198           else                                          /* Vendor ID */
199             {
200               if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
201                 return parse_error;
202               cat = ID_VENDOR;
203               p += 5;
204             }
205           id2 = id3 = id4 = 0;
206         }
207       else if (cat == ID_UNKNOWN)                       /* Nested entries in RFU blocks are skipped */
208         continue;
209       else if (nest == 1)                               /* Nesting level 1 */
210         switch (cat)
211           {
212           case ID_VENDOR:
213           case ID_DEVICE:
214           case ID_SUBSYSTEM:
215             if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
216               return parse_error;
217             p += 5;
218             cat = ID_DEVICE;
219             id3 = id4 = 0;
220             break;
221           case ID_GEN_SUBSYSTEM:
222             if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
223               return parse_error;
224             p += 5;
225             id3 = id4 = 0;
226             break;
227           case ID_CLASS:
228           case ID_SUBCLASS:
229           case ID_PROGIF:
230             if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
231               return parse_error;
232             p += 3;
233             cat = ID_SUBCLASS;
234             id3 = id4 = 0;
235             break;
236           default:
237             return parse_error;
238           }
239       else if (nest == 2)                               /* Nesting level 2 */
240         switch (cat)
241           {
242           case ID_DEVICE:
243           case ID_SUBSYSTEM:
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]))
245               return parse_error;
246             p += 10;
247             cat = ID_SUBSYSTEM;
248             break;
249           case ID_CLASS:
250           case ID_SUBCLASS:
251           case ID_PROGIF:
252             if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
253               return parse_error;
254             p += 3;
255             cat = ID_PROGIF;
256             id4 = 0;
257             break;
258           default:
259             return parse_error;
260           }
261       else                                              /* Nesting level 3 or more */
262         return parse_error;
263       while (id_white_p(*p))
264         p++;
265       if (!*p)
266         return parse_error;
267       if (id_insert(a, cat, id1, id2, id3, id4, p))
268         return "Duplicate entry";
269     }
270   return NULL;
271 }
272
273 int
274 pci_load_name_list(struct pci_access *a)
275 {
276   FILE *f;
277   int lino;
278   const char *err;
279
280   pci_free_name_list(a);
281   if (!(f = fopen(a->id_file_name, "r")))
282     return 0;
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))
287     err = "I/O error";
288   fclose(f);
289   if (err)
290     a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
291   return 1;
292 }
293
294 void
295 pci_free_name_list(struct pci_access *a)
296 {
297   pci_mfree(a->id_hash);
298   a->id_hash = NULL;
299   while (a->current_id_bucket)
300     {
301       struct id_bucket *buck = a->current_id_bucket;
302       a->current_id_bucket = buck->next;
303       pci_mfree(buck);
304     }
305 }
306
307 static struct id_entry *id_lookup_subsys(struct pci_access *a, int iv, int id, int isv, int isd)
308 {
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);
316   return d;
317 }
318
319 char *
320 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
321 {
322   va_list args;
323   int num, res, synth;
324   struct id_entry *v, *d, *cls, *pif;
325   int iv, id, isv, isd, icls, ipif;
326
327   va_start(args, flags);
328
329   num = 0;
330   if ((flags & PCI_LOOKUP_NUMERIC) || a->numeric_ids)
331     {
332       flags &= ~PCI_LOOKUP_NUMERIC;
333       num = 1;
334     }
335   else if (!a->id_hash)
336     {
337       if (!pci_load_name_list(a))
338         num = a->numeric_ids = 1;
339     }
340
341   if (flags & PCI_LOOKUP_NO_NUMBERS)
342     {
343       flags &= ~PCI_LOOKUP_NO_NUMBERS;
344       synth = 0;
345       if (num)
346         return NULL;
347     }
348   else
349     synth = 1;
350
351   switch (flags)
352     {
353     case PCI_LOOKUP_VENDOR:
354       iv = va_arg(args, int);
355       if (num)
356         res = snprintf(buf, size, "%04x", iv);
357       else if (v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0))
358         return v->name;
359       else
360         res = snprintf(buf, size, "Unknown vendor %04x", iv);
361       break;
362     case PCI_LOOKUP_DEVICE:
363       iv = va_arg(args, int);
364       id = va_arg(args, int);
365       if (num)
366         res = snprintf(buf, size, "%04x", id);
367       else if (d = id_lookup(a, ID_DEVICE, iv, id, 0, 0))
368         return d->name;
369       else if (synth)
370         res = snprintf(buf, size, "Unknown device %04x", id);
371       else
372         return NULL;
373       break;
374     case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
375       iv = va_arg(args, int);
376       id = va_arg(args, int);
377       if (num)
378         res = snprintf(buf, size, "%04x:%04x", iv, id);
379       else
380         {
381           v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
382           d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
383           if (v && d)
384             res = snprintf(buf, size, "%s %s", v->name, d->name);
385           else if (!synth)
386             return NULL;
387           else if (!v)
388             res = snprintf(buf, size, "Unknown device %04x:%04x", iv, id);
389           else  /* !d */
390             res = snprintf(buf, size, "%s Unknown device %04x", v->name, id);
391         }
392       break;
393     case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
394       isv = va_arg(args, int);
395       if (num)
396         res = snprintf(buf, size, "%04x", isv);
397       else if (v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0))
398         return v->name;
399       else if (synth)
400         res = snprintf(buf, size, "Unknown vendor %04x", isv);
401       else
402         return NULL;
403       break;
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);
409       if (num)
410         res = snprintf(buf, size, "%04x", isd);
411       else if (d = id_lookup_subsys(a, iv, id, isv, isd))
412         return d->name;
413       else if (synth)
414         res = snprintf(buf, size, "Unknown device %04x", isd);
415       else
416         return NULL;
417       break;
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);
423       if (num)
424         res = snprintf(buf, size, "%04x:%04x", isv, isd);
425       else
426         {
427           v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
428           d = id_lookup_subsys(a, iv, id, isv, isd);
429           if (v && d)
430             res = snprintf(buf, size, "%s %s", v->name, d->name);
431           else if (!synth)
432             return NULL;
433           else if (!v)
434             res = snprintf(buf, size, "Unknown device %04x:%04x", isv, isd);
435           else /* !d */
436             res = snprintf(buf, size, "%s Unknown device %04x", v->name, isd);
437         }
438       break;
439     case PCI_LOOKUP_CLASS:
440       icls = va_arg(args, int);
441       if (num)
442         res = snprintf(buf, size, "%04x", icls);
443       else if (cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0))
444         return cls->name;
445       else if (cls = id_lookup(a, ID_CLASS, icls, 0, 0, 0))
446         res = snprintf(buf, size, "%s [%04x]", cls->name, icls);
447       else if (synth)
448         res = snprintf(buf, size, "Class %04x", icls);
449       else
450         return NULL;
451       break;
452     case PCI_LOOKUP_PROGIF:
453       icls = va_arg(args, int);
454       ipif = va_arg(args, int);
455       if (num)
456         res = snprintf(buf, size, "%02x", ipif);
457       else if (pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0))
458         return pif->name;
459       else if (icls == 0x0101 && !(ipif & 0x70))
460         {
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)
469             buf[--res] = 0;
470         }
471       else if (synth)
472         res = snprintf(buf, size, "ProgIf %02x", ipif);
473       else
474         return NULL;
475       break;
476     default:
477       return "<pci_lookup_name: invalid request>";
478     }
479   if (res < 0 || res >= size)
480     return "<pci_lookup_name: buffer too small>";
481   else
482     return buf;
483 }