Skip to main content

usbd_storage/
transport.rs

1//! USB Mass Storage transports
2
3use core::fmt::Debug;
4use usb_device::UsbError;
5use usb_device::bus::UsbBus;
6use usb_device::class::ControlIn;
7use usb_device::descriptor::DescriptorWriter;
8
9#[cfg(feature = "bbb")]
10pub mod bbb;
11
12/// Interface protocol for specific transports
13pub const TRANSPORT_VENDOR_SPECIFIC: u8 = 0xFF;
14
15/// USB Mass Storage transport.
16///
17/// An implementation of this trait can be used as an underlying transport for subclasses
18/// defined in the [subclass] module.
19///
20/// [subclass]: crate::subclass
21pub trait Transport {
22    /// Interface protocol code
23    const PROTO: u8;
24
25    type Bus: UsbBus;
26    #[cfg(not(feature = "defmt"))]
27    type Error: Debug;
28    #[cfg(feature = "defmt")]
29    type Error: Debug + defmt::Format;
30
31    /// Registers all required USB **endpoints** using a provided `writer`.
32    fn get_endpoint_descriptors(&self, writer: &mut DescriptorWriter) -> Result<(), UsbError>;
33
34    /// Called after a USB reset after the bus reset sequence is complete.
35    fn reset(&mut self);
36
37    fn control_in(&mut self, xfer: ControlIn<Self::Bus>);
38
39    /// Drives the IO.
40    ///
41    /// Must be called as often as possible. Usually, from the
42    /// wrapping [usb_device::class::UsbClass::poll]
43    fn poll(&mut self) -> Result<(), TransportError<Self::Error>>;
44}
45
46/// Generic error type that could be used by [Transport] impls.
47#[derive(Debug)]
48#[cfg_attr(feature = "defmt", derive(defmt::Format))]
49pub enum TransportError<E: Debug> {
50    /// USB stack error
51    Usb(UsbError),
52    /// Transport-specific error
53    Error(E),
54}
55
56/// The status of a Mass Storage command.
57///
58/// Refer to the USB-MS doc.
59#[repr(u8)]
60#[derive(Default, Copy, Clone)]
61#[cfg_attr(feature = "defmt", derive(defmt::Format))]
62pub enum CommandStatus {
63    #[default]
64    Passed = 0x00,
65    Failed = 0x01,
66    PhaseError = 0x02,
67}