From 66471ddcec217a08d2017a375cd82d33f2e073f8 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 10 Jan 2022 19:08:45 -0600 Subject: [PATCH] Let lsusb to use /etc/usb.ids for english descriptions. --- toys/other/lsusb.c | 80 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/toys/other/lsusb.c b/toys/other/lsusb.c index 031dbd93..6df21aec 100644 --- a/toys/other/lsusb.c +++ b/toys/other/lsusb.c @@ -13,30 +13,52 @@ config LSUSB List USB hosts/devices. */ +#define FOR_lsusb #include "toys.h" +GLOBALS( + void *ids; +) + +struct ids { + struct ids *next, *child; + int id; + char name[]; +}; + static int list_device(struct dirtree *new) { FILE *file; char *name; - int busnum = 0, devnum = 0, pid = 0, vid = 0; + struct ids *ids; + int busnum = 0, devnum = 0, pid = 0, vid = 0, count = 0; if (!new->parent) return DIRTREE_RECURSE; if (new->name[0] == '.') return 0; - name = dirtree_path(new, 0); - sprintf(toybuf, "%s/uevent", name); - file = fopen(toybuf, "r"); - if (file) { - int count = 0; - - while (fgets(toybuf, sizeof(toybuf), file)) - if (sscanf(toybuf, "BUSNUM=%u\n", &busnum) - || sscanf(toybuf, "DEVNUM=%u\n", &devnum) - || sscanf(toybuf, "PRODUCT=%x/%x/", &pid, &vid)) count++; - - if (count == 3) - printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid); - fclose(file); + + // Read data from proc file + sprintf(toybuf, "%s/uevent", name = dirtree_path(new, 0)); + if (!(file = fopen(toybuf, "r"))) return 0; + while (fgets(toybuf, sizeof(toybuf), file)) + if (sscanf(toybuf, "BUSNUM=%u\n", &busnum) + || sscanf(toybuf, "DEVNUM=%u\n", &devnum) + || sscanf(toybuf, "PRODUCT=%x/%x/", &pid, &vid)) count++; + fclose(file); + + // Output with any matching ids data + if (count == 3) { + printf("Bus %03d Device %03d: ID %04x:%04x", busnum, devnum, pid, vid); + for (ids = TT.ids; ids; ids = ids->next) { + if (pid != ids->id) continue; + printf("%s", ids->name); + for (ids = ids->child; ids; ids = ids->next) { + if (vid != ids->id) continue; + printf("%s", ids->name); + break; + } + break; + } + xputc('\n'); } free(name); @@ -45,5 +67,33 @@ static int list_device(struct dirtree *new) void lsusb_main(void) { + int fd; + + // Parse http://www.linux-usb.org/usb.ids file (if available) + if (-1 != (fd = open("/etc/usb.ids", O_RDONLY))) { + FILE *fp = fdopen(fd, "r"); + char *s, *ss; + struct ids *ids, *tids; + + while ((s = xgetline(fp))) { + fd = estrtol(s, &ss, 16); + if (ss == s+4+(*s=='\t') && *ss++==' ') { + ids = xmalloc(sizeof(*ids)+strlen(ss)+1); + ids->child = 0; + ids->id = fd; + strcpy(ids->name, ss); + if (!TT.ids || *s!='\t') { + ids->next = TT.ids; + TT.ids = ids; + } else { + tids = TT.ids; + ids->next = tids->child; + tids->child = ids; + } + } + free(s); + } + fclose(fp); + } dirtree_read("/sys/bus/usb/devices/", list_device); } -- 2.39.2