2022-03-09 01:34:35 +01:00
|
|
|
#![no_std]
|
|
|
|
#![feature(generic_associated_types)]
|
2022-03-25 21:46:14 +01:00
|
|
|
#![feature(type_alias_impl_trait)]
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
// This mod MUST go first, so that the others see its macros.
|
|
|
|
pub(crate) mod fmt;
|
|
|
|
|
|
|
|
mod builder;
|
2022-03-25 21:46:14 +01:00
|
|
|
pub mod control;
|
2022-03-09 01:34:35 +01:00
|
|
|
pub mod descriptor;
|
2022-04-16 04:47:27 +02:00
|
|
|
mod descriptor_reader;
|
2022-03-09 01:34:35 +01:00
|
|
|
pub mod driver;
|
|
|
|
pub mod types;
|
|
|
|
|
2022-04-13 19:09:08 +02:00
|
|
|
use embassy::util::{select, Either};
|
2022-03-28 02:20:01 +02:00
|
|
|
use heapless::Vec;
|
2022-03-27 23:12:57 +02:00
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
use crate::descriptor_reader::foreach_endpoint;
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
use self::control::*;
|
|
|
|
use self::descriptor::*;
|
2022-03-30 20:17:15 +02:00
|
|
|
use self::driver::{Bus, Driver, Event};
|
2022-03-09 01:34:35 +01:00
|
|
|
use self::types::*;
|
|
|
|
|
2022-04-16 02:17:24 +02:00
|
|
|
pub use self::builder::Builder;
|
2022-03-09 01:34:35 +01:00
|
|
|
pub use self::builder::Config;
|
|
|
|
|
|
|
|
/// 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)]
|
2022-04-10 21:41:51 +02:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2022-03-09 01:34:35 +01:00
|
|
|
pub enum UsbDeviceState {
|
2022-04-10 21:41:51 +02:00
|
|
|
/// The USB device is disabled.
|
|
|
|
Disabled,
|
|
|
|
|
|
|
|
/// The USB device has just been enabled or reset.
|
2022-03-09 01:34:35 +01:00
|
|
|
Default,
|
|
|
|
|
|
|
|
/// The USB device has received an address from the host.
|
|
|
|
Addressed,
|
|
|
|
|
|
|
|
/// The USB device has been configured and is fully functional.
|
|
|
|
Configured,
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:09:08 +02:00
|
|
|
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum RemoteWakeupError {
|
|
|
|
InvalidState,
|
|
|
|
Unsupported,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<driver::Unsupported> for RemoteWakeupError {
|
|
|
|
fn from(_: driver::Unsupported) -> Self {
|
|
|
|
RemoteWakeupError::Unsupported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
/// 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;
|
|
|
|
|
2022-03-28 03:19:07 +02:00
|
|
|
pub const MAX_INTERFACE_COUNT: usize = 4;
|
2022-03-28 02:20:01 +02:00
|
|
|
|
2022-04-23 04:40:57 +02:00
|
|
|
const STRING_INDEX_MANUFACTURER: u8 = 1;
|
|
|
|
const STRING_INDEX_PRODUCT: u8 = 2;
|
|
|
|
const STRING_INDEX_SERIAL_NUMBER: u8 = 3;
|
|
|
|
const STRING_INDEX_CUSTOM_START: u8 = 4;
|
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
/// A handler trait for changes in the device state of the [UsbDevice].
|
|
|
|
pub trait DeviceStateHandler {
|
2022-04-13 19:09:08 +02:00
|
|
|
/// Called when the USB device has been enabled or disabled.
|
|
|
|
fn enabled(&self, _enabled: bool) {}
|
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
/// Called when the host resets the device.
|
|
|
|
fn reset(&self) {}
|
|
|
|
|
|
|
|
/// Called when the host has set the address of the device to `addr`.
|
|
|
|
fn addressed(&self, _addr: u8) {}
|
|
|
|
|
|
|
|
/// Called when the host has enabled or disabled the configuration of the device.
|
|
|
|
fn configured(&self, _configured: bool) {}
|
|
|
|
|
|
|
|
/// Called when the bus has entered or exited the suspend state.
|
|
|
|
fn suspended(&self, _suspended: bool) {}
|
|
|
|
|
|
|
|
/// Called when remote wakeup feature is enabled or disabled.
|
|
|
|
fn remote_wakeup_enabled(&self, _enabled: bool) {}
|
|
|
|
}
|
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
struct Interface<'d> {
|
|
|
|
handler: Option<&'d mut dyn ControlHandler>,
|
|
|
|
current_alt_setting: u8,
|
|
|
|
num_alt_settings: u8,
|
2022-04-23 04:40:57 +02:00
|
|
|
num_strings: u8,
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 19:09:08 +02:00
|
|
|
pub struct UsbDevice<'d, D: Driver<'d>> {
|
2022-03-09 23:06:27 +01:00
|
|
|
bus: D::Bus,
|
2022-04-10 21:41:51 +02:00
|
|
|
handler: Option<&'d dyn DeviceStateHandler>,
|
2022-03-30 02:26:30 +02:00
|
|
|
control: ControlPipe<D::ControlPipe>,
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
config: Config<'d>,
|
|
|
|
device_descriptor: &'d [u8],
|
|
|
|
config_descriptor: &'d [u8],
|
|
|
|
bos_descriptor: &'d [u8],
|
2022-03-29 21:09:24 +02:00
|
|
|
control_buf: &'d mut [u8],
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
device_state: UsbDeviceState,
|
2022-04-10 21:41:51 +02:00
|
|
|
suspended: bool,
|
2022-03-09 01:34:35 +01:00
|
|
|
remote_wakeup_enabled: bool,
|
|
|
|
self_powered: bool,
|
|
|
|
pending_address: u8,
|
2022-03-25 21:46:14 +01:00
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
interfaces: Vec<Interface<'d>, MAX_INTERFACE_COUNT>,
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 19:09:08 +02:00
|
|
|
impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
2022-04-10 21:41:51 +02:00
|
|
|
pub(crate) fn build(
|
2022-03-09 01:34:35 +01:00
|
|
|
mut driver: D,
|
|
|
|
config: Config<'d>,
|
2022-04-10 21:41:51 +02:00
|
|
|
handler: Option<&'d dyn DeviceStateHandler>,
|
2022-03-09 01:34:35 +01:00
|
|
|
device_descriptor: &'d [u8],
|
|
|
|
config_descriptor: &'d [u8],
|
|
|
|
bos_descriptor: &'d [u8],
|
2022-04-16 04:47:27 +02:00
|
|
|
interfaces: Vec<Interface<'d>, MAX_INTERFACE_COUNT>,
|
2022-03-29 21:09:24 +02:00
|
|
|
control_buf: &'d mut [u8],
|
2022-04-13 19:09:08 +02:00
|
|
|
) -> UsbDevice<'d, D> {
|
2022-03-25 21:46:14 +01:00
|
|
|
let control = driver
|
|
|
|
.alloc_control_pipe(config.max_packet_size_0 as u16)
|
2022-03-09 01:34:35 +01:00
|
|
|
.expect("failed to alloc control endpoint");
|
|
|
|
|
|
|
|
// Enable the USB bus.
|
|
|
|
// This prevent further allocation by consuming the driver.
|
2022-04-10 21:41:51 +02:00
|
|
|
let bus = driver.into_bus();
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
Self {
|
2022-04-07 04:10:18 +02:00
|
|
|
bus,
|
2022-03-09 01:34:35 +01:00
|
|
|
config,
|
2022-04-10 21:41:51 +02:00
|
|
|
handler,
|
2022-03-30 20:17:15 +02:00
|
|
|
control: ControlPipe::new(control),
|
2022-03-09 01:34:35 +01:00
|
|
|
device_descriptor,
|
|
|
|
config_descriptor,
|
|
|
|
bos_descriptor,
|
2022-03-29 21:09:24 +02:00
|
|
|
control_buf,
|
2022-04-13 19:09:08 +02:00
|
|
|
device_state: UsbDeviceState::Disabled,
|
2022-04-10 21:41:51 +02:00
|
|
|
suspended: false,
|
2022-03-09 01:34:35 +01:00
|
|
|
remote_wakeup_enabled: false,
|
|
|
|
self_powered: false,
|
|
|
|
pending_address: 0,
|
2022-03-28 03:19:07 +02:00
|
|
|
interfaces,
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 22:04:31 +02:00
|
|
|
/// Runs the `UsbDevice` forever.
|
|
|
|
///
|
|
|
|
/// This future may leave the bus in an invalid state if it is dropped.
|
|
|
|
/// After dropping the future, [`UsbDevice::disable()`] should be called
|
|
|
|
/// before calling any other `UsbDevice` methods to fully reset the
|
|
|
|
/// peripheral.
|
2022-04-10 21:41:51 +02:00
|
|
|
pub async fn run(&mut self) -> ! {
|
2022-04-13 22:04:31 +02:00
|
|
|
loop {
|
|
|
|
self.run_until_suspend().await;
|
|
|
|
self.wait_resume().await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the `UsbDevice` until the bus is suspended.
|
|
|
|
///
|
|
|
|
/// This future may leave the bus in an invalid state if it is dropped.
|
|
|
|
/// After dropping the future, [`UsbDevice::disable()`] should be called
|
|
|
|
/// before calling any other `UsbDevice` methods to fully reset the
|
|
|
|
/// peripheral.
|
|
|
|
pub async fn run_until_suspend(&mut self) -> () {
|
2022-04-13 19:09:08 +02:00
|
|
|
if self.device_state == UsbDeviceState::Disabled {
|
2022-04-10 21:41:51 +02:00
|
|
|
self.bus.enable().await;
|
2022-04-13 19:09:08 +02:00
|
|
|
self.device_state = UsbDeviceState::Default;
|
|
|
|
|
|
|
|
if let Some(h) = &self.handler {
|
|
|
|
h.enabled(true);
|
|
|
|
}
|
2022-04-10 21:41:51 +02:00
|
|
|
}
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
loop {
|
2022-03-25 21:46:14 +01:00
|
|
|
let control_fut = self.control.setup();
|
2022-03-09 23:06:27 +01:00
|
|
|
let bus_fut = self.bus.poll();
|
2022-04-13 19:09:08 +02:00
|
|
|
match select(bus_fut, control_fut).await {
|
2022-04-13 22:04:31 +02:00
|
|
|
Either::First(evt) => {
|
|
|
|
self.handle_bus_event(evt);
|
|
|
|
if self.suspended {
|
|
|
|
return;
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
2022-04-13 19:09:08 +02:00
|
|
|
}
|
2022-04-13 22:04:31 +02:00
|
|
|
Either::Second(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-04-10 21:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 22:04:31 +02:00
|
|
|
/// Disables the USB peripheral.
|
2022-04-13 19:09:08 +02:00
|
|
|
pub async fn disable(&mut self) {
|
|
|
|
if self.device_state != UsbDeviceState::Disabled {
|
|
|
|
self.bus.disable().await;
|
|
|
|
self.device_state = UsbDeviceState::Disabled;
|
|
|
|
self.suspended = false;
|
|
|
|
self.remote_wakeup_enabled = false;
|
|
|
|
|
|
|
|
if let Some(h) = &self.handler {
|
|
|
|
h.enabled(false);
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 19:09:08 +02:00
|
|
|
}
|
2022-04-10 21:41:51 +02:00
|
|
|
|
2022-04-13 22:04:31 +02:00
|
|
|
/// Waits for a resume condition on the USB bus.
|
|
|
|
///
|
|
|
|
/// This future is cancel-safe.
|
|
|
|
pub async fn wait_resume(&mut self) {
|
|
|
|
while self.suspended {
|
|
|
|
let evt = self.bus.poll().await;
|
|
|
|
self.handle_bus_event(evt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initiates a device remote wakeup on the USB bus.
|
|
|
|
///
|
|
|
|
/// If the bus is not suspended or remote wakeup is not enabled, an error
|
|
|
|
/// will be returned.
|
|
|
|
///
|
|
|
|
/// This future may leave the bus in an inconsistent state if dropped.
|
|
|
|
/// After dropping the future, [`UsbDevice::disable()`] should be called
|
|
|
|
/// before calling any other `UsbDevice` methods to fully reset the peripheral.
|
2022-04-13 19:09:08 +02:00
|
|
|
pub async fn remote_wakeup(&mut self) -> Result<(), RemoteWakeupError> {
|
2022-04-13 22:04:31 +02:00
|
|
|
if self.suspended && self.remote_wakeup_enabled {
|
2022-04-13 19:09:08 +02:00
|
|
|
self.bus.remote_wakeup().await?;
|
|
|
|
self.suspended = false;
|
|
|
|
|
|
|
|
if let Some(h) = &self.handler {
|
|
|
|
h.suspended(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(RemoteWakeupError::InvalidState)
|
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 22:04:31 +02:00
|
|
|
fn handle_bus_event(&mut self, evt: Event) {
|
|
|
|
match evt {
|
|
|
|
Event::Reset => {
|
|
|
|
trace!("usb: reset");
|
|
|
|
self.device_state = UsbDeviceState::Default;
|
|
|
|
self.suspended = false;
|
|
|
|
self.remote_wakeup_enabled = false;
|
|
|
|
self.pending_address = 0;
|
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
for iface in self.interfaces.iter_mut() {
|
|
|
|
iface.current_alt_setting = 0;
|
|
|
|
if let Some(h) = &mut iface.handler {
|
|
|
|
h.reset();
|
|
|
|
h.set_alternate_setting(0);
|
|
|
|
}
|
2022-04-13 22:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(h) = &self.handler {
|
|
|
|
h.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Resume => {
|
|
|
|
trace!("usb: resume");
|
|
|
|
self.suspended = false;
|
|
|
|
if let Some(h) = &self.handler {
|
|
|
|
h.suspended(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Suspend => {
|
|
|
|
trace!("usb: suspend");
|
|
|
|
self.suspended = true;
|
|
|
|
if let Some(h) = &self.handler {
|
|
|
|
h.suspended(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:17:15 +02:00
|
|
|
async fn handle_control_out(&mut self, req: Request, stage: DataOutStage) {
|
2022-03-09 01:34:35 +01:00
|
|
|
const CONFIGURATION_NONE_U16: u16 = CONFIGURATION_NONE as u16;
|
|
|
|
const CONFIGURATION_VALUE_U16: u16 = CONFIGURATION_VALUE as u16;
|
2022-03-29 23:13:16 +02:00
|
|
|
|
2022-03-30 20:17:15 +02: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 02:26:30 +02:00
|
|
|
}
|
2022-03-29 23:13:16 +02:00
|
|
|
};
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-03-28 03:16:45 +02: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 01:34:35 +01:00
|
|
|
self.remote_wakeup_enabled = false;
|
2022-04-11 18:00:05 +02:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 21:41:51 +02:00
|
|
|
h.remote_wakeup_enabled(false);
|
|
|
|
}
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept(stage)
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(Request::SET_FEATURE, Request::FEATURE_DEVICE_REMOTE_WAKEUP) => {
|
2022-03-09 01:34:35 +01:00
|
|
|
self.remote_wakeup_enabled = true;
|
2022-04-11 18:00:05 +02:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 21:41:51 +02:00
|
|
|
h.remote_wakeup_enabled(true);
|
|
|
|
}
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept(stage)
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-04-02 22:35:03 +02:00
|
|
|
(Request::SET_ADDRESS, addr @ 1..=127) => {
|
|
|
|
self.pending_address = addr as u8;
|
2022-04-16 04:47:27 +02:00
|
|
|
self.bus.set_address(self.pending_address);
|
2022-04-10 21:41:51 +02:00
|
|
|
self.device_state = UsbDeviceState::Addressed;
|
2022-04-11 18:00:05 +02:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 21:41:51 +02:00
|
|
|
h.addressed(self.pending_address);
|
|
|
|
}
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept(stage)
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => {
|
2022-04-16 04:47:27 +02:00
|
|
|
debug!("SET_CONFIGURATION: configured");
|
2022-03-09 01:34:35 +01:00
|
|
|
self.device_state = UsbDeviceState::Configured;
|
2022-04-16 04:47:27 +02:00
|
|
|
|
|
|
|
// Enable all endpoints of selected alt settings.
|
|
|
|
foreach_endpoint(self.config_descriptor, |ep| {
|
|
|
|
let iface = &self.interfaces[ep.interface as usize];
|
|
|
|
self.bus.endpoint_set_enabled(
|
|
|
|
ep.ep_address,
|
|
|
|
iface.current_alt_setting == ep.interface_alt,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Notify handler.
|
2022-04-11 18:00:05 +02:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 21:41:51 +02:00
|
|
|
h.configured(true);
|
|
|
|
}
|
2022-04-16 04:47:27 +02:00
|
|
|
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept(stage)
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => match self.device_state {
|
2022-03-30 20:17:15 +02:00
|
|
|
UsbDeviceState::Default => self.control.accept(stage),
|
2022-03-28 03:16:45 +02:00
|
|
|
_ => {
|
2022-04-16 04:47:27 +02:00
|
|
|
debug!("SET_CONFIGURATION: unconfigured");
|
2022-03-28 03:16:45 +02:00
|
|
|
self.device_state = UsbDeviceState::Addressed;
|
2022-04-16 04:47:27 +02:00
|
|
|
|
|
|
|
// Disable all endpoints.
|
|
|
|
foreach_endpoint(self.config_descriptor, |ep| {
|
|
|
|
self.bus.endpoint_set_enabled(ep.ep_address, false);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Notify handler.
|
2022-04-11 18:00:05 +02:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 21:41:51 +02:00
|
|
|
h.configured(false);
|
|
|
|
}
|
2022-04-16 04:47:27 +02:00
|
|
|
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept(stage)
|
2022-03-28 03:16:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => self.control.reject(),
|
|
|
|
},
|
2022-04-16 04:47:27 +02:00
|
|
|
(RequestType::Standard, Recipient::Interface) => {
|
|
|
|
let iface = match self.interfaces.get_mut(req.index as usize) {
|
|
|
|
Some(iface) => iface,
|
|
|
|
None => return self.control.reject(),
|
|
|
|
};
|
|
|
|
|
|
|
|
match req.request {
|
|
|
|
Request::SET_INTERFACE => {
|
|
|
|
let new_altsetting = req.value as u8;
|
|
|
|
|
|
|
|
if new_altsetting >= iface.num_alt_settings {
|
|
|
|
warn!("SET_INTERFACE: trying to select alt setting out of range.");
|
|
|
|
return self.control.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
iface.current_alt_setting = new_altsetting;
|
|
|
|
|
|
|
|
// Enable/disable EPs of this interface as needed.
|
|
|
|
foreach_endpoint(self.config_descriptor, |ep| {
|
|
|
|
if ep.interface == req.index as u8 {
|
|
|
|
self.bus.endpoint_set_enabled(
|
|
|
|
ep.ep_address,
|
|
|
|
iface.current_alt_setting == ep.interface_alt,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// TODO check it is valid (not out of range)
|
|
|
|
// TODO actually enable/disable endpoints.
|
|
|
|
|
|
|
|
if let Some(handler) = &mut iface.handler {
|
|
|
|
handler.set_alternate_setting(new_altsetting);
|
|
|
|
}
|
|
|
|
self.control.accept(stage)
|
|
|
|
}
|
|
|
|
_ => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(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();
|
2022-04-16 04:47:27 +02:00
|
|
|
self.bus.endpoint_set_stalled(ep_addr, true);
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept(stage)
|
2022-03-28 03:16:45 +02:00
|
|
|
}
|
|
|
|
(Request::CLEAR_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
|
|
|
|
let ep_addr = ((req.index as u8) & 0x8f).into();
|
2022-04-16 04:47:27 +02:00
|
|
|
self.bus.endpoint_set_stalled(ep_addr, false);
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept(stage)
|
2022-03-28 03:16:45 +02:00
|
|
|
}
|
2022-03-25 21:46:14 +01:00
|
|
|
_ => self.control.reject(),
|
2022-03-09 01:34:35 +01:00
|
|
|
},
|
2022-04-16 04:47:27 +02:00
|
|
|
(RequestType::Class, Recipient::Interface) => {
|
|
|
|
let iface = match self.interfaces.get_mut(req.index as usize) {
|
|
|
|
Some(iface) => iface,
|
|
|
|
None => return self.control.reject(),
|
|
|
|
};
|
|
|
|
match &mut iface.handler {
|
|
|
|
Some(handler) => match handler.control_out(req, data) {
|
|
|
|
OutResponse::Accepted => self.control.accept(stage),
|
|
|
|
OutResponse::Rejected => self.control.reject(),
|
|
|
|
},
|
2022-03-28 03:19:07 +02:00
|
|
|
None => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 21:46:14 +01:00
|
|
|
_ => self.control.reject(),
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 03:02:13 +02:00
|
|
|
async fn handle_control_in(&mut self, req: Request, mut stage: DataInStage) {
|
|
|
|
// If we don't have an address yet, respond with max 1 packet.
|
|
|
|
// The host doesn't know our EP0 max packet size yet, and might assume
|
|
|
|
// a full-length packet is a short packet, thinking we're done sending data.
|
|
|
|
// See https://github.com/hathach/tinyusb/issues/184
|
|
|
|
const DEVICE_DESCRIPTOR_LEN: u8 = 18;
|
|
|
|
if self.pending_address == 0
|
|
|
|
&& self.config.max_packet_size_0 < DEVICE_DESCRIPTOR_LEN
|
|
|
|
&& (self.config.max_packet_size_0 as usize) < stage.length
|
|
|
|
{
|
|
|
|
trace!("received control req while not addressed: capping response to 1 packet.");
|
|
|
|
stage.length = self.config.max_packet_size_0 as _;
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:16:45 +02:00
|
|
|
match (req.request_type, req.recipient) {
|
|
|
|
(RequestType::Standard, Recipient::Device) => match req.request {
|
|
|
|
Request::GET_STATUS => {
|
2022-03-09 01:34:35 +01:00
|
|
|
let mut status: u16 = 0x0000;
|
|
|
|
if self.self_powered {
|
|
|
|
status |= 0x0001;
|
|
|
|
}
|
|
|
|
if self.remote_wakeup_enabled {
|
|
|
|
status |= 0x0002;
|
|
|
|
}
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept_in(&status.to_le_bytes(), stage).await
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-30 20:17:15 +02:00
|
|
|
Request::GET_DESCRIPTOR => self.handle_get_descriptor(req, stage).await,
|
2022-03-28 03:16:45 +02:00
|
|
|
Request::GET_CONFIGURATION => {
|
2022-03-09 01:34:35 +01:00
|
|
|
let status = match self.device_state {
|
|
|
|
UsbDeviceState::Configured => CONFIGURATION_VALUE,
|
|
|
|
_ => CONFIGURATION_NONE,
|
|
|
|
};
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept_in(&status.to_le_bytes(), stage).await
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
_ => self.control.reject(),
|
|
|
|
},
|
2022-04-16 04:47:27 +02:00
|
|
|
(RequestType::Standard, Recipient::Interface) => {
|
|
|
|
let iface = match self.interfaces.get_mut(req.index as usize) {
|
|
|
|
Some(iface) => iface,
|
|
|
|
None => return self.control.reject(),
|
|
|
|
};
|
|
|
|
|
|
|
|
match req.request {
|
|
|
|
Request::GET_STATUS => {
|
|
|
|
let status: u16 = 0;
|
|
|
|
self.control.accept_in(&status.to_le_bytes(), stage).await
|
|
|
|
}
|
|
|
|
Request::GET_INTERFACE => {
|
|
|
|
self.control
|
|
|
|
.accept_in(&[iface.current_alt_setting], stage)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
Request::GET_DESCRIPTOR => match &mut iface.handler {
|
|
|
|
Some(handler) => match handler.get_descriptor(req, self.control_buf) {
|
|
|
|
InResponse::Accepted(data) => self.control.accept_in(data, stage).await,
|
|
|
|
InResponse::Rejected => self.control.reject(),
|
|
|
|
},
|
|
|
|
None => self.control.reject(),
|
|
|
|
},
|
|
|
|
_ => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(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;
|
2022-04-16 04:47:27 +02:00
|
|
|
if self.bus.endpoint_is_stalled(ep_addr) {
|
2022-03-28 03:16:45 +02:00
|
|
|
status |= 0x0001;
|
|
|
|
}
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control.accept_in(&status.to_le_bytes(), stage).await
|
2022-03-28 03:16:45 +02:00
|
|
|
}
|
|
|
|
_ => self.control.reject(),
|
|
|
|
},
|
2022-04-16 04:47:27 +02:00
|
|
|
(RequestType::Class, Recipient::Interface) => {
|
|
|
|
let iface = match self.interfaces.get_mut(req.index as usize) {
|
|
|
|
Some(iface) => iface,
|
|
|
|
None => return self.control.reject(),
|
|
|
|
};
|
|
|
|
|
|
|
|
match &mut iface.handler {
|
|
|
|
Some(handler) => match handler.control_in(req, self.control_buf) {
|
|
|
|
InResponse::Accepted(data) => self.control.accept_in(data, stage).await,
|
|
|
|
InResponse::Rejected => self.control.reject(),
|
|
|
|
},
|
2022-03-28 03:19:07 +02:00
|
|
|
None => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 21:46:14 +01:00
|
|
|
_ => self.control.reject(),
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:17:15 +02:00
|
|
|
async fn handle_get_descriptor(&mut self, req: Request, stage: DataInStage) {
|
2022-03-09 01:34:35 +01:00
|
|
|
let (dtype, index) = req.descriptor_type_index();
|
|
|
|
|
|
|
|
match dtype {
|
2022-03-30 20:17:15 +02: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 01:34:35 +01:00
|
|
|
descriptor_type::STRING => {
|
|
|
|
if index == 0 {
|
2022-03-30 02:26:30 +02:00
|
|
|
self.control
|
2022-03-30 20:17:15 +02:00
|
|
|
.accept_in_writer(req, stage, |w| {
|
2022-03-30 02:26:30 +02:00
|
|
|
w.write(descriptor_type::STRING, &lang_id::ENGLISH_US.to_le_bytes());
|
|
|
|
})
|
|
|
|
.await
|
2022-03-09 01:34:35 +01:00
|
|
|
} else {
|
|
|
|
let s = match index {
|
2022-04-23 04:40:57 +02:00
|
|
|
STRING_INDEX_MANUFACTURER => self.config.manufacturer,
|
|
|
|
STRING_INDEX_PRODUCT => self.config.product,
|
|
|
|
STRING_INDEX_SERIAL_NUMBER => self.config.serial_number,
|
2022-03-09 01:34:35 +01:00
|
|
|
_ => {
|
2022-04-23 04:40:57 +02:00
|
|
|
// Find out which iface owns this string index.
|
|
|
|
let mut index_left = index - STRING_INDEX_CUSTOM_START;
|
|
|
|
let mut the_iface = None;
|
|
|
|
for iface in &mut self.interfaces {
|
|
|
|
if index_left < iface.num_strings {
|
|
|
|
the_iface = Some(iface);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
index_left -= iface.num_strings;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(iface) = the_iface {
|
|
|
|
if let Some(handler) = &mut iface.handler {
|
|
|
|
let index = StringIndex::new(index);
|
|
|
|
let lang_id = req.index;
|
|
|
|
handler.get_string(index, lang_id, self.control_buf)
|
|
|
|
} else {
|
|
|
|
warn!("String requested to an interface with no handler.");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!("String requested but didn't match to an interface.");
|
|
|
|
None
|
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(s) = s {
|
2022-03-30 20:17:15 +02:00
|
|
|
self.control
|
|
|
|
.accept_in_writer(req, stage, |w| w.string(s))
|
|
|
|
.await
|
2022-03-09 01:34:35 +01:00
|
|
|
} else {
|
2022-03-25 21:46:14 +01:00
|
|
|
self.control.reject()
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 21:46:14 +01:00
|
|
|
_ => self.control.reject(),
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|