blob: 40e054170263a420b635bb3ca8f7bc2cc614338f [file]
************
Mass Storage
************
Roles: device and host. TinyUSB implements the USB Mass Storage Bulk-Only
Transport (BOT) and common SCSI commands. It exposes logical blocks; a
filesystem such as FatFs is a separate application layer.
Device
======
Enable ``CFG_TUD_MSC`` and set ``CFG_TUD_MSC_EP_BUFSIZE``. The driver supports
a single Mass Storage function, so add one ``TUD_MSC_DESCRIPTOR``. Multiple
logical units (LUNs) share that function and are selected through the ``lun``
callback argument.
``CFG_TUD_MSC_EP_BUFSIZE`` has no default and is required when MSC is enabled.
It is the class transfer-buffer size, not the media capacity. Using at least
one logical block is efficient; larger values improve large transfers at the
cost of static RAM. It must be at least 64 and below 65535 bytes, and a
multiple of the Bulk-In endpoint's maximum packet size (64 at full speed, 512
at high speed), so that a full READ10 chunk ends on a packet boundary: the
interface fails to open otherwise. Keep it consistent with controller/DMA
constraints.
The application must implement ``tud_msc_test_unit_ready_cb()``,
``tud_msc_capacity_cb()``, ``tud_msc_read10_cb()``, ``tud_msc_write10_cb()``
and ``tud_msc_scsi_cb()``. The other callbacks below have weak defaults.
.. list-table::
:header-rows: 1
:widths: 38 62
* - Callback
- Purpose
* - ``tud_msc_inquiry2_cb()``
- Fill the INQUIRY response and return its length. The older
``tud_msc_inquiry_cb()`` still works when this returns 0.
* - ``tud_msc_test_unit_ready_cb()``
- Report whether media is present and usable.
* - ``tud_msc_capacity_cb()``
- Return the logical block count and block size.
* - ``tud_msc_read10_cb()``
- Read ``bufsize`` bytes from ``lba`` plus ``offset``.
* - ``tud_msc_write10_cb()``
- Write ``bufsize`` bytes to ``lba`` plus ``offset``.
* - ``tud_msc_scsi_cb()``
- Handle commands not implemented by the class driver.
* - ``tud_msc_get_maxlun_cb()``
- Returns the number of LUNs (default 1); the driver reports this minus
one to the host. Implement it for multiple LUNs.
* - ``tud_msc_is_writable_cb()``
- Reports write protection before WRITE10.
* - ``tud_msc_start_stop_cb()``
- Handles load/eject and start/stop requests, including safe-eject policy.
Read and write callbacks may cover only part of a logical block; honor both
``lba`` and ``offset`` instead of assuming one callback per block.
Each READ10/WRITE10 is checked against ``tud_msc_capacity_cb()`` before
either callback runs: the blocks must lie within the reported block count and
the transfer length must be the block count times the reported block size.
The callbacks therefore only see an in-range ``lba`` with ``offset`` less than
the block size, so they stay within the disk without their own bounds check.
Set useful sense data with ``tud_msc_set_sense()`` before returning an error
from a command callback, such as ``tud_msc_scsi_cb()`` or read10/write10.
For a ``TUD_MSC_RET_ERROR`` from read10/write10, TinyUSB keeps any sense still
stored and otherwise defaults to NOT READY / MEDIUM NOT PRESENT.
For temporarily busy media, return ``TUD_MSC_RET_BUSY``; TinyUSB will invoke
the callback again with the same parameters. For true background I/O, return
``TUD_MSC_RET_ASYNC`` and later call ``tud_msc_async_io_done()`` with the byte
count or error. Do not report a write complete until data has reached the
durability level promised by the product: finish that storage work before
returning success from ``tud_msc_write10_cb()`` or calling
``tud_msc_async_io_done()``. ``tud_msc_write10_complete_cb()`` runs only after
the host has received the command status, so a cache flush there is
best-effort and cannot make an acknowledged write durable.
See :doc:`../../examples/device/cdc_msc` for a RAM disk and
:doc:`../../examples/device/msc_dual_lun` for multiple LUNs.
Host
====
Set ``CFG_TUH_MSC`` to 1 to enable the driver; it keeps state for every device
address, so ``CFG_TUH_DEVICE_MAX`` sets how many Mass Storage devices can be
used at once. ``CFG_TUH_MSC_MAXLUN`` is the LUN limit per device. After
``tuh_msc_mount_cb(dev_addr)``, query cached geometry with
``tuh_msc_get_block_count()`` and ``tuh_msc_get_block_size()``.
``tuh_msc_get_maxlun()`` returns the LUN count the device reports, clamped to
``CFG_TUH_MSC_MAXLUN``; for a LUN at or past it the geometry getters return 0
and commands are refused. ``CFG_TUH_MSC_MAXLUN`` defaults to 4 and allocates
cached state per possible LUN for each of the ``CFG_TUH_DEVICE_MAX``
addresses. Enumeration only reads the geometry of LUN 0, so other LUNs read
back as 0. Only the first Bulk-Only interface of a device is claimed; a second
one is left to other drivers.
During enumeration a LUN 0 that answers Test Unit Ready with an error is
retried, each failure followed by Request Sense, for up to
``CFG_TUH_MSC_READY_TIMEOUT_MS`` (default 10000) from the first Test Unit
Ready. Sense NOT READY / MEDIUM NOT PRESENT (02/3A/00, e.g. an empty card
reader slot) is not retried. Either way the interface is left unmounted:
``tuh_msc_mount_cb()`` is not called.
``tuh_msc_read10()`` and ``tuh_msc_write10()`` are asynchronous. Keep the
buffer valid, correctly aligned, cache coherent, and accessible to the USB
controller until the ``tuh_msc_complete_cb_t`` callback runs. Check
``tuh_msc_ready()`` before starting another command and check
``cb_data->csw->status`` in the completion callback. The callback has no
separate USB transfer result: a failed CBW or CSW transfer is reported through
the CSW status, as below.
If a command's data or status stage cannot be queued on the host controller
after its CBW was sent, or the CBW transfer fails, or the CSW transfer fails
or is short, the driver completes the command at once with a locally built
CSW whose status is ``MSC_CSW_STATUS_PHASE_ERROR``. The device is then in an
unknown state, so the driver refuses further commands: ``tuh_msc_ready()``
stays ``false`` and every command API returns ``false`` until
``tuh_msc_reset()`` succeeds or the device is re-enumerated. The failed
command is not retried: for a write, the device may have stored part of it.
A Phase Error CSW that a mounted device sends itself is passed to the callback
as is and does not fence the interface. If the device answers an enumeration
command with a Phase Error CSW, the interface stays unmounted and refuses
commands until it is re-enumerated.
.. list-table::
:header-rows: 1
:widths: 40 60
* - Host API or callback
- What it does
* - ``tuh_msc_mounted()`` / ``tuh_msc_ready()``
- Tests whether the MSC device is present or currently able to accept a
new SCSI command.
* - ``tuh_msc_get_maxlun()`` /
``tuh_msc_get_block_count()`` /
``tuh_msc_get_block_size()``
- Returns the LUN count, clamped to ``CFG_TUH_MSC_MAXLUN``, and the
geometry cached during enumeration (LUN 0 only).
* - ``tuh_msc_read10()`` / ``tuh_msc_write10()``
- Queues an integral number of logical blocks and completes through the
supplied callback. Returns ``false`` without queuing if the device is
not mounted or is refusing commands after a failed stage submit or a
failed CBW/CSW transfer, the
LUN is not below ``tuh_msc_get_maxlun()``, or ``block_count`` x block
size exceeds the 32-bit CBW data length.
* - ``tuh_msc_inquiry()`` / ``tuh_msc_request_sense()``
- Queues standard SCSI identification or detailed-error requests into an
application-owned response buffer.
* - ``tuh_msc_scsi_command()``
- Queues a caller-built command block wrapper for commands without a
convenience API.
* - ``tuh_msc_reset()``
- Runs Bulk-Only Reset Recovery on an idle mounted interface: the class
Bulk-Only Mass Storage Reset, then Clear HALT on Bulk-In and Bulk-Out
with a host-side data toggle reset. Its callback reports the result;
only success lifts the refusal above. Host controllers that cannot
reset the toggle (rp2040, pio-usb, WCH USBFS, MAX3421E, RUSB2, OHCI on
an endpoint it has not halted, and SAMD when Bulk-In and Bulk-Out share
an endpoint number) fail it and leave the interface refusing commands.
* - ``tuh_msc_mount_cb()`` / ``tuh_msc_umount_cb()``
- Announces cached capacity availability or invalidates all filesystem and
media state for the address.
TinyUSB does not mount a filesystem automatically. Connect the sector API to
your filesystem's disk I/O layer and invalidate that state in
``tuh_msc_umount_cb()``. The :doc:`../../examples/host/msc_file_explorer`
example demonstrates this with FatFs.
Practical notes
===============
* Hosts cache filesystem data. Physical removal or firmware reset without an
eject/unmount can lose data even when USB transfers succeeded.
* Use the medium-present and write-protect responses consistently; mismatched
capacity or readiness information causes repeated SCSI recovery traffic.
* Validate every LUN and block range before calculating a storage address.
Specification used: *USB Mass Storage Class Bulk-Only Transport*, Revision
1.0. Command formats and sense data come from the applicable SCSI command set.