1 /* $FreeBSD: head/sys/dev/usb/usb_msctest.c 269578 2014-08-05 09:59:16Z n_hibma $ */
3 * Copyright (c) 2008,2011 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
28 * The following file contains code that will detect USB autoinstall
31 * TODO: Potentially we could add code to automatically detect USB
32 * mass storage quirks for not supported SCSI commands!
35 #include <sys/stdint.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <bus/u4b/usb.h>
53 #include <bus/u4b/usbdi.h>
54 #include <bus/u4b/usbdi_util.h>
56 #define USB_DEBUG_VAR usb_debug
58 #include <bus/u4b/usb_busdma.h>
59 #include <bus/u4b/usb_process.h>
60 #include <bus/u4b/usb_transfer.h>
61 #include <bus/u4b/usb_msctest.h>
62 #include <bus/u4b/usb_debug.h>
63 #include <bus/u4b/usb_device.h>
64 #include <bus/u4b/usb_request.h>
65 #include <bus/u4b/usb_util.h>
66 #include <bus/u4b/quirk/usb_quirk.h>
84 #define SCSI_MAX_LEN MAX(SCSI_FIXED_BLOCK_SIZE, USB_MSCTEST_BULK_SIZE)
85 #define SCSI_INQ_LEN 0x24
86 #define SCSI_SENSE_LEN 0xFF
87 #define SCSI_FIXED_BLOCK_SIZE 512 /* bytes */
89 static uint8_t scsi_test_unit_ready[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
90 static uint8_t scsi_inquiry[] = { 0x12, 0x00, 0x00, 0x00, SCSI_INQ_LEN, 0x00 };
91 static uint8_t scsi_rezero_init[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
92 static uint8_t scsi_start_stop_unit[] = { 0x1b, 0x00, 0x00, 0x00, 0x02, 0x00 };
93 static uint8_t scsi_ztestor_eject[] = { 0x85, 0x01, 0x01, 0x01, 0x18, 0x01,
94 0x01, 0x01, 0x01, 0x01, 0x00, 0x00 };
95 static uint8_t scsi_cmotech_eject[] = { 0xff, 0x52, 0x44, 0x45, 0x56, 0x43,
97 static uint8_t scsi_huawei_eject[] = { 0x11, 0x06, 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00 };
100 static uint8_t scsi_huawei_eject2[] = { 0x11, 0x06, 0x20, 0x00, 0x00, 0x01,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102 0x00, 0x00, 0x00, 0x00 };
103 static uint8_t scsi_tct_eject[] = { 0x06, 0xf5, 0x04, 0x02, 0x52, 0x70 };
104 static uint8_t scsi_sync_cache[] = { 0x35, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00 };
106 static uint8_t scsi_request_sense[] = { 0x03, 0x00, 0x00, 0x00, 0x12, 0x00,
107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
108 static uint8_t scsi_read_capacity[] = { 0x25, 0x00, 0x00, 0x00, 0x00, 0x00,
109 0x00, 0x00, 0x00, 0x00 };
110 static uint8_t scsi_prevent_removal[] = { 0x1e, 0, 0, 0, 1, 0 };
111 static uint8_t scsi_allow_removal[] = { 0x1e, 0, 0, 0, 0, 0 };
113 #ifndef USB_MSCTEST_BULK_SIZE
114 #define USB_MSCTEST_BULK_SIZE 64 /* dummy */
117 #define ERR_CSW_FAILED -1
119 /* Command Block Wrapper */
121 uDWord dCBWSignature;
122 #define CBWSIGNATURE 0x43425355
124 uDWord dCBWDataTransferLength;
126 #define CBWFLAGS_OUT 0x00
127 #define CBWFLAGS_IN 0x80
130 #define CBWCDBLENGTH 16
131 uByte CBWCDB[CBWCDBLENGTH];
134 /* Command Status Wrapper */
136 uDWord dCSWSignature;
137 #define CSWSIGNATURE 0x53425355
139 uDWord dCSWDataResidue;
141 #define CSWSTATUS_GOOD 0x0
142 #define CSWSTATUS_FAILED 0x1
143 #define CSWSTATUS_PHASE 0x2
146 struct bbb_transfer {
152 struct usb_xfer *xfer[ST_MAX];
156 usb_size_t data_len; /* bytes */
157 usb_size_t data_rem; /* bytes */
158 usb_timeout_t data_timeout; /* ms */
159 usb_frlength_t actlen; /* bytes */
160 usb_frlength_t buffer_size; /* bytes */
162 uint8_t cmd_len; /* bytes */
172 static usb_callback_t bbb_command_callback;
173 static usb_callback_t bbb_data_read_callback;
174 static usb_callback_t bbb_data_rd_cs_callback;
175 static usb_callback_t bbb_data_write_callback;
176 static usb_callback_t bbb_data_wr_cs_callback;
177 static usb_callback_t bbb_status_callback;
178 static usb_callback_t bbb_raw_write_callback;
180 static void bbb_done(struct bbb_transfer *, int);
181 static void bbb_transfer_start(struct bbb_transfer *, uint8_t);
182 static void bbb_data_clear_stall_callback(struct usb_xfer *, uint8_t,
184 static int bbb_command_start(struct bbb_transfer *, uint8_t, uint8_t,
185 void *, size_t, void *, size_t, usb_timeout_t);
186 static struct bbb_transfer *bbb_attach(struct usb_device *, uint8_t, uint8_t);
187 static void bbb_detach(struct bbb_transfer *);
189 static const struct usb_config bbb_config[ST_MAX] = {
193 .endpoint = UE_ADDR_ANY,
194 .direction = UE_DIR_OUT,
195 .bufsize = sizeof(struct bbb_cbw),
196 .callback = &bbb_command_callback,
197 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
202 .endpoint = UE_ADDR_ANY,
203 .direction = UE_DIR_IN,
204 .bufsize = SCSI_MAX_LEN,
205 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
206 .callback = &bbb_data_read_callback,
207 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
212 .endpoint = 0x00, /* Control pipe */
213 .direction = UE_DIR_ANY,
214 .bufsize = sizeof(struct usb_device_request),
215 .callback = &bbb_data_rd_cs_callback,
216 .timeout = 1 * USB_MS_HZ, /* 1 second */
221 .endpoint = UE_ADDR_ANY,
222 .direction = UE_DIR_OUT,
223 .bufsize = SCSI_MAX_LEN,
224 .flags = {.ext_buffer = 1,.proxy_buffer = 1,},
225 .callback = &bbb_data_write_callback,
226 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
231 .endpoint = 0x00, /* Control pipe */
232 .direction = UE_DIR_ANY,
233 .bufsize = sizeof(struct usb_device_request),
234 .callback = &bbb_data_wr_cs_callback,
235 .timeout = 1 * USB_MS_HZ, /* 1 second */
240 .endpoint = UE_ADDR_ANY,
241 .direction = UE_DIR_IN,
242 .bufsize = sizeof(struct bbb_csw),
243 .flags = {.short_xfer_ok = 1,},
244 .callback = &bbb_status_callback,
245 .timeout = 1 * USB_MS_HZ, /* 1 second */
249 static const struct usb_config bbb_raw_config[1] = {
252 .type = UE_BULK_INTR,
253 .endpoint = UE_ADDR_ANY,
254 .direction = UE_DIR_OUT,
255 .bufsize = SCSI_MAX_LEN,
256 .flags = {.ext_buffer = 1,.proxy_buffer = 1,},
257 .callback = &bbb_raw_write_callback,
258 .timeout = 1 * USB_MS_HZ, /* 1 second */
263 bbb_done(struct bbb_transfer *sc, int error)
266 sc->state = ST_COMMAND;
272 bbb_transfer_start(struct bbb_transfer *sc, uint8_t xfer_index)
274 sc->state = xfer_index;
275 usbd_transfer_start(sc->xfer[xfer_index]);
279 bbb_data_clear_stall_callback(struct usb_xfer *xfer,
280 uint8_t next_xfer, uint8_t stall_xfer)
282 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
284 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) {
285 switch (USB_GET_STATE(xfer)) {
287 case USB_ST_TRANSFERRED:
288 bbb_transfer_start(sc, next_xfer);
291 bbb_done(sc, USB_ERR_STALLED);
298 bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
300 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
303 switch (USB_GET_STATE(xfer)) {
304 case USB_ST_TRANSFERRED:
306 (sc, ((sc->dir == DIR_IN) ? ST_DATA_RD :
307 (sc->dir == DIR_OUT) ? ST_DATA_WR :
313 tag = UGETDW(sc->cbw->dCBWTag) + 1;
314 USETDW(sc->cbw->dCBWSignature, CBWSIGNATURE);
315 USETDW(sc->cbw->dCBWTag, tag);
316 USETDW(sc->cbw->dCBWDataTransferLength, (uint32_t)sc->data_len);
317 sc->cbw->bCBWFlags = ((sc->dir == DIR_IN) ? CBWFLAGS_IN : CBWFLAGS_OUT);
318 sc->cbw->bCBWLUN = sc->lun;
319 sc->cbw->bCDBLength = sc->cmd_len;
320 if (sc->cbw->bCDBLength > sizeof(sc->cbw->CBWCDB)) {
321 sc->cbw->bCDBLength = sizeof(sc->cbw->CBWCDB);
322 DPRINTFN(0, "Truncating long command\n");
324 usbd_xfer_set_frame_len(xfer, 0,
325 sizeof(struct bbb_cbw));
326 usbd_transfer_submit(xfer);
336 bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
338 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
339 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
342 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
344 switch (USB_GET_STATE(xfer)) {
345 case USB_ST_TRANSFERRED:
346 sc->data_rem -= actlen;
347 sc->data_ptr += actlen;
348 sc->actlen += actlen;
350 if (actlen < sumlen) {
355 DPRINTF("max_bulk=%d, data_rem=%d\n",
356 max_bulk, sc->data_rem);
358 if (sc->data_rem == 0) {
359 bbb_transfer_start(sc, ST_STATUS);
362 if (max_bulk > sc->data_rem) {
363 max_bulk = sc->data_rem;
365 usbd_xfer_set_timeout(xfer, sc->data_timeout);
366 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
367 usbd_transfer_submit(xfer);
371 if (error == USB_ERR_CANCELLED) {
374 bbb_transfer_start(sc, ST_DATA_RD_CS);
381 bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
383 bbb_data_clear_stall_callback(xfer, ST_STATUS,
388 bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
390 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
391 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
394 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
396 switch (USB_GET_STATE(xfer)) {
397 case USB_ST_TRANSFERRED:
398 sc->data_rem -= actlen;
399 sc->data_ptr += actlen;
400 sc->actlen += actlen;
402 if (actlen < sumlen) {
407 DPRINTF("max_bulk=%d, data_rem=%d\n",
408 max_bulk, sc->data_rem);
410 if (sc->data_rem == 0) {
411 bbb_transfer_start(sc, ST_STATUS);
414 if (max_bulk > sc->data_rem) {
415 max_bulk = sc->data_rem;
417 usbd_xfer_set_timeout(xfer, sc->data_timeout);
418 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
419 usbd_transfer_submit(xfer);
423 if (error == USB_ERR_CANCELLED) {
426 bbb_transfer_start(sc, ST_DATA_WR_CS);
433 bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
435 bbb_data_clear_stall_callback(xfer, ST_STATUS,
440 bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
442 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
446 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
448 switch (USB_GET_STATE(xfer)) {
449 case USB_ST_TRANSFERRED:
451 /* very simple status check */
453 if (actlen < (int)sizeof(struct bbb_csw)) {
454 bbb_done(sc, USB_ERR_SHORT_XFER);
455 } else if (sc->csw->bCSWStatus == CSWSTATUS_GOOD) {
456 bbb_done(sc, 0); /* success */
458 bbb_done(sc, ERR_CSW_FAILED); /* error */
463 usbd_xfer_set_frame_len(xfer, 0,
464 sizeof(struct bbb_csw));
465 usbd_transfer_submit(xfer);
469 DPRINTF("Failed to read CSW: %s, try %d\n",
470 usbd_errstr(error), sc->status_try);
472 if (error == USB_ERR_CANCELLED || sc->status_try) {
476 bbb_transfer_start(sc, ST_DATA_RD_CS);
483 bbb_raw_write_callback(struct usb_xfer *xfer, usb_error_t error)
485 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
486 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
489 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
491 switch (USB_GET_STATE(xfer)) {
492 case USB_ST_TRANSFERRED:
493 sc->data_rem -= actlen;
494 sc->data_ptr += actlen;
495 sc->actlen += actlen;
497 if (actlen < sumlen) {
502 DPRINTF("max_bulk=%d, data_rem=%d\n",
503 max_bulk, sc->data_rem);
505 if (sc->data_rem == 0) {
509 if (max_bulk > sc->data_rem) {
510 max_bulk = sc->data_rem;
512 usbd_xfer_set_timeout(xfer, sc->data_timeout);
513 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
514 usbd_transfer_submit(xfer);
523 /*------------------------------------------------------------------------*
524 * bbb_command_start - execute a SCSI command synchronously
529 *------------------------------------------------------------------------*/
531 bbb_command_start(struct bbb_transfer *sc, uint8_t dir, uint8_t lun,
532 void *data_ptr, size_t data_len, void *cmd_ptr, size_t cmd_len,
533 usb_timeout_t data_timeout)
536 sc->dir = data_len ? dir : DIR_NONE;
537 sc->data_ptr = data_ptr;
538 sc->data_len = data_len;
539 sc->data_rem = data_len;
540 sc->data_timeout = (data_timeout + USB_MS_HZ);
543 sc->cmd_len = cmd_len;
544 memset(&sc->cbw->CBWCDB, 0, sizeof(sc->cbw->CBWCDB));
545 memcpy(&sc->cbw->CBWCDB, cmd_ptr, cmd_len);
546 DPRINTFN(1, "SCSI cmd_len = %d, cmd %s\n", (int)cmd_len, (char *)sc->cbw->CBWCDB);
548 lockmgr(&sc->lock, LK_EXCLUSIVE);
549 usbd_transfer_start(sc->xfer[sc->state]);
551 while (usbd_transfer_pending(sc->xfer[sc->state])) {
552 cv_wait(&sc->cv, &sc->lock);
554 lockmgr(&sc->lock, LK_RELEASE);
558 /*------------------------------------------------------------------------*
559 * bbb_raw_write - write a raw BULK message synchronously
564 *------------------------------------------------------------------------*/
566 bbb_raw_write(struct bbb_transfer *sc, const void *data_ptr, size_t data_len,
567 usb_timeout_t data_timeout)
569 sc->data_ptr = __DECONST(void *, data_ptr);
570 sc->data_len = data_len;
571 sc->data_rem = data_len;
572 sc->data_timeout = (data_timeout + USB_MS_HZ);
576 // DPRINTFN(1, "BULK DATA = %*D\n", (int)data_len,
577 // (const char *)data_ptr, ":");
579 lockmgr(&sc->lock, LK_EXCLUSIVE);
580 usbd_transfer_start(sc->xfer[0]);
581 while (usbd_transfer_pending(sc->xfer[0]))
582 cv_wait(&sc->cv, &sc->lock);
583 lockmgr(&sc->lock, LK_RELEASE);
587 static struct bbb_transfer *
588 bbb_attach(struct usb_device *udev, uint8_t iface_index,
589 uint8_t bInterfaceClass)
591 struct usb_interface *iface;
592 struct usb_interface_descriptor *id;
593 const struct usb_config *pconfig;
594 struct bbb_transfer *sc;
598 #if USB_HAVE_MSCTEST_DETACH
601 /* Prevent re-enumeration */
602 do_unlock = usbd_enum_lock(udev);
605 * Make sure any driver which is hooked up to this interface,
606 * like umass is gone:
608 usb_detach_device(udev, iface_index, 0);
611 usbd_enum_unlock(udev);
614 iface = usbd_get_iface(udev, iface_index);
619 if (id == NULL || id->bInterfaceClass != bInterfaceClass)
622 switch (id->bInterfaceClass) {
624 switch (id->bInterfaceSubClass) {
625 case UISUBCLASS_SCSI:
627 case UISUBCLASS_SFF8020I:
628 case UISUBCLASS_SFF8070I:
633 switch (id->bInterfaceProtocol) {
634 case UIPROTO_MASS_BBB_OLD:
635 case UIPROTO_MASS_BBB:
640 pconfig = bbb_config;
644 switch (id->bInterfaceSubClass) {
650 pconfig = bbb_raw_config;
657 sc = kmalloc(sizeof(*sc), M_USB, M_WAITOK | M_ZERO);
658 lockinit(&sc->lock, "USB autoinstall", 0, 0);
659 cv_init(&sc->cv, "WBBB");
661 err = usbd_transfer_setup(udev, &iface_index, sc->xfer, pconfig,
662 nconfig, sc, &sc->lock);
667 switch (id->bInterfaceClass) {
669 /* store pointer to DMA buffers */
670 sc->buffer = usbd_xfer_get_frame_buffer(
671 sc->xfer[ST_DATA_RD], 0);
673 usbd_xfer_max_len(sc->xfer[ST_DATA_RD]);
674 sc->cbw = usbd_xfer_get_frame_buffer(
675 sc->xfer[ST_COMMAND], 0);
676 sc->csw = usbd_xfer_get_frame_buffer(
677 sc->xfer[ST_STATUS], 0);
686 bbb_detach(struct bbb_transfer *sc)
688 usbd_transfer_unsetup(sc->xfer, ST_MAX);
689 lockuninit(&sc->lock);
694 /*------------------------------------------------------------------------*
698 * 1: This interface is an auto install disk (CD-ROM)
699 * 0: Not an auto install disk.
700 *------------------------------------------------------------------------*/
702 usb_iface_is_cdrom(struct usb_device *udev, uint8_t iface_index)
704 struct bbb_transfer *sc;
710 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
715 timeout = 4; /* tries */
717 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
718 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
721 if (err == 0 && sc->actlen > 0) {
722 sid_type = sc->buffer[0] & 0x1F;
723 if (sid_type == 0x05)
726 } else if (err != ERR_CSW_FAILED)
727 break; /* non retryable error */
728 usb_pause_mtx(NULL, hz);
735 usb_msc_get_max_lun(struct usb_device *udev, uint8_t iface_index)
737 struct usb_device_request req;
742 /* The Get Max Lun command is a class-specific request. */
743 req.bmRequestType = UT_READ_CLASS_INTERFACE;
744 req.bRequest = 0xFE; /* GET_MAX_LUN */
745 USETW(req.wValue, 0);
746 req.wIndex[0] = iface_index;
748 USETW(req.wLength, 1);
750 err = usbd_do_request(udev, NULL, &req, &buf);
758 usb_msc_auto_quirk(struct usb_device *udev, uint8_t iface_index)
760 struct bbb_transfer *sc;
762 uint8_t is_no_direct;
766 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
771 * Some devices need a delay after that the configuration
772 * value is set to function properly:
774 usb_pause_mtx(NULL, hz);
776 if (usb_msc_get_max_lun(udev, iface_index) == 0) {
777 DPRINTF("Device has only got one LUN.\n");
778 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_GETMAXLUN);
782 for (timeout = 4; timeout != 0; timeout--) {
783 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
784 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
787 if (err == 0 && sc->actlen > 0) {
788 sid_type = sc->buffer[0] & 0x1F;
789 if (sid_type == 0x00)
792 } else if (err != ERR_CSW_FAILED) {
793 DPRINTF("Device is not responding "
794 "properly to SCSI INQUIRY command.\n");
795 goto error; /* non retryable error */
797 usb_pause_mtx(NULL, hz);
801 DPRINTF("Device is not direct access.\n");
805 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
806 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
810 if (err != ERR_CSW_FAILED)
812 DPRINTF("Test unit ready failed\n");
815 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
816 &scsi_prevent_removal, sizeof(scsi_prevent_removal),
820 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
821 &scsi_allow_removal, sizeof(scsi_allow_removal),
826 if (err != ERR_CSW_FAILED)
828 DPRINTF("Device doesn't handle prevent and allow removal\n");
829 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
835 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
836 &scsi_sync_cache, sizeof(scsi_sync_cache),
841 if (err != ERR_CSW_FAILED)
844 DPRINTF("Device doesn't handle synchronize cache\n");
846 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
850 * Certain Kingston memory sticks fail the first
851 * read capacity after a synchronize cache command
852 * has been issued. Disable the synchronize cache
853 * command for such devices.
856 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
857 &scsi_read_capacity, sizeof(scsi_read_capacity),
861 if (err != ERR_CSW_FAILED)
864 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
865 &scsi_read_capacity, sizeof(scsi_read_capacity),
870 goto retry_sync_cache;
872 DPRINTF("Device most likely doesn't "
873 "handle synchronize cache\n");
875 usbd_add_dynamic_quirk(udev,
876 UQ_MSC_NO_SYNC_CACHE);
878 if (err != ERR_CSW_FAILED)
884 /* clear sense status of any failed commands on the device */
886 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
887 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
890 DPRINTF("Inquiry = %d\n", err);
894 if (err != ERR_CSW_FAILED)
898 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
899 SCSI_SENSE_LEN, &scsi_request_sense,
900 sizeof(scsi_request_sense), USB_MS_HZ);
902 DPRINTF("Request sense = %d\n", err);
906 if (err != ERR_CSW_FAILED)
917 DPRINTF("Device did not respond, enabling all quirks\n");
919 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
920 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
921 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_TEST_UNIT_READY);
923 /* Need to re-enumerate the device */
924 usbd_req_re_enumerate(udev, NULL);
926 return (USB_ERR_STALLED);
930 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method)
932 struct bbb_transfer *sc;
935 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
937 return (USB_ERR_INVAL);
940 case MSC_EJECT_STOPUNIT:
941 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
942 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
944 DPRINTF("Test unit ready status: %s\n", usbd_errstr(err));
945 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
946 &scsi_start_stop_unit, sizeof(scsi_start_stop_unit),
949 case MSC_EJECT_REZERO:
950 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
951 &scsi_rezero_init, sizeof(scsi_rezero_init),
954 case MSC_EJECT_ZTESTOR:
955 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
956 &scsi_ztestor_eject, sizeof(scsi_ztestor_eject),
959 case MSC_EJECT_CMOTECH:
960 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
961 &scsi_cmotech_eject, sizeof(scsi_cmotech_eject),
964 case MSC_EJECT_HUAWEI:
965 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
966 &scsi_huawei_eject, sizeof(scsi_huawei_eject),
969 case MSC_EJECT_HUAWEI2:
970 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
971 &scsi_huawei_eject2, sizeof(scsi_huawei_eject2),
976 * TCTMobile needs DIR_IN flag. To get it, we
977 * supply a dummy data with the command.
979 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
980 sc->buffer_size, &scsi_tct_eject,
981 sizeof(scsi_tct_eject), USB_MS_HZ);
984 DPRINTF("Unknown eject method (%d)\n", method);
986 return (USB_ERR_INVAL);
989 DPRINTF("Eject CD command status: %s\n", usbd_errstr(err));
996 usb_dymo_eject(struct usb_device *udev, uint8_t iface_index)
998 static const uint8_t data[3] = { 0x1b, 0x5a, 0x01 };
999 struct bbb_transfer *sc;
1002 sc = bbb_attach(udev, iface_index, UICLASS_HID);
1004 return (USB_ERR_INVAL);
1005 err = bbb_raw_write(sc, data, sizeof(data), USB_MS_HZ);
1011 usb_msc_read_10(struct usb_device *udev, uint8_t iface_index,
1012 uint32_t lba, uint32_t blocks, void *buffer)
1014 struct bbb_transfer *sc;
1018 cmd[0] = 0x28; /* READ_10 */
1025 cmd[7] = blocks >> 8;
1029 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1031 return (USB_ERR_INVAL);
1033 err = bbb_command_start(sc, DIR_IN, 0, buffer,
1034 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
1042 usb_msc_write_10(struct usb_device *udev, uint8_t iface_index,
1043 uint32_t lba, uint32_t blocks, void *buffer)
1045 struct bbb_transfer *sc;
1049 cmd[0] = 0x2a; /* WRITE_10 */
1056 cmd[7] = blocks >> 8;
1060 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1062 return (USB_ERR_INVAL);
1064 err = bbb_command_start(sc, DIR_OUT, 0, buffer,
1065 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
1073 usb_msc_read_capacity(struct usb_device *udev, uint8_t iface_index,
1074 uint32_t *lba_last, uint32_t *block_size)
1076 struct bbb_transfer *sc;
1079 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1081 return (USB_ERR_INVAL);
1083 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
1084 &scsi_read_capacity, sizeof(scsi_read_capacity),
1088 (sc->buffer[0] << 24) |
1089 (sc->buffer[1] << 16) |
1090 (sc->buffer[2] << 8) |
1094 (sc->buffer[4] << 24) |
1095 (sc->buffer[5] << 16) |
1096 (sc->buffer[6] << 8) |
1099 /* we currently only support one block size */
1100 if (*block_size != SCSI_FIXED_BLOCK_SIZE)
1101 err = USB_ERR_INVAL;