From 58cb9cd956731eab97a6145c7704c66b55da44d8 Mon Sep 17 00:00:00 2001 From: hselasky Date: Mon, 30 Jul 2018 09:16:47 +0000 Subject: [PATCH] MFC r335669: Improve the userspace USB string reading function in LibUSB. Some USB devices does not allow a partial descriptor readout. Found by: bz @ Sponsored by: Mellanox Technologies git-svn-id: svn://svn.freebsd.org/base/stable/10@336884 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- lib/libusb/libusb20.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/libusb/libusb20.c b/lib/libusb/libusb20.c index f0552b6e5..7a345de14 100644 --- a/lib/libusb/libusb20.c +++ b/lib/libusb/libusb20.c @@ -794,6 +794,7 @@ libusb20_dev_req_string_sync(struct libusb20_device *pdev, { struct LIBUSB20_CONTROL_SETUP_DECODED req; int error; + int flags; /* make sure memory is initialised */ memset(ptr, 0, len); @@ -820,22 +821,24 @@ libusb20_dev_req_string_sync(struct libusb20_device *pdev, error = libusb20_dev_request_sync(pdev, &req, ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK); if (error) { - return (error); + /* try to request full string */ + req.wLength = 255; + flags = 0; + } else { + /* extract length and request full string */ + req.wLength = *(uint8_t *)ptr; + flags = LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK; } - req.wLength = *(uint8_t *)ptr; /* bytes */ if (req.wLength > len) { /* partial string read */ req.wLength = len; } - error = libusb20_dev_request_sync(pdev, &req, - ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK); - - if (error) { + error = libusb20_dev_request_sync(pdev, &req, ptr, NULL, 1000, flags); + if (error) return (error); - } - if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) { + + if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) return (LIBUSB20_ERROR_OTHER); - } return (0); /* success */ } -- 2.42.0