2 * $Id: names.c,v 1.3 1998/01/27 11:50:10 mj Exp $
4 * Linux PCI Utilities -- Device ID to Name Translation
6 * Copyright (c) 1997, 1998 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 char *pci_ids = ETC_PCI_IDS;
25 static byte *name_list;
26 static int name_list_loaded;
29 struct nl_entry *next;
36 #define ID1_SUBCLASS -3
39 #define HASH_SIZE 1024
41 static struct nl_entry *nl_hash[HASH_SIZE];
43 static inline unsigned int nl_calc_hash(int id1, int id2)
49 return h & (HASH_SIZE-1);
52 static struct nl_entry *nl_lookup(int id1, int id2)
54 unsigned int h = nl_calc_hash(id1, id2);
55 struct nl_entry *n = nl_hash[h];
57 while (n && (n->id1 != id1 || n->id2 != id2))
62 static int nl_add(int id1, int id2, byte *text)
64 unsigned int h = nl_calc_hash(id1, id2);
65 struct nl_entry *n = nl_hash[h];
67 while (n && (n->id1 != id1 || n->id2 != id2))
71 n = xmalloc(sizeof(struct nl_entry));
81 err_name_list(char *msg)
83 fprintf(stderr, "%s: %s: %m\n", pci_ids, msg);
101 while (*p && *p != '\n')
106 while (*p && *p != '\n')
119 while (r > q && r[-1] == ' ')
124 if (strlen(q) < 5 || q[4] != ' ')
128 if (q[0] == 'C' && q[1] == ' ')
130 if (sscanf(q+2, "%x", &j) != 1)
136 if (sscanf(q, "%x", &j) != 1)
145 if (sscanf(q, "%x", &j) != 1)
147 if (id1 == ID1_ERROR)
149 if (id1 == ID1_CLASS)
164 fprintf(stderr, "%s, line %d: duplicate entry\n", pci_ids, lino);
171 fprintf(stderr, "%s, line %d: parse error\n", pci_ids, lino);
181 fd = open(pci_ids, O_RDONLY);
184 show_numeric_ids = 1;
187 if (fstat(fd, &st) < 0)
188 err_name_list("stat");
189 name_list = xmalloc(st.st_size + 1);
190 if (read(fd, name_list, st.st_size) != st.st_size)
191 err_name_list("read");
192 name_list[st.st_size] = 0;
195 name_list_loaded = 1;
199 lookup_vendor(word i)
201 static char vendbuf[6];
203 if (!show_numeric_ids && !name_list_loaded)
205 if (!show_numeric_ids)
209 e = nl_lookup(ID1_VENDOR, i);
213 sprintf(vendbuf, "%04x", i);
218 lookup_device(word v, word i)
220 static char devbuf[6];
222 if (!show_numeric_ids && !name_list_loaded)
224 if (!show_numeric_ids)
232 sprintf(devbuf, "%04x", i);
237 lookup_device_full(word v, word i)
239 static char fullbuf[256];
241 if (!show_numeric_ids && !name_list_loaded)
243 if (!show_numeric_ids)
245 struct nl_entry *e, *e2;
247 e = nl_lookup(ID1_VENDOR, v);
248 e2 = nl_lookup(v, i);
250 sprintf(fullbuf, "Unknown device %04x:%04x", v, i);
252 sprintf(fullbuf, "%s: Unknown device %04x", e->name, i);
254 sprintf(fullbuf, "%s %s", e->name, e2->name);
257 sprintf(fullbuf, "%04x:%04x", v, i);
264 static char classbuf[80];
266 if (!show_numeric_ids && !name_list_loaded)
268 if (!show_numeric_ids)
272 e = nl_lookup(ID1_SUBCLASS, c);
275 e = nl_lookup(ID1_CLASS, c);
277 sprintf(classbuf, "%s [%04x]", e->name, c);
279 sprintf(classbuf, "Unknown class [%04x]", c);
282 sprintf(classbuf, "Class %04x", c);