platform/upstream/libusb.git
2 years agoCI: Add MinGW build on MSYS2 (Windows)
Tormod Volden [Mon, 28 Mar 2022 13:37:28 +0000 (15:37 +0200)]
CI: Add MinGW build on MSYS2 (Windows)

Closes #1102

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agolibusb 1.0.26-rc1
Tormod Volden [Tue, 22 Mar 2022 22:13:35 +0000 (23:13 +0100)]
libusb 1.0.26-rc1

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agodarwin: Reset by re-enumerate on non-macOS platforms
osy [Sun, 16 May 2021 06:29:54 +0000 (23:29 -0700)]
darwin: Reset by re-enumerate on non-macOS platforms

On non-macOS platforms, ResetDevice() does nothing so we use the "old" way of
calling re-enumerate. If the device is captured, we have to re-enumerate, then
re-capture, re-authorize, and finally restore the state.

Closes #1088

2 years agodarwin: Fix Makefile build on macOS 10.11 and older
Tormod Volden [Wed, 23 Mar 2022 08:34:00 +0000 (09:34 +0100)]
darwin: Fix Makefile build on macOS 10.11 and older

TARGET_OS_OSX is not defined when building with the clang 8 provided by
Xcode 8.2.1 on 10.11.5 (unless the Xcode project is used).

If it is not defined we'll just assume we have a "OSX" target.

Closes #1097

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agowindows: Correct isochronous frame calculation for full-speed devices
Seneral [Thu, 2 Jul 2020 00:23:36 +0000 (02:23 +0200)]
windows: Correct isochronous frame calculation for full-speed devices

The WinUSB isochronous transfer implementation always assumed HighSpeed
microframes.

https://github.com/libusb/libusb/issues/747#issuecomment-649491619

References #747
References #749

2 years agowindows: Fix WinUSB isochronous transfer regression
Keith Ahluwalia [Mon, 21 Mar 2022 10:51:55 +0000 (10:51 +0000)]
windows: Fix WinUSB isochronous transfer regression

Put back missing CHECK_WINUSBX_AVAILABLE macro.

The subapi is generally set in the backend functions themselves, using
the macro CHECK_WINUSBX_AVAILABLE. However, this macro was (likely
inadvertently) removed from winusbx_copy_transfer_data() in commit
9c28ad2 causing the subapi in this function to always be SUB_API_NOTSET.
This results in the error:

  libusb: debug [winusbx_copy_transfer_data] unsupported API call for 'copy_transfer_data' (unrecognized device driver)

https://github.com/libusb/libusb/issues/747#issuecomment-649619615
References #749
Closes #1095

2 years agocore: Install first context as implicit default (and warn about its use)
Benjamin Berg [Tue, 22 Feb 2022 10:45:38 +0000 (11:45 +0100)]
core: Install first context as implicit default (and warn about its use)

There was a behaviour change in libusb 1.0.25 which triggers issues when
the API is misused. This caused for instance gutenprint to crash, see
https://bugzilla.redhat.com/show_bug.cgi?id=2055504

This seems to affect several applications "out in the wild".

For now, work around this by installing an implicit default. But, change
the code to log an error in case this "feature" is being used.

This will allow some grace time for developers to fix their
applications, before we at a later point revert to the stricter
behaviour.

Fixes #1089

2 years agowindows: Fix product string retrieval on HID composite devices
Mathias Hjärtström [Thu, 17 Mar 2022 11:43:18 +0000 (12:43 +0100)]
windows: Fix product string retrieval on HID composite devices

A HID composite device with three interfaces (e.g. keyboard and touch
screen built into one), with all three interfaces referencing their own
names in the interface descriptor, was reported to have an iProduct
string equal to the name of the last interface instead of the actual
product name (e.g. "TOUCH" repeated twice instead of "PRODUCT" and
"TOUCH").

This behavior differ from what for instance Microsoft USB Device Viewer
will report for the same device. This fix will make them report the same
thing.

Use HidD_GetIndexedString() instead of HidD_GetProductString(), as the
latter would otherwise return the name of the interface instead of the
iProduct string whenever the iInterface member of the
USB_INTERFACE_DESCRIPTOR structure for the interface is nonzero (see
Remarks section in the Microsoft documentation of the HID API routines).

Closes #1091

2 years agodarwin: Always use GetPipePropertiesV3 when available
Tormod Volden [Wed, 2 Feb 2022 15:12:25 +0000 (16:12 +0100)]
darwin: Always use GetPipePropertiesV3 when available

The V3 API goes back to macOS 10.8.2 (2012), and we have already been
using it when targeting 10.9 or higher for a while. However in a few
places we were still using the older API.

Affects get_endpoints() and submit_iso_transfer().

In the latter also:
- Check success of pipe properties retrieval.
- Remove a duplicate call to GetPipeProperties().

References #919
Closes #1056

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoAdd interface bounds checks in darwin and windows endpoint functions
Ryan Hileman [Fri, 18 Mar 2022 15:54:52 +0000 (08:54 -0700)]
Add interface bounds checks in darwin and windows endpoint functions

A broken device with invalid interface numbering could cause
an out-of-bounds array access.

Fixes #1039
Closes #1093

2 years agoandroid: Fix typo and syntax in comments
Yegor Yefremov [Sat, 12 Feb 2022 14:07:36 +0000 (15:07 +0100)]
android: Fix typo and syntax in comments

Fix a typo and add missing commas.

Also, delete a blank line at the end of the file and
use /* */ for comments consistently.

Closes #1071

Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com>
2 years agoappveyor: Retain MinGW and cygwin build artifacts
Tormod Volden [Wed, 9 Feb 2022 10:41:26 +0000 (11:41 +0100)]
appveyor: Retain MinGW and cygwin build artifacts

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agowindows: Add "or later" to debug print of Windows version 8
Tormod Volden [Thu, 3 Feb 2022 22:57:48 +0000 (23:57 +0100)]
windows: Add "or later" to debug print of Windows version 8

Windows 10 and 11 is detected as Windows 8 unless there is an
application manifest. We simply cannot tell by appropriate means.

Note that we currently only use the detected version to check for the
minimal version (Vista), so it doesn't matter much.

Closes #1021

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agowindows: Discern Windows 11 (if manifest allows)
Tormod Volden [Wed, 9 Feb 2022 11:35:17 +0000 (12:35 +0100)]
windows: Discern Windows 11 (if manifest allows)

Pre-releases of Windows 11 had build numbers from 20000.

Thanks to Pete Batard for extensive explanation.

Fixes #1021

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agowindows: Add Windows 11 to list of defined releases
Tormod Volden [Thu, 3 Feb 2022 22:52:55 +0000 (23:52 +0100)]
windows: Add Windows 11 to list of defined releases

And guess "12" for whatever comes after. Although with macOS already at
12 this is not given :)

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agodarwin: Avoid cast warnings in time calculation
Tormod Volden [Wed, 2 Feb 2022 17:15:59 +0000 (18:15 +0100)]
darwin: Avoid cast warnings in time calculation

Also use the USEC_PER_SEC macro for readability.

References #1035

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoAdd container build for umockdev
Benjamin Berg [Fri, 18 Feb 2022 16:00:45 +0000 (17:00 +0100)]
Add container build for umockdev

2 years agotests: Add some umockdev based tests
Benjamin Berg [Fri, 18 Feb 2022 15:26:29 +0000 (16:26 +0100)]
tests: Add some umockdev based tests

Add some basic umockdev tests. The setup in this case is all in one
process, but umockdev in principle could also work between processes.

This is just a start, more tests would make sense.

2 years agodarwin: Avoid possibly undefined version macro in configure.ac
Tormod Volden [Mon, 21 Feb 2022 22:45:15 +0000 (23:45 +0100)]
darwin: Avoid possibly undefined version macro in configure.ac

References #1056

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agodarwin: Use usbi_get_monotonic_time() wrapper
Andrew Goodney [Wed, 16 Feb 2022 21:28:14 +0000 (13:28 -0800)]
darwin: Use usbi_get_monotonic_time() wrapper

On macOS < 10.12 we cannot use clock_gettime().

Closes #1070

2 years agowindows: Link gcc helpers statically on MinGW
Tormod Volden [Thu, 10 Feb 2022 17:43:23 +0000 (18:43 +0100)]
windows: Link gcc helpers statically on MinGW

Fixes #1049

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agolinux_usbfs: Fix maybe-uninitialized error with -O3
Yegor Yefremov [Sun, 6 Feb 2022 06:12:14 +0000 (07:12 +0100)]
linux_usbfs: Fix maybe-uninitialized error with -O3

Initialize active_config to an invalid value to avoid the following
compilation error:

os/linux_usbfs.c: In function ‘op_get_configuration’:
os/linux_usbfs.c:1452:12: error: ‘active_config’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
 1452 |  *config = (uint8_t)active_config;

Closes #1062
Closes #1063

Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com>
2 years agoconfigure.ac: link with -latomic if needed
Fabrice Fontaine [Mon, 7 Feb 2022 10:55:54 +0000 (11:55 +0100)]
configure.ac: link with -latomic if needed

libusb unconditionally uses atomic_fetch_add since version 1.0.25 and
commit 1a08aa8 and commit eed8a37 but some architectures (e.g. sparc)
needs to link with -latomic to be able to use it. So check if -latomic
is needed and update libusb-1.0.pc accordingly to avoid the following
build failure with openocd:

/home/buildroot/autobuild/instance-0/output-1/host/opt/ext-toolchain/bin/../lib/gcc/sparc-buildroot-linux-uclibc/10.3.0/../../../../sparc-buildroot-linux-uclibc/bin/ld: /home/buildroot/autobuild/instance-0/output-1/host/sparc-buildroot-linux-uclibc/sysroot/usr/lib/libusb-1.0.so: undefined reference to `__atomic_fetch_add_4'
collect2: error: ld returned 1 exit status

Full build log:
http://autobuild.buildroot.org/results/4a27a769bb3cdf78643c3049b87d792178d6512c

Closes #1064

Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
2 years agocore: Suppress hotplug events during initial enumeration
Benjamin Berg [Tue, 1 Mar 2022 14:08:38 +0000 (15:08 +0100)]
core: Suppress hotplug events during initial enumeration

The initial enumeration should not result in hotplug events to be fired.
This is just a convenience though, API users still need to be prepared
to be notified a second time for a device that was plugged in between
libusb_init and libusb_hotplug_register_callback.

This regressed with commit 6929b82 ("Fix segmentation fault in
libusb_init() if usbi_backend.init() fails"). This commit avoids the
mentioned segmentation fault by avoiding to clean up the hotplug code if
it was not yet initialised.

Fixes #1082
Closes #1090
References #989

2 years agoio: Track device in usbi_transfer
Benjamin Berg [Tue, 15 Feb 2022 10:13:41 +0000 (11:13 +0100)]
io: Track device in usbi_transfer

transfer->dev_handle currently has the behaviour that it will be unset
if the device is closed. The sync API uses this fact to catch an error
case.

In other cases, transfer->dev_handle will keep its value, which means
that if the transfer lives longer than the device handle, the pointer
becomes invalid.

The transfer does however keep a reference to the device, which owns the
pointer to the context. As such, we can track this reference internal to
the transfer, and it is set while the transfer is in-flight.

With this, switch the logging infrastructure to use itransfer->dev->ctx
while checking that itransfer->dev is non-NULL.

Note that this was a regression caused by 6cae9c6 ("core: update
usbi_dbg to take the context as an argument"), specifically when
resolving the context while freeing a transfer after closing a device.

Note that the transfer will now keep a reference to the device until it
is free'ed. This allows it to use the correct context for logging even
in libusb_free_transfer.

The alternative to all this would be to just explicitly pass NULL to the
log handler in libusb_free_transfer.

Fixes #1038
Closes #1073

2 years agocore: Unset device ctx if it has been destroyed
Benjamin Berg [Fri, 4 Feb 2022 21:50:28 +0000 (22:50 +0100)]
core: Unset device ctx if it has been destroyed

Devices can outlive their context in some cases (in particular with
python garbage collection). Guard against this by clearing the ctx
pointer so that it is not pointing to uninitialized memory.

Closes #1058

2 years agolibusb 1.0.25 upstream/1.0.25
Tormod Volden [Mon, 31 Jan 2022 12:56:37 +0000 (13:56 +0100)]
libusb 1.0.25

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoappveyor: Retain selected build artifacts
Tormod Volden [Sat, 29 Jan 2022 21:04:08 +0000 (22:04 +0100)]
appveyor: Retain selected build artifacts

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoexamples: Do not assume positive errno macros
Tormod Volden [Sat, 22 Jan 2022 13:39:45 +0000 (14:39 +0100)]
examples: Do not assume positive errno macros

Some functions were returning e.g. -ENOMEM and the caller would check
for negative return values. However, on Haiku, contrary to modern
standards, these macros are negative, so this logic would fail. In any
case, change the function to return -1 instead. For good measure also
set errno to the appropriate value, although it is not used in the
current code.

This was discovered on Haiku builds because the value for ENOMEM is
INT_MIN which cannot be negated without overflow.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agonetbsd: Fix typo preventing build
Tormod Volden [Sat, 22 Jan 2022 11:53:57 +0000 (12:53 +0100)]
netbsd: Fix typo preventing build

Fixup of commit 6cae9c6

Tested-by: Xiaofan Chen <xiaofanc@gmail.com>
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agolibusb 1.0.25-rc1
Tormod Volden [Fri, 21 Jan 2022 20:10:16 +0000 (21:10 +0100)]
libusb 1.0.25-rc1

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoAdd libusb_set_option to libusb-1.0.def manually
Tormod Volden [Fri, 21 Jan 2022 19:20:59 +0000 (20:20 +0100)]
Add libusb_set_option to libusb-1.0.def manually

Since libusb_set_option() is variadic, there is no symbol
for it with @ appended in the DLL, and the automatic
generation of libusb-1.0.def misses it.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoUpdate AUTHORS
Tormod Volden [Fri, 21 Jan 2022 17:20:51 +0000 (18:20 +0100)]
Update AUTHORS

Based on `git shortlog -s | cut -c8-` and a lot of manual fixups.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoUpdate libusb-1.0.def
Tormod Volden [Fri, 21 Jan 2022 16:58:59 +0000 (17:58 +0100)]
Update libusb-1.0.def

Updated by running the following on a libusb-1.0 DLL generated by MinGW32:

echo -e "LIBRARY \"libusb-1.0.dll\"\nEXPORTS" > libusb-1.0.def
strings libusb-1.0.dll | grep ^libusb | grep @ | LC_ALL=C sort | uniq | \
 sed -e "s/\(.*\)@\([0-9]*\)/  \1\n  \1@\2 = \1/" | LC_ALL=C sort >> libusb-1.0.def

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoAdd github action for macOS
Ludovic Rousseau [Fri, 31 Dec 2021 17:29:28 +0000 (18:29 +0100)]
Add github action for macOS

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
2 years agoAdd github action for GNU/Linux
Ludovic Rousseau [Fri, 31 Dec 2021 17:29:04 +0000 (18:29 +0100)]
Add github action for GNU/Linux

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
2 years agoThe current code for calculating the timeout in darwin_reenumerate_device_timeout...
Andrew Goodney [Tue, 28 Dec 2021 04:34:05 +0000 (20:34 -0800)]
The current code for calculating the timeout in darwin_reenumerate_device_timeout() doesn't calculate elapsed microseconds, it counts the number of times the loop runs.

This results in very long timeouts.

This PR uses clock_gettime(CLOCK_MONOTONIC, ...) to calculate the elapsed time

Closes #1035

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agodarwin: Minor tidy up in darwin_set_interface_altsetting()
Tormod Volden [Wed, 1 Dec 2021 10:38:49 +0000 (11:38 +0100)]
darwin: Minor tidy up in darwin_set_interface_altsetting()

Closes #1032

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agodarwin: fix behavior of libusb_set_interface_alt_setting when it stalls the pipe
Nathan Hjelm [Tue, 30 Nov 2021 22:10:01 +0000 (15:10 -0700)]
darwin: fix behavior of libusb_set_interface_alt_setting when it stalls the pipe

In some versions of macOS a pipe stall returned when setting the alternate interface
causes the interface to become unusable. To handle this case the backend was always
re-claiming the interface before clearing the pipe stall. In macOS Monterey
unconditionally re-claiming the interface leads to an error due to the process already
having exclusive access. To resolve this issue we attempt to clear the halt and only
re-claim the interface if clearing the pipe stall returns kIOUSBUnknownPipeErr. Tested
with 12.0.1 and 10.13 and get the expected results in both cases with a custom USB
device that has this behavior.

Fixes #1011
Closes #1031

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agoBump LIBUSB_API_VERSION
Tormod Volden [Fri, 1 Oct 2021 21:57:14 +0000 (23:57 +0200)]
Bump LIBUSB_API_VERSION

LIBUSB_OPTION_NO_DEVICE_DISCOVERY is new.

Closes #1004

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@me.com>
2 years agoKeep LIBUSB_OPTION_WEAK_AUTHORITY as a macro with same value
Tormod Volden [Fri, 1 Oct 2021 22:05:32 +0000 (00:05 +0200)]
Keep LIBUSB_OPTION_WEAK_AUTHORITY as a macro with same value

If the value is always the same we retain binary compatibility.

References #935

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@me.com>
2 years agoFuzzing: Add CIFuzz
AdamKorcz [Thu, 25 Nov 2021 11:03:30 +0000 (11:03 +0000)]
Fuzzing: Add CIFuzz

2 years agocore: Unlock and clear default ctx in all error paths in libusb_init
Matthias Bolte [Wed, 22 Sep 2021 11:40:23 +0000 (13:40 +0200)]
core: Unlock and clear default ctx in all error paths in libusb_init

Commit 32a22069428cda9d63aa666e92fb8882a83d4515 reordered and
refactored libusb_init. This resulted in moving code outside the
default ctx lock that was unrelated to it. But this resulted in
broken error path cleanup logic that leaves the default ctx as
locked, half initialized and freed in case a libusb_set_option or
the usbi_io_init call fails.

Undo part of the previous reordering to unlock and clear the default
ctx in all error paths in libusb_init.

Closes #995

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agocore: Minor code cleanup in libusb_init (NOP)
Tormod Volden [Tue, 9 Nov 2021 08:57:57 +0000 (09:57 +0100)]
core: Minor code cleanup in libusb_init (NOP)

After commit f7084fe the if clause would always be true, so remove it.

Thanks to "xloem" for reporting.

References #942

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoWindows: Allow synchronous control transfers (for libusb0)
Tormod Volden [Thu, 28 Oct 2021 12:48:55 +0000 (14:48 +0200)]
Windows: Allow synchronous control transfers (for libusb0)

Some of the changes in commit 9c28ad2 rely on all transfers having (or
appearing to have) asynchronous completion.

However, the libusb0.sys backend of libusbk.dll performs all control
transfers synchronously and ignores any "overlapped" structure handed to
it. Our asynchronous handling will in this case be pending and
eventually time out although the USB request itself was successful.

Therefore restore the possibility of synchronous completion of control
transfers, by forcing the completion handling on a successful return
from request submission. This brings the code closer to how it was
established in commit ce95f65.

Fixes #94

Tested-by: Xiaofan Chen <xiaofanc@gmail.com>
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoWindows: Fix timeouts if only endpoint 0 exists
Julian Scheel [Mon, 8 Jun 2020 12:32:16 +0000 (14:32 +0200)]
Windows: Fix timeouts if only endpoint 0 exists

The default timeouts of 5 seconds used by WinUSB are cleared in
configure_endpoints(). This was not called if not at least one extra
endpoint apart from endpoint 0 existed, so that in those cases the
default timeout of 5 seconds remained in place.
Fix this by calling configure_endpoints, even if no interface specific
endpoints were found, so that it can configure endpoint 0.

Closes #963

Signed-off-by: Julian Scheel <julian@jusst.de>
[Tormod: Refactored to reduce duplicated code]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agoWindows: Ignore any device interface GUIDs beyond the first
Tormod Volden [Sat, 16 Oct 2021 22:13:44 +0000 (00:13 +0200)]
Windows: Ignore any device interface GUIDs beyond the first

The NZXT Kraken Z73 device has been reported to brokenly have two
identical device interface GUIDs registered. Accept such cases by
treating the ERROR_MORE_DATA return value as success and processing the
first returned GUID as if it would be the only one.

If one day we see legitimate cases of multiple GUIDs we will need to
deal with them properly.

References jdkvision/libusb#1
Closes #1010

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2 years agodarwin: Fix compilation on macOS 10.9 and earlier
Evan Miller [Sat, 6 Nov 2021 23:42:53 +0000 (19:42 -0400)]
darwin: Fix compilation on macOS 10.9 and earlier

kUSBReEnumerateCaptureDeviceMask was introduced in the 10.10 SDK, so
compiling on 10.9 and earlier will fail with:

 os/darwin_usb.c: In function 'darwin_reenumerate_device':
 os/darwin_usb.c:1749:18: error: 'kUSBReEnumerateCaptureDeviceMask' undeclared (first use in this function); did you mean 'USBReEnumerateOptions'?
        options |= kUSBReEnumerateCaptureDeviceMask;
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                   USBReEnumerateOptions
 os/darwin_usb.c:1749:18: note: each undeclared identifier is reported only once for each function it appears in

Downstream report: https://trac.macports.org/ticket/63668

Closes #1023

3 years agoexamples: Add example for unrooted Android
Peter-St [Wed, 30 Dec 2020 21:07:23 +0000 (22:07 +0100)]
examples: Add example for unrooted Android

Closes #830

3 years agodescriptor: Provide more detailed warning if suspicious bLength
Tormod Volden [Mon, 1 Nov 2021 22:40:44 +0000 (23:40 +0100)]
descriptor: Provide more detailed warning if suspicious bLength

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
3 years agodoc: Explain default options in libusb_set_option documentation
Tormod Volden [Sat, 2 Oct 2021 14:34:46 +0000 (16:34 +0200)]
doc: Explain default options in libusb_set_option documentation

References #942

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
3 years agocore: Apply default options to all new contexts
Bruno Harbulot [Thu, 12 Aug 2021 19:26:12 +0000 (20:26 +0100)]
core: Apply default options to all new contexts

The default options configured with libusb_set_option(NULL, ...)
were only applied when the default context was created, and were
ignored when calling libusb_init() with a non-null context pointer.

Make sure the default options will be applied to all new contexts. This
is important when using LIBUSB_OPTION_NO_DEVICE_DISCOVERY which must
be respected during the context initialization in order to work.

Closes #942

[Tormod: amend comments]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
3 years agoxusb: Print total length of configuration descriptor
Tormod Volden [Sun, 31 Oct 2021 17:32:25 +0000 (18:32 +0100)]
xusb: Print total length of configuration descriptor

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
3 years agoFix various typos in docs/comments
luz paz [Sat, 30 Oct 2021 21:15:04 +0000 (17:15 -0400)]
Fix various typos in docs/comments

Found via `codespell -q 3`

Closes #1015

3 years agoxusb: Print configuration descriptor length
Tormod Volden [Sun, 31 Oct 2021 08:12:07 +0000 (09:12 +0100)]
xusb: Print configuration descriptor length

Also inform about reading OS string descriptor.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
3 years agodarwin: After last CFRelease of struct field, set it to NULL
Sean McBride [Fri, 20 Aug 2021 19:57:52 +0000 (15:57 -0400)]
darwin: After last CFRelease of struct field, set it to NULL

Other code checks this field for being NULL or not, so NULLing it upon
last release seems the right thing to do.  I noticed this during code
review, I never actually reproduced any problem.

Closes #981

3 years agodoc: Improve comments related to device discovery and hotplug
Sean McBride [Fri, 29 Oct 2021 13:47:01 +0000 (09:47 -0400)]
doc: Improve comments related to device discovery and hotplug

Closes #1013

3 years agodoc: Update note about LIBUSB_TRANSFER_ADD_ZERO_PACKET availability
Tormod Volden [Sat, 2 Oct 2021 09:03:29 +0000 (11:03 +0200)]
doc: Update note about LIBUSB_TRANSFER_ADD_ZERO_PACKET availability

Closes #1005

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
3 years agoWindows: Fix ssize_t unconditionally defined
Paul Cercueil [Wed, 6 Oct 2021 13:31:24 +0000 (14:31 +0100)]
Windows: Fix ssize_t unconditionally defined

The standard procedure to define ssize_t on Windows is to wrap it around
a check for the _SSIZE_T_DEFINED macro.

If not done, it makes it impossible to use the libusb.h header along
with other headers (from other libraries) that also attempt to define
ssize_t.

Closes #1007

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
3 years agoWindows: Partial fix for data race in composite_copy_transfer_data
Bohdan Tymkiv [Wed, 28 Jul 2021 14:46:04 +0000 (17:46 +0300)]
Windows: Partial fix for data race in composite_copy_transfer_data

This only fixes issue (A):
libusb: error [composite_copy_transfer_data] program assertion failed - no function to copy transfer data

References #966
Closes #967

3 years agodarwin: fix deprecation warning about kIOMasterPortDefault
Nathan Hjelm [Thu, 28 Oct 2021 16:36:30 +0000 (10:36 -0600)]
darwin: fix deprecation warning about kIOMasterPortDefault

The kIOMasterPortDefault constant has been replaced by kIOMainPortDefault. Both
are synonmys for 0. To avoid extra checks on the target macOS release just go
ahead and use 0.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: add support for handling new error code (kUSBHostReturnPipeStalled)
Nathan Hjelm [Thu, 28 Oct 2021 16:25:03 +0000 (10:25 -0600)]
darwin: add support for handling new error code (kUSBHostReturnPipeStalled)

With macOS Monterey Apple started using a new error code to indicate a pipe
stall. This new error code needs to be handled both in translation to libusb
error codes and when handling errors setting the alt setting.

References #1011

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agogit hooks: Remove "source" bashism
Tormod Volden [Wed, 22 Sep 2021 13:55:33 +0000 (15:55 +0200)]
git hooks: Remove "source" bashism

"source" is not supported in Bourne shell

Closes #997

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
3 years agoUpdate Android Readme
Peter-St [Mon, 21 Dec 2020 20:47:24 +0000 (21:47 +0100)]
Update Android Readme

Add another approach for connecting USB devices on Android.

References #830
Closes #996

3 years agoFix segmentation fault in libusb_init() if usbi_backend.init() fails
Ankur Verma [Tue, 14 Sep 2021 01:25:30 +0000 (18:25 -0700)]
Fix segmentation fault in libusb_init() if usbi_backend.init() fails

If the backend init fails, the control goes to err_io_exit which tries
to clean up hotplug related lists that aren't initialized. Moving
hotplug_init before makes sure the lists are valid so if backend init
fails they get cleaned up without errors.

Closes #989

3 years agoFix comment typos
Sean McBride [Fri, 20 Aug 2021 19:57:52 +0000 (15:57 -0400)]
Fix comment typos

References #981

3 years agoAndroid build name correction
xloem [Wed, 4 Aug 2021 10:38:07 +0000 (10:38 +0000)]
Android build name correction

Fixes #31

See #143 and #596 for solutions to the whole build system.

Closes #971

3 years agoFix Windows HID backend missing byte
mcuee [Fri, 27 Aug 2021 09:49:30 +0000 (17:49 +0800)]
Fix Windows HID backend missing byte

When the report id is zero, we should indeed skip the first byte of
transfer_priv->hid_buffer, since it is not part of the actual report.
But the report size should not change.

Thanks to Jonas Malaco who has done the investigation in this issue.
Reference: Jonas Malaco's comments
https://github.com/libusb/libusb/issues/902#issuecomment-810726897

Fixes #902
Fixes #977
Closes #986

3 years agodarwin: fix typo
Nathan Hjelm [Sun, 8 Aug 2021 15:22:31 +0000 (09:22 -0600)]
darwin: fix typo

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: improve the error message on kernel driver detach failure
Nathan Hjelm [Sun, 8 Aug 2021 15:16:38 +0000 (09:16 -0600)]
darwin: improve the error message on kernel driver detach failure

Detaching a kernel driver in macOS requires that either the binary has the
com.apple.vm.device-access entitlement or is run with effective UID 0 (root). This
commit adds an effective UID check before attempting to capture a device and prints
a warning that neither is the user root nor does the binary have the correct
entitlement.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: fix USB capture for root
Nathan Hjelm [Sun, 8 Aug 2021 05:02:16 +0000 (23:02 -0600)]
darwin: fix USB capture for root

This commit restores the ability to capture-open a USB device when running as root.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoconfigure: remove usage of deprecated AC_HELP_STRING
Nathan Hjelm [Tue, 27 Jul 2021 05:29:01 +0000 (23:29 -0600)]
configure: remove usage of deprecated AC_HELP_STRING

This macro was replaced by AS_HELP_STRING. This commit updates the lingering deprecated call
with the new macro as used elsewhere in configure.ac.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agocore: set default backend options before calling backend init
Nathan Hjelm [Tue, 27 Jul 2021 05:23:12 +0000 (23:23 -0600)]
core: set default backend options before calling backend init

This commit restores the previous behavior with regards to setting some backend options and
restores the ability to set default context options before libusb_init. None of the backends
use anything set in init in their set_option function so this should be safe with all
backends and options.

References #942

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoUpdate libusb-1.0.def
mcuee [Sun, 25 Jul 2021 08:26:37 +0000 (16:26 +0800)]
Update libusb-1.0.def

This fixes issue #944 C++ symbol for libusb_set_option not exported correctly.
As per the report in issue #944, original commit is correct but subsequent edits cause the regression.
https://github.com/libusb/libusb/blob/v1.0.22-rc1/libusb/libusb-1.0.def#L156

Closes #964

Signed-off-by: Xiaofan Chen xiaofanc@gmail.com
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: only attempt to auto-detach driver is one is active
Nathan Hjelm [Thu, 22 Jul 2021 05:12:19 +0000 (23:12 -0600)]
darwin: only attempt to auto-detach driver is one is active

This commit improves the auto-detach code so that it doesn't attempt to detach a kernel
driver if none exists. This will avoid extra info/warning output on the failed detach.

Closes #960

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: use the IO registry to detect if a kernel driver is attached to an interface
Nathan Hjelm [Wed, 21 Jul 2021 17:49:37 +0000 (11:49 -0600)]
darwin: use the IO registry to detect if a kernel driver is attached to an interface

The implementation of libusb_kernel_driver_active was attempting to open the interface to
check if a driver is attached. This may have side effects (like configuring the device)
that may be unexpected to the user. This commit updates the code to find the interface's
IO registry entry (either IOUSBHostInterface or the legacy IOUSBInterface) and check if
the entry has a child entry. A child entry indicates that a driver is currently attached.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoxusb: add check for interface kernel driver
Nathan Hjelm [Wed, 21 Jul 2021 17:47:25 +0000 (11:47 -0600)]
xusb: add check for interface kernel driver

This commit adds a call to libusb_kernel_driver_active and prints out the result. This
provides a way to quickly check the result when testing.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agocore: update usbi_dbg to take the context as an argument
Nathan Hjelm [Tue, 20 Jul 2021 15:31:06 +0000 (09:31 -0600)]
core: update usbi_dbg to take the context as an argument

This commit fixes a performance issue caused by the disconnection of the first context
allocated from the default context. usbi_dbg now takes the explicit context instead of
relying on the default context (which may not exist) in most cases. All call sites have
been updated to pass the context or explicitly pass NULL if the context is not
available. We should actively discourage using NULL as the context in the future and
patch all call sites to always pass the context.

Fixes #951

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: downgrade error message on failure to open interface to info
Nathan Hjelm [Tue, 20 Jul 2021 21:48:27 +0000 (15:48 -0600)]
darwin: downgrade error message on failure to open interface to info

This resolves an issue introduced by the implementation of the kernel detach code. This
code used claim_interface to detect if a kernel driver is attached. The issue is that if
a driver is attached or the interface is not available and an error message was printed
out. Since this is not an error the info level is more appropriate.

Also downgrading a number of warnings to clean up the output.

Closes #955

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: add a comment on how the bus number is calculated
Nathan Hjelm [Tue, 20 Jul 2021 21:44:59 +0000 (15:44 -0600)]
darwin: add a comment on how the bus number is calculated

This commit adds a comment about the bus number calculation to indicate how the
location ID is constructed and why libusb is using the top byte for the bus
number. It also indicates that the bus number starts at 0.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: update the list of endpoints after successfull SetAlternateInterface()
Dmitry Zakablukov [Tue, 13 Jul 2021 14:50:28 +0000 (17:50 +0300)]
darwin: update the list of endpoints after successfull SetAlternateInterface()

Closes #950

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agowinusb: Ignore missing DeviceInterfaceGUID
Tormod Volden [Sun, 11 Jul 2021 12:40:01 +0000 (14:40 +0200)]
winusb: Ignore missing DeviceInterfaceGUID

Closes #948

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: improve support for auto-detaching a kernel driver
Nathan Hjelm [Fri, 9 Jul 2021 15:17:33 +0000 (09:17 -0600)]
darwin: improve support for auto-detaching a kernel driver

This commit improves the code around auto attach/detach of a kernel driver:

 - Log a warning when a driver can not be auto-detached and continue when claiming an
   interface. It could be the case that the interface has no driver attached or that
   the claim will still succeed.

 - Log a warning if the driver can not be auto-attached and return success when
   releasing an interface. The operation did complete successfully but the
   re-enumeration may have failed. This can be discovered on the next device
   access.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: fix SEGV on libusb_close after failed re-enumeration
Nathan Hjelm [Fri, 9 Jul 2021 04:43:28 +0000 (22:43 -0600)]
darwin: fix SEGV on libusb_close after failed re-enumeration

This commit fixes an issue that can occur if the user calls libusb_close after a failed attempt
of libusb_reset (re-enumerate). This can lead to an attempt to dereference dpriv->device which
was set to NULL during the re-enumeration.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years ago[Issue-912] Lock open_devs_lock in context to avoid access violation
Dmitry Zakablukov [Wed, 30 Jun 2021 16:45:53 +0000 (19:45 +0300)]
[Issue-912] Lock open_devs_lock in context to avoid access violation

Closes #943

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoNew NO_DEVICE_DISCOVERY option to replace WEAK_AUTHORITY option
Tormod Volden [Tue, 15 Jun 2021 20:33:45 +0000 (22:33 +0200)]
New NO_DEVICE_DISCOVERY option to replace WEAK_AUTHORITY option

The option disables device enumeration (and hotplug, which depends on
enumeration). Since this has a much broader usage scope than "weak
authority", give the option a name reflecting what it does.

For now, keep the old option name working as an alias.

Closes #935

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agowindows: Allow GUID with and without trailing zeroes
Craig Hutchinson [Tue, 8 Jun 2021 18:03:43 +0000 (20:03 +0200)]
windows: Allow GUID with and without trailing zeroes

Improve the GUID length check to accommodate the cases where the GUID
string fetched from registry does not have a trailing zero, or if a
multi-string entry, one or two trailing zeroes.

Closes #927

Signed-off-by: Craig Hutchinson <54269136+CraigHutchinson@users.noreply.github.com>
[Tormod: Use correct types and simplify comparisons]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoFix build failure on Mac OS X 10.7
Ryan Schmidt [Sat, 12 Jun 2021 03:19:49 +0000 (22:19 -0500)]
Fix build failure on Mac OS X 10.7

Apple documentation says kUSBDeviceSpeedSuper is available in 10.7 and
later but in fact it is only defined in the 10.8 SDK and later.

Closes #410
Closes #929

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: release device parent reference when re-enumerating device
Nathan Hjelm [Sat, 12 Jun 2021 04:27:19 +0000 (22:27 -0600)]
darwin: release device parent reference when re-enumerating device

This commit fixes a dangling device reference that can occur when a device is
being re-enumerated. The problem is the code was unconditionally re-caching the
parent device which then takes a reference on the parent device. If the field
was already populated this reference was not needed. To ensure the code works
even if the parent device also changes during the re-enumeration the code
release the parent reference and grabs a fresh reference.

Fixes #924

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoDo not restrict "weak authority" option to Android
Tormod Volden [Mon, 7 Jun 2021 16:19:41 +0000 (18:19 +0200)]
Do not restrict "weak authority" option to Android

libusb_wrap_sys_device() is an often needed feature on Android, but can
also be very convenient on other Linux platforms.

In many cases the initial device scanning is not needed and
LIBUSB_OPTION_WEAK_AUTHORITY thus provides a performance enhancement.

Closes #925

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agocore: really fix dangling devices
Nathan Hjelm [Fri, 11 Jun 2021 13:59:23 +0000 (07:59 -0600)]
core: really fix dangling devices

The prior fix made an incorrect assumption about the reference counting of
parent devices. This CL fixes the issue by checking if the parent device
should be removed from the list as well. This is needed as when the child
device is released the parent may also be released.

Closes #924

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin_set_interface_altsetting: Avoid device reset on pipe error
Tormod Volden [Thu, 14 Jan 2021 19:59:56 +0000 (20:59 +0100)]
darwin_set_interface_altsetting: Avoid device reset on pipe error

If the device doesn't support the SET_INTERFACE request (which is
allowed if there are no alternate interfaces) it should stall the pipe.
A successful device request would clear all endpoints of the interface.
We therefore plaster over the failed request by clearing the endpoints
and returning success, effectively replacing the missing device request
support.

Up to now this was done by resetting the device, but this would affect
other interfaces as well. So only clear the endpoints of the selected
interface.

Only do this in the case the request tried setting the already
selected setting. Otherwise return the pipe error to the caller.

For any other error than pipe error return with failure.

Note that we need to reclaim the interface after the pipe error.
This is maybe papering over a bug in libusb or darwin: The interface
becomes inaccessible after the failed SetAlternateInterface() call. The
reclaim recovers from it, without device side-effects. The need to
recover was maybe the reason for the original reset "hammer".

Closes #852

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agowinusb: Check for ClassGuid changes on re-enumeration
Tormod Volden [Thu, 25 Mar 2021 22:47:27 +0000 (23:47 +0100)]
winusb: Check for ClassGuid changes on re-enumeration

Previously only the dev_id (VID/PID/serial) was checked, but if the
bcdDevice has changed, Windows will have re-enumerated the device and
possibly attributed another ClassGuid to it.

Fixes issue #897.
Closes #900

Thanks to Craig Hutchinson for reporting, suggestion and testing.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agocore: only increment devices_released when a device is destroyed
Nathan Hjelm [Fri, 11 Jun 2021 03:02:05 +0000 (21:02 -0600)]
core: only increment devices_released when a device is destroyed

Fix a typo in the last commit to ensure the loop does exit when there is
a dangling reference.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agocore: ensure that all devices are properly cleaned up on libusb_exit
Nathan Hjelm [Fri, 11 Jun 2021 02:51:44 +0000 (20:51 -0600)]
core: ensure that all devices are properly cleaned up on libusb_exit

When cleaning up the context on libusb_exit the last step is to to call
hotplug_exit. This function took one pass over the devices and released any
devices where the reference count had reached 0. All remaining devices
were assumed to have leaked references and a warning message was printed
out. Unfortunately, this cleanup was too simplistic. It ignored the
references created when a device was the parent of another device (which
takes a reference). This reference is released when the child device is
released.

To ensure that we do not erroneously warn about devices with leftover
references the code now loops over the device list until no more devices
have a single reference. This ensures that we will eventually remove
all devices with no external references.

Fixes #924

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agocore: allow libusb_set_option on the default context before libusb_init
Nathan Hjelm [Sun, 6 Jun 2021 18:19:05 +0000 (12:19 -0600)]
core: allow libusb_set_option on the default context before libusb_init

This commit updates libusb_set_option to save the options if setting them on the default
context. This ensures the options 1) can be set before libusb_init(NULL, ...), and 2)
are honored even if the default context is destroyed and re-created.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoWindows: Try alternative methods to query port number
Jim Chen [Wed, 3 Feb 2021 18:13:55 +0000 (13:13 -0500)]
Windows: Try alternative methods to query port number

This commit is similar to #634 but I think it offers a better approach. The commit adds two more methods for querying the port number, in addition to the existing SPDRP_ADDRESS method, which may not actually return the port number when using USB drivers other than the Microsoft driver.

The first method uses SPDRP_LOCATION_INFORMATION, which may return a port in a format similar to "Port_#0002.Hub_#000D".

The second method uses SPDRP_LOCATION_PATHS, which returns the device location in a format similar to "PCIROOT(B2)#PCI(0300)#PCI(0000)#USBROOT(0)#USB(1)#USB(2)#USBMI(3)", where the port number is the number within the last "#USB()" token.

If both methods fail, SPDRP_ADDRESS is still used as a fallback.

Closes #867

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoWindows: Support LIBUSB_TRANSFER_ADD_ZERO_PACKET on winusb
Patrick Stewart [Fri, 9 Jun 2017 19:54:45 +0000 (20:54 +0100)]
Windows: Support LIBUSB_TRANSFER_ADD_ZERO_PACKET on winusb

Closes #312

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodoc: Add since version tag to libusb_wrap_sys_device()
Ihor Dutchak [Mon, 17 May 2021 20:56:34 +0000 (23:56 +0300)]
doc: Add since version tag to libusb_wrap_sys_device()

Closes #914

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodoc: correction of LIBUSB_OPTION_WEAK_AUTHORITY availability
Ihor Dutchak [Fri, 21 May 2021 12:32:24 +0000 (15:32 +0300)]
doc: correction of LIBUSB_OPTION_WEAK_AUTHORITY availability

Even though its implementation is in linux/usbfs,
in fact it is under `ifdef` and only used by Android builds.

Closes #916

Signed-off-by: Nathan Hjelm <hjelmn@google.com>