diff options
| author | hathach <[email protected]> | 2013-11-26 13:15:40 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2013-11-26 13:15:40 +0700 |
| commit | 8f70a6a886aa557dd87086b807367eef662a7e77 (patch) | |
| tree | 062c136d68c807be53bf74677c8210940bc5d661 /tinyusb/class | |
| parent | 51def3f7ed6b1ac849cb357a9868328d8c5e3117 (diff) | |
change endian conversion to native to be & be to native
completely deferred xfer isr event to usbd task
complete read10, write10 sequence for large data transfer
Diffstat (limited to 'tinyusb/class')
| -rw-r--r-- | tinyusb/class/msc_device.c | 145 | ||||
| -rw-r--r-- | tinyusb/class/msc_device.h | 4 | ||||
| -rw-r--r-- | tinyusb/class/msc_host.c | 8 |
3 files changed, 92 insertions, 65 deletions
diff --git a/tinyusb/class/msc_device.c b/tinyusb/class/msc_device.c index 94545d802..7c334491d 100644 --- a/tinyusb/class/msc_device.c +++ b/tinyusb/class/msc_device.c @@ -136,91 +136,118 @@ tusb_error_t mscd_control_request(uint8_t coreid, tusb_control_request_t const * return TUSB_ERROR_NONE;
}
-void mscd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes)
+// return true if data phase is complete, false if not yet complete
+static bool read10_write10_data_xfer(mscd_interface_t* p_msc)
{
- // TODO failed --> STALL pipe, on clear STALL --> queue endpoint OUT
- mscd_interface_t * const p_msc = &mscd_data;
msc_cmd_block_wrapper_t * const p_cbw = &p_msc->cbw;
msc_cmd_status_wrapper_t * const p_csw = &p_msc->csw;
- if ( endpointhandle_is_equal(p_msc->edpt_in, edpt_hdl) )
- {
- return; // currently no need to handle bulk in
- }
+ scsi_read10_t* p_readwrite = (scsi_read10_t*) &p_cbw->command; // read10 & write10 has the same format
- ASSERT( endpointhandle_is_equal(p_msc->edpt_out, edpt_hdl) &&
- xferred_bytes == sizeof(msc_cmd_block_wrapper_t) &&
- event == TUSB_EVENT_XFER_COMPLETE &&
- p_cbw->signature == MSC_CBW_SIGNATURE, VOID_RETURN );
+ endpoint_handle_t const edpt_hdl = BIT_TEST_(p_cbw->dir, 7) ? p_msc->edpt_in : p_msc->edpt_out;
+ uint32_t const lba = __be2n(p_readwrite->lba);
+ uint16_t const block_count = __be2n_16(p_readwrite->block_count);
void *p_buffer = NULL;
- uint16_t actual_length = p_cbw->xfer_bytes;
- p_csw->signature = MSC_CSW_SIGNATURE;
- p_csw->tag = p_cbw->tag;
- p_csw->data_residue = 0;
+ uint16_t xferred_block = (SCSI_CMD_READ_10 == p_cbw->command[0]) ? tusbd_msc_read10_cb (edpt_hdl.coreid, p_cbw->lun, &p_buffer, lba, block_count) :
+ tusbd_msc_write10_cb(edpt_hdl.coreid, p_cbw->lun, &p_buffer, lba, block_count);
+ xferred_block = min16_of(xferred_block, block_count);
+
+ uint16_t xferred_byte = xferred_block * (p_cbw->xfer_bytes / block_count);
- switch(p_cbw->command[0])
+ if ( 0 == xferred_block )
+ { // xferred_block is zero will cause pipe is stalled & status in CSW set to failed
+ p_csw->data_residue = __n2be(p_cbw->xfer_bytes);
+ p_csw->status = MSC_CSW_STATUS_FAILED;
+
+ (void) dcd_pipe_stall(edpt_hdl);
+
+ return true;
+ } else if (xferred_block < block_count)
{
- case SCSI_CMD_READ_10:
- case SCSI_CMD_WRITE_10:
- {
- scsi_read10_t const * const p_read10 = &p_cbw->command; // read10 & write10 has the same data structure
- uint32_t const lba = __be2le(p_read10->lba);
- uint16_t const block_count = __be2h_16(p_read10->block_count);
+ ASSERT_STATUS( dcd_pipe_xfer( edpt_hdl, p_buffer, xferred_byte, true) );
- uint16_t actual_block_count;
+ // adjust lba, block_count, xfer_bytes for the next call
+ p_readwrite->lba = __n2be(lba+xferred_block);
+ p_readwrite->block_count = __n2be_16(block_count - xferred_block);
+ p_cbw->xfer_bytes -= xferred_byte;
- if (SCSI_CMD_READ_10 == p_cbw->command[0])
- {
- actual_block_count = tusbd_msc_read10_cb (edpt_hdl.coreid, p_cbw->lun, &p_buffer, lba, block_count);
- }else
- {
- actual_block_count = tusbh_msc_write10_cb(edpt_hdl.coreid, p_cbw->lun, &p_buffer, lba, block_count);
- }
+ return false;
+ }else
+ {
+ p_csw->status = MSC_CSW_STATUS_PASSED;
+ ASSERT_STATUS( dcd_pipe_queue_xfer( edpt_hdl, p_buffer, xferred_byte) );
+ return true;
+ }
+}
- ASSERT( actual_block_count <= block_count, VOID_RETURN);
+tusb_error_t mscd_xfer_cb(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes)
+{
+ // TODO failed --> STALL pipe, on clear STALL --> queue endpoint OUT
+ static bool is_waiting_read10_write10 = false; // indicate we are transferring data in READ10, WRITE10 command
- if ( actual_block_count < block_count )
- {
- uint32_t const block_size = p_cbw->xfer_bytes / block_count;
- actual_length = block_size * actual_block_count;
- p_csw->data_residue = p_cbw->xfer_bytes - actual_length;
- p_csw->status = MSC_CSW_STATUS_FAILED;
- }else
+ mscd_interface_t * const p_msc = &mscd_data;
+ msc_cmd_block_wrapper_t * const p_cbw = &p_msc->cbw;
+ msc_cmd_status_wrapper_t * const p_csw = &p_msc->csw;
+
+ if ( !is_waiting_read10_write10)
+ { // new CBW received
+ if ( endpointhandle_is_equal(p_msc->edpt_in, edpt_hdl) ) return TUSB_ERROR_NONE; // bulk in interrupt for dcd to clean up
+
+ ASSERT( endpointhandle_is_equal(p_msc->edpt_out, edpt_hdl) &&
+ xferred_bytes == sizeof(msc_cmd_block_wrapper_t) &&
+ event == TUSB_EVENT_XFER_COMPLETE &&
+ p_cbw->signature == MSC_CBW_SIGNATURE, TUSB_ERROR_INVALID_PARA );
+
+ if ( (SCSI_CMD_READ_10 != p_cbw->command[0]) && (SCSI_CMD_WRITE_10 != p_cbw->command[0]) )
+ {
+ void *p_buffer = NULL;
+ uint16_t actual_length = p_cbw->xfer_bytes;
+
+ p_csw->status = tusbd_msc_scsi_received_isr(edpt_hdl.coreid, p_cbw->lun, p_cbw->command, &p_buffer, &actual_length);
+
+ //------------- Data Phase -------------//
+ if ( p_cbw->xfer_bytes )
{
- p_csw->status = MSC_CSW_STATUS_PASSED;
+ ASSERT( p_cbw->xfer_bytes >= actual_length, TUSB_ERROR_INVALID_PARA );
+ if ( p_buffer == NULL || actual_length == 0 )
+ { // application does not provide data to response --> possibly unsupported SCSI command
+ ASSERT_STATUS( dcd_pipe_stall(p_msc->edpt_in) );
+ p_csw->status = MSC_CSW_STATUS_FAILED;
+ }else
+ {
+ ASSERT_STATUS( dcd_pipe_queue_xfer( BIT_TEST_(p_cbw->dir, 7) ? p_msc->edpt_in : p_msc->edpt_out,
+ p_buffer, actual_length) );
+ }
}
}
- break;
-
- default:
- p_csw->status = tusbd_msc_scsi_received_isr(edpt_hdl.coreid, p_cbw->lun, p_cbw->command, &p_buffer, &actual_length);
- break;
}
- ASSERT( p_cbw->xfer_bytes >= actual_length, VOID_RETURN );
-
- //------------- Data Phase -------------//
- if ( p_cbw->xfer_bytes )
+ //------------- Data Phase For READ10 & WRITE10 (can be executed several times) -------------//
+ if ( (SCSI_CMD_READ_10 == p_cbw->command[0]) || (SCSI_CMD_WRITE_10 == p_cbw->command[0]) )
{
- if ( p_buffer == NULL || actual_length == 0 )
- { // application does not provide data to response --> possibly unsupported SCSI command
- ASSERT( TUSB_ERROR_NONE == dcd_pipe_stall(p_msc->edpt_in), VOID_RETURN );
- p_csw->status = MSC_CSW_STATUS_FAILED;
- }else
- {
- ASSERT( dcd_pipe_queue_xfer( BIT_TEST_(p_cbw->dir, 7) ? p_msc->edpt_in : p_msc->edpt_out,
- p_buffer, actual_length) == TUSB_ERROR_NONE, VOID_RETURN);
+ if (is_waiting_read10_write10)
+ { // continue with read10, write10 data transfer, interrupt must come from endpoint IN
+ ASSERT( endpointhandle_is_equal(p_msc->edpt_in, edpt_hdl) && event == TUSB_EVENT_XFER_COMPLETE, TUSB_ERROR_INVALID_PARA);
}
+ is_waiting_read10_write10 = !read10_write10_data_xfer(p_msc);
}
//------------- Status Phase -------------//
- ASSERT( dcd_pipe_xfer( p_msc->edpt_in , p_csw, sizeof(msc_cmd_status_wrapper_t), true) == TUSB_ERROR_NONE, VOID_RETURN ); // need to be true for dcd to clean up qtd !!
+ if (!is_waiting_read10_write10)
+ {
+ p_csw->signature = MSC_CSW_SIGNATURE;
+ p_csw->tag = p_cbw->tag;
+ p_csw->data_residue = 0;
+
+ ASSERT_STATUS( dcd_pipe_xfer( p_msc->edpt_in , p_csw, sizeof(msc_cmd_status_wrapper_t), true) ); // need to be true for dcd to clean up qtd !!
- //------------- Queue the next CBW -------------//
- ASSERT( dcd_pipe_xfer( p_msc->edpt_out, p_cbw, sizeof(msc_cmd_block_wrapper_t), true) == TUSB_ERROR_NONE, VOID_RETURN );
+ //------------- Queue the next CBW -------------//
+ ASSERT_STATUS( dcd_pipe_xfer( p_msc->edpt_out, p_cbw, sizeof(msc_cmd_block_wrapper_t), true) );
+ }
+ return TUSB_ERROR_NONE;
}
#endif
diff --git a/tinyusb/class/msc_device.h b/tinyusb/class/msc_device.h index 53ba5a280..9c2a5dd67 100644 --- a/tinyusb/class/msc_device.h +++ b/tinyusb/class/msc_device.h @@ -67,7 +67,7 @@ void tusbd_msc_unmounted_cb(uint8_t coreid); msc_csw_status_t tusbd_msc_scsi_received_isr (uint8_t coreid, uint8_t lun, uint8_t scsi_cmd[16], void ** pp_buffer, uint16_t* p_length);
uint16_t tusbd_msc_read10_cb (uint8_t coreid, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count) ATTR_WARN_UNUSED_RESULT;
-uint16_t tusbh_msc_write10_cb(uint8_t coreid, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count) ATTR_WARN_UNUSED_RESULT;
+uint16_t tusbd_msc_write10_cb(uint8_t coreid, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count) ATTR_WARN_UNUSED_RESULT;
/** @} */
/** @} */
@@ -80,7 +80,7 @@ uint16_t tusbh_msc_write10_cb(uint8_t coreid, uint8_t lun, void** pp_buffer, uin void mscd_init(void);
tusb_error_t mscd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_interface_desc, uint16_t *p_length);
tusb_error_t mscd_control_request(uint8_t coreid, tusb_control_request_t const * p_request);
-void mscd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes);
+tusb_error_t mscd_xfer_cb(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes);
void mscd_close(uint8_t coreid);
#endif
diff --git a/tinyusb/class/msc_host.c b/tinyusb/class/msc_host.c index 75f6fec88..bd8b449b7 100644 --- a/tinyusb/class/msc_host.c +++ b/tinyusb/class/msc_host.c @@ -255,7 +255,7 @@ tusb_error_t tusbh_msc_read10(uint8_t dev_addr, uint8_t lun, void * p_buffer, u scsi_read10_t cmd_read10 = { .cmd_code = SCSI_CMD_READ_10, - .lba = __le2be(lba), + .lba = __n2be(lba), .block_count = u16_le2be(block_count) }; @@ -281,7 +281,7 @@ tusb_error_t tusbh_msc_write10(uint8_t dev_addr, uint8_t lun, void const * p_buf scsi_write10_t cmd_write10 = { .cmd_code = SCSI_CMD_WRITE_10, - .lba = __le2be(lba), + .lba = __n2be(lba), .block_count = u16_le2be(block_count) }; @@ -394,8 +394,8 @@ tusb_error_t msch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con SUBTASK_ASSERT_STATUS(error); } - msch_data[dev_addr-1].last_lba = __be2le( ((scsi_read_capacity10_data_t*)msch_buffer)->last_lba ); - msch_data[dev_addr-1].block_size = (uint16_t) __be2le( ((scsi_read_capacity10_data_t*)msch_buffer)->block_size ); + msch_data[dev_addr-1].last_lba = __be2n( ((scsi_read_capacity10_data_t*)msch_buffer)->last_lba ); + msch_data[dev_addr-1].block_size = (uint16_t) __be2n( ((scsi_read_capacity10_data_t*)msch_buffer)->block_size ); msch_data[dev_addr-1].is_initialized = true; tusbh_msc_mounted_cb(dev_addr); |
