-
Notifications
You must be signed in to change notification settings - Fork 1.2k
MSC Device: Add asynchronous IO support #2967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HiFiPhile
wants to merge
9
commits into
hathach:master
Choose a base branch
from
HiFiPhile:async_io
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
I'm using this in a work project. I'd love to see something around these lines landed. |
This is a very practical feature, and I also hope that it can be implemented as soon as possible. |
Signed-off-by: HiFiPhile <[email protected]>
Comment on lines
419
to
602
}else { | ||
proc_read10_cmd(rhport, p_msc); | ||
proc_read10_cmd(p_msc); | ||
} | ||
} else if (SCSI_CMD_WRITE_10 == p_cbw->command[0]) { | ||
proc_write10_new_data(rhport, p_msc, xferred_bytes); | ||
proc_write10_new_data(p_msc, xferred_bytes); | ||
} else { | ||
p_msc->xferred_len += xferred_bytes; | ||
|
||
// OUT transfer, invoke callback if needed | ||
if ( !is_data_in(p_cbw->dir) ) { | ||
int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_epbuf.buf, (uint16_t) p_msc->total_len); | ||
|
||
if ( cb_result < 0 ) { | ||
// unsupported command | ||
TU_LOG_DRV(" SCSI unsupported command\r\n"); | ||
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED); | ||
fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED); | ||
}else { | ||
// TODO haven't implement this scenario any further yet | ||
} | ||
} | ||
|
||
if ( p_msc->xferred_len >= p_msc->total_len ) { | ||
// Data Stage is complete | ||
p_msc->stage = MSC_STAGE_STATUS; | ||
} else { | ||
// This scenario with command that take more than one transfer is already rejected at Command stage | ||
TU_BREAKPOINT(); | ||
} | ||
} | ||
break; | ||
|
||
case MSC_STAGE_STATUS: | ||
// processed immediately after this switch, supposedly to be empty | ||
break; | ||
|
||
case MSC_STAGE_STATUS_SENT: | ||
// Wait for the Status phase to 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); | ||
|
||
// Invoke complete callback if defined | ||
// Note: There is racing issue with samd51 + qspi flash testing with arduino | ||
// if complete_cb() is invoked after queuing the status. | ||
switch (p_cbw->command[0]) { | ||
case SCSI_CMD_READ_10: | ||
if (tud_msc_read10_complete_cb) { | ||
tud_msc_read10_complete_cb(p_cbw->lun); | ||
} | ||
break; | ||
|
||
case SCSI_CMD_WRITE_10: | ||
if (tud_msc_write10_complete_cb) { | ||
tud_msc_write10_complete_cb(p_cbw->lun); | ||
} | ||
break; | ||
|
||
default: | ||
if (tud_msc_scsi_complete_cb) { | ||
tud_msc_scsi_complete_cb(p_cbw->lun, p_cbw->command); | ||
} | ||
break; | ||
} | ||
|
||
TU_ASSERT(prepare_cbw(rhport, p_msc)); | ||
TU_ASSERT(prepare_cbw(p_msc)); | ||
} else { | ||
// Any xfer ended here is consider unknown error, ignore it | ||
TU_LOG1(" Warning expect SCSI Status but received unknown data\r\n"); | ||
} | ||
break; | ||
|
||
default: break; | ||
} | ||
|
||
if (p_msc->stage == MSC_STAGE_STATUS) { | ||
// 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(rhport, p_msc)); | ||
} | ||
} | ||
|
||
#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(rhport, p_msc); | ||
} | ||
#endif | ||
TU_ASSERT(proc_stage_status(p_msc)); |
Check notice
Code scanning / CodeQL
Long switch case Note
Switch has at least one case that is too long: .
Switch has at least one case that is too long: .
Switch has at least one case that is too long: .
MSC_STAGE_CMD (99 lines)
Error loading related location
LoadingSwitch has at least one case that is too long:
MSC_STAGE_DATA (41 lines)
Error loading related location
LoadingSwitch has at least one case that is too long:
MSC_STAGE_STATUS_SENT (35 lines)
Error loading related location
Loading
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe the PR
Currently, mixing the MSC class with other classes could be problematic since IO operations could block
tud_task()
for a long time and degrade real-time performance, especially for isochronous and interrupt transfer-based classes.Now, it's possible to delegate IO operations to a background task.
The
cdc_msc_freertos
example has been updated for demonstration. One can changeCFG_EXAMPLE_MSC_IO_DELAY_MS
to a high value and type in the console while doing disk IO. WithCFG_EXAMPLE_MSC_ASYNC_IO
ON and OFF, the latency difference should be very noticeable.