1 /* $FreeBSD: head/tools/tools/usbtest/usb_msc_test.c 260587 2014-01-13 13:27:00Z hselasky $ */
3 * Copyright (c) 2007-2012 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/sysctl.h>
39 #include <libusb20_desc.h>
41 #include <bus/u4b/usb_endian.h>
45 #include "usb_msc_test.h"
47 /* Command Block Wrapper */
50 #define CBWSIGNATURE 0x43425355
52 uDWord dCBWDataTransferLength;
54 #define CBWFLAGS_OUT 0x00
55 #define CBWFLAGS_IN 0x80
58 #define CBWCDBLENGTH 16
59 uByte CBWCDB[CBWCDBLENGTH];
62 #define UMASS_BBB_CBW_SIZE 31
64 /* Command Status Wrapper */
67 #define CSWSIGNATURE 0x53425355
68 #define CSWSIGNATURE_IMAGINATION_DBX1 0x43425355
69 #define CSWSIGNATURE_OLYMPUS_C1 0x55425355
71 uDWord dCSWDataResidue;
73 #define CSWSTATUS_GOOD 0x0
74 #define CSWSTATUS_FAILED 0x1
75 #define CSWSTATUS_PHASE 0x2
78 #define UMASS_BBB_CSW_SIZE 13
80 #define SC_READ_6 0x08
81 #define SC_READ_10 0x28
82 #define SC_READ_12 0xa8
83 #define SC_WRITE_6 0x0a
84 #define SC_WRITE_10 0x2a
85 #define SC_WRITE_12 0xaa
89 uint64_t xfer_success;
91 uint64_t xfer_rx_bytes;
92 uint64_t xfer_tx_bytes;
96 static uint32_t xfer_current_id;
97 static uint32_t xfer_wrapper_sig;
98 static uint32_t block_size = 512;
100 static struct libusb20_transfer *xfer_in;
101 static struct libusb20_transfer *xfer_out;
102 static struct libusb20_device *usb_pdev;
103 static uint8_t usb_iface;
104 static int sense_recurse;
107 * SCSI commands sniffed off the wire - LUN maybe needs to be
108 * adjusted! Refer to "bus/u4b/storage/ustorage_fs.c" for more
111 static uint8_t mode_sense_6[0x6] = {0x1a, 0, 0x3f, 0, 0x0c};
112 static uint8_t read_capacity[0xA] = {0x25,};
113 static uint8_t request_sense[0xC] = {0x03, 0, 0, 0, 0x12};
114 static uint8_t test_unit_ready[0x6] = {0};
115 static uint8_t mode_page_inquiry[0x6] = {0x12, 1, 0x80, 0, 0xff, 0};
116 static uint8_t request_invalid[0xC] = {0xEA, 0, 0, 0, 0};
117 static uint8_t prevent_removal[0x6] = {0x1E, 0, 0, 0, 1};
118 static uint8_t read_toc[0xA] = {0x43, 0x02, 0, 0, 0, 0xAA, 0, 0x0C};
120 #define TIMEOUT_FILTER(x) (x)
122 static void usb_request_sense(uint8_t lun);
125 do_msc_reset(uint8_t lun)
127 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
129 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &setup);
131 setup.bmRequestType = LIBUSB20_REQUEST_TYPE_CLASS |
132 LIBUSB20_RECIPIENT_INTERFACE;
133 setup.bRequest = 0xFF; /* BBB reset */
135 setup.wIndex = usb_iface;
138 if (libusb20_dev_request_sync(usb_pdev, &setup, NULL, NULL, 5000, 0)) {
139 printf("ERROR: %s\n", __FUNCTION__);
142 libusb20_tr_clear_stall_sync(xfer_in);
143 libusb20_tr_clear_stall_sync(xfer_out);
147 usb_request_sense(lun);
151 do_msc_cmd(uint8_t *pcmd, uint8_t cmdlen, void *pdata, uint32_t datalen,
152 uint8_t isread, uint8_t isshort, uint8_t lun, uint8_t flags)
156 struct libusb20_transfer *xfer_io;
161 memset(&cbw, 0, sizeof(cbw));
163 USETDW(cbw.dCBWSignature, xfer_wrapper_sig);
164 USETDW(cbw.dCBWTag, xfer_current_id);
166 USETDW(cbw.dCBWDataTransferLength, datalen);
167 cbw.bCBWFlags = (isread ? CBWFLAGS_IN : CBWFLAGS_OUT);
169 cbw.bCDBLength = cmdlen;
170 bcopy(pcmd, cbw.CBWCDB, cmdlen);
174 timeout = ((datalen + 299999) / 300000) * 1000;
177 if ((error = libusb20_tr_bulk_intr_sync(xfer_out,
178 &cbw, sizeof(cbw), &actlen, TIMEOUT_FILTER(1000)))) {
179 printf("ERROR: CBW reception: %d\n", error);
183 if (actlen != sizeof(cbw)) {
184 printf("ERROR: CBW length: %d != %d\n",
185 actlen, (int)sizeof(cbw));
193 xfer_io = isread ? xfer_in : xfer_out;
195 if ((error = libusb20_tr_bulk_intr_sync(xfer_io,
196 pdata, datalen, &actlen, TIMEOUT_FILTER(timeout)))) {
197 printf("ERROR: Data transfer: %d\n", error);
201 if ((actlen != datalen) && (!isshort)) {
202 printf("ERROR: Short data: %d of %d bytes\n",
212 error = libusb20_tr_bulk_intr_sync(xfer_in, &csw,
213 sizeof(csw), &actlen, TIMEOUT_FILTER(1000));
215 if (error == LIBUSB20_TRANSFER_TIMED_OUT) {
216 printf("TIMEOUT: Trying to get CSW again. "
217 "%d tries left.\n", timeout);
227 libusb20_tr_clear_stall_sync(xfer_in);
228 error = libusb20_tr_bulk_intr_sync(xfer_in, &csw,
229 sizeof(csw), &actlen, TIMEOUT_FILTER(1000));
231 libusb20_tr_clear_stall_sync(xfer_in);
232 printf("ERROR: Could not read CSW: Stalled or "
233 "timeout (%d).\n", error);
238 if (UGETDW(csw.dCSWSignature) != CSWSIGNATURE) {
239 printf("ERROR: Wrong CSW signature\n");
243 if (actlen != sizeof(csw)) {
244 printf("ERROR: Wrong CSW length: %d != %d\n",
245 actlen, (int)sizeof(csw));
249 if (csw.bCSWStatus != 0) {
250 printf("ERROR: CSW status: %d\n", (int)csw.bCSWStatus);
253 stats.xfer_success++;
259 do_msc_shorter_cmd(uint8_t lun)
261 uint8_t buffer[sizeof(umass_bbb_cbw_t)];
266 memset(buffer, 0, sizeof(buffer));
268 for (x = 0; x != (sizeof(buffer) - 1); x++) {
269 error = libusb20_tr_bulk_intr_sync(xfer_out,
270 buffer, x, &actlen, 250);
272 printf("Sent short %d of %d bytes wrapper block, "
273 "status = %d\n", x, (int)(sizeof(buffer) - 1),
279 printf("ERROR: Too short command wrapper "
280 "was not accepted\n");
288 do_read_10(uint32_t lba, uint32_t len, void *buf, uint8_t lun)
290 static uint8_t cmd[10];
305 retval = do_msc_cmd(cmd, 10, buf, len * block_size, 1, 0, lun, 0);
308 printf("ERROR: %s\n", __FUNCTION__);
315 do_write_10(uint32_t lba, uint32_t len, void *buf, uint8_t lun)
317 static uint8_t cmd[10];
321 cmd[0] = SC_WRITE_10;
335 retval = do_msc_cmd(cmd, 10, buf, (len * block_size), 0, 0, lun, abort);
338 printf("ERROR: %s\n", __FUNCTION__);
345 do_io_test(struct usb_msc_params *p, uint8_t lun, uint32_t lba_max,
346 uint8_t *buffer, uint8_t *reference)
354 switch (p->io_mode) {
355 case USB_MSC_IO_MODE_WRITE_ONLY:
358 case USB_MSC_IO_MODE_READ_WRITE:
359 do_read = (usb_ts_rand_noise() & 1);
366 switch (p->io_offset) {
367 case USB_MSC_IO_OFF_RANDOM:
368 io_offset = usb_ts_rand_noise();
375 switch (p->io_delay) {
376 case USB_MSC_IO_DELAY_RANDOM_10MS:
377 usleep(((uint32_t)usb_ts_rand_noise()) % 10000U);
379 case USB_MSC_IO_DELAY_RANDOM_100MS:
380 usleep(((uint32_t)usb_ts_rand_noise()) % 100000U);
382 case USB_MSC_IO_DELAY_FIXED_10MS:
385 case USB_MSC_IO_DELAY_FIXED_100MS:
392 switch (p->io_size) {
393 case USB_MSC_IO_SIZE_RANDOM:
394 io_size = ((uint32_t)usb_ts_rand_noise()) & 65535U;
396 case USB_MSC_IO_SIZE_INCREASING:
397 io_size = (xfer_current_id & 65535U);
399 case USB_MSC_IO_SIZE_FIXED_1BLK:
402 case USB_MSC_IO_SIZE_FIXED_2BLK:
405 case USB_MSC_IO_SIZE_FIXED_4BLK:
408 case USB_MSC_IO_SIZE_FIXED_8BLK:
411 case USB_MSC_IO_SIZE_FIXED_16BLK:
414 case USB_MSC_IO_SIZE_FIXED_32BLK:
417 case USB_MSC_IO_SIZE_FIXED_64BLK:
420 case USB_MSC_IO_SIZE_FIXED_128BLK:
423 case USB_MSC_IO_SIZE_FIXED_256BLK:
426 case USB_MSC_IO_SIZE_FIXED_512BLK:
429 case USB_MSC_IO_SIZE_FIXED_1024BLK:
440 io_offset %= lba_max;
442 temp = (lba_max - io_offset);
448 retval = do_read_10(io_offset, io_size * block_size,
449 buffer + (io_offset * block_size), lun);
452 if (bcmp(buffer + (io_offset * block_size),
453 reference + (io_offset * block_size),
454 io_size * block_size)) {
455 printf("ERROR: Data comparison failure\n");
460 stats.xfer_rx_bytes += (io_size * block_size);
464 retval = do_write_10(io_offset, io_size * block_size,
465 reference + (io_offset * block_size), lun);
467 stats.xfer_tx_bytes += (io_size * block_size);
470 if ((stats.xfer_error + stats.data_error +
471 stats.xfer_reset) >= p->max_errors) {
472 printf("Maximum number of errors exceeded\n");
478 usb_request_sense(uint8_t lun)
480 uint8_t dummy_buf[255];
487 do_msc_cmd(request_sense, sizeof(request_sense),
488 dummy_buf, 255, 1, 1, lun, 0);
494 usb_msc_test(struct usb_msc_params *p)
496 struct stats last_stat;
497 struct timeval sub_tv;
498 struct timeval ref_tv;
499 struct timeval res_tv;
500 uint8_t *buffer = NULL;
501 uint8_t *reference = NULL;
502 uint32_t dummy_buf[65536 / 4];
506 uint32_t capacity_lba;
507 uint32_t capacity_bs;
512 memset(&last_stat, 0, sizeof(last_stat));
515 case USB_MSC_IO_LUN_0:
518 case USB_MSC_IO_LUN_1:
521 case USB_MSC_IO_LUN_2:
524 case USB_MSC_IO_LUN_3:
534 sense_recurse = p->try_sense_on_error ? 0 : 1;
536 printf("Resetting device ...\n");
540 printf("Testing SCSI commands ...\n");
542 if (p->try_all_lun) {
543 printf("Requesting sense from LUN 0..255 ... ");
544 for (x = y = 0; x != 256; x++) {
545 if (do_msc_cmd(mode_sense_6, sizeof(mode_sense_6),
546 dummy_buf, 255, 1, 1, x, 0))
549 if (libusb20_dev_check_connected(usb_pdev) != 0) {
550 printf(" disconnect ");
554 printf("Passed=%d, Failed=%d\n", 256 - y, y);
556 do_msc_cmd(mode_sense_6, sizeof(mode_sense_6),
557 dummy_buf, 255, 1, 1, lun, 0);
558 do_msc_cmd(request_sense, sizeof(request_sense),
559 dummy_buf, 255, 1, 1, lun, 0);
561 for (tries = 0; tries != 4; tries++) {
563 memset(dummy_buf, 0, sizeof(dummy_buf));
565 if (do_msc_cmd(read_capacity, sizeof(read_capacity),
566 dummy_buf, 255, 1, 1, lun, 0) != 0) {
567 printf("Cannot read disk capacity (%u / 4)\n", tries);
577 capacity_lba = be32toh(dummy_buf[0]);
578 capacity_bs = be32toh(dummy_buf[1]);
580 printf("Disk reports a capacity of LBA=%u and BS=%u\n",
581 capacity_lba, capacity_bs);
583 block_size = capacity_bs;
585 if (capacity_bs > 65535) {
586 printf("Blocksize is too big\n");
589 if (capacity_bs < 1) {
590 printf("Blocksize is too small\n");
593 if (capacity_bs != 512)
594 printf("INFO: Blocksize is not 512 bytes\n");
596 if (p->try_shorter_wrapper_block) {
597 printf("Trying too short command wrapper:\n");
598 do_msc_shorter_cmd(lun);
601 if (p->try_invalid_scsi_command) {
604 for (tries = 0; tries != 4; tries++) {
606 printf("Trying invalid SCSI command: ");
608 status = do_msc_cmd(request_invalid,
609 sizeof(request_invalid), dummy_buf,
612 printf("Result%s as expected\n", status ? "" : " NOT");
617 if (p->try_invalid_wrapper_block) {
620 for (tries = 0; tries != 4; tries++) {
622 printf("Trying invalid USB wrapper block signature: ");
624 xfer_wrapper_sig = 0x55663322;
626 status = do_msc_cmd(read_capacity,
627 sizeof(read_capacity), dummy_buf,
630 printf("Result%s as expected\n", status ? "" : " NOT");
632 xfer_wrapper_sig = CBWSIGNATURE;
637 do_msc_cmd(request_sense, sizeof(request_sense), dummy_buf, 255, 1, 1, lun, 0);
638 do_msc_cmd(read_capacity, sizeof(read_capacity), dummy_buf, 255, 1, 1, lun, 0);
639 do_msc_cmd(request_sense, sizeof(request_sense), dummy_buf, 255, 1, 1, lun, 0);
640 do_msc_cmd(test_unit_ready, sizeof(test_unit_ready), 0, 0, 1, 1, lun, 0);
641 do_msc_cmd(test_unit_ready, sizeof(test_unit_ready), 0, 0, 1, 1, lun, 0);
642 do_msc_cmd(request_sense, sizeof(request_sense), dummy_buf, 255, 1, 1, lun, 0);
643 do_msc_cmd(test_unit_ready, sizeof(test_unit_ready), 0, 0, 1, 1, lun, 0);
644 do_msc_cmd(request_sense, sizeof(request_sense), dummy_buf, 255, 1, 1, lun, 0);
645 do_msc_cmd(test_unit_ready, sizeof(test_unit_ready), 0, 0, 1, 1, lun, 0);
646 do_msc_cmd(test_unit_ready, sizeof(test_unit_ready), 0, 0, 1, 1, lun, 0);
647 do_msc_cmd(test_unit_ready, sizeof(test_unit_ready), 0, 0, 1, 1, lun, 0);
648 do_msc_cmd(request_sense, sizeof(request_sense), dummy_buf, 255, 1, 1, lun, 0);
649 do_msc_cmd(test_unit_ready, sizeof(test_unit_ready), 0, 0, 1, 1, lun, 0);
650 do_msc_cmd(read_capacity, sizeof(read_capacity), dummy_buf, 255, 1, 1, lun, 0);
651 do_msc_cmd(mode_page_inquiry, sizeof(mode_page_inquiry), dummy_buf, 255, 1, 1, lun, 0);
652 do_msc_cmd(mode_page_inquiry, sizeof(mode_page_inquiry), dummy_buf, 255, 1, 1, lun, 0);
654 if (do_msc_cmd(prevent_removal, sizeof(prevent_removal),
655 0, 0, 1, 1, lun, 0)) {
656 printf("INFO: Prevent medium removal failed\n");
658 if (do_msc_cmd(read_toc, sizeof(read_toc),
659 dummy_buf, 255, 1, 1, lun, 0)) {
660 printf("INFO: Read Table Of Content failed\n");
662 if (p->try_last_lba) {
664 for (y = 0, x = (1UL << 31); x; x >>= 1) {
665 if (do_read_10(x | y, block_size, dummy_buf, lun) == 0)
669 printf("Highest readable LBA: %u (%s), "
670 "Capacity is %u MBytes\n", y,
671 (capacity_lba != y) ? "WRONG" : "OK",
672 (int)((((uint64_t)(y) * (uint64_t)block_size) +
673 (uint64_t)block_size) / 1000000ULL));
678 printf("Highest readable LBA: %u (not "
679 "verified), Capacity is %u MBytes\n", y,
680 (int)((((uint64_t)(y) * (uint64_t)block_size) +
681 (uint64_t)block_size) / 1000000ULL));
684 if (y != 0xFFFFFFFFU)
689 switch (p->io_area) {
690 case USB_MSC_IO_AREA_1MB:
693 case USB_MSC_IO_AREA_16MB:
696 case USB_MSC_IO_AREA_256MB:
697 lba_max = 1024 * 256;
699 case USB_MSC_IO_AREA_COMPLETE:
707 printf("Highest testable LBA: %u\n", (int)lba_max);
709 buffer = malloc(block_size * lba_max);
710 if (buffer == NULL) {
711 printf("ERROR: Could not allocate memory\n");
714 reference = malloc(block_size * lba_max);
715 if (reference == NULL) {
716 printf("ERROR: Could not allocate memory\n");
721 printf("Setting up initial data pattern, "
722 "LBA limit = %u ... ", lba_max);
724 switch (p->io_mode) {
725 case USB_MSC_IO_MODE_WRITE_ONCE_READ_ONLY:
726 case USB_MSC_IO_MODE_WRITE_ONLY:
727 case USB_MSC_IO_MODE_READ_WRITE:
729 switch (p->io_pattern) {
730 case USB_MSC_IO_PATTERN_FIXED:
731 for (x = 0; x != (block_size * lba_max); x += 8) {
732 reference[x + 0] = x >> 24;
733 reference[x + 1] = x >> 16;
734 reference[x + 2] = x >> 8;
735 reference[x + 3] = x >> 0;
736 reference[x + 4] = 0xFF;
737 reference[x + 5] = 0x00;
738 reference[x + 6] = 0xFF;
739 reference[x + 7] = 0x00;
741 if (do_write_10(0, lba_max * block_size,
746 goto retry_read_init;
751 case USB_MSC_IO_PATTERN_RANDOM:
752 for (x = 0; x != (block_size * lba_max); x++) {
753 reference[x] = usb_ts_rand_noise() % 255U;
755 if (do_write_10(0, lba_max * block_size,
760 goto retry_read_init;
766 if (do_read_10(0, lba_max * block_size,
771 goto retry_read_init;
780 if (do_read_10(0, lba_max * block_size, reference, lun)) {
784 goto retry_read_init;
792 if (p->try_abort_data_write) {
793 if (do_write_10(0, (2 * block_size) | 1, reference, lun))
794 printf("Aborted data write failed (OK)!\n");
796 printf("Aborted data write did not fail (ERROR)!\n");
798 if (do_read_10(0, (2 * block_size), reference, lun))
799 printf("Post-aborted data read failed (ERROR)\n");
801 printf("Post-aborted data read success (OK)!\n");
803 printf("Starting test ...\n");
805 gettimeofday(&ref_tv, 0);
807 last_sec = ref_tv.tv_sec;
813 gettimeofday(&sub_tv, 0);
815 if (last_sec != sub_tv.tv_sec) {
817 printf("STATUS: ID=%u, RX=%u bytes/sec, "
818 "TX=%u bytes/sec, ERR=%u, RST=%u, DERR=%u\n",
819 (int)xfer_current_id,
820 (int)(stats.xfer_rx_bytes -
821 last_stat.xfer_rx_bytes),
822 (int)(stats.xfer_tx_bytes -
823 last_stat.xfer_tx_bytes),
824 (int)(stats.xfer_error),
825 (int)(stats.xfer_reset),
826 (int)(stats.data_error));
830 last_sec = sub_tv.tv_sec;
833 timersub(&sub_tv, &ref_tv, &res_tv);
835 if ((res_tv.tv_sec < 0) || (res_tv.tv_sec >= (int)p->duration))
838 do_io_test(p, lun, lba_max, buffer, reference);
840 if (libusb20_dev_check_connected(usb_pdev) != 0) {
841 printf("Device disconnected\n");
845 printf("Maximum number of errors exceeded\n");
850 printf("\nTest done!\n");
860 show_host_device_selection(uint8_t level, uint16_t *pvid, uint16_t *ppid)
862 struct libusb20_backend *pbe;
863 struct libusb20_device *pdev;
864 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
866 uint16_t vid[USB_DEVICES_MAX];
867 uint16_t pid[USB_DEVICES_MAX];
875 pbe = libusb20_be_alloc_default();
879 printf("\n[] Select USB device:\n");
881 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
883 if (libusb20_dev_get_mode(pdev) != LIBUSB20_MODE_HOST)
886 if (index < USB_DEVICES_MAX) {
887 ddesc = libusb20_dev_get_device_desc(pdev);
888 ptr = libusb20_dev_get_desc(pdev);
889 printf("%s%d) %s\n", indent[level], index, ptr);
890 vid[index] = ddesc->idVendor;
891 pid[index] = ddesc->idProduct;
898 printf("%sr) Refresh device list\n", indent[level]);
899 printf("%sx) Return to previous menu\n", indent[level]);
902 libusb20_be_free(pbe);
909 if ((sel < 0) || (sel >= index)) {
918 struct libusb20_device *
919 find_usb_device(uint16_t vid, uint16_t pid)
921 struct libusb20_backend *pbe = libusb20_be_alloc_default();
922 struct libusb20_device *pdev = NULL;
923 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
925 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
927 if (libusb20_dev_get_mode(pdev) != LIBUSB20_MODE_HOST)
930 ddesc = libusb20_dev_get_device_desc(pdev);
932 if ((vid == ddesc->idVendor) &&
933 (pid == ddesc->idProduct)) {
934 libusb20_be_dequeue_device(pbe, pdev);
940 libusb20_be_free(pbe);
946 find_usb_endpoints(struct libusb20_device *pdev, uint8_t class,
947 uint8_t subclass, uint8_t protocol, uint8_t alt_setting,
948 uint8_t *pif, uint8_t *in_ep, uint8_t *out_ep, uint8_t next_if)
950 struct libusb20_config *pcfg;
951 struct libusb20_interface *iface;
952 struct libusb20_endpoint *ep;
961 pcfg = libusb20_dev_alloc_config(pdev,
962 libusb20_dev_get_config_index(pdev));
967 for (x = 0; x != pcfg->num_interface; x++) {
971 iface = (pcfg->interface + x);
973 if ((iface->desc.bInterfaceClass == class) &&
974 (iface->desc.bInterfaceSubClass == subclass ||
976 (iface->desc.bInterfaceProtocol == protocol ||
981 if (x == pcfg->num_interface)
983 iface = (pcfg->interface + x);
987 for (z = 0; z != iface->num_endpoints; z++) {
988 ep = iface->endpoints + z;
991 if ((ep->desc.bmAttributes & 3) != 2)
994 if (ep->desc.bEndpointAddress & 0x80)
995 *in_ep = ep->desc.bEndpointAddress;
997 *out_ep = ep->desc.bEndpointAddress;
1007 exec_host_msc_test(struct usb_msc_params *p, uint16_t vid, uint16_t pid)
1009 struct libusb20_device *pdev;
1017 memset(&stats, 0, sizeof(stats));
1019 xfer_current_id = 0;
1020 xfer_wrapper_sig = CBWSIGNATURE;
1022 pdev = find_usb_device(vid, pid);
1024 printf("USB device not found\n");
1027 find_usb_endpoints(pdev, 8, 6, 0x50, 0, &iface, &in_ep, &out_ep, 0);
1029 if ((in_ep == 0) || (out_ep == 0)) {
1030 printf("Could not find USB endpoints\n");
1031 libusb20_dev_free(pdev);
1034 printf("Attaching to: %s @ iface %d\n",
1035 libusb20_dev_get_desc(pdev), iface);
1037 if (libusb20_dev_open(pdev, 2)) {
1038 printf("Could not open USB device\n");
1039 libusb20_dev_free(pdev);
1042 if (libusb20_dev_detach_kernel_driver(pdev, iface)) {
1043 printf("WARNING: Could not detach kernel driver\n");
1045 xfer_in = libusb20_tr_get_pointer(pdev, 0);
1046 error = libusb20_tr_open(xfer_in, 65536, 1, in_ep);
1048 printf("Could not open USB endpoint %d\n", in_ep);
1049 libusb20_dev_free(pdev);
1052 xfer_out = libusb20_tr_get_pointer(pdev, 1);
1053 error = libusb20_tr_open(xfer_out, 65536, 1, out_ep);
1055 printf("Could not open USB endpoint %d\n", out_ep);
1056 libusb20_dev_free(pdev);
1064 libusb20_dev_free(pdev);
1068 set_defaults(struct usb_msc_params *p)
1070 memset(p, 0, sizeof(*p));
1072 p->duration = 60; /* seconds */
1073 p->try_invalid_scsi_command = 1;
1074 p->try_invalid_wrapper_block = 1;
1075 p->try_last_lba = 1;
1080 get_io_mode(const struct usb_msc_params *p)
1083 switch (p->io_mode) {
1084 case USB_MSC_IO_MODE_READ_ONLY:
1085 return ("Read Only");
1086 case USB_MSC_IO_MODE_WRITE_ONCE_READ_ONLY:
1087 return ("Write Once, Read Only");
1088 case USB_MSC_IO_MODE_WRITE_ONLY:
1089 return ("Write Only");
1090 case USB_MSC_IO_MODE_READ_WRITE:
1091 return ("Read and Write");
1098 get_io_pattern(const struct usb_msc_params *p)
1101 switch (p->io_pattern) {
1102 case USB_MSC_IO_PATTERN_FIXED:
1104 case USB_MSC_IO_PATTERN_RANDOM:
1106 case USB_MSC_IO_PATTERN_PRESERVE:
1107 return ("Preserve");
1114 get_io_size(const struct usb_msc_params *p)
1117 switch (p->io_size) {
1118 case USB_MSC_IO_SIZE_RANDOM:
1120 case USB_MSC_IO_SIZE_INCREASING:
1121 return ("Increasing");
1122 case USB_MSC_IO_SIZE_FIXED_1BLK:
1123 return ("Single block");
1124 case USB_MSC_IO_SIZE_FIXED_2BLK:
1125 return ("2 blocks");
1126 case USB_MSC_IO_SIZE_FIXED_4BLK:
1127 return ("4 blocks");
1128 case USB_MSC_IO_SIZE_FIXED_8BLK:
1129 return ("8 blocks");
1130 case USB_MSC_IO_SIZE_FIXED_16BLK:
1131 return ("16 blocks");
1132 case USB_MSC_IO_SIZE_FIXED_32BLK:
1133 return ("32 blocks");
1134 case USB_MSC_IO_SIZE_FIXED_64BLK:
1135 return ("64 blocks");
1136 case USB_MSC_IO_SIZE_FIXED_128BLK:
1137 return ("128 blocks");
1138 case USB_MSC_IO_SIZE_FIXED_256BLK:
1139 return ("256 blocks");
1140 case USB_MSC_IO_SIZE_FIXED_512BLK:
1141 return ("512 blocks");
1142 case USB_MSC_IO_SIZE_FIXED_1024BLK:
1143 return ("1024 blocks");
1150 get_io_delay(const struct usb_msc_params *p)
1153 switch (p->io_delay) {
1154 case USB_MSC_IO_DELAY_NONE:
1156 case USB_MSC_IO_DELAY_RANDOM_10MS:
1157 return ("Random 10ms");
1158 case USB_MSC_IO_DELAY_RANDOM_100MS:
1159 return ("Random 100ms");
1160 case USB_MSC_IO_DELAY_FIXED_10MS:
1161 return ("Fixed 10ms");
1162 case USB_MSC_IO_DELAY_FIXED_100MS:
1163 return ("Fixed 100ms");
1170 get_io_offset(const struct usb_msc_params *p)
1173 switch (p->io_offset) {
1174 case USB_MSC_IO_OFF_START_OF_DISK:
1175 return ("Start Of Disk");
1176 case USB_MSC_IO_OFF_RANDOM:
1177 return ("Random Offset");
1184 get_io_area(const struct usb_msc_params *p)
1187 switch (p->io_area) {
1188 case USB_MSC_IO_AREA_COMPLETE:
1189 return ("Complete Disk");
1190 case USB_MSC_IO_AREA_1MB:
1191 return ("First MegaByte");
1192 case USB_MSC_IO_AREA_16MB:
1193 return ("First 16 MegaBytes");
1194 case USB_MSC_IO_AREA_256MB:
1195 return ("First 256 MegaBytes");
1202 show_host_msc_test(uint8_t level, uint16_t vid,
1203 uint16_t pid, uint32_t duration)
1205 struct usb_msc_params params;
1208 set_defaults(¶ms);
1210 params.duration = duration;
1214 retval = usb_ts_show_menu(level,
1215 "Mass Storage Test Parameters",
1216 " 1) Toggle I/O mode: <%s>\n"
1217 " 2) Toggle I/O size: <%s>\n"
1218 " 3) Toggle I/O delay: <%s>\n"
1219 " 4) Toggle I/O offset: <%s>\n"
1220 " 5) Toggle I/O area: <%s>\n"
1221 " 6) Toggle I/O pattern: <%s>\n"
1222 " 7) Toggle try invalid SCSI command: <%s>\n"
1223 " 8) Toggle try invalid wrapper block: <%s>\n"
1224 " 9) Toggle try invalid MaxPacketSize: <%s>\n"
1225 "10) Toggle try last Logical Block Address: <%s>\n"
1226 "11) Toggle I/O lun: <%d>\n"
1227 "12) Set maximum number of errors: <%d>\n"
1228 "13) Set test duration: <%d> seconds\n"
1229 "14) Toggle try aborted write transfer: <%s>\n"
1230 "15) Toggle request sense on error: <%s>\n"
1231 "16) Toggle try all LUN: <%s>\n"
1232 "17) Toggle try too short wrapper block: <%s>\n"
1233 "20) Reset parameters\n"
1234 "30) Start test (VID=0x%04x, PID=0x%04x)\n"
1235 "40) Select another device\n"
1236 " x) Return to previous menu \n",
1237 get_io_mode(¶ms),
1238 get_io_size(¶ms),
1239 get_io_delay(¶ms),
1240 get_io_offset(¶ms),
1241 get_io_area(¶ms),
1242 get_io_pattern(¶ms),
1243 (params.try_invalid_scsi_command ? "YES" : "NO"),
1244 (params.try_invalid_wrapper_block ? "YES" : "NO"),
1245 (params.try_invalid_max_packet_size ? "YES" : "NO"),
1246 (params.try_last_lba ? "YES" : "NO"),
1248 (int)params.max_errors,
1249 (int)params.duration,
1250 (params.try_abort_data_write ? "YES" : "NO"),
1251 (params.try_sense_on_error ? "YES" : "NO"),
1252 (params.try_all_lun ? "YES" : "NO"),
1253 (params.try_shorter_wrapper_block ? "YES" : "NO"),
1260 params.io_mode %= USB_MSC_IO_MODE_MAX;
1264 params.io_size %= USB_MSC_IO_SIZE_MAX;
1268 params.io_delay %= USB_MSC_IO_DELAY_MAX;
1272 params.io_offset %= USB_MSC_IO_OFF_MAX;
1276 params.io_area %= USB_MSC_IO_AREA_MAX;
1279 params.io_pattern++;
1280 params.io_pattern %= USB_MSC_IO_PATTERN_MAX;
1283 params.try_invalid_scsi_command ^= 1;
1286 params.try_invalid_wrapper_block ^= 1;
1289 params.try_invalid_max_packet_size ^= 1;
1292 params.try_last_lba ^= 1;
1296 params.io_lun %= USB_MSC_IO_LUN_MAX;
1299 params.max_errors = get_integer();
1302 params.duration = get_integer();
1305 params.try_abort_data_write ^= 1;
1308 params.try_sense_on_error ^= 1;
1311 params.try_all_lun ^= 1;
1314 params.try_shorter_wrapper_block ^= 1;
1317 set_defaults(¶ms);
1320 exec_host_msc_test(¶ms, vid, pid);
1323 show_host_device_selection(level + 1, &vid, &pid);