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] = {
201 .endpoint = UE_ADDR_ANY,
202 .direction = UE_DIR_OUT,
203 .bufsize = sizeof(struct bbb_cbw),
204 .callback = &bbb_command_callback,
205 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
210 .endpoint = UE_ADDR_ANY,
211 .direction = UE_DIR_IN,
212 .bufsize = SCSI_MAX_LEN,
213 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
214 .callback = &bbb_data_read_callback,
215 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
220 .endpoint = 0x00, /* Control pipe */
221 .direction = UE_DIR_ANY,
222 .bufsize = sizeof(struct usb_device_request),
223 .callback = &bbb_data_rd_cs_callback,
224 .timeout = 1 * USB_MS_HZ, /* 1 second */
229 .endpoint = UE_ADDR_ANY,
230 .direction = UE_DIR_OUT,
231 .bufsize = SCSI_MAX_LEN,
232 .flags = {.ext_buffer = 1,.proxy_buffer = 1,},
233 .callback = &bbb_data_write_callback,
234 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
239 .endpoint = 0x00, /* Control pipe */
240 .direction = UE_DIR_ANY,
241 .bufsize = sizeof(struct usb_device_request),
242 .callback = &bbb_data_wr_cs_callback,
243 .timeout = 1 * USB_MS_HZ, /* 1 second */
248 .endpoint = UE_ADDR_ANY,
249 .direction = UE_DIR_IN,
250 .bufsize = sizeof(struct bbb_csw),
251 .flags = {.short_xfer_ok = 1,},
252 .callback = &bbb_status_callback,
253 .timeout = 1 * USB_MS_HZ, /* 1 second */
257 static const struct usb_config bbb_raw_config[1] = {
260 .type = UE_BULK_INTR,
261 .endpoint = UE_ADDR_ANY,
262 .direction = UE_DIR_OUT,
263 .bufsize = SCSI_MAX_LEN,
264 .flags = {.ext_buffer = 1,.proxy_buffer = 1,},
265 .callback = &bbb_raw_write_callback,
266 .timeout = 1 * USB_MS_HZ, /* 1 second */
271 bbb_done(struct bbb_transfer *sc, int error)
274 sc->state = ST_COMMAND;
280 bbb_transfer_start(struct bbb_transfer *sc, uint8_t xfer_index)
282 sc->state = xfer_index;
283 usbd_transfer_start(sc->xfer[xfer_index]);
287 bbb_data_clear_stall_callback(struct usb_xfer *xfer,
288 uint8_t next_xfer, uint8_t stall_xfer)
290 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
292 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) {
293 switch (USB_GET_STATE(xfer)) {
295 case USB_ST_TRANSFERRED:
296 bbb_transfer_start(sc, next_xfer);
299 bbb_done(sc, USB_ERR_STALLED);
306 bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
308 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
311 switch (USB_GET_STATE(xfer)) {
312 case USB_ST_TRANSFERRED:
314 (sc, ((sc->dir == DIR_IN) ? ST_DATA_RD :
315 (sc->dir == DIR_OUT) ? ST_DATA_WR :
321 tag = UGETDW(sc->cbw->dCBWTag) + 1;
322 USETDW(sc->cbw->dCBWSignature, CBWSIGNATURE);
323 USETDW(sc->cbw->dCBWTag, tag);
324 USETDW(sc->cbw->dCBWDataTransferLength, (uint32_t)sc->data_len);
325 sc->cbw->bCBWFlags = ((sc->dir == DIR_IN) ? CBWFLAGS_IN : CBWFLAGS_OUT);
326 sc->cbw->bCBWLUN = sc->lun;
327 sc->cbw->bCDBLength = sc->cmd_len;
328 if (sc->cbw->bCDBLength > sizeof(sc->cbw->CBWCDB)) {
329 sc->cbw->bCDBLength = sizeof(sc->cbw->CBWCDB);
330 DPRINTFN(0, "Truncating long command\n");
332 usbd_xfer_set_frame_len(xfer, 0,
333 sizeof(struct bbb_cbw));
334 usbd_transfer_submit(xfer);
344 bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
346 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
347 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
350 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
352 switch (USB_GET_STATE(xfer)) {
353 case USB_ST_TRANSFERRED:
354 sc->data_rem -= actlen;
355 sc->data_ptr += actlen;
356 sc->actlen += actlen;
358 if (actlen < sumlen) {
363 DPRINTF("max_bulk=%d, data_rem=%d\n",
364 max_bulk, sc->data_rem);
366 if (sc->data_rem == 0) {
367 bbb_transfer_start(sc, ST_STATUS);
370 if (max_bulk > sc->data_rem) {
371 max_bulk = sc->data_rem;
373 usbd_xfer_set_timeout(xfer, sc->data_timeout);
374 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
375 usbd_transfer_submit(xfer);
379 if (error == USB_ERR_CANCELLED) {
382 bbb_transfer_start(sc, ST_DATA_RD_CS);
389 bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
391 bbb_data_clear_stall_callback(xfer, ST_STATUS,
396 bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
398 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
399 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
402 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
404 switch (USB_GET_STATE(xfer)) {
405 case USB_ST_TRANSFERRED:
406 sc->data_rem -= actlen;
407 sc->data_ptr += actlen;
408 sc->actlen += actlen;
410 if (actlen < sumlen) {
415 DPRINTF("max_bulk=%d, data_rem=%d\n",
416 max_bulk, sc->data_rem);
418 if (sc->data_rem == 0) {
419 bbb_transfer_start(sc, ST_STATUS);
422 if (max_bulk > sc->data_rem) {
423 max_bulk = sc->data_rem;
425 usbd_xfer_set_timeout(xfer, sc->data_timeout);
426 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
427 usbd_transfer_submit(xfer);
431 if (error == USB_ERR_CANCELLED) {
434 bbb_transfer_start(sc, ST_DATA_WR_CS);
441 bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
443 bbb_data_clear_stall_callback(xfer, ST_STATUS,
448 bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
450 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
454 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
456 switch (USB_GET_STATE(xfer)) {
457 case USB_ST_TRANSFERRED:
459 /* very simple status check */
461 if (actlen < (int)sizeof(struct bbb_csw)) {
462 bbb_done(sc, USB_ERR_SHORT_XFER);
463 } else if (sc->csw->bCSWStatus == CSWSTATUS_GOOD) {
464 bbb_done(sc, 0); /* success */
466 bbb_done(sc, ERR_CSW_FAILED); /* error */
471 usbd_xfer_set_frame_len(xfer, 0,
472 sizeof(struct bbb_csw));
473 usbd_transfer_submit(xfer);
477 DPRINTF("Failed to read CSW: %s, try %d\n",
478 usbd_errstr(error), sc->status_try);
480 if (error == USB_ERR_CANCELLED || sc->status_try) {
484 bbb_transfer_start(sc, ST_DATA_RD_CS);
491 bbb_raw_write_callback(struct usb_xfer *xfer, usb_error_t error)
493 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
494 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
497 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
499 switch (USB_GET_STATE(xfer)) {
500 case USB_ST_TRANSFERRED:
501 sc->data_rem -= actlen;
502 sc->data_ptr += actlen;
503 sc->actlen += actlen;
505 if (actlen < sumlen) {
510 DPRINTF("max_bulk=%d, data_rem=%d\n",
511 max_bulk, sc->data_rem);
513 if (sc->data_rem == 0) {
517 if (max_bulk > sc->data_rem) {
518 max_bulk = sc->data_rem;
520 usbd_xfer_set_timeout(xfer, sc->data_timeout);
521 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
522 usbd_transfer_submit(xfer);
531 /*------------------------------------------------------------------------*
532 * bbb_command_start - execute a SCSI command synchronously
537 *------------------------------------------------------------------------*/
539 bbb_command_start(struct bbb_transfer *sc, uint8_t dir, uint8_t lun,
540 void *data_ptr, size_t data_len, void *cmd_ptr, size_t cmd_len,
541 usb_timeout_t data_timeout)
544 sc->dir = data_len ? dir : DIR_NONE;
545 sc->data_ptr = data_ptr;
546 sc->data_len = data_len;
547 sc->data_rem = data_len;
548 sc->data_timeout = (data_timeout + USB_MS_HZ);
551 sc->cmd_len = cmd_len;
552 memset(&sc->cbw->CBWCDB, 0, sizeof(sc->cbw->CBWCDB));
553 memcpy(&sc->cbw->CBWCDB, cmd_ptr, cmd_len);
554 DPRINTFN(1, "SCSI cmd = %*D\n", (int)cmd_len, (char *)sc->cbw->CBWCDB, ":");
556 USB_MTX_LOCK(&sc->mtx);
557 usbd_transfer_start(sc->xfer[sc->state]);
559 while (usbd_transfer_pending(sc->xfer[sc->state])) {
560 cv_wait(&sc->cv, &sc->mtx);
562 USB_MTX_UNLOCK(&sc->mtx);
566 /*------------------------------------------------------------------------*
567 * bbb_raw_write - write a raw BULK message synchronously
572 *------------------------------------------------------------------------*/
574 bbb_raw_write(struct bbb_transfer *sc, const void *data_ptr, size_t data_len,
575 usb_timeout_t data_timeout)
577 sc->data_ptr = __DECONST(void *, data_ptr);
578 sc->data_len = data_len;
579 sc->data_rem = data_len;
580 sc->data_timeout = (data_timeout + USB_MS_HZ);
584 DPRINTFN(1, "BULK DATA = %*D\n", (int)data_len,
585 (const char *)data_ptr, ":");
587 USB_MTX_LOCK(&sc->mtx);
588 usbd_transfer_start(sc->xfer[0]);
589 while (usbd_transfer_pending(sc->xfer[0]))
590 cv_wait(&sc->cv, &sc->mtx);
591 USB_MTX_UNLOCK(&sc->mtx);
595 static struct bbb_transfer *
596 bbb_attach(struct usb_device *udev, uint8_t iface_index,
597 uint8_t bInterfaceClass)
599 struct usb_interface *iface;
600 struct usb_interface_descriptor *id;
601 const struct usb_config *pconfig;
602 struct bbb_transfer *sc;
606 #if USB_HAVE_MSCTEST_DETACH
609 /* Prevent re-enumeration */
610 do_unlock = usbd_enum_lock(udev);
613 * Make sure any driver which is hooked up to this interface,
614 * like umass is gone:
616 usb_detach_device(udev, iface_index, 0);
619 usbd_enum_unlock(udev);
622 iface = usbd_get_iface(udev, iface_index);
627 if (id == NULL || id->bInterfaceClass != bInterfaceClass)
630 switch (id->bInterfaceClass) {
632 switch (id->bInterfaceSubClass) {
633 case UISUBCLASS_SCSI:
635 case UISUBCLASS_SFF8020I:
636 case UISUBCLASS_SFF8070I:
641 switch (id->bInterfaceProtocol) {
642 case UIPROTO_MASS_BBB_OLD:
643 case UIPROTO_MASS_BBB:
648 pconfig = bbb_config;
652 switch (id->bInterfaceSubClass) {
658 pconfig = bbb_raw_config;
665 sc = malloc(sizeof(*sc), M_USB, M_WAITOK | M_ZERO);
666 mtx_init(&sc->mtx, "USB autoinstall", NULL, MTX_DEF);
667 cv_init(&sc->cv, "WBBB");
669 err = usbd_transfer_setup(udev, &iface_index, sc->xfer, pconfig,
670 nconfig, sc, &sc->mtx);
675 switch (id->bInterfaceClass) {
677 /* store pointer to DMA buffers */
678 sc->buffer = usbd_xfer_get_frame_buffer(
679 sc->xfer[ST_DATA_RD], 0);
681 usbd_xfer_max_len(sc->xfer[ST_DATA_RD]);
682 sc->cbw = usbd_xfer_get_frame_buffer(
683 sc->xfer[ST_COMMAND], 0);
684 sc->csw = usbd_xfer_get_frame_buffer(
685 sc->xfer[ST_STATUS], 0);
694 bbb_detach(struct bbb_transfer *sc)
696 usbd_transfer_unsetup(sc->xfer, ST_MAX);
697 mtx_destroy(&sc->mtx);
702 /*------------------------------------------------------------------------*
706 * 1: This interface is an auto install disk (CD-ROM)
707 * 0: Not an auto install disk.
708 *------------------------------------------------------------------------*/
710 usb_iface_is_cdrom(struct usb_device *udev, uint8_t iface_index)
712 struct bbb_transfer *sc;
718 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
723 timeout = 4; /* tries */
725 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
726 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
729 if (err == 0 && sc->actlen > 0) {
730 sid_type = sc->buffer[0] & 0x1F;
731 if (sid_type == 0x05)
734 } else if (err != ERR_CSW_FAILED)
735 break; /* non retryable error */
736 usb_pause_mtx(NULL, hz);
743 usb_msc_get_max_lun(struct usb_device *udev, uint8_t iface_index)
745 struct usb_device_request req;
750 /* The Get Max Lun command is a class-specific request. */
751 req.bmRequestType = UT_READ_CLASS_INTERFACE;
752 req.bRequest = 0xFE; /* GET_MAX_LUN */
753 USETW(req.wValue, 0);
754 req.wIndex[0] = iface_index;
756 USETW(req.wLength, 1);
758 err = usbd_do_request(udev, NULL, &req, &buf);
766 usb_msc_auto_quirk(struct usb_device *udev, uint8_t iface_index)
768 struct bbb_transfer *sc;
770 uint8_t is_no_direct;
774 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
779 * Some devices need a delay after that the configuration
780 * value is set to function properly:
782 usb_pause_mtx(NULL, hz);
784 if (usb_msc_get_max_lun(udev, iface_index) == 0) {
785 DPRINTF("Device has only got one LUN.\n");
786 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_GETMAXLUN);
790 for (timeout = 4; timeout != 0; timeout--) {
791 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
792 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
795 if (err == 0 && sc->actlen > 0) {
796 sid_type = sc->buffer[0] & 0x1F;
797 if (sid_type == 0x00)
800 } else if (err != ERR_CSW_FAILED) {
801 DPRINTF("Device is not responding "
802 "properly to SCSI INQUIRY command.\n");
803 goto error; /* non retryable error */
805 usb_pause_mtx(NULL, hz);
809 DPRINTF("Device is not direct access.\n");
813 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
814 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
818 if (err != ERR_CSW_FAILED)
820 DPRINTF("Test unit ready failed\n");
823 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
824 &scsi_prevent_removal, sizeof(scsi_prevent_removal),
828 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
829 &scsi_allow_removal, sizeof(scsi_allow_removal),
834 if (err != ERR_CSW_FAILED)
836 DPRINTF("Device doesn't handle prevent and allow removal\n");
837 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
843 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
844 &scsi_sync_cache, sizeof(scsi_sync_cache),
849 if (err != ERR_CSW_FAILED)
852 DPRINTF("Device doesn't handle synchronize cache\n");
854 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
858 * Certain Kingston memory sticks fail the first
859 * read capacity after a synchronize cache command
860 * has been issued. Disable the synchronize cache
861 * command for such devices.
864 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
865 &scsi_read_capacity, sizeof(scsi_read_capacity),
869 if (err != ERR_CSW_FAILED)
872 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
873 &scsi_read_capacity, sizeof(scsi_read_capacity),
878 goto retry_sync_cache;
880 DPRINTF("Device most likely doesn't "
881 "handle synchronize cache\n");
883 usbd_add_dynamic_quirk(udev,
884 UQ_MSC_NO_SYNC_CACHE);
886 if (err != ERR_CSW_FAILED)
892 /* clear sense status of any failed commands on the device */
894 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
895 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
898 DPRINTF("Inquiry = %d\n", err);
902 if (err != ERR_CSW_FAILED)
906 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
907 SCSI_SENSE_LEN, &scsi_request_sense,
908 sizeof(scsi_request_sense), USB_MS_HZ);
910 DPRINTF("Request sense = %d\n", err);
914 if (err != ERR_CSW_FAILED)
925 DPRINTF("Device did not respond, enabling all quirks\n");
927 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
928 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
929 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_TEST_UNIT_READY);
931 /* Need to re-enumerate the device */
932 usbd_req_re_enumerate(udev, NULL);
934 return (USB_ERR_STALLED);
938 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method)
940 struct bbb_transfer *sc;
943 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
945 return (USB_ERR_INVAL);
948 case MSC_EJECT_STOPUNIT:
949 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
950 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
952 DPRINTF("Test unit ready status: %s\n", usbd_errstr(err));
953 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
954 &scsi_start_stop_unit, sizeof(scsi_start_stop_unit),
957 case MSC_EJECT_REZERO:
958 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
959 &scsi_rezero_init, sizeof(scsi_rezero_init),
962 case MSC_EJECT_ZTESTOR:
963 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
964 &scsi_ztestor_eject, sizeof(scsi_ztestor_eject),
967 case MSC_EJECT_CMOTECH:
968 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
969 &scsi_cmotech_eject, sizeof(scsi_cmotech_eject),
972 case MSC_EJECT_HUAWEI:
973 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
974 &scsi_huawei_eject, sizeof(scsi_huawei_eject),
977 case MSC_EJECT_HUAWEI2:
978 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
979 &scsi_huawei_eject2, sizeof(scsi_huawei_eject2),
984 * TCTMobile needs DIR_IN flag. To get it, we
985 * supply a dummy data with the command.
987 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
988 sc->buffer_size, &scsi_tct_eject,
989 sizeof(scsi_tct_eject), USB_MS_HZ);
992 DPRINTF("Unknown eject method (%d)\n", method);
994 return (USB_ERR_INVAL);
997 DPRINTF("Eject CD command status: %s\n", usbd_errstr(err));
1004 usb_dymo_eject(struct usb_device *udev, uint8_t iface_index)
1006 static const uint8_t data[3] = { 0x1b, 0x5a, 0x01 };
1007 struct bbb_transfer *sc;
1010 sc = bbb_attach(udev, iface_index, UICLASS_HID);
1012 return (USB_ERR_INVAL);
1013 err = bbb_raw_write(sc, data, sizeof(data), USB_MS_HZ);
1019 usb_msc_read_10(struct usb_device *udev, uint8_t iface_index,
1020 uint32_t lba, uint32_t blocks, void *buffer)
1022 struct bbb_transfer *sc;
1026 cmd[0] = 0x28; /* READ_10 */
1033 cmd[7] = blocks >> 8;
1037 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1039 return (USB_ERR_INVAL);
1041 err = bbb_command_start(sc, DIR_IN, 0, buffer,
1042 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
1050 usb_msc_write_10(struct usb_device *udev, uint8_t iface_index,
1051 uint32_t lba, uint32_t blocks, void *buffer)
1053 struct bbb_transfer *sc;
1057 cmd[0] = 0x2a; /* WRITE_10 */
1064 cmd[7] = blocks >> 8;
1068 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1070 return (USB_ERR_INVAL);
1072 err = bbb_command_start(sc, DIR_OUT, 0, buffer,
1073 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
1081 usb_msc_read_capacity(struct usb_device *udev, uint8_t iface_index,
1082 uint32_t *lba_last, uint32_t *block_size)
1084 struct bbb_transfer *sc;
1087 sc = bbb_attach(udev, iface_index, UICLASS_MASS);
1089 return (USB_ERR_INVAL);
1091 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
1092 &scsi_read_capacity, sizeof(scsi_read_capacity),
1096 (sc->buffer[0] << 24) |
1097 (sc->buffer[1] << 16) |
1098 (sc->buffer[2] << 8) |
1102 (sc->buffer[4] << 24) |
1103 (sc->buffer[5] << 16) |
1104 (sc->buffer[6] << 8) |
1107 /* we currently only support one block size */
1108 if (*block_size != SCSI_FIXED_BLOCK_SIZE)
1109 err = USB_ERR_INVAL;