summaryrefslogtreecommitdiff
path: root/tinyusb
diff options
context:
space:
mode:
authorhathach <[email protected]>2018-04-18 13:20:31 +0700
committerhathach <[email protected]>2018-04-18 13:20:31 +0700
commite872962b2591362d5b2159bb1cb38b575214d9cd (patch)
treea4909f643b3422b1e72071760f0bb8a1a1322de8 /tinyusb
parenta18f47353c73612049ff1071ef129ae292e61ba4 (diff)
improving msc device driver
Diffstat (limited to 'tinyusb')
-rw-r--r--tinyusb/class/msc/msc_device.c23
-rw-r--r--tinyusb/class/msc/msc_device.h17
2 files changed, 22 insertions, 18 deletions
diff --git a/tinyusb/class/msc/msc_device.c b/tinyusb/class/msc/msc_device.c
index afb341f61..3c7cb9751 100644
--- a/tinyusb/class/msc/msc_device.c
+++ b/tinyusb/class/msc/msc_device.c
@@ -187,7 +187,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
p_csw->tag = p_cbw->tag;
p_csw->data_residue = 0;
- // Valid command -> move to Data Stage
+ /*------------- Parse command and prepare DATA -------------*/
p_msc->stage = MSC_STAGE_DATA;
p_msc->data_len = p_cbw->xfer_bytes;
p_msc->xferred_len = 0;
@@ -205,7 +205,8 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
// TODO SCSI data out transfer is not yet supported
TU_ASSERT( !(p_cbw->xfer_bytes > 0 && !BIT_TEST_(p_cbw->dir, 7)), TUSB_ERROR_NOT_SUPPORTED_YET);
- p_csw->status = tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, &p_buffer, &p_msc->data_len);
+ // Invoke callback
+ p_csw->status = tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, p_msc->scsi_data, &p_msc->data_len);
if ( p_cbw->xfer_bytes == 0)
{
@@ -215,22 +216,20 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
else
{
// Data Phase (non READ10, WRITE10)
- TU_ASSERT( p_cbw->xfer_bytes >= p_msc->data_len, TUSB_ERROR_INVALID_PARA );
+ TU_ASSERT( p_cbw->xfer_bytes >= p_msc->data_len, TUSB_ERROR_INVALID_PARA ); // cannot return more than host expect
TU_ASSERT( sizeof(p_msc->scsi_data) >= p_msc->data_len, TUSB_ERROR_NOT_ENOUGH_MEMORY); // needs to increase size for scsi_data
- uint8_t const ep_data = BIT_TEST_(p_cbw->dir, 7) ? p_msc->ep_in : p_msc->ep_out;
+ uint8_t const ep = BIT_TEST_(p_cbw->dir, 7) ? p_msc->ep_in : p_msc->ep_out;
- if ( p_buffer == NULL || p_msc->data_len == 0 )
+ if ( p_msc->data_len )
+ {
+ TU_ASSERT( dcd_edpt_xfer(rhport, ep, p_msc->scsi_data, p_msc->data_len), TUSB_ERROR_DCD_EDPT_XFER );
+ }else
{
// application does not provide data to response --> possibly unsupported SCSI command
- dcd_edpt_stall(rhport, ep_data);
+ dcd_edpt_stall(rhport, ep);
p_csw->status = MSC_CSW_STATUS_FAILED;
-
p_msc->stage = MSC_STAGE_STATUS;
- }else
- {
- memcpy(p_msc->scsi_data, p_buffer, p_msc->data_len);
- TU_ASSERT( dcd_edpt_xfer(rhport, ep_data, p_msc->scsi_data, p_msc->data_len), TUSB_ERROR_DCD_EDPT_XFER );
}
}
}
@@ -284,8 +283,10 @@ static bool read10_write10_data_xfer(uint8_t rhport, mscd_interface_t* p_msc)
uint8_t const ep_data = BIT_TEST_(p_cbw->dir, 7) ? p_msc->ep_in : p_msc->ep_out;
+ // LBA and Block count are in Big Endian
uint32_t lba = __be2n(p_readwrite->lba);
uint16_t block_count = __be2n_16(p_readwrite->block_count);
+
uint16_t const block_size = p_cbw->xfer_bytes / block_count;
// Adjust lba and block count according to byte transferred so far
diff --git a/tinyusb/class/msc/msc_device.h b/tinyusb/class/msc/msc_device.h
index 1d7b11aec..a379d102a 100644
--- a/tinyusb/class/msc/msc_device.h
+++ b/tinyusb/class/msc/msc_device.h
@@ -53,15 +53,18 @@
* @{ */
//--------------------------------------------------------------------+
-// APPLICATION API
+// APPLICATION API (Multiple Root Hub Ports)
+// Should be used only with MCU that support more than 1 ports
//--------------------------------------------------------------------+
+
+
//--------------------------------------------------------------------+
// APPLICATION CALLBACK API
//--------------------------------------------------------------------+
/** \brief Callback that is invoked when tinyusb stack received \ref SCSI_CMD_READ_10 command from host
- * \param[in] rhport USB Controller ID
+ * \param[in] rhport Root hub port
* \param[in] lun Targeted Logical Unit
* \param[out] pp_buffer Pointer to buffer which application need to update with the response data's address.
* Must be accessible by USB controller (see \ref CFG_TUSB_ATTR_USBRAM)
@@ -79,7 +82,7 @@
uint16_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count);
/** \brief Callback that is invoked when tinyusb stack received \ref SCSI_CMD_WRITE_10 command from host
- * \param[in] rhport USB Controller ID
+ * \param[in] rhport Root hub port
* \param[in] lun Targeted Logical Unit
* \param[out] pp_buffer Pointer to buffer which application need to update with the address to hold data from host
* Must be accessible by USB controller (see \ref CFG_TUSB_ATTR_USBRAM)
@@ -99,19 +102,19 @@ uint16_t tud_msc_write10_cb(uint8_t rhport, uint8_t lun, void** pp_buffer, uint3
// p_length [in,out] allocated/maximum length, application update with actual length
/** \brief Callback that is invoked when tinyusb stack received an SCSI command other than \ref SCSI_CMD_WRITE_10 and
* \ref SCSI_CMD_READ_10 command from host
- * \param[in] rhport USB Controller ID
+ * \param[in] rhport Root hub port
* \param[in] lun Targeted Logical Unit
* \param[in] scsi_cmd SCSI command contents, application should examine this command block to know which command host requested
- * \param[out] pp_buffer Pointer to buffer which application need to update with the address to transfer data with host.
+ * \param[out] buffer Pointer to buffer which application need to update with the address to transfer data with host.
* The buffer address can be anywhere since the stack will copy its contents to a internal USB-accessible buffer.
- * \param[in] p_length length
+ * \param[in] p_length Expected length from host, Application could update to actual data, but could not larger than original value.
* \retval non-zero Actual number of block that application can receive and must be less than or equal to \a \b block_count.
* \retval zero Indicate error in retrieving data from application. Tinyusb device stack will \b STALL the corresponding
* endpoint and return failed status in command status wrapper phase.
* \note Although this callback is called by tinyusb device task (non-isr context), however as all the classes share
* the same task (to save resource), any delay in this callback will cause delay in reponse on other classes.
*/
-msc_csw_status_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t scsi_cmd[16], void const ** pp_buffer, uint16_t* p_length);
+msc_csw_status_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t scsi_cmd[16], void* buffer, uint16_t* p_len);
/** @} */
/** @} */