xusb.c 38 KB

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