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