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/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
56 #include <bus/u4b/usb.h>
57 #include <bus/u4b/usbdi.h>
58 #include <bus/u4b/usbdevs.h>
61 #define USB_DEBUG_VAR ustorage_fs_debug
62 #include <bus/u4b/usb_debug.h>
65 static int ustorage_fs_debug = 0;
67 SYSCTL_NODE(_hw_usb, OID_AUTO, ustorage_fs, CTLFLAG_RW, 0, "USB ustorage_fs");
68 SYSCTL_INT(_hw_usb_ustorage_fs, OID_AUTO, debug, CTLFLAG_RW,
69 &ustorage_fs_debug, 0, "ustorage_fs debug level");
72 /* Define some limits */
74 #ifndef USTORAGE_FS_BULK_SIZE
75 #define USTORAGE_FS_BULK_SIZE (1UL << 17) /* bytes */
78 #ifndef USTORAGE_FS_MAX_LUN
79 #define USTORAGE_FS_MAX_LUN 8 /* units */
82 #ifndef USTORAGE_QDATA_MAX
83 #define USTORAGE_QDATA_MAX 40 /* bytes */
86 #define sc_cmd_data sc_cbw.CBWCDB
89 * The SCSI ID string must be exactly 28 characters long
90 * exluding the terminating zero.
92 #ifndef USTORAGE_FS_ID_STRING
93 #define USTORAGE_FS_ID_STRING \
95 "File-Stor Gadget" /* 16 */ \
100 * The following macro defines the number of
101 * sectors to be allocated for the RAM disk:
103 #ifndef USTORAGE_FS_RAM_SECT
104 #define USTORAGE_FS_RAM_SECT (1UL << 13)
107 static uint8_t *ustorage_fs_ramdisk;
109 /* USB transfer definitions */
111 #define USTORAGE_FS_T_BBB_COMMAND 0
112 #define USTORAGE_FS_T_BBB_DATA_DUMP 1
113 #define USTORAGE_FS_T_BBB_DATA_READ 2
114 #define USTORAGE_FS_T_BBB_DATA_WRITE 3
115 #define USTORAGE_FS_T_BBB_STATUS 4
116 #define USTORAGE_FS_T_BBB_MAX 5
118 /* USB data stage direction */
124 /* USB interface specific control request */
126 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
127 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
129 /* Command Block Wrapper */
131 uDWord dCBWSignature;
132 #define CBWSIGNATURE 0x43425355
134 uDWord dCBWDataTransferLength;
136 #define CBWFLAGS_OUT 0x00
137 #define CBWFLAGS_IN 0x80
140 #define CBWCDBLENGTH 16
141 uByte CBWCDB[CBWCDBLENGTH];
142 } __packed ustorage_fs_bbb_cbw_t;
144 #define USTORAGE_FS_BBB_CBW_SIZE 31
146 /* Command Status Wrapper */
148 uDWord dCSWSignature;
149 #define CSWSIGNATURE 0x53425355
151 uDWord dCSWDataResidue;
153 #define CSWSTATUS_GOOD 0x0
154 #define CSWSTATUS_FAILED 0x1
155 #define CSWSTATUS_PHASE 0x2
156 } __packed ustorage_fs_bbb_csw_t;
158 #define USTORAGE_FS_BBB_CSW_SIZE 13
160 struct ustorage_fs_lun {
162 uint8_t *memory_image;
164 uint32_t num_sectors;
166 uint32_t sense_data_info;
167 uint32_t unit_attention_data;
170 uint8_t prevent_medium_removal:1;
171 uint8_t info_valid:1;
175 struct ustorage_fs_softc {
177 ustorage_fs_bbb_cbw_t sc_cbw; /* Command Wrapper Block */
178 ustorage_fs_bbb_csw_t sc_csw; /* Command Status Block */
182 struct ustorage_fs_lun sc_lun[USTORAGE_FS_MAX_LUN];
186 struct ustorage_fs_lun *currlun;
188 uint32_t data_rem; /* bytes, as reported by the command
190 uint32_t offset; /* bytes */
196 uint8_t data_short:1;
197 uint8_t data_error:1;
201 struct usb_device *sc_udev;
202 struct usb_xfer *sc_xfer[USTORAGE_FS_T_BBB_MAX];
204 uint8_t sc_iface_no; /* interface number */
206 uint8_t sc_last_xfer_index;
207 uint8_t sc_qdata[USTORAGE_QDATA_MAX];
212 static device_probe_t ustorage_fs_probe;
213 static device_attach_t ustorage_fs_attach;
214 static device_detach_t ustorage_fs_detach;
215 static device_suspend_t ustorage_fs_suspend;
216 static device_resume_t ustorage_fs_resume;
217 static usb_handle_request_t ustorage_fs_handle_request;
219 static usb_callback_t ustorage_fs_t_bbb_command_callback;
220 static usb_callback_t ustorage_fs_t_bbb_data_dump_callback;
221 static usb_callback_t ustorage_fs_t_bbb_data_read_callback;
222 static usb_callback_t ustorage_fs_t_bbb_data_write_callback;
223 static usb_callback_t ustorage_fs_t_bbb_status_callback;
225 static void ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index);
226 static void ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc);
228 static uint8_t ustorage_fs_verify(struct ustorage_fs_softc *sc);
229 static uint8_t ustorage_fs_inquiry(struct ustorage_fs_softc *sc);
230 static uint8_t ustorage_fs_request_sense(struct ustorage_fs_softc *sc);
231 static uint8_t ustorage_fs_read_capacity(struct ustorage_fs_softc *sc);
232 static uint8_t ustorage_fs_mode_sense(struct ustorage_fs_softc *sc);
233 static uint8_t ustorage_fs_start_stop(struct ustorage_fs_softc *sc);
234 static uint8_t ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc);
235 static uint8_t ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc);
236 static uint8_t ustorage_fs_mode_select(struct ustorage_fs_softc *sc);
237 static uint8_t ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask);
238 static uint8_t ustorage_fs_read(struct ustorage_fs_softc *sc);
239 static uint8_t ustorage_fs_write(struct ustorage_fs_softc *sc);
240 static uint8_t ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t cmd_size, uint16_t mask, uint8_t needs_medium);
241 static uint8_t ustorage_fs_do_cmd(struct ustorage_fs_softc *sc);
243 static device_method_t ustorage_fs_methods[] = {
245 DEVMETHOD(usb_handle_request, ustorage_fs_handle_request),
247 /* Device interface */
248 DEVMETHOD(device_probe, ustorage_fs_probe),
249 DEVMETHOD(device_attach, ustorage_fs_attach),
250 DEVMETHOD(device_detach, ustorage_fs_detach),
251 DEVMETHOD(device_suspend, ustorage_fs_suspend),
252 DEVMETHOD(device_resume, ustorage_fs_resume),
257 static driver_t ustorage_fs_driver = {
258 .name = "ustorage_fs",
259 .methods = ustorage_fs_methods,
260 .size = sizeof(struct ustorage_fs_softc),
263 static devclass_t ustorage_fs_devclass;
265 DRIVER_MODULE(ustorage_fs, uhub, ustorage_fs_driver, ustorage_fs_devclass, NULL, NULL);
266 MODULE_VERSION(ustorage_fs, 0);
267 MODULE_DEPEND(ustorage_fs, usb, 1, 1, 1);
269 static struct usb_config ustorage_fs_bbb_config[USTORAGE_FS_T_BBB_MAX] = {
271 [USTORAGE_FS_T_BBB_COMMAND] = {
273 .endpoint = UE_ADDR_ANY,
274 .direction = UE_DIR_OUT,
275 .bufsize = sizeof(ustorage_fs_bbb_cbw_t),
276 .flags = {.ext_buffer = 1,},
277 .callback = &ustorage_fs_t_bbb_command_callback,
278 .usb_mode = USB_MODE_DEVICE,
281 [USTORAGE_FS_T_BBB_DATA_DUMP] = {
283 .endpoint = UE_ADDR_ANY,
284 .direction = UE_DIR_OUT,
285 .bufsize = 0, /* use wMaxPacketSize */
286 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
287 .callback = &ustorage_fs_t_bbb_data_dump_callback,
288 .usb_mode = USB_MODE_DEVICE,
291 [USTORAGE_FS_T_BBB_DATA_READ] = {
293 .endpoint = UE_ADDR_ANY,
294 .direction = UE_DIR_OUT,
295 .bufsize = USTORAGE_FS_BULK_SIZE,
296 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
297 .callback = &ustorage_fs_t_bbb_data_read_callback,
298 .usb_mode = USB_MODE_DEVICE,
301 [USTORAGE_FS_T_BBB_DATA_WRITE] = {
303 .endpoint = UE_ADDR_ANY,
304 .direction = UE_DIR_IN,
305 .bufsize = USTORAGE_FS_BULK_SIZE,
306 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer = 1},
307 .callback = &ustorage_fs_t_bbb_data_write_callback,
308 .usb_mode = USB_MODE_DEVICE,
311 [USTORAGE_FS_T_BBB_STATUS] = {
313 .endpoint = UE_ADDR_ANY,
314 .direction = UE_DIR_IN,
315 .bufsize = sizeof(ustorage_fs_bbb_csw_t),
316 .flags = {.short_xfer_ok = 1,.ext_buffer = 1,},
317 .callback = &ustorage_fs_t_bbb_status_callback,
318 .usb_mode = USB_MODE_DEVICE,
323 * USB device probe/attach/detach
327 ustorage_fs_probe(device_t dev)
329 struct usb_attach_arg *uaa = device_get_ivars(dev);
330 struct usb_interface_descriptor *id;
332 if (uaa->usb_mode != USB_MODE_DEVICE) {
335 /* Check for a standards compliant device */
336 id = usbd_get_interface_descriptor(uaa->iface);
338 (id->bInterfaceClass != UICLASS_MASS) ||
339 (id->bInterfaceSubClass != UISUBCLASS_SCSI) ||
340 (id->bInterfaceProtocol != UIPROTO_MASS_BBB)) {
343 return (BUS_PROBE_GENERIC);
347 ustorage_fs_attach(device_t dev)
349 struct ustorage_fs_softc *sc = device_get_softc(dev);
350 struct usb_attach_arg *uaa = device_get_ivars(dev);
351 struct usb_interface_descriptor *id;
356 * NOTE: the softc struct is cleared in device_set_driver.
357 * We can safely call ustorage_fs_detach without specifically
358 * initializing the struct.
362 sc->sc_udev = uaa->device;
363 unit = device_get_unit(dev);
365 /* enable power saving mode */
366 usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE);
369 if (ustorage_fs_ramdisk == NULL) {
371 * allocate a memory image for our ramdisk until
374 ustorage_fs_ramdisk =
375 kmalloc(USTORAGE_FS_RAM_SECT << 9, M_USB,
378 sc->sc_lun[0].memory_image = ustorage_fs_ramdisk;
379 sc->sc_lun[0].num_sectors = USTORAGE_FS_RAM_SECT;
380 sc->sc_lun[0].removable = 1;
383 device_set_usb_desc(dev);
385 lockinit(&sc->sc_lock, "USTORAGE_FS lock", 0, LK_CANRECURSE);
387 /* get interface index */
389 id = usbd_get_interface_descriptor(uaa->iface);
391 device_printf(dev, "failed to get "
392 "interface number\n");
395 sc->sc_iface_no = id->bInterfaceNumber;
397 err = usbd_transfer_setup(uaa->device,
398 &uaa->info.bIfaceIndex, sc->sc_xfer, ustorage_fs_bbb_config,
399 USTORAGE_FS_T_BBB_MAX, sc, &sc->sc_lock);
401 device_printf(dev, "could not setup required "
402 "transfers, %s\n", usbd_errstr(err));
405 /* start Mass Storage State Machine */
407 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
408 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_COMMAND);
409 lockmgr(&sc->sc_lock, LK_RELEASE);
411 return (0); /* success */
414 ustorage_fs_detach(dev);
415 return (ENXIO); /* failure */
419 ustorage_fs_detach(device_t dev)
421 struct ustorage_fs_softc *sc = device_get_softc(dev);
423 /* teardown our statemachine */
425 usbd_transfer_unsetup(sc->sc_xfer, USTORAGE_FS_T_BBB_MAX);
427 lockuninit(&sc->sc_lock);
429 return (0); /* success */
433 ustorage_fs_suspend(device_t dev)
435 device_printf(dev, "suspending\n");
436 return (0); /* success */
440 ustorage_fs_resume(device_t dev)
442 device_printf(dev, "resuming\n");
443 return (0); /* success */
447 * Generic functions to handle transfers
451 ustorage_fs_transfer_start(struct ustorage_fs_softc *sc, uint8_t xfer_index)
453 if (sc->sc_xfer[xfer_index]) {
454 sc->sc_last_xfer_index = xfer_index;
455 usbd_transfer_start(sc->sc_xfer[xfer_index]);
460 ustorage_fs_transfer_stop(struct ustorage_fs_softc *sc)
462 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
463 lockmgr(&sc->sc_lock, LK_RELEASE);
464 usbd_transfer_drain(sc->sc_xfer[sc->sc_last_xfer_index]);
465 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
469 ustorage_fs_handle_request(device_t dev,
470 const void *preq, void **pptr, uint16_t *plen,
471 uint16_t offset, uint8_t *pstate)
473 struct ustorage_fs_softc *sc = device_get_softc(dev);
474 const struct usb_device_request *req = preq;
475 uint8_t is_complete = *pstate;
478 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
479 (req->bRequest == UR_BBB_RESET)) {
481 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
482 ustorage_fs_transfer_stop(sc);
483 sc->sc_transfer.data_error = 1;
484 ustorage_fs_transfer_start(sc,
485 USTORAGE_FS_T_BBB_COMMAND);
486 lockmgr(&sc->sc_lock, LK_RELEASE);
488 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
489 (req->bRequest == UR_BBB_GET_MAX_LUN)) {
492 *pptr = &sc->sc_last_lun;
499 return (ENXIO); /* use builtin handler */
503 ustorage_fs_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
505 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
511 switch (USB_GET_STATE(xfer)) {
512 case USB_ST_TRANSFERRED:
514 tag = UGETDW(sc->sc_cbw.dCBWSignature);
516 if (tag != CBWSIGNATURE) {
518 DPRINTF("invalid signature 0x%08x\n", tag);
521 tag = UGETDW(sc->sc_cbw.dCBWTag);
524 USETDW(sc->sc_csw.dCSWTag, tag);
527 sc->sc_csw.bCSWStatus = 0;
529 /* reset data offset, data length and data remainder */
530 sc->sc_transfer.offset = 0;
531 sc->sc_transfer.data_rem =
532 UGETDW(sc->sc_cbw.dCBWDataTransferLength);
534 /* reset data flags */
535 sc->sc_transfer.data_short = 0;
538 sc->sc_transfer.lun = sc->sc_cbw.bCBWLUN;
540 if (sc->sc_transfer.data_rem == 0) {
541 sc->sc_transfer.cbw_dir = DIR_NONE;
543 if (sc->sc_cbw.bCBWFlags & CBWFLAGS_IN) {
544 sc->sc_transfer.cbw_dir = DIR_WRITE;
546 sc->sc_transfer.cbw_dir = DIR_READ;
550 sc->sc_transfer.cmd_len = sc->sc_cbw.bCDBLength;
551 if ((sc->sc_transfer.cmd_len > sizeof(sc->sc_cbw.CBWCDB)) ||
552 (sc->sc_transfer.cmd_len == 0)) {
553 /* just halt - this is invalid */
554 DPRINTF("invalid command length %d bytes\n",
555 sc->sc_transfer.cmd_len);
559 err = ustorage_fs_do_cmd(sc);
562 DPRINTF("command failed\n");
565 if ((sc->sc_transfer.data_rem > 0) &&
566 (sc->sc_transfer.cbw_dir != sc->sc_transfer.cmd_dir)) {
567 /* contradicting data transfer direction */
569 DPRINTF("data direction mismatch\n");
572 switch (sc->sc_transfer.cbw_dir) {
574 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_READ);
577 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_DATA_WRITE);
580 ustorage_fs_transfer_start(sc,
581 USTORAGE_FS_T_BBB_STATUS);
588 if (sc->sc_transfer.data_error) {
589 sc->sc_transfer.data_error = 0;
590 usbd_xfer_set_stall(xfer);
591 DPRINTF("stall pipe\n");
594 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_cbw,
596 usbd_transfer_submit(xfer);
601 if (error == USB_ERR_CANCELLED) {
604 /* If the pipe is already stalled, don't do another stall */
605 if (!usbd_xfer_is_stalled(xfer))
606 sc->sc_transfer.data_error = 1;
612 if (sc->sc_csw.bCSWStatus == 0) {
613 /* set some default error code */
614 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
616 if (sc->sc_transfer.cbw_dir == DIR_READ) {
618 ustorage_fs_transfer_start(sc,
619 USTORAGE_FS_T_BBB_DATA_DUMP);
622 if (sc->sc_transfer.cbw_dir == DIR_WRITE) {
623 /* need to stall before status */
624 sc->sc_transfer.data_error = 1;
626 ustorage_fs_transfer_start(sc, USTORAGE_FS_T_BBB_STATUS);
631 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer *xfer, usb_error_t error)
633 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
634 uint32_t max_bulk = usbd_xfer_max_len(xfer);
637 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
641 switch (USB_GET_STATE(xfer)) {
642 case USB_ST_TRANSFERRED:
643 sc->sc_transfer.data_rem -= actlen;
644 sc->sc_transfer.offset += actlen;
646 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
647 /* short transfer or end of data */
648 ustorage_fs_transfer_start(sc,
649 USTORAGE_FS_T_BBB_STATUS);
656 if (max_bulk > sc->sc_transfer.data_rem) {
657 max_bulk = sc->sc_transfer.data_rem;
659 if (sc->sc_transfer.data_error) {
660 sc->sc_transfer.data_error = 0;
661 usbd_xfer_set_stall(xfer);
663 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
664 usbd_transfer_submit(xfer);
668 if (error == USB_ERR_CANCELLED) {
672 * If the pipe is already stalled, don't do another stall:
674 if (!usbd_xfer_is_stalled(xfer))
675 sc->sc_transfer.data_error = 1;
683 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
685 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
686 uint32_t max_bulk = usbd_xfer_max_len(xfer);
689 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
693 switch (USB_GET_STATE(xfer)) {
694 case USB_ST_TRANSFERRED:
695 sc->sc_transfer.data_rem -= actlen;
696 sc->sc_transfer.data_ptr += actlen;
697 sc->sc_transfer.offset += actlen;
699 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
700 /* short transfer or end of data */
701 ustorage_fs_transfer_start(sc,
702 USTORAGE_FS_T_BBB_STATUS);
709 if (max_bulk > sc->sc_transfer.data_rem) {
710 max_bulk = sc->sc_transfer.data_rem;
712 if (sc->sc_transfer.data_error) {
713 sc->sc_transfer.data_error = 0;
714 usbd_xfer_set_stall(xfer);
717 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
719 usbd_transfer_submit(xfer);
723 if (error == USB_ERR_CANCELLED) {
726 /* If the pipe is already stalled, don't do another stall */
727 if (!usbd_xfer_is_stalled(xfer))
728 sc->sc_transfer.data_error = 1;
736 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
738 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
739 uint32_t max_bulk = usbd_xfer_max_len(xfer);
742 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
746 switch (USB_GET_STATE(xfer)) {
747 case USB_ST_TRANSFERRED:
748 sc->sc_transfer.data_rem -= actlen;
749 sc->sc_transfer.data_ptr += actlen;
750 sc->sc_transfer.offset += actlen;
752 if (actlen != sumlen || sc->sc_transfer.data_rem == 0) {
753 /* short transfer or end of data */
754 ustorage_fs_transfer_start(sc,
755 USTORAGE_FS_T_BBB_STATUS);
760 if (max_bulk >= sc->sc_transfer.data_rem) {
761 max_bulk = sc->sc_transfer.data_rem;
762 if (sc->sc_transfer.data_short)
763 usbd_xfer_set_flag(xfer, USB_FORCE_SHORT_XFER);
765 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
767 usbd_xfer_clr_flag(xfer, USB_FORCE_SHORT_XFER);
769 if (sc->sc_transfer.data_error) {
770 sc->sc_transfer.data_error = 0;
771 usbd_xfer_set_stall(xfer);
774 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
776 usbd_transfer_submit(xfer);
780 if (error == USB_ERR_CANCELLED) {
784 * If the pipe is already stalled, don't do another
787 if (!usbd_xfer_is_stalled(xfer))
788 sc->sc_transfer.data_error = 1;
796 ustorage_fs_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
798 struct ustorage_fs_softc *sc = usbd_xfer_softc(xfer);
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 usbd_xfer_set_stall(xfer);
817 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_csw,
819 usbd_transfer_submit(xfer);
823 if (error == USB_ERR_CANCELLED) {
826 /* If the pipe is already stalled, don't do another stall */
827 if (!usbd_xfer_is_stalled(xfer))
828 sc->sc_transfer.data_error = 1;
835 /* SCSI commands that we recognize */
836 #define SC_FORMAT_UNIT 0x04
837 #define SC_INQUIRY 0x12
838 #define SC_MODE_SELECT_6 0x15
839 #define SC_MODE_SELECT_10 0x55
840 #define SC_MODE_SENSE_6 0x1a
841 #define SC_MODE_SENSE_10 0x5a
842 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
843 #define SC_READ_6 0x08
844 #define SC_READ_10 0x28
845 #define SC_READ_12 0xa8
846 #define SC_READ_CAPACITY 0x25
847 #define SC_READ_FORMAT_CAPACITIES 0x23
848 #define SC_RELEASE 0x17
849 #define SC_REQUEST_SENSE 0x03
850 #define SC_RESERVE 0x16
851 #define SC_SEND_DIAGNOSTIC 0x1d
852 #define SC_START_STOP_UNIT 0x1b
853 #define SC_SYNCHRONIZE_CACHE 0x35
854 #define SC_TEST_UNIT_READY 0x00
855 #define SC_VERIFY 0x2f
856 #define SC_WRITE_6 0x0a
857 #define SC_WRITE_10 0x2a
858 #define SC_WRITE_12 0xaa
860 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
861 #define SS_NO_SENSE 0
862 #define SS_COMMUNICATION_FAILURE 0x040800
863 #define SS_INVALID_COMMAND 0x052000
864 #define SS_INVALID_FIELD_IN_CDB 0x052400
865 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
866 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
867 #define SS_MEDIUM_NOT_PRESENT 0x023a00
868 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
869 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
870 #define SS_RESET_OCCURRED 0x062900
871 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
872 #define SS_UNRECOVERED_READ_ERROR 0x031100
873 #define SS_WRITE_ERROR 0x030c02
874 #define SS_WRITE_PROTECTED 0x072700
876 #define SK(x) ((uint8_t) ((x) >> 16)) /* Sense Key byte, etc. */
877 #define ASC(x) ((uint8_t) ((x) >> 8))
878 #define ASCQ(x) ((uint8_t) (x))
880 /* Routines for unaligned data access */
883 get_be16(uint8_t *buf)
885 return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
889 get_be32(uint8_t *buf)
891 return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
892 ((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
896 put_be16(uint8_t *buf, uint16_t val)
903 put_be32(uint8_t *buf, uint32_t val)
911 /*------------------------------------------------------------------------*
917 *------------------------------------------------------------------------*/
919 ustorage_fs_verify(struct ustorage_fs_softc *sc)
921 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
924 uint64_t file_offset;
925 uint64_t amount_left;
928 * Get the starting Logical Block Address
930 lba = get_be32(&sc->sc_cmd_data[2]);
933 * We allow DPO (Disable Page Out = don't save data in the cache)
934 * but we don't implement it.
936 if ((sc->sc_cmd_data[1] & ~0x10) != 0) {
937 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
940 vlen = get_be16(&sc->sc_cmd_data[7]);
944 /* No default reply */
946 /* Prepare to carry out the file verify */
956 (vlen > currlun->num_sectors) ||
957 (lba >= currlun->num_sectors)) {
958 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
961 /* XXX TODO: verify that data is readable */
963 return (ustorage_fs_min_len(sc, 0, 0 - 1));
966 /*------------------------------------------------------------------------*
967 * ustorage_fs_inquiry
972 *------------------------------------------------------------------------*/
974 ustorage_fs_inquiry(struct ustorage_fs_softc *sc)
976 uint8_t *buf = sc->sc_transfer.data_ptr;
978 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
980 if (!sc->sc_transfer.currlun) {
981 /* Unsupported LUNs are okay */
984 /* Unsupported, no device - type */
985 return (ustorage_fs_min_len(sc, 36, 0 - 1));
988 /* Non - removable, direct - access device */
989 if (currlun->removable)
992 /* ANSI SCSI level 2 */
994 /* SCSI - 2 INQUIRY data format */
996 /* Additional length */
997 /* No special options */
998 /* Copy in ID string */
999 memcpy(buf + 8, USTORAGE_FS_ID_STRING, 28);
1001 #if (USTORAGE_QDATA_MAX < 36)
1002 #error "(USTORAGE_QDATA_MAX < 36)"
1004 return (ustorage_fs_min_len(sc, 36, 0 - 1));
1007 /*------------------------------------------------------------------------*
1008 * ustorage_fs_request_sense
1013 *------------------------------------------------------------------------*/
1015 ustorage_fs_request_sense(struct ustorage_fs_softc *sc)
1017 uint8_t *buf = sc->sc_transfer.data_ptr;
1018 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1024 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1026 * If a REQUEST SENSE command is received from an initiator
1027 * with a pending unit attention condition (before the target
1028 * generates the contingent allegiance condition), then the
1029 * target shall either:
1030 * a) report any pending sense data and preserve the unit
1031 * attention condition on the logical unit, or,
1032 * b) report the unit attention condition, may discard any
1033 * pending sense data, and clear the unit attention
1034 * condition on the logical unit for that initiator.
1036 * FSG normally uses option a); enable this code to use option b).
1039 if (currlun && currlun->unit_attention_data != SS_NO_SENSE) {
1040 currlun->sense_data = currlun->unit_attention_data;
1041 currlun->unit_attention_data = SS_NO_SENSE;
1046 /* Unsupported LUNs are okay */
1047 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1051 sd = currlun->sense_data;
1052 sdinfo = currlun->sense_data_info;
1053 valid = currlun->info_valid << 7;
1054 currlun->sense_data = SS_NO_SENSE;
1055 currlun->sense_data_info = 0;
1056 currlun->info_valid = 0;
1060 buf[0] = valid | 0x70;
1061 /* Valid, current error */
1063 put_be32(&buf[3], sdinfo);
1064 /* Sense information */
1066 /* Additional sense length */
1070 #if (USTORAGE_QDATA_MAX < 18)
1071 #error "(USTORAGE_QDATA_MAX < 18)"
1073 return (ustorage_fs_min_len(sc, 18, 0 - 1));
1076 /*------------------------------------------------------------------------*
1077 * ustorage_fs_read_capacity
1082 *------------------------------------------------------------------------*/
1084 ustorage_fs_read_capacity(struct ustorage_fs_softc *sc)
1086 uint8_t *buf = sc->sc_transfer.data_ptr;
1087 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1088 uint32_t lba = get_be32(&sc->sc_cmd_data[2]);
1089 uint8_t pmi = sc->sc_cmd_data[8];
1091 /* Check the PMI and LBA fields */
1092 if ((pmi > 1) || ((pmi == 0) && (lba != 0))) {
1093 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1096 /* Max logical block */
1097 put_be32(&buf[0], currlun->num_sectors - 1);
1099 put_be32(&buf[4], 512);
1101 #if (USTORAGE_QDATA_MAX < 8)
1102 #error "(USTORAGE_QDATA_MAX < 8)"
1104 return (ustorage_fs_min_len(sc, 8, 0 - 1));
1107 /*------------------------------------------------------------------------*
1108 * ustorage_fs_mode_sense
1113 *------------------------------------------------------------------------*/
1115 ustorage_fs_mode_sense(struct ustorage_fs_softc *sc)
1117 uint8_t *buf = sc->sc_transfer.data_ptr;
1118 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1122 uint8_t mscmnd = sc->sc_cmd_data[0];
1125 uint8_t changeable_values;
1130 if ((sc->sc_cmd_data[1] & ~0x08) != 0) {
1132 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1135 pc = sc->sc_cmd_data[2] >> 6;
1136 page_code = sc->sc_cmd_data[2] & 0x3f;
1138 currlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1141 changeable_values = (pc == 1);
1142 all_pages = (page_code == 0x3f);
1145 * Write the mode parameter header. Fixed values are: default
1146 * medium type, no cache control (DPOFUA), and no block descriptors.
1147 * The only variable value is the WriteProtect bit. We will fill in
1148 * the mode data length later.
1151 if (mscmnd == SC_MODE_SENSE_6) {
1152 buf[2] = (currlun->read_only ? 0x80 : 0x00);
1157 /* SC_MODE_SENSE_10 */
1158 buf[3] = (currlun->read_only ? 0x80 : 0x00);
1162 /* Should really be mod_data.buflen */
1165 /* No block descriptors */
1168 * The mode pages, in numerical order.
1170 if ((page_code == 0x08) || all_pages) {
1175 memset(buf + 2, 0, 10);
1176 /* None of the fields are changeable */
1178 if (!changeable_values) {
1180 /* Write cache enable, */
1181 /* Read cache not disabled */
1182 /* No cache retention priorities */
1183 put_be16(&buf[4], 0xffff);
1184 /* Don 't disable prefetch */
1185 /* Minimum prefetch = 0 */
1186 put_be16(&buf[8], 0xffff);
1187 /* Maximum prefetch */
1188 put_be16(&buf[10], 0xffff);
1189 /* Maximum prefetch ceiling */
1194 * Check that a valid page was requested and the mode data length
1199 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1202 /* Store the mode data length */
1203 if (mscmnd == SC_MODE_SENSE_6)
1206 put_be16(buf0, len - 2);
1208 #if (USTORAGE_QDATA_MAX < 24)
1209 #error "(USTORAGE_QDATA_MAX < 24)"
1211 return (ustorage_fs_min_len(sc, len, 0 - 1));
1214 /*------------------------------------------------------------------------*
1215 * ustorage_fs_start_stop
1220 *------------------------------------------------------------------------*/
1222 ustorage_fs_start_stop(struct ustorage_fs_softc *sc)
1224 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1229 if (!currlun->removable) {
1230 currlun->sense_data = SS_INVALID_COMMAND;
1233 immed = sc->sc_cmd_data[1] & 0x01;
1234 loej = sc->sc_cmd_data[4] & 0x02;
1235 start = sc->sc_cmd_data[4] & 0x01;
1237 if (immed || loej || start) {
1243 /*------------------------------------------------------------------------*
1244 * ustorage_fs_prevent_allow
1249 *------------------------------------------------------------------------*/
1251 ustorage_fs_prevent_allow(struct ustorage_fs_softc *sc)
1253 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1256 if (!currlun->removable) {
1257 currlun->sense_data = SS_INVALID_COMMAND;
1260 prevent = sc->sc_cmd_data[4] & 0x01;
1261 if ((sc->sc_cmd_data[4] & ~0x01) != 0) {
1262 /* Mask away Prevent */
1263 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1266 if (currlun->prevent_medium_removal && !prevent) {
1267 //fsync_sub(currlun);
1269 currlun->prevent_medium_removal = prevent;
1273 /*------------------------------------------------------------------------*
1274 * ustorage_fs_read_format_capacities
1279 *------------------------------------------------------------------------*/
1281 ustorage_fs_read_format_capacities(struct ustorage_fs_softc *sc)
1283 uint8_t *buf = sc->sc_transfer.data_ptr;
1284 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1286 buf[0] = buf[1] = buf[2] = 0;
1288 /* Only the Current / Maximum Capacity Descriptor */
1291 /* Number of blocks */
1292 put_be32(&buf[0], currlun->num_sectors);
1294 put_be32(&buf[4], 512);
1295 /* Current capacity */
1298 #if (USTORAGE_QDATA_MAX < 12)
1299 #error "(USTORAGE_QDATA_MAX < 12)"
1301 return (ustorage_fs_min_len(sc, 12, 0 - 1));
1304 /*------------------------------------------------------------------------*
1305 * ustorage_fs_mode_select
1310 *------------------------------------------------------------------------*/
1312 ustorage_fs_mode_select(struct ustorage_fs_softc *sc)
1314 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1316 /* We don't support MODE SELECT */
1317 currlun->sense_data = SS_INVALID_COMMAND;
1321 /*------------------------------------------------------------------------*
1322 * ustorage_fs_synchronize_cache
1327 *------------------------------------------------------------------------*/
1329 ustorage_fs_synchronize_cache(struct ustorage_fs_softc *sc)
1332 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1336 * We ignore the requested LBA and write out all dirty data buffers.
1340 currlun->sense_data = SS_WRITE_ERROR;
1346 /*------------------------------------------------------------------------*
1347 * ustorage_fs_read - read data from disk
1352 *------------------------------------------------------------------------*/
1354 ustorage_fs_read(struct ustorage_fs_softc *sc)
1356 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1357 uint64_t file_offset;
1362 * Get the starting Logical Block Address and check that it's not
1365 if (sc->sc_cmd_data[0] == SC_READ_6) {
1366 lba = (((uint32_t)sc->sc_cmd_data[1]) << 16) |
1367 get_be16(&sc->sc_cmd_data[2]);
1369 lba = get_be32(&sc->sc_cmd_data[2]);
1372 * We allow DPO (Disable Page Out = don't save data in the
1373 * cache) and FUA (Force Unit Access = don't read from the
1374 * cache), but we don't implement them.
1376 if ((sc->sc_cmd_data[1] & ~0x18) != 0) {
1377 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1381 len = sc->sc_transfer.data_rem >> 9;
1385 (len > currlun->num_sectors) ||
1386 (lba >= currlun->num_sectors)) {
1387 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1393 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1398 /*------------------------------------------------------------------------*
1399 * ustorage_fs_write - write data to disk
1404 *------------------------------------------------------------------------*/
1406 ustorage_fs_write(struct ustorage_fs_softc *sc)
1408 struct ustorage_fs_lun *currlun = sc->sc_transfer.currlun;
1409 uint64_t file_offset;
1413 if (currlun->read_only) {
1414 currlun->sense_data = SS_WRITE_PROTECTED;
1417 /* XXX clear SYNC */
1420 * Get the starting Logical Block Address and check that it's not
1423 if (sc->sc_cmd_data[0] == SC_WRITE_6)
1424 lba = (((uint32_t)sc->sc_cmd_data[1]) << 16) |
1425 get_be16(&sc->sc_cmd_data[2]);
1427 lba = get_be32(&sc->sc_cmd_data[2]);
1430 * We allow DPO (Disable Page Out = don't save data in the
1431 * cache) and FUA (Force Unit Access = write directly to the
1432 * medium). We don't implement DPO; we implement FUA by
1433 * performing synchronous output.
1435 if ((sc->sc_cmd_data[1] & ~0x18) != 0) {
1436 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1439 if (sc->sc_cmd_data[1] & 0x08) {
1441 /* XXX set SYNC flag here */
1445 len = sc->sc_transfer.data_rem >> 9;
1449 (len > currlun->num_sectors) ||
1450 (lba >= currlun->num_sectors)) {
1451 currlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1457 sc->sc_transfer.data_ptr = currlun->memory_image + file_offset;
1462 /*------------------------------------------------------------------------*
1463 * ustorage_fs_min_len
1468 *------------------------------------------------------------------------*/
1470 ustorage_fs_min_len(struct ustorage_fs_softc *sc, uint32_t len, uint32_t mask)
1472 if (len != sc->sc_transfer.data_rem) {
1474 if (sc->sc_transfer.cbw_dir == DIR_READ) {
1476 * there must be something wrong about this SCSI
1479 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1482 /* compute the minimum length */
1484 if (sc->sc_transfer.data_rem > len) {
1485 /* data ends prematurely */
1486 sc->sc_transfer.data_rem = len;
1487 sc->sc_transfer.data_short = 1;
1489 /* check length alignment */
1491 if (sc->sc_transfer.data_rem & ~mask) {
1492 /* data ends prematurely */
1493 sc->sc_transfer.data_rem &= mask;
1494 sc->sc_transfer.data_short = 1;
1500 /*------------------------------------------------------------------------*
1501 * ustorage_fs_check_cmd - check command routine
1503 * Check whether the command is properly formed and whether its data
1504 * size and direction agree with the values we already have.
1509 *------------------------------------------------------------------------*/
1511 ustorage_fs_check_cmd(struct ustorage_fs_softc *sc, uint8_t min_cmd_size,
1512 uint16_t mask, uint8_t needs_medium)
1514 struct ustorage_fs_lun *currlun;
1515 uint8_t lun = (sc->sc_cmd_data[1] >> 5);
1518 /* Verify the length of the command itself */
1519 if (min_cmd_size > sc->sc_transfer.cmd_len) {
1520 DPRINTF("%u > %u\n",
1521 min_cmd_size, sc->sc_transfer.cmd_len);
1522 sc->sc_csw.bCSWStatus = CSWSTATUS_PHASE;
1525 /* Mask away the LUN */
1526 sc->sc_cmd_data[1] &= 0x1f;
1528 /* Check if LUN is correct */
1529 if (lun != sc->sc_transfer.lun) {
1533 if (sc->sc_transfer.lun <= sc->sc_last_lun) {
1534 sc->sc_transfer.currlun = currlun =
1535 sc->sc_lun + sc->sc_transfer.lun;
1536 if (sc->sc_cmd_data[0] != SC_REQUEST_SENSE) {
1537 currlun->sense_data = SS_NO_SENSE;
1538 currlun->sense_data_info = 0;
1539 currlun->info_valid = 0;
1542 * If a unit attention condition exists, only INQUIRY
1543 * and REQUEST SENSE commands are allowed. Anything
1546 if ((currlun->unit_attention_data != SS_NO_SENSE) &&
1547 (sc->sc_cmd_data[0] != SC_INQUIRY) &&
1548 (sc->sc_cmd_data[0] != SC_REQUEST_SENSE)) {
1549 currlun->sense_data = currlun->unit_attention_data;
1550 currlun->unit_attention_data = SS_NO_SENSE;
1554 sc->sc_transfer.currlun = currlun = NULL;
1557 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1558 * to use unsupported LUNs; all others may not.
1560 if ((sc->sc_cmd_data[0] != SC_INQUIRY) &&
1561 (sc->sc_cmd_data[0] != SC_REQUEST_SENSE)) {
1567 * Check that only command bytes listed in the mask are
1570 for (i = 0; i != min_cmd_size; i++) {
1571 if (sc->sc_cmd_data[i] && !(mask & (1UL << i))) {
1573 currlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1580 * If the medium isn't mounted and the command needs to access
1581 * it, return an error.
1583 if (currlun && (!currlun->memory_image) && needs_medium) {
1584 currlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1590 /*------------------------------------------------------------------------*
1591 * ustorage_fs_do_cmd - do command
1596 *------------------------------------------------------------------------*/
1598 ustorage_fs_do_cmd(struct ustorage_fs_softc *sc)
1603 const uint32_t mask9 = (0xFFFFFFFFUL >> 9) << 9;
1605 /* set default data transfer pointer */
1606 sc->sc_transfer.data_ptr = sc->sc_qdata;
1608 DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1609 sc->sc_cmd_data[0], sc->sc_transfer.data_rem);
1611 switch (sc->sc_cmd_data[0]) {
1613 sc->sc_transfer.cmd_dir = DIR_WRITE;
1614 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1618 error = ustorage_fs_check_cmd(sc, 6,
1623 error = ustorage_fs_inquiry(sc);
1627 case SC_MODE_SELECT_6:
1628 sc->sc_transfer.cmd_dir = DIR_READ;
1629 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1633 error = ustorage_fs_check_cmd(sc, 6,
1634 (1UL << 1) | (1UL << 4) | 1, 0);
1638 error = ustorage_fs_mode_select(sc);
1642 case SC_MODE_SELECT_10:
1643 sc->sc_transfer.cmd_dir = DIR_READ;
1644 error = ustorage_fs_min_len(sc,
1645 get_be16(&sc->sc_cmd_data[7]), 0 - 1);
1649 error = ustorage_fs_check_cmd(sc, 10,
1650 (1UL << 1) | (3UL << 7) | 1, 0);
1654 error = ustorage_fs_mode_select(sc);
1658 case SC_MODE_SENSE_6:
1659 sc->sc_transfer.cmd_dir = DIR_WRITE;
1660 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1664 error = ustorage_fs_check_cmd(sc, 6,
1665 (1UL << 1) | (1UL << 2) | (1UL << 4) | 1, 0);
1669 error = ustorage_fs_mode_sense(sc);
1673 case SC_MODE_SENSE_10:
1674 sc->sc_transfer.cmd_dir = DIR_WRITE;
1675 error = ustorage_fs_min_len(sc,
1676 get_be16(&sc->sc_cmd_data[7]), 0 - 1);
1680 error = ustorage_fs_check_cmd(sc, 10,
1681 (1UL << 1) | (1UL << 2) | (3UL << 7) | 1, 0);
1685 error = ustorage_fs_mode_sense(sc);
1689 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1690 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1694 error = ustorage_fs_check_cmd(sc, 6,
1699 error = ustorage_fs_prevent_allow(sc);
1704 i = sc->sc_cmd_data[4];
1705 sc->sc_transfer.cmd_dir = DIR_WRITE;
1706 temp = ((i == 0) ? 256UL : i);
1707 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1711 error = ustorage_fs_check_cmd(sc, 6,
1712 (7UL << 1) | (1UL << 4) | 1, 1);
1716 error = ustorage_fs_read(sc);
1721 sc->sc_transfer.cmd_dir = DIR_WRITE;
1722 temp = get_be16(&sc->sc_cmd_data[7]);
1723 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1727 error = ustorage_fs_check_cmd(sc, 10,
1728 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1732 error = ustorage_fs_read(sc);
1737 sc->sc_transfer.cmd_dir = DIR_WRITE;
1738 temp = get_be32(&sc->sc_cmd_data[6]);
1739 if (temp >= (1UL << (32 - 9))) {
1740 /* numerical overflow */
1741 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
1745 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1749 error = ustorage_fs_check_cmd(sc, 12,
1750 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1754 error = ustorage_fs_read(sc);
1758 case SC_READ_CAPACITY:
1759 sc->sc_transfer.cmd_dir = DIR_WRITE;
1760 error = ustorage_fs_check_cmd(sc, 10,
1761 (0xfUL << 2) | (1UL << 8) | 1, 1);
1765 error = ustorage_fs_read_capacity(sc);
1769 case SC_READ_FORMAT_CAPACITIES:
1770 sc->sc_transfer.cmd_dir = DIR_WRITE;
1771 error = ustorage_fs_min_len(sc,
1772 get_be16(&sc->sc_cmd_data[7]), 0 - 1);
1776 error = ustorage_fs_check_cmd(sc, 10,
1781 error = ustorage_fs_read_format_capacities(sc);
1785 case SC_REQUEST_SENSE:
1786 sc->sc_transfer.cmd_dir = DIR_WRITE;
1787 error = ustorage_fs_min_len(sc, sc->sc_cmd_data[4], 0 - 1);
1791 error = ustorage_fs_check_cmd(sc, 6,
1796 error = ustorage_fs_request_sense(sc);
1800 case SC_START_STOP_UNIT:
1801 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1805 error = ustorage_fs_check_cmd(sc, 6,
1806 (1UL << 1) | (1UL << 4) | 1, 0);
1810 error = ustorage_fs_start_stop(sc);
1814 case SC_SYNCHRONIZE_CACHE:
1815 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1819 error = ustorage_fs_check_cmd(sc, 10,
1820 (0xfUL << 2) | (3UL << 7) | 1, 1);
1824 error = ustorage_fs_synchronize_cache(sc);
1828 case SC_TEST_UNIT_READY:
1829 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1833 error = ustorage_fs_check_cmd(sc, 6,
1838 * Although optional, this command is used by MS-Windows.
1839 * We support a minimal version: BytChk must be 0.
1842 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1846 error = ustorage_fs_check_cmd(sc, 10,
1847 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1851 error = ustorage_fs_verify(sc);
1856 i = sc->sc_cmd_data[4];
1857 sc->sc_transfer.cmd_dir = DIR_READ;
1858 temp = ((i == 0) ? 256UL : i);
1859 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1863 error = ustorage_fs_check_cmd(sc, 6,
1864 (7UL << 1) | (1UL << 4) | 1, 1);
1868 error = ustorage_fs_write(sc);
1873 sc->sc_transfer.cmd_dir = DIR_READ;
1874 temp = get_be16(&sc->sc_cmd_data[7]);
1875 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1879 error = ustorage_fs_check_cmd(sc, 10,
1880 (1UL << 1) | (0xfUL << 2) | (3UL << 7) | 1, 1);
1884 error = ustorage_fs_write(sc);
1889 sc->sc_transfer.cmd_dir = DIR_READ;
1890 temp = get_be32(&sc->sc_cmd_data[6]);
1891 if (temp > (mask9 >> 9)) {
1892 /* numerical overflow */
1893 sc->sc_csw.bCSWStatus = CSWSTATUS_FAILED;
1897 error = ustorage_fs_min_len(sc, temp << 9, mask9);
1901 error = ustorage_fs_check_cmd(sc, 12,
1902 (1UL << 1) | (0xfUL << 2) | (0xfUL << 6) | 1, 1);
1906 error = ustorage_fs_write(sc);
1911 * Some mandatory commands that we recognize but don't
1912 * implement. They don't mean much in this setting.
1913 * It's left as an exercise for anyone interested to
1914 * implement RESERVE and RELEASE in terms of Posix
1917 case SC_FORMAT_UNIT:
1920 case SC_SEND_DIAGNOSTIC:
1924 error = ustorage_fs_min_len(sc, 0, 0 - 1);
1928 error = ustorage_fs_check_cmd(sc, sc->sc_transfer.cmd_len,
1933 sc->sc_transfer.currlun->sense_data =