dbus: add missing dbus_connection_unref() for shared connection
[platform/upstream/libusb.git] / examples / xusb.c
index b1d338f..352a5d7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * xusb: Generic USB test program
- * Copyright © 2009-2012 Pete Batard <pete@akeo.ie>
+ * Copyright Â© 2009-2012 Pete Batard <pete@akeo.ie>
  * Contributions to Mass Storage by Alan Stern.
  *
  * This library is free software; you can redistribute it and/or
 #if defined(_WIN32)
 #define msleep(msecs) Sleep(msecs)
 #else
-#include <unistd.h>
-#define msleep(msecs) usleep(1000*msecs)
+#include <time.h>
+#define msleep(msecs) nanosleep(&(struct timespec){msecs / 1000, (msecs * 1000000) % 1000000000UL}, NULL);
 #endif
 
-#if !defined(_MSC_VER) || _MSC_VER<=1200
-#define sscanf_s sscanf
+#if defined(_MSC_VER)
+#define snprintf _snprintf
+#define putenv _putenv
 #endif
 
 #if !defined(bool)
 #define false (!true)
 #endif
 
-
-// Future versions of libusbx will use usb_interface instead of interface
+// Future versions of libusb will use usb_interface instead of interface
 // in libusb_config_descriptor => catter for that
 #define usb_interface interface
 
 // Global variables
-bool binary_dump = false;
-char binary_name[64];
+static bool binary_dump = false;
+static bool extra_info = false;
+static bool force_device_request = false;      // For WCID descriptor queries
+static const char* binary_name = NULL;
 
-inline static int perr(char const *format, ...)
+static int perr(char const *format, ...)
 {
        va_list args;
        int r;
@@ -68,7 +70,7 @@ inline static int perr(char const *format, ...)
        return r;
 }
 
-#define ERR_EXIT(errcode) do { perr("   %s\n", libusb_error_name((enum libusb_error)errcode)); return -1; } while (0)
+#define ERR_EXIT(errcode) do { perr("   %s\n", libusb_strerror((enum libusb_error)errcode)); return -1; } while (0)
 #define CALL_CHECK(fcall) do { r=fcall; if (r < 0) ERR_EXIT(r); } while (0);
 #define B(x) (((x)!=0)?1:0)
 #define be_to_int32(buf) (((buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<8)|(buf)[3])
@@ -80,9 +82,14 @@ inline static int perr(char const *format, ...)
 
 // HID Class-Specific Requests values. See section 7.2 of the HID specifications
 #define HID_GET_REPORT                0x01
+#define HID_GET_IDLE                  0x02
+#define HID_GET_PROTOCOL              0x03
 #define HID_SET_REPORT                0x09
+#define HID_SET_IDLE                  0x0A
+#define HID_SET_PROTOCOL              0x0B
 #define HID_REPORT_TYPE_INPUT         0x01
 #define HID_REPORT_TYPE_OUTPUT        0x02
+#define HID_REPORT_TYPE_FEATURE       0x03
 
 // Mass Storage Requests values. See section 3 of the Bulk-Only Mass Storage Class specifications
 #define BOMS_RESET                    0xFF
@@ -127,15 +134,16 @@ static uint8_t cdb_length[256] = {
        00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,  //  F
 };
 
-enum test_type {
+static enum test_type {
        USE_GENERIC,
        USE_PS3,
        USE_XBOX,
        USE_SCSI,
+       USE_HID,
 } test_mode;
-uint16_t VID, PID;
+static uint16_t VID, PID;
 
-void display_buffer_hex(unsigned char *buffer, unsigned size)
+static void display_buffer_hex(unsigned char *buffer, unsigned size)
 {
        unsigned i, j, k;
 
@@ -163,9 +171,19 @@ void display_buffer_hex(unsigned char *buffer, unsigned size)
        printf("\n" );
 }
 
+static char* uuid_to_string(const uint8_t* uuid)
+{
+       static char uuid_string[40];
+       if (uuid == NULL) return NULL;
+       sprintf(uuid_string, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
+               uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
+               uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
+       return uuid_string;
+}
+
 // The PS3 Controller is really a HID device that got its HID Report Descriptors
 // removed by Sony
-int display_ps3_status(libusb_device_handle *handle)
+static int display_ps3_status(libusb_device_handle *handle)
 {
        int r;
        uint8_t input_report[49];
@@ -256,7 +274,7 @@ int display_ps3_status(libusb_device_handle *handle)
 // The XBOX Controller is really a HID device that got its HID Report Descriptors
 // removed by Microsoft.
 // Input/Output reports described at http://euc.jp/periphs/xbox-controller.ja.html
-int display_xbox_status(libusb_device_handle *handle)
+static int display_xbox_status(libusb_device_handle *handle)
 {
        int r;
        uint8_t input_report[20];
@@ -277,7 +295,7 @@ int display_xbox_status(libusb_device_handle *handle)
        return 0;
 }
 
-int set_xbox_actuators(libusb_device_handle *handle, uint8_t left, uint8_t right)
+static int set_xbox_actuators(libusb_device_handle *handle, uint8_t left, uint8_t right)
 {
        int r;
        uint8_t output_report[6];
@@ -294,7 +312,7 @@ int set_xbox_actuators(libusb_device_handle *handle, uint8_t left, uint8_t right
        return 0;
 }
 
-int send_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint, uint8_t lun,
+static int send_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint, uint8_t lun,
        uint8_t *cdb, uint8_t direction, int data_length, uint32_t *ret_tag)
 {
        static uint32_t tag = 1;
@@ -342,7 +360,7 @@ int send_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint, ui
                i++;
        } while ((r == LIBUSB_ERROR_PIPE) && (i<RETRY_MAX));
        if (r != LIBUSB_SUCCESS) {
-               perr("   send_mass_storage_command: %s\n", libusb_error_name(r));
+               perr("   send_mass_storage_command: %s\n", libusb_strerror((enum libusb_error)r));
                return -1;
        }
 
@@ -350,7 +368,7 @@ int send_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint, ui
        return 0;
 }
 
-int get_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint32_t expected_tag)
+static int get_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint32_t expected_tag)
 {
        int i, r, size;
        struct command_status_wrapper csw;
@@ -366,7 +384,7 @@ int get_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint
                i++;
        } while ((r == LIBUSB_ERROR_PIPE) && (i<RETRY_MAX));
        if (r != LIBUSB_SUCCESS) {
-               perr("   get_mass_storage_status: %s\n", libusb_error_name(r));
+               perr("   get_mass_storage_status: %s\n", libusb_strerror((enum libusb_error)r));
                return -1;
        }
        if (size != 13) {
@@ -397,12 +415,13 @@ int get_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint
        return 0;
 }
 
-void get_sense(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
+static void get_sense(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
 {
        uint8_t cdb[16];        // SCSI Command Descriptor Block
        uint8_t sense[18];
        uint32_t expected_tag;
        int size;
+       int rc;
 
        // Request Sense
        printf("Request Sense:\n");
@@ -412,7 +431,12 @@ void get_sense(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoi
        cdb[4] = REQUEST_SENSE_LENGTH;
 
        send_mass_storage_command(handle, endpoint_out, 0, cdb, LIBUSB_ENDPOINT_IN, REQUEST_SENSE_LENGTH, &expected_tag);
-       libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&sense, REQUEST_SENSE_LENGTH, &size, 1000);
+       rc = libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&sense, REQUEST_SENSE_LENGTH, &size, 1000);
+       if (rc < 0)
+       {
+               printf("libusb_bulk_transfer failed: %s\n", libusb_error_name(rc));
+               return;
+       }
        printf("   received %d bytes\n", size);
 
        if ((sense[0] != 0x70) && (sense[0] != 0x71)) {
@@ -427,7 +451,7 @@ void get_sense(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoi
 }
 
 // Mass Storage device to test bulk transfers (non destructive test)
-int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
+static int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
 {
        int r, size;
        uint8_t lun;
@@ -448,7 +472,7 @@ int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t
        if (r == 0) {
                lun = 0;
        } else if (r < 0) {
-               perr("   Failed: %s", libusb_error_name((enum libusb_error)r));
+               perr("   Failed: %s", libusb_strerror((enum libusb_error)r));
        }
        printf("   Max LUN = %d\n", lun);
 
@@ -493,6 +517,7 @@ int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t
                get_sense(handle, endpoint_in, endpoint_out);
        }
 
+       // coverity[tainted_data]
        data = (unsigned char*) calloc(1, block_size);
        if (data == NULL) {
                perr("   unable to allocate data buffer\n");
@@ -525,8 +550,159 @@ int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t
        return 0;
 }
 
+// HID
+static int get_hid_record_size(uint8_t *hid_report_descriptor, int size, int type)
+{
+       uint8_t i, j = 0;
+       uint8_t offset;
+       int record_size[3] = {0, 0, 0};
+       int nb_bits = 0, nb_items = 0;
+       bool found_record_marker;
+
+       found_record_marker = false;
+       for (i = hid_report_descriptor[0]+1; i < size; i += offset) {
+               offset = (hid_report_descriptor[i]&0x03) + 1;
+               if (offset == 4)
+                       offset = 5;
+               switch (hid_report_descriptor[i] & 0xFC) {
+               case 0x74:      // bitsize
+                       nb_bits = hid_report_descriptor[i+1];
+                       break;
+               case 0x94:      // count
+                       nb_items = 0;
+                       for (j=1; j<offset; j++) {
+                               nb_items = ((uint32_t)hid_report_descriptor[i+j]) << (8*(j-1));
+                       }
+                       break;
+               case 0x80:      // input
+                       found_record_marker = true;
+                       j = 0;
+                       break;
+               case 0x90:      // output
+                       found_record_marker = true;
+                       j = 1;
+                       break;
+               case 0xb0:      // feature
+                       found_record_marker = true;
+                       j = 2;
+                       break;
+               case 0xC0:      // end of collection
+                       nb_items = 0;
+                       nb_bits = 0;
+                       break;
+               default:
+                       continue;
+               }
+               if (found_record_marker) {
+                       found_record_marker = false;
+                       record_size[j] += nb_items*nb_bits;
+               }
+       }
+       if ((type < HID_REPORT_TYPE_INPUT) || (type > HID_REPORT_TYPE_FEATURE)) {
+               return 0;
+       } else {
+               return (record_size[type - HID_REPORT_TYPE_INPUT]+7)/8;
+       }
+}
+
+static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
+{
+       int r, size, descriptor_size;
+       uint8_t hid_report_descriptor[256];
+       uint8_t *report_buffer;
+       FILE *fd;
+
+       printf("\nReading HID Report Descriptors:\n");
+       descriptor_size = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_STANDARD|LIBUSB_RECIPIENT_INTERFACE,
+               LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_DT_REPORT<<8, 0, hid_report_descriptor, sizeof(hid_report_descriptor), 1000);
+       if (descriptor_size < 0) {
+               printf("   Failed\n");
+               return -1;
+       }
+       display_buffer_hex(hid_report_descriptor, descriptor_size);
+       if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) {
+               if (fwrite(hid_report_descriptor, 1, descriptor_size, fd) != descriptor_size) {
+                       printf("   Error writing descriptor to file\n");
+               }
+               fclose(fd);
+       }
+
+       size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_FEATURE);
+       if (size <= 0) {
+               printf("\nSkipping Feature Report readout (None detected)\n");
+       } else {
+               report_buffer = (uint8_t*) calloc(size, 1);
+               if (report_buffer == NULL) {
+                       return -1;
+               }
+
+               printf("\nReading Feature Report (length %d)...\n", size);
+               r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
+                       HID_GET_REPORT, (HID_REPORT_TYPE_FEATURE<<8)|0, 0, report_buffer, (uint16_t)size, 5000);
+               if (r >= 0) {
+                       display_buffer_hex(report_buffer, size);
+               } else {
+                       switch(r) {
+                       case LIBUSB_ERROR_NOT_FOUND:
+                               printf("   No Feature Report available for this device\n");
+                               break;
+                       case LIBUSB_ERROR_PIPE:
+                               printf("   Detected stall - resetting pipe...\n");
+                               libusb_clear_halt(handle, 0);
+                               break;
+                       default:
+                               printf("   Error: %s\n", libusb_strerror((enum libusb_error)r));
+                               break;
+                       }
+               }
+               free(report_buffer);
+       }
+
+       size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_INPUT);
+       if (size <= 0) {
+               printf("\nSkipping Input Report readout (None detected)\n");
+       } else {
+               report_buffer = (uint8_t*) calloc(size, 1);
+               if (report_buffer == NULL) {
+                       return -1;
+               }
+
+               printf("\nReading Input Report (length %d)...\n", size);
+               r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
+                       HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x00, 0, report_buffer, (uint16_t)size, 5000);
+               if (r >= 0) {
+                       display_buffer_hex(report_buffer, size);
+               } else {
+                       switch(r) {
+                       case LIBUSB_ERROR_TIMEOUT:
+                               printf("   Timeout! Please make sure you act on the device within the 5 seconds allocated...\n");
+                               break;
+                       case LIBUSB_ERROR_PIPE:
+                               printf("   Detected stall - resetting pipe...\n");
+                               libusb_clear_halt(handle, 0);
+                               break;
+                       default:
+                               printf("   Error: %s\n", libusb_strerror((enum libusb_error)r));
+                               break;
+                       }
+               }
+
+               // Attempt a bulk read from endpoint 0 (this should just return a raw input report)
+               printf("\nTesting interrupt read using endpoint %02X...\n", endpoint_in);
+               r = libusb_interrupt_transfer(handle, endpoint_in, report_buffer, size, &size, 5000);
+               if (r >= 0) {
+                       display_buffer_hex(report_buffer, size);
+               } else {
+                       printf("   %s\n", libusb_strerror((enum libusb_error)r));
+               }
+
+               free(report_buffer);
+       }
+       return 0;
+}
+
 // Read the MS WinUSB Feature Descriptors, that are used on Windows 8 for automated driver installation
-void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uint8_t bRequest, int iface_number)
+static void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uint8_t bRequest, int iface_number)
 {
 #define MAX_OS_FD_LENGTH 256
        int i, r;
@@ -534,7 +710,7 @@ void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uint8_t bR
        uint32_t length;
        void* le_type_punning_IS_fine;
        struct {
-               char* desc;
+               const char* desc;
                uint8_t recipient;
                uint16_t index;
                uint16_t header_size;
@@ -544,21 +720,20 @@ void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uint8_t bR
        };
 
        if (iface_number < 0) return;
+       // WinUSB has a limitation that forces wIndex to the interface number when issuing
+       // an Interface Request. To work around that, we can force a Device Request for
+       // the Extended Properties, assuming the device answers both equally.
+       if (force_device_request)
+               os_fd[1].recipient = LIBUSB_RECIPIENT_DEVICE;
 
        for (i=0; i<2; i++) {
                printf("\nReading %s OS Feature Descriptor (wIndex = 0x%04d):\n", os_fd[i].desc, os_fd[i].index);
 
                // Read the header part
-               r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|LIBUSB_RECIPIENT_DEVICE),
-                       // NB: We should use os_fd[i].recipient instead of LIBUSB_RECIPIENT_DEVICE above, as
-                       // LIBUSB_RECIPIENT_INTERFACE should be used for the Extended Properties.
-                       // However, for Interface requests, the WinUSB DLL forces the low byte of wIndex
-                       // to the interface number, regardless of what you set it to, so we have to
-                       // fallback to Device and hope the firmware answers both equally.
-                       // See http://www.lvr.com/forum/index.php?topic=331
+               r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|os_fd[i].recipient),
                        bRequest, (uint16_t)(((iface_number)<< 8)|0x00), os_fd[i].index, os_desc, os_fd[i].header_size, 1000);
                if (r < os_fd[i].header_size) {
-                       perr("   Failed: %s", (r<0)?libusb_error_name((enum libusb_error)r):"header size is too small");
+                       perr("   Failed: %s", (r<0)?libusb_strerror((enum libusb_error)r):"header size is too small");
                        return;
                }
                le_type_punning_IS_fine = (void*)os_desc;
@@ -571,7 +746,7 @@ void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uint8_t bR
                r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|os_fd[i].recipient),
                        bRequest, (uint16_t)(((iface_number)<< 8)|0x00), os_fd[i].index, os_desc, (uint16_t)length, 1000);
                if (r < 0) {
-                       perr("   Failed: %s", libusb_error_name((enum libusb_error)r));
+                       perr("   Failed: %s", libusb_strerror((enum libusb_error)r));
                        return;
                } else {
                        display_buffer_hex(os_desc, r);
@@ -579,29 +754,63 @@ void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uint8_t bR
        }
 }
 
-int test_device(uint16_t vid, uint16_t pid)
+static void print_device_cap(struct libusb_bos_dev_capability_descriptor *dev_cap)
+{
+       switch(dev_cap->bDevCapabilityType) {
+       case LIBUSB_BT_USB_2_0_EXTENSION: {
+               struct libusb_usb_2_0_extension_descriptor *usb_2_0_ext = NULL;
+               libusb_get_usb_2_0_extension_descriptor(NULL, dev_cap, &usb_2_0_ext);
+               if (usb_2_0_ext) {
+                       printf("    USB 2.0 extension:\n");
+                       printf("      attributes             : %02X\n", usb_2_0_ext->bmAttributes);
+                       libusb_free_usb_2_0_extension_descriptor(usb_2_0_ext);
+               }
+               break;
+       }
+       case LIBUSB_BT_SS_USB_DEVICE_CAPABILITY: {
+               struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap = NULL;
+               libusb_get_ss_usb_device_capability_descriptor(NULL, dev_cap, &ss_usb_device_cap);
+               if (ss_usb_device_cap) {
+                       printf("    USB 3.0 capabilities:\n");
+                       printf("      attributes             : %02X\n", ss_usb_device_cap->bmAttributes);
+                       printf("      supported speeds       : %04X\n", ss_usb_device_cap->wSpeedSupported);
+                       printf("      supported functionality: %02X\n", ss_usb_device_cap->bFunctionalitySupport);
+                       libusb_free_ss_usb_device_capability_descriptor(ss_usb_device_cap);
+               }
+               break;
+       }
+       case LIBUSB_BT_CONTAINER_ID: {
+               struct libusb_container_id_descriptor *container_id = NULL;
+               libusb_get_container_id_descriptor(NULL, dev_cap, &container_id);
+               if (container_id) {
+                       printf("    Container ID:\n      %s\n", uuid_to_string(container_id->ContainerID));
+                       libusb_free_container_id_descriptor(container_id);
+               }
+               break;
+       }
+       default:
+               printf("    Unknown BOS device capability %02x:\n", dev_cap->bDevCapabilityType);
+       }       
+}
+
+static int test_device(uint16_t vid, uint16_t pid)
 {
        libusb_device_handle *handle;
        libusb_device *dev;
-#ifdef HAS_GETPORTPATH
        uint8_t bus, port_path[8];
-#endif
+       struct libusb_bos_descriptor *bos_desc;
        struct libusb_config_descriptor *conf_desc;
        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
-       int iface_detached = -1;
-#endif
        struct libusb_device_descriptor dev_desc;
-       char* speed_name[5] = { "Unknown", "1.5 Mbit/s (USB 1.0 LowSpeed)", "12 Mbit/s (USB 1.0 FullSpeed)",
-               "480 Mbit/s (USB 2.0 HighSpeed)", "5000 Mbit/s (USB 3.0 SuperSpeed)"};
+       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)"};
        char string[128];
        uint8_t string_index[3];        // indexes of the string descriptors
        uint8_t endpoint_in = 0, endpoint_out = 0;      // default IN and OUT endpoints
 
-       printf("Opening device...\n");
+       printf("Opening device %04X:%04X...\n", vid, pid);
        handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
 
        if (handle == NULL) {
@@ -610,20 +819,22 @@ int test_device(uint16_t vid, uint16_t pid)
        }
 
        dev = libusb_get_device(handle);
-#ifdef HAS_GETPORTPATH
        bus = libusb_get_bus_number(dev);
-       r = libusb_get_port_path(NULL, dev, port_path, sizeof(port_path));
-       if (r > 0) {
-               printf("bus: %d, port path from HCD: %d", bus, port_path[0]);
-               for (i=1; i<r; i++) {
-                       printf("->%d", port_path[i]);
+       if (extra_info) {
+               r = libusb_get_port_numbers(dev, port_path, sizeof(port_path));
+               if (r > 0) {
+                       printf("\nDevice properties:\n");
+                       printf("        bus number: %d\n", bus);
+                       printf("         port path: %d", port_path[0]);
+                       for (i=1; i<r; i++) {
+                               printf("->%d", port_path[i]);
+                       }
+                       printf(" (from root hub)\n");
                }
-               printf("\n");
+               r = libusb_get_device_speed(dev);
+               if ((r<0) || (r>4)) r=0;
+               printf("             speed: %s\n", speed_name[r]);
        }
-#endif
-       r = libusb_get_device_speed(dev);
-       if ((r<0) || (r>4)) r=0;
-       printf("speed: %s\n", speed_name[r]);
 
        printf("\nReading device descriptor:\n");
        CALL_CHECK(libusb_get_device_descriptor(dev, &dev_desc));
@@ -639,7 +850,17 @@ int test_device(uint16_t vid, uint16_t pid)
        string_index[1] = dev_desc.iProduct;
        string_index[2] = dev_desc.iSerialNumber;
 
-       printf("\nReading configuration descriptors:\n");
+       printf("\nReading BOS descriptor: ");
+       if (libusb_get_bos_descriptor(handle, &bos_desc) == LIBUSB_SUCCESS) {
+               printf("%d caps\n", bos_desc->bNumDeviceCaps);
+               for (i = 0; i < bos_desc->bNumDeviceCaps; i++)
+                       print_device_cap(bos_desc->dev_capability[i]);
+               libusb_free_bos_descriptor(bos_desc);
+       } else {
+               printf("no descriptor\n");
+       }
+
+       printf("\nReading first configuration descriptor:\n");
        CALL_CHECK(libusb_get_config_descriptor(dev, 0, &conf_desc));
        nb_ifaces = conf_desc->bNumInterfaces;
        printf("             nb interfaces: %d\n", nb_ifaces);
@@ -663,10 +884,11 @@ int test_device(uint16_t vid, uint16_t pid)
                                test_mode = USE_SCSI;
                        }
                        for (k=0; k<conf_desc->usb_interface[i].altsetting[j].bNumEndpoints; k++) {
+                               struct libusb_ss_endpoint_companion_descriptor *ep_comp = NULL;
                                endpoint = &conf_desc->usb_interface[i].altsetting[j].endpoint[k];
                                printf("       endpoint[%d].address: %02X\n", k, endpoint->bEndpointAddress);
-                               // Use the first bulk IN/OUT endpoints found as default for testing
-                               if ((endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == LIBUSB_TRANSFER_TYPE_BULK) {
+                               // Use the first interrupt or bulk IN/OUT endpoints as default for testing
+                               if ((endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) & (LIBUSB_TRANSFER_TYPE_BULK | LIBUSB_TRANSFER_TYPE_INTERRUPT)) {
                                        if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
                                                if (!endpoint_in)
                                                        endpoint_in = endpoint->bEndpointAddress;
@@ -677,25 +899,22 @@ int test_device(uint16_t vid, uint16_t pid)
                                }
                                printf("           max packet size: %04X\n", endpoint->wMaxPacketSize);
                                printf("          polling interval: %02X\n", endpoint->bInterval);
+                               libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
+                               if (ep_comp) {
+                                       printf("                 max burst: %02X   (USB 3.0)\n", ep_comp->bMaxBurst);
+                                       printf("        bytes per interval: %04X (USB 3.0)\n", ep_comp->wBytesPerInterval);
+                                       libusb_free_ss_endpoint_companion_descriptor(ep_comp);
+                               }
                        }
                }
        }
        libusb_free_config_descriptor(conf_desc);
 
+       libusb_set_auto_detach_kernel_driver(handle, 1);
        for (iface = 0; iface < nb_ifaces; iface++)
        {
                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);
-               }
-#endif
                if (r != LIBUSB_SUCCESS) {
                        perr("   Failed.\n");
                }
@@ -729,9 +948,12 @@ int test_device(uint16_t vid, uint16_t pid)
                msleep(2000);
                CALL_CHECK(set_xbox_actuators(handle, 0, 0));
                break;
+       case USE_HID:
+               test_hid(handle, endpoint_in);
+               break;
        case USE_SCSI:
                CALL_CHECK(test_mass_storage(handle, endpoint_in, endpoint_out));
-       default:
+       case USE_GENERIC:
                break;
        }
 
@@ -741,13 +963,6 @@ 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);
 
@@ -758,13 +973,12 @@ int main(int argc, char** argv)
 {
        bool show_help = false;
        bool debug_mode = false;
-#ifdef HAS_GETVERSION
        const struct libusb_version* version;
-#endif
        int j, r;
        size_t i, arglen;
        unsigned tmp_vid, tmp_pid;
        uint16_t endian_test = 0xBE00;
+       char *error_lang = NULL, *old_dbg_str = NULL, str[256];
 
        // Default to generic, expecting VID:PID
        VID = 0;
@@ -786,15 +1000,26 @@ int main(int argc, char** argv)
                                case 'd':
                                        debug_mode = true;
                                        break;
+                               case 'i':
+                                       extra_info = true;
+                                       break;
+                               case 'w':
+                                       force_device_request = true;
+                                       break;
                                case 'b':
-                                       strcat(binary_name, "raw.bin");
-                                       if (j+1 < argc) {
-                                               strncpy(binary_name, argv[j+1], 64);
-                                               j++;
+                                       if ((j+1 >= argc) || (argv[j+1][0] == '-') || (argv[j+1][0] == '/')) {
+                                               printf("   Option -b requires a file name\n");
+                                               return 1;
                                        }
+                                       binary_name = argv[++j];
                                        binary_dump = true;
                                        break;
-                               case 'g':
+                               case 'l':
+                                       if ((j+1 >= argc) || (argv[j+1][0] == '-') || (argv[j+1][0] == '/')) {
+                                               printf("   Option -l requires an ISO 639-1 language parameter\n");
+                                               return 1;
+                                       }
+                                       error_lang = argv[++j];
                                        break;
                                case 'j':
                                        // OLIMEX ARM-USB-TINY JTAG, 2 channel composite device - 2 interfaces
@@ -817,6 +1042,12 @@ int main(int argc, char** argv)
                                        PID = 0x0268;
                                        test_mode = USE_PS3;
                                        break;
+                               case 's':
+                                       // Microsoft Sidewinder Precision Pro Joystick - 1 HID interface
+                                       VID = 0x045E;
+                                       PID = 0x0008;
+                                       test_mode = USE_HID;
+                                       break;
                                case 'x':
                                        // Microsoft XBox Controller Type S - 1 interface
                                        VID = 0x045E;
@@ -833,7 +1064,7 @@ int main(int argc, char** argv)
                                                break;
                                }
                                if (i != arglen) {
-                                       if (sscanf_s(argv[j], "%x:%x" , &tmp_vid, &tmp_pid) != 2) {
+                                       if (sscanf(argv[j], "%x:%x" , &tmp_vid, &tmp_pid) != 2) {
                                                printf("   Please specify VID & PID as \"vid:pid\" in hexadecimal format\n");
                                                return 1;
                                        }
@@ -847,32 +1078,53 @@ int main(int argc, char** argv)
        }
 
        if ((show_help) || (argc == 1) || (argc > 7)) {
-               printf("usage: %s [-d] [-b [file]] [-h] [-i] [-j] [-k] [-x] [vid:pid]\n", argv[0]);
-               printf("   -h: display usage\n");
-               printf("   -d: enable debug output (if library was compiled with debug enabled)\n");
-               printf("   -b: dump Mass Storage first block to binary file\n");
-               printf("   -g: short generic test (default)\n");
-               printf("   -k: test generic Mass Storage USB device (using WinUSB)\n");
-               printf("   -j: test FTDI based JTAG device (using WinUSB)\n");
-               printf("   -p: test Sony PS3 SixAxis controller (using WinUSB)\n");
-               printf("   -x: test Microsoft XBox Controller Type S (using WinUSB)\n");
+               printf("usage: %s [-h] [-d] [-i] [-k] [-b file] [-l lang] [-j] [-x] [-s] [-p] [-w] [vid:pid]\n", argv[0]);
+               printf("   -h      : display usage\n");
+               printf("   -d      : enable debug output\n");
+               printf("   -i      : print topology and speed info\n");
+               printf("   -j      : test composite FTDI based JTAG device\n");
+               printf("   -k      : test Mass Storage device\n");
+               printf("   -b file : dump Mass Storage data to file 'file'\n");
+               printf("   -p      : test Sony PS3 SixAxis controller\n");
+               printf("   -s      : test Microsoft Sidewinder Precision Pro (HID)\n");
+               printf("   -x      : test Microsoft XBox Controller Type S\n");
+               printf("   -l lang : language to report errors in (ISO 639-1)\n");
+               printf("   -w      : force the use of device requests when querying WCID descriptors\n");
+               printf("If only the vid:pid is provided, xusb attempts to run the most appropriate test\n");
                return 0;
        }
 
-#ifdef HAS_GETVERSION
-       version = libusb_getversion(); */
-       printf("Using libusbx v%d.%d.%d.%d\n\n", version->major, version->minor, version->micro, version->nano);
-#endif
+       // xusb is commonly used as a debug tool, so it's convenient to have debug output during libusb_init(),
+       // but since we can't call on libusb_set_debug() before libusb_init(), we use the env variable method
+       old_dbg_str = getenv("LIBUSB_DEBUG");
+       if (debug_mode) {
+               if (putenv("LIBUSB_DEBUG=4") != 0)      // LIBUSB_LOG_LEVEL_DEBUG
+                       printf("Unable to set debug level");
+       }
+
+       version = libusb_get_version();
+       printf("Using libusb v%d.%d.%d.%d\n\n", version->major, version->minor, version->micro, version->nano);
        r = libusb_init(NULL);
        if (r < 0)
                return r;
 
-       // Info = 3, Debug = 4
-       libusb_set_debug(NULL, debug_mode?4:3);
+       // If not set externally, and no debug option was given, use info log level
+       if ((old_dbg_str == NULL) && (!debug_mode))
+               libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_INFO);
+       if (error_lang != NULL) {
+               r = libusb_setlocale(error_lang);
+               if (r < 0)
+                       printf("Invalid or unsupported locale '%s': %s\n", error_lang, libusb_strerror((enum libusb_error)r));
+       }
 
        test_device(VID, PID);
 
        libusb_exit(NULL);
 
+       if (debug_mode) {
+               snprintf(str, sizeof(str), "LIBUSB_DEBUG=%s", (old_dbg_str == NULL)?"":old_dbg_str);
+               str[sizeof(str) - 1] = 0;       // Windows may not NUL terminate the string
+       }
+
        return 0;
 }