2 * xusb: Generic USB test program
3 * Copyright © 2009-2012 Pete Batard <pete@akeo.ie>
4 * Contributions to Mass Storage by Alan Stern.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #define msleep(msecs) Sleep(msecs)
33 #define msleep(msecs) usleep(1000*msecs)
36 #if !defined(_MSC_VER) || _MSC_VER<=1200
37 #define sscanf_s sscanf
51 // Future versions of libusbx will use usb_interface instead of interface
52 // in libusb_config_descriptor => catter for that
53 #define usb_interface interface
56 bool binary_dump = false;
57 char binary_name[64] = "raw.bin";
59 static int perr(char const *format, ...)
64 va_start (args, format);
65 r = vfprintf(stderr, format, args);
71 #define ERR_EXIT(errcode) do { perr(" %s\n", libusb_error_name((enum libusb_error)errcode)); return -1; } while (0)
72 #define CALL_CHECK(fcall) do { r=fcall; if (r < 0) ERR_EXIT(r); } while (0);
73 #define B(x) (((x)!=0)?1:0)
74 #define be_to_int32(buf) (((buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<8)|(buf)[3])
77 #define REQUEST_SENSE_LENGTH 0x12
78 #define INQUIRY_LENGTH 0x24
79 #define READ_CAPACITY_LENGTH 0x08
81 // HID Class-Specific Requests values. See section 7.2 of the HID specifications
82 #define HID_GET_REPORT 0x01
83 #define HID_GET_IDLE 0x02
84 #define HID_GET_PROTOCOL 0x03
85 #define HID_SET_REPORT 0x09
86 #define HID_SET_IDLE 0x0A
87 #define HID_SET_PROTOCOL 0x0B
88 #define HID_REPORT_TYPE_INPUT 0x01
89 #define HID_REPORT_TYPE_OUTPUT 0x02
90 #define HID_REPORT_TYPE_FEATURE 0x03
92 // Mass Storage Requests values. See section 3 of the Bulk-Only Mass Storage Class specifications
93 #define BOMS_RESET 0xFF
94 #define BOMS_GET_MAX_LUN 0xFE
96 // Section 5.1: Command Block Wrapper (CBW)
97 struct command_block_wrapper {
98 uint8_t dCBWSignature[4];
100 uint32_t dCBWDataTransferLength;
103 uint8_t bCBWCBLength;
107 // Section 5.2: Command Status Wrapper (CSW)
108 struct command_status_wrapper {
109 uint8_t dCSWSignature[4];
111 uint32_t dCSWDataResidue;
115 static uint8_t cdb_length[256] = {
116 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
117 06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06, // 0
118 06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06, // 1
119 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // 2
120 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // 3
121 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // 4
122 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // 5
123 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00, // 6
124 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00, // 7
125 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, // 8
126 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, // 9
127 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, // A
128 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, // B
129 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00, // C
130 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00, // D
131 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00, // E
132 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00, // F
144 static void display_buffer_hex(unsigned char *buffer, unsigned size)
148 for (i=0; i<size; i+=16) {
149 printf("\n %08x ", i);
150 for(j=0,k=0; k<16; j++,k++) {
152 printf("%02x", buffer[i+j]);
159 for(j=0,k=0; k<16; j++,k++) {
161 if ((buffer[i+j] < 32) || (buffer[i+j] > 126)) {
164 printf("%c", buffer[i+j]);
172 // The PS3 Controller is really a HID device that got its HID Report Descriptors
174 static int display_ps3_status(libusb_device_handle *handle)
177 uint8_t input_report[49];
178 uint8_t master_bt_address[8];
179 uint8_t device_bt_address[18];
181 // Get the controller's bluetooth address of its master device
182 CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
183 HID_GET_REPORT, 0x03f5, 0, master_bt_address, sizeof(master_bt_address), 100));
184 printf("\nMaster's bluetooth address: %02X:%02X:%02X:%02X:%02X:%02X\n", master_bt_address[2], master_bt_address[3],
185 master_bt_address[4], master_bt_address[5], master_bt_address[6], master_bt_address[7]);
187 // Get the controller's bluetooth address
188 CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
189 HID_GET_REPORT, 0x03f2, 0, device_bt_address, sizeof(device_bt_address), 100));
190 printf("\nMaster's bluetooth address: %02X:%02X:%02X:%02X:%02X:%02X\n", device_bt_address[4], device_bt_address[5],
191 device_bt_address[6], device_bt_address[7], device_bt_address[8], device_bt_address[9]);
193 // Get the status of the controller's buttons via its HID report
194 printf("\nReading PS3 Input Report...\n");
195 CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
196 HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x01, 0, input_report, sizeof(input_report), 1000));
197 switch(input_report[2]){ /** Direction pad plus start, select, and joystick buttons */
199 printf("\tSELECT pressed\n");
202 printf("\tLEFT 3 pressed\n");
205 printf("\tRIGHT 3 pressed\n");
208 printf("\tSTART presed\n");
211 printf("\tUP pressed\n");
214 printf("\tRIGHT pressed\n");
217 printf("\tDOWN pressed\n");
220 printf("\tLEFT pressed\n");
223 switch(input_report[3]){ /** Shapes plus top right and left buttons */
225 printf("\tLEFT 2 pressed\n");
228 printf("\tRIGHT 2 pressed\n");
231 printf("\tLEFT 1 pressed\n");
234 printf("\tRIGHT 1 presed\n");
237 printf("\tTRIANGLE pressed\n");
240 printf("\tCIRCLE pressed\n");
243 printf("\tCROSS pressed\n");
246 printf("\tSQUARE pressed\n");
249 printf("\tPS button: %d\n", input_report[4]);
250 printf("\tLeft Analog (X,Y): (%d,%d)\n", input_report[6], input_report[7]);
251 printf("\tRight Analog (X,Y): (%d,%d)\n", input_report[8], input_report[9]);
252 printf("\tL2 Value: %d\tR2 Value: %d\n", input_report[18], input_report[19]);
253 printf("\tL1 Value: %d\tR1 Value: %d\n", input_report[20], input_report[21]);
254 printf("\tRoll (x axis): %d Yaw (y axis): %d Pitch (z axis) %d\n",
255 //(((input_report[42] + 128) % 256) - 128),
256 (int8_t)(input_report[42]),
257 (int8_t)(input_report[44]),
258 (int8_t)(input_report[46]));
259 printf("\tAcceleration: %d\n\n", (int8_t)(input_report[48]));
262 // The XBOX Controller is really a HID device that got its HID Report Descriptors
263 // removed by Microsoft.
264 // Input/Output reports described at http://euc.jp/periphs/xbox-controller.ja.html
265 static int display_xbox_status(libusb_device_handle *handle)
268 uint8_t input_report[20];
269 printf("\nReading XBox Input Report...\n");
270 CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
271 HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x00, 0, input_report, 20, 1000));
272 printf(" D-pad: %02X\n", input_report[2]&0x0F);
273 printf(" Start:%d, Back:%d, Left Stick Press:%d, Right Stick Press:%d\n", B(input_report[2]&0x10), B(input_report[2]&0x20),
274 B(input_report[2]&0x40), B(input_report[2]&0x80));
275 // A, B, X, Y, Black, White are pressure sensitive
276 printf(" A:%d, B:%d, X:%d, Y:%d, White:%d, Black:%d\n", input_report[4], input_report[5],
277 input_report[6], input_report[7], input_report[9], input_report[8]);
278 printf(" Left Trigger: %d, Right Trigger: %d\n", input_report[10], input_report[11]);
279 printf(" Left Analog (X,Y): (%d,%d)\n", (int16_t)((input_report[13]<<8)|input_report[12]),
280 (int16_t)((input_report[15]<<8)|input_report[14]));
281 printf(" Right Analog (X,Y): (%d,%d)\n", (int16_t)((input_report[17]<<8)|input_report[16]),
282 (int16_t)((input_report[19]<<8)|input_report[18]));
286 static int set_xbox_actuators(libusb_device_handle *handle, uint8_t left, uint8_t right)
289 uint8_t output_report[6];
291 printf("\nWriting XBox Controller Output Report...\n");
293 memset(output_report, 0, sizeof(output_report));
294 output_report[1] = sizeof(output_report);
295 output_report[3] = left;
296 output_report[5] = right;
298 CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
299 HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT<<8)|0x00, 0, output_report, 06, 1000));
303 static int send_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint, uint8_t lun,
304 uint8_t *cdb, uint8_t direction, int data_length, uint32_t *ret_tag)
306 static uint32_t tag = 1;
309 struct command_block_wrapper cbw;
315 if (endpoint & LIBUSB_ENDPOINT_IN) {
316 perr("send_mass_storage_command: cannot send command on IN endpoint\n");
320 cdb_len = cdb_length[cdb[0]];
321 if ((cdb_len == 0) || (cdb_len > sizeof(cbw.CBWCB))) {
322 perr("send_mass_storage_command: don't know how to handle this command (%02X, length %d)\n",
327 memset(&cbw, 0, sizeof(cbw));
328 cbw.dCBWSignature[0] = 'U';
329 cbw.dCBWSignature[1] = 'S';
330 cbw.dCBWSignature[2] = 'B';
331 cbw.dCBWSignature[3] = 'C';
334 cbw.dCBWDataTransferLength = data_length;
335 cbw.bmCBWFlags = direction;
337 // Subclass is 1 or 6 => cdb_len
338 cbw.bCBWCBLength = cdb_len;
339 memcpy(cbw.CBWCB, cdb, cdb_len);
343 // The transfer length must always be exactly 31 bytes.
344 r = libusb_bulk_transfer(handle, endpoint, (unsigned char*)&cbw, 31, &size, 1000);
345 if (r == LIBUSB_ERROR_PIPE) {
346 libusb_clear_halt(handle, endpoint);
349 } while ((r == LIBUSB_ERROR_PIPE) && (i<RETRY_MAX));
350 if (r != LIBUSB_SUCCESS) {
351 perr(" send_mass_storage_command: %s\n", libusb_error_name(r));
355 printf(" sent %d CDB bytes\n", cdb_len);
359 static int get_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint32_t expected_tag)
362 struct command_status_wrapper csw;
364 // The device is allowed to STALL this transfer. If it does, you have to
365 // clear the stall and try again.
368 r = libusb_bulk_transfer(handle, endpoint, (unsigned char*)&csw, 13, &size, 1000);
369 if (r == LIBUSB_ERROR_PIPE) {
370 libusb_clear_halt(handle, endpoint);
373 } while ((r == LIBUSB_ERROR_PIPE) && (i<RETRY_MAX));
374 if (r != LIBUSB_SUCCESS) {
375 perr(" get_mass_storage_status: %s\n", libusb_error_name(r));
379 perr(" get_mass_storage_status: received %d bytes (expected 13)\n", size);
382 if (csw.dCSWTag != expected_tag) {
383 perr(" get_mass_storage_status: mismatched tags (expected %08X, received %08X)\n",
384 expected_tag, csw.dCSWTag);
387 // For this test, we ignore the dCSWSignature check for validity...
388 printf(" Mass Storage Status: %02X (%s)\n", csw.bCSWStatus, csw.bCSWStatus?"FAILED":"Success");
389 if (csw.dCSWTag != expected_tag)
391 if (csw.bCSWStatus) {
392 // REQUEST SENSE is appropriate only if bCSWStatus is 1, meaning that the
393 // command failed somehow. Larger values (2 in particular) mean that
394 // the command couldn't be understood.
395 if (csw.bCSWStatus == 1)
396 return -2; // request Get Sense
401 // In theory we also should check dCSWDataResidue. But lots of devices
406 static void get_sense(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
408 uint8_t cdb[16]; // SCSI Command Descriptor Block
410 uint32_t expected_tag;
414 printf("Request Sense:\n");
415 memset(sense, 0, sizeof(sense));
416 memset(cdb, 0, sizeof(cdb));
417 cdb[0] = 0x03; // Request Sense
418 cdb[4] = REQUEST_SENSE_LENGTH;
420 send_mass_storage_command(handle, endpoint_out, 0, cdb, LIBUSB_ENDPOINT_IN, REQUEST_SENSE_LENGTH, &expected_tag);
421 libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&sense, REQUEST_SENSE_LENGTH, &size, 1000);
422 printf(" received %d bytes\n", size);
424 if ((sense[0] != 0x70) && (sense[0] != 0x71)) {
425 perr(" ERROR No sense data\n");
427 perr(" ERROR Sense: %02X %02X %02X\n", sense[2]&0x0F, sense[12], sense[13]);
429 // Strictly speaking, the get_mass_storage_status() call should come
430 // before these perr() lines. If the status is nonzero then we must
431 // assume there's no data in the buffer. For xusb it doesn't matter.
432 get_mass_storage_status(handle, endpoint_in, expected_tag);
435 // Mass Storage device to test bulk transfers (non destructive test)
436 static int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
440 uint32_t expected_tag;
441 uint32_t i, max_lba, block_size;
443 uint8_t cdb[16]; // SCSI Command Descriptor Block
445 char vid[9], pid[9], rev[5];
449 printf("Reading Max LUN:\n");
450 r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
451 BOMS_GET_MAX_LUN, 0, 0, &lun, 1, 1000);
452 // Some devices send a STALL instead of the actual value.
453 // In such cases we should set lun to 0.
457 perr(" Failed: %s", libusb_error_name((enum libusb_error)r));
459 printf(" Max LUN = %d\n", lun);
462 printf("Sending Inquiry:\n");
463 memset(buffer, 0, sizeof(buffer));
464 memset(cdb, 0, sizeof(cdb));
465 cdb[0] = 0x12; // Inquiry
466 cdb[4] = INQUIRY_LENGTH;
468 send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, INQUIRY_LENGTH, &expected_tag);
469 CALL_CHECK(libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&buffer, INQUIRY_LENGTH, &size, 1000));
470 printf(" received %d bytes\n", size);
471 // The following strings are not zero terminated
472 for (i=0; i<8; i++) {
473 vid[i] = buffer[8+i];
474 pid[i] = buffer[16+i];
475 rev[i/2] = buffer[32+i/2]; // instead of another loop
480 printf(" VID:PID:REV \"%8s\":\"%8s\":\"%4s\"\n", vid, pid, rev);
481 if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
482 get_sense(handle, endpoint_in, endpoint_out);
486 printf("Reading Capacity:\n");
487 memset(buffer, 0, sizeof(buffer));
488 memset(cdb, 0, sizeof(cdb));
489 cdb[0] = 0x25; // Read Capacity
491 send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, READ_CAPACITY_LENGTH, &expected_tag);
492 CALL_CHECK(libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&buffer, READ_CAPACITY_LENGTH, &size, 1000));
493 printf(" received %d bytes\n", size);
494 max_lba = be_to_int32(&buffer[0]);
495 block_size = be_to_int32(&buffer[4]);
496 device_size = ((double)(max_lba+1))*block_size/(1024*1024*1024);
497 printf(" Max LBA: %08X, Block Size: %08X (%.2f GB)\n", max_lba, block_size, device_size);
498 if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
499 get_sense(handle, endpoint_in, endpoint_out);
502 data = (unsigned char*) calloc(1, block_size);
504 perr(" unable to allocate data buffer\n");
509 printf("Attempting to read %d bytes:\n", block_size);
510 memset(cdb, 0, sizeof(cdb));
512 cdb[0] = 0x28; // Read(10)
513 cdb[8] = 0x01; // 1 block
515 send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, block_size, &expected_tag);
516 libusb_bulk_transfer(handle, endpoint_in, data, block_size, &size, 5000);
517 printf(" READ: received %d bytes\n", size);
518 if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
519 get_sense(handle, endpoint_in, endpoint_out);
521 display_buffer_hex(data, size);
522 if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) {
523 if (fwrite(data, 1, (size_t)size, fd) != (unsigned int)size) {
524 perr(" unable to write binary data\n");
535 static int get_hid_record_size(uint8_t *hid_report_descriptor, int size, int type)
539 int record_size[3] = {0, 0, 0};
540 int nb_bits = 0, nb_items = 0;
541 bool found_record_marker;
543 found_record_marker = false;
544 for (i = hid_report_descriptor[0]+1; i < size; i += offset) {
545 offset = (hid_report_descriptor[i]&0x03) + 1;
548 switch (hid_report_descriptor[i] & 0xFC) {
549 case 0x74: // bitsize
550 nb_bits = hid_report_descriptor[i+1];
554 for (j=1; j<offset; j++) {
555 nb_items = ((uint32_t)hid_report_descriptor[i+j]) << (8*(j-1));
559 found_record_marker = true;
563 found_record_marker = true;
566 case 0xb0: // feature
567 found_record_marker = true;
570 case 0xC0: // end of collection
577 if (found_record_marker) {
578 found_record_marker = false;
579 record_size[j] += nb_items*nb_bits;
582 if ((type < HID_REPORT_TYPE_INPUT) || (type > HID_REPORT_TYPE_FEATURE)) {
585 return (record_size[type - HID_REPORT_TYPE_INPUT]+7)/8;
589 static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
591 int r, size, descriptor_size;
592 uint8_t hid_report_descriptor[256];
593 uint8_t *report_buffer;
596 printf("\nReading HID Report Descriptors:\n");
597 descriptor_size = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_STANDARD|LIBUSB_RECIPIENT_INTERFACE,
598 LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_DT_REPORT<<8, 0, hid_report_descriptor, sizeof(hid_report_descriptor), 1000);
599 if (descriptor_size < 0) {
603 display_buffer_hex(hid_report_descriptor, descriptor_size);
604 if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) {
605 if (fwrite(hid_report_descriptor, 1, descriptor_size, fd) != descriptor_size) {
606 printf(" Error writing descriptor to file\n");
611 size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_FEATURE);
613 printf("\nSkipping Feature Report readout (None detected)\n");
615 report_buffer = (uint8_t*) calloc(size, 1);
616 if (report_buffer == NULL) {
620 printf("\nReading Feature Report (length %d)...\n", size);
621 r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
622 HID_GET_REPORT, (HID_REPORT_TYPE_FEATURE<<8)|0, 0, report_buffer, (uint16_t)size, 5000);
624 display_buffer_hex(report_buffer, size);
627 case LIBUSB_ERROR_NOT_FOUND:
628 printf(" No Feature Report available for this device\n");
630 case LIBUSB_ERROR_PIPE:
631 printf(" Detected stall - resetting pipe...\n");
632 libusb_clear_halt(handle, 0);
635 printf(" Error: %s\n", libusb_error_name(r));
642 size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_INPUT);
644 printf("\nSkipping Input Report readout (None detected)\n");
646 report_buffer = (uint8_t*) calloc(size, 1);
647 if (report_buffer == NULL) {
651 printf("\nReading Input Report (length %d)...\n", size);
652 r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
653 HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x00, 0, report_buffer, (uint16_t)size, 5000);
655 display_buffer_hex(report_buffer, size);
658 case LIBUSB_ERROR_TIMEOUT:
659 printf(" Timeout! Please make sure you act on the device within the 5 seconds allocated...\n");
661 case LIBUSB_ERROR_PIPE:
662 printf(" Detected stall - resetting pipe...\n");
663 libusb_clear_halt(handle, 0);
666 printf(" Error: %s\n", libusb_error_name(r));
671 // Attempt a bulk read from endpoint 0 (this should just return a raw input report)
672 printf("\nTesting interrupt read using endpoint %02X...\n", endpoint_in);
673 r = libusb_interrupt_transfer(handle, endpoint_in, report_buffer, size, &size, 5000);
675 display_buffer_hex(report_buffer, size);
677 printf(" %s\n", libusb_error_name(r));
685 // Read the MS WinUSB Feature Descriptors, that are used on Windows 8 for automated driver installation
686 static void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uint8_t bRequest, int iface_number)
688 #define MAX_OS_FD_LENGTH 256
690 uint8_t os_desc[MAX_OS_FD_LENGTH];
692 void* le_type_punning_IS_fine;
696 uint16_t header_size;
698 {"Extended Compat ID", 0x0004, 0x10},
699 {"Extended Properties", 0x0005, 0x0A}
702 if (iface_number < 0) return;
704 for (i=0; i<2; i++) {
705 printf("\nReading %s OS Feature Descriptor (wIndex = 0x%04d):\n", os_fd[i].desc, os_fd[i].index);
707 // Read the header part
708 r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|LIBUSB_RECIPIENT_DEVICE),
709 bRequest, (uint16_t)(((iface_number)<< 8)|0x00), os_fd[i].index, os_desc, os_fd[i].header_size, 1000);
710 if (r < os_fd[i].header_size) {
711 perr(" Failed: %s", (r<0)?libusb_error_name((enum libusb_error)r):"header size is too small");
714 le_type_punning_IS_fine = (void*)os_desc;
715 length = *((uint32_t*)le_type_punning_IS_fine);
716 if (length > MAX_OS_FD_LENGTH) {
717 length = MAX_OS_FD_LENGTH;
720 // Read the full feature descriptor
721 r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|LIBUSB_RECIPIENT_DEVICE),
722 bRequest, (uint16_t)(((iface_number)<< 8)|0x00), os_fd[i].index, os_desc, (uint16_t)length, 1000);
724 perr(" Failed: %s", libusb_error_name((enum libusb_error)r));
727 display_buffer_hex(os_desc, r);
732 static int test_device(uint16_t vid, uint16_t pid)
734 libusb_device_handle *handle;
736 uint8_t bus, port_path[8];
737 struct libusb_config_descriptor *conf_desc;
738 const struct libusb_endpoint_descriptor *endpoint;
740 int iface, nb_ifaces, first_iface = -1;
741 #if defined(__linux__)
742 // Attaching/detaching the kernel driver is only relevant for Linux
743 int iface_detached = -1;
745 struct libusb_device_descriptor dev_desc;
746 const char* speed_name[5] = { "Unknown", "1.5 Mbit/s (USB LowSpeed)", "12 Mbit/s (USB FullSpeed)",
747 "480 Mbit/s (USB HighSpeed)", "5000 Mbit/s (USB SuperSpeed)"};
749 uint8_t string_index[3]; // indexes of the string descriptors
750 uint8_t endpoint_in = 0, endpoint_out = 0; // default IN and OUT endpoints
752 printf("Opening device...\n");
753 handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
755 if (handle == NULL) {
760 dev = libusb_get_device(handle);
761 bus = libusb_get_bus_number(dev);
762 r = libusb_get_port_path(NULL, dev, port_path, sizeof(port_path));
764 printf("bus: %d, port path from HCD: %d", bus, port_path[0]);
765 for (i=1; i<r; i++) {
766 printf("->%d", port_path[i]);
770 r = libusb_get_device_speed(dev);
771 if ((r<0) || (r>4)) r=0;
772 printf("speed: %s\n", speed_name[r]);
774 printf("\nReading device descriptor:\n");
775 CALL_CHECK(libusb_get_device_descriptor(dev, &dev_desc));
776 printf(" length: %d\n", dev_desc.bLength);
777 printf(" device class: %d\n", dev_desc.bDeviceClass);
778 printf(" S/N: %d\n", dev_desc.iSerialNumber);
779 printf(" VID:PID: %04X:%04X\n", dev_desc.idVendor, dev_desc.idProduct);
780 printf(" bcdDevice: %04X\n", dev_desc.bcdDevice);
781 printf(" iMan:iProd:iSer: %d:%d:%d\n", dev_desc.iManufacturer, dev_desc.iProduct, dev_desc.iSerialNumber);
782 printf(" nb confs: %d\n", dev_desc.bNumConfigurations);
783 // Copy the string descriptors for easier parsing
784 string_index[0] = dev_desc.iManufacturer;
785 string_index[1] = dev_desc.iProduct;
786 string_index[2] = dev_desc.iSerialNumber;
788 printf("\nReading configuration descriptors:\n");
789 CALL_CHECK(libusb_get_config_descriptor(dev, 0, &conf_desc));
790 nb_ifaces = conf_desc->bNumInterfaces;
791 printf(" nb interfaces: %d\n", nb_ifaces);
793 first_iface = conf_desc->usb_interface[0].altsetting[0].bInterfaceNumber;
794 for (i=0; i<nb_ifaces; i++) {
795 printf(" interface[%d]: id = %d\n", i,
796 conf_desc->usb_interface[i].altsetting[0].bInterfaceNumber);
797 for (j=0; j<conf_desc->usb_interface[i].num_altsetting; j++) {
798 printf("interface[%d].altsetting[%d]: num endpoints = %d\n",
799 i, j, conf_desc->usb_interface[i].altsetting[j].bNumEndpoints);
800 printf(" Class.SubClass.Protocol: %02X.%02X.%02X\n",
801 conf_desc->usb_interface[i].altsetting[j].bInterfaceClass,
802 conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass,
803 conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol);
804 if ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE)
805 && ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x01)
806 || (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x06) )
807 && (conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol == 0x50) ) {
808 // Mass storage devices that can use basic SCSI commands
809 test_mode = USE_SCSI;
811 for (k=0; k<conf_desc->usb_interface[i].altsetting[j].bNumEndpoints; k++) {
812 endpoint = &conf_desc->usb_interface[i].altsetting[j].endpoint[k];
813 printf(" endpoint[%d].address: %02X\n", k, endpoint->bEndpointAddress);
814 // Use the first interrupt or bulk IN/OUT endpoints as default for testing
815 if ((endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) & (LIBUSB_TRANSFER_TYPE_BULK | LIBUSB_TRANSFER_TYPE_INTERRUPT)) {
816 if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
818 endpoint_in = endpoint->bEndpointAddress;
821 endpoint_out = endpoint->bEndpointAddress;
824 printf(" max packet size: %04X\n", endpoint->wMaxPacketSize);
825 printf(" polling interval: %02X\n", endpoint->bInterval);
829 libusb_free_config_descriptor(conf_desc);
831 for (iface = 0; iface < nb_ifaces; iface++)
833 printf("\nClaiming interface %d...\n", iface);
834 r = libusb_claim_interface(handle, iface);
835 #if defined(__linux__)
836 if ((r != LIBUSB_SUCCESS) && (iface == 0)) {
837 // Maybe we need to detach the driver
838 perr(" Failed. Trying to detach driver...\n");
839 libusb_detach_kernel_driver(handle, iface);
840 iface_detached = iface;
841 printf(" Claiming interface again...\n");
842 r = libusb_claim_interface(handle, iface);
845 if (r != LIBUSB_SUCCESS) {
850 printf("\nReading string descriptors:\n");
851 for (i=0; i<3; i++) {
852 if (string_index[i] == 0) {
855 if (libusb_get_string_descriptor_ascii(handle, string_index[i], (unsigned char*)string, 128) >= 0) {
856 printf(" String (0x%02X): \"%s\"\n", string_index[i], string);
859 // Read the OS String Descriptor
860 if (libusb_get_string_descriptor_ascii(handle, 0xEE, (unsigned char*)string, 128) >= 0) {
861 printf(" String (0x%02X): \"%s\"\n", 0xEE, string);
862 // If this is a Microsoft OS String Descriptor,
863 // attempt to read the WinUSB extended Feature Descriptors
864 if (strncmp(string, "MSFT100", 7) == 0)
865 read_ms_winsub_feature_descriptors(handle, string[7], first_iface);
870 CALL_CHECK(display_ps3_status(handle));
873 CALL_CHECK(display_xbox_status(handle));
874 CALL_CHECK(set_xbox_actuators(handle, 128, 222));
876 CALL_CHECK(set_xbox_actuators(handle, 0, 0));
879 test_hid(handle, endpoint_in);
882 CALL_CHECK(test_mass_storage(handle, endpoint_in, endpoint_out));
888 for (iface = 0; iface<nb_ifaces; iface++) {
889 printf("Releasing interface %d...\n", iface);
890 libusb_release_interface(handle, iface);
893 #if defined(__linux__)
894 if (iface_detached >= 0) {
895 printf("Re-attaching kernel driver...\n");
896 libusb_attach_kernel_driver(handle, iface_detached);
900 printf("Closing device...\n");
901 libusb_close(handle);
906 int main(int argc, char** argv)
908 bool show_help = false;
909 bool debug_mode = false;
910 const struct libusb_version* version;
913 unsigned tmp_vid, tmp_pid;
914 uint16_t endian_test = 0xBE00;
916 // Default to generic, expecting VID:PID
919 test_mode = USE_GENERIC;
921 if (((uint8_t*)&endian_test)[0] == 0xBE) {
922 printf("Despite their natural superiority for end users, big endian\n"
923 "CPUs are not supported with this program, sorry.\n");
928 for (j = 1; j<argc; j++) {
929 arglen = strlen(argv[j]);
930 if ( ((argv[j][0] == '-') || (argv[j][0] == '/'))
938 // WDK's OACR doesn't like strncpy...
939 for (i=0; (i<(sizeof(binary_name)-1)) && (argv[j+1][i] != 0); i++)
940 binary_name[i] = argv[j+1][i];
949 // OLIMEX ARM-USB-TINY JTAG, 2 channel composite device - 2 interfaces
956 // Generic 2 GB USB Key (SCSI Transparent/Bulk Only) - 1 interface
962 // The following tests will force VID:PID if already provided
964 // Sony PS3 Controller - 1 interface
970 // Microsoft Sidewinder Precision Pro Joystick - 1 HID interface
976 // Microsoft XBox Controller Type S - 1 interface
979 test_mode = USE_XBOX;
986 for (i=0; i<arglen; i++) {
987 if (argv[j][i] == ':')
991 if (sscanf_s(argv[j], "%x:%x" , &tmp_vid, &tmp_pid) != 2) {
992 printf(" Please specify VID & PID as \"vid:pid\" in hexadecimal format\n");
995 VID = (uint16_t)tmp_vid;
996 PID = (uint16_t)tmp_pid;
1004 if ((show_help) || (argc == 1) || (argc > 7)) {
1005 printf("usage: %s [-d] [-b [file]] [-h] [-i] [-j] [-k] [-x] [vid:pid]\n", argv[0]);
1006 printf(" -h: display usage\n");
1007 printf(" -d: enable debug output (if library was compiled with debug enabled)\n");
1008 printf(" -b: dump Mass Storage first block to binary file\n");
1009 printf(" -g: short generic test (default)\n");
1010 printf(" -k: test generic Mass Storage USB device (using WinUSB)\n");
1011 printf(" -j: test FTDI based JTAG device (using WinUSB)\n");
1012 printf(" -p: test Sony PS3 SixAxis controller (using WinUSB)\n");
1013 printf(" -s: test Microsoft Sidewinder Precision Pro (using HID)\n");
1014 printf(" -x: test Microsoft XBox Controller Type S (using WinUSB)\n");
1018 version = libusb_get_version();
1019 printf("Using libusbx v%d.%d.%d.%d\n\n", version->major, version->minor, version->micro, version->nano);
1020 r = libusb_init(NULL);
1024 libusb_set_debug(NULL, debug_mode?LIBUSB_LOG_LEVEL_DEBUG:LIBUSB_LOG_LEVEL_INFO);
1026 test_device(VID, PID);