improve msc device

- change tud_msc_scsi_cb return type to simply bool
- change tud_msc_write10_cb, tud_msc_read10_cb params order
This commit is contained in:
hathach 2018-04-18 16:55:44 +07:00
parent 6280c50ad7
commit 9aaf86bffd
5 changed files with 41 additions and 45 deletions

View File

@ -59,8 +59,8 @@ static scsi_inquiry_data_t const mscd_inquiry_data =
static scsi_read_capacity10_data_t const mscd_read_capacity10_data = static scsi_read_capacity10_data_t const mscd_read_capacity10_data =
{ {
.last_lba = ENDIAN_BE(DISK_BLOCK_NUM-1), // read capacity .last_lba = ENDIAN_BE(DISK_BLOCK_NUM-1), // Big Endian
.block_size = ENDIAN_BE(DISK_BLOCK_SIZE) .block_size = ENDIAN_BE(DISK_BLOCK_SIZE) // Big Endian
}; };
static scsi_sense_fixed_data_t mscd_sense_data = static scsi_sense_fixed_data_t mscd_sense_data =
@ -99,7 +99,7 @@ void msc_app_umount(uint8_t rhport)
} }
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) bool tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t scsi_cmd[16], void* buffer, uint16_t* p_len)
{ {
// read10 & write10 has their own callback and MUST not be handled here // read10 & write10 has their own callback and MUST not be handled here
@ -145,13 +145,13 @@ msc_csw_status_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t scsi_cmd[
default: default:
(*p_len) = 0; (*p_len) = 0;
return MSC_CSW_STATUS_FAILED; return false;
} }
if ( bufptr && buflen ) if ( bufptr && buflen )
{ {
// Response len must not larger than expected from host // Response len must not larger than expected from host
TU_ASSERT( (*p_len) >= buflen, MSC_CSW_STATUS_FAILED); TU_ASSERT( (*p_len) >= buflen );
memcpy(buffer, bufptr, buflen); memcpy(buffer, bufptr, buflen);
(*p_len) = buflen; (*p_len) = buflen;
@ -165,7 +165,7 @@ msc_csw_status_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t scsi_cmd[
mscd_sense_data.additional_sense_qualifier = 0; mscd_sense_data.additional_sense_qualifier = 0;
} }
return MSC_CSW_STATUS_PASSED; return true;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@ -91,13 +91,13 @@ uint8_t msc_device_ramdisk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// IMPLEMENTATION // IMPLEMENTATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint16_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count) uint16_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint16_t block_count, void** pp_buffer)
{ {
(*pp_buffer) = msc_device_ramdisk[lba]; (*pp_buffer) = msc_device_ramdisk[lba];
return min16_of(block_count, DISK_BLOCK_NUM); return min16_of(block_count, DISK_BLOCK_NUM);
} }
uint16_t tud_msc_write10_cb(uint8_t rhport, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count) uint16_t tud_msc_write10_cb(uint8_t rhport, uint8_t lun, uint32_t lba, uint16_t block_count, void** pp_buffer)
{ {
(*pp_buffer) = msc_device_ramdisk[lba]; (*pp_buffer) = msc_device_ramdisk[lba];

View File

@ -97,7 +97,7 @@ static uint8_t sector_buffer[DISK_BLOCK_SIZE];
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// IMPLEMENTATION // IMPLEMENTATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint16_t tusbd_msc_read10_cb (uint8_t rhport, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count) uint16_t tusbd_msc_read10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint16_t block_count, void** pp_buffer)
{ {
memcpy(sector_buffer, msc_device_app_rommdisk[lba], DISK_BLOCK_SIZE); memcpy(sector_buffer, msc_device_app_rommdisk[lba], DISK_BLOCK_SIZE);
(*pp_buffer) = sector_buffer; (*pp_buffer) = sector_buffer;
@ -106,7 +106,7 @@ uint16_t tusbd_msc_read10_cb (uint8_t rhport, uint8_t lun, void** pp_buffer, uin
} }
// Stall write10 by return 0, as this is readonly disk // Stall write10 by return 0, as this is readonly disk
uint16_t tusbd_msc_write10_cb(uint8_t rhport, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count) uint16_t tusbd_msc_write10_cb(uint8_t rhport, uint8_t lun, uint32_t lba, uint16_t block_count, void** pp_buffer)
{ {
(*pp_buffer) = NULL; (*pp_buffer) = NULL;

View File

@ -162,9 +162,6 @@ tusb_error_t mscd_control_request_st(uint8_t rhport, tusb_control_request_t cons
OSAL_SUBTASK_END OSAL_SUBTASK_END
} }
//--------------------------------------------------------------------+
// MSCD APPLICATION CALLBACK
//--------------------------------------------------------------------+
tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes) tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes)
{ {
mscd_interface_t* const p_msc = &mscd_data; mscd_interface_t* const p_msc = &mscd_data;
@ -206,7 +203,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
if ( p_cbw->xfer_bytes == 0) if ( p_cbw->xfer_bytes == 0)
{ {
p_csw->status = tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, p_msc->scsi_data, &p_msc->data_len); p_csw->status = tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, p_msc->scsi_data, &p_msc->data_len) ? MSC_CSW_STATUS_PASSED : MSC_CSW_STATUS_FAILED;
p_msc->stage = MSC_STAGE_STATUS; p_msc->stage = MSC_STAGE_STATUS;
} }
else if ( !BIT_TEST_(p_cbw->dir, 7) ) else if ( !BIT_TEST_(p_cbw->dir, 7) )
@ -216,7 +213,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
} }
else else
{ {
p_csw->status = tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, p_msc->scsi_data, &p_msc->data_len); p_csw->status = tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, p_msc->scsi_data, &p_msc->data_len) ? MSC_CSW_STATUS_PASSED : MSC_CSW_STATUS_FAILED;
TU_ASSERT( p_cbw->xfer_bytes >= p_msc->data_len, TUSB_ERROR_INVALID_PARA ); // cannot return more than host expect 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 TU_ASSERT( sizeof(p_msc->scsi_data) >= p_msc->data_len, TUSB_ERROR_NOT_ENOUGH_MEMORY); // needs to increase size for scsi_data
@ -257,7 +254,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
// Invoke callback if it is not READ10, WRITE10 // Invoke callback if it is not READ10, WRITE10
if ( ! ((SCSI_CMD_READ_10 == p_cbw->command[0]) || (SCSI_CMD_WRITE_10 == p_cbw->command[0])) ) if ( ! ((SCSI_CMD_READ_10 == p_cbw->command[0]) || (SCSI_CMD_WRITE_10 == p_cbw->command[0])) )
{ {
p_csw->status = tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, p_msc->scsi_data, &p_msc->data_len); p_csw->status = tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, p_msc->scsi_data, &p_msc->data_len) ? MSC_CSW_STATUS_PASSED : MSC_CSW_STATUS_FAILED;
} }
p_msc->stage = MSC_STAGE_STATUS; p_msc->stage = MSC_STAGE_STATUS;
@ -308,10 +305,10 @@ static bool read10_write10_data_xfer(uint8_t rhport, mscd_interface_t* p_msc)
if (SCSI_CMD_READ_10 == p_cbw->command[0]) if (SCSI_CMD_READ_10 == p_cbw->command[0])
{ {
xfer_block = tud_msc_read10_cb (rhport, p_cbw->lun, &p_buffer, lba, block_count); xfer_block = tud_msc_read10_cb (rhport, p_cbw->lun, lba, block_count, &p_buffer);
}else }else
{ {
xfer_block = tud_msc_write10_cb(rhport, p_cbw->lun, &p_buffer, lba, block_count); xfer_block = tud_msc_write10_cb(rhport, p_cbw->lun, lba, block_count, &p_buffer);
} }
xfer_block = min16_of(xfer_block, block_count); xfer_block = min16_of(xfer_block, block_count);

View File

@ -57,64 +57,63 @@
// Should be used only with MCU that support more than 1 ports // Should be used only with MCU that support more than 1 ports
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// APPLICATION CALLBACK API // APPLICATION CALLBACK API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
/** \brief Callback invoked when received \ref SCSI_CMD_READ_10 command
/** \brief Callback that is invoked when tinyusb stack received \ref SCSI_CMD_READ_10 command from host * \param[in] \rhport Root hub port
* \param[in] rhport Root hub port
* \param[in] lun Targeted Logical Unit * \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) * Must be accessible by USB controller (see \ref CFG_TUSB_ATTR_USBRAM)
* \param[in] lba Starting Logical Block Address to be read * \param[in] lba Starting Logical Block Address to be read
* \param[in] block_count Number of requested block * \param[in] block_count Number of requested block
* \param[out] pp_buffer Pointer to buffer which application need to update with the response data's address.
*
* \retval non-zero Actual number of block that application processed, must be less than or equal to \a \b block_count. * \retval non-zero Actual number of block that application processed, 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 * \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. * endpoint and return failed status in command status wrapper phase.
*
* \note Host can request dozens of Kbytes in one command e.g \a \b block_count = 128, it is insufficient to require * \note Host can request dozens of Kbytes in one command e.g \a \b block_count = 128, it is insufficient to require
* application to have that much of buffer. Instead, application can return the number of blocks it can processed, * application to have that much of buffer. Instead, application can return the number of blocks it can processed,
* the stack after transferred that amount of data will continue to invoke this callback with adjusted \a \b lba and \a \b block_count. * the stack after transferred that amount of data will continue to invoke this callback with adjusted \a \b lba and \a \b block_count.
* \n\n Although this callback is called by tinyusb device task (non-isr context), however as all the classes share * \n\n 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. * the same task (to save resource), any delay in this callback will cause delay in response on other classes.
*/ */
uint16_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count); uint16_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint16_t block_count, void** pp_buffer);
/** \brief Callback that is invoked when tinyusb stack received \ref SCSI_CMD_WRITE_10 command from host //void tud_msc_read10_cmpl_cb(uint8_t rhport, uint8_t lun);
/** \brief Callback invoked when received \ref SCSI_CMD_WRITE_10 command
* \param[in] rhport Root hub port * \param[in] rhport Root hub port
* \param[in] lun Targeted Logical Unit * \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) * Must be accessible by USB controller (see \ref CFG_TUSB_ATTR_USBRAM)
* \param[in] lba Starting Logical Block Address to be write * \param[in] lba Starting Logical Block Address to be write
* \param[in] block_count Number of requested block * \param[in] block_count Number of requested block
* \param[out] pp_buffer Pointer to buffer which application need to update with the address to hold data from host
*
* \retval non-zero Actual number of block that application can receive and must be less than or equal to \a \b block_count. * \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 * \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. * endpoint and return failed status in command status wrapper phase.
*
* \note Host can request dozens of Kbytes in one command e.g \a \b block_count = 128, it is insufficient to require * \note Host can request dozens of Kbytes in one command e.g \a \b block_count = 128, it is insufficient to require
* application to have that much of buffer. Instead, application can return the number of blocks it can processed, * application to have that much of buffer. Instead, application can return the number of blocks it can processed,
* the stack after transferred that amount of data will continue to invoke this callback with adjusted \a \b lba and \a \b block_count. * the stack after transferred that amount of data will continue to invoke this callback with adjusted \a \b lba and \a \b block_count.
* \n\n Although this callback is called by tinyusb device task (non-isr context), however as all the classes share * \n\n 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. * the same task (to save resource), any delay in this callback will cause delay in response on other classes.
*/ */
uint16_t tud_msc_write10_cb(uint8_t rhport, uint8_t lun, void** pp_buffer, uint32_t lba, uint16_t block_count); uint16_t tud_msc_write10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint16_t block_count, void** pp_buffer);
// p_length [in,out] allocated/maximum length, application update with actual length /** \brief Callback invoked when received an SCSI command other than \ref SCSI_CMD_WRITE_10 and \ref SCSI_CMD_READ_10
/** \brief Callback that is invoked when tinyusb stack received an SCSI command other than \ref SCSI_CMD_WRITE_10 and * \param[in] rhport Root hub port
* \ref SCSI_CMD_READ_10 command from host * \param[in] lun Targeted Logical Unit
* \param[in] rhport Root hub port * \param[in] scsi_cmd SCSI command contents, application should examine this command block to know which command host requested
* \param[in] lun Targeted Logical Unit * \param[out] buffer Pointer to buffer which application need to update with the address to transfer data with host.
* \param[in] scsi_cmd SCSI command contents, application should examine this command block to know which command host requested * The buffer address can be anywhere since the stack will copy its contents to a internal USB-accessible buffer.
* \param[out] buffer Pointer to buffer which application need to update with the address to transfer data with host. * \param[in,out] p_len Expected length from host, Application could update to actual data, but could not larger than original value.
* The buffer address can be anywhere since the stack will copy its contents to a internal USB-accessible buffer. *
* \param[in] p_len Expected length from host, Application could update to actual data, but could not larger than original value. * \retval true if successful, false otherwise. Tinyusb will then \b STALL the corresponding endpoint and return
* \retval non-zero Actual number of block that application can receive and must be less than or equal to \a \b block_count. * failed status in command status wrapper stage.
* \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* buffer, uint16_t* p_len); bool tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t scsi_cmd[16], void* buffer, uint16_t* p_len);
/** @} */ /** @} */
/** @} */ /** @} */