On 2023-10-11 11:00, Alan Stern wrote:
On Wed, Oct 11, 2023 at 11:30:39AM +0200, Greg KH wrote:On Thu, Oct 05, 2023 at 10:49:10PM -0400, Douglas Gilbert wrote:The code in lsusb-t.c seems to assign a special meaning to "-1" devices and there is only one of those: "5-1". And the device associated with "5-1" is the one that does _not_ appear in the output of 'lsusb -t' but does appear in the output of 'lsusb'.The code patch of the '-t' option in lsusb is totally separate and apart from the "normal" portion of lsusb, as you note, it is a separate .c file as well. -t uses the sysfs representation of the USB devices, while the other code path uses the 'libusb' representation of the USB devices. And those seem to differ here (as they do for everyone.) So if someone wants to take the time to figure out which representation is "more correct", that would be great. I don't have the bandwidth to do so for the next few weeks due to travel requirements on my end, sorry.Doug, I've looked through the source code in lsusb-t.c (usbutils 015) and I didn't notice any place where it treats device names containing "-1" specially. Can you point it out? Also, if I suggested some debugging additions to the source file, would you be able to build them and test the result?
Hi Alan, Attached is a patch that adds support for a '-S <sysroot>' option to lsusb from usbutils found in GKH's github account. It only works when the '-t' option is given to show USB devices in a tree like representation. Without the '-t' option lsusb uses the enumeration services in libusb. The 'lsusb' invocation does find the device at /tmp/sys/bus/usb/devices/5-1 which is a "product : STEVAL-USBC2DP Type-C to DisplayPort adapter" made by ST Micro. Also attached is a pruned representation of /sys and /dev from my machine whichis a Thinkpad X13 G3 with a Lenovo TB3 dock [40AN] connected via USB-C. The "missing" adapter is connected to that dock. However that indirect connection
is probably _not_ significant since if I move that dongle to the other USB-C receptacle on the X13G3 (it has two), the same seen/not_seen issue is reproduced. And with the direct connect the adapter moves to /sys/bus/usb/devices/3-5 . So that debunks my theory that the "1" in the "5-1" is somehow significant. The attached files differ from those I sent to GKH in one important respect. I sent Greg my _whole_ sysfs, around 55,000 nodes and that would have included serial numbers of my machine, my storage devices, MAC addresses, etc. In the tarball attached below only about 5000 nodes are present after some pruning with my clone_pseudo_fs utility (in my github account). Doug Gilbert
diff --git a/lsusb-t.c b/lsusb-t.c index 839321f..3172016 100644 --- a/lsusb-t.c +++ b/lsusb-t.c @@ -101,7 +101,11 @@ LIST_HEAD(interfacelist); LIST_HEAD(usbdevlist); static struct usbbusnode *usbbuslist; -static const char sys_bus_usb_devices[] = "/sys/bus/usb/devices"; +#define SBUD_MAX_LEN 128 + +int max_sbud_len = SBUD_MAX_LEN; +char sys_bus_usb_devices[SBUD_MAX_LEN] = "/sys/bus/usb/devices"; + static int indent; #if 0 @@ -717,6 +721,7 @@ static void print_tree(void) int lsusb_t(void) { +fprintf(stderr, "about to opendir(%s)\n", sys_bus_usb_devices); DIR *sbud = opendir(sys_bus_usb_devices); if (sbud) { walk_usb_devices(sbud); diff --git a/lsusb.c b/lsusb.c index 7fa1555..3fbc990 100644 --- a/lsusb.c +++ b/lsusb.c @@ -3710,6 +3710,7 @@ int main(int argc, char *argv[]) { "version", 0, 0, 'V' }, { "verbose", 0, 0, 'v' }, { "help", 0, 0, 'h' }, + { "sysfsroot", required_argument, 0, 'S' }, { "tree", 0, 0, 't' }, { 0, 0, 0, 0 } }; @@ -3718,17 +3719,18 @@ int main(int argc, char *argv[]) unsigned int treemode = 0; int bus = -1, devnum = -1, vendor = -1, product = -1; const char *devdump = NULL; + const char *sysrtp = NULL; int help = 0; char *cp; int status; setlocale(LC_CTYPE, ""); - while ((c = getopt_long(argc, argv, "D:vtP:p:s:d:Vh", + while ((c = getopt_long(argc, argv, "D:vtP:p:s:S:d:Vh", long_options, NULL)) != EOF) { switch (c) { case 'V': - printf("lsusb (" PACKAGE ") " VERSION "\n"); + printf("lsusb (" PACKAGE ") " VERSION "++\n"); return EXIT_SUCCESS; case 'v': verblevel++; @@ -3773,6 +3775,10 @@ int main(int argc, char *argv[]) devdump = optarg; break; + case 'S': + sysrtp = optarg; + break; + case '?': default: err++; @@ -3794,6 +3800,9 @@ int main(int argc, char *argv[]) " Selects which device lsusb will examine\n" " -t, --tree\n" " Dump the physical USB device hierarchy as a tree\n" + " -S <sys_root>, --sysroot=<sys_root>\n" + " give alternate root directory for sysfs and devfs " + "(def: / )\n" " -V, --version\n" " Show version of program\n" " -h, --help\n" @@ -3805,6 +3814,23 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } + if (sysrtp) { + if (sysrtp[0] != '/') { + fprintf(stderr, "Alternate root for sysfs and devfs " + "must start with '/'\n"); + return EXIT_FAILURE; + } + if (! set_sysfs_root_name(sysrtp)) { + fprintf(stderr, "Failed to set alternate root for " + "sysfs, name too long?\n"); + return EXIT_FAILURE; + } + if (! set_devfs_root_name(sysrtp)) { + fprintf(stderr, "Failed to set alternate root for " + "devfs, name too long?\n"); + return EXIT_FAILURE; + } + } /* by default, print names as well as numbers */ if (names_init() < 0) diff --git a/sysfs.c b/sysfs.c index e073aa8..46d9ba1 100644 --- a/sysfs.c +++ b/sysfs.c @@ -12,19 +12,36 @@ #include <fcntl.h> #include <unistd.h> #include <stdio.h> +#include <string.h> #include <linux/limits.h> #include <libusb.h> #include "sysfs.h" +extern int max_sbud_len; +extern char sys_bus_usb_devices[]; + /* * The documentation of libusb_get_port_numbers() says "As per the USB 3.0 * specs, the current maximum limit for the depth is 7." */ #define USB_MAX_DEPTH 7 -#define SYSFS_DEV_ATTR_PATH "/sys/bus/usb/devices/%s/%s" +/* #define SYSFS_DEV_ATTR_PATH "/sys/bus/usb/devices/%s/%s" */ + +/* static char sysfs_dev_attr_path[196] = "/sys/bus/usb/devices/%s/%s"; */ + +int /* bool */ set_sysfs_root_name(const char * alternate_root) +{ + if ((alternate_root == NULL) || + (strlen(alternate_root) >= (size_t)max_sbud_len)) + return 0; /* false */ + snprintf(sys_bus_usb_devices, max_sbud_len, "%s/sys/bus/usb/devices", + alternate_root); +fprintf(stderr, "new sysfs root name: %s\n", sys_bus_usb_devices); + return 1; /* true */ +} int get_sysfs_name(char *buf, size_t size, libusb_device *dev) { @@ -56,7 +73,7 @@ int read_sysfs_prop(char *buf, size_t size, char *sysfs_name, char *propname) char path[PATH_MAX]; buf[0] = '\0'; - snprintf(path, sizeof(path), SYSFS_DEV_ATTR_PATH, sysfs_name, propname); + snprintf(path, sizeof(path), "%s/%s/%s", sys_bus_usb_devices, sysfs_name, propname); fd = open(path, O_RDONLY); if (fd == -1) diff --git a/sysfs.h b/sysfs.h index 63771f5..f7f381b 100644 --- a/sysfs.h +++ b/sysfs.h @@ -12,5 +12,7 @@ int get_sysfs_name(char *buf, size_t size, libusb_device *dev); extern int read_sysfs_prop(char *buf, size_t size, char *sysfs_name, char *propname); +int /* bool */ set_sysfs_root_name(const char * alternate_root); + /* ---------------------------------------------------------------------- */ #endif /* _SYSFS_H */ diff --git a/usbmisc.c b/usbmisc.c index b12928f..ee776e7 100644 --- a/usbmisc.c +++ b/usbmisc.c @@ -25,12 +25,29 @@ #include "usbmisc.h" +#define DBU_MAX_LEN 256 + /* ---------------------------------------------------------------------- */ -static const char *devbususb = "/dev/bus/usb"; +/* static const char *devbususb = "/dev/bus/usb"; */ +#define DBU_DEF_PATH "/dev/bus/usb" +static char devbususb[DBU_MAX_LEN] = DBU_DEF_PATH; /* ---------------------------------------------------------------------- */ +int /* bool */ set_devfs_root_name(const char * alternate_root) +{ + static const size_t dbu_def_path_sz = sizeof(DBU_DEF_PATH); + const size_t ar_sz = strlen(alternate_root); + + if (dbu_def_path_sz + ar_sz >= (size_t)DBU_MAX_LEN) + return 0; /* false */ + snprintf(devbususb, ar_sz + dbu_def_path_sz + 1, "%s/dev/bus/usb", + alternate_root); +fprintf(stderr, "new devfs root name: %s\n", devbususb); + return 1; /* true */ +} + static int readlink_recursive(const char *path, char *buf, size_t bufsize) { char temp[PATH_MAX + 1]; diff --git a/usbmisc.h b/usbmisc.h index 24b27ee..fc06505 100644 --- a/usbmisc.h +++ b/usbmisc.h @@ -16,5 +16,7 @@ extern libusb_device *get_usb_device(libusb_context *ctx, const char *path); extern char *get_dev_string(libusb_device_handle *dev, uint8_t id); +int /* bool */ set_devfs_root_name(const char * alternate_root); + /* ---------------------------------------------------------------------- */ #endif /* _USBMISC_H */
Attachment:
lsusb_t_sys_dev.tar.xz
Description: application/xz