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 #ifdef USB_GLOBAL_INCLUDE_FILE
36 #include USB_GLOBAL_INCLUDE_FILE
38 #include <sys/stdint.h>
39 #include <sys/stddef.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
61 #define USB_DEBUG_VAR usb_debug
63 #include <dev/usb/usb_busdma.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_transfer.h>
66 #include <dev/usb/usb_msctest.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_device.h>
69 #include <dev/usb/usb_request.h>
70 #include <dev/usb/usb_util.h>
71 #include <dev/usb/quirk/usb_quirk.h>
72 #endif /* USB_GLOBAL_INCLUDE_FILE */
90 #define SCSI_MAX_LEN MAX(SCSI_FIXED_BLOCK_SIZE, USB_MSCTEST_BULK_SIZE)
91 #define SCSI_INQ_LEN 0x24
92 #define SCSI_SENSE_LEN 0xFF
93 #define SCSI_FIXED_BLOCK_SIZE 512 /* bytes */
95 static uint8_t scsi_test_unit_ready[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
96 static uint8_t scsi_inquiry[] = { 0x12, 0x00, 0x00, 0x00, SCSI_INQ_LEN, 0x00 };
97 static uint8_t scsi_rezero_init[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
98 static uint8_t scsi_start_stop_unit[] = { 0x1b, 0x00, 0x00, 0x00, 0x02, 0x00 };
99 static uint8_t scsi_ztestor_eject[] = { 0x85, 0x01, 0x01, 0x01, 0x18, 0x01,
100 0x01, 0x01, 0x01, 0x01, 0x00, 0x00 };
101 static uint8_t scsi_cmotech_eject[] = { 0xff, 0x52, 0x44, 0x45, 0x56, 0x43,
103 static uint8_t scsi_huawei_eject[] = { 0x11, 0x06, 0x00, 0x00, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00 };
106 static uint8_t scsi_huawei_eject2[] = { 0x11, 0x06, 0x20, 0x00, 0x00, 0x01,
107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 0x00, 0x00, 0x00, 0x00 };
109 static uint8_t scsi_tct_eject[] = { 0x06, 0xf5, 0x04, 0x02, 0x52, 0x70 };
110 static uint8_t scsi_sync_cache[] = { 0x35, 0x00, 0x00, 0x00, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00 };
112 static uint8_t scsi_request_sense[] = { 0x03, 0x00, 0x00, 0x00, 0x12, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
114 static uint8_t scsi_read_capacity[] = { 0x25, 0x00, 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00 };
116 static uint8_t scsi_prevent_removal[] = { 0x1e, 0, 0, 0, 1, 0 };
117 static uint8_t scsi_allow_removal[] = { 0x1e, 0, 0, 0, 0, 0 };
119 #ifndef USB_MSCTEST_BULK_SIZE
120 #define USB_MSCTEST_BULK_SIZE 64 /* dummy */
123 #define ERR_CSW_FAILED -1
125 /* Command Block Wrapper */
127 uDWord dCBWSignature;
128 #define CBWSIGNATURE 0x43425355
130 uDWord dCBWDataTransferLength;
132 #define CBWFLAGS_OUT 0x00
133 #define CBWFLAGS_IN 0x80
136 #define CBWCDBLENGTH 16
137 uByte CBWCDB[CBWCDBLENGTH];
140 /* Command Status Wrapper */
142 uDWord dCSWSignature;
143 #define CSWSIGNATURE 0x53425355
145 uDWord dCSWDataResidue;
147 #define CSWSTATUS_GOOD 0x0
148 #define CSWSTATUS_FAILED 0x1
149 #define CSWSTATUS_PHASE 0x2
152 struct bbb_transfer {
158 struct usb_xfer *xfer[ST_MAX];
162 usb_size_t data_len; /* bytes */
163 usb_size_t data_rem; /* bytes */
164 usb_timeout_t data_timeout; /* ms */
165 usb_frlength_t actlen; /* bytes */
166 usb_frlength_t buffer_size; /* bytes */
168 uint8_t cmd_len; /* bytes */
178 static usb_callback_t bbb_command_callback;
179 static usb_callback_t bbb_data_read_callback;
180 static usb_callback_t bbb_data_rd_cs_callback;
181 static usb_callback_t bbb_data_write_callback;
182 static usb_callback_t bbb_data_wr_cs_callback;
183 static usb_callback_t bbb_status_callback;
184 static usb_callback_t bbb_raw_write_callback;
186 static void bbb_done(struct bbb_transfer *, int);
187 static void bbb_transfer_start(struct bbb_transfer *, uint8_t);
188 static void bbb_data_clear_stall_callback(struct usb_xfer *, uint8_t,
190 static int bbb_command_start(struct bbb_transfer *, uint8_t, uint8_t,
191 void *, size_t, void *, size_t, usb_timeout_t);
192 static struct bbb_transfer *bbb_attach(struct usb_device *, uint8_t, uint8_t);
193 static void bbb_detach(struct bbb_transfer *);
195 static const struct usb_config bbb_config[ST_MAX] = {
199 .endpoint = UE_ADDR_ANY,
200 .direction = UE_DIR_OUT,
201 .bufsize = sizeof(struct bbb_cbw),
202 .callback = &bbb_command_callback,
203 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
208 .endpoint = UE_ADDR_ANY,
209 .direction = UE_DIR_IN,
210 .bufsize = SCSI_MAX_LEN,
211 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
212 .callback = &bbb_data_read_callback,
213 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
218 .endpoint = 0x00, /* Control pipe */
219 .direction = UE_DIR_ANY,
220 .bufsize = sizeof(struct usb_device_request),
221 .callback = &bbb_data_rd_cs_callback,
222 .timeout = 1 * USB_MS_HZ, /* 1 second */
227 .endpoint = UE_ADDR_ANY,
228 .direction = UE_DIR_OUT,
229 .bufsize = SCSI_MAX_LEN,
230 .flags = {.ext_buffer = 1,.proxy_buffer = 1,},
231 .callback = &bbb_data_write_callback,
232 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
237 .endpoint = 0x00, /* Control pipe */
238 .direction = UE_DIR_ANY,
239 .bufsize = sizeof(struct usb_device_request),
240 .callback = &bbb_data_wr_cs_callback,
241 .timeout = 1 * USB_MS_HZ, /* 1 second */
246 .endpoint = UE_ADDR_ANY,
247 .direction = UE_DIR_IN,
248 .bufsize = sizeof(struct bbb_csw),
249 .flags = {.short_xfer_ok = 1,},
250 .callback = &bbb_status_callback,
251 .timeout = 1 * USB_MS_HZ, /* 1 second */
255 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;
748 /* The Get Max Lun command is a class-specific request. */
749 req.bmRequestType = UT_READ_CLASS_INTERFACE;
750 req.bRequest = 0xFE; /* GET_MAX_LUN */
751 USETW(req.wValue, 0);
752 req.wIndex[0] = iface_index;
754 USETW(req.wLength, 1);
756 err = usbd_do_request(udev, NULL, &req, &buf);
764 usb_msc_auto_quirk(struct usb_device *udev, uint8_t iface_index)
766 struct bbb_transfer *sc;
768 uint8_t is_no_direct;
772 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
777 * Some devices need a delay after that the configuration
778 * value is set to function properly:
780 usb_pause_mtx(NULL, hz);
782 if (usb_msc_get_max_lun(udev, iface_index) == 0) {
783 DPRINTF("Device has only got one LUN.\n");
784 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_GETMAXLUN);
788 for (timeout = 4; timeout != 0; timeout--) {
789 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
790 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
793 if (err == 0 && sc->actlen > 0) {
794 sid_type = sc->buffer[0] & 0x1F;
795 if (sid_type == 0x00)
798 } else if (err != ERR_CSW_FAILED) {
799 DPRINTF("Device is not responding "
800 "properly to SCSI INQUIRY command.\n");
801 goto error; /* non retryable error */
803 usb_pause_mtx(NULL, hz);
807 DPRINTF("Device is not direct access.\n");
811 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
812 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
816 if (err != ERR_CSW_FAILED)
818 DPRINTF("Test unit ready failed\n");
821 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
822 &scsi_prevent_removal, sizeof(scsi_prevent_removal),
826 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
827 &scsi_allow_removal, sizeof(scsi_allow_removal),
832 if (err != ERR_CSW_FAILED)
834 DPRINTF("Device doesn't handle prevent and allow removal\n");
835 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
841 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
842 &scsi_sync_cache, sizeof(scsi_sync_cache),
847 if (err != ERR_CSW_FAILED)
850 DPRINTF("Device doesn't handle synchronize cache\n");
852 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
856 * Certain Kingston memory sticks fail the first
857 * read capacity after a synchronize cache command
858 * has been issued. Disable the synchronize cache
859 * command for such devices.
862 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
863 &scsi_read_capacity, sizeof(scsi_read_capacity),
867 if (err != ERR_CSW_FAILED)
870 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
871 &scsi_read_capacity, sizeof(scsi_read_capacity),
876 goto retry_sync_cache;
878 DPRINTF("Device most likely doesn't "
879 "handle synchronize cache\n");
881 usbd_add_dynamic_quirk(udev,
882 UQ_MSC_NO_SYNC_CACHE);
884 if (err != ERR_CSW_FAILED)
890 /* clear sense status of any failed commands on the device */
892 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
893 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
896 DPRINTF("Inquiry = %d\n", err);
900 if (err != ERR_CSW_FAILED)
904 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
905 SCSI_SENSE_LEN, &scsi_request_sense,
906 sizeof(scsi_request_sense), USB_MS_HZ);
908 DPRINTF("Request sense = %d\n", err);
912 if (err != ERR_CSW_FAILED)
923 DPRINTF("Device did not respond, enabling all quirks\n");
925 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
926 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
927 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_TEST_UNIT_READY);
929 /* Need to re-enumerate the device */
930 usbd_req_re_enumerate(udev, NULL);
932 return (USB_ERR_STALLED);
936 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method)
938 struct bbb_transfer *sc;
941 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
943 return (USB_ERR_INVAL);
946 case MSC_EJECT_STOPUNIT:
947 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
948 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
950 DPRINTF("Test unit ready status: %s\n", usbd_errstr(err));
951 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
952 &scsi_start_stop_unit, sizeof(scsi_start_stop_unit),
955 case MSC_EJECT_REZERO:
956 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
957 &scsi_rezero_init, sizeof(scsi_rezero_init),
960 case MSC_EJECT_ZTESTOR:
961 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
962 &scsi_ztestor_eject, sizeof(scsi_ztestor_eject),
965 case MSC_EJECT_CMOTECH:
966 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
967 &scsi_cmotech_eject, sizeof(scsi_cmotech_eject),
970 case MSC_EJECT_HUAWEI:
971 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
972 &scsi_huawei_eject, sizeof(scsi_huawei_eject),
975 case MSC_EJECT_HUAWEI2:
976 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
977 &scsi_huawei_eject2, sizeof(scsi_huawei_eject2),
982 * TCTMobile needs DIR_IN flag. To get it, we
983 * supply a dummy data with the command.
985 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
986 sc->buffer_size, &scsi_tct_eject,
987 sizeof(scsi_tct_eject), USB_MS_HZ);
990 DPRINTF("Unknown eject method (%d)\n", method);
992 return (USB_ERR_INVAL);
995 DPRINTF("Eject CD command status: %s\n", usbd_errstr(err));
1002 usb_dymo_eject(struct usb_device *udev, uint8_t iface_index)
1004 static const uint8_t data[3] = { 0x1b, 0x5a, 0x01 };
1005 struct bbb_transfer *sc;
1008 sc = bbb_attach(udev, iface_index, UICLASS_HID);
1010 return (USB_ERR_INVAL);
1011 err = bbb_raw_write(sc, data, sizeof(data), USB_MS_HZ);
1017 usb_msc_read_10(struct usb_device *udev, uint8_t iface_index,
1018 uint32_t lba, uint32_t blocks, void *buffer)
1020 struct bbb_transfer *sc;
1024 cmd[0] = 0x28; /* READ_10 */
1031 cmd[7] = blocks >> 8;
1035 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1037 return (USB_ERR_INVAL);
1039 err = bbb_command_start(sc, DIR_IN, 0, buffer,
1040 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
1048 usb_msc_write_10(struct usb_device *udev, uint8_t iface_index,
1049 uint32_t lba, uint32_t blocks, void *buffer)
1051 struct bbb_transfer *sc;
1055 cmd[0] = 0x2a; /* WRITE_10 */
1062 cmd[7] = blocks >> 8;
1066 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1068 return (USB_ERR_INVAL);
1070 err = bbb_command_start(sc, DIR_OUT, 0, buffer,
1071 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
1079 usb_msc_read_capacity(struct usb_device *udev, uint8_t iface_index,
1080 uint32_t *lba_last, uint32_t *block_size)
1082 struct bbb_transfer *sc;
1085 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1087 return (USB_ERR_INVAL);
1089 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
1090 &scsi_read_capacity, sizeof(scsi_read_capacity),
1094 (sc->buffer[0] << 24) |
1095 (sc->buffer[1] << 16) |
1096 (sc->buffer[2] << 8) |
1100 (sc->buffer[4] << 24) |
1101 (sc->buffer[5] << 16) |
1102 (sc->buffer[6] << 8) |
1105 /* we currently only support one block size */
1106 if (*block_size != SCSI_FIXED_BLOCK_SIZE)
1107 err = USB_ERR_INVAL;