1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
/*
* $Id: names.c,v 1.2.2.1 2005/04/06 23:18:11 stekloff Exp $
*
* The PCI Library -- ID to Name Translation
*
* Copyright (c) 1997--2002 Martin Mares <mj@ucw.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include "names.h"
struct nl_entry {
struct nl_entry *next;
unsigned short id1, id2, id3, id4;
int cat;
unsigned char *name;
};
#define NL_VENDOR 0
#define NL_DEVICE 1
#define NL_SUBSYSTEM 2
#define NL_CLASS 3
#define NL_SUBCLASS 4
#define NL_PROGIF 5
#define HASH_SIZE 1024
static inline unsigned int nl_calc_hash(int cat, int id1, int id2, int id3, int id4)
{
unsigned int h;
h = id1 ^ id2 ^ id3 ^ id4 ^ (cat << 5);
h += (h >> 6);
return h & (HASH_SIZE-1);
}
static struct nl_entry *nl_lookup(struct pci_access *a, int num, int cat, int id1, int id2, int id3, int id4)
{
unsigned int h;
struct nl_entry *n;
if (num)
return NULL;
h = nl_calc_hash(cat, id1, id2, id3, id4);
n = a->nl_hash[h];
while (n && (n->id1 != id1 || n->id2 != id2 || n->id3 != id3 || n->id4 != id4 || n->cat != cat))
n = n->next;
return n;
}
static int nl_add(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, unsigned char *text)
{
unsigned int h = nl_calc_hash(cat, id1, id2, id3, id4);
struct nl_entry *n = a->nl_hash[h];
while (n && (n->id1 != id1 || n->id2 != id2 || n->id3 != id3 || n->id4 != id4 || n->cat != cat))
n = n->next;
if (n)
return 1;
n = malloc(sizeof(struct nl_entry));
bzero(n, sizeof(struct nl_entry));
n->id1 = id1;
n->id2 = id2;
n->id3 = id3;
n->id4 = id4;
n->cat = cat;
n->name = text;
n->next = a->nl_hash[h];
a->nl_hash[h] = n;
return 0;
}
static void
err_name_list(struct pci_access *a, unsigned char *msg)
{
fprintf(stderr, "%s: %s: %s\n", a->pci_id_file_name, msg, strerror(errno));
}
static void
parse_name_list(struct pci_access *a)
{
unsigned char *p = a->nl_list;
unsigned char *q, *r;
int lino = 0;
unsigned int id1=0, id2=0, id3=0, id4=0;
int cat = -1;
while (*p)
{
lino++;
q = p;
while (*p && *p != '\n')
p++;
if (*p == '\n')
*p++ = 0;
if (!*q || *q == '#')
continue;
r = p;
while (r > q && r[-1] == ' ')
*--r = 0;
r = q;
while (*q == '\t')
q++;
if (q == r)
{
if (q[0] == 'C' && q[1] == ' ')
{
if (strlen(q+2) < 3 ||
q[4] != ' ' ||
sscanf(q+2, "%x", &id1) != 1)
goto parserr;
cat = NL_CLASS;
}
else
{
if (strlen(q) < 5 ||
q[4] != ' ' ||
sscanf(q, "%x", &id1) != 1)
goto parserr;
cat = NL_VENDOR;
}
id2 = id3 = id4 = 0;
q += 4;
}
else if (q == r+1)
switch (cat)
{
case NL_VENDOR:
case NL_DEVICE:
case NL_SUBSYSTEM:
if (sscanf(q, "%x", &id2) != 1 || q[4] != ' ')
goto parserr;
q += 5;
cat = NL_DEVICE;
id3 = id4 = 0;
break;
case NL_CLASS:
case NL_SUBCLASS:
case NL_PROGIF:
if (sscanf(q, "%x", &id2) != 1 || q[2] != ' ')
goto parserr;
q += 3;
cat = NL_SUBCLASS;
id3 = id4 = 0;
break;
default:
goto parserr;
}
else if (q == r+2)
switch (cat)
{
case NL_DEVICE:
case NL_SUBSYSTEM:
if (sscanf(q, "%x%x", &id3, &id4) != 2 || q[9] != ' ')
goto parserr;
q += 10;
cat = NL_SUBSYSTEM;
break;
case NL_CLASS:
case NL_SUBCLASS:
case NL_PROGIF:
if (sscanf(q, "%x", &id3) != 1 || q[2] != ' ')
goto parserr;
q += 3;
cat = NL_PROGIF;
id4 = 0;
break;
default:
goto parserr;
}
else
goto parserr;
while (*q == ' ')
q++;
if (!*q)
goto parserr;
if (nl_add(a, cat, id1, id2, id3, id4, q))
fprintf(stderr, "%s, line %d: duplicate entry", a->pci_id_file_name, lino);
}
return;
parserr:
fprintf(stderr, "%s, line %d: parse error", a->pci_id_file_name, lino);
}
static void
load_name_list(struct pci_access *a)
{
int fd;
struct stat st;
fd = open(a->pci_id_file_name, O_RDONLY);
if (fd < 0)
{
a->numeric_ids = 1;
return;
}
if (fstat(fd, &st) < 0)
err_name_list(a, "stat");
a->nl_list = malloc(st.st_size + 1);
if (read(fd, a->nl_list, st.st_size) != st.st_size)
err_name_list(a, "read");
a->nl_list[st.st_size] = 0;
a->nl_hash = malloc(sizeof(struct nl_entry *) * HASH_SIZE);
bzero(a->nl_hash, sizeof(struct nl_entry *) * HASH_SIZE);
parse_name_list(a);
close(fd);
}
void
pci_free_name_list(struct pci_access *a)
{
int i = 0;
struct nl_entry *n = NULL, *temp = NULL;
free(a->nl_list);
a->nl_list = NULL;
if (a->nl_hash != NULL) {
for (i = 0; i < HASH_SIZE; i++) {
if (a->nl_hash[i] != NULL) {
n = a->nl_hash[i];
do {
temp = n->next;
free (n);
n = temp;
} while (temp != NULL);
}
}
}
free(a->nl_hash);
a->nl_hash = NULL;
}
unsigned char *
pci_lookup_name(struct pci_access *a, unsigned char *buf, int size, int flags, unsigned int arg1, unsigned int arg2, unsigned int arg3, unsigned int arg4)
{
int num = a->numeric_ids;
int res;
struct nl_entry *n;
if (flags & PCI_LOOKUP_NUMERIC)
{
flags &= PCI_LOOKUP_NUMERIC;
num = 1;
}
if (!a->nl_hash && !num)
{
load_name_list(a);
num = a->numeric_ids;
}
switch (flags)
{
case PCI_LOOKUP_VENDOR:
if ((n = nl_lookup(a, num, NL_VENDOR, arg1, 0, 0, 0)))
return n->name;
else
res = snprintf(buf, size, "%04x", arg1);
break;
case PCI_LOOKUP_DEVICE:
if ((n = nl_lookup(a, num, NL_DEVICE, arg1, arg2, 0, 0)))
return n->name;
else
res = snprintf(buf, size, "%04x", arg2);
break;
case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
if (!num)
{
struct nl_entry *e, *e2;
e = nl_lookup(a, 0, NL_VENDOR, arg1, 0, 0, 0);
e2 = nl_lookup(a, 0, NL_DEVICE, arg1, arg2, 0, 0);
if (!e)
res = snprintf(buf, size, "Unknown device %04x:%04x", arg1, arg2);
else if (!e2)
res = snprintf(buf, size, "%s: Unknown device %04x", e->name, arg2);
else
res = snprintf(buf, size, "%s %s", e->name, e2->name);
}
else
res = snprintf(buf, size, "%04x:%04x", arg1, arg2);
break;
case PCI_LOOKUP_VENDOR | PCI_LOOKUP_SUBSYSTEM:
if ((n = nl_lookup(a, num, NL_VENDOR, arg3, 0, 0, 0)))
return n->name;
else
res = snprintf(buf, size, "%04x", arg2);
break;
case PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
if ((n = nl_lookup(a, num, NL_SUBSYSTEM, arg1, arg2, arg3, arg4)))
return n->name;
else if (arg1 == arg3 && arg2 == arg4 && (n = nl_lookup(a, num, NL_DEVICE, arg1, arg2, 0, 0)))
return n->name;
else
res = snprintf(buf, size, "%04x", arg4);
break;
case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
if (!num)
{
struct nl_entry *e, *e2;
e = nl_lookup(a, 0, NL_VENDOR, arg3, 0, 0, 0);
e2 = nl_lookup(a, 0, NL_SUBSYSTEM, arg1, arg2, arg3, arg4);
if (!e2 && arg1 == arg3 && arg2 == arg4)
/* Cheat for vendors blindly setting subsystem ID same as device ID */
e2 = nl_lookup(a, 0, NL_DEVICE, arg1, arg2, 0, 0);
if (!e)
res = snprintf(buf, size, "Unknown device %04x:%04x", arg3, arg4);
else if (!e2)
res = snprintf(buf, size, "%s: Unknown device %04x", e->name, arg4);
else
res = snprintf(buf, size, "%s %s", e->name, e2->name);
}
else
res = snprintf(buf, size, "%04x:%04x", arg3, arg4);
break;
case PCI_LOOKUP_CLASS:
if ((n = nl_lookup(a, num, NL_SUBCLASS, arg1 >> 8, arg1 & 0xff, 0, 0)))
return n->name;
else if ((n = nl_lookup(a, num, NL_CLASS, arg1, 0, 0, 0)))
res = snprintf(buf, size, "%s [%04x]", n->name, arg1);
else
res = snprintf(buf, size, "Class %04x", arg1);
break;
case PCI_LOOKUP_PROGIF:
if ((n = nl_lookup(a, num, NL_PROGIF, arg1 >> 8, arg1 & 0xff, arg2, 0)))
return n->name;
if (arg1 == 0x0101)
{
/* IDE controllers have complex prog-if semantics */
if (arg2 & 0x70)
return NULL;
res = snprintf(buf, size, "%s%s%s%s%s",
(arg2 & 0x80) ? "Master " : "",
(arg2 & 0x08) ? "SecP " : "",
(arg2 & 0x04) ? "SecO " : "",
(arg2 & 0x02) ? "PriP " : "",
(arg2 & 0x01) ? "PriO " : "");
if (res)
buf[--res] = 0;
break;
}
return NULL;
default:
return "<pci_lookup_name: invalid request>";
}
if (res == size)
return "<too-large>";
else
return buf;
}
|