msc host: data stage DMAs to/from the caller's buffer, which need not be aligned
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- c
- Domain
- embedded-iot
Research direction
Start with src/class/msc/msc_host.c, especially msch_epbuf_t, data_stage_xfer(), and the continuation logic in msch_xfer_cb(). Read the existing endpoint buffer configuration and related alignment change in #4517, then check MSC host tests or examples that exercise reads and writes. Done means unaligned caller buffers transfer correctly through aligned staging memory in chunks, with data copied in the right direction.
Written by the indexing model from the issue text.
Description
Found while reviewing the WCH USBHS v1 host driver against #4517 (usbd/usbh: assert the controller's transfer buffer alignment).
Problem
The MSC host keeps the CBW and CSW in its own CFG_TUH_MEM_SECTION endpoint buffer (msch_epbuf_t, src/class/msc/msc_host.c:71-75), but the data stage runs the bulk transfer directly on the application's buffer:
// src/class/msc/msc_host.c:89-92
static bool data_stage_xfer(uint8_t daddr, msch_interface_t* p_msc, const msc_cbw_t* cbw) {
...
return usbh_edpt_xfer(daddr, ep_data, (uint8_t*) p_msc->buffer + p_msc->data_xferred, len);
}
That buffer comes unchanged from tuh_msc_scsi_command(), tuh_msc_read10(), tuh_msc_write10(), tuh_msc_inquiry(), tuh_msc_request_sense() and tuh_msc_read_capacity(). Nothing requires it to be in CFG_TUH_MEM_SECTION memory or aligned to CFG_TUH_MEM_ALIGN.
Why it breaks
With #4517, usbh_edpt_xfer_with_callback() asserts the buffer meets TUP_HCD_MEM_ALIGN, so an unaligned caller buffer now fails at submission instead of DMAing to the wrong address. FatFS passes the caller's f_read()/f_write() buffer straight to disk_read()/disk_write() for whole sectors (lib/fatfs/source/ff.c:4048 read, :4163 write), and examples/host/msc_file_explorer/src/msc_app.c:252 forwards it to tuh_msc_write10(). So f_write(&fp, buf + 1, 1024, &bw) from an ordinary application buffer now fails with FR_DISK_ERR.
Proposed fix
Mirror the device class (CFG_TUD_MSC_EP_BUFSIZE): add an aligned CFG_TUH_MEM_SECTION data buffer to msch_epbuf_t (e.g. CFG_TUH_MSC_EP_BUFSIZE, a multiple of the bulk MPS) and run the data stage through it in chunks, copying to/from p_msc->buffer around each chunk. The existing MSCH_DATA_XFER_MAX chunking in msch_xfer_cb() already continues a data stage across several transfers. The copy cost could be skipped when the caller's buffer already qualifies, but only if "qualifies" can be decided portably, so the plain staging path should come first.
Deferred because it changes msc_host's RAM footprint and data path for every port. It is separate from #4517, which only makes the existing requirement fail loudly.
Related: #4517 — adds the alignment assert that turns this into a submission failure.
Related: #3985 — msc host example and BOT recovery; touches the same data-stage path, not buffer placement.
Related: #4488 — the same 4-byte RX DMA alignment rule, on the WCH USBHS device side.
- Dominant language
- C
- Stars
- 7.3k
- Forks
- 1.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 67
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from hathach/tinyusb
-
follow-up Port WCH USBFS Prio 📌
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 1 day
-
follow-up Prio 📌
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
hil-contract: a background hil_remote.py launch is killed at the Bash default 10 min timeoutPossibly taken @hathach claimed this 16 days ago. Openfollow-up Prio 📌
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
ch32v30x: debug UART loses queued bytes when its TX ring fillsPossibly taken @hathach claimed this 16 days ago. Openfollow-up Prio 📌
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
followup Prio 📌
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
-
Missing slice offset check in jvp_equal fast path corrupts slice comparison and aborts delpathsOpen
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 3 days
-
Difficulty 1/5 Under an hour Newbie friendliness 83/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
area9innovation/flow9#1805 ·
Maintainers usually reply within 3 days
-
AI:Robustness triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
open5gs/open5gs#5246 · 1 comment ·
Maintainers usually reply within 1 day