msc host: data stage DMAs to/from the caller's buffer, which need not be aligned

Open
#4,550 0 comments 0 reactions 0 assignees View on GitHub

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

follow-up Prio 📌

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from hathach/tinyusb

All issues in hathach/tinyusb

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.