platform/upstream/libusb.git
4 years agoappveyor.yml: Revert back to Visual Studio 2015 image
Chris Dickens [Fri, 24 Jan 2020 02:17:25 +0000 (18:17 -0800)]
appveyor.yml: Revert back to Visual Studio 2015 image

Apparently AppVeyor dropped support for some compilation environments in
the 2017 image, so fall back to the 2015 image and forego compilation
with the newer tools.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoMisc: Use the UNREFERENCED_PARAMETER macro for UNUSED when available
Chris Dickens [Wed, 22 Jan 2020 19:45:24 +0000 (11:45 -0800)]
Misc: Use the UNREFERENCED_PARAMETER macro for UNUSED when available

This will squelch compiler warnings if platform headers provide this.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoWindows: Improve poll abstraction
Chris Dickens [Wed, 22 Jan 2020 19:06:29 +0000 (11:06 -0800)]
Windows: Improve poll abstraction

Commit 395e5a8a6f ("windows: remove total fds (256) limitations") and
commit c730a8410c ("windows: workaround WaitForMultipleObjects max 64
events limitation.") lifted some hard-coded limits in the number of
HANDLEs that can be used within the library. This change improves on
these changes to make them more efficient.

A bitmap has been added to provide an efficient lookup mechanism for
located unused file descriptor indices. This avoids the O(n) lookup time
for traversing the entire fd_table. This bitmap is dynamically resized
along with the fd_table.

The incremental size of the fd_table has been reduced from 256 to 64.
The vast majority of applications won't need to use 256 HANDLEs, so we
can optimize memory usage a bit.

Commit fb864b7cde ("fix windows crash when multi-thread do sync
transfer") added usbi_inc_fds_ref() and usbi_dec_fds_ref() functions to
work around a reference count issue. Remove these functions and change
the implementation of usbi_poll() to take a reference to each file
descriptor upon entry and drop the references when returning. If the
application experiences any kind of crash, there is a problem elsewhere.

Finally, make the thread executing usbi_poll() take part in the waiting.
The original implementation had this thread simply waiting on a single
event while separate threads waited on the HANDLEs. Now this thread will
wait on MAXIMUM_WAIT_OBJECTS - 1 HANDLEs, thereby reducing the number of
threads that are created. Additionally there is now only a single event
object that is shared amongst all waiting threads.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoWindows: Add missing '%' character for printf format specifier
Chris Dickens [Wed, 22 Jan 2020 18:30:08 +0000 (10:30 -0800)]
Windows: Add missing '%' character for printf format specifier

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoWindows: Kill the dedicated thread for monotonic clock_gettime()
Chris Dickens [Wed, 22 Jan 2020 17:43:37 +0000 (09:43 -0800)]
Windows: Kill the dedicated thread for monotonic clock_gettime()

According to Microsoft, anything prior to Vista could provide
inconsistent results for the value of QueryPerformanceCounter() across
different cores. Now that XP is no longer supported, we can drop the
significant overhead of using a dedicated thread pinned to a single core
just to read a timestamp.

The C++11 steady_clock implementation directly wraps
QueryPerformanceCounter(), so if it is good enough for that then it is
good enough for our purposes.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoWindows: Fix some MinGW build warnings and printf format specifiers
Chris Dickens [Wed, 22 Jan 2020 00:43:32 +0000 (16:43 -0800)]
Windows: Fix some MinGW build warnings and printf format specifiers

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoWindows: Drop support for Windows XP
Chris Dickens [Wed, 22 Jan 2020 00:13:07 +0000 (16:13 -0800)]
Windows: Drop support for Windows XP

XP is nearly 20 years old and there are hoops we jump through to keep
supporting it. Time to say goodbye and simplify some things.

Closes #267

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoWindows: Add Visual Studio 2019 support
Chris Dickens [Tue, 21 Jan 2020 23:40:45 +0000 (15:40 -0800)]
Windows: Add Visual Studio 2019 support

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoMisc: Remove obsolete Windows DDK build files
Chris Dickens [Tue, 21 Jan 2020 23:39:34 +0000 (15:39 -0800)]
Misc: Remove obsolete Windows DDK build files

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoMisc: Clean up Visual Studio project files
Chris Dickens [Tue, 21 Jan 2020 23:38:56 +0000 (15:38 -0800)]
Misc: Clean up Visual Studio project files

Commit a9b34d170a ("Adding support for ARM & ARM64 Windows Platform")
introduced a dependency on a particular version of the Windows 10 SDK
for *all* platforms. This is particularly annoying for most users who
will only be building for Windows. Fix this by specifying the SDK
dependency only for the ARM/ARM64 platforms and bump to the latest.

Commit 77037c4dd6 ("Adds /utf-8 to compile options") added this compiler
option to all versions of the Visual Studio project files. This results
in a number of warnings with the older versions that don't recognize
this option. Fix this by keeping this option only for 2015 and newer.

Explicitly specify library dependencies for non-static targets. With a
small change in the UsbDk backend we can completely remove all
depenencies other than kernel32.lib.

Lastly, remove the 'MinimalRebuild' option for 2015 and newer project
files as this option is now deprecated and results in a warning for each
project.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoWindows: Remove support for WinCE and Visual Studio older than 2013
Chris Dickens [Tue, 21 Jan 2020 02:02:19 +0000 (18:02 -0800)]
Windows: Remove support for WinCE and Visual Studio older than 2013

There appears to be no need for the WinCE backend anymore, and it is
increasingly difficult to keep healthy as the rest of the library
changes.

Require at least Visual Studio 2013 to compile. This simplifies matters
as there is some semblance of C99 support there.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agolinux_usbfs: Wait until all URBs have been reaped before freeing them
Chris Dickens [Thu, 16 Jan 2020 22:17:12 +0000 (14:17 -0800)]
linux_usbfs: Wait until all URBs have been reaped before freeing them

Prior to this change, the URBs allocated for an individual transfer were
freed when the last URB in the transfer was reaped. Normally this causes
no issues because URBs are reaped in the order they were submitted. If
the device is disconnected while multiple URBs are queued, these URBs
may be reaped in an order that does not match that of submission.

Change the logic to free the URBs when all the URBs of a transfer have
been reaped rather than the last one. While in here, improve some debug
messages.

Closes #607

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Do not attempt to destroy a default context that doesn't exist
Chris Dickens [Mon, 13 Jan 2020 23:05:00 +0000 (15:05 -0800)]
core: Do not attempt to destroy a default context that doesn't exist

Calling libusb_exit(NULL) when a successful call to libusb_init(NULL)
has not been made results in a segmentation violation. This is
definitely a user error, but we can easily guard against it.

Closes #511

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Fix libusb_get_max_iso_packet_size() for superspeed plus
Chris Dickens [Mon, 13 Jan 2020 22:07:31 +0000 (14:07 -0800)]
core: Fix libusb_get_max_iso_packet_size() for superspeed plus

The current logic fails to consider superspeed plus devices properly.
Fix this by checking for superspeed or greater instead of matching
against superspeed.

Closes #553

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agohotplug: Added libusb_hotplug_get_user_data()
gudenau [Mon, 22 Jan 2018 03:21:46 +0000 (21:21 -0600)]
hotplug: Added libusb_hotplug_get_user_data()

Closes #380

[dickens] Fixed up API version in doxygen, whitespace issues, function
visibility annotation (can't use API_EXPORTED for pointer values) and
added new function to libusb-1.0.defs

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoIncrement LIBUSB_API_VERSION for next release
Chris Dickens [Mon, 13 Jan 2020 21:12:40 +0000 (13:12 -0800)]
Increment LIBUSB_API_VERSION for next release

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoexamples: testlibusb: Always print VID and PID in addition to strings
Chris Dickens [Mon, 13 Jan 2020 20:50:40 +0000 (12:50 -0800)]
examples: testlibusb: Always print VID and PID in addition to strings

Previously the program would only print the VID when the manufacturer
string is unavailable and the PID when the product string is
unavailable. Change this to print the VID and PID unconditionally and
print the manufacturer and product strings similar to how the serial
number string is being printed. In addition, line up the string values
with the rest of the output.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoexamples: testlibusb: Formatting, whitespace and functional cleanup
Chris Dickens [Mon, 13 Jan 2020 20:23:02 +0000 (12:23 -0800)]
examples: testlibusb: Formatting, whitespace and functional cleanup

Make the coding style and whitespace consistent with the rest of the
library source code.

In the print_device() function, the 'level' argument is unnecessary, so
remove it and the associated space padding. Switch to use printf()
directly instead of formatting the description into a single buffer.
This not only simplifies the code but avoids truncating the description
for devices with larger descriptor strings.

Make the output formatting consistent by lining up all the printed
values and using the same notation for all hexadecimal values.

Inspired by PR #452, enable printing all available BOS device
capability descriptors, not just the first one.

Closes #452

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: fix build warning on newer versions of gcc
Greg Kroah-Hartman [Fri, 10 Jan 2020 19:04:52 +0000 (20:04 +0100)]
core: fix build warning on newer versions of gcc

When building libusb on a "newer" version of gcc (9.2), a lot of
warnings are thrown about zero-length messages as being part of a format
string.

An example of this is:

descriptor.c:546:11: warning: zero-length gnu_printf format string [-Wformat-zero-length]
  546 |  usbi_dbg("");
      |           ^~

Fix this up by replacing all calls of:
usbi_dbg("");
with
usbi_dbg(" ");
as obviously we still want to keep the implicit tracing message in the
log.

Closes #674

Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agowindows: relax HCD root hub enumeration errors
Jie Zhang [Mon, 8 Oct 2018 18:53:13 +0000 (14:53 -0400)]
windows: relax HCD root hub enumeration errors

Enumeration errors happen on one HCD root hub should not prevent
enumerating other hubs. This will fix an issue seen on some Windows 7
hosts with USB 3.0 host controller.

Closes #441, Closes #483

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoChange the errcode argument in libusb_strerror to be of type int
VladimirTechMan [Sun, 11 Aug 2019 11:10:54 +0000 (14:10 +0300)]
Change the errcode argument in libusb_strerror to be of type int

It would be more convenient in practice if the type of error code
argument in libusb_strerror was declared as "int", rather than
enum libusb_error: In practice, the value for the argument almost
always comes from a value returned by libusb function, thus its
type is "int" at that point. In which case, depending on the
checks enabled when compiling the C or C++ code of applications
using libusb's API, every call to libusb_strerror often requires
explicit type casting – while not a biggie, it gets inconvenient
as the number of logging calls using libusb_strerror grows.

It is worth of noting that the "peer" function libusb_error_name
already takes the int-type argument, thus eliminating the need in
extra value casting. Aligning the signature of libusb_strerror
to be the same would also improve the consistency between those
two parts of the libusb API.

Given that libusb_strerror does a range validation of its argument
value, there should not be any real drawbacks or negative effects
from "relaxing" the type declaration from enumeration to integer.
The existing application code that already does explicit casting
to enum, will keep compiling and running like before.

Closes #606

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agowindows: Translate ERROR_DEVICE_NOT_CONNECTED to LIBUSB_TRANSFER_NO_DEVICE
Matthias Bolte [Fri, 13 Dec 2019 17:05:08 +0000 (18:05 +0100)]
windows: Translate ERROR_DEVICE_NOT_CONNECTED to LIBUSB_TRANSFER_NO_DEVICE

Windows 7 reports error code ERROR_DEVICE_NOT_CONNECTED if transters fail
due to unplugging the device.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agowindows: Fix USBD_STATUS to LIBUSB_TRANSFER_STATUS translation
Matthias Bolte [Mon, 16 Dec 2019 12:38:35 +0000 (13:38 +0100)]
windows: Fix USBD_STATUS to LIBUSB_TRANSFER_STATUS translation

The MSDN documentaion says that the most significant 4 bits of the
USBD_STATUS value indicate success/pending/error state, but then gives
them as 2 bit values.

The broken translations code assumes these to be the lower 2 bits:

0b0000 for success
0b0001 for pending
0b0010 for error
0b0011 for error

But actually it's the higher 2 bits:

0b0000 for success
0b0100 for pending
0b1000 for error
0b1100 for error

The USBDK code already deals with USBD_STATUS and gets it correct.

Another problem is that the broken translations code then masks off
the most significant 4 bits of the USBD_STATUS value, but then compares
it to the full 32 bit error codes to figure out the actual error. This
switch will always jump to the default case, because all checked error
codes have their most significant 2 bits set, but the values they are
compared against have those bits masked off.

Move the working code from the USBDK backend to the shared header and
reuse it in the WinUSB backend too.

[dickens] Fixed whitespace, removed unused definitions, add ifdef guard,
and retained name as USBD_SUCCESS to match w32api header.

Closes #665

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: include sys/time.h on any GNU libc OS
Pino Toscano [Fri, 27 Dec 2019 17:50:27 +0000 (18:50 +0100)]
core: include sys/time.h on any GNU libc OS

GNU libc provides sys/time.h, so unconditionally include it when using
that libc.

Since sys/time.h is already included on a number of OSes, include it to
minimize the differences between OSes when using libusb. Arguably,
sources using functions from sys/time.h (such as gettimeofday) ought to
already include it on their own; OTOH, let's avoid making such issues
OS-specific.

4 years agoAdd Null POSIX backend
Pino Toscano [Fri, 27 Dec 2019 17:41:28 +0000 (18:41 +0100)]
Add Null POSIX backend

Add a simple null backend for POSIX platforms that reports no available
devices, and provides no capabilities. Make use of this new backend on
all the OSes without an existing backend, so libusb can be built even on
OSes without USB support.

4 years agoSet project name for doxygen
Aleksandr Mezin [Sun, 22 Dec 2019 03:39:11 +0000 (09:39 +0600)]
Set project name for doxygen

Currently, generated documentation shows `My Project` as the title: see http://libusb.sourceforge.net/api-1.0/index.html
Set it to `libusb`.

Signed-off-by: Aleksandr Mezin <mezin.alexander@gmail.com>
4 years agolinux_udev: silently ignore "bind" action
John Keeping [Tue, 12 Nov 2019 14:01:40 +0000 (14:01 +0000)]
linux_udev: silently ignore "bind" action

When a driver is bound to a device, udev emits a "bind" action that
causes libusb to log an error message:

libusb: error [udev_hotplug_event] ignoring udev action bind

Since we know this action is not relevant for libusb, silently ignore it
to avoid people thinking there is something wrong.

There is already a debug log entry for the action, so there is no need
to add a duplicate here.

Signed-off-by: John Keeping <john@metanate.com>
4 years agoLinux: Improved system out of memory handling
Slash Gordon [Tue, 3 Dec 2019 11:47:38 +0000 (12:47 +0100)]
Linux: Improved system out of memory handling

Maps ENOMEM system error to LIBUSB_ERROR_NO_MEM.

4 years agoLinux backend: fix ressource leak
Ludovic Rousseau [Fri, 1 Nov 2019 15:04:02 +0000 (16:04 +0100)]
Linux backend: fix ressource leak

Issue detected by Coverity:
22. leaked_handle: Handle variable fd going out of scope leaks the handle.

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
4 years agoXcode: add tests/stress target
Ludovic Rousseau [Fri, 1 Nov 2019 15:29:38 +0000 (16:29 +0100)]
Xcode: add tests/stress target

Also build tests/stress

4 years agoXcode: apply suggested changes from Xcode 11.2
Ludovic Rousseau [Fri, 1 Nov 2019 15:13:09 +0000 (16:13 +0100)]
Xcode: apply suggested changes from Xcode 11.2

Enable code signing.

4 years agowindows: workaround WaitForMultipleObjects max 64 events limitation.
Frank Li [Thu, 15 Aug 2019 15:21:17 +0000 (10:21 -0500)]
windows: workaround WaitForMultipleObjects max 64 events limitation.

WaitForMultiObjects have limiation. Only can wait max 64 events.

but usbi_poll may pass more than 64 fds. In previous implement,
only wait for first 64 events. if previous 64 events were not trigger
usbi_poll will wait for about 10s timemout eventhough other event triggered.

This patch workaround this limitation.

If max events less than 64, call WaitforMultiObjects directly.
If max events more than 64, group every 63 events into one work thread.

This thread call waitformulitobjects wait for this groug events and one
addtional thread exit events.
If any events trigger, this thread will trigger main notify events.

The main usbi_poll thread call waitforsingleobject wait for notify
events. If this events trigger, that means any of work threads get events.
Then call exit notify events let all working thread exit safely.

Return value changed, 0 means timeout. 1 - N means which event
triggered.

Closes #612

Signed-off-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoFix linux_scan_devices() in Android
Vinicius Tinti [Mon, 7 Oct 2019 11:24:17 +0000 (08:24 -0300)]
Fix linux_scan_devices() in Android

Use linux_default_scan_devices() in Android platform.

When building for Android the USE_UDEV is false and __ANDROID__ is true
resulting in no implementation for linux_scan_devices(). This commit fix
it by using linux_default_scan_devices() for Android.

Closes #637

Signed-off-by: Vinicius Tinti <vinicius.tinti@almg.gov.br>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoSolaris: format mismatch in some usbi_dbg()
Kenjiro Tsuji [Fri, 11 Oct 2019 02:46:28 +0000 (19:46 -0700)]
Solaris: format mismatch in some usbi_dbg()

In sunos_usb.c, some usbi_dbg() calls have format mismatch. For example, "%d"
is specified for a size_t variable and "%llx" is specified for a uint64_t
variable. In LP64 mode, they are format mismatch.  For those specifications
that are incompatible between ILP32 and LP64, format macros defined in
<inttypes.h>, such as PRIuPTR and PRIx64 should be used.

Closes #640
Closes #638

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoDoxygen: do not hardcode the sourceforge.net username
Ludovic Rousseau [Wed, 30 Oct 2019 12:03:36 +0000 (13:03 +0100)]
Doxygen: do not hardcode the sourceforge.net username

The username to use to login to web.sourceforge.net should be configured
in ~/.ssh/config so that different developers (with different login
names) can update the documentation.

4 years agoDoxygen: add libusb_wrap_sys_device() in the API list
Ludovic Rousseau [Wed, 30 Oct 2019 11:56:47 +0000 (12:56 +0100)]
Doxygen: add libusb_wrap_sys_device() in the API list

4 years agoDoxygen: update config file from 1.8.8 to 1.8.16
Ludovic Rousseau [Wed, 30 Oct 2019 11:49:08 +0000 (12:49 +0100)]
Doxygen: update config file from 1.8.8 to 1.8.16

4 years agoSolaris backend depends on Solaris private symbols
Kenjiro Tsuji [Wed, 25 Sep 2019 23:40:11 +0000 (16:40 -0700)]
Solaris backend depends on Solaris private symbols

Solaris backend depends on the following double-linked list operation functions
from libcmdutils:
list_insert_tail, list_create, list_remote_head, list_head, list_next
However, they are private symbols in libcmdutils library and moreover libcmdutils
library is also a private library. libusb should not directly refer those symbols.
Due to this issue, linking a program with libusb fails on the SPARC platform.
This issue has been reported as #586.
Since the code using those functions is just to build the argument list for execv,
the double-linked list operation functions do not need to be used.

Closes #627

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoSolaris: fails to find USB devices on SPARC platform
Kenjiro Tsuji [Thu, 26 Sep 2019 00:11:12 +0000 (17:11 -0700)]
Solaris: fails to find USB devices on SPARC platform

On SPARC platform, libusb fails to find USB devices. sunos_fill_in_dev_info()
reads the usb-dev-descriptor property and stores idVendor and idProduct to
the dev_sesc structure in little endian.  However, when creating a device name
for 'match_str' to be specified to sunos_physpath_to_devlink(), it's using the
idVendor and idProduct in dev_descr as is without converting them to the host
endian, that is big endian for SPARC.  As a result, 'match_str' has a wrong
device name and sunos_physpath_to_devlink() fails to find the device path
corresponding to the USB device. idVendor and idProduct need to be converted
to the host endian when creating 'match_str'.
This is a fix for #587.

Closes #628

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoSolaris backend is not correctly setting the one transfer mode for Interrupt-In pipe
Kenjiro Tsuji [Thu, 26 Sep 2019 00:51:43 +0000 (17:51 -0700)]
Solaris backend is not correctly setting the one transfer mode for Interrupt-In pipe

In sunos_check_device_and_status_open() function, if the target is an Interrupt-In
pipe, it opens the status endpoint first and writes USB_EP_INTR_ONE_XFER control
to the status endpoint to enable the one transfer mode. And it then closes the
status endpoint. After that, it opens the xfer endpoint and re-opens the status
endpoint. This is not correct, because closing the status endpoint is implicitly
disables the one tranfer mode.  As a result, an event notification won't be
delivered to the client in timely manner.  According to the comment in the source,
closing the status endpoint first and opening the xfer endpoint and status endpoint
in this order is intentional to avoit an issue that may happen if the USB device
has multiple configurations, which may be due to a ugen driver issue. To address
both issues, it can open the xfer endpoint first and immediately close it, which
will take care of the switch of configurations, if necessary.  Then, it can open
the status endpoint, enable the one transfer mode, and re-open the xfer endpoint.
This is a fix of libusb#620.

Closes #629

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agofix constant not in range of enumerated type
mrstock [Fri, 20 Sep 2019 12:36:07 +0000 (14:36 +0200)]
fix constant not in range of enumerated type

fix "Integer constant not in range of enumerated type 'enum libusb_transfer_status'"

LIBUSB_ERROR_NO_DEVICE doesn't exist on enum libusb_transfer_status

4 years agoXcode: apply suggested changes from Xcode 10.3
Ludovic Rousseau [Sun, 15 Sep 2019 15:50:37 +0000 (17:50 +0200)]
Xcode: apply suggested changes from Xcode 10.3

4 years agoon to 1.0.23 upstream/1.0.23
Nathan Hjelm [Wed, 28 Aug 2019 20:56:18 +0000 (13:56 -0700)]
on to 1.0.23

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoUpdate poll_windows.c
winterrace [Wed, 21 Aug 2019 05:59:11 +0000 (07:59 +0200)]
Update poll_windows.c

Set fd_size to 0 when freeing the fd_table array.

Closes #614

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoFix NULL pointer dereference crashes
winterrace [Tue, 20 Aug 2019 21:34:00 +0000 (23:34 +0200)]
Fix NULL pointer dereference crashes

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agomove on to 1.0.23-rc3
Nathan Hjelm [Wed, 14 Aug 2019 23:20:57 +0000 (16:20 -0700)]
move on to 1.0.23-rc3

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agowindows: fix build
Nathan Hjelm [Wed, 14 Aug 2019 23:05:10 +0000 (16:05 -0700)]
windows: fix build

Make code compliant with C89 so it will compile.

References #609
Closes #611

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agowindows: Fix libusb_get_device_list failure if virual usb host installled
Frank Li [Tue, 13 Aug 2019 18:17:00 +0000 (13:17 -0500)]
windows: Fix libusb_get_device_list failure if virual usb host installled

If a virtual usb host driver install, but no port on that.
libusb_get_device_list will return failure.

but it should skip this host devices.

the below is virtual host control information

============USB Host Controller =======================

        +++++++++++++++++ Device Information ++++++++++++++++++
Device Description       : Leostream Corporation Virtual USB
Host Controller
Device Path              :
\\?\root#system#0004#{3abf6f2d-71c4-462a-8a92-1e6861e6af27}
Device ID                : ROOT\SYSTEM\0004
Hardware IDs             : *lsusbdhcDevice
Driver KeyName           :
{4d36e97d-e325-11ce-bfc1-08002be10318}\0049
(GUID_DEVCLASS_SYSTEM)
Driver                   :
\SystemRoot\System32\drivers\lsusbdbus.sys (Version: 6.3.2.2570
Date: 2018-11-12)
Driver Inf               : C:\WINDOWS\inf\oem25.inf
Legacy BusType           : Internal
Class                    : System
Class GUID               :
{4d36e97d-e325-11ce-bfc1-08002be10318} (GUID_DEVCLASS_SYSTEM)
Interface GUID           :
{3abf6f2d-71c4-462a-8a92-1e6861e6af27}
(GUID_DEVINTERFACE_USB_HOST_CONTROLLER)
Service                  : lsusbdbus
Enumerator               : ROOT
Location Info            : -
Manufacturer Info        : Leostream Corporation
Capabilities             : 0x00 (-)
Status                   : 0x0180200B (DN_ROOT_ENUMERATED,
DN_DRIVER_LOADED, DN_STARTED, DN_DISABLEABLE, DN_NT_ENUMERATOR,
DN_NT_DRIVER)
Problem Code             : 0
Power State              : D0 (supported: D0, D3)

HcInfo0 Error            : ERROR_INVALID_FUNCTION

Roothub Symbolic Link    : LSUSBDBUS

BusStatistics Error      : ERROR_INVALID_FUNCTION

DriverVersion Error      : ERROR_INVALID_FUNCTION

BandwidthInfo Error      : ERROR_INVALID_FUNCTION

PowerInfo Error          : ERROR_INVALID_FUNCTION

Closes #608

Signed-off-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agocore: protect against changes to the pollfd list during handle_events
Nathan Hjelm [Wed, 14 Aug 2019 21:00:41 +0000 (14:00 -0700)]
core: protect against changes to the pollfd list during handle_events

It is possible a file descriptor to be removed due to a close and the
same file descriptor value to be added during the same event loop. This
can cause the backend to get a stale revent and incorrectly remove the
file descriptor.

This commit addresses the issue by delaying the actual removal of the
ipollfds entry until just before the backend handle_events is called.
handle_events then goes through the list of closed fds and clears out
the revents associated with the closed file descriptor.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agocore: abandon sync transfers after libusb_close()
Nathan Hjelm [Wed, 14 Aug 2019 04:11:15 +0000 (21:11 -0700)]
core: abandon sync transfers after libusb_close()

This commit adds an additional check to synchronous transfer
progression to prevent accessing a potentially deleted context. This
access can occur if a code calls libusb_close() then libusb_exit()
from one thread while another thread is in a synchronous transfer.

Note that this is likely a user error. The application should wait
for all transfers to complete before calling libusb_close(). This
commit is being offered as a best-effort attempt to prevent the
invalid access and return a useful error to the application.

There may still be a window where an invalid access is possible.
This commit is a work around until a better fix is available.

References #610

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoupdate to 1.0.23-rc2
Nathan Hjelm [Fri, 9 Aug 2019 16:26:41 +0000 (09:26 -0700)]
update to 1.0.23-rc2

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agowindows_winusb: Add support for reporting super speed plus devices
Harry Mallon [Thu, 7 Feb 2019 15:15:03 +0000 (15:15 +0000)]
windows_winusb: Add support for reporting super speed plus devices

Closes #531

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agowindows_winusb: fix pedantic format string warnings.
Josh Gao [Mon, 8 Jul 2019 21:12:14 +0000 (14:12 -0700)]
windows_winusb: fix pedantic format string warnings.

clang is pedantic about requiring the l prefix for DWORD, which is a
typedef to unsigned long.

Closes #590

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agowindows_winusb: fix size_t format strings.
Josh Gao [Mon, 8 Jul 2019 21:14:28 +0000 (14:14 -0700)]
windows_winusb: fix size_t format strings.

Use %zu to print size_t for 64-bit compatibility.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agowindows: remove total fds (256) limitations
Frank Li [Fri, 12 Jul 2019 19:29:12 +0000 (14:29 -0500)]
windows: remove total fds (256) limitations

When queue more usb requests, and more devices working at the same time
256's limitation is easy to reach.

This patch remove such limitation of windows version.

dymatic allocate map fd_table.
each time increase 256 entry if request more fds.

Closes #592

Signed-off-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoUse `%zu` for sizes instead of `%d`
William Orr [Fri, 12 Jul 2019 21:23:32 +0000 (23:23 +0200)]
Use `%zu` for sizes instead of `%d`

Closes #593

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoRemove infinite recursion in OpenBSD backend
William Orr [Fri, 12 Jul 2019 21:18:27 +0000 (23:18 +0200)]
Remove infinite recursion in OpenBSD backend

`usbi_dbg` calls `usbi_backend.clock_gettime`, so `obsd_clock_gettime` should
not call `usbi_dbg`

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoUse a designated initializer for openbsd backend
William Orr [Fri, 12 Jul 2019 21:15:17 +0000 (23:15 +0200)]
Use a designated initializer for openbsd backend

At some point `wrap_sys_device` was added to the `usbi_backend` struct, and
OpenBSD's backend was never updated along with it. Use a designated initializer
for this, so that we can avoid this class of error.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoWindows: Workaround for composite devices with U2F on Win10 1903
Axel Gembe [Fri, 2 Aug 2019 06:32:21 +0000 (13:32 +0700)]
Windows: Workaround for composite devices with U2F on Win10 1903

On Windows 10 version 1903 and later Windows blocks attempts to open
HID devices with a U2F usage unless running as administrator. This
patch makes the composite_open function ignore when opening the HID
part of a composite device fails.

Closes #603

Signed-off-by: Axel Gembe <derago@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoWindows: Fix detection of Windows 10 when manifest declares support
Axel Gembe [Sat, 3 Aug 2019 02:15:41 +0000 (09:15 +0700)]
Windows: Fix detection of Windows 10 when manifest declares support

On Windows 10 GetVersionEx will return version 10.0 when the running
application has a manifest that declares Windows 10 compatibility.

https://docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests#supportedOS

When no such manifest is found, the call will report version 6.2,
which is Windows 8.

The 6.4 version is returned by early Windows 10 Insider Previews
and Windows Server 2017 Technical Preview 1.

Closes #604

Signed-off-by: Axel Gembe <derago@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agodarwin: fix bugs in the reenumeration of devices
Nathan Hjelm [Fri, 9 Aug 2019 15:59:27 +0000 (08:59 -0700)]
darwin: fix bugs in the reenumeration of devices

This commit fixes two bugs in device re-enumeration (enumeration after
init):

 - The internal device object (cached device) was being re-used but the
   user-visible one was not. This would cause the user to get stale data
   when using the device afer reset. Now the (per libusb spec) the
   user-visible device object is re-used and updated.

 - If multiple libusb contexts were active then only the first context
   was handled correctly. Fixed by moving the cached device search
   out of process_new_device().

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
4 years agoUnix: fix library version for next release
Ludovic Rousseau [Wed, 7 Aug 2019 22:13:17 +0000 (00:13 +0200)]
Unix: fix library version for next release

https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
says:

4. If any interfaces have been added, removed, or changed since the last
update, increment current, and set revision to 0.

5. If any interfaces have been added since the last public release, then
increment age.

So both current and age must be incremented. The patch in
fde20bb9b7cba0ea6e96db920d0a0169c361ca92 was not complete.

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
5 years agoVarious fixes for the Haiku port
Adrien Destugues [Tue, 30 Apr 2019 17:41:57 +0000 (19:41 +0200)]
Various fixes for the Haiku port

- Use native API for getting full configuration descriptors instead of
  using a raw request
- Implement some callbacks that are mandatory and result in crashes if
  not available

Closes #565

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoFix some -Wformat warnings.
Josh Gao [Wed, 17 Apr 2019 23:01:52 +0000 (16:01 -0700)]
Fix some -Wformat warnings.

Closes #561

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agowindows_winusb: Set policy ISO_ALWAYS_START_ASAP for libusbK
Léo Lam [Sat, 4 May 2019 17:53:56 +0000 (19:53 +0200)]
windows_winusb: Set policy ISO_ALWAYS_START_ASAP for libusbK

In some cases, this is required to avoid eventually getting a
USBD_STATUS_BAD_START_FRAME error back from the Windows USB stack.

This makes the libusbK code match the behaviour of the Linux backend.

It appears that the libusbK backend tried to get this behaviour by
setting StartFrame to 0. However, libusbK docs state that:

"Specifing 0 for KISO_CONTEXT::StartFrame (start transfer ASAP) is
restricted to the first transaction on a newly opened or reset pipe."

Closes #569

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agowindows_winusb: Bounds check interface for check_valid_interface
Léo Lam [Fri, 3 May 2019 10:57:43 +0000 (12:57 +0200)]
windows_winusb: Bounds check interface for check_valid_interface

This prevents a nasty out-of-bounds read and a likely crash when
the interface number is invalid.

Closes #568

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agowindows_winusb: Mask wIndex correctly when checking interfaces
Léo Lam [Fri, 3 May 2019 10:49:00 +0000 (12:49 +0200)]
windows_winusb: Mask wIndex correctly when checking interfaces

According to the USB 3.0 spec (9.3.4), the interface number is in the
lower 8 bits of wIndex.

Note that wIndex must not be modified because some (non-compliant?)
devices actually use the higher 8 bits for custom data with
recipient-interface requests.

Closes #567

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agowindows: fix return type of usbi_{inc,dec}_fds_ref.
Josh Gao [Wed, 17 Apr 2019 23:02:18 +0000 (16:02 -0700)]
windows: fix return type of usbi_{inc,dec}_fds_ref.

These functions were declared as returning int, but failed to actually
return anything, and no one was using their return values anyway.

Closes #562

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoTypo fixes in comments
follower [Sun, 21 Apr 2019 04:52:28 +0000 (16:52 +1200)]
Typo fixes in comments

Closes #563

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agomingw fix
RipleyTom [Thu, 2 May 2019 16:19:42 +0000 (18:19 +0200)]
mingw fix

Closes #566

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoAdds /utf-8 to compile options
RipleyTom [Thu, 2 May 2019 15:43:36 +0000 (17:43 +0200)]
Adds /utf-8 to compile options

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoChange all $(SolutionDir) to $(ProjectDir)
RipleyTom [Thu, 2 May 2019 14:13:20 +0000 (16:13 +0200)]
Change all $(SolutionDir) to $(ProjectDir)

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoAddress issue 554: Default context is erase before backend exits in libusb_exit
mac Wang [Mon, 8 Apr 2019 20:45:14 +0000 (13:45 -0700)]
Address issue 554: Default context is erase before backend exits in libusb_exit

Closes #554
Closes #556

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoIssue 555: Memory passed to ioctl is not initialized in reap_for_handle
mac Wang [Mon, 8 Apr 2019 21:03:36 +0000 (14:03 -0700)]
Issue 555: Memory passed to ioctl is not initialized in reap_for_handle

Closes #557
Closes #555

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoupdate to 1.0.23-rc1
Nathan Hjelm [Fri, 5 Apr 2019 18:15:48 +0000 (12:15 -0600)]
update to 1.0.23-rc1

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoAdd support for isochronous transfers with WinUSB.
philemonf [Fri, 5 Apr 2019 07:27:39 +0000 (00:27 -0700)]
Add support for isochronous transfers with WinUSB.

Closes #284

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agowindows: get usb device until usb configuration is set
haidong.zheng [Wed, 12 Dec 2018 03:07:18 +0000 (11:07 +0800)]
windows: get usb device until usb configuration is set

There are low possiblity to open device before windows complete usb
configuration. Then claim interface will be always failure.
this patch workaround this problem, make sure return device until
windows finish usb configuration.

Closes #505

Signed-off-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Tom Zheng <haidong.zheng@nxp.com>
Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agodarwin: Retry IOCreatePlugInInterfaceForService on failure
Juan Cruz Viotti [Wed, 20 Mar 2019 11:34:41 +0000 (11:34 +0000)]
darwin: Retry IOCreatePlugInInterfaceForService on failure

We have a case where a device might not be detected by libusb if we plug
it after initializing the library. We were able to reproduce this issue
pretty consistently on a Mac Mini and several MacBook Pros running very
recent macOS versions (Mojave) using the Balena Fin board
(https://www.balena.io/fin/). I don't know if this happens with other
USB devices.

Enabling debug output revealed the following:

```
[ 7.901582] [00004f07] libusb: debug [darwin_get_cached_device] finding cached device for sessionID 0x2a9b5279f04
[ 7.901764] [00004f07] libusb: debug [darwin_get_cached_device] matching sessionID 0x2a9b5279f04 against cached device with sessionID 0x17dbd029b6a
[ 7.901798] [00004f07] libusb: debug [darwin_get_cached_device] matching sessionID 0x2a9b5279f04 against cached device with sessionID 0x1688763269f
[ 7.901818] [00004f07] libusb: debug [darwin_get_cached_device] matching sessionID 0x2a9b5279f04 against cached device with sessionID 0x4c0aefaa
[ 7.901831] [00004f07] libusb: debug [darwin_get_cached_device] matching sessionID 0x2a9b5279f04 against cached device with sessionID 0x0
[ 7.901845] [00004f07] libusb: debug [darwin_get_cached_device] caching new device with sessionID 0x2a9b5279f04
[ 7.903377] [00004f07] libusb: debug [darwin_device_from_service] could not set up plugin for service: out of resources
[ 8.029152] [00000307] libusb: debug [libusb_get_device_list]
```

The "out of resources" error comes from
`IOCreatePlugInInterfaceForService` which will apparently return this
error if the `Start` method of the interface is not success (see
https://github.com/opensource-apple/IOKitUser/blob/b80a5cbc0ebfb5c4954ef6d757918db0e4dc4b7f/IOCFPlugIn.c#L232).

Retrying `IOCreatePlugInInterfaceForService` makes it work for me.

Closes #542

Signed-off-by: Juan Cruz Viotti <juan@balena.io>
Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoxusb: Report SuperSpeedPlus
Harry Mallon [Thu, 7 Feb 2019 15:02:59 +0000 (15:02 +0000)]
xusb: Report SuperSpeedPlus

Closes #530

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agosolaris: Fix crash on closing libusb handle
Alexander Pyhalov [Thu, 24 May 2018 11:57:04 +0000 (14:57 +0300)]
solaris: Fix crash on closing libusb handle

libusb_close() can interrupt transfer to device,
setting dev_handle to NULL.
In this case all async requests should exit gracefully.

Also usbi_signal_transfer_completion() can dereference dev_handle
in this case.

Closes #434

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agowindows: Improved log message output to MSVC
dmitrykos [Thu, 31 Jan 2019 21:17:06 +0000 (23:17 +0200)]
windows: Improved log message output to MSVC

Improved log message output to MSVC handling by converting multi-byte
string (assumed to be UTF-8) to WCHAR string if UNICODE is defined, if
UNICODE is not defined then multi-byte string is sent to MSVC (Windows
CE defines UNICODE always, so it will use UNICODE case), added check
for 0 to avoid sending trash to MSVC (and possibly crash the process)
if conversion to WCHAR string fails.

Closes #504

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoFixed log message incorrect data format sent to MSVC if Unicode defined.
Dmitry Kostjuchenko [Tue, 11 Dec 2018 11:41:24 +0000 (13:41 +0200)]
Fixed log message incorrect data format sent to MSVC if Unicode defined.

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoAllow set configuration control requests when using libusb0.sys
Frederik Carlier [Tue, 18 Sep 2018 13:27:14 +0000 (15:27 +0200)]
Allow set configuration control requests when using libusb0.sys

Closes #473

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoFix control transfer interface selection
kuro68k [Mon, 26 Feb 2018 15:46:11 +0000 (15:46 +0000)]
Fix control transfer interface selection

When bmRequestType indicates the recipient is an interface, get
interface number from wIndex.

Closes #398

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoUse more portable di_prop_lookup_ints() interface
Alexander Pyhalov [Thu, 24 May 2018 11:54:02 +0000 (14:54 +0300)]
Use more portable di_prop_lookup_ints() interface

Closes #433

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agotransfer_lock should be released before usbi_handle_transfer_completion() call
Alexander Pyhalov [Thu, 24 May 2018 12:06:46 +0000 (15:06 +0300)]
transfer_lock should be released before usbi_handle_transfer_completion() call

Closes #430

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoAdding support for ARM & ARM64 Windows Platform
Dj Art [Fri, 27 Apr 2018 10:04:14 +0000 (18:04 +0800)]
Adding support for ARM & ARM64 Windows Platform

Closes #423

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agofix windows crash when multi-thread do sync transfer
Frank Li [Fri, 11 Jan 2019 19:09:51 +0000 (13:09 -0600)]
fix windows crash when multi-thread do sync transfer

fun()
{
    libusb_open()
    ... sync transfer
    libusb_close()
}

two thread call fun infininately.

to speed up crash, enable application verifier

below 20 cycle, assert(fd!=NULL) happen at check_pollfds
below 100 cycle, crash at pollable_fd->overlappend in
winusb_get_overlapped result

with this fix, success fun over 1000 cycles

in handle_events

usbi_mutex_lock()
fds = ctx->pollfds
nfds = ctx->pollfds_cnt;
usbi_mutex_unclock()

usbi_poll()
callback.

usbi poll is not in mutex. pollfds may be change by usbi_add_pollfd
and usbi_remove_pollfd.

Although usbi_add_pollfd and usbi_remove_pollfd hold mutex, but
usbi_poll and callback is not in protext of mutex.

windows use fd as index of fb_table. fb_table may changed by
usbi_remove_pollfd.  the map between fd and internal file_descriptor may
be wrong.

this patch added ref count for file_descriptor, only free file_desciptor
and remove it from fb_table when ref count is 0.

ref count will be increase when fds copy with mutex lock.
so fd always index validate file_descriptor.

ref count will be descress before return from handle_events.
the file_descriptor can be free safely at this time.

Closes #521

Signed-off-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoFix copy paste error in libusb_free_* docs
Harry Mallon [Tue, 12 Feb 2019 13:18:56 +0000 (13:18 +0000)]
Fix copy paste error in libusb_free_* docs

Closes #535

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoFixed Xcode 10.1 warning by setting project setting 'ALWAYS_SEARCH_USER_PATHS = NO'
Sean McBride [Thu, 7 Feb 2019 20:11:56 +0000 (15:11 -0500)]
Fixed Xcode 10.1 warning by setting project setting 'ALWAYS_SEARCH_USER_PATHS = NO'

The warning was: "Traditional headermap style is no longer supported; please migrate to using separate headermaps and set 'ALWAYS_SEARCH_USER_PATHS' to NO."

Also made sure all targets are referencing the xcconfig files.

Closes #532

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoInitialize the non-control endpoint fds in netbsd_open()
Aymeric Vincent [Thu, 28 Feb 2019 21:38:21 +0000 (22:38 +0100)]
Initialize the non-control endpoint fds in netbsd_open()

Not all users of libusb use libusb_claim_interface(), and in that
case, the NetBSD backend fails to open() the necessary /dev/ugenX.Y
and will try to send ioctl()s to fd 0, which fortunately fails.

This unbreaks e.g. openocd on NetBSD.

Closes #539

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agodarwin: fix reset device
Nathan Hjelm [Thu, 4 Apr 2019 18:02:25 +0000 (12:02 -0600)]
darwin: fix reset device

This commit fixes the backend of reset device to restore the state
of the device if possible. This fixes a bug introduced in
c14ab5fc4d22749aab9e3534d56012718a0b0f67. The previous commit
was necessary due to changes in the system USB stack that
essentially turned the ResetDevice function into a no-op. This
required libusb to move to USBDevuceReEnumerate to effectively
reset the device. The problem is that both the device handle and
libusb devices became invalid. This commit fixes the bug by
waiting for the re-enumeration to complete then 1) checking
whether the descriptors changed, 2) restoring the active
configuration, and 3) restoring claimed interfaces.

Closes #523

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoFixed -Wformat warnings by casting intptr_t to void*
Sean McBride [Thu, 17 Jan 2019 22:28:59 +0000 (17:28 -0500)]
Fixed -Wformat warnings by casting intptr_t to void*

%p is for pointers, not integers; therefore added a cast.

Fixes the following warnings:
libusb/core.c:1252:33: Format specifies type 'void *' but the argument has type 'intptr_t' (aka 'long')
libusb/core.c:1276:45: Format specifies type 'void *' but the argument has type 'intptr_t' (aka 'long')

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
5 years agohaiku_usb_raw: Add missing wrap_sys_device field to usbi_os_backend
Andrew Aldridge [Fri, 25 Jan 2019 02:59:38 +0000 (21:59 -0500)]
haiku_usb_raw: Add missing wrap_sys_device field to usbi_os_backend

Closes #527

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agohaiku_usb_raw: return written length instead of LIBUSB_SUCCESS
Andrew Aldridge [Thu, 24 Jan 2019 16:51:45 +0000 (11:51 -0500)]
haiku_usb_raw: return written length instead of LIBUSB_SUCCESS

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoFixed many compiler warnings about sign and size mismatch
Sean McBride [Sat, 15 Dec 2018 22:08:34 +0000 (17:08 -0500)]
Fixed many compiler warnings about sign and size mismatch

- added various casts
- added some asserts where the casts made assumptions
- enabled additional warnings in Xcode project (especially -Wshorten-64-to-32)

Closes #509

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agofix race condition at event_handles
Frank Li [Fri, 11 Jan 2019 17:56:47 +0000 (11:56 -0600)]
fix race condition at event_handles

event_handles supposed just run at a thread.

There are re-entry check at begin.

1: if (usbi_handling_events(ctx))
2: return LIBUSB_ERROR_BUSY;

3: usbi_stat_event_handle(ctx);

this code is hold any lock

it is possible two thread check 1 at the same time, then
go through to 3. So two threads will run event_handles.

above 3 line code should hold event_data_lock to avoid above
race condition.

1: usbi_mutex_lock($ctx->event_data_lock);
2: r = 0;
3: if (usbi_handling_events(ctx))
4:  r = LIBUSB_ERROR_BUSY;
5: else
6:  usbi_start_event_handling(ctx);
7: usbi_mutex_unlock($ctx->event_data_lock);

8: if(r)
9: return r;

check and set in an atomic operations.

Closes #520

Signed-off-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agoAdd missing structure entry to avoid erroneous shift of following items
winterrace [Sat, 26 Jan 2019 21:59:29 +0000 (22:59 +0100)]
Add missing structure entry to avoid erroneous shift of following items

Closes #526

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agodarwin: use internal define when querying plugin interface
Nathan Hjelm [Thu, 17 Jan 2019 04:08:07 +0000 (21:08 -0700)]
darwin: use internal define when querying plugin interface

Apple changed the definition of kIOUSBInterfaceInterfaceID in the SDK
for macOS 10.14. This looks like it breaks support for building for
older macOS versions when running 10.14. I don't know if the change
was intentional or a mistake but in order to guarantee libusb
builds as it has in the past we can no longer rely on
kIOUSBInterfaceInterfaceID.

Closes #519

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
5 years agodarwin: shorten hotplug poll timeout
Nathan Hjelm [Wed, 9 Jan 2019 01:26:48 +0000 (18:26 -0700)]
darwin: shorten hotplug poll timeout

Looks like 5 seconds was indeed too long. Shortened the timeout to
1ms instead.

Closes #516

Signed-off-by: Nathan Hjelm <hjelmn@me.com>