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/stddef.h>
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
53 #include <sys/unistd.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
63 #define USB_DEBUG_VAR ustorage_fs_debug
64 #include <dev/usb/usb_debug.h>
67 static int ustorage_fs_debug = 0;
69 SYSCTL_NODE(_hw_usb, OID_AUTO, ustorage_fs, CTLFLAG_RW, 0, "USB ustorage_fs");
70 SYSCTL_INT(_hw_usb_ustorage_fs, OID_AUTO, debug, CTLFLAG_RW,
71 &ustorage_fs_debug, 0, "ustorage_fs debug level");
74 /* Define some limits */
76 #ifndef USTORAGE_FS_BULK_SIZE
77 #define USTORAGE_FS_BULK_SIZE (1UL << 17) /* bytes */
80 #ifndef USTORAGE_FS_MAX_LUN
81 #define USTORAGE_FS_MAX_LUN 8 /* units */
84 #ifndef USTORAGE_QDATA_MAX
85 #define USTORAGE_QDATA_MAX 40 /* bytes */
88 #define sc_cmd_data sc_cbw.CBWCDB
91 * The SCSI ID string must be exactly 28 characters long
92 * exluding the terminating zero.
94 #ifndef USTORAGE_FS_ID_STRING
95 #define USTORAGE_FS_ID_STRING \
97 "File-Stor Gadget" /* 16 */ \
102 * The following macro defines the number of
103 * sectors to be allocated for the RAM disk:
105 #ifndef USTORAGE_FS_RAM_SECT
106 #define USTORAGE_FS_RAM_SECT (1UL << 13)
109 static uint8_t *ustorage_fs_ramdisk;
111 /* USB transfer definitions */
113 #define USTORAGE_FS_T_BBB_COMMAND 0
114 #define USTORAGE_FS_T_BBB_DATA_DUMP 1
115 #define USTORAGE_FS_T_BBB_DATA_READ 2
116 #define USTORAGE_FS_T_BBB_DATA_WRITE 3
117 #define USTORAGE_FS_T_BBB_STATUS 4
118 #define USTORAGE_FS_T_BBB_MAX 5
120 /* USB data stage direction */
126 /* USB interface specific control request */
128 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
129 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
131 /* Command Block Wrapper */
133 uDWord dCBWSignature;
134 #define CBWSIGNATURE 0x43425355
136 uDWord dCBWDataTransferLength;
138 #define CBWFLAGS_OUT 0x00
139 #define CBWFLAGS_IN 0x80
142 #define CBWCDBLENGTH 16
143 uByte CBWCDB[CBWCDBLENGTH];
144 } __packed ustorage_fs_bbb_cbw_t;
146 #define USTORAGE_FS_BBB_CBW_SIZE 31
148 /* Command Status Wrapper */
150 uDWord dCSWSignature;
151 #define CSWSIGNATURE 0x53425355
153 uDWord dCSWDataResidue;
155 #define CSWSTATUS_GOOD 0x0
156 #define CSWSTATUS_FAILED 0x1
157 #define CSWSTATUS_PHASE 0x2
158 } __packed ustorage_fs_bbb_csw_t;
160 #define USTORAGE_FS_BBB_CSW_SIZE 13
162 struct ustorage_fs_lun {
164 uint8_t *memory_image;
166 uint32_t num_sectors;
168 uint32_t sense_data_info;
169 uint32_t unit_attention_data;
172 uint8_t prevent_medium_removal:1;
173 uint8_t info_valid:1;
177 struct ustorage_fs_softc {
179 ustorage_fs_bbb_cbw_t sc_cbw; /* Command Wrapper Block */
180 ustorage_fs_bbb_csw_t sc_csw; /* Command Status Block */
184 struct ustorage_fs_lun sc_lun[USTORAGE_FS_MAX_LUN];
188 struct ustorage_fs_lun *currlun;
190 uint32_t data_rem; /* bytes, as reported by the command
192 uint32_t offset; /* bytes */
198 uint8_t data_short:1;
199 uint8_t data_error:1;
203 struct usb_device *sc_udev;
204 struct usb_xfer *sc_xfer[USTORAGE_FS_T_BBB_MAX];
206 uint8_t sc_iface_no; /* interface number */
208 uint8_t sc_last_xfer_index;
209 uint8_t sc_qdata[USTORAGE_QDATA_MAX];
214 static device_probe_t ustorage_fs_probe;
215 static device_attach_t ustorage_fs_attach;
216 static device_detach_t ustorage_fs_detach;
217 static device_suspend_t ustorage_fs_suspend;
218 static device_resume_t ustorage_fs_resume;
219 static usb_handle_request_t ustorage_fs_handle_request;
221 static usb_callback_t ustorage_fs_t_bbb_command_callback;
222 static usb_callback_t ustorage_fs_t_bbb_data_dump_callback;
223 static usb_callback_t ustorage_fs_t_bbb_data_read_callback;
224 static usb_callback_t ustorage_fs_t_bbb_data_write_callback;
225 static usb_callback_t ustorage_fs_t_bbb_status_callback;
227 static void ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index);
228 static void ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc);
230 static uint8_t ustorage_fs_verify(struct ustorage_fs_softc *sc);
231 static uint8_t ustorage_fs_inquiry(struct ustorage_fs_softc *sc);
232 static uint8_t ustorage_fs_request_sense(struct ustorage_fs_softc *sc);
233 static uint8_t ustorage_fs_read_capacity(struct ustorage_fs_softc *sc);
234 static uint8_t ustorage_fs_mode_sense(struct ustorage_fs_softc *sc);
235 static uint8_t ustorage_fs_start_stop(struct ustorage_fs_softc *sc);
236 static uint8_t ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc);
237 static uint8_t ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc);
238 static uint8_t ustorage_fs_mode_select(struct ustorage_fs_softc *sc);
239 static uint8_t ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask);
240 static uint8_t ustorage_fs_read(struct ustorage_fs_softc *sc);
241 static uint8_t ustorage_fs_write(struct ustorage_fs_softc *sc);
242 static uint8_t ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t cmd_size, uint16_t mask, uint8_t needs_medium);
243 static uint8_t ustorage_fs_do_cmd(struct ustorage_fs_softc *sc);
245 static device_method_t ustorage_fs_methods[] = {
247 DEVMETHOD(usb_handle_request, ustorage_fs_handle_request),
249 /* Device interface */
250 DEVMETHOD(device_probe, ustorage_fs_probe),
251 DEVMETHOD(device_attach, ustorage_fs_attach),
252 DEVMETHOD(device_detach, ustorage_fs_detach),
253 DEVMETHOD(device_suspend, ustorage_fs_suspend),
254 DEVMETHOD(device_resume, ustorage_fs_resume),
259 static driver_t ustorage_fs_driver = {
260 .name = "ustorage_fs",
261 .methods = ustorage_fs_methods,
262 .size = sizeof(struct ustorage_fs_softc),
265 static devclass_t ustorage_fs_devclass;
267 DRIVER_MODULE(ustorage_fs, uhub, ustorage_fs_driver, ustorage_fs_devclass, NULL, 0);
268 MODULE_VERSION(ustorage_fs, 0);
269 MODULE_DEPEND(ustorage_fs, usb, 1, 1, 1);
271 static struct usb_config ustorage_fs_bbb_config[USTORAGE_FS_T_BBB_MAX] = {
273 [USTORAGE_FS_T_BBB_COMMAND] = {
275 .endpoint = UE_ADDR_ANY,
276 .direction = UE_DIR_OUT,
277 .bufsize = sizeof(ustorage_fs_bbb_cbw_t),
278 .flags = {.ext_buffer = 1,},
279 .callback = &ustorage_fs_t_bbb_command_callback,
280 .usb_mode = USB_MODE_DEVICE,
283 [USTORAGE_FS_T_BBB_DATA_DUMP] = {
285 .endpoint = UE_ADDR_ANY,
286 .direction = UE_DIR_OUT,
287 .bufsize = 0, /* use wMaxPacketSize */
288 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
289 .callback = &ustorage_fs_t_bbb_data_dump_callback,
290 .usb_mode = USB_MODE_DEVICE,
293 [USTORAGE_FS_T_BBB_DATA_READ] = {
295 .endpoint = UE_ADDR_ANY,
296 .direction = UE_DIR_OUT,
297 .bufsize = USTORAGE_FS_BULK_SIZE,
298 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
299 .callback = &ustorage_fs_t_bbb_data_read_callback,
300 .usb_mode = USB_MODE_DEVICE,
303 [USTORAGE_FS_T_BBB_DATA_WRITE] = {
305 .endpoint = UE_ADDR_ANY,
306 .direction = UE_DIR_IN,
307 .bufsize = USTORAGE_FS_BULK_SIZE,
308 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
309 .callback = &ustorage_fs_t_bbb_data_write_callback,
310 .usb_mode = USB_MODE_DEVICE,
313 [USTORAGE_FS_T_BBB_STATUS] = {
315 .endpoint = UE_ADDR_ANY,
316 .direction = UE_DIR_IN,
317 .bufsize = sizeof(ustorage_fs_bbb_csw_t),
318 .flags = {.short_xfer_ok = 1,.ext_buffer = 1,},
319 .callback = &ustorage_fs_t_bbb_status_callback,
320 .usb_mode = USB_MODE_DEVICE,
325 * USB device probe/attach/detach
329 ustorage_fs_probe(device_t dev)
331 struct usb_attach_arg *uaa = device_get_ivars(dev);
332 struct usb_interface_descriptor *id;
334 if (uaa->usb_mode != USB_MODE_DEVICE) {
337 /* Check for a standards compliant device */
338 id = usbd_get_interface_descriptor(uaa->iface);
340 (id->bInterfaceClass != UICLASS_MASS) ||
341 (id->bInterfaceSubClass != UISUBCLASS_SCSI) ||
342 (id->bInterfaceProtocol != UIPROTO_MASS_BBB)) {
345 return (BUS_PROBE_GENERIC);
349 ustorage_fs_attach(device_t dev)
351 struct ustorage_fs_softc *sc = device_get_softc(dev);
352 struct usb_attach_arg *uaa = device_get_ivars(dev);
353 struct usb_interface_descriptor *id;
358 * NOTE: the softc struct is bzero-ed in device_set_driver.
359 * We can safely call ustorage_fs_detach without specifically
360 * initializing the struct.
364 sc->sc_udev = uaa->device;
365 unit = device_get_unit(dev);
368 if (ustorage_fs_ramdisk == NULL) {
370 * allocate a memory image for our ramdisk until
373 ustorage_fs_ramdisk =
374 malloc(USTORAGE_FS_RAM_SECT << 9, M_USB, M_ZERO | M_WAITOK);
375 if (ustorage_fs_ramdisk == NULL) {
379 sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
380 sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
381 sc->sc_lun[0].removable = 1;
384 device_set_usb_desc(dev);
386 mtx_init(&sc->sc_mtx, "USTORAGE_FS lock",
387 NULL, (MTX_DEF | MTX_RECURSE));
389 /* get interface index */
391 id = usbd_get_interface_descriptor(uaa->iface);
393 device_printf(dev, "failed to get "
394 "interface number\n");
397 sc->sc_iface_no = id->bInterfaceNumber;
399 err = usbd_transfer_setup(uaa->device,
400 &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
401 USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_mtx);
403 device_printf(dev, "could not setup required "
404 "transfers, %s\n", usbd_errstr(err));
407 /* start Mass Storage State Machine */
409 mtx_lock(&sc->sc_mtx);
410 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
411 mtx_unlock(&sc->sc_mtx);
413 return (0); /* success */
416 ustorage_fs_detach(dev);
417 return (ENXIO); /* failure */
421 ustorage_fs_detach(device_t dev)
423 struct ustorage_fs_softc *sc = device_get_softc(dev);
425 /* teardown our statemachine */
427 usbd_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
429 mtx_destroy(&sc->sc_mtx);
431 return (0); /* success */
435 ustorage_fs_suspend(device_t dev)
437 device_printf(dev, "suspending\n");
438 return (0); /* success */
442 ustorage_fs_resume(device_t dev)
444 device_printf(dev, "resuming\n");
445 return (0); /* success */
449 * Generic functions to handle transfers
453 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
455 if (sc->sc_xfer[xfer_index]) {
456 sc->sc_last_xfer_index = xfer_index;
457 usbd_transfer_start(sc->sc_xfer[xfer_index]);
462 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
464 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
465 mtx_unlock(&sc->sc_mtx);
466 usbd_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
467 mtx_lock(&sc->sc_mtx);
471 ustorage_fs_handle_request(device_t dev,
472 const void *preq, void **pptr, uint16_t *plen,
473 uint16_t offset, uint8_t *pstate)
475 struct ustorage_fs_softc *sc = device_get_softc(dev);
476 const struct usb_device_request *req = preq;
477 uint8_t is_complete = *pstate;
480 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
481 (req->bRequest == UR_BBB_RESET)) {
483 mtx_lock(&sc->sc_mtx);
484 ustorage_fs_transfer_stop(sc);
485 sc->sc_transfer.data_error = 1;
486 ustorage_fs_transfer_start(sc,
487 USTORAGE_FS_T_BBB_COMMAND);
488 mtx_unlock(&sc->sc_mtx);
490 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
491 (req->bRequest == UR_BBB_GET_MAX_LUN)) {
494 *pptr = &sc->sc_last_lun;
501 return (ENXIO); /* use builtin handler */
505 ustorage_fs_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
507 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
513 switch (USB_GET_STATE(xfer)) {
514 case USB_ST_TRANSFERRED:
516 tag = UGETDW(sc->sc_cbw.dCBWSignature);
518 if (tag != CBWSIGNATURE) {
520 DPRINTF("invalid signature 0x%08x\n", tag);
523 tag = UGETDW(sc->sc_cbw.dCBWTag);
526 USETDW(sc->sc_csw.dCSWTag, tag);
529 sc->sc_csw.bCSWStatus = 0;
531 /* reset data offset, data length and data remainder */
532 sc->sc_transfer.offset = 0;
533 sc->sc_transfer.data_rem =
534 UGETDW(sc->sc_cbw.dCBWDataTransferLength);
536 /* reset data flags */
537 sc->sc_transfer.data_short = 0;
540 sc->sc_transfer.lun = sc->sc_cbw.bCBWLUN;
542 if (sc->sc_transfer.data_rem == 0) {
543 sc->sc_transfer.cbw_dir = DIR_NONE;
545 if (sc->sc_cbw.bCBWFlags & CBWFLAGS_IN) {
546 sc->sc_transfer.cbw_dir = DIR_WRITE;
548 sc->sc_transfer.cbw_dir = DIR_READ;
552 sc->sc_transfer.cmd_len = sc->sc_cbw.bCDBLength;
553 if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw.CBWCDB)) ||
554 (sc->sc_transfer.cmd_len == 0)) {
555 /* just halt - this is invalid */
556 DPRINTF("invalid command length %d bytes\n",
557 sc->sc_transfer.cmd_len);
561 err = ustorage_fs_do_cmd(sc);
564 DPRINTF("command failed\n");
567 if ((sc->sc_transfer.data_rem > 0) &&
568 (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
569 /* contradicting data transfer direction */
571 DPRINTF("data direction mismatch\n");
574 switch (sc->sc_transfer.cbw_dir) {
576 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
579 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
582 ustorage_fs_transfer_start(sc,
583 USTORAGE_FS_T_BBB_STATUS);
590 if (sc->sc_transfer.data_error) {
591 sc->sc_transfer.data_error = 0;
592 usbd_xfer_set_stall(xfer);
593 DPRINTF("stall pipe\n");
596 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_cbw,
598 usbd_transfer_submit(xfer);
603 if (error == USB_ERR_CANCELLED) {
606 /* If the pipe is already stalled, don't do another stall */
607 if (!usbd_xfer_is_stalled(xfer))
608 sc->sc_transfer.data_error = 1;
614 if (sc->sc_csw.bCSWStatus == 0) {
615 /* set some default error code */
616 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
618 if (sc->sc_transfer.cbw_dir == DIR_READ) {
620 ustorage_fs_transfer_start(sc,
621 USTORAGE_FS_T_BBB_DATA_DUMP);
624 if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
625 /* need to stall before status */
626 sc->sc_transfer.data_error = 1;
628 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
633 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer *xfer, usb_error_t error)
635 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
636 uint32_t max_bulk = usbd_xfer_max_len(xfer);
639 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
643 switch (USB_GET_STATE(xfer)) {
644 case USB_ST_TRANSFERRED:
645 sc->sc_transfer.data_rem -= actlen;
646 sc->sc_transfer.offset += actlen;
648 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
649 /* short transfer or end of data */
650 ustorage_fs_transfer_start(sc,
651 USTORAGE_FS_T_BBB_STATUS);
658 if (max_bulk > sc->sc_transfer.data_rem) {
659 max_bulk = sc->sc_transfer.data_rem;
661 if (sc->sc_transfer.data_error) {
662 sc->sc_transfer.data_error = 0;
663 usbd_xfer_set_stall(xfer);
665 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
666 usbd_transfer_submit(xfer);
670 if (error == USB_ERR_CANCELLED) {
674 * If the pipe is already stalled, don't do another stall:
676 if (!usbd_xfer_is_stalled(xfer))
677 sc->sc_transfer.data_error = 1;
685 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
687 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
688 uint32_t max_bulk = usbd_xfer_max_len(xfer);
691 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
695 switch (USB_GET_STATE(xfer)) {
696 case USB_ST_TRANSFERRED:
697 sc->sc_transfer.data_rem -= actlen;
698 sc->sc_transfer.data_ptr += actlen;
699 sc->sc_transfer.offset += actlen;
701 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
702 /* short transfer or end of data */
703 ustorage_fs_transfer_start(sc,
704 USTORAGE_FS_T_BBB_STATUS);
711 if (max_bulk > sc->sc_transfer.data_rem) {
712 max_bulk = sc->sc_transfer.data_rem;
714 if (sc->sc_transfer.data_error) {
715 sc->sc_transfer.data_error = 0;
716 usbd_xfer_set_stall(xfer);
719 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
721 usbd_transfer_submit(xfer);
725 if (error == USB_ERR_CANCELLED) {
728 /* If the pipe is already stalled, don't do another stall */
729 if (!usbd_xfer_is_stalled(xfer))
730 sc->sc_transfer.data_error = 1;
738 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
740 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
741 uint32_t max_bulk = usbd_xfer_max_len(xfer);
744 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
748 switch (USB_GET_STATE(xfer)) {
749 case USB_ST_TRANSFERRED:
750 sc->sc_transfer.data_rem -= actlen;
751 sc->sc_transfer.data_ptr += actlen;
752 sc->sc_transfer.offset += actlen;
754 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
755 /* short transfer or end of data */
756 ustorage_fs_transfer_start(sc,
757 USTORAGE_FS_T_BBB_STATUS);
762 if (max_bulk >= sc->sc_transfer.data_rem) {
763 max_bulk = sc->sc_transfer.data_rem;
764 if (sc->sc_transfer.data_short)
765 usbd_xfer_set_flag(xfer, USB_FORCE_SHORT_XFER);
767 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
769 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
771 if (sc->sc_transfer.data_error) {
772 sc->sc_transfer.data_error = 0;
773 usbd_xfer_set_stall(xfer);
776 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
778 usbd_transfer_submit(xfer);
782 if (error == USB_ERR_CANCELLED) {
786 * If the pipe is already stalled, don't do another
789 if (!usbd_xfer_is_stalled(xfer))
790 sc->sc_transfer.data_error = 1;
798 ustorage_fs_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
800 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
804 switch (USB_GET_STATE(xfer)) {
805 case USB_ST_TRANSFERRED:
806 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
811 USETDW(sc->sc_csw.dCSWSignature, CSWSIGNATURE);
812 USETDW(sc->sc_csw.dCSWDataResidue, sc->sc_transfer.data_rem);
814 if (sc->sc_transfer.data_error) {
815 sc->sc_transfer.data_error = 0;
816 usbd_xfer_set_stall(xfer);
819 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_csw,
821 usbd_transfer_submit(xfer);
825 if (error == USB_ERR_CANCELLED) {
828 /* If the pipe is already stalled, don't do another stall */
829 if (!usbd_xfer_is_stalled(xfer))
830 sc->sc_transfer.data_error = 1;
837 /* SCSI commands that we recognize */
838 #define SC_FORMAT_UNIT 0x04
839 #define SC_INQUIRY 0x12
840 #define SC_MODE_SELECT_6 0x15
841 #define SC_MODE_SELECT_10 0x55
842 #define SC_MODE_SENSE_6 0x1a
843 #define SC_MODE_SENSE_10 0x5a
844 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
845 #define SC_READ_6 0x08
846 #define SC_READ_10 0x28
847 #define SC_READ_12 0xa8
848 #define SC_READ_CAPACITY 0x25
849 #define SC_READ_FORMAT_CAPACITIES 0x23
850 #define SC_RELEASE 0x17
851 #define SC_REQUEST_SENSE 0x03
852 #define SC_RESERVE 0x16
853 #define SC_SEND_DIAGNOSTIC 0x1d
854 #define SC_START_STOP_UNIT 0x1b
855 #define SC_SYNCHRONIZE_CACHE 0x35
856 #define SC_TEST_UNIT_READY 0x00
857 #define SC_VERIFY 0x2f
858 #define SC_WRITE_6 0x0a
859 #define SC_WRITE_10 0x2a
860 #define SC_WRITE_12 0xaa
862 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
863 #define SS_NO_SENSE 0
864 #define SS_COMMUNICATION_FAILURE 0x040800
865 #define SS_INVALID_COMMAND 0x052000
866 #define SS_INVALID_FIELD_IN_CDB 0x052400
867 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
868 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
869 #define SS_MEDIUM_NOT_PRESENT 0x023a00
870 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
871 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
872 #define SS_RESET_OCCURRED 0x062900
873 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
874 #define SS_UNRECOVERED_READ_ERROR 0x031100
875 #define SS_WRITE_ERROR 0x030c02
876 #define SS_WRITE_PROTECTED 0x072700
878 #define SK(x) ((uint8_t) ((x) >> 16)) /* Sense Key byte, etc. */
879 #define ASC(x) ((uint8_t) ((x) >> 8))
880 #define ASCQ(x) ((uint8_t) (x))
882 /* Routines for unaligned data access */
885 get_be16(uint8_t *buf)
887 return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
891 get_be32(uint8_t *buf)
893 return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
894 ((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
898 put_be16(uint8_t *buf, uint16_t val)
905 put_be32(uint8_t *buf, uint32_t val)
913 /*------------------------------------------------------------------------*
919 *------------------------------------------------------------------------*/
921 ustorage_fs_verify(struct ustorage_fs_softc *sc)
923 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
926 uint64_t file_offset;
927 uint64_t amount_left;
930 * Get the starting Logical Block Address
932 lba = get_be32(&sc->sc_cmd_data[2]);
935 * We allow DPO (Disable Page Out = don't save data in the cache)
936 * but we don't implement it.
938 if ((sc->sc_cmd_data[1] & ~0x10) != 0) {
939 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
942 vlen = get_be16(&sc->sc_cmd_data[7]);
946 /* No default reply */
948 /* Prepare to carry out the file verify */
958 (vlen > currlun->num_sectors) ||
959 (lba >= currlun->num_sectors)) {
960 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
963 /* XXX TODO: verify that data is readable */
965 return (ustorage_fs_min_len(sc, 0, 0 - 1));
968 /*------------------------------------------------------------------------*
969 * ustorage_fs_inquiry
974 *------------------------------------------------------------------------*/
976 ustorage_fs_inquiry(struct ustorage_fs_softc *sc)
978 uint8_t *buf = sc->sc_transfer.data_ptr;
980 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
982 if (!sc->sc_transfer.currlun) {
983 /* Unsupported LUNs are okay */
986 /* Unsupported, no device - type */
987 return (ustorage_fs_min_len(sc, 36, 0 - 1));
990 /* Non - removable, direct - access device */
991 if (currlun->removable)
994 /* ANSI SCSI level 2 */
996 /* SCSI - 2 INQUIRY data format */
998 /* Additional length */
999 /* No special options */
1000 /* Copy in ID string */
1001 memcpy(buf + 8, USTORAGE_FS_ID_STRING, 28);
1003 #if (USTORAGE_QDATA_MAX < 36)
1004 #error "(USTORAGE_QDATA_MAX < 36)"
1006 return (ustorage_fs_min_len(sc, 36, 0 - 1));
1009 /*------------------------------------------------------------------------*
1010 * ustorage_fs_request_sense
1015 *------------------------------------------------------------------------*/
1017 ustorage_fs_request_sense(struct ustorage_fs_softc *sc)
1019 uint8_t *buf = sc->sc_transfer.data_ptr;
1020 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1026 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1028 * If a REQUEST SENSE command is received from an initiator
1029 * with a pending unit attention condition (before the target
1030 * generates the contingent allegiance condition), then the
1031 * target shall either:
1032 * a) report any pending sense data and preserve the unit
1033 * attention condition on the logical unit, or,
1034 * b) report the unit attention condition, may discard any
1035 * pending sense data, and clear the unit attention
1036 * condition on the logical unit for that initiator.
1038 * FSG normally uses option a); enable this code to use option b).
1041 if (currlun && currlun->unit_attention_data != SS_NO_SENSE) {
1042 currlun->sense_data = currlun->unit_attention_data;
1043 currlun->unit_attention_data = SS_NO_SENSE;
1048 /* Unsupported LUNs are okay */
1049 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1053 sd = currlun->sense_data;
1054 sdinfo = currlun->sense_data_info;
1055 valid = currlun->info_valid << 7;
1056 currlun->sense_data = SS_NO_SENSE;
1057 currlun->sense_data_info = 0;
1058 currlun->info_valid = 0;
1062 buf[0] = valid | 0x70;
1063 /* Valid, current error */
1065 put_be32(&buf[3], sdinfo);
1066 /* Sense information */
1068 /* Additional sense length */
1072 #if (USTORAGE_QDATA_MAX < 18)
1073 #error "(USTORAGE_QDATA_MAX < 18)"
1075 return (ustorage_fs_min_len(sc, 18, 0 - 1));
1078 /*------------------------------------------------------------------------*
1079 * ustorage_fs_read_capacity
1084 *------------------------------------------------------------------------*/
1086 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1088 uint8_t *buf = sc->sc_transfer.data_ptr;
1089 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1090 uint32_t lba = get_be32(&sc->sc_cmd_data[2]);
1091 uint8_t pmi = sc->sc_cmd_data[8];
1093 /* Check the PMI and LBA fields */
1094 if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1095 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1098 /* Max logical block */
1099 put_be32(&buf[0], currlun->num_sectors - 1);
1101 put_be32(&buf[4], 512);
1103 #if (USTORAGE_QDATA_MAX < 8)
1104 #error "(USTORAGE_QDATA_MAX < 8)"
1106 return (ustorage_fs_min_len(sc, 8, 0 - 1));
1109 /*------------------------------------------------------------------------*
1110 * ustorage_fs_mode_sense
1115 *------------------------------------------------------------------------*/
1117 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1119 uint8_t *buf = sc->sc_transfer.data_ptr;
1120 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1124 uint8_t mscmnd = sc->sc_cmd_data[0];
1127 uint8_t changeable_values;
1132 if ((sc->sc_cmd_data[1] & ~0x08) != 0) {
1134 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1137 pc = sc->sc_cmd_data[2] >> 6;
1138 page_code = sc->sc_cmd_data[2] & 0x3f;
1140 currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1143 changeable_values = (pc == 1);
1144 all_pages = (page_code == 0x3f);
1147 * Write the mode parameter header. Fixed values are: default
1148 * medium type, no cache control (DPOFUA), and no block descriptors.
1149 * The only variable value is the WriteProtect bit. We will fill in
1150 * the mode data length later.
1153 if (mscmnd == SC_MODE_SENSE_6) {
1154 buf[2] = (currlun->read_only ? 0x80 : 0x00);
1159 /* SC_MODE_SENSE_10 */
1160 buf[3] = (currlun->read_only ? 0x80 : 0x00);
1164 /* Should really be mod_data.buflen */
1167 /* No block descriptors */
1170 * The mode pages, in numerical order.
1172 if ((page_code == 0x08) || all_pages) {
1177 memset(buf + 2, 0, 10);
1178 /* None of the fields are changeable */
1180 if (!changeable_values) {
1182 /* Write cache enable, */
1183 /* Read cache not disabled */
1184 /* No cache retention priorities */
1185 put_be16(&buf[4], 0xffff);
1186 /* Don 't disable prefetch */
1187 /* Minimum prefetch = 0 */
1188 put_be16(&buf[8], 0xffff);
1189 /* Maximum prefetch */
1190 put_be16(&buf[10], 0xffff);
1191 /* Maximum prefetch ceiling */
1196 * Check that a valid page was requested and the mode data length
1201 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1204 /* Store the mode data length */
1205 if (mscmnd == SC_MODE_SENSE_6)
1208 put_be16(buf0, len - 2);
1210 #if (USTORAGE_QDATA_MAX < 24)
1211 #error "(USTORAGE_QDATA_MAX < 24)"
1213 return (ustorage_fs_min_len(sc, len, 0 - 1));
1216 /*------------------------------------------------------------------------*
1217 * ustorage_fs_start_stop
1222 *------------------------------------------------------------------------*/
1224 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1226 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1231 if (!currlun->removable) {
1232 currlun->sense_data = SS_INVALID_COMMAND;
1235 immed = sc->sc_cmd_data[1] & 0x01;
1236 loej = sc->sc_cmd_data[4] & 0x02;
1237 start = sc->sc_cmd_data[4] & 0x01;
1239 if (immed || loej || start) {
1245 /*------------------------------------------------------------------------*
1246 * ustorage_fs_prevent_allow
1251 *------------------------------------------------------------------------*/
1253 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1255 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1258 if (!currlun->removable) {
1259 currlun->sense_data = SS_INVALID_COMMAND;
1262 prevent = sc->sc_cmd_data[4] & 0x01;
1263 if ((sc->sc_cmd_data[4] & ~0x01) != 0) {
1264 /* Mask away Prevent */
1265 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1268 if (currlun->prevent_medium_removal && !prevent) {
1269 //fsync_sub(currlun);
1271 currlun->prevent_medium_removal = prevent;
1275 /*------------------------------------------------------------------------*
1276 * ustorage_fs_read_format_capacities
1281 *------------------------------------------------------------------------*/
1283 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1285 uint8_t *buf = sc->sc_transfer.data_ptr;
1286 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1288 buf[0] = buf[1] = buf[2] = 0;
1290 /* Only the Current / Maximum Capacity Descriptor */
1293 /* Number of blocks */
1294 put_be32(&buf[0], currlun->num_sectors);
1296 put_be32(&buf[4], 512);
1297 /* Current capacity */
1300 #if (USTORAGE_QDATA_MAX < 12)
1301 #error "(USTORAGE_QDATA_MAX < 12)"
1303 return (ustorage_fs_min_len(sc, 12, 0 - 1));
1306 /*------------------------------------------------------------------------*
1307 * ustorage_fs_mode_select
1312 *------------------------------------------------------------------------*/
1314 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1316 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1318 /* We don't support MODE SELECT */
1319 currlun->sense_data = SS_INVALID_COMMAND;
1323 /*------------------------------------------------------------------------*
1324 * ustorage_fs_synchronize_cache
1329 *------------------------------------------------------------------------*/
1331 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1334 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1338 * We ignore the requested LBA and write out all dirty data buffers.
1342 currlun->sense_data = SS_WRITE_ERROR;
1348 /*------------------------------------------------------------------------*
1349 * ustorage_fs_read - read data from disk
1354 *------------------------------------------------------------------------*/
1356 ustorage_fs_read(struct ustorage_fs_softc *sc)
1358 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1359 uint64_t file_offset;
1364 * Get the starting Logical Block Address and check that it's not
1367 if (sc->sc_cmd_data[0] == SC_READ_6) {
1368 lba = (((uint32_t)sc->sc_cmd_data[1]) << 16) |
1369 get_be16(&sc->sc_cmd_data[2]);
1371 lba = get_be32(&sc->sc_cmd_data[2]);
1374 * We allow DPO (Disable Page Out = don't save data in the
1375 * cache) and FUA (Force Unit Access = don't read from the
1376 * cache), but we don't implement them.
1378 if ((sc->sc_cmd_data[1] & ~0x18) != 0) {
1379 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1383 len = sc->sc_transfer.data_rem >> 9;
1387 (len > currlun->num_sectors) ||
1388 (lba >= currlun->num_sectors)) {
1389 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1395 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1400 /*------------------------------------------------------------------------*
1401 * ustorage_fs_write - write data to disk
1406 *------------------------------------------------------------------------*/
1408 ustorage_fs_write(struct ustorage_fs_softc *sc)
1410 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1411 uint64_t file_offset;
1415 if (currlun->read_only) {
1416 currlun->sense_data = SS_WRITE_PROTECTED;
1419 /* XXX clear SYNC */
1422 * Get the starting Logical Block Address and check that it's not
1425 if (sc->sc_cmd_data[0] == SC_WRITE_6)
1426 lba = (((uint32_t)sc->sc_cmd_data[1]) << 16) |
1427 get_be16(&sc->sc_cmd_data[2]);
1429 lba = get_be32(&sc->sc_cmd_data[2]);
1432 * We allow DPO (Disable Page Out = don't save data in the
1433 * cache) and FUA (Force Unit Access = write directly to the
1434 * medium). We don't implement DPO; we implement FUA by
1435 * performing synchronous output.
1437 if ((sc->sc_cmd_data[1] & ~0x18) != 0) {
1438 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1441 if (sc->sc_cmd_data[1] & 0x08) {
1443 /* XXX set SYNC flag here */
1447 len = sc->sc_transfer.data_rem >> 9;
1451 (len > currlun->num_sectors) ||
1452 (lba >= currlun->num_sectors)) {
1453 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1459 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1464 /*------------------------------------------------------------------------*
1465 * ustorage_fs_min_len
1470 *------------------------------------------------------------------------*/
1472 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1474 if (len != sc->sc_transfer.data_rem) {
1476 if (sc->sc_transfer.cbw_dir == DIR_READ) {
1478 * there must be something wrong about this SCSI
1481 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1484 /* compute the minimum length */
1486 if (sc->sc_transfer.data_rem > len) {
1487 /* data ends prematurely */
1488 sc->sc_transfer.data_rem = len;
1489 sc->sc_transfer.data_short = 1;
1491 /* check length alignment */
1493 if (sc->sc_transfer.data_rem & ~mask) {
1494 /* data ends prematurely */
1495 sc->sc_transfer.data_rem &= mask;
1496 sc->sc_transfer.data_short = 1;
1502 /*------------------------------------------------------------------------*
1503 * ustorage_fs_check_cmd - check command routine
1505 * Check whether the command is properly formed and whether its data
1506 * size and direction agree with the values we already have.
1511 *------------------------------------------------------------------------*/
1513 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1514 uint16_t mask, uint8_t needs_medium)
1516 struct ustorage_fs_lun *currlun;
1517 uint8_t lun = (sc->sc_cmd_data[1] >> 5);
1520 /* Verify the length of the command itself */
1521 if (min_cmd_size > sc->sc_transfer.cmd_len) {
1522 DPRINTF("%u > %u\n",
1523 min_cmd_size, sc->sc_transfer.cmd_len);
1524 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1527 /* Mask away the LUN */
1528 sc->sc_cmd_data[1] &= 0x1f;
1530 /* Check if LUN is correct */
1531 if (lun != sc->sc_transfer.lun) {
1535 if (sc->sc_transfer.lun <= sc->sc_last_lun) {
1536 sc->sc_transfer.currlun = currlun =
1537 sc->sc_lun + sc->sc_transfer.lun;
1538 if (sc->sc_cmd_data[0] != SC_REQUEST_SENSE) {
1539 currlun->sense_data = SS_NO_SENSE;
1540 currlun->sense_data_info = 0;
1541 currlun->info_valid = 0;
1544 * If a unit attention condition exists, only INQUIRY
1545 * and REQUEST SENSE commands are allowed. Anything
1548 if ((currlun->unit_attention_data != SS_NO_SENSE) &&
1549 (sc->sc_cmd_data[0] != SC_INQUIRY) &&
1550 (sc->sc_cmd_data[0] != SC_REQUEST_SENSE)) {
1551 currlun->sense_data = currlun->unit_attention_data;
1552 currlun->unit_attention_data = SS_NO_SENSE;
1556 sc->sc_transfer.currlun = currlun = NULL;
1559 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1560 * to use unsupported LUNs; all others may not.
1562 if ((sc->sc_cmd_data[0] != SC_INQUIRY) &&
1563 (sc->sc_cmd_data[0] != SC_REQUEST_SENSE)) {
1569 * Check that only command bytes listed in the mask are
1572 for (i = 0; i != min_cmd_size; i++) {
1573 if (sc->sc_cmd_data[i] && !(mask & (1UL << i))) {
1575 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1582 * If the medium isn't mounted and the command needs to access
1583 * it, return an error.
1585 if (currlun && (!currlun->memory_image) && needs_medium) {
1586 currlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1592 /*------------------------------------------------------------------------*
1593 * ustorage_fs_do_cmd - do command
1598 *------------------------------------------------------------------------*/
1600 ustorage_fs_do_cmd(struct ustorage_fs_softc *sc)
1605 const uint32_t mask9 = (0xFFFFFFFFUL >> 9) << 9;
1607 /* set default data transfer pointer */
1608 sc->sc_transfer.data_ptr = sc->sc_qdata;
1610 DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1611 sc->sc_cmd_data[0], sc->sc_transfer.data_rem);
1613 switch (sc->sc_cmd_data[0]) {
1615 sc->sc_transfer.cmd_dir = DIR_WRITE;
1616 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1620 error = ustorage_fs_check_cmd(sc, 6,
1625 error = ustorage_fs_inquiry(sc);
1629 case SC_MODE_SELECT_6:
1630 sc->sc_transfer.cmd_dir = DIR_READ;
1631 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1635 error = ustorage_fs_check_cmd(sc, 6,
1636 (1UL << 1) | (1UL << 4) | 1, 0);
1640 error = ustorage_fs_mode_select(sc);
1644 case SC_MODE_SELECT_10:
1645 sc->sc_transfer.cmd_dir = DIR_READ;
1646 error = ustorage_fs_min_len(sc,
1647 get_be16(&sc->sc_cmd_data[7]), 0 - 1);
1651 error = ustorage_fs_check_cmd(sc, 10,
1652 (1UL << 1) | (3UL << 7) | 1, 0);
1656 error = ustorage_fs_mode_select(sc);
1660 case SC_MODE_SENSE_6:
1661 sc->sc_transfer.cmd_dir = DIR_WRITE;
1662 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1666 error = ustorage_fs_check_cmd(sc, 6,
1667 (1UL << 1) | (1UL << 2) | (1UL << 4) | 1, 0);
1671 error = ustorage_fs_mode_sense(sc);
1675 case SC_MODE_SENSE_10:
1676 sc->sc_transfer.cmd_dir = DIR_WRITE;
1677 error = ustorage_fs_min_len(sc,
1678 get_be16(&sc->sc_cmd_data[7]), 0 - 1);
1682 error = ustorage_fs_check_cmd(sc, 10,
1683 (1UL << 1) | (1UL << 2) | (3UL << 7) | 1, 0);
1687 error = ustorage_fs_mode_sense(sc);
1691 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1692 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1696 error = ustorage_fs_check_cmd(sc, 6,
1701 error = ustorage_fs_prevent_allow(sc);
1706 i = sc->sc_cmd_data[4];
1707 sc->sc_transfer.cmd_dir = DIR_WRITE;
1708 temp = ((i == 0) ? 256UL : i);
1709 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1713 error = ustorage_fs_check_cmd(sc, 6,
1714 (7UL << 1) | (1UL << 4) | 1, 1);
1718 error = ustorage_fs_read(sc);
1723 sc->sc_transfer.cmd_dir = DIR_WRITE;
1724 temp = get_be16(&sc->sc_cmd_data[7]);
1725 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1729 error = ustorage_fs_check_cmd(sc, 10,
1730 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1734 error = ustorage_fs_read(sc);
1739 sc->sc_transfer.cmd_dir = DIR_WRITE;
1740 temp = get_be32(&sc->sc_cmd_data[6]);
1741 if (temp >= (1UL << (32 - 9))) {
1742 /* numerical overflow */
1743 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
1747 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1751 error = ustorage_fs_check_cmd(sc, 12,
1752 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1756 error = ustorage_fs_read(sc);
1760 case SC_READ_CAPACITY:
1761 sc->sc_transfer.cmd_dir = DIR_WRITE;
1762 error = ustorage_fs_check_cmd(sc, 10,
1763 (0xfUL << 2) | (1UL << 8) | 1, 1);
1767 error = ustorage_fs_read_capacity(sc);
1771 case SC_READ_FORMAT_CAPACITIES:
1772 sc->sc_transfer.cmd_dir = DIR_WRITE;
1773 error = ustorage_fs_min_len(sc,
1774 get_be16(&sc->sc_cmd_data[7]), 0 - 1);
1778 error = ustorage_fs_check_cmd(sc, 10,
1783 error = ustorage_fs_read_format_capacities(sc);
1787 case SC_REQUEST_SENSE:
1788 sc->sc_transfer.cmd_dir = DIR_WRITE;
1789 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1793 error = ustorage_fs_check_cmd(sc, 6,
1798 error = ustorage_fs_request_sense(sc);
1802 case SC_START_STOP_UNIT:
1803 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1807 error = ustorage_fs_check_cmd(sc, 6,
1808 (1UL << 1) | (1UL << 4) | 1, 0);
1812 error = ustorage_fs_start_stop(sc);
1816 case SC_SYNCHRONIZE_CACHE:
1817 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1821 error = ustorage_fs_check_cmd(sc, 10,
1822 (0xfUL << 2) | (3UL << 7) | 1, 1);
1826 error = ustorage_fs_synchronize_cache(sc);
1830 case SC_TEST_UNIT_READY:
1831 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1835 error = ustorage_fs_check_cmd(sc, 6,
1840 * Although optional, this command is used by MS-Windows.
1841 * We support a minimal version: BytChk must be 0.
1844 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1848 error = ustorage_fs_check_cmd(sc, 10,
1849 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1853 error = ustorage_fs_verify(sc);
1858 i = sc->sc_cmd_data[4];
1859 sc->sc_transfer.cmd_dir = DIR_READ;
1860 temp = ((i == 0) ? 256UL : i);
1861 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1865 error = ustorage_fs_check_cmd(sc, 6,
1866 (7UL << 1) | (1UL << 4) | 1, 1);
1870 error = ustorage_fs_write(sc);
1875 sc->sc_transfer.cmd_dir = DIR_READ;
1876 temp = get_be16(&sc->sc_cmd_data[7]);
1877 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1881 error = ustorage_fs_check_cmd(sc, 10,
1882 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1886 error = ustorage_fs_write(sc);
1891 sc->sc_transfer.cmd_dir = DIR_READ;
1892 temp = get_be32(&sc->sc_cmd_data[6]);
1893 if (temp > (mask9 >> 9)) {
1894 /* numerical overflow */
1895 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
1899 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1903 error = ustorage_fs_check_cmd(sc, 12,
1904 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1908 error = ustorage_fs_write(sc);
1913 * Some mandatory commands that we recognize but don't
1914 * implement. They don't mean much in this setting.
1915 * It's left as an exercise for anyone interested to
1916 * implement RESERVE and RELEASE in terms of Posix
1919 case SC_FORMAT_UNIT:
1922 case SC_SEND_DIAGNOSTIC:
1926 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1930 error = ustorage_fs_check_cmd(sc, sc->sc_transfer.cmd_len,
1935 sc->sc_transfer.currlun->sense_data =