]>
git.ipfire.org Git - thirdparty/pciutils.git/blob - lib/names.c
2 * $Id: names.c,v 1.8 2002/03/30 15:13:06 mj Exp $
4 * The PCI Library -- ID to Name Translation
6 * Copyright (c) 1997--2002 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
22 struct nl_entry
*next
;
23 word id1
, id2
, id3
, id4
;
30 #define NL_SUBSYSTEM 2
35 #define HASH_SIZE 1024
37 static inline unsigned int nl_calc_hash(int cat
, int id1
, int id2
, int id3
, int id4
)
41 h
= id1
^ id2
^ id3
^ id4
^ (cat
<< 5);
43 return h
& (HASH_SIZE
-1);
46 static struct nl_entry
*nl_lookup(struct pci_access
*a
, int num
, int cat
, int id1
, int id2
, int id3
, int id4
)
53 h
= nl_calc_hash(cat
, id1
, id2
, id3
, id4
);
55 while (n
&& (n
->id1
!= id1
|| n
->id2
!= id2
|| n
->id3
!= id3
|| n
->id4
!= id4
|| n
->cat
!= cat
))
60 static int nl_add(struct pci_access
*a
, int cat
, int id1
, int id2
, int id3
, int id4
, byte
*text
)
62 unsigned int h
= nl_calc_hash(cat
, id1
, id2
, id3
, id4
);
63 struct nl_entry
*n
= a
->nl_hash
[h
];
65 while (n
&& (n
->id1
!= id1
|| n
->id2
!= id2
|| n
->id3
!= id3
|| n
->id4
!= id4
|| n
->cat
!= cat
))
69 n
= pci_malloc(a
, sizeof(struct nl_entry
));
76 n
->next
= a
->nl_hash
[h
];
82 err_name_list(struct pci_access
*a
, char *msg
)
84 a
->error("%s: %s: %s\n", a
->id_file_name
, msg
, strerror(errno
));
88 parse_name_list(struct pci_access
*a
)
93 unsigned int id1
=0, id2
=0, id3
=0, id4
=0;
100 while (*p
&& *p
!= '\n')
104 if (!*q
|| *q
== '#')
107 while (r
> q
&& r
[-1] == ' ')
114 if (q
[0] == 'C' && q
[1] == ' ')
116 if (strlen(q
+2) < 3 ||
118 sscanf(q
+2, "%x", &id1
) != 1)
126 sscanf(q
, "%x", &id1
) != 1)
139 if (sscanf(q
, "%x", &id2
) != 1 || q
[4] != ' ')
148 if (sscanf(q
, "%x", &id2
) != 1 || q
[2] != ' ')
162 if (sscanf(q
, "%x%x", &id3
, &id4
) != 2 || q
[9] != ' ')
170 if (sscanf(q
, "%x", &id3
) != 1 || q
[2] != ' ')
185 if (nl_add(a
, cat
, id1
, id2
, id3
, id4
, q
))
186 a
->error("%s, line %d: duplicate entry", a
->id_file_name
, lino
);
191 a
->error("%s, line %d: parse error", a
->id_file_name
, lino
);
195 load_name_list(struct pci_access
*a
)
200 fd
= open(a
->id_file_name
, O_RDONLY
);
206 if (fstat(fd
, &st
) < 0)
207 err_name_list(a
, "stat");
208 a
->nl_list
= pci_malloc(a
, st
.st_size
+ 1);
209 if (read(fd
, a
->nl_list
, st
.st_size
) != st
.st_size
)
210 err_name_list(a
, "read");
211 a
->nl_list
[st
.st_size
] = 0;
212 a
->nl_hash
= pci_malloc(a
, sizeof(struct nl_entry
*) * HASH_SIZE
);
213 bzero(a
->nl_hash
, sizeof(struct nl_entry
*) * HASH_SIZE
);
219 pci_free_name_list(struct pci_access
*a
)
221 pci_mfree(a
->nl_list
);
223 pci_mfree(a
->nl_hash
);
228 pci_lookup_name(struct pci_access
*a
, char *buf
, int size
, int flags
, u32 arg1
, u32 arg2
, u32 arg3
, u32 arg4
)
230 int num
= a
->numeric_ids
;
234 if (flags
& PCI_LOOKUP_NUMERIC
)
236 flags
&= PCI_LOOKUP_NUMERIC
;
239 if (!a
->nl_hash
&& !num
)
242 num
= a
->numeric_ids
;
246 case PCI_LOOKUP_VENDOR
:
247 if (n
= nl_lookup(a
, num
, NL_VENDOR
, arg1
, 0, 0, 0))
250 res
= snprintf(buf
, size
, "%04x", arg1
);
252 case PCI_LOOKUP_DEVICE
:
253 if (n
= nl_lookup(a
, num
, NL_DEVICE
, arg1
, arg2
, 0, 0))
256 res
= snprintf(buf
, size
, "%04x", arg2
);
258 case PCI_LOOKUP_VENDOR
| PCI_LOOKUP_DEVICE
:
261 struct nl_entry
*e
, *e2
;
262 e
= nl_lookup(a
, 0, NL_VENDOR
, arg1
, 0, 0, 0);
263 e2
= nl_lookup(a
, 0, NL_DEVICE
, arg1
, arg2
, 0, 0);
265 res
= snprintf(buf
, size
, "Unknown device %04x:%04x", arg1
, arg2
);
267 res
= snprintf(buf
, size
, "%s: Unknown device %04x", e
->name
, arg2
);
269 res
= snprintf(buf
, size
, "%s %s", e
->name
, e2
->name
);
272 res
= snprintf(buf
, size
, "%04x:%04x", arg1
, arg2
);
274 case PCI_LOOKUP_VENDOR
| PCI_LOOKUP_SUBSYSTEM
:
275 if (n
= nl_lookup(a
, num
, NL_VENDOR
, arg3
, 0, 0, 0))
278 res
= snprintf(buf
, size
, "%04x", arg2
);
280 case PCI_LOOKUP_DEVICE
| PCI_LOOKUP_SUBSYSTEM
:
281 if (n
= nl_lookup(a
, num
, NL_SUBSYSTEM
, arg1
, arg2
, arg3
, arg4
))
283 else if (arg1
== arg3
&& arg2
== arg4
&& (n
= nl_lookup(a
, num
, NL_DEVICE
, arg1
, arg2
, 0, 0)))
286 res
= snprintf(buf
, size
, "%04x", arg4
);
288 case PCI_LOOKUP_VENDOR
| PCI_LOOKUP_DEVICE
| PCI_LOOKUP_SUBSYSTEM
:
291 struct nl_entry
*e
, *e2
;
292 e
= nl_lookup(a
, 0, NL_VENDOR
, arg3
, 0, 0, 0);
293 e2
= nl_lookup(a
, 0, NL_SUBSYSTEM
, arg1
, arg2
, arg3
, arg4
);
294 if (!e2
&& arg1
== arg3
&& arg2
== arg4
)
295 /* Cheat for vendors blindly setting subsystem ID same as device ID */
296 e2
= nl_lookup(a
, 0, NL_DEVICE
, arg1
, arg2
, 0, 0);
298 res
= snprintf(buf
, size
, "Unknown device %04x:%04x", arg3
, arg4
);
300 res
= snprintf(buf
, size
, "%s: Unknown device %04x", e
->name
, arg4
);
302 res
= snprintf(buf
, size
, "%s %s", e
->name
, e2
->name
);
305 res
= snprintf(buf
, size
, "%04x:%04x", arg3
, arg4
);
307 case PCI_LOOKUP_CLASS
:
308 if (n
= nl_lookup(a
, num
, NL_SUBCLASS
, arg1
>> 8, arg1
& 0xff, 0, 0))
310 else if (n
= nl_lookup(a
, num
, NL_CLASS
, arg1
, 0, 0, 0))
311 res
= snprintf(buf
, size
, "%s [%04x]", n
->name
, arg1
);
313 res
= snprintf(buf
, size
, "Class %04x", arg1
);
315 case PCI_LOOKUP_PROGIF
:
316 if (n
= nl_lookup(a
, num
, NL_PROGIF
, arg1
>> 8, arg1
& 0xff, arg2
, 0))
320 /* IDE controllers have complex prog-if semantics */
323 res
= snprintf(buf
, size
, "%s%s%s%s%s",
324 (arg2
& 0x80) ? "Master " : "",
325 (arg2
& 0x08) ? "SecP " : "",
326 (arg2
& 0x04) ? "SecO " : "",
327 (arg2
& 0x02) ? "PriP " : "",
328 (arg2
& 0x01) ? "PriO " : "");
335 return "<pci_lookup_name: invalid request>";
337 return (res
== size
) ? "<too-large>" : buf
;