1use super::constants::*;
2use super::helpers::{checked_slice, descriptor_bytes, read_u16_le, read_u32_le, require_len};
3use super::parser_trait::DescriptorParser;
4use super::types::*;
5use super::validation::{
6 validate_ac_interface_header, validate_feature_unit, validate_input_terminal,
7 validate_output_terminal,
8};
9use crate::descriptors::error::DescriptorError;
10
11pub struct AudioControlParser;
12
13impl AudioControlParser {
14 pub fn parse_ac_header(&self, data: &[u8]) -> Result<AcInterfaceHeader, DescriptorError> {
15 const HEADER_LEN: usize = 9;
16 let data = descriptor_bytes(data, HEADER_LEN)?;
17 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC2_AC_HEADER {
18 return Err(DescriptorError::Invalid("not CS_AC header".to_string()));
19 }
20 let h = AcInterfaceHeader {
21 bcd_adc: read_u16_le(data, 3)?,
22 b_category: data[5],
23 w_total_length: read_u16_le(data, 6)?,
24 bm_controls: data[8] as u16,
25 };
26 validate_ac_interface_header(&h)?;
27 Ok(h)
28 }
29
30 pub fn parse_ac_header_v1(&self, data: &[u8]) -> Result<AcInterfaceHeaderV1, DescriptorError> {
31 let data = descriptor_bytes(data, 8)?;
32 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC_AC_HEADER {
33 return Err(DescriptorError::Invalid("not UAC1 AC header".to_string()));
34 }
35 let bcd_adc = read_u16_le(data, 3)?;
36 let w_total_length = read_u16_le(data, 5)?;
37 let b_in_collection = data[7];
38 let len = data[0] as usize;
39 if len != 8 + b_in_collection as usize {
40 return Err(DescriptorError::Invalid(
41 "UAC1 AC interface collection length mismatch".to_string(),
42 ));
43 }
44 require_len(data, len)?;
45 let ba_interface_nr: Vec<u8> = data[8..len].to_vec();
46 Ok(AcInterfaceHeaderV1 {
47 bcd_adc,
48 w_total_length,
49 b_in_collection,
50 ba_interface_nr,
51 })
52 }
53
54 fn detect_and_parse_ac_header(
55 &self,
56 data: &[u8],
57 ) -> Result<AudioControlDescriptor, DescriptorError> {
58 let data = descriptor_bytes(data, 5)?;
59 match read_u16_le(data, 3)? {
60 UAC1_BCD_ADC => self
61 .parse_ac_header_v1(data)
62 .map(AudioControlDescriptor::HeaderV1),
63 UAC2_BCD_ADC => self
64 .parse_ac_header(data)
65 .map(AudioControlDescriptor::HeaderV2),
66 version => Err(DescriptorError::Invalid(format!(
67 "unsupported AudioControl version 0x{version:04x}"
68 ))),
69 }
70 }
71
72 pub fn parse_input_terminal(&self, data: &[u8]) -> Result<InputTerminal, DescriptorError> {
73 const LEN: usize = 17;
74 let data = descriptor_bytes(data, LEN)?;
75 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC2_INPUT_TERMINAL {
76 return Err(DescriptorError::Invalid("not input terminal".to_string()));
77 }
78 let t = InputTerminal {
79 b_terminal_id: data[3],
80 w_terminal_type: read_u16_le(data, 4)?,
81 b_assoc_terminal: data[6],
82 b_c_source_id: data[7],
83 b_nr_channels: data[8] as u16,
84 w_channel_config: read_u32_le(data, 9)?,
85 i_terminal: data[16],
86 };
87 validate_input_terminal(&t)?;
88 Ok(t)
89 }
90
91 pub fn parse_output_terminal(&self, data: &[u8]) -> Result<OutputTerminal, DescriptorError> {
92 const LEN: usize = 12;
93 let data = descriptor_bytes(data, LEN)?;
94 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC2_OUTPUT_TERMINAL {
95 return Err(DescriptorError::Invalid("not output terminal".to_string()));
96 }
97 let t = OutputTerminal {
98 b_terminal_id: data[3],
99 w_terminal_type: read_u16_le(data, 4)?,
100 b_assoc_terminal: data[6],
101 b_source_id: data[7],
102 i_terminal: data[11],
103 };
104 validate_output_terminal(&t)?;
105 Ok(t)
106 }
107
108 pub fn parse_feature_unit(&self, data: &[u8]) -> Result<FeatureUnit, DescriptorError> {
109 const MIN_LEN: usize = 10;
110 let data = descriptor_bytes(data, MIN_LEN)?;
111 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC2_FEATURE_UNIT {
112 return Err(DescriptorError::Invalid("not feature unit".to_string()));
113 }
114 let len = data[0] as usize;
115 if len < MIN_LEN || (len - 6) % 4 != 0 {
116 return Err(DescriptorError::Invalid(
117 "invalid feature unit length".to_string(),
118 ));
119 }
120 let n = (len - 6) / 4;
121 require_len(data, len)?;
122 let bma_controls: Vec<u32> = (0..n)
123 .map(|i| read_u32_le(data, 5 + i * 4))
124 .collect::<Result<_, _>>()?;
125 let f = FeatureUnit {
126 b_unit_id: data[3],
127 b_source_id: data[4],
128 b_control_size: 4,
130 bma_controls,
131 };
132 validate_feature_unit(&f)?;
133 Ok(f)
134 }
135
136 pub fn parse_input_terminal_v1(&self, data: &[u8]) -> Result<InputTerminal, DescriptorError> {
137 let data = descriptor_bytes(data, 12)?;
138 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC_INPUT_TERMINAL {
139 return Err(DescriptorError::Invalid(
140 "not UAC1 input terminal".to_string(),
141 ));
142 }
143 let terminal = InputTerminal {
144 b_terminal_id: data[3],
145 w_terminal_type: read_u16_le(data, 4)?,
146 b_assoc_terminal: data[6],
147 b_c_source_id: 0,
148 b_nr_channels: data[7] as u16,
149 w_channel_config: read_u16_le(data, 8)? as u32,
150 i_terminal: data[11],
151 };
152 validate_input_terminal(&terminal)?;
153 Ok(terminal)
154 }
155
156 pub fn parse_output_terminal_v1(&self, data: &[u8]) -> Result<OutputTerminal, DescriptorError> {
157 let data = descriptor_bytes(data, 9)?;
158 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC_OUTPUT_TERMINAL {
159 return Err(DescriptorError::Invalid(
160 "not UAC1 output terminal".to_string(),
161 ));
162 }
163 let terminal = OutputTerminal {
164 b_terminal_id: data[3],
165 w_terminal_type: read_u16_le(data, 4)?,
166 b_assoc_terminal: data[6],
167 b_source_id: data[7],
168 i_terminal: data[8],
169 };
170 validate_output_terminal(&terminal)?;
171 Ok(terminal)
172 }
173
174 pub fn parse_feature_unit_v1(&self, data: &[u8]) -> Result<FeatureUnit, DescriptorError> {
175 let data = descriptor_bytes(data, 7)?;
176 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC_FEATURE_UNIT {
177 return Err(DescriptorError::Invalid(
178 "not UAC1 feature unit".to_string(),
179 ));
180 }
181 let len = data[0] as usize;
182 let size = data[5] as usize;
183 if !(1..=4).contains(&size) || len < 7 + size || (len - 7) % size != 0 {
184 return Err(DescriptorError::Invalid(
185 "invalid UAC1 feature unit length".to_string(),
186 ));
187 }
188 require_len(data, len)?;
189 let bma_controls = data[6..len - 1]
190 .chunks_exact(size)
191 .map(|bytes| {
192 let mut word = [0u8; 4];
193 word[..size].copy_from_slice(bytes);
194 u32::from_le_bytes(word)
195 })
196 .collect();
197 let unit = FeatureUnit {
198 b_unit_id: data[3],
199 b_source_id: data[4],
200 b_control_size: size as u8,
201 bma_controls,
202 };
203 validate_feature_unit(&unit)?;
204 Ok(unit)
205 }
206
207 pub fn parse_clock_source(&self, data: &[u8]) -> Result<ClockSource, DescriptorError> {
208 const LEN: usize = 8;
209 let data = descriptor_bytes(data, LEN)?;
210 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC2_CLOCK_SOURCE {
211 return Err(DescriptorError::Invalid("not clock source".to_string()));
212 }
213 Ok(ClockSource {
214 b_clock_id: data[3],
215 bm_attributes: data[4],
216 bm_controls: data[5],
217 b_assoc_terminal: data[6],
218 i_clock_source: data[7],
219 })
220 }
221
222 pub fn parse_clock_selector(&self, data: &[u8]) -> Result<ClockSelector, DescriptorError> {
223 const MIN_LEN: usize = 7;
224 let data = descriptor_bytes(data, MIN_LEN)?;
225 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC2_CLOCK_SELECTOR {
226 return Err(DescriptorError::Invalid("not clock selector".to_string()));
227 }
228 let len = data[0] as usize;
229 require_len(data, len)?;
230 let b_nr_in_pins = data[4];
231 let expected_len = 7 + b_nr_in_pins as usize;
232 if len < expected_len {
233 return Err(DescriptorError::Invalid(
234 "clock selector length mismatch".to_string(),
235 ));
236 }
237 let ba_c_source_id: Vec<u8> = data[5..5 + b_nr_in_pins as usize].to_vec();
238 let bm_controls_offset = 5 + b_nr_in_pins as usize;
239 let bm_controls = data.get(bm_controls_offset).copied().unwrap_or(0);
240 let i_clock_selector = data.get(bm_controls_offset + 1).copied().unwrap_or(0);
241 Ok(ClockSelector {
242 b_clock_id: data[3],
243 b_nr_in_pins,
244 ba_c_source_id,
245 bm_controls,
246 i_clock_selector,
247 })
248 }
249
250 pub fn parse_clock_multiplier(&self, data: &[u8]) -> Result<ClockMultiplier, DescriptorError> {
251 const LEN: usize = 7;
252 let data = descriptor_bytes(data, LEN)?;
253 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC2_CLOCK_MULTIPLIER {
254 return Err(DescriptorError::Invalid("not clock multiplier".to_string()));
255 }
256 Ok(ClockMultiplier {
257 b_clock_id: data[3],
258 b_c_source_id: data[4],
259 bm_controls: data[5],
260 i_clock_multiplier: data[6],
261 })
262 }
263
264 pub fn parse_mixer_unit(&self, data: &[u8]) -> Result<MixerUnit, DescriptorError> {
265 const MIN_LEN: usize = 10;
266 let data = descriptor_bytes(data, MIN_LEN)?;
267 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC_MIXER_UNIT {
268 return Err(DescriptorError::Invalid("not mixer unit".to_string()));
269 }
270 let len = data[0] as usize;
271 require_len(data, len)?;
272 let b_nr_in_pins = data[5];
273 let ba_source_id = checked_slice(data, 6, usize::from(b_nr_in_pins))?.to_vec();
275 let channels_offset = 6 + b_nr_in_pins as usize;
276 require_len(data, channels_offset + 7)?;
277 let b_nr_channels = read_u16_le(data, channels_offset)?;
278 let w_channel_config = read_u32_le(data, channels_offset + 2)?;
279 let i_mixer_offset = channels_offset + 6;
280 let i_mixer = data[i_mixer_offset];
281 let bm_controls =
282 checked_slice(data, i_mixer_offset + 1, len - (i_mixer_offset + 1))?.to_vec();
283 Ok(MixerUnit {
284 b_unit_id: data[3],
285 b_nr_in_pins,
286 ba_source_id,
287 b_nr_channels,
288 w_channel_config,
289 i_mixer,
290 bm_controls,
291 })
292 }
293
294 pub fn parse_selector_unit(&self, data: &[u8]) -> Result<SelectorUnit, DescriptorError> {
295 const MIN_LEN: usize = 6;
296 let data = descriptor_bytes(data, MIN_LEN)?;
297 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC_SELECTOR_UNIT {
298 return Err(DescriptorError::Invalid("not selector unit".to_string()));
299 }
300 let len = data[0] as usize;
301 require_len(data, len)?;
302 let b_nr_in_pins = data[5];
303 let ba_source_id = checked_slice(data, 6, usize::from(b_nr_in_pins))?.to_vec();
304 let i_sel_offset = 6 + b_nr_in_pins as usize;
305 require_len(data, i_sel_offset + 1)?;
306 let i_selector = data[i_sel_offset];
307 let bm_controls = data.get(i_sel_offset + 1).copied();
308 Ok(SelectorUnit {
309 b_unit_id: data[3],
310 b_nr_in_pins,
311 ba_source_id,
312 i_selector,
313 bm_controls,
314 })
315 }
316
317 pub fn parse_effect_unit(&self, data: &[u8]) -> Result<EffectUnit, DescriptorError> {
318 const LEN: usize = 8;
319 let data = descriptor_bytes(data, LEN)?;
320 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC_EFFECT_UNIT {
321 return Err(DescriptorError::Invalid("not effect unit".to_string()));
322 }
323 Ok(EffectUnit {
324 b_unit_id: data[3],
325 b_source_id: data[4],
326 b_effect_id: read_u16_le(data, 5)?,
327 i_effect: data[7],
328 })
329 }
330
331 pub fn parse_processing_unit(&self, data: &[u8]) -> Result<ProcessingUnit, DescriptorError> {
332 const MIN_LEN: usize = 14;
333 let data = descriptor_bytes(data, MIN_LEN)?;
334 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC_PROCESSING_UNIT {
335 return Err(DescriptorError::Invalid("not processing unit".to_string()));
336 }
337 let len = data[0] as usize;
338 require_len(data, len)?;
339 let b_nr_channels = read_u16_le(data, 7)?;
340 let w_channel_config = read_u32_le(data, 9)?;
341 let i_processing = data[13];
342 let bm_controls = checked_slice(data, 14, len - 14)?.to_vec();
343 Ok(ProcessingUnit {
344 b_unit_id: data[3],
345 b_source_id: data[4],
346 w_process_type: read_u16_le(data, 5)?,
347 b_nr_channels,
348 w_channel_config,
349 i_processing,
350 bm_controls,
351 })
352 }
353
354 pub fn parse_extension_unit(&self, data: &[u8]) -> Result<ExtensionUnit, DescriptorError> {
355 const MIN_LEN: usize = 13;
356 let data = descriptor_bytes(data, MIN_LEN)?;
357 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC_EXTENSION_UNIT {
358 return Err(DescriptorError::Invalid("not extension unit".to_string()));
359 }
360 let len = data[0] as usize;
361 require_len(data, len)?;
362 let b_nr_in_pins = data[7];
363 let ba_source_id = checked_slice(data, 8, usize::from(b_nr_in_pins))?.to_vec();
364 let ch_offset = 8 + usize::from(b_nr_in_pins);
365 require_len(data, ch_offset + 7)?;
366 let b_nr_channels = read_u16_le(data, ch_offset)?;
367 let w_channel_config = read_u32_le(data, ch_offset + 2)?;
368 let i_extension = data[ch_offset + 6];
369 let bm_controls = checked_slice(data, ch_offset + 7, len - (ch_offset + 7))?.to_vec();
370 Ok(ExtensionUnit {
371 b_unit_id: data[3],
372 w_extension_code: read_u16_le(data, 5)?,
373 b_nr_in_pins,
374 ba_source_id,
375 b_nr_channels,
376 w_channel_config,
377 i_extension,
378 bm_controls,
379 })
380 }
381}
382
383impl DescriptorParser for AudioControlParser {
384 type Output = AudioControlDescriptor;
385
386 fn parse(&self, data: &[u8]) -> Result<Self::Output, DescriptorError> {
387 if data.len() < 3 {
388 return Err(DescriptorError::Invalid("descriptor too short".to_string()));
389 }
390 let data = descriptor_bytes(data, 3)?;
391 if data[1] != USB_DT_CS_INTERFACE {
392 return Err(DescriptorError::Invalid("not CS interface".to_string()));
393 }
394 match data[2] {
395 UAC_AC_HEADER => self.detect_and_parse_ac_header(data),
396 UAC_INPUT_TERMINAL => self
397 .parse_input_terminal(data)
398 .map(AudioControlDescriptor::InputTerminal),
399 UAC_OUTPUT_TERMINAL => self
400 .parse_output_terminal(data)
401 .map(AudioControlDescriptor::OutputTerminal),
402 UAC_FEATURE_UNIT => self
403 .parse_feature_unit(data)
404 .map(AudioControlDescriptor::FeatureUnit),
405 UAC2_CLOCK_SOURCE => self
406 .parse_clock_source(data)
407 .map(AudioControlDescriptor::ClockSource),
408 UAC2_CLOCK_SELECTOR => self
409 .parse_clock_selector(data)
410 .map(AudioControlDescriptor::ClockSelector),
411 UAC2_CLOCK_MULTIPLIER => self
412 .parse_clock_multiplier(data)
413 .map(AudioControlDescriptor::ClockMultiplier),
414 UAC_MIXER_UNIT => self
415 .parse_mixer_unit(data)
416 .map(AudioControlDescriptor::MixerUnit),
417 UAC_SELECTOR_UNIT => self
418 .parse_selector_unit(data)
419 .map(AudioControlDescriptor::SelectorUnit),
420 UAC_EFFECT_UNIT => self
421 .parse_effect_unit(data)
422 .map(AudioControlDescriptor::EffectUnit),
423 UAC_PROCESSING_UNIT => self
424 .parse_processing_unit(data)
425 .map(AudioControlDescriptor::ProcessingUnit),
426 UAC_EXTENSION_UNIT => self
427 .parse_extension_unit(data)
428 .map(AudioControlDescriptor::ExtensionUnit),
429 _ => Err(DescriptorError::Invalid(format!(
430 "unknown AC descriptor subtype {}",
431 data[2]
432 ))),
433 }
434 }
435}