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;
|
2022-04-16 02:47:27 +00:00
|
|
|
mod descriptor_reader;
|
2022-03-09 00:34:35 +00:00
|
|
|
pub mod driver;
|
|
|
|
pub mod types;
|
|
|
|
|
2022-04-13 17:09:08 +00:00
|
|
|
use embassy::util::{select, Either};
|
2022-03-28 00:20:01 +00:00
|
|
|
use heapless::Vec;
|
2022-03-27 21:12:57 +00:00
|
|
|
|
2022-04-16 02:47:27 +00:00
|
|
|
use crate::descriptor_reader::foreach_endpoint;
|
2022-05-09 01:25:21 +00:00
|
|
|
use crate::driver::ControlPipe;
|
2022-04-16 02:47:27 +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-04-16 00:17:24 +00:00
|
|
|
pub use self::builder::Builder;
|
2022-03-09 00:34:35 +00: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 19:41:51 +00:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2022-03-09 00:34:35 +00:00
|
|
|
pub enum UsbDeviceState {
|
2022-04-10 19:41:51 +00:00
|
|
|
/// The USB device is disabled.
|
|
|
|
Disabled,
|
|
|
|
|
|
|
|
/// The USB device has just been enabled or reset.
|
2022-03-09 00:34:35 +00: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 17:09:08 +00: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 00:34:35 +00: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 01:19:07 +00:00
|
|
|
pub const MAX_INTERFACE_COUNT: usize = 4;
|
2022-03-28 00:20:01 +00:00
|
|
|
|
2022-04-23 02:40:57 +00: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 19:41:51 +00:00
|
|
|
/// A handler trait for changes in the device state of the [UsbDevice].
|
|
|
|
pub trait DeviceStateHandler {
|
2022-04-13 17:09:08 +00:00
|
|
|
/// Called when the USB device has been enabled or disabled.
|
|
|
|
fn enabled(&self, _enabled: bool) {}
|
|
|
|
|
2022-04-10 19:41:51 +00: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 02:47:27 +00:00
|
|
|
struct Interface<'d> {
|
|
|
|
handler: Option<&'d mut dyn ControlHandler>,
|
|
|
|
current_alt_setting: u8,
|
|
|
|
num_alt_settings: u8,
|
2022-04-23 02:40:57 +00:00
|
|
|
num_strings: u8,
|
2022-04-16 02:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 17:09:08 +00:00
|
|
|
pub struct UsbDevice<'d, D: Driver<'d>> {
|
2022-05-09 00:11:02 +00:00
|
|
|
control_buf: &'d mut [u8],
|
2022-05-09 01:25:21 +00:00
|
|
|
control: D::ControlPipe,
|
2022-05-09 00:11:02 +00:00
|
|
|
inner: Inner<'d, D>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Inner<'d, D: Driver<'d>> {
|
2022-03-09 22:06:27 +00:00
|
|
|
bus: D::Bus,
|
2022-04-10 19:41:51 +00:00
|
|
|
handler: Option<&'d dyn DeviceStateHandler>,
|
2022-03-09 00:34:35 +00:00
|
|
|
|
|
|
|
config: Config<'d>,
|
|
|
|
device_descriptor: &'d [u8],
|
|
|
|
config_descriptor: &'d [u8],
|
|
|
|
bos_descriptor: &'d [u8],
|
|
|
|
|
|
|
|
device_state: UsbDeviceState,
|
2022-04-10 19:41:51 +00:00
|
|
|
suspended: bool,
|
2022-03-09 00:34:35 +00:00
|
|
|
remote_wakeup_enabled: bool,
|
|
|
|
self_powered: bool,
|
|
|
|
pending_address: u8,
|
2022-03-25 20:46:14 +00:00
|
|
|
|
2022-04-16 02:47:27 +00:00
|
|
|
interfaces: Vec<Interface<'d>, MAX_INTERFACE_COUNT>,
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 17:09:08 +00:00
|
|
|
impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
2022-04-10 19:41:51 +00:00
|
|
|
pub(crate) fn build(
|
2022-05-10 14:53:42 +00:00
|
|
|
driver: D,
|
2022-03-09 00:34:35 +00:00
|
|
|
config: Config<'d>,
|
2022-04-10 19:41:51 +00:00
|
|
|
handler: Option<&'d dyn DeviceStateHandler>,
|
2022-03-09 00:34:35 +00:00
|
|
|
device_descriptor: &'d [u8],
|
|
|
|
config_descriptor: &'d [u8],
|
|
|
|
bos_descriptor: &'d [u8],
|
2022-04-16 02:47:27 +00:00
|
|
|
interfaces: Vec<Interface<'d>, MAX_INTERFACE_COUNT>,
|
2022-03-29 19:09:24 +00:00
|
|
|
control_buf: &'d mut [u8],
|
2022-04-13 17:09:08 +00:00
|
|
|
) -> UsbDevice<'d, D> {
|
2022-05-10 14:53:42 +00:00
|
|
|
// Start the USB bus.
|
2022-03-09 00:34:35 +00:00
|
|
|
// This prevent further allocation by consuming the driver.
|
2022-05-10 14:53:42 +00:00
|
|
|
let (bus, control) = driver.start(config.max_packet_size_0 as u16);
|
2022-03-09 00:34:35 +00:00
|
|
|
|
|
|
|
Self {
|
2022-03-29 19:09:24 +00:00
|
|
|
control_buf,
|
2022-05-09 01:25:21 +00:00
|
|
|
control,
|
2022-05-09 00:11:02 +00:00
|
|
|
inner: Inner {
|
|
|
|
bus,
|
|
|
|
config,
|
|
|
|
handler,
|
|
|
|
device_descriptor,
|
|
|
|
config_descriptor,
|
|
|
|
bos_descriptor,
|
|
|
|
|
|
|
|
device_state: UsbDeviceState::Disabled,
|
|
|
|
suspended: false,
|
|
|
|
remote_wakeup_enabled: false,
|
|
|
|
self_powered: false,
|
|
|
|
pending_address: 0,
|
|
|
|
interfaces,
|
|
|
|
},
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 20:04:31 +00: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 19:41:51 +00:00
|
|
|
pub async fn run(&mut self) -> ! {
|
2022-04-13 20:04:31 +00: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-05-09 00:11:02 +00:00
|
|
|
if self.inner.device_state == UsbDeviceState::Disabled {
|
|
|
|
self.inner.bus.enable().await;
|
|
|
|
self.inner.device_state = UsbDeviceState::Default;
|
2022-04-13 17:09:08 +00:00
|
|
|
|
2022-05-09 00:11:02 +00:00
|
|
|
if let Some(h) = &self.inner.handler {
|
2022-04-13 17:09:08 +00:00
|
|
|
h.enabled(true);
|
|
|
|
}
|
2022-04-10 19:41:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 01:25:21 +00:00
|
|
|
while !self.inner.suspended {
|
2022-03-25 20:46:14 +00:00
|
|
|
let control_fut = self.control.setup();
|
2022-05-09 00:11:02 +00:00
|
|
|
let bus_fut = self.inner.bus.poll();
|
2022-04-13 17:09:08 +00:00
|
|
|
match select(bus_fut, control_fut).await {
|
2022-05-09 01:25:21 +00:00
|
|
|
Either::First(evt) => self.inner.handle_bus_event(evt),
|
|
|
|
Either::Second(req) => self.handle_control(req).await,
|
2022-04-10 19:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 20:04:31 +00:00
|
|
|
/// Disables the USB peripheral.
|
2022-04-13 17:09:08 +00:00
|
|
|
pub async fn disable(&mut self) {
|
2022-05-09 00:11:02 +00:00
|
|
|
if self.inner.device_state != UsbDeviceState::Disabled {
|
|
|
|
self.inner.bus.disable().await;
|
|
|
|
self.inner.device_state = UsbDeviceState::Disabled;
|
|
|
|
self.inner.suspended = false;
|
|
|
|
self.inner.remote_wakeup_enabled = false;
|
2022-04-13 17:09:08 +00:00
|
|
|
|
2022-05-09 00:11:02 +00:00
|
|
|
if let Some(h) = &self.inner.handler {
|
2022-04-13 17:09:08 +00:00
|
|
|
h.enabled(false);
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 17:09:08 +00:00
|
|
|
}
|
2022-04-10 19:41:51 +00:00
|
|
|
|
2022-04-13 20:04:31 +00:00
|
|
|
/// Waits for a resume condition on the USB bus.
|
|
|
|
///
|
|
|
|
/// This future is cancel-safe.
|
|
|
|
pub async fn wait_resume(&mut self) {
|
2022-05-09 00:11:02 +00:00
|
|
|
while self.inner.suspended {
|
|
|
|
let evt = self.inner.bus.poll().await;
|
|
|
|
self.inner.handle_bus_event(evt);
|
2022-04-13 20:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 17:09:08 +00:00
|
|
|
pub async fn remote_wakeup(&mut self) -> Result<(), RemoteWakeupError> {
|
2022-05-09 00:11:02 +00:00
|
|
|
if self.inner.suspended && self.inner.remote_wakeup_enabled {
|
|
|
|
self.inner.bus.remote_wakeup().await?;
|
|
|
|
self.inner.suspended = false;
|
2022-04-13 17:09:08 +00:00
|
|
|
|
2022-05-09 00:11:02 +00:00
|
|
|
if let Some(h) = &self.inner.handler {
|
2022-04-13 17:09:08 +00:00
|
|
|
h.suspended(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(RemoteWakeupError::InvalidState)
|
|
|
|
}
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-05-09 01:25:21 +00:00
|
|
|
|
2022-05-12 16:14:48 +00:00
|
|
|
async fn handle_control(&mut self, req: [u8; 8]) {
|
|
|
|
let req = Request::parse(&req);
|
|
|
|
|
2022-05-09 01:25:21 +00:00
|
|
|
trace!("control request: {:02x}", req);
|
|
|
|
|
|
|
|
match req.direction {
|
|
|
|
UsbDirection::In => self.handle_control_in(req).await,
|
|
|
|
UsbDirection::Out => self.handle_control_out(req).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_control_in(&mut self, req: Request) {
|
|
|
|
let mut resp_length = req.length as usize;
|
|
|
|
let max_packet_size = self.control.max_packet_size();
|
|
|
|
|
|
|
|
// 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: usize = 18;
|
|
|
|
if self.inner.pending_address == 0
|
|
|
|
&& max_packet_size < DEVICE_DESCRIPTOR_LEN
|
|
|
|
&& (max_packet_size as usize) < resp_length
|
|
|
|
{
|
|
|
|
trace!("received control req while not addressed: capping response to 1 packet.");
|
|
|
|
resp_length = max_packet_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.inner.handle_control_in(req, &mut self.control_buf) {
|
|
|
|
InResponse::Accepted(data) => {
|
|
|
|
let len = data.len().min(resp_length);
|
|
|
|
let need_zlp = len != resp_length && (len % usize::from(max_packet_size)) == 0;
|
|
|
|
|
|
|
|
let mut chunks = data[0..len]
|
|
|
|
.chunks(max_packet_size)
|
|
|
|
.chain(need_zlp.then(|| -> &[u8] { &[] }));
|
|
|
|
|
|
|
|
while let Some(chunk) = chunks.next() {
|
|
|
|
match self.control.data_in(chunk, chunks.size_hint().0 == 0).await {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => {
|
|
|
|
warn!("control accept_in failed: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InResponse::Rejected => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_control_out(&mut self, req: Request) {
|
|
|
|
let req_length = req.length as usize;
|
|
|
|
let max_packet_size = self.control.max_packet_size();
|
|
|
|
let mut total = 0;
|
|
|
|
|
|
|
|
for chunk in self.control_buf[..req_length].chunks_mut(max_packet_size) {
|
|
|
|
let size = match self.control.data_out(chunk).await {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("usb: failed to read CONTROL OUT data stage: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
total += size;
|
|
|
|
if size < max_packet_size || total == req_length {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = &self.control_buf[0..total];
|
|
|
|
#[cfg(feature = "defmt")]
|
|
|
|
trace!(" control out data: {:02x}", data);
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
|
|
|
trace!(" control out data: {:02x?}", data);
|
|
|
|
|
|
|
|
match self.inner.handle_control_out(req, data) {
|
|
|
|
OutResponse::Accepted => self.control.accept(),
|
|
|
|
OutResponse::Rejected => self.control.reject(),
|
|
|
|
}
|
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
}
|
2022-03-09 00:34:35 +00:00
|
|
|
|
2022-05-09 00:11:02 +00:00
|
|
|
impl<'d, D: Driver<'d>> Inner<'d, D> {
|
2022-04-13 20:04:31 +00: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 02:47:27 +00: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 20:04:31 +00: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-05-09 00:11:02 +00:00
|
|
|
fn handle_control_out(&mut self, req: Request, data: &[u8]) -> OutResponse {
|
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-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-04-11 16:00:05 +00:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 19:41:51 +00:00
|
|
|
h.remote_wakeup_enabled(false);
|
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
OutResponse::Accepted
|
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-04-11 16:00:05 +00:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 19:41:51 +00:00
|
|
|
h.remote_wakeup_enabled(true);
|
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
OutResponse::Accepted
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-04-02 20:35:03 +00:00
|
|
|
(Request::SET_ADDRESS, addr @ 1..=127) => {
|
|
|
|
self.pending_address = addr as u8;
|
2022-04-16 02:47:27 +00:00
|
|
|
self.bus.set_address(self.pending_address);
|
2022-04-10 19:41:51 +00:00
|
|
|
self.device_state = UsbDeviceState::Addressed;
|
2022-04-11 16:00:05 +00:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 19:41:51 +00:00
|
|
|
h.addressed(self.pending_address);
|
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
OutResponse::Accepted
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-03-28 01:16:45 +00:00
|
|
|
(Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => {
|
2022-04-16 02:47:27 +00:00
|
|
|
debug!("SET_CONFIGURATION: configured");
|
2022-03-09 00:34:35 +00:00
|
|
|
self.device_state = UsbDeviceState::Configured;
|
2022-04-16 02:47:27 +00: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 16:00:05 +00:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 19:41:51 +00:00
|
|
|
h.configured(true);
|
|
|
|
}
|
2022-04-16 02:47:27 +00:00
|
|
|
|
2022-05-09 00:11:02 +00:00
|
|
|
OutResponse::Accepted
|
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-05-09 00:11:02 +00:00
|
|
|
UsbDeviceState::Default => OutResponse::Accepted,
|
2022-03-28 01:16:45 +00:00
|
|
|
_ => {
|
2022-04-16 02:47:27 +00:00
|
|
|
debug!("SET_CONFIGURATION: unconfigured");
|
2022-03-28 01:16:45 +00:00
|
|
|
self.device_state = UsbDeviceState::Addressed;
|
2022-04-16 02:47:27 +00: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 16:00:05 +00:00
|
|
|
if let Some(h) = &self.handler {
|
2022-04-10 19:41:51 +00:00
|
|
|
h.configured(false);
|
|
|
|
}
|
2022-04-16 02:47:27 +00:00
|
|
|
|
2022-05-09 00:11:02 +00:00
|
|
|
OutResponse::Accepted
|
2022-03-28 01:16:45 +00:00
|
|
|
}
|
|
|
|
},
|
2022-05-09 00:11:02 +00:00
|
|
|
_ => OutResponse::Rejected,
|
2022-03-28 01:16:45 +00:00
|
|
|
},
|
2022-04-16 02:47:27 +00:00
|
|
|
(RequestType::Standard, Recipient::Interface) => {
|
|
|
|
let iface = match self.interfaces.get_mut(req.index as usize) {
|
|
|
|
Some(iface) => iface,
|
2022-05-09 00:11:02 +00:00
|
|
|
None => return OutResponse::Rejected,
|
2022-04-16 02:47:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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.");
|
2022-05-09 00:11:02 +00:00
|
|
|
return OutResponse::Rejected;
|
2022-04-16 02:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
OutResponse::Accepted
|
2022-04-16 02:47:27 +00:00
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
_ => OutResponse::Rejected,
|
2022-04-16 02:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 01:16:45 +00: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 02:47:27 +00:00
|
|
|
self.bus.endpoint_set_stalled(ep_addr, true);
|
2022-05-09 00:11:02 +00:00
|
|
|
OutResponse::Accepted
|
2022-03-28 01:16:45 +00:00
|
|
|
}
|
|
|
|
(Request::CLEAR_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
|
|
|
|
let ep_addr = ((req.index as u8) & 0x8f).into();
|
2022-04-16 02:47:27 +00:00
|
|
|
self.bus.endpoint_set_stalled(ep_addr, false);
|
2022-05-09 00:11:02 +00:00
|
|
|
OutResponse::Accepted
|
2022-03-28 01:16:45 +00:00
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
_ => OutResponse::Rejected,
|
2022-03-09 00:34:35 +00:00
|
|
|
},
|
2022-04-16 02:47:27 +00:00
|
|
|
(RequestType::Class, Recipient::Interface) => {
|
|
|
|
let iface = match self.interfaces.get_mut(req.index as usize) {
|
|
|
|
Some(iface) => iface,
|
2022-05-09 00:11:02 +00:00
|
|
|
None => return OutResponse::Rejected,
|
2022-04-16 02:47:27 +00:00
|
|
|
};
|
|
|
|
match &mut iface.handler {
|
2022-05-09 00:11:02 +00:00
|
|
|
Some(handler) => handler.control_out(req, data),
|
|
|
|
None => OutResponse::Rejected,
|
2022-03-28 01:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
_ => OutResponse::Rejected,
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 00:11:02 +00:00
|
|
|
fn handle_control_in<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> InResponse<'a> {
|
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-05-09 00:11:02 +00:00
|
|
|
buf[..2].copy_from_slice(&status.to_le_bytes());
|
|
|
|
InResponse::Accepted(&buf[..2])
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
Request::GET_DESCRIPTOR => self.handle_get_descriptor(req, buf),
|
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-05-09 00:11:02 +00:00
|
|
|
buf[0] = status;
|
|
|
|
InResponse::Accepted(&buf[..1])
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
_ => InResponse::Rejected,
|
2022-03-28 01:16:45 +00:00
|
|
|
},
|
2022-04-16 02:47:27 +00:00
|
|
|
(RequestType::Standard, Recipient::Interface) => {
|
|
|
|
let iface = match self.interfaces.get_mut(req.index as usize) {
|
|
|
|
Some(iface) => iface,
|
2022-05-09 00:11:02 +00:00
|
|
|
None => return InResponse::Rejected,
|
2022-04-16 02:47:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match req.request {
|
|
|
|
Request::GET_STATUS => {
|
|
|
|
let status: u16 = 0;
|
2022-05-09 00:11:02 +00:00
|
|
|
buf[..2].copy_from_slice(&status.to_le_bytes());
|
|
|
|
InResponse::Accepted(&buf[..2])
|
2022-04-16 02:47:27 +00:00
|
|
|
}
|
|
|
|
Request::GET_INTERFACE => {
|
2022-05-09 00:11:02 +00:00
|
|
|
buf[0] = iface.current_alt_setting;
|
|
|
|
InResponse::Accepted(&buf[..1])
|
2022-04-16 02:47:27 +00:00
|
|
|
}
|
|
|
|
Request::GET_DESCRIPTOR => match &mut iface.handler {
|
2022-05-09 00:11:02 +00:00
|
|
|
Some(handler) => handler.get_descriptor(req, buf),
|
|
|
|
None => InResponse::Rejected,
|
2022-04-16 02:47:27 +00:00
|
|
|
},
|
2022-05-09 00:11:02 +00:00
|
|
|
_ => InResponse::Rejected,
|
2022-04-16 02:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 01:16:45 +00: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 02:47:27 +00:00
|
|
|
if self.bus.endpoint_is_stalled(ep_addr) {
|
2022-03-28 01:16:45 +00:00
|
|
|
status |= 0x0001;
|
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
buf[..2].copy_from_slice(&status.to_le_bytes());
|
|
|
|
InResponse::Accepted(&buf[..2])
|
2022-03-28 01:16:45 +00:00
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
_ => InResponse::Rejected,
|
2022-03-28 01:16:45 +00:00
|
|
|
},
|
2022-04-16 02:47:27 +00:00
|
|
|
(RequestType::Class, Recipient::Interface) => {
|
|
|
|
let iface = match self.interfaces.get_mut(req.index as usize) {
|
|
|
|
Some(iface) => iface,
|
2022-05-09 00:11:02 +00:00
|
|
|
None => return InResponse::Rejected,
|
2022-04-16 02:47:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match &mut iface.handler {
|
2022-05-09 00:11:02 +00:00
|
|
|
Some(handler) => handler.control_in(req, buf),
|
|
|
|
None => InResponse::Rejected,
|
2022-03-28 01:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
_ => InResponse::Rejected,
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 00:11:02 +00:00
|
|
|
fn handle_get_descriptor<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> InResponse<'a> {
|
2022-03-09 00:34:35 +00:00
|
|
|
let (dtype, index) = req.descriptor_type_index();
|
|
|
|
|
|
|
|
match dtype {
|
2022-05-09 00:11:02 +00:00
|
|
|
descriptor_type::BOS => InResponse::Accepted(self.bos_descriptor),
|
|
|
|
descriptor_type::DEVICE => InResponse::Accepted(self.device_descriptor),
|
|
|
|
descriptor_type::CONFIGURATION => InResponse::Accepted(self.config_descriptor),
|
2022-03-09 00:34:35 +00:00
|
|
|
descriptor_type::STRING => {
|
|
|
|
if index == 0 {
|
2022-05-09 00:11:02 +00:00
|
|
|
buf[0] = 4; // len
|
|
|
|
buf[1] = descriptor_type::STRING;
|
|
|
|
buf[2] = lang_id::ENGLISH_US as u8;
|
|
|
|
buf[3] = (lang_id::ENGLISH_US >> 8) as u8;
|
|
|
|
InResponse::Accepted(&buf[..4])
|
2022-03-09 00:34:35 +00:00
|
|
|
} else {
|
|
|
|
let s = match index {
|
2022-04-23 02:40:57 +00:00
|
|
|
STRING_INDEX_MANUFACTURER => self.config.manufacturer,
|
|
|
|
STRING_INDEX_PRODUCT => self.config.product,
|
|
|
|
STRING_INDEX_SERIAL_NUMBER => self.config.serial_number,
|
2022-03-09 00:34:35 +00:00
|
|
|
_ => {
|
2022-04-23 02:40:57 +00: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;
|
2022-05-09 00:11:02 +00:00
|
|
|
handler.get_string(index, lang_id)
|
2022-04-23 02:40:57 +00:00
|
|
|
} 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 00:34:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(s) = s {
|
2022-05-09 00:11:02 +00:00
|
|
|
if buf.len() < 2 {
|
|
|
|
panic!("control buffer too small");
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[1] = descriptor_type::STRING;
|
|
|
|
let mut pos = 2;
|
|
|
|
for c in s.encode_utf16() {
|
|
|
|
if pos >= buf.len() {
|
|
|
|
panic!("control buffer too small");
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes());
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = pos as u8;
|
|
|
|
InResponse::Accepted(&buf[..pos])
|
2022-03-09 00:34:35 +00:00
|
|
|
} else {
|
2022-05-09 00:11:02 +00:00
|
|
|
InResponse::Rejected
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-09 00:11:02 +00:00
|
|
|
_ => InResponse::Rejected,
|
2022-03-09 00:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|