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
7mod source;
8pub(crate) use source::resolve as source_quirks;
9pub use source::{AlsaQuirkFlag, QuirkPolicy, SourceQuirkEntry, SOURCE_QUIRKS};
10
11/// One behaviour switch for a device. Only switches the transport actually
12/// honours are listed.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[non_exhaustive]
15pub enum Quirk {
16    /// The clock readback is unreliable (GET_CUR/GET_RANGE return 0 or
17    /// fail). Permit acknowledged PCM SET in an unverified tier.
18    SkipClockValidation,
19    /// The DAC reports a rate that differs from the one just set (some report
20    /// 0 Hz) while playing correctly; zero remains unverified and positive mismatches are refused.
21    IgnoreInvalidSampleRate,
22    /// Refuse to stream unless the DAC confirms the sampling frequency, even
23    /// if [`DacConfig::require_verified_rate`](crate::DacConfig) is off.
24    RequireVerifiedRate,
25    /// Wait this many milliseconds after programming the clock.
26    SettleDelayMs(u64),
27    /// Prefer the 4-byte subslot over the 3-byte one for 24-bit PCM.
28    PreferPadded24BitTransport,
29    /// Override the subslot size (1, 2 or 4 bytes) of native DSD alternate
30    /// settings, for devices whose descriptors are wrong or ambiguous.
31    DsdSubslotSize(u8),
32    /// Native DSD words go on the wire most-significant byte first.
33    DsdBigEndian,
34    /// Reverse the bit order within each native DSD byte.
35    DsdBitReverse,
36    /// Decode four-byte high-speed feedback as frames per OUT service interval.
37    /// Opt in only with a device trace proving these units (e.g. Linux UAC2 gadget).
38    /// No built-in device enables this compatibility rule.
39    ExplicitFeedbackPerServiceInterval,
40    /// Wait after class-control requests, including failed replies.
41    ControlMessageDelayUs(u64),
42    /// Wait after a successful SET_INTERFACE.
43    InterfaceDelayMs(u64),
44    /// Activate the proven playback alternate before clock programming.
45    SetInterfaceFirst,
46    /// Start with twelve URBs where the 255-packet cap permits.
47    PlaybackUrbFixup,
48    /// Omit unreliable frequency GET_CUR; retain CLOCK_UNVERIFIED.
49    SkipSampleRateReadback,
50    /// Refuse implicit sources for this device.
51    SkipImplicitFeedback,
52    /// The verified raw minimum means silence on playback volume controls.
53    MixerPlaybackMinMute,
54    /// The playback raw range is linear amplitude from silence to unity.
55    MixerPlaybackLinearVolume,
56    /// Exact source-backed RAW_DATA UAC2 alternate uses 32-bit big-endian words.
57    RawDsd32Be,
58    /// Skip a rate write only after fixed descriptor and exact live rate proof.
59    FixedRate,
60    /// Replace at most one empty implicit completion with measured-cadence silence.
61    ImplicitSilenceOnEmpty,
62    /// Read current clock selector pins but never rewrite them.
63    SkipClockSelectorWrites,
64    /// Skip unusable optional mixer controls; never waive rate, attenuation or SET proof.
65    IgnoreOptionalControlErrors,
66    /// Exact Zoom standard PCM OUT profile: four-byte LE audio-length prefix.
67    TxLengthPrefix,
68    /// Start bounded nominal OUT silence before a proven implicit IN source.
69    PlaybackFirst,
70}
71
72struct BuiltinEntry {
73    vendor_id: u16,
74    product_id: u16,
75    /// Empty matches any product name with this VID/PID.
76    product_name_contains: &'static str,
77    quirks: &'static [Quirk],
78}
79
80const BUILTIN: &[BuiltinEntry] = &[
81    // MOONDROP Dawn Pro: dual CS43131 behind an XMOS-style bridge, DSD_U32_BE.
82    BuiltinEntry {
83        vendor_id: 12230,  // 0x2FC6
84        product_id: 61546, // 0xF09A
85        product_name_contains: "MOONDROP Dawn Pro",
86        quirks: &[
87            Quirk::RequireVerifiedRate,
88            Quirk::SettleDelayMs(50),
89            Quirk::PreferPadded24BitTransport,
90            Quirk::DsdSubslotSize(4),
91            Quirk::DsdBigEndian,
92        ],
93    },
94    // Fosi Audio DS2 (Savitech bridge): GET_RANGE returns no subranges and
95    // GET_CUR reads 0 Hz even after a successful SET_CUR, yet the DAC plays at
96    // the requested rate. Trust SET_CUR and skip readback validation.
97    BuiltinEntry {
98        vendor_id: 9770, // 0x262A
99        product_id: 1,
100        product_name_contains: "",
101        quirks: &[Quirk::SkipClockValidation, Quirk::IgnoreInvalidSampleRate],
102    },
103];
104
105/// The built-in quirks for a device, in table order.
106pub(crate) fn builtin_quirks(vendor_id: u16, product_id: u16, product_name: &str) -> Vec<Quirk> {
107    let mut result = Vec::new();
108    for entry in BUILTIN {
109        if entry.vendor_id == vendor_id
110            && entry.product_id == product_id
111            && (entry.product_name_contains.is_empty()
112                || product_name.contains(entry.product_name_contains))
113        {
114            result.extend_from_slice(entry.quirks);
115        }
116    }
117    result
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[test]
125    fn dawn_pro_quirks_found_by_vid_pid() {
126        let quirks = builtin_quirks(12230, 61546, "MOONDROP Dawn Pro");
127        assert!(quirks.contains(&Quirk::RequireVerifiedRate));
128        assert!(quirks.contains(&Quirk::PreferPadded24BitTransport));
129        assert!(quirks.contains(&Quirk::DsdBigEndian));
130        assert!(quirks.contains(&Quirk::SettleDelayMs(50)));
131    }
132
133    #[test]
134    fn the_product_name_must_match_when_the_entry_names_one() {
135        assert!(builtin_quirks(12230, 61546, "Some Other Product").is_empty());
136    }
137
138    #[test]
139    fn unknown_device_returns_empty() {
140        assert!(builtin_quirks(0x0000, 0x0000, "Unknown").is_empty());
141    }
142
143    #[test]
144    fn fosi_ds2_quirks_registered_for_any_name() {
145        let quirks = builtin_quirks(9770, 1, "Fosi Audio DS2");
146        assert!(quirks.contains(&Quirk::SkipClockValidation));
147        assert!(quirks.contains(&Quirk::IgnoreInvalidSampleRate));
148        assert!(!builtin_quirks(9770, 1, "").is_empty());
149    }
150}