platform/upstream/libusb.git
8 years agodarwin: updates to support el capitan
Nathan Hjelm [Sat, 21 Nov 2015 17:30:23 +0000 (10:30 -0700)]
darwin: updates to support el capitan

With the new USB stack Apple changed the underlying device class from
IOUSBDevice to IOUSBHostDevice. Additionally, they removed the PortNum
property from the IO registry entry. This commit add code to detect the
currently running xnu version. If the version is at least 15 (10.11.0)
the new device class is used to find usb devices and the port number
is read from the IOService parent of the USB device instead of the
PortNum property.

Closes #127

May have some effect on either #123 or #96.

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
8 years agodarwin: ignore root hub simulation devices
Nathan Hjelm [Fri, 9 Oct 2015 19:25:24 +0000 (13:25 -0600)]
darwin: ignore root hub simulation devices

This commit fixes #108. In the 10.11.0 release it can take up to 1
second to query the configuration of one of Apple's root hub
simulation devices. Since the devices are not usable anyway go ahead
and mark them as unconfigured.

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
8 years agocore: Prevent attempts to recursively hande events
Chris Dickens [Thu, 10 Sep 2015 09:39:20 +0000 (02:39 -0700)]
core: Prevent attempts to recursively hande events

Prior to this commit, it was possible to call certain functions from
within a hotplug or transfer completion callback that would in turn
attempt to handle events (e.g. any of the sync transfer APIs). This
is dangerous because certain events may cause the nested calls to
free memory that is currently in use by the previous calls.

This implementation uses thread-local storage to store a key within
the context that is set to a non-NULL value whenever event handling
is occurring. This allows us to detect when dangerous calls are made
from within event handling context. Such calls will return an error
code of LIBUSB_ERROR_BUSY.

Note that this implementation was chosen because of its portability.
It is supported on all platforms that libusb supports.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoWindows: Fix potential memory leak
Chris Dickens [Sun, 20 Sep 2015 22:24:05 +0000 (15:24 -0700)]
Windows: Fix potential memory leak

The windows_assign_endpoints() function does not free the config
descriptor before returning in two cases.

Issue reported by dmitrykos <dmitrykos@neutroncode.com>

Closes #98

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoMiscellaneous fixes
Tobias Klauser [Tue, 14 Apr 2015 15:43:05 +0000 (17:43 +0200)]
Miscellaneous fixes

* Use UNUSED macro instead of open coding it
* Use calloc to allocate and zero out buffers where appropriate
* Make unnecessarily global variables static
* Use strdup() instead of open-coding it
* Use passed fd in set_fd_cloexec_nb()
* Remove unused parameter from linux_device_disconnected()
* Closes #65

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolinux_usbfs: Add support for kernels that can reap after disconnect
Chris Dickens [Fri, 18 Sep 2015 05:27:22 +0000 (22:27 -0700)]
linux_usbfs: Add support for kernels that can reap after disconnect

For kernels that support the REAP_AFTER_DISCONNECT capability, it is
no longer necessary to use the nasty workaround that is the
usbi_handle_disconnect() function. All transfers can be properly
reaped as one would expect and will have an appropriate URB status.

Note that moving usbi_handle_disconnect() after the hotplug section
is fine because the hotplug disconnect message simply gets added to
the hotplug_msgs list to be processed the next time handle_events()
is called.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolinux_usbfs: Avoid file descriptor leak in error case
Chris Dickens [Fri, 18 Sep 2015 05:27:21 +0000 (22:27 -0700)]
linux_usbfs: Avoid file descriptor leak in error case

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolinux: Filter hotplug events by DEVTYPE of "usb_device"
Chris Dickens [Fri, 18 Sep 2015 05:27:20 +0000 (22:27 -0700)]
linux: Filter hotplug events by DEVTYPE of "usb_device"

Many different hotplug events can occur for the "usb" subsystem
(e.g. attaching/detaching a class driver), but libusb only needs
to care about events where the device type is "usb_device". All
other events cause needless attempts to lookup the device address,
which will fail as the paths for such events do not correspond to
actual USB device nodes.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolinux_usbfs: Only remove the device fd from the pollfd list once
Chris Dickens [Fri, 18 Sep 2015 05:27:19 +0000 (22:27 -0700)]
linux_usbfs: Only remove the device fd from the pollfd list once

If the file descriptor gets removed because POLLERR has been
detected, do not attempt to remove it again when closing the device.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoDocs: Add missing functions to API page
Chris Dickens [Mon, 14 Sep 2015 05:43:55 +0000 (22:43 -0700)]
Docs: Add missing functions to API page

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoWindows: Regenerate libusb-1.0.def file from latest DLL
Chris Dickens [Mon, 14 Sep 2015 05:37:30 +0000 (22:37 -0700)]
Windows: Regenerate libusb-1.0.def file from latest DLL

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolibusb 1.0.20
Chris Dickens [Mon, 14 Sep 2015 00:37:12 +0000 (17:37 -0700)]
libusb 1.0.20

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolibusb 1.0.20-rc3
Chris Dickens [Sat, 5 Sep 2015 17:33:07 +0000 (10:33 -0700)]
libusb 1.0.20-rc3

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agocore: Add RC string to libusb version message
Chris Dickens [Sat, 5 Sep 2015 17:31:45 +0000 (10:31 -0700)]
core: Add RC string to libusb version message

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoWindows: Fix some build warnings/link issues
Chris Dickens [Sat, 5 Sep 2015 17:23:38 +0000 (10:23 -0700)]
Windows: Fix some build warnings/link issues

* Fix MSVC error C4334 (32-bit bit shift)
* Load User32.dll functions at runtime instead of linking directly
  against library (fixes unresovled symbol errors on DDK build)

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoio: Always complete transfers regardless of timerfd errors
Chris Dickens [Fri, 4 Sep 2015 00:23:22 +0000 (17:23 -0700)]
io: Always complete transfers regardless of timerfd errors

This commit addresses two issues.

First, commit a886bb02 introduced an issue where transfer callbacks
would not be called if the transfer that completed was the first
in line and the timerfd needed to be armed for another transfer.

Second, failure to set the timerfd should not prevent the transfer
from completing. The behavior has always been such, but it will
leave clients waiting for their transfer callbacks to be called.
This change will let the transfer callback complete and will return
the error through the event handling path.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolibusb 1.0.20-rc2
Chris Dickens [Thu, 3 Sep 2015 02:54:44 +0000 (19:54 -0700)]
libusb 1.0.20-rc2

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agocore: Record when a transfer timeout has been handled
Chris Dickens [Sat, 22 Aug 2015 07:41:58 +0000 (00:41 -0700)]
core: Record when a transfer timeout has been handled

Commit efd02e73 introduced a bug where transfers that timed out
would be handled repeatedly if the cancellation was not successful.
This behavior blocks any event handling from occurring.

This commit adds a new transfer flag to record whether a timeout has
been handled.

Thanks to Jeffrey Nichols for reporting this.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoExamples: fxload improvements
Chris Dickens [Wed, 5 Aug 2015 06:51:12 +0000 (23:51 -0700)]
Examples: fxload improvements

* Add two-stage load support
* Fix leaks in error-handling paths
* Closes #12

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agocore: Correctly report cancellations due to timeouts
Chris Dickens [Mon, 3 Aug 2015 06:26:19 +0000 (23:26 -0700)]
core: Correctly report cancellations due to timeouts

Prior to this commit, the handle_timeout() function would always
set the USBI_TRANSFER_TIMED_OUT flag on the transfer. However, in
some cases the actual cancellation of the transfer does not succeed,
perhaps because the transfer had just completed. This would cause
occasional false reporting of LIBUSB_TRANSFER_TIMED_OUT when the
transfer did not in fact timeout. This commit adds a check for
successful cancellation before setting the transfer flag.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolinux_usbfs: Set reap action to CANCELLED only when successful
Chris Dickens [Mon, 3 Aug 2015 06:19:57 +0000 (23:19 -0700)]
linux_usbfs: Set reap action to CANCELLED only when successful

Prior to this commit, a call to libusb_cancel_transfer() would set
the reap action of the transfer to CANCELLED regardless of whether
the cancellation was successful or not. This can cause the transfer
status to incorrectly report LIBUSB_TRANSFER_CANCELLED when in some
cases this is not the case. This commit adds a check for a successful
cancellation before setting the reap action as such.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolinux: Assume usbfs path = /dev/bus/usb when using UDEV
Hans de Goede [Mon, 3 Aug 2015 08:06:05 +0000 (10:06 +0200)]
linux: Assume usbfs path = /dev/bus/usb when using UDEV

On some exotic hardware, e.g. HP ProLiant Moonshot Cartridge servers there
are no usb controllers, so no usb devices at all.

In this case currently libusb_init will fail, because find_usbfs_path
fails. Many apps don't handle this gracefully, and even if they do not crash
the result still is not pretty, e.g.:

unable to initialize libusb: -99

Where one simply would expect empty output.

Since on systems using udev the usbfs path should always be /dev/bus/usb
(as that gets created by udev), simply assume /dev/bus/usb when build with
USE_UDEV and the path cannot be found in the traditional way.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
8 years agoMisc: Remove autom4te.cache directory from Haiku during 'make dist'
Chris Dickens [Wed, 29 Jul 2015 17:15:42 +0000 (10:15 -0700)]
Misc: Remove autom4te.cache directory from Haiku during 'make dist'

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolibusb 1.0.20-rc1
Chris Dickens [Wed, 29 Jul 2015 08:06:37 +0000 (01:06 -0700)]
libusb 1.0.20-rc1

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoAPI: Add libusb_free_pollfds() function
Chris Dickens [Sun, 1 Mar 2015 08:45:11 +0000 (00:45 -0800)]
API: Add libusb_free_pollfds() function

This patch adds a new API call to ensure that the same memory
allocator is used to both allocate and free the list of libusb_pollfd
structures. It is an incorrect assumption that the user's free() will
use the same memory allocator that libusb uses internally.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agolinux_usbfs: use calloc for multiplication
Marcus Meissner [Sat, 13 Jun 2015 21:50:35 +0000 (23:50 +0200)]
linux_usbfs: use calloc for multiplication

calloc can do n, elemsize multiplication itself

* Closes #74

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoUse locally defined MIN macro instead of min which is (possibly) defined in windows.h
Tim Hutt [Wed, 3 Jun 2015 14:01:06 +0000 (15:01 +0100)]
Use locally defined MIN macro instead of min which is (possibly) defined in windows.h

Some users have to define NOMINMAX which means that windows.h doesn't define min() or max(). Instead of relying on it it is better to use a locally defined function / macro. Fortunately libusbi.h already defines MIN and MAX so we can just use those.

* Closes #72
* Closes #73

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoWindows: Close HID handles when closing composite devices
Chris Dickens [Wed, 29 Jul 2015 05:51:09 +0000 (22:51 -0700)]
Windows: Close HID handles when closing composite devices

* Closes #81

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoWindows: Remove redundant check in windows_claim_interface()
Chris Dickens [Wed, 29 Jul 2015 05:38:10 +0000 (22:38 -0700)]
Windows: Remove redundant check in windows_claim_interface()

* Checking of this parameter is already done by the core
* Closes #77

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
8 years agoWindows: Destroy autoclaim_lock during cleanup
Chris Dickens [Wed, 29 Jul 2015 05:29:11 +0000 (22:29 -0700)]
Windows: Destroy autoclaim_lock during cleanup

* Fixes a resource leak during init error handling or final exit
* Closes #76

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agolinux_usbfs: Fix memory allocation failure.
Gaurav [Wed, 13 May 2015 07:49:14 +0000 (13:19 +0530)]
linux_usbfs: Fix memory allocation failure.

strdup returns NULL in case memory allocation failure occurs.
If Null check is not there, it will crash while dereferencing in "strrchr".

Closes #66

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoMisc: Fix doxygen comment for MaxPower field with respect to super-speed
Tobias Klauser [Wed, 13 May 2015 12:19:12 +0000 (14:19 +0200)]
Misc: Fix doxygen comment for MaxPower field with respect to super-speed

The MaxPower field of a configuration descriptor is in units of 8 mA
when the device is operating in super-speed mode according to the USB
3.0 specification, section 9.6.3, table 9-12:

  Maximum power consumption of the device from the
  bus in this specific configuration when the device is
  fully operational. Expressed in 2-mA units when the
  device is operating in high-speed mode and in 8-mA
  units when operating at Gen X speed.
  (i.e., 50 = 100 mA when operating at high-speed and
  50 = 400 mA when operating at Gen X speed).

Adjust the doxygen comment for the MaxPower member of struct
libusb_config_descriptor accordingly.

Closes #69

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWindows: Check for "calloc" allocation failure.
Gaurav [Wed, 13 May 2015 08:21:46 +0000 (13:51 +0530)]
Windows: Check for "calloc" allocation failure.

It need to check return value  of calloc. It may fail & return Null.

Closes #67

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Copy transfer timeout to local variable before using
Chris Dickens [Thu, 7 May 2015 19:45:33 +0000 (12:45 -0700)]
core: Copy transfer timeout to local variable before using

The flying_transfers lock is taken in libusb_get_next_timeout()
to find the transfer with the next timeout, but the transfer was
being used outside of the lock, resulting in the possibility that
the transfer had been removed from the list and freed at the time
it was used.

Issue reported by Michel Sanches.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Miscellaneous transfer timeout improvements
Chris Dickens [Mon, 27 Apr 2015 09:16:28 +0000 (02:16 -0700)]
core: Miscellaneous transfer timeout improvements

  * When removing a transfer from the flying_transfers list, only
    rearm the timerfd if the transfer being removed was the first
    transfer *and* had a timeout. This is the only case where the
    timerfd should be altered by a transfer being removed.

  * When searching the flying_transfers list for the next timeout,
    searching can cease when the first transfer with an infinite
    timeout is encountered.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoexamples: Address issues reported by Markus Elfring
Chris Dickens [Tue, 28 Apr 2015 21:56:00 +0000 (14:56 -0700)]
examples: Address issues reported by Markus Elfring

  * Use sig_atomic_t as data type for variable do_exit
  * Use async-safe function within signal handler

Closes #34

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Remove unused member add_iso_packet_size from struct usbi_os_backend
Tobias Klauser [Tue, 14 Apr 2015 09:20:24 +0000 (11:20 +0200)]
core: Remove unused member add_iso_packet_size from struct usbi_os_backend

The member add_iso_packet_size of struct usbi_os_backend is set to 0 in
all backends and thus has no actual use. This has been the case since
commit ad6e2b71 ("Linux: fire multiple URBs at once for split
transfers").

Also, the comment above the member actually states the fact that it is
unused and could potentially be removed, so remove it.

Closes #62

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoMisc: Remove unnecessary \n from log messages
Matthias Bolte [Mon, 16 Mar 2015 17:29:11 +0000 (18:29 +0100)]
Misc: Remove unnecessary \n from log messages

The final end-of-line marker is added in usbi_log_v() using the
USBI_LOG_LINE_END define.

Also don't mix hardcoded \n and USBI_LOG_LINE_END in usbi_log_v().

Closes #59

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWinCE: Unlock mutex when error occurs during event handling
Chris Dickens [Tue, 28 Apr 2015 00:23:39 +0000 (17:23 -0700)]
WinCE: Unlock mutex when error occurs during event handling

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWinCE: Fix return from void function
Chris Dickens [Tue, 28 Apr 2015 00:23:08 +0000 (17:23 -0700)]
WinCE: Fix return from void function

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWinCE: Make whitespace consistent across entire file
Chris Dickens [Tue, 28 Apr 2015 00:17:36 +0000 (17:17 -0700)]
WinCE: Make whitespace consistent across entire file

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWinCE: Remove use of dedicated timer thread
Chris Dickens [Tue, 28 Apr 2015 00:02:37 +0000 (17:02 -0700)]
WinCE: Remove use of dedicated timer thread

Without the ability to set thread affinity, there is no need
to maintain a separate thread to service monotonic clock_gettime()
requests. The determinism of QueryPerformanceCounter() will not
change or improve when run from a dedicated thread, so this just
creates additional overhead.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWinCE: Remove unused errno header and local variable
Chris Dickens [Mon, 27 Apr 2015 23:54:17 +0000 (16:54 -0700)]
WinCE: Remove unused errno header and local variable

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: generalize comment about setting configurations with claimed interfaces
Antonio Ospite [Wed, 22 Apr 2015 10:48:51 +0000 (12:48 +0200)]
core: generalize comment about setting configurations with claimed interfaces

The comment about libusb_set_configuration() failing when other programs
or drivers have claimed interfaces is valid whether or not the desired
configuration is _different_ from the current one, so generalize the
statement.

While at it also make it more explicit that in case of kernel drivers,
those must be detached from _all_ interfaces before a configuration can
be set.

Finally, move the item as last as the new details also suggest
a solution for when a lightweight reset is desired but kernel drivers
have claimed other interfaces, like in the fingerprint reader example
mentioned in the same section.

Signed-off-by: Antonio Ospite <ao2@ao2.it>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: use the actual signature of libusb_get_configuration() in the docs
Antonio Ospite [Wed, 22 Apr 2015 10:48:50 +0000 (12:48 +0200)]
core: use the actual signature of libusb_get_configuration() in the docs

Use the actual signature of the libusb_get_configuration() function in
the code example in the "caveats" section of the documentation.

Signed-off-by: Antonio Ospite <ao2@ao2.it>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWindows: Remove erroneous call to CloseHandle and add comments
Chris Dickens [Mon, 27 Apr 2015 08:40:50 +0000 (01:40 -0700)]
Windows: Remove erroneous call to CloseHandle and add comments

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWindows: Improve monotonic clock_gettime() implementation
Chris Dickens [Wed, 22 Apr 2015 07:15:53 +0000 (00:15 -0700)]
Windows: Improve monotonic clock_gettime() implementation

  * Fix setting of timer thread affinity
  * Simplify request mechanism by using a message queue, which also
    yields a 10% performance improvement

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWindows: Fix wIndex in setup packet for config descriptor request
Chris Dickens [Wed, 22 Apr 2015 05:04:31 +0000 (22:04 -0700)]
Windows: Fix wIndex in setup packet for config descriptor request

Per the USB spec, the wIndex field should be zero or the language ID.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agowindows: fix broken bus number lookup
Pete Batard [Thu, 5 Mar 2015 22:16:39 +0000 (22:16 +0000)]
windows: fix broken bus number lookup

* The current ancestry lookup for bus number stopped at the first
  non-enumerated grandparent device instead of continuing up to
  the HCD.
* Issue reported by Daniel Pfeffer

9 years agoRemove -c flag. Erroneous output caused on android
Venkatesh Shukla [Sat, 21 Jun 2014 17:30:21 +0000 (23:00 +0530)]
Remove -c flag. Erroneous output caused on android

While cross-building for android, the -c flag was causing errors.
This -c option ends up in Libs.private of libusb-1.0.pc file.
On its usage, it is interpreted as "Compile and assemble, but do not link"
option of gcc. Usage of -c in this way might be unintended.
Hence, removing this option.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoMisc: Update README to include Haiku backend
Chris Dickens [Sun, 1 Mar 2015 09:03:04 +0000 (01:03 -0800)]
Misc: Update README to include Haiku backend

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoMisc: Remove comment that no longer makes sense
Chris Dickens [Sun, 1 Mar 2015 08:51:07 +0000 (00:51 -0800)]
Misc: Remove comment that no longer makes sense

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agolinux: Fix format specifiers for sscanf() calls
Chris Dickens [Sun, 1 Mar 2015 08:49:01 +0000 (00:49 -0800)]
linux: Fix format specifiers for sscanf() calls

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Add internal transfer state management
Chris Dickens [Mon, 19 Jan 2015 01:07:13 +0000 (17:07 -0800)]
core: Add internal transfer state management

This patch adds some new flags to keep track of transfer state.
These flags are used to properly handle transfers that are on
the flying_transfers list for devices that are disconnected.

The motivation for this patch is to release the requirement of
holding the flying_transfers_lock for the duration of a call to
libusb_submit_transfer(). Holding this lock is the simplest and
safest way to submit a transfer, but it has performance impacts
as it serializes transfer submission for a given context.

With proper transfer state management, the library can handle a
device disconnect without needing to prevent multiple transfers
from being concurrently submitted.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agomisc: fix typos in comments
orbitcowboy [Fri, 13 Feb 2015 21:58:49 +0000 (22:58 +0100)]
misc: fix typos in comments

* Closes #53

9 years agodarwin: fix resource leak (cached devices are never freed)
parafin [Mon, 9 Feb 2015 21:28:43 +0000 (00:28 +0300)]
darwin: fix resource leak (cached devices are never freed)

For each new device (processed by hotplug code) darwin_ref_cached_device
is called twice - in process_new_device and in darwin_get_cached_device
(which is called from the first function). But when the device is detached
only one darwin_deref_cached_device is done - in darwin_destroy_device.
So there is inbalance between ref and deref which causes darwin_cached_devices
list to monotonically grow and consume more and more resources over time.
This commit fixes the problem by adding darwin_deref_cached_device call
to darwin_devices_detached.

Closes #52

Signed-off-by: Nathan Hjelm <hjelmn@mac.com>
9 years agodarwin: Fix build break and warning caused by commit 699db154
Chris Dickens [Sun, 1 Feb 2015 06:25:24 +0000 (22:25 -0800)]
darwin: Fix build break and warning caused by commit 699db154

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agohaiku: Transition to use new transfer completion API
Chris Dickens [Wed, 21 Jan 2015 08:18:44 +0000 (00:18 -0800)]
haiku: Transition to use new transfer completion API

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoopenbsd: Transition to use new transfer completion API
Chris Dickens [Wed, 21 Jan 2015 08:18:43 +0000 (00:18 -0800)]
openbsd: Transition to use new transfer completion API

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agonetbsd: Transition to use new transfer completion API
Chris Dickens [Wed, 21 Jan 2015 08:18:42 +0000 (00:18 -0800)]
netbsd: Transition to use new transfer completion API

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agodarwin: Transition to use new transfer completion API
Chris Dickens [Wed, 21 Jan 2015 08:18:41 +0000 (00:18 -0800)]
darwin: Transition to use new transfer completion API

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Implement new transfer completion API
Chris Dickens [Wed, 21 Jan 2015 08:18:40 +0000 (00:18 -0800)]
core: Implement new transfer completion API

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agodarwin: change Xcode project end-of-line
Ludovic Rousseau [Sun, 25 Jan 2015 12:52:38 +0000 (13:52 +0100)]
darwin: change Xcode project end-of-line

The end of line character was CR (0x0D) instead of NL (0x0A).

It looks like the mistake is from
1eff220474f63d7ea7f8f99bef2a3da9da5324eb

9 years agolinux_usbfs: Update MAX_ISO_BUFFER_LENGTH
Chris Dickens [Thu, 22 Jan 2015 08:25:16 +0000 (00:25 -0800)]
linux_usbfs: Update MAX_ISO_BUFFER_LENGTH

Newer kernels have raised the maximum length of individual ISO
packets and URBs. There's no easy way to detect the limit, so we
will define MAX_ISO_BUFFER_LENGTH as the largest known limit.

If a user runs this on an earlier kernel and submits an ISO transfer
that is too large, they will receive LIBUSB_ERROR_INVALID_PARAM.

The documentation has been updated to note this behavior, under
"Considerations for isochronous transfers".

Closes #23

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoFix up backends for new transfer completion API.
Chris Dickens [Wed, 21 Jan 2015 06:58:04 +0000 (22:58 -0800)]
Fix up backends for new transfer completion API.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Add alternative API for backends to handle transfer completion
Chris Dickens [Wed, 21 Jan 2015 06:49:59 +0000 (22:49 -0800)]
core: Add alternative API for backends to handle transfer completion

Currently all non-Linux POSIX backends emulate the Linux design of
having individual pollable file descriptors for each device. This
patch introduces a new internal API that allows backends to handle
transfer completion notification in a different way.

A new function, usbi_signal_transfer_completion(), is added. Each
backend can call this function when it detects a transfer has
completed. This function will place the transfer on a new
completed_transfers list and will signal the context's event.

Backends that use this new API will no longer provide a handle_events()
function but instead provide a handle_transfer_completion() function.
When a thread is doing event handling, it will call this backend
function for all transfers on the completed_transfers list.

With this change, backends that do not really have natively pollable
file descriptors can completely move away from this design and avoid
poll() altogether.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agohotplug: Provide a default enum value for libusb_hotplug_flag
Chris Dickens [Tue, 13 Jan 2015 07:32:48 +0000 (23:32 -0800)]
hotplug: Provide a default enum value for libusb_hotplug_flag

* Closes #35

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoExamples: Fix device leak in hotplugtest
Chris Dickens [Mon, 12 Jan 2015 08:12:02 +0000 (00:12 -0800)]
Examples: Fix device leak in hotplugtest

Device leak occurred if either of the following occured:
  1) Detach followed by attach
  2) Two consecutive attach

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWindows: Fix broken build caused by missing rename in 63a440f1
Chris Dickens [Sun, 11 Jan 2015 16:16:24 +0000 (08:16 -0800)]
Windows: Fix broken build caused by missing rename in 63a440f1

9 years agocore: Fix handling of duplicate transfer submission/cancellation
Chris Dickens [Thu, 8 Jan 2015 20:45:56 +0000 (12:45 -0800)]
core: Fix handling of duplicate transfer submission/cancellation

In the docs, we claim to report LIBUSB_ERROR_BUSY if submitting a
transfer that has already been submitted. Linux was the only backend
that actually checked and reported this, but unfortunately the code
in libusb_submit_transfer() resulted in a segfault. This is because
submission failure would delete the (active) transfer from the
flying_transfers list, and when the transfer finally completes it
would be deleted from the flying_transfers list a second time.

Instead of modifying each backend to check for a busy transfer, this
patch adds a flag to indicate when a transfer is in-flight. The core
library will check this flag and return LIBUSB_ERROR_BUSY immediately.

This patch also modifies libusb_cancel_transfer() to check that a
transfer is in-flight before cancelling and to check that a transfer
has not already been cancelled, returning LIBUSB_ERROR_NOT_FOUND in
both cases.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoWindows: Free all WinUSB handles when closing a device
Chris Dickens [Sun, 11 Jan 2015 07:55:28 +0000 (23:55 -0800)]
Windows: Free all WinUSB handles when closing a device

* libusbK (as of v3.0.7.0) will fail after 32 opens because
  resources from claimed interfaces are not freed by simply
  closing just the device handle
* Closes #16

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Add debug statements to help trace transfers
Chris Dickens [Thu, 8 Jan 2015 19:09:32 +0000 (11:09 -0800)]
core: Add debug statements to help trace transfers

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoDo not use IOUSBDeviceInterface550 or kIOUSBInterfaceInterfaceID550 on OSX versions
Nathan Hjelm [Thu, 8 Jan 2015 03:43:47 +0000 (20:43 -0700)]
Do not use IOUSBDeviceInterface550 or kIOUSBInterfaceInterfaceID550 on OSX versions
older than 10.9.

Closes #48

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
9 years agocore: Make event_data_lock non-recursive
Chris Dickens [Tue, 6 Jan 2015 18:59:01 +0000 (10:59 -0800)]
core: Make event_data_lock non-recursive

Hans pointed out that usbi_fd_notification() is only used by the
functions that add/remove pollfds. They already hold the required
lock, so to make it less expensive we will assume the lock is
already held.

The usbi_fd_notification() function has also been moved from core.c
into io.c and made static, since it now only has one use case.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoFixing erroneous double semi-colon
Chris Dickens [Tue, 6 Jan 2015 07:34:45 +0000 (23:34 -0800)]
Fixing erroneous double semi-colon

9 years agoWindows: Direct control requests to a specific interface when possible
Chris Dickens [Tue, 6 Jan 2015 04:47:47 +0000 (20:47 -0800)]
Windows: Direct control requests to a specific interface when possible

Control requests on composite devices for Windows is tricky, because not
all APIs are able to correctly process all types of requests. To improve
robustness, this change checks whether the control request recipient is
an interface or endpoint. If it is either, the control request will be
attempted against that interface's API first.

Additionally, when cycling through the interfaces to find a working API,
it will no longer fail if that API does not support the specific type of
control request because there is a chance that another API will succeed.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Improve the handling of the pollfd list
Chris Dickens [Sun, 21 Dec 2014 22:58:35 +0000 (14:58 -0800)]
core: Improve the handling of the pollfd list

This change consists of two parts that must be taken together.

Part 1 moves the pollfd list under the protection of event_data_lock
and eliminates the pollfd_lock. Since modifications to the pollfd
list are considered an event, it makes sense to merge this. Another
benefit of doing so is an enhancement to event handling. The event
handler can get the updated pollfd list upon entry into handle_events()
and can clear the event pipe if no other pending events exist, which
saves a needless iteration.

Part 2 makes notification of pollfd list changes part of adding or
removing a pollfd from the list. Previously this was done in two
distinct steps, however nothing prevented a new pollfd from being
used by an event handler before an explicit notification was sent out.
This change eliminates the need for USBI_TRANSFER_UPDATED_FDS and
reverts 9a9ef3ec2b9c691609ec9f8b82ac4436a662df18.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Signal the event pipe at most one time
Chris Dickens [Tue, 18 Nov 2014 07:53:14 +0000 (23:53 -0800)]
core: Signal the event pipe at most one time

This change will ensure that the event pipe is only signalled
at most one time during the course of any incoming events. New
events that occur while the event pipe is in the signalled state
will not cause the event pipe to be signalled again.

This change will provide support for the use of native events on
the Windows/WinCE backends, as these events are binary and do not
"count" the number of times they are signalled.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Eliminate hotplug pipe, using list and event pipe instead
Chris Dickens [Tue, 18 Nov 2014 07:53:13 +0000 (23:53 -0800)]
core: Eliminate hotplug pipe, using list and event pipe instead

To further consolidate libusb internal events, this change removes
the hotplug pipe. Hotplug messages are now kept in a list within the
context, and the event pipe is signalled when a new hotplug message
is added. When handling events, the hotplug messages will be processed
from the list instead of from a separate pipe.

This change is greatly beneficial for the Windows/WinCE backends which
do not allow pipes to be used in the WaitFor* functions.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Add new flag to record when a new poll fd is added
Chris Dickens [Tue, 18 Nov 2014 07:53:12 +0000 (23:53 -0800)]
core: Add new flag to record when a new poll fd is added

This flag will be useful in a subsequent commit that further
consolidates event handling.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Add helper functions to signal and clear the event pipe
Chris Dickens [Tue, 18 Nov 2014 07:53:11 +0000 (23:53 -0800)]
core: Add helper functions to signal and clear the event pipe

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Rename pipe used to signal internal events
Chris Dickens [Tue, 18 Nov 2014 07:53:10 +0000 (23:53 -0800)]
core: Rename pipe used to signal internal events

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Transition device close event to use event_data_lock
Chris Dickens [Tue, 18 Nov 2014 07:53:09 +0000 (23:53 -0800)]
core: Transition device close event to use event_data_lock

This change removes the device_close_lock and uses the shared event
data lock to protect the value of the device_close counter.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Add new lock to protect internal context event data
Chris Dickens [Tue, 18 Nov 2014 07:53:08 +0000 (23:53 -0800)]
core: Add new lock to protect internal context event data

This lock will be used in subsequent changes that will consolidate
all different event sources (i.e. device close, fd notification,
hotplug message) into a single event.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Rename pollfd_modify counter and its lock to device_close
Chris Dickens [Tue, 18 Nov 2014 07:53:07 +0000 (23:53 -0800)]
core: Rename pollfd_modify counter and its lock to device_close

This counter is now solely used for closing a device, so rename
the variable appropriately.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agocore: Remove taking of events lock inside usbi_fd_notification()
Chris Dickens [Tue, 18 Nov 2014 07:53:06 +0000 (23:53 -0800)]
core: Remove taking of events lock inside usbi_fd_notification()

It is unnecessary to take the events lock when a thread needs to
interrupt an event handler to receive a change list of poll fds.
It is sufficient to simply write to the control pipe and let the
event handler be notified of this event when it polls.

Taking the events lock inside this function leads to opportunity
for deadlock in certain situations, such as a client program
running on an OS that uses fd notification on each individual
transfer. If the client program were to protect a list of transfers
with a single lock, it could deadlock if that lock were taken in
two separate threads, one which is attempting to submit a new
transfer and one which is executing inside the transfer completion
callback.

Thanks to Dmitry Fleytman and Pavel Gurvich for reporting this.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoDoxygen: upgrade doxygen.cfg.in file
Ludovic Rousseau [Tue, 16 Dec 2014 20:55:37 +0000 (21:55 +0100)]
Doxygen: upgrade doxygen.cfg.in file

Warning: Tag `XML_SCHEMA' at line 942 of file `doxygen.cfg' has become obsolete.
         To avoid this warning please remove this line from your configuration file or upgrade it using "doxygen -u"
Warning: Tag `XML_DTD' at line 948 of file `doxygen.cfg' has become obsolete.
         To avoid this warning please remove this line from your configuration file or upgrade it using "doxygen -u"

I upgrade the doxygen.cfg.in using: "doxygen -u doxygen.cfg.in"

The configuration file changed from Doxyfile 1.5.3 to Doxyfile 1.8.8

9 years agoDoxygen: fix a duplicate label
Ludovic Rousseau [Tue, 16 Dec 2014 20:50:19 +0000 (21:50 +0100)]
Doxygen: fix a duplicate label

libusb/io.c:46: warning: multiple use of section label 'intro' while
adding section, (first occurrence: libusb/core.c, line 79)

Use label "io_intro" instead of "intro"

9 years agoDoxygen: fix a duplicate label
Ludovic Rousseau [Tue, 16 Dec 2014 20:47:44 +0000 (21:47 +0100)]
Doxygen: fix a duplicate label

libusb/hotplug.c:46: warning: multiple use of section label 'intro'
while adding section, (first occurrence: libusb/core.c, line 79)

Use label "hotplug_intro" instead of "intro"

9 years agoAPI: remove unused libusb_hotplug_callback struct
Ludovic Rousseau [Tue, 16 Dec 2014 20:17:16 +0000 (21:17 +0100)]
API: remove unused libusb_hotplug_callback struct

The structure libusb_hotplug_callback was declared (but not defined) and
never used in the public API.

9 years agoAPI: Add an Application Programming Interface page
Ludovic Rousseau [Mon, 15 Dec 2014 10:46:41 +0000 (11:46 +0100)]
API: Add an Application Programming Interface page

This page lists the all the public functions, structures and enums
provided by libusb.
The HTML page is generated by Doxygen.

9 years agoWindows: fix 2 bugs in windows_handle_events()
Pete Batard [Tue, 16 Dec 2014 19:19:50 +0000 (19:19 +0000)]
Windows: fix 2 bugs in windows_handle_events()

* Var found was not reset to false before list_for_each_entry()
* ctx->open_devs_lock was not released on error.
* Issues reported by Yongjian Xu

9 years agomisc: Fix grammar in a comment
Simon Newton [Tue, 25 Nov 2014 04:09:57 +0000 (20:09 -0800)]
misc: Fix grammar in a comment

# Closes #41

9 years agoWindows: Silence VS2013 code analysis warnings
Pete Batard [Thu, 13 Nov 2014 22:03:30 +0000 (22:03 +0000)]
Windows: Silence VS2013 code analysis warnings

* Also update Windows version report for Windows 10

9 years agoWindows: Fix cygwin64 build
Pete Batard [Thu, 6 Nov 2014 00:50:01 +0000 (00:50 +0000)]
Windows: Fix cygwin64 build

* Issue reported by Thejus

9 years agoFix broken 'make install'
Akshay Jaggi [Fri, 26 Sep 2014 05:05:07 +0000 (10:35 +0530)]
Fix broken 'make install'

...that was broken with dc97425bb415422423b8876af0e34111e566d56d (Haiku inclusion)

9 years agohaiku: Add Haiku support
Akshay Jaggi [Wed, 24 Sep 2014 21:46:17 +0000 (22:46 +0100)]
haiku: Add Haiku support

9 years agoio: Move timerfd close into error handling path
Chris Dickens [Fri, 29 Aug 2014 23:12:55 +0000 (16:12 -0700)]
io: Move timerfd close into error handling path

This makes error handling consistent with other areas. This is a
follow-on change to commit b72f4cf77edb1975cada8aab5ae63361c68c5992.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agoio: Fix memory leaks in usbi_io_init()
Chris Dickens [Wed, 27 Aug 2014 06:36:12 +0000 (23:36 -0700)]
io: Fix memory leaks in usbi_io_init()

* If the hotplug pipe failed to be created, the usbi_pollfd for the
  control pipe was being leaked.
* If the usbi_pollfd for timerfd failed to be added, the usbi_pollfd
  for the hotplug pipe was being leaked.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
9 years agothreads_posix: Fix compiler warning due to redefinition of _GNU_SOURCE
Chris Dickens [Sun, 7 Sep 2014 04:59:03 +0000 (21:59 -0700)]
threads_posix: Fix compiler warning due to redefinition of _GNU_SOURCE

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