1967 Commits

Author SHA1 Message Date
Ryan Metcalfe
99f1942e81 xusb: Print info from IAD descriptors
Signed-off-by: Ryan Metcalfe <ryan.metcalfe@novanta.com>
2023-01-19 15:02:11 +01:00
Ryan Metcalfe
5ec2821fdc windows: Allow claiming all interfaces for a WINUSBX composite function
On Windows 10, when a USB composite device is using usbccgp to manage
the parent device, there can be problems when an application tries to
claim multiple interfaces for a WINSBX-enabled subfunction. For example,
a common scenario is that an application is able to claim the first
interface of a given child function, but not the others.

This commit attempts to resolve this issue by making use of
(potentially) available interface association descriptors (IADs).

Within set_composite_interface(), which is normally called for the first
interface of a composite child function (Some MI_XX device, where XX is
the first interface), the set of IADs for the current configuration is
retrieved and, if the current interface matches the 'bFirstInterface'
for one of the returned IADs then the API backend, sub_api is also set
up for the other 'associated' interfaces. Subsequent calls to
libusb_claim_interface for any of these 'associated' interfaces will get
properly routed to winusbx_claim_interface.

Two fields have been added to winusb_device_priv.usb_interface:
  num_associated_interfaces
  first_associated_interface

These are made use of by winusbx_claim_interface() to decide whether
Initialize() or GetAssociatedInterface() function calls are needed, as
well as within winusbx_close() to perform proper cleanup for groups of
associated functions.

About composite functions in Windows (by Maciej T. Nowak):

(1) Non-composite device: Single device is created with all interfaces
grouped (associated) together. WinUSB_Initialize gives access to first
interface and WinUSB_GetAssociatedInteface can be used to get access to
remaining interfaces.

(2) Composite device without IAD is presented by set of devices each
containing single interface. As they are separate devices, separate
WinUSB_Initialize calls are required to gain access to each interface.
As there are no more interfaces in each device,
WinUSB_GetAssociatedInterface has no use in this case. In other words:
Each interface is grouped (associated) with itself.

(3) When we add IAD to a composite device, interfaces specified in each
IAD are grouped (associated) into single devices where WinUSB_Initialize
is required to open the first interface in each association and
WinUSB_GetAssociatedInterface for the remaining interfaces in each
group. Effectively this case is a mix of (1) and (2).

Closes #965

Signed-off-by: Ryan Metcalfe <ryan.metcalfe@novanta.com>
[Tormod: Removed redundant is_associated_interface member]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-19 15:02:11 +01:00
Ryan Metcalfe
809a6df614 descriptor: Introduce interface association descriptors (IAD)
Types:
  struct libusb_interface_association_descriptor
  struct libusb_interface_association_descriptor_array

Accessor / cleanup functions:
  libusb_get_interface_association_descriptors
  libusb_get_active_interface_association_descriptors
  libusb_free_interface_association_descriptors

Signed-off-by: Ryan Metcalfe <ryan.metcalfe@novanta.com>
[Tormod: Fixed Doxygen comment]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-19 15:01:22 +01:00
Edgar Fuß
e263e32f20 netbsd: Add comment about udi_speed values matching libusb enum
Point out that only because NetBSD's #define's for USB speeds happen to
match the corresponding libusb enum values, it's OK to assign struct
usb_device_info's udi_speed field to the struct libusb_devices's speed
field.

References #1230
2023-01-18 14:05:37 +01:00
Sonatique
285b292d73 msvc: Add configurations with /MT compilation flag
Allows statically linking vc++ runtime dependencies inside libusb-1.0
Windows libraries.

'Release' and 'Debug' configurations produce /MD compiled binaries while
the new 'Release-MT' and 'Debug-MT" configurations produce /MT compiled
binaries.

The /MT flag causes the application to include the multithread, static
version of the vc++ run-time library, whereas the default flag /MD will
cause the vc++ run-time library to be dynamically linked run-time. Using
/MT thus builds a standalone libusb DLL that doesn't require a vc++
runtime DLL to be shipped along with it.

For the official description of /MT see:
https://docs.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library

Closes #1188
2023-01-18 13:45:23 +01:00
Nathan Hjelm
17bf45baee Add tags and cscope.out to .gitignore
Fixes #1061

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-01-09 21:15:50 -07:00
Sylvain
f2fd5bb6a3 git ignore "build" folder
Fixed #1203

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-01-09 20:48:08 -07:00
Francis Hart
565bb18011 Fix setting log level before libusb_init()
The header docs state its possible to set a default log level before
calling libusb_init(), and that this log level will be used for all
contexts created after (to quote):

    "Note that libusb_set_option(NULL, ...) is special, and adds
     an option to a list of default options for new contexts."

This updates the logic inside libusb_init() to ensure this
behaviour is followed.

Fixes #1207

Signed-off-by: Francis Hart <francis@kuvacode.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-01-09 20:47:02 -07:00
Alexander Mot
b09074ea77 windows: Define __func__ for vs2015
The __func__ macro is defined by the C99 standard (but not C++99/03).

The default C compiler in msvc implements C89, plus Microsoft extensions,
some of which are part of C99.

It appears that both vs2013 and vs2015 don't define __func__.

Closes #1170
2023-01-07 00:19:04 +01:00
Martin Ling
d014a11545 docs: Clear endpoint halt issued after cancellation only for macOS < 10.5
Also remove comment about possible endpoint halt, which seems not
correct, as discussed in issue #1110.

References #1110
Closes #1117

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-06 23:33:57 +01:00
Nathan Hjelm
65e4a70e0d darwin: Do not clear device data toggle on macOS 10.5 or newer
Historically Mac OS X always cleared the data toggle on the host side. For
consistency, libusb has been calling ClearPipeStallBothEnds to also clear the
device side toggle. Newer versions of the IOUSBLib do not clear the host side
toggle so there is no need to make this call. Additionally, some buggy devices
may fail to correctly implement clearing the data toggle.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
[Tormod: Return result from AbortPipe]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-06 23:33:29 +01:00
Rosen Penev
1c6e76a483 windows: Remove __stdcall libusb_set_option from .def file
libusb_set_option uses variable arguments and therefore cannot use
__stdcall.

In practice, this does not get exported anyway. Verified under MinGW's
objdump tool with 32 and 64-bit combinations.

Closes #1197

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2022-12-20 00:17:24 +01:00
Tormod Volden
1c99382a3d core: Do not warn about new context as implicit default
Only leave a debug message.

Fixes #1215

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-12-19 22:13:26 +01:00
Tormod Volden
49cb073626 CI: Run multithreaded stress test on Linux
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-12-19 21:50:51 +01:00
Tormod Volden
47751f0b7d tests: Add multithreaded stress test for Posix platforms
Thanks to Ilya Averyanov for initial version.

This test detects issues like #1124.

Adaptation to Windows threads is on the wishlist (#1128).

References #1124
References #1128
Closes #1189

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-12-19 21:50:51 +01:00
Mario Kleiner
69e4ee63c9 windows: Fix off-by-one error in HID backend for devices without report IDs
When the windows HID backend is used for interrupt or bulk transfers on
USB-HID compliant devices which don't use HID report IDs, the current
code properly adds a zero prefix byte for report ID 0x00 before an OUT
transfer, and properly strips the leading zero prefix byte from buffers
received by IN transfers. Length of transmitted data is increased by +1
accordingly to account for the extra prefix byte.

However, on transfer completion the length of sent or received data is
not adjusted back again by one. This leads to misreporting of effective
transfer length for both OUT and IN transfers, reporting one byte too
much. It also causes a memcpy for IN transfers which copies one byte of
data too much into the client provided target buffer
transfer_priv->hid_dest, appending an extra zero byte at the end and
thereby writing one byte past the size of the client target buffer. This
could lead to memory corruption or a access violation if the client
application is unlucky.

This commit fixes the problem by detecting this type of HID transfers
and adjusting the effective length of the transfer down one byte.

The fix has been successfully tested with a Griffin PowerMate USB-HID
device that doesn't support report IDs, performing interrupt transfers
from/to the device.

Closes #1217

Signed-off-by: Mario Kleiner <mario.kleiner.de@gmail.com>
2022-12-19 21:49:50 +01:00
Ihor Dutchak
bed8d3034e darwin/windows: Find interface descriptor by bInterfaceNumber
For some (non USB-compliant) devices the interface number doesn't match
its index in the list of interfaces in the configuration descriptor. For
robustness and consistency across platforms, find the descriptor by
iterating over the list to match the bInterfaceNumber.

References #1093
References #1039
References #1175
Closes #1191

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-12-19 21:39:25 +01:00
Pablo Prietz
18a64374e1 core: Remove leftover usbi_mutex_unlock in libusb_set_interface_alt_setting
Fixup of commit 1a08aa8.

Closes #1200
2022-12-19 21:38:21 +01:00
Tormod Volden
f3619c4078 windows: Silence warnings when building without logging
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-09-04 21:11:48 +02:00
Rosen Penev
be33e58489 windows: Fix build with --disable-log
On Windows we use UNREFERENCED_PARAMETER for UNUSED (commit 521105f).
UNREFERENCED_PARAMETER evaluates to {(ctx) = (ctx);} which errors out:
  error: lvalue required as left operand of assignment.
in the cases where ctx is NULL.

Closes #1152

[Tormod: Add ctx reference to avoid unused variable warnings]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-09-04 21:11:09 +02:00
Rosen Penev
5c27abd59e windows: Mark libusb_set_option as WINAPIV
WINAPI evaluates to __stdcall, which does not work with variadic
functions. MSVC and MinGW implicitly demote this to __cdecl (clang does
too and also warns about it). Instead of having this implicit, make it
explicit.

Closes #1160

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2022-09-04 21:10:40 +02:00
Tormod Volden
687bf21a7d windows: Avoid invalid dereference in debug print
The device structure is not always valid at this point.

Fixes #1179
Closes #1186

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-09-03 11:58:01 +02:00
Tormod Volden
432bc0954d core: Avoid mutex lock while printing error and warning
Closes #1187

Reviewed-by: Dmitry Zakablukov <dimaz@passware.com>
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-09-03 11:55:10 +02:00
Ihor Dutchak
2ca64b364b Fix possible memory leak of configuration descriptor (Darwin/Windows)
The config descriptor was not released in some (rare) cases:

- Darwin: never released in case GetPipeProperties failed
- Windows: not released for some non-USB compliant descriptors

Follow-up of commit f5275f9a and commit aa1d76cd
References #1093
References #1039
Closes #1183

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-09-03 11:46:21 +02:00
Ingvar Stepanyan
ff8fe9397d Add Emscripten backend for WebAssembly + WebUSB support
Fixes #501
Closes #1057
2022-06-26 20:44:54 +02:00
Tormod Volden
4689bd50d0 windows: Correct function name in comment
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-06-26 17:31:14 +02:00
Shawn Hoffman
c060e9ce30 windows: Use sdk-defined GUIDs where possible
Closes #1115
2022-06-26 17:09:14 +02:00
Shawn Hoffman
44e1c533e2 msvc: Correct usage comments about linking
Closes #1114
2022-06-26 17:09:14 +02:00
Shawn Hoffman
0ff79bc2ad msvc: Implement LIBUSB_DEPRECATED_FOR
Closes #1113
2022-06-26 17:09:14 +02:00
Shawn Hoffman
273f808101 windows: Whitespace fix
Closes #1111
2022-06-26 17:09:14 +02:00
Shawn Hoffman
aa633daaac msvc: Rework msbuild files
Refactor common settings into .props files. Enables building full
PlatformToolset, Platform, Configuration matrix easily.

Also update the appveyor file. Technically we no longer need different
images (could just have a single vs2022 image which is executed multiple
times in parallel, or so)

Closes #1116
2022-06-26 17:09:14 +02:00
Tormod Volden
cd8078db05 core: Remove select case not possibly reached
References #1067

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-06-26 17:09:14 +02:00
Lonnie Abelbeck
95e601ce11 configure.ac: Link with -latomic only if no atomic builtins
Follow-up to 561dbda, a check of GCC atomic builtins needs to be done
first.

I'm no autoconf guru, but using this:
0df485c285/configure.ac (L469)
as inspiration, I created a pre-check before calling AC_SEARCH_LIBS(...)

Fixes #1135
Closes #1139
2022-06-26 17:09:14 +02:00
Ludovic Rousseau
0c09ba6daf Doxygen: Improve libusb_wrap_sys_device() documentation
Create links to:
- libusb_set_option()
- LIBUSB_OPTION_NO_DEVICE_DISCOVERY
2022-06-26 17:09:14 +02:00
Ludovic Rousseau
69a0145d6f Doxygen: Add references to LIBUSB_ERROR codes
Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
2022-06-26 17:09:14 +02:00
Rosen Penev
5691f303ad windows: Add clock_gettime check
Under MSYS2, the windows usbi_get_monotonic_time interferes with the
static inline function under libusbi.h

Closes #1151
2022-06-26 17:09:14 +02:00
Sean McBride
fa9a25b3c4 examples: Replace most uses of sprintf with safer snprintf
Closes #1155
2022-06-26 17:09:14 +02:00
Tormod Volden
347d21a385 windows: Pass on returned isochronous packet status on libusbK
Closes #1109

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-06-26 17:09:14 +02:00
Sean McBride
d520f4d5e9 Simplify clock_gettime() conditionalization and fallback on macOS
Removed the logic in configure.ac (for Darwin only) that detected if the
clock_gettime() function exists.  Instead, just use the preprocessor to
introspect the macOS SDK version and deployment target and call
clock_gettime() if they are new enough.

Also replaced the fallback code for older macOS with
mach_absolute_time() and gettimeofday(), which are simplier because they
do not need setup and teardown carefully balanced and their usage is in
one place instead of spread in multiple places in the file.

Closes #1080
2022-06-26 17:09:07 +02:00
Nathan Hjelm
ba698478af config: allow configure to continue without pkg-config installed
libusb only uses pkg-config macros in configure to check for umockdev. Since
this is only required for testing it makes sense to protect the usage of these
macros to only be used when pkg-config is installed. This will be the case for
maintainers and testers.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2022-04-15 21:41:08 -06:00
Nathan Hjelm
4e2de881cf darwin: add missing locking around cached device list cleanup
The cleanup function darwin_cleanup_devices was intented to be called only once
at program exit. When the initialization/finalization code was changed to
destroy the cached device list on last exit this function should have been
modified to require a lock on darwin_cached_devices_lock. This commit updates
cleanup to protect the cached device list with the cached devices mutex and
updates darwin_init to print out an error and return if a reference leak is
detected.

Also using this opportunity to correct the naming of the mutex. Changed
darwin_cached_devices_lock to darwin_cached_devices_mutex. Also cleaning the
initialization code up a bit. Removing the context pointer from the hotplug
thread as it is not used for anything but logging.

Fixes #1124

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2022-04-14 10:31:39 -06:00
Craig Hutchinson
a8f8d4ba3d Fix exit order to match reverse of init
This commit resolve the ordering of 'exit' dependencies as was started for 'init' in 0846456f3a

(Relates to https://github.com/xloem/libusb/pull/5)

Closes #1126

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2022-04-13 08:47:50 -06:00
Tormod Volden
4239bc3a50 libusb 1.0.26
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-04-10 10:57:16 +02:00
Martin Ling
f1fafe8ac7 darwin: Document OS-specific behaviour of libusb_cancel_transfer
The behaviour of libusb_cancel_transfer is different on Darwin:

- Cancellation cancels all transfers on the same endpoint.

- A ClearFeature(ENDPOINT_HALT) request is sent after cancellation.

This documents both differences and their implications.

Fixes #1110
Closes #1121
2022-04-09 12:34:05 +02:00
Benjamin Berg
ff05357a83 darwin: Fix crash in log handler when stopping event thread
The darwin event thread (in contrast to other OS implementations) tries
to log to the context that created it. However, this context is only
guaranteed to be valid until the thread has started. It may be that
another context is the last one to be destroyed, causing the event
thread to log using an already destroyed context.

Fix this by only passing on ctx where it is acceptable.

Fixes #1108
2022-04-09 10:00:13 +02:00
Shawn Hoffman
32f3c6db95 windows: Silence zero-sized array warning in libusb.h for msvc
Silence "nonstandard extension used : zero-sized array in struct/union"
(C4200) warning caused by use of ZERO_SIZED_ARRAY in structures defined
in libusb.h when compiling with msvc.

A libusb user including the header from a c++ file is more likely to be
annoyed by the warning because it is activated at level 2 as opposed to
being included from a C file, where it's not activated until level 4.
(on msvc, level 4 is the last step before -Wall).

This causes problems when you also enable -Werror for your own source,
since the libusb header just gets merged into the compilation unit.

9>hotplugtest.c
9>C:\src\libusb\libusb\libusb.h(786,11): error C2220: the following warning is treated as an error
9>C:\src\libusb\libusb\libusb.h(786,11): error C2220: 	uint8_t  dev_capability_data[ZERO_SIZED_ARRAY];
9>C:\src\libusb\libusb\libusb.h(786,11): error C2220: 	         ^
9>C:\src\libusb\libusb\libusb.h(786,11): warning C4200: nonstandard extension used: zero-sized array in struct/union
9>C:\src\libusb\libusb\libusb.h(786,11): warning C4200: 	uint8_t  dev_capability_data[ZERO_SIZED_ARRAY];
9>C:\src\libusb\libusb\libusb.h(786,11): warning C4200: 	         ^
9>C:\src\libusb\libusb\libusb.h(811,47): warning C4200: nonstandard extension used: zero-sized array in struct/union
5>C:\src\libusb\libusb\libusb.h(811,47): warning C4200: 	struct libusb_bos_dev_capability_descriptor *dev_capability[ZERO_SIZED_ARRAY];
5>C:\src\libusb\libusb\libusb.h(811,47): warning C4200: 	                                             ^
9>C:\src\libusb\libusb\libusb.h(1284,38): warning C4200: nonstandard extension used: zero-sized array in struct/union
9>C:\src\libusb\libusb\libusb.h(1284,38): warning C4200: 	struct libusb_iso_packet_descriptor iso_packet_desc[ZERO_SIZED_ARRAY];
9>C:\src\libusb\libusb\libusb.h(1284,38): warning C4200: 	                                    ^
9>Done building project "hotplugtest.vcxproj" -- FAILED.

Closes #1112
2022-04-06 21:42:07 +02:00
Tormod Volden
bfbef179b1 windows: Update isochronous OUT packet actual length
On Windows, unlike other platforms, the isochronous packet actual_length
value is not set on completion of OUT transfers. However, our API
requires the user to check this value for isochronous transfers instead
of the transfer actual_length, if the transferred length is of interest.

The usbd Length field is not used for isochronous OUT transfers:
https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/usb/ns-usb-_usbd_iso_packet_descriptor

To make it consistent with other platforms, just return the requested
length.

Fixes #1105
Closes #1107

Tested-by: Xiaofan Chen <xiaofanc@gmail.com>
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-04-04 13:38:45 +02:00
Tormod Volden
bfa8b8535a windows: Fix type mismatch in winusbx_copy_transfer_data
We cannot use the CHECK_WINUSBX_AVAILABLE macro since it
may return with the wrong error type/value.

Fixes #1100
Closes #1101

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-04-04 13:32:23 +02:00
Tormod Volden
385eaafba1 CI: Add MinGW build on MSYS2 (Windows)
Closes #1102

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-04-04 13:30:49 +02:00
Tormod Volden
78de7dc4b9 libusb 1.0.26-rc1
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-03-27 19:37:43 +02:00