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 #ifdef USB_GLOBAL_INCLUDE_FILE
40 #include USB_GLOBAL_INCLUDE_FILE
42 #include <sys/stdint.h>
43 #include <sys/stddef.h>
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
66 #define USB_DEBUG_VAR ustorage_fs_debug
67 #include <dev/usb/usb_debug.h>
68 #endif /* USB_GLOBAL_INCLUDE_FILE */
71 static int ustorage_fs_debug = 0;
73 SYSCTL_NODE(_hw_usb, OID_AUTO, ustorage_fs, CTLFLAG_RW, 0, "USB ustorage_fs");
74 SYSCTL_INT(_hw_usb_ustorage_fs, OID_AUTO, debug, CTLFLAG_RW,
75 &ustorage_fs_debug, 0, "ustorage_fs debug level");
78 /* Define some limits */
80 #ifndef USTORAGE_FS_BULK_SIZE
81 #define USTORAGE_FS_BULK_SIZE (1U << 17) /* bytes */
84 #ifndef USTORAGE_FS_MAX_LUN
85 #define USTORAGE_FS_MAX_LUN 8 /* units */
88 #ifndef USTORAGE_QDATA_MAX
89 #define USTORAGE_QDATA_MAX 40 /* bytes */
93 * The SCSI ID string must be exactly 28 characters long
94 * exluding the terminating zero.
96 #ifndef USTORAGE_FS_ID_STRING
97 #define USTORAGE_FS_ID_STRING \
99 "File-Stor Gadget" /* 16 */ \
104 * The following macro defines the number of
105 * sectors to be allocated for the RAM disk:
107 #ifndef USTORAGE_FS_RAM_SECT
108 #define USTORAGE_FS_RAM_SECT (1UL << 13)
111 static uint8_t *ustorage_fs_ramdisk;
113 /* USB transfer definitions */
115 #define USTORAGE_FS_T_BBB_COMMAND 0
116 #define USTORAGE_FS_T_BBB_DATA_DUMP 1
117 #define USTORAGE_FS_T_BBB_DATA_READ 2
118 #define USTORAGE_FS_T_BBB_DATA_WRITE 3
119 #define USTORAGE_FS_T_BBB_STATUS 4
120 #define USTORAGE_FS_T_BBB_MAX 5
122 /* USB data stage direction */
128 /* USB interface specific control request */
130 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
131 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
133 /* Command Block Wrapper */
135 uDWord dCBWSignature;
136 #define CBWSIGNATURE 0x43425355
138 uDWord dCBWDataTransferLength;
140 #define CBWFLAGS_OUT 0x00
141 #define CBWFLAGS_IN 0x80
144 #define CBWCDBLENGTH 16
145 uByte CBWCDB[CBWCDBLENGTH];
146 } __packed ustorage_fs_bbb_cbw_t;
148 #define USTORAGE_FS_BBB_CBW_SIZE 31
150 /* Command Status Wrapper */
152 uDWord dCSWSignature;
153 #define CSWSIGNATURE 0x53425355
155 uDWord dCSWDataResidue;
157 #define CSWSTATUS_GOOD 0x0
158 #define CSWSTATUS_FAILED 0x1
159 #define CSWSTATUS_PHASE 0x2
160 } __packed ustorage_fs_bbb_csw_t;
162 #define USTORAGE_FS_BBB_CSW_SIZE 13
164 struct ustorage_fs_lun {
166 uint8_t *memory_image;
168 uint32_t num_sectors;
170 uint32_t sense_data_info;
171 uint32_t unit_attention_data;
174 uint8_t prevent_medium_removal:1;
175 uint8_t info_valid:1;
179 struct ustorage_fs_softc {
181 ustorage_fs_bbb_cbw_t *sc_cbw; /* Command Wrapper Block */
182 ustorage_fs_bbb_csw_t *sc_csw; /* Command Status Block */
183 void *sc_dma_ptr; /* Main data buffer */
187 struct ustorage_fs_lun sc_lun[USTORAGE_FS_MAX_LUN];
191 struct ustorage_fs_lun *currlun;
193 uint32_t data_rem; /* bytes, as reported by the command
195 uint32_t offset; /* bytes */
201 uint8_t data_short:1;
202 uint8_t data_error:1;
206 struct usb_device *sc_udev;
207 struct usb_xfer *sc_xfer[USTORAGE_FS_T_BBB_MAX];
209 uint8_t sc_iface_no; /* interface number */
211 uint8_t sc_last_xfer_index;
212 uint8_t sc_qdata[USTORAGE_QDATA_MAX];
217 static device_probe_t ustorage_fs_probe;
218 static device_attach_t ustorage_fs_attach;
219 static device_detach_t ustorage_fs_detach;
220 static device_suspend_t ustorage_fs_suspend;
221 static device_resume_t ustorage_fs_resume;
222 static usb_handle_request_t ustorage_fs_handle_request;
224 static usb_callback_t ustorage_fs_t_bbb_command_callback;
225 static usb_callback_t ustorage_fs_t_bbb_data_dump_callback;
226 static usb_callback_t ustorage_fs_t_bbb_data_read_callback;
227 static usb_callback_t ustorage_fs_t_bbb_data_write_callback;
228 static usb_callback_t ustorage_fs_t_bbb_status_callback;
230 static void ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index);
231 static void ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc);
233 static uint8_t ustorage_fs_verify(struct ustorage_fs_softc *sc);
234 static uint8_t ustorage_fs_inquiry(struct ustorage_fs_softc *sc);
235 static uint8_t ustorage_fs_request_sense(struct ustorage_fs_softc *sc);
236 static uint8_t ustorage_fs_read_capacity(struct ustorage_fs_softc *sc);
237 static uint8_t ustorage_fs_mode_sense(struct ustorage_fs_softc *sc);
238 static uint8_t ustorage_fs_start_stop(struct ustorage_fs_softc *sc);
239 static uint8_t ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc);
240 static uint8_t ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc);
241 static uint8_t ustorage_fs_mode_select(struct ustorage_fs_softc *sc);
242 static uint8_t ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask);
243 static uint8_t ustorage_fs_read(struct ustorage_fs_softc *sc);
244 static uint8_t ustorage_fs_write(struct ustorage_fs_softc *sc);
245 static uint8_t ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t cmd_size, uint16_t mask, uint8_t needs_medium);
246 static uint8_t ustorage_fs_do_cmd(struct ustorage_fs_softc *sc);
248 static device_method_t ustorage_fs_methods[] = {
250 DEVMETHOD(usb_handle_request, ustorage_fs_handle_request),
252 /* Device interface */
253 DEVMETHOD(device_probe, ustorage_fs_probe),
254 DEVMETHOD(device_attach, ustorage_fs_attach),
255 DEVMETHOD(device_detach, ustorage_fs_detach),
256 DEVMETHOD(device_suspend, ustorage_fs_suspend),
257 DEVMETHOD(device_resume, ustorage_fs_resume),
262 static driver_t ustorage_fs_driver = {
263 .name = "ustorage_fs",
264 .methods = ustorage_fs_methods,
265 .size = sizeof(struct ustorage_fs_softc),
268 static devclass_t ustorage_fs_devclass;
270 DRIVER_MODULE(ustorage_fs, uhub, ustorage_fs_driver, ustorage_fs_devclass, NULL, 0);
271 MODULE_VERSION(ustorage_fs, 0);
272 MODULE_DEPEND(ustorage_fs, usb, 1, 1, 1);
274 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 if (ustorage_fs_ramdisk == NULL) {
386 sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
387 sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
388 sc->sc_lun[0].removable = 1;
391 device_set_usb_desc(dev);
393 mtx_init(&sc->sc_mtx, "USTORAGE_FS lock",
394 NULL, (MTX_DEF | MTX_RECURSE));
396 /* get interface index */
398 id = usbd_get_interface_descriptor(uaa->iface);
400 device_printf(dev, "failed to get "
401 "interface number\n");
404 sc->sc_iface_no = id->bInterfaceNumber;
406 err = usbd_transfer_setup(uaa->device,
407 &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
408 USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_mtx);
410 device_printf(dev, "could not setup required "
411 "transfers, %s\n", usbd_errstr(err));
415 sc->sc_cbw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
416 USTORAGE_FS_T_BBB_COMMAND], 0);
417 sc->sc_csw = usbd_xfer_get_frame_buffer(sc->sc_xfer[
418 USTORAGE_FS_T_BBB_STATUS], 0);
419 sc->sc_dma_ptr = usbd_xfer_get_frame_buffer(sc->sc_xfer[
420 USTORAGE_FS_T_BBB_DATA_READ], 0);
422 /* start Mass Storage State Machine */
424 mtx_lock(&sc->sc_mtx);
425 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
426 mtx_unlock(&sc->sc_mtx);
428 return (0); /* success */
431 ustorage_fs_detach(dev);
432 return (ENXIO); /* failure */
436 ustorage_fs_detach(device_t dev)
438 struct ustorage_fs_softc *sc = device_get_softc(dev);
440 /* teardown our statemachine */
442 usbd_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
444 mtx_destroy(&sc->sc_mtx);
446 return (0); /* success */
450 ustorage_fs_suspend(device_t dev)
452 device_printf(dev, "suspending\n");
453 return (0); /* success */
457 ustorage_fs_resume(device_t dev)
459 device_printf(dev, "resuming\n");
460 return (0); /* success */
464 * Generic functions to handle transfers
468 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
470 if (sc->sc_xfer[xfer_index]) {
471 sc->sc_last_xfer_index = xfer_index;
472 usbd_transfer_start(sc->sc_xfer[xfer_index]);
477 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
479 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
480 mtx_unlock(&sc->sc_mtx);
481 usbd_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
482 mtx_lock(&sc->sc_mtx);
486 ustorage_fs_handle_request(device_t dev,
487 const void *preq, void **pptr, uint16_t *plen,
488 uint16_t offset, uint8_t *pstate)
490 struct ustorage_fs_softc *sc = device_get_softc(dev);
491 const struct usb_device_request *req = preq;
492 uint8_t is_complete = *pstate;
495 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
496 (req->bRequest == UR_BBB_RESET)) {
498 mtx_lock(&sc->sc_mtx);
499 ustorage_fs_transfer_stop(sc);
500 sc->sc_transfer.data_error = 1;
501 ustorage_fs_transfer_start(sc,
502 USTORAGE_FS_T_BBB_COMMAND);
503 mtx_unlock(&sc->sc_mtx);
505 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
506 (req->bRequest == UR_BBB_GET_MAX_LUN)) {
509 *pptr = &sc->sc_last_lun;
516 return (ENXIO); /* use builtin handler */
520 ustorage_fs_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
522 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
528 switch (USB_GET_STATE(xfer)) {
529 case USB_ST_TRANSFERRED:
531 tag = UGETDW(sc->sc_cbw->dCBWSignature);
533 if (tag != CBWSIGNATURE) {
535 DPRINTF("invalid signature 0x%08x\n", tag);
538 tag = UGETDW(sc->sc_cbw->dCBWTag);
541 USETDW(sc->sc_csw->dCSWTag, tag);
544 sc->sc_csw->bCSWStatus = 0;
546 /* reset data offset, data length and data remainder */
547 sc->sc_transfer.offset = 0;
548 sc->sc_transfer.data_rem =
549 UGETDW(sc->sc_cbw->dCBWDataTransferLength);
551 /* reset data flags */
552 sc->sc_transfer.data_short = 0;
555 sc->sc_transfer.lun = sc->sc_cbw->bCBWLUN;
557 if (sc->sc_transfer.data_rem == 0) {
558 sc->sc_transfer.cbw_dir = DIR_NONE;
560 if (sc->sc_cbw->bCBWFlags & CBWFLAGS_IN) {
561 sc->sc_transfer.cbw_dir = DIR_WRITE;
563 sc->sc_transfer.cbw_dir = DIR_READ;
567 sc->sc_transfer.cmd_len = sc->sc_cbw->bCDBLength;
568 if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw->CBWCDB)) ||
569 (sc->sc_transfer.cmd_len == 0)) {
570 /* just halt - this is invalid */
571 DPRINTF("invalid command length %d bytes\n",
572 sc->sc_transfer.cmd_len);
576 err = ustorage_fs_do_cmd(sc);
579 DPRINTF("command failed\n");
582 if ((sc->sc_transfer.data_rem > 0) &&
583 (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
584 /* contradicting data transfer direction */
586 DPRINTF("data direction mismatch\n");
589 switch (sc->sc_transfer.cbw_dir) {
591 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
594 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
597 ustorage_fs_transfer_start(sc,
598 USTORAGE_FS_T_BBB_STATUS);
605 if (sc->sc_transfer.data_error) {
606 sc->sc_transfer.data_error = 0;
607 usbd_xfer_set_stall(xfer);
608 DPRINTF("stall pipe\n");
610 usbd_xfer_set_frame_len(xfer, 0,
611 sizeof(ustorage_fs_bbb_cbw_t));
612 usbd_transfer_submit(xfer);
617 if (error == USB_ERR_CANCELLED) {
620 /* If the pipe is already stalled, don't do another stall */
621 if (!usbd_xfer_is_stalled(xfer))
622 sc->sc_transfer.data_error = 1;
628 if (sc->sc_csw->bCSWStatus == 0) {
629 /* set some default error code */
630 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
632 if (sc->sc_transfer.cbw_dir == DIR_READ) {
634 ustorage_fs_transfer_start(sc,
635 USTORAGE_FS_T_BBB_DATA_DUMP);
638 if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
639 /* need to stall before status */
640 sc->sc_transfer.data_error = 1;
642 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
647 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer *xfer, usb_error_t error)
649 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
650 uint32_t max_bulk = usbd_xfer_max_len(xfer);
653 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
657 switch (USB_GET_STATE(xfer)) {
658 case USB_ST_TRANSFERRED:
659 sc->sc_transfer.data_rem -= actlen;
660 sc->sc_transfer.offset += actlen;
662 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
663 /* short transfer or end of data */
664 ustorage_fs_transfer_start(sc,
665 USTORAGE_FS_T_BBB_STATUS);
672 if (max_bulk > sc->sc_transfer.data_rem) {
673 max_bulk = sc->sc_transfer.data_rem;
675 if (sc->sc_transfer.data_error) {
676 sc->sc_transfer.data_error = 0;
677 usbd_xfer_set_stall(xfer);
679 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
680 usbd_transfer_submit(xfer);
684 if (error == USB_ERR_CANCELLED) {
688 * If the pipe is already stalled, don't do another stall:
690 if (!usbd_xfer_is_stalled(xfer))
691 sc->sc_transfer.data_error = 1;
699 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
701 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
702 uint32_t max_bulk = usbd_xfer_max_len(xfer);
705 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
709 switch (USB_GET_STATE(xfer)) {
710 case USB_ST_TRANSFERRED:
711 /* XXX copy data from DMA buffer */
712 memcpy(sc->sc_transfer.data_ptr, sc->sc_dma_ptr, actlen);
714 sc->sc_transfer.data_rem -= actlen;
715 sc->sc_transfer.data_ptr += actlen;
716 sc->sc_transfer.offset += actlen;
718 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
719 /* short transfer or end of data */
720 ustorage_fs_transfer_start(sc,
721 USTORAGE_FS_T_BBB_STATUS);
728 if (max_bulk > sc->sc_transfer.data_rem) {
729 max_bulk = sc->sc_transfer.data_rem;
731 if (sc->sc_transfer.data_error) {
732 sc->sc_transfer.data_error = 0;
733 usbd_xfer_set_stall(xfer);
736 usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
737 usbd_transfer_submit(xfer);
741 if (error == USB_ERR_CANCELLED) {
744 /* If the pipe is already stalled, don't do another stall */
745 if (!usbd_xfer_is_stalled(xfer))
746 sc->sc_transfer.data_error = 1;
754 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
756 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
757 uint32_t max_bulk = usbd_xfer_max_len(xfer);
760 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
764 switch (USB_GET_STATE(xfer)) {
765 case USB_ST_TRANSFERRED:
766 sc->sc_transfer.data_rem -= actlen;
767 sc->sc_transfer.data_ptr += actlen;
768 sc->sc_transfer.offset += actlen;
770 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
771 /* short transfer or end of data */
772 ustorage_fs_transfer_start(sc,
773 USTORAGE_FS_T_BBB_STATUS);
778 if (max_bulk >= sc->sc_transfer.data_rem) {
779 max_bulk = sc->sc_transfer.data_rem;
780 if (sc->sc_transfer.data_short)
781 usbd_xfer_set_flag(xfer, USB_FORCE_SHORT_XFER);
783 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
785 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
787 if (sc->sc_transfer.data_error) {
788 sc->sc_transfer.data_error = 0;
789 usbd_xfer_set_stall(xfer);
792 /* XXX copy data to DMA buffer */
793 memcpy(sc->sc_dma_ptr, sc->sc_transfer.data_ptr, max_bulk);
795 usbd_xfer_set_frame_data(xfer, 0, sc->sc_dma_ptr, max_bulk);
796 usbd_transfer_submit(xfer);
800 if (error == USB_ERR_CANCELLED) {
804 * If the pipe is already stalled, don't do another
807 if (!usbd_xfer_is_stalled(xfer))
808 sc->sc_transfer.data_error = 1;
816 ustorage_fs_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
818 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
822 switch (USB_GET_STATE(xfer)) {
823 case USB_ST_TRANSFERRED:
824 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
829 USETDW(sc->sc_csw->dCSWSignature, CSWSIGNATURE);
830 USETDW(sc->sc_csw->dCSWDataResidue, sc->sc_transfer.data_rem);
832 if (sc->sc_transfer.data_error) {
833 sc->sc_transfer.data_error = 0;
834 usbd_xfer_set_stall(xfer);
836 usbd_xfer_set_frame_len(xfer, 0,
837 sizeof(ustorage_fs_bbb_csw_t));
838 usbd_transfer_submit(xfer);
842 if (error == USB_ERR_CANCELLED) {
845 /* If the pipe is already stalled, don't do another stall */
846 if (!usbd_xfer_is_stalled(xfer))
847 sc->sc_transfer.data_error = 1;
854 /* SCSI commands that we recognize */
855 #define SC_FORMAT_UNIT 0x04
856 #define SC_INQUIRY 0x12
857 #define SC_MODE_SELECT_6 0x15
858 #define SC_MODE_SELECT_10 0x55
859 #define SC_MODE_SENSE_6 0x1a
860 #define SC_MODE_SENSE_10 0x5a
861 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
862 #define SC_READ_6 0x08
863 #define SC_READ_10 0x28
864 #define SC_READ_12 0xa8
865 #define SC_READ_CAPACITY 0x25
866 #define SC_READ_FORMAT_CAPACITIES 0x23
867 #define SC_RELEASE 0x17
868 #define SC_REQUEST_SENSE 0x03
869 #define SC_RESERVE 0x16
870 #define SC_SEND_DIAGNOSTIC 0x1d
871 #define SC_START_STOP_UNIT 0x1b
872 #define SC_SYNCHRONIZE_CACHE 0x35
873 #define SC_TEST_UNIT_READY 0x00
874 #define SC_VERIFY 0x2f
875 #define SC_WRITE_6 0x0a
876 #define SC_WRITE_10 0x2a
877 #define SC_WRITE_12 0xaa
879 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
880 #define SS_NO_SENSE 0
881 #define SS_COMMUNICATION_FAILURE 0x040800
882 #define SS_INVALID_COMMAND 0x052000
883 #define SS_INVALID_FIELD_IN_CDB 0x052400
884 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
885 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
886 #define SS_MEDIUM_NOT_PRESENT 0x023a00
887 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
888 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
889 #define SS_RESET_OCCURRED 0x062900
890 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
891 #define SS_UNRECOVERED_READ_ERROR 0x031100
892 #define SS_WRITE_ERROR 0x030c02
893 #define SS_WRITE_PROTECTED 0x072700
895 #define SK(x) ((uint8_t) ((x) >> 16)) /* Sense Key byte, etc. */
896 #define ASC(x) ((uint8_t) ((x) >> 8))
897 #define ASCQ(x) ((uint8_t) (x))
899 /* Routines for unaligned data access */
902 get_be16(uint8_t *buf)
904 return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
908 get_be32(uint8_t *buf)
910 return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
911 ((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
915 put_be16(uint8_t *buf, uint16_t val)
922 put_be32(uint8_t *buf, uint32_t val)
930 /*------------------------------------------------------------------------*
936 *------------------------------------------------------------------------*/
938 ustorage_fs_verify(struct ustorage_fs_softc *sc)
940 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
943 uint64_t file_offset;
944 uint64_t amount_left;
947 * Get the starting Logical Block Address
949 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
952 * We allow DPO (Disable Page Out = don't save data in the cache)
953 * but we don't implement it.
955 if ((sc->sc_cbw->CBWCDB[1] & ~0x10) != 0) {
956 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
959 vlen = get_be16(&sc->sc_cbw->CBWCDB[7]);
963 /* No default reply */
965 /* Prepare to carry out the file verify */
975 (vlen > currlun->num_sectors) ||
976 (lba >= currlun->num_sectors)) {
977 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
980 /* XXX TODO: verify that data is readable */
982 return (ustorage_fs_min_len(sc, 0, -1U));
985 /*------------------------------------------------------------------------*
986 * ustorage_fs_inquiry
991 *------------------------------------------------------------------------*/
993 ustorage_fs_inquiry(struct ustorage_fs_softc *sc)
995 uint8_t *buf = sc->sc_transfer.data_ptr;
997 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
999 if (!sc->sc_transfer.currlun) {
1000 /* Unsupported LUNs are okay */
1003 /* Unsupported, no device - type */
1004 return (ustorage_fs_min_len(sc, 36, -1U));
1007 /* Non - removable, direct - access device */
1008 if (currlun->removable)
1011 /* ANSI SCSI level 2 */
1013 /* SCSI - 2 INQUIRY data format */
1015 /* Additional length */
1016 /* No special options */
1017 /* Copy in ID string */
1018 memcpy(buf + 8, USTORAGE_FS_ID_STRING, 28);
1020 #if (USTORAGE_QDATA_MAX < 36)
1021 #error "(USTORAGE_QDATA_MAX < 36)"
1023 return (ustorage_fs_min_len(sc, 36, -1U));
1026 /*------------------------------------------------------------------------*
1027 * ustorage_fs_request_sense
1032 *------------------------------------------------------------------------*/
1034 ustorage_fs_request_sense(struct ustorage_fs_softc *sc)
1036 uint8_t *buf = sc->sc_transfer.data_ptr;
1037 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1043 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1045 * If a REQUEST SENSE command is received from an initiator
1046 * with a pending unit attention condition (before the target
1047 * generates the contingent allegiance condition), then the
1048 * target shall either:
1049 * a) report any pending sense data and preserve the unit
1050 * attention condition on the logical unit, or,
1051 * b) report the unit attention condition, may discard any
1052 * pending sense data, and clear the unit attention
1053 * condition on the logical unit for that initiator.
1055 * FSG normally uses option a); enable this code to use option b).
1058 if (currlun && currlun->unit_attention_data != SS_NO_SENSE) {
1059 currlun->sense_data = currlun->unit_attention_data;
1060 currlun->unit_attention_data = SS_NO_SENSE;
1065 /* Unsupported LUNs are okay */
1066 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1070 sd = currlun->sense_data;
1071 sdinfo = currlun->sense_data_info;
1072 valid = currlun->info_valid << 7;
1073 currlun->sense_data = SS_NO_SENSE;
1074 currlun->sense_data_info = 0;
1075 currlun->info_valid = 0;
1079 buf[0] = valid | 0x70;
1080 /* Valid, current error */
1082 put_be32(&buf[3], sdinfo);
1083 /* Sense information */
1085 /* Additional sense length */
1089 #if (USTORAGE_QDATA_MAX < 18)
1090 #error "(USTORAGE_QDATA_MAX < 18)"
1092 return (ustorage_fs_min_len(sc, 18, -1U));
1095 /*------------------------------------------------------------------------*
1096 * ustorage_fs_read_capacity
1101 *------------------------------------------------------------------------*/
1103 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1105 uint8_t *buf = sc->sc_transfer.data_ptr;
1106 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1107 uint32_t lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1108 uint8_t pmi = sc->sc_cbw->CBWCDB[8];
1110 /* Check the PMI and LBA fields */
1111 if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1112 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1115 /* Max logical block */
1116 put_be32(&buf[0], currlun->num_sectors - 1);
1118 put_be32(&buf[4], 512);
1120 #if (USTORAGE_QDATA_MAX < 8)
1121 #error "(USTORAGE_QDATA_MAX < 8)"
1123 return (ustorage_fs_min_len(sc, 8, -1U));
1126 /*------------------------------------------------------------------------*
1127 * ustorage_fs_mode_sense
1132 *------------------------------------------------------------------------*/
1134 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1136 uint8_t *buf = sc->sc_transfer.data_ptr;
1137 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1141 uint8_t mscmnd = sc->sc_cbw->CBWCDB[0];
1144 uint8_t changeable_values;
1149 if ((sc->sc_cbw->CBWCDB[1] & ~0x08) != 0) {
1151 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1154 pc = sc->sc_cbw->CBWCDB[2] >> 6;
1155 page_code = sc->sc_cbw->CBWCDB[2] & 0x3f;
1157 currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1160 changeable_values = (pc == 1);
1161 all_pages = (page_code == 0x3f);
1164 * Write the mode parameter header. Fixed values are: default
1165 * medium type, no cache control (DPOFUA), and no block descriptors.
1166 * The only variable value is the WriteProtect bit. We will fill in
1167 * the mode data length later.
1170 if (mscmnd == SC_MODE_SENSE_6) {
1171 buf[2] = (currlun->read_only ? 0x80 : 0x00);
1176 /* SC_MODE_SENSE_10 */
1177 buf[3] = (currlun->read_only ? 0x80 : 0x00);
1181 /* Should really be mod_data.buflen */
1184 /* No block descriptors */
1187 * The mode pages, in numerical order.
1189 if ((page_code == 0x08) || all_pages) {
1194 memset(buf + 2, 0, 10);
1195 /* None of the fields are changeable */
1197 if (!changeable_values) {
1199 /* Write cache enable, */
1200 /* Read cache not disabled */
1201 /* No cache retention priorities */
1202 put_be16(&buf[4], 0xffff);
1203 /* Don 't disable prefetch */
1204 /* Minimum prefetch = 0 */
1205 put_be16(&buf[8], 0xffff);
1206 /* Maximum prefetch */
1207 put_be16(&buf[10], 0xffff);
1208 /* Maximum prefetch ceiling */
1213 * Check that a valid page was requested and the mode data length
1218 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1221 /* Store the mode data length */
1222 if (mscmnd == SC_MODE_SENSE_6)
1225 put_be16(buf0, len - 2);
1227 #if (USTORAGE_QDATA_MAX < 24)
1228 #error "(USTORAGE_QDATA_MAX < 24)"
1230 return (ustorage_fs_min_len(sc, len, -1U));
1233 /*------------------------------------------------------------------------*
1234 * ustorage_fs_start_stop
1239 *------------------------------------------------------------------------*/
1241 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1243 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1248 if (!currlun->removable) {
1249 currlun->sense_data = SS_INVALID_COMMAND;
1252 immed = sc->sc_cbw->CBWCDB[1] & 0x01;
1253 loej = sc->sc_cbw->CBWCDB[4] & 0x02;
1254 start = sc->sc_cbw->CBWCDB[4] & 0x01;
1256 if (immed || loej || start) {
1262 /*------------------------------------------------------------------------*
1263 * ustorage_fs_prevent_allow
1268 *------------------------------------------------------------------------*/
1270 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1272 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1275 if (!currlun->removable) {
1276 currlun->sense_data = SS_INVALID_COMMAND;
1279 prevent = sc->sc_cbw->CBWCDB[4] & 0x01;
1280 if ((sc->sc_cbw->CBWCDB[4] & ~0x01) != 0) {
1281 /* Mask away Prevent */
1282 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1285 if (currlun->prevent_medium_removal && !prevent) {
1286 //fsync_sub(currlun);
1288 currlun->prevent_medium_removal = prevent;
1292 /*------------------------------------------------------------------------*
1293 * ustorage_fs_read_format_capacities
1298 *------------------------------------------------------------------------*/
1300 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1302 uint8_t *buf = sc->sc_transfer.data_ptr;
1303 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1305 buf[0] = buf[1] = buf[2] = 0;
1307 /* Only the Current / Maximum Capacity Descriptor */
1310 /* Number of blocks */
1311 put_be32(&buf[0], currlun->num_sectors);
1313 put_be32(&buf[4], 512);
1314 /* Current capacity */
1317 #if (USTORAGE_QDATA_MAX < 12)
1318 #error "(USTORAGE_QDATA_MAX < 12)"
1320 return (ustorage_fs_min_len(sc, 12, -1U));
1323 /*------------------------------------------------------------------------*
1324 * ustorage_fs_mode_select
1329 *------------------------------------------------------------------------*/
1331 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1333 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1335 /* We don't support MODE SELECT */
1336 currlun->sense_data = SS_INVALID_COMMAND;
1340 /*------------------------------------------------------------------------*
1341 * ustorage_fs_synchronize_cache
1346 *------------------------------------------------------------------------*/
1348 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1351 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1355 * We ignore the requested LBA and write out all dirty data buffers.
1359 currlun->sense_data = SS_WRITE_ERROR;
1365 /*------------------------------------------------------------------------*
1366 * ustorage_fs_read - read data from disk
1371 *------------------------------------------------------------------------*/
1373 ustorage_fs_read(struct ustorage_fs_softc *sc)
1375 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1376 uint64_t file_offset;
1381 * Get the starting Logical Block Address and check that it's not
1384 if (sc->sc_cbw->CBWCDB[0] == SC_READ_6) {
1385 lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1386 get_be16(&sc->sc_cbw->CBWCDB[2]);
1388 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1391 * We allow DPO (Disable Page Out = don't save data in the
1392 * cache) and FUA (Force Unit Access = don't read from the
1393 * cache), but we don't implement them.
1395 if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1396 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1400 len = sc->sc_transfer.data_rem >> 9;
1404 (len > currlun->num_sectors) ||
1405 (lba >= currlun->num_sectors)) {
1406 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1412 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1417 /*------------------------------------------------------------------------*
1418 * ustorage_fs_write - write data to disk
1423 *------------------------------------------------------------------------*/
1425 ustorage_fs_write(struct ustorage_fs_softc *sc)
1427 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1428 uint64_t file_offset;
1432 if (currlun->read_only) {
1433 currlun->sense_data = SS_WRITE_PROTECTED;
1436 /* XXX clear SYNC */
1439 * Get the starting Logical Block Address and check that it's not
1442 if (sc->sc_cbw->CBWCDB[0] == SC_WRITE_6)
1443 lba = (((uint32_t)sc->sc_cbw->CBWCDB[1]) << 16) |
1444 get_be16(&sc->sc_cbw->CBWCDB[2]);
1446 lba = get_be32(&sc->sc_cbw->CBWCDB[2]);
1449 * We allow DPO (Disable Page Out = don't save data in the
1450 * cache) and FUA (Force Unit Access = write directly to the
1451 * medium). We don't implement DPO; we implement FUA by
1452 * performing synchronous output.
1454 if ((sc->sc_cbw->CBWCDB[1] & ~0x18) != 0) {
1455 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1458 if (sc->sc_cbw->CBWCDB[1] & 0x08) {
1460 /* XXX set SYNC flag here */
1464 len = sc->sc_transfer.data_rem >> 9;
1468 (len > currlun->num_sectors) ||
1469 (lba >= currlun->num_sectors)) {
1470 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1476 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1481 /*------------------------------------------------------------------------*
1482 * ustorage_fs_min_len
1487 *------------------------------------------------------------------------*/
1489 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1491 if (len != sc->sc_transfer.data_rem) {
1493 if (sc->sc_transfer.cbw_dir == DIR_READ) {
1495 * there must be something wrong about this SCSI
1498 sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1501 /* compute the minimum length */
1503 if (sc->sc_transfer.data_rem > len) {
1504 /* data ends prematurely */
1505 sc->sc_transfer.data_rem = len;
1506 sc->sc_transfer.data_short = 1;
1508 /* check length alignment */
1510 if (sc->sc_transfer.data_rem & ~mask) {
1511 /* data ends prematurely */
1512 sc->sc_transfer.data_rem &= mask;
1513 sc->sc_transfer.data_short = 1;
1519 /*------------------------------------------------------------------------*
1520 * ustorage_fs_check_cmd - check command routine
1522 * Check whether the command is properly formed and whether its data
1523 * size and direction agree with the values we already have.
1528 *------------------------------------------------------------------------*/
1530 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1531 uint16_t mask, uint8_t needs_medium)
1533 struct ustorage_fs_lun *currlun;
1534 uint8_t lun = (sc->sc_cbw->CBWCDB[1] >> 5);
1537 /* Verify the length of the command itself */
1538 if (min_cmd_size > sc->sc_transfer.cmd_len) {
1539 DPRINTF("%u > %u\n",
1540 min_cmd_size, sc->sc_transfer.cmd_len);
1541 sc->sc_csw->bCSWStatus = CSWSTATUS_PHASE;
1544 /* Mask away the LUN */
1545 sc->sc_cbw->CBWCDB[1] &= 0x1f;
1547 /* Check if LUN is correct */
1548 if (lun != sc->sc_transfer.lun) {
1552 if (sc->sc_transfer.lun <= sc->sc_last_lun) {
1553 sc->sc_transfer.currlun = currlun =
1554 sc->sc_lun + sc->sc_transfer.lun;
1555 if (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE) {
1556 currlun->sense_data = SS_NO_SENSE;
1557 currlun->sense_data_info = 0;
1558 currlun->info_valid = 0;
1561 * If a unit attention condition exists, only INQUIRY
1562 * and REQUEST SENSE commands are allowed. Anything
1565 if ((currlun->unit_attention_data != SS_NO_SENSE) &&
1566 (sc->sc_cbw->CBWCDB[0] != SC_INQUIRY) &&
1567 (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE)) {
1568 currlun->sense_data = currlun->unit_attention_data;
1569 currlun->unit_attention_data = SS_NO_SENSE;
1573 sc->sc_transfer.currlun = currlun = NULL;
1576 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1577 * to use unsupported LUNs; all others may not.
1579 if ((sc->sc_cbw->CBWCDB[0] != SC_INQUIRY) &&
1580 (sc->sc_cbw->CBWCDB[0] != SC_REQUEST_SENSE)) {
1586 * Check that only command bytes listed in the mask are
1589 for (i = 0; i != min_cmd_size; i++) {
1590 if (sc->sc_cbw->CBWCDB[i] && !(mask & (1UL << i))) {
1592 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1599 * If the medium isn't mounted and the command needs to access
1600 * it, return an error.
1602 if (currlun && (!currlun->memory_image) && needs_medium) {
1603 currlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1609 /*------------------------------------------------------------------------*
1610 * ustorage_fs_do_cmd - do command
1615 *------------------------------------------------------------------------*/
1617 ustorage_fs_do_cmd(struct ustorage_fs_softc *sc)
1622 const uint32_t mask9 = (0xFFFFFFFFUL >> 9) << 9;
1624 /* set default data transfer pointer */
1625 sc->sc_transfer.data_ptr = sc->sc_qdata;
1627 DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1628 sc->sc_cbw->CBWCDB[0], sc->sc_transfer.data_rem);
1630 switch (sc->sc_cbw->CBWCDB[0]) {
1632 sc->sc_transfer.cmd_dir = DIR_WRITE;
1633 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1637 error = ustorage_fs_check_cmd(sc, 6,
1642 error = ustorage_fs_inquiry(sc);
1646 case SC_MODE_SELECT_6:
1647 sc->sc_transfer.cmd_dir = DIR_READ;
1648 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1652 error = ustorage_fs_check_cmd(sc, 6,
1653 (1UL << 1) | (1UL << 4) | 1, 0);
1657 error = ustorage_fs_mode_select(sc);
1661 case SC_MODE_SELECT_10:
1662 sc->sc_transfer.cmd_dir = DIR_READ;
1663 error = ustorage_fs_min_len(sc,
1664 get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1668 error = ustorage_fs_check_cmd(sc, 10,
1669 (1UL << 1) | (3UL << 7) | 1, 0);
1673 error = ustorage_fs_mode_select(sc);
1677 case SC_MODE_SENSE_6:
1678 sc->sc_transfer.cmd_dir = DIR_WRITE;
1679 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1683 error = ustorage_fs_check_cmd(sc, 6,
1684 (1UL << 1) | (1UL << 2) | (1UL << 4) | 1, 0);
1688 error = ustorage_fs_mode_sense(sc);
1692 case SC_MODE_SENSE_10:
1693 sc->sc_transfer.cmd_dir = DIR_WRITE;
1694 error = ustorage_fs_min_len(sc,
1695 get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1699 error = ustorage_fs_check_cmd(sc, 10,
1700 (1UL << 1) | (1UL << 2) | (3UL << 7) | 1, 0);
1704 error = ustorage_fs_mode_sense(sc);
1708 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1709 error = ustorage_fs_min_len(sc, 0, -1U);
1713 error = ustorage_fs_check_cmd(sc, 6,
1718 error = ustorage_fs_prevent_allow(sc);
1723 i = sc->sc_cbw->CBWCDB[4];
1724 sc->sc_transfer.cmd_dir = DIR_WRITE;
1725 temp = ((i == 0) ? 256UL : i);
1726 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1730 error = ustorage_fs_check_cmd(sc, 6,
1731 (7UL << 1) | (1UL << 4) | 1, 1);
1735 error = ustorage_fs_read(sc);
1740 sc->sc_transfer.cmd_dir = DIR_WRITE;
1741 temp = get_be16(&sc->sc_cbw->CBWCDB[7]);
1742 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1746 error = ustorage_fs_check_cmd(sc, 10,
1747 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1751 error = ustorage_fs_read(sc);
1756 sc->sc_transfer.cmd_dir = DIR_WRITE;
1757 temp = get_be32(&sc->sc_cbw->CBWCDB[6]);
1758 if (temp >= (1UL << (32 - 9))) {
1759 /* numerical overflow */
1760 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
1764 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1768 error = ustorage_fs_check_cmd(sc, 12,
1769 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1773 error = ustorage_fs_read(sc);
1777 case SC_READ_CAPACITY:
1778 sc->sc_transfer.cmd_dir = DIR_WRITE;
1779 error = ustorage_fs_check_cmd(sc, 10,
1780 (0xfUL << 2) | (1UL << 8) | 1, 1);
1784 error = ustorage_fs_read_capacity(sc);
1788 case SC_READ_FORMAT_CAPACITIES:
1789 sc->sc_transfer.cmd_dir = DIR_WRITE;
1790 error = ustorage_fs_min_len(sc,
1791 get_be16(&sc->sc_cbw->CBWCDB[7]), -1U);
1795 error = ustorage_fs_check_cmd(sc, 10,
1800 error = ustorage_fs_read_format_capacities(sc);
1804 case SC_REQUEST_SENSE:
1805 sc->sc_transfer.cmd_dir = DIR_WRITE;
1806 error = ustorage_fs_min_len(sc, sc->sc_cbw->CBWCDB[4], -1U);
1810 error = ustorage_fs_check_cmd(sc, 6,
1815 error = ustorage_fs_request_sense(sc);
1819 case SC_START_STOP_UNIT:
1820 error = ustorage_fs_min_len(sc, 0, -1U);
1824 error = ustorage_fs_check_cmd(sc, 6,
1825 (1UL << 1) | (1UL << 4) | 1, 0);
1829 error = ustorage_fs_start_stop(sc);
1833 case SC_SYNCHRONIZE_CACHE:
1834 error = ustorage_fs_min_len(sc, 0, -1U);
1838 error = ustorage_fs_check_cmd(sc, 10,
1839 (0xfUL << 2) | (3UL << 7) | 1, 1);
1843 error = ustorage_fs_synchronize_cache(sc);
1847 case SC_TEST_UNIT_READY:
1848 error = ustorage_fs_min_len(sc, 0, -1U);
1852 error = ustorage_fs_check_cmd(sc, 6,
1857 * Although optional, this command is used by MS-Windows.
1858 * We support a minimal version: BytChk must be 0.
1861 error = ustorage_fs_min_len(sc, 0, -1U);
1865 error = ustorage_fs_check_cmd(sc, 10,
1866 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1870 error = ustorage_fs_verify(sc);
1875 i = sc->sc_cbw->CBWCDB[4];
1876 sc->sc_transfer.cmd_dir = DIR_READ;
1877 temp = ((i == 0) ? 256UL : i);
1878 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1882 error = ustorage_fs_check_cmd(sc, 6,
1883 (7UL << 1) | (1UL << 4) | 1, 1);
1887 error = ustorage_fs_write(sc);
1892 sc->sc_transfer.cmd_dir = DIR_READ;
1893 temp = get_be16(&sc->sc_cbw->CBWCDB[7]);
1894 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1898 error = ustorage_fs_check_cmd(sc, 10,
1899 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1903 error = ustorage_fs_write(sc);
1908 sc->sc_transfer.cmd_dir = DIR_READ;
1909 temp = get_be32(&sc->sc_cbw->CBWCDB[6]);
1910 if (temp > (mask9 >> 9)) {
1911 /* numerical overflow */
1912 sc->sc_csw->bCSWStatus = CSWSTATUS_FAILED;
1916 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1920 error = ustorage_fs_check_cmd(sc, 12,
1921 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1925 error = ustorage_fs_write(sc);
1930 * Some mandatory commands that we recognize but don't
1931 * implement. They don't mean much in this setting.
1932 * It's left as an exercise for anyone interested to
1933 * implement RESERVE and RELEASE in terms of Posix
1936 case SC_FORMAT_UNIT:
1939 case SC_SEND_DIAGNOSTIC:
1943 error = ustorage_fs_min_len(sc, 0, -1U);
1947 error = ustorage_fs_check_cmd(sc, sc->sc_transfer.cmd_len,
1952 sc->sc_transfer.currlun->sense_data =