From fc2b212758df6eef5d30899628ce7db40474d8eb Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Fri, 16 Apr 2010 10:47:37 +0100 Subject: better handling of attach/detach for Linux in xusb.c --- examples/xusb.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/examples/xusb.c b/examples/xusb.c index 661201b..ee98613 100644 --- a/examples/xusb.c +++ b/examples/xusb.c @@ -600,7 +600,8 @@ int test_device(uint16_t vid, uint16_t pid) const struct libusb_endpoint_descriptor *endpoint; int i, j, k, r; int iface, nb_ifaces, nb_strings; -#ifndef OS_WINDOWS +#ifdef OS_LINUX + // Attaching/detaching the kernel driver is only relevant for Linux int iface_detached = -1; #endif bool test_scsi = false; @@ -669,17 +670,18 @@ int test_device(uint16_t vid, uint16_t pid) { printf("\nClaiming interface %d...\n", iface); r = libusb_claim_interface(handle, iface); +#ifdef OS_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); + } +#endif if (r != LIBUSB_SUCCESS) { - if (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"); - libusb_claim_interface(handle, iface); - } else { - printf("failed.\n"); - } + perr(" Failed.\n"); } } #endif @@ -719,7 +721,7 @@ int test_device(uint16_t vid, uint16_t pid) libusb_release_interface(handle, iface); } -#ifndef OS_WINDOWS +#ifdef OS_LINUX if (iface_detached >= 0) { printf("Re-attaching kernel driver...\n"); libusb_attach_kernel_driver(handle, iface_detached); -- cgit v1.2.1