3 * Copyright (C) 2003-2005 Alan Stern
4 * Copyright (C) 2008 Hans Petter Selasky
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 * without modification.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The names of the above-listed copyright holders may not be used
17 * to endorse or promote products derived from this software without
18 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * NOTE: Much of the SCSI statemachine handling code derives from the
36 * Linux USB gadget stack.
39 #include <sys/stdint.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/condvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <bus/u4b/usb.h>
56 #include <bus/u4b/usbdi.h>
60 #define USB_DEBUG_VAR ustorage_fs_debug
61 #include <bus/u4b/usb_debug.h>
64 static int ustorage_fs_debug = 0;
66 SYSCTL_NODE(_hw_usb, OID_AUTO, ustorage_fs, CTLFLAG_RW, 0, "USB ustorage_fs");
67 SYSCTL_INT(_hw_usb_ustorage_fs, OID_AUTO, debug, CTLFLAG_RW,
68 &ustorage_fs_debug, 0, "ustorage_fs debug level");
71 /* Define some limits */
73 #ifndef USTORAGE_FS_BULK_SIZE
74 #define USTORAGE_FS_BULK_SIZE (1UL << 17) /* bytes */
77 #ifndef USTORAGE_FS_MAX_LUN
78 #define USTORAGE_FS_MAX_LUN 8 /* units */
81 #ifndef USTORAGE_QDATA_MAX
82 #define USTORAGE_QDATA_MAX 40 /* bytes */
85 #define sc_cmd_data sc_cbw.CBWCDB
88 * The SCSI ID string must be exactly 28 characters long
89 * exluding the terminating zero.
91 #ifndef USTORAGE_FS_ID_STRING
92 #define USTORAGE_FS_ID_STRING \
94 "File-Stor Gadget" /* 16 */ \
99 * The following macro defines the number of
100 * sectors to be allocated for the RAM disk:
102 #ifndef USTORAGE_FS_RAM_SECT
103 #define USTORAGE_FS_RAM_SECT (1UL << 13)
106 static uint8_t *ustorage_fs_ramdisk;
108 /* USB transfer definitions */
110 #define USTORAGE_FS_T_BBB_COMMAND 0
111 #define USTORAGE_FS_T_BBB_DATA_DUMP 1
112 #define USTORAGE_FS_T_BBB_DATA_READ 2
113 #define USTORAGE_FS_T_BBB_DATA_WRITE 3
114 #define USTORAGE_FS_T_BBB_STATUS 4
115 #define USTORAGE_FS_T_BBB_MAX 5
117 /* USB data stage direction */
123 /* USB interface specific control request */
125 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
126 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
128 /* Command Block Wrapper */
130 uDWord dCBWSignature;
131 #define CBWSIGNATURE 0x43425355
133 uDWord dCBWDataTransferLength;
135 #define CBWFLAGS_OUT 0x00
136 #define CBWFLAGS_IN 0x80
139 #define CBWCDBLENGTH 16
140 uByte CBWCDB[CBWCDBLENGTH];
141 } __packed ustorage_fs_bbb_cbw_t;
143 #define USTORAGE_FS_BBB_CBW_SIZE 31
145 /* Command Status Wrapper */
147 uDWord dCSWSignature;
148 #define CSWSIGNATURE 0x53425355
150 uDWord dCSWDataResidue;
152 #define CSWSTATUS_GOOD 0x0
153 #define CSWSTATUS_FAILED 0x1
154 #define CSWSTATUS_PHASE 0x2
155 } __packed ustorage_fs_bbb_csw_t;
157 #define USTORAGE_FS_BBB_CSW_SIZE 13
159 struct ustorage_fs_lun {
161 uint8_t *memory_image;
163 uint32_t num_sectors;
165 uint32_t sense_data_info;
166 uint32_t unit_attention_data;
169 uint8_t prevent_medium_removal:1;
170 uint8_t info_valid:1;
174 struct ustorage_fs_softc {
176 ustorage_fs_bbb_cbw_t *sc_cbw; /* Command Wrapper Block */
177 ustorage_fs_bbb_csw_t *sc_csw; /* Command Status Block */
178 void *sc_dma_ptr; /* Main data buffer */
182 struct ustorage_fs_lun sc_lun[USTORAGE_FS_MAX_LUN];
186 struct ustorage_fs_lun *currlun;
188 uint32_t data_rem; /* bytes, as reported by the command
190 uint32_t offset; /* bytes */
196 uint8_t data_short:1;
197 uint8_t data_error:1;
201 struct usb_device *sc_udev;
202 struct usb_xfer *sc_xfer[USTORAGE_FS_T_BBB_MAX];
204 uint8_t sc_iface_no; /* interface number */
206 uint8_t sc_last_xfer_index;
207 uint8_t sc_qdata[USTORAGE_QDATA_MAX];
212 static device_probe_t ustorage_fs_probe;
213 static device_attach_t ustorage_fs_attach;
214 static device_detach_t ustorage_fs_detach;
215 static device_suspend_t ustorage_fs_suspend;
216 static device_resume_t ustorage_fs_resume;
217 static usb_handle_request_t ustorage_fs_handle_request;
219 static usb_callback_t ustorage_fs_t_bbb_command_callback;
220 static usb_callback_t ustorage_fs_t_bbb_data_dump_callback;
221 static usb_callback_t ustorage_fs_t_bbb_data_read_callback;
222 static usb_callback_t ustorage_fs_t_bbb_data_write_callback;
223 static usb_callback_t ustorage_fs_t_bbb_status_callback;
225 static void ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index);
226 static void ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc);
228 static uint8_t ustorage_fs_verify(struct ustorage_fs_softc *sc);
229 static uint8_t ustorage_fs_inquiry(struct ustorage_fs_softc *sc);
230 static uint8_t ustorage_fs_request_sense(struct ustorage_fs_softc *sc);
231 static uint8_t ustorage_fs_read_capacity(struct ustorage_fs_softc *sc);
232 static uint8_t ustorage_fs_mode_sense(struct ustorage_fs_softc *sc);
233 static uint8_t ustorage_fs_start_stop(struct ustorage_fs_softc *sc);
234 static uint8_t ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc);
235 static uint8_t ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc);
236 static uint8_t ustorage_fs_mode_select(struct ustorage_fs_softc *sc);
237 static uint8_t ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask);
238 static uint8_t ustorage_fs_read(struct ustorage_fs_softc *sc);
239 static uint8_t ustorage_fs_write(struct ustorage_fs_softc *sc);
240 static uint8_t ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t cmd_size, uint16_t mask, uint8_t needs_medium);
241 static uint8_t ustorage_fs_do_cmd(struct ustorage_fs_softc *sc);
243 static device_method_t ustorage_fs_methods[] = {
245 DEVMETHOD(usb_handle_request, ustorage_fs_handle_request),
247 /* Device interface */
248 DEVMETHOD(device_probe, ustorage_fs_probe),
249 DEVMETHOD(device_attach, ustorage_fs_attach),
250 DEVMETHOD(device_detach, ustorage_fs_detach),
251 DEVMETHOD(device_suspend, ustorage_fs_suspend),
252 DEVMETHOD(device_resume, ustorage_fs_resume),
257 static driver_t ustorage_fs_driver = {
258 .name = "ustorage_fs",
259 .methods = ustorage_fs_methods,
260 .size = sizeof(struct ustorage_fs_softc),
263 static devclass_t ustorage_fs_devclass;
265 DRIVER_MODULE(ustorage_fs, uhub, ustorage_fs_driver, ustorage_fs_devclass, NULL, NULL);
266 MODULE_VERSION(ustorage_fs, 0);
267 MODULE_DEPEND(ustorage_fs, usb, 1, 1, 1);
269 static struct usb_config ustorage_fs_bbb_config[USTORAGE_FS_T_BBB_MAX] = {
271 [USTORAGE_FS_T_BBB_COMMAND] = {
273 .endpoint = UE_ADDR_ANY,
274 .direction = UE_DIR_OUT,
275 .bufsize = sizeof(ustorage_fs_bbb_cbw_t),
276 .callback = &ustorage_fs_t_bbb_command_callback,
277 .usb_mode = USB_MODE_DEVICE,
280 [USTORAGE_FS_T_BBB_DATA_DUMP] = {
282 .endpoint = UE_ADDR_ANY,
283 .direction = UE_DIR_OUT,
284 .bufsize = 0, /* use wMaxPacketSize */
285 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
286 .callback = &ustorage_fs_t_bbb_data_dump_callback,
287 .usb_mode = USB_MODE_DEVICE,
290 [USTORAGE_FS_T_BBB_DATA_READ] = {
292 .endpoint = UE_ADDR_ANY,
293 .direction = UE_DIR_OUT,
294 .bufsize = USTORAGE_FS_BULK_SIZE,
295 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1},
296 .callback = &ustorage_fs_t_bbb_data_read_callback,
297 .usb_mode = USB_MODE_DEVICE,
300 [USTORAGE_FS_T_BBB_DATA_WRITE] = {
302 .endpoint = UE_ADDR_ANY,
303 .direction = UE_DIR_IN,
304 .bufsize = USTORAGE_FS_BULK_SIZE,
305 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
306 .callback = &ustorage_fs_t_bbb_data_write_callback,
307 .usb_mode = USB_MODE_DEVICE,
310 [USTORAGE_FS_T_BBB_STATUS] = {
312 .endpoint = UE_ADDR_ANY,
313 .direction = UE_DIR_IN,
314 .bufsize = sizeof(ustorage_fs_bbb_csw_t),
315 .flags = {.short_xfer_ok = 1},
316 .callback = &ustorage_fs_t_bbb_status_callback,
317 .usb_mode = USB_MODE_DEVICE,
322 * USB device probe/attach/detach
326 ustorage_fs_probe(device_t dev)
328 struct usb_attach_arg *uaa = device_get_ivars(dev);
329 struct usb_interface_descriptor *id;
331 if (uaa->usb_mode != USB_MODE_DEVICE) {
334 /* Check for a standards compliant device */
335 id = usbd_get_interface_descriptor(uaa->iface);
337 (id->bInterfaceClass != UICLASS_MASS) ||
338 (id->bInterfaceSubClass != UISUBCLASS_SCSI) ||
339 (id->bInterfaceProtocol != UIPROTO_MASS_BBB)) {
342 return (BUS_PROBE_GENERIC);
346 ustorage_fs_attach(device_t dev)
348 struct ustorage_fs_softc *sc = device_get_softc(dev);
349 struct usb_attach_arg *uaa = device_get_ivars(dev);
350 struct usb_interface_descriptor *id;
355 * NOTE: the softc struct is cleared in device_set_driver.
356 * We can safely call ustorage_fs_detach without specifically
357 * initializing the struct.
361 sc->sc_udev = uaa->device;
362 unit = device_get_unit(dev);
364 /* enable power saving mode */
365 usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE);
368 if (ustorage_fs_ramdisk == NULL) {
370 * allocate a memory image for our ramdisk until
373 ustorage_fs_ramdisk =
374 kmalloc(USTORAGE_FS_RAM_SECT << 9, M_USB,
377 if (ustorage_fs_ramdisk == NULL) {
381 sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
382 sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
383 sc->sc_lun[0].removable = 1;
386 device_set_usb_desc(dev);
388 lockinit(&sc->sc_lock, "USTORAGE_FS lock", 0, LK_CANRECURSE);
390 /* get interface index */
392 id = usbd_get_interface_descriptor(uaa->iface);
394 device_printf(dev, "failed to get "
395 "interface number\n");
398 sc->sc_iface_no = id->bInterfaceNumber;
400 err = usbd_transfer_setup(uaa->device,
401 &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
402 USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_lock);
404 device_printf(dev, "could not setup required "
405 "transfers, %s\n", usbd_errstr(err));
409 sc->sc_cbw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
410 USTORAGE_FS_T_BBB_COMMAND], 0);
411 sc->sc_csw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
412 USTORAGE_FS_T_BBB_STATUS], 0);
413 sc->sc_dma_ptr = usbd_xfer_get_frame_buffer(sc->sc_xfer[
414 USTORAGE_FS_T_BBB_DATA_READ], 0);
416 /* start Mass Storage State Machine */
418 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
419 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
420 lockmgr(&sc->sc_lock, LK_RELEASE);
422 return (0); /* success */
425 ustorage_fs_detach(dev);
426 return (ENXIO); /* failure */
430 ustorage_fs_detach(device_t dev)
432 struct ustorage_fs_softc *sc = device_get_softc(dev);
434 /* teardown our statemachine */
436 usbd_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
438 lockuninit(&sc->sc_lock);
440 return (0); /* success */
444 ustorage_fs_suspend(device_t dev)
446 device_printf(dev, "suspending\n");
447 return (0); /* success */
451 ustorage_fs_resume(device_t dev)
453 device_printf(dev, "resuming\n");
454 return (0); /* success */
458 * Generic functions to handle transfers
462 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
464 if (sc->sc_xfer[xfer_index]) {
465 sc->sc_last_xfer_index = xfer_index;
466 usbd_transfer_start(sc->sc_xfer[xfer_index]);
471 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
473 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
474 lockmgr(&sc->sc_lock, LK_RELEASE);
475 usbd_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
476 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
480 ustorage_fs_handle_request(device_t dev,
481 const void *preq, void **pptr, uint16_t *plen,
482 uint16_t offset, uint8_t *pstate)
484 struct ustorage_fs_softc *sc = device_get_softc(dev);
485 const struct usb_device_request *req = preq;
486 uint8_t is_complete = *pstate;
489 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
490 (req->bRequest == UR_BBB_RESET)) {
492 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
493 ustorage_fs_transfer_stop(sc);
494 sc->sc_transfer.data_error = 1;
495 ustorage_fs_transfer_start(sc,
496 USTORAGE_FS_T_BBB_COMMAND);
497 lockmgr(&sc->sc_lock, LK_RELEASE);
499 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
500 (req->bRequest == UR_BBB_GET_MAX_LUN)) {
503 *pptr = &sc->sc_last_lun;
510 return (ENXIO); /* use builtin handler */
514 ustorage_fs_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
516 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
522 switch (USB_GET_STATE(xfer)) {
523 case USB_ST_TRANSFERRED:
525 tag = UGETDW(sc->sc_cbw->dCBWSignature);
527 if (tag != CBWSIGNATURE) {
529 DPRINTF("invalid signature 0x%08x\n", tag);
532 tag = UGETDW(sc->sc_cbw->dCBWTag);
535 USETDW(sc->sc_csw->dCSWTag, tag);
538 sc->sc_csw->bCSWStatus = 0;
540 /* reset data offset, data length and data remainder */
541 sc->sc_transfer.offset = 0;
542 sc->sc_transfer.data_rem =
543 UGETDW(sc->sc_cbw->dCBWDataTransferLength);
545 /* reset data flags */
546 sc->sc_transfer.data_short = 0;
549 sc->sc_transfer.lun = sc->sc_cbw->bCBWLUN;
551 if (sc->sc_transfer.data_rem == 0) {
552 sc->sc_transfer.cbw_dir = DIR_NONE;
554 if (sc->sc_cbw->bCBWFlags & CBWFLAGS_IN) {
555 sc->sc_transfer.cbw_dir = DIR_WRITE;
557 sc->sc_transfer.cbw_dir = DIR_READ;
561 sc->sc_transfer.cmd_len = sc->sc_cbw->bCDBLength;
562 if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw->CBWCDB)) ||
563 (sc->sc_transfer.cmd_len == 0)) {
564 /* just halt - this is invalid */
565 DPRINTF("invalid command length %d bytes\n",
566 sc->sc_transfer.cmd_len);
570 err = ustorage_fs_do_cmd(sc);
573 DPRINTF("command failed\n");
576 if ((sc->sc_transfer.data_rem > 0) &&
577 (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
578 /* contradicting data transfer direction */
580 DPRINTF("data direction mismatch\n");
583 switch (sc->sc_transfer.cbw_dir) {
585 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
588 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
591 ustorage_fs_transfer_start(sc,
592 USTORAGE_FS_T_BBB_STATUS);
599 if (sc->sc_transfer.data_error) {
600 sc->sc_transfer.data_error = 0;
601 usbd_xfer_set_stall(xfer);
602 DPRINTF("stall pipe\n");
604 usbd_xfer_set_frame_len(xfer, 0,
605 sizeof(ustorage_fs_bbb_cbw_t));
606 usbd_transfer_submit(xfer);
611 if (error == USB_ERR_CANCELLED) {
614 /* If the pipe is already stalled, don't do another stall */
615 if (!usbd_xfer_is_stalled(xfer))
616 sc->sc_transfer.data_error = 1;
622 if (sc->sc_csw->bCSWStatus == 0) {
623 /* set some default error code */
624 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
626 if (sc->sc_transfer.cbw_dir == DIR_READ) {
628 ustorage_fs_transfer_start(sc,
629 USTORAGE_FS_T_BBB_DATA_DUMP);
632 if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
633 /* need to stall before status */
634 sc->sc_transfer.data_error = 1;
636 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
641 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer *xfer, usb_error_t error)
643 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
644 uint32_t max_bulk = usbd_xfer_max_len(xfer);
647 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
651 switch (USB_GET_STATE(xfer)) {
652 case USB_ST_TRANSFERRED:
653 sc->sc_transfer.data_rem -= actlen;
654 sc->sc_transfer.offset += actlen;
656 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
657 /* short transfer or end of data */
658 ustorage_fs_transfer_start(sc,
659 USTORAGE_FS_T_BBB_STATUS);
666 if (max_bulk > sc->sc_transfer.data_rem) {
667 max_bulk = sc->sc_transfer.data_rem;
669 if (sc->sc_transfer.data_error) {
670 sc->sc_transfer.data_error = 0;
671 usbd_xfer_set_stall(xfer);
673 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
674 usbd_transfer_submit(xfer);
678 if (error == USB_ERR_CANCELLED) {
682 * If the pipe is already stalled, don't do another stall:
684 if (!usbd_xfer_is_stalled(xfer))
685 sc->sc_transfer.data_error = 1;
693 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
695 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
696 uint32_t max_bulk = usbd_xfer_max_len(xfer);
699 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
703 switch (USB_GET_STATE(xfer)) {
704 case USB_ST_TRANSFERRED:
705 /* XXX copy data from DMA buffer */
706 memcpy(sc->sc_transfer.data_ptr, sc->sc_dma_ptr, actlen);
708 sc->sc_transfer.data_rem -= actlen;
709 sc->sc_transfer.data_ptr += actlen;
710 sc->sc_transfer.offset += actlen;
712 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
713 /* short transfer or end of data */
714 ustorage_fs_transfer_start(sc,
715 USTORAGE_FS_T_BBB_STATUS);
722 if (max_bulk > sc->sc_transfer.data_rem) {
723 max_bulk = sc->sc_transfer.data_rem;
725 if (sc->sc_transfer.data_error) {
726 sc->sc_transfer.data_error = 0;
727 usbd_xfer_set_stall(xfer);
730 usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
731 usbd_transfer_submit(xfer);
735 if (error == USB_ERR_CANCELLED) {
738 /* If the pipe is already stalled, don't do another stall */
739 if (!usbd_xfer_is_stalled(xfer))
740 sc->sc_transfer.data_error = 1;
748 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
750 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
751 uint32_t max_bulk = usbd_xfer_max_len(xfer);
754 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
758 switch (USB_GET_STATE(xfer)) {
759 case USB_ST_TRANSFERRED:
760 sc->sc_transfer.data_rem -= actlen;
761 sc->sc_transfer.data_ptr += actlen;
762 sc->sc_transfer.offset += actlen;
764 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
765 /* short transfer or end of data */
766 ustorage_fs_transfer_start(sc,
767 USTORAGE_FS_T_BBB_STATUS);
772 if (max_bulk >= sc->sc_transfer.data_rem) {
773 max_bulk = sc->sc_transfer.data_rem;
774 if (sc->sc_transfer.data_short)
775 usbd_xfer_set_flag(xfer, USB_FORCE_SHORT_XFER);
777 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
779 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
781 if (sc->sc_transfer.data_error) {
782 sc->sc_transfer.data_error = 0;
783 usbd_xfer_set_stall(xfer);
786 /* XXX copy data to DMA buffer */
787 memcpy(sc->sc_dma_ptr, sc->sc_transfer.data_ptr, max_bulk);
789 usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
790 usbd_transfer_submit(xfer);
794 if (error == USB_ERR_CANCELLED) {
798 * If the pipe is already stalled, don't do another
801 if (!usbd_xfer_is_stalled(xfer))
802 sc->sc_transfer.data_error = 1;
810 ustorage_fs_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
812 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
816 switch (USB_GET_STATE(xfer)) {
817 case USB_ST_TRANSFERRED:
818 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
823 USETDW(sc->sc_csw->dCSWSignature, CSWSIGNATURE);
824 USETDW(sc->sc_csw->dCSWDataResidue, sc->sc_transfer.data_rem);
826 if (sc->sc_transfer.data_error) {
827 sc->sc_transfer.data_error = 0;
828 usbd_xfer_set_stall(xfer);
830 usbd_xfer_set_frame_len(xfer, 0,
831 sizeof(ustorage_fs_bbb_csw_t));
832 usbd_transfer_submit(xfer);
836 if (error == USB_ERR_CANCELLED) {
839 /* If the pipe is already stalled, don't do another stall */
840 if (!usbd_xfer_is_stalled(xfer))
841 sc->sc_transfer.data_error = 1;
848 /* SCSI commands that we recognize */
849 #define SC_FORMAT_UNIT 0x04
850 #define SC_INQUIRY 0x12
851 #define SC_MODE_SELECT_6 0x15
852 #define SC_MODE_SELECT_10 0x55
853 #define SC_MODE_SENSE_6 0x1a
854 #define SC_MODE_SENSE_10 0x5a
855 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
856 #define SC_READ_6 0x08
857 #define SC_READ_10 0x28
858 #define SC_READ_12 0xa8
859 #define SC_READ_CAPACITY 0x25
860 #define SC_READ_FORMAT_CAPACITIES 0x23
861 #define SC_RELEASE 0x17
862 #define SC_REQUEST_SENSE 0x03
863 #define SC_RESERVE 0x16
864 #define SC_SEND_DIAGNOSTIC 0x1d
865 #define SC_START_STOP_UNIT 0x1b
866 #define SC_SYNCHRONIZE_CACHE 0x35
867 #define SC_TEST_UNIT_READY 0x00
868 #define SC_VERIFY 0x2f
869 #define SC_WRITE_6 0x0a
870 #define SC_WRITE_10 0x2a
871 #define SC_WRITE_12 0xaa
873 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
874 #define SS_NO_SENSE 0
875 #define SS_COMMUNICATION_FAILURE 0x040800
876 #define SS_INVALID_COMMAND 0x052000
877 #define SS_INVALID_FIELD_IN_CDB 0x052400
878 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
879 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
880 #define SS_MEDIUM_NOT_PRESENT 0x023a00
881 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
882 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
883 #define SS_RESET_OCCURRED 0x062900
884 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
885 #define SS_UNRECOVERED_READ_ERROR 0x031100
886 #define SS_WRITE_ERROR 0x030c02
887 #define SS_WRITE_PROTECTED 0x072700
889 #define SK(x) ((uint8_t) ((x) >> 16)) /* Sense Key byte, etc. */
890 #define ASC(x) ((uint8_t) ((x) >> 8))
891 #define ASCQ(x) ((uint8_t) (x))
893 /* Routines for unaligned data access */
896 get_be16(uint8_t *buf)
898 return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
902 get_be32(uint8_t *buf)
904 return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
905 ((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
909 put_be16(uint8_t *buf, uint16_t val)
916 put_be32(uint8_t *buf, uint32_t val)
924 /*------------------------------------------------------------------------*
930 *------------------------------------------------------------------------*/
932 ustorage_fs_verify(struct ustorage_fs_softc *sc)
934 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
937 uint64_t file_offset;
938 uint64_t amount_left;
941 * Get the starting Logical Block Address
943 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
946 * We allow DPO (Disable Page Out = don't save data in the cache)
947 * but we don't implement it.
949 if ((sc->sc_cbw->CBWCDB[1] & ~0x10) != 0) {
950 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
953 vlen = get_be16(&sc->sc_cbw->CBWCDB[7]);
957 /* No default reply */
959 /* Prepare to carry out the file verify */
969 (vlen > currlun->num_sectors) ||
970 (lba >= currlun->num_sectors)) {
971 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
974 /* XXX TODO: verify that data is readable */
976 return (ustorage_fs_min_len(sc, 0, -1U));
979 /*------------------------------------------------------------------------*
980 * ustorage_fs_inquiry
985 *------------------------------------------------------------------------*/
987 ustorage_fs_inquiry(struct ustorage_fs_softc *sc)
989 uint8_t *buf = sc->sc_transfer.data_ptr;
991 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
993 if (!sc->sc_transfer.currlun) {
994 /* Unsupported LUNs are okay */
997 /* Unsupported, no device - type */
998 return (ustorage_fs_min_len(sc, 36, -1U));
1001 /* Non - removable, direct - access device */
1002 if (currlun->removable)
1005 /* ANSI SCSI level 2 */
1007 /* SCSI - 2 INQUIRY data format */
1009 /* Additional length */
1010 /* No special options */
1011 /* Copy in ID string */
1012 memcpy(buf + 8, USTORAGE_FS_ID_STRING, 28);
1014 #if (USTORAGE_QDATA_MAX < 36)
1015 #error "(USTORAGE_QDATA_MAX < 36)"
1017 return (ustorage_fs_min_len(sc, 36, -1U));
1020 /*------------------------------------------------------------------------*
1021 * ustorage_fs_request_sense
1026 *------------------------------------------------------------------------*/
1028 ustorage_fs_request_sense(struct ustorage_fs_softc *sc)
1030 uint8_t *buf = sc->sc_transfer.data_ptr;
1031 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1037 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1039 * If a REQUEST SENSE command is received from an initiator
1040 * with a pending unit attention condition (before the target
1041 * generates the contingent allegiance condition), then the
1042 * target shall either:
1043 * a) report any pending sense data and preserve the unit
1044 * attention condition on the logical unit, or,
1045 * b) report the unit attention condition, may discard any
1046 * pending sense data, and clear the unit attention
1047 * condition on the logical unit for that initiator.
1049 * FSG normally uses option a); enable this code to use option b).
1052 if (currlun && currlun->unit_attention_data != SS_NO_SENSE) {
1053 currlun->sense_data = currlun->unit_attention_data;
1054 currlun->unit_attention_data = SS_NO_SENSE;
1059 /* Unsupported LUNs are okay */
1060 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1064 sd = currlun->sense_data;
1065 sdinfo = currlun->sense_data_info;
1066 valid = currlun->info_valid << 7;
1067 currlun->sense_data = SS_NO_SENSE;
1068 currlun->sense_data_info = 0;
1069 currlun->info_valid = 0;
1073 buf[0] = valid | 0x70;
1074 /* Valid, current error */
1076 put_be32(&buf[3], sdinfo);
1077 /* Sense information */
1079 /* Additional sense length */
1083 #if (USTORAGE_QDATA_MAX < 18)
1084 #error "(USTORAGE_QDATA_MAX < 18)"
1086 return (ustorage_fs_min_len(sc, 18, -1U));
1089 /*------------------------------------------------------------------------*
1090 * ustorage_fs_read_capacity
1095 *------------------------------------------------------------------------*/
1097 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1099 uint8_t *buf = sc->sc_transfer.data_ptr;
1100 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1101 uint32_t lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1102 uint8_t pmi = sc->sc_cbw->CBWCDB[8];
1104 /* Check the PMI and LBA fields */
1105 if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1106 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1109 /* Max logical block */
1110 put_be32(&buf[0], currlun->num_sectors - 1);
1112 put_be32(&buf[4], 512);
1114 #if (USTORAGE_QDATA_MAX < 8)
1115 #error "(USTORAGE_QDATA_MAX < 8)"
1117 return (ustorage_fs_min_len(sc, 8, -1U));
1120 /*------------------------------------------------------------------------*
1121 * ustorage_fs_mode_sense
1126 *------------------------------------------------------------------------*/
1128 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1130 uint8_t *buf = sc->sc_transfer.data_ptr;
1131 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1135 uint8_t mscmnd = sc->sc_cbw->CBWCDB[0];
1138 uint8_t changeable_values;
1143 if ((sc->sc_cbw->CBWCDB[1] & ~0x08) != 0) {
1145 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1148 pc = sc->sc_cbw->CBWCDB[2] >> 6;
1149 page_code = sc->sc_cbw->CBWCDB[2] & 0x3f;
1151 currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1154 changeable_values = (pc == 1);
1155 all_pages = (page_code == 0x3f);
1158 * Write the mode parameter header. Fixed values are: default
1159 * medium type, no cache control (DPOFUA), and no block descriptors.
1160 * The only variable value is the WriteProtect bit. We will fill in
1161 * the mode data length later.
1164 if (mscmnd == SC_MODE_SENSE_6) {
1165 buf[2] = (currlun->read_only ? 0x80 : 0x00);
1170 /* SC_MODE_SENSE_10 */
1171 buf[3] = (currlun->read_only ? 0x80 : 0x00);
1175 /* Should really be mod_data.buflen */
1178 /* No block descriptors */
1181 * The mode pages, in numerical order.
1183 if ((page_code == 0x08) || all_pages) {
1188 memset(buf + 2, 0, 10);
1189 /* None of the fields are changeable */
1191 if (!changeable_values) {
1193 /* Write cache enable, */
1194 /* Read cache not disabled */
1195 /* No cache retention priorities */
1196 put_be16(&buf[4], 0xffff);
1197 /* Don 't disable prefetch */
1198 /* Minimum prefetch = 0 */
1199 put_be16(&buf[8], 0xffff);
1200 /* Maximum prefetch */
1201 put_be16(&buf[10], 0xffff);
1202 /* Maximum prefetch ceiling */
1207 * Check that a valid page was requested and the mode data length
1212 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1215 /* Store the mode data length */
1216 if (mscmnd == SC_MODE_SENSE_6)
1219 put_be16(buf0, len - 2);
1221 #if (USTORAGE_QDATA_MAX < 24)
1222 #error "(USTORAGE_QDATA_MAX < 24)"
1224 return (ustorage_fs_min_len(sc, len, -1U));
1227 /*------------------------------------------------------------------------*
1228 * ustorage_fs_start_stop
1233 *------------------------------------------------------------------------*/
1235 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1237 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1242 if (!currlun->removable) {
1243 currlun->sense_data = SS_INVALID_COMMAND;
1246 immed = sc->sc_cbw->CBWCDB[1] & 0x01;
1247 loej = sc->sc_cbw->CBWCDB[4] & 0x02;
1248 start = sc->sc_cbw->CBWCDB[4] & 0x01;
1250 if (immed || loej || start) {
1256 /*------------------------------------------------------------------------*
1257 * ustorage_fs_prevent_allow
1262 *------------------------------------------------------------------------*/
1264 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1266 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1269 if (!currlun->removable) {
1270 currlun->sense_data = SS_INVALID_COMMAND;
1273 prevent = sc->sc_cbw->CBWCDB[4] & 0x01;
1274 if ((sc->sc_cbw->CBWCDB[4] & ~0x01) != 0) {
1275 /* Mask away Prevent */
1276 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1279 if (currlun->prevent_medium_removal && !prevent) {
1280 //fsync_sub(currlun);
1282 currlun->prevent_medium_removal = prevent;
1286 /*------------------------------------------------------------------------*
1287 * ustorage_fs_read_format_capacities
1292 *------------------------------------------------------------------------*/
1294 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1296 uint8_t *buf = sc->sc_transfer.data_ptr;
1297 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1299 buf[0] = buf[1] = buf[2] = 0;
1301 /* Only the Current / Maximum Capacity Descriptor */
1304 /* Number of blocks */
1305 put_be32(&buf[0], currlun->num_sectors);
1307 put_be32(&buf[4], 512);
1308 /* Current capacity */
1311 #if (USTORAGE_QDATA_MAX < 12)
1312 #error "(USTORAGE_QDATA_MAX < 12)"
1314 return (ustorage_fs_min_len(sc, 12, -1U));
1317 /*------------------------------------------------------------------------*
1318 * ustorage_fs_mode_select
1323 *------------------------------------------------------------------------*/
1325 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1327 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1329 /* We don't support MODE SELECT */
1330 currlun->sense_data = SS_INVALID_COMMAND;
1334 /*------------------------------------------------------------------------*
1335 * ustorage_fs_synchronize_cache
1340 *------------------------------------------------------------------------*/
1342 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1345 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1349 * We ignore the requested LBA and write out all dirty data buffers.
1353 currlun->sense_data = SS_WRITE_ERROR;
1359 /*------------------------------------------------------------------------*
1360 * ustorage_fs_read - read data from disk
1365 *------------------------------------------------------------------------*/
1367 ustorage_fs_read(struct ustorage_fs_softc *sc)
1369 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1370 uint64_t file_offset;
1375 * Get the starting Logical Block Address and check that it's not
1378 if (sc->sc_cbw->CBWCDB[0] == SC_READ_6) {
1379 lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1380 get_be16(&sc->sc_cbw->CBWCDB[2]);
1382 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1385 * We allow DPO (Disable Page Out = don't save data in the
1386 * cache) and FUA (Force Unit Access = don't read from the
1387 * cache), but we don't implement them.
1389 if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1390 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1394 len = sc->sc_transfer.data_rem >> 9;
1398 (len > currlun->num_sectors) ||
1399 (lba >= currlun->num_sectors)) {
1400 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1406 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1411 /*------------------------------------------------------------------------*
1412 * ustorage_fs_write - write data to disk
1417 *------------------------------------------------------------------------*/
1419 ustorage_fs_write(struct ustorage_fs_softc *sc)
1421 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1422 uint64_t file_offset;
1426 if (currlun->read_only) {
1427 currlun->sense_data = SS_WRITE_PROTECTED;
1430 /* XXX clear SYNC */
1433 * Get the starting Logical Block Address and check that it's not
1436 if (sc->sc_cbw->CBWCDB[0] == SC_WRITE_6)
1437 lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1438 get_be16(&sc->sc_cbw->CBWCDB[2]);
1440 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1443 * We allow DPO (Disable Page Out = don't save data in the
1444 * cache) and FUA (Force Unit Access = write directly to the
1445 * medium). We don't implement DPO; we implement FUA by
1446 * performing synchronous output.
1448 if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1449 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1452 if (sc->sc_cbw->CBWCDB[1] & 0x08) {
1454 /* XXX set SYNC flag here */
1458 len = sc->sc_transfer.data_rem >> 9;
1462 (len > currlun->num_sectors) ||
1463 (lba >= currlun->num_sectors)) {
1464 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1470 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1475 /*------------------------------------------------------------------------*
1476 * ustorage_fs_min_len
1481 *------------------------------------------------------------------------*/
1483 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1485 if (len != sc->sc_transfer.data_rem) {
1487 if (sc->sc_transfer.cbw_dir == DIR_READ) {
1489 * there must be something wrong about this SCSI
1492 sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1495 /* compute the minimum length */
1497 if (sc->sc_transfer.data_rem > len) {
1498 /* data ends prematurely */
1499 sc->sc_transfer.data_rem = len;
1500 sc->sc_transfer.data_short = 1;
1502 /* check length alignment */
1504 if (sc->sc_transfer.data_rem & ~mask) {
1505 /* data ends prematurely */
1506 sc->sc_transfer.data_rem &= mask;
1507 sc->sc_transfer.data_short = 1;
1513 /*------------------------------------------------------------------------*
1514 * ustorage_fs_check_cmd - check command routine
1516 * Check whether the command is properly formed and whether its data
1517 * size and direction agree with the values we already have.
1522 *------------------------------------------------------------------------*/
1524 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1525 uint16_t mask, uint8_t needs_medium)
1527 struct ustorage_fs_lun *currlun;
1528 uint8_t lun = (sc->sc_cbw->CBWCDB[1] >> 5);
1531 /* Verify the length of the command itself */
1532 if (min_cmd_size > sc->sc_transfer.cmd_len) {
1533 DPRINTF("%u > %u\n",
1534 min_cmd_size, sc->sc_transfer.cmd_len);
1535 sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1538 /* Mask away the LUN */
1539 sc->sc_cbw->CBWCDB[1] &= 0x1f;
1541 /* Check if LUN is correct */
1542 if (lun != sc->sc_transfer.lun) {
1546 if (sc->sc_transfer.lun <= sc->sc_last_lun) {
1547 sc->sc_transfer.currlun = currlun =
1548 sc->sc_lun + sc->sc_transfer.lun;
1549 if (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE) {
1550 currlun->sense_data = SS_NO_SENSE;
1551 currlun->sense_data_info = 0;
1552 currlun->info_valid = 0;
1555 * If a unit attention condition exists, only INQUIRY
1556 * and REQUEST SENSE commands are allowed. Anything
1559 if ((currlun->unit_attention_data != SS_NO_SENSE) &&
1560 (sc->sc_cbw->CBWCDB[0] != SC_INQUIRY) &&
1561 (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE)) {
1562 currlun->sense_data = currlun->unit_attention_data;
1563 currlun->unit_attention_data = SS_NO_SENSE;
1567 sc->sc_transfer.currlun = currlun = NULL;
1570 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1571 * to use unsupported LUNs; all others may not.
1573 if ((sc->sc_cbw->CBWCDB[0] != SC_INQUIRY) &&
1574 (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE)) {
1580 * Check that only command bytes listed in the mask are
1583 for (i = 0; i != min_cmd_size; i++) {
1584 if (sc->sc_cbw->CBWCDB[i] && !(mask & (1UL << i))) {
1586 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1593 * If the medium isn't mounted and the command needs to access
1594 * it, return an error.
1596 if (currlun && (!currlun->memory_image) && needs_medium) {
1597 currlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1603 /*------------------------------------------------------------------------*
1604 * ustorage_fs_do_cmd - do command
1609 *------------------------------------------------------------------------*/
1611 ustorage_fs_do_cmd(struct ustorage_fs_softc *sc)
1616 const uint32_t mask9 = (0xFFFFFFFFUL >> 9) << 9;
1618 /* set default data transfer pointer */
1619 sc->sc_transfer.data_ptr = sc->sc_qdata;
1621 DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1622 sc->sc_cbw->CBWCDB[0], sc->sc_transfer.data_rem);
1624 switch (sc->sc_cbw->CBWCDB[0]) {
1626 sc->sc_transfer.cmd_dir = DIR_WRITE;
1627 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1631 error = ustorage_fs_check_cmd(sc, 6,
1636 error = ustorage_fs_inquiry(sc);
1640 case SC_MODE_SELECT_6:
1641 sc->sc_transfer.cmd_dir = DIR_READ;
1642 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1646 error = ustorage_fs_check_cmd(sc, 6,
1647 (1UL << 1) | (1UL << 4) | 1, 0);
1651 error = ustorage_fs_mode_select(sc);
1655 case SC_MODE_SELECT_10:
1656 sc->sc_transfer.cmd_dir = DIR_READ;
1657 error = ustorage_fs_min_len(sc,
1658 get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1662 error = ustorage_fs_check_cmd(sc, 10,
1663 (1UL << 1) | (3UL << 7) | 1, 0);
1667 error = ustorage_fs_mode_select(sc);
1671 case SC_MODE_SENSE_6:
1672 sc->sc_transfer.cmd_dir = DIR_WRITE;
1673 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1677 error = ustorage_fs_check_cmd(sc, 6,
1678 (1UL << 1) | (1UL << 2) | (1UL << 4) | 1, 0);
1682 error = ustorage_fs_mode_sense(sc);
1686 case SC_MODE_SENSE_10:
1687 sc->sc_transfer.cmd_dir = DIR_WRITE;
1688 error = ustorage_fs_min_len(sc,
1689 get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1693 error = ustorage_fs_check_cmd(sc, 10,
1694 (1UL << 1) | (1UL << 2) | (3UL << 7) | 1, 0);
1698 error = ustorage_fs_mode_sense(sc);
1702 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1703 error = ustorage_fs_min_len(sc, 0, -1U);
1707 error = ustorage_fs_check_cmd(sc, 6,
1712 error = ustorage_fs_prevent_allow(sc);
1717 i = sc->sc_cbw->CBWCDB[4];
1718 sc->sc_transfer.cmd_dir = DIR_WRITE;
1719 temp = ((i == 0) ? 256UL : i);
1720 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1724 error = ustorage_fs_check_cmd(sc, 6,
1725 (7UL << 1) | (1UL << 4) | 1, 1);
1729 error = ustorage_fs_read(sc);
1734 sc->sc_transfer.cmd_dir = DIR_WRITE;
1735 temp = get_be16(&sc->sc_cbw->CBWCDB[7]);
1736 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1740 error = ustorage_fs_check_cmd(sc, 10,
1741 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1745 error = ustorage_fs_read(sc);
1750 sc->sc_transfer.cmd_dir = DIR_WRITE;
1751 temp = get_be32(&sc->sc_cbw->CBWCDB[6]);
1752 if (temp >= (1UL << (32 - 9))) {
1753 /* numerical overflow */
1754 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
1758 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1762 error = ustorage_fs_check_cmd(sc, 12,
1763 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1767 error = ustorage_fs_read(sc);
1771 case SC_READ_CAPACITY:
1772 sc->sc_transfer.cmd_dir = DIR_WRITE;
1773 error = ustorage_fs_check_cmd(sc, 10,
1774 (0xfUL << 2) | (1UL << 8) | 1, 1);
1778 error = ustorage_fs_read_capacity(sc);
1782 case SC_READ_FORMAT_CAPACITIES:
1783 sc->sc_transfer.cmd_dir = DIR_WRITE;
1784 error = ustorage_fs_min_len(sc,
1785 get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1789 error = ustorage_fs_check_cmd(sc, 10,
1794 error = ustorage_fs_read_format_capacities(sc);
1798 case SC_REQUEST_SENSE:
1799 sc->sc_transfer.cmd_dir = DIR_WRITE;
1800 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1804 error = ustorage_fs_check_cmd(sc, 6,
1809 error = ustorage_fs_request_sense(sc);
1813 case SC_START_STOP_UNIT:
1814 error = ustorage_fs_min_len(sc, 0, -1U);
1818 error = ustorage_fs_check_cmd(sc, 6,
1819 (1UL << 1) | (1UL << 4) | 1, 0);
1823 error = ustorage_fs_start_stop(sc);
1827 case SC_SYNCHRONIZE_CACHE:
1828 error = ustorage_fs_min_len(sc, 0, -1U);
1832 error = ustorage_fs_check_cmd(sc, 10,
1833 (0xfUL << 2) | (3UL << 7) | 1, 1);
1837 error = ustorage_fs_synchronize_cache(sc);
1841 case SC_TEST_UNIT_READY:
1842 error = ustorage_fs_min_len(sc, 0, -1U);
1846 error = ustorage_fs_check_cmd(sc, 6,
1851 * Although optional, this command is used by MS-Windows.
1852 * We support a minimal version: BytChk must be 0.
1855 error = ustorage_fs_min_len(sc, 0, -1U);
1859 error = ustorage_fs_check_cmd(sc, 10,
1860 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1864 error = ustorage_fs_verify(sc);
1869 i = sc->sc_cbw->CBWCDB[4];
1870 sc->sc_transfer.cmd_dir = DIR_READ;
1871 temp = ((i == 0) ? 256UL : i);
1872 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1876 error = ustorage_fs_check_cmd(sc, 6,
1877 (7UL << 1) | (1UL << 4) | 1, 1);
1881 error = ustorage_fs_write(sc);
1886 sc->sc_transfer.cmd_dir = DIR_READ;
1887 temp = get_be16(&sc->sc_cbw->CBWCDB[7]);
1888 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1892 error = ustorage_fs_check_cmd(sc, 10,
1893 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1897 error = ustorage_fs_write(sc);
1902 sc->sc_transfer.cmd_dir = DIR_READ;
1903 temp = get_be32(&sc->sc_cbw->CBWCDB[6]);
1904 if (temp > (mask9 >> 9)) {
1905 /* numerical overflow */
1906 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
1910 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1914 error = ustorage_fs_check_cmd(sc, 12,
1915 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1919 error = ustorage_fs_write(sc);
1924 * Some mandatory commands that we recognize but don't
1925 * implement. They don't mean much in this setting.
1926 * It's left as an exercise for anyone interested to
1927 * implement RESERVE and RELEASE in terms of Posix
1930 case SC_FORMAT_UNIT:
1933 case SC_SEND_DIAGNOSTIC:
1937 error = ustorage_fs_min_len(sc, 0, -1U);
1941 error = ustorage_fs_check_cmd(sc, sc->sc_transfer.cmd_len,
1946 sc->sc_transfer.currlun->sense_data =