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;
185 static void bbb_done(struct bbb_transfer *, int);
186 static void bbb_transfer_start(struct bbb_transfer *, uint8_t);
187 static void bbb_data_clear_stall_callback(struct usb_xfer *, uint8_t,
189 static int bbb_command_start(struct bbb_transfer *, uint8_t, uint8_t,
190 void *, size_t, void *, size_t, usb_timeout_t);
191 static struct bbb_transfer *bbb_attach(struct usb_device *, uint8_t);
192 static void bbb_detach(struct bbb_transfer *);
194 static const struct usb_config bbb_config[ST_MAX] = {
198 .endpoint = UE_ADDR_ANY,
199 .direction = UE_DIR_OUT,
200 .bufsize = sizeof(struct bbb_cbw),
201 .callback = &bbb_command_callback,
202 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
207 .endpoint = UE_ADDR_ANY,
208 .direction = UE_DIR_IN,
209 .bufsize = SCSI_MAX_LEN,
210 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
211 .callback = &bbb_data_read_callback,
212 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
217 .endpoint = 0x00, /* Control pipe */
218 .direction = UE_DIR_ANY,
219 .bufsize = sizeof(struct usb_device_request),
220 .callback = &bbb_data_rd_cs_callback,
221 .timeout = 1 * USB_MS_HZ, /* 1 second */
226 .endpoint = UE_ADDR_ANY,
227 .direction = UE_DIR_OUT,
228 .bufsize = SCSI_MAX_LEN,
229 .flags = {.ext_buffer = 1,.proxy_buffer = 1,},
230 .callback = &bbb_data_write_callback,
231 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
236 .endpoint = 0x00, /* Control pipe */
237 .direction = UE_DIR_ANY,
238 .bufsize = sizeof(struct usb_device_request),
239 .callback = &bbb_data_wr_cs_callback,
240 .timeout = 1 * USB_MS_HZ, /* 1 second */
245 .endpoint = UE_ADDR_ANY,
246 .direction = UE_DIR_IN,
247 .bufsize = sizeof(struct bbb_csw),
248 .flags = {.short_xfer_ok = 1,},
249 .callback = &bbb_status_callback,
250 .timeout = 1 * USB_MS_HZ, /* 1 second */
255 bbb_done(struct bbb_transfer *sc, int error)
258 sc->state = ST_COMMAND;
264 bbb_transfer_start(struct bbb_transfer *sc, uint8_t xfer_index)
266 sc->state = xfer_index;
267 usbd_transfer_start(sc->xfer[xfer_index]);
271 bbb_data_clear_stall_callback(struct usb_xfer *xfer,
272 uint8_t next_xfer, uint8_t stall_xfer)
274 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
276 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) {
277 switch (USB_GET_STATE(xfer)) {
279 case USB_ST_TRANSFERRED:
280 bbb_transfer_start(sc, next_xfer);
283 bbb_done(sc, USB_ERR_STALLED);
290 bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
292 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
295 switch (USB_GET_STATE(xfer)) {
296 case USB_ST_TRANSFERRED:
298 (sc, ((sc->dir == DIR_IN) ? ST_DATA_RD :
299 (sc->dir == DIR_OUT) ? ST_DATA_WR :
305 tag = UGETDW(sc->cbw->dCBWTag) + 1;
306 USETDW(sc->cbw->dCBWSignature, CBWSIGNATURE);
307 USETDW(sc->cbw->dCBWTag, tag);
308 USETDW(sc->cbw->dCBWDataTransferLength, (uint32_t)sc->data_len);
309 sc->cbw->bCBWFlags = ((sc->dir == DIR_IN) ? CBWFLAGS_IN : CBWFLAGS_OUT);
310 sc->cbw->bCBWLUN = sc->lun;
311 sc->cbw->bCDBLength = sc->cmd_len;
312 if (sc->cbw->bCDBLength > sizeof(sc->cbw->CBWCDB)) {
313 sc->cbw->bCDBLength = sizeof(sc->cbw->CBWCDB);
314 DPRINTFN(0, "Truncating long command\n");
316 usbd_xfer_set_frame_len(xfer, 0,
317 sizeof(struct bbb_cbw));
318 usbd_transfer_submit(xfer);
328 bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
330 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
331 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
334 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
336 switch (USB_GET_STATE(xfer)) {
337 case USB_ST_TRANSFERRED:
338 sc->data_rem -= actlen;
339 sc->data_ptr += actlen;
340 sc->actlen += actlen;
342 if (actlen < sumlen) {
347 DPRINTF("max_bulk=%d, data_rem=%d\n",
348 max_bulk, sc->data_rem);
350 if (sc->data_rem == 0) {
351 bbb_transfer_start(sc, ST_STATUS);
354 if (max_bulk > sc->data_rem) {
355 max_bulk = sc->data_rem;
357 usbd_xfer_set_timeout(xfer, sc->data_timeout);
358 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
359 usbd_transfer_submit(xfer);
363 if (error == USB_ERR_CANCELLED) {
366 bbb_transfer_start(sc, ST_DATA_RD_CS);
373 bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
375 bbb_data_clear_stall_callback(xfer, ST_STATUS,
380 bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
382 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
383 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
386 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
388 switch (USB_GET_STATE(xfer)) {
389 case USB_ST_TRANSFERRED:
390 sc->data_rem -= actlen;
391 sc->data_ptr += actlen;
392 sc->actlen += actlen;
394 if (actlen < sumlen) {
399 DPRINTF("max_bulk=%d, data_rem=%d\n",
400 max_bulk, sc->data_rem);
402 if (sc->data_rem == 0) {
403 bbb_transfer_start(sc, ST_STATUS);
406 if (max_bulk > sc->data_rem) {
407 max_bulk = sc->data_rem;
409 usbd_xfer_set_timeout(xfer, sc->data_timeout);
410 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
411 usbd_transfer_submit(xfer);
415 if (error == USB_ERR_CANCELLED) {
418 bbb_transfer_start(sc, ST_DATA_WR_CS);
425 bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
427 bbb_data_clear_stall_callback(xfer, ST_STATUS,
432 bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
434 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
438 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
440 switch (USB_GET_STATE(xfer)) {
441 case USB_ST_TRANSFERRED:
443 /* very simple status check */
445 if (actlen < (int)sizeof(struct bbb_csw)) {
446 bbb_done(sc, USB_ERR_SHORT_XFER);
447 } else if (sc->csw->bCSWStatus == CSWSTATUS_GOOD) {
448 bbb_done(sc, 0); /* success */
450 bbb_done(sc, ERR_CSW_FAILED); /* error */
455 usbd_xfer_set_frame_len(xfer, 0,
456 sizeof(struct bbb_csw));
457 usbd_transfer_submit(xfer);
461 DPRINTF("Failed to read CSW: %s, try %d\n",
462 usbd_errstr(error), sc->status_try);
464 if (error == USB_ERR_CANCELLED || sc->status_try) {
468 bbb_transfer_start(sc, ST_DATA_RD_CS);
474 /*------------------------------------------------------------------------*
475 * bbb_command_start - execute a SCSI command synchronously
480 *------------------------------------------------------------------------*/
482 bbb_command_start(struct bbb_transfer *sc, uint8_t dir, uint8_t lun,
483 void *data_ptr, size_t data_len, void *cmd_ptr, size_t cmd_len,
484 usb_timeout_t data_timeout)
487 sc->dir = data_len ? dir : DIR_NONE;
488 sc->data_ptr = data_ptr;
489 sc->data_len = data_len;
490 sc->data_rem = data_len;
491 sc->data_timeout = (data_timeout + USB_MS_HZ);
494 sc->cmd_len = cmd_len;
495 memset(&sc->cbw->CBWCDB, 0, sizeof(sc->cbw->CBWCDB));
496 memcpy(&sc->cbw->CBWCDB, cmd_ptr, cmd_len);
497 DPRINTFN(1, "SCSI cmd = %*D\n", (int)cmd_len, (char *)sc->cbw->CBWCDB, ":");
500 usbd_transfer_start(sc->xfer[sc->state]);
502 while (usbd_transfer_pending(sc->xfer[sc->state])) {
503 cv_wait(&sc->cv, &sc->mtx);
505 mtx_unlock(&sc->mtx);
509 static struct bbb_transfer *
510 bbb_attach(struct usb_device *udev, uint8_t iface_index)
512 struct usb_interface *iface;
513 struct usb_interface_descriptor *id;
514 struct bbb_transfer *sc;
517 #if USB_HAVE_MSCTEST_DETACH
520 /* Prevent re-enumeration */
521 do_unlock = usbd_enum_lock(udev);
524 * Make sure any driver which is hooked up to this interface,
525 * like umass is gone:
527 usb_detach_device(udev, iface_index, 0);
530 usbd_enum_unlock(udev);
533 iface = usbd_get_iface(udev, iface_index);
538 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
541 switch (id->bInterfaceSubClass) {
542 case UISUBCLASS_SCSI:
544 case UISUBCLASS_SFF8020I:
545 case UISUBCLASS_SFF8070I:
551 switch (id->bInterfaceProtocol) {
552 case UIPROTO_MASS_BBB_OLD:
553 case UIPROTO_MASS_BBB:
559 sc = malloc(sizeof(*sc), M_USB, M_WAITOK | M_ZERO);
560 mtx_init(&sc->mtx, "USB autoinstall", NULL, MTX_DEF);
561 cv_init(&sc->cv, "WBBB");
563 err = usbd_transfer_setup(udev, &iface_index, sc->xfer, bbb_config,
564 ST_MAX, sc, &sc->mtx);
569 /* store pointer to DMA buffers */
570 sc->buffer = usbd_xfer_get_frame_buffer(
571 sc->xfer[ST_DATA_RD], 0);
573 usbd_xfer_max_len(sc->xfer[ST_DATA_RD]);
574 sc->cbw = usbd_xfer_get_frame_buffer(
575 sc->xfer[ST_COMMAND], 0);
576 sc->csw = usbd_xfer_get_frame_buffer(
577 sc->xfer[ST_STATUS], 0);
583 bbb_detach(struct bbb_transfer *sc)
585 usbd_transfer_unsetup(sc->xfer, ST_MAX);
586 mtx_destroy(&sc->mtx);
591 /*------------------------------------------------------------------------*
595 * 1: This interface is an auto install disk (CD-ROM)
596 * 0: Not an auto install disk.
597 *------------------------------------------------------------------------*/
599 usb_iface_is_cdrom(struct usb_device *udev, uint8_t iface_index)
601 struct bbb_transfer *sc;
607 sc = bbb_attach(udev, iface_index);
612 timeout = 4; /* tries */
614 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
615 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
618 if (err == 0 && sc->actlen > 0) {
619 sid_type = sc->buffer[0] & 0x1F;
620 if (sid_type == 0x05)
623 } else if (err != ERR_CSW_FAILED)
624 break; /* non retryable error */
625 usb_pause_mtx(NULL, hz);
632 usb_msc_get_max_lun(struct usb_device *udev, uint8_t iface_index)
634 struct usb_device_request req;
639 /* The Get Max Lun command is a class-specific request. */
640 req.bmRequestType = UT_READ_CLASS_INTERFACE;
641 req.bRequest = 0xFE; /* GET_MAX_LUN */
642 USETW(req.wValue, 0);
643 req.wIndex[0] = iface_index;
645 USETW(req.wLength, 1);
647 err = usbd_do_request(udev, NULL, &req, &buf);
655 usb_msc_auto_quirk(struct usb_device *udev, uint8_t iface_index)
657 struct bbb_transfer *sc;
659 uint8_t is_no_direct;
663 sc = bbb_attach(udev, iface_index);
668 * Some devices need a delay after that the configuration
669 * value is set to function properly:
671 usb_pause_mtx(NULL, hz);
673 if (usb_msc_get_max_lun(udev, iface_index) == 0) {
674 DPRINTF("Device has only got one LUN.\n");
675 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_GETMAXLUN);
679 for (timeout = 4; timeout != 0; timeout--) {
680 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
681 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
684 if (err == 0 && sc->actlen > 0) {
685 sid_type = sc->buffer[0] & 0x1F;
686 if (sid_type == 0x00)
689 } else if (err != ERR_CSW_FAILED) {
690 DPRINTF("Device is not responding "
691 "properly to SCSI INQUIRY command.\n");
692 goto error; /* non retryable error */
694 usb_pause_mtx(NULL, hz);
698 DPRINTF("Device is not direct access.\n");
702 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
703 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
707 if (err != ERR_CSW_FAILED)
709 DPRINTF("Test unit ready failed\n");
712 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
713 &scsi_prevent_removal, sizeof(scsi_prevent_removal),
717 err = bbb_command_start(sc, DIR_OUT, 0, NULL, 0,
718 &scsi_allow_removal, sizeof(scsi_allow_removal),
723 if (err != ERR_CSW_FAILED)
725 DPRINTF("Device doesn't handle prevent and allow removal\n");
726 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
732 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
733 &scsi_sync_cache, sizeof(scsi_sync_cache),
738 if (err != ERR_CSW_FAILED)
741 DPRINTF("Device doesn't handle synchronize cache\n");
743 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
747 * Certain Kingston memory sticks fail the first
748 * read capacity after a synchronize cache command
749 * has been issued. Disable the synchronize cache
750 * command for such devices.
753 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
754 &scsi_read_capacity, sizeof(scsi_read_capacity),
758 if (err != ERR_CSW_FAILED)
761 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
762 &scsi_read_capacity, sizeof(scsi_read_capacity),
767 goto retry_sync_cache;
769 DPRINTF("Device most likely doesn't "
770 "handle synchronize cache\n");
772 usbd_add_dynamic_quirk(udev,
773 UQ_MSC_NO_SYNC_CACHE);
775 if (err != ERR_CSW_FAILED)
781 /* clear sense status of any failed commands on the device */
783 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
784 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
787 DPRINTF("Inquiry = %d\n", err);
791 if (err != ERR_CSW_FAILED)
795 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
796 SCSI_SENSE_LEN, &scsi_request_sense,
797 sizeof(scsi_request_sense), USB_MS_HZ);
799 DPRINTF("Request sense = %d\n", err);
803 if (err != ERR_CSW_FAILED)
814 DPRINTF("Device did not respond, enabling all quirks\n");
816 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
817 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_PREVENT_ALLOW);
818 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_TEST_UNIT_READY);
820 /* Need to re-enumerate the device */
821 usbd_req_re_enumerate(udev, NULL);
823 return (USB_ERR_STALLED);
827 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method)
829 struct bbb_transfer *sc;
832 sc = bbb_attach(udev, iface_index);
834 return (USB_ERR_INVAL);
837 case MSC_EJECT_STOPUNIT:
838 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
839 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
841 DPRINTF("Test unit ready status: %s\n", usbd_errstr(err));
842 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
843 &scsi_start_stop_unit, sizeof(scsi_start_stop_unit),
846 case MSC_EJECT_REZERO:
847 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
848 &scsi_rezero_init, sizeof(scsi_rezero_init),
851 case MSC_EJECT_ZTESTOR:
852 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
853 &scsi_ztestor_eject, sizeof(scsi_ztestor_eject),
856 case MSC_EJECT_CMOTECH:
857 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
858 &scsi_cmotech_eject, sizeof(scsi_cmotech_eject),
861 case MSC_EJECT_HUAWEI:
862 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
863 &scsi_huawei_eject, sizeof(scsi_huawei_eject),
866 case MSC_EJECT_HUAWEI2:
867 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
868 &scsi_huawei_eject2, sizeof(scsi_huawei_eject2),
873 * TCTMobile needs DIR_IN flag. To get it, we
874 * supply a dummy data with the command.
876 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
877 sc->buffer_size, &scsi_tct_eject,
878 sizeof(scsi_tct_eject), USB_MS_HZ);
881 DPRINTF("Unknown eject method (%d)\n", method);
883 return (USB_ERR_INVAL);
886 DPRINTF("Eject CD command status: %s\n", usbd_errstr(err));
893 usb_msc_read_10(struct usb_device *udev, uint8_t iface_index,
894 uint32_t lba, uint32_t blocks, void *buffer)
896 struct bbb_transfer *sc;
900 cmd[0] = 0x28; /* READ_10 */
907 cmd[7] = blocks >> 8;
911 sc = bbb_attach(udev, iface_index);
913 return (USB_ERR_INVAL);
915 err = bbb_command_start(sc, DIR_IN, 0, buffer,
916 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
924 usb_msc_write_10(struct usb_device *udev, uint8_t iface_index,
925 uint32_t lba, uint32_t blocks, void *buffer)
927 struct bbb_transfer *sc;
931 cmd[0] = 0x2a; /* WRITE_10 */
938 cmd[7] = blocks >> 8;
942 sc = bbb_attach(udev, iface_index);
944 return (USB_ERR_INVAL);
946 err = bbb_command_start(sc, DIR_OUT, 0, buffer,
947 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ);
955 usb_msc_read_capacity(struct usb_device *udev, uint8_t iface_index,
956 uint32_t *lba_last, uint32_t *block_size)
958 struct bbb_transfer *sc;
961 sc = bbb_attach(udev, iface_index);
963 return (USB_ERR_INVAL);
965 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
966 &scsi_read_capacity, sizeof(scsi_read_capacity),
970 (sc->buffer[0] << 24) |
971 (sc->buffer[1] << 16) |
972 (sc->buffer[2] << 8) |
976 (sc->buffer[4] << 24) |
977 (sc->buffer[5] << 16) |
978 (sc->buffer[6] << 8) |
981 /* we currently only support one block size */
982 if (*block_size != SCSI_FIXED_BLOCK_SIZE)