Skip to main content

usbdac_core/
quirk.rs

1//! Per-device behaviour switches.
2//!
3//! The built-in table only holds devices that were verified on hardware in the
4//! project this library was extracted from. A host can add its own entries
5//! through [`DacConfig::quirks`](crate::DacConfig::quirks).
6
7/// One behaviour switch for a device. Only switches the transport actually
8/// honours are listed.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Quirk {
12    /// The clock readback is unreliable (GET_CUR/GET_RANGE return 0 or
13    /// fail). Trust SET_CUR for PCM and skip verification.
14    SkipClockValidation,
15    /// The DAC reports a rate that differs from the one just set (some report
16    /// 0 Hz) while playing correctly; do not refuse over the mismatch (PCM).
17    IgnoreInvalidSampleRate,
18    /// Refuse to stream unless the DAC confirms the sampling frequency, even
19    /// if [`DacConfig::require_verified_rate`](crate::DacConfig) is off.
20    RequireVerifiedRate,
21    /// Wait this many milliseconds after programming the clock.
22    SettleDelayMs(u64),
23    /// Prefer the 4-byte subslot over the 3-byte one for 24-bit PCM.
24    PreferPadded24BitTransport,
25    /// Override the subslot size (1, 2 or 4 bytes) of native DSD alternate
26    /// settings, for devices whose descriptors are wrong or ambiguous.
27    DsdSubslotSize(u8),
28    /// Native DSD words go on the wire most-significant byte first.
29    DsdBigEndian,
30    /// Reverse the bit order within each native DSD byte.
31    DsdBitReverse,
32    /// Decode four-byte high-speed feedback as frames per OUT service interval.
33    /// Opt in only with a device trace proving these units (e.g. Linux UAC2 gadget).
34    /// No built-in device enables this compatibility rule.
35    ExplicitFeedbackPerServiceInterval,
36}
37
38struct BuiltinEntry {
39    vendor_id: u16,
40    product_id: u16,
41    /// Empty matches any product name with this VID/PID.
42    product_name_contains: &'static str,
43    quirks: &'static [Quirk],
44}
45
46const BUILTIN: &[BuiltinEntry] = &[
47    // MOONDROP Dawn Pro: dual CS43131 behind an XMOS-style bridge, DSD_U32_BE.
48    BuiltinEntry {
49        vendor_id: 12230, // 0x2FC6
50        product_id: 61546, // 0xF09A
51        product_name_contains: "MOONDROP Dawn Pro",
52        quirks: &[
53            Quirk::RequireVerifiedRate,
54            Quirk::SettleDelayMs(50),
55            Quirk::PreferPadded24BitTransport,
56            Quirk::DsdSubslotSize(4),
57            Quirk::DsdBigEndian,
58        ],
59    },
60    // Fosi Audio DS2 (Savitech bridge): GET_RANGE returns no subranges and
61    // GET_CUR reads 0 Hz even after a successful SET_CUR, yet the DAC plays at
62    // the requested rate. Trust SET_CUR and skip readback validation.
63    BuiltinEntry {
64        vendor_id: 9770, // 0x262A
65        product_id: 1,
66        product_name_contains: "",
67        quirks: &[Quirk::SkipClockValidation, Quirk::IgnoreInvalidSampleRate],
68    },
69];
70
71/// The built-in quirks for a device, in table order.
72pub(crate) fn builtin_quirks(vendor_id: u16, product_id: u16, product_name: &str) -> Vec<Quirk> {
73    let mut result = Vec::new();
74    for entry in BUILTIN {
75        if entry.vendor_id == vendor_id
76            && entry.product_id == product_id
77            && (entry.product_name_contains.is_empty()
78                || product_name.contains(entry.product_name_contains))
79        {
80            result.extend_from_slice(entry.quirks);
81        }
82    }
83    result
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn dawn_pro_quirks_found_by_vid_pid() {
92        let quirks = builtin_quirks(12230, 61546, "MOONDROP Dawn Pro");
93        assert!(quirks.contains(&Quirk::RequireVerifiedRate));
94        assert!(quirks.contains(&Quirk::PreferPadded24BitTransport));
95        assert!(quirks.contains(&Quirk::DsdBigEndian));
96        assert!(quirks.contains(&Quirk::SettleDelayMs(50)));
97    }
98
99    #[test]
100    fn the_product_name_must_match_when_the_entry_names_one() {
101        assert!(builtin_quirks(12230, 61546, "Some Other Product").is_empty());
102    }
103
104    #[test]
105    fn unknown_device_returns_empty() {
106        assert!(builtin_quirks(0x0000, 0x0000, "Unknown").is_empty());
107    }
108
109    #[test]
110    fn fosi_ds2_quirks_registered_for_any_name() {
111        let quirks = builtin_quirks(9770, 1, "Fosi Audio DS2");
112        assert!(quirks.contains(&Quirk::SkipClockValidation));
113        assert!(quirks.contains(&Quirk::IgnoreInvalidSampleRate));
114        assert!(!builtin_quirks(9770, 1, "").is_empty());
115    }
116}