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 | CTLFLAG_MPSAFE, 0,
77 SYSCTL_INT(_hw_usb_ustorage_fs, OID_AUTO, debug, CTLFLAG_RWTUN,
78 &ustorage_fs_debug, 0, "ustorage_fs debug level");
81 /* Define some limits */
83 #ifndef USTORAGE_FS_BULK_SIZE
84 #define USTORAGE_FS_BULK_SIZE (1U << 17) /* bytes */
87 #ifndef USTORAGE_FS_MAX_LUN
88 #define USTORAGE_FS_MAX_LUN 8 /* units */
91 #ifndef USTORAGE_QDATA_MAX
92 #define USTORAGE_QDATA_MAX 40 /* bytes */
96 * The SCSI ID string must be exactly 28 characters long
97 * exluding the terminating zero.
99 #ifndef USTORAGE_FS_ID_STRING
100 #define USTORAGE_FS_ID_STRING \
102 "File-Stor Gadget" /* 16 */ \
107 * The following macro defines the number of
108 * sectors to be allocated for the RAM disk:
110 #ifndef USTORAGE_FS_RAM_SECT
111 #define USTORAGE_FS_RAM_SECT (1UL << 13)
114 static uint8_t *ustorage_fs_ramdisk;
116 /* USB transfer definitions */
118 #define USTORAGE_FS_T_BBB_COMMAND 0
119 #define USTORAGE_FS_T_BBB_DATA_DUMP 1
120 #define USTORAGE_FS_T_BBB_DATA_READ 2
121 #define USTORAGE_FS_T_BBB_DATA_WRITE 3
122 #define USTORAGE_FS_T_BBB_STATUS 4
123 #define USTORAGE_FS_T_BBB_MAX 5
125 /* USB data stage direction */
131 /* USB interface specific control request */
133 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
134 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
136 /* Command Block Wrapper */
138 uDWord dCBWSignature;
139 #define CBWSIGNATURE 0x43425355
141 uDWord dCBWDataTransferLength;
143 #define CBWFLAGS_OUT 0x00
144 #define CBWFLAGS_IN 0x80
147 #define CBWCDBLENGTH 16
148 uByte CBWCDB[CBWCDBLENGTH];
149 } __packed ustorage_fs_bbb_cbw_t;
151 #define USTORAGE_FS_BBB_CBW_SIZE 31
153 /* Command Status Wrapper */
155 uDWord dCSWSignature;
156 #define CSWSIGNATURE 0x53425355
158 uDWord dCSWDataResidue;
160 #define CSWSTATUS_GOOD 0x0
161 #define CSWSTATUS_FAILED 0x1
162 #define CSWSTATUS_PHASE 0x2
163 } __packed ustorage_fs_bbb_csw_t;
165 #define USTORAGE_FS_BBB_CSW_SIZE 13
167 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 {
182 ustorage_fs_bbb_cbw_t *sc_cbw; /* Command Wrapper Block */
183 ustorage_fs_bbb_csw_t *sc_csw; /* Command Status Block */
184 void *sc_dma_ptr; /* Main data buffer */
188 struct ustorage_fs_lun sc_lun[USTORAGE_FS_MAX_LUN];
192 struct ustorage_fs_lun *currlun;
194 uint32_t data_rem; /* bytes, as reported by the command
196 uint32_t offset; /* bytes */
202 uint8_t data_short:1;
203 uint8_t data_error:1;
207 struct usb_device *sc_udev;
208 struct usb_xfer *sc_xfer[USTORAGE_FS_T_BBB_MAX];
210 uint8_t sc_iface_no; /* interface number */
212 uint8_t sc_last_xfer_index;
213 uint8_t sc_qdata[USTORAGE_QDATA_MAX];
218 static device_probe_t ustorage_fs_probe;
219 static device_attach_t ustorage_fs_attach;
220 static device_detach_t ustorage_fs_detach;
221 static device_suspend_t ustorage_fs_suspend;
222 static device_resume_t ustorage_fs_resume;
223 static usb_handle_request_t ustorage_fs_handle_request;
225 static usb_callback_t ustorage_fs_t_bbb_command_callback;
226 static usb_callback_t ustorage_fs_t_bbb_data_dump_callback;
227 static usb_callback_t ustorage_fs_t_bbb_data_read_callback;
228 static usb_callback_t ustorage_fs_t_bbb_data_write_callback;
229 static usb_callback_t ustorage_fs_t_bbb_status_callback;
231 static void ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index);
232 static void ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc);
234 static uint8_t ustorage_fs_verify(struct ustorage_fs_softc *sc);
235 static uint8_t ustorage_fs_inquiry(struct ustorage_fs_softc *sc);
236 static uint8_t ustorage_fs_request_sense(struct ustorage_fs_softc *sc);
237 static uint8_t ustorage_fs_read_capacity(struct ustorage_fs_softc *sc);
238 static uint8_t ustorage_fs_mode_sense(struct ustorage_fs_softc *sc);
239 static uint8_t ustorage_fs_start_stop(struct ustorage_fs_softc *sc);
240 static uint8_t ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc);
241 static uint8_t ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc);
242 static uint8_t ustorage_fs_mode_select(struct ustorage_fs_softc *sc);
243 static uint8_t ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask);
244 static uint8_t ustorage_fs_read(struct ustorage_fs_softc *sc);
245 static uint8_t ustorage_fs_write(struct ustorage_fs_softc *sc);
246 static uint8_t ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t cmd_size, uint16_t mask, uint8_t needs_medium);
247 static uint8_t ustorage_fs_do_cmd(struct ustorage_fs_softc *sc);
249 static device_method_t ustorage_fs_methods[] = {
251 DEVMETHOD(usb_handle_request, ustorage_fs_handle_request),
253 /* Device interface */
254 DEVMETHOD(device_probe, ustorage_fs_probe),
255 DEVMETHOD(device_attach, ustorage_fs_attach),
256 DEVMETHOD(device_detach, ustorage_fs_detach),
257 DEVMETHOD(device_suspend, ustorage_fs_suspend),
258 DEVMETHOD(device_resume, ustorage_fs_resume),
263 static driver_t ustorage_fs_driver = {
264 .name = "ustorage_fs",
265 .methods = ustorage_fs_methods,
266 .size = sizeof(struct ustorage_fs_softc),
269 static devclass_t ustorage_fs_devclass;
271 DRIVER_MODULE(ustorage_fs, uhub, ustorage_fs_driver, ustorage_fs_devclass, NULL, 0);
272 MODULE_VERSION(ustorage_fs, 0);
273 MODULE_DEPEND(ustorage_fs, usb, 1, 1, 1);
275 static struct usb_config ustorage_fs_bbb_config[USTORAGE_FS_T_BBB_MAX] = {
276 [USTORAGE_FS_T_BBB_COMMAND] = {
278 .endpoint = UE_ADDR_ANY,
279 .direction = UE_DIR_OUT,
280 .bufsize = sizeof(ustorage_fs_bbb_cbw_t),
281 .callback = &ustorage_fs_t_bbb_command_callback,
282 .usb_mode = USB_MODE_DEVICE,
285 [USTORAGE_FS_T_BBB_DATA_DUMP] = {
287 .endpoint = UE_ADDR_ANY,
288 .direction = UE_DIR_OUT,
289 .bufsize = 0, /* use wMaxPacketSize */
290 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
291 .callback = &ustorage_fs_t_bbb_data_dump_callback,
292 .usb_mode = USB_MODE_DEVICE,
295 [USTORAGE_FS_T_BBB_DATA_READ] = {
297 .endpoint = UE_ADDR_ANY,
298 .direction = UE_DIR_OUT,
299 .bufsize = USTORAGE_FS_BULK_SIZE,
300 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1},
301 .callback = &ustorage_fs_t_bbb_data_read_callback,
302 .usb_mode = USB_MODE_DEVICE,
305 [USTORAGE_FS_T_BBB_DATA_WRITE] = {
307 .endpoint = UE_ADDR_ANY,
308 .direction = UE_DIR_IN,
309 .bufsize = USTORAGE_FS_BULK_SIZE,
310 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
311 .callback = &ustorage_fs_t_bbb_data_write_callback,
312 .usb_mode = USB_MODE_DEVICE,
315 [USTORAGE_FS_T_BBB_STATUS] = {
317 .endpoint = UE_ADDR_ANY,
318 .direction = UE_DIR_IN,
319 .bufsize = sizeof(ustorage_fs_bbb_csw_t),
320 .flags = {.short_xfer_ok = 1},
321 .callback = &ustorage_fs_t_bbb_status_callback,
322 .usb_mode = USB_MODE_DEVICE,
327 * USB device probe/attach/detach
331 ustorage_fs_probe(device_t dev)
333 struct usb_attach_arg *uaa = device_get_ivars(dev);
334 struct usb_interface_descriptor *id;
336 if (uaa->usb_mode != USB_MODE_DEVICE) {
339 /* Check for a standards compliant device */
340 id = usbd_get_interface_descriptor(uaa->iface);
342 (id->bInterfaceClass != UICLASS_MASS) ||
343 (id->bInterfaceSubClass != UISUBCLASS_SCSI) ||
344 (id->bInterfaceProtocol != UIPROTO_MASS_BBB)) {
347 return (BUS_PROBE_GENERIC);
351 ustorage_fs_attach(device_t dev)
353 struct ustorage_fs_softc *sc = device_get_softc(dev);
354 struct usb_attach_arg *uaa = device_get_ivars(dev);
355 struct usb_interface_descriptor *id;
360 * NOTE: the softc struct is cleared in device_set_driver.
361 * We can safely call ustorage_fs_detach without specifically
362 * initializing the struct.
366 sc->sc_udev = uaa->device;
367 unit = device_get_unit(dev);
369 /* enable power saving mode */
370 usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE);
373 if (ustorage_fs_ramdisk == NULL) {
375 * allocate a memory image for our ramdisk until
378 ustorage_fs_ramdisk =
379 malloc(USTORAGE_FS_RAM_SECT << 9, M_USB,
382 sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
383 sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
384 sc->sc_lun[0].removable = 1;
387 device_set_usb_desc(dev);
389 mtx_init(&sc->sc_mtx, "USTORAGE_FS lock",
390 NULL, (MTX_DEF | MTX_RECURSE));
392 /* get interface index */
394 id = usbd_get_interface_descriptor(uaa->iface);
396 device_printf(dev, "failed to get "
397 "interface number\n");
400 sc->sc_iface_no = id->bInterfaceNumber;
402 err = usbd_transfer_setup(uaa->device,
403 &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
404 USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_mtx);
406 device_printf(dev, "could not setup required "
407 "transfers, %s\n", usbd_errstr(err));
411 sc->sc_cbw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
412 USTORAGE_FS_T_BBB_COMMAND], 0);
413 sc->sc_csw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
414 USTORAGE_FS_T_BBB_STATUS], 0);
415 sc->sc_dma_ptr = usbd_xfer_get_frame_buffer(sc->sc_xfer[
416 USTORAGE_FS_T_BBB_DATA_READ], 0);
418 /* start Mass Storage State Machine */
420 mtx_lock(&sc->sc_mtx);
421 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
422 mtx_unlock(&sc->sc_mtx);
424 return (0); /* success */
427 ustorage_fs_detach(dev);
428 return (ENXIO); /* failure */
432 ustorage_fs_detach(device_t dev)
434 struct ustorage_fs_softc *sc = device_get_softc(dev);
436 /* teardown our statemachine */
438 usbd_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
440 mtx_destroy(&sc->sc_mtx);
442 return (0); /* success */
446 ustorage_fs_suspend(device_t dev)
448 device_printf(dev, "suspending\n");
449 return (0); /* success */
453 ustorage_fs_resume(device_t dev)
455 device_printf(dev, "resuming\n");
456 return (0); /* success */
460 * Generic functions to handle transfers
464 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
466 if (sc->sc_xfer[xfer_index]) {
467 sc->sc_last_xfer_index = xfer_index;
468 usbd_transfer_start(sc->sc_xfer[xfer_index]);
473 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
475 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
476 mtx_unlock(&sc->sc_mtx);
477 usbd_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
478 mtx_lock(&sc->sc_mtx);
482 ustorage_fs_handle_request(device_t dev,
483 const void *preq, void **pptr, uint16_t *plen,
484 uint16_t offset, uint8_t *pstate)
486 struct ustorage_fs_softc *sc = device_get_softc(dev);
487 const struct usb_device_request *req = preq;
488 uint8_t is_complete = *pstate;
491 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
492 (req->bRequest == UR_BBB_RESET)) {
494 mtx_lock(&sc->sc_mtx);
495 ustorage_fs_transfer_stop(sc);
496 sc->sc_transfer.data_error = 1;
497 ustorage_fs_transfer_start(sc,
498 USTORAGE_FS_T_BBB_COMMAND);
499 mtx_unlock(&sc->sc_mtx);
501 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
502 (req->bRequest == UR_BBB_GET_MAX_LUN)) {
505 *pptr = &sc->sc_last_lun;
512 return (ENXIO); /* use builtin handler */
516 ustorage_fs_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
518 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
524 switch (USB_GET_STATE(xfer)) {
525 case USB_ST_TRANSFERRED:
527 tag = UGETDW(sc->sc_cbw->dCBWSignature);
529 if (tag != CBWSIGNATURE) {
531 DPRINTF("invalid signature 0x%08x\n", tag);
534 tag = UGETDW(sc->sc_cbw->dCBWTag);
537 USETDW(sc->sc_csw->dCSWTag, tag);
540 sc->sc_csw->bCSWStatus = 0;
542 /* reset data offset, data length and data remainder */
543 sc->sc_transfer.offset = 0;
544 sc->sc_transfer.data_rem =
545 UGETDW(sc->sc_cbw->dCBWDataTransferLength);
547 /* reset data flags */
548 sc->sc_transfer.data_short = 0;
551 sc->sc_transfer.lun = sc->sc_cbw->bCBWLUN;
553 if (sc->sc_transfer.data_rem == 0) {
554 sc->sc_transfer.cbw_dir = DIR_NONE;
556 if (sc->sc_cbw->bCBWFlags & CBWFLAGS_IN) {
557 sc->sc_transfer.cbw_dir = DIR_WRITE;
559 sc->sc_transfer.cbw_dir = DIR_READ;
563 sc->sc_transfer.cmd_len = sc->sc_cbw->bCDBLength;
564 if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw->CBWCDB)) ||
565 (sc->sc_transfer.cmd_len == 0)) {
566 /* just halt - this is invalid */
567 DPRINTF("invalid command length %d bytes\n",
568 sc->sc_transfer.cmd_len);
572 err = ustorage_fs_do_cmd(sc);
575 DPRINTF("command failed\n");
578 if ((sc->sc_transfer.data_rem > 0) &&
579 (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
580 /* contradicting data transfer direction */
582 DPRINTF("data direction mismatch\n");
585 switch (sc->sc_transfer.cbw_dir) {
587 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
590 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
593 ustorage_fs_transfer_start(sc,
594 USTORAGE_FS_T_BBB_STATUS);
601 if (sc->sc_transfer.data_error) {
602 sc->sc_transfer.data_error = 0;
603 usbd_xfer_set_stall(xfer);
604 DPRINTF("stall pipe\n");
606 usbd_xfer_set_frame_len(xfer, 0,
607 sizeof(ustorage_fs_bbb_cbw_t));
608 usbd_transfer_submit(xfer);
613 if (error == USB_ERR_CANCELLED) {
616 /* If the pipe is already stalled, don't do another stall */
617 if (!usbd_xfer_is_stalled(xfer))
618 sc->sc_transfer.data_error = 1;
624 if (sc->sc_csw->bCSWStatus == 0) {
625 /* set some default error code */
626 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
628 if (sc->sc_transfer.cbw_dir == DIR_READ) {
630 ustorage_fs_transfer_start(sc,
631 USTORAGE_FS_T_BBB_DATA_DUMP);
634 if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
635 /* need to stall before status */
636 sc->sc_transfer.data_error = 1;
638 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
643 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer *xfer, usb_error_t error)
645 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
646 uint32_t max_bulk = usbd_xfer_max_len(xfer);
649 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
653 switch (USB_GET_STATE(xfer)) {
654 case USB_ST_TRANSFERRED:
655 sc->sc_transfer.data_rem -= actlen;
656 sc->sc_transfer.offset += actlen;
658 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
659 /* short transfer or end of data */
660 ustorage_fs_transfer_start(sc,
661 USTORAGE_FS_T_BBB_STATUS);
668 if (max_bulk > sc->sc_transfer.data_rem) {
669 max_bulk = sc->sc_transfer.data_rem;
671 if (sc->sc_transfer.data_error) {
672 sc->sc_transfer.data_error = 0;
673 usbd_xfer_set_stall(xfer);
675 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
676 usbd_transfer_submit(xfer);
680 if (error == USB_ERR_CANCELLED) {
684 * If the pipe is already stalled, don't do another stall:
686 if (!usbd_xfer_is_stalled(xfer))
687 sc->sc_transfer.data_error = 1;
695 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
697 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
698 uint32_t max_bulk = usbd_xfer_max_len(xfer);
701 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
705 switch (USB_GET_STATE(xfer)) {
706 case USB_ST_TRANSFERRED:
707 /* XXX copy data from DMA buffer */
708 memcpy(sc->sc_transfer.data_ptr, sc->sc_dma_ptr, actlen);
710 sc->sc_transfer.data_rem -= actlen;
711 sc->sc_transfer.data_ptr += actlen;
712 sc->sc_transfer.offset += actlen;
714 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
715 /* short transfer or end of data */
716 ustorage_fs_transfer_start(sc,
717 USTORAGE_FS_T_BBB_STATUS);
724 if (max_bulk > sc->sc_transfer.data_rem) {
725 max_bulk = sc->sc_transfer.data_rem;
727 if (sc->sc_transfer.data_error) {
728 sc->sc_transfer.data_error = 0;
729 usbd_xfer_set_stall(xfer);
732 usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
733 usbd_transfer_submit(xfer);
737 if (error == USB_ERR_CANCELLED) {
740 /* If the pipe is already stalled, don't do another stall */
741 if (!usbd_xfer_is_stalled(xfer))
742 sc->sc_transfer.data_error = 1;
750 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
752 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
753 uint32_t max_bulk = usbd_xfer_max_len(xfer);
756 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
760 switch (USB_GET_STATE(xfer)) {
761 case USB_ST_TRANSFERRED:
762 sc->sc_transfer.data_rem -= actlen;
763 sc->sc_transfer.data_ptr += actlen;
764 sc->sc_transfer.offset += actlen;
766 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
767 /* short transfer or end of data */
768 ustorage_fs_transfer_start(sc,
769 USTORAGE_FS_T_BBB_STATUS);
774 if (max_bulk >= sc->sc_transfer.data_rem) {
775 max_bulk = sc->sc_transfer.data_rem;
776 if (sc->sc_transfer.data_short)
777 usbd_xfer_set_flag(xfer, USB_FORCE_SHORT_XFER);
779 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
781 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
783 if (sc->sc_transfer.data_error) {
784 sc->sc_transfer.data_error = 0;
785 usbd_xfer_set_stall(xfer);
788 /* XXX copy data to DMA buffer */
789 memcpy(sc->sc_dma_ptr, sc->sc_transfer.data_ptr, max_bulk);
791 usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
792 usbd_transfer_submit(xfer);
796 if (error == USB_ERR_CANCELLED) {
800 * If the pipe is already stalled, don't do another
803 if (!usbd_xfer_is_stalled(xfer))
804 sc->sc_transfer.data_error = 1;
812 ustorage_fs_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
814 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
818 switch (USB_GET_STATE(xfer)) {
819 case USB_ST_TRANSFERRED:
820 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
825 USETDW(sc->sc_csw->dCSWSignature, CSWSIGNATURE);
826 USETDW(sc->sc_csw->dCSWDataResidue, sc->sc_transfer.data_rem);
828 if (sc->sc_transfer.data_error) {
829 sc->sc_transfer.data_error = 0;
830 usbd_xfer_set_stall(xfer);
832 usbd_xfer_set_frame_len(xfer, 0,
833 sizeof(ustorage_fs_bbb_csw_t));
834 usbd_transfer_submit(xfer);
838 if (error == USB_ERR_CANCELLED) {
841 /* If the pipe is already stalled, don't do another stall */
842 if (!usbd_xfer_is_stalled(xfer))
843 sc->sc_transfer.data_error = 1;
850 /* SCSI commands that we recognize */
851 #define SC_FORMAT_UNIT 0x04
852 #define SC_INQUIRY 0x12
853 #define SC_MODE_SELECT_6 0x15
854 #define SC_MODE_SELECT_10 0x55
855 #define SC_MODE_SENSE_6 0x1a
856 #define SC_MODE_SENSE_10 0x5a
857 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
858 #define SC_READ_6 0x08
859 #define SC_READ_10 0x28
860 #define SC_READ_12 0xa8
861 #define SC_READ_CAPACITY 0x25
862 #define SC_READ_FORMAT_CAPACITIES 0x23
863 #define SC_RELEASE 0x17
864 #define SC_REQUEST_SENSE 0x03
865 #define SC_RESERVE 0x16
866 #define SC_SEND_DIAGNOSTIC 0x1d
867 #define SC_START_STOP_UNIT 0x1b
868 #define SC_SYNCHRONIZE_CACHE 0x35
869 #define SC_TEST_UNIT_READY 0x00
870 #define SC_VERIFY 0x2f
871 #define SC_WRITE_6 0x0a
872 #define SC_WRITE_10 0x2a
873 #define SC_WRITE_12 0xaa
875 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
876 #define SS_NO_SENSE 0
877 #define SS_COMMUNICATION_FAILURE 0x040800
878 #define SS_INVALID_COMMAND 0x052000
879 #define SS_INVALID_FIELD_IN_CDB 0x052400
880 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
881 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
882 #define SS_MEDIUM_NOT_PRESENT 0x023a00
883 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
884 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
885 #define SS_RESET_OCCURRED 0x062900
886 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
887 #define SS_UNRECOVERED_READ_ERROR 0x031100
888 #define SS_WRITE_ERROR 0x030c02
889 #define SS_WRITE_PROTECTED 0x072700
891 #define SK(x) ((uint8_t) ((x) >> 16)) /* Sense Key byte, etc. */
892 #define ASC(x) ((uint8_t) ((x) >> 8))
893 #define ASCQ(x) ((uint8_t) (x))
895 /* Routines for unaligned data access */
898 get_be16(uint8_t *buf)
900 return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
904 get_be32(uint8_t *buf)
906 return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
907 ((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
911 put_be16(uint8_t *buf, uint16_t val)
918 put_be32(uint8_t *buf, uint32_t val)
926 /*------------------------------------------------------------------------*
932 *------------------------------------------------------------------------*/
934 ustorage_fs_verify(struct ustorage_fs_softc *sc)
936 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
939 uint64_t file_offset;
940 uint64_t amount_left;
943 * Get the starting Logical Block Address
945 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
948 * We allow DPO (Disable Page Out = don't save data in the cache)
949 * but we don't implement it.
951 if ((sc->sc_cbw->CBWCDB[1] & ~0x10) != 0) {
952 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
955 vlen = get_be16(&sc->sc_cbw->CBWCDB[7]);
959 /* No default reply */
961 /* Prepare to carry out the file verify */
971 (vlen > currlun->num_sectors) ||
972 (lba >= currlun->num_sectors)) {
973 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
976 /* XXX TODO: verify that data is readable */
978 return (ustorage_fs_min_len(sc, 0, -1U));
981 /*------------------------------------------------------------------------*
982 * ustorage_fs_inquiry
987 *------------------------------------------------------------------------*/
989 ustorage_fs_inquiry(struct ustorage_fs_softc *sc)
991 uint8_t *buf = sc->sc_transfer.data_ptr;
993 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
995 if (!sc->sc_transfer.currlun) {
996 /* Unsupported LUNs are okay */
999 /* Unsupported, no device - type */
1000 return (ustorage_fs_min_len(sc, 36, -1U));
1003 /* Non - removable, direct - access device */
1004 if (currlun->removable)
1007 /* ANSI SCSI level 2 */
1009 /* SCSI - 2 INQUIRY data format */
1011 /* Additional length */
1012 /* No special options */
1013 /* Copy in ID string */
1014 memcpy(buf + 8, USTORAGE_FS_ID_STRING, 28);
1016 #if (USTORAGE_QDATA_MAX < 36)
1017 #error "(USTORAGE_QDATA_MAX < 36)"
1019 return (ustorage_fs_min_len(sc, 36, -1U));
1022 /*------------------------------------------------------------------------*
1023 * ustorage_fs_request_sense
1028 *------------------------------------------------------------------------*/
1030 ustorage_fs_request_sense(struct ustorage_fs_softc *sc)
1032 uint8_t *buf = sc->sc_transfer.data_ptr;
1033 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1039 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1041 * If a REQUEST SENSE command is received from an initiator
1042 * with a pending unit attention condition (before the target
1043 * generates the contingent allegiance condition), then the
1044 * target shall either:
1045 * a) report any pending sense data and preserve the unit
1046 * attention condition on the logical unit, or,
1047 * b) report the unit attention condition, may discard any
1048 * pending sense data, and clear the unit attention
1049 * condition on the logical unit for that initiator.
1051 * FSG normally uses option a); enable this code to use option b).
1054 if (currlun && currlun->unit_attention_data != SS_NO_SENSE) {
1055 currlun->sense_data = currlun->unit_attention_data;
1056 currlun->unit_attention_data = SS_NO_SENSE;
1061 /* Unsupported LUNs are okay */
1062 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1066 sd = currlun->sense_data;
1067 sdinfo = currlun->sense_data_info;
1068 valid = currlun->info_valid << 7;
1069 currlun->sense_data = SS_NO_SENSE;
1070 currlun->sense_data_info = 0;
1071 currlun->info_valid = 0;
1075 buf[0] = valid | 0x70;
1076 /* Valid, current error */
1078 put_be32(&buf[3], sdinfo);
1079 /* Sense information */
1081 /* Additional sense length */
1085 #if (USTORAGE_QDATA_MAX < 18)
1086 #error "(USTORAGE_QDATA_MAX < 18)"
1088 return (ustorage_fs_min_len(sc, 18, -1U));
1091 /*------------------------------------------------------------------------*
1092 * ustorage_fs_read_capacity
1097 *------------------------------------------------------------------------*/
1099 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1101 uint8_t *buf = sc->sc_transfer.data_ptr;
1102 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1103 uint32_t lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1104 uint8_t pmi = sc->sc_cbw->CBWCDB[8];
1106 /* Check the PMI and LBA fields */
1107 if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1108 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1111 /* Max logical block */
1112 put_be32(&buf[0], currlun->num_sectors - 1);
1114 put_be32(&buf[4], 512);
1116 #if (USTORAGE_QDATA_MAX < 8)
1117 #error "(USTORAGE_QDATA_MAX < 8)"
1119 return (ustorage_fs_min_len(sc, 8, -1U));
1122 /*------------------------------------------------------------------------*
1123 * ustorage_fs_mode_sense
1128 *------------------------------------------------------------------------*/
1130 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1132 uint8_t *buf = sc->sc_transfer.data_ptr;
1133 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1137 uint8_t mscmnd = sc->sc_cbw->CBWCDB[0];
1140 uint8_t changeable_values;
1145 if ((sc->sc_cbw->CBWCDB[1] & ~0x08) != 0) {
1147 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1150 pc = sc->sc_cbw->CBWCDB[2] >> 6;
1151 page_code = sc->sc_cbw->CBWCDB[2] & 0x3f;
1153 currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1156 changeable_values = (pc == 1);
1157 all_pages = (page_code == 0x3f);
1160 * Write the mode parameter header. Fixed values are: default
1161 * medium type, no cache control (DPOFUA), and no block descriptors.
1162 * The only variable value is the WriteProtect bit. We will fill in
1163 * the mode data length later.
1166 if (mscmnd == SC_MODE_SENSE_6) {
1167 buf[2] = (currlun->read_only ? 0x80 : 0x00);
1172 /* SC_MODE_SENSE_10 */
1173 buf[3] = (currlun->read_only ? 0x80 : 0x00);
1177 /* Should really be mod_data.buflen */
1180 /* No block descriptors */
1183 * The mode pages, in numerical order.
1185 if ((page_code == 0x08) || all_pages) {
1190 memset(buf + 2, 0, 10);
1191 /* None of the fields are changeable */
1193 if (!changeable_values) {
1195 /* Write cache enable, */
1196 /* Read cache not disabled */
1197 /* No cache retention priorities */
1198 put_be16(&buf[4], 0xffff);
1199 /* Don 't disable prefetch */
1200 /* Minimum prefetch = 0 */
1201 put_be16(&buf[8], 0xffff);
1202 /* Maximum prefetch */
1203 put_be16(&buf[10], 0xffff);
1204 /* Maximum prefetch ceiling */
1209 * Check that a valid page was requested and the mode data length
1214 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1217 /* Store the mode data length */
1218 if (mscmnd == SC_MODE_SENSE_6)
1221 put_be16(buf0, len - 2);
1223 #if (USTORAGE_QDATA_MAX < 24)
1224 #error "(USTORAGE_QDATA_MAX < 24)"
1226 return (ustorage_fs_min_len(sc, len, -1U));
1229 /*------------------------------------------------------------------------*
1230 * ustorage_fs_start_stop
1235 *------------------------------------------------------------------------*/
1237 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1239 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1244 if (!currlun->removable) {
1245 currlun->sense_data = SS_INVALID_COMMAND;
1248 immed = sc->sc_cbw->CBWCDB[1] & 0x01;
1249 loej = sc->sc_cbw->CBWCDB[4] & 0x02;
1250 start = sc->sc_cbw->CBWCDB[4] & 0x01;
1252 if (immed || loej || start) {
1258 /*------------------------------------------------------------------------*
1259 * ustorage_fs_prevent_allow
1264 *------------------------------------------------------------------------*/
1266 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1268 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1271 if (!currlun->removable) {
1272 currlun->sense_data = SS_INVALID_COMMAND;
1275 prevent = sc->sc_cbw->CBWCDB[4] & 0x01;
1276 if ((sc->sc_cbw->CBWCDB[4] & ~0x01) != 0) {
1277 /* Mask away Prevent */
1278 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1281 if (currlun->prevent_medium_removal && !prevent) {
1282 //fsync_sub(currlun);
1284 currlun->prevent_medium_removal = prevent;
1288 /*------------------------------------------------------------------------*
1289 * ustorage_fs_read_format_capacities
1294 *------------------------------------------------------------------------*/
1296 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1298 uint8_t *buf = sc->sc_transfer.data_ptr;
1299 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1301 buf[0] = buf[1] = buf[2] = 0;
1303 /* Only the Current / Maximum Capacity Descriptor */
1306 /* Number of blocks */
1307 put_be32(&buf[0], currlun->num_sectors);
1309 put_be32(&buf[4], 512);
1310 /* Current capacity */
1313 #if (USTORAGE_QDATA_MAX < 12)
1314 #error "(USTORAGE_QDATA_MAX < 12)"
1316 return (ustorage_fs_min_len(sc, 12, -1U));
1319 /*------------------------------------------------------------------------*
1320 * ustorage_fs_mode_select
1325 *------------------------------------------------------------------------*/
1327 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1329 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1331 /* We don't support MODE SELECT */
1332 currlun->sense_data = SS_INVALID_COMMAND;
1336 /*------------------------------------------------------------------------*
1337 * ustorage_fs_synchronize_cache
1342 *------------------------------------------------------------------------*/
1344 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1347 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1351 * We ignore the requested LBA and write out all dirty data buffers.
1355 currlun->sense_data = SS_WRITE_ERROR;
1361 /*------------------------------------------------------------------------*
1362 * ustorage_fs_read - read data from disk
1367 *------------------------------------------------------------------------*/
1369 ustorage_fs_read(struct ustorage_fs_softc *sc)
1371 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1372 uint64_t file_offset;
1377 * Get the starting Logical Block Address and check that it's not
1380 if (sc->sc_cbw->CBWCDB[0] == SC_READ_6) {
1381 lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1382 get_be16(&sc->sc_cbw->CBWCDB[2]);
1384 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1387 * We allow DPO (Disable Page Out = don't save data in the
1388 * cache) and FUA (Force Unit Access = don't read from the
1389 * cache), but we don't implement them.
1391 if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1392 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1396 len = sc->sc_transfer.data_rem >> 9;
1400 (len > currlun->num_sectors) ||
1401 (lba >= currlun->num_sectors)) {
1402 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1408 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1413 /*------------------------------------------------------------------------*
1414 * ustorage_fs_write - write data to disk
1419 *------------------------------------------------------------------------*/
1421 ustorage_fs_write(struct ustorage_fs_softc *sc)
1423 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1424 uint64_t file_offset;
1428 if (currlun->read_only) {
1429 currlun->sense_data = SS_WRITE_PROTECTED;
1432 /* XXX clear SYNC */
1435 * Get the starting Logical Block Address and check that it's not
1438 if (sc->sc_cbw->CBWCDB[0] == SC_WRITE_6)
1439 lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1440 get_be16(&sc->sc_cbw->CBWCDB[2]);
1442 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1445 * We allow DPO (Disable Page Out = don't save data in the
1446 * cache) and FUA (Force Unit Access = write directly to the
1447 * medium). We don't implement DPO; we implement FUA by
1448 * performing synchronous output.
1450 if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1451 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1454 if (sc->sc_cbw->CBWCDB[1] & 0x08) {
1456 /* XXX set SYNC flag here */
1460 len = sc->sc_transfer.data_rem >> 9;
1464 (len > currlun->num_sectors) ||
1465 (lba >= currlun->num_sectors)) {
1466 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1472 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1477 /*------------------------------------------------------------------------*
1478 * ustorage_fs_min_len
1483 *------------------------------------------------------------------------*/
1485 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1487 if (len != sc->sc_transfer.data_rem) {
1488 if (sc->sc_transfer.cbw_dir == DIR_READ) {
1490 * there must be something wrong about this SCSI
1493 sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1496 /* compute the minimum length */
1498 if (sc->sc_transfer.data_rem > len) {
1499 /* data ends prematurely */
1500 sc->sc_transfer.data_rem = len;
1501 sc->sc_transfer.data_short = 1;
1503 /* check length alignment */
1505 if (sc->sc_transfer.data_rem & ~mask) {
1506 /* data ends prematurely */
1507 sc->sc_transfer.data_rem &= mask;
1508 sc->sc_transfer.data_short = 1;
1514 /*------------------------------------------------------------------------*
1515 * ustorage_fs_check_cmd - check command routine
1517 * Check whether the command is properly formed and whether its data
1518 * size and direction agree with the values we already have.
1523 *------------------------------------------------------------------------*/
1525 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1526 uint16_t mask, uint8_t needs_medium)
1528 struct ustorage_fs_lun *currlun;
1529 uint8_t lun = (sc->sc_cbw->CBWCDB[1] >> 5);
1532 /* Verify the length of the command itself */
1533 if (min_cmd_size > sc->sc_transfer.cmd_len) {
1534 DPRINTF("%u > %u\n",
1535 min_cmd_size, sc->sc_transfer.cmd_len);
1536 sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1539 /* Mask away the LUN */
1540 sc->sc_cbw->CBWCDB[1] &= 0x1f;
1542 /* Check if LUN is correct */
1543 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 =