platform/upstream/libusb.git
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>
3 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>
3 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>
3 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>
3 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>
3 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>
3 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>
3 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>
3 years agoWindows: Filter out non-libusb I/O completions
Chris Dickens [Sat, 16 Jan 2021 01:18:34 +0000 (17:18 -0800)]
Windows: Filter out non-libusb I/O completions

The I/O completion port thread gets notified of every I/O operation that
completes for a given HANDLE, but not all I/O operations originate from
within libusb. For example, libusbK performs a number of I/O control
calls during initialization and specific device operations, such as
setting an interface alternate setting. These non-libusb operations need
to be ignored as the OVERLAPPED associated with such operations is not
tied to a libusb_transfer structure.

Resolve this situation by using the libusb device handle as the I/O
completion port's unique key and keeping a list of active transfers for
each device handle. When the I/O competion port thread is notified of an
I/O completion, it will first match the OVERLAPPED to an outstanding
transfer before acting on it.

Closes #844

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoconfigure.ac: Consider Mac OS X target version for clock_gettime()
Chris Dickens [Fri, 15 Jan 2021 20:42:35 +0000 (12:42 -0800)]
configure.ac: Consider Mac OS X target version for clock_gettime()

The AC_CHECK_FUNCS() macro checks whether a program compiled on the host
will link against the function, but it does not help when targeting
older systems where that function is not available. Additionally, the
way that autoconf compiles the program ignores any attributes or
preprocessor guards that installed headers might have applied to that
function.

Resolve this by verifying that the OS X target version is 10.12 or newer
before checking for clock_gettime().

Closes #836

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolinux_usbfs: Fix exit crash on Android
Mark Kuo [Sun, 10 Jan 2021 05:58:26 +0000 (16:58 +1100)]
linux_usbfs: Fix exit crash on Android

In commit "89b810ec Android: Add option LIBUSB_OPTION_WEAK_AUTHORITY
to support used in apk", we should also check weak_authority when
exiting the backend.

[dickens] Fixed whitespace

Closes #842

Signed-off-by: Mark Kuo <starryalley@gmail.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agowindows: Update link about control transfer size limits
Tormod Volden [Tue, 5 Jan 2021 18:46:39 +0000 (19:46 +0100)]
windows: Update link about control transfer size limits

Closes #839

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolibusb/core.c: bump DISCOVERED_DEVICES_SIZE_STEP to 16
Mikolaj Kucharski [Fri, 1 Jan 2021 15:43:29 +0000 (15:43 +0000)]
libusb/core.c: bump DISCOVERED_DEVICES_SIZE_STEP to 16

On OpenBSD USB controllers are shown as normal devices, making the
initial limit too small. On a recent machine this value is almost
always exceeded, so bump it.

Change committed to OpenBSD ports tree by Matthias Kilian <kili@openbsd.org>
on Tue Jun 19 21:10:59 2012 UTC

Closes #835

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoPrefix hex values with 0x in core.c and os/openbsd_usb.c
Mikolaj Kucharski [Sun, 27 Dec 2020 14:02:49 +0000 (14:02 +0000)]
Prefix hex values with 0x in core.c and os/openbsd_usb.c

It makes it clear from debug logs, that values are in hex,
without a need to check the source code.

Closes #832

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoUpdate changelog in preperation for rolling 1.0.25
Nathan Hjelm [Wed, 13 Jan 2021 22:59:33 +0000 (15:59 -0700)]
Update changelog in preperation for rolling 1.0.25

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoMacOS: Silence pipe error in set_interface_alt_setting()
Freek Dijkstra [Mon, 11 Jan 2021 00:49:42 +0000 (01:49 +0100)]
MacOS: Silence pipe error in set_interface_alt_setting()

darwin_set_interface_altsetting() no longer returns the status of the underlying SetAlternateInterface(), but instead the result of a subsequent GetNumEndpoints() call. This reverts to the behaviour prior to commit 065e586.

Since darwin_set_interface_altsetting() resets the interface after SetAlternateInterface(), one of the consequences is that if SetAlternateInterface() returns a kIOUSBPipeStalled error, and the interface is successfully reset, darwin_set_interface_altsetting() now returns LIBUSB_SUCCESS instead of LIBUSB_ERROR_PIPE.

Closes: #838

Signed-off-by: Freek Dijkstra
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoWindows: Break dependency on OLE32
Chris Dickens [Sat, 19 Dec 2020 23:33:56 +0000 (15:33 -0800)]
Windows: Break dependency on OLE32

The OLE32 library provides the IIDFromString() function to parse a
string to a GUID, but we can implement a simple GUID parsing function
and remove this dependence. Doing so also avoids fetching the Unicode
form of the "DeviceInterfaceGUIDs" property.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Cleanup header includes and definiions
Chris Dickens [Sat, 19 Dec 2020 21:30:23 +0000 (13:30 -0800)]
Windows: Cleanup header includes and definiions

Now that a sufficiently recent toolchain is required to build, we can
drop a bunch of redundant definitions and get the definitions from the
Windows headers instead. We can also remove a number of '#ifndef'
guards because no header included by the source would have defined the
symbols being protected.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agotravis: Turn off Homebrew updates
Chris Dickens [Sat, 19 Dec 2020 21:29:38 +0000 (13:29 -0800)]
travis: Turn off Homebrew updates

The builds no longer succeed on the xcode9.4 image because updating
Homebrew takes too much time. The packages we need from Homebrew are not
frequently updated, so using the latest formulae that the image provides
should be sufficient.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoUse stdatomic instead of gcc-internal atomics
Nathan Hjelm [Thu, 17 Dec 2020 03:57:57 +0000 (20:57 -0700)]
Use stdatomic instead of gcc-internal atomics

The __atomic operations are internal to gcc and not necessarily supported
by all c11 compilers. Use the atomics in stdatomic instead.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agocore: Introduce atomic type and operations
Chris Dickens [Wed, 16 Dec 2020 18:35:45 +0000 (10:35 -0800)]
core: Introduce atomic type and operations

An atomic variable is useful for reference counting and is much less
overhead than accessing such a variable while holding a lock. To that
end, replace the libusb_device 'refcnt' variable with an atomic and use
the atomic operations to manipulate it. This removes the need for the
mutex in the libusb_device.

Also convert the 'attached' variable to an atomic as well. This variable
was previously accessed both while holding the libusb_device mutex and
not.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolinux_usbfs: Fix parsing of descriptors for multi-configuration devices
Chris Dickens [Sun, 13 Dec 2020 23:49:19 +0000 (15:49 -0800)]
linux_usbfs: Fix parsing of descriptors for multi-configuration devices

Commit e2be556bd2 ("linux_usbfs: Parse config descriptors during device
initialization") introduced a regression for devices with multiple
configurations. The logic that verifies the reported length of the
configuration descriptors failed to count the length of the
configuration descriptor itself and would truncate the actual length by
9 bytes, leading to a parsing error for subsequent descriptors.

Closes #825

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoCorrect a typo in the Changelog and clean up a stray file
Chris Dickens [Sun, 13 Dec 2020 23:46:27 +0000 (15:46 -0800)]
Correct a typo in the Changelog and clean up a stray file

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolibusb 1.0.24 upstream/1.0.24
Chris Dickens [Thu, 10 Dec 2020 18:53:59 +0000 (10:53 -0800)]
libusb 1.0.24

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoautotools: fix out-of-tree building of documentation
Aurelien Jarno [Wed, 2 Dec 2020 22:46:40 +0000 (23:46 +0100)]
autotools: fix out-of-tree building of documentation

When building libusb out-of-tree, ie when running configure from another
directory, building the documentation fails with:

  $ make -C doc
  make: Entering directory '/tmp/build/doc'
  make: *** No rule to make target 'libusb.png', needed by 'api-1.0'.  Stop.
  make: Leaving directory '/tmp/build/doc'

This has likely been broken by commit 4a5540a925e4 ("autotools: Fix a number of
issues"). This patch fixes that by prefixing libusb.png with @top_srcdir@/doc.

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
3 years agofix eol of msvc/*.vcxproj and msvc/*.vcxproj.filters files
Mikolaj Kucharski [Mon, 30 Nov 2020 15:51:34 +0000 (15:51 +0000)]
fix eol of msvc/*.vcxproj and msvc/*.vcxproj.filters files

Closes #812
Closes #813

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolibusb 1.0.24-rc1
Chris Dickens [Sat, 28 Nov 2020 05:56:30 +0000 (21:56 -0800)]
libusb 1.0.24-rc1

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Fix a few more benign compiler warnings
Chris Dickens [Sat, 28 Nov 2020 02:42:52 +0000 (18:42 -0800)]
Windows: Fix a few more benign compiler warnings

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Support building dpfp and sam3u_benchmark with MSVC
Chris Dickens [Sat, 28 Nov 2020 02:39:53 +0000 (18:39 -0800)]
Windows: Support building dpfp and sam3u_benchmark with MSVC

Closes #151

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Add missing header files to MSVC projects
Chris Dickens [Fri, 27 Nov 2020 23:56:02 +0000 (15:56 -0800)]
Windows: Add missing header files to MSVC projects

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agobuild: Prepare config.h for inclusion in examples/tests
Chris Dickens [Fri, 27 Nov 2020 23:22:29 +0000 (15:22 -0800)]
build: Prepare config.h for inclusion in examples/tests

There are certain games played in the examples and tests source to
account for differences in build environments and target platforms. This
can be simplified by including config.h and using the definitions there.

To that end, move the printf function attribute definition from
libusbi.h to config.h and leverage it where it is used in the examples
and tests.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agobuild: Fix Android and Xcode compilation errors/warnings
Chris Dickens [Mon, 16 Nov 2020 21:03:52 +0000 (13:03 -0800)]
build: Fix Android and Xcode compilation errors/warnings

Commit f69548c3b3 ("examples: Enable all examples to build on all
platforms") modified dpfp and sam3u_benchmark to be buildable on any
platform, however there were some oversights and regressions introduced
for Android and Xcode.

Update the Android and Xcode build files to account for the removal of
dpfp_threaded.c as well as the inclusion of config.h from examples
and/or tests source.

Additionally switch the threaded version of dpfp to use sem_open()
instead of sem_init() as the latter is in fact deprecated on MacOS.

Closes #808

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoexamples: Enable all examples to build on all platforms
Chris Dickens [Tue, 15 Sep 2020 21:15:00 +0000 (14:15 -0700)]
examples: Enable all examples to build on all platforms

Prior to this change, the dpfp and sam3u_benchmark examples were only
built on POSIX platforms due to a dependency on sigaction(). Furthermore
the dpfp_threaded example only worked with POSIX threads.

Lift this limitation by breaking the dependence on sigaction(). Also
provide a minimal threading abstraction so that dpfp_threaded can be
built on Windows as well.

Also merge the sources for dpfp and dpfp_threaded. The only difference
between the two is how libusb's event handling functions are called and
this can be easily handled within a single source file.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: ignore all broken devices during enumeration
Ihor Dutchak [Mon, 9 Nov 2020 23:39:28 +0000 (01:39 +0200)]
Windows: ignore all broken devices during enumeration

- explicitly check/log info, when a device has broken descriptor;

Closes #791
Closes #806

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoFix various CI build warnings
Chris Dickens [Mon, 9 Nov 2020 23:51:44 +0000 (15:51 -0800)]
Fix various CI build warnings

  * [-Wpointer-arith] arithmetic on a pointer to void is a GNU extension
  * [-Wswitch-enum] enumeration values 'E1, ...' not explicitly handled in switch
  * [-Wunused-parameter] unused parameter 'p'

For '-Wswitch-enum', the switch statements in the individual backends'
set_option() function has been removed. It is not expected that backends
will need to handle or be aware of all the options.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agotravis: Update build environments
Chris Dickens [Mon, 9 Nov 2020 19:01:10 +0000 (11:01 -0800)]
travis: Update build environments

Add Ubuntu's Focal distribution to get the latest Linux toolchain.

Add Xcode 12.2 to get the latest MacOS toolchain.

Remove Xcode 7.3 as it is now obsolete and will not successfully build.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agotests: Refactor testlib to simplify and fix Cygwin build
Chris Dickens [Sun, 13 Sep 2020 22:30:04 +0000 (15:30 -0700)]
tests: Refactor testlib to simplify and fix Cygwin build

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolibusb.h: Base Class EFh (Miscellaneous) was missing in class definition.
Uwe Bonnes [Sun, 25 Oct 2020 11:50:06 +0000 (12:50 +0100)]
libusb.h: Base Class EFh (Miscellaneous) was missing in class definition.

Closes #802

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Restrict path for dynamic library loading
Chris Dickens [Wed, 21 Oct 2020 06:08:50 +0000 (23:08 -0700)]
Windows: Restrict path for dynamic library loading

The LoadLibraryA() function performs a search through various paths when
provided a library name that does not include a path element. All of the
libraries that libusb needs to dynamically load are installed in the
Windows system directory, thus it is not necessary to search any paths.

To harden the security of libusb and prevent loading libraries from
outside of the system directory should an attacker be able to alter the
environment or place an identically named library somewhere in the
search paths that occur before the Windows system directory, introduce a
function that calls LoadLibraryA() with a full path to the requested
library witin the Windows system directory. Note that we cannot call
SetDllDirectoryA() because as a library we should not alter the DLL
search path of the application. We also cannot use LoadLibraryExA() with
the LOAD_LIBRARY_SEARCH_* flags because those flags require a specific
security update to be installed on Vista and 7.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Iterate over completed transfers using the correct list node
Chris Dickens [Fri, 16 Oct 2020 04:40:19 +0000 (21:40 -0700)]
core: Iterate over completed transfers using the correct list node

Commit 006ca0fbaa ("Guard against getting stuck while handling events")
caused a regression using the wrong list node when iterating a list. Fix
this by introducing and using an alternate for_each iterator.

Closes #796

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Fix the newly added list_splice_front() function
Chris Dickens [Sun, 13 Sep 2020 22:28:16 +0000 (15:28 -0700)]
core: Fix the newly added list_splice_front() function

One statement was missing to preserve the consistency of the list.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Fix unused variable warnings on release builds
Chris Dickens [Tue, 29 Sep 2020 00:10:04 +0000 (17:10 -0700)]
core: Fix unused variable warnings on release builds

The recently introduced PTHREAD_CHECK and WINAPI_CHECK macros cause a
large number of compiler warnings for unused variables on release
builds. Fix this by implementing those macros in terms of some new
macros that are defined based on the definition of NDEBUG.

Closes #788

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agodarwin: code cleanup in darwin_handle_transfer_completion
Nathan Hjelm [Tue, 15 Sep 2020 04:09:55 +0000 (22:09 -0600)]
darwin: code cleanup in darwin_handle_transfer_completion

This commit adds an extra parameter check to darwin_handle_transfer_completion
and cleans the code up a bit.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agolibusbi.h: fix warning about redefined macros
Nathan Hjelm [Tue, 15 Sep 2020 04:06:14 +0000 (22:06 -0600)]
libusbi.h: fix warning about redefined macros

In macOS 11.x the USEC_PER_SEC and NSEC_PER_SEC are defined by a system
header. The values are the same so use the system defines if they are
available.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoexamples/testlibusb: Print speed of devices
Hans de Goede [Mon, 7 Sep 2020 09:25:54 +0000 (11:25 +0200)]
examples/testlibusb: Print speed of devices

Print the speed of devices to exercise libusb_get_device_speed().

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
3 years agoexamples/testlibusb: Add support for testing libusb_wrap_sys_device()
Hans de Goede [Mon, 7 Sep 2020 09:12:16 +0000 (11:12 +0200)]
examples/testlibusb: Add support for testing libusb_wrap_sys_device()

Sometimes it is useful to be able to test libusb_wrap_sys_device(),
add support for this to the testlibusb example

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
3 years agolinux: Fix libusb_get_device_speed() not working on wrapped devices
Hans de Goede [Mon, 7 Sep 2020 08:30:54 +0000 (10:30 +0200)]
linux: Fix libusb_get_device_speed() not working on wrapped devices

We don't have a sysfs_dir for wrapped devices, so we cannot read the speed
from sysfs.

The Linux kernel has supported a new ioctl to get the speed directly from
the fd for a while now, use that when we don't have sysfs access.

Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=1871818
Reported-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
3 years agoDoxygen: Fix warnings about undocumented compounds
Chris Dickens [Fri, 28 Aug 2020 22:42:41 +0000 (15:42 -0700)]
Doxygen: Fix warnings about undocumented compounds

Commit fa3f91e7c4 ("descriptor: Miscellaneous improvements to the
overall source") introduced some union types, however since descriptor.c
is parsed by Doxygen it will complain with the following warnings:

  libusb/descriptor.c:49: warning: Compound string_desc_buf is not documented.
  libusb/descriptor.c:55: warning: Compound bos_desc_buf is not documented.
  libusb/descriptor.c:43: warning: Compound config_desc_buf is not documented.

Fix this by moving the union definitions into libusbi.h.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agothreads_posix: Use monotonic clock for condition variables where possible
Chris Dickens [Thu, 27 Aug 2020 20:54:21 +0000 (13:54 -0700)]
threads_posix: Use monotonic clock for condition variables where possible

The pthread_condattr_setclock() function allows one to specify that the
condition variable use CLOCK_MONOTONIC as the base for timed waits. This
is desirable to avoid premature timeouts if the system time is adjusted
while a timed wait is occurring. Add a check for the availability of
this function and use it where possible.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Split usbi_clock_gettime() into two separate functions
Chris Dickens [Thu, 27 Aug 2020 20:46:32 +0000 (13:46 -0700)]
core: Split usbi_clock_gettime() into two separate functions

Most of the library only uses the monotonic clock. In fact, the only use
of the realtime clock is to implement the POSIX version of
usbi_cond_timedwait(). Now that Windows can no longer use the POSIX
thread abstraction, there is no need for Windows to implement a means of
reading the realtime clock.

This change replaces usbi_clock_gettime() with usbi_get_monotonic_time()
and usbi_get_real_time(). When clock_gettime() is available, both
functions are implemented as simple inline calls, otherwise the backend
must provide a definition for usbi_get_monotonic_time() *AND*
usbi_get_real_time() iff the platform is POSIX.

Reading the clocks is also never expected to fail. In practice, if it
ever did there would be much more than libusb that would not function
correctly. The new functions therefore have no return value, thus
allowing the callers to assume success and remove a bunch of error
handling code. The clock_gettime() wrappers have a simple error check
that is only enforced in debug builds.

This change also makes it unnecessary to check for and use
clock_gettime() on Windows, so remove it and always provide
usbi_get_monotonic_time().

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Add validation to timeval parameters
Chris Dickens [Thu, 27 Aug 2020 18:05:11 +0000 (11:05 -0700)]
core: Add validation to timeval parameters

Prior to this change, the timeval structures provided by users did not
go through any type of validation, therefore an invalid timeval would
result in potentially unclear or confusing errors when used later on.
Add checks to the core API functions that accept timevals and return
LIBUSB_ERROR_INVALID_PARAM if the timeval is not valid.

While at it, add some macro definitions to avoid magic numbers.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Miscellaneous internal logging cleanup
Chris Dickens [Thu, 27 Aug 2020 17:58:10 +0000 (10:58 -0700)]
core: Miscellaneous internal logging cleanup

  * Correct comments for usbi_{sn,vsn}printf(); strings are terminated
    with a NUL byte (ASCII value), not NULL (pointer value)
  * Make usbi_log_v() static; it is not used anywhere else in the
    library
  * Remove an unnecessary NUL-termination
  * Remove check for LIBUSB_LOG_LEVEL_NONE in log_str()
  * Remove UNICODE case for system logging on Windows; it was only used
    for WinCE
  * Remove 'usbi_' prefix from static functions
  * Simplify check for whether the logging level is sufficiently high to
    allow the message to be printed
  * Avoid calling usbi_clock_gettime() unless the timestamp will
    actually be used
  * Use the monotonic clock for logging timestamps; this prevents jumps
    in the timestamps if the system clock is changed
  * Use TIMESPEC_SUB() macro to get relative timestamp instead of
    open-coding the calculation
  * Use PRIxPTR to print sys_dev in libusb_wrap_sys_device() instead of
    casting to a void pointer

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Simplify thread abstractions and add debug checks
Chris Dickens [Thu, 27 Aug 2020 00:42:59 +0000 (17:42 -0700)]
core: Simplify thread abstractions and add debug checks

The POSIX thread mutex initialization function can potentially fail, but
in practice this is unlikely to occur. There is also inconsistent use of
the result of the mutex initialization within the library. The result is
only checked for mutexes in the libusb_device and libusb_device_handle
structures but is ignored in all other cases.

Simplify the mutex initialization function by changing the abstraction's
wrapper to a void function, much like all the other functions that
already exist. To that end, introduce macros for the abstractions that
will check the return value on debug builds.

Also remove the dependence on the core library needing errno.h to
translate errors from usbi_cond_timedwait(). The abstraction will
convert the implementation-specific error codes to LIBUSB_ERROR values.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agobuild: Merge events and threads into single platform abstraction
Chris Dickens [Wed, 26 Aug 2020 22:42:39 +0000 (15:42 -0700)]
build: Merge events and threads into single platform abstraction

The split between events and threads abstractions is unnecessary.
Simplify the library by merging the two into a "platform" abstraction.
The only meaningful change is that Cygwin builds will no longer use the
POSIX threads abstraction but will instead use the native Windows one.

The downside to doing this is that the dpfp_threaded example program
will no longer be available on Cygwin builds. This should be fine, and
future work will make dpfp_threaded available for all forms of Windows
build systems.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoGuard against getting stuck while handling events
Chris Dickens [Sun, 13 Sep 2020 01:39:28 +0000 (18:39 -0700)]
Guard against getting stuck while handling events

Alba Mendez (user mildsunrise) reports that a thread can get stuck at a
specific point while handling events, thus preventing other events from
being handled. This change addresses two such points in the code:

  1) When processing completed transfers in handle_event_trigger(), the
     function wll loop for as long as the completed_transfers list is
     not empty. Since the event data lock is released and reacquired for
     each completed transfer, it is possible for the completed_transfers
     list to never be emptied. Address this by cutting the list and only
     process the transfers that have completed at that point in time.

  2) When processing events, the Linux backend will reap transfers for
     each device that indicates activity until the reap fails (either
     because there are no completed transfers or some other error
     occurs). It is possible for transfers to be reaped at a rate slower
     than that at which they are completing, thus preventing the loop
     from exiting. Address this by limiting the number of transfers
     reaped for each device to 25 for every call to the Linux backend's
     handle_events() function.

Closes #780

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoAndroid: Add option LIBUSB_OPTION_WEAK_AUTHORITY to support used in apk
chris [Mon, 17 Aug 2020 02:39:18 +0000 (10:39 +0800)]
Android: Add option LIBUSB_OPTION_WEAK_AUTHORITY to support used in apk

Closes #760

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoevents_posix: Remove unnecessary and incorrect assertion
Chris Dickens [Tue, 25 Aug 2020 16:57:06 +0000 (09:57 -0700)]
events_posix: Remove unnecessary and incorrect assertion

Thomas Chiantia reports that the POSIX events abstraction is failing
periodically because of incorrect assertion logic. Remove it as there is
another (correct) check further into the function that will serve check
the intended condition.

Closes #775

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Ensure that reported event bits are initialized
Chris Dickens [Mon, 24 Aug 2020 18:23:09 +0000 (11:23 -0700)]
core: Ensure that reported event bits are initialized

Closes #774

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoExchange README.md and README
Ludovic Rousseau [Sun, 23 Aug 2020 15:25:17 +0000 (17:25 +0200)]
Exchange README.md and README

Make README the real file and README.md the symbolic link to fix an
installation issue with npm packaging tool.

Fixes https://github.com/libusb/libusb/issues/772
" README file not included with npm packaging tools #772 "

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
3 years agoFix typos detected by codespell and manual inspection
Chris Dickens [Tue, 18 Aug 2020 22:49:22 +0000 (15:49 -0700)]
Fix typos detected by codespell and manual inspection

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoDocumentation: Specify the constraint of the 'completed' parameter
Chris Dickens [Tue, 18 Aug 2020 22:03:32 +0000 (15:03 -0700)]
Documentation: Specify the constraint of the 'completed' parameter

Closes #482

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoDocumentation: Update the Windows binary snapshot README regarding UsbDk
Chris Dickens [Tue, 18 Aug 2020 21:39:11 +0000 (14:39 -0700)]
Documentation: Update the Windows binary snapshot README regarding UsbDk

Closes #443

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoDocumentation: Add details regarding thread-safety
Chris Dickens [Tue, 18 Aug 2020 21:25:59 +0000 (14:25 -0700)]
Documentation: Add details regarding thread-safety

Closes #352

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoDocumentation: Improve details of libusb_transfer's 'user_data' field
Chris Dickens [Tue, 18 Aug 2020 20:16:52 +0000 (13:16 -0700)]
Documentation: Improve details of libusb_transfer's 'user_data' field

Closes #351

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoDocumentation: Add a note regarding the execution context of callbacks
Chris Dickens [Tue, 18 Aug 2020 19:58:57 +0000 (12:58 -0700)]
Documentation: Add a note regarding the execution context of callbacks

Make it more clear that callbacks are only called when
libusb_handle_events() is called and that all callbacks will be called
on the same thread calling that function.

Closes #349

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoDocumentation: Add details regarding timeouts and partial transfer of data
Chris Dickens [Tue, 18 Aug 2020 19:25:11 +0000 (12:25 -0700)]
Documentation: Add details regarding timeouts and partial transfer of data

Closes #348

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoDocumentation: Add section regarding transfer length limitations
Chris Dickens [Tue, 18 Aug 2020 17:14:50 +0000 (10:14 -0700)]
Documentation: Add section regarding transfer length limitations

Closes #204

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Update INSTALL_WIN.txt
Chris Dickens [Tue, 18 Aug 2020 17:07:46 +0000 (10:07 -0700)]
Windows: Update INSTALL_WIN.txt

Add info regarding where the 'INSTALL' file comes from.

Remove link for Windows SDKs as this is no longer relevant.

Update to account for Visual Studio 2019.

Closes #235

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoAutotools: Use built-in shell tests and exec autoreconf
Chris Dickens [Tue, 18 Aug 2020 16:28:27 +0000 (09:28 -0700)]
Autotools: Use built-in shell tests and exec autoreconf

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Fix I/O completion port thread behavior on error
Chris Dickens [Tue, 18 Aug 2020 16:19:03 +0000 (09:19 -0700)]
Windows: Fix I/O completion port thread behavior on error

The GetQueuedCompletionStatus() returns FALSE when the I/O operation
itself does not complete successfully. The I/O completion thread should
therefore not exit on this condition. Adjust the check to verify that an
OVERLAPPED structure was provided and use an explicit completion key
value in the exit path to disambiguate receiving a valid NULL OVERLAPPED
pointer.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Improve how root hubs are reported
Chris Dickens [Mon, 17 Aug 2020 21:15:10 +0000 (14:15 -0700)]
Windows: Improve how root hubs are reported

Microsoft provides very little information about the actual root hub
characterstics beyond the number of ports. Determining the maximum
supported speed of the root hub is not directly possible but instead
requires that a device matching the highest speed of the root hub is
connected. Additionally, Windows 8 or later is required for _any_
successful detection of SuperSpeed devices.

One other inconvenience is that there are no descriptors exposed for
root hubs. This does not fit well with the structure of the library
because root hubs are considered first-class devices.

This change addresses some of these shortcomings. Each root hub is now
given a configuration descriptor that is matched to the fastest speed
detected for the root hub. The information is most accurate on Windows 8
or later, but the best information possible is constructed on earlier
versions. The device descriptor is also adjusted accordingly based on
the detected speed.

This solution is not perfect but is better than the status quo.

Closes #688

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Prevent NULL pointer dereference when ancestor is missing
Chris Dickens [Mon, 17 Aug 2020 15:40:33 +0000 (08:40 -0700)]
Windows: Prevent NULL pointer dereference when ancestor is missing

A buggy virtual USB device driver can cause the device enumeration
process to fail during the init_device() function when trying to
determine the bus number of the device. Guard against this by checking
that the ancestor device was actually found and skipping the bogus
device when there is no ancestor.

Closes #491

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agodescriptor: Miscellaneous improvements to the overall source
Chris Dickens [Thu, 13 Aug 2020 17:31:51 +0000 (10:31 -0700)]
descriptor: Miscellaneous improvements to the overall source

Introduce union types that simplify working with descriptors. Previously
there were simple arrays declared on the stack and these were either
parsed into the target descriptor structure or were accessed with magic
offsets into the array. Using the union type allows the descriptors to
be read with the need to parse it into a separate structure.

Fix a memory leak that would occur in the interface parsing code if the
usbi_reallocf() function failed. Each interface has a separately
allocated array of endpoints and potential extra descriptors. These
cannot be freed when using usbi_reallocf(), so switch to use realloc().

Fix an obscure limitation where extra descriptors would not be appended
to the configuration is any previous extra descriptors had already been
found.

Make the error checking and error messages consistent across all the
parsing functions. This includes printing unknown or unexpected
descriptor types in hex format, which is often easier to lookup as most
specifications use hex notation.

Constify the input buffer argument to the parsing functions.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Optimize check for pending events
Chris Dickens [Thu, 13 Aug 2020 05:34:03 +0000 (22:34 -0700)]
core: Optimize check for pending events

Prior to this commit, a check for whether any events are pending
involved checking four different variables within the context. Optimize
this by using multiple bits within a single unsigned integer to
represent all the possible events that could be pending. This reduces
the check for whether any events are pending to a single load.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Add support for eventfd
Chris Dickens [Thu, 13 Aug 2020 01:44:31 +0000 (18:44 -0700)]
core: Add support for eventfd

On systems that support it, using an eventfd for simple signalling is
much more efficient than using a pipe. Add detection of eventfd to the
configure script and wire it up in the POSIX event abstraction.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Introduce platform events abstraction
Chris Dickens [Wed, 12 Aug 2020 23:06:38 +0000 (16:06 -0700)]
core: Introduce platform events abstraction

The way in which system handles or resources are represented differs
greatly between Unix-like operating systems and Windows. Ever since
Windows support was added to libusb, Windows been emulating principles
of Unix-like operating systems such as file descriptors and poll().

This commit introduces an abstraction layer that completely removes the
need to perform any emulation. Fundamentally there are three things that
each platform provides to libusb:

  1) A signallable event
  2) A timer (not required, but useful)
  3) A means to wait for event sources such as the above to be triggered

The POSIX abstraction for Unix-like operating systems uses file
descriptors as the "handles" to the underlying system resources. The
signallable event is implemented using a pipe, the timer as a timerfd
(where supported) and the poll() system call is used to wait for events.

The Windows abstraction uses native HANDLEs as the "handles" to the
underlying system resources. The signallable event is implemented using
a manual-reset event, the timer as a manual-reset waitable timer, and
the WaitForMultipleObjects() system call is used to wait for events.

Closes #252

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoWindows: Use I/O completion ports for transfers
Chris Dickens [Tue, 11 Aug 2020 02:19:21 +0000 (19:19 -0700)]
Windows: Use I/O completion ports for transfers

As a first step in removing the Windows poll() emulation, switch the
transfers to use an I/O completion port. A dedicated per-context thread
will wait on the I/O completion port and report transfer completions
using usbi_signal_transfer_completion(). This enables the complete
removal of the handle_events() function for the Windows backend and
removes the notion of one "file descriptor" per transfer.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agocore: Introduce list iteration helpers
Chris Dickens [Tue, 11 Aug 2020 02:01:42 +0000 (19:01 -0700)]
core: Introduce list iteration helpers

The syntax for traversing over lists is somewhat cluttered. It could be
made much better with the use of the 'typeof' keyword, but unfortunately
this is not universally supported by all compilers. We can, however,
improve the situation by introducing some macros for the common cases.
To that end, this commit introduces a number of 'for_each' macros that
iterate over a specific linked list.

Current syntax:

  list_for_each_entry(itransfer, &ctx->flying_transfers, list, struct usbi_transfer)

New syntax:

  for_each_transfer(ctx, itransfer)

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoOpenBSD: support libusb_get_port_number
Theo Buehler [Wed, 5 Aug 2020 13:53:14 +0000 (15:53 +0200)]
OpenBSD: support libusb_get_port_number

From mpi@openbsd.org

Fixes #314
Closes #764

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoOpenBSD: allow opening ugen devices multiple times
Theo Buehler [Wed, 5 Aug 2020 13:23:45 +0000 (15:23 +0200)]
OpenBSD: allow opening ugen devices multiple times

Fix an OpenBSD backend bug where an existing open file descriptor is
overwritten if a libusb user attempts to open the same ugen(4) device
multiple times. This was observed with sane-backends and broke scanning.

Fix from stsp@openbsd.org

Closes #763

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agoAPI docs: Elaborate on "NOT_SUPPORTED" status for set_configuration()
Martin Thierer [Sun, 2 Aug 2020 13:08:31 +0000 (15:08 +0200)]
API docs: Elaborate on "NOT_SUPPORTED" status for set_configuration()

Closes #762

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agolibusb: fix a memory leak in sunos_new_string_list func
Zhiqiang Liu [Mon, 27 Jul 2020 09:46:01 +0000 (17:46 +0800)]
libusb: fix a memory leak in sunos_new_string_list func

In sunos_new_string_list func, if alloc list->string fails,
we will return NULL without free list, which has alread been
allocated successfully.

Fixes: 17348731b4 ('Solaris backend depends on Solaris private symbols')

Closes #756

Signed-off-by: Zhiqiang Liu <lzhq28@mail.ustc.edu.cn>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agostress: test_get_device_list: change printf format for ssize_t
Uri Lublin [Sun, 5 Jul 2020 13:29:57 +0000 (16:29 +0300)]
stress: test_get_device_list: change printf format for ssize_t

The type of list_size is ssize_t.
First idea was to use "%zd" printf format specifier.
But that is not supported by Visual Studio 2013.

Use "%ld" + cast list_size to long.

Closes #751

Suggested-by: Chris Dickens <christopher.a.dickens@gmail.com>
Signed-off-by: Uri Lublin <uril@redhat.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agodescriptor: Fix alignment for 32-bit words in parse_descriptor
Saleem Rashid [Thu, 21 May 2020 17:12:04 +0000 (18:12 +0100)]
descriptor: Fix alignment for 32-bit words in parse_descriptor

parse_descriptor was aligning 32-bit words to 2 bytes, instead of 4
bytes. This didn't cause any issues before, because the only time the
32-bit word code path is used is from a 3 byte offset (which
incidentally aligns to 4 bytes). However, a 1 byte offset would
incorrectly align to 2 bytes.

Closes #734

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agowinusb: setting a non-default config not supported
Martin Thierer [Wed, 8 Jul 2020 18:47:57 +0000 (20:47 +0200)]
winusb: setting a non-default config not supported

Just because winusb doesn't seem to support setting a different
configuration, that doesn't mean it's an invalid parameter.

It's generally supported by libusb, just not on this platform.

So return LIBUSB_ERROR_NOT_SUPPORTED instead of
LIBUSB_ERROR_INVALID_PARAM.

Closes #743
Closes #752

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