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 cleared 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);
367 /* enable power saving mode */
368 usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE);
371 if (ustorage_fs_ramdisk == NULL) {
373 * allocate a memory image for our ramdisk until
376 ustorage_fs_ramdisk =
377 malloc(USTORAGE_FS_RAM_SECT << 9, M_USB,
380 if (ustorage_fs_ramdisk == NULL) {
384 sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
385 sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
386 sc->sc_lun[0].removable = 1;
389 device_set_usb_desc(dev);
391 mtx_init(&sc->sc_mtx, "USTORAGE_FS lock",
392 NULL, (MTX_DEF | MTX_RECURSE));
394 /* get interface index */
396 id = usbd_get_interface_descriptor(uaa->iface);
398 device_printf(dev, "failed to get "
399 "interface number\n");
402 sc->sc_iface_no = id->bInterfaceNumber;
404 err = usbd_transfer_setup(uaa->device,
405 &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
406 USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_mtx);
408 device_printf(dev, "could not setup required "
409 "transfers, %s\n", usbd_errstr(err));
412 /* start Mass Storage State Machine */
414 mtx_lock(&sc->sc_mtx);
415 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
416 mtx_unlock(&sc->sc_mtx);
418 return (0); /* success */
421 ustorage_fs_detach(dev);
422 return (ENXIO); /* failure */
426 ustorage_fs_detach(device_t dev)
428 struct ustorage_fs_softc *sc = device_get_softc(dev);
430 /* teardown our statemachine */
432 usbd_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
434 mtx_destroy(&sc->sc_mtx);
436 return (0); /* success */
440 ustorage_fs_suspend(device_t dev)
442 device_printf(dev, "suspending\n");
443 return (0); /* success */
447 ustorage_fs_resume(device_t dev)
449 device_printf(dev, "resuming\n");
450 return (0); /* success */
454 * Generic functions to handle transfers
458 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
460 if (sc->sc_xfer[xfer_index]) {
461 sc->sc_last_xfer_index = xfer_index;
462 usbd_transfer_start(sc->sc_xfer[xfer_index]);
467 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
469 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
470 mtx_unlock(&sc->sc_mtx);
471 usbd_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
472 mtx_lock(&sc->sc_mtx);
476 ustorage_fs_handle_request(device_t dev,
477 const void *preq, void **pptr, uint16_t *plen,
478 uint16_t offset, uint8_t *pstate)
480 struct ustorage_fs_softc *sc = device_get_softc(dev);
481 const struct usb_device_request *req = preq;
482 uint8_t is_complete = *pstate;
485 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
486 (req->bRequest == UR_BBB_RESET)) {
488 mtx_lock(&sc->sc_mtx);
489 ustorage_fs_transfer_stop(sc);
490 sc->sc_transfer.data_error = 1;
491 ustorage_fs_transfer_start(sc,
492 USTORAGE_FS_T_BBB_COMMAND);
493 mtx_unlock(&sc->sc_mtx);
495 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
496 (req->bRequest == UR_BBB_GET_MAX_LUN)) {
499 *pptr = &sc->sc_last_lun;
506 return (ENXIO); /* use builtin handler */
510 ustorage_fs_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
512 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
518 switch (USB_GET_STATE(xfer)) {
519 case USB_ST_TRANSFERRED:
521 tag = UGETDW(sc->sc_cbw.dCBWSignature);
523 if (tag != CBWSIGNATURE) {
525 DPRINTF("invalid signature 0x%08x\n", tag);
528 tag = UGETDW(sc->sc_cbw.dCBWTag);
531 USETDW(sc->sc_csw.dCSWTag, tag);
534 sc->sc_csw.bCSWStatus = 0;
536 /* reset data offset, data length and data remainder */
537 sc->sc_transfer.offset = 0;
538 sc->sc_transfer.data_rem =
539 UGETDW(sc->sc_cbw.dCBWDataTransferLength);
541 /* reset data flags */
542 sc->sc_transfer.data_short = 0;
545 sc->sc_transfer.lun = sc->sc_cbw.bCBWLUN;
547 if (sc->sc_transfer.data_rem == 0) {
548 sc->sc_transfer.cbw_dir = DIR_NONE;
550 if (sc->sc_cbw.bCBWFlags & CBWFLAGS_IN) {
551 sc->sc_transfer.cbw_dir = DIR_WRITE;
553 sc->sc_transfer.cbw_dir = DIR_READ;
557 sc->sc_transfer.cmd_len = sc->sc_cbw.bCDBLength;
558 if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw.CBWCDB)) ||
559 (sc->sc_transfer.cmd_len == 0)) {
560 /* just halt - this is invalid */
561 DPRINTF("invalid command length %d bytes\n",
562 sc->sc_transfer.cmd_len);
566 err = ustorage_fs_do_cmd(sc);
569 DPRINTF("command failed\n");
572 if ((sc->sc_transfer.data_rem > 0) &&
573 (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
574 /* contradicting data transfer direction */
576 DPRINTF("data direction mismatch\n");
579 switch (sc->sc_transfer.cbw_dir) {
581 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
584 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
587 ustorage_fs_transfer_start(sc,
588 USTORAGE_FS_T_BBB_STATUS);
595 if (sc->sc_transfer.data_error) {
596 sc->sc_transfer.data_error = 0;
597 usbd_xfer_set_stall(xfer);
598 DPRINTF("stall pipe\n");
601 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_cbw,
603 usbd_transfer_submit(xfer);
608 if (error == USB_ERR_CANCELLED) {
611 /* If the pipe is already stalled, don't do another stall */
612 if (!usbd_xfer_is_stalled(xfer))
613 sc->sc_transfer.data_error = 1;
619 if (sc->sc_csw.bCSWStatus == 0) {
620 /* set some default error code */
621 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
623 if (sc->sc_transfer.cbw_dir == DIR_READ) {
625 ustorage_fs_transfer_start(sc,
626 USTORAGE_FS_T_BBB_DATA_DUMP);
629 if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
630 /* need to stall before status */
631 sc->sc_transfer.data_error = 1;
633 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
638 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer *xfer, usb_error_t error)
640 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
641 uint32_t max_bulk = usbd_xfer_max_len(xfer);
644 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
648 switch (USB_GET_STATE(xfer)) {
649 case USB_ST_TRANSFERRED:
650 sc->sc_transfer.data_rem -= actlen;
651 sc->sc_transfer.offset += actlen;
653 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
654 /* short transfer or end of data */
655 ustorage_fs_transfer_start(sc,
656 USTORAGE_FS_T_BBB_STATUS);
663 if (max_bulk > sc->sc_transfer.data_rem) {
664 max_bulk = sc->sc_transfer.data_rem;
666 if (sc->sc_transfer.data_error) {
667 sc->sc_transfer.data_error = 0;
668 usbd_xfer_set_stall(xfer);
670 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
671 usbd_transfer_submit(xfer);
675 if (error == USB_ERR_CANCELLED) {
679 * If the pipe is already stalled, don't do another stall:
681 if (!usbd_xfer_is_stalled(xfer))
682 sc->sc_transfer.data_error = 1;
690 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
692 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
693 uint32_t max_bulk = usbd_xfer_max_len(xfer);
696 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
700 switch (USB_GET_STATE(xfer)) {
701 case USB_ST_TRANSFERRED:
702 sc->sc_transfer.data_rem -= actlen;
703 sc->sc_transfer.data_ptr += actlen;
704 sc->sc_transfer.offset += actlen;
706 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
707 /* short transfer or end of data */
708 ustorage_fs_transfer_start(sc,
709 USTORAGE_FS_T_BBB_STATUS);
716 if (max_bulk > sc->sc_transfer.data_rem) {
717 max_bulk = sc->sc_transfer.data_rem;
719 if (sc->sc_transfer.data_error) {
720 sc->sc_transfer.data_error = 0;
721 usbd_xfer_set_stall(xfer);
724 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
726 usbd_transfer_submit(xfer);
730 if (error == USB_ERR_CANCELLED) {
733 /* If the pipe is already stalled, don't do another stall */
734 if (!usbd_xfer_is_stalled(xfer))
735 sc->sc_transfer.data_error = 1;
743 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
745 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
746 uint32_t max_bulk = usbd_xfer_max_len(xfer);
749 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
753 switch (USB_GET_STATE(xfer)) {
754 case USB_ST_TRANSFERRED:
755 sc->sc_transfer.data_rem -= actlen;
756 sc->sc_transfer.data_ptr += actlen;
757 sc->sc_transfer.offset += actlen;
759 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
760 /* short transfer or end of data */
761 ustorage_fs_transfer_start(sc,
762 USTORAGE_FS_T_BBB_STATUS);
767 if (max_bulk >= sc->sc_transfer.data_rem) {
768 max_bulk = sc->sc_transfer.data_rem;
769 if (sc->sc_transfer.data_short)
770 usbd_xfer_set_flag(xfer, USB_FORCE_SHORT_XFER);
772 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
774 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
776 if (sc->sc_transfer.data_error) {
777 sc->sc_transfer.data_error = 0;
778 usbd_xfer_set_stall(xfer);
781 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
783 usbd_transfer_submit(xfer);
787 if (error == USB_ERR_CANCELLED) {
791 * If the pipe is already stalled, don't do another
794 if (!usbd_xfer_is_stalled(xfer))
795 sc->sc_transfer.data_error = 1;
803 ustorage_fs_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
805 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
809 switch (USB_GET_STATE(xfer)) {
810 case USB_ST_TRANSFERRED:
811 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
816 USETDW(sc->sc_csw.dCSWSignature, CSWSIGNATURE);
817 USETDW(sc->sc_csw.dCSWDataResidue, sc->sc_transfer.data_rem);
819 if (sc->sc_transfer.data_error) {
820 sc->sc_transfer.data_error = 0;
821 usbd_xfer_set_stall(xfer);
824 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_csw,
826 usbd_transfer_submit(xfer);
830 if (error == USB_ERR_CANCELLED) {
833 /* If the pipe is already stalled, don't do another stall */
834 if (!usbd_xfer_is_stalled(xfer))
835 sc->sc_transfer.data_error = 1;
842 /* SCSI commands that we recognize */
843 #define SC_FORMAT_UNIT 0x04
844 #define SC_INQUIRY 0x12
845 #define SC_MODE_SELECT_6 0x15
846 #define SC_MODE_SELECT_10 0x55
847 #define SC_MODE_SENSE_6 0x1a
848 #define SC_MODE_SENSE_10 0x5a
849 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
850 #define SC_READ_6 0x08
851 #define SC_READ_10 0x28
852 #define SC_READ_12 0xa8
853 #define SC_READ_CAPACITY 0x25
854 #define SC_READ_FORMAT_CAPACITIES 0x23
855 #define SC_RELEASE 0x17
856 #define SC_REQUEST_SENSE 0x03
857 #define SC_RESERVE 0x16
858 #define SC_SEND_DIAGNOSTIC 0x1d
859 #define SC_START_STOP_UNIT 0x1b
860 #define SC_SYNCHRONIZE_CACHE 0x35
861 #define SC_TEST_UNIT_READY 0x00
862 #define SC_VERIFY 0x2f
863 #define SC_WRITE_6 0x0a
864 #define SC_WRITE_10 0x2a
865 #define SC_WRITE_12 0xaa
867 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
868 #define SS_NO_SENSE 0
869 #define SS_COMMUNICATION_FAILURE 0x040800
870 #define SS_INVALID_COMMAND 0x052000
871 #define SS_INVALID_FIELD_IN_CDB 0x052400
872 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
873 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
874 #define SS_MEDIUM_NOT_PRESENT 0x023a00
875 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
876 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
877 #define SS_RESET_OCCURRED 0x062900
878 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
879 #define SS_UNRECOVERED_READ_ERROR 0x031100
880 #define SS_WRITE_ERROR 0x030c02
881 #define SS_WRITE_PROTECTED 0x072700
883 #define SK(x) ((uint8_t) ((x) >> 16)) /* Sense Key byte, etc. */
884 #define ASC(x) ((uint8_t) ((x) >> 8))
885 #define ASCQ(x) ((uint8_t) (x))
887 /* Routines for unaligned data access */
890 get_be16(uint8_t *buf)
892 return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
896 get_be32(uint8_t *buf)
898 return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
899 ((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
903 put_be16(uint8_t *buf, uint16_t val)
910 put_be32(uint8_t *buf, uint32_t val)
918 /*------------------------------------------------------------------------*
924 *------------------------------------------------------------------------*/
926 ustorage_fs_verify(struct ustorage_fs_softc *sc)
928 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
931 uint64_t file_offset;
932 uint64_t amount_left;
935 * Get the starting Logical Block Address
937 lba = get_be32(&sc->sc_cmd_data[2]);
940 * We allow DPO (Disable Page Out = don't save data in the cache)
941 * but we don't implement it.
943 if ((sc->sc_cmd_data[1] & ~0x10) != 0) {
944 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
947 vlen = get_be16(&sc->sc_cmd_data[7]);
951 /* No default reply */
953 /* Prepare to carry out the file verify */
963 (vlen > currlun->num_sectors) ||
964 (lba >= currlun->num_sectors)) {
965 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
968 /* XXX TODO: verify that data is readable */
970 return (ustorage_fs_min_len(sc, 0, -1U));
973 /*------------------------------------------------------------------------*
974 * ustorage_fs_inquiry
979 *------------------------------------------------------------------------*/
981 ustorage_fs_inquiry(struct ustorage_fs_softc *sc)
983 uint8_t *buf = sc->sc_transfer.data_ptr;
985 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
987 if (!sc->sc_transfer.currlun) {
988 /* Unsupported LUNs are okay */
991 /* Unsupported, no device - type */
992 return (ustorage_fs_min_len(sc, 36, -1U));
995 /* Non - removable, direct - access device */
996 if (currlun->removable)
999 /* ANSI SCSI level 2 */
1001 /* SCSI - 2 INQUIRY data format */
1003 /* Additional length */
1004 /* No special options */
1005 /* Copy in ID string */
1006 memcpy(buf + 8, USTORAGE_FS_ID_STRING, 28);
1008 #if (USTORAGE_QDATA_MAX < 36)
1009 #error "(USTORAGE_QDATA_MAX < 36)"
1011 return (ustorage_fs_min_len(sc, 36, -1U));
1014 /*------------------------------------------------------------------------*
1015 * ustorage_fs_request_sense
1020 *------------------------------------------------------------------------*/
1022 ustorage_fs_request_sense(struct ustorage_fs_softc *sc)
1024 uint8_t *buf = sc->sc_transfer.data_ptr;
1025 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1031 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1033 * If a REQUEST SENSE command is received from an initiator
1034 * with a pending unit attention condition (before the target
1035 * generates the contingent allegiance condition), then the
1036 * target shall either:
1037 * a) report any pending sense data and preserve the unit
1038 * attention condition on the logical unit, or,
1039 * b) report the unit attention condition, may discard any
1040 * pending sense data, and clear the unit attention
1041 * condition on the logical unit for that initiator.
1043 * FSG normally uses option a); enable this code to use option b).
1046 if (currlun && currlun->unit_attention_data != SS_NO_SENSE) {
1047 currlun->sense_data = currlun->unit_attention_data;
1048 currlun->unit_attention_data = SS_NO_SENSE;
1053 /* Unsupported LUNs are okay */
1054 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1058 sd = currlun->sense_data;
1059 sdinfo = currlun->sense_data_info;
1060 valid = currlun->info_valid << 7;
1061 currlun->sense_data = SS_NO_SENSE;
1062 currlun->sense_data_info = 0;
1063 currlun->info_valid = 0;
1067 buf[0] = valid | 0x70;
1068 /* Valid, current error */
1070 put_be32(&buf[3], sdinfo);
1071 /* Sense information */
1073 /* Additional sense length */
1077 #if (USTORAGE_QDATA_MAX < 18)
1078 #error "(USTORAGE_QDATA_MAX < 18)"
1080 return (ustorage_fs_min_len(sc, 18, -1U));
1083 /*------------------------------------------------------------------------*
1084 * ustorage_fs_read_capacity
1089 *------------------------------------------------------------------------*/
1091 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1093 uint8_t *buf = sc->sc_transfer.data_ptr;
1094 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1095 uint32_t lba = get_be32(&sc->sc_cmd_data[2]);
1096 uint8_t pmi = sc->sc_cmd_data[8];
1098 /* Check the PMI and LBA fields */
1099 if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1100 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1103 /* Max logical block */
1104 put_be32(&buf[0], currlun->num_sectors - 1);
1106 put_be32(&buf[4], 512);
1108 #if (USTORAGE_QDATA_MAX < 8)
1109 #error "(USTORAGE_QDATA_MAX < 8)"
1111 return (ustorage_fs_min_len(sc, 8, -1U));
1114 /*------------------------------------------------------------------------*
1115 * ustorage_fs_mode_sense
1120 *------------------------------------------------------------------------*/
1122 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1124 uint8_t *buf = sc->sc_transfer.data_ptr;
1125 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1129 uint8_t mscmnd = sc->sc_cmd_data[0];
1132 uint8_t changeable_values;
1137 if ((sc->sc_cmd_data[1] & ~0x08) != 0) {
1139 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1142 pc = sc->sc_cmd_data[2] >> 6;
1143 page_code = sc->sc_cmd_data[2] & 0x3f;
1145 currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1148 changeable_values = (pc == 1);
1149 all_pages = (page_code == 0x3f);
1152 * Write the mode parameter header. Fixed values are: default
1153 * medium type, no cache control (DPOFUA), and no block descriptors.
1154 * The only variable value is the WriteProtect bit. We will fill in
1155 * the mode data length later.
1158 if (mscmnd == SC_MODE_SENSE_6) {
1159 buf[2] = (currlun->read_only ? 0x80 : 0x00);
1164 /* SC_MODE_SENSE_10 */
1165 buf[3] = (currlun->read_only ? 0x80 : 0x00);
1169 /* Should really be mod_data.buflen */
1172 /* No block descriptors */
1175 * The mode pages, in numerical order.
1177 if ((page_code == 0x08) || all_pages) {
1182 memset(buf + 2, 0, 10);
1183 /* None of the fields are changeable */
1185 if (!changeable_values) {
1187 /* Write cache enable, */
1188 /* Read cache not disabled */
1189 /* No cache retention priorities */
1190 put_be16(&buf[4], 0xffff);
1191 /* Don 't disable prefetch */
1192 /* Minimum prefetch = 0 */
1193 put_be16(&buf[8], 0xffff);
1194 /* Maximum prefetch */
1195 put_be16(&buf[10], 0xffff);
1196 /* Maximum prefetch ceiling */
1201 * Check that a valid page was requested and the mode data length
1206 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1209 /* Store the mode data length */
1210 if (mscmnd == SC_MODE_SENSE_6)
1213 put_be16(buf0, len - 2);
1215 #if (USTORAGE_QDATA_MAX < 24)
1216 #error "(USTORAGE_QDATA_MAX < 24)"
1218 return (ustorage_fs_min_len(sc, len, -1U));
1221 /*------------------------------------------------------------------------*
1222 * ustorage_fs_start_stop
1227 *------------------------------------------------------------------------*/
1229 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1231 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1236 if (!currlun->removable) {
1237 currlun->sense_data = SS_INVALID_COMMAND;
1240 immed = sc->sc_cmd_data[1] & 0x01;
1241 loej = sc->sc_cmd_data[4] & 0x02;
1242 start = sc->sc_cmd_data[4] & 0x01;
1244 if (immed || loej || start) {
1250 /*------------------------------------------------------------------------*
1251 * ustorage_fs_prevent_allow
1256 *------------------------------------------------------------------------*/
1258 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1260 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1263 if (!currlun->removable) {
1264 currlun->sense_data = SS_INVALID_COMMAND;
1267 prevent = sc->sc_cmd_data[4] & 0x01;
1268 if ((sc->sc_cmd_data[4] & ~0x01) != 0) {
1269 /* Mask away Prevent */
1270 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1273 if (currlun->prevent_medium_removal && !prevent) {
1274 //fsync_sub(currlun);
1276 currlun->prevent_medium_removal = prevent;
1280 /*------------------------------------------------------------------------*
1281 * ustorage_fs_read_format_capacities
1286 *------------------------------------------------------------------------*/
1288 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1290 uint8_t *buf = sc->sc_transfer.data_ptr;
1291 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1293 buf[0] = buf[1] = buf[2] = 0;
1295 /* Only the Current / Maximum Capacity Descriptor */
1298 /* Number of blocks */
1299 put_be32(&buf[0], currlun->num_sectors);
1301 put_be32(&buf[4], 512);
1302 /* Current capacity */
1305 #if (USTORAGE_QDATA_MAX < 12)
1306 #error "(USTORAGE_QDATA_MAX < 12)"
1308 return (ustorage_fs_min_len(sc, 12, -1U));
1311 /*------------------------------------------------------------------------*
1312 * ustorage_fs_mode_select
1317 *------------------------------------------------------------------------*/
1319 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1321 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1323 /* We don't support MODE SELECT */
1324 currlun->sense_data = SS_INVALID_COMMAND;
1328 /*------------------------------------------------------------------------*
1329 * ustorage_fs_synchronize_cache
1334 *------------------------------------------------------------------------*/
1336 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1339 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1343 * We ignore the requested LBA and write out all dirty data buffers.
1347 currlun->sense_data = SS_WRITE_ERROR;
1353 /*------------------------------------------------------------------------*
1354 * ustorage_fs_read - read data from disk
1359 *------------------------------------------------------------------------*/
1361 ustorage_fs_read(struct ustorage_fs_softc *sc)
1363 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1364 uint64_t file_offset;
1369 * Get the starting Logical Block Address and check that it's not
1372 if (sc->sc_cmd_data[0] == SC_READ_6) {
1373 lba = (((uint32_t)sc->sc_cmd_data[1]) << 16) |
1374 get_be16(&sc->sc_cmd_data[2]);
1376 lba = get_be32(&sc->sc_cmd_data[2]);
1379 * We allow DPO (Disable Page Out = don't save data in the
1380 * cache) and FUA (Force Unit Access = don't read from the
1381 * cache), but we don't implement them.
1383 if ((sc->sc_cmd_data[1] & ~0x18) != 0) {
1384 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1388 len = sc->sc_transfer.data_rem >> 9;
1392 (len > currlun->num_sectors) ||
1393 (lba >= currlun->num_sectors)) {
1394 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1400 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1405 /*------------------------------------------------------------------------*
1406 * ustorage_fs_write - write data to disk
1411 *------------------------------------------------------------------------*/
1413 ustorage_fs_write(struct ustorage_fs_softc *sc)
1415 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1416 uint64_t file_offset;
1420 if (currlun->read_only) {
1421 currlun->sense_data = SS_WRITE_PROTECTED;
1424 /* XXX clear SYNC */
1427 * Get the starting Logical Block Address and check that it's not
1430 if (sc->sc_cmd_data[0] == SC_WRITE_6)
1431 lba = (((uint32_t)sc->sc_cmd_data[1]) << 16) |
1432 get_be16(&sc->sc_cmd_data[2]);
1434 lba = get_be32(&sc->sc_cmd_data[2]);
1437 * We allow DPO (Disable Page Out = don't save data in the
1438 * cache) and FUA (Force Unit Access = write directly to the
1439 * medium). We don't implement DPO; we implement FUA by
1440 * performing synchronous output.
1442 if ((sc->sc_cmd_data[1] & ~0x18) != 0) {
1443 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1446 if (sc->sc_cmd_data[1] & 0x08) {
1448 /* XXX set SYNC flag here */
1452 len = sc->sc_transfer.data_rem >> 9;
1456 (len > currlun->num_sectors) ||
1457 (lba >= currlun->num_sectors)) {
1458 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1464 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1469 /*------------------------------------------------------------------------*
1470 * ustorage_fs_min_len
1475 *------------------------------------------------------------------------*/
1477 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1479 if (len != sc->sc_transfer.data_rem) {
1481 if (sc->sc_transfer.cbw_dir == DIR_READ) {
1483 * there must be something wrong about this SCSI
1486 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1489 /* compute the minimum length */
1491 if (sc->sc_transfer.data_rem > len) {
1492 /* data ends prematurely */
1493 sc->sc_transfer.data_rem = len;
1494 sc->sc_transfer.data_short = 1;
1496 /* check length alignment */
1498 if (sc->sc_transfer.data_rem & ~mask) {
1499 /* data ends prematurely */
1500 sc->sc_transfer.data_rem &= mask;
1501 sc->sc_transfer.data_short = 1;
1507 /*------------------------------------------------------------------------*
1508 * ustorage_fs_check_cmd - check command routine
1510 * Check whether the command is properly formed and whether its data
1511 * size and direction agree with the values we already have.
1516 *------------------------------------------------------------------------*/
1518 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1519 uint16_t mask, uint8_t needs_medium)
1521 struct ustorage_fs_lun *currlun;
1522 uint8_t lun = (sc->sc_cmd_data[1] >> 5);
1525 /* Verify the length of the command itself */
1526 if (min_cmd_size > sc->sc_transfer.cmd_len) {
1527 DPRINTF("%u > %u\n",
1528 min_cmd_size, sc->sc_transfer.cmd_len);
1529 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1532 /* Mask away the LUN */
1533 sc->sc_cmd_data[1] &= 0x1f;
1535 /* Check if LUN is correct */
1536 if (lun != sc->sc_transfer.lun) {
1540 if (sc->sc_transfer.lun <= sc->sc_last_lun) {
1541 sc->sc_transfer.currlun = currlun =
1542 sc->sc_lun + sc->sc_transfer.lun;
1543 if (sc->sc_cmd_data[0] != SC_REQUEST_SENSE) {
1544 currlun->sense_data = SS_NO_SENSE;
1545 currlun->sense_data_info = 0;
1546 currlun->info_valid = 0;
1549 * If a unit attention condition exists, only INQUIRY
1550 * and REQUEST SENSE commands are allowed. Anything
1553 if ((currlun->unit_attention_data != SS_NO_SENSE) &&
1554 (sc->sc_cmd_data[0] != SC_INQUIRY) &&
1555 (sc->sc_cmd_data[0] != SC_REQUEST_SENSE)) {
1556 currlun->sense_data = currlun->unit_attention_data;
1557 currlun->unit_attention_data = SS_NO_SENSE;
1561 sc->sc_transfer.currlun = currlun = NULL;
1564 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1565 * to use unsupported LUNs; all others may not.
1567 if ((sc->sc_cmd_data[0] != SC_INQUIRY) &&
1568 (sc->sc_cmd_data[0] != SC_REQUEST_SENSE)) {
1574 * Check that only command bytes listed in the mask are
1577 for (i = 0; i != min_cmd_size; i++) {
1578 if (sc->sc_cmd_data[i] && !(mask & (1UL << i))) {
1580 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1587 * If the medium isn't mounted and the command needs to access
1588 * it, return an error.
1590 if (currlun && (!currlun->memory_image) && needs_medium) {
1591 currlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1597 /*------------------------------------------------------------------------*
1598 * ustorage_fs_do_cmd - do command
1603 *------------------------------------------------------------------------*/
1605 ustorage_fs_do_cmd(struct ustorage_fs_softc *sc)
1610 const uint32_t mask9 = (0xFFFFFFFFUL >> 9) << 9;
1612 /* set default data transfer pointer */
1613 sc->sc_transfer.data_ptr = sc->sc_qdata;
1615 DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1616 sc->sc_cmd_data[0], sc->sc_transfer.data_rem);
1618 switch (sc->sc_cmd_data[0]) {
1620 sc->sc_transfer.cmd_dir = DIR_WRITE;
1621 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], -1U);
1625 error = ustorage_fs_check_cmd(sc, 6,
1630 error = ustorage_fs_inquiry(sc);
1634 case SC_MODE_SELECT_6:
1635 sc->sc_transfer.cmd_dir = DIR_READ;
1636 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], -1U);
1640 error = ustorage_fs_check_cmd(sc, 6,
1641 (1UL << 1) | (1UL << 4) | 1, 0);
1645 error = ustorage_fs_mode_select(sc);
1649 case SC_MODE_SELECT_10:
1650 sc->sc_transfer.cmd_dir = DIR_READ;
1651 error = ustorage_fs_min_len(sc,
1652 get_be16(&sc->sc_cmd_data[7]), -1U);
1656 error = ustorage_fs_check_cmd(sc, 10,
1657 (1UL << 1) | (3UL << 7) | 1, 0);
1661 error = ustorage_fs_mode_select(sc);
1665 case SC_MODE_SENSE_6:
1666 sc->sc_transfer.cmd_dir = DIR_WRITE;
1667 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], -1U);
1671 error = ustorage_fs_check_cmd(sc, 6,
1672 (1UL << 1) | (1UL << 2) | (1UL << 4) | 1, 0);
1676 error = ustorage_fs_mode_sense(sc);
1680 case SC_MODE_SENSE_10:
1681 sc->sc_transfer.cmd_dir = DIR_WRITE;
1682 error = ustorage_fs_min_len(sc,
1683 get_be16(&sc->sc_cmd_data[7]), -1U);
1687 error = ustorage_fs_check_cmd(sc, 10,
1688 (1UL << 1) | (1UL << 2) | (3UL << 7) | 1, 0);
1692 error = ustorage_fs_mode_sense(sc);
1696 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1697 error = ustorage_fs_min_len(sc, 0, -1U);
1701 error = ustorage_fs_check_cmd(sc, 6,
1706 error = ustorage_fs_prevent_allow(sc);
1711 i = sc->sc_cmd_data[4];
1712 sc->sc_transfer.cmd_dir = DIR_WRITE;
1713 temp = ((i == 0) ? 256UL : i);
1714 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1718 error = ustorage_fs_check_cmd(sc, 6,
1719 (7UL << 1) | (1UL << 4) | 1, 1);
1723 error = ustorage_fs_read(sc);
1728 sc->sc_transfer.cmd_dir = DIR_WRITE;
1729 temp = get_be16(&sc->sc_cmd_data[7]);
1730 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1734 error = ustorage_fs_check_cmd(sc, 10,
1735 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1739 error = ustorage_fs_read(sc);
1744 sc->sc_transfer.cmd_dir = DIR_WRITE;
1745 temp = get_be32(&sc->sc_cmd_data[6]);
1746 if (temp >= (1UL << (32 - 9))) {
1747 /* numerical overflow */
1748 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
1752 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1756 error = ustorage_fs_check_cmd(sc, 12,
1757 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1761 error = ustorage_fs_read(sc);
1765 case SC_READ_CAPACITY:
1766 sc->sc_transfer.cmd_dir = DIR_WRITE;
1767 error = ustorage_fs_check_cmd(sc, 10,
1768 (0xfUL << 2) | (1UL << 8) | 1, 1);
1772 error = ustorage_fs_read_capacity(sc);
1776 case SC_READ_FORMAT_CAPACITIES:
1777 sc->sc_transfer.cmd_dir = DIR_WRITE;
1778 error = ustorage_fs_min_len(sc,
1779 get_be16(&sc->sc_cmd_data[7]), -1U);
1783 error = ustorage_fs_check_cmd(sc, 10,
1788 error = ustorage_fs_read_format_capacities(sc);
1792 case SC_REQUEST_SENSE:
1793 sc->sc_transfer.cmd_dir = DIR_WRITE;
1794 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], -1U);
1798 error = ustorage_fs_check_cmd(sc, 6,
1803 error = ustorage_fs_request_sense(sc);
1807 case SC_START_STOP_UNIT:
1808 error = ustorage_fs_min_len(sc, 0, -1U);
1812 error = ustorage_fs_check_cmd(sc, 6,
1813 (1UL << 1) | (1UL << 4) | 1, 0);
1817 error = ustorage_fs_start_stop(sc);
1821 case SC_SYNCHRONIZE_CACHE:
1822 error = ustorage_fs_min_len(sc, 0, -1U);
1826 error = ustorage_fs_check_cmd(sc, 10,
1827 (0xfUL << 2) | (3UL << 7) | 1, 1);
1831 error = ustorage_fs_synchronize_cache(sc);
1835 case SC_TEST_UNIT_READY:
1836 error = ustorage_fs_min_len(sc, 0, -1U);
1840 error = ustorage_fs_check_cmd(sc, 6,
1845 * Although optional, this command is used by MS-Windows.
1846 * We support a minimal version: BytChk must be 0.
1849 error = ustorage_fs_min_len(sc, 0, -1U);
1853 error = ustorage_fs_check_cmd(sc, 10,
1854 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1858 error = ustorage_fs_verify(sc);
1863 i = sc->sc_cmd_data[4];
1864 sc->sc_transfer.cmd_dir = DIR_READ;
1865 temp = ((i == 0) ? 256UL : i);
1866 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1870 error = ustorage_fs_check_cmd(sc, 6,
1871 (7UL << 1) | (1UL << 4) | 1, 1);
1875 error = ustorage_fs_write(sc);
1880 sc->sc_transfer.cmd_dir = DIR_READ;
1881 temp = get_be16(&sc->sc_cmd_data[7]);
1882 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1886 error = ustorage_fs_check_cmd(sc, 10,
1887 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1891 error = ustorage_fs_write(sc);
1896 sc->sc_transfer.cmd_dir = DIR_READ;
1897 temp = get_be32(&sc->sc_cmd_data[6]);
1898 if (temp > (mask9 >> 9)) {
1899 /* numerical overflow */
1900 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
1904 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1908 error = ustorage_fs_check_cmd(sc, 12,
1909 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1913 error = ustorage_fs_write(sc);
1918 * Some mandatory commands that we recognize but don't
1919 * implement. They don't mean much in this setting.
1920 * It's left as an exercise for anyone interested to
1921 * implement RESERVE and RELEASE in terms of Posix
1924 case SC_FORMAT_UNIT:
1927 case SC_SEND_DIAGNOSTIC:
1931 error = ustorage_fs_min_len(sc, 0, -1U);
1935 error = ustorage_fs_check_cmd(sc, sc->sc_transfer.cmd_len,
1940 sc->sc_transfer.currlun->sense_data =