1967 Commits

Author SHA1 Message Date
Petr Pazourek
fdab67b14a windows: Allow device GUIDs missing terminating characters or separators
Allow REG_MULTI_SZ strings with multiple GUIDs, and use the first GUID
for enumerating the device interfaces.

This solves issues with e.g. a composite device with four WinUSB
interfaces, where the device implements "Extended Properties OS Feature
Descriptor" with multiple GUIDs. An attempt to open the device fails
with an error LIBUSB_ERROR_NOT_FOUND (-5).

Fixes #1307

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-11-29 17:50:15 +01:00
Nathan Hjelm
d66ffcdd12 darwin: fix potential crash at darwin_exit
If initialization fails before hotplug can be set up then libusb_init will crash when it calls libusb_exit to clean up. This commit fixes the issue by only tearing down hotplug if it has been initialized.

Closes #1275

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-11-28 15:32:39 -07:00
Ingvar Stepanyan
0a2f511914 examples/hotplugtest: Match any device by default
I spent some time trying to figure out why, when I run this example,
it's not showing any events, until I looked into the code and saw it has
some hardcoded VID:PID.

Most people probably don't have the specific device hardcoded here, and
it seems better to default to showing events for any device unless
overridden.

While at it, also updated device detached message to match the attached
one to show VID:PID.

Closes #1350

[Tormod: Print error strings on failures]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-11-27 23:06:32 +01:00
Simon Chan
67ac8a0803 windows: Recognize Samsung composite device driver
Closes #1331
2023-11-05 14:46:00 +01:00
Addison Crump
24d79282af descriptor: Guard against corrupted descriptor length field
A messed-up descriptor could potentially cause an infinite loop.

Also applied to an instance in the Linux backend.

Closes #1308

[Tormod: Rephrase error messages]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-11-05 14:46:00 +01:00
Sylvain Fasel
4b732d9422 windows: No longer wait for device to get an active configuration
This commit essentially reverts commit eb164d75 (from 2018, pre 1.0.23).

Commit eb164d75 was made with the assumption that when a device is not
configured (i.e. it returns CurrentConfigurationValue == 0 during
init_device), then it is only a temporary condition caused by Windows
already listing the device but not having completed its setup yet. The
commit added a delay of up to 1 second (20 x 50ms) for re-obtaining
information and checking until CurrentConfigurationValue would
eventually change to a value > 0. The commit also forced libusb internal
structure to contain 1 as the active configuration even if the wait
failed to retrieve an active configuration, creating a discrepancy in
between the value seen from libusb and actual value managed by Windows.

There is a problem with the above because devices could be in the
unconfigured state for permanent reasons. For instance when a device is
disabled by Windows (either manually using Device Manager or
automatically because of a resource conflict). In this case the device
is listed with CurrentConfigurationValue == 0. Therefore init_device
will add a 1 second delay for each disabled device, at every device
enumeration.

A device which is not configured has no active interface nor available
endpoint. Therefore it cannot be used (except for standard request on
endpoint 0) and therefore shall not be returned by libusb as an active
device. It is the responsibility of the OS to bring the device to a
configured state. When libusb returns active configuration == 1 to a
user, the behavior of actually using this device is not defined.

The potential gain of being able to wait for Windows actually completing
device setup is not in par with the delay caused by the waiting which
has high-level impact (for instance in GUI listing devices) for standard
scenarios.

The initial problem addressed by commit eb164d75 is simply a race
condition occurring (rarely) when libusb enumeration happens exactly at
the instant when Windows is not yet done loading the driver etc. for an
enabled device. Calling init_device slightly earlier would have return
no result for this device, and slightly later would have return a fully
active device. In the first case a new enumeration would be necessary
anyway. Given hot-plug is not supported under Windows it is a good
practice to re-enumerate devices to detect newly plugged ones anyway, so
the race condition could be easily solved by simply enumerating
regularly.

To summarize: This commit reverts to handling the described race
condition by ignoring any non-configured device (not returning them from
get_device_list), instead of waiting for them to maybe get an active
configuration at a later time. The benefit is to avoid introducing
arbitrary large delay in common scenarios where some devices are
disabled at the OS level.

Moreover this commit reverts assigning a wrong value to
priv->active_config, and returning such device from get_device_list,
preventing the risk of later misuse of not configured devices.

Note: Disabled devices might not necessarily be listed under USB devices
in Windows Device Manager, but can be other system peripherals, one
example being Bluetooth chips with HCI USB interfaces.

Closes #1270
2023-11-05 14:46:00 +01:00
Sylvain Fasel
c875f158b5 windows: Define bus numbers without using HCD
Define and assign bus number to root hubs during the hub pass of winusb
libusb_get_device_list, using the definition of a root hub as being a
hub without parent at port 0.

Note: Assigned bus number values for a given setup are not guaranteed to
remain constant when migrating from the previous implementation.

The rationale behind this change:

- Implementation using CM_Get_Child from HCD to get root hub devInst is
not working when using "FabulaTech Virtual USB Host Controller"
(USB-Ethernet bridge) under Hyper-V virtual machine.

- This new way of enumerating is inspired by "USB Device Viewer" Microsoft
driver sample code which is capable of correctly enumerating hubs and
devices in the above mentioned configuration as well as in regular
configurations.

Closes #1202
2023-11-05 14:46:00 +01:00
Tormod Volden
1313049d7d tests: Remove unused function in init_context.c
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-11-05 14:45:37 +01:00
Tormod Volden
188a913b07 Add HACKING file with hints on proper commits
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-11-05 14:36:28 +01:00
Tormod Volden
6bf2db6fea netbsd: Add error number to debug output
Same as for OpenBSD.

References #1295

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-07-07 13:33:18 +02:00
Tormod Volden
cfc66948ac netbsd: Print control transfer request debug values in hex
Same as for OpenBSD.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-07-07 13:33:18 +02:00
Tormod Volden
9891cf2057 netbsd/openbsd: Remove extraneous parentheses around ioctl call
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-07-07 13:33:18 +02:00
Tormod Volden
0a29e4f906 netbsd/openbsd: Drop parentheses around return values
It was used inconsistently, and seems not to be customary any longer
even for OpenBSD developers (https://man.openbsd.org/style).

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-07-07 13:33:18 +02:00
Joshua M. Clulow
681a851445 sunos: Return error if OS paths hit their limits
Closes #1174
2023-07-07 13:33:02 +02:00
Joshua M. Clulow
7218b0dcc6 sunos: Fix error detection in sunos_submit_transfer() 2023-07-07 13:23:50 +02:00
Tormod Volden
ba83b68c2f core: Fix always-true condition in log callback function setting
This could possibly scribble the arg union for other options
although it wouldn't happen with our current set of options.

Fixup of commit 84ac0b0

References #1265

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-07-06 11:06:20 +02:00
Tormod Volden
76df571c15 core: Avoid crash due to premature va_end
After va_end(ap) the variable ap is undefined and cannot be
accessed.

Fixes #1274
Closes #1284

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-07-06 10:14:20 +02:00
Tormod Volden
8450cc93f6 Revert "windows: Fix off-by-one error in HID backend for devices without report IDs"
This reverts commit 69e4ee63c9.

In the commit (from https://github.com/libusb/libusb/pull/1217) it was
wrongly assumed what the transfer length actually includes, and the
change caused wrong values for Output Report and Feature Report.

See also https://github.com/libusb/libusb/pull/1285 where the regression
was first analyzed.

More discussion about lengths reported from HIDAPI point of view:
https://github.com/libusb/hidapi/issues/589

References #1217
References #1285
2023-06-21 13:40:07 +02:00
Alex Feinman
cc498ded18 windows: Refactor parsing of MI_XX string taking into account it's 16-based
Fixes failure to enumerate interfaces above 9 (e.g. MI_0A).

Fixes #1280
Closes #1281
2023-05-19 10:18:12 +02:00
Ludovic Rousseau
07441f5424 Fix github action warnings
build
Node.js 12 actions are deprecated. Please update the following actions to use Node.js 16: actions/checkout@v2. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
2023-05-09 22:48:25 +02:00
Ludovic Rousseau
a8a70f62c0 Fix build errors of tests with --enable-debug-log
The errors were:
../../tests/set_option.c:146:31: error: no member named 'debug' in 'struct libusb_context'
  LIBUSB_EXPECT(==, test_ctx->debug, 4);
                    ~~~~~~~~  ^
../../tests/set_option.c:74:40: note: expanded from macro 'LIBUSB_EXPECT'
    int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
                                       ^~~
../../tests/set_option.c:152:31: error: no member named 'debug' in 'struct libusb_context'
  LIBUSB_EXPECT(==, test_ctx->debug, 4);
                    ~~~~~~~~  ^
../../tests/set_option.c:74:40: note: expanded from macro 'LIBUSB_EXPECT'
    int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
                                       ^~~
2 errors generated.
../../tests/init_context.c:112:31: error: no member named 'debug' in 'struct libusb_context'
  LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_ERROR);
                    ~~~~~~~~  ^
../../tests/init_context.c:74:40: note: expanded from macro 'LIBUSB_EXPECT'
    int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
                                       ^~~
../../tests/init_context.c:140:31: error: no member named 'log_handler' in 'struct libusb_context'
  LIBUSB_EXPECT(==, test_ctx->log_handler, test_log_cb);
                    ~~~~~~~~  ^
../../tests/init_context.c:74:40: note: expanded from macro 'LIBUSB_EXPECT'
    int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
                                       ^~~
2 errors generated.

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
2023-05-09 22:48:01 +02:00
Ludovic Rousseau
3bace666b7 Fix build error with --enable-debug-log
The error is:
core.c:2451:8: error: no member named 'debug' in 'struct libusb_context'
        _ctx->debug = LIBUSB_LOG_LEVEL_NONE;
        ~~~~  ^

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
2023-05-09 22:47:27 +02:00
Ludovic Rousseau
7c169b8f14 Add github action build for --enable-debug-log
Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
2023-05-09 22:46:57 +02:00
Niklas Gürtler
c5aec6bd67 Fix #1263 by acquiring the waiters lock while calling transfer callbacks and swapping the order of operations in sync_transfer_cb
Fixes #1267, #1263

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-04-12 13:06:57 -06:00
Nathan Hjelm
84ac0b0f60 Add support for setting the log callback with libusb_set_option/libusb_init_context
This commit effectively deprecates libusb_set_log_cb by adding support for
setting the callback in either libusb_set_option or libusb_init_context. Since
the libusb_set_log_cb is already in use we can not easily deprecate it without
first incrementing the major version. We may do that in the future.

Closes #1265

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-04-12 13:02:35 -06:00
Xiaofan Chen
fcf0c710ef Do not deprecate libusb_set_debug and libusb_init
These functions are used in a lot of existing downstream code
and the deprecation makes -Werror builds fail.

It seems reasonable to keep these functions supported at least until a
major API overhaul.

Closes #990
Closes #1236
2023-03-12 14:52:23 +01:00
Zeng Guang
3593d5469d windows: Add USBD_STATUS_STALL_PID to usbd_status translation
The value is returned if "The device returned a stall packet
identifier", so translate it to LIBUSB_TRANSFER_STALL.

Closes #1257
2023-03-12 14:52:23 +01:00
Brad Smith
4dbcefaf61 threads_posix: Use proper getthrid() for thread ID on OpenBSD
OpenBSD no longer allows syscalls like this:
https://lwn.net/Articles/806776/

Closes #1247
2023-03-12 14:52:23 +01:00
Tormod Volden
223cf90b27 tests/stress_mt: Also run with device enumeration
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-03-12 14:52:23 +01:00
Tormod Volden
ab61296036 tests/stress_mt: Add Windows threads support
Build the test on all platforms.

Closes #1128

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-03-12 14:52:08 +01:00
Nathan Hjelm
71dd672abe tests: Add unit test for libusb_set_option
The behavior of libusb_set_option was not matching the documentation
when the log level was set in the environment. This has been fixed but a
regression test is needed to ensure the behavior does not deviate. This
commit adds a set of unit tests to cover some of the functionality of
libusb_set_option.

Closes #1245

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
[Xiaofan: Add Windows setenv/unsetenv wrappers]
Signed-off-by: Xiaofan Chen <xiaofanc@gmail.com>
[Tormod: Fix test_no_discovery() build on Linux, add msvc build]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-03-12 12:47:08 +01:00
Nathan Hjelm
d2da7f9b1d libusb_set_options: Adjust semantics of libusb_set_option for log level
This commit updates the actual semantics of:

libusb_set_option(_, LIBUSB_OPTION_LOG_LEVEL, _)

to match the documentation. The documentation states that if the
LIBUSB_DEBUG environment variable is set then that level overrides the
value set by libusb_set_option.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-03-12 12:36:43 +01:00
Tormod Volden
2abb2a186e xusb: Improve Max LUN retrieval stall handling
Do not show an uninitialized Max LUN value if the pipe stalled.

As suggested by the code comment (but not the old code), set a default
zero value only on pipe stall. On other errors simply error out.

References #1181
Closes #1239

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-28 11:53:06 +01:00
Tormod Volden
0dbe62bde3 xusb: Improve kernel driver probe messages
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-28 11:40:16 +01:00
Xiaofan Chen
c2525a034e CI: Add build jobs for MSYS clang32/64
References #1192

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-28 11:39:31 +01:00
Tormod Volden
ec4c3c4317 windows: Only link with --add-stdcall-alias if supported by linker
E.g. the LLVM LLD doesn't support this option.

Closes #1192
References #944

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-28 11:39:17 +01:00
Tormod Volden
9fb5e2edb2 windows: Omit -mwin32 GCC option for MSYS/MinGW
Only keep it for Cygwin.

Clang doesn't support this option, and it seems only needed for
Cygwin since MinGW predefines e.g. the _WIN32 macro anyway.

From GCC documentation: -mwin32  This option is available for Cygwin
and MinGW targets. It specifies that the typical Microsoft Windows
predefined macros are to be set in the pre-processor, but does not
influence the choice of runtime library/startup code.

References #1192

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-28 11:38:53 +01:00
Tormod Volden
54350bd83f Bump LIBUSB_API_VERSION to 0x0100010A
Several API changes targeted for 1.0.27

New API functions:
- libusb_init_context
- libusb_get_max_alt_packet_size
- libusb_get_interface_association_descriptors
- libusb_get_active_interface_association_descriptors
- libusb_free_interface_association_descriptors
- libusb_get_platform_descriptor
- libusb_free_platform_descriptor
(and their associated structures)

New option LIBUSB_OPTION_WINUSB_RAW_IO

NetBSD bus/address changes (not strictly part of API)

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-26 12:51:50 +01:00
Tormod Volden
9e077421b8 xusb: Add newlines in mass-storage test output
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-21 14:03:07 +01:00
Tormod Volden
5b7d57e61a Fix most -Wpedantic warnings
On Linux and gcc 12.2 at least.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-21 11:30:06 +01:00
xloem
a92e21baa7 linux: Keep the no_device_discovery option per context
Fixes #1003
Closes #1231

[Tormod: Adapted to current master]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-21 11:26:55 +01:00
Orhan aib Kavrakoglu
9b42fdd787 core: Add libusb_get_max_alt_packet_size()
Closes #1167
2023-01-21 11:26:11 +01:00
Orhan aib Kavrakoglu
94eb23991b core: Add helper find_alt_endpoint() 2023-01-20 17:51:34 +01:00
Orhan aib Kavrakoglu
6c2149662a core: Factor out helper get_endpoint_max_packet_size()
[Tormod: Initialize r to please compilers]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-20 17:50:55 +01:00
Edgar Fuß
b1445d9146 netbsd: Map NetBSD's udi_bus/udi_addr to libusb's 1-based API
NetBSD's udi_bus/udi_addr values in struct usb_device_info start from 0.

However, libusb's bus_number/device_address in struct libusb_device use
0 to represent a missing value and the libusb API states that a value of
0 returned by libusb_get_bus_number() or libusb_get_device_address()
means "not available" so applications may refuse to match on zero
values.

Adjust between the two worlds by adding an offset of 1.

Closes #1230
2023-01-20 11:05:01 +01:00
Martin Ling
9e4bb9cbe5 windows: Add option for WinUSB RAW_IO endpoint policy
Add support for enabling or disabling the WinUSB RAW_IO pipe policy for
a given endpoint, which is documented here:

https://learn.microsoft.com/en-us/windows-hardware/drivers/usbcon/winusb-functions-for-pipe-policy-modification

This is necessary to increase performance. Without this option the
WinUSB backend will only queue one inbound operation at a time, even if
the libusb async API is used to submit multiple transfers.

For real-time sampling devices with high sample rates and small buffers,
the use of RAW_IO combined with queued async transfers is essential to
maintaining the necessary throughput and avoiding lost samples or buffer
overruns.

Examples of devices affected include Cypress FX2 based logic analyzers
accessed using Sigrok, and the HackRF software defined radio.

The new option must be set by calling libusb_set_option with the arguments:

libusb_set_option(ctx, LIBUSB_OPTION_WINUSB_RAW_IO, dev_handle,
                  endpoint_address, enable, max_transfer_size_ptr)

where the types of the variadic arguments are:

libusb_device_handle *dev_handle;
unsigned int endpoint_address;
unsigned int enable;
unsigned int *max_transfer_size_ptr;

The dev_handle and endpoint_address parameters must identify a valid IN
endpoint on an open device. If enable is nonzero, RAW_IO is enabled,
otherwise it is disabled. Unless max_transfer_size_ptr is NULL, then on
a successful call to enable RAW_IO, the pointer destination will be
written with the MAXIMUM_TRANSFER_SIZE value for the endpoint.

Whilst RAW_IO is enabled for an endpoint, all transfers on that endpoint
must meet the following two requirements:

- The buffer length must be a multiple of the maximum endpoint packet size.
- The length must be less than or equal to the MAXIMUM_TRANSFER_SIZE value.

This option should not be changed when any transfer is in progress on
the specified endpoint.

This option only affects the WinUSB backend. On other backends it is
ignored and returns LIBUSB_ERROR_NOT_SUPPORTED, without modifying the
value pointed to by max_transfer_size_ptr.

A great deal of credit is due to Petteri Aimonen and Patrick Stewart for
previous work, and to everyone else who participated in discussions.

Fixes #490
Closes #1208
2023-01-20 11:05:01 +01:00
Martin Ling
aac2e12e5f windows: Add GetPipePolicy to struct winusb_interface 2023-01-20 11:05:01 +01:00
Nathan Hjelm
3c33e499a0 Update tests and example to use the new libusb_init_context() function
This commit updates all test and example code to use the newer
libusb_init_context() function instead of libusb_init().

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
[Tormod: Update umockdev.c as well]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-20 11:04:14 +01:00
Nathan Hjelm
6622f386f5 core: Add libusb_init_context() setting options at context creation
The new initialization function addresses some shortcomings of the
libusb_set_option() interface. Namely, it allows setting the
no-enumeration option (and others) on only the contexts where it is
requested. The old initialization function (libusb_init()) is deprecated
and will be removed in a future release. For now it translates to a call
to libusb_init_context() with no options specified.

Closes #1026

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
[Tormod: Doxygen description of libusb_init_option structure]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-20 10:16:43 +01:00
Dominik Boehi
82d912307f descriptor: Add support for BOS platform descriptor parsing
Also add BOS platform descriptor dump to xusb example.

Closes #1133

[Tormod: Fixed copy-pasto in Doxygen comment]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-19 15:04:48 +01:00