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>
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 */
179 void *sc_dma_ptr
; /* Main data buffer */
183 struct ustorage_fs_lun sc_lun
[USTORAGE_FS_MAX_LUN
];
187 struct ustorage_fs_lun
*currlun
;
189 uint32_t data_rem
; /* bytes, as reported by the command
191 uint32_t offset
; /* bytes */
197 uint8_t data_short
:1;
198 uint8_t data_error
:1;
202 struct usb_device
*sc_udev
;
203 struct usb_xfer
*sc_xfer
[USTORAGE_FS_T_BBB_MAX
];
205 uint8_t sc_iface_no
; /* interface number */
207 uint8_t sc_last_xfer_index
;
208 uint8_t sc_qdata
[USTORAGE_QDATA_MAX
];
213 static device_probe_t ustorage_fs_probe
;
214 static device_attach_t ustorage_fs_attach
;
215 static device_detach_t ustorage_fs_detach
;
216 static device_suspend_t ustorage_fs_suspend
;
217 static device_resume_t ustorage_fs_resume
;
218 static usb_handle_request_t ustorage_fs_handle_request
;
220 static usb_callback_t ustorage_fs_t_bbb_command_callback
;
221 static usb_callback_t ustorage_fs_t_bbb_data_dump_callback
;
222 static usb_callback_t ustorage_fs_t_bbb_data_read_callback
;
223 static usb_callback_t ustorage_fs_t_bbb_data_write_callback
;
224 static usb_callback_t ustorage_fs_t_bbb_status_callback
;
226 static void ustorage_fs_transfer_start(struct ustorage_fs_softc
*sc
, uint8_t xfer_index
);
227 static void ustorage_fs_transfer_stop(struct ustorage_fs_softc
*sc
);
229 static uint8_t ustorage_fs_verify(struct ustorage_fs_softc
*sc
);
230 static uint8_t ustorage_fs_inquiry(struct ustorage_fs_softc
*sc
);
231 static uint8_t ustorage_fs_request_sense(struct ustorage_fs_softc
*sc
);
232 static uint8_t ustorage_fs_read_capacity(struct ustorage_fs_softc
*sc
);
233 static uint8_t ustorage_fs_mode_sense(struct ustorage_fs_softc
*sc
);
234 static uint8_t ustorage_fs_start_stop(struct ustorage_fs_softc
*sc
);
235 static uint8_t ustorage_fs_prevent_allow(struct ustorage_fs_softc
*sc
);
236 static uint8_t ustorage_fs_read_format_capacities(struct ustorage_fs_softc
*sc
);
237 static uint8_t ustorage_fs_mode_select(struct ustorage_fs_softc
*sc
);
238 static uint8_t ustorage_fs_min_len(struct ustorage_fs_softc
*sc
, uint32_t len
, uint32_t mask
);
239 static uint8_t ustorage_fs_read(struct ustorage_fs_softc
*sc
);
240 static uint8_t ustorage_fs_write(struct ustorage_fs_softc
*sc
);
241 static uint8_t ustorage_fs_check_cmd(struct ustorage_fs_softc
*sc
, uint8_t cmd_size
, uint16_t mask
, uint8_t needs_medium
);
242 static uint8_t ustorage_fs_do_cmd(struct ustorage_fs_softc
*sc
);
244 static device_method_t ustorage_fs_methods
[] = {
246 DEVMETHOD(usb_handle_request
, ustorage_fs_handle_request
),
248 /* Device interface */
249 DEVMETHOD(device_probe
, ustorage_fs_probe
),
250 DEVMETHOD(device_attach
, ustorage_fs_attach
),
251 DEVMETHOD(device_detach
, ustorage_fs_detach
),
252 DEVMETHOD(device_suspend
, ustorage_fs_suspend
),
253 DEVMETHOD(device_resume
, ustorage_fs_resume
),
258 static driver_t ustorage_fs_driver
= {
259 .name
= "ustorage_fs",
260 .methods
= ustorage_fs_methods
,
261 .size
= sizeof(struct ustorage_fs_softc
),
264 static devclass_t ustorage_fs_devclass
;
266 DRIVER_MODULE(ustorage_fs
, uhub
, ustorage_fs_driver
, ustorage_fs_devclass
, NULL
, NULL
);
267 MODULE_VERSION(ustorage_fs
, 0);
268 MODULE_DEPEND(ustorage_fs
, usb
, 1, 1, 1);
270 static struct usb_config ustorage_fs_bbb_config
[USTORAGE_FS_T_BBB_MAX
] = {
272 [USTORAGE_FS_T_BBB_COMMAND
] = {
274 .endpoint
= UE_ADDR_ANY
,
275 .direction
= UE_DIR_OUT
,
276 .bufsize
= sizeof(ustorage_fs_bbb_cbw_t
),
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},
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},
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 if (ustorage_fs_ramdisk
== NULL
) {
382 sc
->sc_lun
[0].memory_image
= ustorage_fs_ramdisk
;
383 sc
->sc_lun
[0].num_sectors
= USTORAGE_FS_RAM_SECT
;
384 sc
->sc_lun
[0].removable
= 1;
387 device_set_usb_desc(dev
);
389 lockinit(&sc
->sc_lock
, "USTORAGE_FS lock", 0, LK_CANRECURSE
);
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_lock
);
405 device_printf(dev
, "could not setup required "
406 "transfers, %s\n", usbd_errstr(err
));
410 sc
->sc_cbw
= usbd_xfer_get_frame_buffer(sc
->sc_xfer
[
411 USTORAGE_FS_T_BBB_COMMAND
], 0);
412 sc
->sc_csw
= usbd_xfer_get_frame_buffer(sc
->sc_xfer
[
413 USTORAGE_FS_T_BBB_STATUS
], 0);
414 sc
->sc_dma_ptr
= usbd_xfer_get_frame_buffer(sc
->sc_xfer
[
415 USTORAGE_FS_T_BBB_DATA_READ
], 0);
417 /* start Mass Storage State Machine */
419 lockmgr(&sc
->sc_lock
, LK_EXCLUSIVE
);
420 ustorage_fs_transfer_start(sc
, USTORAGE_FS_T_BBB_COMMAND
);
421 lockmgr(&sc
->sc_lock
, LK_RELEASE
);
423 return (0); /* success */
426 ustorage_fs_detach(dev
);
427 return (ENXIO
); /* failure */
431 ustorage_fs_detach(device_t dev
)
433 struct ustorage_fs_softc
*sc
= device_get_softc(dev
);
435 /* teardown our statemachine */
437 usbd_transfer_unsetup(sc
->sc_xfer
, USTORAGE_FS_T_BBB_MAX
);
439 lockuninit(&sc
->sc_lock
);
441 return (0); /* success */
445 ustorage_fs_suspend(device_t dev
)
447 device_printf(dev
, "suspending\n");
448 return (0); /* success */
452 ustorage_fs_resume(device_t dev
)
454 device_printf(dev
, "resuming\n");
455 return (0); /* success */
459 * Generic functions to handle transfers
463 ustorage_fs_transfer_start(struct ustorage_fs_softc
*sc
, uint8_t xfer_index
)
465 if (sc
->sc_xfer
[xfer_index
]) {
466 sc
->sc_last_xfer_index
= xfer_index
;
467 usbd_transfer_start(sc
->sc_xfer
[xfer_index
]);
472 ustorage_fs_transfer_stop(struct ustorage_fs_softc
*sc
)
474 usbd_transfer_stop(sc
->sc_xfer
[sc
->sc_last_xfer_index
]);
475 lockmgr(&sc
->sc_lock
, LK_RELEASE
);
476 usbd_transfer_drain(sc
->sc_xfer
[sc
->sc_last_xfer_index
]);
477 lockmgr(&sc
->sc_lock
, LK_EXCLUSIVE
);
481 ustorage_fs_handle_request(device_t dev
,
482 const void *preq
, void **pptr
, uint16_t *plen
,
483 uint16_t offset
, uint8_t *pstate
)
485 struct ustorage_fs_softc
*sc
= device_get_softc(dev
);
486 const struct usb_device_request
*req
= preq
;
487 uint8_t is_complete
= *pstate
;
490 if ((req
->bmRequestType
== UT_WRITE_CLASS_INTERFACE
) &&
491 (req
->bRequest
== UR_BBB_RESET
)) {
493 lockmgr(&sc
->sc_lock
, LK_EXCLUSIVE
);
494 ustorage_fs_transfer_stop(sc
);
495 sc
->sc_transfer
.data_error
= 1;
496 ustorage_fs_transfer_start(sc
,
497 USTORAGE_FS_T_BBB_COMMAND
);
498 lockmgr(&sc
->sc_lock
, LK_RELEASE
);
500 } else if ((req
->bmRequestType
== UT_READ_CLASS_INTERFACE
) &&
501 (req
->bRequest
== UR_BBB_GET_MAX_LUN
)) {
504 *pptr
= &sc
->sc_last_lun
;
511 return (ENXIO
); /* use builtin handler */
515 ustorage_fs_t_bbb_command_callback(struct usb_xfer
*xfer
, usb_error_t error
)
517 struct ustorage_fs_softc
*sc
= usbd_xfer_softc(xfer
);
523 switch (USB_GET_STATE(xfer
)) {
524 case USB_ST_TRANSFERRED
:
526 tag
= UGETDW(sc
->sc_cbw
->dCBWSignature
);
528 if (tag
!= CBWSIGNATURE
) {
530 DPRINTF("invalid signature 0x%08x\n", tag
);
533 tag
= UGETDW(sc
->sc_cbw
->dCBWTag
);
536 USETDW(sc
->sc_csw
->dCSWTag
, tag
);
539 sc
->sc_csw
->bCSWStatus
= 0;
541 /* reset data offset, data length and data remainder */
542 sc
->sc_transfer
.offset
= 0;
543 sc
->sc_transfer
.data_rem
=
544 UGETDW(sc
->sc_cbw
->dCBWDataTransferLength
);
546 /* reset data flags */
547 sc
->sc_transfer
.data_short
= 0;
550 sc
->sc_transfer
.lun
= sc
->sc_cbw
->bCBWLUN
;
552 if (sc
->sc_transfer
.data_rem
== 0) {
553 sc
->sc_transfer
.cbw_dir
= DIR_NONE
;
555 if (sc
->sc_cbw
->bCBWFlags
& CBWFLAGS_IN
) {
556 sc
->sc_transfer
.cbw_dir
= DIR_WRITE
;
558 sc
->sc_transfer
.cbw_dir
= DIR_READ
;
562 sc
->sc_transfer
.cmd_len
= sc
->sc_cbw
->bCDBLength
;
563 if ((sc
->sc_transfer
.cmd_len
> sizeof(sc
->sc_cbw
->CBWCDB
)) ||
564 (sc
->sc_transfer
.cmd_len
== 0)) {
565 /* just halt - this is invalid */
566 DPRINTF("invalid command length %d bytes\n",
567 sc
->sc_transfer
.cmd_len
);
571 err
= ustorage_fs_do_cmd(sc
);
574 DPRINTF("command failed\n");
577 if ((sc
->sc_transfer
.data_rem
> 0) &&
578 (sc
->sc_transfer
.cbw_dir
!= sc
->sc_transfer
.cmd_dir
)) {
579 /* contradicting data transfer direction */
581 DPRINTF("data direction mismatch\n");
584 switch (sc
->sc_transfer
.cbw_dir
) {
586 ustorage_fs_transfer_start(sc
, USTORAGE_FS_T_BBB_DATA_READ
);
589 ustorage_fs_transfer_start(sc
, USTORAGE_FS_T_BBB_DATA_WRITE
);
592 ustorage_fs_transfer_start(sc
,
593 USTORAGE_FS_T_BBB_STATUS
);
600 if (sc
->sc_transfer
.data_error
) {
601 sc
->sc_transfer
.data_error
= 0;
602 usbd_xfer_set_stall(xfer
);
603 DPRINTF("stall pipe\n");
605 usbd_xfer_set_frame_len(xfer
, 0,
606 sizeof(ustorage_fs_bbb_cbw_t
));
607 usbd_transfer_submit(xfer
);
612 if (error
== USB_ERR_CANCELLED
) {
615 /* If the pipe is already stalled, don't do another stall */
616 if (!usbd_xfer_is_stalled(xfer
))
617 sc
->sc_transfer
.data_error
= 1;
623 if (sc
->sc_csw
->bCSWStatus
== 0) {
624 /* set some default error code */
625 sc
->sc_csw
->bCSWStatus
= CSWSTATUS_FAILED
;
627 if (sc
->sc_transfer
.cbw_dir
== DIR_READ
) {
629 ustorage_fs_transfer_start(sc
,
630 USTORAGE_FS_T_BBB_DATA_DUMP
);
633 if (sc
->sc_transfer
.cbw_dir
== DIR_WRITE
) {
634 /* need to stall before status */
635 sc
->sc_transfer
.data_error
= 1;
637 ustorage_fs_transfer_start(sc
, USTORAGE_FS_T_BBB_STATUS
);
642 ustorage_fs_t_bbb_data_dump_callback(struct usb_xfer
*xfer
, usb_error_t error
)
644 struct ustorage_fs_softc
*sc
= usbd_xfer_softc(xfer
);
645 uint32_t max_bulk
= usbd_xfer_max_len(xfer
);
648 usbd_xfer_status(xfer
, &actlen
, &sumlen
, NULL
, NULL
);
652 switch (USB_GET_STATE(xfer
)) {
653 case USB_ST_TRANSFERRED
:
654 sc
->sc_transfer
.data_rem
-= actlen
;
655 sc
->sc_transfer
.offset
+= actlen
;
657 if (actlen
!= sumlen
|| sc
->sc_transfer
.data_rem
== 0) {
658 /* short transfer or end of data */
659 ustorage_fs_transfer_start(sc
,
660 USTORAGE_FS_T_BBB_STATUS
);
667 if (max_bulk
> sc
->sc_transfer
.data_rem
) {
668 max_bulk
= sc
->sc_transfer
.data_rem
;
670 if (sc
->sc_transfer
.data_error
) {
671 sc
->sc_transfer
.data_error
= 0;
672 usbd_xfer_set_stall(xfer
);
674 usbd_xfer_set_frame_len(xfer
, 0, max_bulk
);
675 usbd_transfer_submit(xfer
);
679 if (error
== USB_ERR_CANCELLED
) {
683 * If the pipe is already stalled, don't do another stall:
685 if (!usbd_xfer_is_stalled(xfer
))
686 sc
->sc_transfer
.data_error
= 1;
694 ustorage_fs_t_bbb_data_read_callback(struct usb_xfer
*xfer
, usb_error_t error
)
696 struct ustorage_fs_softc
*sc
= usbd_xfer_softc(xfer
);
697 uint32_t max_bulk
= usbd_xfer_max_len(xfer
);
700 usbd_xfer_status(xfer
, &actlen
, &sumlen
, NULL
, NULL
);
704 switch (USB_GET_STATE(xfer
)) {
705 case USB_ST_TRANSFERRED
:
706 /* XXX copy data from DMA buffer */
707 memcpy(sc
->sc_transfer
.data_ptr
, sc
->sc_dma_ptr
, actlen
);
709 sc
->sc_transfer
.data_rem
-= actlen
;
710 sc
->sc_transfer
.data_ptr
+= actlen
;
711 sc
->sc_transfer
.offset
+= actlen
;
713 if (actlen
!= sumlen
|| sc
->sc_transfer
.data_rem
== 0) {
714 /* short transfer or end of data */
715 ustorage_fs_transfer_start(sc
,
716 USTORAGE_FS_T_BBB_STATUS
);
723 if (max_bulk
> sc
->sc_transfer
.data_rem
) {
724 max_bulk
= sc
->sc_transfer
.data_rem
;
726 if (sc
->sc_transfer
.data_error
) {
727 sc
->sc_transfer
.data_error
= 0;
728 usbd_xfer_set_stall(xfer
);
731 usbd_xfer_set_frame_data(xfer
, 0, sc
->sc_dma_ptr
, max_bulk
);
732 usbd_transfer_submit(xfer
);
736 if (error
== USB_ERR_CANCELLED
) {
739 /* If the pipe is already stalled, don't do another stall */
740 if (!usbd_xfer_is_stalled(xfer
))
741 sc
->sc_transfer
.data_error
= 1;
749 ustorage_fs_t_bbb_data_write_callback(struct usb_xfer
*xfer
, usb_error_t error
)
751 struct ustorage_fs_softc
*sc
= usbd_xfer_softc(xfer
);
752 uint32_t max_bulk
= usbd_xfer_max_len(xfer
);
755 usbd_xfer_status(xfer
, &actlen
, &sumlen
, NULL
, NULL
);
759 switch (USB_GET_STATE(xfer
)) {
760 case USB_ST_TRANSFERRED
:
761 sc
->sc_transfer
.data_rem
-= actlen
;
762 sc
->sc_transfer
.data_ptr
+= actlen
;
763 sc
->sc_transfer
.offset
+= actlen
;
765 if (actlen
!= sumlen
|| sc
->sc_transfer
.data_rem
== 0) {
766 /* short transfer or end of data */
767 ustorage_fs_transfer_start(sc
,
768 USTORAGE_FS_T_BBB_STATUS
);
773 if (max_bulk
>= sc
->sc_transfer
.data_rem
) {
774 max_bulk
= sc
->sc_transfer
.data_rem
;
775 if (sc
->sc_transfer
.data_short
)
776 usbd_xfer_set_flag(xfer
, USB_FORCE_SHORT_XFER
);
778 usbd_xfer_clr_flag(xfer
, USB_FORCE_SHORT_XFER
);
780 usbd_xfer_clr_flag(xfer
, USB_FORCE_SHORT_XFER
);
782 if (sc
->sc_transfer
.data_error
) {
783 sc
->sc_transfer
.data_error
= 0;
784 usbd_xfer_set_stall(xfer
);
787 /* XXX copy data to DMA buffer */
788 memcpy(sc
->sc_dma_ptr
, sc
->sc_transfer
.data_ptr
, max_bulk
);
790 usbd_xfer_set_frame_data(xfer
, 0, sc
->sc_dma_ptr
, max_bulk
);
791 usbd_transfer_submit(xfer
);
795 if (error
== USB_ERR_CANCELLED
) {
799 * If the pipe is already stalled, don't do another
802 if (!usbd_xfer_is_stalled(xfer
))
803 sc
->sc_transfer
.data_error
= 1;
811 ustorage_fs_t_bbb_status_callback(struct usb_xfer
*xfer
, usb_error_t error
)
813 struct ustorage_fs_softc
*sc
= usbd_xfer_softc(xfer
);
817 switch (USB_GET_STATE(xfer
)) {
818 case USB_ST_TRANSFERRED
:
819 ustorage_fs_transfer_start(sc
, USTORAGE_FS_T_BBB_COMMAND
);
824 USETDW(sc
->sc_csw
->dCSWSignature
, CSWSIGNATURE
);
825 USETDW(sc
->sc_csw
->dCSWDataResidue
, sc
->sc_transfer
.data_rem
);
827 if (sc
->sc_transfer
.data_error
) {
828 sc
->sc_transfer
.data_error
= 0;
829 usbd_xfer_set_stall(xfer
);
831 usbd_xfer_set_frame_len(xfer
, 0,
832 sizeof(ustorage_fs_bbb_csw_t
));
833 usbd_transfer_submit(xfer
);
837 if (error
== USB_ERR_CANCELLED
) {
840 /* If the pipe is already stalled, don't do another stall */
841 if (!usbd_xfer_is_stalled(xfer
))
842 sc
->sc_transfer
.data_error
= 1;
849 /* SCSI commands that we recognize */
850 #define SC_FORMAT_UNIT 0x04
851 #define SC_INQUIRY 0x12
852 #define SC_MODE_SELECT_6 0x15
853 #define SC_MODE_SELECT_10 0x55
854 #define SC_MODE_SENSE_6 0x1a
855 #define SC_MODE_SENSE_10 0x5a
856 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
857 #define SC_READ_6 0x08
858 #define SC_READ_10 0x28
859 #define SC_READ_12 0xa8
860 #define SC_READ_CAPACITY 0x25
861 #define SC_READ_FORMAT_CAPACITIES 0x23
862 #define SC_RELEASE 0x17
863 #define SC_REQUEST_SENSE 0x03
864 #define SC_RESERVE 0x16
865 #define SC_SEND_DIAGNOSTIC 0x1d
866 #define SC_START_STOP_UNIT 0x1b
867 #define SC_SYNCHRONIZE_CACHE 0x35
868 #define SC_TEST_UNIT_READY 0x00
869 #define SC_VERIFY 0x2f
870 #define SC_WRITE_6 0x0a
871 #define SC_WRITE_10 0x2a
872 #define SC_WRITE_12 0xaa
874 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
875 #define SS_NO_SENSE 0
876 #define SS_COMMUNICATION_FAILURE 0x040800
877 #define SS_INVALID_COMMAND 0x052000
878 #define SS_INVALID_FIELD_IN_CDB 0x052400
879 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
880 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
881 #define SS_MEDIUM_NOT_PRESENT 0x023a00
882 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
883 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
884 #define SS_RESET_OCCURRED 0x062900
885 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
886 #define SS_UNRECOVERED_READ_ERROR 0x031100
887 #define SS_WRITE_ERROR 0x030c02
888 #define SS_WRITE_PROTECTED 0x072700
890 #define SK(x) ((uint8_t) ((x) >> 16)) /* Sense Key byte, etc. */
891 #define ASC(x) ((uint8_t) ((x) >> 8))
892 #define ASCQ(x) ((uint8_t) (x))
894 /* Routines for unaligned data access */
897 get_be16(uint8_t *buf
)
899 return ((uint16_t)buf
[0] << 8) | ((uint16_t)buf
[1]);
903 get_be32(uint8_t *buf
)
905 return ((uint32_t)buf
[0] << 24) | ((uint32_t)buf
[1] << 16) |
906 ((uint32_t)buf
[2] << 8) | ((uint32_t)buf
[3]);
910 put_be16(uint8_t *buf
, uint16_t val
)
917 put_be32(uint8_t *buf
, uint32_t val
)
925 /*------------------------------------------------------------------------*
931 *------------------------------------------------------------------------*/
933 ustorage_fs_verify(struct ustorage_fs_softc
*sc
)
935 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
938 uint64_t file_offset
;
939 uint64_t amount_left
;
942 * Get the starting Logical Block Address
944 lba
= get_be32(&sc
->sc_cbw
->CBWCDB
[2]);
947 * We allow DPO (Disable Page Out = don't save data in the cache)
948 * but we don't implement it.
950 if ((sc
->sc_cbw
->CBWCDB
[1] & ~0x10) != 0) {
951 currlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
954 vlen
= get_be16(&sc
->sc_cbw
->CBWCDB
[7]);
958 /* No default reply */
960 /* Prepare to carry out the file verify */
970 (vlen
> currlun
->num_sectors
) ||
971 (lba
>= currlun
->num_sectors
)) {
972 currlun
->sense_data
= SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
975 /* XXX TODO: verify that data is readable */
977 return (ustorage_fs_min_len(sc
, 0, -1U));
980 /*------------------------------------------------------------------------*
981 * ustorage_fs_inquiry
986 *------------------------------------------------------------------------*/
988 ustorage_fs_inquiry(struct ustorage_fs_softc
*sc
)
990 uint8_t *buf
= sc
->sc_transfer
.data_ptr
;
992 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
994 if (!sc
->sc_transfer
.currlun
) {
995 /* Unsupported LUNs are okay */
998 /* Unsupported, no device - type */
999 return (ustorage_fs_min_len(sc
, 36, -1U));
1002 /* Non - removable, direct - access device */
1003 if (currlun
->removable
)
1006 /* ANSI SCSI level 2 */
1008 /* SCSI - 2 INQUIRY data format */
1010 /* Additional length */
1011 /* No special options */
1012 /* Copy in ID string */
1013 memcpy(buf
+ 8, USTORAGE_FS_ID_STRING
, 28);
1015 #if (USTORAGE_QDATA_MAX < 36)
1016 #error "(USTORAGE_QDATA_MAX < 36)"
1018 return (ustorage_fs_min_len(sc
, 36, -1U));
1021 /*------------------------------------------------------------------------*
1022 * ustorage_fs_request_sense
1027 *------------------------------------------------------------------------*/
1029 ustorage_fs_request_sense(struct ustorage_fs_softc
*sc
)
1031 uint8_t *buf
= sc
->sc_transfer
.data_ptr
;
1032 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1038 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1040 * If a REQUEST SENSE command is received from an initiator
1041 * with a pending unit attention condition (before the target
1042 * generates the contingent allegiance condition), then the
1043 * target shall either:
1044 * a) report any pending sense data and preserve the unit
1045 * attention condition on the logical unit, or,
1046 * b) report the unit attention condition, may discard any
1047 * pending sense data, and clear the unit attention
1048 * condition on the logical unit for that initiator.
1050 * FSG normally uses option a); enable this code to use option b).
1053 if (currlun
&& currlun
->unit_attention_data
!= SS_NO_SENSE
) {
1054 currlun
->sense_data
= currlun
->unit_attention_data
;
1055 currlun
->unit_attention_data
= SS_NO_SENSE
;
1060 /* Unsupported LUNs are okay */
1061 sd
= SS_LOGICAL_UNIT_NOT_SUPPORTED
;
1065 sd
= currlun
->sense_data
;
1066 sdinfo
= currlun
->sense_data_info
;
1067 valid
= currlun
->info_valid
<< 7;
1068 currlun
->sense_data
= SS_NO_SENSE
;
1069 currlun
->sense_data_info
= 0;
1070 currlun
->info_valid
= 0;
1074 buf
[0] = valid
| 0x70;
1075 /* Valid, current error */
1077 put_be32(&buf
[3], sdinfo
);
1078 /* Sense information */
1080 /* Additional sense length */
1084 #if (USTORAGE_QDATA_MAX < 18)
1085 #error "(USTORAGE_QDATA_MAX < 18)"
1087 return (ustorage_fs_min_len(sc
, 18, -1U));
1090 /*------------------------------------------------------------------------*
1091 * ustorage_fs_read_capacity
1096 *------------------------------------------------------------------------*/
1098 ustorage_fs_read_capacity(struct ustorage_fs_softc
*sc
)
1100 uint8_t *buf
= sc
->sc_transfer
.data_ptr
;
1101 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1102 uint32_t lba
= get_be32(&sc
->sc_cbw
->CBWCDB
[2]);
1103 uint8_t pmi
= sc
->sc_cbw
->CBWCDB
[8];
1105 /* Check the PMI and LBA fields */
1106 if ((pmi
> 1) || ((pmi
== 0) && (lba
!= 0))) {
1107 currlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1110 /* Max logical block */
1111 put_be32(&buf
[0], currlun
->num_sectors
- 1);
1113 put_be32(&buf
[4], 512);
1115 #if (USTORAGE_QDATA_MAX < 8)
1116 #error "(USTORAGE_QDATA_MAX < 8)"
1118 return (ustorage_fs_min_len(sc
, 8, -1U));
1121 /*------------------------------------------------------------------------*
1122 * ustorage_fs_mode_sense
1127 *------------------------------------------------------------------------*/
1129 ustorage_fs_mode_sense(struct ustorage_fs_softc
*sc
)
1131 uint8_t *buf
= sc
->sc_transfer
.data_ptr
;
1132 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1136 uint8_t mscmnd
= sc
->sc_cbw
->CBWCDB
[0];
1139 uint8_t changeable_values
;
1144 if ((sc
->sc_cbw
->CBWCDB
[1] & ~0x08) != 0) {
1146 currlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1149 pc
= sc
->sc_cbw
->CBWCDB
[2] >> 6;
1150 page_code
= sc
->sc_cbw
->CBWCDB
[2] & 0x3f;
1152 currlun
->sense_data
= SS_SAVING_PARAMETERS_NOT_SUPPORTED
;
1155 changeable_values
= (pc
== 1);
1156 all_pages
= (page_code
== 0x3f);
1159 * Write the mode parameter header. Fixed values are: default
1160 * medium type, no cache control (DPOFUA), and no block descriptors.
1161 * The only variable value is the WriteProtect bit. We will fill in
1162 * the mode data length later.
1165 if (mscmnd
== SC_MODE_SENSE_6
) {
1166 buf
[2] = (currlun
->read_only
? 0x80 : 0x00);
1171 /* SC_MODE_SENSE_10 */
1172 buf
[3] = (currlun
->read_only
? 0x80 : 0x00);
1176 /* Should really be mod_data.buflen */
1179 /* No block descriptors */
1182 * The mode pages, in numerical order.
1184 if ((page_code
== 0x08) || all_pages
) {
1189 memset(buf
+ 2, 0, 10);
1190 /* None of the fields are changeable */
1192 if (!changeable_values
) {
1194 /* Write cache enable, */
1195 /* Read cache not disabled */
1196 /* No cache retention priorities */
1197 put_be16(&buf
[4], 0xffff);
1198 /* Don 't disable prefetch */
1199 /* Minimum prefetch = 0 */
1200 put_be16(&buf
[8], 0xffff);
1201 /* Maximum prefetch */
1202 put_be16(&buf
[10], 0xffff);
1203 /* Maximum prefetch ceiling */
1208 * Check that a valid page was requested and the mode data length
1213 currlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1216 /* Store the mode data length */
1217 if (mscmnd
== SC_MODE_SENSE_6
)
1220 put_be16(buf0
, len
- 2);
1222 #if (USTORAGE_QDATA_MAX < 24)
1223 #error "(USTORAGE_QDATA_MAX < 24)"
1225 return (ustorage_fs_min_len(sc
, len
, -1U));
1228 /*------------------------------------------------------------------------*
1229 * ustorage_fs_start_stop
1234 *------------------------------------------------------------------------*/
1236 ustorage_fs_start_stop(struct ustorage_fs_softc
*sc
)
1238 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1243 if (!currlun
->removable
) {
1244 currlun
->sense_data
= SS_INVALID_COMMAND
;
1247 immed
= sc
->sc_cbw
->CBWCDB
[1] & 0x01;
1248 loej
= sc
->sc_cbw
->CBWCDB
[4] & 0x02;
1249 start
= sc
->sc_cbw
->CBWCDB
[4] & 0x01;
1251 if (immed
|| loej
|| start
) {
1257 /*------------------------------------------------------------------------*
1258 * ustorage_fs_prevent_allow
1263 *------------------------------------------------------------------------*/
1265 ustorage_fs_prevent_allow(struct ustorage_fs_softc
*sc
)
1267 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1270 if (!currlun
->removable
) {
1271 currlun
->sense_data
= SS_INVALID_COMMAND
;
1274 prevent
= sc
->sc_cbw
->CBWCDB
[4] & 0x01;
1275 if ((sc
->sc_cbw
->CBWCDB
[4] & ~0x01) != 0) {
1276 /* Mask away Prevent */
1277 currlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1280 if (currlun
->prevent_medium_removal
&& !prevent
) {
1281 //fsync_sub(currlun);
1283 currlun
->prevent_medium_removal
= prevent
;
1287 /*------------------------------------------------------------------------*
1288 * ustorage_fs_read_format_capacities
1293 *------------------------------------------------------------------------*/
1295 ustorage_fs_read_format_capacities(struct ustorage_fs_softc
*sc
)
1297 uint8_t *buf
= sc
->sc_transfer
.data_ptr
;
1298 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1300 buf
[0] = buf
[1] = buf
[2] = 0;
1302 /* Only the Current / Maximum Capacity Descriptor */
1305 /* Number of blocks */
1306 put_be32(&buf
[0], currlun
->num_sectors
);
1308 put_be32(&buf
[4], 512);
1309 /* Current capacity */
1312 #if (USTORAGE_QDATA_MAX < 12)
1313 #error "(USTORAGE_QDATA_MAX < 12)"
1315 return (ustorage_fs_min_len(sc
, 12, -1U));
1318 /*------------------------------------------------------------------------*
1319 * ustorage_fs_mode_select
1324 *------------------------------------------------------------------------*/
1326 ustorage_fs_mode_select(struct ustorage_fs_softc
*sc
)
1328 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1330 /* We don't support MODE SELECT */
1331 currlun
->sense_data
= SS_INVALID_COMMAND
;
1335 /*------------------------------------------------------------------------*
1336 * ustorage_fs_synchronize_cache
1341 *------------------------------------------------------------------------*/
1343 ustorage_fs_synchronize_cache(struct ustorage_fs_softc
*sc
)
1346 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1350 * We ignore the requested LBA and write out all dirty data buffers.
1354 currlun
->sense_data
= SS_WRITE_ERROR
;
1360 /*------------------------------------------------------------------------*
1361 * ustorage_fs_read - read data from disk
1366 *------------------------------------------------------------------------*/
1368 ustorage_fs_read(struct ustorage_fs_softc
*sc
)
1370 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1371 uint64_t file_offset
;
1376 * Get the starting Logical Block Address and check that it's not
1379 if (sc
->sc_cbw
->CBWCDB
[0] == SC_READ_6
) {
1380 lba
= (((uint32_t)sc
->sc_cbw
->CBWCDB
[1]) << 16) |
1381 get_be16(&sc
->sc_cbw
->CBWCDB
[2]);
1383 lba
= get_be32(&sc
->sc_cbw
->CBWCDB
[2]);
1386 * We allow DPO (Disable Page Out = don't save data in the
1387 * cache) and FUA (Force Unit Access = don't read from the
1388 * cache), but we don't implement them.
1390 if ((sc
->sc_cbw
->CBWCDB
[1] & ~0x18) != 0) {
1391 currlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1395 len
= sc
->sc_transfer
.data_rem
>> 9;
1399 (len
> currlun
->num_sectors
) ||
1400 (lba
>= currlun
->num_sectors
)) {
1401 currlun
->sense_data
= SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
1407 sc
->sc_transfer
.data_ptr
= currlun
->memory_image
+ file_offset
;
1412 /*------------------------------------------------------------------------*
1413 * ustorage_fs_write - write data to disk
1418 *------------------------------------------------------------------------*/
1420 ustorage_fs_write(struct ustorage_fs_softc
*sc
)
1422 struct ustorage_fs_lun
*currlun
= sc
->sc_transfer
.currlun
;
1423 uint64_t file_offset
;
1427 if (currlun
->read_only
) {
1428 currlun
->sense_data
= SS_WRITE_PROTECTED
;
1431 /* XXX clear SYNC */
1434 * Get the starting Logical Block Address and check that it's not
1437 if (sc
->sc_cbw
->CBWCDB
[0] == SC_WRITE_6
)
1438 lba
= (((uint32_t)sc
->sc_cbw
->CBWCDB
[1]) << 16) |
1439 get_be16(&sc
->sc_cbw
->CBWCDB
[2]);
1441 lba
= get_be32(&sc
->sc_cbw
->CBWCDB
[2]);
1444 * We allow DPO (Disable Page Out = don't save data in the
1445 * cache) and FUA (Force Unit Access = write directly to the
1446 * medium). We don't implement DPO; we implement FUA by
1447 * performing synchronous output.
1449 if ((sc
->sc_cbw
->CBWCDB
[1] & ~0x18) != 0) {
1450 currlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1453 if (sc
->sc_cbw
->CBWCDB
[1] & 0x08) {
1455 /* XXX set SYNC flag here */
1459 len
= sc
->sc_transfer
.data_rem
>> 9;
1463 (len
> currlun
->num_sectors
) ||
1464 (lba
>= currlun
->num_sectors
)) {
1465 currlun
->sense_data
= SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
1471 sc
->sc_transfer
.data_ptr
= currlun
->memory_image
+ file_offset
;
1476 /*------------------------------------------------------------------------*
1477 * ustorage_fs_min_len
1482 *------------------------------------------------------------------------*/
1484 ustorage_fs_min_len(struct ustorage_fs_softc
*sc
, uint32_t len
, uint32_t mask
)
1486 if (len
!= sc
->sc_transfer
.data_rem
) {
1488 if (sc
->sc_transfer
.cbw_dir
== DIR_READ
) {
1490 * there must be something wrong about this SCSI
1493 sc
->sc_csw
->bCSWStatus
= CSWSTATUS_PHASE
;
1496 /* compute the minimum length */
1498 if (sc
->sc_transfer
.data_rem
> len
) {
1499 /* data ends prematurely */
1500 sc
->sc_transfer
.data_rem
= len
;
1501 sc
->sc_transfer
.data_short
= 1;
1503 /* check length alignment */
1505 if (sc
->sc_transfer
.data_rem
& ~mask
) {
1506 /* data ends prematurely */
1507 sc
->sc_transfer
.data_rem
&= mask
;
1508 sc
->sc_transfer
.data_short
= 1;
1514 /*------------------------------------------------------------------------*
1515 * ustorage_fs_check_cmd - check command routine
1517 * Check whether the command is properly formed and whether its data
1518 * size and direction agree with the values we already have.
1523 *------------------------------------------------------------------------*/
1525 ustorage_fs_check_cmd(struct ustorage_fs_softc
*sc
, uint8_t min_cmd_size
,
1526 uint16_t mask
, uint8_t needs_medium
)
1528 struct ustorage_fs_lun
*currlun
;
1529 uint8_t lun
= (sc
->sc_cbw
->CBWCDB
[1] >> 5);
1532 /* Verify the length of the command itself */
1533 if (min_cmd_size
> sc
->sc_transfer
.cmd_len
) {
1534 DPRINTF("%u > %u\n",
1535 min_cmd_size
, sc
->sc_transfer
.cmd_len
);
1536 sc
->sc_csw
->bCSWStatus
= CSWSTATUS_PHASE
;
1539 /* Mask away the LUN */
1540 sc
->sc_cbw
->CBWCDB
[1] &= 0x1f;
1542 /* Check if LUN is correct */
1543 if (lun
!= sc
->sc_transfer
.lun
) {
1547 if (sc
->sc_transfer
.lun
<= sc
->sc_last_lun
) {
1548 sc
->sc_transfer
.currlun
= currlun
=
1549 sc
->sc_lun
+ sc
->sc_transfer
.lun
;
1550 if (sc
->sc_cbw
->CBWCDB
[0] != SC_REQUEST_SENSE
) {
1551 currlun
->sense_data
= SS_NO_SENSE
;
1552 currlun
->sense_data_info
= 0;
1553 currlun
->info_valid
= 0;
1556 * If a unit attention condition exists, only INQUIRY
1557 * and REQUEST SENSE commands are allowed. Anything
1560 if ((currlun
->unit_attention_data
!= SS_NO_SENSE
) &&
1561 (sc
->sc_cbw
->CBWCDB
[0] != SC_INQUIRY
) &&
1562 (sc
->sc_cbw
->CBWCDB
[0] != SC_REQUEST_SENSE
)) {
1563 currlun
->sense_data
= currlun
->unit_attention_data
;
1564 currlun
->unit_attention_data
= SS_NO_SENSE
;
1568 sc
->sc_transfer
.currlun
= currlun
= NULL
;
1571 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1572 * to use unsupported LUNs; all others may not.
1574 if ((sc
->sc_cbw
->CBWCDB
[0] != SC_INQUIRY
) &&
1575 (sc
->sc_cbw
->CBWCDB
[0] != SC_REQUEST_SENSE
)) {
1581 * Check that only command bytes listed in the mask are
1584 for (i
= 0; i
!= min_cmd_size
; i
++) {
1585 if (sc
->sc_cbw
->CBWCDB
[i
] && !(mask
& (1UL << i
))) {
1587 currlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1594 * If the medium isn't mounted and the command needs to access
1595 * it, return an error.
1597 if (currlun
&& (!currlun
->memory_image
) && needs_medium
) {
1598 currlun
->sense_data
= SS_MEDIUM_NOT_PRESENT
;
1604 /*------------------------------------------------------------------------*
1605 * ustorage_fs_do_cmd - do command
1610 *------------------------------------------------------------------------*/
1612 ustorage_fs_do_cmd(struct ustorage_fs_softc
*sc
)
1617 const uint32_t mask9
= (0xFFFFFFFFUL
>> 9) << 9;
1619 /* set default data transfer pointer */
1620 sc
->sc_transfer
.data_ptr
= sc
->sc_qdata
;
1622 DPRINTF("cmd_data[0]=0x%02x, data_rem=0x%08x\n",
1623 sc
->sc_cbw
->CBWCDB
[0], sc
->sc_transfer
.data_rem
);
1625 switch (sc
->sc_cbw
->CBWCDB
[0]) {
1627 sc
->sc_transfer
.cmd_dir
= DIR_WRITE
;
1628 error
= ustorage_fs_min_len(sc
, sc
->sc_cbw
->CBWCDB
[4], -1U);
1632 error
= ustorage_fs_check_cmd(sc
, 6,
1637 error
= ustorage_fs_inquiry(sc
);
1641 case SC_MODE_SELECT_6
:
1642 sc
->sc_transfer
.cmd_dir
= DIR_READ
;
1643 error
= ustorage_fs_min_len(sc
, sc
->sc_cbw
->CBWCDB
[4], -1U);
1647 error
= ustorage_fs_check_cmd(sc
, 6,
1648 (1UL << 1) | (1UL << 4) | 1, 0);
1652 error
= ustorage_fs_mode_select(sc
);
1656 case SC_MODE_SELECT_10
:
1657 sc
->sc_transfer
.cmd_dir
= DIR_READ
;
1658 error
= ustorage_fs_min_len(sc
,
1659 get_be16(&sc
->sc_cbw
->CBWCDB
[7]), -1U);
1663 error
= ustorage_fs_check_cmd(sc
, 10,
1664 (1UL << 1) | (3UL << 7) | 1, 0);
1668 error
= ustorage_fs_mode_select(sc
);
1672 case SC_MODE_SENSE_6
:
1673 sc
->sc_transfer
.cmd_dir
= DIR_WRITE
;
1674 error
= ustorage_fs_min_len(sc
, sc
->sc_cbw
->CBWCDB
[4], -1U);
1678 error
= ustorage_fs_check_cmd(sc
, 6,
1679 (1UL << 1) | (1UL << 2) | (1UL << 4) | 1, 0);
1683 error
= ustorage_fs_mode_sense(sc
);
1687 case SC_MODE_SENSE_10
:
1688 sc
->sc_transfer
.cmd_dir
= DIR_WRITE
;
1689 error
= ustorage_fs_min_len(sc
,
1690 get_be16(&sc
->sc_cbw
->CBWCDB
[7]), -1U);
1694 error
= ustorage_fs_check_cmd(sc
, 10,
1695 (1UL << 1) | (1UL << 2) | (3UL << 7) | 1, 0);
1699 error
= ustorage_fs_mode_sense(sc
);
1703 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL
:
1704 error
= ustorage_fs_min_len(sc
, 0, -1U);
1708 error
= ustorage_fs_check_cmd(sc
, 6,
1713 error
= ustorage_fs_prevent_allow(sc
);
1718 i
= sc
->sc_cbw
->CBWCDB
[4];
1719 sc
->sc_transfer
.cmd_dir
= DIR_WRITE
;
1720 temp
= ((i
== 0) ? 256UL : i
);
1721 error
= ustorage_fs_min_len(sc
, temp
<< 9, mask9
);
1725 error
= ustorage_fs_check_cmd(sc
, 6,
1726 (7UL << 1) | (1UL << 4) | 1, 1);
1730 error
= ustorage_fs_read(sc
);
1735 sc
->sc_transfer
.cmd_dir
= DIR_WRITE
;
1736 temp
= get_be16(&sc
->sc_cbw
->CBWCDB
[7]);
1737 error
= ustorage_fs_min_len(sc
, temp
<< 9, mask9
);
1741 error
= ustorage_fs_check_cmd(sc
, 10,
1742 (1UL << 1) | (0xfUL
<< 2) | (3UL << 7) | 1, 1);
1746 error
= ustorage_fs_read(sc
);
1751 sc
->sc_transfer
.cmd_dir
= DIR_WRITE
;
1752 temp
= get_be32(&sc
->sc_cbw
->CBWCDB
[6]);
1753 if (temp
>= (1UL << (32 - 9))) {
1754 /* numerical overflow */
1755 sc
->sc_csw
->bCSWStatus
= CSWSTATUS_FAILED
;
1759 error
= ustorage_fs_min_len(sc
, temp
<< 9, mask9
);
1763 error
= ustorage_fs_check_cmd(sc
, 12,
1764 (1UL << 1) | (0xfUL
<< 2) | (0xfUL
<< 6) | 1, 1);
1768 error
= ustorage_fs_read(sc
);
1772 case SC_READ_CAPACITY
:
1773 sc
->sc_transfer
.cmd_dir
= DIR_WRITE
;
1774 error
= ustorage_fs_check_cmd(sc
, 10,
1775 (0xfUL
<< 2) | (1UL << 8) | 1, 1);
1779 error
= ustorage_fs_read_capacity(sc
);
1783 case SC_READ_FORMAT_CAPACITIES
:
1784 sc
->sc_transfer
.cmd_dir
= DIR_WRITE
;
1785 error
= ustorage_fs_min_len(sc
,
1786 get_be16(&sc
->sc_cbw
->CBWCDB
[7]), -1U);
1790 error
= ustorage_fs_check_cmd(sc
, 10,
1795 error
= ustorage_fs_read_format_capacities(sc
);
1799 case SC_REQUEST_SENSE
:
1800 sc
->sc_transfer
.cmd_dir
= DIR_WRITE
;
1801 error
= ustorage_fs_min_len(sc
, sc
->sc_cbw
->CBWCDB
[4], -1U);
1805 error
= ustorage_fs_check_cmd(sc
, 6,
1810 error
= ustorage_fs_request_sense(sc
);
1814 case SC_START_STOP_UNIT
:
1815 error
= ustorage_fs_min_len(sc
, 0, -1U);
1819 error
= ustorage_fs_check_cmd(sc
, 6,
1820 (1UL << 1) | (1UL << 4) | 1, 0);
1824 error
= ustorage_fs_start_stop(sc
);
1828 case SC_SYNCHRONIZE_CACHE
:
1829 error
= ustorage_fs_min_len(sc
, 0, -1U);
1833 error
= ustorage_fs_check_cmd(sc
, 10,
1834 (0xfUL
<< 2) | (3UL << 7) | 1, 1);
1838 error
= ustorage_fs_synchronize_cache(sc
);
1842 case SC_TEST_UNIT_READY
:
1843 error
= ustorage_fs_min_len(sc
, 0, -1U);
1847 error
= ustorage_fs_check_cmd(sc
, 6,
1852 * Although optional, this command is used by MS-Windows.
1853 * We support a minimal version: BytChk must be 0.
1856 error
= ustorage_fs_min_len(sc
, 0, -1U);
1860 error
= ustorage_fs_check_cmd(sc
, 10,
1861 (1UL << 1) | (0xfUL
<< 2) | (3UL << 7) | 1, 1);
1865 error
= ustorage_fs_verify(sc
);
1870 i
= sc
->sc_cbw
->CBWCDB
[4];
1871 sc
->sc_transfer
.cmd_dir
= DIR_READ
;
1872 temp
= ((i
== 0) ? 256UL : i
);
1873 error
= ustorage_fs_min_len(sc
, temp
<< 9, mask9
);
1877 error
= ustorage_fs_check_cmd(sc
, 6,
1878 (7UL << 1) | (1UL << 4) | 1, 1);
1882 error
= ustorage_fs_write(sc
);
1887 sc
->sc_transfer
.cmd_dir
= DIR_READ
;
1888 temp
= get_be16(&sc
->sc_cbw
->CBWCDB
[7]);
1889 error
= ustorage_fs_min_len(sc
, temp
<< 9, mask9
);
1893 error
= ustorage_fs_check_cmd(sc
, 10,
1894 (1UL << 1) | (0xfUL
<< 2) | (3UL << 7) | 1, 1);
1898 error
= ustorage_fs_write(sc
);
1903 sc
->sc_transfer
.cmd_dir
= DIR_READ
;
1904 temp
= get_be32(&sc
->sc_cbw
->CBWCDB
[6]);
1905 if (temp
> (mask9
>> 9)) {
1906 /* numerical overflow */
1907 sc
->sc_csw
->bCSWStatus
= CSWSTATUS_FAILED
;
1911 error
= ustorage_fs_min_len(sc
, temp
<< 9, mask9
);
1915 error
= ustorage_fs_check_cmd(sc
, 12,
1916 (1UL << 1) | (0xfUL
<< 2) | (0xfUL
<< 6) | 1, 1);
1920 error
= ustorage_fs_write(sc
);
1925 * Some mandatory commands that we recognize but don't
1926 * implement. They don't mean much in this setting.
1927 * It's left as an exercise for anyone interested to
1928 * implement RESERVE and RELEASE in terms of Posix
1931 case SC_FORMAT_UNIT
:
1934 case SC_SEND_DIAGNOSTIC
:
1938 error
= ustorage_fs_min_len(sc
, 0, -1U);
1942 error
= ustorage_fs_check_cmd(sc
, sc
->sc_transfer
.cmd_len
,
1947 sc
->sc_transfer
.currlun
->sense_data
=