much progress, descriptors and sample rates getting basically working?
This commit is contained in:
+558
-111
@@ -1,22 +1,35 @@
|
||||
#![no_std]
|
||||
#![allow(dead_code)]
|
||||
|
||||
mod constants;
|
||||
pub mod constants;
|
||||
mod cursor;
|
||||
mod descriptors;
|
||||
pub mod descriptors;
|
||||
mod log;
|
||||
|
||||
use core::cmp::Ordering;
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use byteorder_embedded_io::{LittleEndian, WriteBytesExt};
|
||||
use constants::*;
|
||||
use descriptors::*;
|
||||
use log::*;
|
||||
|
||||
use usb_device::class_prelude::*;
|
||||
use num_traits::{ConstZero, Zero};
|
||||
use usb_device::control::{Recipient, Request, RequestType};
|
||||
use usb_device::device::DEFAULT_ALTERNATE_SETTING;
|
||||
use usb_device::endpoint::{self, Endpoint, EndpointDirection, In, Out};
|
||||
use usb_device::{UsbDirection, class_prelude::*};
|
||||
|
||||
pub use constants::USB_CLASS_AUDIO;
|
||||
|
||||
#[cfg(feature = "defmt")]
|
||||
use defmt;
|
||||
|
||||
mod sealed {
|
||||
pub trait Sealed {}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum UsbAudioClassError {
|
||||
NotImplemented,
|
||||
Other,
|
||||
@@ -28,7 +41,16 @@ impl<T: core::error::Error> From<T> for UsbAudioClassError {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RangeType: sealed::Sealed {}
|
||||
impl From<UsbAudioClassError> for UsbError {
|
||||
fn from(_value: UsbAudioClassError) -> Self {
|
||||
UsbError::Unsupported
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RangeType:
|
||||
sealed::Sealed + num_traits::PrimInt + num_traits::ToBytes + ConstZero
|
||||
{
|
||||
}
|
||||
impl sealed::Sealed for i8 {}
|
||||
impl sealed::Sealed for i16 {}
|
||||
impl sealed::Sealed for i32 {}
|
||||
@@ -43,55 +65,81 @@ impl RangeType for u8 {}
|
||||
impl RangeType for u16 {}
|
||||
impl RangeType for u32 {}
|
||||
|
||||
#[derive(PartialEq, Eq, Ord)]
|
||||
pub struct RangeEntry<T: RangeType> {
|
||||
min: T,
|
||||
max: T,
|
||||
res: T,
|
||||
pub min: T,
|
||||
pub max: T,
|
||||
pub res: T,
|
||||
}
|
||||
|
||||
impl<T: RangeType> RangeEntry<T> {
|
||||
pub fn new(min: T, max: T, res: T) -> Self {
|
||||
pub const fn new(min: T, max: T, res: T) -> Self {
|
||||
Self { min, max, res }
|
||||
}
|
||||
pub const fn new_fixed(fixed: T) -> Self {
|
||||
Self {
|
||||
min: fixed,
|
||||
max: fixed,
|
||||
res: T::ZERO,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write<W: embedded_io::Write>(
|
||||
&self,
|
||||
mut buf: W,
|
||||
) -> core::result::Result<usize, W::Error> {
|
||||
buf.write_all(self.min.to_le_bytes().as_ref())?;
|
||||
buf.write_all(self.max.to_le_bytes().as_ref())?;
|
||||
buf.write_all(self.res.to_le_bytes().as_ref())?;
|
||||
Ok(T::ZERO.count_zeros() as usize * 3)
|
||||
}
|
||||
}
|
||||
|
||||
/// The spec guarantees that ranges do not overlap, so compare by min is correct.
|
||||
impl<T: RangeType + PartialOrd> PartialOrd for RangeEntry<T> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.min.partial_cmp(&other.min)
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait for implementing USB Audio Class 2 devices
|
||||
///
|
||||
/// Contains optional callback methods which will be called by the class driver. All
|
||||
/// Contains callback methods which will be called by the class driver. All
|
||||
/// callbacks are optional, which may be useful for a tight-loop polling implementation
|
||||
/// but most implementations will want to implement at least `audio_data_rx`.
|
||||
///
|
||||
/// Unimplemented callbacks should return `Err(UsbAudioClassError::NotImplemented)`. Other
|
||||
/// errors will panic (the underlying callbacks are not fallible). If you need to handle errors,
|
||||
/// you should use the callback to infalliably signal another task.
|
||||
/// Unimplemented callbacks should return `Ok(())` if a result is required.
|
||||
|
||||
pub trait UsbAudioClass<'a, B: UsbBus> {
|
||||
/// Called when audio data is received from the host. The `Endpoint`
|
||||
/// is ready for `read()`.
|
||||
fn audio_data_rx(
|
||||
/// Called when audio data is received from the host. `ep` is ready for
|
||||
/// `ep.read()`.
|
||||
fn audio_data_rx(&self, ep: &Endpoint<'a, B, endpoint::Out>) {}
|
||||
|
||||
/// Called when the alternate setting of `terminal`'s interface is changed,
|
||||
/// before the `AudioStream` is updated. Currently not very useful since we
|
||||
/// don't implement alternate settings.
|
||||
fn alternate_setting_changed<CS: UsbAudioClockImpl, AU: UsbAudioClass<'a, B>>(
|
||||
&self,
|
||||
ep: &Endpoint<'a, B, endpoint::Out>,
|
||||
) -> core::result::Result<(), UsbAudioClassError> {
|
||||
Err(UsbAudioClassError::NotImplemented)
|
||||
ac: &mut AudioClass<'a, B, CS, AU>,
|
||||
terminal: UsbDirection,
|
||||
alt_setting: u8,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait for implementing Sampling Frequency Control for USB Audio Clock Sources
|
||||
/// ref: USB Audio Class Specification 2.0 5.2.5.1.1
|
||||
///
|
||||
/// Contains optional callback methods which will be called by the class driver. If
|
||||
/// `set_sample_rate` is implemented, `get_sample_rate` must also be implemented.
|
||||
/// Callbacks run in USB context, so should not block.
|
||||
/// Contains callback methods which will be called by the class driver.
|
||||
///
|
||||
/// Unimplemented callbacks should return `Err(UsbAudioClassError::NotImplemented)`. Other
|
||||
/// errors will panic (the underlying callbacks are not fallible). If you need to handle errors,
|
||||
/// you should use the callback to infalliably signal another task.
|
||||
pub trait UsbAudioClockSource {
|
||||
pub trait UsbAudioClockImpl {
|
||||
const CLOCK_TYPE: ClockType;
|
||||
const SOF_SYNC: bool;
|
||||
/// Called when the host requests the current sample rate. Returns the sample rate in Hz.
|
||||
fn get_sample_rate(&self) -> core::result::Result<u32, UsbAudioClassError> {
|
||||
Err(UsbAudioClassError::NotImplemented)
|
||||
}
|
||||
fn get_sample_rate(&self) -> core::result::Result<u32, UsbAudioClassError>;
|
||||
/// Called when the host requests to set the sample rate. Should reconfigure the clock source
|
||||
/// if necessary.
|
||||
fn set_sample_rate(
|
||||
@@ -116,9 +164,9 @@ pub trait UsbAudioClockSource {
|
||||
}
|
||||
}
|
||||
|
||||
/// Called when the hosts makes a RANGE request for the clock source. Returns a slice of possible sample rates.
|
||||
/// Called during allocation and when the hosts makes a RANGE request for the clock source.
|
||||
///
|
||||
/// Must be implemented if the clock source returns programmable get_frequency_access
|
||||
/// Returns a slice of possible sample rates.
|
||||
///
|
||||
/// Rates must meet the invariants in the specification:
|
||||
/// * The subranges must be ordered in ascendingorder
|
||||
@@ -127,9 +175,7 @@ pub trait UsbAudioClockSource {
|
||||
/// its MIN and MAX subattribute and the RES subattribute must be set to zero
|
||||
///
|
||||
/// ref: USB Audio Class Specification 2.0 5.2.1 & 5.2.3.3
|
||||
fn get_rates(&self) -> core::result::Result<&[RangeEntry<u32>], UsbAudioClassError> {
|
||||
Err(UsbAudioClassError::NotImplemented)
|
||||
}
|
||||
fn get_rates(&self) -> core::result::Result<&[RangeEntry<u32>], UsbAudioClassError>;
|
||||
|
||||
/// Build the ClockSource descriptor. It is not intended to override this method.
|
||||
///
|
||||
@@ -208,13 +254,17 @@ impl<D: EndpointDirection> TerminalConfig<D> {
|
||||
_direction: PhantomData,
|
||||
}
|
||||
}
|
||||
// Bytes per audio frame
|
||||
pub fn bytes_per_frame(&self) -> u32 {
|
||||
self.format.bytes_per_sample as u32 * self.num_channels as u32
|
||||
}
|
||||
}
|
||||
impl<'a> TerminalConfigurationDescriptors for TerminalConfig<In> {
|
||||
fn get_configuration_descriptors(&self) -> (InputTerminal, OutputTerminal) {
|
||||
let input_terminal = InputTerminal {
|
||||
id: self.base_id,
|
||||
terminal_type: TerminalType::UsbStreaming,
|
||||
assoc_terminal: self.base_id + 1,
|
||||
assoc_terminal: 0,
|
||||
clock_source: self.clock_source_id,
|
||||
num_channels: self.num_channels,
|
||||
channel_config: self.channel_config,
|
||||
@@ -226,12 +276,12 @@ impl<'a> TerminalConfigurationDescriptors for TerminalConfig<In> {
|
||||
underflow_control: AccessControl::NotPresent,
|
||||
overflow_control: AccessControl::NotPresent,
|
||||
phantom_power_control: AccessControl::NotPresent,
|
||||
string: None,
|
||||
string: self.string,
|
||||
};
|
||||
let output_terminal = OutputTerminal {
|
||||
id: self.base_id + 1,
|
||||
terminal_type: self.terminal_type,
|
||||
assoc_terminal: self.base_id,
|
||||
assoc_terminal: 0,
|
||||
source_id: self.base_id,
|
||||
clock_source: self.clock_source_id,
|
||||
copy_protect_control: AccessControl::NotPresent,
|
||||
@@ -251,7 +301,7 @@ impl<'a> TerminalConfigurationDescriptors for TerminalConfig<Out> {
|
||||
let output_terminal = OutputTerminal {
|
||||
id: self.base_id,
|
||||
terminal_type: TerminalType::UsbStreaming,
|
||||
assoc_terminal: self.base_id + 1,
|
||||
assoc_terminal: 0,
|
||||
source_id: self.base_id + 1,
|
||||
clock_source: self.clock_source_id,
|
||||
copy_protect_control: AccessControl::NotPresent,
|
||||
@@ -264,7 +314,7 @@ impl<'a> TerminalConfigurationDescriptors for TerminalConfig<Out> {
|
||||
let input_terminal = InputTerminal {
|
||||
id: self.base_id + 1,
|
||||
terminal_type: self.terminal_type,
|
||||
assoc_terminal: self.base_id,
|
||||
assoc_terminal: 0,
|
||||
clock_source: self.clock_source_id,
|
||||
num_channels: self.num_channels,
|
||||
channel_config: self.channel_config,
|
||||
@@ -281,40 +331,13 @@ impl<'a> TerminalConfigurationDescriptors for TerminalConfig<Out> {
|
||||
(input_terminal, output_terminal)
|
||||
}
|
||||
}
|
||||
impl<D: EndpointDirection> TerminalConfig<D> {}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum UsbSpeed {
|
||||
Low, // Not supported for audio
|
||||
Full,
|
||||
High,
|
||||
Super, // Not supported by crate
|
||||
}
|
||||
|
||||
/// Since usb-device doesn't expose the underlying speed of the bus, the user needs to provide an implementation.
|
||||
///
|
||||
///
|
||||
///
|
||||
/// This will be called whenever descriptors are sent to the host..
|
||||
pub trait UsbSpeedProvider {
|
||||
fn speed(&self) -> UsbSpeed;
|
||||
}
|
||||
|
||||
/// Convenience implementation of UsbSpeedProvider for devices which only support one speed.
|
||||
pub struct ConstSpeedProvider {
|
||||
speed: UsbSpeed,
|
||||
}
|
||||
|
||||
impl ConstSpeedProvider {
|
||||
pub const fn new(speed: UsbSpeed) -> Self {
|
||||
ConstSpeedProvider { speed }
|
||||
}
|
||||
}
|
||||
|
||||
impl UsbSpeedProvider for ConstSpeedProvider {
|
||||
fn speed(&self) -> UsbSpeed {
|
||||
self.speed
|
||||
}
|
||||
Super,
|
||||
}
|
||||
|
||||
/// Configuration and references to the Audio Class descriptors
|
||||
@@ -335,32 +358,43 @@ impl UsbSpeedProvider for ConstSpeedProvider {
|
||||
/// A single Clock Source is always required, but a fully custom descriptor set can be built by only providing
|
||||
/// the Clock Source and additional descriptors, if the Terminal descriptors are inappropriate.
|
||||
///
|
||||
pub struct AudioClassConfig<'a, CS: UsbAudioClockSource, SP: UsbSpeedProvider> {
|
||||
pub speed_provider: SP,
|
||||
pub struct AudioClassConfig<'a, B: UsbBus, CS: UsbAudioClockImpl, AU: UsbAudioClass<'a, B>> {
|
||||
pub speed: UsbSpeed,
|
||||
pub device_category: FunctionCode,
|
||||
pub clock: CS,
|
||||
pub clock_impl: &'a CS,
|
||||
pub audio_impl: &'a AU,
|
||||
pub input_config: Option<TerminalConfig<Out>>,
|
||||
pub output_config: Option<TerminalConfig<In>>,
|
||||
pub additional_descriptors: Option<&'a [AudioClassDescriptor]>,
|
||||
_bus: PhantomData<B>,
|
||||
}
|
||||
|
||||
impl<'a, SP: UsbSpeedProvider, CS: UsbAudioClockSource> AudioClassConfig<'a, CS, SP> {
|
||||
pub fn new(speed_provider: SP, device_category: FunctionCode, clock: CS) -> Self {
|
||||
impl<'a, B: UsbBus, CS: UsbAudioClockImpl, AU: UsbAudioClass<'a, B>>
|
||||
AudioClassConfig<'a, B, CS, AU>
|
||||
{
|
||||
pub fn new(
|
||||
speed: UsbSpeed,
|
||||
device_category: FunctionCode,
|
||||
clock_impl: &'a CS,
|
||||
audio_impl: &'a AU,
|
||||
) -> Self {
|
||||
Self {
|
||||
speed_provider,
|
||||
speed,
|
||||
device_category,
|
||||
clock,
|
||||
clock_impl,
|
||||
audio_impl,
|
||||
input_config: None,
|
||||
output_config: None,
|
||||
additional_descriptors: None,
|
||||
_bus: PhantomData,
|
||||
}
|
||||
}
|
||||
pub fn with_input_config(mut self, input_config: TerminalConfig<Out>) -> Self {
|
||||
self.input_config = Some(input_config);
|
||||
self
|
||||
}
|
||||
pub fn with_output_terminal(mut self, output_terminal: TerminalConfig<In>) -> Self {
|
||||
self.output_config = Some(output_terminal);
|
||||
pub fn with_output_config(mut self, output_config: TerminalConfig<In>) -> Self {
|
||||
self.output_config = Some(output_config);
|
||||
self
|
||||
}
|
||||
pub fn with_additional_descriptors(
|
||||
@@ -371,9 +405,88 @@ impl<'a, SP: UsbSpeedProvider, CS: UsbAudioClockSource> AudioClassConfig<'a, CS,
|
||||
self
|
||||
}
|
||||
|
||||
// pub fn build<'a, B: UsbBus>(self, alloc: &'a UsbBusAllocator) -> Result<AudioClass<'a, B>> {
|
||||
// Err(Error::InvalidValue)
|
||||
// }
|
||||
/// Allocate the various USB IDs, and build the class implementation
|
||||
pub fn build(self, alloc: &'a UsbBusAllocator<B>) -> Result<AudioClass<'a, B, CS, AU>> {
|
||||
let speed = self.speed;
|
||||
let interval = match speed {
|
||||
UsbSpeed::Full => 1,
|
||||
UsbSpeed::High | UsbSpeed::Super => 4, // rate = 2^(4-1) * 125us = 1ms, same as full speed
|
||||
UsbSpeed::Low => return Err(Error::InvalidSpeed),
|
||||
};
|
||||
let max_rate = self
|
||||
.clock_impl
|
||||
.get_rates()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.max()
|
||||
.unwrap()
|
||||
.max;
|
||||
let control_iface = alloc.interface();
|
||||
|
||||
let mut ac = AudioClass {
|
||||
control_iface,
|
||||
clock_impl: self.clock_impl,
|
||||
audio_impl: self.audio_impl,
|
||||
output: None,
|
||||
input: None,
|
||||
feedback: None,
|
||||
additional_descriptors: self.additional_descriptors,
|
||||
device_category: self.device_category,
|
||||
in_iface: 0,
|
||||
out_iface: 0,
|
||||
in_ep: 0,
|
||||
out_ep: 0,
|
||||
fb_ep: 0,
|
||||
};
|
||||
|
||||
if let Some(config) = self.output_config {
|
||||
let interface = alloc.interface();
|
||||
let endpoint = alloc.isochronous(
|
||||
config.sync_type,
|
||||
IsochronousUsageType::Data,
|
||||
((config.bytes_per_frame() * max_rate) / 1000) as u16,
|
||||
interval,
|
||||
);
|
||||
let feedback_ep = alloc.isochronous(
|
||||
IsochronousSynchronizationType::NoSynchronization,
|
||||
IsochronousUsageType::Feedback,
|
||||
4,
|
||||
interval,
|
||||
);
|
||||
let alt_setting = DEFAULT_ALTERNATE_SETTING;
|
||||
ac.in_iface = interface.into();
|
||||
ac.in_ep = endpoint.address().index();
|
||||
ac.fb_ep = feedback_ep.address().index();
|
||||
ac.input = Some(AudioStream {
|
||||
config,
|
||||
interface,
|
||||
endpoint,
|
||||
alt_setting,
|
||||
});
|
||||
ac.feedback = Some(feedback_ep);
|
||||
}
|
||||
|
||||
if let Some(config) = self.input_config {
|
||||
let interface = alloc.interface();
|
||||
let endpoint = alloc.isochronous(
|
||||
config.sync_type,
|
||||
IsochronousUsageType::Data,
|
||||
((config.bytes_per_frame() * max_rate) / 1000) as u16,
|
||||
interval,
|
||||
);
|
||||
let alt_setting = DEFAULT_ALTERNATE_SETTING;
|
||||
ac.out_iface = interface.into();
|
||||
ac.out_ep = endpoint.address().index();
|
||||
ac.output = Some(AudioStream {
|
||||
config,
|
||||
interface,
|
||||
endpoint,
|
||||
alt_setting,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(ac)
|
||||
}
|
||||
}
|
||||
|
||||
/// USB audio errors, including possible USB Stack errors
|
||||
@@ -382,6 +495,7 @@ pub enum Error {
|
||||
InvalidValue,
|
||||
BandwidthExceeded,
|
||||
StreamNotInitialized,
|
||||
InvalidSpeed,
|
||||
UsbError(usb_device::UsbError),
|
||||
}
|
||||
|
||||
@@ -402,6 +516,10 @@ struct AudioStream<'a, B: UsbBus, D: EndpointDirection> {
|
||||
|
||||
impl<'a, B: UsbBus, D: EndpointDirection> AudioStream<'a, B, D> {
|
||||
fn write_interface_descriptors(&self, writer: &mut DescriptorWriter) -> usb_device::Result<()> {
|
||||
debug!(
|
||||
" AudioStream<{}>.write_interface_descriptors",
|
||||
core::any::type_name::<D>()
|
||||
);
|
||||
// UAC2 4.9.1 Standard AS Interface Descriptor
|
||||
// zero bandwidth configuration per 3.16.2
|
||||
//
|
||||
@@ -410,18 +528,20 @@ impl<'a, B: UsbBus, D: EndpointDirection> AudioStream<'a, B, D> {
|
||||
// (Alternate Setting 0) with zero bandwidth requirements (no
|
||||
// isochronous data endpoint defined) and an additional Alternate
|
||||
// Setting that contains the actual isochronous data endpoint.
|
||||
debug!("writer.interface AudioStreaming");
|
||||
writer.interface(
|
||||
self.interface,
|
||||
AUDIO,
|
||||
USB_CLASS_AUDIO,
|
||||
InterfaceSubclass::AudioStreaming as u8,
|
||||
InterfaceProtocol::Version2 as u8,
|
||||
)?;
|
||||
// UAC2 4.9.1 Standard AS Interface Descriptor
|
||||
// live data configuration
|
||||
debug!("writer.interface_alt AudioStreaming");
|
||||
writer.interface_alt(
|
||||
self.interface,
|
||||
1,
|
||||
AUDIO,
|
||||
USB_CLASS_AUDIO,
|
||||
InterfaceSubclass::AudioStreaming as u8,
|
||||
InterfaceProtocol::Version2 as u8,
|
||||
None,
|
||||
@@ -448,6 +568,11 @@ impl<'a, B: UsbBus, D: EndpointDirection> AudioStream<'a, B, D> {
|
||||
|
||||
fn write_endpoint_descriptors(&self, writer: &mut DescriptorWriter) -> usb_device::Result<()> {
|
||||
// UAC2 4.10.1.1 Standard AS Isochronous Audio Data Endpoint Descriptor
|
||||
debug!(
|
||||
" AudioStream<{}>.write_endpoint_descriptors",
|
||||
core::any::type_name::<D>()
|
||||
);
|
||||
debug!("writer.endpoint");
|
||||
writer.endpoint(&self.endpoint)?;
|
||||
// UAC2 4.10.1.2 Class-Specific AS Isochronous Audio Data Endpoint Descriptor
|
||||
let cs_ep = AudioStreamingEndpoint {
|
||||
@@ -462,77 +587,99 @@ impl<'a, B: UsbBus, D: EndpointDirection> AudioStream<'a, B, D> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AudioClass<'a, B: UsbBus, CS: UsbAudioClockSource, SP: UsbSpeedProvider> {
|
||||
config: AudioClassConfig<'a, CS, SP>,
|
||||
pub struct AudioClass<'a, B: UsbBus, CS: UsbAudioClockImpl, AU: UsbAudioClass<'a, B>> {
|
||||
control_iface: InterfaceNumber,
|
||||
clock_impl: &'a CS,
|
||||
audio_impl: &'a AU,
|
||||
output: Option<AudioStream<'a, B, Out>>,
|
||||
input: Option<AudioStream<'a, B, In>>,
|
||||
feedback: Option<Endpoint<'a, B, In>>,
|
||||
additional_descriptors: Option<&'a [AudioClassDescriptor]>,
|
||||
device_category: FunctionCode,
|
||||
in_iface: u8,
|
||||
out_iface: u8,
|
||||
in_ep: usize,
|
||||
out_ep: usize,
|
||||
fb_ep: usize,
|
||||
}
|
||||
|
||||
impl<'a, B: UsbBus, CS: UsbAudioClockSource, SP: UsbSpeedProvider> AudioClass<'a, B, CS, SP> {
|
||||
fn get_interface_descriptors(&self, writer: &mut DescriptorWriter) -> usb_device::Result<()> {
|
||||
// Control + 1 or 2 streaming
|
||||
let n_interfaces = 1
|
||||
+ (self.config.input_config.is_some() as u8)
|
||||
+ (self.config.output_config.is_some() as u8);
|
||||
impl<'a, B: UsbBus, CS: UsbAudioClockImpl, AU: UsbAudioClass<'a, B>> UsbClass<B>
|
||||
for AudioClass<'a, B, CS, AU>
|
||||
{
|
||||
fn get_configuration_descriptors(
|
||||
&self,
|
||||
writer: &mut DescriptorWriter<'_>,
|
||||
) -> usb_device::Result<()> {
|
||||
info!(" AudioClass::get_configuration_descriptors");
|
||||
// Control + 0-2 streaming
|
||||
let n_interfaces = 1 + (self.input.is_some() as u8) + (self.input.is_some() as u8);
|
||||
|
||||
debug!("writer.iad()");
|
||||
// UAC2 4.6 Interface Association Descriptor
|
||||
writer.iad(
|
||||
self.control_iface,
|
||||
n_interfaces,
|
||||
AUDIO,
|
||||
InterfaceSubclass::AudioControl as u8,
|
||||
USB_CLASS_AUDIO,
|
||||
InterfaceSubclass::Undefined as u8,
|
||||
FunctionProtocol::Version2 as u8,
|
||||
None,
|
||||
)?;
|
||||
|
||||
debug!("writer.interface()");
|
||||
// UAC2 4.7 Standard AC Interface Descriptor
|
||||
writer.interface(
|
||||
self.control_iface,
|
||||
0,
|
||||
AUDIO,
|
||||
USB_CLASS_AUDIO,
|
||||
InterfaceSubclass::AudioControl as u8,
|
||||
InterfaceProtocol::Version2 as u8,
|
||||
)?;
|
||||
|
||||
// BUILD CONFIGURATION DESCRIPTORS //
|
||||
let mut total_length: u16 = 9; // HEADER
|
||||
let clock_desc = self.config.clock.get_configuration_descriptor(1, None)?;
|
||||
let clock_desc = self.clock_impl.get_configuration_descriptor(1, None)?;
|
||||
total_length += clock_desc.size() as u16;
|
||||
let output_descs = match &self.config.output_config {
|
||||
Some(config) => {
|
||||
let descs = config.get_configuration_descriptors();
|
||||
let output_descs = match &self.output {
|
||||
Some(stream) => {
|
||||
let descs = stream.config.get_configuration_descriptors();
|
||||
total_length += descs.0.size() as u16 + descs.1.size() as u16;
|
||||
Some(descs)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let input_descs = match &self.config.input_config {
|
||||
Some(config) => {
|
||||
let descs = config.get_configuration_descriptors();
|
||||
let input_descs = match &self.input {
|
||||
Some(stream) => {
|
||||
let descs = stream.config.get_configuration_descriptors();
|
||||
total_length += descs.0.size() as u16 + descs.1.size() as u16;
|
||||
Some(descs)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let additional_descs = match &self.config.additional_descriptors {
|
||||
let additional_descs = match &self.additional_descriptors {
|
||||
Some(descs) => {
|
||||
total_length += descs.iter().map(|desc| desc.size() as u16).sum::<u16>();
|
||||
Some(descs)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
debug!(
|
||||
"have output: {}, have input: {}, have additional: {}, total length: {}",
|
||||
output_descs.is_some(),
|
||||
input_descs.is_some(),
|
||||
additional_descs.is_some(),
|
||||
total_length
|
||||
);
|
||||
|
||||
// UAC2 4.7.2 Class-specific AC Interface Descriptor
|
||||
let ac_header: [u8; 7] = [
|
||||
ClassSpecificACInterfaceDescriptorSubtype::Header as u8,
|
||||
0, // bcdADC[0]
|
||||
2, // bcdADC[1]
|
||||
self.config.device_category as u8, // bCategory
|
||||
self.device_category as u8, // bCategory
|
||||
(total_length & 0xff) as u8, // wTotalLength LSB
|
||||
((total_length >> 8) & 0xff) as u8, // wTotalLength MSB
|
||||
0, // bmControls
|
||||
];
|
||||
debug!("writer.write (AC header)");
|
||||
writer.write(ClassSpecificDescriptorType::Interface as u8, &ac_header)?;
|
||||
|
||||
// UAC2 4.7.2.1 Clock Source Descriptor
|
||||
@@ -563,6 +710,7 @@ impl<'a, B: UsbBus, CS: UsbAudioClockSource, SP: UsbSpeedProvider> AudioClass<'a
|
||||
// UAC2 4.9.2.1 Feedback Endpoint Descriptor
|
||||
// Should always be present if an OUT endpoint is present
|
||||
if let Some(feedback) = &self.feedback {
|
||||
debug!("writer.endpoint (feedback)");
|
||||
writer.endpoint(feedback)?;
|
||||
}
|
||||
|
||||
@@ -573,18 +721,317 @@ impl<'a, B: UsbBus, CS: UsbAudioClockSource, SP: UsbSpeedProvider> AudioClass<'a
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: UsbBus, CS: UsbAudioClockSource, SP: UsbSpeedProvider> UsbClass<B>
|
||||
for AudioClass<'_, B, CS, SP>
|
||||
{
|
||||
/// Writes the class-specific configuration descriptor set (after bDescriptortype INTERFACE)
|
||||
fn get_configuration_descriptors(
|
||||
&self,
|
||||
writer: &mut DescriptorWriter<'_>,
|
||||
) -> usb_device::Result<()> {
|
||||
self.get_interface_descriptors(writer)?;
|
||||
|
||||
Ok(())
|
||||
fn control_out(&mut self, xfer: ControlOut<B>) {
|
||||
let req = xfer.request();
|
||||
match req.request_type {
|
||||
RequestType::Standard => self.standard_request_out(xfer),
|
||||
RequestType::Class => self.class_request_out(xfer),
|
||||
_ => {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
}
|
||||
}
|
||||
fn control_in(&mut self, xfer: ControlIn<B>) {
|
||||
let req = xfer.request();
|
||||
match req.request_type {
|
||||
RequestType::Standard => self.standard_request_in(xfer),
|
||||
RequestType::Class => self.class_request_in(xfer),
|
||||
_ => {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, B: UsbBus, CS: UsbAudioClockImpl, AU: UsbAudioClass<'a, B>> AudioClass<'a, B, CS, AU> {
|
||||
fn standard_request_out(&mut self, xfer: ControlOut<B>) {
|
||||
let req = xfer.request();
|
||||
match (req.recipient, req.request) {
|
||||
(Recipient::Interface, Request::SET_INTERFACE) => self.set_alt_interface(xfer),
|
||||
_ => {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn standard_request_in(&mut self, xfer: ControlIn<B>) {
|
||||
let req = xfer.request();
|
||||
match (req.recipient, req.request) {
|
||||
(Recipient::Interface, Request::GET_INTERFACE) => self.get_alt_interface(xfer),
|
||||
_ => {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn class_request_out(&mut self, xfer: ControlOut<B>) {
|
||||
let req = xfer.request();
|
||||
match (req.recipient, req.request.try_into()) {
|
||||
(Recipient::Interface, Ok(ClassSpecificRequest::Cur)) => self.set_interface_cur(xfer),
|
||||
(Recipient::Interface, Ok(ClassSpecificRequest::Range)) => {}
|
||||
(Recipient::Endpoint, Ok(ClassSpecificRequest::Cur)) => self.set_endpoint_cur(xfer),
|
||||
(Recipient::Endpoint, Ok(ClassSpecificRequest::Range)) => {}
|
||||
_ => {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn class_request_in(&mut self, xfer: ControlIn<B>) {
|
||||
let req = xfer.request();
|
||||
|
||||
match (req.recipient, req.request.try_into()) {
|
||||
(Recipient::Interface, Ok(ClassSpecificRequest::Cur)) => self.get_interface_cur(xfer),
|
||||
(Recipient::Interface, Ok(ClassSpecificRequest::Range)) => {
|
||||
self.get_interface_range(xfer)
|
||||
}
|
||||
(Recipient::Endpoint, Ok(ClassSpecificRequest::Cur)) => self.get_endpoint_cur(xfer),
|
||||
(Recipient::Endpoint, Ok(ClassSpecificRequest::Range)) => self.get_endpoint_range(xfer),
|
||||
_ => {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_alt_interface(&mut self, xfer: ControlOut<B>) {
|
||||
let req = xfer.request();
|
||||
let iface = req.index as u8;
|
||||
let alt_setting = req.value as u8;
|
||||
|
||||
debug!(" SET_ALT_INTERFACE {} {}", iface, alt_setting);
|
||||
|
||||
if self.input.is_some() && iface == self.in_iface {
|
||||
let old_alt = self.input.as_ref().unwrap().alt_setting;
|
||||
if old_alt != alt_setting {
|
||||
self.audio_impl
|
||||
.alternate_setting_changed(self, UsbDirection::In, alt_setting);
|
||||
self.input.as_mut().unwrap().alt_setting = alt_setting;
|
||||
xfer.accept().ok();
|
||||
}
|
||||
} else if self.output.is_some() && iface == self.out_iface {
|
||||
let old_alt = self.output.as_ref().unwrap().alt_setting;
|
||||
if old_alt != alt_setting {
|
||||
self.audio_impl
|
||||
.alternate_setting_changed(self, UsbDirection::Out, alt_setting);
|
||||
self.output.as_mut().unwrap().alt_setting = alt_setting;
|
||||
xfer.accept().ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
fn get_alt_interface(&mut self, xfer: ControlIn<B>) {
|
||||
let req = xfer.request();
|
||||
let iface = req.index as u8;
|
||||
debug!(" GET_ALT_INTERFACE {}", iface);
|
||||
if self.input.is_some() && iface == self.in_iface {
|
||||
xfer.accept_with(&[self.input.as_ref().unwrap().alt_setting])
|
||||
.ok();
|
||||
return;
|
||||
} else if self.output.is_some() && iface == self.out_iface {
|
||||
xfer.accept_with(&[self.output.as_ref().unwrap().alt_setting])
|
||||
.ok();
|
||||
return;
|
||||
}
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
fn get_interface_cur(&mut self, xfer: ControlIn<B>) {
|
||||
let req = xfer.request();
|
||||
let entity = (req.index >> 8) as u8;
|
||||
let interface = (req.index & 0xff) as u8;
|
||||
let control = (req.value >> 8) as u8;
|
||||
let channel = (req.value & 0xff) as u8;
|
||||
|
||||
debug!(
|
||||
" GET_INTERFACE_CUR entity: {} iface: {} control: {} channel: {}",
|
||||
entity, interface, control, channel
|
||||
);
|
||||
if interface == self.control_iface.into() {
|
||||
return self.get_control_interface_cur(xfer, entity, channel, control);
|
||||
}
|
||||
debug!(" Unimpleneted.");
|
||||
}
|
||||
fn set_interface_cur(&mut self, xfer: ControlOut<B>) {
|
||||
let req = xfer.request();
|
||||
let entity = (req.index >> 8) as u8;
|
||||
let interface = (req.index & 0xff) as u8;
|
||||
let control = (req.value >> 8) as u8;
|
||||
let channel = (req.value & 0xff) as u8;
|
||||
|
||||
debug!(
|
||||
" SET_INTERFACE_CUR entity: {} iface: {} control: {} channel: {}",
|
||||
entity, interface, control, channel
|
||||
);
|
||||
|
||||
if interface == self.control_iface.into() {
|
||||
return self.set_control_interface_cur(xfer, entity, channel, control);
|
||||
} else if interface == self.in_iface {
|
||||
return self.set_streaming_interface_cur(
|
||||
xfer,
|
||||
UsbDirection::In,
|
||||
entity,
|
||||
channel,
|
||||
control,
|
||||
);
|
||||
} else if interface == self.out_iface {
|
||||
return self.set_streaming_interface_cur(
|
||||
xfer,
|
||||
UsbDirection::Out,
|
||||
entity,
|
||||
channel,
|
||||
control,
|
||||
);
|
||||
}
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
|
||||
fn get_control_interface_cur(
|
||||
&mut self,
|
||||
xfer: ControlIn<B>,
|
||||
entity: u8,
|
||||
channel: u8,
|
||||
control: u8,
|
||||
) {
|
||||
match entity {
|
||||
1 => return self.get_clock_cur(xfer, channel, control),
|
||||
_ => {}
|
||||
}
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
|
||||
fn set_control_interface_cur(
|
||||
&mut self,
|
||||
xfer: ControlOut<B>,
|
||||
entity: u8,
|
||||
channel: u8,
|
||||
control: u8,
|
||||
) {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
|
||||
fn set_streaming_interface_cur(
|
||||
&mut self,
|
||||
xfer: ControlOut<B>,
|
||||
direction: UsbDirection,
|
||||
entity: u8,
|
||||
channel: u8,
|
||||
control: u8,
|
||||
) {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
|
||||
fn get_endpoint_cur(&mut self, xfer: ControlIn<B>) {
|
||||
let req = xfer.request();
|
||||
let entity = (req.index >> 8) as u8;
|
||||
let interface = (req.index & 0xff) as u8;
|
||||
let control = (req.value >> 8) as u8;
|
||||
let channel = (req.value & 0xff) as u8;
|
||||
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
fn get_endpoint_range(&mut self, xfer: ControlIn<B>) {
|
||||
let req = xfer.request();
|
||||
let entity = (req.index >> 8) as u8;
|
||||
let interface = (req.index & 0xff) as u8;
|
||||
let control = (req.value >> 8) as u8;
|
||||
let channel = (req.value & 0xff) as u8;
|
||||
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
|
||||
fn set_endpoint_cur(&mut self, xfer: ControlOut<B>) {
|
||||
let req = xfer.request();
|
||||
let entity = (req.index >> 8) as u8;
|
||||
let interface = (req.index & 0xff) as u8;
|
||||
let control = (req.value >> 8) as u8;
|
||||
let channel = (req.value & 0xff) as u8;
|
||||
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
|
||||
fn get_interface_range(&mut self, xfer: ControlIn<B>) {
|
||||
let req = xfer.request();
|
||||
|
||||
let entity = (req.index >> 8) as u8;
|
||||
let interface = (req.index & 0xff) as u8;
|
||||
let control = (req.value >> 8) as u8;
|
||||
let channel = (req.value & 0xff) as u8;
|
||||
|
||||
debug!(
|
||||
" GET_INTERFACE_RANGE entity: {} iface: {} control: {} channel: {}",
|
||||
entity, interface, control, channel
|
||||
);
|
||||
if interface == self.control_iface.into() {
|
||||
return self.get_control_interface_range(xfer, entity, channel, control);
|
||||
}
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
|
||||
fn get_control_interface_range(
|
||||
&mut self,
|
||||
xfer: ControlIn<B>,
|
||||
entity: u8,
|
||||
channel: u8,
|
||||
control: u8,
|
||||
) {
|
||||
match entity {
|
||||
1 => self.get_clock_range(xfer, channel, control), // clock source
|
||||
_ => {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
}
|
||||
}
|
||||
fn get_clock_cur(&mut self, xfer: ControlIn<B>, channel: u8, control: u8) {
|
||||
match control.try_into() {
|
||||
Ok(ClockSourceControlSelector::SamFreqControl) => {
|
||||
debug!(" SamplingFreqControl");
|
||||
if channel != 0 {
|
||||
error!(
|
||||
" Invalid channel {} for SamplingFreqControl GET RANGE. Ignoring.",
|
||||
channel
|
||||
);
|
||||
}
|
||||
xfer.accept(|mut buf| match self.clock_impl.get_sample_rate() {
|
||||
Ok(rate) => {
|
||||
debug!(" {}", rate);
|
||||
buf.write_u32::<LittleEndian>(rate)
|
||||
.map_err(|_e| UsbError::BufferOverflow)?;
|
||||
Ok(4)
|
||||
}
|
||||
Err(_e) => Err(UsbError::InvalidState),
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
_ => debug!(" Unimplemented."),
|
||||
}
|
||||
}
|
||||
fn get_clock_range(&mut self, xfer: ControlIn<B>, channel: u8, control: u8) {
|
||||
match control.try_into() {
|
||||
Ok(ClockSourceControlSelector::SamFreqControl) => {
|
||||
debug!(" SamplingFreqControl");
|
||||
if channel != 0 {
|
||||
error!(
|
||||
" Invalid channel {} for SamplingFreqControl GET RANGE. Ignoring.",
|
||||
channel
|
||||
);
|
||||
}
|
||||
xfer.accept(|mut buf| match self.clock_impl.get_rates() {
|
||||
Ok(rates) => {
|
||||
buf.write_u16::<LittleEndian>(rates.len() as u16)
|
||||
.map_err(|_e| UsbError::BufferOverflow)?;
|
||||
let mut written = 2usize;
|
||||
for rate in rates {
|
||||
written += rate
|
||||
.write(&mut buf)
|
||||
.map_err(|_e| UsbError::BufferOverflow)?
|
||||
}
|
||||
Ok(written)
|
||||
}
|
||||
Err(_) => Err(UsbError::InvalidState),
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
_ => {
|
||||
debug!(" Unimplemented.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user