platform/upstream/libusb.git
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>
7 years agoWindows: Slightly optimize the sanitize_path() function
Chris Dickens [Thu, 12 Jan 2017 22:42:47 +0000 (14:42 -0800)]
Windows: Slightly optimize the sanitize_path() function

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Switch to snprintf() in windows_get_version()
Chris Dickens [Thu, 12 Jan 2017 22:40:42 +0000 (14:40 -0800)]
Windows: Switch to snprintf() in windows_get_version()

Also remove "Windows" from the constructed string since that will
always be there and just complicates the logic.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Fix memory and handle leaks in winusbx_claim_interface()
Chris Dickens [Thu, 12 Jan 2017 22:36:20 +0000 (14:36 -0800)]
Windows: Fix memory and handle leaks in winusbx_claim_interface()

When using the libusb0 filter driver, the loop could potentially leak the
memory for dev_interface_details. In that same loop, if the dev path
without the GUID matches the interface path, the new file handle would
be leaked.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Fix some minor styling issues
Chris Dickens [Thu, 12 Jan 2017 22:35:15 +0000 (14:35 -0800)]
Windows: Fix some minor styling issues

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Clean up unnecessary variable initialization
Chris Dickens [Thu, 12 Jan 2017 22:29:32 +0000 (14:29 -0800)]
Windows: Clean up unnecessary variable initialization

Don't zero out variables that already are zero (calloc'ed) and
don't set variables that aren't read before being set again.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows/WinCE: Remove uses of safe_* macros where not useful
Chris Dickens [Thu, 12 Jan 2017 22:27:14 +0000 (14:27 -0800)]
Windows/WinCE: Remove uses of safe_* macros where not useful

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows/WinCE: Improvements to windows_error_str() function
Chris Dickens [Thu, 12 Jan 2017 22:07:02 +0000 (14:07 -0800)]
Windows/WinCE: Improvements to windows_error_str() function

1) Add FORMAT_MESSAGE_IGNORE_INSERTS to flags for security.
2) Optimize removal of CR/LF terminators
3) Switch to use snprintf()

Windows-specific:
  1) Don't waste time converting debug messages to unicode

WinCE-specific:
  1) Get rid of TCHAR code

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Provide C99 compliant implementations of (v)snprintf()
Chris Dickens [Thu, 12 Jan 2017 21:55:55 +0000 (13:55 -0800)]
Windows: Provide C99 compliant implementations of (v)snprintf()

The Microsoft implementations of _snprintf() and vsnprintf() do not
guarantee that the output buffer is NULL-terminated, and they do not
return the number of bytes that *would* have been written if truncation
were to occur. This commit adds implementations that do just that.

Note that VS2015 and above provide C99 compliant versions of these
functions, so those will be used when available.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Hash table improvements
Chris Dickens [Thu, 12 Jan 2017 20:46:18 +0000 (12:46 -0800)]
Windows: Hash table improvements

1) The current implementation was not entirely thread-safe. The hash
   table was read outside of the mutex, which in some cases could lead
   to improper hashing.
2) The hash creation function accepted a size parameter that was
   hard-coded. Eliminating this saves time because we can ensure that
   the pre-defined size is a prime number instead of requiring code to
   calculate a prime number.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoconfigure.ac: Clean up style
Chris Dickens [Thu, 12 Jan 2017 21:50:35 +0000 (13:50 -0800)]
configure.ac: Clean up style

For consistency, only use quotes for strings where variables are
interpolated.

Also make the check for syslog capability dependent upon whether system
logging has been enabled.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agocore: Remove use of gettimeofday()
Chris Dickens [Thu, 12 Jan 2017 19:01:59 +0000 (11:01 -0800)]
core: Remove use of gettimeofday()

Prior to this commit, gettimeofday() was being used for timestamps
in debug messages. This function has been marked as deprecated in
the latest POSIX specification and furthermore is not supported on
Windows, thus requiring our own implementation as usbi_gettimeofday().

This commit changes the logging code to obtain timestamps using the
clock_gettime() function provided by the backend. The implementation of
usbi_gettimeofday() for Windows was actually equivalent to that of the
USBI_CLOCK_REALTIME implementation for clock_gettime(), so this
eliminates code duplication. In addition, the USBI_CLOCK_REALTIME
implementation has been updated for Windows to leverage the
timespec_get() function available in VS 2015 and later.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agocore: Remove check for and inclusion of signal.h
Chris Dickens [Thu, 12 Jan 2017 17:59:13 +0000 (09:59 -0800)]
core: Remove check for and inclusion of signal.h

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Fix DDK build's missing files
Chris Dickens [Mon, 9 Jan 2017 23:29:55 +0000 (15:29 -0800)]
Windows: Fix DDK build's missing files

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoWindows: Add Visual Studio 2017 support
Chris Dickens [Fri, 6 Jan 2017 18:58:47 +0000 (10:58 -0800)]
Windows: Add Visual Studio 2017 support

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agotravis: remove -fdiagnostics-color=auto
Ludovic Rousseau [Wed, 4 Jan 2017 21:52:08 +0000 (22:52 +0100)]
travis: remove -fdiagnostics-color=auto

-fdiagnostics-color=auto is not supported bug gcc version 4.8.4
gcc: error: unrecognized command line option '-fdiagnostics-color=auto'

7 years agotravis: remove -Werror
Ludovic Rousseau [Wed, 4 Jan 2017 19:10:35 +0000 (20:10 +0100)]
travis: remove -Werror

Using -Werror in CFLAGS make ./configure to fails with:
 checking whether the C compiler works... no
 configure: error: in `/home/travis/build/LudovicRousseau/libusb':
 configure: error: C compiler cannot create executables
 See `config.log' for more details

-Werror had no effect because CFLAGS was _not_ exported.

7 years agotravis: use bash for travis-autogen.sh
Ludovic Rousseau [Wed, 4 Jan 2017 17:15:30 +0000 (18:15 +0100)]
travis: use bash for travis-autogen.sh

Problem was:
./travis-autogen.sh: 6: ./travis-autogen.sh: CFLAGS+= -Wbad-function-cast: not found
./travis-autogen.sh: 8: ./travis-autogen.sh: CFLAGS+= -Wchar-subscripts: not found
[...]

7 years agotravis: do not use parallel compilations
Ludovic Rousseau [Wed, 4 Jan 2017 16:54:35 +0000 (17:54 +0100)]
travis: do not use parallel compilations

The goal is not to be fast but to get a clean log with not mixed
messages between 2 compilations executed at the same time.

7 years agotravis: make clean before building
Ludovic Rousseau [Wed, 4 Jan 2017 16:51:04 +0000 (17:51 +0100)]
travis: make clean before building

Call "make clean" to be sure the compilation will be done even if some
files were already compiled.

The problem was that the travis script does a double ./configure + build
on Linux. The second build just did nothing since the files were already
compiled.

7 years agodarwin: Replace bzero() with memset()
Vianney le Clément de Saint-Marcq [Mon, 11 Apr 2016 10:33:20 +0000 (12:33 +0200)]
darwin: Replace bzero() with memset()

The bzero() function is a deprecated BSD function that can be replaced
by the standard C90 memset() function.

From memset(4) manpage:
  The memset() function conforms to ISO/IEC 9899:1990 (``ISO C90'').

From bzero(3) manpage:
  bzero() was deprecated in IEEE Std 1003.1-2001 (``POSIX.1'') and removed
  in IEEE Std 1003.1-2008 (``POSIX.1'').

When compiling for POSIX 2008, the bzero() function is undefined on the
OS X platform.

7 years agoMisc: Fix a Doxygen comment typo
mojocorp [Tue, 3 Jan 2017 21:56:46 +0000 (22:56 +0100)]
Misc: Fix a Doxygen comment typo

7 years agolinux_get_parent_info: Check for NULL priv->sysfs_dir before strcmp
Matthew Stapleton [Mon, 30 Nov 2015 01:22:00 +0000 (11:22 +1000)]
linux_get_parent_info: Check for NULL priv->sysfs_dir before strcmp

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agoFix a race condition with libusb_interrupt_event_handler().
Lars Kanis [Sun, 27 Nov 2016 16:41:01 +0000 (17:41 +0100)]
Fix a race condition with libusb_interrupt_event_handler().

USBI_EVENT_USER_INTERRUPT isn't set in case of another pending event. When
libusb_interrupt_event_handler() is called and USBI_EVENT_POLLFDS_MODIFIED is
already set, but libusb_handle_events() isn't currently active waiting in
poll(), the interrupt request will be cleared before poll() is called. poll()
therefore blocks and the event handler doesn't return.

This is especially the case, when libusb_interrupt_event_handler() is called
before the first call to libusb_handle_events(). In this case
libusb_handle_events() blocks instead of returning immediately.

This patch fixes the race condition by setting the event flag the same way a
usbi_fd_notification().

Signed-off-by: Lars Kanis <lars@greiz-reinsdorf.de>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agolinux_udev: Retry poll() on EINTR
Romain Vimont [Tue, 11 Oct 2016 19:51:31 +0000 (21:51 +0200)]
linux_udev: Retry poll() on EINTR

The poll() syscall may temporarily fail when it is interrupted by a
signal; -1 is returned and errno is set to EINTR.

When this occurred, the udev event thread exited.

Instead, since this is a temporary failure, just try the call again.
<https://www.gnu.org/software/libc/manual/html_node/Interrupted-Primitives.html>

Signed-off-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agoRelease 1.0.21 upstream/1.0.21
Nathan Hjelm [Tue, 25 Oct 2016 16:07:38 +0000 (10:07 -0600)]
Release 1.0.21

Closes #93

Signed-off-by: Nathan Hjelm <hjelmn@mac.com>
7 years agoRemove generated file
Nathan Hjelm [Tue, 25 Oct 2016 16:05:20 +0000 (10:05 -0600)]
Remove generated file

Signed-off-by: Nathan Hjelm <hjelmn@mac.com>
7 years agofix travis matrix build
Nathan Hjelm [Sat, 1 Oct 2016 14:12:19 +0000 (08:12 -0600)]
fix travis matrix build

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agotry to get travis building with multiple macOS versions
Nathan Hjelm [Sat, 1 Oct 2016 13:42:30 +0000 (07:42 -0600)]
try to get travis building with multiple macOS versions

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agotravis osx fix
Nathan Hjelm [Sat, 1 Oct 2016 13:20:40 +0000 (07:20 -0600)]
travis osx fix

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agoWindows: Fix possible false error message during device enumeration
Chris Dickens [Wed, 14 Sep 2016 05:09:50 +0000 (22:09 -0700)]
Windows: Fix possible false error message during device enumeration

During early phases of enumeration, it is possible for a device to be
enumerated before its parent. When this occurs, the device assigned as
the parent will actually be a grandparent. During later phases of
enumeration, the parent device will in fact exist but will not match
what has already been assigned to the device.

This commit adds code to check for and update the parent device when
this situation occurs.

Closes #206

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
7 years agoon to 1.0.21-rc6
Nathan Hjelm [Sat, 1 Oct 2016 12:49:09 +0000 (06:49 -0600)]
on to 1.0.21-rc6

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agodarwin: fix OSAtomicAdd32Barrier argument ordering
Nathan Hjelm [Wed, 21 Sep 2016 15:01:38 +0000 (09:01 -0600)]
darwin: fix OSAtomicAdd32Barrier argument ordering

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agoon to 1.0.21-rc5
Nathan Hjelm [Wed, 14 Sep 2016 18:32:54 +0000 (12:32 -0600)]
on to 1.0.21-rc5

Signed-off-by: Nathan Hjelm <hjelmn@me.gov>
7 years agoFixed Obj-C GC support; fix pthread name
Sean McBride [Wed, 14 Sep 2016 16:50:09 +0000 (12:50 -0400)]
Fixed Obj-C GC support; fix pthread name

Fixed some recent regressions:
- Always call pthread_setname_np. Recent change removed this if > 10.8.
- Always call objc_registerThreadWithCollector(), except if deploying to 10.12 or later, which GC is removed anyway.

NTH: Note, the header in macOS 10.12 documents that the auto-release
feature was removed and "does nothing" since 10.8. The source code
available from 10.8-10.11 clearly shows that the
objc_registerThreadWithCollector call does something so we will leave
it enabled for this version range. More details are needed from Apple
to interpret the meaning of the deprecation message in 10.12.

Closes #210

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agoREADME: fix my email address
Nathan Hjelm [Tue, 13 Sep 2016 04:04:22 +0000 (22:04 -0600)]
README: fix my email address

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agodarwin: fix typo
Nathan Hjelm [Tue, 13 Sep 2016 04:00:04 +0000 (22:00 -0600)]
darwin: fix typo

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agoon to 1.0.21-rc4
Nathan Hjelm [Tue, 13 Sep 2016 03:42:23 +0000 (21:42 -0600)]
on to 1.0.21-rc4

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agodarwin: do not use deprecated OSAtomicIncrement32Barrier in 10.12
Nathan Hjelm [Tue, 13 Sep 2016 03:38:44 +0000 (21:38 -0600)]
darwin: do not use deprecated OSAtomicIncrement32Barrier in 10.12

This commit fixes a warning introduced by macOS 10.12 Sierra when
using the OS atomics. These atomics have been deprecated in favor
of the ones provided by C11 (stdatomic.h). On older versions of
OS X we still use the OS atomics (now OSAtomicAdd32Barrier) and
on 10.12.0 and newer we use atomic_fetch_add.

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agodarwin: do not use objc_registerThreadWithCollector where deprecated
Nathan Hjelm [Thu, 8 Sep 2016 00:50:43 +0000 (18:50 -0600)]
darwin: do not use objc_registerThreadWithCollector where deprecated

This commit updates the check around objc_registerThreadWithCollector
to enable the call only between 10.6 (introduced) and 10.8 (deprecated).

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agodarwin: work around devices with buggy endpoint descriptors
Nathan Hjelm [Thu, 8 Sep 2016 00:47:25 +0000 (18:47 -0600)]
darwin: work around devices with buggy endpoint descriptors

This commit adds a workaround for devices that have buggy endpoint
descriptors but are otherwise functional. These devices will have
endpoints that fail the GetPipeProperties call. Since we only care
about the endpoint address we can read it from the descriptor
itself.

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agorelease: on to 1.0.21-rc3
Nathan Hjelm [Mon, 22 Aug 2016 02:32:39 +0000 (20:32 -0600)]
release: on to 1.0.21-rc3

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agocore: Document why we drop the flying_transfers_lock before submit_transfer
Hans de Goede [Thu, 11 Aug 2016 09:12:56 +0000 (11:12 +0200)]
core: Document why we drop the flying_transfers_lock before submit_transfer

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agolinux_usbfs: Deal with receiving POLLERR before all transfers have completed
Hans de Goede [Tue, 7 Jun 2016 10:12:38 +0000 (12:12 +0200)]
linux_usbfs: Deal with receiving POLLERR before all transfers have completed

The linux kernel will set its internal device state to USB_STATE_NOTATTACHED
as soon as it detects the disconnect, and then start a worker thread to
deal with the actual disconnection, kill outstanding urbs, etc.

The usbfs poll implementation will return POLL_ERR as soon as
ps->dev->state == USB_STATE_NOTATTACHED. The kernel will not wakeup
the poll until it is done with processing the disconnection. But if
we happen to call poll() between the state change and the disconnection
being fully processed, we may not be able to reap all outstanding transfers,
even on kernels with the USBFS_CAP_REAP_AFTER_DISCONNECT capability.

This commit deals with this by trying to reap as many transfers as possible
on disconnect on USBFS_CAP_REAP_AFTER_DISCONNECT capable kernels and then
calling usbi_handle_disconnect(handle) to deal with any remaining ones.
On USBFS_CAP_REAP_AFTER_DISCONNECT capable kernels this will be a no-op
unless we hit the race.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agocore: Move calculate_timeout call to add_to_flying_transfers
Hans de Goede [Wed, 8 Jun 2016 08:34:05 +0000 (10:34 +0200)]
core: Move calculate_timeout call to add_to_flying_transfers

This cleans-up libusb_submit_transfer a bit by avoiding an error
exit path with unlock calls.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agocore: Fix unlocked access to timeout_flags
Hans de Goede [Mon, 6 Jun 2016 15:43:23 +0000 (17:43 +0200)]
core: Fix unlocked access to timeout_flags

There is a race between handle_timeout() and the completion functions.
When one thread is in handle_timeout() and another thread wakes
up from a poll(), there exists a window where the transfer has been
cancelled, but USBI_TRANSFER_TIMED_OUT is not yet set in timeout_flags.
Therefore, usbi_handle_transfer_completion() is sometimes called
with LIBUSB_TRANSFER_CANCELLED instead of the expected
LIBUSB_TRANSFER_TIMED_OUT.

timeout_flags is protected by the flying_transfers_lock, this commit
makes usbi_handle_transfer_cancellation() take that lock before
checking for USBI_TRANSFER_TIMED_OUT in timeout_flags, fixing this.

Reported-by: Joost Muller <joostmuller@gmail.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agocore: Fix do_close locking
Hans de Goede [Mon, 6 Jun 2016 15:42:12 +0000 (17:42 +0200)]
core: Fix do_close locking

Put the lock / unlock calls around the part of the code which actually
checks the flags which the lock protect.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agocore: Test for LIBUSB_SUCCESS instead of 0 in handle_timeout()
Hans de Goede [Mon, 6 Jun 2016 14:54:42 +0000 (16:54 +0200)]
core: Test for LIBUSB_SUCCESS instead of 0 in handle_timeout()

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agocore: Do not arm timer-fd for transfers where the os handles timeout
Hans de Goede [Mon, 6 Jun 2016 14:53:53 +0000 (16:53 +0200)]
core: Do not arm timer-fd for transfers where the os handles timeout

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agocore: Refactor code related to transfer flags and timeout handling
Chris Dickens [Mon, 26 Oct 2015 13:18:33 +0000 (14:18 +0100)]
core: Refactor code related to transfer flags and timeout handling

Commit a886bb02 sped up the library a bit by removing the serialization
of transfer submission with respect to the flying_transfers list, but
it introduced two separate issues.

1) A deadlock scenario is possible given the following sequence:

   - Thread A submits transfer with very short timeout (say 1ms)
     -> takes transfer->lock
     -> adds transfer to flying_transfers list and arms timerfd
     -> actually calls backend to submit transfer, but it fails
   <context switch>
   - Thread B is doing event handling and sees the timerfd trigger
     -> takes ctx->flying_transfers_lock
     -> finds the transfer above on the list
     -> calls libusb_cancel_transfer() for this transfer
       --> takes transfer->lock
   <context switch>
   - Thread A sees the transfer failed to submit
     -> removes transfer from flying_transfers list
       --> takes ctx->flying_transfers_lock (still holding transfer->lock)
   ** DEADLOCK **

2) The transfer state flags (e.g. submitting, in-flight) were protected
    by transfer->flags_lock, but the timeout-related flags were OR'ed in
    during timeout handling operations outside of the lock. This leads to
    the possibility that transfer state might get overwritten.

This change corrects these issues and simplifies the transfer submission
code a bit by separating the state and timeout flags into their own flag
variables. The state flags are protected by the transfer lock. The timeout
flags are protected by the flying_transfers_lock.

The transfer submission code sheds some weight because it no longer needs
to worry about the timing of events that modify the transfer state flags.
These flags are always viewed and modified under the protection of the
transfer lock. Since libusb_submit_transfer() holds the transfer lock for
the entire duration of the operation, the other code paths that would
possibly touch the transfer (e.g. usbi_handle_disconnect() and
usbi_handle_transfer_completion()) have to wait for transfer submission
to fully complete. This eliminates any possible race conditions.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
[hdegoede@redhat.com: Reworked libusb_submit_transfer changes so that in
 case both flying_transfer_lock and itransfer->lock are taken
 flying_transfers_lock is always taken first]
[hdegoede@redhat.com: Removed some unrelated changes (will be submitted
 as separate patches)]
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agowinnt: Do not differ between cancel / timeout ourselves
Hans de Goede [Mon, 6 Jun 2016 14:43:18 +0000 (16:43 +0200)]
winnt: Do not differ between cancel / timeout ourselves

(itransfer->flags & USBI_TRANSFER_TIMED_OUT) is already checked by
usbi_handle_transfer_cancellation(), make windows_transfer_callback()
call usbi_handle_transfer_cancellation() when
status == LIBUSB_TRANSFER_CANCELLED like all other os backends do, and
leave USBI_TRANSFER_TIMED_OUT handling up to the core, so that future
changes to timeout handling do no break winnt.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
Note: untested

7 years agowince: Do not differ between cancel / timeout ourselves
Hans de Goede [Mon, 6 Jun 2016 14:40:54 +0000 (16:40 +0200)]
wince: Do not differ between cancel / timeout ourselves

(itransfer->flags & USBI_TRANSFER_TIMED_OUT) is already checked by
usbi_handle_transfer_cancellation(), which wince_transfer_callback()
will call when status == LIBUSB_TRANSFER_CANCELLED. Leave this up
to the core, so that future changes to timeout handling do no break
wince.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
Note: untested

7 years agocore: Change event handling lock to traditional (non-recursive) type
Chris Dickens [Mon, 26 Oct 2015 13:11:53 +0000 (14:11 +0100)]
core: Change event handling lock to traditional (non-recursive) type

The event handling lock was previously required to be of the recursive
type because the libusb_close() path requires the lock and may be
called by a thread that is handling events (e.g. from within a
transfer or hotplug callback). With commit 960a6e75, it is possible to
determine whether the current function is being called from an event
handling context, thus the recursive lock type is no longer necessary.

References:
* http://libusb.org/ticket/82
74282582cc879f091ad1d847411337bc3fa78a2b
c775c2f43037cd235b65410583179195e25f9c4a

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
[hdegoede@redhat.com: rebase on top of current master]
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agoRevert "io: Fix race condition in handle_timeout()"
Hans de Goede [Wed, 10 Aug 2016 10:17:00 +0000 (12:17 +0200)]
Revert "io: Fix race condition in handle_timeout()"

This reverts commit bd8d5b5019b72b2dc2d074d96c9992e2f6e7e0b7.

Chris Dickens and me have been working on a patch-set refactoring
the transfer flag handling which fixes this differently. Revert
this commit so that the refactoring changes can be merged cleanly.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7 years agoon to 1.0.21-rc2
Nathan Hjelm [Mon, 25 Jul 2016 02:04:34 +0000 (20:04 -0600)]
on to 1.0.21-rc2

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agoUpdated TODO link
Anil Nair [Sun, 24 Jul 2016 15:42:26 +0000 (21:12 +0530)]
Updated TODO link

Closes #198

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agochangelog: add entry for new solaris backend
Nathan Hjelm [Mon, 25 Jul 2016 02:01:15 +0000 (20:01 -0600)]
changelog: add entry for new solaris backend

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agoSolaris backend
Lei Chen [Fri, 19 Feb 2016 03:58:39 +0000 (11:58 +0800)]
Solaris backend

Closes #177

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agochangelog: add missing entry
Nathan Hjelm [Fri, 22 Jul 2016 12:38:09 +0000 (06:38 -0600)]
changelog: add missing entry

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
7 years agowindows: use appropriate version for VS2015 solution files
Pete Batard [Fri, 22 Jul 2016 11:57:40 +0000 (12:57 +0100)]
windows: use appropriate version for VS2015 solution files

* Closes #193

7 years agolibusb 1.0.21-rc1
Nathan Hjelm [Fri, 22 Jul 2016 05:12:56 +0000 (23:12 -0600)]
libusb 1.0.21-rc1

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