diff options
author | Pete Batard <pete@akeo.ie> | 2013-03-17 22:13:53 +0000 |
---|---|---|
committer | Pete Batard <pete@akeo.ie> | 2013-04-02 19:13:47 +0100 |
commit | e0628c566a3fb928baab9e4359760cdc5afe5e95 (patch) | |
tree | a3848aefed49a67f85ee9640ac7746f81f962b16 /examples | |
parent | f00edf639d3c1272744ab37f07f96fea5246976d (diff) | |
download | libusb-e0628c566a3fb928baab9e4359760cdc5afe5e95.tar.gz |
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
Diffstat (limited to 'examples')
-rw-r--r-- | examples/xusb.c | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/examples/xusb.c b/examples/xusb.c index 463cc2f..856f723 100644 --- a/examples/xusb.c +++ b/examples/xusb.c @@ -735,10 +735,8 @@ static int test_device(uint16_t vid, uint16_t pid) const struct libusb_endpoint_descriptor *endpoint; int i, j, k, r; int iface, nb_ifaces, first_iface = -1; -#if defined(__linux__) - // Attaching/detaching the kernel driver is only relevant for Linux + // For attaching/detaching the kernel driver, if needed int iface_detached = -1; -#endif struct libusb_device_descriptor dev_desc; const char* speed_name[5] = { "Unknown", "1.5 Mbit/s (USB LowSpeed)", "12 Mbit/s (USB FullSpeed)", "480 Mbit/s (USB HighSpeed)", "5000 Mbit/s (USB SuperSpeed)"}; @@ -833,16 +831,17 @@ static int test_device(uint16_t vid, uint16_t pid) { printf("\nClaiming interface %d...\n", iface); r = libusb_claim_interface(handle, iface); -#if defined(__linux__) - if ((r != LIBUSB_SUCCESS) && (iface == 0)) { - // Maybe we need to detach the driver - perr(" Failed. Trying to detach driver...\n"); - libusb_detach_kernel_driver(handle, iface); - iface_detached = iface; - printf(" Claiming interface again...\n"); - r = libusb_claim_interface(handle, iface); + if ((r != LIBUSB_SUCCESS) && libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER) + && (libusb_kernel_driver_active(handle, iface) > 0)) { + // Try to detach the kernel driver + perr(" A kernel driver is active, trying to detach it...\n"); + r = libusb_detach_kernel_driver(handle, iface); + if (r == LIBUSB_SUCCESS) { + iface_detached = iface; + printf(" Claiming interface again...\n"); + r = libusb_claim_interface(handle, iface); + } } -#endif if (r != LIBUSB_SUCCESS) { perr(" Failed.\n"); } @@ -891,12 +890,10 @@ static int test_device(uint16_t vid, uint16_t pid) libusb_release_interface(handle, iface); } -#if defined(__linux__) if (iface_detached >= 0) { printf("Re-attaching kernel driver...\n"); libusb_attach_kernel_driver(handle, iface_detached); } -#endif printf("Closing device...\n"); libusb_close(handle); |