2 * $Id: names.c,v 1.2 1997/12/27 18:38:30 mj Exp $
4 * Linux PCI Utilities -- Device ID to Name Translation
6 * Copyright (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
17 #include <linux/pci.h>
23 static byte *name_list;
24 static int name_list_loaded;
27 struct nl_entry *next;
34 #define ID1_SUBCLASS -3
37 #define HASH_SIZE 1024
39 static struct nl_entry *nl_hash[HASH_SIZE];
41 static inline unsigned int nl_calc_hash(int id1, int id2)
47 return h & (HASH_SIZE-1);
50 static struct nl_entry *nl_lookup(int id1, int id2)
52 unsigned int h = nl_calc_hash(id1, id2);
53 struct nl_entry *n = nl_hash[h];
55 while (n && (n->id1 != id1 || n->id2 != id2))
60 static int nl_add(int id1, int id2, byte *text)
62 unsigned int h = nl_calc_hash(id1, id2);
63 struct nl_entry *n = nl_hash[h];
65 while (n && (n->id1 != id1 || n->id2 != id2))
69 n = xmalloc(sizeof(struct nl_entry));
79 err_name_list(char *msg)
81 fprintf(stderr, ETC_PCI_IDS ": %s: %m\n", msg);
99 while (*p && *p != '\n')
104 while (*p && *p != '\n')
117 while (r > q && r[-1] == ' ')
122 if (strlen(q) < 5 || q[4] != ' ')
126 if (q[0] == 'C' && q[1] == ' ')
128 if (sscanf(q+2, "%x", &j) != 1)
134 if (sscanf(q, "%x", &j) != 1)
143 if (sscanf(q, "%x", &j) != 1)
145 if (id1 == ID1_ERROR)
147 if (id1 == ID1_CLASS)
162 fprintf(stderr, ETC_PCI_IDS ", line %d: duplicate entry\n", lino);
169 fprintf(stderr, ETC_PCI_IDS ", line %d: parse error\n", lino);
179 fd = open(ETC_PCI_IDS, O_RDONLY);
182 show_numeric_ids = 1;
185 if (fstat(fd, &st) < 0)
186 err_name_list("stat");
187 name_list = xmalloc(st.st_size + 1);
188 if (read(fd, name_list, st.st_size) != st.st_size)
189 err_name_list("read");
190 name_list[st.st_size] = 0;
193 name_list_loaded = 1;
197 lookup_vendor(word i)
199 static char vendbuf[6];
201 if (!show_numeric_ids && !name_list_loaded)
203 if (!show_numeric_ids)
207 e = nl_lookup(ID1_VENDOR, i);
211 sprintf(vendbuf, "%04x", i);
216 lookup_device(word v, word i)
218 static char devbuf[6];
220 if (!show_numeric_ids && !name_list_loaded)
222 if (!show_numeric_ids)
230 sprintf(devbuf, "%04x", i);
235 lookup_device_full(word v, word i)
237 static char fullbuf[256];
239 if (!show_numeric_ids && !name_list_loaded)
241 if (!show_numeric_ids)
243 struct nl_entry *e, *e2;
245 e = nl_lookup(ID1_VENDOR, v);
246 e2 = nl_lookup(v, i);
248 sprintf(fullbuf, "Unknown device %04x:%04x", v, i);
250 sprintf(fullbuf, "%s: Unknown device %04x", e->name, i);
252 sprintf(fullbuf, "%s %s", e->name, e2->name);
255 sprintf(fullbuf, "%04x:%04x", v, i);
262 static char classbuf[80];
264 if (!show_numeric_ids && !name_list_loaded)
266 if (!show_numeric_ids)
270 e = nl_lookup(ID1_SUBCLASS, c);
273 e = nl_lookup(ID1_CLASS, c);
275 sprintf(classbuf, "%s [%04x]", e->name, c);
277 sprintf(classbuf, "Unknown class [%04x]", c);
280 sprintf(classbuf, "Class %04x", c);