From 8885505f1dd18a004b647a5f915edfd498911492 Mon Sep 17 00:00:00 2001 From: Thierry Vignaud Date: Fri, 16 Aug 2002 16:03:22 +0000 Subject: - usb.c: allocate mem as pci.c does - usb.c: kill dead code - usb.c: give url to get info on /proc/bus/usb/devices format - usb.c: the /proc/bus/usb/devices really is a state machine; use switch to make it clearer - {usb,pci}.c: r.nb is zeroed in all path, let's do it only time --- Makefile | 2 +- pci.c | 11 +++++------ usb.c | 63 +++++++++++++++++++++++++++++++-------------------------------- 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index ac7d295..d4ffee3 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ includedir = $(prefix)/include binaries = lspcidrake libraries = libldetect.a -CFLAGS = -Wall -W -Os -Wstrict-prototypes -g +CFLAGS = -Wall -W -Wstrict-prototypes -g build: $(binaries) $(libraries) diff --git a/pci.c b/pci.c index 77b8b5a..157b15b 100644 --- a/pci.c +++ b/pci.c @@ -16,6 +16,7 @@ extern struct pciusb_entries pci_probe(int probe_type) { unsigned int id; struct pciusb_entries r; + r.nb = 0; if (!(f = fopen(proc_pci_path, "r"))) { char *err_msg; asprintf(&err_msg, "unable to open \"%s\"\n" @@ -23,12 +24,12 @@ extern struct pciusb_entries pci_probe(int probe_type) { "fopen() sets errno to", proc_pci_path); perror(err_msg); free(err_msg); - r.nb = 0; r.entries = NULL; + r.entries = NULL; return r; } r.entries = malloc(sizeof(struct pciusb_entry) * MAX_DEVICES); - for (r.nb = 0; fgets(buf, sizeof(buf) - 1, f) && r.nb < MAX_DEVICES; r.nb++) { + for (; fgets(buf, sizeof(buf) - 1, f) && r.nb < MAX_DEVICES; r.nb++) { struct pciusb_entry *e = &r.entries[r.nb]; pciusb_initialize(e); @@ -57,10 +58,9 @@ extern struct pciusb_entries pci_probe(int probe_type) { 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 */ + + 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); } @@ -79,4 +79,3 @@ extern struct pciusb_entries pci_probe(int probe_type) { /* should not happen */ exit(1); } - diff --git a/usb.c b/usb.c index 75712b4..7a8f751 100644 --- a/usb.c +++ b/usb.c @@ -14,12 +14,11 @@ extern struct pciusb_entries usb_probe(void) { FILE *f; char buf[BUF_SIZE]; int line; - struct pciusb_entry t[MAX_DEVICES]; struct pciusb_entries r; struct pciusb_entry *e = NULL; - + r.nb = 0; if (access(proc_pci_path, R_OK) != 0) { - r.nb = 0; r.entries = NULL; + r.entries = NULL; return r; } @@ -31,49 +30,48 @@ extern struct pciusb_entries usb_probe(void) { "fopen() sets errno to", proc_usb_path); perror(err_msg); free(err_msg); - } /*else { - asprintf(&err_msg, "unable to open \"%s\"\n" - "You should enable the usb service (as root, type 'service usb start'.\n" - "fopen() sets errno to", proc_usb_path); - perror(err_msg); - free(err_msg); - } */ - r.nb = 0; r.entries = NULL; + } + r.entries = NULL; return r; } - - for(r.nb = 0, line = 1; fgets(buf, sizeof(buf) - 1, f) && r.nb < psizeof(t); line++) { - if (buf[0] == 'T') { - unsigned short pci_bus, pci_device; - e = &t[r.nb++]; + + r.entries = malloc(sizeof(struct pciusb_entry) * MAX_DEVICES); + /* for further information on the format parsed by this state machine, + * read /usr/share/doc/kernel-doc-X.Y.Z/usb/proc_usb_info.txt */ + for(line = 1; fgets(buf, sizeof(buf) - 1, f) && r.nb < MAX_DEVICES; line++) { + + switch (buf[0]) { + case 'T': { + unsigned short pci_bus, pci_device; + e = &r.entries[r.nb++]; pciusb_initialize(e); if (sscanf(buf, "T: Bus=%02hd Lev=%*02d Prnt=%*02d Port=%*02d Cnt=%*02d Dev#=%3hd Spd=%*3s MxCh=%*2d", &pci_bus, &pci_device) == 2) { e->pci_bus = pci_bus; e->pci_device = pci_device; - } else { - fprintf(stderr, "%s %d: unknown ``T'' line\n", proc_usb_path, line); - } - - } else if (buf[0] == 'P') { - unsigned short vendor, device; + } else fprintf(stderr, "%s %d: unknown ``T'' line\n", proc_usb_path, line); + break; + } + case 'P': { + unsigned short vendor, device; if (sscanf(buf, "P: Vendor=%hx ProdID=%hx", &vendor, &device) == 2) { e->vendor = vendor; e->device = device; - } else { - fprintf(stderr, "%s %d: unknown ``P'' line\n", proc_usb_path, line); - } - } else if (e && buf[0] == 'I' && e->class_ == 0) { + } else fprintf(stderr, "%s %d: unknown ``P'' line\n", proc_usb_path, line); + break; + } + case 'I': if (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; + /* see linux/sound/usb/usbaudio.c::usb_audio_ids */ if (e->class_ == (0x1*0x100+ 0x01)) /* USB_AUDIO_CLASS*0x100 + USB_SUBCLASS_AUDIO_CONTROL*/ e->module = "snd-usb-audio"; - } else { - fprintf(stderr, "%s %d: unknown ``I'' line\n", proc_usb_path, line); - } - } else if (e && buf[0] == 'S') { + } else fprintf(stderr, "%s %d: unknown ``I'' line\n", proc_usb_path, line); + break; + } + case 'S': { int offset; char dummy; size_t length = strlen(buf) -1; @@ -83,14 +81,15 @@ extern struct pciusb_entries usb_probe(void) { } else if (sscanf(buf, "S: Product=%n%c", &offset, &dummy) == 1) { if (!e->text) e->text = strdup("Unknown|"); - buf[length - 1] = 0; /* removing '\n' */ + buf[length - 1] = 0; /* removing '\n' */ e->text = realloc(e->text, strlen(e->text) + length-offset + 2); strcat(e->text, buf + offset); } } + } } fclose(f); - r.entries = memdup(t, sizeof(struct pciusb_entry) * r.nb); + realloc(r.entries, sizeof(struct pciusb_entry) * r.nb); pciusb_find_modules(&r, "usbtable", 1 /* no_subid */); return r; -- cgit v1.2.1