3 * SPDX-License-Identifier: BSD-3-Clause
5 * Copyright (C) 2003-2005 Alan Stern
6 * Copyright (C) 2008 Hans Petter Selasky
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * NOTE: Much of the SCSI statemachine handling code derives from the
38 * Linux USB gadget stack.
41 #ifdef USB_GLOBAL_INCLUDE_FILE
42 #include USB_GLOBAL_INCLUDE_FILE
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
68 #define USB_DEBUG_VAR ustorage_fs_debug
69 #include <dev/usb/usb_debug.h>
70 #endif /* USB_GLOBAL_INCLUDE_FILE */
73 static int ustorage_fs_debug = 0;
75 SYSCTL_NODE(_hw_usb, OID_AUTO, ustorage_fs, CTLFLAG_RW, 0, "USB ustorage_fs");
76 SYSCTL_INT(_hw_usb_ustorage_fs, OID_AUTO, debug, CTLFLAG_RWTUN,
77 &ustorage_fs_debug, 0, "ustorage_fs debug level");
80 /* Define some limits */
82 #ifndef USTORAGE_FS_BULK_SIZE
83 #define USTORAGE_FS_BULK_SIZE (1U << 17) /* bytes */
86 #ifndef USTORAGE_FS_MAX_LUN
87 #define USTORAGE_FS_MAX_LUN 8 /* units */
90 #ifndef USTORAGE_QDATA_MAX
91 #define USTORAGE_QDATA_MAX 40 /* bytes */
95 * The SCSI ID string must be exactly 28 characters long
96 * exluding the terminating zero.
98 #ifndef USTORAGE_FS_ID_STRING
99 #define USTORAGE_FS_ID_STRING \
101 "File-Stor Gadget" /* 16 */ \
106 * The following macro defines the number of
107 * sectors to be allocated for the RAM disk:
109 #ifndef USTORAGE_FS_RAM_SECT
110 #define USTORAGE_FS_RAM_SECT (1UL << 13)
113 static uint8_t *ustorage_fs_ramdisk;
115 /* USB transfer definitions */
117 #define USTORAGE_FS_T_BBB_COMMAND 0
118 #define USTORAGE_FS_T_BBB_DATA_DUMP 1
119 #define USTORAGE_FS_T_BBB_DATA_READ 2
120 #define USTORAGE_FS_T_BBB_DATA_WRITE 3
121 #define USTORAGE_FS_T_BBB_STATUS 4
122 #define USTORAGE_FS_T_BBB_MAX 5
124 /* USB data stage direction */
130 /* USB interface specific control request */
132 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
133 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
135 /* Command Block Wrapper */
137 uDWord dCBWSignature;
138 #define CBWSIGNATURE 0x43425355
140 uDWord dCBWDataTransferLength;
142 #define CBWFLAGS_OUT 0x00
143 #define CBWFLAGS_IN 0x80
146 #define CBWCDBLENGTH 16
147 uByte CBWCDB[CBWCDBLENGTH];
148 } __packed ustorage_fs_bbb_cbw_t;
150 #define USTORAGE_FS_BBB_CBW_SIZE 31
152 /* Command Status Wrapper */
154 uDWord dCSWSignature;
155 #define CSWSIGNATURE 0x53425355
157 uDWord dCSWDataResidue;
159 #define CSWSTATUS_GOOD 0x0
160 #define CSWSTATUS_FAILED 0x1
161 #define CSWSTATUS_PHASE 0x2
162 } __packed ustorage_fs_bbb_csw_t;
164 #define USTORAGE_FS_BBB_CSW_SIZE 13
166 struct ustorage_fs_lun {
168 uint8_t *memory_image;
170 uint32_t num_sectors;
172 uint32_t sense_data_info;
173 uint32_t unit_attention_data;
176 uint8_t prevent_medium_removal:1;
177 uint8_t info_valid:1;
181 struct ustorage_fs_softc {
183 ustorage_fs_bbb_cbw_t *sc_cbw; /* Command Wrapper Block */
184 ustorage_fs_bbb_csw_t *sc_csw; /* Command Status Block */
185 void *sc_dma_ptr; /* Main data buffer */
189 struct ustorage_fs_lun sc_lun[USTORAGE_FS_MAX_LUN];
193 struct ustorage_fs_lun *currlun;
195 uint32_t data_rem; /* bytes, as reported by the command
197 uint32_t offset; /* bytes */
203 uint8_t data_short:1;
204 uint8_t data_error:1;
208 struct usb_device *sc_udev;
209 struct usb_xfer *sc_xfer[USTORAGE_FS_T_BBB_MAX];
211 uint8_t sc_iface_no; /* interface number */
213 uint8_t sc_last_xfer_index;
214 uint8_t sc_qdata[USTORAGE_QDATA_MAX];
219 static device_probe_t ustorage_fs_probe;
220 static device_attach_t ustorage_fs_attach;
221 static device_detach_t ustorage_fs_detach;
222 static device_suspend_t ustorage_fs_suspend;
223 static device_resume_t ustorage_fs_resume;
224 static usb_handle_request_t ustorage_fs_handle_request;
226 static usb_callback_t ustorage_fs_t_bbb_command_callback;
227 static usb_callback_t ustorage_fs_t_bbb_data_dump_callback;
228 static usb_callback_t ustorage_fs_t_bbb_data_read_callback;
229 static usb_callback_t ustorage_fs_t_bbb_data_write_callback;
230 static usb_callback_t ustorage_fs_t_bbb_status_callback;
232 static void ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index);
233 static void ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc);
235 static uint8_t ustorage_fs_verify(struct ustorage_fs_softc *sc);
236 static uint8_t ustorage_fs_inquiry(struct ustorage_fs_softc *sc);
237 static uint8_t ustorage_fs_request_sense(struct ustorage_fs_softc *sc);
238 static uint8_t ustorage_fs_read_capacity(struct ustorage_fs_softc *sc);
239 static uint8_t ustorage_fs_mode_sense(struct ustorage_fs_softc *sc);
240 static uint8_t ustorage_fs_start_stop(struct ustorage_fs_softc *sc);
241 static uint8_t ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc);
242 static uint8_t ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc);
243 static uint8_t ustorage_fs_mode_select(struct ustorage_fs_softc *sc);
244 static uint8_t ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask);
245 static uint8_t ustorage_fs_read(struct ustorage_fs_softc *sc);
246 static uint8_t ustorage_fs_write(struct ustorage_fs_softc *sc);
247 static uint8_t ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t cmd_size, uint16_t mask, uint8_t needs_medium);
248 static uint8_t ustorage_fs_do_cmd(struct ustorage_fs_softc *sc);
250 static device_method_t ustorage_fs_methods[] = {
252 DEVMETHOD(usb_handle_request, ustorage_fs_handle_request),
254 /* Device interface */
255 DEVMETHOD(device_probe, ustorage_fs_probe),
256 DEVMETHOD(device_attach, ustorage_fs_attach),
257 DEVMETHOD(device_detach, ustorage_fs_detach),
258 DEVMETHOD(device_suspend, ustorage_fs_suspend),
259 DEVMETHOD(device_resume, ustorage_fs_resume),
264 static driver_t ustorage_fs_driver = {
265 .name = "ustorage_fs",
266 .methods = ustorage_fs_methods,
267 .size = sizeof(struct ustorage_fs_softc),
270 static devclass_t ustorage_fs_devclass;
272 DRIVER_MODULE(ustorage_fs, uhub, ustorage_fs_driver, ustorage_fs_devclass, NULL, 0);
273 MODULE_VERSION(ustorage_fs, 0);
274 MODULE_DEPEND(ustorage_fs, usb, 1, 1, 1);
276 static struct usb_config ustorage_fs_bbb_config[USTORAGE_FS_T_BBB_MAX] = {
278 [USTORAGE_FS_T_BBB_COMMAND] = {
280 .endpoint = UE_ADDR_ANY,
281 .direction = UE_DIR_OUT,
282 .bufsize = sizeof(ustorage_fs_bbb_cbw_t),
283 .callback = &ustorage_fs_t_bbb_command_callback,
284 .usb_mode = USB_MODE_DEVICE,
287 [USTORAGE_FS_T_BBB_DATA_DUMP] = {
289 .endpoint = UE_ADDR_ANY,
290 .direction = UE_DIR_OUT,
291 .bufsize = 0, /* use wMaxPacketSize */
292 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
293 .callback = &ustorage_fs_t_bbb_data_dump_callback,
294 .usb_mode = USB_MODE_DEVICE,
297 [USTORAGE_FS_T_BBB_DATA_READ] = {
299 .endpoint = UE_ADDR_ANY,
300 .direction = UE_DIR_OUT,
301 .bufsize = USTORAGE_FS_BULK_SIZE,
302 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1},
303 .callback = &ustorage_fs_t_bbb_data_read_callback,
304 .usb_mode = USB_MODE_DEVICE,
307 [USTORAGE_FS_T_BBB_DATA_WRITE] = {
309 .endpoint = UE_ADDR_ANY,
310 .direction = UE_DIR_IN,
311 .bufsize = USTORAGE_FS_BULK_SIZE,
312 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
313 .callback = &ustorage_fs_t_bbb_data_write_callback,
314 .usb_mode = USB_MODE_DEVICE,
317 [USTORAGE_FS_T_BBB_STATUS] = {
319 .endpoint = UE_ADDR_ANY,
320 .direction = UE_DIR_IN,
321 .bufsize = sizeof(ustorage_fs_bbb_csw_t),
322 .flags = {.short_xfer_ok = 1},
323 .callback = &ustorage_fs_t_bbb_status_callback,
324 .usb_mode = USB_MODE_DEVICE,
329 * USB device probe/attach/detach
333 ustorage_fs_probe(device_t dev)
335 struct usb_attach_arg *uaa = device_get_ivars(dev);
336 struct usb_interface_descriptor *id;
338 if (uaa->usb_mode != USB_MODE_DEVICE) {
341 /* Check for a standards compliant device */
342 id = usbd_get_interface_descriptor(uaa->iface);
344 (id->bInterfaceClass != UICLASS_MASS) ||
345 (id->bInterfaceSubClass != UISUBCLASS_SCSI) ||
346 (id->bInterfaceProtocol != UIPROTO_MASS_BBB)) {
349 return (BUS_PROBE_GENERIC);
353 ustorage_fs_attach(device_t dev)
355 struct ustorage_fs_softc *sc = device_get_softc(dev);
356 struct usb_attach_arg *uaa = device_get_ivars(dev);
357 struct usb_interface_descriptor *id;
362 * NOTE: the softc struct is cleared in device_set_driver.
363 * We can safely call ustorage_fs_detach without specifically
364 * initializing the struct.
368 sc->sc_udev = uaa->device;
369 unit = device_get_unit(dev);
371 /* enable power saving mode */
372 usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE);
375 if (ustorage_fs_ramdisk == NULL) {
377 * allocate a memory image for our ramdisk until
380 ustorage_fs_ramdisk =
381 malloc(USTORAGE_FS_RAM_SECT << 9, M_USB,
384 if (ustorage_fs_ramdisk == NULL) {
388 sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
389 sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
390 sc->sc_lun[0].removable = 1;
393 device_set_usb_desc(dev);
395 mtx_init(&sc->sc_mtx, "USTORAGE_FS lock",
396 NULL, (MTX_DEF | MTX_RECURSE));
398 /* get interface index */
400 id = usbd_get_interface_descriptor(uaa->iface);
402 device_printf(dev, "failed to get "
403 "interface number\n");
406 sc->sc_iface_no = id->bInterfaceNumber;
408 err = usbd_transfer_setup(uaa->device,
409 &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
410 USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_mtx);
412 device_printf(dev, "could not setup required "
413 "transfers, %s\n", usbd_errstr(err));
417 sc->sc_cbw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
418 USTORAGE_FS_T_BBB_COMMAND], 0);
419 sc->sc_csw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
420 USTORAGE_FS_T_BBB_STATUS], 0);
421 sc->sc_dma_ptr = usbd_xfer_get_frame_buffer(sc->sc_xfer[
422 USTORAGE_FS_T_BBB_DATA_READ], 0);
424 /* start Mass Storage State Machine */
426 mtx_lock(&sc->sc_mtx);
427 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
428 mtx_unlock(&sc->sc_mtx);
430 return (0); /* success */
433 ustorage_fs_detach(dev);
434 return (ENXIO); /* failure */
438 ustorage_fs_detach(device_t dev)
440 struct ustorage_fs_softc *sc = device_get_softc(dev);
442 /* teardown our statemachine */
444 usbd_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
446 mtx_destroy(&sc->sc_mtx);
448 return (0); /* success */
452 ustorage_fs_suspend(device_t dev)
454 device_printf(dev, "suspending\n");
455 return (0); /* success */
459 ustorage_fs_resume(device_t dev)
461 device_printf(dev, "resuming\n");
462 return (0); /* success */
466 * Generic functions to handle transfers
470 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
472 if (sc->sc_xfer[xfer_index]) {
473 sc->sc_last_xfer_index = xfer_index;
474 usbd_transfer_start(sc->sc_xfer[xfer_index]);
479 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
481 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
482 mtx_unlock(&sc->sc_mtx);
483 usbd_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
484 mtx_lock(&sc->sc_mtx);
488 ustorage_fs_handle_request(device_t dev,
489 const void *preq, void **pptr, uint16_t *plen,
490 uint16_t offset, uint8_t *pstate)
492 struct ustorage_fs_softc *sc = device_get_softc(dev);
493 const struct usb_device_request *req = preq;
494 uint8_t is_complete = *pstate;
497 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
498 (req->bRequest == UR_BBB_RESET)) {
500 mtx_lock(&sc->sc_mtx);
501 ustorage_fs_transfer_stop(sc);
502 sc->sc_transfer.data_error = 1;
503 ustorage_fs_transfer_start(sc,
504 USTORAGE_FS_T_BBB_COMMAND);
505 mtx_unlock(&sc->sc_mtx);
507 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
508 (req->bRequest == UR_BBB_GET_MAX_LUN)) {
511 *pptr = &sc->sc_last_lun;
518 return (ENXIO); /* use builtin handler */
522 ustorage_fs_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
524 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
530 switch (USB_GET_STATE(xfer)) {
531 case USB_ST_TRANSFERRED:
533 tag = UGETDW(sc->sc_cbw->dCBWSignature);
535 if (tag != CBWSIGNATURE) {
537 DPRINTF("invalid signature 0x%08x\n", tag);
540 tag = UGETDW(sc->sc_cbw->dCBWTag);
543 USETDW(sc->sc_csw->dCSWTag, tag);
546 sc->sc_csw->bCSWStatus = 0;
548 /* reset data offset, data length and data remainder */
549 sc->sc_transfer.offset = 0;
550 sc->sc_transfer.data_rem =
551 UGETDW(sc->sc_cbw->dCBWDataTransferLength);
553 /* reset data flags */
554 sc->sc_transfer.data_short = 0;
557 sc->sc_transfer.lun = sc->sc_cbw->bCBWLUN;
559 if (sc->sc_transfer.data_rem == 0) {
560 sc->sc_transfer.cbw_dir = DIR_NONE;
562 if (sc->sc_cbw->bCBWFlags & CBWFLAGS_IN) {
563 sc->sc_transfer.cbw_dir = DIR_WRITE;
565 sc->sc_transfer.cbw_dir = DIR_READ;
569 sc->sc_transfer.cmd_len = sc->sc_cbw->bCDBLength;
570 if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw->CBWCDB)) ||
571 (sc->sc_transfer.cmd_len == 0)) {
572 /* just halt - this is invalid */
573 DPRINTF("invalid command length %d bytes\n",
574 sc->sc_transfer.cmd_len);
578 err = ustorage_fs_do_cmd(sc);
581 DPRINTF("command failed\n");
584 if ((sc->sc_transfer.data_rem > 0) &&
585 (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
586 /* contradicting data transfer direction */
588 DPRINTF("data direction mismatch\n");
591 switch (sc->sc_transfer.cbw_dir) {
593 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
596 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
599 ustorage_fs_transfer_start(sc,
600 USTORAGE_FS_T_BBB_STATUS);
607 if (sc->sc_transfer.data_error) {
608 sc->sc_transfer.data_error = 0;
609 usbd_xfer_set_stall(xfer);
610 DPRINTF("stall pipe\n");
612 usbd_xfer_set_frame_len(xfer, 0,
613 sizeof(ustorage_fs_bbb_cbw_t));
614 usbd_transfer_submit(xfer);
619 if (error == USB_ERR_CANCELLED) {
622 /* If the pipe is already stalled, don't do another stall */
623 if (!usbd_xfer_is_stalled(xfer))
624 sc->sc_transfer.data_error = 1;
630 if (sc->sc_csw->bCSWStatus == 0) {
631 /* set some default error code */
632 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
634 if (sc->sc_transfer.cbw_dir == DIR_READ) {
636 ustorage_fs_transfer_start(sc,
637 USTORAGE_FS_T_BBB_DATA_DUMP);
640 if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
641 /* need to stall before status */
642 sc->sc_transfer.data_error = 1;
644 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
649 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer *xfer, usb_error_t error)
651 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
652 uint32_t max_bulk = usbd_xfer_max_len(xfer);
655 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
659 switch (USB_GET_STATE(xfer)) {
660 case USB_ST_TRANSFERRED:
661 sc->sc_transfer.data_rem -= actlen;
662 sc->sc_transfer.offset += actlen;
664 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
665 /* short transfer or end of data */
666 ustorage_fs_transfer_start(sc,
667 USTORAGE_FS_T_BBB_STATUS);
674 if (max_bulk > sc->sc_transfer.data_rem) {
675 max_bulk = sc->sc_transfer.data_rem;
677 if (sc->sc_transfer.data_error) {
678 sc->sc_transfer.data_error = 0;
679 usbd_xfer_set_stall(xfer);
681 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
682 usbd_transfer_submit(xfer);
686 if (error == USB_ERR_CANCELLED) {
690 * If the pipe is already stalled, don't do another stall:
692 if (!usbd_xfer_is_stalled(xfer))
693 sc->sc_transfer.data_error = 1;
701 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
703 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
704 uint32_t max_bulk = usbd_xfer_max_len(xfer);
707 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
711 switch (USB_GET_STATE(xfer)) {
712 case USB_ST_TRANSFERRED:
713 /* XXX copy data from DMA buffer */
714 memcpy(sc->sc_transfer.data_ptr, sc->sc_dma_ptr, actlen);
716 sc->sc_transfer.data_rem -= actlen;
717 sc->sc_transfer.data_ptr += actlen;
718 sc->sc_transfer.offset += actlen;
720 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
721 /* short transfer or end of data */
722 ustorage_fs_transfer_start(sc,
723 USTORAGE_FS_T_BBB_STATUS);
730 if (max_bulk > sc->sc_transfer.data_rem) {
731 max_bulk = sc->sc_transfer.data_rem;
733 if (sc->sc_transfer.data_error) {
734 sc->sc_transfer.data_error = 0;
735 usbd_xfer_set_stall(xfer);
738 usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
739 usbd_transfer_submit(xfer);
743 if (error == USB_ERR_CANCELLED) {
746 /* If the pipe is already stalled, don't do another stall */
747 if (!usbd_xfer_is_stalled(xfer))
748 sc->sc_transfer.data_error = 1;
756 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
758 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
759 uint32_t max_bulk = usbd_xfer_max_len(xfer);
762 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
766 switch (USB_GET_STATE(xfer)) {
767 case USB_ST_TRANSFERRED:
768 sc->sc_transfer.data_rem -= actlen;
769 sc->sc_transfer.data_ptr += actlen;
770 sc->sc_transfer.offset += actlen;
772 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
773 /* short transfer or end of data */
774 ustorage_fs_transfer_start(sc,
775 USTORAGE_FS_T_BBB_STATUS);
780 if (max_bulk >= sc->sc_transfer.data_rem) {
781 max_bulk = sc->sc_transfer.data_rem;
782 if (sc->sc_transfer.data_short)
783 usbd_xfer_set_flag(xfer, USB_FORCE_SHORT_XFER);
785 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
787 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
789 if (sc->sc_transfer.data_error) {
790 sc->sc_transfer.data_error = 0;
791 usbd_xfer_set_stall(xfer);
794 /* XXX copy data to DMA buffer */
795 memcpy(sc->sc_dma_ptr, sc->sc_transfer.data_ptr, max_bulk);
797 usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
798 usbd_transfer_submit(xfer);
802 if (error == USB_ERR_CANCELLED) {
806 * If the pipe is already stalled, don't do another
809 if (!usbd_xfer_is_stalled(xfer))
810 sc->sc_transfer.data_error = 1;
818 ustorage_fs_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
820 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
824 switch (USB_GET_STATE(xfer)) {
825 case USB_ST_TRANSFERRED:
826 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
831 USETDW(sc->sc_csw->dCSWSignature, CSWSIGNATURE);
832 USETDW(sc->sc_csw->dCSWDataResidue, sc->sc_transfer.data_rem);
834 if (sc->sc_transfer.data_error) {
835 sc->sc_transfer.data_error = 0;
836 usbd_xfer_set_stall(xfer);
838 usbd_xfer_set_frame_len(xfer, 0,
839 sizeof(ustorage_fs_bbb_csw_t));
840 usbd_transfer_submit(xfer);
844 if (error == USB_ERR_CANCELLED) {
847 /* If the pipe is already stalled, don't do another stall */
848 if (!usbd_xfer_is_stalled(xfer))
849 sc->sc_transfer.data_error = 1;
856 /* SCSI commands that we recognize */
857 #define SC_FORMAT_UNIT 0x04
858 #define SC_INQUIRY 0x12
859 #define SC_MODE_SELECT_6 0x15
860 #define SC_MODE_SELECT_10 0x55
861 #define SC_MODE_SENSE_6 0x1a
862 #define SC_MODE_SENSE_10 0x5a
863 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
864 #define SC_READ_6 0x08
865 #define SC_READ_10 0x28
866 #define SC_READ_12 0xa8
867 #define SC_READ_CAPACITY 0x25
868 #define SC_READ_FORMAT_CAPACITIES 0x23
869 #define SC_RELEASE 0x17
870 #define SC_REQUEST_SENSE 0x03
871 #define SC_RESERVE 0x16
872 #define SC_SEND_DIAGNOSTIC 0x1d
873 #define SC_START_STOP_UNIT 0x1b
874 #define SC_SYNCHRONIZE_CACHE 0x35
875 #define SC_TEST_UNIT_READY 0x00
876 #define SC_VERIFY 0x2f
877 #define SC_WRITE_6 0x0a
878 #define SC_WRITE_10 0x2a
879 #define SC_WRITE_12 0xaa
881 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
882 #define SS_NO_SENSE 0
883 #define SS_COMMUNICATION_FAILURE 0x040800
884 #define SS_INVALID_COMMAND 0x052000
885 #define SS_INVALID_FIELD_IN_CDB 0x052400
886 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
887 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
888 #define SS_MEDIUM_NOT_PRESENT 0x023a00
889 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
890 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
891 #define SS_RESET_OCCURRED 0x062900
892 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
893 #define SS_UNRECOVERED_READ_ERROR 0x031100
894 #define SS_WRITE_ERROR 0x030c02
895 #define SS_WRITE_PROTECTED 0x072700
897 #define SK(x) ((uint8_t) ((x) >> 16)) /* Sense Key byte, etc. */
898 #define ASC(x) ((uint8_t) ((x) >> 8))
899 #define ASCQ(x) ((uint8_t) (x))
901 /* Routines for unaligned data access */
904 get_be16(uint8_t *buf)
906 return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
910 get_be32(uint8_t *buf)
912 return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
913 ((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
917 put_be16(uint8_t *buf, uint16_t val)
924 put_be32(uint8_t *buf, uint32_t val)
932 /*------------------------------------------------------------------------*
938 *------------------------------------------------------------------------*/
940 ustorage_fs_verify(struct ustorage_fs_softc *sc)
942 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
945 uint64_t file_offset;
946 uint64_t amount_left;
949 * Get the starting Logical Block Address
951 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
954 * We allow DPO (Disable Page Out = don't save data in the cache)
955 * but we don't implement it.
957 if ((sc->sc_cbw->CBWCDB[1] & ~0x10) != 0) {
958 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
961 vlen = get_be16(&sc->sc_cbw->CBWCDB[7]);
965 /* No default reply */
967 /* Prepare to carry out the file verify */
977 (vlen > currlun->num_sectors) ||
978 (lba >= currlun->num_sectors)) {
979 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
982 /* XXX TODO: verify that data is readable */
984 return (ustorage_fs_min_len(sc, 0, -1U));
987 /*------------------------------------------------------------------------*
988 * ustorage_fs_inquiry
993 *------------------------------------------------------------------------*/
995 ustorage_fs_inquiry(struct ustorage_fs_softc *sc)
997 uint8_t *buf = sc->sc_transfer.data_ptr;
999 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1001 if (!sc->sc_transfer.currlun) {
1002 /* Unsupported LUNs are okay */
1005 /* Unsupported, no device - type */
1006 return (ustorage_fs_min_len(sc, 36, -1U));
1009 /* Non - removable, direct - access device */
1010 if (currlun->removable)
1013 /* ANSI SCSI level 2 */
1015 /* SCSI - 2 INQUIRY data format */
1017 /* Additional length */
1018 /* No special options */
1019 /* Copy in ID string */
1020 memcpy(buf + 8, USTORAGE_FS_ID_STRING, 28);
1022 #if (USTORAGE_QDATA_MAX < 36)
1023 #error "(USTORAGE_QDATA_MAX < 36)"
1025 return (ustorage_fs_min_len(sc, 36, -1U));
1028 /*------------------------------------------------------------------------*
1029 * ustorage_fs_request_sense
1034 *------------------------------------------------------------------------*/
1036 ustorage_fs_request_sense(struct ustorage_fs_softc *sc)
1038 uint8_t *buf = sc->sc_transfer.data_ptr;
1039 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1045 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1047 * If a REQUEST SENSE command is received from an initiator
1048 * with a pending unit attention condition (before the target
1049 * generates the contingent allegiance condition), then the
1050 * target shall either:
1051 * a) report any pending sense data and preserve the unit
1052 * attention condition on the logical unit, or,
1053 * b) report the unit attention condition, may discard any
1054 * pending sense data, and clear the unit attention
1055 * condition on the logical unit for that initiator.
1057 * FSG normally uses option a); enable this code to use option b).
1060 if (currlun && currlun->unit_attention_data != SS_NO_SENSE) {
1061 currlun->sense_data = currlun->unit_attention_data;
1062 currlun->unit_attention_data = SS_NO_SENSE;
1067 /* Unsupported LUNs are okay */
1068 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1072 sd = currlun->sense_data;
1073 sdinfo = currlun->sense_data_info;
1074 valid = currlun->info_valid << 7;
1075 currlun->sense_data = SS_NO_SENSE;
1076 currlun->sense_data_info = 0;
1077 currlun->info_valid = 0;
1081 buf[0] = valid | 0x70;
1082 /* Valid, current error */
1084 put_be32(&buf[3], sdinfo);
1085 /* Sense information */
1087 /* Additional sense length */
1091 #if (USTORAGE_QDATA_MAX < 18)
1092 #error "(USTORAGE_QDATA_MAX < 18)"
1094 return (ustorage_fs_min_len(sc, 18, -1U));
1097 /*------------------------------------------------------------------------*
1098 * ustorage_fs_read_capacity
1103 *------------------------------------------------------------------------*/
1105 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1107 uint8_t *buf = sc->sc_transfer.data_ptr;
1108 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1109 uint32_t lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1110 uint8_t pmi = sc->sc_cbw->CBWCDB[8];
1112 /* Check the PMI and LBA fields */
1113 if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1114 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1117 /* Max logical block */
1118 put_be32(&buf[0], currlun->num_sectors - 1);
1120 put_be32(&buf[4], 512);
1122 #if (USTORAGE_QDATA_MAX < 8)
1123 #error "(USTORAGE_QDATA_MAX < 8)"
1125 return (ustorage_fs_min_len(sc, 8, -1U));
1128 /*------------------------------------------------------------------------*
1129 * ustorage_fs_mode_sense
1134 *------------------------------------------------------------------------*/
1136 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1138 uint8_t *buf = sc->sc_transfer.data_ptr;
1139 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1143 uint8_t mscmnd = sc->sc_cbw->CBWCDB[0];
1146 uint8_t changeable_values;
1151 if ((sc->sc_cbw->CBWCDB[1] & ~0x08) != 0) {
1153 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1156 pc = sc->sc_cbw->CBWCDB[2] >> 6;
1157 page_code = sc->sc_cbw->CBWCDB[2] & 0x3f;
1159 currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1162 changeable_values = (pc == 1);
1163 all_pages = (page_code == 0x3f);
1166 * Write the mode parameter header. Fixed values are: default
1167 * medium type, no cache control (DPOFUA), and no block descriptors.
1168 * The only variable value is the WriteProtect bit. We will fill in
1169 * the mode data length later.
1172 if (mscmnd == SC_MODE_SENSE_6) {
1173 buf[2] = (currlun->read_only ? 0x80 : 0x00);
1178 /* SC_MODE_SENSE_10 */
1179 buf[3] = (currlun->read_only ? 0x80 : 0x00);
1183 /* Should really be mod_data.buflen */
1186 /* No block descriptors */
1189 * The mode pages, in numerical order.
1191 if ((page_code == 0x08) || all_pages) {
1196 memset(buf + 2, 0, 10);
1197 /* None of the fields are changeable */
1199 if (!changeable_values) {
1201 /* Write cache enable, */
1202 /* Read cache not disabled */
1203 /* No cache retention priorities */
1204 put_be16(&buf[4], 0xffff);
1205 /* Don 't disable prefetch */
1206 /* Minimum prefetch = 0 */
1207 put_be16(&buf[8], 0xffff);
1208 /* Maximum prefetch */
1209 put_be16(&buf[10], 0xffff);
1210 /* Maximum prefetch ceiling */
1215 * Check that a valid page was requested and the mode data length
1220 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1223 /* Store the mode data length */
1224 if (mscmnd == SC_MODE_SENSE_6)
1227 put_be16(buf0, len - 2);
1229 #if (USTORAGE_QDATA_MAX < 24)
1230 #error "(USTORAGE_QDATA_MAX < 24)"
1232 return (ustorage_fs_min_len(sc, len, -1U));
1235 /*------------------------------------------------------------------------*
1236 * ustorage_fs_start_stop
1241 *------------------------------------------------------------------------*/
1243 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1245 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1250 if (!currlun->removable) {
1251 currlun->sense_data = SS_INVALID_COMMAND;
1254 immed = sc->sc_cbw->CBWCDB[1] & 0x01;
1255 loej = sc->sc_cbw->CBWCDB[4] & 0x02;
1256 start = sc->sc_cbw->CBWCDB[4] & 0x01;
1258 if (immed || loej || start) {
1264 /*------------------------------------------------------------------------*
1265 * ustorage_fs_prevent_allow
1270 *------------------------------------------------------------------------*/
1272 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1274 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1277 if (!currlun->removable) {
1278 currlun->sense_data = SS_INVALID_COMMAND;
1281 prevent = sc->sc_cbw->CBWCDB[4] & 0x01;
1282 if ((sc->sc_cbw->CBWCDB[4] & ~0x01) != 0) {
1283 /* Mask away Prevent */
1284 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1287 if (currlun->prevent_medium_removal && !prevent) {
1288 //fsync_sub(currlun);
1290 currlun->prevent_medium_removal = prevent;
1294 /*------------------------------------------------------------------------*
1295 * ustorage_fs_read_format_capacities
1300 *------------------------------------------------------------------------*/
1302 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1304 uint8_t *buf = sc->sc_transfer.data_ptr;
1305 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1307 buf[0] = buf[1] = buf[2] = 0;
1309 /* Only the Current / Maximum Capacity Descriptor */
1312 /* Number of blocks */
1313 put_be32(&buf[0], currlun->num_sectors);
1315 put_be32(&buf[4], 512);
1316 /* Current capacity */
1319 #if (USTORAGE_QDATA_MAX < 12)
1320 #error "(USTORAGE_QDATA_MAX < 12)"
1322 return (ustorage_fs_min_len(sc, 12, -1U));
1325 /*------------------------------------------------------------------------*
1326 * ustorage_fs_mode_select
1331 *------------------------------------------------------------------------*/
1333 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1335 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1337 /* We don't support MODE SELECT */
1338 currlun->sense_data = SS_INVALID_COMMAND;
1342 /*------------------------------------------------------------------------*
1343 * ustorage_fs_synchronize_cache
1348 *------------------------------------------------------------------------*/
1350 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1353 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1357 * We ignore the requested LBA and write out all dirty data buffers.
1361 currlun->sense_data = SS_WRITE_ERROR;
1367 /*------------------------------------------------------------------------*
1368 * ustorage_fs_read - read data from disk
1373 *------------------------------------------------------------------------*/
1375 ustorage_fs_read(struct ustorage_fs_softc *sc)
1377 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1378 uint64_t file_offset;
1383 * Get the starting Logical Block Address and check that it's not
1386 if (sc->sc_cbw->CBWCDB[0] == SC_READ_6) {
1387 lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1388 get_be16(&sc->sc_cbw->CBWCDB[2]);
1390 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1393 * We allow DPO (Disable Page Out = don't save data in the
1394 * cache) and FUA (Force Unit Access = don't read from the
1395 * cache), but we don't implement them.
1397 if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1398 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1402 len = sc->sc_transfer.data_rem >> 9;
1406 (len > currlun->num_sectors) ||
1407 (lba >= currlun->num_sectors)) {
1408 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1414 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1419 /*------------------------------------------------------------------------*
1420 * ustorage_fs_write - write data to disk
1425 *------------------------------------------------------------------------*/
1427 ustorage_fs_write(struct ustorage_fs_softc *sc)
1429 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1430 uint64_t file_offset;
1434 if (currlun->read_only) {
1435 currlun->sense_data = SS_WRITE_PROTECTED;
1438 /* XXX clear SYNC */
1441 * Get the starting Logical Block Address and check that it's not
1444 if (sc->sc_cbw->CBWCDB[0] == SC_WRITE_6)
1445 lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1446 get_be16(&sc->sc_cbw->CBWCDB[2]);
1448 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1451 * We allow DPO (Disable Page Out = don't save data in the
1452 * cache) and FUA (Force Unit Access = write directly to the
1453 * medium). We don't implement DPO; we implement FUA by
1454 * performing synchronous output.
1456 if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1457 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1460 if (sc->sc_cbw->CBWCDB[1] & 0x08) {
1462 /* XXX set SYNC flag here */
1466 len = sc->sc_transfer.data_rem >> 9;
1470 (len > currlun->num_sectors) ||
1471 (lba >= currlun->num_sectors)) {
1472 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1478 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1483 /*------------------------------------------------------------------------*
1484 * ustorage_fs_min_len
1489 *------------------------------------------------------------------------*/
1491 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1493 if (len != sc->sc_transfer.data_rem) {
1495 if (sc->sc_transfer.cbw_dir == DIR_READ) {
1497 * there must be something wrong about this SCSI
1500 sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1503 /* compute the minimum length */
1505 if (sc->sc_transfer.data_rem > len) {
1506 /* data ends prematurely */
1507 sc->sc_transfer.data_rem = len;
1508 sc->sc_transfer.data_short = 1;
1510 /* check length alignment */
1512 if (sc->sc_transfer.data_rem & ~mask) {
1513 /* data ends prematurely */
1514 sc->sc_transfer.data_rem &= mask;
1515 sc->sc_transfer.data_short = 1;
1521 /*------------------------------------------------------------------------*
1522 * ustorage_fs_check_cmd - check command routine
1524 * Check whether the command is properly formed and whether its data
1525 * size and direction agree with the values we already have.
1530 *------------------------------------------------------------------------*/
1532 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1533 uint16_t mask, uint8_t needs_medium)
1535 struct ustorage_fs_lun *currlun;
1536 uint8_t lun = (sc->sc_cbw->CBWCDB[1] >> 5);
1539 /* Verify the length of the command itself */
1540 if (min_cmd_size > sc->sc_transfer.cmd_len) {
1541 DPRINTF("%u > %u\n",
1542 min_cmd_size, sc->sc_transfer.cmd_len);
1543 sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1546 /* Mask away the LUN */
1547 sc->sc_cbw->CBWCDB[1] &= 0x1f;
1549 /* Check if LUN is correct */
1550 if (lun != sc->sc_transfer.lun) {
1554 if (sc->sc_transfer.lun <= sc->sc_last_lun) {
1555 sc->sc_transfer.currlun = currlun =
1556 sc->sc_lun + sc->sc_transfer.lun;
1557 if (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE) {
1558 currlun->sense_data = SS_NO_SENSE;
1559 currlun->sense_data_info = 0;
1560 currlun->info_valid = 0;
1563 * If a unit attention condition exists, only INQUIRY
1564 * and REQUEST SENSE commands are allowed. Anything
1567 if ((currlun->unit_attention_data != SS_NO_SENSE) &&
1568 (sc->sc_cbw->CBWCDB[0] != SC_INQUIRY) &&
1569 (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE)) {
1570 currlun->sense_data = currlun->unit_attention_data;
1571 currlun->unit_attention_data = SS_NO_SENSE;
1575 sc->sc_transfer.currlun = currlun = NULL;
1578 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1579 * to use unsupported LUNs; all others may not.
1581 if ((sc->sc_cbw->CBWCDB[0] != SC_INQUIRY) &&
1582 (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE)) {
1588 * Check that only command bytes listed in the mask are
1591 for (i = 0; i != min_cmd_size; i++) {
1592 if (sc->sc_cbw->CBWCDB[i] && !(mask & (1UL << i))) {
1594 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1601 * If the medium isn't mounted and the command needs to access
1602 * it, return an error.
1604 if (currlun && (!currlun->memory_image) && needs_medium) {
1605 currlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1611 /*------------------------------------------------------------------------*
1612 * ustorage_fs_do_cmd - do command
1617 *------------------------------------------------------------------------*/
1619 ustorage_fs_do_cmd(struct ustorage_fs_softc *sc)
1624 const uint32_t mask9 = (0xFFFFFFFFUL >> 9) << 9;
1626 /* set default data transfer pointer */
1627 sc->sc_transfer.data_ptr = sc->sc_qdata;
1629 DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1630 sc->sc_cbw->CBWCDB[0], sc->sc_transfer.data_rem);
1632 switch (sc->sc_cbw->CBWCDB[0]) {
1634 sc->sc_transfer.cmd_dir = DIR_WRITE;
1635 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1639 error = ustorage_fs_check_cmd(sc, 6,
1644 error = ustorage_fs_inquiry(sc);
1648 case SC_MODE_SELECT_6:
1649 sc->sc_transfer.cmd_dir = DIR_READ;
1650 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1654 error = ustorage_fs_check_cmd(sc, 6,
1655 (1UL << 1) | (1UL << 4) | 1, 0);
1659 error = ustorage_fs_mode_select(sc);
1663 case SC_MODE_SELECT_10:
1664 sc->sc_transfer.cmd_dir = DIR_READ;
1665 error = ustorage_fs_min_len(sc,
1666 get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1670 error = ustorage_fs_check_cmd(sc, 10,
1671 (1UL << 1) | (3UL << 7) | 1, 0);
1675 error = ustorage_fs_mode_select(sc);
1679 case SC_MODE_SENSE_6:
1680 sc->sc_transfer.cmd_dir = DIR_WRITE;
1681 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1685 error = ustorage_fs_check_cmd(sc, 6,
1686 (1UL << 1) | (1UL << 2) | (1UL << 4) | 1, 0);
1690 error = ustorage_fs_mode_sense(sc);
1694 case SC_MODE_SENSE_10:
1695 sc->sc_transfer.cmd_dir = DIR_WRITE;
1696 error = ustorage_fs_min_len(sc,
1697 get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1701 error = ustorage_fs_check_cmd(sc, 10,
1702 (1UL << 1) | (1UL << 2) | (3UL << 7) | 1, 0);
1706 error = ustorage_fs_mode_sense(sc);
1710 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1711 error = ustorage_fs_min_len(sc, 0, -1U);
1715 error = ustorage_fs_check_cmd(sc, 6,
1720 error = ustorage_fs_prevent_allow(sc);
1725 i = sc->sc_cbw->CBWCDB[4];
1726 sc->sc_transfer.cmd_dir = DIR_WRITE;
1727 temp = ((i == 0) ? 256UL : i);
1728 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1732 error = ustorage_fs_check_cmd(sc, 6,
1733 (7UL << 1) | (1UL << 4) | 1, 1);
1737 error = ustorage_fs_read(sc);
1742 sc->sc_transfer.cmd_dir = DIR_WRITE;
1743 temp = get_be16(&sc->sc_cbw->CBWCDB[7]);
1744 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1748 error = ustorage_fs_check_cmd(sc, 10,
1749 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1753 error = ustorage_fs_read(sc);
1758 sc->sc_transfer.cmd_dir = DIR_WRITE;
1759 temp = get_be32(&sc->sc_cbw->CBWCDB[6]);
1760 if (temp >= (1UL << (32 - 9))) {
1761 /* numerical overflow */
1762 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
1766 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1770 error = ustorage_fs_check_cmd(sc, 12,
1771 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1775 error = ustorage_fs_read(sc);
1779 case SC_READ_CAPACITY:
1780 sc->sc_transfer.cmd_dir = DIR_WRITE;
1781 error = ustorage_fs_check_cmd(sc, 10,
1782 (0xfUL << 2) | (1UL << 8) | 1, 1);
1786 error = ustorage_fs_read_capacity(sc);
1790 case SC_READ_FORMAT_CAPACITIES:
1791 sc->sc_transfer.cmd_dir = DIR_WRITE;
1792 error = ustorage_fs_min_len(sc,
1793 get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1797 error = ustorage_fs_check_cmd(sc, 10,
1802 error = ustorage_fs_read_format_capacities(sc);
1806 case SC_REQUEST_SENSE:
1807 sc->sc_transfer.cmd_dir = DIR_WRITE;
1808 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1812 error = ustorage_fs_check_cmd(sc, 6,
1817 error = ustorage_fs_request_sense(sc);
1821 case SC_START_STOP_UNIT:
1822 error = ustorage_fs_min_len(sc, 0, -1U);
1826 error = ustorage_fs_check_cmd(sc, 6,
1827 (1UL << 1) | (1UL << 4) | 1, 0);
1831 error = ustorage_fs_start_stop(sc);
1835 case SC_SYNCHRONIZE_CACHE:
1836 error = ustorage_fs_min_len(sc, 0, -1U);
1840 error = ustorage_fs_check_cmd(sc, 10,
1841 (0xfUL << 2) | (3UL << 7) | 1, 1);
1845 error = ustorage_fs_synchronize_cache(sc);
1849 case SC_TEST_UNIT_READY:
1850 error = ustorage_fs_min_len(sc, 0, -1U);
1854 error = ustorage_fs_check_cmd(sc, 6,
1859 * Although optional, this command is used by MS-Windows.
1860 * We support a minimal version: BytChk must be 0.
1863 error = ustorage_fs_min_len(sc, 0, -1U);
1867 error = ustorage_fs_check_cmd(sc, 10,
1868 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1872 error = ustorage_fs_verify(sc);
1877 i = sc->sc_cbw->CBWCDB[4];
1878 sc->sc_transfer.cmd_dir = DIR_READ;
1879 temp = ((i == 0) ? 256UL : i);
1880 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1884 error = ustorage_fs_check_cmd(sc, 6,
1885 (7UL << 1) | (1UL << 4) | 1, 1);
1889 error = ustorage_fs_write(sc);
1894 sc->sc_transfer.cmd_dir = DIR_READ;
1895 temp = get_be16(&sc->sc_cbw->CBWCDB[7]);
1896 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1900 error = ustorage_fs_check_cmd(sc, 10,
1901 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1905 error = ustorage_fs_write(sc);
1910 sc->sc_transfer.cmd_dir = DIR_READ;
1911 temp = get_be32(&sc->sc_cbw->CBWCDB[6]);
1912 if (temp > (mask9 >> 9)) {
1913 /* numerical overflow */
1914 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
1918 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1922 error = ustorage_fs_check_cmd(sc, 12,
1923 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1927 error = ustorage_fs_write(sc);
1932 * Some mandatory commands that we recognize but don't
1933 * implement. They don't mean much in this setting.
1934 * It's left as an exercise for anyone interested to
1935 * implement RESERVE and RELEASE in terms of Posix
1938 case SC_FORMAT_UNIT:
1941 case SC_SEND_DIAGNOSTIC:
1945 error = ustorage_fs_min_len(sc, 0, -1U);
1949 error = ustorage_fs_check_cmd(sc, sc->sc_transfer.cmd_len,
1954 sc->sc_transfer.currlun->sense_data =