msc_device.c declare and separate epbuf from interface struct, make cbw and csw as normal variable. Also reformat code

This commit is contained in:
hathach 2024-11-22 11:22:41 +07:00
parent f252ab4364
commit f148670753
No known key found for this signature in database
GPG Key ID: 26FAB84F615C3C52

View File

@ -53,18 +53,8 @@ enum {
}; };
typedef struct { typedef struct {
union { TU_ATTR_ALIGNED(4) msc_cbw_t cbw;
CFG_TUD_MEM_ALIGN msc_cbw_t cbw; TU_ATTR_ALIGNED(4) msc_csw_t csw;
TUD_DCACHE_PADDING;
};
union {
CFG_TUD_MEM_ALIGN msc_csw_t csw;
TUD_DCACHE_PADDING;
};
union {
CFG_TUD_MEM_ALIGN uint8_t ep_buf[CFG_TUD_MSC_EP_BUFSIZE];
TUD_DCACHE_PADDING;
};
uint8_t itf_num; uint8_t itf_num;
uint8_t ep_in; uint8_t ep_in;
@ -72,6 +62,7 @@ typedef struct {
// Bulk Only Transfer (BOT) Protocol // Bulk Only Transfer (BOT) Protocol
uint8_t stage; uint8_t stage;
uint32_t total_len; // byte to be transferred, can be smaller than total_bytes in cbw 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 uint32_t xferred_len; // numbered of bytes transferred so far in the Data Stage
@ -81,7 +72,11 @@ typedef struct {
uint8_t add_sense_qualifier; uint8_t add_sense_qualifier;
}mscd_interface_t; }mscd_interface_t;
CFG_TUD_MEM_SECTION CFG_TUD_MEM_ALIGN static mscd_interface_t _mscd_itf; static mscd_interface_t _mscd_itf;
CFG_TUD_MEM_SECTION static struct {
TUD_EPBUF_DEF(ep_buf, CFG_TUD_MSC_EP_BUFSIZE);
} _mscd_epbuf;
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION // INTERNAL OBJECT & FUNCTION DECLARATION
@ -99,14 +94,14 @@ TU_ATTR_ALWAYS_INLINE static inline bool is_data_in(uint8_t dir) {
static inline bool send_csw(uint8_t rhport, mscd_interface_t* p_msc) { static inline bool send_csw(uint8_t rhport, mscd_interface_t* p_msc) {
// Data residue is always = host expect - actual transferred // Data residue is always = host expect - actual transferred
p_msc->csw.data_residue = p_msc->cbw.total_bytes - p_msc->xferred_len; p_msc->csw.data_residue = p_msc->cbw.total_bytes - p_msc->xferred_len;
p_msc->stage = MSC_STAGE_STATUS_SENT; p_msc->stage = MSC_STAGE_STATUS_SENT;
return usbd_edpt_xfer(rhport, p_msc->ep_in , (uint8_t*) &p_msc->csw, sizeof(msc_csw_t)); memcpy(_mscd_epbuf.ep_buf, &p_msc->csw, sizeof(msc_csw_t));
return usbd_edpt_xfer(rhport, p_msc->ep_in , _mscd_epbuf.ep_buf, sizeof(msc_csw_t));
} }
static inline bool prepare_cbw(uint8_t rhport, mscd_interface_t* p_msc) { static inline bool prepare_cbw(uint8_t rhport, mscd_interface_t* p_msc) {
p_msc->stage = MSC_STAGE_CMD; p_msc->stage = MSC_STAGE_CMD;
return usbd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)); return usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_epbuf.ep_buf, sizeof(msc_cbw_t));
} }
static void fail_scsi_op(uint8_t rhport, mscd_interface_t* p_msc, uint8_t status) { static void fail_scsi_op(uint8_t rhport, mscd_interface_t* p_msc, uint8_t status) {
@ -134,10 +129,8 @@ static void fail_scsi_op(uint8_t rhport, mscd_interface_t* p_msc, uint8_t status
static inline uint32_t rdwr10_get_lba(uint8_t const command[]) { static inline uint32_t rdwr10_get_lba(uint8_t const command[]) {
// use offsetof to avoid pointer to the odd/unaligned address // use offsetof to avoid pointer to the odd/unaligned address
uint32_t const lba = tu_unaligned_read32(command + offsetof(scsi_write10_t, lba)); const uint32_t lba = tu_unaligned_read32(command + offsetof(scsi_write10_t, lba));
return tu_ntohl(lba); // lba is in Big Endian
// lba is in Big Endian
return tu_ntohl(lba);
} }
static inline uint16_t rdwr10_get_blockcount(msc_cbw_t const* cbw) { static inline uint16_t rdwr10_get_blockcount(msc_cbw_t const* cbw) {
@ -148,10 +141,9 @@ static inline uint16_t rdwr10_get_blockcount(msc_cbw_t const* cbw) {
static inline uint16_t rdwr10_get_blocksize(msc_cbw_t const* cbw) { static inline uint16_t rdwr10_get_blocksize(msc_cbw_t const* cbw) {
// first extract block count in the command // first extract block count in the command
uint16_t const block_count = rdwr10_get_blockcount(cbw); uint16_t const block_count = rdwr10_get_blockcount(cbw);
if (block_count == 0) {
// invalid block count return 0; // invalid block count
if (block_count == 0) return 0; }
return (uint16_t) (cbw->total_bytes / block_count); return (uint16_t) (cbw->total_bytes / block_count);
} }
@ -214,19 +206,15 @@ TU_ATTR_UNUSED tu_static tu_lookup_table_t const _msc_scsi_cmd_table = {
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// APPLICATION API // APPLICATION API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier) bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier) {
{
(void) lun; (void) lun;
_mscd_itf.sense_key = sense_key; _mscd_itf.sense_key = sense_key;
_mscd_itf.add_sense_code = add_sense_code; _mscd_itf.add_sense_code = add_sense_code;
_mscd_itf.add_sense_qualifier = add_sense_qualifier; _mscd_itf.add_sense_qualifier = add_sense_qualifier;
return true; return true;
} }
static inline void set_sense_medium_not_present(uint8_t lun) static inline void set_sense_medium_not_present(uint8_t lun) {
{
// default sense is NOT READY, MEDIUM NOT PRESENT // default sense is NOT READY, MEDIUM NOT PRESENT
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3A, 0x00); tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3A, 0x00);
} }
@ -239,47 +227,38 @@ void mscd_init(void) {
} }
bool mscd_deinit(void) { bool mscd_deinit(void) {
// nothing to do return true; // nothing to do
return true;
} }
void mscd_reset(uint8_t rhport) void mscd_reset(uint8_t rhport) {
{
(void) rhport; (void) rhport;
tu_memclr(&_mscd_itf, sizeof(mscd_interface_t)); tu_memclr(&_mscd_itf, sizeof(mscd_interface_t));
} }
uint16_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) uint16_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) {
{
// only support SCSI's BOT protocol // only support SCSI's BOT protocol
TU_VERIFY(TUSB_CLASS_MSC == itf_desc->bInterfaceClass && TU_VERIFY(TUSB_CLASS_MSC == itf_desc->bInterfaceClass &&
MSC_SUBCLASS_SCSI == itf_desc->bInterfaceSubClass && MSC_SUBCLASS_SCSI == itf_desc->bInterfaceSubClass &&
MSC_PROTOCOL_BOT == itf_desc->bInterfaceProtocol, 0); MSC_PROTOCOL_BOT == itf_desc->bInterfaceProtocol, 0);
// msc driver length is fixed
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); uint16_t const drv_len = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t);
TU_ASSERT(max_len >= drv_len, 0); // Max length must be at least 1 interface + 2 endpoints
// Max length must be at least 1 interface + 2 endpoints
TU_ASSERT(max_len >= drv_len, 0);
mscd_interface_t * p_msc = &_mscd_itf; mscd_interface_t * p_msc = &_mscd_itf;
p_msc->itf_num = itf_desc->bInterfaceNumber; p_msc->itf_num = itf_desc->bInterfaceNumber;
// Open endpoint pair // Open endpoint pair
TU_ASSERT( usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in), 0 ); TU_ASSERT(usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in), 0);
// Prepare for Command Block Wrapper // Prepare for Command Block Wrapper
TU_ASSERT( prepare_cbw(rhport, p_msc), drv_len); TU_ASSERT(prepare_cbw(rhport, p_msc), drv_len);
return drv_len; return drv_len;
} }
static void proc_bot_reset(mscd_interface_t* p_msc) static void proc_bot_reset(mscd_interface_t* p_msc) {
{
p_msc->stage = MSC_STAGE_CMD; p_msc->stage = MSC_STAGE_CMD;
p_msc->total_len = 0; p_msc->total_len = 0;
p_msc->xferred_len = 0; p_msc->xferred_len = 0;
p_msc->sense_key = 0; p_msc->sense_key = 0;
p_msc->add_sense_code = 0; p_msc->add_sense_code = 0;
p_msc->add_sense_qualifier = 0; p_msc->add_sense_qualifier = 0;
@ -288,10 +267,10 @@ static void proc_bot_reset(mscd_interface_t* p_msc)
// Invoked when a control transfer occurred on an interface of this class // Invoked when a control transfer occurred on an interface of this class
// Driver response accordingly to the request and the transfer stage (setup/data/ack) // Driver response accordingly to the request and the transfer stage (setup/data/ack)
// return false to stall control endpoint (e.g unsupported request) // return false to stall control endpoint (e.g unsupported request)
bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) {
{ if (stage != CONTROL_STAGE_SETUP) {
// nothing to do with DATA & ACK stage return true; // nothing to do with DATA & ACK stage
if (stage != CONTROL_STAGE_SETUP) return true; }
mscd_interface_t* p_msc = &_mscd_itf; mscd_interface_t* p_msc = &_mscd_itf;
@ -299,35 +278,25 @@ bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
if ( TUSB_REQ_TYPE_STANDARD == request->bmRequestType_bit.type && if ( TUSB_REQ_TYPE_STANDARD == request->bmRequestType_bit.type &&
TUSB_REQ_RCPT_ENDPOINT == request->bmRequestType_bit.recipient && TUSB_REQ_RCPT_ENDPOINT == request->bmRequestType_bit.recipient &&
TUSB_REQ_CLEAR_FEATURE == request->bRequest && TUSB_REQ_CLEAR_FEATURE == request->bRequest &&
TUSB_REQ_FEATURE_EDPT_HALT == request->wValue ) TUSB_REQ_FEATURE_EDPT_HALT == request->wValue ) {
{
uint8_t const ep_addr = tu_u16_low(request->wIndex); uint8_t const ep_addr = tu_u16_low(request->wIndex);
if ( p_msc->stage == MSC_STAGE_NEED_RESET ) if (p_msc->stage == MSC_STAGE_NEED_RESET) {
{
// reset recovery is required to recover from this stage // reset recovery is required to recover from this stage
// Clear Stall request cannot resolve this -> continue to stall endpoint // Clear Stall request cannot resolve this -> continue to stall endpoint
usbd_edpt_stall(rhport, ep_addr); usbd_edpt_stall(rhport, ep_addr);
} } else {
else if (ep_addr == p_msc->ep_in) {
{ if (p_msc->stage == MSC_STAGE_STATUS) {
if ( ep_addr == p_msc->ep_in )
{
if ( p_msc->stage == MSC_STAGE_STATUS )
{
// resume sending SCSI status if we are in this stage previously before stalled // resume sending SCSI status if we are in this stage previously before stalled
TU_ASSERT( send_csw(rhport, p_msc) ); TU_ASSERT(send_csw(rhport, p_msc));
} }
} } else if (ep_addr == p_msc->ep_out) {
else if ( ep_addr == p_msc->ep_out ) if (p_msc->stage == MSC_STAGE_CMD) {
{
if ( p_msc->stage == MSC_STAGE_CMD )
{
// part of reset recovery (probably due to invalid CBW) -> prepare for new command // part of reset recovery (probably due to invalid CBW) -> prepare for new command
// Note: skip if already queued previously // Note: skip if already queued previously
if ( usbd_edpt_ready(rhport, p_msc->ep_out) ) if (usbd_edpt_ready(rhport, p_msc->ep_out)) {
{ TU_ASSERT(prepare_cbw(rhport, p_msc));
TU_ASSERT( prepare_cbw(rhport, p_msc) );
} }
} }
} }
@ -339,33 +308,27 @@ bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
// From this point only handle class request only // From this point only handle class request only
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS); TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
switch ( request->bRequest ) switch ( request->bRequest ) {
{
case MSC_REQ_RESET: case MSC_REQ_RESET:
TU_LOG_DRV(" MSC BOT Reset\r\n"); TU_LOG_DRV(" MSC BOT Reset\r\n");
TU_VERIFY(request->wValue == 0 && request->wLength == 0); TU_VERIFY(request->wValue == 0 && request->wLength == 0);
proc_bot_reset(p_msc); // driver state reset
// driver state reset
proc_bot_reset(p_msc);
tud_control_status(rhport, request); tud_control_status(rhport, request);
break; break;
case MSC_REQ_GET_MAX_LUN: case MSC_REQ_GET_MAX_LUN: {
{
TU_LOG_DRV(" MSC Get Max Lun\r\n"); TU_LOG_DRV(" MSC Get Max Lun\r\n");
TU_VERIFY(request->wValue == 0 && request->wLength == 1); TU_VERIFY(request->wValue == 0 && request->wLength == 1);
uint8_t maxlun = 1; uint8_t maxlun = 1;
if (tud_msc_get_maxlun_cb) maxlun = tud_msc_get_maxlun_cb(); if (tud_msc_get_maxlun_cb) {
TU_VERIFY(maxlun); maxlun = tud_msc_get_maxlun_cb();
// MAX LUN is minus 1 by specs
maxlun--;
tud_control_xfer(rhport, request, &maxlun, 1);
} }
TU_VERIFY(maxlun);
maxlun--; // MAX LUN is minus 1 by specs
tud_control_xfer(rhport, request, &maxlun, 1);
break; break;
}
default: return false; // stall unsupported request default: return false; // stall unsupported request
} }
@ -373,35 +336,34 @@ bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
return true; return true;
} }
bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) {
{
(void) event; (void) event;
mscd_interface_t* p_msc = &_mscd_itf; mscd_interface_t* p_msc = &_mscd_itf;
msc_cbw_t const * p_cbw = &p_msc->cbw; msc_cbw_t * p_cbw = &p_msc->cbw;
msc_csw_t * p_csw = &p_msc->csw; msc_csw_t * p_csw = &p_msc->csw;
switch (p_msc->stage) switch (p_msc->stage) {
{
case MSC_STAGE_CMD: case MSC_STAGE_CMD:
//------------- new CBW received -------------// //------------- new CBW received -------------//
// Complete IN while waiting for CMD is usually Status of previous SCSI op, ignore it // Complete IN while waiting for CMD is usually Status of previous SCSI op, ignore it
if(ep_addr != p_msc->ep_out) return true; if (ep_addr != p_msc->ep_out) {
return true;
}
if ( !(xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE) ) const uint32_t signature = tu_le32toh(tu_unaligned_read32(_mscd_epbuf.ep_buf));
{
TU_LOG_DRV(" SCSI CBW is not valid\r\n");
if (!(xferred_bytes == sizeof(msc_cbw_t) && signature == MSC_CBW_SIGNATURE)) {
// BOT 6.6.1 If CBW is not valid stall both endpoints until reset recovery // BOT 6.6.1 If CBW is not valid stall both endpoints until reset recovery
TU_LOG_DRV(" SCSI CBW is not valid\r\n");
p_msc->stage = MSC_STAGE_NEED_RESET; p_msc->stage = MSC_STAGE_NEED_RESET;
// invalid CBW stall both endpoints
usbd_edpt_stall(rhport, p_msc->ep_in); usbd_edpt_stall(rhport, p_msc->ep_in);
usbd_edpt_stall(rhport, p_msc->ep_out); usbd_edpt_stall(rhport, p_msc->ep_out);
return false; return false;
} }
memcpy(p_cbw, _mscd_epbuf.ep_buf, sizeof(msc_cbw_t));
TU_LOG_DRV(" SCSI Command [Lun%u]: %s\r\n", p_cbw->lun, tu_lookup_find(&_msc_scsi_cmd_table, p_cbw->command[0])); TU_LOG_DRV(" SCSI Command [Lun%u]: %s\r\n", p_cbw->lun, tu_lookup_find(&_msc_scsi_cmd_table, p_cbw->command[0]));
//TU_LOG_MEM(MSC_DEBUG, p_cbw, xferred_bytes, 2); //TU_LOG_MEM(MSC_DEBUG, p_cbw, xferred_bytes, 2);
@ -416,87 +378,65 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
p_msc->xferred_len = 0; p_msc->xferred_len = 0;
// Read10 or Write10 // Read10 or 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])) {
{
uint8_t const status = rdwr10_validate_cmd(p_cbw); uint8_t const status = rdwr10_validate_cmd(p_cbw);
if ( status != MSC_CSW_STATUS_PASSED) if (status != MSC_CSW_STATUS_PASSED) {
{
fail_scsi_op(rhport, p_msc, status); fail_scsi_op(rhport, p_msc, status);
}else if ( p_cbw->total_bytes ) } else if (p_cbw->total_bytes) {
{ if (SCSI_CMD_READ_10 == p_cbw->command[0]) {
if (SCSI_CMD_READ_10 == p_cbw->command[0])
{
proc_read10_cmd(rhport, p_msc); proc_read10_cmd(rhport, p_msc);
}else } else {
{
proc_write10_cmd(rhport, p_msc); proc_write10_cmd(rhport, p_msc);
} }
}else } else {
{
// no data transfer, only exist in complaint test suite // no data transfer, only exist in complaint test suite
p_msc->stage = MSC_STAGE_STATUS; p_msc->stage = MSC_STAGE_STATUS;
} }
} } else {
else
{
// For other SCSI commands // For other SCSI commands
// 1. OUT : queue transfer (invoke app callback after done) // 1. OUT : queue transfer (invoke app callback after done)
// 2. IN & Zero: Process if is built-in, else Invoke app callback. Skip DATA if zero length // 2. IN & Zero: Process if is built-in, else Invoke app callback. Skip DATA if zero length
if ( (p_cbw->total_bytes > 0 ) && !is_data_in(p_cbw->dir) ) if ((p_cbw->total_bytes > 0) && !is_data_in(p_cbw->dir)) {
{ if (p_cbw->total_bytes > CFG_TUD_MSC_EP_BUFSIZE) {
if (p_cbw->total_bytes > CFG_TUD_MSC_EP_BUFSIZE)
{
TU_LOG_DRV(" SCSI reject non READ10/WRITE10 with large data\r\n"); TU_LOG_DRV(" SCSI reject non READ10/WRITE10 with large data\r\n");
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED); fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
}else } else {
{
// Didn't check for case 9 (Ho > Dn), which requires examining scsi command first // Didn't check for case 9 (Ho > Dn), which requires examining scsi command first
// but it is OK to just receive data then responded with failed status // but it is OK to just receive data then responded with failed status
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, p_msc->ep_buf, (uint16_t) p_msc->total_len) ); TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_epbuf.ep_buf, (uint16_t) p_msc->total_len));
} }
}else } else {
{
// First process if it is a built-in commands // First process if it is a built-in commands
int32_t resplen = proc_builtin_scsi(p_cbw->lun, p_cbw->command, p_msc->ep_buf, CFG_TUD_MSC_EP_BUFSIZE); int32_t resplen = proc_builtin_scsi(p_cbw->lun, p_cbw->command, _mscd_epbuf.ep_buf, CFG_TUD_MSC_EP_BUFSIZE);
// Invoke user callback if not built-in // Invoke user callback if not built-in
if ( (resplen < 0) && (p_msc->sense_key == 0) ) if ((resplen < 0) && (p_msc->sense_key == 0)) {
{ resplen = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_epbuf.ep_buf, (uint16_t)p_msc->total_len);
resplen = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, p_msc->ep_buf, (uint16_t) p_msc->total_len);
} }
if ( resplen < 0 ) if (resplen < 0) {
{
// unsupported command // unsupported command
TU_LOG_DRV(" SCSI unsupported or failed command\r\n"); TU_LOG_DRV(" SCSI unsupported or failed command\r\n");
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED); fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
} } else if (resplen == 0) {
else if (resplen == 0) if (p_cbw->total_bytes) {
{
if (p_cbw->total_bytes)
{
// 6.7 The 13 Cases: case 4 (Hi > Dn) // 6.7 The 13 Cases: case 4 (Hi > Dn)
// TU_LOG(MSC_DEBUG, " SCSI case 4 (Hi > Dn): %lu\r\n", p_cbw->total_bytes); // TU_LOG(MSC_DEBUG, " SCSI case 4 (Hi > Dn): %lu\r\n", p_cbw->total_bytes);
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED); fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
}else } else {
{
// case 1 Hn = Dn: all good // case 1 Hn = Dn: all good
p_msc->stage = MSC_STAGE_STATUS; p_msc->stage = MSC_STAGE_STATUS;
} }
} } else {
else if (p_cbw->total_bytes == 0) {
{
if ( p_cbw->total_bytes == 0 )
{
// 6.7 The 13 Cases: case 2 (Hn < Di) // 6.7 The 13 Cases: case 2 (Hn < Di)
// TU_LOG(MSC_DEBUG, " SCSI case 2 (Hn < Di): %lu\r\n", p_cbw->total_bytes); // TU_LOG(MSC_DEBUG, " SCSI case 2 (Hn < Di): %lu\r\n", p_cbw->total_bytes);
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED); fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
}else } else {
{
// cannot return more than host expect // cannot return more than host expect
p_msc->total_len = tu_min32((uint32_t) resplen, p_cbw->total_bytes); p_msc->total_len = tu_min32((uint32_t)resplen, p_cbw->total_bytes);
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, p_msc->ep_buf, (uint16_t) p_msc->total_len) ); TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_epbuf.ep_buf, (uint16_t) p_msc->total_len));
} }
} }
} }
@ -507,50 +447,37 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
TU_LOG_DRV(" SCSI Data [Lun%u]\r\n", p_cbw->lun); TU_LOG_DRV(" SCSI Data [Lun%u]\r\n", p_cbw->lun);
//TU_LOG_MEM(MSC_DEBUG, p_msc->ep_buf, xferred_bytes, 2); //TU_LOG_MEM(MSC_DEBUG, p_msc->ep_buf, xferred_bytes, 2);
if (SCSI_CMD_READ_10 == p_cbw->command[0]) if (SCSI_CMD_READ_10 == p_cbw->command[0]) {
{
p_msc->xferred_len += xferred_bytes; p_msc->xferred_len += xferred_bytes;
if ( p_msc->xferred_len >= p_msc->total_len ) if ( p_msc->xferred_len >= p_msc->total_len ) {
{
// Data Stage is complete // Data Stage is complete
p_msc->stage = MSC_STAGE_STATUS; p_msc->stage = MSC_STAGE_STATUS;
}else }else {
{
proc_read10_cmd(rhport, p_msc); proc_read10_cmd(rhport, p_msc);
} }
} } else if (SCSI_CMD_WRITE_10 == p_cbw->command[0]) {
else if (SCSI_CMD_WRITE_10 == p_cbw->command[0])
{
proc_write10_new_data(rhport, p_msc, xferred_bytes); proc_write10_new_data(rhport, p_msc, xferred_bytes);
} } else {
else
{
p_msc->xferred_len += xferred_bytes; p_msc->xferred_len += xferred_bytes;
// OUT transfer, invoke callback if needed // OUT transfer, invoke callback if needed
if ( !is_data_in(p_cbw->dir) ) if ( !is_data_in(p_cbw->dir) ) {
{ int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_epbuf.ep_buf, (uint16_t) p_msc->total_len);
int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, p_msc->ep_buf, (uint16_t) p_msc->total_len);
if ( cb_result < 0 ) if ( cb_result < 0 ) {
{
// unsupported command // unsupported command
TU_LOG_DRV(" SCSI unsupported command\r\n"); TU_LOG_DRV(" SCSI unsupported command\r\n");
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED); fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
}else }else {
{
// TODO haven't implement this scenario any further yet // TODO haven't implement this scenario any further yet
} }
} }
if ( p_msc->xferred_len >= p_msc->total_len ) if ( p_msc->xferred_len >= p_msc->total_len ) {
{
// Data Stage is complete // Data Stage is complete
p_msc->stage = MSC_STAGE_STATUS; p_msc->stage = MSC_STAGE_STATUS;
} } else {
else
{
// This scenario with command that take more than one transfer is already rejected at Command stage // This scenario with command that take more than one transfer is already rejected at Command stage
TU_BREAKPOINT(); TU_BREAKPOINT();
} }
@ -563,53 +490,52 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
case MSC_STAGE_STATUS_SENT: case MSC_STAGE_STATUS_SENT:
// Wait for the Status phase to complete // Wait for the Status phase to complete
if( (ep_addr == p_msc->ep_in) && (xferred_bytes == sizeof(msc_csw_t)) ) 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_DRV(" SCSI Status [Lun%u] = %u\r\n", p_cbw->lun, p_csw->status);
// TU_LOG_MEM(MSC_DEBUG, p_csw, xferred_bytes, 2); // TU_LOG_MEM(MSC_DEBUG, p_csw, xferred_bytes, 2);
// Invoke complete callback if defined // Invoke complete callback if defined
// Note: There is racing issue with samd51 + qspi flash testing with arduino // Note: There is racing issue with samd51 + qspi flash testing with arduino
// if complete_cb() is invoked after queuing the status. // if complete_cb() is invoked after queuing the status.
switch(p_cbw->command[0]) switch (p_cbw->command[0]) {
{
case SCSI_CMD_READ_10: case SCSI_CMD_READ_10:
if ( tud_msc_read10_complete_cb ) tud_msc_read10_complete_cb(p_cbw->lun); if (tud_msc_read10_complete_cb) {
tud_msc_read10_complete_cb(p_cbw->lun);
}
break; break;
case SCSI_CMD_WRITE_10: case SCSI_CMD_WRITE_10:
if ( tud_msc_write10_complete_cb ) tud_msc_write10_complete_cb(p_cbw->lun); if (tud_msc_write10_complete_cb) {
tud_msc_write10_complete_cb(p_cbw->lun);
}
break; break;
default: default:
if ( tud_msc_scsi_complete_cb ) tud_msc_scsi_complete_cb(p_cbw->lun, p_cbw->command); if (tud_msc_scsi_complete_cb) {
tud_msc_scsi_complete_cb(p_cbw->lun, p_cbw->command);
}
break; break;
} }
TU_ASSERT( prepare_cbw(rhport, p_msc) ); TU_ASSERT(prepare_cbw(rhport, p_msc));
}else } else {
{
// Any xfer ended here is consider unknown error, ignore it // Any xfer ended here is consider unknown error, ignore it
TU_LOG1(" Warning expect SCSI Status but received unknown data\r\n"); TU_LOG1(" Warning expect SCSI Status but received unknown data\r\n");
} }
break; break;
default : break; default: break;
} }
if ( p_msc->stage == MSC_STAGE_STATUS ) if (p_msc->stage == MSC_STAGE_STATUS) {
{
// skip status if epin is currently stalled, will do it when received Clear Stall request // 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 (!usbd_edpt_stalled(rhport, p_msc->ep_in)) {
{ if ((p_cbw->total_bytes > p_msc->xferred_len) && is_data_in(p_cbw->dir)) {
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 // 6.7 The 13 Cases: case 5 (Hi > Di): STALL before status
// TU_LOG(MSC_DEBUG, " SCSI case 5 (Hi > Di): %lu > %lu\r\n", p_cbw->total_bytes, p_msc->xferred_len); // TU_LOG(MSC_DEBUG, " 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); usbd_edpt_stall(rhport, p_msc->ep_in);
}else } else {
{ TU_ASSERT(send_csw(rhport, p_msc));
TU_ASSERT( send_csw(rhport, p_msc) );
} }
} }
@ -617,8 +543,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
// WORKAROUND: cxd56 has its own nuttx usb stack which does not forward Set/ClearFeature(Endpoint) to DCD. // 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 // 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 // hope everything will work
if ( usbd_edpt_stalled(rhport, p_msc->ep_in) ) if ( usbd_edpt_stalled(rhport, p_msc->ep_in) ) {
{
usbd_edpt_clear_stall(rhport, p_msc->ep_in); usbd_edpt_clear_stall(rhport, p_msc->ep_in);
send_csw(rhport, p_msc); send_csw(rhport, p_msc);
} }
@ -634,40 +559,39 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
// return response's length (copied to buffer). Negative if it is not an built-in command or indicate Failed status (CSW) // return response's length (copied to buffer). Negative if it is not an built-in command or indicate Failed status (CSW)
// In case of a failed status, sense key must be set for reason of failure // In case of a failed status, sense key must be set for reason of failure
static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buffer, uint32_t bufsize) static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buffer, uint32_t bufsize) {
{ (void)bufsize; // TODO refractor later
(void) bufsize; // TODO refractor later
int32_t resplen; int32_t resplen;
mscd_interface_t* p_msc = &_mscd_itf; mscd_interface_t* p_msc = &_mscd_itf;
switch ( scsi_cmd[0] ) switch (scsi_cmd[0]) {
{
case SCSI_CMD_TEST_UNIT_READY: case SCSI_CMD_TEST_UNIT_READY:
resplen = 0; resplen = 0;
if ( !tud_msc_test_unit_ready_cb(lun) ) if (!tud_msc_test_unit_ready_cb(lun)) {
{
// Failed status response // Failed status response
resplen = - 1; resplen = -1;
// set default sense if not set by callback // set default sense if not set by callback
if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun); if (p_msc->sense_key == 0) {
set_sense_medium_not_present(lun);
}
} }
break; break;
case SCSI_CMD_START_STOP_UNIT: case SCSI_CMD_START_STOP_UNIT:
resplen = 0; resplen = 0;
if (tud_msc_start_stop_cb) if (tud_msc_start_stop_cb) {
{ scsi_start_stop_unit_t const* start_stop = (scsi_start_stop_unit_t const*)scsi_cmd;
scsi_start_stop_unit_t const * start_stop = (scsi_start_stop_unit_t const *) scsi_cmd; if (!tud_msc_start_stop_cb(lun, start_stop->power_condition, start_stop->start, start_stop->load_eject)) {
if ( !tud_msc_start_stop_cb(lun, start_stop->power_condition, start_stop->start, start_stop->load_eject) )
{
// Failed status response // Failed status response
resplen = - 1; resplen = -1;
// set default sense if not set by callback // set default sense if not set by callback
if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun); if (p_msc->sense_key == 0) {
set_sense_medium_not_present(lun);
}
} }
} }
break; break;
@ -675,40 +599,39 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
resplen = 0; resplen = 0;
if (tud_msc_prevent_allow_medium_removal_cb) if (tud_msc_prevent_allow_medium_removal_cb) {
{ scsi_prevent_allow_medium_removal_t const* prevent_allow = (scsi_prevent_allow_medium_removal_t const*)scsi_cmd;
scsi_prevent_allow_medium_removal_t const * prevent_allow = (scsi_prevent_allow_medium_removal_t const *) scsi_cmd; if (!tud_msc_prevent_allow_medium_removal_cb(lun, prevent_allow->prohibit_removal, prevent_allow->control)) {
if ( !tud_msc_prevent_allow_medium_removal_cb(lun, prevent_allow->prohibit_removal, prevent_allow->control) )
{
// Failed status response // Failed status response
resplen = - 1; resplen = -1;
// set default sense if not set by callback // set default sense if not set by callback
if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun); if (p_msc->sense_key == 0) {
set_sense_medium_not_present(lun);
}
} }
} }
break; break;
case SCSI_CMD_READ_CAPACITY_10: case SCSI_CMD_READ_CAPACITY_10: {
{
uint32_t block_count; uint32_t block_count;
uint32_t block_size; uint32_t block_size;
uint16_t block_size_u16; uint16_t block_size_u16;
tud_msc_capacity_cb(lun, &block_count, &block_size_u16); tud_msc_capacity_cb(lun, &block_count, &block_size_u16);
block_size = (uint32_t) block_size_u16; block_size = (uint32_t)block_size_u16;
// Invalid block size/count from callback, possibly unit is not ready // Invalid block size/count from callback, possibly unit is not ready
// stall this request, set sense key to NOT READY // stall this request, set sense key to NOT READY
if (block_count == 0 || block_size == 0) if (block_count == 0 || block_size == 0) {
{
resplen = -1; resplen = -1;
// set default sense if not set by callback // set default sense if not set by callback
if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun); if (p_msc->sense_key == 0) {
}else set_sense_medium_not_present(lun);
{ }
} else {
scsi_read_capacity10_resp_t read_capa10; scsi_read_capacity10_resp_t read_capa10;
read_capa10.last_lba = tu_htonl(block_count-1); read_capa10.last_lba = tu_htonl(block_count-1);
@ -720,8 +643,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
} }
break; break;
case SCSI_CMD_READ_FORMAT_CAPACITY: 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, .list_length = 8,
@ -737,14 +659,14 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
// Invalid block size/count from callback, possibly unit is not ready // Invalid block size/count from callback, possibly unit is not ready
// stall this request, set sense key to NOT READY // stall this request, set sense key to NOT READY
if (block_count == 0 || block_size == 0) if (block_count == 0 || block_size == 0) {
{
resplen = -1; resplen = -1;
// set default sense if not set by callback // set default sense if not set by callback
if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun); if (p_msc->sense_key == 0) {
}else set_sense_medium_not_present(lun);
{ }
} else {
read_fmt_capa.block_num = tu_htonl(block_count); read_fmt_capa.block_num = tu_htonl(block_count);
read_fmt_capa.block_size_u16 = tu_htons(block_size); read_fmt_capa.block_size_u16 = tu_htons(block_size);
@ -754,8 +676,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
} }
break; break;
case SCSI_CMD_INQUIRY: case SCSI_CMD_INQUIRY: {
{
scsi_inquiry_resp_t inquiry_rsp = scsi_inquiry_resp_t inquiry_rsp =
{ {
.is_removable = 1, .is_removable = 1,
@ -765,8 +686,8 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
}; };
// vendor_id, product_id, product_rev is space padded string // vendor_id, product_id, product_rev is space padded string
memset(inquiry_rsp.vendor_id , ' ', sizeof(inquiry_rsp.vendor_id)); memset(inquiry_rsp.vendor_id, ' ', sizeof(inquiry_rsp.vendor_id));
memset(inquiry_rsp.product_id , ' ', sizeof(inquiry_rsp.product_id)); memset(inquiry_rsp.product_id, ' ', sizeof(inquiry_rsp.product_id));
memset(inquiry_rsp.product_rev, ' ', sizeof(inquiry_rsp.product_rev)); memset(inquiry_rsp.product_rev, ' ', sizeof(inquiry_rsp.product_rev));
tud_msc_inquiry_cb(lun, inquiry_rsp.vendor_id, inquiry_rsp.product_id, inquiry_rsp.product_rev); tud_msc_inquiry_cb(lun, inquiry_rsp.vendor_id, inquiry_rsp.product_id, inquiry_rsp.product_rev);
@ -776,8 +697,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
} }
break; break;
case SCSI_CMD_MODE_SENSE_6: case SCSI_CMD_MODE_SENSE_6: {
{
scsi_mode_sense6_resp_t mode_resp = scsi_mode_sense6_resp_t mode_resp =
{ {
.data_len = 3, .data_len = 3,
@ -788,8 +708,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
}; };
bool writable = true; bool writable = true;
if ( tud_msc_is_writable_cb ) if (tud_msc_is_writable_cb) {
{
writable = tud_msc_is_writable_cb(lun); writable = tud_msc_is_writable_cb(lun);
} }
@ -800,8 +719,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
} }
break; break;
case SCSI_CMD_REQUEST_SENSE: 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 .response_code = 0x70, // current, fixed format
@ -809,7 +727,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
}; };
sense_rsp.add_sense_len = sizeof(scsi_sense_fixed_resp_t) - 8; sense_rsp.add_sense_len = sizeof(scsi_sense_fixed_resp_t) - 8;
sense_rsp.sense_key = (uint8_t) (p_msc->sense_key & 0x0F); sense_rsp.sense_key = (uint8_t)(p_msc->sense_key & 0x0F);
sense_rsp.add_sense_code = p_msc->add_sense_code; sense_rsp.add_sense_code = p_msc->add_sense_code;
sense_rsp.add_sense_qualifier = p_msc->add_sense_qualifier; sense_rsp.add_sense_qualifier = p_msc->add_sense_qualifier;
@ -817,9 +735,8 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &sense_rsp, (size_t) resplen)); TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &sense_rsp, (size_t) resplen));
// request sense callback could overwrite the sense data // request sense callback could overwrite the sense data
if (tud_msc_request_sense_cb) if (tud_msc_request_sense_cb) {
{ resplen = tud_msc_request_sense_cb(lun, buffer, (uint16_t)bufsize);
resplen = tud_msc_request_sense_cb(lun, buffer, (uint16_t) bufsize);
} }
// Clear sense data after copy // Clear sense data after copy
@ -827,15 +744,15 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
} }
break; break;
default: resplen = -1; break; default: resplen = -1;
break;
} }
return resplen; return resplen;
} }
static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc) static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc) {
{ msc_cbw_t const* p_cbw = &p_msc->cbw;
msc_cbw_t const * p_cbw = &p_msc->cbw;
// block size already verified not zero // block size already verified not zero
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw); uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
@ -844,14 +761,13 @@ static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz); uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
// remaining bytes capped at class buffer // 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); 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 // Application can consume smaller bytes
uint32_t const offset = p_msc->xferred_len % block_sz; uint32_t const offset = p_msc->xferred_len % block_sz;
nbytes = tud_msc_read10_cb(p_cbw->lun, lba, offset, p_msc->ep_buf, (uint32_t) nbytes); nbytes = tud_msc_read10_cb(p_cbw->lun, lba, offset, _mscd_epbuf.ep_buf, (uint32_t)nbytes);
if ( nbytes < 0 ) if (nbytes < 0) {
{
// negative means error -> endpoint is stalled & status in CSW set to failed // 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(" tud_msc_read10_cb() return -1\r\n");
@ -859,30 +775,23 @@ static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
set_sense_medium_not_present(p_cbw->lun); set_sense_medium_not_present(p_cbw->lun);
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED); fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
} } else if (nbytes == 0) {
else if ( nbytes == 0 )
{
// zero means not ready -> simulate an transfer complete so that this driver callback will fired again // zero means not ready -> simulate an transfer complete so that this driver callback will fired again
dcd_event_xfer_complete(rhport, p_msc->ep_in, 0, XFER_RESULT_SUCCESS, false); dcd_event_xfer_complete(rhport, p_msc->ep_in, 0, XFER_RESULT_SUCCESS, false);
} } else {
else TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_epbuf.ep_buf, (uint16_t) nbytes),);
{
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, p_msc->ep_buf, (uint16_t) nbytes), );
} }
} }
static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc) static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc) {
{ msc_cbw_t const* p_cbw = &p_msc->cbw;
msc_cbw_t const * p_cbw = &p_msc->cbw;
bool writable = true; bool writable = true;
if ( tud_msc_is_writable_cb ) if (tud_msc_is_writable_cb) {
{
writable = tud_msc_is_writable_cb(p_cbw->lun); writable = tud_msc_is_writable_cb(p_cbw->lun);
} }
if ( !writable ) if (!writable) {
{
// Not writable, complete this SCSI op with error // Not writable, complete this SCSI op with error
// Sense = Write protected // Sense = Write protected
tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_DATA_PROTECT, 0x27, 0x00); tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_DATA_PROTECT, 0x27, 0x00);
@ -891,16 +800,15 @@ static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
} }
// remaining bytes capped at class buffer // 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); 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 // Write10 callback will be called later when usb transfer complete
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, p_msc->ep_buf, nbytes), ); TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_epbuf.ep_buf, nbytes),);
} }
// process new data arrived from WRITE10 // process new data arrived from WRITE10
static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint32_t xferred_bytes) static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint32_t xferred_bytes) {
{ msc_cbw_t const* p_cbw = &p_msc->cbw;
msc_cbw_t const * p_cbw = &p_msc->cbw;
// block size already verified not zero // block size already verified not zero
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw); uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
@ -910,10 +818,9 @@ static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint3
// Invoke callback to consume new data // Invoke callback to consume new data
uint32_t const offset = p_msc->xferred_len % block_sz; uint32_t const offset = p_msc->xferred_len % block_sz;
int32_t nbytes = tud_msc_write10_cb(p_cbw->lun, lba, offset, p_msc->ep_buf, xferred_bytes); int32_t nbytes = tud_msc_write10_cb(p_cbw->lun, lba, offset, _mscd_epbuf.ep_buf, xferred_bytes);
if ( nbytes < 0 ) if (nbytes < 0) {
{
// negative means error -> failed this scsi op // negative means error -> failed this scsi op
TU_LOG_DRV(" tud_msc_write10_cb() return -1\r\n"); TU_LOG_DRV(" tud_msc_write10_cb() return -1\r\n");
@ -924,32 +831,25 @@ static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint3
set_sense_medium_not_present(p_cbw->lun); set_sense_medium_not_present(p_cbw->lun);
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED); fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
}else } else {
{
// Application consume less than what we got (including zero) // Application consume less than what we got (including zero)
if ( (uint32_t) nbytes < xferred_bytes ) if ((uint32_t)nbytes < xferred_bytes) {
{ uint32_t const left_over = xferred_bytes - (uint32_t)nbytes;
uint32_t const left_over = xferred_bytes - (uint32_t) nbytes; if (nbytes > 0) {
if ( nbytes > 0 ) p_msc->xferred_len += (uint16_t)nbytes;
{ memmove(_mscd_epbuf.ep_buf, _mscd_epbuf.ep_buf + nbytes, left_over);
p_msc->xferred_len += (uint16_t) nbytes;
memmove(p_msc->ep_buf, p_msc->ep_buf+nbytes, left_over);
} }
// simulate an transfer complete with adjusted parameters --> callback will be invoked with adjusted parameter // simulate an transfer complete with adjusted parameters --> callback will be invoked with adjusted parameter
dcd_event_xfer_complete(rhport, p_msc->ep_out, left_over, XFER_RESULT_SUCCESS, false); dcd_event_xfer_complete(rhport, p_msc->ep_out, left_over, XFER_RESULT_SUCCESS, false);
} } else {
else
{
// Application consume all bytes in our buffer // Application consume all bytes in our buffer
p_msc->xferred_len += xferred_bytes; p_msc->xferred_len += xferred_bytes;
if ( p_msc->xferred_len >= p_msc->total_len ) if (p_msc->xferred_len >= p_msc->total_len) {
{
// Data Stage is complete // Data Stage is complete
p_msc->stage = MSC_STAGE_STATUS; p_msc->stage = MSC_STAGE_STATUS;
}else } else {
{
// prepare to receive more data from host // prepare to receive more data from host
proc_write10_cmd(rhport, p_msc); proc_write10_cmd(rhport, p_msc);
} }