summaryrefslogtreecommitdiff
Commit message (Collapse)AuthorAgeFilesLines
...
* linux: Switch to kernel version check to determine if sysfs can relate devsHans de Goede2013-05-242-67/+25
| | | | | | | | | | | | 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>
* linux: Switch to kernel version check to determine if sysfs has descriptorsHans de Goede2013-05-232-23/+25
| | | | | | | | | | | | | | 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>
* linux: Clear descriptor buffer before read when using usbfsHans de Goede2013-05-232-1/+6
| | | | | | | | | | | | | 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>
* linux: Cache the entire descriptors at device-initHans de Goede2013-05-232-78/+50
| | | | | | | | | | | | | | | | | | | | | | | 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>
* linux: Add a _get_usbfs_fd helper functionHans de Goede2013-05-222-39/+35
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* usbi_alloc_device: Drop unnecessary memset 0Hans de Goede2013-05-222-2/+1
| | | | | | The memory gets calloc-ed, so there no need to clear it. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* linux: Fix host_endian handlingHans de Goede2013-05-182-7/+11
| | | | | | | | | | | | | | | | | | | -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>
* Examples: Fix use of deprecated libusb_get_port_pathHans de Goede2013-05-173-11/+7
| | | | | | While at it also simplify the path printing in listdevs Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* core: Add a new public libusb_get_port_numbers functionHans de Goede2013-05-173-10/+34
| | | | | | | | | | | | 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>
* core: Fix handle_events return code on hotplug pipe read errorHans de Goede2013-05-172-2/+2
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* usbi_handle_disconnect: Fix race condition leading to double completionHans de Goede2013-05-163-16/+28
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* usbi_handle_disconnect: Add some debugging wrt cancelled transfersHans de Goede2013-05-162-1/+4
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* linux: linux_get_parent_info: properly propagate malloc errorsHans de Goede2013-05-162-6/+10
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* listdevs: Also print portpath for non root hubsHans de Goede2013-05-162-3/+16
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* All: ref dev->parent_devHans de Goede2013-05-165-13/+6
| | | | | | | | | | | | | | 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>
* core: Improve error / debug messages for hotplug pipe handlingHans de Goede2013-05-162-1/+5
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* linux: Fixup init_count tracking on hotplug init errorHans de Goede2013-05-162-6/+9
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* linux_udev: linux_start_event_monitor: Properly cleanup on errorHans de Goede2013-05-162-5/+18
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* linux: Replace should-not-happen checks with assertsHans de Goede2013-05-163-23/+17
| | | | | | | 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>
* linux: Not finding a device on hotplug_disconnect is not an errorHans de Goede2013-05-162-3/+4
| | | | | | | | 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>
* linux: Improve topology related debug messagesHans de Goede2013-05-162-4/+6
| | | | | | And use usbi_dbg, rather then fprintf(stderr, ... Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* linux: Set device->port_numberHans de Goede2013-05-162-12/+13
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* linux: Deal with devices being enumerated before their parents areHans de Goede2013-05-162-2/+14
| | | | | | | | | | | | | | | | | | | | | | 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>
* linux: Move check for a device being added twice to linux_enumerate_deviceHans de Goede2013-05-162-7/+7
| | | | | | | | | | | | 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>
* linux: hotplug_enumerate and _disconnect take active_contexts_lockHans de Goede2013-05-162-1/+5
| | | | | | | | | 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>
* linux: Replace pthread_mutex with usbi_mutex_staticHans de Goede2013-05-162-11/+11
| | | | | | | 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>
* core: Fixup default_context handlingHans de Goede2013-05-162-10/+9
| | | | | | | | | | 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>
* core: Fixup cleanup on init errorHans de Goede2013-05-162-8/+11
| | | | | | | -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>
* core: Add ctx to the active context list before backend->initHans de Goede2013-05-162-10/+12
| | | | | | | | 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>
* linux: Take hotplug_lock for scan_devices, hotplug_enumerate and _disconnectHans de Goede2013-05-162-4/+8
| | | | | | | | | | | | 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>
* linux: linux_parent_dev search for . from the rightHans de Goede2013-05-162-3/+3
| | | | | | | 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>
* configure: Fix --enable-udev help textHans de Goede2013-05-162-2/+2
| | | | | | | 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>
* linux: fix topologyNathan Hjelm2013-05-162-70/+52
| | | | | | | | | | | 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>
* darwin: fix topologyNathan Hjelm2013-05-162-15/+2
| | | | | | | | | | | 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>
* darwin: clean up extraneous includesNathan Hjelm2013-05-162-8/+2
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* Add hotplugtest to msvc project filesPete Batard2013-05-1622-5/+499
|
* Misc fixesPete Batard2013-05-164-7/+12
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* examples: Only check for HAVE_SIGACTION once in Makefile.amPeter Stuge2013-05-152-3/+1
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* Add INSTALL_WIN.txt eol=crlf to .gitattributesHans de Goede2013-05-152-1/+2
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* Add some editor meta-comments for proper tab usageNathan Hjelm2013-05-155-1/+5
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* Core: use C99 flexible array member when possibleSean McBride2013-05-152-4/+22
| | | | | | | 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>
* darwin: return LIBUSB_ERROR_NOT_FOUND when reading the active config ↵Nathan Hjelm2013-05-152-2/+2
| | | | | | | | descriptor when the device is not configured. fixes #172 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* darwin: clean up old codehjelmn@cs.unm.edu2013-05-153-53/+22
| | | | | | | | | 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>
* darwin: fix memory leaks in the darwin backend.Nathan Hjelm2013-05-152-3/+9
| | | | | | | | | 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>
* core: release the context device list when the context is deleted.Nathan Hjelm2013-05-152-4/+18
| | | | | | | | 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>
* darwin: don't assume an interval of 1 or high/super speedNathan Hjelm2013-05-152-4/+11
| | | | | | | | | 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>
* darwin: add support for the LIBUSB_TRANSFER_ADD_ZERO_PACKET transfer flag.Nathan Hjelm2013-05-152-9/+18
| | | | | | | | | | | | 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>
* darwin/libusb_reset: reenumerate device if the device descriptor or any ↵Nathan Hjelm2013-05-152-14/+61
| | | | | | | | | | | | | | | | | | | 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>
* darwin: dpriv->device will never be NULL for a valid device. remove ↵Nathan Hjelm2013-05-152-44/+3
| | | | | | unnecessary code that handled this case Signed-off-by: Hans de Goede <hdegoede@redhat.com>
* darwin: allow control messages on devices we can not exclusively openNathan Hjelm2013-05-152-48/+24
| | | | Signed-off-by: Hans de Goede <hdegoede@redhat.com>