2 * The PCI Library -- ID to Name Translation
4 * Copyright (c) 1997--2002 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
20 struct nl_entry *next;
21 word id1, id2, id3, id4;
28 #define NL_SUBSYSTEM 2
33 #define HASH_SIZE 1024
35 static inline unsigned int nl_calc_hash(int cat, int id1, int id2, int id3, int id4)
39 h = id1 ^ id2 ^ id3 ^ id4 ^ (cat << 5);
41 return h & (HASH_SIZE-1);
44 static struct nl_entry *nl_lookup(struct pci_access *a, int num, int cat, int id1, int id2, int id3, int id4)
51 h = nl_calc_hash(cat, id1, id2, id3, id4);
53 while (n && (n->id1 != id1 || n->id2 != id2 || n->id3 != id3 || n->id4 != id4 || n->cat != cat))
58 static int nl_add(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, byte *text)
60 unsigned int h = nl_calc_hash(cat, id1, id2, id3, id4);
61 struct nl_entry *n = a->nl_hash[h];
63 while (n && (n->id1 != id1 || n->id2 != id2 || n->id3 != id3 || n->id4 != id4 || n->cat != cat))
67 n = pci_malloc(a, sizeof(struct nl_entry));
74 n->next = a->nl_hash[h];
80 err_name_list(struct pci_access *a, char *msg)
82 a->error("%s: %s: %s\n", a->id_file_name, msg, strerror(errno));
86 parse_name_list(struct pci_access *a)
91 unsigned int id1=0, id2=0, id3=0, id4=0;
98 while (*p && *p != '\n')
102 if (!*q || *q == '#')
105 while (r > q && r[-1] == ' ')
112 if (q[0] == 'C' && q[1] == ' ')
114 if (strlen(q+2) < 3 ||
116 sscanf(q+2, "%x", &id1) != 1)
124 sscanf(q, "%x", &id1) != 1)
137 if (sscanf(q, "%x", &id2) != 1 || q[4] != ' ')
146 if (sscanf(q, "%x", &id2) != 1 || q[2] != ' ')
160 if (sscanf(q, "%x%x", &id3, &id4) != 2 || q[9] != ' ')
168 if (sscanf(q, "%x", &id3) != 1 || q[2] != ' ')
183 if (nl_add(a, cat, id1, id2, id3, id4, q))
184 a->error("%s, line %d: duplicate entry", a->id_file_name, lino);
189 a->error("%s, line %d: parse error", a->id_file_name, lino);
193 load_name_list(struct pci_access *a)
198 fd = open(a->id_file_name, O_RDONLY);
204 if (fstat(fd, &st) < 0)
205 err_name_list(a, "stat");
206 a->nl_list = pci_malloc(a, st.st_size + 1);
207 if (read(fd, a->nl_list, st.st_size) != st.st_size)
208 err_name_list(a, "read");
209 a->nl_list[st.st_size] = 0;
210 a->nl_hash = pci_malloc(a, sizeof(struct nl_entry *) * HASH_SIZE);
211 bzero(a->nl_hash, sizeof(struct nl_entry *) * HASH_SIZE);
217 pci_free_name_list(struct pci_access *a)
219 pci_mfree(a->nl_list);
221 pci_mfree(a->nl_hash);
226 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
228 int num = a->numeric_ids;
232 if (flags & PCI_LOOKUP_NUMERIC)
234 flags &= PCI_LOOKUP_NUMERIC;
237 if (!a->nl_hash && !num)
240 num = a->numeric_ids;
244 case PCI_LOOKUP_VENDOR:
245 if (n = nl_lookup(a, num, NL_VENDOR, arg1, 0, 0, 0))
248 res = snprintf(buf, size, "%04x", arg1);
250 case PCI_LOOKUP_DEVICE:
251 if (n = nl_lookup(a, num, NL_DEVICE, arg1, arg2, 0, 0))
254 res = snprintf(buf, size, "%04x", arg2);
256 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
259 struct nl_entry *e, *e2;
260 e = nl_lookup(a, 0, NL_VENDOR, arg1, 0, 0, 0);
261 e2 = nl_lookup(a, 0, NL_DEVICE, arg1, arg2, 0, 0);
263 res = snprintf(buf, size, "Unknown device %04x:%04x", arg1, arg2);
265 res = snprintf(buf, size, "%s: Unknown device %04x", e->name, arg2);
267 res = snprintf(buf, size, "%s %s", e->name, e2->name);
270 res = snprintf(buf, size, "%04x:%04x", arg1, arg2);
272 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_SUBSYSTEM:
273 if (n = nl_lookup(a, num, NL_VENDOR, arg3, 0, 0, 0))
276 res = snprintf(buf, size, "%04x", arg3);
278 case PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
279 if (n = nl_lookup(a, num, NL_SUBSYSTEM, arg1, arg2, arg3, arg4))
281 else if (arg1 == arg3 && arg2 == arg4 && (n = nl_lookup(a, num, NL_DEVICE, arg1, arg2, 0, 0)))
284 res = snprintf(buf, size, "%04x", arg4);
286 case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
289 struct nl_entry *e, *e2;
290 e = nl_lookup(a, 0, NL_VENDOR, arg3, 0, 0, 0);
291 e2 = nl_lookup(a, 0, NL_SUBSYSTEM, arg1, arg2, arg3, arg4);
292 if (!e2 && arg1 == arg3 && arg2 == arg4)
293 /* Cheat for vendors blindly setting subsystem ID same as device ID */
294 e2 = nl_lookup(a, 0, NL_DEVICE, arg1, arg2, 0, 0);
296 res = snprintf(buf, size, "Unknown device %04x:%04x", arg3, arg4);
298 res = snprintf(buf, size, "%s: Unknown device %04x", e->name, arg4);
300 res = snprintf(buf, size, "%s %s", e->name, e2->name);
303 res = snprintf(buf, size, "%04x:%04x", arg3, arg4);
305 case PCI_LOOKUP_CLASS:
306 if (n = nl_lookup(a, num, NL_SUBCLASS, arg1 >> 8, arg1 & 0xff, 0, 0))
308 else if (n = nl_lookup(a, num, NL_CLASS, arg1, 0, 0, 0))
309 res = snprintf(buf, size, "%s [%04x]", n->name, arg1);
311 res = snprintf(buf, size, "Class %04x", arg1);
313 case PCI_LOOKUP_PROGIF:
314 if (n = nl_lookup(a, num, NL_PROGIF, arg1 >> 8, arg1 & 0xff, arg2, 0))
318 /* IDE controllers have complex prog-if semantics */
321 res = snprintf(buf, size, "%s%s%s%s%s",
322 (arg2 & 0x80) ? "Master " : "",
323 (arg2 & 0x08) ? "SecP " : "",
324 (arg2 & 0x04) ? "SecO " : "",
325 (arg2 & 0x02) ? "PriP " : "",
326 (arg2 & 0x01) ? "PriO " : "");
333 return "<pci_lookup_name: invalid request>";
335 return (res == size) ? "<too-large>" : buf;