landley / toybox

toybox
http://landley.net/toybox
BSD Zero Clause License
2.37k stars 331 forks source link

feature request: print manufacturer and and product in lsusb #385

Open apprehensions opened 1 year ago

apprehensions commented 1 year ago
diff --git a/util-linux/lsusb.c b/util-linux/lsusb.c
index e27aa7f31..b7b7abb99 100644
--- a/util-linux/lsusb.c
+++ b/util-linux/lsusb.c
@@ -15,7 +15,16 @@
 //config:  system and devices connected to them.
 //config:
 //config:  This version uses sysfs (/sys/bus/usb/devices) only.
-
+//config:
+//config:config FEATURE_LSUSB_STRINGS
+//config:  bool "Print vendor and product strings if they exist"
+//config:  default y
+//config:  depends on LSUSB
+//config:  help
+//config:  lsusb is a utility for displaying information about USB busses in the
+//config:  system and devices connected to them. This option prints out the
+//config:  vendor and product strings if they are available. This option
+//config:  replicates a simpler version of util-linux lsusb output.
 //applet:IF_LSUSB(APPLET_NOEXEC(lsusb, lsusb, BB_DIR_USR_BIN, BB_SUID_DROP, lsusb))

 //kbuild:lib-$(CONFIG_LSUSB) += lsusb.o
@@ -37,6 +46,16 @@ static int FAST_FUNC fileAction(
    int product_vid = 0, product_did = 0;
    char *uevent_filename = concat_path_file(fileName, "/uevent");

+#if ENABLE_FEATURE_LSUSB_STRINGS
+   ssize_t vlen, plen;
+   // max length standard allows, any longer and someones being naughty
+   char vendor[256] = { 0 }, product[256] = { 0 };
+   vlen = open_read_close(concat_path_file(fileName, "/manufacturer"), vendor, sizeof(vendor));
+   vendor[vlen - 1] = '\0';
+   plen = open_read_close(concat_path_file(fileName, "/product"), product, sizeof(product));
+   product[plen - 1] = '\0';
+#endif
+
    parser = config_open2(uevent_filename, fopen_for_read);
    free(uevent_filename);

@@ -64,7 +83,12 @@ static int FAST_FUNC fileAction(
    config_close(parser);

    if (busnum) {
-       printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
+       printf("Bus %s Device %s: ID %04x:%04x", busnum, devnum, product_vid, product_did);
+#if ENABLE_FEATURE_LSUSB_STRINGS
+       if(vlen) printf(" %s", vendor);
+       if(plen) printf(" %s", product);
+#endif
+       printf("\n");
        free(busnum);
        free(devnum);
    }

i have been trying to convert this into toybox for lsusb as a patch for my personal use, but never figured it out.

landley commented 1 year ago

In theory support's been built in to toybox since January? See mailing list thread http://lists.landley.net/pipermail/toybox-landley.net/2022-February/012782.html and commit 66471ddcec21 and so on.

The descriptor files come from https://pci-ids.ucw.cz/v2.2/pci.ids.gz and http://www.linux-usb.org/usb.ids respectively, wget should pull them just fine. You can stick them (with or without .gz extension, in which case it'll run it through zcat) anywhere in the search path /etc:/vendor:/usr/share/misc

apprehensions commented 1 year ago

why not simply retrieve them from the manufacterer and vendor files as seen in the busybox patch?

apprehensions commented 1 year ago

on another note, http://www.linux-usb.org/usb.ids doesn't change the output of lsusb.

0 $ ~/.local/src/toybox/toybox lsusb -i usb.ids  
   Bus 001 Device 007: ID 1038:12b3 
 Bus 001 Device 010: ID 8087:0aaa 
Bus 001 Device 006: ID 0b05:18a3 
  Bus 001 Device 001: ID 1d6b:0002 
  Bus 001 Device 008: ID 2ea8:2203 
    Bus 001 Device 002: ID 1532:008a 
 Bus 001 Device 005: ID 0781:5591 
  Bus 002 Device 001: ID 1d6b:0003 
  Bus 001 Device 004: ID 05e3:0610 
Bus 001 Device 009: ID 1e71:2007 
   Bus 001 Device 003: ID 6964:0075 
  0 $ 

sorta.

landley commented 1 year ago

When you send me GPLv2 code copied from busybox, I'm not gonna look closely at it.

apprehensions commented 1 year ago

heres a little shell script i made that prints the usb stuff and everything.

#!/bin/sh

for usb in /sys/bus/usb/devices/*; do
        [ -f $usb/uevent ] && [ -f $usb/idVendor ] && {
                eval $(cat $usb/uevent)
                read -r idproduct < $usb/idProduct
                [ -f $usb/product ] && read -r product < $usb/product
                read -r idvendor < $usb/idVendor
                [ -f $usb/manufacturer ] && read -r vendor < $usb/manufacturer
                printf 'Bus %s Device %s: ID %s:%s %s %s\n' \
                        $BUSNUM $DEVNUM $idvendor $idproduct "$vendor" "$product"
                unset idproduct product idvendor vendor BUSNUM DEVNUM
        }
done

this should sort of explain it.