usbdac-core 0.1.8

Direct USB DAC playback for Rust audio applications on Android and Linux, with PCM, DoP, native DSD and bit-perfect reporting.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! UAC1 interrupt status decoding and scoped active-route verification.

use crate::error::Error;
use std::collections::HashSet;

#[derive(Clone, Debug)]
pub(super) struct StreamWatch {
    pub(super) interface: u8,
    pub(super) alternate: u8,
    pub(super) endpoint: u8,
    pub(super) rate: Option<u32>,
    pub(super) terminal: Option<u8>,
    /// Every endpoint in the selected alternate, including synchronization pipes.
    pub(super) endpoints: Vec<u8>,
}

struct Collection {
    streams: HashSet<u8>,
    entities: HashSet<u8>,
    terminals: HashSet<u8>,
}

fn collection(extra: &[u8]) -> Option<Collection> {
    let mut header = None;
    let mut entities = HashSet::new();
    let mut terminals = HashSet::new();
    let mut offset = 0;
    let mut class_length = 0;
    while offset < extra.len() {
        let length = usize::from(*extra.get(offset)?);
        if length < 2 {
            return None;
        }
        let descriptor = extra.get(offset..offset.checked_add(length)?)?;
        offset += length;
        if descriptor[1] != 0x24 {
            continue;
        }
        class_length += length;
        match *descriptor.get(2)? {
            1 => {
                if header.is_some()
                    || descriptor.len() < 8
                    || descriptor[3..5] != [0, 1]
                    || descriptor.len() != 8 + usize::from(descriptor[7])
                {
                    return None;
                }
                let streams: HashSet<_> = descriptor[8..].iter().copied().collect();
                if streams.len() != descriptor[8..].len() {
                    return None;
                }
                header = Some((
                    streams,
                    usize::from(u16::from_le_bytes([descriptor[5], descriptor[6]])),
                ));
            }
            subtype @ 2..=8 => {
                let id = *descriptor.get(3)?;
                if id == 0 || !entities.insert(id) {
                    return None;
                }
                if matches!(subtype, 2 | 3) {
                    terminals.insert(id);
                }
            }
            _ => {}
        }
    }
    let (streams, total) = header?;
    if total != class_length {
        return None;
    }
    Some(Collection {
        streams,
        entities,
        terminals,
    })
}

/// A streaming interface belongs to exactly one UAC1 AC alternate-zero header.
pub(super) fn collection_owner(config: &rusb::ConfigDescriptor, stream: u8) -> Option<u8> {
    let mut owner = None;
    for interface in config.interfaces() {
        for ac in interface.descriptors().filter(|ac| {
            ac.setting_number() == 0
                && ac.class_code() == 1
                && ac.sub_class_code() == 1
                && ac.protocol_code() == 0
        }) {
            if collection(ac.extra()).is_some_and(|collection| collection.streams.contains(&stream))
            {
                if owner.is_some() {
                    return None;
                }
                owner = Some(ac.interface_number());
            }
        }
    }
    owner
}

pub(super) struct Uac1Status {
    control: u8,
    entities: HashSet<u8>,
    streams: Vec<StreamWatch>,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum Origin {
    Entity(u8),
    Interface(u8),
    Endpoint(u8),
}

fn endpoint_address(address: u8) -> bool {
    address & 0x70 == 0 && address & 0x0f != 0
}

impl Uac1Status {
    pub(super) fn new(control: u8, ac_extra: &[u8], streams: Vec<StreamWatch>) -> Option<Self> {
        let collection = collection(ac_extra)?;
        let mut interfaces = HashSet::new();
        let mut endpoints = HashSet::new();
        if streams.is_empty() {
            return None;
        }
        for stream in &streams {
            if !collection.streams.contains(&stream.interface)
                || stream.interface == control
                || !interfaces.insert(stream.interface)
                || stream.alternate == 0
                || !stream.endpoints.contains(&stream.endpoint)
                || stream
                    .rate
                    .is_some_and(|rate| rate == 0 || rate > 0x7f_ffff)
                || stream
                    .terminal
                    .is_some_and(|terminal| !collection.terminals.contains(&terminal))
                || stream
                    .endpoints
                    .iter()
                    .any(|&endpoint| !endpoint_address(endpoint) || !endpoints.insert(endpoint))
            {
                return None;
            }
        }
        Some(Self {
            control,
            entities: collection.entities,
            streams,
        })
    }

    fn origin(&self, status: u8, code: u8) -> Option<Origin> {
        // D7 is Interrupt Pending; D6 is the unsupported private-memory path.
        if status & 0xf0 != 0x80 {
            return None;
        }
        match status & 0x0f {
            0 if code == 0 || self.entities.contains(&code) => Some(Origin::Entity(code)),
            1 if self.streams.iter().any(|stream| stream.interface == code) => {
                Some(Origin::Interface(code))
            }
            2 if endpoint_address(code) => {
                let mut addresses = self
                    .streams
                    .iter()
                    .flat_map(|stream| &stream.endpoints)
                    .copied()
                    .filter(|&address| {
                        if code & 0x80 != 0 {
                            address == code
                        } else {
                            address & 0x0f == code
                        }
                    });
                let address = addresses.next()?;
                if addresses.next().is_some() {
                    return None;
                }
                Some(Origin::Endpoint(address))
            }
            _ => None,
        }
    }

    pub(super) fn poll(
        &self,
        packet: &[u8],
        mut read: impl FnMut(u8, u8, u16, u16, usize) -> Result<Vec<u8>, Error>,
    ) -> Result<(), Error> {
        // Reject the whole malformed packet before issuing any control request.
        if packet.len() % 2 != 0 {
            return Ok(());
        }
        let mut seen = HashSet::new();
        let mut verify = HashSet::new();
        let mut notices = Vec::new();
        for record in packet.chunks_exact(2) {
            let Some(origin) = self.origin(record[0], record[1]) else {
                continue;
            };
            if !seen.insert(origin) {
                continue;
            }
            let affected: Vec<_> = self
                .streams
                .iter()
                .enumerate()
                .filter_map(|(index, stream)| {
                    let active = match origin {
                        Origin::Entity(entity) => entity == 0 || stream.terminal == Some(entity),
                        Origin::Interface(interface) => stream.interface == interface,
                        Origin::Endpoint(endpoint) => stream.endpoints.contains(&endpoint),
                    };
                    active.then_some(index)
                })
                .collect();
            notices.push((origin, affected));
        }
        // Complete active proofs before optional unrelated acknowledgements can
        // consume the interrupt handler's bounded control-transfer budget.
        let mut optional = Vec::new();
        for (origin, affected) in notices {
            if affected.is_empty() {
                optional.push(origin);
                continue;
            }
            let (request_type, index) = match origin {
                Origin::Entity(entity) => (0xa1, u16::from(self.control) | u16::from(entity) << 8),
                Origin::Interface(interface) => (0xa1, u16::from(interface)),
                Origin::Endpoint(endpoint) => (0xa2, u16::from(endpoint)),
            };
            // UAC1 5.2.4.2 reserves the data and currently specifies a null reply.
            // Keep a data stage in the request; accept a short (including null) reply.
            let cleared = read(request_type, 0xff, 0, index, 2).and_then(|data| {
                if data.len() <= 2 {
                    Ok(())
                } else {
                    Err(Error::Control(
                        "UAC1 GET_STAT returned oversized data".into(),
                    ))
                }
            });
            match cleared {
                Err(Error::DeviceLost) => return Err(Error::DeviceLost),
                Err(error) if !affected.is_empty() => return Err(error),
                _ => {}
            }
            verify.extend(affected);
        }
        // Preserve stream ordering, independent of hash iteration order.
        for (index, stream) in self.streams.iter().enumerate() {
            if !verify.contains(&index) {
                continue;
            }
            if read(0x81, 0x0a, 0, u16::from(stream.interface), 1)? != [stream.alternate] {
                return Err(Error::Control("UAC1 active alternate changed".into()));
            }
            if let Some(expected) = stream.rate {
                let endpoint = u16::from(stream.endpoint);
                let result = read(0xa2, 0x81, 0x0100, endpoint, 3);
                let data = match result {
                    Err(Error::DeviceLost) => return Err(Error::DeviceLost),
                    Ok(data)
                        if data.len() == 3
                            && (1..=0x7f_ffff)
                                .contains(&u32::from_le_bytes([data[0], data[1], data[2], 0])) =>
                    {
                        data
                    }
                    _ if stream.interface != 0 => read(
                        0xa2,
                        0x81,
                        0x0100,
                        endpoint | u16::from(stream.interface) << 8,
                        3,
                    )?,
                    Err(error) => return Err(error),
                    Ok(data) => data,
                };
                if data.len() != 3 || u32::from_le_bytes([data[0], data[1], data[2], 0]) != expected
                {
                    return Err(Error::Control(
                        "UAC1 active sampling frequency changed or could not be verified".into(),
                    ));
                }
            }
        }
        for origin in optional {
            let Origin::Entity(entity) = origin else {
                continue;
            };
            if let Err(Error::DeviceLost) = read(
                0xa1,
                0xff,
                0,
                u16::from(self.control) | u16::from(entity) << 8,
                2,
            ) {
                return Err(Error::DeviceLost);
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn extra() -> Vec<u8> {
        // One AS interface, two terminals and an unrelated Feature Unit.
        vec![
            9, 0x24, 1, 0, 1, 21, 0, 1, 2, 4, 0x24, 2, 5, 4, 0x24, 3, 6, 4, 0x24, 6, 7,
        ]
    }

    fn stream() -> StreamWatch {
        StreamWatch {
            interface: 2,
            alternate: 1,
            endpoint: 1,
            rate: Some(48_000),
            terminal: Some(5),
            endpoints: vec![1, 0x82],
        }
    }

    fn target() -> Uac1Status {
        Uac1Status::new(0, &extra(), vec![stream()]).unwrap()
    }

    #[test]
    fn malformed_memory_reserved_and_unknown_notices_issue_no_requests() {
        for packet in [
            vec![0x80, 5, 0],
            vec![0xc0, 5],
            vec![0x90, 5],
            vec![0, 5],
            vec![0x80, 99],
            vec![0x81, 3],
            vec![0x82, 0x83],
            vec![0x83, 5],
        ] {
            target()
                .poll(&packet, |_, _, _, _, _| panic!("unexpected read"))
                .unwrap();
        }
    }

    #[test]
    fn null_status_and_reserved_payload_both_revalidate_once() {
        for payload in [vec![], vec![0xfe, 0xff]] {
            let mut calls = Vec::new();
            target()
                .poll(
                    &[0x80, 5, 0x80, 5, 0x81, 2],
                    |kind, request, value, index, length| {
                        calls.push((kind, request, value, index, length));
                        Ok(match request {
                            0xff => payload.clone(),
                            0x0a => vec![1],
                            0x81 => vec![0x80, 0xbb, 0],
                            _ => unreachable!(),
                        })
                    },
                )
                .unwrap();
            assert_eq!(
                calls,
                vec![
                    (0xa1, 0xff, 0, 0x500, 2),
                    (0xa1, 0xff, 0, 2, 2),
                    (0x81, 0x0a, 0, 2, 1),
                    (0xa2, 0x81, 0x100, 1, 3)
                ]
            );
        }
    }

    #[test]
    fn timing_endpoint_number_maps_to_full_address_but_reads_audio_rate() {
        let mut calls = Vec::new();
        target()
            .poll(&[0x82, 2, 0x82, 0x82], |kind, request, _, index, _| {
                calls.push((kind, request, index));
                Ok(match request {
                    0xff => vec![],
                    0x0a => vec![1],
                    _ => vec![0x80, 0xbb, 0],
                })
            })
            .unwrap();
        assert_eq!(
            calls,
            vec![(0xa2, 0xff, 0x82), (0x81, 0x0a, 2), (0xa2, 0x81, 1)]
        );
    }

    #[test]
    fn ambiguous_endpoint_number_is_ignored_but_full_in_address_is_exact() {
        let mut watch = stream();
        watch.endpoints.push(0x81);
        let target = Uac1Status::new(0, &extra(), vec![watch]).unwrap();
        target
            .poll(&[0x82, 1], |_, _, _, _, _| {
                panic!("ambiguous endpoint read")
            })
            .unwrap();
        let mut calls = 0;
        target
            .poll(&[0x82, 0x81], |_, request, _, _, _| {
                calls += 1;
                Ok(match request {
                    0xff => vec![],
                    0x0a => vec![1],
                    _ => vec![0x80, 0xbb, 0],
                })
            })
            .unwrap();
        assert_eq!(calls, 3);
    }

    #[test]
    fn unrelated_entity_is_optional_and_runs_after_active_proof() {
        let mut calls = Vec::new();
        target()
            .poll(&[0x80, 7, 0x80, 0], |_, request, _, index, _| {
                calls.push((request, index));
                if index == 0x700 {
                    return Err(Error::Control("unsupported".into()));
                }
                Ok(match request {
                    0xff => vec![],
                    0x0a => vec![1],
                    _ => vec![0x80, 0xbb, 0],
                })
            })
            .unwrap();
        assert_eq!(calls, vec![(0xff, 0), (0x0a, 2), (0x81, 1), (0xff, 0x700)]);
        assert_eq!(
            target().poll(&[0x80, 7], |_, _, _, _, _| Err(Error::DeviceLost)),
            Err(Error::DeviceLost)
        );
    }

    #[test]
    fn active_clear_alternate_and_rate_failures_invalidate() {
        assert!(target()
            .poll(&[0x81, 2], |_, _, _, _, _| Err(Error::Control(
                "stall".into()
            )))
            .is_err());
        for alternate in [vec![], vec![0], vec![1, 1]] {
            assert!(target()
                .poll(&[0x81, 2], |_, request, _, _, _| Ok(if request == 0xff {
                    vec![]
                } else {
                    alternate.clone()
                }))
                .is_err());
        }
        assert!(target()
            .poll(&[0x82, 1], |_, request, _, _, _| Ok(match request {
                0xff => vec![],
                0x0a => vec![1],
                _ => vec![0x44, 0xac, 0],
            }))
            .is_err());
    }

    #[test]
    fn rate_compatibility_fallback_never_hides_detach_or_a_valid_changed_rate() {
        let mut indexes = Vec::new();
        target()
            .poll(&[0x81, 2], |_, request, _, index, _| {
                indexes.push(index);
                match request {
                    0xff => Ok(vec![]),
                    0x0a => Ok(vec![1]),
                    _ if index == 1 => Err(Error::Control("stall".into())),
                    _ => Ok(vec![0x80, 0xbb, 0]),
                }
            })
            .unwrap();
        assert_eq!(indexes, vec![2, 2, 1, 0x201]);
        for invalid in [vec![], vec![0, 0, 0], vec![0, 0, 0x80]] {
            target()
                .poll(&[0x81, 2], |_, request, _, index, _| {
                    Ok(match request {
                        0xff => vec![],
                        0x0a => vec![1],
                        _ if index == 1 => invalid.clone(),
                        _ => vec![0x80, 0xbb, 0],
                    })
                })
                .unwrap();
        }
        let mut rate_reads = 0;
        assert_eq!(
            target().poll(&[0x81, 2], |_, request, _, _, _| {
                match request {
                    0xff => Ok(vec![]),
                    0x0a => Ok(vec![1]),
                    _ => {
                        rate_reads += 1;
                        Err(Error::DeviceLost)
                    }
                }
            }),
            Err(Error::DeviceLost)
        );
        assert_eq!(rate_reads, 1);
    }

    #[test]
    fn collection_and_watch_validation_fails_closed() {
        let mut bytes = extra();
        bytes[3] = 1;
        assert!(Uac1Status::new(0, &bytes, vec![stream()]).is_none());
        let mut watch = stream();
        watch.interface = 3;
        assert!(Uac1Status::new(0, &extra(), vec![watch]).is_none());
        let mut watch = stream();
        watch.terminal = Some(7);
        assert!(Uac1Status::new(0, &extra(), vec![watch]).is_none());
        let mut watch = stream();
        watch.rate = Some(0x80_0000);
        assert!(Uac1Status::new(0, &extra(), vec![watch]).is_none());
        let mut watch = stream();
        watch.endpoints.push(1);
        assert!(Uac1Status::new(0, &extra(), vec![watch]).is_none());
        assert!(Uac1Status::new(0, &extra(), vec![stream(), stream()]).is_none());
    }
}