platform/upstream/libusb.git
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>
11 years agolinux: get_configuration don't return LIBUSB_ERROR_NOT_SUPPORTED with usbfs
Hans de Goede [Wed, 22 May 2013 18:45:26 +0000 (20:45 +0200)]
linux: get_configuration don't return LIBUSB_ERROR_NOT_SUPPORTED with usbfs

We have an implementation of get_configuration with usbfs, so lets use it.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Use cached config descriptors
Hans de Goede [Wed, 22 May 2013 18:38:51 +0000 (20:38 +0200)]
linux: Use cached config descriptors

Use cached config descriptors instead of doing tons file io, because:
- Less fileio is more
- Less code is more, diffstat for this patch:
 1 file changed, 128 insertions(+), 307 deletions(-)

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: init: Only stop the event monitor if we also started it
Hans de Goede [Wed, 22 May 2013 13:57:29 +0000 (15:57 +0200)]
linux: init: Only stop the event monitor if we also started it

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Switch to kernel version check to determine if sysfs can relate devs
Hans de Goede [Wed, 22 May 2013 13:48:22 +0000 (15:48 +0200)]
linux: Switch to kernel version check to determine if sysfs can relate devs

We rely on the kernel version for determining capabilities, except for
sysfs_can_relate_devices. This changes sysfs_can_relate_devices over to
a kernel version check to.

This makes things more consistent, removes a whole bunch of code, and since
it stops us from needlessly banging sysfs, it reduces the avarage run time for
the stress test on my system from 21.8 to 16.7 seconds.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Switch to kernel version check to determine if sysfs has descriptors
Hans de Goede [Wed, 22 May 2013 13:25:06 +0000 (15:25 +0200)]
linux: Switch to kernel version check to determine if sysfs has descriptors

With the refactoring of the Linux descriptors handling, we only want to use
sysfs descriptors if they have the complete config descriptors (versus just
the active config descriptor), which is the case since Linux 2.6.26.

This means that we will fallback to using the usbfs descriptors for the device
descriptor on Linux 2.6.23 - 2.6.25, which only have the active config
descriptor. This will cause a tiny performance penalty, but only on these
3 kernel versions (which are not used in any still supported Linux distros),
and then only on library init since we now cache everything.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Clear descriptor buffer before read when using usbfs
Hans de Goede [Wed, 22 May 2013 12:52:37 +0000 (14:52 +0200)]
linux: Clear descriptor buffer before read when using usbfs

In usbfs the config descriptors are config.wTotalLength bytes apart, but the
device may actually return a shorter descriptor then advertised, in this
case the kernel will simply skip over any bytes read which the device did
not actually return. Note the kernel will *not* return 0 data, it will simply
leave the memory passed in to the read call as is.

Therefor this patch clears the buffer before calling read, to ensure that
the non existing parts of the descriptors are always properly zero-ed.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Cache the entire descriptors at device-init
Hans de Goede [Wed, 22 May 2013 10:57:32 +0000 (12:57 +0200)]
linux: Cache the entire descriptors at device-init

Now that the core caches device-descriptors, we end up opening each
usbfs-node or sysfs-descriptors file once on libusb_init anyways.

So we might as well do this on device-init, rather then waiting for the
core to call op_get_device_descriptor. This allows us to simplify the
code in various places.

While we've it open, read the entire file rather then only reading the
device-descriptor. This is practically free, since most of the cost is in
the opening of the file, not in reading it.

Running the stress test, which does 10000 libusb_init calls, takes 21.8 seconds
on avarage on my idle system with 17 usb devices both before and after this
patch, showing that the cost of also reading the config descriptors while
we've the file open is truely neglible.

Note that this patch does not yet use the cached config descriptors, this is
done by a later patch in this series.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Add a _get_usbfs_fd helper function
Hans de Goede [Wed, 22 May 2013 10:40:51 +0000 (12:40 +0200)]
linux: Add a _get_usbfs_fd helper function

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agousbi_alloc_device: Drop unnecessary memset 0
Hans de Goede [Wed, 22 May 2013 10:12:28 +0000 (12:12 +0200)]
usbi_alloc_device: Drop unnecessary memset 0

The memory gets calloc-ed, so there no need to clear it.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Fix host_endian handling
Hans de Goede [Sat, 18 May 2013 12:07:49 +0000 (14:07 +0200)]
linux: Fix host_endian handling

-in 2 cases the passed in host_endian was not being set
-get_config_descriptor was wrongly calling seek_to_next_config with
 host_endian set to 1, but the only case where host_endian is 1 is when
 reading the device-desc from usbfs, even in usbfs the config descriptors
 are in raw format

Note that the 2nd change partly reverts commit
7f2e9f0776386997d2b4c4c47598ab88e3caeb7a
"Linux: Fix usbfs/sysfs config descriptor handling on big-endian"
Which commit msg says: "checked against Documentation/usb/proc_usb_info.txt"

Well guess what, I checked the actual drivers/usb/core/devio.c code
and Documentation/usb/proc_usb_info.txt is *wrong*. I'll send a patch to
update it.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoExamples: Fix use of deprecated libusb_get_port_path
Hans de Goede [Fri, 17 May 2013 08:42:47 +0000 (10:42 +0200)]
Examples: Fix use of deprecated libusb_get_port_path

While at it also simplify the path printing in listdevs

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore: Add a new public libusb_get_port_numbers function
Hans de Goede [Fri, 17 May 2013 08:36:32 +0000 (10:36 +0200)]
core: Add a new public libusb_get_port_numbers function

This new function replaces the now deprecated libusb_get_port_path function,
as that is the only function operating on a libusb_device which also takes
a libusb_context, which is rather inconsistent.

Note we will keep libusb_get_port_path around in the 1.0.x for the forseeable
future for ABI compatibility reasons, but it should not be used in any new
code.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore: Fix handle_events return code on hotplug pipe read error
Hans de Goede [Fri, 17 May 2013 08:07:25 +0000 (10:07 +0200)]
core: Fix handle_events return code on hotplug pipe read error

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agousbi_handle_disconnect: Fix race condition leading to double completion
Hans de Goede [Fri, 3 May 2013 19:19:28 +0000 (21:19 +0200)]
usbi_handle_disconnect: Fix race condition leading to double completion

It took me quite a while to debug this, here is a step by step for the race
which I believe is happening in some cases:

1) app calls libusb_submit_transfer
2) libusb_submit_transfer locks itransfer->lock
3) libusb_submit_transfer adds the transfer to the flying list
4) *thread switch*
5) other thread notices POLL_ERR on device fd, calls usbi_handle_disconnect
6) usbi_handle_disconnect find the transfer which is in progress of being
   submitted in the flying list
7) usbi_handle_disconnect calls usbi_backend->clear_transfer_priv on the
   transfer, this blocks waiting on itransfer->lock
8) *thread switch*
9) libusb_submit_transfer actually tries to submit the transfer now,
   calls usbi_backend->submit_transfer, which fails with -ENODEV
10) libusb_submit_transfer *removes* the transfer from the flying list,
   unlocks itransfer->lock and returns an error to its caller
11) the caller frees the transfer, meaning the to_cancel pointer in
   usbi_handle_disconnect now points to free-ed memory, for extra mayhem
12) *thread switch*
13) usbi_handle_disconnect calls usbi_handle_transfer_completion
14) usbi_handle_transfer_completion tries to remove the transfer from
    the flying list *for the 2nd time*
    But the first call done from libusb_submit_transfer has already done
    this. libusb's list_del looks like this:

    static inline void list_del(struct list_head *entry)
    {
        entry->next->prev = entry->prev;
        entry->prev->next = entry->next;
        entry->next = entry->prev = NULL;
    }

    So the first call sets it next and prev to NULL, and then the 2nd call
    tries to deref next -> BOOM

    For an example backtrace caused by this, see:
    https://bugs.freedesktop.org/show_bug.cgi?id=55619#c7

This patch fixes this by letting libusb_submit keep the flying transfers list
locked during submission, so the submission flow changes from:

1) lock flying transfers
   add to flying transfers
   unlock
2) submit
3) on submission error:
   lock flying transfers
   remove from flying transfers
   unlock

to:

1) lock flying transfers
2) add to flying transfers
3) submit
4) on submission error:
   remove from flying transfers
5) unlock

This means that the os backends submit handler now gets called with the
flying transfers lock held! I've looked at all the backends and this should
not be a problem. Only the windows and win-ce backends care about the
flying transfers list at all, and then only in their handle_events handler.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agousbi_handle_disconnect: Add some debugging wrt cancelled transfers
Hans de Goede [Fri, 3 May 2013 18:19:06 +0000 (20:19 +0200)]
usbi_handle_disconnect: Add some debugging wrt cancelled transfers

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: linux_get_parent_info: properly propagate malloc errors
Hans de Goede [Thu, 16 May 2013 20:40:13 +0000 (22:40 +0200)]
linux: linux_get_parent_info: properly propagate malloc errors

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolistdevs: Also print portpath for non root hubs
Hans de Goede [Thu, 16 May 2013 20:36:26 +0000 (22:36 +0200)]
listdevs: Also print portpath for non root hubs

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoAll: ref dev->parent_dev
Hans de Goede [Thu, 16 May 2013 20:09:58 +0000 (22:09 +0200)]
All: ref dev->parent_dev

An app can hold a reference to just a leave device, if then an entire
hub goes away, all removed devices will get there final unref, except for
the one ref-ed by the app. If the app then tries to use parent_dev in anyway
after this, we've a use-after-free bug. This fixes this.

Also remove the lets re-enumerate fix for this from libusb_get_port_path,
I'm not sure what the exact idea behind this fix was, but after this patch
it is no longer needed, and this patch also fixes usage of for example
libusb_get_parent().

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore: Improve error / debug messages for hotplug pipe handling
Hans de Goede [Thu, 16 May 2013 19:50:50 +0000 (21:50 +0200)]
core: Improve error / debug messages for hotplug pipe handling

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Fixup init_count tracking on hotplug init error
Hans de Goede [Thu, 16 May 2013 19:31:31 +0000 (21:31 +0200)]
linux: Fixup init_count tracking on hotplug init error

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux_udev: linux_start_event_monitor: Properly cleanup on error
Hans de Goede [Thu, 16 May 2013 19:30:42 +0000 (21:30 +0200)]
linux_udev: linux_start_event_monitor: Properly cleanup on error

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Replace should-not-happen checks with asserts
Hans de Goede [Thu, 16 May 2013 19:18:11 +0000 (21:18 +0200)]
linux: Replace should-not-happen checks with asserts

These all really should never happen, so rather then to do error handling
when they do, simple assert them.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Not finding a device on hotplug_disconnect is not an error
Hans de Goede [Thu, 16 May 2013 18:49:01 +0000 (20:49 +0200)]
linux: Not finding a device on hotplug_disconnect is not an error

This can happen if the device gets removed between registering the hot-plug
event handler and initial device enumeration running. So lets turn this
into a debug message to avoid spurious bug-reports.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Improve topology related debug messages
Hans de Goede [Thu, 16 May 2013 18:46:07 +0000 (20:46 +0200)]
linux: Improve topology related debug messages

And use usbi_dbg, rather then fprintf(stderr, ...

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Set device->port_number
Hans de Goede [Thu, 16 May 2013 18:37:51 +0000 (20:37 +0200)]
linux: Set device->port_number

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Deal with devices being enumerated before their parents are
Hans de Goede [Thu, 16 May 2013 15:13:11 +0000 (17:13 +0200)]
linux: Deal with devices being enumerated before their parents are

The linux netlink enumeration code processes devices in readdir order, which
means devices may get enumerated before their parent is, IE on my system
readdir order is (ls -U)
1-1   usb4       2-1.8        6-0:1.0    1-1.4.3    1-1.4.4.1:1.0
2-1   usb5       6-1.4        6-1:1.0    1-1.4.4    1-1.4.4.1:1.1
6-1   usb6       3-0:1.0      2-1.8:1.0  1-0:1.0    1-1.4.4.1:1.2
7-1   usb7       1-1.4.3:1.0  2-1.8:1.1  1-1:1.0
usb1  1-1.4.4.1  1-1.4.4:1.0  2-1.8:1.2  6-1.4:1.0
usb2  1-1.4:1.0  4-0:1.0      7-0:1.0    2-0:1.0
usb3  1-1.4      5-0:1.0      7-1:1.0    2-1:1.0

So 1.4.4.1 will get added (way) before 1.4.4 and indeed:
[ 0.002243] [00004055] libusbx: debug [linux_get_device_address] scan 1-1.4.4.1
Dev 0x1973bc0 (1-1.4.4.1) has parent (nil)

This patch fixes this by forcing enumeration of the parent from
linux_parent_dev.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Move check for a device being added twice to linux_enumerate_device
Hans de Goede [Thu, 16 May 2013 15:02:08 +0000 (17:02 +0200)]
linux: Move check for a device being added twice to linux_enumerate_device

It is possible for a device to show up between the hotplug code starting to
listen for new devices, and the enumeration of existing devices. This will
cause a device to get enumerated twice.

The next patch in this series adds a different code path which can cause
a device to be enumerated twice, this patch therefor moves the check for
this to linux_enumerate_device, so that this will get caught in all cases.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: hotplug_enumerate and _disconnect take active_contexts_lock
Hans de Goede [Thu, 16 May 2013 14:56:40 +0000 (16:56 +0200)]
linux: hotplug_enumerate and _disconnect take active_contexts_lock

Take the active_contexts_lock when enumerating over active_contexts.

Note that the active_contexts_lock is taken *before* the hotplug lock, as they
are taken in the same order in the libusb_init path.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Replace pthread_mutex with usbi_mutex_static
Hans de Goede [Thu, 16 May 2013 14:54:02 +0000 (16:54 +0200)]
linux: Replace pthread_mutex with usbi_mutex_static

Boils down to the same thing, but mixing and matching lock types in one
file looks ugly.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore: Fixup default_context handling
Hans de Goede [Thu, 16 May 2013 14:43:24 +0000 (16:43 +0200)]
core: Fixup default_context handling

Now that we set the default context earlier in libusb_init:
-There is no need to do it a second time at the end of libusb_init
-We need to set it to NULL if libusb_init fails later on
-Since it now can be set then reset by libusb_init, libusb_exit needs to
 take the lock while testing if the passed in ctx == default_context

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore: Fixup cleanup on init error
Hans de Goede [Thu, 16 May 2013 14:38:41 +0000 (16:38 +0200)]
core: Fixup cleanup on init error

-mutexes were not being destroyed on backend init failure
-hotplug_cbs_lock mutex was never being destroyed

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore: Add ctx to the active context list before backend->init
Hans de Goede [Thu, 16 May 2013 14:32:33 +0000 (16:32 +0200)]
core: Add ctx to the active context list before backend->init

As soon as backend->init has completed hotplug events may fire, and they
will not get processed on the created context until it has been added
to the active_context list.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: Take hotplug_lock for scan_devices, hotplug_enumerate and _disconnect
Hans de Goede [Thu, 16 May 2013 14:21:23 +0000 (16:21 +0200)]
linux: Take hotplug_lock for scan_devices, hotplug_enumerate and _disconnect

As soon as we've started listening for hp events, hotplug_enumerate and
_disconnect can run, they do test then add / remove operations on the
device list. This can race with scan_devices adding devices, so take
the lock around all 3 to avoid the race.

Also fix the lock not being released in case of linux_start_event_monitor
failure.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: linux_parent_dev search for . from the right
Hans de Goede [Thu, 16 May 2013 13:02:12 +0000 (15:02 +0200)]
linux: linux_parent_dev search for . from the right

If hubs are chained to other hubs there will be multiple '.' characters, and
want the last one.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoconfigure: Fix --enable-udev help text
Hans de Goede [Thu, 16 May 2013 11:08:58 +0000 (13:08 +0200)]
configure: Fix --enable-udev help text

We use AC_ARG_ENABLE so the help string should be --enable-udev, so that
the user knows to pass --disable-udev to disable udev

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolinux: fix topology
Nathan Hjelm [Wed, 15 May 2013 13:54:53 +0000 (07:54 -0600)]
linux: fix topology

Find the parent dev by removing the last number after the . or -
(and prepending with usb if only one number is left) and comparing
it against the sysfs_dir or previously scanned devices.

HdG: Drop hardcoded special casing of root-hubs, this is fixed in a better
way in a later commit.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin: fix topology
Nathan Hjelm [Wed, 15 May 2013 01:25:55 +0000 (19:25 -0600)]
darwin: fix topology

The parent device should always already be in the context when a
device is processed. Use the parent device's location to find the
parent dev.

Note: we might want to replicate the notion of a locationID in the
core. If done right the parent dev could be determined in the core.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin: clean up extraneous includes
Nathan Hjelm [Sat, 8 Dec 2012 02:55:21 +0000 (19:55 -0700)]
darwin: clean up extraneous includes

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoAdd hotplugtest to msvc project files
Pete Batard [Wed, 15 May 2013 15:39:47 +0000 (17:39 +0200)]
Add hotplugtest to msvc project files

11 years agoMisc fixes
Pete Batard [Wed, 15 May 2013 15:34:01 +0000 (17:34 +0200)]
Misc fixes

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoexamples: Only check for HAVE_SIGACTION once in Makefile.am
Peter Stuge [Tue, 8 May 2012 17:22:31 +0000 (19:22 +0200)]
examples: Only check for HAVE_SIGACTION once in Makefile.am

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoAdd INSTALL_WIN.txt eol=crlf to .gitattributes
Hans de Goede [Wed, 15 May 2013 14:19:02 +0000 (16:19 +0200)]
Add INSTALL_WIN.txt eol=crlf to .gitattributes

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoAdd some editor meta-comments for proper tab usage
Nathan Hjelm [Wed, 15 May 2013 14:11:22 +0000 (16:11 +0200)]
Add some editor meta-comments for proper tab usage

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoCore: use C99 flexible array member when possible
Sean McBride [Fri, 12 Oct 2012 21:34:25 +0000 (17:34 -0400)]
Core: use C99 flexible array member when possible

Fix clang warning by using C99 flexible array member instead of zero
length array gcc extension

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin: return LIBUSB_ERROR_NOT_FOUND when reading the active config descriptor when...
Nathan Hjelm [Fri, 3 May 2013 14:03:37 +0000 (08:03 -0600)]
darwin: return LIBUSB_ERROR_NOT_FOUND when reading the active config descriptor when the device is not configured.

fixes #172

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin: clean up old code
hjelmn@cs.unm.edu [Tue, 23 Apr 2013 22:35:33 +0000 (16:35 -0600)]
darwin: clean up old code

For some time only one type of completion message has been in use in the darwin
backend. The commit cleans up this code. This commit also removes some code
that has never done anything useful. If a pipe has an error it probably means
the device was closed so there is nothing to do.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin: fix memory leaks in the darwin backend.
Nathan Hjelm [Fri, 26 Apr 2013 03:12:45 +0000 (21:12 -0600)]
darwin: fix memory leaks in the darwin backend.

There were two memory leaks in the darwin backend:
 - A CFString allocated by IORegistryEntryCreateCFProperty in darwin_get_interface()
   was not released.
 - The runloop is retained by darwin_open() but was not release by darwin_close().

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agocore: release the context device list when the context is deleted.
Nathan Hjelm [Fri, 26 Apr 2013 03:09:19 +0000 (21:09 -0600)]
core: release the context device list when the context is deleted.

This commit fixes a memory leak in the context list patch. Devices need
to be unref'd when the context is deleted. This can happen if either 1)
the backend init fails, or 2) the context is destroyed by libusb_exit().

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin: don't assume an interval of 1 or high/super speed
Nathan Hjelm [Wed, 3 Apr 2013 04:12:47 +0000 (22:12 -0600)]
darwin: don't assume an interval of 1 or high/super speed

This patch updates submit_iso_transfer to use the bInterval value of the
endpoint and the speed of the device to determine the last frame of the
transaction.

Fixes: #165
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin: add support for the LIBUSB_TRANSFER_ADD_ZERO_PACKET transfer flag.
Nathan Hjelm [Fri, 8 Feb 2013 07:12:13 +0000 (00:12 -0700)]
darwin: add support for the LIBUSB_TRANSFER_ADD_ZERO_PACKET transfer flag.

The zero-length packet is sent by the async event thread if all the following
conditions are met:

1) The packet size is not a multiple of maxPacketSize, and
2) the transfer completed successfully.

References #6.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin/libusb_reset: reenumerate device if the device descriptor or any configuration...
Nathan Hjelm [Fri, 8 Feb 2013 04:56:30 +0000 (21:56 -0700)]
darwin/libusb_reset: reenumerate device if the device descriptor or any configuration descriptor has changed

Some background: the Linux kernel causes a device to be reenumeration when
reset if the device's descriptors have changed. Darwin does not provide
this feature but instead provides a call to force the reenumeration of
a device. This behavior is required to support the firmware update
protocol used by some devices.

This patch updates darwin_reset to check if either the device or
configuration descriptors have changed. If they have darwin_reset will
ask the OS to reenumerate the device and return LIBUSB_ERROR_NOT_FOUND
or LIBUSB_SUCCESS accordingly.

Fixes #169

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin: dpriv->device will never be NULL for a valid device. remove unnecessary code...
Nathan Hjelm [Sat, 1 Dec 2012 02:17:43 +0000 (19:17 -0700)]
darwin: dpriv->device will never be NULL for a valid device. remove unnecessary code that handled this case

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agodarwin: allow control messages on devices we can not exclusively open
Nathan Hjelm [Thu, 29 Nov 2012 23:19:11 +0000 (16:19 -0700)]
darwin: allow control messages on devices we can not exclusively open

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoBump LIBUSBX_API_VERSION for new hotplug API
Hans de Goede [Wed, 15 May 2013 10:17:42 +0000 (12:17 +0200)]
Bump LIBUSBX_API_VERSION for new hotplug API

And document the minimum version under which hotplug is supported

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoAdd hotplug support to the Linux backend.
Nathan Hjelm [Thu, 29 Nov 2012 23:14:03 +0000 (16:14 -0700)]
Add hotplug support to the Linux backend.

There are two ways to configure hotplug support for Linux: udev, and netlink. It
is strongly recommened that udev support is used on systems that utilize udev.
We reenforce this recommendation by defaulting to --with-udev=yes at configure
time. To enable netlink support run configure with --with-udev=no. If udev
support is enabled all device enumeration is done with udev.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoAdd hotplug support to the Darwin backend.
Nathan Hjelm [Thu, 29 Nov 2012 23:13:00 +0000 (16:13 -0700)]
Add hotplug support to the Darwin backend.

Hotplug events are handled by the async event thread. This thread listens for
two events: kIOTerminatedNotification, and kIOFirstMatchNotification. If
either of these events fires the thread will iterate through the appropriate
iterator and will either enumerate or call usbi_disconnect_device depending
on the event.

While adding hotplug support it was discovered that when reading from the IO
registry we need to use kCFNumberSInt32Type for the device location. Using
kCFNumberLongType would cause locations to be sign-extended and not match
what is stored for the session id. This is now fixed.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoAdd hotplug support.
Nathan Hjelm [Thu, 29 Nov 2012 21:23:26 +0000 (14:23 -0700)]
Add hotplug support.

The internal API is changing as follows:
 - Adding two new functions. usbi_connect_device, and usbi_disconnect_device.
   Backends must call these functions to add them to the context's device list
   at one of two places: initial enumeration (done at init), and on device
   attach and removal. These functions need to be called once per context.
 - Backends that support hotplug should not provide a get_device_list funtion.
   This function is now deprecated and will likely be removed once all backends
   support hotplug.

The external API is changing as follows:
 - Two new functions have been added to register and deregister callbacks for
   hotplug notification: libusb_hotplug_register_callback(),
   libusb_hotplug_deregister_callback(). Hotplug callbacks are called by
   libusb_handle_events(). Details of the new API can be found in libusb.h.
 - A new capability check has been added to check for hotplug support. See
   LIBUSB_CAP_HAS_HOTPLUG.

Aa suggested by Xiaofan add new example has been added to show how to use
the new external hotplug API. See examples/hotplugtest.c.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoFixup .gitignore for examples and tests now living in subdirs
Pete Batard [Wed, 15 May 2013 08:21:16 +0000 (10:21 +0200)]
Fixup .gitignore for examples and tests now living in subdirs

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoCore: Cache device descriptors in the core
Nathan Hjelm [Thu, 29 Nov 2012 20:53:41 +0000 (13:53 -0700)]
Core: Cache device descriptors in the core

Discussion: It is necessary to cache the device descriptor in the core to
support reading the descriptors after a device is disconnected. We could
either 1) allow each backend to handle this caching (which would most
certainly duplicate code), or 2) cache the descritors when a device
is added. This patch does the later.

Further discussion: It might be beneficial to cache more than just the
device descriptors in the core. It might also be worthwhile caching the
configuration and BOS descriptors as well.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoCore: Keep a list of contexts created by libusb_init()
Nathan Hjelm [Sun, 30 Sep 2012 17:49:01 +0000 (11:49 -0600)]
Core: Keep a list of contexts created by libusb_init()

* This list can be used by backends to determine which contexts
  need notification of device removal (and in the future device arrival).
  The active_contexts_list is protected from multiple access by the active_contexts_lock.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agolibusb.h: Correct errors in doxygen comment for struct libusb_device
Hans de Goede [Tue, 23 Apr 2013 08:46:32 +0000 (10:46 +0200)]
libusb.h: Correct errors in doxygen comment for struct libusb_device

The comment refers to the non-existing libusb_device_ref and
libusb_device_unref functions, this should be libusb_ref_device resp.
libusb_unref_device.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 years agoWinCE: Include COPYING and license information in binary snapshots.
Toby Gray [Tue, 16 Apr 2013 14:22:14 +0000 (15:22 +0100)]
WinCE: Include COPYING and license information in binary snapshots.

11 years agolibusbx-1.0.15
Pete Batard [Mon, 15 Apr 2013 22:15:10 +0000 (23:15 +0100)]
libusbx-1.0.15

11 years agoSamples: Fix timeout error on FX3 and logging improvements for fxload
Pete Batard [Sun, 14 Apr 2013 21:38:52 +0000 (22:38 +0100)]
Samples: Fix timeout error on FX3 and logging improvements for fxload

* Add a specific ezusb_fx3_jump() that handles timeout as a non-error
  This is required as a successful jump call makes the device disconnect from the bus
* Set default verbosity to 1 and adjust some messages' verbosity level
* Add a new -q option to decrease verbosity
* Add readout of the bootloader version for FX3 devices
* Filter the image types actually supported for FX3
* Fix the "errcode shadows a global variable" warning on some systems

11 years agoWindows: Correctly clear backend transfer private information
Toby Gray [Thu, 11 Apr 2013 12:40:31 +0000 (13:40 +0100)]
Windows: Correctly clear backend transfer private information

* Without this fix if a transfer is reused then there is a period of
  time between it being adding to the flying transfer list and
  submitted where the fd value will be the old fd value.
* If this occurs at the same time as all of the following conditions
  then the incorrect transfer will be handled as having completed:
* The old fd value in the reused transfer has been recycled for a
  currently pending transfer.
* This other pending transfer has a later timeout than the reused
  transfer (so therefore comes later in the flying transfer list).
* The other pending transfer completes, therefore signalling the fd.

As the flying transfer list is examined in order when handling events,
the resubmitted transfer with the old fd value will be considered as
completed. This will generally cause a NULL pointer dereference as the
OVERLAPPED structure was already freed.

Also see:
http://libusbx.1081486.n5.nabble.com/Libusbx-devel-PATCH-Fix-NULL-pointer-dereference-in-Windows-and-WinCE-backends-when-reusing-transfers-tt1041.html

11 years agoWinCE: Add internal build script for WinCE binaries
Toby Gray [Thu, 4 Apr 2013 12:32:31 +0000 (13:32 +0100)]
WinCE: Add internal build script for WinCE binaries

* Also copy over .pdb for WinCE and WDK generated DLLs

11 years agoWinCE: Fix x86 build on Visual Studio 2005
Toby Gray [Thu, 4 Apr 2013 12:33:20 +0000 (13:33 +0100)]
WinCE: Fix x86 build on Visual Studio 2005

11 years agolibusbx 1.0.15-rc3
Pete Batard [Thu, 4 Apr 2013 18:03:15 +0000 (19:03 +0100)]
libusbx 1.0.15-rc3

11 years agoOS X: Don't try to work around buggy devices with bInterval = 0 on isochronous...
Nathan Hjelm [Thu, 4 Apr 2013 20:51:03 +0000 (14:51 -0600)]
OS X: Don't try to work around buggy devices with bInterval = 0 on isochronous...

11 years agoCore: fix bug in libusb_has_capability()
Ludovic Rousseau [Thu, 4 Apr 2013 16:08:39 +0000 (18:08 +0200)]
Core: fix bug in libusb_has_capability()

The problem was detected by clang on Mac OS X:
warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
note: use '&' for a bitwise operation

We want to use a bitwise operation and not a logical operation.

11 years agolibusbx 1.0.15-rc2
Pete Batard [Wed, 3 Apr 2013 18:49:10 +0000 (19:49 +0100)]
libusbx 1.0.15-rc2

11 years agoOS X: Don't assume an interval of 1
Nathan Hjelm [Wed, 3 Apr 2013 10:12:47 +0000 (04:12 -0600)]
OS X: Don't assume an interval of 1

11 years agoWinCE: Fix missing sources from dist tarball
Toby Gray [Wed, 3 Apr 2013 11:02:50 +0000 (12:02 +0100)]
WinCE: Fix missing sources from dist tarball

11 years agolibusbx 1.0.15-rc1
Pete Batard [Tue, 2 Apr 2013 18:52:16 +0000 (19:52 +0100)]
libusbx 1.0.15-rc1

* Also fix missing header in release tarball

11 years agoCore: Add HID and kernel detach capability detection for all backends
Pete Batard [Sun, 17 Mar 2013 22:13:53 +0000 (22:13 +0000)]
Core: Add HID and kernel detach capability detection for all backends

* Also remove Linux special case from xusb sample.
* Note that LIBUSBX_API_VERSION is incremented as a result of
  libusb_has_capability() returning nonzero rather than 1 when
  a capability is supported.
* A LIBUSB_CAP_HAS_HOTPLUG is also added, though it is currently
  not implemented on any platform
* Closes #102

11 years agoWindows: Fix USB descriptor creation code for HID devices
Toby Gray [Fri, 22 Mar 2013 13:50:01 +0000 (13:50 +0000)]
Windows: Fix USB descriptor creation code for HID devices

* Prior to this fix the location for the next endpoint structure was
  obtained by using ed++. This doesn't work as
  sizeof(libusb_endpoint_descriptor) is greater than
  LIBUSB_DT_ENDPOINT_SIZE due to extra members.
* Closes #110

11 years agoSamples: More fxload improvements and cleanup
Pete Batard [Sat, 16 Mar 2013 23:36:47 +0000 (23:36 +0000)]
Samples: More fxload improvements and cleanup

* Type detection when bus,addr is specified
* Improved check for FX3 image header
* Switch back to using -p for bus,addr and use a comma to
  match the output of listdevs

11 years agoTests: Suppress Visual Studio 2012 Code Analysis warnings
Pete Batard [Sat, 16 Mar 2013 16:39:14 +0000 (16:39 +0000)]
Tests: Suppress Visual Studio 2012 Code Analysis warnings

11 years agoSamples: fix compiler warning
Ludovic Rousseau [Fri, 15 Mar 2013 08:35:16 +0000 (09:35 +0100)]
Samples: fix compiler warning

Make fx3_load_ram a static function.

ezusb.c:534: warning: no previous prototype for ‘fx3_load_ram’

11 years agoSamples: fxload improvements and cleanup
Federico Manzan [Sun, 10 Mar 2013 07:56:56 +0000 (07:56 +0000)]
Samples: fxload improvements and cleanup

* fix C++ compilation
* use stdint types
* allow to specify bus:address as a parameter
* allocate a buffer when transferring FX3 image and check for R/W errors

11 years agoOS X: Do not assume HS when submitting isochronous transfer
Vegard Storheil Eriksen [Tue, 12 Mar 2013 15:30:05 +0000 (23:30 +0800)]
OS X: Do not assume HS when submitting isochronous transfer

* Closes #99

11 years agoOS X: Use kCFNumberSInt32Type when reading device location from the IO registry
Nathan Hjelm [Tue, 12 Mar 2013 15:09:07 +0000 (23:09 +0800)]
OS X: Use kCFNumberSInt32Type when reading device location from the IO registry

* Closes #97