usbdac-core 0.1.21

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
//! Standard UAC1/UAC2 adaptive capture explicit host-to-device rate endpoints.
use super::adc3::FeedforwardEndpoint;
use super::endpoint_geometry::ss_feedback_geometry;
use super::service_interval_micros;
use rusb::Speed;

/// `descriptors` must contain the descriptors of one AS alternate, excluding
/// its interface header. Return the feedback descriptor's index in that slice.
pub(super) fn find_standard_capture_feedforward(
    protocol: u8,
    data: &[u8],
    descriptors: &[&[u8]],
    speed: Speed,
) -> Result<(FeedforwardEndpoint, usize), String> {
    if protocol == 0 {
        return find_uac1_capture_feedforward(data, descriptors, speed);
    }
    if protocol != 0x20 {
        return Err("standard capture feedforward requires UAC1 or UAC2".into());
    }
    if !matches!(
        speed,
        Speed::Full | Speed::High | Speed::Super | Speed::SuperPlus
    ) {
        return Err("UAC2 capture feedforward requires a recognized isochronous USB speed".into());
    }
    // ADC2 4.10.1/4.10.2 use exactly seven-byte standard endpoint descriptors;
    // an ADC1 bRefresh/bSynchAddress tail must never be silently interpreted.
    if !standard_endpoint(data) || data[2] & 0x80 == 0 || data[3] != 0x09 {
        return Err("UAC2 feedforward requires an Adaptive/Data isochronous IN endpoint".into());
    }
    if !valid_data_packet(data, speed) {
        return Err("UAC2 capture data endpoint has invalid packet capacity".into());
    }
    let (index, report) = same_alternate_rate_endpoint(data, descriptors, "UAC2")?;
    // ADC2 3.16.2 adopts USB2 9.6.6's numbering scheme: the first data
    // endpoint and its feedback pipe have the same number, opposite direction.
    // Exactly one data endpoint is admitted here, so that association is unique.
    if !standard_endpoint(report) || report[3] != 0x11 || report[2] != (data[2] ^ 0x80) {
        return Err(
            "UAC2 capture feedforward requires opposite matching Isochronous/NoSync/Feedback OUT"
                .into(),
        );
    }
    let bytes = usize::from(u16::from_le_bytes([report[4], report[5]]));
    let service_us = match speed {
        Speed::Full if bytes == 3 => service_interval_micros(speed, report[6]),
        Speed::High if bytes == 4 => service_interval_micros(speed, report[6]),
        Speed::Super | Speed::SuperPlus => {
            // Only descriptors belonging to this endpoint may prove capacity.
            // ss_feedback_geometry requires an immediate, unique unburst
            // companion and exact four-byte legacy/eight-byte enhanced capacity.
            let end = descriptors[index + 1..]
                .iter()
                .position(|d| d[1] == 5)
                .map_or(descriptors.len(), |offset| index + 1 + offset);
            let extra: Vec<u8> = descriptors[index + 1..end]
                .iter()
                .flat_map(|d| d.iter().copied())
                .collect();
            ss_feedback_geometry(bytes as u16, report[6], &extra, true)?.1
        }
        _ => return Err("UAC2 capture feedforward has invalid fixed-point packet size".into()),
    };
    Ok((
        FeedforwardEndpoint {
            address: report[2],
            interval: report[6],
            service_us,
            bytes,
            full_speed: speed == Speed::Full,
            refresh: None,
        },
        index,
    ))
}

// ADC1 Table 4-20 fixes Full Speed data bInterval at one; the generic
// High Speed path permits USB2 data intervals 1..16.
// ADC1 3.7.2.2 associates the adaptive source with its explicit rate
// endpoint by bSynchAddress, rather than ADC2's endpoint-number convention.
fn find_uac1_capture_feedforward(
    data: &[u8],
    descriptors: &[&[u8]],
    speed: Speed,
) -> Result<(FeedforwardEndpoint, usize), String> {
    if !matches!(speed, Speed::Full | Speed::High) {
        return Err("UAC1 capture feedforward requires Full or High Speed".into());
    }
    if !uac1_endpoint(data)
        || data[2] & 0x80 == 0
        || data[3] != 0x09
        || (speed == Speed::Full && data[6] != 1)
        || data[7] != 0
        || !valid_out_address(data[8])
    {
        return Err("UAC1 feedforward requires Adaptive/Data IN with explicit OUT bSynchAddress and zero bRefresh".into());
    }
    if !valid_data_packet(data, speed) {
        return Err("UAC1 capture data endpoint has invalid packet capacity".into());
    }
    let (index, report) = same_alternate_rate_endpoint(data, descriptors, "UAC1")?;
    let bytes = if speed == Speed::Full { 3 } else { 4 };
    // ADC1 4.6.2: the synchronization endpoint uses NoSync isochronous
    // attributes, bInterval=1, bRefresh 1..9, and no further bSynchAddress.
    // At High Speed retain ALSA's generic UAC1 four-byte Q16.16 convention:
    // the rate unit and bRefresh exponent are microframes, not milliseconds.
    if !uac1_endpoint(report)
        || report[2] != data[8]
        || report[3] != 0x01
        || u16::from_le_bytes([report[4], report[5]]) != bytes as u16
        || report[6] != 1
        || !(1..=9).contains(&report[7])
        || report[8] != 0
    {
        return Err(
            "UAC1 capture feedforward requires the exact linked synchronization OUT endpoint"
                .into(),
        );
    }
    Ok((
        FeedforwardEndpoint {
            address: report[2],
            interval: report[6],
            service_us: service_interval_micros(speed, report[6]),
            bytes,
            full_speed: speed == Speed::Full,
            refresh: Some(report[7]),
        },
        index,
    ))
}

fn valid_data_packet(data: &[u8], speed: Speed) -> bool {
    let packet = u16::from_le_bytes([data[4], data[5]]);
    let base = packet & 0x07ff;
    match speed {
        Speed::Full => packet == base && (1..=1023).contains(&base),
        Speed::High => {
            packet & 0xe000 == 0 && packet & 0x1800 != 0x1800 && (1..=1024).contains(&base)
        }
        Speed::Super | Speed::SuperPlus => (1..=1024).contains(&packet),
        _ => false,
    }
}

fn same_alternate_rate_endpoint<'a>(
    data: &[u8],
    descriptors: &[&'a [u8]],
    protocol: &str,
) -> Result<(usize, &'a [u8]), String> {
    let mut data_occurrences = 0;
    let mut endpoint_count = 0;
    let mut report = None;
    for (index, descriptor) in descriptors.iter().copied().enumerate() {
        if descriptor.len() < 2
            || usize::from(descriptor[0]) != descriptor.len()
            || matches!(descriptor[1], 2 | 4)
        {
            return Err(format!(
                "{protocol} feedforward requires a well-formed single-alternate descriptor slice"
            ));
        }
        if descriptor[1] == 5 {
            endpoint_count += 1;
            if descriptor == data {
                data_occurrences += 1;
            } else {
                report = Some((index, descriptor));
            }
        }
    }
    if data_occurrences != 1 || endpoint_count != 2 {
        return Err(format!("{protocol} feedforward requires exactly one matching data endpoint and two same-alternate endpoints"));
    }
    report.ok_or_else(|| format!("{protocol} capture feedforward endpoint missing"))
}

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

fn uac1_endpoint(d: &[u8]) -> bool {
    d.len() == 9
        && d[0] == 9
        && d[1] == 5
        && d[2] & 0x70 == 0
        && d[2] & 0x0f != 0
        && (1..=16).contains(&d[6])
}

fn standard_endpoint(d: &[u8]) -> bool {
    d.len() == 7
        && d[0] == 7
        && d[1] == 5
        && d[2] & 0x70 == 0
        && d[2] & 0x0f != 0
        && (1..=16).contains(&d[6])
}

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

    const DATA: [u8; 7] = [7, 5, 0x81, 0x09, 192, 0, 1];
    const REPORT: [u8; 7] = [7, 5, 1, 0x11, 3, 0, 4];

    const UAC1_DATA: [u8; 9] = [9, 5, 0x81, 0x09, 192, 0, 1, 0, 2];
    const UAC1_REPORT: [u8; 9] = [9, 5, 2, 0x01, 3, 0, 1, 4, 0];

    #[test]
    fn uac1_capture_links_an_independently_numbered_rate_pipe_and_refresh() {
        let class_specific = [3, 0x25, 1];
        for refresh in 1..=9 {
            let mut report = UAC1_REPORT;
            report[7] = refresh;
            let (pipe, index) = find_standard_capture_feedforward(
                0,
                &UAC1_DATA,
                &[&class_specific, &report, &UAC1_DATA],
                Speed::Full,
            )
            .unwrap();
            assert_eq!(index, 1);
            assert_eq!(
                pipe,
                FeedforwardEndpoint {
                    address: 2,
                    interval: 1,
                    service_us: 1000,
                    bytes: 3,
                    full_speed: true,
                    refresh: Some(refresh)
                }
            );
            assert_eq!(pipe.nominal(48_000).unwrap(), [0, 0, 12]);
            assert_eq!(pipe.nominal(44_100).unwrap(), [0x66, 0x06, 0x0b]);
        }
        let mut report = UAC1_REPORT;
        report[4] = 4;
        let mut hs_data = UAC1_DATA;
        hs_data[6] = 4;
        let (pipe, _) =
            find_standard_capture_feedforward(0, &hs_data, &[&hs_data, &report], Speed::High)
                .unwrap();
        assert_eq!(
            (pipe.bytes, pipe.service_us, pipe.refresh),
            (4, 125, Some(4))
        );
        assert_eq!(pipe.nominal(48_000).unwrap(), [0, 0, 6, 0]);
        assert_eq!(pipe.nominal(44_100).unwrap(), [0x33, 0x83, 5, 0]);
    }

    #[test]
    fn uac1_capture_rejects_unproven_pairs_and_malformed_descriptor_tails() {
        let parse = |data: &[u8], descriptors: &[&[u8]]| {
            find_standard_capture_feedforward(0, data, descriptors, Speed::Full)
        };
        assert!(parse(&UAC1_DATA, &[&UAC1_REPORT]).is_err());
        assert!(parse(&UAC1_DATA, &[&UAC1_DATA, &UAC1_DATA, &UAC1_REPORT]).is_err());
        assert!(parse(&UAC1_DATA, &[&UAC1_DATA, &UAC1_REPORT, &UAC1_REPORT]).is_err());
        assert!(parse(
            &UAC1_DATA,
            &[&UAC1_DATA, &[9, 4, 1, 1, 2, 1, 2, 0, 0], &UAC1_REPORT]
        )
        .is_err());
        assert!(parse(&UAC1_DATA, &[&UAC1_DATA, &[3, 0x25], &UAC1_REPORT]).is_err());
        let mut other = UAC1_DATA;
        other[4] += 1;
        assert!(parse(&other, &[&UAC1_DATA, &UAC1_REPORT]).is_err());
        for (field, value) in [
            (0, 7),
            (1, 0x25),
            (2, 0),
            (2, 0x80),
            (2, 0x91),
            (2, 1),
            (3, 0x05),
            (3, 0x29),
            (4, 0),
            (5, 8),
            (6, 0),
            (6, 2),
            (6, 17),
            (7, 1),
            (8, 0),
            (8, 0x80),
            (8, 0x82),
            (8, 0x12),
        ] {
            let mut bad = UAC1_DATA;
            bad[field] = value;
            assert!(parse(&bad, &[&bad, &UAC1_REPORT]).is_err(), "data {bad:?}");
        }
        for (field, value) in [
            (0, 7),
            (1, 0x25),
            (2, 0),
            (2, 1),
            (2, 0x82),
            (2, 0x12),
            (3, 0x11),
            (3, 0x05),
            (3, 0x09),
            (4, 4),
            (5, 8),
            (6, 0),
            (6, 2),
            (7, 0),
            (7, 10),
            (8, 1),
        ] {
            let mut bad = UAC1_REPORT;
            bad[field] = value;
            assert!(
                parse(&UAC1_DATA, &[&UAC1_DATA, &bad]).is_err(),
                "report {bad:?}"
            );
        }
        assert!(parse(&UAC1_DATA, &[&UAC1_DATA, &REPORT]).is_err());
        for speed in [
            Speed::Unknown,
            Speed::Low,
            Speed::Super,
            Speed::SuperPlus,
            Speed::High,
        ] {
            assert!(find_standard_capture_feedforward(
                0,
                &UAC1_DATA,
                &[&UAC1_DATA, &UAC1_REPORT],
                speed
            )
            .is_err());
        }
        let mut report = UAC1_REPORT;
        report[4] = 4;
        let mut bad_data = UAC1_DATA;
        bad_data[5] = 0x18;
        assert!(find_standard_capture_feedforward(
            0,
            &bad_data,
            &[&bad_data, &report],
            Speed::High
        )
        .is_err());
    }

    #[test]
    fn uac2_capture_rate_pipe_retains_its_own_interval_and_raw_index() {
        let class_specific = [3, 0x24, 1];
        let descriptors = [&class_specific[..], &DATA, &REPORT];
        let (pipe, index) =
            find_standard_capture_feedforward(0x20, &DATA, &descriptors, Speed::Full).unwrap();
        assert_eq!(index, 2);
        assert_eq!(
            pipe,
            FeedforwardEndpoint {
                address: 1,
                interval: 4,
                service_us: 8000,
                bytes: 3,
                full_speed: true,
                refresh: None
            }
        );
        assert_eq!(pipe.nominal(48_000).unwrap(), [0, 0, 12]);
        let mut report = REPORT;
        report[4] = 4;
        let (pipe, _) =
            find_standard_capture_feedforward(0x20, &DATA, &[&report, &DATA], Speed::High).unwrap();
        assert_eq!(pipe.service_us, 1000);
        assert_eq!(pipe.nominal(48_000).unwrap(), [0, 0, 6, 0]);
    }

    #[test]
    fn uac2_capture_requires_exact_proven_same_alternate_endpoint_pair() {
        let parse = |data: &[u8], descriptors: &[&[u8]]| {
            find_standard_capture_feedforward(0x20, data, descriptors, Speed::Full)
        };
        assert!(parse(&DATA, &[&REPORT]).is_err());
        assert!(parse(&DATA, &[&DATA, &DATA, &REPORT]).is_err());
        assert!(parse(&DATA, &[&DATA, &REPORT, &REPORT]).is_err());
        assert!(parse(&DATA, &[&DATA, &[9, 4, 1, 1, 2, 1, 2, 0x20, 0], &REPORT]).is_err());
        assert!(parse(&DATA, &[&DATA, &[3, 0x24], &REPORT]).is_err());
        let mut other = DATA;
        other[4] += 1;
        assert!(parse(&other, &[&DATA, &REPORT]).is_err());
        for (field, value) in [
            (0, 8),
            (1, 0x25),
            (2, 0),
            (2, 0x11),
            (2, 0x81),
            (2, 2),
            (3, 0x01),
            (3, 0x15),
            (4, 4),
            (5, 8),
            (6, 0),
            (6, 17),
        ] {
            let mut bad = REPORT;
            bad[field] = value;
            assert!(parse(&DATA, &[&DATA, &bad]).is_err(), "{bad:?}");
        }
        for (field, value) in [
            (0, 8),
            (2, 1),
            (2, 0x80),
            (2, 0x91),
            (3, 0x05),
            (3, 0x29),
            (4, 0),
            (5, 8),
            (6, 0),
            (6, 17),
        ] {
            let mut bad = DATA;
            bad[field] = value;
            assert!(parse(&bad, &[&bad, &REPORT]).is_err(), "{bad:?}");
        }
        let extended = [9, 5, 1, 0x11, 3, 0, 4, 0, 0];
        assert!(parse(&DATA, &[&DATA, &extended]).is_err());
        for protocol in [0, 0x30, 0xff] {
            assert!(find_standard_capture_feedforward(
                protocol,
                &DATA,
                &[&DATA, &REPORT],
                Speed::Full
            )
            .is_err());
        }
        for speed in [Speed::Unknown, Speed::Low, Speed::High] {
            assert!(
                find_standard_capture_feedforward(0x20, &DATA, &[&DATA, &REPORT], speed).is_err()
            );
        }
    }

    #[test]
    fn superspeed_capture_rate_pipe_requires_its_own_exact_unburst_companion() {
        for speed in [Speed::Super, Speed::SuperPlus] {
            for bytes in [4, 8] {
                let mut report = REPORT;
                report[4] = bytes;
                let companion = [6, 0x30, 0, 0, bytes, 0];
                let (pipe, index) = find_standard_capture_feedforward(
                    0x20,
                    &DATA,
                    &[&DATA, &report, &companion],
                    speed,
                )
                .unwrap();
                assert_eq!(
                    (pipe.bytes, pipe.service_us, index),
                    (usize::from(bytes), 1000, 1)
                );
                for descriptors in [
                    vec![&DATA[..], &report],
                    vec![&DATA[..], &companion, &report],
                    vec![&DATA[..], &report, &companion, &companion],
                ] {
                    assert!(
                        find_standard_capture_feedforward(0x20, &DATA, &descriptors, speed)
                            .is_err()
                    );
                }
                for (field, value) in [(2, 1), (3, 1), (3, 0x80), (4, bytes - 1), (5, 1)] {
                    let mut bad = companion;
                    bad[field] = value;
                    assert!(find_standard_capture_feedforward(
                        0x20,
                        &DATA,
                        &[&DATA, &report, &bad],
                        speed
                    )
                    .is_err());
                }
            }
        }
    }
}