2022-03-09 00:34:35 +00:00
|
|
|
#![no_std]
|
|
|
|
#![feature(generic_associated_types)]
|
2022-03-25 20:46:14 +00:00
|
|
|
#![feature(type_alias_impl_trait)]
|
2022-03-09 00:34:35 +00:00
|
|
|
|
|
|
|
// This mod MUST go first, so that the others see its macros.
|
|
|
|
pub(crate) mod fmt;
|
|
|
|
|
|
|
|
mod builder;
|
2022-03-25 20:46:14 +00:00
|
|
|
pub mod control;
|
2022-03-09 00:34:35 +00:00
|
|
|
pub mod descriptor;
|
|
|
|
pub mod driver;
|
|
|
|
pub mod types;
|
2022-03-09 22:06:27 +00:00
|
|
|
mod util;
|
2022-03-09 00:34:35 +00:00
|
|
|
|
2022-03-28 00:20:01 +00:00
|
|
|
use heapless::Vec;
|
2022-03-27 21:12:57 +00:00
|
|
|
|
2022-03-09 00:34:35 +00:00
|
|
|
use self::control::*;
|
|
|
|
use self::descriptor::*;
|
2022-03-30 18:17:15 +00:00
|
|
|
use self::driver::{Bus, Driver, Event};
|
2022-03-09 00:34:35 +00:00
|
|
|
use self::types::*;
|
2022-03-09 22:06:27 +00:00
|
|
|
use self::util::*;
|
2022-03-09 00:34:35 +00:00
|
|
|
|
|
|
|
pub use self::builder::Config;
|
|
|
|
pub use self::builder::UsbDeviceBuilder;
|
|
|
|
|
|
|
|
/// The global state of the USB device.
|
|
|
|
///
|
|
|
|
/// In general class traffic is only possible in the `Configured` state.
|
|
|
|
#[repr(u8)]
|
|
|
|
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
|
|
|
pub enum UsbDeviceState {
|
|
|
|
/// The USB device has just been created or reset.
|
|
|
|
Default,
|
|
|
|
|
|
|
|
/// The USB device has received an address from the host.
|
|
|
|
Addressed,
|
|
|
|
|
|
|
|
/// The USB device has been configured and is fully functional.
|
|
|
|
Configured,
|
|
|
|
|
|
|
|
/// The USB device has been suspended by the host or it has been unplugged from the USB bus.
|
|
|
|
Suspend,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The bConfiguration value for the not configured state.
|
|
|
|
pub const CONFIGURATION_NONE: u8 = 0;
|
|
|
|
|
|
|
|
/// The bConfiguration value for the single configuration supported by this device.
|
|
|
|
pub const CONFIGURATION_VALUE: u8 = 1;
|
|
|
|
|
|
|
|
/// The default value for bAlternateSetting for all interfaces.
|
|
|
|
pub const DEFAULT_ALTERNATE_SETTING: u8 = 0;
|
|
|
|
|
2022-03-28 01:19:07 +00:00
|
|
|
pub const MAX_INTERFACE_COUNT: usize = 4;
|
2022-03-28 00:20:01 +00:00
|
|
|
|
2022-03-27 21:12:57 +00:00
|
|
|
pub struct UsbDevice<'d, D: Driver<'d>> {
|
2022-03-09 22:06:27 +00:00
|
|
|
bus: D::Bus,
|
2022-03-30 00:26:30 +00:00
|
|
|
control: ControlPipe<D::ControlPipe>,
|
2022-03-09 00:34:35 +00:00
|
|
|
|
|
|
|
config: Config<'d>,
|
|
|
|
device_descriptor: &'d [u8],
|
|
|
|
config_descriptor: &'d [u8],
|
|
|
|
bos_descriptor: &'d [u8],
|
2022-03-29 19:09:24 +00:00
|
|
|
control_buf: &'d mut [u8],
|
2022-03-09 00:34:35 +00:00
|
|
|
|
|
|
|
device_state: UsbDeviceState,
|
|
|
|
remote_wakeup_enabled: bool,
|
|
|
|
self_powered: bool,
|
|
|
|
pending_address: u8,
|
2022-03-25 20:46:14 +00:00
|
|
|
|
2022-03-28 01:19:07 +00:00
|
|
|
interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>,
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 21:12:57 +00:00
|
|
|
impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
2022-03-09 00:34:35 +00:00
|
|
|
pub(crate) fn build(
|
|
|
|
mut driver: D,
|
|
|
|
config: Config<'d>,
|
|
|
|
device_descriptor: &'d [u8],
|
|
|
|
config_descriptor: &'d [u8],
|
|
|
|
bos_descriptor: &'d [u8],
|
2022-03-28 01:19:07 +00:00
|
|
|
interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>,
|
2022-03-29 19:09:24 +00:00
|
|
|
control_buf: &'d mut [u8],
|
2022-03-09 00:34:35 +00:00
|
|
|
) -> Self {
|
2022-03-25 20:46:14 +00:00
|
|
|
let control = driver
|
|
|
|
.alloc_control_pipe(config.max_packet_size_0 as u16)
|
2022-03-09 00:34:35 +00:00
|
|
|
.expect("failed to alloc control endpoint");
|
|
|
|
|
|
|
|
// Enable the USB bus.
|
|
|
|
// This prevent further allocation by consuming the driver.
|
|
|
|
let driver = driver.enable();
|
|
|
|
|
|
|
|
Self {
|
2022-03-09 22:06:27 +00:00
|
|
|
bus: driver,
|
2022-03-09 00:34:35 +00:00
|
|
|
config,
|
2022-03-30 18:17:15 +00:00
|
|
|
control: ControlPipe::new(control),
|
2022-03-09 00:34:35 +00:00
|
|
|
device_descriptor,
|
|
|
|
config_descriptor,
|
|
|
|
bos_descriptor,
|
2022-03-29 19:09:24 +00:00
|
|
|
control_buf,
|
2022-03-09 00:34:35 +00:00
|
|
|
device_state: UsbDeviceState::Default,
|
|
|
|
remote_wakeup_enabled: false,
|
|
|
|
self_powered: false,
|
|
|
|
pending_address: 0,
|
2022-03-28 01:19:07 +00:00
|
|
|
interfaces,
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(&mut self) {
|
|
|
|
loop {
|
2022-03-25 20:46:14 +00:00
|
|
|
let control_fut = self.control.setup();
|
2022-03-09 22:06:27 +00:00
|
|
|
let bus_fut = self.bus.poll();
|
|
|
|
match select(bus_fut, control_fut).await {
|
|
|
|
Either::Left(evt) => match evt {
|
|
|
|
Event::Reset => {
|
2022-04-02 03:27:45 +00:00
|
|
|
trace!("usb: reset");
|
2022-03-09 22:06:27 +00:00
|
|
|
self.bus.reset();
|
|
|
|
|
|
|
|
self.device_state = UsbDeviceState::Default;
|
|
|
|
self.remote_wakeup_enabled = false;
|
|
|
|
self.pending_address = 0;
|
|
|
|
|
2022-03-28 01:19:07 +00:00
|
|
|
for (_, h) in self.interfaces.iter_mut() {
|
|
|
|
h.reset();
|
2022-03-27 21:12:57 +00:00
|
|
|
}
|
2022-03-09 22:06:27 +00:00
|
|
|
}
|
2022-04-02 03:27:45 +00:00
|
|
|
Event::Resume => {
|
|
|
|
trace!("usb: resume");
|
|
|
|
}
|
2022-03-09 22:06:27 +00:00
|
|
|
Event::Suspend => {
|
2022-04-02 03:27:45 +00:00
|
|
|
trace!("usb: suspend");
|
2022-03-09 22:06:27 +00:00
|
|
|
self.bus.suspend();
|
|
|
|
self.device_state = UsbDeviceState::Suspend;
|
|
|
|
}
|
|
|
|
},
|
2022-04-02 02:53:42 +00:00
|
|
|
Either::Right(req) => match req {
|
|
|
|
Setup::DataIn(req, stage) => self.handle_control_in(req, stage).await,
|
|
|
|
Setup::DataOut(req, stage) => self.handle_control_out(req, stage).await,
|
|
|
|
},
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 18:17:15 +00:00
|
|
|
async fn handle_control_out(&mut self, req: Request, stage: DataOutStage) {
|
2022-03-09 00:34:35 +00:00
|
|
|
const CONFIGURATION_NONE_U16: u16 = CONFIGURATION_NONE as u16;
|
|
|
|
const CONFIGURATION_VALUE_U16: u16 = CONFIGURATION_VALUE as u16;
|
2022-03-29 21:13:16 +00:00
|
|
|
|
2022-03-30 18:17:15 +00:00
|
|
|
let (data, stage) = match self.control.data_out(self.control_buf, stage).await {
|
|
|
|
Ok(data) => data,
|
|
|
|
Err(_) => {
|
|
|
|
warn!("usb: failed to read CONTROL OUT data stage.");
|
|
|
|
return;
|
2022-03-30 00:26:30 +00:00
|
|
|
}
|
2022-03-29 21:13:16 +00:00
|
|
|
};
|
2022-03-09 00:34:35 +00:00
|
|
|
|
2022-03-28 01:16:45 +00:00
|
|
|
match (req.request_type, req.recipient) {
|
|
|
|
(RequestType::Standard, Recipient::Device) => match (req.request, req.value) {
|
|
|
|
(Request::CLEAR_FEATURE, Request::FEATURE_DEVICE_REMOTE_WAKEUP) => {
|
2022-03-09 00:34:35 +00:00
|
|
|
self.remote_wakeup_enabled = false;
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept(stage)
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-03-28 01:16:45 +00:00
|
|
|
(Request::SET_FEATURE, Request::FEATURE_DEVICE_REMOTE_WAKEUP) => {
|
2022-03-09 00:34:35 +00:00
|
|
|
self.remote_wakeup_enabled = true;
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept(stage)
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-03-28 01:16:45 +00:00
|
|
|
(Request::SET_ADDRESS, 1..=127) => {
|
2022-03-09 00:34:35 +00:00
|
|
|
self.pending_address = req.value as u8;
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept(stage)
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-03-28 01:16:45 +00:00
|
|
|
(Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => {
|
2022-03-09 00:34:35 +00:00
|
|
|
self.device_state = UsbDeviceState::Configured;
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept(stage)
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-03-28 01:16:45 +00:00
|
|
|
(Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => match self.device_state {
|
2022-03-30 18:17:15 +00:00
|
|
|
UsbDeviceState::Default => self.control.accept(stage),
|
2022-03-28 01:16:45 +00:00
|
|
|
_ => {
|
|
|
|
self.device_state = UsbDeviceState::Addressed;
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept(stage)
|
2022-03-28 01:16:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => self.control.reject(),
|
|
|
|
},
|
|
|
|
(RequestType::Standard, Recipient::Endpoint) => match (req.request, req.value) {
|
|
|
|
(Request::SET_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
|
|
|
|
let ep_addr = ((req.index as u8) & 0x8f).into();
|
|
|
|
self.bus.set_stalled(ep_addr, true);
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept(stage)
|
2022-03-28 01:16:45 +00:00
|
|
|
}
|
|
|
|
(Request::CLEAR_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
|
|
|
|
let ep_addr = ((req.index as u8) & 0x8f).into();
|
|
|
|
self.bus.set_stalled(ep_addr, false);
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept(stage)
|
2022-03-28 01:16:45 +00:00
|
|
|
}
|
2022-03-25 20:46:14 +00:00
|
|
|
_ => self.control.reject(),
|
2022-03-09 00:34:35 +00:00
|
|
|
},
|
2022-03-29 21:13:16 +00:00
|
|
|
(_, Recipient::Interface) => {
|
2022-03-28 01:19:07 +00:00
|
|
|
let handler = self
|
|
|
|
.interfaces
|
|
|
|
.iter_mut()
|
|
|
|
.find(|(i, _)| req.index == *i as _)
|
|
|
|
.map(|(_, h)| h);
|
2022-03-29 21:13:16 +00:00
|
|
|
|
2022-03-28 01:19:07 +00:00
|
|
|
match handler {
|
2022-03-29 21:13:16 +00:00
|
|
|
Some(handler) => {
|
|
|
|
let response = match (req.request_type, req.request) {
|
|
|
|
(RequestType::Standard, Request::SET_INTERFACE) => {
|
|
|
|
handler.set_interface(req.value)
|
|
|
|
}
|
|
|
|
_ => handler.control_out(req, data),
|
|
|
|
};
|
|
|
|
match response {
|
2022-03-30 18:17:15 +00:00
|
|
|
OutResponse::Accepted => self.control.accept(stage),
|
2022-03-29 21:13:16 +00:00
|
|
|
OutResponse::Rejected => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 01:19:07 +00:00
|
|
|
None => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 20:46:14 +00:00
|
|
|
_ => self.control.reject(),
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 18:17:15 +00:00
|
|
|
async fn handle_control_in(&mut self, req: Request, stage: DataInStage) {
|
2022-03-28 01:16:45 +00:00
|
|
|
match (req.request_type, req.recipient) {
|
|
|
|
(RequestType::Standard, Recipient::Device) => match req.request {
|
|
|
|
Request::GET_STATUS => {
|
2022-03-09 00:34:35 +00:00
|
|
|
let mut status: u16 = 0x0000;
|
|
|
|
if self.self_powered {
|
|
|
|
status |= 0x0001;
|
|
|
|
}
|
|
|
|
if self.remote_wakeup_enabled {
|
|
|
|
status |= 0x0002;
|
|
|
|
}
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept_in(&status.to_le_bytes(), stage).await
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-03-30 18:17:15 +00:00
|
|
|
Request::GET_DESCRIPTOR => self.handle_get_descriptor(req, stage).await,
|
2022-03-28 01:16:45 +00:00
|
|
|
Request::GET_CONFIGURATION => {
|
2022-03-09 00:34:35 +00:00
|
|
|
let status = match self.device_state {
|
|
|
|
UsbDeviceState::Configured => CONFIGURATION_VALUE,
|
|
|
|
_ => CONFIGURATION_NONE,
|
|
|
|
};
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept_in(&status.to_le_bytes(), stage).await
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-03-28 01:16:45 +00:00
|
|
|
_ => self.control.reject(),
|
|
|
|
},
|
|
|
|
(RequestType::Standard, Recipient::Endpoint) => match req.request {
|
|
|
|
Request::GET_STATUS => {
|
|
|
|
let ep_addr: EndpointAddress = ((req.index as u8) & 0x8f).into();
|
|
|
|
let mut status: u16 = 0x0000;
|
|
|
|
if self.bus.is_stalled(ep_addr) {
|
|
|
|
status |= 0x0001;
|
|
|
|
}
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control.accept_in(&status.to_le_bytes(), stage).await
|
2022-03-28 01:16:45 +00:00
|
|
|
}
|
|
|
|
_ => self.control.reject(),
|
|
|
|
},
|
2022-03-29 21:13:16 +00:00
|
|
|
(_, Recipient::Interface) => {
|
2022-03-28 01:19:07 +00:00
|
|
|
let handler = self
|
|
|
|
.interfaces
|
|
|
|
.iter_mut()
|
|
|
|
.find(|(i, _)| req.index == *i as _)
|
|
|
|
.map(|(_, h)| h);
|
2022-03-29 21:13:16 +00:00
|
|
|
|
2022-03-28 01:19:07 +00:00
|
|
|
match handler {
|
2022-03-29 21:13:16 +00:00
|
|
|
Some(handler) => {
|
|
|
|
let response = match (req.request_type, req.request) {
|
|
|
|
(RequestType::Standard, Request::GET_STATUS) => {
|
|
|
|
handler.get_status(self.control_buf)
|
|
|
|
}
|
|
|
|
(RequestType::Standard, Request::GET_INTERFACE) => {
|
|
|
|
handler.get_interface(self.control_buf)
|
|
|
|
}
|
|
|
|
_ => handler.control_in(req, self.control_buf),
|
|
|
|
};
|
|
|
|
|
|
|
|
match response {
|
2022-03-30 18:17:15 +00:00
|
|
|
InResponse::Accepted(data) => self.control.accept_in(data, stage).await,
|
2022-03-29 21:13:16 +00:00
|
|
|
InResponse::Rejected => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 01:19:07 +00:00
|
|
|
None => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 20:46:14 +00:00
|
|
|
_ => self.control.reject(),
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 18:17:15 +00:00
|
|
|
async fn handle_get_descriptor(&mut self, req: Request, stage: DataInStage) {
|
2022-03-09 00:34:35 +00:00
|
|
|
let (dtype, index) = req.descriptor_type_index();
|
|
|
|
|
|
|
|
match dtype {
|
2022-03-30 18:17:15 +00:00
|
|
|
descriptor_type::BOS => self.control.accept_in(self.bos_descriptor, stage).await,
|
|
|
|
descriptor_type::DEVICE => self.control.accept_in(self.device_descriptor, stage).await,
|
|
|
|
descriptor_type::CONFIGURATION => {
|
|
|
|
self.control.accept_in(self.config_descriptor, stage).await
|
|
|
|
}
|
2022-03-09 00:34:35 +00:00
|
|
|
descriptor_type::STRING => {
|
|
|
|
if index == 0 {
|
2022-03-30 00:26:30 +00:00
|
|
|
self.control
|
2022-03-30 18:17:15 +00:00
|
|
|
.accept_in_writer(req, stage, |w| {
|
2022-03-30 00:26:30 +00:00
|
|
|
w.write(descriptor_type::STRING, &lang_id::ENGLISH_US.to_le_bytes());
|
|
|
|
})
|
|
|
|
.await
|
2022-03-09 00:34:35 +00:00
|
|
|
} else {
|
|
|
|
let s = match index {
|
|
|
|
1 => self.config.manufacturer,
|
|
|
|
2 => self.config.product,
|
|
|
|
3 => self.config.serial_number,
|
|
|
|
_ => {
|
2022-03-29 23:18:37 +00:00
|
|
|
let _index = StringIndex::new(index);
|
|
|
|
let _lang_id = req.index;
|
|
|
|
// TODO
|
2022-03-09 00:34:35 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(s) = s {
|
2022-03-30 18:17:15 +00:00
|
|
|
self.control
|
|
|
|
.accept_in_writer(req, stage, |w| w.string(s))
|
|
|
|
.await
|
2022-03-09 00:34:35 +00:00
|
|
|
} else {
|
2022-03-25 20:46:14 +00:00
|
|
|
self.control.reject()
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 20:46:14 +00:00
|
|
|
_ => self.control.reject(),
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|