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