3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008,2011 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * The following file contains code that will detect USB autoinstall
33 * TODO: Potentially we could add code to automatically detect USB
34 * mass storage quirks for not supported SCSI commands!
37 #ifdef USB_GLOBAL_INCLUDE_FILE
38 #include USB_GLOBAL_INCLUDE_FILE
40 #include <sys/stdint.h>
41 #include <sys/stddef.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/condvar.h>
52 #include <sys/sysctl.h>
54 #include <sys/unistd.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
63 #define USB_DEBUG_VAR usb_debug
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_process.h>
67 #include <dev/usb/usb_transfer.h>
68 #include <dev/usb/usb_msctest.h>
69 #include <dev/usb/usb_debug.h>
70 #include <dev/usb/usb_device.h>
71 #include <dev/usb/usb_request.h>
72 #include <dev/usb/usb_util.h>
73 #include <dev/usb/quirk/usb_quirk.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
92 #define SCSI_MAX_LEN MAX(SCSI_FIXED_BLOCK_SIZE, USB_MSCTEST_BULK_SIZE)
93 #define SCSI_INQ_LEN 0x24
94 #define SCSI_SENSE_LEN 0xFF
95 #define SCSI_FIXED_BLOCK_SIZE 512 /* bytes */
97 static uint8_t scsi_test_unit_ready[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
98 static uint8_t scsi_inquiry[] = { 0x12, 0x00, 0x00, 0x00, SCSI_INQ_LEN, 0x00 };
99 static uint8_t scsi_rezero_init[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
100 static uint8_t scsi_start_stop_unit[] = { 0x1b, 0x00, 0x00, 0x00, 0x02, 0x00 };
101 static uint8_t scsi_ztestor_eject[] = { 0x85, 0x01, 0x01, 0x01, 0x18, 0x01,
102 0x01, 0x01, 0x01, 0x01, 0x00, 0x00 };
103 static uint8_t scsi_cmotech_eject[] = { 0xff, 0x52, 0x44, 0x45, 0x56, 0x43,
105 static uint8_t scsi_huawei_eject[] = { 0x11, 0x06, 0x00, 0x00, 0x00, 0x00,
106 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107 0x00, 0x00, 0x00, 0x00 };
108 static uint8_t scsi_huawei_eject2[] = { 0x11, 0x06, 0x20, 0x00, 0x00, 0x01,
109 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110 0x00, 0x00, 0x00, 0x00 };
111 static uint8_t scsi_tct_eject[] = { 0x06, 0xf5, 0x04, 0x02, 0x52, 0x70 };
112 static uint8_t scsi_sync_cache[] = { 0x35, 0x00, 0x00, 0x00, 0x00, 0x00,
113 0x00, 0x00, 0x00, 0x00 };
114 static uint8_t scsi_request_sense[] = { 0x03, 0x00, 0x00, 0x00, 0x12, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
116 static uint8_t scsi_read_capacity[] = { 0x25, 0x00, 0x00, 0x00, 0x00, 0x00,
117 0x00, 0x00, 0x00, 0x00 };
118 static uint8_t scsi_prevent_removal[] = { 0x1e, 0, 0, 0, 1, 0 };
119 static uint8_t scsi_allow_removal[] = { 0x1e, 0, 0, 0, 0, 0 };
121 #ifndef USB_MSCTEST_BULK_SIZE
122 #define USB_MSCTEST_BULK_SIZE 64 /* dummy */
125 #define ERR_CSW_FAILED -1
127 /* Command Block Wrapper */
129 uDWord dCBWSignature;
130 #define CBWSIGNATURE 0x43425355
132 uDWord dCBWDataTransferLength;
134 #define CBWFLAGS_OUT 0x00
135 #define CBWFLAGS_IN 0x80
138 #define CBWCDBLENGTH 16
139 uByte CBWCDB[CBWCDBLENGTH];
142 /* Command Status Wrapper */
144 uDWord dCSWSignature;
145 #define CSWSIGNATURE 0x53425355
147 uDWord dCSWDataResidue;
149 #define CSWSTATUS_GOOD 0x0
150 #define CSWSTATUS_FAILED 0x1
151 #define CSWSTATUS_PHASE 0x2
154 struct bbb_transfer {
160 struct usb_xfer *xfer[ST_MAX];
164 usb_size_t data_len; /* bytes */
165 usb_size_t data_rem; /* bytes */
166 usb_timeout_t data_timeout; /* ms */
167 usb_frlength_t actlen; /* bytes */
168 usb_frlength_t buffer_size; /* bytes */
170 uint8_t cmd_len; /* bytes */
180 static usb_callback_t bbb_command_callback;
181 static usb_callback_t bbb_data_read_callback;
182 static usb_callback_t bbb_data_rd_cs_callback;
183 static usb_callback_t bbb_data_write_callback;
184 static usb_callback_t bbb_data_wr_cs_callback;
185 static usb_callback_t bbb_status_callback;
186 static usb_callback_t bbb_raw_write_callback;
188 static void bbb_done(struct bbb_transfer *, int);
189 static void bbb_transfer_start(struct bbb_transfer *, uint8_t);
190 static void bbb_data_clear_stall_callback(struct usb_xfer *, uint8_t,
192 static int bbb_command_start(struct bbb_transfer *, uint8_t, uint8_t,
193 void *, size_t, void *, size_t, usb_timeout_t);
194 static struct bbb_transfer *bbb_attach(struct usb_device *, uint8_t, uint8_t);
195 static void bbb_detach(struct bbb_transfer *);
197 static const struct usb_config bbb_config[ST_MAX] = {
200 .endpoint = UE_ADDR_ANY,
201 .direction = UE_DIR_OUT,
202 .bufsize = sizeof(struct bbb_cbw),
203 .callback = &bbb_command_callback,
204 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
209 .endpoint = UE_ADDR_ANY,
210 .direction = UE_DIR_IN,
211 .bufsize = SCSI_MAX_LEN,
212 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
213 .callback = &bbb_data_read_callback,
214 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
219 .endpoint = 0x00, /* Control pipe */
220 .direction = UE_DIR_ANY,
221 .bufsize = sizeof(struct usb_device_request),
222 .callback = &bbb_data_rd_cs_callback,
223 .timeout = 1 * USB_MS_HZ, /* 1 second */
228 .endpoint = UE_ADDR_ANY,
229 .direction = UE_DIR_OUT,
230 .bufsize = SCSI_MAX_LEN,
231 .flags = {.ext_buffer = 1,.proxy_buffer = 1,},
232 .callback = &bbb_data_write_callback,
233 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
238 .endpoint = 0x00, /* Control pipe */
239 .direction = UE_DIR_ANY,
240 .bufsize = sizeof(struct usb_device_request),
241 .callback = &bbb_data_wr_cs_callback,
242 .timeout = 1 * USB_MS_HZ, /* 1 second */
247 .endpoint = UE_ADDR_ANY,
248 .direction = UE_DIR_IN,
249 .bufsize = sizeof(struct bbb_csw),
250 .flags = {.short_xfer_ok = 1,},
251 .callback = &bbb_status_callback,
252 .timeout = 1 * USB_MS_HZ, /* 1 second */
256 static const struct usb_config bbb_raw_config[1] = {
258 .type = UE_BULK_INTR,
259 .endpoint = UE_ADDR_ANY,
260 .direction = UE_DIR_OUT,
261 .bufsize = SCSI_MAX_LEN,
262 .flags = {.ext_buffer = 1,.proxy_buffer = 1,},
263 .callback = &bbb_raw_write_callback,
264 .timeout = 1 * USB_MS_HZ, /* 1 second */
269 bbb_done(struct bbb_transfer *sc, int error)
272 sc->state = ST_COMMAND;
278 bbb_transfer_start(struct bbb_transfer *sc, uint8_t xfer_index)
280 sc->state = xfer_index;
281 usbd_transfer_start(sc->xfer[xfer_index]);
285 bbb_data_clear_stall_callback(struct usb_xfer *xfer,
286 uint8_t next_xfer, uint8_t stall_xfer)
288 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
290 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) {
291 switch (USB_GET_STATE(xfer)) {
293 case USB_ST_TRANSFERRED:
294 bbb_transfer_start(sc, next_xfer);
297 bbb_done(sc, USB_ERR_STALLED);
304 bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
306 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
309 switch (USB_GET_STATE(xfer)) {
310 case USB_ST_TRANSFERRED:
312 (sc, ((sc->dir == DIR_IN) ? ST_DATA_RD :
313 (sc->dir == DIR_OUT) ? ST_DATA_WR :
319 tag = UGETDW(sc->cbw->dCBWTag) + 1;
320 USETDW(sc->cbw->dCBWSignature, CBWSIGNATURE);
321 USETDW(sc->cbw->dCBWTag, tag);
322 USETDW(sc->cbw->dCBWDataTransferLength, (uint32_t)sc->data_len);
323 sc->cbw->bCBWFlags = ((sc->dir == DIR_IN) ? CBWFLAGS_IN : CBWFLAGS_OUT);
324 sc->cbw->bCBWLUN = sc->lun;
325 sc->cbw->bCDBLength = sc->cmd_len;
326 if (sc->cbw->bCDBLength > sizeof(sc->cbw->CBWCDB)) {
327 sc->cbw->bCDBLength = sizeof(sc->cbw->CBWCDB);
328 DPRINTFN(0, "Truncating long command\n");
330 usbd_xfer_set_frame_len(xfer, 0,
331 sizeof(struct bbb_cbw));
332 usbd_transfer_submit(xfer);
342 bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
344 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
345 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
348 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
350 switch (USB_GET_STATE(xfer)) {
351 case USB_ST_TRANSFERRED:
352 sc->data_rem -= actlen;
353 sc->data_ptr += actlen;
354 sc->actlen += actlen;
356 if (actlen < sumlen) {
361 DPRINTF("max_bulk=%d, data_rem=%d\n",
362 max_bulk, sc->data_rem);
364 if (sc->data_rem == 0) {
365 bbb_transfer_start(sc, ST_STATUS);
368 if (max_bulk > sc->data_rem) {
369 max_bulk = sc->data_rem;
371 usbd_xfer_set_timeout(xfer, sc->data_timeout);
372 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
373 usbd_transfer_submit(xfer);
377 if (error == USB_ERR_CANCELLED) {
380 bbb_transfer_start(sc, ST_DATA_RD_CS);
387 bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
389 bbb_data_clear_stall_callback(xfer, ST_STATUS,
394 bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
396 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
397 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
400 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
402 switch (USB_GET_STATE(xfer)) {
403 case USB_ST_TRANSFERRED:
404 sc->data_rem -= actlen;
405 sc->data_ptr += actlen;
406 sc->actlen += actlen;
408 if (actlen < sumlen) {
413 DPRINTF("max_bulk=%d, data_rem=%d\n",
414 max_bulk, sc->data_rem);
416 if (sc->data_rem == 0) {
417 bbb_transfer_start(sc, ST_STATUS);
420 if (max_bulk > sc->data_rem) {
421 max_bulk = sc->data_rem;
423 usbd_xfer_set_timeout(xfer, sc->data_timeout);
424 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
425 usbd_transfer_submit(xfer);
429 if (error == USB_ERR_CANCELLED) {
432 bbb_transfer_start(sc, ST_DATA_WR_CS);
439 bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
441 bbb_data_clear_stall_callback(xfer, ST_STATUS,
446 bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
448 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
452 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
454 switch (USB_GET_STATE(xfer)) {
455 case USB_ST_TRANSFERRED:
457 /* very simple status check */
459 if (actlen < (int)sizeof(struct bbb_csw)) {
460 bbb_done(sc, USB_ERR_SHORT_XFER);
461 } else if (sc->csw->bCSWStatus == CSWSTATUS_GOOD) {
462 bbb_done(sc, 0); /* success */
464 bbb_done(sc, ERR_CSW_FAILED); /* error */
469 usbd_xfer_set_frame_len(xfer, 0,
470 sizeof(struct bbb_csw));
471 usbd_transfer_submit(xfer);
475 DPRINTF("Failed to read CSW: %s, try %d\n",
476 usbd_errstr(error), sc->status_try);
478 if (error == USB_ERR_CANCELLED || sc->status_try) {
482 bbb_transfer_start(sc, ST_DATA_RD_CS);
489 bbb_raw_write_callback(struct usb_xfer *xfer, usb_error_t error)
491 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
492 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
495 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
497 switch (USB_GET_STATE(xfer)) {
498 case USB_ST_TRANSFERRED:
499 sc->data_rem -= actlen;
500 sc->data_ptr += actlen;
501 sc->actlen += actlen;
503 if (actlen < sumlen) {
508 DPRINTF("max_bulk=%d, data_rem=%d\n",
509 max_bulk, sc->data_rem);
511 if (sc->data_rem == 0) {
515 if (max_bulk > sc->data_rem) {
516 max_bulk = sc->data_rem;
518 usbd_xfer_set_timeout(xfer, sc->data_timeout);
519 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
520 usbd_transfer_submit(xfer);
529 /*------------------------------------------------------------------------*
530 * bbb_command_start - execute a SCSI command synchronously
535 *------------------------------------------------------------------------*/
537 bbb_command_start(struct bbb_transfer *sc, uint8_t dir, uint8_t lun,
538 void *data_ptr, size_t data_len, void *cmd_ptr, size_t cmd_len,
539 usb_timeout_t data_timeout)
542 sc->dir = data_len ? dir : DIR_NONE;
543 sc->data_ptr = data_ptr;
544 sc->data_len = data_len;
545 sc->data_rem = data_len;
546 sc->data_timeout = (data_timeout + USB_MS_HZ);
549 sc->cmd_len = cmd_len;
550 memset(&sc->cbw->CBWCDB, 0, sizeof(sc->cbw->CBWCDB));
551 memcpy(&sc->cbw->CBWCDB, cmd_ptr, cmd_len);
552 DPRINTFN(1, "SCSI cmd = %*D\n", (int)cmd_len, (char *)sc->cbw->CBWCDB, ":");
554 USB_MTX_LOCK(&sc->mtx);
555 usbd_transfer_start(sc->xfer[sc->state]);
557 while (usbd_transfer_pending(sc->xfer[sc->state])) {
558 cv_wait(&sc->cv, &sc->mtx);
560 USB_MTX_UNLOCK(&sc->mtx);
564 /*------------------------------------------------------------------------*
565 * bbb_raw_write - write a raw BULK message synchronously
570 *------------------------------------------------------------------------*/
572 bbb_raw_write(struct bbb_transfer *sc, const void *data_ptr, size_t data_len,
573 usb_timeout_t data_timeout)
575 sc->data_ptr = __DECONST(void *, data_ptr);
576 sc->data_len = data_len;
577 sc->data_rem = data_len;
578 sc->data_timeout = (data_timeout + USB_MS_HZ);
582 DPRINTFN(1, "BULK DATA = %*D\n", (int)data_len,
583 (const char *)data_ptr, ":");
585 USB_MTX_LOCK(&sc->mtx);
586 usbd_transfer_start(sc->xfer[0]);
587 while (usbd_transfer_pending(sc->xfer[0]))
588 cv_wait(&sc->cv, &sc->mtx);
589 USB_MTX_UNLOCK(&sc->mtx);
593 static struct bbb_transfer *
594 bbb_attach(struct usb_device *udev, uint8_t iface_index,
595 uint8_t bInterfaceClass)
597 struct usb_interface *iface;
598 struct usb_interface_descriptor *id;
599 const struct usb_config *pconfig;
600 struct bbb_transfer *sc;
604 #if USB_HAVE_MSCTEST_DETACH
607 /* Prevent re-enumeration */
608 do_unlock = usbd_enum_lock(udev);
611 * Make sure any driver which is hooked up to this interface,
612 * like umass is gone:
614 usb_detach_device(udev, iface_index, 0);
617 usbd_enum_unlock(udev);
620 iface = usbd_get_iface(udev, iface_index);
625 if (id == NULL || id->bInterfaceClass != bInterfaceClass)
628 switch (id->bInterfaceClass) {
630 switch (id->bInterfaceSubClass) {
631 case UISUBCLASS_SCSI:
633 case UISUBCLASS_SFF8020I:
634 case UISUBCLASS_SFF8070I:
639 switch (id->bInterfaceProtocol) {
640 case UIPROTO_MASS_BBB_OLD:
641 case UIPROTO_MASS_BBB:
646 pconfig = bbb_config;
650 switch (id->bInterfaceSubClass) {
656 pconfig = bbb_raw_config;
663 sc = malloc(sizeof(*sc), M_USB, M_WAITOK | M_ZERO);
664 mtx_init(&sc->mtx, "USB autoinstall", NULL, MTX_DEF);
665 cv_init(&sc->cv, "WBBB");
667 err = usbd_transfer_setup(udev, &iface_index, sc->xfer, pconfig,
668 nconfig, sc, &sc->mtx);
673 switch (id->bInterfaceClass) {
675 /* store pointer to DMA buffers */
676 sc->buffer = usbd_xfer_get_frame_buffer(
677 sc->xfer[ST_DATA_RD], 0);
679 usbd_xfer_max_len(sc->xfer[ST_DATA_RD]);
680 sc->cbw = usbd_xfer_get_frame_buffer(
681 sc->xfer[ST_COMMAND], 0);
682 sc->csw = usbd_xfer_get_frame_buffer(
683 sc->xfer[ST_STATUS], 0);
692 bbb_detach(struct bbb_transfer *sc)
694 usbd_transfer_unsetup(sc->xfer, ST_MAX);
695 mtx_destroy(&sc->mtx);
700 /*------------------------------------------------------------------------*
704 * 1: This interface is an auto install disk (CD-ROM)
705 * 0: Not an auto install disk.
706 *------------------------------------------------------------------------*/
708 usb_iface_is_cdrom(struct usb_device *udev, uint8_t iface_index)
710 struct bbb_transfer *sc;
716 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
721 timeout = 4; /* tries */
723 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
724 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
727 if (err == 0 && sc->actlen > 0) {
728 sid_type = sc->buffer[0] & 0x1F;
729 if (sid_type == 0x05)
732 } else if (err != ERR_CSW_FAILED)
733 break; /* non retryable error */
734 usb_pause_mtx(NULL, hz);
741 usb_msc_get_max_lun(struct usb_device *udev, uint8_t iface_index)
743 struct usb_device_request req;
747 /* The Get Max Lun command is a class-specific request. */
748 req.bmRequestType = UT_READ_CLASS_INTERFACE;
749 req.bRequest = 0xFE; /* GET_MAX_LUN */
750 USETW(req.wValue, 0);
751 req.wIndex[0] = iface_index;
753 USETW(req.wLength, 1);
755 err = usbd_do_request(udev, NULL, &req, &buf);
763 usb_msc_auto_quirk(struct usb_device *udev, uint8_t iface_index)
765 struct bbb_transfer *sc;
767 uint8_t is_no_direct;
771 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
776 * Some devices need a delay after that the configuration
777 * value is set to function properly:
779 usb_pause_mtx(NULL, hz);
781 if (usb_msc_get_max_lun(udev, iface_index) == 0) {
782 DPRINTF("Device has only got one LUN.\n");
783 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_GETMAXLUN);
787 for (timeout = 4; timeout != 0; timeout--) {
788 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
789 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
792 if (err == 0 && sc->actlen > 0) {
793 sid_type = sc->buffer[0] & 0x1F;
794 if (sid_type == 0x00)
797 } else if (err != ERR_CSW_FAILED) {
798 DPRINTF("Device is not responding "
799 "properly to SCSI INQUIRY command.\n");
800 goto error; /* non retryable error */
802 usb_pause_mtx(NULL, hz);
806 DPRINTF("Device is not direct access.\n");
810 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
811 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
815 if (err != ERR_CSW_FAILED)
817 DPRINTF("Test unit ready failed\n");
820 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
821 &scsi_prevent_removal, sizeof(scsi_prevent_removal),
825 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
826 &scsi_allow_removal, sizeof(scsi_allow_removal),
831 if (err != ERR_CSW_FAILED)
833 DPRINTF("Device doesn't handle prevent and allow removal\n");
834 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
840 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
841 &scsi_sync_cache, sizeof(scsi_sync_cache),
845 if (err != ERR_CSW_FAILED)
848 DPRINTF("Device doesn't handle synchronize cache\n");
850 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
853 * Certain Kingston memory sticks fail the first
854 * read capacity after a synchronize cache command
855 * has been issued. Disable the synchronize cache
856 * command for such devices.
859 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
860 &scsi_read_capacity, sizeof(scsi_read_capacity),
864 if (err != ERR_CSW_FAILED)
867 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
868 &scsi_read_capacity, sizeof(scsi_read_capacity),
873 goto retry_sync_cache;
875 DPRINTF("Device most likely doesn't "
876 "handle synchronize cache\n");
878 usbd_add_dynamic_quirk(udev,
879 UQ_MSC_NO_SYNC_CACHE);
881 if (err != ERR_CSW_FAILED)
887 /* clear sense status of any failed commands on the device */
889 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
890 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
893 DPRINTF("Inquiry = %d\n", err);
896 if (err != ERR_CSW_FAILED)
900 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
901 SCSI_SENSE_LEN, &scsi_request_sense,
902 sizeof(scsi_request_sense), USB_MS_HZ);
904 DPRINTF("Request sense = %d\n", err);
907 if (err != ERR_CSW_FAILED)
918 DPRINTF("Device did not respond, enabling all quirks\n");
920 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
921 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
922 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_TEST_UNIT_READY);
924 /* Need to re-enumerate the device */
925 usbd_req_re_enumerate(udev, NULL);
927 return (USB_ERR_STALLED);
931 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method)
933 struct bbb_transfer *sc;
936 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
938 return (USB_ERR_INVAL);
941 case MSC_EJECT_STOPUNIT:
942 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
943 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
945 DPRINTF("Test unit ready status: %s\n", usbd_errstr(err));
946 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
947 &scsi_start_stop_unit, sizeof(scsi_start_stop_unit),
950 case MSC_EJECT_REZERO:
951 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
952 &scsi_rezero_init, sizeof(scsi_rezero_init),
955 case MSC_EJECT_ZTESTOR:
956 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
957 &scsi_ztestor_eject, sizeof(scsi_ztestor_eject),
960 case MSC_EJECT_CMOTECH:
961 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
962 &scsi_cmotech_eject, sizeof(scsi_cmotech_eject),
965 case MSC_EJECT_HUAWEI:
966 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
967 &scsi_huawei_eject, sizeof(scsi_huawei_eject),
970 case MSC_EJECT_HUAWEI2:
971 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
972 &scsi_huawei_eject2, sizeof(scsi_huawei_eject2),
977 * TCTMobile needs DIR_IN flag. To get it, we
978 * supply a dummy data with the command.
980 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
981 sc->buffer_size, &scsi_tct_eject,
982 sizeof(scsi_tct_eject), USB_MS_HZ);
985 DPRINTF("Unknown eject method (%d)\n", method);
987 return (USB_ERR_INVAL);
990 DPRINTF("Eject CD command status: %s\n", usbd_errstr(err));
997 usb_dymo_eject(struct usb_device *udev, uint8_t iface_index)
999 static const uint8_t data[3] = { 0x1b, 0x5a, 0x01 };
1000 struct bbb_transfer *sc;
1003 sc = bbb_attach(udev, iface_index, UICLASS_HID);
1005 return (USB_ERR_INVAL);
1006 err = bbb_raw_write(sc, data, sizeof(data), USB_MS_HZ);
1012 usb_msc_read_10(struct usb_device *udev, uint8_t iface_index,
1013 uint32_t lba, uint32_t blocks, void *buffer)
1015 struct bbb_transfer *sc;
1019 cmd[0] = 0x28; /* READ_10 */
1026 cmd[7] = blocks >> 8;
1030 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1032 return (USB_ERR_INVAL);
1034 err = bbb_command_start(sc, DIR_IN, 0, buffer,
1035 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
1043 usb_msc_write_10(struct usb_device *udev, uint8_t iface_index,
1044 uint32_t lba, uint32_t blocks, void *buffer)
1046 struct bbb_transfer *sc;
1050 cmd[0] = 0x2a; /* WRITE_10 */
1057 cmd[7] = blocks >> 8;
1061 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1063 return (USB_ERR_INVAL);
1065 err = bbb_command_start(sc, DIR_OUT, 0, buffer,
1066 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
1074 usb_msc_read_capacity(struct usb_device *udev, uint8_t iface_index,
1075 uint32_t *lba_last, uint32_t *block_size)
1077 struct bbb_transfer *sc;
1080 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1082 return (USB_ERR_INVAL);
1084 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
1085 &scsi_read_capacity, sizeof(scsi_read_capacity),
1089 (sc->buffer[0] << 24) |
1090 (sc->buffer[1] << 16) |
1091 (sc->buffer[2] << 8) |
1095 (sc->buffer[4] << 24) |
1096 (sc->buffer[5] << 16) |
1097 (sc->buffer[6] << 8) |
1100 /* we currently only support one block size */
1101 if (*block_size != SCSI_FIXED_BLOCK_SIZE)
1102 err = USB_ERR_INVAL;