When one process has two contexts to libusb, it may trigger a reenumeration
while the other context accesses `*dpriv->device`. Adding a mutex to access
`device` can be costly because it is used in every function. Instead, we
maintain an invariant: the `darwin_cached_device` must be valid at all
times. In particular, this means the fields `device` and `service` must not
be stale or we will have a use after free triggerable through a race
condition.
Previously, in `darwin_devices_detached` during reenumeration, it was
possible for `old_device->device` to be freed. We now defer this free to
`darwin_get_cached_device` after a new `device` is written. Note that the
order is important because we do not have locks, so we must free the old
device after writing the new one. The same point applies to `service` which
we take care to free after the new `service` is written.
A caveat here is that even if we do not crash it is still possible for one
context to be using an outdated `device` and `service` leading to an error
return. This should be acceptable because it would be a similar situation
to the device being detached during an API call.
Visual Studio version above 2013
Fix compilation errors starting with version 17.14 of Visual Studio 2022
that enforces this warning as error.
References #1649Closes#1677
This fixes an issue where an error that occurred in thread #X will create a false
error message stating that thread #X+1 did not discover the correct number of devices
Closes#1447
Depending on the devices or drivers, open() may return
LIBUSB_ERROR_ACCESS
LIBUSB_ERROR_NOT_FOUND
LIBUSB_ERROR_NOT_SUPPORTED
for devices "out of reach" to libusb.
The const in the libusb_cpu_to_le16() function declaration was useless.
The consts in the libusb_version struct did have an effect, but were
overly restrictive.
Closes#1540
We want to keep only one device open for simplicity, but instead of
closing the last opened device already before /trying/ to open a new
one, close it only if the new one was opened successfully. This way
we may keep the device open longer, and in the simple case where no
later arriving devices could be opened, until it is detached again.
Closes#1550
Signed-off-by: Tormod Volden <[email protected]>
If no device ID is specified, and for instance a hub is plugged, many
devices will be attached (and later detached) at the same time. The old
code counting only 2 events would then exit prematurely.
By counting attach and detach events separately, we preserve the old
behaviour in the single-device case, but also allow more complex
sequences to be handled in a way that mostly appears intuitive.
This is not fool-proof, and we can still end up leaving after a surprise
detachment of a "pre-existing" device while we have one of the "new"
devices open. In this case print a warning.
References #1455
Signed-off-by: Tormod Volden <[email protected]>
There was a mixup of "ret" and "r", so use "r" consistently as
in the rest of the function.
Closes#1636
Signed-off-by: Tormod Volden <[email protected]>
1.0.28 shipped with new SuperSpeed+ functions, but the
LIBUSB_API_VERSION macro had unfortunately not been changed.
Fixes#1642
Signed-off-by: Tormod Volden <[email protected]>
Programs using the previous version may use the new version as drop-in
replacement, but programs using the new version may use APIs not present
in the previous one.
Signed-off-by: Tormod Volden <[email protected]>
Silently ignore invalid values, since we cannot use logging inside
the get_env_debug_level() function.
Fixes#1619
Signed-off-by: Tormod Volden <[email protected]>
Introducing the ANDROID_OS macro that is supposed to be set by clients
of libusb that are running on Android at the OS level.
If Android_OS is set (and HAVE_LIBUDEV is not) then linux netlink event
monitoring (and consequently hotplug functionality) is enabled. This
works for Android services or simple binaries running at the OS level as
they have the necessary permissions, in contrast to usual Android apps
(see commit 2f3bc98).
Note: the __ANDROID__ macro is set by GCC for all targets running on
Android (both OS-level services and apps).
Closes#1577
Signed-off-by: Nathan Hjelm <[email protected]>
Summary of problem:
The LIBUSB_TRANSFER_ADD_ZERO_PACKET flag does not work as expected on
Darwin. On my USB 3 device, with maxPacketSize = 1024, I see libusb
sending ZLP for packets multiple of 5120 instead of 1024.
The ZLPs are not being sent when they should because
darwin_get_pipe_properties() retrieve the "full" wMaxPacketSize via
GetPipePropertiesV3(), which includes bandwidth multipliers.
Summary of solution:
GetEndpointPropertiesV3() does retrieve unaltered wMaxPacketSize,
without multipliers. To call it, we first use GetPipePropertiesV3() to
set the fields required.
Fixes#1581Closes#1582
Fix recent regression introduced in commit 9d595d4.
get_dev_port_number() was always returning port number 0 for any port,
due to the incorrect assumption that strtoll's 'end' pointer will point
to a null terminator on success. This was causing
winusb_get_device_list() to always fail. Since we actually know the
expected values of *end for both the SPDRP_LOCATION_INFORMATION and the
SPDRP_LOCATION_PATHS case, check for those instead.
Additionally, document why a return value of 0 is treated as a failure
in this particular function, for valid but somewhat coincidental reasons
that may not be immediately obvious.
Closes#1544
For the 16-bit case especially, the result of the `or` is implicitly
promoted to `int`, then when returned was warning:
warning: implicit conversion loses integer precision: 'int' to 'uint16_t' (aka 'unsigned short') [-Wimplicit-int-conversion]
Add more casts to shut it up.
Closes#1541
Commit 00454ab0 accidently botched error checking when replacing
rewind() with fseek().
Thanks to GitHub user "anotheruserofgithub" for noticing.
Closes#1539
Instead of filling in the blanks with hard-coded made-up values that are
sometimes correct, use the cached descriptor values retrieved during
enumeration, which should be a better fallback.
References #1360Closes#1378
Signed-off-by: Tormod Volden <[email protected]>
Consider different scenario to return from main, we have to choose the
proper return value. In this way, we can easily check the return code
directly like `echo $?` in Linux to know whether the execution of xusb
is failed.
Closes#1507
Signed-off-by: Yiwei Lin <[email protected]>
The addition could overflow, the upcast needs to be performed before,
not after.
Fixes all clang-tidy bugprone-misplaced-widening-cast warnings
References #1479
Copy-pasting can often result in mistakes like:
```
if (test_value(x)) {
y++;
do_something(x, y);
} else {
y++;
do_something(x, y);
}
```
Thus it is preferable that branch bodies be unique.
Fixes all clang-tidy bugprone-branch-clone warnings
References #1479
These multiplication could indeed have overflowed, but now they are
performed with a bigger type, matching the type they are ultimately
stored in.
Fixes all clang-tidy bugprone-implicit-widening-of-multiplication-result
warnings
References #1479
Depending on the font, an l suffix can look like a 1. Consider "231l".
Thus prefer uppercase.
Fixes all clang-tidy readability-uppercase-literal-suffix warnings
References #1479