diff options
-rw-r--r-- | lspcidrake.c | 102 | ||||
-rw-r--r-- | pci.c | 100 | ||||
-rw-r--r-- | pciusb.c | 217 | ||||
-rw-r--r-- | usb.c | 85 |
4 files changed, 253 insertions, 251 deletions
diff --git a/lspcidrake.c b/lspcidrake.c index 753badd..9056707 100644 --- a/lspcidrake.c +++ b/lspcidrake.c @@ -7,72 +7,72 @@ int verboze = 0; int full_probe = 0; void usage(void) { - printf("usage: lspcidrake [-v] [-f]\n"); - exit(0); + printf("usage: lspcidrake [-v] [-f]\n"); + exit(0); } void print_name(struct pciusb_entry *e) { - printf("%-16s: ", e->module ? e->module : "unknown"); - if (e->text) - printf(e->text); - else - printf("unknown (%04x/%04x/%04x/%04x)", e->vendor, e->device, e->subvendor, e->subdevice); + printf("%-16s: ", e->module ? e->module : "unknown"); + if (e->text) + printf(e->text); + else printf("unknown (%04x/%04x/%04x/%04x)", e->vendor, e->device, e->subvendor, e->subdevice); } void print_id(struct pciusb_entry *e) { - if (verboze && e->text) { - printf(" (vendor:%04x device:%04x", e->vendor, e->device); - if (e->subvendor != 0xffff || e->subdevice != 0xffff) - printf(" subv:%04x subd:%04x", e->subvendor, e->subdevice); - printf(")"); - } + if (verboze && e->text) { + printf(" (vendor:%04x device:%04x", e->vendor, e->device); + if (e->subvendor != 0xffff || e->subdevice != 0xffff) + printf(" subv:%04x subd:%04x", e->subvendor, e->subdevice); + printf(")"); + } } void pci_printit(struct pciusb_entries *entries) { - int i; - for (i = 0; i < entries->nb; i++) { - struct pciusb_entry *e = &entries->entries[i]; - print_name(e); - if (e->class_) { - const char *class_ = pci_class2text(e->class_); - if (strcmp(class_, "NOT_DEFINED") != 0) printf(" [%s]", class_); - } - print_id(e); - printf("\n"); - } + int i; + for (i = 0; i < entries->nb; i++) { + struct pciusb_entry *e = &entries->entries[i]; + print_name(e); + if (e->class_) { + const char *class_ = pci_class2text(e->class_); + if (strcmp(class_, "NOT_DEFINED") != 0) printf(" [%s]", class_); + } + print_id(e); + printf("\n"); + } } void usb_printit(struct pciusb_entries *entries) { - int i; - for (i = 0; i < entries->nb; i++) { - struct pciusb_entry *e = &entries->entries[i]; - print_name(e); - if (e->class_) printf(" [%s]", usb_class2text(e->class_)); - print_id(e); - printf("\n"); - } + int i; + for (i = 0; i < entries->nb; i++) { + struct pciusb_entry *e = &entries->entries[i]; + print_name(e); + if (e->class_) + printf(" [%s]", usb_class2text(e->class_)); + print_id(e); + printf("\n"); + } } int main(int argc, char **argv) { - char ** ptr = argv; - while (ptr && *ptr) { - if (!strcmp(*ptr, "-h") || !strcmp(*ptr, "--help")) - usage(); - if (!strcmp(*ptr, "-v")) { - verboze = 1; - full_probe = 1; - } - if (!strcmp(*ptr, "-f")) - full_probe = 1; - ptr++; - } + char ** ptr = argv; + while (ptr && *ptr) { + if (!strcmp(*ptr, "-h") || !strcmp(*ptr, "--help")) + usage(); + if (!strcmp(*ptr, "-v")) { + verboze = 1; + full_probe = 1; + } + if (!strcmp(*ptr, "-f")) + full_probe = 1; + ptr++; + } - struct pciusb_entries entries = pci_probe(full_probe); - pci_printit(&entries); - pciusb_free(&entries); + struct pciusb_entries entries = pci_probe(full_probe); + pci_printit(&entries); + pciusb_free(&entries); - entries = usb_probe(); - usb_printit(&entries); - pciusb_free(&entries); - exit(0); + entries = usb_probe(); + usb_printit(&entries); + pciusb_free(&entries); + exit(0); } @@ -7,68 +7,68 @@ #include "common.h" static int pci_find_modules(struct pciusb_entries entries, int no_subid) { - return pciusb_find_modules(entries, "pcitable", no_subid); + return pciusb_find_modules(entries, "pcitable", no_subid); } extern struct pciusb_entries pci_probe(int probe_type) { - FILE *f; - char buf[512]; - unsigned short devbusfn; - unsigned int id; - struct pciusb_entry t[100]; - struct pciusb_entries r; + FILE *f; + char buf[512]; + unsigned short devbusfn; + unsigned int id; + struct pciusb_entry t[100]; + struct pciusb_entries r; - if (!(f = fopen("/proc/bus/pci/devices", "r"))) exit(1); + if (!(f = fopen("/proc/bus/pci/devices", "r"))) exit(1); - for (r.nb = 0; fgets(buf, sizeof(buf) - 1, f) && r.nb < psizeof(t); r.nb++) { - struct pciusb_entry e; - pciusb_initialize(&e); + for (r.nb = 0; fgets(buf, sizeof(buf) - 1, f) && r.nb < psizeof(t); r.nb++) { + struct pciusb_entry e; + pciusb_initialize(&e); - sscanf(buf, "%hx %x", &devbusfn, &id); - e.vendor = id >> 16; - e.device = id & 0xffff; - e.pci_bus = devbusfn >> 8; - e.pci_device = (devbusfn & 0xff) >> 3; - e.pci_function = (devbusfn & 0xff) & 0x07; - { - char file[25]; - FILE *f; - snprintf(file, sizeof(file), "/proc/bus/pci/%02x/%02x.%d", e.pci_bus, e.pci_device, e.pci_function); - if (probe_type == 1 && (f = fopen(file, "r"))) { - if (fseek(f, 10, SEEK_SET) == 0) - fread(&e.class_, 2, 1, f); - if (fseek(f, 0x2c, SEEK_SET) == 0) - fread(&e.subvendor, 2, 1, f), fread(&e.subdevice, 2, 1, f); + sscanf(buf, "%hx %x", &devbusfn, &id); + e.vendor = id >> 16; + e.device = id & 0xffff; + e.pci_bus = devbusfn >> 8; + e.pci_device = (devbusfn & 0xff) >> 3; + e.pci_function = (devbusfn & 0xff) & 0x07; + { + char file[25]; + FILE *f; + snprintf(file, sizeof(file), "/proc/bus/pci/%02x/%02x.%d", e.pci_bus, e.pci_device, e.pci_function); + if (probe_type == 1 && (f = fopen(file, "r"))) { + if (fseek(f, 10, SEEK_SET) == 0) + fread(&e.class_, 2, 1, f); + if (fseek(f, 0x2c, SEEK_SET) == 0) + fread(&e.subvendor, 2, 1, f), fread(&e.subdevice, 2, 1, f); - if ((e.subvendor == 0 && e.subdevice == 0) || - (e.subvendor == e.vendor && e.subdevice == e.device)) { - e.subvendor = 0xffff; - e.subdevice = 0xffff; - } + if ((e.subvendor == 0 && e.subdevice == 0) || + (e.subvendor == e.vendor && e.subdevice == e.device)) { + e.subvendor = 0xffff; + e.subdevice = 0xffff; + } - if (fseek(f, 9, SEEK_SET) == 0) { - unsigned char class_prog = 0; - fread(&class_prog, 1, 1, f); - if (e.class_ == PCI_CLASS_SERIAL_USB) { - /* taken from kudzu's pci.c */ - e.module = strdup(class_prog == 0 ? "usb-uhci" : "usb-ohci"); - } - } + if (fseek(f, 9, SEEK_SET) == 0) { + unsigned char class_prog = 0; + fread(&class_prog, 1, 1, f); + if (e.class_ == PCI_CLASS_SERIAL_USB) { + /* taken from kudzu's pci.c */ + e.module = strdup(class_prog == 0 ? "usb-uhci" : "usb-ohci"); + } + } + fclose(f); + } + } + t[r.nb] = e; + } fclose(f); - } - } - t[r.nb] = e; - } - fclose(f); - r.entries = memdup(t, sizeof(struct pciusb_entry) * r.nb); + r.entries = memdup(t, sizeof(struct pciusb_entry) * r.nb); - if (pci_find_modules(r, probe_type)) return r; + if (pci_find_modules(r, probe_type)) return r; - /* ok, let's try again with subids */ - if (probe_type == 0) return pci_probe(1); + /* ok, let's try again with subids */ + if (probe_type == 0) return pci_probe(1); - /* should not happen */ - exit(1); + /* should not happen */ + exit(1); } @@ -14,128 +14,129 @@ #include "common.h" typedef struct { - FILE *f; - pid_t pid; + FILE *f; + pid_t pid; } fh; static fh fh_open(char *fname) { - fh ret; - int length = strlen(fname); - char *fname_gz = alloca(length + sizeof(".gz")); - sprintf(fname_gz, "%s.gz", fname); - - if (access(fname, R_OK) == 0) { - ret.f = fopen(fname, "r"); - ret.pid = 0; - } else if (access(fname_gz, R_OK) == 0) { - int fdno[2]; - if (pipe(fdno)) { perror("pciusb"); exit(1); } - if ((ret.pid = fork()) != 0) { - ret.f = fdopen(fdno[0], "r"); - close(fdno[1]); - } else { - char *cmd[8]; - int ip = 0; - char *ld_loader = getenv("LD_LOADER"); - - if (ld_loader && *ld_loader) { - cmd[ip++] = ld_loader; - } - cmd[ip++] = "gzip"; - cmd[ip++] = "-dc"; - cmd[ip++] = fname_gz; - cmd[ip++] = NULL; - - dup2(fdno[1], STDOUT_FILENO); - close(fdno[0]); - close(fdno[1]); - execvp(cmd[0], cmd); - perror("pciusb"); exit(2); - } - } else { - fprintf(stderr, "Missing pciusbtable (should be %s)\n", fname); - exit(1); - } - return ret; + fh ret; + int length = strlen(fname); + char *fname_gz = alloca(length + sizeof(".gz")); + sprintf(fname_gz, "%s.gz", fname); + + if (access(fname, R_OK) == 0) { + ret.f = fopen(fname, "r"); + ret.pid = 0; + } else if (access(fname_gz, R_OK) == 0) { + int fdno[2]; + if (pipe(fdno)) { perror("pciusb"); exit(1); } + if ((ret.pid = fork()) != 0) { + ret.f = fdopen(fdno[0], "r"); + close(fdno[1]); + } else { + char *cmd[8]; + int ip = 0; + char *ld_loader = getenv("LD_LOADER"); + + if (ld_loader && *ld_loader) { + cmd[ip++] = ld_loader; + } + cmd[ip++] = "gzip"; + cmd[ip++] = "-dc"; + cmd[ip++] = fname_gz; + cmd[ip++] = NULL; + + dup2(fdno[1], STDOUT_FILENO); + close(fdno[0]); + close(fdno[1]); + execvp(cmd[0], cmd); + perror("pciusb"); exit(2); + } + } else { + fprintf(stderr, "Missing pciusbtable (should be %s)\n", fname); + exit(1); + } + return ret; } static void fh_close(fh f) { - fclose(f.f); - if (f.pid > 0) waitpid(f.pid, NULL, 0); + fclose(f.f); + if (f.pid > 0) + waitpid(f.pid, NULL, 0); } extern int pciusb_find_modules(struct pciusb_entries entries, const char *fpciusbtable, int no_subid) { - fh f; - char buf[2048]; - int line; - - char *share_path = getenv("SHARE_PATH"); - char *fname; - if (!share_path || !*share_path) share_path = "/usr/share"; - - fname = alloca(strlen(share_path) + sizeof("/ldetect-lst/") + strlen(fpciusbtable)); - sprintf(fname, "%s/ldetect-lst/%s", share_path, fpciusbtable); - - f = fh_open(fname); - - for (line = 1; fgets(buf, sizeof(buf) - 1, f.f); line++) { - unsigned short vendor, device, subvendor, subdevice; - int offset, i; - int nb = sscanf(buf, "0x%hx\t0x%hx\t0x%hx\t0x%hx\t%n", &vendor, &device, &subvendor, &subdevice, &offset); - if (nb != 4) { - nb = sscanf(buf, "0x%hx\t0x%hx\t%n", &vendor, &device, &offset); - if (nb != 2) { - if (buf[0] != '#') fprintf(stderr, "%s %d: bad line\n", fpciusbtable, line); - continue; - } - } - for (i = 0; i < entries.nb; i++) { - struct pciusb_entry *e = &entries.entries[i]; - if (vendor == e->vendor && device == e->device) { - if (nb == 4 && e->subvendor == 0xffff && e->subdevice == 0xffff && !no_subid) { - pciusb_free(&entries); - fh_close(f); - return 0; /* leave, let the caller call again with subids */ - } - - if ((nb != 4 || (subvendor == e->subvendor && subdevice == e->subdevice)) && !e->module) { - char *p = strdupa(buf + offset + 1); - char *q = strchr(p, '\t'); - if (q) { - q[-1] = 0; - q[strlen(q)-2] = 0; - ifree(e->module); ifree(e->text); - e->module = strcmp(p, "unknown") ? strdup(p) : NULL; - e->text = strdup(q+2); - } + fh f; + char buf[2048]; + int line; + + char *share_path = getenv("SHARE_PATH"); + char *fname; + if (!share_path || !*share_path) share_path = "/usr/share"; + + fname = alloca(strlen(share_path) + sizeof("/ldetect-lst/") + strlen(fpciusbtable)); + sprintf(fname, "%s/ldetect-lst/%s", share_path, fpciusbtable); + + f = fh_open(fname); + + for (line = 1; fgets(buf, sizeof(buf) - 1, f.f); line++) { + unsigned short vendor, device, subvendor, subdevice; + int offset, i; + int nb = sscanf(buf, "0x%hx\t0x%hx\t0x%hx\t0x%hx\t%n", &vendor, &device, &subvendor, &subdevice, &offset); + if (nb != 4) { + nb = sscanf(buf, "0x%hx\t0x%hx\t%n", &vendor, &device, &offset); + if (nb != 2) { + if (buf[0] != '#') + fprintf(stderr, "%s %d: bad line\n", fpciusbtable, line); + continue; + } + } + for (i = 0; i < entries.nb; i++) { + struct pciusb_entry *e = &entries.entries[i]; + if (vendor == e->vendor && device == e->device) { + if (nb == 4 && e->subvendor == 0xffff && e->subdevice == 0xffff && !no_subid) { + pciusb_free(&entries); + fh_close(f); + return 0; /* leave, let the caller call again with subids */ + } + + if ((nb != 4 || (subvendor == e->subvendor && subdevice == e->subdevice)) && !e->module) { + char *p = strdupa(buf + offset + 1); + char *q = strchr(p, '\t'); + if (q) { + q[-1] = 0; + q[strlen(q)-2] = 0; + ifree(e->module); ifree(e->text); + e->module = strcmp(p, "unknown") ? strdup(p) : NULL; + e->text = strdup(q+2); + } + } + } + } } - } - } - } - fh_close(f); - return 1; + fh_close(f); + return 1; } extern void pciusb_initialize(struct pciusb_entry *e) { - e->vendor = 0xffff; - e->device = 0xffff; - e->subvendor = 0xffff; - e->subdevice = 0xffff; - e->class_ = 0; - e->pci_bus = 0xffff; - e->pci_device = 0xffff; - e->pci_function = 0xffff; - e->module = NULL; - e->text = NULL; + e->vendor = 0xffff; + e->device = 0xffff; + e->subvendor = 0xffff; + e->subdevice = 0xffff; + e->class_ = 0; + e->pci_bus = 0xffff; + e->pci_device = 0xffff; + e->pci_function = 0xffff; + e->module = NULL; + e->text = NULL; } extern void pciusb_free(struct pciusb_entries *entries) { - int i; - for (i = 0; i < entries->nb; i++) { - struct pciusb_entry *e = &entries->entries[i]; - ifree(e->module); - ifree(e->text); - } - ifree(entries->entries); + int i; + for (i = 0; i < entries->nb; i++) { + struct pciusb_entry *e = &entries->entries[i]; + ifree(e->module); + ifree(e->text); + } + ifree(entries->entries); } - @@ -6,54 +6,55 @@ #include "common.h" static int usb_find_modules(struct pciusb_entries entries) { - return pciusb_find_modules(entries, "usbtable", 1 /* no_subid */); + return pciusb_find_modules(entries, "usbtable", 1 /* no_subid */); } extern struct pciusb_entries usb_probe(void) { - FILE *f; - char buf[512]; - int line; - const char *file = "/proc/bus/usb/devices"; - struct pciusb_entry t[100]; - struct pciusb_entries r; - struct pciusb_entry *e = NULL; + FILE *f; + char buf[512]; + int line; + const char *file = "/proc/bus/usb/devices"; + struct pciusb_entry t[100]; + struct pciusb_entries r; + struct pciusb_entry *e = NULL; - if (!(f = fopen(file, "r"))) exit(1); + if (!(f = fopen(file, "r"))) + exit(1); - for(r.nb = line = 0; fgets(buf, sizeof(buf) - 1, f) && r.nb < psizeof(t); line++) { - if (buf[0] == 'P') { - e = &t[r.nb++]; - pciusb_initialize(e); + for(r.nb = line = 0; fgets(buf, sizeof(buf) - 1, f) && r.nb < psizeof(t); line++) { + if (buf[0] == 'P') { + e = &t[r.nb++]; + pciusb_initialize(e); - if (sscanf(buf, "P: Vendor=%hx ProdID=%hx", &e->vendor, &e->device) != 2) { - fprintf(stderr, "%s %d: unknown ``P'' line\n", file, line); - pciusb_initialize(e); - } - } else if (e && buf[0] == 'I' && e->class_ == 0) { - int class_, sub, prot = 0; - if (sscanf(buf, "I: If#=%*2d Alt=%*2d #EPs=%*2d Cls=%02x(%*5c) Sub=%02x Prot=%02x", &class_, &sub, &prot) == 3) { - e->class_ = (class_ * 0x100 + sub) * 0x100 + prot; - } else { - fprintf(stderr, "%s %d: unknown ``I'' line\n", file, line); - } - } else if (e && buf[0] == 'S') { - int offset; - char dummy; - if (sscanf(buf, "S: Manufacturer=%n%c", &offset, &dummy) == 1) { - buf[strlen(buf) - 1] = '|'; /* replacing '\n' by '|' */ - e->text = strdup(buf + offset); - } else if (sscanf(buf, "S: Product=%n%c", &offset, &dummy) == 1) { - if (!e->text) e->text = strdup("Unknown|"); - buf[strlen(buf) - 1] = 0; /* removing '\n' */ - e->text = realloc(e->text, strlen(e->text) + strlen(buf + offset) + 1); - strcat(e->text, buf + offset); - } - } - } - fclose(f); - r.entries = memdup(t, sizeof(struct pciusb_entry) * r.nb); + if (sscanf(buf, "P: Vendor=%hx ProdID=%hx", &e->vendor, &e->device) != 2) { + fprintf(stderr, "%s %d: unknown ``P'' line\n", file, line); + pciusb_initialize(e); + } + } else if (e && buf[0] == 'I' && e->class_ == 0) { + int class_, sub, prot = 0; + if (sscanf(buf, "I: If#=%*2d Alt=%*2d #EPs=%*2d Cls=%02x(%*5c) Sub=%02x Prot=%02x", &class_, &sub, &prot) == 3) { + e->class_ = (class_ * 0x100 + sub) * 0x100 + prot; + } else { + fprintf(stderr, "%s %d: unknown ``I'' line\n", file, line); + } + } else if (e && buf[0] == 'S') { + int offset; + char dummy; + if (sscanf(buf, "S: Manufacturer=%n%c", &offset, &dummy) == 1) { + buf[strlen(buf) - 1] = '|'; /* replacing '\n' by '|' */ + e->text = strdup(buf + offset); + } else if (sscanf(buf, "S: Product=%n%c", &offset, &dummy) == 1) { + if (!e->text) e->text = strdup("Unknown|"); + buf[strlen(buf) - 1] = 0; /* removing '\n' */ + e->text = realloc(e->text, strlen(e->text) + strlen(buf + offset) + 1); + strcat(e->text, buf + offset); + } + } + } + fclose(f); + r.entries = memdup(t, sizeof(struct pciusb_entry) * r.nb); - usb_find_modules(r); - return r; + usb_find_modules(r); + return r; } |