platform/upstream/libusb.git
12 months agocore: Suppress hotplug events during initial enumeration 50/290950/1 accepted/tizen_8.0_unified accepted/tizen_unified tizen tizen_8.0 accepted/tizen/8.0/unified/20231005.095128 accepted/tizen/unified/20230406.165731 tizen_8.0_m2_release
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

[Cherrypick from mainline commit 0846456 ("core: Suppress hotplug events during initial enumeration")]
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Change-Id: Idbae2c394ba01694f9e6006d8a52036f6c73da77

12 months agoio: Track device in usbi_transfer 49/290949/1
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

[Cherrypick from mainline commit 7cc06ea ("io: Track device in usbi_transfer")]
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Change-Id: I5fa80503e166ed0aa9cf906985140d80eccdb8dd

12 months agocore: Unset device ctx if it has been destroyed 48/290948/1
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

[Cherrypick from mainline commit c3639bc ("core: Unset device ctx if it has been destroyed")]
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Change-Id: Ic1a27aea2b29d07c93d96d98a684facc799b4259

19 months agoMerge tag 'v1.0.25' into tizen 42/281242/1 accepted/tizen/unified/20230105.154718
Karol Lewandowski [Wed, 14 Sep 2022 22:52:14 +0000 (00:52 +0200)]
Merge tag 'v1.0.25' into tizen

Change-Id: I3d3326f530535ae89b190a0911f4f1a1bd3df7ef

19 months agoMerge tag 'v1.0.24' into tizen 41/281241/1
Karol Lewandowski [Wed, 14 Sep 2022 22:22:59 +0000 (00:22 +0200)]
Merge tag 'v1.0.24' into tizen

Change-Id: I1d8ba762cb8f1afdbf7c6f39f0f1e6de98c1ce19

19 months agoMerge tag 'v1.0.23' into tizen 40/281240/1
Karol Lewandowski [Wed, 14 Sep 2022 21:41:47 +0000 (23:41 +0200)]
Merge tag 'v1.0.23' into tizen

Change-Id: I6205f6d3a23077db72becca3e0964573d38b9f7c

19 months agoMerge tag 'v1.0.22' into tizen 39/281239/1
Karol Lewandowski [Wed, 14 Sep 2022 21:33:04 +0000 (23:33 +0200)]
Merge tag 'v1.0.22' into tizen

Change-Id: I70f02528deaee92ecf67356bfdef0174835b6747

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

2 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

2 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>
2 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>
2 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>
2 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>
2 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

2 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>
2 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

2 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

2 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>
2 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>
2 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

2 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>
2 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>
2 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>
2 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

2 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

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

References #981

2 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

2 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

2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 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>
2 years agodarwin: do not reset darwin_cached_devices on last call to libusb_exit
Nathan Hjelm [Sun, 6 Jun 2021 15:29:41 +0000 (09:29 -0600)]
darwin: do not reset darwin_cached_devices on last call to libusb_exit

This fixes a bug in the change that removed the library destructor function. We should not
be setting the darwin_cached_devices next and prev pointers to NULL as other parts of
libusb may still have references to the devices (hotplug for example). When those references
are release it may modify darwin_cached_devices. This commit fixes the issue by only
initializing darwin_cached_devices on the very first call to libusb_init and not modifying
darwin_cached_devices in libusb_exit (beyond unrefing the devices).

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agocore: Refactor initialization and how the default context is handled
Chris Dickens [Wed, 20 Jan 2021 19:18:35 +0000 (11:18 -0800)]
core: Refactor initialization and how the default context is handled

Highlights for this change:

 - usbi_default_context is only set if libusb_init() is called with NULL.
 - All hotplug related functionality (e.g. initialization, processing) has been
   moved to hotplug.c
 - Backends are simplified by removing initialization mutexes. Mutual exclusion
   between init()/exit() is provided by default_context_lock.
 - Make hotplug types and functions part of libusbi.h with the common usbi_
   prefixes (removes hotplug.h).

Addresses issue highlighted in #855

Closes #856

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agomake libusb.h usable with Watcom
Ozkan Sezer [Sun, 4 Apr 2021 08:11:10 +0000 (11:11 +0300)]
make libusb.h usable with Watcom

Closes #904

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agoUpdate ChangeLog in preparation for 1.0.25
Nathan Hjelm [Sun, 16 May 2021 21:18:06 +0000 (15:18 -0600)]
Update ChangeLog in preparation for 1.0.25

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agodarwin: remove redundant macOS version checks
osy [Wed, 12 May 2021 04:01:11 +0000 (21:01 -0700)]
darwin: remove redundant macOS version checks

The InterfaceVersion is what we want and there is no reason to also check
that the InterfaceVersion matches the macOS version it was introduced in.
This check inadvertently disables features for other Apple platforms that
use IOKit.

Closes #911

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agodarwin: add authorization for device capture
osy [Wed, 12 May 2021 03:58:59 +0000 (20:58 -0700)]
darwin: add authorization for device capture

To use USBDeviceReEnumerate with kUSBReEnumerateCaptureDeviceMask your app
either needs to be running as root OR have the 'com.apple.vm.device-access'
entitlement AND have the user authorization requested via
IOServiceAuthorize().

We can use the capture re-enumerate APIs if either 1) the process is running
as root or 2) the 'com.apple.vm.device-access' entitlement is used AND
IOServiceAuthorize() is called. We assume that if the entitlement is not
there then we are running as root--if this is not true, then
darwin_detach_kernel_driver will fail anyways.

The authorization status is cached in the device's start() so we have to
stop() and start() the device by destroying the plugin and recreating it
again.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agodarwin: use detach kernel APIs for capture
osy [Wed, 12 May 2021 03:56:27 +0000 (20:56 -0700)]
darwin: use detach kernel APIs for capture

When libusb_detach_kernel_driver() is called on the first interface, we use
the capture re-enumerate APIs to force kernel drivers to detach. On
subsequent calls, a counter is incremented to keep track of the number of
detach calls. When libusb_attach_kernel_driver() is called for the same
number of times, then we re-enumerate (reset) and let kernel drivers
re-attach.

darwin_kernel_driver_active() is changed to attempt to claim an interface
and return 1 only if a kIOReturnExclusiveAccess status is returned. The old
implementation which looks for a CFBundleID in the IORegistryEntry does not
seem to work in all cases.

darwin_reset_device() is aware of the capture status and will re-set twice
if capture mode is enabled: once to do a USB reset and again to re-capture.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agodarwin: rename darwin_reset_device
osy [Wed, 12 May 2021 03:45:44 +0000 (20:45 -0700)]
darwin: rename darwin_reset_device

Since it calls USBDeviceReEnumerate() and not USBDeviceReset(), we name
it accordingly.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2 years agodarwin: add timeout for reset reenumerate
osy [Wed, 12 May 2021 03:36:31 +0000 (20:36 -0700)]
darwin: add timeout for reset reenumerate

USBDeviceReEnumerate() does not return an error code (bug?) so if it fails
we could be stuck waiting forever. Set a sane timeout to 10s.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoautogen: Support being called from external build dir
Marco Trevisan (Treviño) [Tue, 13 Apr 2021 17:07:46 +0000 (19:07 +0200)]
autogen: Support being called from external build dir

Building libusb from another build directory doesn't properly work right
now, as calling autogen.sh from there won't work.

An example is when using jhbuild to build it.

So always use absolute paths to call configure and bootstrap scripts

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
3 years agoDoxygen: fix warning about PAPER_TYPE
Ludovic Rousseau [Mon, 22 Feb 2021 20:34:10 +0000 (21:34 +0100)]
Doxygen: fix warning about PAPER_TYPE

warning: argument 'a4wide' for option PAPER_TYPE is not a valid enum value
Using the default: a4!

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
3 years agoDoxygen: update config file from 1.8.16 to 1.9.0
Ludovic Rousseau [Mon, 22 Feb 2021 16:58:00 +0000 (17:58 +0100)]
Doxygen: update config file from 1.8.16 to 1.9.0

warning: Tag 'TCL_SUBST' at line 255 of file 'doxygen.cfg.in' has become obsolete.
         This tag has been removed.
warning: Tag 'COLS_IN_ALPHA_INDEX' at line 1056 of file 'doxygen.cfg.in' has become obsolete.
         This tag has been removed.

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
3 years agodarwin: Don't drop partial data on timeout
Vegard Storheil Eriksen [Mon, 15 Feb 2021 21:57:06 +0000 (22:57 +0100)]
darwin: Don't drop partial data on timeout

A timed out transfer might have been partially filled. Propagate
tpriv->size to avoid dropping this data.

Closes #876

Signed-off-by: Nathan Hjelm <hjelmn@cs.unm.edu>
3 years agodoc: Add since version tag to libusb_set_log_cb()
Victor Toso [Thu, 11 Feb 2021 11:06:00 +0000 (12:06 +0100)]
doc: Add since version tag to libusb_set_log_cb()

The commit f74c7f5 "core: new API libusb_set_log_cb() to redirect
global and per context log messages to the provided log handling
function" in 2018-12-05 is present from 1.0.23 onwards.

This commit just adds this to the docs.

Closes #872

Signed-off-by: Victor Toso <victortoso@redhat.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolinux_usbfs: Gracefully handle buggy devices with a configuration 0
Chris Dickens [Mon, 8 Feb 2021 19:56:13 +0000 (11:56 -0800)]
linux_usbfs: Gracefully handle buggy devices with a configuration 0

The USB spec states that a configuration value of 0 is reserved and is
used to indicate the device in not configured (e.g. is in the address
state). Unfortunately some devices do exist that violate this and use 0
as the bConfigurationValue of the configuration descriptor.

Improve how the Linux backend handles such non-conformant devices by
adding special handling around the configuration value 0. Most devices
will not require this special handling, but for those that do there is
no way to distinguish between the device being unconfigured and using
configuration 0.

Closes #850

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolinux_usbfs: Accept sysfs attributes not terminated with newline
Chris Dickens [Mon, 8 Feb 2021 17:27:20 +0000 (09:27 -0800)]
linux_usbfs: Accept sysfs attributes not terminated with newline

Benjamin Berg reports that some CI systems that simulate sysfs devices
are causing libusb to report errors because such systems are not
conforming to the sysfs pattern of terminating attribute values with a
newline character. Reduce the severity of encountering such
non-conforming attibute values from an error that prevents enumeration
to a warning message.

Closes #857

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolinux_netlink: Change order of header includes per netlink(7)
Chris Dickens [Mon, 8 Feb 2021 17:06:42 +0000 (09:06 -0800)]
linux_netlink: Change order of header includes per netlink(7)

Closes #869

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agodarwin: fix const array size to work with GCC compilers
Michael Dickens [Wed, 30 Dec 2020 02:32:45 +0000 (21:32 -0500)]
darwin: fix const array size to work with GCC compilers

Closes #833

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Fix HID and synchronous transfer completion
Chris Dickens [Mon, 1 Feb 2021 23:12:17 +0000 (15:12 -0800)]
Windows: Fix HID and synchronous transfer completion

Commit b51c743e42 ("Windows: Filter out non-libusb I/O completions")
causes problems with the HID backend functions and synchronous transfer
completions. Address these by fetching the correct pointer in the HID
functions and posting an I/O completion for synchronous transfers.

Closes #863

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>