platform/upstream/libusb.git
6 years agodarwin: Cleanup and fixes related to Mac OS IOUSBLib interface checks
Sean McBride [Fri, 29 Dec 2017 18:15:07 +0000 (13:15 -0500)]
darwin: Cleanup and fixes related to Mac OS IOUSBLib interface checks

- revisited fix to issue #48:
  - guard kIOUSBDeviceInterfaceID500 with 10.8 deployment (not 10.9).
    I believe a typo in the bug report caused this to be guarded too
    strictly.
  - added deployment target tests to every interface check. Necessary
    because merely checking for a macro definition tells us nothing
    about what features are available at runtime. It is common to build
    against new SDKs but deploy to older OSes.
- properly guard kIOUSBInterfaceInterfaceID700 against 10.10 deployment
  (not 10.9).  It was added in 10.10.  Probably a copy-pasto.
- changed non-existent kIOUSBDeviceInterfaceID220 to
  kIOUSBDeviceInterfaceID197.  Probably a copy-pasto.
- don't use defines like MAC_OS_X_VERSION_10_9 since they don't exist in
  older SDKs and would fail to compile, use raw numbers instead.
- added tests for newer interfaces: kIOUSBInterfaceInterfaceID800,
  kIOUSBInterfaceInterfaceID650, kIOUSBDeviceInterfaceID650.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoAdded some Xcode-related items to .gitignore
Sean McBride [Fri, 29 Dec 2017 18:02:05 +0000 (13:02 -0500)]
Added some Xcode-related items to .gitignore

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agodarwin: Configure Xcode project to specify tab/space, indent amount
Sean McBride [Fri, 29 Dec 2017 17:57:26 +0000 (12:57 -0500)]
darwin: Configure Xcode project to specify tab/space, indent amount

All source files are now tagged as using tabs of 4 spaces, except
darwin_usb.c and darwin_usb.h which use spaces of 2 spaces. This allows
editing files in Xcode without it messing up any particular files.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Replace "Mac OS X" with "macOS", its current name
Sean McBride [Fri, 29 Dec 2017 17:50:15 +0000 (12:50 -0500)]
Misc: Replace "Mac OS X" with "macOS", its current name

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Ditch UsbDk build configuration
Sameeh Jubran [Sun, 15 Jan 2017 09:40:57 +0000 (11:40 +0200)]
Windows: Ditch UsbDk build configuration

Previous patch introduced run-time UsbDk backend activation,
therefore separate build configuration is not needed anymore.

Signed-off-by: Sameeh Jubran <sameeh@daynix.com>
6 years agoWindows: Enable dynamic selection between WinUSB and UsbDk backends
Chris Dickens [Mon, 8 Jan 2018 18:17:26 +0000 (10:17 -0800)]
Windows: Enable dynamic selection between WinUSB and UsbDk backends

This commit unifies the two Windows backends into a single project and
enables the user to switch to the UsbDk backend, if available, using the
libusb_set_option() function. All contexts will use the WinUSB backend
by default for backwards compatibility.

With this change, the UsbDk-specific projects are no longer required.

Closes #309

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Rework poll() emulation to a much simpler and effective design
Chris Dickens [Sun, 7 Jan 2018 05:43:12 +0000 (21:43 -0800)]
Windows: Rework poll() emulation to a much simpler and effective design

The previous poll() implementation worked okay but had some issues. It
was inefficient, had a large footprint, and there were simply some use
cases that didn't work (e.g. a synchronous transfer that completes when
no other event or transfer is pending would not be processed until the
next poll() timeout).

This commit introduces a new, simpler design that simply associates an
OVERLAPPED structure to an integer that acts as a file descriptor. The
poll() emulation now solely cares about the OVERLAPPED structure, not
transfers or HANDLEs or cancelation functions. These details have been
moved up into the higher OS-specific layers.

For Windows NT environments, several deficiencies have been addressed:

1) It was previously possible to successfully submit a transfer but fail
   to add the "file descriptor" to the pollfd set. This was silently
   ignored and would result in the user never seeing the transfer being
   completed.

2) Synchronously completed transfers would previously not be processed
   unless another event (such as a timeout or other transfer completion)
   was processed.

3) Canceling any one transfer on an endpoint would previously result in
   *all* transfers on that endpoint being canceled, due to the use of
   the AbortPipe() function.

This commit addresses all of these issues. In particular, run-time
detection of the CancelIoEx() function will allow the user to cancel a
single outstanding transfer without affecting any others still in
process.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Add check for ERROR_FILE_NOT_FOUND, which means device removed
Chris Dickens [Sun, 7 Jan 2018 05:32:23 +0000 (21:32 -0800)]
Windows: Add check for ERROR_FILE_NOT_FOUND, which means device removed

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: UsbDk: Fix some minor style/naming issues
Chris Dickens [Fri, 5 Jan 2018 23:49:58 +0000 (15:49 -0800)]
Windows: UsbDk: Fix some minor style/naming issues

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: UsbDk: Return appropriate error code for malloc failure
Chris Dickens [Fri, 5 Jan 2018 23:47:27 +0000 (15:47 -0800)]
Windows: UsbDk: Return appropriate error code for malloc failure

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: UsbDk: Fix direction determination for control transfers
Chris Dickens [Fri, 5 Jan 2018 23:45:18 +0000 (15:45 -0800)]
Windows: UsbDk: Fix direction determination for control transfers

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: UsbDk: Remove unnecessary empty functions
Chris Dickens [Fri, 5 Jan 2018 23:35:26 +0000 (15:35 -0800)]
Windows: UsbDk: Remove unnecessary empty functions

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: UsbDk: Cache system redirector handle
Chris Dickens [Fri, 5 Jan 2018 23:32:16 +0000 (15:32 -0800)]
Windows: UsbDk: Cache system redirector handle

This handle is needed for each transfer submission, so eliminate
repeated calls to obtain the handle by caching it during open.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: UsbDk: Use a better macro for getting context
Chris Dickens [Fri, 5 Jan 2018 23:23:06 +0000 (15:23 -0800)]
Windows: UsbDk: Use a better macro for getting context

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: UsbDk: Fix spelling of InterruptTransferType enum
Chris Dickens [Fri, 5 Jan 2018 23:16:19 +0000 (15:16 -0800)]
Windows: UsbDk: Fix spelling of InterruptTransferType enum

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: UsbDk: Fix some strange type-casting
Chris Dickens [Fri, 5 Jan 2018 23:22:10 +0000 (15:22 -0800)]
Windows: UsbDk: Fix some strange type-casting

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Add TargetName to getopt VS project files to supress warning
Chris Dickens [Fri, 5 Jan 2018 07:44:57 +0000 (23:44 -0800)]
Misc: Add TargetName to getopt VS project files to supress warning

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Sort projects alphabetically in VS solution files
Chris Dickens [Fri, 5 Jan 2018 07:33:09 +0000 (23:33 -0800)]
Misc: Sort projects alphabetically in VS solution files

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Fix references section in fxload VS project files
Chris Dickens [Fri, 5 Jan 2018 07:32:33 +0000 (23:32 -0800)]
Misc: Fix references section in fxload VS project files

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Fix linker options in getopt VS project files
Chris Dickens [Fri, 5 Jan 2018 07:32:14 +0000 (23:32 -0800)]
Misc: Fix linker options in getopt VS project files

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Add missing files to VS2005 projects for libusb
Chris Dickens [Fri, 5 Jan 2018 07:21:48 +0000 (23:21 -0800)]
Misc: Add missing files to VS2005 projects for libusb

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Fix minor discrepancies in WinCE MSVC files
Chris Dickens [Fri, 5 Jan 2018 07:19:47 +0000 (23:19 -0800)]
Misc: Fix minor discrepancies in WinCE MSVC files

Add missing header files, fix preprocessor definitions for static
library, and sort projects and files by name.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWinCE: Give a variable a more appropriate name, matching other patterns
Chris Dickens [Fri, 5 Jan 2018 05:55:01 +0000 (21:55 -0800)]
WinCE: Give a variable a more appropriate name, matching other patterns

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWinCE: Remove empty function for unsupported ISO transfers
Chris Dickens [Fri, 5 Jan 2018 05:54:01 +0000 (21:54 -0800)]
WinCE: Remove empty function for unsupported ISO transfers

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWinCE: Add back mistakenly removed errno variable
Chris Dickens [Fri, 5 Jan 2018 05:51:04 +0000 (21:51 -0800)]
WinCE: Add back mistakenly removed errno variable

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Rework WinUSB enumeration process to fix issues on Win8+
Chris Dickens [Fri, 5 Jan 2018 00:37:09 +0000 (16:37 -0800)]
Windows: Rework WinUSB enumeration process to fix issues on Win8+

As they sometimes do, Microsoft made changes to the way in which the
SetupAPI functions list the devices returned by SetupDiGetClassDevs().
In particular, composite devices started returning their interfaces
before the parent device, which caused some issues with the way the
enumeration logic was assigning things. For composite devices, it
appears that the first interface behaves much like the parent device in
some regards, so the library was creating a device specifically for the
first interface and then again when the actual parent device was
encountered. This caused composite devices to appear in the device list
twice, with the first instance being unusable for most operations.

This commit significantly changes the way in which the enumeration
process is done. Previously we would scan for HCDs, hubs, and generic
devices, in that order and in distinct passes (obtaining a new listing
of devices from SetupAPI). Now we will obtain a single snapshot at the
beginning of the enumeration process and iterate through this to scan
for each type of device.

With a single snapshot, we can be assured that the device instance
handle will not change between passes and thus we can use this as the
unique identifier. This completely removes the need to hash the device
instance ID to obtain a unique identifier and simplifies the process.

The previous enumeration process also created "dummy" libusb_device
instances for the HCDs that were never exposed to the user. This has
been removed in favor of identifying which of the encountered hubs are
actually root hubs.

Finally, the query for the port number has been moved to the GENeric
pass at the point where the devices are actually initialized. This query
operation has been relaxed to allow failure, since some virtual USB
devices don't properly implement this query in their drivers.

Closes #215, Closes #251, Closes #257, Closes #288

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Remove port member from device's private structure
Chris Dickens [Thu, 4 Jan 2018 23:52:56 +0000 (15:52 -0800)]
Windows: Remove port member from device's private structure

The libusb_device structure already has a port_number member that stores
the same information, so don't duplicate this elsewhere.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Be a bit smarter when setting composite interfaces
Chris Dickens [Thu, 4 Jan 2018 23:45:07 +0000 (15:45 -0800)]
Windows: Be a bit smarter when setting composite interfaces

Instead of open-coding the search for the "MI_" substring, use the
strstr() function to find the string and verify that the following two
characters are indeed digits.

Also guard against the possibility of the interface number being larger
than what we can support.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoPOSIX: Improve threading abstraction as done in f06c4f5b
Chris Dickens [Thu, 4 Jan 2018 21:47:34 +0000 (13:47 -0800)]
POSIX: Improve threading abstraction as done in f06c4f5b

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Further improve thread abstraction
Chris Dickens [Thu, 4 Jan 2018 21:42:52 +0000 (13:42 -0800)]
Windows: Further improve thread abstraction

Adopt typedefs and inline functions to get the benefits of type checking.

Convert all trivial functions to inline and remove return values where
they aren't checked.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Constify variables that aren't supposed to change
Chris Dickens [Thu, 4 Jan 2018 07:12:50 +0000 (23:12 -0800)]
Windows: Constify variables that aren't supposed to change

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Kill empty functions and switch to runtime checking
Chris Dickens [Thu, 4 Jan 2018 07:09:09 +0000 (23:09 -0800)]
Windows: Kill empty functions and switch to runtime checking

Instead of requiring each driver API to provide a complete set of
functions, some of which may be simple containers for returning an
unsupported error code, allow function pointers to be NULL and return
an error when a NULL pointer is encountered.

Also remove the unused sub_api parameter from the API init/exit
functions.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Remove unused DLL functions and header definitions
Chris Dickens [Thu, 4 Jan 2018 07:02:00 +0000 (23:02 -0800)]
Windows: Remove unused DLL functions and header definitions

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Fix some minor style issues
Chris Dickens [Thu, 4 Jan 2018 06:43:56 +0000 (22:43 -0800)]
Windows: Fix some minor style issues

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Improve locking in threading abstraction
Chris Dickens [Thu, 4 Jan 2018 05:18:02 +0000 (21:18 -0800)]
Windows: Improve locking in threading abstraction

Convert the usbi_mutex_t type to a CRITICAL_SECTION object. There
are numerous advantages including lower resource usage and a better
fast-path (doesn't require entering kernel space).

Simplify the condition variable implementation by not associating a
wait structure with a particular thread ID. This is not needed and
causes an unnecessary search through the linked list of any available
wait structures when the real optimization is just reusing an already
created event object.

Also, while here remove all the checks for NULL pointers because we
don't do such a silly thing inside the library.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Ensure that both WINVER and _WIN32_WINNT are defined for Windows builds
Chris Dickens [Thu, 4 Jan 2018 03:19:39 +0000 (19:19 -0800)]
Misc: Ensure that both WINVER and _WIN32_WINNT are defined for Windows builds

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Improve enumeration process
Chris Dickens [Wed, 3 Jan 2018 21:53:56 +0000 (13:53 -0800)]
Windows: Improve enumeration process

Prior to this commit, there were some limitations and inefficiencies
during the enmeration process.

First, the maximum number of device interface GUIDs that could be
enumerated was fixed at 64. This limit has been removed and the list
of GUIDs is dynamically resized as new ones are encountered. Logic has
also been added to detect the presence of duplicate GUIDs in order to
speed up the enumeration process.

Next, when searching for device interface GUIDs, only the
"DeviceInterfaceGUIDs" registry key was being consulted. Now we will
also consider "DeviceInterfaceGUID" in order to support devices that
have the GUID listed under this key (such as some WCID devices).

Finally, there used to be a static list of USB PnP enumerator strings
that were used to detect devices during the GENeric enumeration pass. In
many cases, this is wasteful as these enumerators are only present with
very specific hardware. To improve this, we now keep track of the USB
PnP enumerator string encountered as we enumerate the hubs. This allows
the enumeration process to only search for devices that could possibly
be present on the system given the hardware and drivers that were
encountered.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Kill perpetually growing list of hub driver names
Chris Dickens [Wed, 3 Jan 2018 21:53:25 +0000 (13:53 -0800)]
Windows: Kill perpetually growing list of hub driver names

During enumeration, it is unnecessary to match a hub against a list of
known driver names because hubs are enumerated with a specific device
interface GUID. This will eliminate the need for further expansions to
the hub driver list as manufacturers release new drivers.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Add _CRT_SECURE_NO_WARNINGS to testlibusb to suppress warning C4996
Chris Dickens [Fri, 29 Dec 2017 06:52:36 +0000 (22:52 -0800)]
Misc: Add _CRT_SECURE_NO_WARNINGS to testlibusb to suppress warning C4996

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agohotplug: Cast ssize_t to int in error path to avoid compiler warning
Chris Dickens [Fri, 29 Dec 2017 06:51:28 +0000 (22:51 -0800)]
hotplug: Cast ssize_t to int in error path to avoid compiler warning

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWinCE: Fixed incorrect check of return value, found by cppcheck
Sean McBride [Thu, 28 Dec 2017 04:55:34 +0000 (23:55 -0500)]
WinCE: Fixed incorrect check of return value, found by cppcheck

cppcheck was warning:

nullPointerRedundantCheck,libusb/os/wince_usb.c:280,warning,Either the condition '!discdevs' is redundant or there is possible null pointer dereference: discdevs.

In fact, the issue was just checking the wrong thing.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoFixed various trivial cppcheck 1.80 warnings
Sean McBride [Thu, 28 Dec 2017 04:53:27 +0000 (23:53 -0500)]
Fixed various trivial cppcheck 1.80 warnings

Specifically:

redundantAssignment,examples/dpfp.c:422,style,Variable 'r' is reassigned a value before the old one has been used.
redundantAssignment,libusb/os/threads_posix.c:64,style,Variable 'ret' is reassigned a value before the old one has been used.
unreadVariable,libusb/os/netbsd_usb.c:217,style,Variable 'hpriv' is assigned a value that is never used.
unreadVariable,libusb/os/netbsd_usb.c:235,style,Variable 'hpriv' is assigned a value that is never used.
unreadVariable,libusb/os/openbsd_usb.c:251,style,Variable 'hpriv' is assigned a value that is never used.
unreadVariable,libusb/os/openbsd_usb.c:275,style,Variable 'hpriv' is assigned a value that is never used.
unsignedLessThanZero,libusb/os/windows_winusb.c:259,style,Checking if unsigned variable '_index' is less than zero.
unsignedLessThanZero,libusb/os/windows_winusb.c:298,style,Checking if unsigned variable '_index' is less than zero.
unsignedLessThanZero,libusb/os/windows_winusb.c:367,style,Checking if unsigned variable '_index' is less than zero.
invalidPrintfArgType_sint,examples/xusb.c:534,warning,%d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoRemoved accidental double semi colons at end of line
Sean McBride [Thu, 28 Dec 2017 04:35:42 +0000 (23:35 -0500)]
Removed accidental double semi colons at end of line

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agocore: Fixed deprecated attribute to work on clang
Sean McBride [Thu, 28 Dec 2017 03:42:28 +0000 (22:42 -0500)]
core: Fixed deprecated attribute to work on clang

clang was warning:

core.c:2011:5: Declaration is marked with '\deprecated' command but
does not have a deprecation attribute

This was because LIBUSB_DEPRECATED_FOR was checking for gcc >= 4.5
whereas clang identifies itself as gcc 4.2. So fallback to
__attribute__((deprecated)) without a message string on older GCCs
(and thus clang).

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoexamples: xusb: replaced insecure sprintf with snprintf
Sean McBride [Thu, 28 Dec 2017 03:34:22 +0000 (22:34 -0500)]
examples: xusb: replaced insecure sprintf with snprintf

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoFix unused parameter warnings
Sean McBride [Thu, 28 Dec 2017 03:32:15 +0000 (22:32 -0500)]
Fix unused parameter warnings

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoexamples: ezusb: Renamed header guard variable to not use reserved identifier
Sean McBride [Wed, 14 Sep 2016 16:45:39 +0000 (12:45 -0400)]
examples: ezusb: Renamed header guard variable to not use reserved identifier

Fixes clang -Wreserved-id-macro warning.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agolinux_usbfs: libusb_init() should succeed if no devices are present
Chris Dickens [Thu, 28 Dec 2017 08:40:14 +0000 (00:40 -0800)]
linux_usbfs: libusb_init() should succeed if no devices are present

When using sysfs to scan for devices, libusb_init() will fail if there
are no USB devices present. There is no reason for this behavior, so
this commit modifies the logic to only return an error if one or more
devices are present but none could be successfully enumerated.

Closes #301

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agohotplug: Improve internal structure and deregistration mechanism
Chris Dickens [Thu, 28 Dec 2017 07:30:16 +0000 (23:30 -0800)]
hotplug: Improve internal structure and deregistration mechanism

This commit shrinks the size of the internal hotplug callback structure
by removing unused fields, using the correctly sized types for matching
fields, and adding a new flags field whose bits control how the callback
structure should behave.

The hotplug callback handle ID counter has also been moved to the
context structure instead of being a global variable shared amongst all
contexts. This lets each context independently manage handle IDs and use
the maximum range of possible IDs.

Finally, the hotplug callback deregistration mechanism has been improved
to signal to the event handler that an explicit deregistration needs to
be handled. This removes the need to send a dummy hotplug message, which
was using an invalid libusb_hotplug_event value anyway that was causing
some compilers to complain.

Closes #373

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Fix testlibusb build on VS2013 and earlier and suppress warnings
Chris Dickens [Wed, 27 Dec 2017 07:58:00 +0000 (23:58 -0800)]
Misc: Fix testlibusb build on VS2013 and earlier and suppress warnings

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agolinux_usbfs: Improve isochronous transfer submission and error reporting
Chris Dickens [Wed, 27 Dec 2017 03:15:42 +0000 (19:15 -0800)]
linux_usbfs: Improve isochronous transfer submission and error reporting

The Linux kernel has changed the maximum allowed packet length per
isochronous packet numerous times, which can create difficulties in
trying to report appropriate errors back to the user when submitting too
large of a packet on older kernels.

In an attempt to improve this situation, this commit adds logic that
will use different per-packet limits based on the detected kernel
version. Additionally, the logic has been improved to split URBs based
on the number of isochronous packets per URB, which is currently (and
has been forever) limited to 128.

Finally, the error reporting during URB submission has been improved to
catch and report errors relating to the transfer length being too large.

Closes #118

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Add VS 2010/2012/2013/2017 project files for testlibusb
Chris Dickens [Wed, 27 Dec 2017 01:25:12 +0000 (17:25 -0800)]
Windows: Add VS 2010/2012/2013/2017 project files for testlibusb

Inspired by PR #201 (commit fa19c152), this commit adds the same project
files for the other supported versions of Visual Studio.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: testlibusb example for VS2015
Anil Nair [Fri, 29 Jul 2016 15:36:28 +0000 (21:06 +0530)]
Windows: testlibusb example for VS2015

Closes #201

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoExamples: Misc. cleanup to xusb
Chris Dickens [Wed, 27 Dec 2017 00:37:34 +0000 (16:37 -0800)]
Examples: Misc. cleanup to xusb

Make data that is unchanged const, remove an unused return value, and
add a missing newline to an error message.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoExamples: Improvements to xusb's support for Microsoft OS descriptors
Chris Dickens [Wed, 27 Dec 2017 00:37:22 +0000 (16:37 -0800)]
Examples: Improvements to xusb's support for Microsoft OS descriptors

As noted in issue #269, the current xusb implementation does not handle
Microsoft OS descriptors from devices whose vendor code is greater than
0x7F. This commit addresses this limitation by using
libusb_get_string_descriptor() instead of the ASCII variant and parsing
the descriptor separately. Note that this issue was addressed in PR #276,
but that approach was too cryptic to read.

Closes #269, Closes #276

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Fix pre-commit hook to not emit errors in non-Bash shells
Chris Dickens [Tue, 26 Dec 2017 23:46:13 +0000 (15:46 -0800)]
Misc: Fix pre-commit hook to not emit errors in non-Bash shells

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoSolaris: Add detach/attach kernel driver support
Joshua [Fri, 2 Dec 2016 02:37:23 +0000 (10:37 +0800)]
Solaris: Add detach/attach kernel driver support

CD: Add missing error checking/reporting and fix some style issues

Closes #236

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Report correct usagePage and usage values for HID
chunyu.xie [Wed, 8 Mar 2017 10:45:11 +0000 (18:45 +0800)]
Windows: Report correct usagePage and usage values for HID

Closes #278, Closes #281

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoWindows: Fix libusb_get_string_descriptor for HID devices
chunyu.xie [Wed, 8 Mar 2017 10:11:06 +0000 (18:11 +0800)]
Windows: Fix libusb_get_string_descriptor for HID devices

When querying vendor-defined string descriptors, use GetIndexedString
of the HID library.

Closes #279, Closes #280

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agolinux_usbfs: Remove old comment
Victor Toso [Wed, 13 Dec 2017 09:27:21 +0000 (10:27 +0100)]
linux_usbfs: Remove old comment

This comment was introduced in 737ba04ea40f195 together with
a few locking calls usbi_mutex_lock() which were removed by
a886bb02c87dd5f.

Signed-off-by: Victor Toso <victortoso@redhat.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agolinux_udev: Initialize pointer before calling external API
Victor Toso [Wed, 13 Dec 2017 08:17:43 +0000 (09:17 +0100)]
linux_udev: Initialize pointer before calling external API

udev_list_entry_foreach() sets the entry variable for each device but
initializing the pointer to NULL to avoid warnings.

Found by Coverity:

libusb-1.0.21/libusb/os/linux_udev.c:273:
error[uninitvar]: Uninitialized variable: entry
>   271|    devices = udev_enumerate_get_list_entry(enumerator);
>   272|
>   273|->  udev_list_entry_foreach(entry, devices) {
>   274|        const char *path = udev_list_entry_get_name(entry);
>   275|        uint8_t busnum = 0, devaddr = 0;

Signed-off-by: Victor Toso <victortoso@redhat.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agolinux_usbfs: Set urb->buffer_length for isochronous transfers
Jakub Klama [Wed, 22 Nov 2017 19:40:09 +0000 (20:40 +0100)]
linux_usbfs: Set urb->buffer_length for isochronous transfers

Linux 4.13 expects buffer_length to match sum of iso descriptor lengths.

Closes #365

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agolinux_usbfs: Fixed excessive check
Roman Kalashnikov [Sat, 28 Oct 2017 17:31:39 +0000 (20:31 +0300)]
linux_usbfs: Fixed excessive check

Closes #358

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoexamples: Fix device handle leak in xusb
Chris Dickens [Thu, 24 Aug 2017 22:52:39 +0000 (15:52 -0700)]
examples: Fix device handle leak in xusb

The CALL_CHECK macro returns from the current function, so in the
test_device() function the device handle was being leaked when one of
the functions failed. This commit adds a new CALL_CHECK_CLOSE macro that
does the same as CALL_CHECK but also closes the device handle before
returning. In addition, the macros are changed to declare their needed
variable rather than relying on the variable to already exist within the
scope of the function.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoio: Clear transfer timeout struct when transfer has no timeout
Chris Dickens [Sat, 19 Aug 2017 00:30:38 +0000 (17:30 -0700)]
io: Clear transfer timeout struct when transfer has no timeout

Prior to this commit, a transfer that was submitted with a non-zero
timeout would retain this timeout if the transfer was resubmitted with
a zero (unlimited) timeout. This commit corrects this by clearing the
transfer's timeout if the desired timeout is zero.

Closes #332

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agodarwnin: fix compilation warning
Ludovic Rousseau [Fri, 28 Jul 2017 09:43:42 +0000 (11:43 +0200)]
darwnin: fix compilation warning

os/darwin_usb.c:81:14: warning: initializing 'char *' with an expression of type
      'const char [12]' discards qualifiers
      [-Wincompatible-pointer-types-discards-qualifiers]
static char *darwin_device_class = kIOUSBDeviceClassName;
             ^                     ~~~~~~~~~~~~~~~~~~~~~

6 years agoFix typo in exit() function signature
jonner [Thu, 27 Jul 2017 20:28:48 +0000 (15:28 -0500)]
Fix typo in exit() function signature

This typo caused a bunch of compile warnings about incompatible pointer types.

Closes #327

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agocore: Preparation for handling backend-specific options
Chris Dickens [Sun, 16 Jul 2017 21:41:55 +0000 (14:41 -0700)]
core: Preparation for handling backend-specific options

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agocore: Pass context to backend when calling exit() function
Chris Dickens [Sun, 16 Jul 2017 21:18:30 +0000 (14:18 -0700)]
core: Pass context to backend when calling exit() function

Providing the context to the exit() function will allow backends to
perform context-specific teardown, if needed.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agocore: Introduce libusb_set_option() API function
Chris Dickens [Tue, 11 Jul 2017 05:37:13 +0000 (22:37 -0700)]
core: Introduce libusb_set_option() API function

This new function allows more flexibility in extending the library to
support more user-configurable options. It is intended to provide a
single API that can support a wide variety of needs and eliminates the
need for new API functions to set future options.

The function is introduced with a single option (LIBUSB_OPTION_LOG_LEVEL)
that replaces the libusb_set_debug() function. Documentation relating to
libusb_set_debug() and the uses of this function in the examples and
tests have been updated accordingly.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agocore: Slightly refactor and improve logging code
Chris Dickens [Wed, 12 Jul 2017 18:29:37 +0000 (11:29 -0700)]
core: Slightly refactor and improve logging code

This change refactors the code that reads the LIBUSB_DEBUG environment
variable into a function to avoid code duplication and ensure that the
value is coerced into a valid libusb_log_level value.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoexamples: reformat testlibusb.c
Ludovic Rousseau [Wed, 12 Jul 2017 14:54:44 +0000 (16:54 +0200)]
examples: reformat testlibusb.c

convert from DOS to Unix text format
remove extra spaces at end of line

6 years agocore: Fix --disable-log option to remove logging functionality
Chris Dickens [Thu, 6 Jul 2017 01:42:25 +0000 (18:42 -0700)]
core: Fix --disable-log option to remove logging functionality

Prior to this commit, building the library with the '--disable-log'
option would compile out the debug messages, but the logging functions
remained and would never be used. This commit brings the logging
functions themselves into consideration when building the library.

Additionally, the logging code has been optimized to compile away
unnecessary checks when the '--enable-debug-log' is used and to remove
the debug variables from the context if the library is compiled in such
a way that the debug level is not configurable.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agocore: Add provisions for per-context private backend data
Chris Dickens [Wed, 5 Jul 2017 22:01:58 +0000 (15:01 -0700)]
core: Add provisions for per-context private backend data

This functionality will be useful for backends that need to maintain
data specific to a context.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agocore: Kill usbi_os_backend structure definition madness
Chris Dickens [Wed, 5 Jul 2017 20:44:30 +0000 (13:44 -0700)]
core: Kill usbi_os_backend structure definition madness

Prior to this commit, each backend provided its own uniquely named
usbi_os_backend structure and a massive pile of #ifdefs assigned the
global usbi_backend pointer to the correct one. This commit kills off
all this code and instead has each backend provide the usbi_backend
symbol directly. The linker can inform of any issues that might arise
with symbols.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Ensure the 'os_priv' member of structures is pointer aligned
Chris Dickens [Wed, 5 Jul 2017 20:15:21 +0000 (13:15 -0700)]
Misc: Ensure the 'os_priv' member of structures is pointer aligned

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
6 years agoMisc: Refactor zero-length array declarations
Chris Dickens [Wed, 5 Jul 2017 20:09:29 +0000 (13:09 -0700)]
Misc: Refactor zero-length array declarations

Instead of checking for __STDC_VERSION__ at every instance, define a
ZERO_SIZED_ARRAY macro that has the appropriate definition and use that
everywhere it is needed.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agolibusb.h: use __linux__ instead of __linux
Adrian Bunk [Sun, 18 Jun 2017 20:45:54 +0000 (22:45 +0200)]
libusb.h: use __linux__ instead of __linux

The check was added since sys/time.h is not available on Windows,
but breaks on PowerPC where __linux is not defined by GCC in strict
standards modes.

Signed-off-by: Adrian Bunk <bunk@debian.org>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
7 years agoEmpty commit to resync nano
Chris Dickens [Tue, 6 Jun 2017 06:24:21 +0000 (23:24 -0700)]
Empty commit to resync nano

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Fixed USB_CONFIGURATION_DESCRIPTOR struct alignment issue (10 bytes size...
l_proger [Fri, 21 Apr 2017 11:38:32 +0000 (14:38 +0300)]
Windows: Fixed USB_CONFIGURATION_DESCRIPTOR struct alignment issue (10 bytes size instead of 9)

Closes #293

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Header file cleanup
Chris Dickens [Tue, 6 Jun 2017 06:18:13 +0000 (23:18 -0700)]
Windows: Header file cleanup

This commit removes unused or redundant definitions from the
windows_winusb.h header file and leverages existing definitions
within the core library headers. The definitions of structures
related to the Windows API are also modified to match official
headers.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoAlign error code handling with WinUsb
Sameeh Jubran [Sun, 14 May 2017 09:14:25 +0000 (12:14 +0300)]
Align error code handling with WinUsb

WinUsb uses ERROR_GEN_FAILURE instead of ERROR_FUNCTION_FAILED,
this diffrence was observed while using Libusb's xusb example
application while alternating backends.

Signed-off-by: Sameeh Jubran <sameeh@daynix.com>
Reviewed-by: Dmitry Fleytman <dmitry@daynix.com>
7 years agoWindows: Fix regression introduced by 05b0b55b
Chris Dickens [Fri, 12 May 2017 04:33:04 +0000 (21:33 -0700)]
Windows: Fix regression introduced by 05b0b55b

Closes #298, Closes #300

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agogit: Don't ignore platform config.h
Andy McFadden [Thu, 30 Mar 2017 22:17:38 +0000 (15:17 -0700)]
git: Don't ignore platform config.h

The android and Xcode config.h headers were being ignored.

7 years agoWindows: Fix possible memory leak when caching descriptors fails
Chris Dickens [Mon, 27 Mar 2017 03:37:41 +0000 (20:37 -0700)]
Windows: Fix possible memory leak when caching descriptors fails

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agolinux: Apply v2 version of patch for CLOEXEC changes
Chris Dickens [Mon, 27 Mar 2017 03:19:40 +0000 (20:19 -0700)]
linux: Apply v2 version of patch for CLOEXEC changes

Somehow I managed to push v1 of the patch, which didn't handle older
versions of the kernel or C library that don't support the O_CLOEXEC
flag (suggested by Tim Roberts). This commit amends the original
changes in aa73b2ec and provides this backwards compatibility.

Closes #286

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agomacOS Sierra: Fix detection of parent devices.
tich [Mon, 13 Mar 2017 16:37:33 +0000 (09:37 -0700)]
macOS Sierra: Fix detection of parent devices.

This fixes the output of libusb_get_port_numbers(), where it used to merely return the port number of the device itself.
Now that libusb properly detects parent devices, we get the entire port path of the device.
For reference, here's the output of the 'listdevs' example before my changes:

tich-mbp:libusb-1.0.21 tich$ ./examples/listdevs
0409:005a (bus 29, device 6) path: 1
05ac:8509 (bus 26, device 2) path: 1
05ac:821d (bus 29, device 7) path: 3
05ac:0252 (bus 29, device 5) path: 3
05ac:8242 (bus 29, device 4) path: 2
0a5c:4500 (bus 29, device 3) path: 1
0424:2513 (bus 29, device 2) path: 8
8087:0024 (bus 29, device 1) path: 1
8087:0024 (bus 26, device 1) path: 1

And here's the output after my changes:

tich-mbp:libusb-1.0.21 tich$ ./examples/listdevs
0409:005a (bus 29, device 6) path: 1.1
05ac:8509 (bus 26, device 2) path: 1.1
05ac:821d (bus 29, device 7) path: 1.8.1.3
05ac:0252 (bus 29, device 5) path: 1.8.3
05ac:8242 (bus 29, device 4) path: 1.8.2
0a5c:4500 (bus 29, device 3) path: 1.8.1
0424:2513 (bus 29, device 2) path: 1.8
8087:0024 (bus 29, device 1) path: 1
8087:0024 (bus 26, device 1) path: 1

Since we're now using "IOUSBHostDevice" as the matching service (starting with El Capitan), we need to walk up the IOService plane to look for parents.
You can manually walk up the IOService plane by executing "ioreg -p IOService -l -w 0 -x | less" and looking for your device. Notice that the parent device is always a parent in the tree, but it's not always the _immediate_ parent of the device.

7 years agodarwin: use clock_gettime() on 10.12+
Nathan Hjelm [Thu, 16 Mar 2017 18:54:22 +0000 (12:54 -0600)]
darwin: use clock_gettime() on 10.12+

Apple implemented the POSIX.2 clock_gettime () call in macOS
10.12. This commit updates the darwin backend to not use the mach
clock if clock_gettime() is available.

Signed-off-by: Nathan Hjelm <hjelmn@mac.com>
7 years agoWindows: Allow HID reports up to 1024 + report id
Stefan Agner [Wed, 2 Nov 2016 01:27:12 +0000 (18:27 -0700)]
Windows: Allow HID reports up to 1024 + report id

HID reports can contain up to 1024 bytes. When using a report id
the complete buffer will be 1025 bytes. Do not return an error
if the user supplies a report id and a buffer of 1025 bytes size.

Closes #222
Closes #226

Signed-off-by: Stefan Agner <stefan.agner@toradex.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agocore: Improve robustness of event handling
Chris Dickens [Wed, 1 Mar 2017 07:06:39 +0000 (23:06 -0800)]
core: Improve robustness of event handling

Prior to this commit, it was possible on Linux to poll a file descriptor
that was not valid for libusb. This could happen given the following
sequence of events:

  1) Application registers hotplug callback
  2) Application calls into libusb to handle events
  3) USB device is unplugged, triggering hotplug event
  4) Event handling thread calls registered hotplug callback
  5) Hotplug callback calls libusb_close(), which closes the fd
  6) Event handling code retries the poll() call before returning

At this point, the file descriptor that is being passed to poll() may be
invalid or worse may not belong to libusb at all.

This commit fixes this by removing the code that restarted the poll() call.
It was originally added to allow the processing of multiple hotplug messages
when these messages were delivered across a pipe, but with a linked list now
holding the hotplug messages this is no longer a concern. If any hotplug
messages are present on the list, they are all processed in one pass.

Closes #238

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agocore: Silence 'unhandled case in switch' warning.
Josh Gao [Wed, 22 Feb 2017 21:27:11 +0000 (13:27 -0800)]
core: Silence 'unhandled case in switch' warning.
linux_usbfs: silence -Wsign-compare.

Closes #274

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Remove initialization of calloc'ed memory
Chris Dickens [Wed, 1 Mar 2017 05:37:20 +0000 (21:37 -0800)]
Windows: Remove initialization of calloc'ed memory

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: return proper status
Doug Johnston [Wed, 28 Dec 2016 19:21:04 +0000 (11:21 -0800)]
Windows: return proper status

currently always returning `LIBUSB_SUCCESS` even if error was found

Closes #183

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agotravis: Pass on arguments given to travis_autogen.sh
Chris Dickens [Wed, 1 Mar 2017 05:29:04 +0000 (21:29 -0800)]
travis: Pass on arguments given to travis_autogen.sh

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agolinux: Mark internal file descriptors as CLOEXEC
Chris Dickens [Mon, 20 Feb 2017 08:55:15 +0000 (00:55 -0800)]
linux: Mark internal file descriptors as CLOEXEC

As a library, libusb should take care to be as friendly as possible
with various use cases. One such way is to ensure that internal file
descriptors have the CLOEXEC flag set, thus allowing processes to do
a fork() + exec() without leaking libusb's file descriptors to the
child process.

References #268

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agolinux_netlink: Retry poll() on EINTR
Chris Dickens [Mon, 20 Feb 2017 08:51:14 +0000 (00:51 -0800)]
linux_netlink: Retry poll() on EINTR

This commit applies the same fix as commit 0a02d121 did for udev.
I've also removed usbi_ from the read()/write() function calls as
it is pointless.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years ago.gitignore: Add some new entries
Chris Dickens [Mon, 20 Feb 2017 08:24:51 +0000 (00:24 -0800)]
.gitignore: Add some new entries

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoconfigure.ac: Remove obsolete AC_ERROR and make formatting consistent
Chris Dickens [Mon, 20 Feb 2017 08:18:33 +0000 (00:18 -0800)]
configure.ac: Remove obsolete AC_ERROR and make formatting consistent

Remove unnecessary quotes around simple string values.
Replace AC_ERROR with AC_MSG_ERROR.
Use [] around arguments to macros.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agomisc: update active developers list
Pete Batard [Wed, 18 Jan 2017 14:17:38 +0000 (14:17 +0000)]
misc: update active developers list

* Removing myself, since I am no longer involved in libusb

7 years agoWindows: Remove unused safe_* macro definitions
Chris Dickens [Thu, 12 Jan 2017 22:43:29 +0000 (14:43 -0800)]
Windows: Remove unused safe_* macro definitions

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