platform/upstream/libusb.git
10 years agoAndroid: Add examples and stress tests
Toby Gray [Thu, 8 Aug 2013 17:22:21 +0000 (18:22 +0100)]
Android: Add examples and stress tests

10 years agoAndroid: Add formal Android support
Toby Gray [Tue, 9 Jul 2013 15:43:53 +0000 (16:43 +0100)]
Android: Add formal Android support

* Also fix an issue with LIBUSB_LOG_LEVEL_NONE

10 years agoCore: Add compile time option to use native OS logging facility
Toby Gray [Tue, 9 Jul 2013 15:05:39 +0000 (16:05 +0100)]
Core: Add compile time option to use native OS logging facility

* This change makes it easier to debug issues in UI applications
  which don't necessarily have a console connected to stderr.
* Outputting to the debugger shouldn't occur in normal situations so
  this change has to be explicitly enabled by a build-time config flag.
* Uses OutputDebugString() on Windows platforms or the syslog facility
  on other OSes, if available.
* Also align the report of configure defaults to autotool's.

10 years agolinux: ensure the netlink socket is correctly opened with older Linux kernels
Nathan Hjelm [Thu, 8 Aug 2013 03:01:43 +0000 (21:01 -0600)]
linux: ensure the netlink socket is correctly opened with older Linux kernels

It is possible for the extra socket flags (available from 2.6.27 on) to be
defined but not available. Check for this case and set the flags correctly
on the netlink socket.

10 years agoCore: Fix potential segfault caused by using freed memory
Chris Dickens [Tue, 6 Aug 2013 20:16:16 +0000 (13:16 -0700)]
Core: Fix potential segfault caused by using freed memory

When a transfer is submitted, the device is referenced in libusb_submit_transfer()
and unreferenced in usbi_handle_transfer_completion(). This transfer could potentially
be freed by any user callback, or is freed by libusb if LIBUSB_TRANSFER_FREE_TRANSFER
is set in the flags. The call to unreference the device uses this potentially freed
memory. Reading the device handle beforehand will prevent this disaster.

10 years agoWindows: Fix race between windows_clock_gettime_threaded() and windows_clock_gettime()
Simon Haggett [Fri, 12 Jul 2013 09:06:00 +0000 (10:06 +0100)]
Windows: Fix race between windows_clock_gettime_threaded() and windows_clock_gettime()

* When the timer thread is created in windows_init(), it performs an
  initialisation phase in which it uses QueryPerformanceFrequency() to determine
  if there is a high resolution timer available, and sets hires_frequency and
  hires_ticks_to_ps appropriately. However, since windows_init() does not wait for
  this initialisation phase to complete, windows_clock_gettime() can be called
  before hires_frequency and hires_ticks_to_ps have been updated. This can result
  in windows_clock_gettime() temporarily returning real-time clock values even
  though the platform supports a monotonic clock.
* See http://sourceforge.net/mailarchive/forum.php?thread_name=1373620013-3574-1-git-send-email-simon.haggett%40realvnc.com&forum_name=libusbx-devel

10 years agoWinCE: Fix race between wince_clock_gettime_threaded() and wince_clock_gettime()
Simon Haggett [Fri, 12 Jul 2013 09:06:00 +0000 (10:06 +0100)]
WinCE: Fix race between wince_clock_gettime_threaded() and wince_clock_gettime()

* When the timer thread is created in wince_init(), it performs an initialisation
  phase in which it uses QueryPerformanceFrequency() to determine if there is a
  high resolution timer available, and sets hires_frequency and hires_ticks_to_ps
  appropriately. However, since wince_init() does not wait for this initialisation
  phase to complete, wince_clock_gettime() can be called before hires_frequency
  and hires_ticks_to_ps have been updated. This can result in
  wince_clock_gettime() temporarily returning real-time clock values even though
  the platform supports a monotonic clock.
* See http://sourceforge.net/mailarchive/forum.php?thread_name=1373619997-3535-1-git-send-email-simon.haggett%40realvnc.com&forum_name=libusbx-devel

10 years agoWindows: fd_to_winfd() shouldn't treat fd 0 as invalid
Simon Haggett [Thu, 11 Jul 2013 16:27:00 +0000 (17:27 +0100)]
Windows: fd_to_winfd() shouldn't treat fd 0 as invalid

* fd_to_winfd() currently returns INVALID_WINFD if fd is 0, but usbi_create_fd()
  can legally assign an fd number of 0 if poll_fd[0] is not already occupied.
* Transfers which are assigned an fd number of 0 for their event handle are then
  unable to have their event handle retrieved later on.

See http://libusbx.1081486.n5.nabble.com/Libusbx-devel-PATCH-1-1-Windows-fd-to-winfd-shouldn-t-treat-fd-0-as-invalid-tt1535.html

10 years agoREADME: Add OS X specific notes
Sean McBride [Fri, 12 Oct 2012 19:04:03 +0000 (15:04 -0400)]
README: Add OS X specific notes

Xcode 4.3 do not provide autotools anymore.
libusbx now provides a Xcode project to ease build Mac OS X.

10 years agoCore: correctly check usbi_read() returned value
Chris Dickens [Fri, 2 Aug 2013 09:33:41 +0000 (11:33 +0200)]
Core: correctly check usbi_read() returned value

For messages received on the hotplug pipe, the message was read via
usbi_read() (ssize_t) and compared against the size of the message
struct (size_t). usbi_read() returns -1 on an error condition, so some
systems can cast the ssize_t to size_t for the comparison, making it
equal to SIZE_MAX and causing the error check condition to incorrectly
evaluate to false.

10 years agoCore: defensive programming
Sean McBride [Fri, 12 Oct 2012 21:31:45 +0000 (17:31 -0400)]
Core: defensive programming

Defensively set return-by-reference value to -1 in error condition

NB: The comments do not match the implementation.

Comments: "[return] the index of the configuration matching a specific
bConfigurationValue in the idx output parameter, or -1 if the config was
not found"

There is a code path where idx is never touched. Perhaps clients of the
function are careful to only read idx if the return value is success,
but also setting idx to -1 is much safer.

10 years agodarwin: fix clang warnings about explicit conversion
hjelmn [Tue, 30 Jul 2013 19:28:27 +0000 (13:28 -0600)]
darwin: fix clang warnings about explicit conversion

There shouldn't be any problems with any supported version of OSX in
converting a UInt64 to an unsigned long. They should be the same size
but even if they are not the session should still be unique.

10 years agofixed link errors by adding missing files to Xcode project
Sean McBride [Fri, 26 Jul 2013 18:11:53 +0000 (14:11 -0400)]
fixed link errors by adding missing files to Xcode project

10 years agomade some globals static to fix warnings
Sean McBride [Fri, 26 Jul 2013 18:13:45 +0000 (14:13 -0400)]
made some globals static to fix warnings

10 years agofixed some clang -Wdocumentation warnings from bad doxygen markup
Sean McBride [Fri, 26 Jul 2013 18:13:40 +0000 (14:13 -0400)]
fixed some clang -Wdocumentation warnings from bad doxygen markup

10 years agofixed compiler warning about possible uninitialized use by zero initializing
Sean McBride [Fri, 26 Jul 2013 18:13:03 +0000 (14:13 -0400)]
fixed compiler warning about possible uninitialized use by zero initializing

10 years agomake targets for examples depend on library target. Generate library named libusb...
Sean McBride [Wed, 31 Oct 2012 17:18:32 +0000 (13:18 -0400)]
make targets for examples depend on library target.  Generate library named libusb-1.0.0.dylib not libusbx.dylib.  Added several xcconfig files to make source control and documentation easier.  Reorganised a few files' positions within the project.  Disable strict aliasing since libusbx breaks strict aliasing rules anyway. Set project format as 3.1-compatible.

Conflicts:
Xcode/libusbx.xcodeproj/project.pbxproj

10 years agoXcode: build for 32 and 64 bits
Ludovic Rousseau [Wed, 31 Oct 2012 08:20:33 +0000 (09:20 +0100)]
Xcode: build for 32 and 64 bits

10 years agoXcode: remove implicit/default configuration
Ludovic Rousseau [Fri, 26 Oct 2012 07:44:16 +0000 (09:44 +0200)]
Xcode: remove implicit/default configuration

10 years agoAdd minimal config.h for Xcode
Ludovic Rousseau [Fri, 26 Oct 2012 07:32:54 +0000 (09:32 +0200)]
Add minimal config.h for Xcode

Xcode do not use ./configure so the config.h file needs to be generated
by hand.
This config.h is the minimal file for libusbx built using Xcode.

10 years agoAdapt Xcode project to the new paths
Ludovic Rousseau [Fri, 26 Oct 2012 07:25:22 +0000 (09:25 +0200)]
Adapt Xcode project to the new paths

10 years agoMove Xcode project in Xcode/ directory
Ludovic Rousseau [Fri, 26 Oct 2012 07:21:35 +0000 (09:21 +0200)]
Move Xcode project in Xcode/ directory

This is to use Xcode special config.h file in a independent directory.
The same is already done for MS Visual C with msvc/ directory.

10 years agoXcode project
Ludovic Rousseau [Fri, 12 Oct 2012 21:30:26 +0000 (23:30 +0200)]
Xcode project

10 years agolinux_netlink: Remove use of pthread_cancel
Hans de Goede [Fri, 19 Jul 2013 08:52:18 +0000 (10:52 +0200)]
linux_netlink: Remove use of pthread_cancel

Using pthread_cancel() presents the opportunity for deadlock, so
use a control pipe to cause the event thread to gracefully exit.

Inspired on the identical patch for linux_udev from Chris Dickens.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
10 years agolinux_netlink: close netlink socket on init error
Hans de Goede [Tue, 30 Jul 2013 14:56:54 +0000 (16:56 +0200)]
linux_netlink: close netlink socket on init error

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
10 years agohotplug: Remove use of pthread_cancel from linux_udev
Chris Dickens [Sat, 20 Jul 2013 20:01:41 +0000 (13:01 -0700)]
hotplug: Remove use of pthread_cancel from linux_udev

Using pthread_cancel() presents the opportunity for deadlock, so
use a control pipe to cause the event thread to gracefully exit.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
10 years agolinux: Use a separate lock to serialize start/stop vs hotplug events
Hans de Goede [Tue, 30 Jul 2013 13:57:16 +0000 (15:57 +0200)]
linux: Use a separate lock to serialize start/stop vs hotplug events

Using one lock for this is a bad idea, as we should not be holding any
locks used by the hotplug thread when trying to stop otherwise the stop
function may wait indefinetely in pthread_join, while the event-thread
is waiting for the lock the caller of the stop function holds.

Using 2 separate locks for this should fix this deadlock, which has been
reported here: https://bugzilla.redhat.com/show_bug.cgi?id=985484

Many thanks to Chris Dickens for figuring out the cause of this deadlock!

CC: Chris Dickens <christopher.a.dickens@gmail.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
10 years agowork around Linux systems that don't provide SOCK_CLOEXEC or SOCK_NONBLOCK
hjelmn [Mon, 29 Jul 2013 16:22:53 +0000 (10:22 -0600)]
work around Linux systems that don't provide SOCK_CLOEXEC or SOCK_NONBLOCK

These options were added in 2.6.27 and are not available on all kernels
that support netlink. Set these options using fcntl when SOCK_CLOEXEC and
SOCK_NONBLOCK are not available.

Closes #124.

10 years agofix warnings in linux_get_device_address
hjelmn [Mon, 29 Jul 2013 16:11:33 +0000 (10:11 -0600)]
fix warnings in linux_get_device_address

Warnings:

os/linux_usbfs.c: In function 'linux_get_device_address':
os/linux_usbfs.c:620: warning: comparison is always false due to limited range of data type
os/linux_usbfs.c:624: warning: comparison is always false due to limited range of data type
os/linux_usbfs.c:628: warning: comparison is always false due to limited range of data type
os/linux_usbfs.c:628: warning: comparison is always false due to limited range of data type

10 years agoinclude stdlib.h for free and realloc in libusbi.h
hjelmn [Mon, 29 Jul 2013 16:10:56 +0000 (10:10 -0600)]
include stdlib.h for free and realloc in libusbi.h

References #124.

10 years agoSilence automake 1.14 warning
Nathan Hjelm [Sat, 27 Jul 2013 19:57:39 +0000 (13:57 -0600)]
Silence automake 1.14 warning

The warning is:

libusb/Makefile.am:57: warning: source file 'os/threads_windows.c' is in a subdirectory,
libusb/Makefile.am:57: but option 'subdir-objects' is disabled
automake: warning: possible forward-incompatibility.
automake: At least a source file is in a subdirectory, but the 'subdir-objects'
automake: automake option hasn't been enabled.  For now, the corresponding output
automake: object file(s) will be placed in the top-level directory.  However,
automake: this behaviour will change in future Automake versions: they will
automake: unconditionally cause object files to be placed in the same subdirectory
automake: of the corresponding sources.
automake: You are advised to start using 'subdir-objects' option throughout your
automake: project, to avoid future incompatibilities.

Fixed by setting the subdir-objects option.

Closes #125.

10 years agoAdd checks for headers needed by linux/netlink.h
Nathan Hjelm [Sat, 27 Jul 2013 18:35:51 +0000 (12:35 -0600)]
Add checks for headers needed by linux/netlink.h

These headers are required by netlink.h. I am not sure how this
worked at all. It certainly doesn't work with older versions of Linux
2.6.

References #124.

10 years agokeep a reference to the device for each active transfer and let the backend handle...
Nathan Hjelm [Wed, 24 Jul 2013 02:56:37 +0000 (20:56 -0600)]
keep a reference to the device for each active transfer and let the backend handle cancelling active transfers when a device is disconnected

This commit should fix issues with active transfers when a device is
disconnected. The backend is responsible for making sure the completion
callbacks are made, not the hotplug code. This should fix a number of
issues including duplicate callbacks and segmentation faults.

References #124.

10 years agohotplug: Pass explicit context to callbacks
Florian Albrechtskirchinger [Fri, 12 Jul 2013 13:10:00 +0000 (14:10 +0100)]
hotplug: Pass explicit context to callbacks

* Instead of passing NULL for the context to hotplug callbacks, if the
  context happens to be the default context, always pass the explicit
  context pointer.

11 years agoPrepare for 1.0.16 final release
Hans de Goede [Thu, 11 Jul 2013 09:04:02 +0000 (11:04 +0200)]
Prepare for 1.0.16 final release

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoPrepare for 1.0.16-rc3 release
Hans de Goede [Sat, 6 Jul 2013 07:09:25 +0000 (09:09 +0200)]
Prepare for 1.0.16-rc3 release

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoDocumentation: add an Using an event handling thread section
Hans de Goede [Fri, 5 Jul 2013 14:57:51 +0000 (16:57 +0200)]
Documentation: add an Using an event handling thread section

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoDocumentation: explain the 2 main viable event handling approaches
Hans de Goede [Fri, 5 Jul 2013 14:17:45 +0000 (16:17 +0200)]
Documentation: explain the 2 main viable event handling approaches

Stop pretending that having a separate event handling thread is a bad thing,
specifically delete the "[this] option is not very nice either, but may be
the nicest option available to you if the "proper" approach can not be applied
to your application", which suggests that using poll integration into a main
loop is the one and only "proper" approach.

Instead clearly document there are 2 viable approaches, using a separate
thread, or poll integration into a main loop.

Also stop claiming that libusb does not use threads internally, as with
the new hotplug support this is no longer true.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agohotplug: Wakeup libusb_handle_events on libusb_hotplug_deregister_callback
Hans de Goede [Thu, 4 Jul 2013 14:38:22 +0000 (16:38 +0200)]
hotplug: Wakeup libusb_handle_events on libusb_hotplug_deregister_callback

This serves 2 purposes:

1) We use lazy free-ing of the callback structure, for it to be actually
free-ed usbi_hotplug_match() needs to be called. This ensures this actually
happens (rather then waiting for a hotplug event to arrive, and not freeing
the callback as long as no such event arrives).

2) It causes libusb_handle_events to return to its caller on a call to
libusb_hotplug_deregister_callback, which is very useful for apps which use
a thread to do their apps (hotplug) event handling, otherwise that thread will
hang when the app tries to stop until some event happens.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agohotplug: Give the usbi_hotplug_match* functions a context parameter
Hans de Goede [Thu, 4 Jul 2013 14:30:36 +0000 (16:30 +0200)]
hotplug: Give the usbi_hotplug_match* functions a context parameter

So that the device parameter can be NULL, in combination with a 0
events parameter, to be used to force lazy deregistration.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoDoc: update hotplug, topology and descriptor documentation
Pete Batard [Wed, 3 Jul 2013 21:41:55 +0000 (22:41 +0100)]
Doc: update hotplug, topology and descriptor documentation

* Also fix some typos
* Closes #95

11 years agoPrepare for 1.0.16-rc2 release
Hans de Goede [Mon, 1 Jul 2013 11:00:43 +0000 (13:00 +0200)]
Prepare for 1.0.16-rc2 release

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoCore: Use fputs(3) instead of fprintf(3)
Tim Roberts [Sun, 30 Jun 2013 07:54:20 +0000 (09:54 +0200)]
Core: Use fputs(3) instead of fprintf(3)

This is a micro-optimisation, but it should make the code easier to understand.

11 years agoCore: Fix writing of log lines a single fprintf call
Pete Batard [Sat, 29 Jun 2013 23:32:10 +0000 (00:32 +0100)]
Core: Fix writing of log lines a single fprintf call

fb4c208c33788068bbca67bdd6d11127b5be5a26 broke cygwin compilation due to __GCC__ not being defined
* The actual issue is that __GNUC__ rather than __GCC__ should have been used all along
* Also fixes gettimeofday() usage for MinGW/Cygwin
* Also increase log buffer size to 1K, fix a broken fprintf in core.c and sort whitespaces

11 years agoCore: Make writing of log lines a single fprintf call.
Toby Gray [Thu, 27 Jun 2013 13:49:21 +0000 (14:49 +0100)]
Core: Make writing of log lines a single fprintf call.

Prior to this change a single line of logging performing several fprintf.

This change gets all the data for a line to be logged in a single
fprintf call. This reduced the chances of writes from another thread
getting intermixed with a log line.

It also makes it easier to change where logs are output to in the future.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore: Always warn when there are leaked device refs on exit
Hans de Goede [Thu, 27 Jun 2013 07:33:28 +0000 (09:33 +0200)]
core: Always warn when there are leaked device refs on exit

This check should done with hotplug capable backends too.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoCore: Avoid passing uninitialised data down the hotplug pipe.
Toby Gray [Wed, 26 Jun 2013 15:26:39 +0000 (16:26 +0100)]
Core: Avoid passing uninitialised data down the hotplug pipe.

Due to alignment requirements, libusb_hotplug_message might have
some padding bytes.

This change makes sure that these padding bytes are
initialised. Valgrind no longer complains about passing uninitialised
data to the write system call.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore.c: Initialize auto_detach_kernel_driver to 0 for new handle
Chris Dickens [Tue, 25 Jun 2013 21:45:07 +0000 (14:45 -0700)]
core.c: Initialize auto_detach_kernel_driver to 0 for new handle

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoopenbsd: Fix memleak in obsd_get_device_list()
Hans de Goede [Wed, 26 Jun 2013 15:13:44 +0000 (17:13 +0200)]
openbsd: Fix memleak in obsd_get_device_list()

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore: Only do hotplug cleanup for hotplug capable backends
Hans de Goede [Wed, 26 Jun 2013 14:50:56 +0000 (16:50 +0200)]
core: Only do hotplug cleanup for hotplug capable backends

Xiaofan encountered a crash while testing on openbsd. The main problem here is
libusb_exit doing hotplug cleanup on a non hotplug capable backend.

If the usb_devs list is non empty (*) at libusb_exit time with a non hotplug
capable backend, then the hotplug cleanup code will unref the devices
in the list. Assuming this is the last unref, then libusb_unref_device
will call usbi_disconnect_device, which will try to take the usb_devs_lock,
which is already hold by libusb_exit. Note that if this deadlock was not
there, that we then also would have a double list_del issue.

*) This should never happen, if it does either libusb or the app has a memleak,
or the app still holds a reference to the device. The latter is an application
bug, since device->ctx will be invalid after libusb_exit, so the application
should not hold references after calling libusb_exit.

In this case we have a memleak the libusb openbsd code causing the usb_devs
list to be non empty. This will be fixed in another commit.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoWinCE: ref result of usbi_get_device_by_session_id()
Hans de Goede [Wed, 26 Jun 2013 14:41:22 +0000 (16:41 +0200)]
WinCE: ref result of usbi_get_device_by_session_id()

After the "WinCE: Fix device reference leak which caused crash on
libusb_exit()" commit, the code always unref-s dev after adding it to
discovered_devs. But if dev comes from usbi_get_device_by_session_id()
it is a weak ref and as such should not be unreffed.

Instead of re-adding comlicate ref tracking logic, this patch fixes this with
a libusb_device_ref(dev) of dev comes from usbi_get_device_by_session_id(),
turning the weak ref into a strong ref.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoWinCE: Fix device reference leak which caused crash on libusb_exit().
Toby Gray [Mon, 24 Jun 2013 11:31:26 +0000 (12:31 +0100)]
WinCE: Fix device reference leak which caused crash on libusb_exit().

The Windows CE device allocation code has always had a bug where it would
leak references to devices when they are allocated. This commit removes the
reference leak.

This leak was highlighted by the new hotplug code which now triggers a NULL
pointer dereference if not all devices are unreferenced before libusb_exit
is called.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoMisc: Update AUTHORS file
Pete Batard [Thu, 20 Jun 2013 22:00:34 +0000 (23:00 +0100)]
Misc: Update AUTHORS file

11 years agoPrepare for 1.0.16-rc1 release
Hans de Goede [Thu, 20 Jun 2013 13:18:10 +0000 (15:18 +0200)]
Prepare for 1.0.16-rc1 release

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoautogen.sh: Honor NOCONFIGURE=1
Colin Walters [Thu, 30 May 2013 17:34:57 +0000 (13:34 -0400)]
autogen.sh: Honor NOCONFIGURE=1

See http://people.gnome.org/~walters/docs/build-api.txt

Hdg: I know this may seem unnecessary since we also have bootstrap.sh, but
the Gnome people are doing continues build testing of Gnome + dependencies
and having all autogen.sh scripts support NOCONFIGURE=1 makes live easier for
them. Note that in return we get "free" continues build-testing and patches
and / or a heads up when we break things.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agosync.c: Remove code duplication
Hans de Goede [Thu, 20 Jun 2013 11:08:58 +0000 (13:08 +0200)]
sync.c: Remove code duplication

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agosync.c: Do not free an incompleted transfer
Hans de Goede [Thu, 20 Jun 2013 10:52:25 +0000 (12:52 +0200)]
sync.c: Do not free an incompleted transfer

Before this patch the code in sync.c would free an incomplete transfer
when libusb_handle_events_completed exits with an error twice in a row.

But we should never free an incomplete transfer, otherwise we may end up
referencing free-ed memory later on.

This patch simply logs an error and keeps on trying until the transfer
completes.

Note that if libusb_handle_events_completed keeps throwing an error the
entire time, without ever completing the transfer, this patch effectively
replaces a potential crash / unspecified behavior, with an endless loop
and logging a ton of errors making clear what is going on.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoRevert "Core: Don't wait for completion if cancel_transfer failed"
Hans de Goede [Thu, 20 Jun 2013 10:41:00 +0000 (12:41 +0200)]
Revert "Core: Don't wait for completion if cancel_transfer failed"

This reverts commit 2f5023c41d3176e56bf0abc521b7c5f8b1ac46642f5023c4
was an attempt to fix: https://github.com/libusbx/libusbx/issues/76

As seen in: http://libusbx.1081486.n5.nabble.com/Libusbx-devel-libusb-interrupt-transfer-does-not-return-in-case-of-error-td626.html

[372849.680990] [0000275b] libusbx: error [reap_for_handle] reap failed error -1 errno=14
[372849.681752] [0000275b] libusbx: error [handle_events] backend handle_events failed with error -1
[372850.680466] [0000275b] libusbx: warning [handle_timeout] async cancel failed -5 errno=22

The problem begins with reap_for_handle failing with errno == 14, which is
EFAULT. So the real problem is the application passing in an invalid (or too
short) buffer, and thus is an application bug.

The fix masks this problem, but causes the problem of calling
libusb_transfer_free() on a non finished transfer, so it is no good.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Conflicts:
AUTHORS
libusb/version_nano.h

11 years agoUse Android logging when building on Android
Ilya Konstantinov [Thu, 20 Jun 2013 10:00:46 +0000 (12:00 +0200)]
Use Android logging when building on Android

Closes #101

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoLinux: Fix get_kernel_driver_active() when another app has claimed the interface
Hans de Goede [Thu, 20 Jun 2013 09:51:38 +0000 (11:51 +0200)]
Linux: Fix get_kernel_driver_active() when another app has claimed the interface

When another app has claimed the interface, IOCTL_USBFS_GETDRIVER will
succeed and report a driver of "usbfs" being attached.

Since this is not a regular kernel-driver (and detach_kernel_driver does
not detach it), get_kernel_driver_active() should return 0 in this case.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoCore: fix another compiler warning in libusb_setlocale()
Pete Batard [Wed, 19 Jun 2013 21:44:58 +0000 (22:44 +0100)]
Core: fix another compiler warning in libusb_setlocale()

* VS2012 (64 bit) produces the following in strerror.c(156):
  warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data

11 years agoCore: fix compiler warning in libusb_setlocale()
Ludovic Rousseau [Wed, 19 Jun 2013 11:21:06 +0000 (13:21 +0200)]
Core: fix compiler warning in libusb_setlocale()

Hello,

A small patch for:

strerror.c: In function 'libusb_setlocale':
strerror.c:148: warning: comparison between signed and unsigned
strerror.c:152: warning: comparison between signed and unsigned

--
 Dr. Ludovic Rousseau

From a4144845845cd0a06fb9074ba2d6669ece3a5b1a Mon Sep 17 00:00:00 2001
From: Ludovic Rousseau <ludovic.rousseau+github@gmail.com>
Date: Wed, 19 Jun 2013 13:16:31 +0200
Subject: [PATCH] Core: fix compiler warning in libusb_setlocale()

strerror.c: In function 'libusb_setlocale':
strerror.c:148: warning: comparison between signed and unsigned
strerror.c:152: warning: comparison between signed and unsigned

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoFix several -Wconversion warnings from GCC inside the static inline functions.
Luca Longinotti [Mon, 17 Jun 2013 07:22:23 +0000 (09:22 +0200)]
Fix several -Wconversion warnings from GCC inside the static inline functions.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoexamples: use libusb_set_auto_detach_kernel_driver()
Hans de Goede [Fri, 14 Jun 2013 09:06:47 +0000 (11:06 +0200)]
examples: use libusb_set_auto_detach_kernel_driver()

What better way to show how useful libusb_set_auto_detach_kernel_driver() is,
then to use it in our examples?

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux_usbfs: Work around a driver binding race in reset handling
Hans de Goede [Thu, 13 Jun 2013 20:37:29 +0000 (22:37 +0200)]
linux_usbfs: Work around a driver binding race in reset handling

I've been seeing these intermittent failures to reclaim an interface after
a device reset. After much debugging and inserting sleeps in strategic
places to make the race window larger I've found the following race:
1) A user is running some software using libusbx which will automatically
   detect, and "bind" to, any newly plugged in USB-devices. For example
   a virtual machine viewer with automatic USB-redirection
2) The user plugs in a new usb-storage device
3) The usb-storage driver is not yet loaded, udev spawns
   "modprobe usb-storage", this blocks on disk-io
4) The libusbx app opens the device, claims all interfaces, does a device-reset
5) While the IOCTL_USBFS_RESET is running the modprobe completes
6) The driver registration blocks on an USB lock held by the reset code path
7) When the reset finishes the driver registration completes and the driver
   binds itself to the device, before IOCTL_USBFS_RESET returns to userspace
8) libusbx tries to re-claim all interfaces it had claimed before the reset
9) libusbx fails as usb-storage is now bound to it

This patch works around this issue by simply unbinding the driver for all
interfaces which were claimed before the reset. Normally this is a no-op as
no driver (other then usbfs) can be bound for claimed interfaces before the
reset.

This patch also improves the error logging, and makes libusb_device_reset
properly return an error when re-claiming fails.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux_usbfs: Add support for new disconnect-and-claim ioctl
Hans de Goede [Thu, 13 Jun 2013 19:49:01 +0000 (21:49 +0200)]
linux_usbfs: Add support for new disconnect-and-claim ioctl

Currently the linux_usbfs detach_kernel_driver_and_claim() helper function
makes 3 system calls:
1) IOCTL_USBFS_GETDRIVER, to check the driver is not usbfs
2) IOCTL_USBFS_DISCONNECT
3) IOCTL_USBFS_CLAIMINTF

Between each of these calls the state of the interface can change, and
things might not work as expected when it does, iow this is inherently racy.

To fix this a new IOCTL_USBFS_DISCONNECT_CLAIM ioctl has been added to the
kernel a while back, which does all 3 in one. This patch adds support for
this ioctl, with a fall back to the old method for kernels lacking this new
ioctl.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolibusb: Add auto-detach-kernel-driver functionality
Hans de Goede [Thu, 13 Jun 2013 19:21:14 +0000 (21:21 +0200)]
libusb: Add auto-detach-kernel-driver functionality

Add auto-detach-kernel-driver functionality, and a
libusb_set_auto_detach_kernel_driver() function.

Note that I went with a libusb_set_auto_detach_kernel_driver() function,
rather then with a libusb_enable_auto_detach_kernel_driver(), so that apps
can also disable it again. This is necessary to handle 2 corner cases:

1) When an app wants to do a libusb_set_configuration after claiming 1 or
more interfaces, it needs to first release the interface(s), and in this
case libusb_release_interface() should *not* (re-)attach the kernel driver

2) Some usb classes use multiple interfaces for one function, ie usb-audio
devices do this. In this case attaching the driver will fail until all
interfaces are released, so the app should first release all interfaces, and
only then (re-)attach the kernel driver.

auto-detach-kernel-driver functionality is still useful for these apps, since
doing libusb_detach_kernel_driver() followed by libusb_claim_interface() in
2 separate calls is inherently racy, but they need to be able to disable the
auto-detach functionality before releasing interfaces to be able to properly
handle the 2 described corner cases.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux_udev: Fix spelling of guarantee in comment
Hans de Goede [Wed, 19 Jun 2013 08:46:19 +0000 (10:46 +0200)]
linux_udev: Fix spelling of guarantee in comment

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoPOSIX: Set usbi_pipe to non-blocking by oring O_NONBLOCK to fd flags.
Chris Dickens [Fri, 14 Jun 2013 17:54:29 +0000 (10:54 -0700)]
POSIX: Set usbi_pipe to non-blocking by oring O_NONBLOCK to fd flags.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux_udev: Don't free the udev_monitor while the event thread may still use it
Hans de Goede [Mon, 17 Jun 2013 06:39:21 +0000 (08:39 +0200)]
linux_udev: Don't free the udev_monitor while the event thread may still use it

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux_udev: Join the hotplug-event-thread when we stop event monitoring
Hans de Goede [Fri, 14 Jun 2013 09:51:38 +0000 (11:51 +0200)]
linux_udev: Join the hotplug-event-thread when we stop event monitoring

To ensure that it is stopped before we continue (and if later libusb
gets re-initialized start another thread).

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolibusb_get_bos_descriptor: Don't log an error on devices without a BOS
Hans de Goede [Fri, 14 Jun 2013 09:10:35 +0000 (11:10 +0200)]
libusb_get_bos_descriptor: Don't log an error on devices without a BOS

An error of LIBUSB_ERROR_PIPE simply means the device has no BOS, so don't
log an error to the console for this.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agohotplug: ensure udev monitor fd is non-blocking
Chris Dickens [Thu, 13 Jun 2013 17:59:11 +0000 (10:59 -0700)]
hotplug: ensure udev monitor fd is non-blocking

Some older versions of udev do not automatically set the udev
monitor fd to non-blocking mode. This patch ensures that this is
always set.

HdG: Get flags then or in O_NONBLOCK and set them, rather then setting flags
to only O_NONBLOCK.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux_udev: Log error code on pthread_create failure
Hans de Goede [Fri, 14 Jun 2013 07:37:05 +0000 (09:37 +0200)]
linux_udev: Log error code on pthread_create failure

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoExamples: add a missing \n at end of error messages
Ludovic Rousseau [Mon, 10 Jun 2013 13:27:23 +0000 (15:27 +0200)]
Examples: add a missing \n at end of error messages

11 years agoCore: Add a libusb_strerror() function
Hans de Goede [Fri, 7 Jun 2013 18:07:16 +0000 (19:07 +0100)]
Core: Add a libusb_strerror() function

This patch adds the much requested libusb_strerror() function, taking into
account all issues people raised wrt previous attempts.

Criteria / Decisions underlying this implementation:
- Must support translated messages
- Must not use gettext as that does not work well in combination with Windows
 (when building with Visual C, or for Windows CE)
- API compatible with FreeBSD and various patched libusb-s floating around
- KISS:
 - Do not add any (other) library dependencies
 - Do not try to deal with message encodings (iconv), simply always return UTF-8
   making encoding the problem of the application using libusb_strerror.
 - Defaults to English, so apps which don't want translated messages,
   don't need to do anything special
 - Defaults to English (with pure ASCII messages), so apps which don't
   call libusb_setlocale() don't need to worry about encoding

11 years agoMerge pull request #114 from hjelmn/darwin_device_cache
Hans de Goede [Thu, 6 Jun 2013 17:07:26 +0000 (10:07 -0700)]
Merge pull request #114 from hjelmn/darwin_device_cache

Darwin device cache

11 years agodarwin: return correct error code from libusb_get_config_descriptor when the configur...
Nathan Hjelm [Mon, 20 May 2013 14:20:33 +0000 (08:20 -0600)]
darwin: return correct error code from libusb_get_config_descriptor when the configuration is not found

11 years agodarwin: increment the reference counter to the parent device
Nathan Hjelm [Fri, 17 May 2013 04:04:18 +0000 (22:04 -0600)]
darwin: increment the reference counter to the parent device

11 years agodarwin: avoid enumerating devices more than once
Nathan Hjelm [Fri, 17 May 2013 02:48:56 +0000 (20:48 -0600)]
darwin: avoid enumerating devices more than once

This patch attempts to avoid enumerating devices multiple times by
keeping a cache of device information (IOUSBDeviceInterface pointers,
device descriptors, etc) between calls to libusb_init(). This should
avoid two potential problems:

 1) Devices that respond slowly to DeviceRequest. These devices can
    take multiple seconds to respond and they usually respond with
    a failure (on older versions of OSX at least). By keeping track
    of these devices the penalty for enumerating them will be paid
    only once.
 2) Repeated enumeration of the same device may cause
    IOCreatePlugInInterfaceForService() to fail. The root cause of
    this problem is not well understood but might be a leak in
    IOUSBFamily.

To clean up the cached device information an atexit() function is
used.

11 years agoWindows: Fix broken WDK compilation
Pete Batard [Wed, 29 May 2013 10:42:05 +0000 (12:42 +0200)]
Windows: Fix broken WDK compilation

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoexamples: Add sam3u_benchmark isochronous example by Harald Welte
Peter Stuge [Tue, 8 May 2012 17:50:17 +0000 (19:50 +0200)]
examples: Add sam3u_benchmark isochronous example by Harald Welte

Copied with the author's permission under LGPL-2.1 from
usb-benchmark-project/host/benchmark.c in
git://git.gnumonks.org/sam3u-tests.git commit
74959f7ee88f1597286cd435f312a8ff52c56b7e

http://git.gnumonks.org/cgi-bin/gitweb.cgi?p=sam3u-tests.git;a=blob;f=usb-benchmark-project/host/benchmark.c;h=74959f7ee88f1597286cd435f312a8ff52c56b7e

An Atmel SAM3U test firmware is also available in the above repository.

Conflicts:
.gitignore

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoAdd BOS descriptor support
Hans de Goede [Mon, 27 May 2013 14:31:48 +0000 (16:31 +0200)]
Add BOS descriptor support

Based on earlier work done on this by Maya Erez <merez@codeaurora.org>,
Nathan Hjelm <hjelmn@me.com> and Pete Batard <pete@akeo.ie>.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoAdd superspeed endpoint companion descriptor support
Hans de Goede [Mon, 27 May 2013 09:12:28 +0000 (11:12 +0200)]
Add superspeed endpoint companion descriptor support

Based on earlier work done on this by Maya Erez <merez@codeaurora.org>,
Nathan Hjelm <hjelmn@me.com> and Pete Batard <pete@akeo.ie>.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agohotplug: Add a hotplug_poll backend function
Hans de Goede [Fri, 24 May 2013 14:15:51 +0000 (16:15 +0200)]
hotplug: Add a hotplug_poll backend function

Apps which were written before hotplug support, may listen for hotplug events
on their own and call libusb_get_device_list on device addition.

In this case libusb_get_device_list will likely return a list without the new
device in there, as the hotplug event thread will still be busy enumerating
the device, which may take a while, or may not even have seen the event yet.

To avoid this add a new hotplug_poll backend function and make
libusb_get_device_list call a this before copying ctx->usb_devs to the user.

In this function the backend should ensure any pending hotplug events are
fully processed before returning.

This patch implements hotplug_poll for linux, it should probably be also
implemented for darwin.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: rework hotplug lock handling
Hans de Goede [Fri, 24 May 2013 12:40:27 +0000 (14:40 +0200)]
linux: rework hotplug lock handling

I could not find if libudev is completely threadsafe anywhere, so rework
the lock handling to serialize all libudev accesses. This is a preparation
patch for adding hotplug_poll support, see the next patch in this series.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Add a linux_netlink_read_message helper function
Hans de Goede [Fri, 24 May 2013 13:52:45 +0000 (15:52 +0200)]
linux: Add a linux_netlink_read_message helper function

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodescriptor.c: AlternateSettings are related by InterfaceNumber
Hans de Goede [Fri, 24 May 2013 12:03:54 +0000 (14:03 +0200)]
descriptor.c: AlternateSettings are related by InterfaceNumber

A block of AlternateSettings for a certain InterfaceNumber does not necessarily
start with AlternateSetting 0, so check Interface Descriptors belong to
each other by using InterfaceNumber, as specified in the spec.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoWinCE: Add hotplug.c to projects
Toby Gray [Fri, 24 May 2013 08:42:56 +0000 (10:42 +0200)]
WinCE: Add hotplug.c to projects

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoWindows: Add new symbol for libusb_get_port_numbers to def file
Toby Gray [Fri, 24 May 2013 08:38:48 +0000 (10:38 +0200)]
Windows: Add new symbol for libusb_get_port_numbers to def file

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoHotplug: Make use of HAVE_SYS_TYPES_H define
Toby Gray [Fri, 24 May 2013 08:36:20 +0000 (10:36 +0200)]
Hotplug: Make use of HAVE_SYS_TYPES_H define

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoPOSIX: Move setting of pipes to non-blocking into usbi_pipe
Toby Gray [Fri, 24 May 2013 08:35:24 +0000 (10:35 +0200)]
POSIX: Move setting of pipes to non-blocking into usbi_pipe

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agowince_get_active_config_descriptor() sets host_endian incorrectly
Simon Haggett [Mon, 20 May 2013 11:40:43 +0000 (12:40 +0100)]
wince_get_active_config_descriptor() sets host_endian incorrectly

wince_get_active_config_descriptor() retrieves configuration descriptors
as raw bytes, in bus-endian order. Therefore, host_endian should be set
to 0 (as in wince_get_config_descriptor()).

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoall: ensure host_endian is always initialized
Hans de Goede [Fri, 24 May 2013 08:24:57 +0000 (10:24 +0200)]
all: ensure host_endian is always initialized

The get_device_descriptor backend call done from descriptor.c was passing
in an uninitialized host_endian value. Likewise for the windows backend,
host_endian was not being set from get_[active_]config_descriptor.

Since the 2 problem cases are for different backend calls, we were never using
an uninitialized host_endian, still lets set it everywhere for safety,

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoall: Allow backend to provide a better get_config_descriptor_by_value
Hans de Goede [Thu, 23 May 2013 17:51:07 +0000 (19:51 +0200)]
all: Allow backend to provide a better get_config_descriptor_by_value

Our core get_config_descriptor_by_value is not exactly pretty nor efficient,
allow the backends to provide something better.

Note that the callback signature differs from get_config_descriptor in that
backend owned memory gets returned. This saves a needless malloc + memcpy +
free. If this turns out to be a problem for some backends we can always
change things to work like get_config_descriptor.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodescriptor.c: Add a raw_desc_to_config helper function
Hans de Goede [Thu, 23 May 2013 17:24:48 +0000 (19:24 +0200)]
descriptor.c: Add a raw_desc_to_config helper function

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodescriptor.c: Consequently check bDescriptorType and bLength everywhere
Hans de Goede [Thu, 23 May 2013 15:20:47 +0000 (17:20 +0200)]
descriptor.c: Consequently check bDescriptorType and bLength everywhere

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodescriptor.c: Read a full config-desc-header on first config-desc read
Hans de Goede [Thu, 23 May 2013 13:20:12 +0000 (15:20 +0200)]
descriptor.c: Read a full config-desc-header on first config-desc read

For some reason the first get config call to determine the total-length
was only reading 8 of the 9 bytes of a full config-desc-header, which
is weird. The Linux kernel reads the full 9 on the first call, so lets
do that in libusb too. Note that for backends which cache the config this
does not matter.

Also check that we've actually gotten back a full header, before parsing it.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoall: Make usbi_os_backend.get_[active_]config_descriptor return len
Hans de Goede [Thu, 23 May 2013 12:40:05 +0000 (14:40 +0200)]
all: Make usbi_os_backend.get_[active_]config_descriptor return len

Since commit 5e479f1821d3294fb1cc70c5867c69eca2551de7:
"Core: Avoid short read failures on broken descriptors"

usbi_os_backend.get_[active_]config_descriptor no longer return on error
(under Linux) when returning less bytes then requested. But
libusb_get_[active_]config_descriptor still not only requests wTotalLength
bytes, but also blindly assumes that on success it has gotten wTotalLength
bytes.

This patch fixes this, it changes all usbi_os_backend.get_*config_descriptor
implementations to return the actual length on success and uses this value as
the descriptor size in parse_configuration().

Note that the linux and wince backends were already returning the actual
length and thus are not touched.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Properly deal with invalid config.wTotalLength in sysfs
Hans de Goede [Thu, 23 May 2013 11:25:08 +0000 (13:25 +0200)]
linux: Properly deal with invalid config.wTotalLength in sysfs

In usbfs wTotalLength can be trusted, in the sense that the kernel simple
has holes in the descriptors file when a device returns a smaller config
descriptor then advertised.

In sysfs this is not the case, sysfs descriptors only contain descriptors
actually returned by the device, with no holes. The kernel does validate
the bLength field of all the descriptors and removes any invalid ones.

So with sysfs we cannot rely on wTotalLength, since we can trust bLength,
this patch searches forward for a descriptor with type of LIBUSB_DT_CONFIG
to find the next config on sysfs.

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