stream pushing ok without access permission of /dev/video0
[rtmpclient.git] / app / src / main / jni / libusb / examples / xusb.c
1 /*
2  * xusb: Generic USB test program
3  * Copyright © 2009-2012 Pete Batard <pete@akeo.ie>
4  * Contributions to Mass Storage by Alan Stern.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26
27 #include "libusb.h"
28
29 #if defined(_WIN32)
30 #define msleep(msecs) Sleep(msecs)
31 #else
32 #include <unistd.h>
33 #define msleep(msecs) usleep(1000*msecs)
34 #endif
35
36 #if !defined(bool)
37 #define bool int
38 #endif
39 #if !defined(true)
40 #define true (1 == 1)
41 #endif
42 #if !defined(false)
43 #define false (!true)
44 #endif
45
46 // Future versions of libusb will use usb_interface instead of interface
47 // in libusb_config_descriptor => catter for that
48 #define usb_interface interface
49
50 // Global variables
51 static bool binary_dump = false;
52 static bool extra_info = false;
53 static bool force_device_request = false;       // For WCID descriptor queries
54 static const char* binary_name = NULL;
55
56 static int perr(char const *format, ...)
57 {
58         va_list args;
59         int r;
60
61         va_start (args, format);
62         r = vfprintf(stderr, format, args);
63         va_end(args);
64
65         return r;
66 }
67
68 #define ERR_EXIT(errcode) do { perr("   %s\n", libusb_strerror((enum libusb_error)errcode)); return -1; } while (0)
69 #define CALL_CHECK(fcall) do { r=fcall; if (r < 0) ERR_EXIT(r); } while (0);
70 #define B(x) (((x)!=0)?1:0)
71 #define be_to_int32(buf) (((buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<8)|(buf)[3])
72
73 #define RETRY_MAX                     5
74 #define REQUEST_SENSE_LENGTH          0x12
75 #define INQUIRY_LENGTH                0x24
76 #define READ_CAPACITY_LENGTH          0x08
77
78 // HID Class-Specific Requests values. See section 7.2 of the HID specifications
79 #define HID_GET_REPORT                0x01
80 #define HID_GET_IDLE                  0x02
81 #define HID_GET_PROTOCOL              0x03
82 #define HID_SET_REPORT                0x09
83 #define HID_SET_IDLE                  0x0A
84 #define HID_SET_PROTOCOL              0x0B
85 #define HID_REPORT_TYPE_INPUT         0x01
86 #define HID_REPORT_TYPE_OUTPUT        0x02
87 #define HID_REPORT_TYPE_FEATURE       0x03
88
89 // Mass Storage Requests values. See section 3 of the Bulk-Only Mass Storage Class specifications
90 #define BOMS_RESET                    0xFF
91 #define BOMS_GET_MAX_LUN              0xFE
92
93 // Section 5.1: Command Block Wrapper (CBW)
94 struct command_block_wrapper {
95         uint8_t dCBWSignature[4];
96         uint32_t dCBWTag;
97         uint32_t dCBWDataTransferLength;
98         uint8_t bmCBWFlags;
99         uint8_t bCBWLUN;
100         uint8_t bCBWCBLength;
101         uint8_t CBWCB[16];
102 };
103
104 // Section 5.2: Command Status Wrapper (CSW)
105 struct command_status_wrapper {
106         uint8_t dCSWSignature[4];
107         uint32_t dCSWTag;
108         uint32_t dCSWDataResidue;
109         uint8_t bCSWStatus;
110 };
111
112 static uint8_t cdb_length[256] = {
113 //       0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
114         06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,  //  0
115         06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,  //  1
116         10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,  //  2
117         10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,  //  3
118         10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,  //  4
119         10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,  //  5
120         00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,  //  6
121         00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,  //  7
122         16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,  //  8
123         16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,  //  9
124         12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,  //  A
125         12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,  //  B
126         00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,  //  C
127         00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,  //  D
128         00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,  //  E
129         00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,  //  F
130 };
131
132 static enum test_type {
133         USE_GENERIC,
134         USE_PS3,
135         USE_XBOX,
136         USE_SCSI,
137         USE_HID,
138 } test_mode;
139 static uint16_t VID, PID;
140
141 static void display_buffer_hex(unsigned char *buffer, unsigned size)
142 {
143         unsigned i, j, k;
144
145         for (i=0; i<size; i+=16) {
146                 printf("\n  %08x  ", i);
147                 for(j=0,k=0; k<16; j++,k++) {
148                         if (i+j < size) {
149                                 printf("%02x", buffer[i+j]);
150                         } else {
151                                 printf("  ");
152                         }
153                         printf(" ");
154                 }
155                 printf(" ");
156                 for(j=0,k=0; k<16; j++,k++) {
157                         if (i+j < size) {
158                                 if ((buffer[i+j] < 32) || (buffer[i+j] > 126)) {
159                                         printf(".");
160                                 } else {
161                                         printf("%c", buffer[i+j]);
162                                 }
163                         }
164                 }
165         }
166         printf("\n" );
167 }
168
169 static char* uuid_to_string(const uint8_t* uuid)
170 {
171         static char uuid_string[40];
172         if (uuid == NULL) return NULL;
173         sprintf(uuid_string, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
174                 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
175                 uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
176         return uuid_string;
177 }
178
179 // The PS3 Controller is really a HID device that got its HID Report Descriptors
180 // removed by Sony
181 static int display_ps3_status(libusb_device_handle *handle)
182 {
183         int r;
184         uint8_t input_report[49];
185         uint8_t master_bt_address[8];
186         uint8_t device_bt_address[18];
187
188         // Get the controller's bluetooth address of its master device
189         CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
190                 HID_GET_REPORT, 0x03f5, 0, master_bt_address, sizeof(master_bt_address), 100));
191         printf("\nMaster's bluetooth address: %02X:%02X:%02X:%02X:%02X:%02X\n", master_bt_address[2], master_bt_address[3],
192                 master_bt_address[4], master_bt_address[5], master_bt_address[6], master_bt_address[7]);
193
194         // Get the controller's bluetooth address
195         CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
196                 HID_GET_REPORT, 0x03f2, 0, device_bt_address, sizeof(device_bt_address), 100));
197         printf("\nMaster's bluetooth address: %02X:%02X:%02X:%02X:%02X:%02X\n", device_bt_address[4], device_bt_address[5],
198                 device_bt_address[6], device_bt_address[7], device_bt_address[8], device_bt_address[9]);
199
200         // Get the status of the controller's buttons via its HID report
201         printf("\nReading PS3 Input Report...\n");
202         CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
203                 HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x01, 0, input_report, sizeof(input_report), 1000));
204         switch(input_report[2]){        /** Direction pad plus start, select, and joystick buttons */
205                 case 0x01:
206                         printf("\tSELECT pressed\n");
207                         break;
208                 case 0x02:
209                         printf("\tLEFT 3 pressed\n");
210                         break;
211                 case 0x04:
212                         printf("\tRIGHT 3 pressed\n");
213                         break;
214                 case 0x08:
215                         printf("\tSTART presed\n");
216                         break;
217                 case 0x10:
218                         printf("\tUP pressed\n");
219                         break;
220                 case 0x20:
221                         printf("\tRIGHT pressed\n");
222                         break;
223                 case 0x40:
224                         printf("\tDOWN pressed\n");
225                         break;
226                 case 0x80:
227                         printf("\tLEFT pressed\n");
228                         break;
229         }
230         switch(input_report[3]){        /** Shapes plus top right and left buttons */
231                 case 0x01:
232                         printf("\tLEFT 2 pressed\n");
233                         break;
234                 case 0x02:
235                         printf("\tRIGHT 2 pressed\n");
236                         break;
237                 case 0x04:
238                         printf("\tLEFT 1 pressed\n");
239                         break;
240                 case 0x08:
241                         printf("\tRIGHT 1 presed\n");
242                         break;
243                 case 0x10:
244                         printf("\tTRIANGLE pressed\n");
245                         break;
246                 case 0x20:
247                         printf("\tCIRCLE pressed\n");
248                         break;
249                 case 0x40:
250                         printf("\tCROSS pressed\n");
251                         break;
252                 case 0x80:
253                         printf("\tSQUARE pressed\n");
254                         break;
255         }
256         printf("\tPS button: %d\n", input_report[4]);
257         printf("\tLeft Analog (X,Y): (%d,%d)\n", input_report[6], input_report[7]);
258         printf("\tRight Analog (X,Y): (%d,%d)\n", input_report[8], input_report[9]);
259         printf("\tL2 Value: %d\tR2 Value: %d\n", input_report[18], input_report[19]);
260         printf("\tL1 Value: %d\tR1 Value: %d\n", input_report[20], input_report[21]);
261         printf("\tRoll (x axis): %d Yaw (y axis): %d Pitch (z axis) %d\n",
262                         //(((input_report[42] + 128) % 256) - 128),
263                         (int8_t)(input_report[42]),
264                         (int8_t)(input_report[44]),
265                         (int8_t)(input_report[46]));
266         printf("\tAcceleration: %d\n\n", (int8_t)(input_report[48]));
267         return 0;
268 }
269 // The XBOX Controller is really a HID device that got its HID Report Descriptors
270 // removed by Microsoft.
271 // Input/Output reports described at http://euc.jp/periphs/xbox-controller.ja.html
272 static int display_xbox_status(libusb_device_handle *handle)
273 {
274         int r;
275         uint8_t input_report[20];
276         printf("\nReading XBox Input Report...\n");
277         CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
278                 HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x00, 0, input_report, 20, 1000));
279         printf("   D-pad: %02X\n", input_report[2]&0x0F);
280         printf("   Start:%d, Back:%d, Left Stick Press:%d, Right Stick Press:%d\n", B(input_report[2]&0x10), B(input_report[2]&0x20),
281                 B(input_report[2]&0x40), B(input_report[2]&0x80));
282         // A, B, X, Y, Black, White are pressure sensitive
283         printf("   A:%d, B:%d, X:%d, Y:%d, White:%d, Black:%d\n", input_report[4], input_report[5],
284                 input_report[6], input_report[7], input_report[9], input_report[8]);
285         printf("   Left Trigger: %d, Right Trigger: %d\n", input_report[10], input_report[11]);
286         printf("   Left Analog (X,Y): (%d,%d)\n", (int16_t)((input_report[13]<<8)|input_report[12]),
287                 (int16_t)((input_report[15]<<8)|input_report[14]));
288         printf("   Right Analog (X,Y): (%d,%d)\n", (int16_t)((input_report[17]<<8)|input_report[16]),
289                 (int16_t)((input_report[19]<<8)|input_report[18]));
290         return 0;
291 }
292
293 static int set_xbox_actuators(libusb_device_handle *handle, uint8_t left, uint8_t right)
294 {
295         int r;
296         uint8_t output_report[6];
297
298         printf("\nWriting XBox Controller Output Report...\n");
299
300         memset(output_report, 0, sizeof(output_report));
301         output_report[1] = sizeof(output_report);
302         output_report[3] = left;
303         output_report[5] = right;
304
305         CALL_CHECK(libusb_control_transfer(handle, LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
306                 HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT<<8)|0x00, 0, output_report, 06, 1000));
307         return 0;
308 }
309
310 static int send_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint, uint8_t lun,
311         uint8_t *cdb, uint8_t direction, int data_length, uint32_t *ret_tag)
312 {
313         static uint32_t tag = 1;
314         uint8_t cdb_len;
315         int i, r, size;
316         struct command_block_wrapper cbw;
317
318         if (cdb == NULL) {
319                 return -1;
320         }
321
322         if (endpoint & LIBUSB_ENDPOINT_IN) {
323                 perr("send_mass_storage_command: cannot send command on IN endpoint\n");
324                 return -1;
325         }
326
327         cdb_len = cdb_length[cdb[0]];
328         if ((cdb_len == 0) || (cdb_len > sizeof(cbw.CBWCB))) {
329                 perr("send_mass_storage_command: don't know how to handle this command (%02X, length %d)\n",
330                         cdb[0], cdb_len);
331                 return -1;
332         }
333
334         memset(&cbw, 0, sizeof(cbw));
335         cbw.dCBWSignature[0] = 'U';
336         cbw.dCBWSignature[1] = 'S';
337         cbw.dCBWSignature[2] = 'B';
338         cbw.dCBWSignature[3] = 'C';
339         *ret_tag = tag;
340         cbw.dCBWTag = tag++;
341         cbw.dCBWDataTransferLength = data_length;
342         cbw.bmCBWFlags = direction;
343         cbw.bCBWLUN = lun;
344         // Subclass is 1 or 6 => cdb_len
345         cbw.bCBWCBLength = cdb_len;
346         memcpy(cbw.CBWCB, cdb, cdb_len);
347
348         i = 0;
349         do {
350                 // The transfer length must always be exactly 31 bytes.
351                 r = libusb_bulk_transfer(handle, endpoint, (unsigned char*)&cbw, 31, &size, 1000);
352                 if (r == LIBUSB_ERROR_PIPE) {
353                         libusb_clear_halt(handle, endpoint);
354                 }
355                 i++;
356         } while ((r == LIBUSB_ERROR_PIPE) && (i<RETRY_MAX));
357         if (r != LIBUSB_SUCCESS) {
358                 perr("   send_mass_storage_command: %s\n", libusb_strerror((enum libusb_error)r));
359                 return -1;
360         }
361
362         printf("   sent %d CDB bytes\n", cdb_len);
363         return 0;
364 }
365
366 static int get_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint32_t expected_tag)
367 {
368         int i, r, size;
369         struct command_status_wrapper csw;
370
371         // The device is allowed to STALL this transfer. If it does, you have to
372         // clear the stall and try again.
373         i = 0;
374         do {
375                 r = libusb_bulk_transfer(handle, endpoint, (unsigned char*)&csw, 13, &size, 1000);
376                 if (r == LIBUSB_ERROR_PIPE) {
377                         libusb_clear_halt(handle, endpoint);
378                 }
379                 i++;
380         } while ((r == LIBUSB_ERROR_PIPE) && (i<RETRY_MAX));
381         if (r != LIBUSB_SUCCESS) {
382                 perr("   get_mass_storage_status: %s\n", libusb_strerror((enum libusb_error)r));
383                 return -1;
384         }
385         if (size != 13) {
386                 perr("   get_mass_storage_status: received %d bytes (expected 13)\n", size);
387                 return -1;
388         }
389         if (csw.dCSWTag != expected_tag) {
390                 perr("   get_mass_storage_status: mismatched tags (expected %08X, received %08X)\n",
391                         expected_tag, csw.dCSWTag);
392                 return -1;
393         }
394         // For this test, we ignore the dCSWSignature check for validity...
395         printf("   Mass Storage Status: %02X (%s)\n", csw.bCSWStatus, csw.bCSWStatus?"FAILED":"Success");
396         if (csw.dCSWTag != expected_tag)
397                 return -1;
398         if (csw.bCSWStatus) {
399                 // REQUEST SENSE is appropriate only if bCSWStatus is 1, meaning that the
400                 // command failed somehow.  Larger values (2 in particular) mean that
401                 // the command couldn't be understood.
402                 if (csw.bCSWStatus == 1)
403                         return -2;      // request Get Sense
404                 else
405                         return -1;
406         }
407
408         // In theory we also should check dCSWDataResidue.  But lots of devices
409         // set it wrongly.
410         return 0;
411 }
412
413 static void get_sense(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
414 {
415         uint8_t cdb[16];        // SCSI Command Descriptor Block
416         uint8_t sense[18];
417         uint32_t expected_tag;
418         int size;
419         int rc;
420
421         // Request Sense
422         printf("Request Sense:\n");
423         memset(sense, 0, sizeof(sense));
424         memset(cdb, 0, sizeof(cdb));
425         cdb[0] = 0x03;  // Request Sense
426         cdb[4] = REQUEST_SENSE_LENGTH;
427
428         send_mass_storage_command(handle, endpoint_out, 0, cdb, LIBUSB_ENDPOINT_IN, REQUEST_SENSE_LENGTH, &expected_tag);
429         rc = libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&sense, REQUEST_SENSE_LENGTH, &size, 1000);
430         if (rc < 0)
431         {
432                 printf("libusb_bulk_transfer failed: %s\n", libusb_error_name(rc));
433                 return;
434         }
435         printf("   received %d bytes\n", size);
436
437         if ((sense[0] != 0x70) && (sense[0] != 0x71)) {
438                 perr("   ERROR No sense data\n");
439         } else {
440                 perr("   ERROR Sense: %02X %02X %02X\n", sense[2]&0x0F, sense[12], sense[13]);
441         }
442         // Strictly speaking, the get_mass_storage_status() call should come
443         // before these perr() lines.  If the status is nonzero then we must
444         // assume there's no data in the buffer.  For xusb it doesn't matter.
445         get_mass_storage_status(handle, endpoint_in, expected_tag);
446 }
447
448 // Mass Storage device to test bulk transfers (non destructive test)
449 static int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
450 {
451         int r, size;
452         uint8_t lun;
453         uint32_t expected_tag;
454         uint32_t i, max_lba, block_size;
455         double device_size;
456         uint8_t cdb[16];        // SCSI Command Descriptor Block
457         uint8_t buffer[64];
458         char vid[9], pid[9], rev[5];
459         unsigned char *data;
460         FILE *fd;
461
462         printf("Reading Max LUN:\n");
463         r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
464                 BOMS_GET_MAX_LUN, 0, 0, &lun, 1, 1000);
465         // Some devices send a STALL instead of the actual value.
466         // In such cases we should set lun to 0.
467         if (r == 0) {
468                 lun = 0;
469         } else if (r < 0) {
470                 perr("   Failed: %s", libusb_strerror((enum libusb_error)r));
471         }
472         printf("   Max LUN = %d\n", lun);
473
474         // Send Inquiry
475         printf("Sending Inquiry:\n");
476         memset(buffer, 0, sizeof(buffer));
477         memset(cdb, 0, sizeof(cdb));
478         cdb[0] = 0x12;  // Inquiry
479         cdb[4] = INQUIRY_LENGTH;
480
481         send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, INQUIRY_LENGTH, &expected_tag);
482         CALL_CHECK(libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&buffer, INQUIRY_LENGTH, &size, 1000));
483         printf("   received %d bytes\n", size);
484         // The following strings are not zero terminated
485         for (i=0; i<8; i++) {
486                 vid[i] = buffer[8+i];
487                 pid[i] = buffer[16+i];
488                 rev[i/2] = buffer[32+i/2];      // instead of another loop
489         }
490         vid[8] = 0;
491         pid[8] = 0;
492         rev[4] = 0;
493         printf("   VID:PID:REV \"%8s\":\"%8s\":\"%4s\"\n", vid, pid, rev);
494         if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
495                 get_sense(handle, endpoint_in, endpoint_out);
496         }
497
498         // Read capacity
499         printf("Reading Capacity:\n");
500         memset(buffer, 0, sizeof(buffer));
501         memset(cdb, 0, sizeof(cdb));
502         cdb[0] = 0x25;  // Read Capacity
503
504         send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, READ_CAPACITY_LENGTH, &expected_tag);
505         CALL_CHECK(libusb_bulk_transfer(handle, endpoint_in, (unsigned char*)&buffer, READ_CAPACITY_LENGTH, &size, 1000));
506         printf("   received %d bytes\n", size);
507         max_lba = be_to_int32(&buffer[0]);
508         block_size = be_to_int32(&buffer[4]);
509         device_size = ((double)(max_lba+1))*block_size/(1024*1024*1024);
510         printf("   Max LBA: %08X, Block Size: %08X (%.2f GB)\n", max_lba, block_size, device_size);
511         if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
512                 get_sense(handle, endpoint_in, endpoint_out);
513         }
514
515         data = (unsigned char*) calloc(1, block_size);
516         if (data == NULL) {
517                 perr("   unable to allocate data buffer\n");
518                 return -1;
519         }
520
521         // Send Read
522         printf("Attempting to read %d bytes:\n", block_size);
523         memset(cdb, 0, sizeof(cdb));
524
525         cdb[0] = 0x28;  // Read(10)
526         cdb[8] = 0x01;  // 1 block
527
528         send_mass_storage_command(handle, endpoint_out, lun, cdb, LIBUSB_ENDPOINT_IN, block_size, &expected_tag);
529         libusb_bulk_transfer(handle, endpoint_in, data, block_size, &size, 5000);
530         printf("   READ: received %d bytes\n", size);
531         if (get_mass_storage_status(handle, endpoint_in, expected_tag) == -2) {
532                 get_sense(handle, endpoint_in, endpoint_out);
533         } else {
534                 display_buffer_hex(data, size);
535                 if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) {
536                         if (fwrite(data, 1, (size_t)size, fd) != (unsigned int)size) {
537                                 perr("   unable to write binary data\n");
538                         }
539                         fclose(fd);
540                 }
541         }
542         free(data);
543
544         return 0;
545 }
546
547 // HID
548 static int get_hid_record_size(uint8_t *hid_report_descriptor, int size, int type)
549 {
550         uint8_t i, j = 0;
551         uint8_t offset;
552         int record_size[3] = {0, 0, 0};
553         int nb_bits = 0, nb_items = 0;
554         bool found_record_marker;
555
556         found_record_marker = false;
557         for (i = hid_report_descriptor[0]+1; i < size; i += offset) {
558                 offset = (hid_report_descriptor[i]&0x03) + 1;
559                 if (offset == 4)
560                         offset = 5;
561                 switch (hid_report_descriptor[i] & 0xFC) {
562                 case 0x74:      // bitsize
563                         nb_bits = hid_report_descriptor[i+1];
564                         break;
565                 case 0x94:      // count
566                         nb_items = 0;
567                         for (j=1; j<offset; j++) {
568                                 nb_items = ((uint32_t)hid_report_descriptor[i+j]) << (8*(j-1));
569                         }
570                         break;
571                 case 0x80:      // input
572                         found_record_marker = true;
573                         j = 0;
574                         break;
575                 case 0x90:      // output
576                         found_record_marker = true;
577                         j = 1;
578                         break;
579                 case 0xb0:      // feature
580                         found_record_marker = true;
581                         j = 2;
582                         break;
583                 case 0xC0:      // end of collection
584                         nb_items = 0;
585                         nb_bits = 0;
586                         break;
587                 default:
588                         continue;
589                 }
590                 if (found_record_marker) {
591                         found_record_marker = false;
592                         record_size[j] += nb_items*nb_bits;
593                 }
594         }
595         if ((type < HID_REPORT_TYPE_INPUT) || (type > HID_REPORT_TYPE_FEATURE)) {
596                 return 0;
597         } else {
598                 return (record_size[type - HID_REPORT_TYPE_INPUT]+7)/8;
599         }
600 }
601
602 static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
603 {
604         int r, size, descriptor_size;
605         uint8_t hid_report_descriptor[256];
606         uint8_t *report_buffer;
607         FILE *fd;
608
609         printf("\nReading HID Report Descriptors:\n");
610         descriptor_size = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_STANDARD|LIBUSB_RECIPIENT_INTERFACE,
611                 LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_DT_REPORT<<8, 0, hid_report_descriptor, sizeof(hid_report_descriptor), 1000);
612         if (descriptor_size < 0) {
613                 printf("   Failed\n");
614                 return -1;
615         }
616         display_buffer_hex(hid_report_descriptor, descriptor_size);
617         if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) {
618                 if (fwrite(hid_report_descriptor, 1, descriptor_size, fd) != descriptor_size) {
619                         printf("   Error writing descriptor to file\n");
620                 }
621                 fclose(fd);
622         }
623
624         size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_FEATURE);
625         if (size <= 0) {
626                 printf("\nSkipping Feature Report readout (None detected)\n");
627         } else {
628                 report_buffer = (uint8_t*) calloc(size, 1);
629                 if (report_buffer == NULL) {
630                         return -1;
631                 }
632
633                 printf("\nReading Feature Report (length %d)...\n", size);
634                 r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
635                         HID_GET_REPORT, (HID_REPORT_TYPE_FEATURE<<8)|0, 0, report_buffer, (uint16_t)size, 5000);
636                 if (r >= 0) {
637                         display_buffer_hex(report_buffer, size);
638                 } else {
639                         switch(r) {
640                         case LIBUSB_ERROR_NOT_FOUND:
641                                 printf("   No Feature Report available for this device\n");
642                                 break;
643                         case LIBUSB_ERROR_PIPE:
644                                 printf("   Detected stall - resetting pipe...\n");
645                                 libusb_clear_halt(handle, 0);
646                                 break;
647                         default:
648                                 printf("   Error: %s\n", libusb_strerror((enum libusb_error)r));
649                                 break;
650                         }
651                 }
652                 free(report_buffer);
653         }
654
655         size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_INPUT);
656         if (size <= 0) {
657                 printf("\nSkipping Input Report readout (None detected)\n");
658         } else {
659                 report_buffer = (uint8_t*) calloc(size, 1);
660                 if (report_buffer == NULL) {
661                         return -1;
662                 }
663
664                 printf("\nReading Input Report (length %d)...\n", size);
665                 r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
666                         HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x00, 0, report_buffer, (uint16_t)size, 5000);
667                 if (r >= 0) {
668                         display_buffer_hex(report_buffer, size);
669                 } else {
670                         switch(r) {
671                         case LIBUSB_ERROR_TIMEOUT:
672                                 printf("   Timeout! Please make sure you act on the device within the 5 seconds allocated...\n");
673                                 break;
674                         case LIBUSB_ERROR_PIPE:
675                                 printf("   Detected stall - resetting pipe...\n");
676                                 libusb_clear_halt(handle, 0);
677                                 break;
678                         default:
679                                 printf("   Error: %s\n", libusb_strerror((enum libusb_error)r));
680                                 break;
681                         }
682                 }
683
684                 // Attempt a bulk read from endpoint 0 (this should just return a raw input report)
685                 printf("\nTesting interrupt read using endpoint %02X...\n", endpoint_in);
686                 r = libusb_interrupt_transfer(handle, endpoint_in, report_buffer, size, &size, 5000);
687                 if (r >= 0) {
688                         display_buffer_hex(report_buffer, size);
689                 } else {
690                         printf("   %s\n", libusb_strerror((enum libusb_error)r));
691                 }
692
693                 free(report_buffer);
694         }
695         return 0;
696 }
697
698 // Read the MS WinUSB Feature Descriptors, that are used on Windows 8 for automated driver installation
699 static void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uint8_t bRequest, int iface_number)
700 {
701 #define MAX_OS_FD_LENGTH 256
702         int i, r;
703         uint8_t os_desc[MAX_OS_FD_LENGTH];
704         uint32_t length;
705         void* le_type_punning_IS_fine;
706         struct {
707                 const char* desc;
708                 uint8_t recipient;
709                 uint16_t index;
710                 uint16_t header_size;
711         } os_fd[2] = {
712                 {"Extended Compat ID", LIBUSB_RECIPIENT_DEVICE, 0x0004, 0x10},
713                 {"Extended Properties", LIBUSB_RECIPIENT_INTERFACE, 0x0005, 0x0A}
714         };
715
716         if (iface_number < 0) return;
717         // WinUSB has a limitation that forces wIndex to the interface number when issuing
718         // an Interface Request. To work around that, we can force a Device Request for
719         // the Extended Properties, assuming the device answers both equally.
720         if (force_device_request)
721                 os_fd[1].recipient = LIBUSB_RECIPIENT_DEVICE;
722
723         for (i=0; i<2; i++) {
724                 printf("\nReading %s OS Feature Descriptor (wIndex = 0x%04d):\n", os_fd[i].desc, os_fd[i].index);
725
726                 // Read the header part
727                 r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|os_fd[i].recipient),
728                         bRequest, (uint16_t)(((iface_number)<< 8)|0x00), os_fd[i].index, os_desc, os_fd[i].header_size, 1000);
729                 if (r < os_fd[i].header_size) {
730                         perr("   Failed: %s", (r<0)?libusb_strerror((enum libusb_error)r):"header size is too small");
731                         return;
732                 }
733                 le_type_punning_IS_fine = (void*)os_desc;
734                 length = *((uint32_t*)le_type_punning_IS_fine);
735                 if (length > MAX_OS_FD_LENGTH) {
736                         length = MAX_OS_FD_LENGTH;
737                 }
738
739                 // Read the full feature descriptor
740                 r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|os_fd[i].recipient),
741                         bRequest, (uint16_t)(((iface_number)<< 8)|0x00), os_fd[i].index, os_desc, (uint16_t)length, 1000);
742                 if (r < 0) {
743                         perr("   Failed: %s", libusb_strerror((enum libusb_error)r));
744                         return;
745                 } else {
746                         display_buffer_hex(os_desc, r);
747                 }
748         }
749 }
750
751 static void print_device_cap(struct libusb_bos_dev_capability_descriptor *dev_cap)
752 {
753         switch(dev_cap->bDevCapabilityType) {
754         case LIBUSB_BT_USB_2_0_EXTENSION: {
755                 struct libusb_usb_2_0_extension_descriptor *usb_2_0_ext = NULL;
756                 libusb_get_usb_2_0_extension_descriptor(NULL, dev_cap, &usb_2_0_ext);
757                 if (usb_2_0_ext) {
758                         printf("    USB 2.0 extension:\n");
759                         printf("      attributes             : %02X\n", usb_2_0_ext->bmAttributes);
760                         libusb_free_usb_2_0_extension_descriptor(usb_2_0_ext);
761                 }
762                 break;
763         }
764         case LIBUSB_BT_SS_USB_DEVICE_CAPABILITY: {
765                 struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap = NULL;
766                 libusb_get_ss_usb_device_capability_descriptor(NULL, dev_cap, &ss_usb_device_cap);
767                 if (ss_usb_device_cap) {
768                         printf("    USB 3.0 capabilities:\n");
769                         printf("      attributes             : %02X\n", ss_usb_device_cap->bmAttributes);
770                         printf("      supported speeds       : %04X\n", ss_usb_device_cap->wSpeedSupported);
771                         printf("      supported functionality: %02X\n", ss_usb_device_cap->bFunctionalitySupport);
772                         libusb_free_ss_usb_device_capability_descriptor(ss_usb_device_cap);
773                 }
774                 break;
775         }
776         case LIBUSB_BT_CONTAINER_ID: {
777                 struct libusb_container_id_descriptor *container_id = NULL;
778                 libusb_get_container_id_descriptor(NULL, dev_cap, &container_id);
779                 if (container_id) {
780                         printf("    Container ID:\n      %s\n", uuid_to_string(container_id->ContainerID));
781                         libusb_free_container_id_descriptor(container_id);
782                 }
783                 break;
784         }
785         default:
786                 printf("    Unknown BOS device capability %02x:\n", dev_cap->bDevCapabilityType);
787         }       
788 }
789
790 static int test_device(uint16_t vid, uint16_t pid)
791 {
792         libusb_device_handle *handle;
793         libusb_device *dev;
794         uint8_t bus, port_path[8];
795         struct libusb_bos_descriptor *bos_desc;
796         struct libusb_config_descriptor *conf_desc;
797         const struct libusb_endpoint_descriptor *endpoint;
798         int i, j, k, r;
799         int iface, nb_ifaces, first_iface = -1;
800         struct libusb_device_descriptor dev_desc;
801         const char* speed_name[5] = { "Unknown", "1.5 Mbit/s (USB LowSpeed)", "12 Mbit/s (USB FullSpeed)",
802                 "480 Mbit/s (USB HighSpeed)", "5000 Mbit/s (USB SuperSpeed)"};
803         char string[128];
804         uint8_t string_index[3];        // indexes of the string descriptors
805         uint8_t endpoint_in = 0, endpoint_out = 0;      // default IN and OUT endpoints
806
807         printf("Opening device %04X:%04X...\n", vid, pid);
808         handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
809
810         if (handle == NULL) {
811                 perr("  Failed.\n");
812                 return -1;
813         }
814
815         dev = libusb_get_device(handle);
816         bus = libusb_get_bus_number(dev);
817         if (extra_info) {
818                 r = libusb_get_port_numbers(dev, port_path, sizeof(port_path));
819                 if (r > 0) {
820                         printf("\nDevice properties:\n");
821                         printf("        bus number: %d\n", bus);
822                         printf("         port path: %d", port_path[0]);
823                         for (i=1; i<r; i++) {
824                                 printf("->%d", port_path[i]);
825                         }
826                         printf(" (from root hub)\n");
827                 }
828                 r = libusb_get_device_speed(dev);
829                 if ((r<0) || (r>4)) r=0;
830                 printf("             speed: %s\n", speed_name[r]);
831         }
832
833         printf("\nReading device descriptor:\n");
834         CALL_CHECK(libusb_get_device_descriptor(dev, &dev_desc));
835         printf("            length: %d\n", dev_desc.bLength);
836         printf("      device class: %d\n", dev_desc.bDeviceClass);
837         printf("               S/N: %d\n", dev_desc.iSerialNumber);
838         printf("           VID:PID: %04X:%04X\n", dev_desc.idVendor, dev_desc.idProduct);
839         printf("         bcdDevice: %04X\n", dev_desc.bcdDevice);
840         printf("   iMan:iProd:iSer: %d:%d:%d\n", dev_desc.iManufacturer, dev_desc.iProduct, dev_desc.iSerialNumber);
841         printf("          nb confs: %d\n", dev_desc.bNumConfigurations);
842         // Copy the string descriptors for easier parsing
843         string_index[0] = dev_desc.iManufacturer;
844         string_index[1] = dev_desc.iProduct;
845         string_index[2] = dev_desc.iSerialNumber;
846
847         printf("\nReading BOS descriptor: ");
848         if (libusb_get_bos_descriptor(handle, &bos_desc) == LIBUSB_SUCCESS) {
849                 printf("%d caps\n", bos_desc->bNumDeviceCaps);
850                 for (i = 0; i < bos_desc->bNumDeviceCaps; i++)
851                         print_device_cap(bos_desc->dev_capability[i]);
852                 libusb_free_bos_descriptor(bos_desc);
853         } else {
854                 printf("no descriptor\n");
855         }
856
857         printf("\nReading first configuration descriptor:\n");
858         CALL_CHECK(libusb_get_config_descriptor(dev, 0, &conf_desc));
859         nb_ifaces = conf_desc->bNumInterfaces;
860         printf("             nb interfaces: %d\n", nb_ifaces);
861         if (nb_ifaces > 0)
862                 first_iface = conf_desc->usb_interface[0].altsetting[0].bInterfaceNumber;
863         for (i=0; i<nb_ifaces; i++) {
864                 printf("              interface[%d]: id = %d\n", i,
865                         conf_desc->usb_interface[i].altsetting[0].bInterfaceNumber);
866                 for (j=0; j<conf_desc->usb_interface[i].num_altsetting; j++) {
867                         printf("interface[%d].altsetting[%d]: num endpoints = %d\n",
868                                 i, j, conf_desc->usb_interface[i].altsetting[j].bNumEndpoints);
869                         printf("   Class.SubClass.Protocol: %02X.%02X.%02X\n",
870                                 conf_desc->usb_interface[i].altsetting[j].bInterfaceClass,
871                                 conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass,
872                                 conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol);
873                         if ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE)
874                           && ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x01)
875                           || (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x06) )
876                           && (conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol == 0x50) ) {
877                                 // Mass storage devices that can use basic SCSI commands
878                                 test_mode = USE_SCSI;
879                         }
880                         for (k=0; k<conf_desc->usb_interface[i].altsetting[j].bNumEndpoints; k++) {
881                                 struct libusb_ss_endpoint_companion_descriptor *ep_comp = NULL;
882                                 endpoint = &conf_desc->usb_interface[i].altsetting[j].endpoint[k];
883                                 printf("       endpoint[%d].address: %02X\n", k, endpoint->bEndpointAddress);
884                                 // Use the first interrupt or bulk IN/OUT endpoints as default for testing
885                                 if ((endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) & (LIBUSB_TRANSFER_TYPE_BULK | LIBUSB_TRANSFER_TYPE_INTERRUPT)) {
886                                         if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
887                                                 if (!endpoint_in)
888                                                         endpoint_in = endpoint->bEndpointAddress;
889                                         } else {
890                                                 if (!endpoint_out)
891                                                         endpoint_out = endpoint->bEndpointAddress;
892                                         }
893                                 }
894                                 printf("           max packet size: %04X\n", endpoint->wMaxPacketSize);
895                                 printf("          polling interval: %02X\n", endpoint->bInterval);
896                                 libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
897                                 if (ep_comp) {
898                                         printf("                 max burst: %02X   (USB 3.0)\n", ep_comp->bMaxBurst);
899                                         printf("        bytes per interval: %04X (USB 3.0)\n", ep_comp->wBytesPerInterval);
900                                         libusb_free_ss_endpoint_companion_descriptor(ep_comp);
901                                 }
902                         }
903                 }
904         }
905         libusb_free_config_descriptor(conf_desc);
906
907         libusb_set_auto_detach_kernel_driver(handle, 1);
908         for (iface = 0; iface < nb_ifaces; iface++)
909         {
910                 printf("\nClaiming interface %d...\n", iface);
911                 r = libusb_claim_interface(handle, iface);
912                 if (r != LIBUSB_SUCCESS) {
913                         perr("   Failed.\n");
914                 }
915         }
916
917         printf("\nReading string descriptors:\n");
918         for (i=0; i<3; i++) {
919                 if (string_index[i] == 0) {
920                         continue;
921                 }
922                 if (libusb_get_string_descriptor_ascii(handle, string_index[i], (unsigned char*)string, 128) >= 0) {
923                         printf("   String (0x%02X): \"%s\"\n", string_index[i], string);
924                 }
925         }
926         // Read the OS String Descriptor
927         if (libusb_get_string_descriptor_ascii(handle, 0xEE, (unsigned char*)string, 128) >= 0) {
928                 printf("   String (0x%02X): \"%s\"\n", 0xEE, string);
929                 // If this is a Microsoft OS String Descriptor,
930                 // attempt to read the WinUSB extended Feature Descriptors
931                 if (strncmp(string, "MSFT100", 7) == 0)
932                         read_ms_winsub_feature_descriptors(handle, string[7], first_iface);
933         }
934
935         switch(test_mode) {
936         case USE_PS3:
937                 CALL_CHECK(display_ps3_status(handle));
938                 break;
939         case USE_XBOX:
940                 CALL_CHECK(display_xbox_status(handle));
941                 CALL_CHECK(set_xbox_actuators(handle, 128, 222));
942                 msleep(2000);
943                 CALL_CHECK(set_xbox_actuators(handle, 0, 0));
944                 break;
945         case USE_HID:
946                 test_hid(handle, endpoint_in);
947                 break;
948         case USE_SCSI:
949                 CALL_CHECK(test_mass_storage(handle, endpoint_in, endpoint_out));
950         case USE_GENERIC:
951                 break;
952         }
953
954         printf("\n");
955         for (iface = 0; iface<nb_ifaces; iface++) {
956                 printf("Releasing interface %d...\n", iface);
957                 libusb_release_interface(handle, iface);
958         }
959
960         printf("Closing device...\n");
961         libusb_close(handle);
962
963         return 0;
964 }
965
966 int main(int argc, char** argv)
967 {
968         bool show_help = false;
969         bool debug_mode = false;
970         const struct libusb_version* version;
971         int j, r;
972         size_t i, arglen;
973         unsigned tmp_vid, tmp_pid;
974         uint16_t endian_test = 0xBE00;
975         char* error_lang = NULL;
976
977         // Default to generic, expecting VID:PID
978         VID = 0;
979         PID = 0;
980         test_mode = USE_GENERIC;
981
982         if (((uint8_t*)&endian_test)[0] == 0xBE) {
983                 printf("Despite their natural superiority for end users, big endian\n"
984                         "CPUs are not supported with this program, sorry.\n");
985                 return 0;
986         }
987
988         if (argc >= 2) {
989                 for (j = 1; j<argc; j++) {
990                         arglen = strlen(argv[j]);
991                         if ( ((argv[j][0] == '-') || (argv[j][0] == '/'))
992                           && (arglen >= 2) ) {
993                                 switch(argv[j][1]) {
994                                 case 'd':
995                                         debug_mode = true;
996                                         break;
997                                 case 'i':
998                                         extra_info = true;
999                                         break;
1000                                 case 'w':
1001                                         force_device_request = true;
1002                                         break;
1003                                 case 'b':
1004                                         if ((j+1 >= argc) || (argv[j+1][0] == '-') || (argv[j+1][0] == '/')) {
1005                                                 printf("   Option -b requires a file name\n");
1006                                                 return 1;
1007                                         }
1008                                         binary_name = argv[++j];
1009                                         binary_dump = true;
1010                                         break;
1011                                 case 'l':
1012                                         if ((j+1 >= argc) || (argv[j+1][0] == '-') || (argv[j+1][0] == '/')) {
1013                                                 printf("   Option -l requires an ISO 639-1 language parameter\n");
1014                                                 return 1;
1015                                         }
1016                                         error_lang = argv[++j];
1017                                         break;
1018                                 case 'j':
1019                                         // OLIMEX ARM-USB-TINY JTAG, 2 channel composite device - 2 interfaces
1020                                         if (!VID && !PID) {
1021                                                 VID = 0x15BA;
1022                                                 PID = 0x0004;
1023                                         }
1024                                         break;
1025                                 case 'k':
1026                                         // Generic 2 GB USB Key (SCSI Transparent/Bulk Only) - 1 interface
1027                                         if (!VID && !PID) {
1028                                                 VID = 0x0204;
1029                                                 PID = 0x6025;
1030                                         }
1031                                         break;
1032                                 // The following tests will force VID:PID if already provided
1033                                 case 'p':
1034                                         // Sony PS3 Controller - 1 interface
1035                                         VID = 0x054C;
1036                                         PID = 0x0268;
1037                                         test_mode = USE_PS3;
1038                                         break;
1039                                 case 's':
1040                                         // Microsoft Sidewinder Precision Pro Joystick - 1 HID interface
1041                                         VID = 0x045E;
1042                                         PID = 0x0008;
1043                                         test_mode = USE_HID;
1044                                         break;
1045                                 case 'x':
1046                                         // Microsoft XBox Controller Type S - 1 interface
1047                                         VID = 0x045E;
1048                                         PID = 0x0289;
1049                                         test_mode = USE_XBOX;
1050                                         break;
1051                                 default:
1052                                         show_help = true;
1053                                         break;
1054                                 }
1055                         } else {
1056                                 for (i=0; i<arglen; i++) {
1057                                         if (argv[j][i] == ':')
1058                                                 break;
1059                                 }
1060                                 if (i != arglen) {
1061                                         if (sscanf(argv[j], "%x:%x" , &tmp_vid, &tmp_pid) != 2) {
1062                                                 printf("   Please specify VID & PID as \"vid:pid\" in hexadecimal format\n");
1063                                                 return 1;
1064                                         }
1065                                         VID = (uint16_t)tmp_vid;
1066                                         PID = (uint16_t)tmp_pid;
1067                                 } else {
1068                                         show_help = true;
1069                                 }
1070                         }
1071                 }
1072         }
1073
1074         if ((show_help) || (argc == 1) || (argc > 7)) {
1075                 printf("usage: %s [-h] [-d] [-i] [-k] [-b file] [-l lang] [-j] [-x] [-s] [-p] [-w] [vid:pid]\n", argv[0]);
1076                 printf("   -h      : display usage\n");
1077                 printf("   -d      : enable debug output\n");
1078                 printf("   -i      : print topology and speed info\n");
1079                 printf("   -j      : test composite FTDI based JTAG device\n");
1080                 printf("   -k      : test Mass Storage device\n");
1081                 printf("   -b file : dump Mass Storage data to file 'file'\n");
1082                 printf("   -p      : test Sony PS3 SixAxis controller\n");
1083                 printf("   -s      : test Microsoft Sidewinder Precision Pro (HID)\n");
1084                 printf("   -x      : test Microsoft XBox Controller Type S\n");
1085                 printf("   -l lang : language to report errors in (ISO 639-1)\n");
1086                 printf("   -w      : force the use of device requests when querying WCID descriptors\n");
1087                 printf("If only the vid:pid is provided, xusb attempts to run the most appropriate test\n");
1088                 return 0;
1089         }
1090
1091         version = libusb_get_version();
1092         printf("Using libusb v%d.%d.%d.%d\n\n", version->major, version->minor, version->micro, version->nano);
1093         r = libusb_init(NULL);
1094         if (r < 0)
1095                 return r;
1096
1097         libusb_set_debug(NULL, debug_mode?LIBUSB_LOG_LEVEL_DEBUG:LIBUSB_LOG_LEVEL_INFO);
1098         if (error_lang != NULL) {
1099                 r = libusb_setlocale(error_lang);
1100                 if (r < 0)
1101                         printf("Invalid or unsupported locale '%s': %s\n", error_lang, libusb_strerror((enum libusb_error)r));
1102         }
1103
1104         test_device(VID, PID);
1105
1106         libusb_exit(NULL);
1107
1108         return 0;
1109 }