refactor async io, add in_isr argument to tud_msc_async_io_done()
use cbw.command[0] for pending IO command
This commit is contained in:
@@ -52,36 +52,27 @@ enum {
|
||||
MSC_STAGE_NEED_RESET,
|
||||
};
|
||||
|
||||
enum {
|
||||
MSC_NEXT_OP_NONE = 0,
|
||||
MSC_NEXT_OP_READ10,
|
||||
MSC_NEXT_OP_WRITE10,
|
||||
MSC_NEXT_OP_STATUS
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
TU_ATTR_ALIGNED(4) msc_cbw_t cbw;
|
||||
TU_ATTR_ALIGNED(4) msc_csw_t csw;
|
||||
|
||||
TU_ATTR_ALIGNED(4) msc_cbw_t cbw; // 31 bytes
|
||||
uint8_t rhport;
|
||||
|
||||
TU_ATTR_ALIGNED(4) msc_csw_t csw; // 13 bytes
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out;
|
||||
|
||||
// Bulk Only Transfer (BOT) Protocol
|
||||
uint8_t stage;
|
||||
|
||||
uint32_t total_len; // byte to be transferred, can be smaller than total_bytes in cbw
|
||||
uint32_t xferred_len; // numbered of bytes transferred so far in the Data Stage
|
||||
|
||||
// Sense Response Data
|
||||
// Bulk Only Transfer (BOT) Protocol
|
||||
uint8_t stage;
|
||||
|
||||
// SCSI Sense Response Data
|
||||
uint8_t sense_key;
|
||||
uint8_t add_sense_code;
|
||||
uint8_t add_sense_qualifier;
|
||||
|
||||
// Async IO
|
||||
uint8_t next_op;
|
||||
uint32_t xferred_bytes;
|
||||
uint8_t pending_io; // pending async IO
|
||||
}mscd_interface_t;
|
||||
|
||||
static mscd_interface_t _mscd_itf;
|
||||
@@ -95,12 +86,11 @@ CFG_TUD_MEM_SECTION static struct {
|
||||
//--------------------------------------------------------------------+
|
||||
static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buffer, uint32_t bufsize);
|
||||
static void proc_read10_cmd(mscd_interface_t* p_msc);
|
||||
static void proc_read10_next(mscd_interface_t* p_msc, int32_t nbytes);
|
||||
static void proc_read_io_data(mscd_interface_t* p_msc, int32_t nbytes);
|
||||
static void proc_write10_cmd(mscd_interface_t* p_msc);
|
||||
static void proc_write10_new_data(mscd_interface_t* p_msc, uint32_t xferred_bytes);
|
||||
static void proc_write10_next(mscd_interface_t* p_msc, uint32_t xferred_bytes, int32_t nbytes);
|
||||
static void proc_write10_host_data(mscd_interface_t* p_msc, uint32_t xferred_bytes);
|
||||
static void proc_write_io_data(mscd_interface_t* p_msc, uint32_t xferred_bytes, int32_t nbytes);
|
||||
static bool proc_stage_status(mscd_interface_t* p_msc);
|
||||
static void tud_msc_async_io_done_cb(void* bytes_processed);
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool is_data_in(uint8_t dir) {
|
||||
return tu_bit_test(dir, 7);
|
||||
@@ -195,30 +185,31 @@ static uint8_t rdwr10_validate_cmd(msc_cbw_t const* cbw) {
|
||||
return status;
|
||||
}
|
||||
|
||||
static bool proc_stage_status(mscd_interface_t* p_msc) {
|
||||
static bool proc_stage_status(mscd_interface_t *p_msc) {
|
||||
uint8_t rhport = p_msc->rhport;
|
||||
msc_cbw_t const* p_cbw = &p_msc->cbw;
|
||||
// skip status if epin is currently stalled, will do it when received Clear Stall request
|
||||
if (!usbd_edpt_stalled(rhport, p_msc->ep_in)) {
|
||||
if ((p_cbw->total_bytes > p_msc->xferred_len) && is_data_in(p_cbw->dir)) {
|
||||
// 6.7 The 13 Cases: case 5 (Hi > Di): STALL before status
|
||||
// TU_LOG_DRV(" SCSI case 5 (Hi > Di): %lu > %lu\r\n", p_cbw->total_bytes, p_msc->xferred_len);
|
||||
usbd_edpt_stall(rhport, p_msc->ep_in);
|
||||
} else {
|
||||
TU_ASSERT(send_csw(p_msc));
|
||||
}
|
||||
}
|
||||
msc_cbw_t const *p_cbw = &p_msc->cbw;
|
||||
|
||||
#if TU_CHECK_MCU(OPT_MCU_CXD56)
|
||||
// WORKAROUND: cxd56 has its own nuttx usb stack which does not forward Set/ClearFeature(Endpoint) to DCD.
|
||||
// There is no way for us to know when EP is un-stall, therefore we will unconditionally un-stall here and
|
||||
// hope everything will work
|
||||
if ( usbd_edpt_stalled(rhport, p_msc->ep_in) ) {
|
||||
usbd_edpt_clear_stall(rhport, p_msc->ep_in);
|
||||
send_csw(p_msc);
|
||||
// skip status if epin is currently stalled, will do it when received Clear Stall request
|
||||
if (!usbd_edpt_stalled(rhport, p_msc->ep_in)) {
|
||||
if ((p_cbw->total_bytes > p_msc->xferred_len) && is_data_in(p_cbw->dir)) {
|
||||
// 6.7 The 13 Cases: case 5 (Hi > Di): STALL before status
|
||||
// TU_LOG_DRV(" SCSI case 5 (Hi > Di): %lu > %lu\r\n", p_cbw->total_bytes, p_msc->xferred_len);
|
||||
usbd_edpt_stall(rhport, p_msc->ep_in);
|
||||
} else {
|
||||
TU_ASSERT(send_csw(p_msc));
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
#if TU_CHECK_MCU(OPT_MCU_CXD56)
|
||||
// WORKAROUND: cxd56 has its own nuttx usb stack which does not forward Set/ClearFeature(Endpoint) to DCD.
|
||||
// There is no way for us to know when EP is un-stall, therefore we will unconditionally un-stall here and
|
||||
// hope everything will work
|
||||
if (usbd_edpt_stalled(rhport, p_msc->ep_in)) {
|
||||
usbd_edpt_clear_stall(rhport, p_msc->ep_in);
|
||||
send_csw(p_msc);
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -258,39 +249,57 @@ bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, u
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void set_sense_medium_not_present(uint8_t lun) {
|
||||
TU_ATTR_ALWAYS_INLINE static inline void set_sense_medium_not_present(uint8_t lun) {
|
||||
// default sense is NOT READY, MEDIUM NOT PRESENT
|
||||
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3A, 0x00);
|
||||
}
|
||||
|
||||
void tud_msc_async_io_done(int32_t bytes_processed) {
|
||||
// Precheck to avoid queueing multiple RW done callback
|
||||
TU_VERIFY(_mscd_itf.next_op != MSC_NEXT_OP_NONE,);
|
||||
// Call usbd_edpt_xfer() in tud_task() to avoid racing condition
|
||||
usbd_defer_func(tud_msc_async_io_done_cb, (void*) (intptr_t)bytes_processed, false);
|
||||
static void proc_async_io_done(void *bytes_processed) {
|
||||
mscd_interface_t *p_msc = &_mscd_itf;
|
||||
TU_VERIFY(p_msc->pending_io, );
|
||||
const int32_t nbytes = (int32_t) (intptr_t) bytes_processed;
|
||||
const uint8_t cmd = p_msc->cbw.command[0];
|
||||
|
||||
p_msc->pending_io = 0;
|
||||
switch (cmd) {
|
||||
case SCSI_CMD_READ_10:
|
||||
proc_read_io_data(p_msc, nbytes);
|
||||
break;
|
||||
|
||||
case SCSI_CMD_WRITE_10:
|
||||
proc_write_io_data(p_msc, (uint32_t) nbytes, nbytes);
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
|
||||
// send status if stage is transitioned to STATUS
|
||||
if (p_msc->stage == MSC_STAGE_STATUS) {
|
||||
proc_stage_status(p_msc);
|
||||
}
|
||||
}
|
||||
|
||||
static void tud_msc_async_io_done_cb(void* bytes_processed) {
|
||||
TU_VERIFY(_mscd_itf.next_op != MSC_NEXT_OP_NONE,);
|
||||
uint8_t next_op = _mscd_itf.next_op;
|
||||
_mscd_itf.next_op = MSC_NEXT_OP_NONE;
|
||||
int32_t nbytes = (int32_t)(intptr_t)bytes_processed;
|
||||
// READ10
|
||||
if (next_op == MSC_NEXT_OP_READ10) {
|
||||
proc_read10_next(&_mscd_itf, nbytes);
|
||||
} else if (next_op == MSC_NEXT_OP_WRITE10) {
|
||||
proc_write10_next(&_mscd_itf, _mscd_itf.xferred_bytes, nbytes);
|
||||
// Need to manually invoke CSW transfer
|
||||
if (_mscd_itf.stage == MSC_STAGE_STATUS) {
|
||||
proc_stage_status(&_mscd_itf);
|
||||
}
|
||||
bool tud_msc_async_io_done(int32_t bytes_io, bool in_isr) {
|
||||
// Precheck to avoid queueing multiple RW done callback
|
||||
TU_VERIFY(_mscd_itf.pending_io);
|
||||
if (bytes_io == 0) {
|
||||
bytes_io = TUD_MSC_RET_ERROR; // 0 is treated as error, no sense to call this with BUSY here
|
||||
}
|
||||
|
||||
if (in_isr) {
|
||||
usbd_defer_func(proc_async_io_done, (void*) (intptr_t)bytes_io, in_isr);
|
||||
} else {
|
||||
proc_async_io_done((void*)(intptr_t) bytes_io);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void mscd_init(void) {
|
||||
TU_LOG_INT(CFG_TUD_MSC_LOG_LEVEL, sizeof(mscd_interface_t));
|
||||
tu_memclr(&_mscd_itf, sizeof(mscd_interface_t));
|
||||
}
|
||||
|
||||
@@ -528,7 +537,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
|
||||
proc_read10_cmd(p_msc);
|
||||
}
|
||||
} else if (SCSI_CMD_WRITE_10 == p_cbw->command[0]) {
|
||||
proc_write10_new_data(p_msc, xferred_bytes);
|
||||
proc_write10_host_data(p_msc, xferred_bytes);
|
||||
} else {
|
||||
p_msc->xferred_len += xferred_bytes;
|
||||
|
||||
@@ -560,7 +569,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
|
||||
break;
|
||||
|
||||
case MSC_STAGE_STATUS_SENT:
|
||||
// Wait for the Status phase to complete
|
||||
// Status phase is complete
|
||||
if ((ep_addr == p_msc->ep_in) && (xferred_bytes == sizeof(msc_csw_t))) {
|
||||
TU_LOG_DRV(" SCSI Status [Lun%u] = %u\r\n", p_cbw->lun, p_csw->status);
|
||||
// TU_LOG_MEM(CFG_TUD_MSC_LOG_LEVEL, p_csw, xferred_bytes, 2);
|
||||
@@ -590,7 +599,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
|
||||
|
||||
TU_ASSERT(prepare_cbw(p_msc));
|
||||
} else {
|
||||
// Any xfer ended here is consider unknown error, ignore it
|
||||
// Any xfer ended here is considered unknown error, ignore it
|
||||
TU_LOG1(" Warning expect SCSI Status but received unknown data\r\n");
|
||||
}
|
||||
break;
|
||||
@@ -696,8 +705,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
|
||||
break;
|
||||
|
||||
case SCSI_CMD_READ_FORMAT_CAPACITY: {
|
||||
scsi_read_format_capacity_data_t read_fmt_capa =
|
||||
{
|
||||
scsi_read_format_capacity_data_t read_fmt_capa = {
|
||||
.list_length = 8,
|
||||
.block_num = 0,
|
||||
.descriptor_type = 2, // formatted media
|
||||
@@ -729,8 +737,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
|
||||
break;
|
||||
|
||||
case SCSI_CMD_INQUIRY: {
|
||||
scsi_inquiry_resp_t inquiry_rsp =
|
||||
{
|
||||
scsi_inquiry_resp_t inquiry_rsp = {
|
||||
.is_removable = 1,
|
||||
.version = 2,
|
||||
.response_data_format = 2,
|
||||
@@ -750,8 +757,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
|
||||
break;
|
||||
|
||||
case SCSI_CMD_MODE_SENSE_6: {
|
||||
scsi_mode_sense6_resp_t mode_resp =
|
||||
{
|
||||
scsi_mode_sense6_resp_t mode_resp = {
|
||||
.data_len = 3,
|
||||
.medium_type = 0,
|
||||
.write_protected = false,
|
||||
@@ -772,8 +778,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
|
||||
break;
|
||||
|
||||
case SCSI_CMD_REQUEST_SENSE: {
|
||||
scsi_sense_fixed_resp_t sense_rsp =
|
||||
{
|
||||
scsi_sense_fixed_resp_t sense_rsp = {
|
||||
.response_code = 0x70, // current, fixed format
|
||||
.valid = 1
|
||||
};
|
||||
@@ -805,32 +810,27 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
|
||||
|
||||
static void proc_read10_cmd(mscd_interface_t* p_msc) {
|
||||
msc_cbw_t const* p_cbw = &p_msc->cbw;
|
||||
|
||||
// block size already verified not zero
|
||||
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
|
||||
|
||||
// Adjust lba with transferred bytes
|
||||
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw); // already verified non-zero
|
||||
// Adjust lba & offset with transferred bytes
|
||||
uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
|
||||
uint32_t const offset = p_msc->xferred_len % block_sz;
|
||||
|
||||
// remaining bytes capped at class buffer
|
||||
int32_t nbytes = (int32_t)tu_min32(CFG_TUD_MSC_EP_BUFSIZE, p_cbw->total_bytes - p_msc->xferred_len);
|
||||
|
||||
// Application can consume smaller bytes
|
||||
uint32_t const offset = p_msc->xferred_len % block_sz;
|
||||
|
||||
p_msc->next_op = MSC_NEXT_OP_READ10;
|
||||
p_msc->pending_io = 1;
|
||||
nbytes = tud_msc_read10_cb(p_cbw->lun, lba, offset, _mscd_epbuf.buf, (uint32_t)nbytes);
|
||||
if (nbytes != TUD_MSC_RET_ASYNC) {
|
||||
p_msc->next_op = MSC_NEXT_OP_NONE;
|
||||
proc_read10_next(p_msc, nbytes);
|
||||
p_msc->pending_io = 0;
|
||||
proc_read_io_data(p_msc, nbytes);
|
||||
}
|
||||
}
|
||||
|
||||
static void proc_read10_next(mscd_interface_t* p_msc, int32_t nbytes) {
|
||||
static void proc_read_io_data(mscd_interface_t* p_msc, int32_t nbytes) {
|
||||
uint8_t rhport = p_msc->rhport;
|
||||
if (nbytes < 0) {
|
||||
// negative means error -> endpoint is stalled & status in CSW set to failed
|
||||
TU_LOG_DRV(" tud_msc_read10_cb() return -1\r\n");
|
||||
TU_LOG_DRV(" IO read() failed\r\n");
|
||||
|
||||
// set sense
|
||||
msc_cbw_t const* p_cbw = &p_msc->cbw;
|
||||
@@ -838,7 +838,7 @@ static void proc_read10_next(mscd_interface_t* p_msc, int32_t nbytes) {
|
||||
|
||||
fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
|
||||
} else if (nbytes == 0) {
|
||||
// zero means not ready -> simulate an transfer complete so that this driver callback will fired again
|
||||
// zero means not ready -> fake a transfer complete so that this driver callback will fire again
|
||||
dcd_event_xfer_complete(rhport, p_msc->ep_in, 0, XFER_RESULT_SUCCESS, false);
|
||||
} else {
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_epbuf.buf, (uint16_t) nbytes),);
|
||||
@@ -864,55 +864,42 @@ static void proc_write10_cmd(mscd_interface_t* p_msc) {
|
||||
// remaining bytes capped at class buffer
|
||||
uint16_t nbytes = (uint16_t)tu_min32(CFG_TUD_MSC_EP_BUFSIZE, p_cbw->total_bytes - p_msc->xferred_len);
|
||||
// Write10 callback will be called later when usb transfer complete
|
||||
uint8_t rhport = p_msc->rhport;
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_epbuf.buf, nbytes),);
|
||||
TU_ASSERT(usbd_edpt_xfer(p_msc->rhport, p_msc->ep_out, _mscd_epbuf.buf, nbytes),);
|
||||
}
|
||||
|
||||
// process new data arrived from WRITE10
|
||||
static void proc_write10_new_data(mscd_interface_t* p_msc, uint32_t xferred_bytes) {
|
||||
static void proc_write10_host_data(mscd_interface_t* p_msc, uint32_t xferred_bytes) {
|
||||
msc_cbw_t const* p_cbw = &p_msc->cbw;
|
||||
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw); // already verified non-zero
|
||||
|
||||
// block size already verified not zero
|
||||
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
|
||||
|
||||
// Adjust lba with transferred bytes
|
||||
// Adjust lba & offset with transferred bytes
|
||||
uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
|
||||
|
||||
// Invoke callback to consume new data
|
||||
uint32_t const offset = p_msc->xferred_len % block_sz;
|
||||
|
||||
p_msc->next_op = MSC_NEXT_OP_WRITE10;
|
||||
p_msc->xferred_bytes = xferred_bytes;
|
||||
p_msc->pending_io = 1;
|
||||
int32_t nbytes = tud_msc_write10_cb(p_cbw->lun, lba, offset, _mscd_epbuf.buf, xferred_bytes);
|
||||
if (nbytes != TUD_MSC_RET_ASYNC) {
|
||||
p_msc->next_op = MSC_NEXT_OP_NONE;
|
||||
proc_write10_next(p_msc, xferred_bytes, nbytes);
|
||||
p_msc->pending_io = 0;
|
||||
proc_write_io_data(p_msc, xferred_bytes, nbytes);
|
||||
}
|
||||
}
|
||||
|
||||
static void proc_write10_next(mscd_interface_t* p_msc, uint32_t xferred_bytes, int32_t nbytes) {
|
||||
static void proc_write_io_data(mscd_interface_t* p_msc, uint32_t xferred_bytes, int32_t nbytes) {
|
||||
if (nbytes < 0) {
|
||||
// negative means error -> failed this scsi op
|
||||
TU_LOG_DRV(" tud_msc_write10_cb() return -1\r\n");
|
||||
|
||||
// update actual byte before failed
|
||||
p_msc->xferred_len += xferred_bytes;
|
||||
|
||||
msc_cbw_t const* p_cbw = &p_msc->cbw;
|
||||
set_sense_medium_not_present(p_cbw->lun);
|
||||
TU_LOG_DRV(" IO write() failed\r\n");
|
||||
set_sense_medium_not_present(p_msc->cbw.lun);
|
||||
fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
|
||||
} else {
|
||||
if ((uint32_t)nbytes < xferred_bytes) {
|
||||
// Application consume less than what we got (including zero)
|
||||
const uint32_t left_over = xferred_bytes - (uint32_t)nbytes;
|
||||
if (nbytes > 0) {
|
||||
p_msc->xferred_len += (uint16_t)nbytes;
|
||||
memmove(_mscd_epbuf.buf, _mscd_epbuf.buf + nbytes, left_over);
|
||||
}
|
||||
|
||||
// simulate a transfer complete with adjusted parameters --> callback will be invoked with adjusted parameter
|
||||
uint8_t rhport = p_msc->rhport;
|
||||
dcd_event_xfer_complete(rhport, p_msc->ep_out, left_over, XFER_RESULT_SUCCESS, false);
|
||||
// fake a transfer complete with adjusted parameters --> callback will be invoked with adjusted parameters
|
||||
dcd_event_xfer_complete(p_msc->rhport, p_msc->ep_out, left_over, XFER_RESULT_SUCCESS, false);
|
||||
} else {
|
||||
// Application consume all bytes in our buffer
|
||||
p_msc->xferred_len += xferred_bytes;
|
||||
|
Reference in New Issue
Block a user