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);
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,
377 if (ustorage_fs_ramdisk == NULL) {
381 sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
382 sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
383 sc->sc_lun[0].removable = 1;
386 device_set_usb_desc(dev);
388 mtx_init(&sc->sc_mtx, "USTORAGE_FS lock",
389 NULL, (MTX_DEF | MTX_RECURSE));
391 /* get interface index */
393 id = usbd_get_interface_descriptor(uaa->iface);
395 device_printf(dev, "failed to get "
396 "interface number\n");
399 sc->sc_iface_no = id->bInterfaceNumber;
401 err = usbd_transfer_setup(uaa->device,
402 &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
403 USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_mtx);
405 device_printf(dev, "could not setup required "
406 "transfers, %s\n", usbd_errstr(err));
409 /* start Mass Storage State Machine */
411 mtx_lock(&sc->sc_mtx);
412 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
413 mtx_unlock(&sc->sc_mtx);
415 return (0); /* success */
418 ustorage_fs_detach(dev);
419 return (ENXIO); /* failure */
423 ustorage_fs_detach(device_t dev)
425 struct ustorage_fs_softc *sc = device_get_softc(dev);
427 /* teardown our statemachine */
429 usbd_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
431 mtx_destroy(&sc->sc_mtx);
433 return (0); /* success */
437 ustorage_fs_suspend(device_t dev)
439 device_printf(dev, "suspending\n");
440 return (0); /* success */
444 ustorage_fs_resume(device_t dev)
446 device_printf(dev, "resuming\n");
447 return (0); /* success */
451 * Generic functions to handle transfers
455 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
457 if (sc->sc_xfer[xfer_index]) {
458 sc->sc_last_xfer_index = xfer_index;
459 usbd_transfer_start(sc->sc_xfer[xfer_index]);
464 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
466 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
467 mtx_unlock(&sc->sc_mtx);
468 usbd_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
469 mtx_lock(&sc->sc_mtx);
473 ustorage_fs_handle_request(device_t dev,
474 const void *preq, void **pptr, uint16_t *plen,
475 uint16_t offset, uint8_t *pstate)
477 struct ustorage_fs_softc *sc = device_get_softc(dev);
478 const struct usb_device_request *req = preq;
479 uint8_t is_complete = *pstate;
482 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
483 (req->bRequest == UR_BBB_RESET)) {
485 mtx_lock(&sc->sc_mtx);
486 ustorage_fs_transfer_stop(sc);
487 sc->sc_transfer.data_error = 1;
488 ustorage_fs_transfer_start(sc,
489 USTORAGE_FS_T_BBB_COMMAND);
490 mtx_unlock(&sc->sc_mtx);
492 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
493 (req->bRequest == UR_BBB_GET_MAX_LUN)) {
496 *pptr = &sc->sc_last_lun;
503 return (ENXIO); /* use builtin handler */
507 ustorage_fs_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
509 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
515 switch (USB_GET_STATE(xfer)) {
516 case USB_ST_TRANSFERRED:
518 tag = UGETDW(sc->sc_cbw.dCBWSignature);
520 if (tag != CBWSIGNATURE) {
522 DPRINTF("invalid signature 0x%08x\n", tag);
525 tag = UGETDW(sc->sc_cbw.dCBWTag);
528 USETDW(sc->sc_csw.dCSWTag, tag);
531 sc->sc_csw.bCSWStatus = 0;
533 /* reset data offset, data length and data remainder */
534 sc->sc_transfer.offset = 0;
535 sc->sc_transfer.data_rem =
536 UGETDW(sc->sc_cbw.dCBWDataTransferLength);
538 /* reset data flags */
539 sc->sc_transfer.data_short = 0;
542 sc->sc_transfer.lun = sc->sc_cbw.bCBWLUN;
544 if (sc->sc_transfer.data_rem == 0) {
545 sc->sc_transfer.cbw_dir = DIR_NONE;
547 if (sc->sc_cbw.bCBWFlags & CBWFLAGS_IN) {
548 sc->sc_transfer.cbw_dir = DIR_WRITE;
550 sc->sc_transfer.cbw_dir = DIR_READ;
554 sc->sc_transfer.cmd_len = sc->sc_cbw.bCDBLength;
555 if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw.CBWCDB)) ||
556 (sc->sc_transfer.cmd_len == 0)) {
557 /* just halt - this is invalid */
558 DPRINTF("invalid command length %d bytes\n",
559 sc->sc_transfer.cmd_len);
563 err = ustorage_fs_do_cmd(sc);
566 DPRINTF("command failed\n");
569 if ((sc->sc_transfer.data_rem > 0) &&
570 (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
571 /* contradicting data transfer direction */
573 DPRINTF("data direction mismatch\n");
576 switch (sc->sc_transfer.cbw_dir) {
578 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
581 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
584 ustorage_fs_transfer_start(sc,
585 USTORAGE_FS_T_BBB_STATUS);
592 if (sc->sc_transfer.data_error) {
593 sc->sc_transfer.data_error = 0;
594 usbd_xfer_set_stall(xfer);
595 DPRINTF("stall pipe\n");
598 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_cbw,
600 usbd_transfer_submit(xfer);
605 if (error == USB_ERR_CANCELLED) {
608 /* If the pipe is already stalled, don't do another stall */
609 if (!usbd_xfer_is_stalled(xfer))
610 sc->sc_transfer.data_error = 1;
616 if (sc->sc_csw.bCSWStatus == 0) {
617 /* set some default error code */
618 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
620 if (sc->sc_transfer.cbw_dir == DIR_READ) {
622 ustorage_fs_transfer_start(sc,
623 USTORAGE_FS_T_BBB_DATA_DUMP);
626 if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
627 /* need to stall before status */
628 sc->sc_transfer.data_error = 1;
630 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
635 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer *xfer, usb_error_t error)
637 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
638 uint32_t max_bulk = usbd_xfer_max_len(xfer);
641 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
645 switch (USB_GET_STATE(xfer)) {
646 case USB_ST_TRANSFERRED:
647 sc->sc_transfer.data_rem -= actlen;
648 sc->sc_transfer.offset += actlen;
650 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
651 /* short transfer or end of data */
652 ustorage_fs_transfer_start(sc,
653 USTORAGE_FS_T_BBB_STATUS);
660 if (max_bulk > sc->sc_transfer.data_rem) {
661 max_bulk = sc->sc_transfer.data_rem;
663 if (sc->sc_transfer.data_error) {
664 sc->sc_transfer.data_error = 0;
665 usbd_xfer_set_stall(xfer);
667 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
668 usbd_transfer_submit(xfer);
672 if (error == USB_ERR_CANCELLED) {
676 * If the pipe is already stalled, don't do another stall:
678 if (!usbd_xfer_is_stalled(xfer))
679 sc->sc_transfer.data_error = 1;
687 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
689 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
690 uint32_t max_bulk = usbd_xfer_max_len(xfer);
693 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
697 switch (USB_GET_STATE(xfer)) {
698 case USB_ST_TRANSFERRED:
699 sc->sc_transfer.data_rem -= actlen;
700 sc->sc_transfer.data_ptr += actlen;
701 sc->sc_transfer.offset += actlen;
703 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
704 /* short transfer or end of data */
705 ustorage_fs_transfer_start(sc,
706 USTORAGE_FS_T_BBB_STATUS);
713 if (max_bulk > sc->sc_transfer.data_rem) {
714 max_bulk = sc->sc_transfer.data_rem;
716 if (sc->sc_transfer.data_error) {
717 sc->sc_transfer.data_error = 0;
718 usbd_xfer_set_stall(xfer);
721 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
723 usbd_transfer_submit(xfer);
727 if (error == USB_ERR_CANCELLED) {
730 /* If the pipe is already stalled, don't do another stall */
731 if (!usbd_xfer_is_stalled(xfer))
732 sc->sc_transfer.data_error = 1;
740 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
742 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
743 uint32_t max_bulk = usbd_xfer_max_len(xfer);
746 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
750 switch (USB_GET_STATE(xfer)) {
751 case USB_ST_TRANSFERRED:
752 sc->sc_transfer.data_rem -= actlen;
753 sc->sc_transfer.data_ptr += actlen;
754 sc->sc_transfer.offset += actlen;
756 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
757 /* short transfer or end of data */
758 ustorage_fs_transfer_start(sc,
759 USTORAGE_FS_T_BBB_STATUS);
764 if (max_bulk >= sc->sc_transfer.data_rem) {
765 max_bulk = sc->sc_transfer.data_rem;
766 if (sc->sc_transfer.data_short)
767 usbd_xfer_set_flag(xfer, USB_FORCE_SHORT_XFER);
769 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
771 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
773 if (sc->sc_transfer.data_error) {
774 sc->sc_transfer.data_error = 0;
775 usbd_xfer_set_stall(xfer);
778 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
780 usbd_transfer_submit(xfer);
784 if (error == USB_ERR_CANCELLED) {
788 * If the pipe is already stalled, don't do another
791 if (!usbd_xfer_is_stalled(xfer))
792 sc->sc_transfer.data_error = 1;
800 ustorage_fs_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
802 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
806 switch (USB_GET_STATE(xfer)) {
807 case USB_ST_TRANSFERRED:
808 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
813 USETDW(sc->sc_csw.dCSWSignature, CSWSIGNATURE);
814 USETDW(sc->sc_csw.dCSWDataResidue, sc->sc_transfer.data_rem);
816 if (sc->sc_transfer.data_error) {
817 sc->sc_transfer.data_error = 0;
818 usbd_xfer_set_stall(xfer);
821 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_csw,
823 usbd_transfer_submit(xfer);
827 if (error == USB_ERR_CANCELLED) {
830 /* If the pipe is already stalled, don't do another stall */
831 if (!usbd_xfer_is_stalled(xfer))
832 sc->sc_transfer.data_error = 1;
839 /* SCSI commands that we recognize */
840 #define SC_FORMAT_UNIT 0x04
841 #define SC_INQUIRY 0x12
842 #define SC_MODE_SELECT_6 0x15
843 #define SC_MODE_SELECT_10 0x55
844 #define SC_MODE_SENSE_6 0x1a
845 #define SC_MODE_SENSE_10 0x5a
846 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
847 #define SC_READ_6 0x08
848 #define SC_READ_10 0x28
849 #define SC_READ_12 0xa8
850 #define SC_READ_CAPACITY 0x25
851 #define SC_READ_FORMAT_CAPACITIES 0x23
852 #define SC_RELEASE 0x17
853 #define SC_REQUEST_SENSE 0x03
854 #define SC_RESERVE 0x16
855 #define SC_SEND_DIAGNOSTIC 0x1d
856 #define SC_START_STOP_UNIT 0x1b
857 #define SC_SYNCHRONIZE_CACHE 0x35
858 #define SC_TEST_UNIT_READY 0x00
859 #define SC_VERIFY 0x2f
860 #define SC_WRITE_6 0x0a
861 #define SC_WRITE_10 0x2a
862 #define SC_WRITE_12 0xaa
864 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
865 #define SS_NO_SENSE 0
866 #define SS_COMMUNICATION_FAILURE 0x040800
867 #define SS_INVALID_COMMAND 0x052000
868 #define SS_INVALID_FIELD_IN_CDB 0x052400
869 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
870 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
871 #define SS_MEDIUM_NOT_PRESENT 0x023a00
872 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
873 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
874 #define SS_RESET_OCCURRED 0x062900
875 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
876 #define SS_UNRECOVERED_READ_ERROR 0x031100
877 #define SS_WRITE_ERROR 0x030c02
878 #define SS_WRITE_PROTECTED 0x072700
880 #define SK(x) ((uint8_t) ((x) >> 16)) /* Sense Key byte, etc. */
881 #define ASC(x) ((uint8_t) ((x) >> 8))
882 #define ASCQ(x) ((uint8_t) (x))
884 /* Routines for unaligned data access */
887 get_be16(uint8_t *buf)
889 return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
893 get_be32(uint8_t *buf)
895 return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
896 ((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
900 put_be16(uint8_t *buf, uint16_t val)
907 put_be32(uint8_t *buf, uint32_t val)
915 /*------------------------------------------------------------------------*
921 *------------------------------------------------------------------------*/
923 ustorage_fs_verify(struct ustorage_fs_softc *sc)
925 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
928 uint64_t file_offset;
929 uint64_t amount_left;
932 * Get the starting Logical Block Address
934 lba = get_be32(&sc->sc_cmd_data[2]);
937 * We allow DPO (Disable Page Out = don't save data in the cache)
938 * but we don't implement it.
940 if ((sc->sc_cmd_data[1] & ~0x10) != 0) {
941 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
944 vlen = get_be16(&sc->sc_cmd_data[7]);
948 /* No default reply */
950 /* Prepare to carry out the file verify */
960 (vlen > currlun->num_sectors) ||
961 (lba >= currlun->num_sectors)) {
962 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
965 /* XXX TODO: verify that data is readable */
967 return (ustorage_fs_min_len(sc, 0, 0 - 1));
970 /*------------------------------------------------------------------------*
971 * ustorage_fs_inquiry
976 *------------------------------------------------------------------------*/
978 ustorage_fs_inquiry(struct ustorage_fs_softc *sc)
980 uint8_t *buf = sc->sc_transfer.data_ptr;
982 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
984 if (!sc->sc_transfer.currlun) {
985 /* Unsupported LUNs are okay */
988 /* Unsupported, no device - type */
989 return (ustorage_fs_min_len(sc, 36, 0 - 1));
992 /* Non - removable, direct - access device */
993 if (currlun->removable)
996 /* ANSI SCSI level 2 */
998 /* SCSI - 2 INQUIRY data format */
1000 /* Additional length */
1001 /* No special options */
1002 /* Copy in ID string */
1003 memcpy(buf + 8, USTORAGE_FS_ID_STRING, 28);
1005 #if (USTORAGE_QDATA_MAX < 36)
1006 #error "(USTORAGE_QDATA_MAX < 36)"
1008 return (ustorage_fs_min_len(sc, 36, 0 - 1));
1011 /*------------------------------------------------------------------------*
1012 * ustorage_fs_request_sense
1017 *------------------------------------------------------------------------*/
1019 ustorage_fs_request_sense(struct ustorage_fs_softc *sc)
1021 uint8_t *buf = sc->sc_transfer.data_ptr;
1022 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1028 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1030 * If a REQUEST SENSE command is received from an initiator
1031 * with a pending unit attention condition (before the target
1032 * generates the contingent allegiance condition), then the
1033 * target shall either:
1034 * a) report any pending sense data and preserve the unit
1035 * attention condition on the logical unit, or,
1036 * b) report the unit attention condition, may discard any
1037 * pending sense data, and clear the unit attention
1038 * condition on the logical unit for that initiator.
1040 * FSG normally uses option a); enable this code to use option b).
1043 if (currlun && currlun->unit_attention_data != SS_NO_SENSE) {
1044 currlun->sense_data = currlun->unit_attention_data;
1045 currlun->unit_attention_data = SS_NO_SENSE;
1050 /* Unsupported LUNs are okay */
1051 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1055 sd = currlun->sense_data;
1056 sdinfo = currlun->sense_data_info;
1057 valid = currlun->info_valid << 7;
1058 currlun->sense_data = SS_NO_SENSE;
1059 currlun->sense_data_info = 0;
1060 currlun->info_valid = 0;
1064 buf[0] = valid | 0x70;
1065 /* Valid, current error */
1067 put_be32(&buf[3], sdinfo);
1068 /* Sense information */
1070 /* Additional sense length */
1074 #if (USTORAGE_QDATA_MAX < 18)
1075 #error "(USTORAGE_QDATA_MAX < 18)"
1077 return (ustorage_fs_min_len(sc, 18, 0 - 1));
1080 /*------------------------------------------------------------------------*
1081 * ustorage_fs_read_capacity
1086 *------------------------------------------------------------------------*/
1088 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1090 uint8_t *buf = sc->sc_transfer.data_ptr;
1091 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1092 uint32_t lba = get_be32(&sc->sc_cmd_data[2]);
1093 uint8_t pmi = sc->sc_cmd_data[8];
1095 /* Check the PMI and LBA fields */
1096 if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1097 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1100 /* Max logical block */
1101 put_be32(&buf[0], currlun->num_sectors - 1);
1103 put_be32(&buf[4], 512);
1105 #if (USTORAGE_QDATA_MAX < 8)
1106 #error "(USTORAGE_QDATA_MAX < 8)"
1108 return (ustorage_fs_min_len(sc, 8, 0 - 1));
1111 /*------------------------------------------------------------------------*
1112 * ustorage_fs_mode_sense
1117 *------------------------------------------------------------------------*/
1119 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1121 uint8_t *buf = sc->sc_transfer.data_ptr;
1122 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1126 uint8_t mscmnd = sc->sc_cmd_data[0];
1129 uint8_t changeable_values;
1134 if ((sc->sc_cmd_data[1] & ~0x08) != 0) {
1136 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1139 pc = sc->sc_cmd_data[2] >> 6;
1140 page_code = sc->sc_cmd_data[2] & 0x3f;
1142 currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1145 changeable_values = (pc == 1);
1146 all_pages = (page_code == 0x3f);
1149 * Write the mode parameter header. Fixed values are: default
1150 * medium type, no cache control (DPOFUA), and no block descriptors.
1151 * The only variable value is the WriteProtect bit. We will fill in
1152 * the mode data length later.
1155 if (mscmnd == SC_MODE_SENSE_6) {
1156 buf[2] = (currlun->read_only ? 0x80 : 0x00);
1161 /* SC_MODE_SENSE_10 */
1162 buf[3] = (currlun->read_only ? 0x80 : 0x00);
1166 /* Should really be mod_data.buflen */
1169 /* No block descriptors */
1172 * The mode pages, in numerical order.
1174 if ((page_code == 0x08) || all_pages) {
1179 memset(buf + 2, 0, 10);
1180 /* None of the fields are changeable */
1182 if (!changeable_values) {
1184 /* Write cache enable, */
1185 /* Read cache not disabled */
1186 /* No cache retention priorities */
1187 put_be16(&buf[4], 0xffff);
1188 /* Don 't disable prefetch */
1189 /* Minimum prefetch = 0 */
1190 put_be16(&buf[8], 0xffff);
1191 /* Maximum prefetch */
1192 put_be16(&buf[10], 0xffff);
1193 /* Maximum prefetch ceiling */
1198 * Check that a valid page was requested and the mode data length
1203 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1206 /* Store the mode data length */
1207 if (mscmnd == SC_MODE_SENSE_6)
1210 put_be16(buf0, len - 2);
1212 #if (USTORAGE_QDATA_MAX < 24)
1213 #error "(USTORAGE_QDATA_MAX < 24)"
1215 return (ustorage_fs_min_len(sc, len, 0 - 1));
1218 /*------------------------------------------------------------------------*
1219 * ustorage_fs_start_stop
1224 *------------------------------------------------------------------------*/
1226 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1228 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1233 if (!currlun->removable) {
1234 currlun->sense_data = SS_INVALID_COMMAND;
1237 immed = sc->sc_cmd_data[1] & 0x01;
1238 loej = sc->sc_cmd_data[4] & 0x02;
1239 start = sc->sc_cmd_data[4] & 0x01;
1241 if (immed || loej || start) {
1247 /*------------------------------------------------------------------------*
1248 * ustorage_fs_prevent_allow
1253 *------------------------------------------------------------------------*/
1255 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1257 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1260 if (!currlun->removable) {
1261 currlun->sense_data = SS_INVALID_COMMAND;
1264 prevent = sc->sc_cmd_data[4] & 0x01;
1265 if ((sc->sc_cmd_data[4] & ~0x01) != 0) {
1266 /* Mask away Prevent */
1267 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1270 if (currlun->prevent_medium_removal && !prevent) {
1271 //fsync_sub(currlun);
1273 currlun->prevent_medium_removal = prevent;
1277 /*------------------------------------------------------------------------*
1278 * ustorage_fs_read_format_capacities
1283 *------------------------------------------------------------------------*/
1285 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1287 uint8_t *buf = sc->sc_transfer.data_ptr;
1288 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1290 buf[0] = buf[1] = buf[2] = 0;
1292 /* Only the Current / Maximum Capacity Descriptor */
1295 /* Number of blocks */
1296 put_be32(&buf[0], currlun->num_sectors);
1298 put_be32(&buf[4], 512);
1299 /* Current capacity */
1302 #if (USTORAGE_QDATA_MAX < 12)
1303 #error "(USTORAGE_QDATA_MAX < 12)"
1305 return (ustorage_fs_min_len(sc, 12, 0 - 1));
1308 /*------------------------------------------------------------------------*
1309 * ustorage_fs_mode_select
1314 *------------------------------------------------------------------------*/
1316 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1318 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1320 /* We don't support MODE SELECT */
1321 currlun->sense_data = SS_INVALID_COMMAND;
1325 /*------------------------------------------------------------------------*
1326 * ustorage_fs_synchronize_cache
1331 *------------------------------------------------------------------------*/
1333 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1336 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1340 * We ignore the requested LBA and write out all dirty data buffers.
1344 currlun->sense_data = SS_WRITE_ERROR;
1350 /*------------------------------------------------------------------------*
1351 * ustorage_fs_read - read data from disk
1356 *------------------------------------------------------------------------*/
1358 ustorage_fs_read(struct ustorage_fs_softc *sc)
1360 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1361 uint64_t file_offset;
1366 * Get the starting Logical Block Address and check that it's not
1369 if (sc->sc_cmd_data[0] == SC_READ_6) {
1370 lba = (((uint32_t)sc->sc_cmd_data[1]) << 16) |
1371 get_be16(&sc->sc_cmd_data[2]);
1373 lba = get_be32(&sc->sc_cmd_data[2]);
1376 * We allow DPO (Disable Page Out = don't save data in the
1377 * cache) and FUA (Force Unit Access = don't read from the
1378 * cache), but we don't implement them.
1380 if ((sc->sc_cmd_data[1] & ~0x18) != 0) {
1381 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1385 len = sc->sc_transfer.data_rem >> 9;
1389 (len > currlun->num_sectors) ||
1390 (lba >= currlun->num_sectors)) {
1391 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1397 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1402 /*------------------------------------------------------------------------*
1403 * ustorage_fs_write - write data to disk
1408 *------------------------------------------------------------------------*/
1410 ustorage_fs_write(struct ustorage_fs_softc *sc)
1412 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1413 uint64_t file_offset;
1417 if (currlun->read_only) {
1418 currlun->sense_data = SS_WRITE_PROTECTED;
1421 /* XXX clear SYNC */
1424 * Get the starting Logical Block Address and check that it's not
1427 if (sc->sc_cmd_data[0] == SC_WRITE_6)
1428 lba = (((uint32_t)sc->sc_cmd_data[1]) << 16) |
1429 get_be16(&sc->sc_cmd_data[2]);
1431 lba = get_be32(&sc->sc_cmd_data[2]);
1434 * We allow DPO (Disable Page Out = don't save data in the
1435 * cache) and FUA (Force Unit Access = write directly to the
1436 * medium). We don't implement DPO; we implement FUA by
1437 * performing synchronous output.
1439 if ((sc->sc_cmd_data[1] & ~0x18) != 0) {
1440 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1443 if (sc->sc_cmd_data[1] & 0x08) {
1445 /* XXX set SYNC flag here */
1449 len = sc->sc_transfer.data_rem >> 9;
1453 (len > currlun->num_sectors) ||
1454 (lba >= currlun->num_sectors)) {
1455 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1461 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1466 /*------------------------------------------------------------------------*
1467 * ustorage_fs_min_len
1472 *------------------------------------------------------------------------*/
1474 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1476 if (len != sc->sc_transfer.data_rem) {
1478 if (sc->sc_transfer.cbw_dir == DIR_READ) {
1480 * there must be something wrong about this SCSI
1483 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1486 /* compute the minimum length */
1488 if (sc->sc_transfer.data_rem > len) {
1489 /* data ends prematurely */
1490 sc->sc_transfer.data_rem = len;
1491 sc->sc_transfer.data_short = 1;
1493 /* check length alignment */
1495 if (sc->sc_transfer.data_rem & ~mask) {
1496 /* data ends prematurely */
1497 sc->sc_transfer.data_rem &= mask;
1498 sc->sc_transfer.data_short = 1;
1504 /*------------------------------------------------------------------------*
1505 * ustorage_fs_check_cmd - check command routine
1507 * Check whether the command is properly formed and whether its data
1508 * size and direction agree with the values we already have.
1513 *------------------------------------------------------------------------*/
1515 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1516 uint16_t mask, uint8_t needs_medium)
1518 struct ustorage_fs_lun *currlun;
1519 uint8_t lun = (sc->sc_cmd_data[1] >> 5);
1522 /* Verify the length of the command itself */
1523 if (min_cmd_size > sc->sc_transfer.cmd_len) {
1524 DPRINTF("%u > %u\n",
1525 min_cmd_size, sc->sc_transfer.cmd_len);
1526 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1529 /* Mask away the LUN */
1530 sc->sc_cmd_data[1] &= 0x1f;
1532 /* Check if LUN is correct */
1533 if (lun != sc->sc_transfer.lun) {
1537 if (sc->sc_transfer.lun <= sc->sc_last_lun) {
1538 sc->sc_transfer.currlun = currlun =
1539 sc->sc_lun + sc->sc_transfer.lun;
1540 if (sc->sc_cmd_data[0] != SC_REQUEST_SENSE) {
1541 currlun->sense_data = SS_NO_SENSE;
1542 currlun->sense_data_info = 0;
1543 currlun->info_valid = 0;
1546 * If a unit attention condition exists, only INQUIRY
1547 * and REQUEST SENSE commands are allowed. Anything
1550 if ((currlun->unit_attention_data != SS_NO_SENSE) &&
1551 (sc->sc_cmd_data[0] != SC_INQUIRY) &&
1552 (sc->sc_cmd_data[0] != SC_REQUEST_SENSE)) {
1553 currlun->sense_data = currlun->unit_attention_data;
1554 currlun->unit_attention_data = SS_NO_SENSE;
1558 sc->sc_transfer.currlun = currlun = NULL;
1561 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1562 * to use unsupported LUNs; all others may not.
1564 if ((sc->sc_cmd_data[0] != SC_INQUIRY) &&
1565 (sc->sc_cmd_data[0] != SC_REQUEST_SENSE)) {
1571 * Check that only command bytes listed in the mask are
1574 for (i = 0; i != min_cmd_size; i++) {
1575 if (sc->sc_cmd_data[i] && !(mask & (1UL << i))) {
1577 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1584 * If the medium isn't mounted and the command needs to access
1585 * it, return an error.
1587 if (currlun && (!currlun->memory_image) && needs_medium) {
1588 currlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1594 /*------------------------------------------------------------------------*
1595 * ustorage_fs_do_cmd - do command
1600 *------------------------------------------------------------------------*/
1602 ustorage_fs_do_cmd(struct ustorage_fs_softc *sc)
1607 const uint32_t mask9 = (0xFFFFFFFFUL >> 9) << 9;
1609 /* set default data transfer pointer */
1610 sc->sc_transfer.data_ptr = sc->sc_qdata;
1612 DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1613 sc->sc_cmd_data[0], sc->sc_transfer.data_rem);
1615 switch (sc->sc_cmd_data[0]) {
1617 sc->sc_transfer.cmd_dir = DIR_WRITE;
1618 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1622 error = ustorage_fs_check_cmd(sc, 6,
1627 error = ustorage_fs_inquiry(sc);
1631 case SC_MODE_SELECT_6:
1632 sc->sc_transfer.cmd_dir = DIR_READ;
1633 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1637 error = ustorage_fs_check_cmd(sc, 6,
1638 (1UL << 1) | (1UL << 4) | 1, 0);
1642 error = ustorage_fs_mode_select(sc);
1646 case SC_MODE_SELECT_10:
1647 sc->sc_transfer.cmd_dir = DIR_READ;
1648 error = ustorage_fs_min_len(sc,
1649 get_be16(&sc->sc_cmd_data[7]), 0 - 1);
1653 error = ustorage_fs_check_cmd(sc, 10,
1654 (1UL << 1) | (3UL << 7) | 1, 0);
1658 error = ustorage_fs_mode_select(sc);
1662 case SC_MODE_SENSE_6:
1663 sc->sc_transfer.cmd_dir = DIR_WRITE;
1664 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1668 error = ustorage_fs_check_cmd(sc, 6,
1669 (1UL << 1) | (1UL << 2) | (1UL << 4) | 1, 0);
1673 error = ustorage_fs_mode_sense(sc);
1677 case SC_MODE_SENSE_10:
1678 sc->sc_transfer.cmd_dir = DIR_WRITE;
1679 error = ustorage_fs_min_len(sc,
1680 get_be16(&sc->sc_cmd_data[7]), 0 - 1);
1684 error = ustorage_fs_check_cmd(sc, 10,
1685 (1UL << 1) | (1UL << 2) | (3UL << 7) | 1, 0);
1689 error = ustorage_fs_mode_sense(sc);
1693 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1694 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1698 error = ustorage_fs_check_cmd(sc, 6,
1703 error = ustorage_fs_prevent_allow(sc);
1708 i = sc->sc_cmd_data[4];
1709 sc->sc_transfer.cmd_dir = DIR_WRITE;
1710 temp = ((i == 0) ? 256UL : i);
1711 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1715 error = ustorage_fs_check_cmd(sc, 6,
1716 (7UL << 1) | (1UL << 4) | 1, 1);
1720 error = ustorage_fs_read(sc);
1725 sc->sc_transfer.cmd_dir = DIR_WRITE;
1726 temp = get_be16(&sc->sc_cmd_data[7]);
1727 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1731 error = ustorage_fs_check_cmd(sc, 10,
1732 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1736 error = ustorage_fs_read(sc);
1741 sc->sc_transfer.cmd_dir = DIR_WRITE;
1742 temp = get_be32(&sc->sc_cmd_data[6]);
1743 if (temp >= (1UL << (32 - 9))) {
1744 /* numerical overflow */
1745 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
1749 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1753 error = ustorage_fs_check_cmd(sc, 12,
1754 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1758 error = ustorage_fs_read(sc);
1762 case SC_READ_CAPACITY:
1763 sc->sc_transfer.cmd_dir = DIR_WRITE;
1764 error = ustorage_fs_check_cmd(sc, 10,
1765 (0xfUL << 2) | (1UL << 8) | 1, 1);
1769 error = ustorage_fs_read_capacity(sc);
1773 case SC_READ_FORMAT_CAPACITIES:
1774 sc->sc_transfer.cmd_dir = DIR_WRITE;
1775 error = ustorage_fs_min_len(sc,
1776 get_be16(&sc->sc_cmd_data[7]), 0 - 1);
1780 error = ustorage_fs_check_cmd(sc, 10,
1785 error = ustorage_fs_read_format_capacities(sc);
1789 case SC_REQUEST_SENSE:
1790 sc->sc_transfer.cmd_dir = DIR_WRITE;
1791 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1795 error = ustorage_fs_check_cmd(sc, 6,
1800 error = ustorage_fs_request_sense(sc);
1804 case SC_START_STOP_UNIT:
1805 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1809 error = ustorage_fs_check_cmd(sc, 6,
1810 (1UL << 1) | (1UL << 4) | 1, 0);
1814 error = ustorage_fs_start_stop(sc);
1818 case SC_SYNCHRONIZE_CACHE:
1819 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1823 error = ustorage_fs_check_cmd(sc, 10,
1824 (0xfUL << 2) | (3UL << 7) | 1, 1);
1828 error = ustorage_fs_synchronize_cache(sc);
1832 case SC_TEST_UNIT_READY:
1833 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1837 error = ustorage_fs_check_cmd(sc, 6,
1842 * Although optional, this command is used by MS-Windows.
1843 * We support a minimal version: BytChk must be 0.
1846 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1850 error = ustorage_fs_check_cmd(sc, 10,
1851 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1855 error = ustorage_fs_verify(sc);
1860 i = sc->sc_cmd_data[4];
1861 sc->sc_transfer.cmd_dir = DIR_READ;
1862 temp = ((i == 0) ? 256UL : i);
1863 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1867 error = ustorage_fs_check_cmd(sc, 6,
1868 (7UL << 1) | (1UL << 4) | 1, 1);
1872 error = ustorage_fs_write(sc);
1877 sc->sc_transfer.cmd_dir = DIR_READ;
1878 temp = get_be16(&sc->sc_cmd_data[7]);
1879 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1883 error = ustorage_fs_check_cmd(sc, 10,
1884 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1888 error = ustorage_fs_write(sc);
1893 sc->sc_transfer.cmd_dir = DIR_READ;
1894 temp = get_be32(&sc->sc_cmd_data[6]);
1895 if (temp > (mask9 >> 9)) {
1896 /* numerical overflow */
1897 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
1901 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1905 error = ustorage_fs_check_cmd(sc, 12,
1906 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1910 error = ustorage_fs_write(sc);
1915 * Some mandatory commands that we recognize but don't
1916 * implement. They don't mean much in this setting.
1917 * It's left as an exercise for anyone interested to
1918 * implement RESERVE and RELEASE in terms of Posix
1921 case SC_FORMAT_UNIT:
1924 case SC_SEND_DIAGNOSTIC:
1928 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1932 error = ustorage_fs_check_cmd(sc, sc->sc_transfer.cmd_len,
1937 sc->sc_transfer.currlun->sense_data =