platform/upstream/libusb.git
20 months agoMerge tag 'v1.0.24' into tizen 41/281241/1
Karol Lewandowski [Wed, 14 Sep 2022 22:22:59 +0000 (00:22 +0200)]
Merge tag 'v1.0.24' into tizen

Change-Id: I1d8ba762cb8f1afdbf7c6f39f0f1e6de98c1ce19

20 months agoMerge tag 'v1.0.23' into tizen 40/281240/1
Karol Lewandowski [Wed, 14 Sep 2022 21:41:47 +0000 (23:41 +0200)]
Merge tag 'v1.0.23' into tizen

Change-Id: I6205f6d3a23077db72becca3e0964573d38b9f7c

20 months agoMerge tag 'v1.0.22' into tizen 39/281239/1
Karol Lewandowski [Wed, 14 Sep 2022 21:33:04 +0000 (23:33 +0200)]
Merge tag 'v1.0.22' into tizen

Change-Id: I70f02528deaee92ecf67356bfdef0174835b6747

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 agoFix memory leak in usb_interface_descriptor error path 43/238843/3 accepted/tizen_6.0_unified accepted/tizen_6.0_unified_hotfix accepted/tizen_6.5_unified accepted/tizen_7.0_unified accepted/tizen_7.0_unified_hotfix tizen_6.0 tizen_6.0_hotfix tizen_6.5 tizen_7.0 tizen_7.0_hotfix accepted/tizen/6.0/unified/20201030.110949 accepted/tizen/6.0/unified/hotfix/20201102.234959 accepted/tizen/6.0/unified/hotfix/20201103.051456 accepted/tizen/6.5/unified/20211029.014207 accepted/tizen/7.0/unified/20221110.062547 accepted/tizen/7.0/unified/hotfix/20221116.111040 accepted/tizen/unified/20200723.161322 submit/tizen/20200721.112435 submit/tizen_6.0/20201029.205502 submit/tizen_6.0_hotfix/20201102.192902 submit/tizen_6.0_hotfix/20201103.115102 submit/tizen_6.5/20211028.163501 tizen_6.0.m2_release tizen_6.5.m2_release tizen_7.0_m2_release
Karol Lewandowski [Fri, 17 Jul 2020 23:04:10 +0000 (01:04 +0200)]
Fix memory leak in usb_interface_descriptor error path

usbi_reallocf() will leak memory if passed pointer has members to free.

Change-Id: I183dce6067905c92a1cf373e67902244c52e9fed
Signed-off-by: lokilee73 <changjoo.lee@samsung.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>
3 years agodarwin: Fix invalid GetPipePropertiesV3 argument
Ido Yariv [Wed, 17 Jun 2020 03:22:31 +0000 (23:22 -0400)]
darwin: Fix invalid GetPipePropertiesV3 argument

GetPipePropertiesV3 seems to require that the bVersion field of the
properties argument be set before calling it:
"Version of the structure. Currently kUSBEndpointPropertiesVersion3.
Need to set this when using this structure"

Not doing so results in an invalid argument error.

Closes #744

Signed-off-by: Ido Yariv <ido@wizery.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoos/darwin: use IOUSBDevice as darwin_device_class explicitly
Igor Anokhin [Wed, 15 Apr 2020 10:47:51 +0000 (13:47 +0300)]
os/darwin: use IOUSBDevice as darwin_device_class explicitly

kIOUSBDeviceClassName define from IOUSBLib.h file was changed from
IOUSBDevice to IOUSBHostDevice in macOS Catalina.

In previous macOS versions, it was always defined as IOUSBDevice:
```
 #define kIOUSBDeviceClassName  "IOUSBDevice"
```
In macOS Catalina it looks as follows:
```
 #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_14
 #define kIOUSBDeviceClassName           "IOUSBDevice"
 #define kIOUSBInterfaceClassName        "IOUSBInterface"
 #else
 #define kIOUSBDeviceClassName           kIOUSBHostDeviceClassName
 #define kIOUSBInterfaceClassName        kIOUSBHostInterfaceClassName
 #endif /* MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_14 */
```

By default, macOS Catalina uses IOUSBHostDevice. The problem is that
using the IOUSBHostDevice class misses some devices.  This has been
described in 044a7ee commit. To work only with IOUSBDevice,
darwin_device_class has been explicitly set to IOUSBDevice.

Closes #693

For reference, here's the output of the 'listdevs' example before my changes:
05ac:027b (bus 128, device 2) path: 5
2109:0102 (bus 20, device 6) path: 4
05e3:0751 (bus 20, device 5) path: 1
1a40:0801 (bus 20, device 3) path: 2
0bda:8153 (bus 0, device 2) path: 3
2109:2817 (bus 20, device 1) path: 8
2109:0817 (bus 0, device 1) path: 4

And here's the output after my changes:
05ac:027b (bus 128, device 2) path: 5
2109:0102 (bus 20, device 6) path: 3.2.4
05e3:0751 (bus 20, device 5) path: 3.2.1
1a40:0801 (bus 20, device 3) path: 3.2
0bda:8153 (bus 0, device 2) path: 2.3
2109:2817 (bus 20, device 1) path: 3
2109:0817 (bus 0, device 1) path: 2

Closes #712

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: use GetPipePropertiesV3 on 10.9 and later
Nathan Hjelm [Fri, 12 Jun 2020 05:31:36 +0000 (23:31 -0600)]
darwin: use GetPipePropertiesV3 on 10.9 and later

Apple added the GetPipePropertiesV3 in IOUSBFamily version 550. The old
function appears to be deprecated. When 10.8.x and older are no longer
supported the GetPipeProperties call should be removed.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agodarwin: add kIOUSBUnknownPipeErr to error string list
Nathan Hjelm [Fri, 12 Jun 2020 05:30:48 +0000 (23:30 -0600)]
darwin: add kIOUSBUnknownPipeErr to error string list

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
3 years agoDocument that libusb_set_option may also return NOT_FOUND
Jonas Malaco [Fri, 22 May 2020 09:36:58 +0000 (06:36 -0300)]
Document that libusb_set_option may also return NOT_FOUND

If the platform is Windows but UsbDk is not available libusb_set_option
returns LIBUSB_ERROR_NOT_FOUND.[1] Adjust the documentation to reflect that.

[1] https://github.com/libusb/libusb/blob/26611eaa494ed9e077b5b0e1f999f5ae377de958/libusb/os/windows_common.c#L570-L578

Closes #735

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 years agodarwin: Fix stale descriptor information post reset
Ido Yariv [Wed, 20 May 2020 20:23:11 +0000 (16:23 -0400)]
darwin: Fix stale descriptor information post reset

As part of the recent removal of the backend get_device_descriptor()
function, the darwin backend was modified to update the cached
device_descriptor when a new device is processed. However, this wasn't
done for cases where the device is reused rather than allocated.
This could potentially result in getting stale data when using the
device following a reset.

In addition, when the device is reused, its session ID should be kept
up-to-date in the cached devices list, so it could be found in subsequent
resets.

Closes #733

Signed-off-by: Ido Yariv <ido@wizery.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoWindows: Translate ERROR_NO_SUCH_DEVICE to LIBUSB_TRANSFER_NO_DEVICE
Sebastian von Ohr [Tue, 28 Apr 2020 14:28:27 +0000 (16:28 +0200)]
Windows: Translate ERROR_NO_SUCH_DEVICE to LIBUSB_TRANSFER_NO_DEVICE

Windows SDK 10.0.18362.0 adds a new error code ERROR_NO_SUCH_DEVICE
which is returned when the device is disconnected.

Closes #721, Closes #722

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agolinux_usbfs: Parse config descriptors during device initialization
Chris Dickens [Wed, 29 Apr 2020 19:39:35 +0000 (12:39 -0700)]
linux_usbfs: Parse config descriptors during device initialization

Do the work ahead of time and cache the results so that fetching config
descriptors becomes a trivial operation.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Narrow the types passed to certain backend functions
Chris Dickens [Wed, 29 Apr 2020 05:04:11 +0000 (22:04 -0700)]
core: Narrow the types passed to certain backend functions

Backend functions dealing with interfaces and alternate settings should
use a type whose range represents that of valid values for interfaces
and alternate settings. Switch to use uint8_t instead of int so that
backends do not have to cast values or do range checks.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agosunos: Fix a number of compiler warnings
Chris Dickens [Tue, 28 Apr 2020 19:36:29 +0000 (12:36 -0700)]
sunos: Fix a number of compiler warnings

  * [-Wdiscarded-qualifiers] assignment discards 'const' qualifier from pointer target type
  * [-Wpointer-sign] pointer targets in passing argument N of 'func' differ in signedness
  * [-Wsign-compare] comparision between signed and unsigned integer expressions
  * [-Wunused-function] 'func' declared 'static' but never defined
  * [-Wunused-parameter] unused parameter 'p'

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoFix some trivial compiler warnings for the Haiku and BSD backends
Chris Dickens [Tue, 28 Apr 2020 19:24:59 +0000 (12:24 -0700)]
Fix some trivial compiler warnings for the Haiku and BSD backends

  * [-Wformat=] format 'S' expects argument of type 'T1', but argument N has type 'T2'
  * [-Wmissing-declarations] no previous declaration for 'func'
  * [-Wreorder] 'Class::Member' will be initialized after
  * [-Wsign-compare] comparison between signed and unsigned integer expressions
  * [-Wunused-but-set-variable] variable 'v' set but not used
  * [-Wunused-parameter] unused parameter 'p'

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agodescriptor: Remove usbi_get_config_index_by_value()
Chris Dickens [Tue, 28 Apr 2020 19:17:50 +0000 (12:17 -0700)]
descriptor: Remove usbi_get_config_index_by_value()

Only one caller of this function exists and it lives in the descriptor
source code. Remove the function and merge it into the function that
calls it.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Kill backend get_device_descriptor() function
Chris Dickens [Tue, 28 Apr 2020 19:08:08 +0000 (12:08 -0700)]
core: Kill backend get_device_descriptor() function

Simplify the library by moving device descriptor initialization to the
backend, while the device is being set up. This removes the duplication
of essentially the same code in every backend.

Add some missing calls to libusb_le16_to_cpu() when reading multi-byte
fields from the "raw" device descriptor. It has worked thus far because
the platforms not using the calls happen to be the same endianness as
the USB bus.

While here, throw in some static assertions to ensure there is no
mismatch between the libusb device descriptor structure and any
device descriptor structure provided by the platform headers.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Fix some minor inconsistencies in API and coding
Chris Dickens [Tue, 28 Apr 2020 01:43:42 +0000 (18:43 -0700)]
core: Fix some minor inconsistencies in API and coding

All of the API functions should take the typedef'ed versions of the
opaque libusb structures, but some recent additions to the API did not
follow this convention. Fix this by making the changes to the
declarations and definitions of the functions.

Make the placement of the asterisk in pointer variable declarations
consistent (always with the variable name, not the type).

Remove some unnecessary casts and initializations relating to
dynamically allocated memory. While at it, make casts within the core
library consistent in style with no space after the closing parenthesis
of the cast. Most of the core already used this style.

When using the 'sizeof' operator, dereference the pointer instead of
using the type.  Most of the core was already doing this, so fix up the
few places that weren't.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Change pointer type from 'unsigned char' to 'void'
Chris Dickens [Fri, 17 Apr 2020 21:21:44 +0000 (14:21 -0700)]
core: Change pointer type from 'unsigned char' to 'void'

This removes the need for pointer casts when calling backend functions.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agodescriptor: Minor improvements to the parse_descriptor() function
Chris Dickens [Fri, 17 Apr 2020 20:22:34 +0000 (13:22 -0700)]
descriptor: Minor improvements to the parse_descriptor() function

Change the type of the source pointer to 'void' so that callers need not
cast to 'unsigned char'. Also change working types to 'uint8_t' to make
it explicit that we are dealing with 8-bit types.

Refactor the parsing loop to avoid unnecessary stack variables. The
generated assembly with this change is more efficient.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Kill the 'host_endian' argument for most functions
Chris Dickens [Fri, 17 Apr 2020 19:57:49 +0000 (12:57 -0700)]
core: Kill the 'host_endian' argument for most functions

The 'host_endian' argument exists only for a special case in the Linux
backend, that being when the device descriptor is read using usbfs
rather than sysfs. It does not apply to any other descriptor types nor
any other backends, so remove it.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Remove redundant definitions for descriptor sizes
Chris Dickens [Fri, 17 Apr 2020 19:21:46 +0000 (12:21 -0700)]
core: Remove redundant definitions for descriptor sizes

The public libusb header provides all the definitions for the various
descriptor sizes, so use them instead of defining them again with
different names.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Remove usbi_parse_descriptor() function
Chris Dickens [Fri, 17 Apr 2020 19:07:38 +0000 (12:07 -0700)]
core: Remove usbi_parse_descriptor() function

The Linux backend was the only caller of this function, but with the
packed structures introduced in commit d06cc52851 its use is no longer
necessary. Convert the function to static and remove the return type as
no callers paid attention to it.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agodescriptor: Optimize descriptor parsing
Chris Dickens [Fri, 17 Apr 2020 18:43:54 +0000 (11:43 -0700)]
descriptor: Optimize descriptor parsing

While iterating over descriptors, we can simplify and speed up the
process by looking at the descriptor header directly rather than calling
the parse function each time.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoAdd packed definitions for some standard USB descriptor types
Chris Dickens [Fri, 17 Apr 2020 18:31:55 +0000 (11:31 -0700)]
Add packed definitions for some standard USB descriptor types

These are going to be used in future commits to cleanup some code. Note
that these are prefixed as 'usbi' rather than 'usb' to avoid conflicts
with definitions provided by some system headers.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agohotplug: Fix definition of libusb_hotplug_register_callback()
Chris Dickens [Thu, 16 Apr 2020 23:47:01 +0000 (16:47 -0700)]
hotplug: Fix definition of libusb_hotplug_register_callback()

The signature of the libusb_hotplug_register_callback() is slightly
incorrect in two regards:

  1) The 'events' parameter is meant to represent a bitwise OR of
     libusb_hotplug_event values. By OR'ing multiple values together,
     the result is not something that is actually a libusb_hotplug_event
     enumeration.

  2) The 'flags' parameter is meant to represent a bitwise OR of
     libusb_hotplug_flag values. The same considerations as above apply,
     though this has not practically been the case as there is currently
     only one flag value defined. However, a special value was already
     defined to represent the absence of any flags, which hinted at the
     problem with how it is currently defined.

Address these two issues by changing the types of the 'events' and
'flags' parameter to plain integers. As enumerations should already be
promoted to integers, this change should not cause any ABI concerns.

Closes #714

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoAdd vcpkg installation instructions
NancyLi1013 [Fri, 17 Apr 2020 08:52:28 +0000 (01:52 -0700)]
Add vcpkg installation instructions

Closes #708, Closes #715

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoAndroid: fixes unknown warning option from ndk build
Aaron Luft [Fri, 17 Apr 2020 15:30:12 +0000 (11:30 -0400)]
Android: fixes unknown warning option from ndk build

A command line option is not correct.
Fix by spelling the option correctly.

BEFORE

[x86] Compile        : xusb <= xusb.c
warning: unknown warning option '-Werror=unintialized'; did you mean '-Werror=uninitialized'? [-Wunknown-warning-option]

AFTER

[x86] Compile        : xusb <= xusb.c
[x86] Executable     : xusb
[x86] Install        : xusb => libs/x86/xusb

4 years agoAdd a way to skip the git hooks via an environment variable
Chris Dickens [Fri, 17 Apr 2020 00:10:39 +0000 (17:10 -0700)]
Add a way to skip the git hooks via an environment variable

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agolibusb.h: Ensure that struct libusb_control_transfer is not padded
Chris Dickens [Thu, 16 Apr 2020 22:57:15 +0000 (15:57 -0700)]
libusb.h: Ensure that struct libusb_control_transfer is not padded

There is nothing explicitly preventing the compiler from adding any sort
of padding to the libusb_control_transfer structure. It does not seem
that any sane compiler would do so, but there is library functionality
that depends on this not happening. Address this by explicitly
instructing the compiler to pack the structure.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agolibusb.h: Simplify condition governing the inclusion of sys/time.h
Chris Dickens [Thu, 16 Apr 2020 21:31:18 +0000 (14:31 -0700)]
libusb.h: Simplify condition governing the inclusion of sys/time.h

The expression for the condition grows each time a new backend is added,
but it can be simplified if changed to key off of the _MSC_VER macro.
The sys/time.h header is ubiquitous across all platforms and build
environments except Visual Studio, so change the condition to be based
on this. This fixes builds on BSD where the condition was not evaluating
to true.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agolibusb.h: Make C-preprocessor checks consistent and safe
Chris Dickens [Thu, 16 Apr 2020 21:26:42 +0000 (14:26 -0700)]
libusb.h: Make C-preprocessor checks consistent and safe

Instead of mixing 'ifdef' with 'if defined(...)', use the latter
everywhere.

Check whether the __GNUC__ macro is defined before using it to avoid
compiler warnings.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agolibusb.h: Make formatting and style consistent across the whole file
Chris Dickens [Thu, 16 Apr 2020 20:54:17 +0000 (13:54 -0700)]
libusb.h: Make formatting and style consistent across the whole file

  * Define USB spec. definitions as hexadecimal values
  * Define bitmask values using a shift operation
  * Fix Doxygen for enum libusb_log_level
  * Fix some broken/missing whitespace
  * Give explicit values to all enumerations
  * Remove trailing comma from all enumerations

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agolibusb.h: Move BOS descriptor enumerations
Chris Dickens [Thu, 16 Apr 2020 19:40:49 +0000 (12:40 -0700)]
libusb.h: Move BOS descriptor enumerations

The location of the enumerations for the various bits in the BOS
descriptors is not consistent with the other enumerations, so move it up
to group it with the rest. In addition, fix the Doxygen documentation to
put these in the descriptors group rather than the devices group.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agocore: Fix definition and use of enum libusb_transfer_type
Chris Dickens [Thu, 16 Apr 2020 19:20:16 +0000 (12:20 -0700)]
core: Fix definition and use of enum libusb_transfer_type

Since the beginning of the libusb-1.0, the libusb_transfer_type enum had
a 1:1 mapping to the endpoint transfer type (bits 1:0 of the endpoint
descriptor's bmAttributes). This was broken with the addition of bulk
stream support because the value of LIBUSB_TRANSFER_TYPE_BULK_STREAM
does not map to a valid value for an endpoint's transfer type.

Fix this by splitting the endpoint's transfer type for its descriptor
and the library's transfer type for its logical transfers into different
enumerations. None of the values are altered, so applications testing
an endpoint descriptor's bmAttributes field against the
libusb_transfer_type enum values will still work.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agonetbsd: Recognize device timeouts.
nia [Fri, 3 Apr 2020 21:15:02 +0000 (22:15 +0100)]
netbsd: Recognize device timeouts.

Closes #710

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoMakefiles: Remove target-specific variables
Chris Dickens [Thu, 9 Apr 2020 19:30:57 +0000 (12:30 -0700)]
Makefiles: Remove target-specific variables

Improve portability with non-GNU make by using standard variables.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
4 years agoconfigure.ac: Enhance compiler checks for pthreads
Chris Dickens [Thu, 9 Apr 2020 19:05:16 +0000 (12:05 -0700)]
configure.ac: Enhance compiler checks for pthreads

There apparently exist some compiler ports (e.g Haiku's GCC) that do not
support the ubiquitous '-pthread' compiler option. Add a check for this
and only use the option if supported.

Also tweak the check for the pthread library to check for the
pthread_create() function. Even though libusb does not use this function,
it seems to be sufficiently distinct such that the standard C library
would not provide this directly if the pthread implementation resided in
a separate library. Also explicitly check whether no additional library
linkage is required.

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