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 <dev/usb/usb.h>
40 #include <dev/usb/usb_mfunc.h>
41 #include <dev/usb/usb_error.h>
43 #define USB_DEBUG_VAR ustorage_fs_debug
45 #include <dev/usb/usb_core.h>
46 #include <dev/usb/usb_util.h>
47 #include <dev/usb/usb_busdma.h>
48 #include <dev/usb/usb_debug.h>
49 #include <dev/usb/usb_process.h>
50 #include <dev/usb/usb_device.h>
53 static int ustorage_fs_debug = 0;
55 SYSCTL_NODE(_hw_usb2, OID_AUTO, ustorage_fs, CTLFLAG_RW, 0, "USB ustorage_fs");
56 SYSCTL_INT(_hw_usb2_ustorage_fs, OID_AUTO, debug, CTLFLAG_RW,
57 &ustorage_fs_debug, 0, "ustorage_fs debug level");
60 /* Define some limits */
62 #ifndef USTORAGE_FS_BULK_SIZE
63 #define USTORAGE_FS_BULK_SIZE (1 << 17) /* bytes */
66 #define USTORAGE_FS_MAX_LUN 8 /* units */
67 #define USTORAGE_FS_RELEASE 0x0101
68 #define USTORAGE_FS_RAM_SECT (1 << 13)
71 * The SCSI ID string must be exactly 28 characters long
72 * exluding the terminating zero.
74 #ifndef USTORAGE_FS_ID_STRING
75 #define USTORAGE_FS_ID_STRING \
77 "File-Stor Gadget" /* 16 */ \
82 * The following macro defines the number of
83 * sectors to be allocated for the RAM disk:
85 #ifndef USTORAGE_FS_RAM_SECT
86 #define USTORAGE_FS_RAM_SECT (1UL << 13)
89 static uint8_t *ustorage_fs_ramdisk;
91 /* USB transfer definitions */
93 #define USTORAGE_FS_T_BBB_COMMAND 0
94 #define USTORAGE_FS_T_BBB_DATA_DUMP 1
95 #define USTORAGE_FS_T_BBB_DATA_READ 2
96 #define USTORAGE_FS_T_BBB_DATA_WRITE 3
97 #define USTORAGE_FS_T_BBB_STATUS 4
98 #define USTORAGE_FS_T_BBB_MAX 5
100 /* USB data stage direction */
106 /* USB interface specific control request */
108 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
109 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
111 /* Command Block Wrapper */
113 uDWord dCBWSignature;
114 #define CBWSIGNATURE 0x43425355
116 uDWord dCBWDataTransferLength;
118 #define CBWFLAGS_OUT 0x00
119 #define CBWFLAGS_IN 0x80
122 #define CBWCDBLENGTH 16
123 uByte CBWCDB[CBWCDBLENGTH];
124 } __packed ustorage_fs_bbb_cbw_t;
126 #define USTORAGE_FS_BBB_CBW_SIZE 31
128 /* Command Status Wrapper */
130 uDWord dCSWSignature;
131 #define CSWSIGNATURE 0x53425355
133 uDWord dCSWDataResidue;
135 #define CSWSTATUS_GOOD 0x0
136 #define CSWSTATUS_FAILED 0x1
137 #define CSWSTATUS_PHASE 0x2
138 } __packed ustorage_fs_bbb_csw_t;
140 #define USTORAGE_FS_BBB_CSW_SIZE 13
142 struct ustorage_fs_lun {
144 uint8_t *memory_image;
146 uint32_t num_sectors;
148 uint32_t sense_data_info;
149 uint32_t unit_attention_data;
152 uint8_t prevent_medium_removal:1;
153 uint8_t info_valid:1;
157 struct ustorage_fs_softc {
159 ustorage_fs_bbb_cbw_t sc_cbw; /* Command Wrapper Block */
160 ustorage_fs_bbb_csw_t sc_csw; /* Command Status Block */
164 struct ustorage_fs_lun sc_lun[USTORAGE_FS_MAX_LUN];
168 struct ustorage_fs_lun *currlun;
170 uint32_t data_rem; /* bytes, as reported by the command
172 uint32_t offset; /* bytes */
177 uint8_t cmd_data[CBWCDBLENGTH];
179 uint8_t data_short:1;
180 uint8_t data_error:1;
184 struct usb2_device *sc_udev;
185 struct usb2_xfer *sc_xfer[USTORAGE_FS_T_BBB_MAX];
190 uint8_t sc_iface_no; /* interface number */
192 uint8_t sc_last_xfer_index;
193 uint8_t sc_qdata[1024];
198 static device_probe_t ustorage_fs_probe;
199 static device_attach_t ustorage_fs_attach;
200 static device_detach_t ustorage_fs_detach;
201 static device_suspend_t ustorage_fs_suspend;
202 static device_resume_t ustorage_fs_resume;
203 static device_shutdown_t ustorage_fs_shutdown;
204 static usb_handle_request_t ustorage_fs_handle_request;
206 static usb2_callback_t ustorage_fs_t_bbb_command_callback;
207 static usb2_callback_t ustorage_fs_t_bbb_data_dump_callback;
208 static usb2_callback_t ustorage_fs_t_bbb_data_read_callback;
209 static usb2_callback_t ustorage_fs_t_bbb_data_write_callback;
210 static usb2_callback_t ustorage_fs_t_bbb_status_callback;
212 static void ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index);
213 static void ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc);
215 static uint8_t ustorage_fs_verify(struct ustorage_fs_softc *sc);
216 static uint8_t ustorage_fs_inquiry(struct ustorage_fs_softc *sc);
217 static uint8_t ustorage_fs_request_sense(struct ustorage_fs_softc *sc);
218 static uint8_t ustorage_fs_read_capacity(struct ustorage_fs_softc *sc);
219 static uint8_t ustorage_fs_mode_sense(struct ustorage_fs_softc *sc);
220 static uint8_t ustorage_fs_start_stop(struct ustorage_fs_softc *sc);
221 static uint8_t ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc);
222 static uint8_t ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc);
223 static uint8_t ustorage_fs_mode_select(struct ustorage_fs_softc *sc);
224 static uint8_t ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask);
225 static uint8_t ustorage_fs_read(struct ustorage_fs_softc *sc);
226 static uint8_t ustorage_fs_write(struct ustorage_fs_softc *sc);
227 static uint8_t ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t cmd_size, uint16_t mask, uint8_t needs_medium);
228 static uint8_t ustorage_fs_do_cmd(struct ustorage_fs_softc *sc);
230 static device_method_t ustorage_fs_methods[] = {
232 DEVMETHOD(usb_handle_request, ustorage_fs_handle_request),
234 /* Device interface */
235 DEVMETHOD(device_probe, ustorage_fs_probe),
236 DEVMETHOD(device_attach, ustorage_fs_attach),
237 DEVMETHOD(device_detach, ustorage_fs_detach),
238 DEVMETHOD(device_suspend, ustorage_fs_suspend),
239 DEVMETHOD(device_resume, ustorage_fs_resume),
240 DEVMETHOD(device_shutdown, ustorage_fs_shutdown),
245 static driver_t ustorage_fs_driver = {
246 .name = "ustorage_fs",
247 .methods = ustorage_fs_methods,
248 .size = sizeof(struct ustorage_fs_softc),
251 static devclass_t ustorage_fs_devclass;
253 DRIVER_MODULE(ustorage_fs, uhub, ustorage_fs_driver, ustorage_fs_devclass, NULL, 0);
254 MODULE_VERSION(ustorage_fs, 0);
255 MODULE_DEPEND(ustorage_fs, usb, 1, 1, 1);
257 struct usb2_config ustorage_fs_bbb_config[USTORAGE_FS_T_BBB_MAX] = {
259 [USTORAGE_FS_T_BBB_COMMAND] = {
261 .endpoint = UE_ADDR_ANY,
262 .direction = UE_DIR_OUT,
263 .md.bufsize = sizeof(ustorage_fs_bbb_cbw_t),
264 .md.flags = {.ext_buffer = 1,},
265 .md.callback = &ustorage_fs_t_bbb_command_callback,
268 [USTORAGE_FS_T_BBB_DATA_DUMP] = {
270 .endpoint = UE_ADDR_ANY,
271 .direction = UE_DIR_OUT,
272 .md.bufsize = 0, /* use wMaxPacketSize */
273 .md.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
274 .md.callback = &ustorage_fs_t_bbb_data_dump_callback,
277 [USTORAGE_FS_T_BBB_DATA_READ] = {
279 .endpoint = UE_ADDR_ANY,
280 .direction = UE_DIR_OUT,
281 .md.bufsize = USTORAGE_FS_BULK_SIZE,
282 .md.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
283 .md.callback = &ustorage_fs_t_bbb_data_read_callback,
286 [USTORAGE_FS_T_BBB_DATA_WRITE] = {
288 .endpoint = UE_ADDR_ANY,
289 .direction = UE_DIR_IN,
290 .md.bufsize = USTORAGE_FS_BULK_SIZE,
291 .md.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
292 .md.callback = &ustorage_fs_t_bbb_data_write_callback,
295 [USTORAGE_FS_T_BBB_STATUS] = {
297 .endpoint = UE_ADDR_ANY,
298 .direction = UE_DIR_IN,
299 .md.bufsize = sizeof(ustorage_fs_bbb_csw_t),
300 .md.flags = {.short_xfer_ok = 1,.ext_buffer = 1,},
301 .md.callback = &ustorage_fs_t_bbb_status_callback,
306 * USB device probe/attach/detach
310 ustorage_fs_probe(device_t dev)
312 struct usb2_attach_arg *uaa = device_get_ivars(dev);
313 struct usb2_interface_descriptor *id;
315 if (uaa->usb2_mode != USB_MODE_DEVICE) {
318 if (uaa->use_generic == 0) {
319 /* give other drivers a try first */
322 /* Check for a standards compliant device */
323 id = usb2_get_interface_descriptor(uaa->iface);
325 (id->bInterfaceClass != UICLASS_MASS) ||
326 (id->bInterfaceSubClass != UISUBCLASS_SCSI) ||
327 (id->bInterfaceProtocol != UIPROTO_MASS_BBB)) {
334 ustorage_fs_attach(device_t dev)
336 struct ustorage_fs_softc *sc = device_get_softc(dev);
337 struct usb2_attach_arg *uaa = device_get_ivars(dev);
338 struct usb2_interface_descriptor *id;
342 * NOTE: the softc struct is bzero-ed in device_set_driver.
343 * We can safely call ustorage_fs_detach without specifically
344 * initializing the struct.
348 sc->sc_udev = uaa->device;
349 sc->sc_unit = device_get_unit(dev);
351 if (sc->sc_unit == 0) {
352 if (ustorage_fs_ramdisk == NULL) {
354 * allocate a memory image for our ramdisk until
357 ustorage_fs_ramdisk =
358 malloc(USTORAGE_FS_RAM_SECT << 9, M_USB, M_ZERO | M_WAITOK);
359 if (ustorage_fs_ramdisk == NULL) {
363 sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
364 sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
365 sc->sc_lun[0].removable = 1;
367 snprintf(sc->sc_name, sizeof(sc->sc_name),
368 "%s", device_get_nameunit(dev));
370 device_set_usb2_desc(dev);
372 mtx_init(&sc->sc_mtx, "USTORAGE_FS lock",
373 NULL, (MTX_DEF | MTX_RECURSE));
375 /* get interface index */
377 id = usb2_get_interface_descriptor(uaa->iface);
379 device_printf(dev, "failed to get "
380 "interface number\n");
383 sc->sc_iface_no = id->bInterfaceNumber;
385 err = usb2_transfer_setup(uaa->device,
386 &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
387 USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_mtx);
389 device_printf(dev, "could not setup required "
390 "transfers, %s\n", usb2_errstr(err));
393 /* start Mass Storage State Machine */
395 mtx_lock(&sc->sc_mtx);
396 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
397 mtx_unlock(&sc->sc_mtx);
399 return (0); /* success */
402 ustorage_fs_detach(dev);
403 return (ENXIO); /* failure */
407 ustorage_fs_detach(device_t dev)
409 struct ustorage_fs_softc *sc = device_get_softc(dev);
411 /* teardown our statemachine */
413 usb2_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
415 mtx_destroy(&sc->sc_mtx);
417 return (0); /* success */
421 ustorage_fs_suspend(device_t dev)
423 device_printf(dev, "suspending\n");
424 return (0); /* success */
428 ustorage_fs_resume(device_t dev)
430 device_printf(dev, "resuming\n");
431 return (0); /* success */
435 ustorage_fs_shutdown(device_t dev)
437 return (0); /* success */
441 * Generic functions to handle transfers
445 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
447 if (sc->sc_xfer[xfer_index]) {
448 sc->sc_last_xfer_index = xfer_index;
449 usb2_transfer_start(sc->sc_xfer[xfer_index]);
454 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
456 usb2_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
457 mtx_unlock(&sc->sc_mtx);
458 usb2_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
459 mtx_lock(&sc->sc_mtx);
463 ustorage_fs_handle_request(device_t dev,
464 const void *preq, void **pptr, uint16_t *plen,
465 uint16_t offset, uint8_t is_complete)
467 struct ustorage_fs_softc *sc = device_get_softc(dev);
468 const struct usb2_device_request *req = preq;
471 if (req->bRequest == UR_BBB_RESET) {
473 mtx_lock(&sc->sc_mtx);
474 ustorage_fs_transfer_stop(sc);
475 sc->sc_transfer.data_error = 1;
476 ustorage_fs_transfer_start(sc,
477 USTORAGE_FS_T_BBB_COMMAND);
478 mtx_unlock(&sc->sc_mtx);
480 } else if (req->bRequest == UR_BBB_GET_MAX_LUN) {
483 *pptr = &sc->sc_last_lun;
490 return (ENXIO); /* use builtin handler */
494 ustorage_fs_t_bbb_command_callback(struct usb2_xfer *xfer)
496 struct ustorage_fs_softc *sc = xfer->priv_sc;
502 switch (USB_GET_STATE(xfer)) {
503 case USB_ST_TRANSFERRED:
505 tag = UGETDW(sc->sc_cbw.dCBWSignature);
507 if (tag != CBWSIGNATURE) {
509 DPRINTF("invalid signature 0x%08x\n", tag);
512 tag = UGETDW(sc->sc_cbw.dCBWTag);
515 USETDW(sc->sc_csw.dCSWTag, tag);
518 sc->sc_csw.bCSWStatus = 0;
520 /* reset data offset, data length and data remainder */
521 sc->sc_transfer.offset = 0;
522 sc->sc_transfer.data_rem =
523 UGETDW(sc->sc_cbw.dCBWDataTransferLength);
525 /* reset data flags */
526 sc->sc_transfer.data_short = 0;
529 sc->sc_transfer.lun = sc->sc_cbw.bCBWLUN;
531 if (sc->sc_transfer.data_rem == 0) {
532 sc->sc_transfer.cbw_dir = DIR_NONE;
534 if (sc->sc_cbw.bCBWFlags & CBWFLAGS_IN) {
535 sc->sc_transfer.cbw_dir = DIR_WRITE;
537 sc->sc_transfer.cbw_dir = DIR_READ;
541 sc->sc_transfer.cmd_len = sc->sc_cbw.bCDBLength;
542 if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw.CBWCDB)) ||
543 (sc->sc_transfer.cmd_len == 0)) {
544 /* just halt - this is invalid */
545 DPRINTF("invalid command length %d bytes\n",
546 sc->sc_transfer.cmd_len);
549 bcopy(sc->sc_cbw.CBWCDB, sc->sc_transfer.cmd_data,
550 sc->sc_transfer.cmd_len);
552 bzero(sc->sc_cbw.CBWCDB + sc->sc_transfer.cmd_len,
553 sizeof(sc->sc_cbw.CBWCDB) - sc->sc_transfer.cmd_len);
555 error = ustorage_fs_do_cmd(sc);
558 DPRINTF("command failed\n");
561 if ((sc->sc_transfer.data_rem > 0) &&
562 (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
563 /* contradicting data transfer direction */
565 DPRINTF("data direction mismatch\n");
568 switch (sc->sc_transfer.cbw_dir) {
570 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
573 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
576 ustorage_fs_transfer_start(sc,
577 USTORAGE_FS_T_BBB_STATUS);
584 if (sc->sc_transfer.data_error) {
585 sc->sc_transfer.data_error = 0;
586 xfer->flags.stall_pipe = 1;
587 DPRINTF("stall pipe\n");
589 xfer->flags.stall_pipe = 0;
592 xfer->frlengths[0] = sizeof(sc->sc_cbw);
593 usb2_set_frame_data(xfer, &sc->sc_cbw, 0);
594 usb2_start_hardware(xfer);
599 if (xfer->error == USB_ERR_CANCELLED) {
602 /* If the pipe is already stalled, don't do another stall */
603 if (!xfer->pipe->is_stalled) {
604 sc->sc_transfer.data_error = 1;
610 if (sc->sc_csw.bCSWStatus == 0) {
611 /* set some default error code */
612 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
614 if (sc->sc_transfer.cbw_dir == DIR_READ) {
616 ustorage_fs_transfer_start(sc,
617 USTORAGE_FS_T_BBB_DATA_DUMP);
620 if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
621 /* need to stall before status */
622 sc->sc_transfer.data_error = 1;
624 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
629 ustorage_fs_t_bbb_data_dump_callback(struct usb2_xfer *xfer)
631 struct ustorage_fs_softc *sc = xfer->priv_sc;
632 uint32_t max_bulk = xfer->max_data_length;
636 switch (USB_GET_STATE(xfer)) {
637 case USB_ST_TRANSFERRED:
638 sc->sc_transfer.data_rem -= xfer->actlen;
639 sc->sc_transfer.offset += xfer->actlen;
641 if ((xfer->actlen != xfer->sumlen) ||
642 (sc->sc_transfer.data_rem == 0)) {
643 /* short transfer or end of data */
644 ustorage_fs_transfer_start(sc,
645 USTORAGE_FS_T_BBB_STATUS);
652 if (max_bulk > sc->sc_transfer.data_rem) {
653 max_bulk = sc->sc_transfer.data_rem;
655 if (sc->sc_transfer.data_error) {
656 sc->sc_transfer.data_error = 0;
657 xfer->flags.stall_pipe = 1;
659 xfer->flags.stall_pipe = 0;
661 xfer->frlengths[0] = max_bulk;
662 usb2_start_hardware(xfer);
666 if (xfer->error == USB_ERR_CANCELLED) {
670 * If the pipe is already stalled, don't do another stall:
672 if (!xfer->pipe->is_stalled) {
673 sc->sc_transfer.data_error = 1;
681 ustorage_fs_t_bbb_data_read_callback(struct usb2_xfer *xfer)
683 struct ustorage_fs_softc *sc = xfer->priv_sc;
684 uint32_t max_bulk = xfer->max_data_length;
688 switch (USB_GET_STATE(xfer)) {
689 case USB_ST_TRANSFERRED:
690 sc->sc_transfer.data_rem -= xfer->actlen;
691 sc->sc_transfer.data_ptr += xfer->actlen;
692 sc->sc_transfer.offset += xfer->actlen;
694 if ((xfer->actlen != xfer->sumlen) ||
695 (sc->sc_transfer.data_rem == 0)) {
696 /* short transfer or end of data */
697 ustorage_fs_transfer_start(sc,
698 USTORAGE_FS_T_BBB_STATUS);
705 if (max_bulk > sc->sc_transfer.data_rem) {
706 max_bulk = sc->sc_transfer.data_rem;
708 if (sc->sc_transfer.data_error) {
709 sc->sc_transfer.data_error = 0;
710 xfer->flags.stall_pipe = 1;
712 xfer->flags.stall_pipe = 0;
715 xfer->frlengths[0] = max_bulk;
716 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
717 usb2_start_hardware(xfer);
721 if (xfer->error == USB_ERR_CANCELLED) {
724 /* If the pipe is already stalled, don't do another stall */
725 if (!xfer->pipe->is_stalled) {
726 sc->sc_transfer.data_error = 1;
734 ustorage_fs_t_bbb_data_write_callback(struct usb2_xfer *xfer)
736 struct ustorage_fs_softc *sc = xfer->priv_sc;
737 uint32_t max_bulk = xfer->max_data_length;
741 switch (USB_GET_STATE(xfer)) {
742 case USB_ST_TRANSFERRED:
743 sc->sc_transfer.data_rem -= xfer->actlen;
744 sc->sc_transfer.data_ptr += xfer->actlen;
745 sc->sc_transfer.offset += xfer->actlen;
747 if ((xfer->actlen != xfer->sumlen) ||
748 (sc->sc_transfer.data_rem == 0)) {
749 /* short transfer or end of data */
750 ustorage_fs_transfer_start(sc,
751 USTORAGE_FS_T_BBB_STATUS);
756 if (max_bulk >= sc->sc_transfer.data_rem) {
757 max_bulk = sc->sc_transfer.data_rem;
758 if (sc->sc_transfer.data_short) {
759 xfer->flags.force_short_xfer = 1;
761 xfer->flags.force_short_xfer = 0;
764 xfer->flags.force_short_xfer = 0;
767 if (sc->sc_transfer.data_error) {
768 sc->sc_transfer.data_error = 0;
769 xfer->flags.stall_pipe = 1;
771 xfer->flags.stall_pipe = 0;
774 xfer->frlengths[0] = max_bulk;
775 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
776 usb2_start_hardware(xfer);
780 if (xfer->error == USB_ERR_CANCELLED) {
784 * If the pipe is already stalled, don't do another
787 if (!xfer->pipe->is_stalled) {
788 sc->sc_transfer.data_error = 1;
796 ustorage_fs_t_bbb_status_callback(struct usb2_xfer *xfer)
798 struct ustorage_fs_softc *sc = xfer->priv_sc;
802 switch (USB_GET_STATE(xfer)) {
803 case USB_ST_TRANSFERRED:
804 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
809 USETDW(sc->sc_csw.dCSWSignature, CSWSIGNATURE);
810 USETDW(sc->sc_csw.dCSWDataResidue, sc->sc_transfer.data_rem);
812 if (sc->sc_transfer.data_error) {
813 sc->sc_transfer.data_error = 0;
814 xfer->flags.stall_pipe = 1;
816 xfer->flags.stall_pipe = 0;
819 xfer->frlengths[0] = sizeof(sc->sc_csw);
820 usb2_set_frame_data(xfer, &sc->sc_csw, 0);
821 usb2_start_hardware(xfer);
825 if (xfer->error == USB_ERR_CANCELLED) {
828 /* If the pipe is already stalled, don't do another stall */
829 if (!xfer->pipe->is_stalled) {
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_transfer.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_transfer.cmd_data[1] & ~0x10) != 0) {
939 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
942 vlen = get_be16(&sc->sc_transfer.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;
979 static const char vendor_id[] = "FreeBSD ";
980 static const char product_id[] = "File-Stor Gadget";
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 */
1003 * NOTE: We are writing an extra zero here, that is not
1004 * transferred to the peer:
1006 snprintf(buf + 8, 28 + 1, "%-8s%-16s%04x", vendor_id, product_id,
1007 USTORAGE_FS_RELEASE);
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 */
1073 return (ustorage_fs_min_len(sc, 18, 0 - 1));
1077 /*------------------------------------------------------------------------*
1078 * ustorage_fs_read_capacity
1083 *------------------------------------------------------------------------*/
1085 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1087 uint8_t *buf = sc->sc_transfer.data_ptr;
1088 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1089 uint32_t lba = get_be32(&sc->sc_transfer.cmd_data[2]);
1090 uint8_t pmi = sc->sc_transfer.cmd_data[8];
1092 /* Check the PMI and LBA fields */
1093 if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1094 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1097 put_be32(&buf[0], currlun->num_sectors - 1);
1098 /* Max logical block */
1099 put_be32(&buf[4], 512);
1101 return (ustorage_fs_min_len(sc, 8, 0 - 1));
1105 /*------------------------------------------------------------------------*
1106 * ustorage_fs_mode_sense
1111 *------------------------------------------------------------------------*/
1113 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1115 uint8_t *buf = sc->sc_transfer.data_ptr;
1116 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1120 uint8_t mscmnd = sc->sc_transfer.cmd_data[0];
1123 uint8_t changeable_values;
1128 if ((sc->sc_transfer.cmd_data[1] & ~0x08) != 0) {
1130 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1133 pc = sc->sc_transfer.cmd_data[2] >> 6;
1134 page_code = sc->sc_transfer.cmd_data[2] & 0x3f;
1136 currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1139 changeable_values = (pc == 1);
1140 all_pages = (page_code == 0x3f);
1143 * Write the mode parameter header. Fixed values are: default
1144 * medium type, no cache control (DPOFUA), and no block descriptors.
1145 * The only variable value is the WriteProtect bit. We will fill in
1146 * the mode data length later.
1149 if (mscmnd == SC_MODE_SENSE_6) {
1150 buf[2] = (currlun->read_only ? 0x80 : 0x00);
1155 /* SC_MODE_SENSE_10 */
1156 buf[3] = (currlun->read_only ? 0x80 : 0x00);
1160 /* Should really be mod_data.buflen */
1163 /* No block descriptors */
1166 * The mode pages, in numerical order.
1168 if ((page_code == 0x08) || all_pages) {
1173 memset(buf + 2, 0, 10);
1174 /* None of the fields are changeable */
1176 if (!changeable_values) {
1178 /* Write cache enable, */
1179 /* Read cache not disabled */
1180 /* No cache retention priorities */
1181 put_be16(&buf[4], 0xffff);
1182 /* Don 't disable prefetch */
1183 /* Minimum prefetch = 0 */
1184 put_be16(&buf[8], 0xffff);
1185 /* Maximum prefetch */
1186 put_be16(&buf[10], 0xffff);
1187 /* Maximum prefetch ceiling */
1192 * Check that a valid page was requested and the mode data length
1197 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1200 /* Store the mode data length */
1201 if (mscmnd == SC_MODE_SENSE_6)
1204 put_be16(buf0, len - 2);
1205 return (ustorage_fs_min_len(sc, len, 0 - 1));
1208 /*------------------------------------------------------------------------*
1209 * ustorage_fs_start_stop
1214 *------------------------------------------------------------------------*/
1216 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1218 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1223 if (!currlun->removable) {
1224 currlun->sense_data = SS_INVALID_COMMAND;
1227 immed = sc->sc_transfer.cmd_data[1] & 0x01;
1228 loej = sc->sc_transfer.cmd_data[4] & 0x02;
1229 start = sc->sc_transfer.cmd_data[4] & 0x01;
1231 if (immed || loej || start) {
1237 /*------------------------------------------------------------------------*
1238 * ustorage_fs_prevent_allow
1243 *------------------------------------------------------------------------*/
1245 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1247 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1250 if (!currlun->removable) {
1251 currlun->sense_data = SS_INVALID_COMMAND;
1254 prevent = sc->sc_transfer.cmd_data[4] & 0x01;
1255 if ((sc->sc_transfer.cmd_data[4] & ~0x01) != 0) {
1256 /* Mask away Prevent */
1257 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1260 if (currlun->prevent_medium_removal && !prevent) {
1261 //fsync_sub(currlun);
1263 currlun->prevent_medium_removal = prevent;
1267 /*------------------------------------------------------------------------*
1268 * ustorage_fs_read_format_capacities
1273 *------------------------------------------------------------------------*/
1275 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1277 uint8_t *buf = sc->sc_transfer.data_ptr;
1278 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1280 buf[0] = buf[1] = buf[2] = 0;
1282 /* Only the Current / Maximum Capacity Descriptor */
1285 put_be32(&buf[0], currlun->num_sectors);
1286 /* Number of blocks */
1287 put_be32(&buf[4], 512);
1290 /* Current capacity */
1291 return (ustorage_fs_min_len(sc, 12, 0 - 1));
1294 /*------------------------------------------------------------------------*
1295 * ustorage_fs_mode_select
1300 *------------------------------------------------------------------------*/
1302 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1304 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1306 /* We don't support MODE SELECT */
1307 currlun->sense_data = SS_INVALID_COMMAND;
1311 /*------------------------------------------------------------------------*
1312 * ustorage_fs_synchronize_cache
1317 *------------------------------------------------------------------------*/
1319 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1322 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1326 * We ignore the requested LBA and write out all dirty data buffers.
1330 currlun->sense_data = SS_WRITE_ERROR;
1336 /*------------------------------------------------------------------------*
1337 * ustorage_fs_read - read data from disk
1342 *------------------------------------------------------------------------*/
1344 ustorage_fs_read(struct ustorage_fs_softc *sc)
1346 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1347 uint64_t file_offset;
1352 * Get the starting Logical Block Address and check that it's not
1355 if (sc->sc_transfer.cmd_data[0] == SC_READ_6) {
1356 lba = (sc->sc_transfer.cmd_data[1] << 16) |
1357 get_be16(&sc->sc_transfer.cmd_data[2]);
1359 lba = get_be32(&sc->sc_transfer.cmd_data[2]);
1362 * We allow DPO (Disable Page Out = don't save data in the
1363 * cache) and FUA (Force Unit Access = don't read from the
1364 * cache), but we don't implement them.
1366 if ((sc->sc_transfer.cmd_data[1] & ~0x18) != 0) {
1367 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1371 len = sc->sc_transfer.data_rem >> 9;
1375 (len > currlun->num_sectors) ||
1376 (lba >= currlun->num_sectors)) {
1377 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1383 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1388 /*------------------------------------------------------------------------*
1389 * ustorage_fs_write - write data to disk
1394 *------------------------------------------------------------------------*/
1396 ustorage_fs_write(struct ustorage_fs_softc *sc)
1398 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1399 uint64_t file_offset;
1403 if (currlun->read_only) {
1404 currlun->sense_data = SS_WRITE_PROTECTED;
1407 /* XXX clear SYNC */
1410 * Get the starting Logical Block Address and check that it's not
1413 if (sc->sc_transfer.cmd_data[0] == SC_WRITE_6)
1414 lba = (sc->sc_transfer.cmd_data[1] << 16) |
1415 get_be16(&sc->sc_transfer.cmd_data[2]);
1417 lba = get_be32(&sc->sc_transfer.cmd_data[2]);
1420 * We allow DPO (Disable Page Out = don't save data in the
1421 * cache) and FUA (Force Unit Access = write directly to the
1422 * medium). We don't implement DPO; we implement FUA by
1423 * performing synchronous output.
1425 if ((sc->sc_transfer.cmd_data[1] & ~0x18) != 0) {
1426 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1429 if (sc->sc_transfer.cmd_data[1] & 0x08) {
1431 /* XXX set SYNC flag here */
1435 len = sc->sc_transfer.data_rem >> 9;
1439 (len > currlun->num_sectors) ||
1440 (lba >= currlun->num_sectors)) {
1441 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1447 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1452 /*------------------------------------------------------------------------*
1453 * ustorage_fs_min_len
1458 *------------------------------------------------------------------------*/
1460 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1462 if (len != sc->sc_transfer.data_rem) {
1464 if (sc->sc_transfer.cbw_dir == DIR_READ) {
1466 * there must be something wrong about this SCSI
1469 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1472 /* compute the minimum length */
1474 if (sc->sc_transfer.data_rem > len) {
1475 /* data ends prematurely */
1476 sc->sc_transfer.data_rem = len;
1477 sc->sc_transfer.data_short = 1;
1479 /* check length alignment */
1481 if (sc->sc_transfer.data_rem & ~mask) {
1482 /* data ends prematurely */
1483 sc->sc_transfer.data_rem &= mask;
1484 sc->sc_transfer.data_short = 1;
1490 /*------------------------------------------------------------------------*
1491 * ustorage_fs_check_cmd - check command routine
1493 * Check whether the command is properly formed and whether its data
1494 * size and direction agree with the values we already have.
1499 *------------------------------------------------------------------------*/
1501 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1502 uint16_t mask, uint8_t needs_medium)
1504 struct ustorage_fs_lun *currlun;
1505 uint8_t lun = (sc->sc_transfer.cmd_data[1] >> 5);
1508 /* Verify the length of the command itself */
1509 if (min_cmd_size > sc->sc_transfer.cmd_len) {
1510 DPRINTF("%u > %u\n",
1511 min_cmd_size, sc->sc_transfer.cmd_len);
1512 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1515 /* Mask away the LUN */
1516 sc->sc_transfer.cmd_data[1] &= 0x1f;
1518 /* Check if LUN is correct */
1519 if (lun != sc->sc_transfer.lun) {
1523 if (sc->sc_transfer.lun <= sc->sc_last_lun) {
1524 sc->sc_transfer.currlun = currlun =
1525 sc->sc_lun + sc->sc_transfer.lun;
1526 if (sc->sc_transfer.cmd_data[0] != SC_REQUEST_SENSE) {
1527 currlun->sense_data = SS_NO_SENSE;
1528 currlun->sense_data_info = 0;
1529 currlun->info_valid = 0;
1532 * If a unit attention condition exists, only INQUIRY
1533 * and REQUEST SENSE commands are allowed. Anything
1536 if ((currlun->unit_attention_data != SS_NO_SENSE) &&
1537 (sc->sc_transfer.cmd_data[0] != SC_INQUIRY) &&
1538 (sc->sc_transfer.cmd_data[0] != SC_REQUEST_SENSE)) {
1539 currlun->sense_data = currlun->unit_attention_data;
1540 currlun->unit_attention_data = SS_NO_SENSE;
1544 sc->sc_transfer.currlun = currlun = NULL;
1547 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1548 * to use unsupported LUNs; all others may not.
1550 if ((sc->sc_transfer.cmd_data[0] != SC_INQUIRY) &&
1551 (sc->sc_transfer.cmd_data[0] != SC_REQUEST_SENSE)) {
1557 * Check that only command bytes listed in the mask are
1560 for (i = 0; i != min_cmd_size; i++) {
1561 if (sc->sc_transfer.cmd_data[i] && !(mask & (1 << i))) {
1563 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1570 * If the medium isn't mounted and the command needs to access
1571 * it, return an error.
1573 if (currlun && (!currlun->memory_image) && needs_medium) {
1574 currlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1580 /*------------------------------------------------------------------------*
1581 * ustorage_fs_do_cmd - do command
1586 *------------------------------------------------------------------------*/
1588 ustorage_fs_do_cmd(struct ustorage_fs_softc *sc)
1593 /* set default data transfer pointer */
1594 sc->sc_transfer.data_ptr = sc->sc_qdata;
1596 DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1597 sc->sc_transfer.cmd_data[0], sc->sc_transfer.data_rem);
1599 switch (sc->sc_transfer.cmd_data[0]) {
1601 sc->sc_transfer.cmd_dir = DIR_WRITE;
1602 error = ustorage_fs_min_len(sc, sc->sc_transfer.cmd_data[4], 0 - 1);
1606 error = ustorage_fs_check_cmd(sc, 6,
1611 error = ustorage_fs_inquiry(sc);
1615 case SC_MODE_SELECT_6:
1616 sc->sc_transfer.cmd_dir = DIR_READ;
1617 error = ustorage_fs_min_len(sc, sc->sc_transfer.cmd_data[4], 0 - 1);
1621 error = ustorage_fs_check_cmd(sc, 6,
1622 (1 << 1) | (1 << 4) | 1, 0);
1626 error = ustorage_fs_mode_select(sc);
1630 case SC_MODE_SELECT_10:
1631 sc->sc_transfer.cmd_dir = DIR_READ;
1632 error = ustorage_fs_min_len(sc,
1633 get_be16(&sc->sc_transfer.cmd_data[7]), 0 - 1);
1637 error = ustorage_fs_check_cmd(sc, 10,
1638 (1 << 1) | (3 << 7) | 1, 0);
1642 error = ustorage_fs_mode_select(sc);
1646 case SC_MODE_SENSE_6:
1647 sc->sc_transfer.cmd_dir = DIR_WRITE;
1648 error = ustorage_fs_min_len(sc, sc->sc_transfer.cmd_data[4], 0 - 1);
1652 error = ustorage_fs_check_cmd(sc, 6,
1653 (1 << 1) | (1 << 2) | (1 << 4) | 1, 0);
1657 error = ustorage_fs_mode_sense(sc);
1661 case SC_MODE_SENSE_10:
1662 sc->sc_transfer.cmd_dir = DIR_WRITE;
1663 error = ustorage_fs_min_len(sc,
1664 get_be16(&sc->sc_transfer.cmd_data[7]), 0 - 1);
1668 error = ustorage_fs_check_cmd(sc, 10,
1669 (1 << 1) | (1 << 2) | (3 << 7) | 1, 0);
1673 error = ustorage_fs_mode_sense(sc);
1677 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1678 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1682 error = ustorage_fs_check_cmd(sc, 6,
1687 error = ustorage_fs_prevent_allow(sc);
1692 i = sc->sc_transfer.cmd_data[4];
1693 sc->sc_transfer.cmd_dir = DIR_WRITE;
1694 error = ustorage_fs_min_len(sc,
1695 ((i == 0) ? 256 : i) << 9, 0 - (1 << 9));
1699 error = ustorage_fs_check_cmd(sc, 6,
1700 (7 << 1) | (1 << 4) | 1, 1);
1704 error = ustorage_fs_read(sc);
1709 sc->sc_transfer.cmd_dir = DIR_WRITE;
1710 error = ustorage_fs_min_len(sc,
1711 get_be16(&sc->sc_transfer.cmd_data[7]) << 9, 0 - (1 << 9));
1715 error = ustorage_fs_check_cmd(sc, 10,
1716 (1 << 1) | (0xf << 2) | (3 << 7) | 1, 1);
1720 error = ustorage_fs_read(sc);
1725 sc->sc_transfer.cmd_dir = DIR_WRITE;
1726 error = ustorage_fs_min_len(sc,
1727 get_be32(&sc->sc_transfer.cmd_data[6]) << 9, 0 - (1 << 9));
1731 error = ustorage_fs_check_cmd(sc, 12,
1732 (1 << 1) | (0xf << 2) | (0xf << 6) | 1, 1);
1736 error = ustorage_fs_read(sc);
1740 case SC_READ_CAPACITY:
1741 sc->sc_transfer.cmd_dir = DIR_WRITE;
1742 error = ustorage_fs_check_cmd(sc, 10,
1743 (0xf << 2) | (1 << 8) | 1, 1);
1747 error = ustorage_fs_read_capacity(sc);
1751 case SC_READ_FORMAT_CAPACITIES:
1752 sc->sc_transfer.cmd_dir = DIR_WRITE;
1753 error = ustorage_fs_min_len(sc,
1754 get_be16(&sc->sc_transfer.cmd_data[7]), 0 - 1);
1758 error = ustorage_fs_check_cmd(sc, 10,
1763 error = ustorage_fs_read_format_capacities(sc);
1767 case SC_REQUEST_SENSE:
1768 sc->sc_transfer.cmd_dir = DIR_WRITE;
1769 error = ustorage_fs_min_len(sc, sc->sc_transfer.cmd_data[4], 0 - 1);
1773 error = ustorage_fs_check_cmd(sc, 6,
1778 error = ustorage_fs_request_sense(sc);
1782 case SC_START_STOP_UNIT:
1783 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1787 error = ustorage_fs_check_cmd(sc, 6,
1788 (1 << 1) | (1 << 4) | 1, 0);
1792 error = ustorage_fs_start_stop(sc);
1796 case SC_SYNCHRONIZE_CACHE:
1797 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1801 error = ustorage_fs_check_cmd(sc, 10,
1802 (0xf << 2) | (3 << 7) | 1, 1);
1806 error = ustorage_fs_synchronize_cache(sc);
1810 case SC_TEST_UNIT_READY:
1811 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1815 error = ustorage_fs_check_cmd(sc, 6,
1820 * Although optional, this command is used by MS-Windows.
1821 * We support a minimal version: BytChk must be 0.
1824 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1828 error = ustorage_fs_check_cmd(sc, 10,
1829 (1 << 1) | (0xf << 2) | (3 << 7) | 1, 1);
1833 error = ustorage_fs_verify(sc);
1838 i = sc->sc_transfer.cmd_data[4];
1839 sc->sc_transfer.cmd_dir = DIR_READ;
1840 error = ustorage_fs_min_len(sc,
1841 ((i == 0) ? 256 : i) << 9, 0 - (1 << 9));
1845 error = ustorage_fs_check_cmd(sc, 6,
1846 (7 << 1) | (1 << 4) | 1, 1);
1850 error = ustorage_fs_write(sc);
1855 sc->sc_transfer.cmd_dir = DIR_READ;
1856 error = ustorage_fs_min_len(sc,
1857 get_be16(&sc->sc_transfer.cmd_data[7]) << 9, 0 - (1 << 9));
1861 error = ustorage_fs_check_cmd(sc, 10,
1862 (1 << 1) | (0xf << 2) | (3 << 7) | 1, 1);
1866 error = ustorage_fs_write(sc);
1871 sc->sc_transfer.cmd_dir = DIR_READ;
1872 error = ustorage_fs_min_len(sc,
1873 get_be32(&sc->sc_transfer.cmd_data[6]) << 9, 0 - (1 << 9));
1877 error = ustorage_fs_check_cmd(sc, 12,
1878 (1 << 1) | (0xf << 2) | (0xf << 6) | 1, 1);
1882 error = ustorage_fs_write(sc);
1887 * Some mandatory commands that we recognize but don't
1888 * implement. They don't mean much in this setting.
1889 * It's left as an exercise for anyone interested to
1890 * implement RESERVE and RELEASE in terms of Posix
1893 case SC_FORMAT_UNIT:
1896 case SC_SEND_DIAGNOSTIC:
1900 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1904 error = ustorage_fs_check_cmd(sc, sc->sc_transfer.cmd_len,
1909 sc->sc_transfer.currlun->sense_data =