From c7a2339b0530aa63814aee824c04872e89613a02 Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Sat, 15 May 2021 13:18:31 +0300 Subject: [PATCH] libusb: add support for potential Android JNI backend --- libusb/hid.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/libusb/hid.c b/libusb/hid.c index 0b8aa1e..0827a41 100644 --- a/libusb/hid.c +++ b/libusb/hid.c @@ -595,6 +595,17 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, res = libusb_open(dev, &handle); if (res >= 0) { +#ifdef __ANDROID__ + /* There is (a potential) libusb Android backend, in which + device descriptor is not accurate up until the device is opened. + https://github.com/libusb/libusb/pull/874#discussion_r632801373 + A workaround is to re-read the descriptor again. + Even if it is not going to be accepted into libusb master, + having it here won't do any harm, since reading the device descriptor + is as cheap as copy 18 bytes of data. */ + libusb_get_device_descriptor(dev, &desc); +#endif + /* Serial Number */ if (desc.iSerialNumber > 0) cur_dev->serial_number = @@ -927,6 +938,12 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) break; } good_open = 1; + +#ifdef __ANDROID__ + /* See remark in hid_enumerate */ + libusb_get_device_descriptor(usb_dev, &desc); +#endif + #ifdef DETACH_KERNEL_DRIVER /* Detach the kernel driver, but only if the device is managed by the kernel */