usb-stack 0.1.0

MCU-independent USB device stack core: bus trait, control pipe and classes (CDC-ACM, HID, GS_USB, i2c-tiny-usb, UAC 1.0)
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! USB Audio Class 1.0 (UAC 1.0).
//!
//! Implements "Universal Serial Bus Device Class Definition for Audio Devices",
//! Release 1.0.  Supports one input stream (e.g. microphone) and/or one output
//! stream (e.g. speaker / headphones) with discrete or continuous sampling rates.
//!
//! # Example — stereo speaker with microphone
//!
//! ```ignore
//! use my_hal_usb::{UsbBusAllocator, UsbDeviceBuilder};
//! use my_hal_usb::class::uac::{
//!     AudioClassBuilder, Format, StreamConfig, TerminalType,
//! };
//! use my_hal_usb::MyBus;
//!
//! static RATES_48K: [u32; 1] = [48000];
//!
//! let alloc = UsbBusAllocator::new(MyBus::new(/* peripheral */));
//! let mut audio = AudioClassBuilder::new()
//!     .input(
//!         StreamConfig::new_discrete(
//!             Format::S16le, 1, &RATES_48K, TerminalType::InMicrophone,
//!         ).unwrap())
//!     .output(
//!         StreamConfig::new_discrete(
//!             Format::S24le, 2, &RATES_48K, TerminalType::OutSpeaker,
//!         ).unwrap())
//!     .build(&alloc);
//!
//! let mut dev = UsbDeviceBuilder::new(&alloc, 0x1234, 0x5678)
//!     .product("USB Audio")
//!     .composite_with_iads()
//!     .build();
//!
//! loop {
//!     dev.poll(&mut [&mut audio]);
//!     let mut buf = [0u8; 288];
//!     if let Ok(n) = audio.read(&mut buf) {
//!         // send to DAC …
//!     }
//!     let pcm = [0u8; 96];
//!     let _ = audio.write(&pcm);
//! }
//! ```

use crate::bus::{InterfaceNumber, UsbBus, UsbBusAllocator};
use crate::class::UsbClass;
use crate::control::{ControlIn, ControlOut, Recipient, RequestType};
use crate::descriptor::DescriptorWriter;
use crate::endpoint::{
    EndpointAddress, EndpointIn, EndpointOut, EndpointType, IsoSynchronization, IsoUsage,
};
use crate::error::{UsbError, UsbResult};

// ─── Terminal types (USB Audio 1.0) ──────────────────────────────────────────

/// USB Audio Terminal Types from "Universal Serial Bus Device Class Definition
/// for Terminal Types, Release 1.0".
#[rustfmt::skip]
#[repr(u16)]
#[non_exhaustive]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum TerminalType {
    // USB Terminal Types
    /// Undefined USB terminal.
    UsbUndefined  = 0x0100,
    /// USB streaming terminal.
    UsbStreaming  = 0x0101,
    /// Vendor-specific USB terminal.
    UsbVendor     = 0x01FF,

    // Input Terminal Types
    /// Undefined input terminal.
    InUndefined                   = 0x0200,
    /// Microphone input terminal.
    InMicrophone                  = 0x0201,
    /// Desktop microphone input terminal.
    InDesktopMicrophone           = 0x0202,
    /// Personal microphone input terminal.
    InPersonalMicrophone          = 0x0203,
    /// Omni-directional microphone input terminal.
    InOmniDirectionalMicrophone   = 0x0204,
    /// Microphone array input terminal.
    InMicrophoneArray             = 0x0205,
    /// Processing microphone array input terminal.
    InProcessingMicrophoneArray   = 0x0206,

    // Output Terminal Types
    /// Undefined output terminal.
    OutUndefined                  = 0x0300,
    /// Speaker output terminal.
    OutSpeaker                    = 0x0301,
    /// Headphones output terminal.
    OutHeadphones                 = 0x0302,
    /// Head-mounted display audio output terminal.
    OutHeadMountedDisplayAudio    = 0x0303,
    /// Desktop speaker output terminal.
    OutDesktopSpeaker             = 0x0304,
    /// Room speaker output terminal.
    OutRoomSpeaker                = 0x0305,
    /// Communication speaker output terminal.
    OutCommunicationSpeaker       = 0x0306,
    /// Low-frequency-effects speaker output terminal.
    OutLowFrequencyEffectsSpeaker = 0x0307,

    // External Terminal Types
    /// Undefined external terminal.
    ExtUndefined               = 0x0600,
    /// Analog connector external terminal.
    ExtAnalogConnector         = 0x0601,
    /// Digital audio interface external terminal.
    ExtDigitalAudioInterface   = 0x0602,
    /// Line connector external terminal.
    ExtLineConnector           = 0x0603,
    /// Legacy audio connector external terminal.
    ExtLegacyAudioConnector    = 0x0604,
    /// S/PDIF connector external terminal.
    ExtSpdifConnector          = 0x0605,
    /// IEEE 1394 DA stream external terminal.
    Ext1394DaStream            = 0x0606,
    /// IEEE 1394 DV stream soundtrack external terminal.
    Ext1394DvStreamSoundtrack  = 0x0607,
}

impl From<TerminalType> for u16 {
    fn from(t: TerminalType) -> u16 {
        t as u16
    }
}

// ─── UAC class-specific constants ─────────────────────────────────────────────

/// Audio Interface Class Code (USB-IF).
pub const UAC_CLASS: u8 = 0x01;
/// Audio Control subclass.
pub const UAC_SUBCLASS_CONTROL: u8 = 0x01;
/// Audio Streaming subclass.
pub const UAC_SUBCLASS_STREAMING: u8 = 0x02;

/// Class-Specific Interface descriptor type (CS_INTERFACE).
const CS_INTERFACE: u8 = 0x24;
/// Class-Specific Endpoint descriptor type (CS_ENDPOINT).
const CS_ENDPOINT: u8 = 0x25;

// AC descriptor subtypes
const AC_HEADER: u8 = 0x01;
const AC_INPUT_TERMINAL: u8 = 0x02;
const AC_OUTPUT_TERMINAL: u8 = 0x03;
const AC_FEATURE_UNIT: u8 = 0x06;

// AS descriptor subtypes
const AS_GENERAL: u8 = 0x01;
const AS_FORMAT_TYPE: u8 = 0x02;

// EP descriptor subtypes
const EP_GENERAL: u8 = 0x01;

// Feature Unit control selectors
const FU_MUTE: u8 = 0x01;
const FU_VOLUME: u8 = 0x02;

// Audio Class-Specific Request Codes (UAC 1.0 §5.2.2)
const CUR_SET: u8 = 0x01;
const CUR_GET: u8 = 0x81;
const MIN_GET: u8 = 0x82;
const MAX_GET: u8 = 0x83;
const RES_GET: u8 = 0x84;

/// PCM format tag (Audio Data Format Type I).
const PCM_TAG: u16 = 0x0001;
/// Format Type I code.
const FORMAT_TYPE_I: u8 = 0x01;

/// Terminal/Unit IDs used in the topology.
///
/// Output (speaker) topology: IT(USB,2) → FU(3) → OT(Speaker,4)
/// Input (mic) topology:     IT(Mic,5)  → FU(6) → OT(USB,7)
const TERM_ID_SPEAKER_USB: u8 = 0x02;
const TERM_ID_SPEAKER_FU: u8 = 0x03;
const TERM_ID_SPEAKER_OUT: u8 = 0x04;
const TERM_ID_MIC_IN: u8 = 0x05;
const TERM_ID_MIC_FU: u8 = 0x06;
const TERM_ID_MIC_USB: u8 = 0x07;

/// Maximum isochronous endpoint packet size for full-speed USB.
const MAX_ISO_SIZE: u32 = 1023;

// ─── Audio format ────────────────────────────────────────────────────────────

/// Audio sample format.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Format {
    /// Signed 16-bit little-endian (2 bytes/subframe).
    S16le,
    /// Signed 24-bit little-endian (3 bytes/subframe).
    S24le,
}

impl Format {
    const fn bytes(self) -> u8 {
        match self {
            Format::S16le => 2,
            Format::S24le => 3,
        }
    }
    const fn bits(self) -> u8 {
        match self {
            Format::S16le => 16,
            Format::S24le => 24,
        }
    }
}

// ─── Sampling rates ──────────────────────────────────────────────────────────

/// Supported sampling rates for an audio stream.
#[derive(Debug, Clone)]
pub enum Rates {
    /// Continuous range `[min, max]` in Hz (0 = continuous in descriptor).
    Continuous(u32, u32),
    /// Discrete set of frequencies in Hz.
    Discrete(&'static [u32]),
}

// ─── StreamConfig ────────────────────────────────────────────────────────────

/// Configuration for one audio stream (input or output).
#[derive(Debug, Clone)]
pub struct StreamConfig {
    /// Sample format.
    pub format: Format,
    /// Number of channels (1 = mono, 2 = stereo, …).
    pub channels: u8,
    /// Supported sampling rates.
    pub rates: Rates,
    /// Terminal type for this stream.
    pub terminal_type: TerminalType,
    /// Computed maximum isochronous endpoint packet size in bytes.
    pub ep_size: u16,
}

impl StreamConfig {
    /// Create with one or more discrete sampling rates.
    ///
    /// `rates` must be a `&'static [u32]` for lifetime simplicity in `no_std`
    /// embedded contexts.
    pub fn new_discrete(
        format: Format,
        channels: u8,
        rates: &'static [u32],
        terminal_type: TerminalType,
    ) -> Result<Self, UsbError> {
        let max_rate = *rates.iter().max().unwrap_or(&48000);
        let ep_size = Self::calc_ep_size(format, channels, max_rate)?;
        Ok(Self {
            format,
            channels,
            rates: Rates::Discrete(rates),
            terminal_type,
            ep_size,
        })
    }

    /// Create with a continuous frequency range `[min_rate, max_rate]`.
    pub fn new_continuous(
        format: Format,
        channels: u8,
        min_rate: u32,
        max_rate: u32,
        terminal_type: TerminalType,
    ) -> Result<Self, UsbError> {
        if min_rate >= max_rate {
            return Err(UsbError::InvalidState);
        }
        let ep_size = Self::calc_ep_size(format, channels, max_rate)?;
        Ok(Self {
            format,
            channels,
            rates: Rates::Continuous(min_rate, max_rate),
            terminal_type,
            ep_size,
        })
    }

    fn calc_ep_size(format: Format, channels: u8, max_rate: u32) -> Result<u16, UsbError> {
        let octets_per_frame = channels as u32 * format.bytes() as u32;
        let size = octets_per_frame * max_rate / 1000;
        if size > MAX_ISO_SIZE {
            return Err(UsbError::BufferOverflow);
        }
        Ok(size as u16)
    }

    fn channel_config(&self) -> u16 {
        match self.channels {
            1 => 0x0001, // L
            2 => 0x0003, // L + R
            4 => 0x0033, // L + R + LS + RS
            6 => 0x003F, // L + R + C + LFE + LS + RS
            8 => 0x00FF, // L + R + C + LFE + LS + RS + LC + RC
            _ => 0x0003, // default: stereo
        }
    }
}

// ─── AudioClassBuilder ───────────────────────────────────────────────────────

/// Builder for [`AudioClass`].
///
/// Use the fluent API to configure input/output streams, then call
/// [`build`](Self::build).
#[derive(Default)]
pub struct AudioClassBuilder {
    input: Option<StreamConfig>,
    output: Option<StreamConfig>,
}

impl AudioClassBuilder {
    /// Create a new builder with no streams configured.
    pub fn new() -> Self {
        Self {
            input: None,
            output: None,
        }
    }

    /// Configure an input stream (device → host, e.g. microphone).
    ///
    /// Calling multiple times replaces the previous configuration.
    pub fn input(mut self, cfg: StreamConfig) -> Self {
        self.input = Some(cfg);
        self
    }

    /// Configure an output stream (host → device, e.g. speaker / headphones).
    ///
    /// Calling multiple times replaces the previous configuration.
    pub fn output(mut self, cfg: StreamConfig) -> Self {
        self.output = Some(cfg);
        self
    }

    /// Build the [`AudioClass`], allocating endpoints and interfaces from `alloc`.
    pub fn build<B: UsbBus>(self, alloc: &UsbBusAllocator<B>) -> AudioClass<'_, B> {
        let control_iface = alloc.interface();

        let (in_ep, in_iface, in_cfg, in_alt) = if let Some(cfg) = self.input {
            let iface = alloc.interface();
            let ep = alloc.isochronous::<crate::endpoint::In>(
                IsoSynchronization::Asynchronous,
                IsoUsage::Data,
                cfg.ep_size,
                1,
            );
            (Some(ep), iface, Some(cfg), 0u8)
        } else {
            (None, InterfaceNumber::new(0), None, 0u8)
        };

        let (out_ep, out_iface, out_cfg, out_alt) = if let Some(cfg) = self.output {
            let iface = alloc.interface();
            let ep = alloc.isochronous::<crate::endpoint::Out>(
                IsoSynchronization::Adaptive,
                IsoUsage::Data,
                cfg.ep_size,
                1,
            );
            (Some(ep), iface, Some(cfg), 0u8)
        } else {
            (None, InterfaceNumber::new(0), None, 0u8)
        };

        AudioClass {
            control_iface,
            in_ep,
            in_iface,
            in_cfg,
            in_alt,
            out_ep,
            out_iface,
            out_cfg,
            out_alt,
            configured: false,
        }
    }
}

// ─── AudioClass ──────────────────────────────────────────────────────────────

/// USB Audio Class 1.0 device class.
///
/// Supports one streaming input (microphone / line-in) and/or one streaming
/// output (speaker / headphones).  Built via [`AudioClassBuilder`].
///
/// The host activates a stream by selecting alternate setting 1
/// (via `SET_INTERFACE`), and deactivates it by selecting alternate setting 0.
pub struct AudioClass<'a, B: UsbBus> {
    // Control interface
    control_iface: InterfaceNumber,

    // Input stream (device → host)
    in_ep: Option<EndpointIn<'a, B>>,
    in_iface: InterfaceNumber,
    in_cfg: Option<StreamConfig>,
    in_alt: u8,

    // Output stream (host → device)
    out_ep: Option<EndpointOut<'a, B>>,
    out_iface: InterfaceNumber,
    out_cfg: Option<StreamConfig>,
    out_alt: u8,

    configured: bool,
}

impl<'a, B: UsbBus> AudioClass<'a, B> {
    /// Read audio data from the host (output / speaker stream).
    ///
    /// Returns the number of bytes read into `buf`, or
    /// [`UsbError::WouldBlock`] when no isochronous packet is available.
    /// Also returns [`UsbError::InvalidState`] if the stream is not
    /// configured or not active (alt setting 0).
    pub fn read(&self, buf: &mut [u8]) -> UsbResult<usize> {
        let ep = self.out_ep.as_ref().ok_or(UsbError::InvalidState)?;
        if !self.configured || self.out_alt == 0 {
            return Err(UsbError::InvalidState);
        }
        ep.read(buf)
    }

    /// Write audio data to the host (input / microphone stream).
    ///
    /// Returns the number of bytes written, or [`UsbError::WouldBlock`]
    /// when the previous packet has not yet been sent.  Also returns
    /// [`UsbError::InvalidState`] if the stream is not configured or
    /// not active (alt setting 0).
    pub fn write(&self, buf: &[u8]) -> UsbResult<usize> {
        let ep = self.in_ep.as_ref().ok_or(UsbError::InvalidState)?;
        if !self.configured || self.in_alt == 0 {
            return Err(UsbError::InvalidState);
        }
        ep.write(buf)
    }

    /// Current alternate setting of the input (microphone) stream.
    /// Returns `None` if no input stream was configured.
    pub fn input_alt_setting(&self) -> Option<u8> {
        self.in_cfg.as_ref().map(|_| self.in_alt)
    }

    /// Current alternate setting of the output (speaker) stream.
    /// Returns `None` if no output stream was configured.
    pub fn output_alt_setting(&self) -> Option<u8> {
        self.out_cfg.as_ref().map(|_| self.out_alt)
    }

    /// `true` when the device has been fully configured by the host.
    pub fn is_configured(&self) -> bool {
        self.configured
    }

    /// `true` when the output (speaker) stream is active (alt setting ≠ 0).
    pub fn output_active(&self) -> bool {
        self.configured && self.out_alt != 0
    }

    /// `true` when the input (microphone) stream is active (alt setting ≠ 0).
    pub fn input_active(&self) -> bool {
        self.configured && self.in_alt != 0
    }

    // ── Descriptor helpers ──────────────────────────────────────────────────

    fn streaming_count(&self) -> u8 {
        self.in_cfg.is_some() as u8 + self.out_cfg.is_some() as u8
    }

    fn write_ac_header(&self, w: &mut DescriptorWriter) -> UsbResult<()> {
        let n = self.streaming_count();
        let total_len = self.ac_total_length();
        let mut hdr = [
            AC_HEADER,
            0x00,
            0x01, // bcdADC = 1.00
            total_len as u8,
            (total_len >> 8) as u8, // wTotalLength
            n,                      // bInCollection
            0x00,
            0x00, // baInterfaceNr[0..1]
        ];
        let mut idx = 6usize;
        if self.in_cfg.is_some() {
            hdr[idx] = self.in_iface.into();
            idx += 1;
        }
        if self.out_cfg.is_some() {
            hdr[idx] = self.out_iface.into();
            idx += 1;
        }
        w.write(CS_INTERFACE, &hdr[..idx])
    }

    /// Compute wTotalLength for the AC interface header — sum of all
    /// class-specific descriptor sizes within the AudioControl interface.
    fn ac_total_length(&self) -> u16 {
        let n = self.streaming_count() as u16;
        // AC Header: 2 (bLength+bType) + 6 (fixed body) + n (baInterfaceNr)
        let mut total: u16 = 8 + n;
        for cfg in [&self.in_cfg, &self.out_cfg]
            .iter()
            .filter_map(|c| c.as_ref())
        {
            // Input Terminal: 2 + 10 = 12
            total += 12;
            // Feature Unit: 2 + (6 + channels) = 8 + channels
            total += 8 + cfg.channels as u16;
            // Output Terminal: 2 + 7 = 9
            total += 9;
        }
        total
    }

    /// Return the number of channels for a given Feature Unit ID,
    /// or 0 if the ID doesn't match any of our units.
    fn fu_channels(&self, unit_id: u8) -> u8 {
        if unit_id == TERM_ID_SPEAKER_FU {
            self.out_cfg.as_ref().map_or(0, |c| c.channels)
        } else if unit_id == TERM_ID_MIC_FU {
            self.in_cfg.as_ref().map_or(0, |c| c.channels)
        } else {
            0
        }
    }

    fn write_terminal_descriptors(
        &self,
        w: &mut DescriptorWriter,
        cfg: &StreamConfig,
        input_term_id: u8,
        fu_id: u8,
        output_term_id: u8,
        is_input: bool,
    ) -> UsbResult<()> {
        let tt: u16 = cfg.terminal_type.into();
        let cc = cfg.channel_config();

        // ── Input Terminal (12 bytes) ────────────────────────────────────
        let it_type = if is_input {
            tt
        } else {
            TerminalType::UsbStreaming.into()
        };
        w.write(
            CS_INTERFACE,
            &[
                AC_INPUT_TERMINAL,
                input_term_id,
                it_type as u8,
                (it_type >> 8) as u8,
                0x00,            // bAssocTerminal
                cfg.channels,    // bNrChannels
                cc as u8,        // wChannelConfig (lo)
                (cc >> 8) as u8, // wChannelConfig (hi)
                0x00,            // iChannelNames
                0x00,            // iTerminal
            ],
        )?;

        // ── Feature Unit (7 + ch + 1 bytes body) ─────────────────────────
        self.write_feature_unit(w, cfg, fu_id, input_term_id)?;

        // ── Output Terminal (9 bytes) ────────────────────────────────────
        let ot_type = if is_input {
            TerminalType::UsbStreaming.into()
        } else {
            tt
        };
        w.write(
            CS_INTERFACE,
            &[
                AC_OUTPUT_TERMINAL,
                output_term_id,
                ot_type as u8,
                (ot_type >> 8) as u8,
                0x00,  // bAssocTerminal
                fu_id, // bSourceID → comes from Feature Unit
                0x00,  // iTerminal
            ],
        )
    }

    /// Write a Feature Unit descriptor with Mute + Volume controls.
    ///
    /// The body is: subtype(1) + unitID(1) + sourceID(1) + controlSize(1)
    ///            + ch+1 bitmap bytes + iFeature(1)
    /// Total body = 4 + (ch+1) + 1 = 6 + ch
    /// Full descriptor = 2 + body
    fn write_feature_unit(
        &self,
        w: &mut DescriptorWriter,
        cfg: &StreamConfig,
        fu_id: u8,
        source_id: u8,
    ) -> UsbResult<()> {
        let ch = cfg.channels as usize;
        // Max channels we support in the stack-allocated buffer: 8
        let mut body = [0u8; 16]; // enough for up to 8 ch (6+8=14 bytes)
        body[0] = AC_FEATURE_UNIT;
        body[1] = fu_id;
        body[2] = source_id;
        body[3] = 1; // bControlSize: 1 byte per control
        // Master channel: Mute + Volume
        body[4] = FU_MUTE | FU_VOLUME; // 0x03
        // Per-channel: Volume only
        for i in 0..ch {
            body[5 + i] = FU_VOLUME; // 0x02
        }
        body[5 + ch] = 0x00; // iFeature
        let body_len = 6 + ch;
        w.write(CS_INTERFACE, &body[..body_len])
    }

    fn write_as_descriptors(
        &self,
        w: &mut DescriptorWriter,
        cfg: &StreamConfig,
        iface: InterfaceNumber,
        ep_addr: EndpointAddress,
        is_input: bool,
    ) -> UsbResult<()> {
        // Alt 0 — stream inactive, 0 endpoints.
        w.interface(iface, 0, UAC_CLASS, UAC_SUBCLASS_STREAMING, 0x00)?;

        // Alt 1 — stream active, 1 isochronous endpoint.
        w.interface_alt(iface, 1, 1, UAC_CLASS, UAC_SUBCLASS_STREAMING, 0x00)?;

        // Terminal link: the terminal in the audio function that this
        // streaming interface is associated with.
        // - Input stream (mic): links to the USB OUT terminal in AC topology
        // - Output stream (speaker): links to the USB IN terminal in AC topology
        let term_link = if is_input {
            TERM_ID_MIC_USB
        } else {
            TERM_ID_SPEAKER_USB
        };

        // Class-Specific AS General Interface descriptor
        w.write(
            CS_INTERFACE,
            &[
                AS_GENERAL,
                term_link,
                0x01, // bDelay = 1 frame
                PCM_TAG as u8,
                (PCM_TAG >> 8) as u8, // wFormatTag = PCM
            ],
        )?;

        // Type I Format Type descriptor
        self.write_format_type(w, cfg)?;

        // Standard Endpoint descriptor
        let ep_type = if is_input {
            EndpointType::Isochronous {
                synchronization: IsoSynchronization::Asynchronous,
                usage: IsoUsage::Data,
            }
        } else {
            EndpointType::Isochronous {
                synchronization: IsoSynchronization::Adaptive,
                usage: IsoUsage::Data,
            }
        };
        w.endpoint(ep_addr, ep_type, cfg.ep_size, 1)?;

        // Class-Specific Isoc Audio Data Endpoint descriptor
        w.write(
            CS_ENDPOINT,
            &[
                EP_GENERAL, // bDescriptorSubtype
                0x00,       // bmAttributes
                0x00,       // bLockDelayUnits
                0x00, 0x00, // wLockDelay
            ],
        )
    }

    fn write_format_type(&self, w: &mut DescriptorWriter, cfg: &StreamConfig) -> UsbResult<()> {
        fn put_u24(buf: &mut [u8], pos: &mut usize, v: u32) {
            buf[*pos] = v as u8;
            *pos += 1;
            buf[*pos] = (v >> 8) as u8;
            *pos += 1;
            buf[*pos] = (v >> 16) as u8;
            *pos += 1;
        }

        let mut buf = [0u8; 64];
        let mut i = 0usize;

        buf[i] = AS_FORMAT_TYPE;
        i += 1;
        buf[i] = FORMAT_TYPE_I;
        i += 1;
        buf[i] = cfg.channels;
        i += 1;
        buf[i] = cfg.format.bytes();
        i += 1;
        buf[i] = cfg.format.bits();
        i += 1;

        match &cfg.rates {
            Rates::Continuous(min, max) => {
                buf[i] = 0x00;
                i += 1;
                put_u24(&mut buf, &mut i, *min);
                put_u24(&mut buf, &mut i, *max);
            }
            Rates::Discrete(rates) => {
                buf[i] = rates.len() as u8;
                i += 1;
                for &r in *rates {
                    put_u24(&mut buf, &mut i, r);
                }
            }
        }
        w.write(CS_INTERFACE, &buf[..i])
    }
}

// ─── UsbClass implementation ─────────────────────────────────────────────────

impl<B: UsbBus> UsbClass<B> for AudioClass<'_, B> {
    fn configuration_descriptors(&self, w: &mut DescriptorWriter) -> UsbResult<()> {
        // NOTE: IAD is intentionally omitted.  The kernel's snd-usb-audio
        // driver locates the AudioControl interface by scanning for
        // bInterfaceClass=AUDIO / bInterfaceSubClass=AUDIOCONTROL.
        // Including an IAD can sometimes cause the USB core to misplace
        // class-specific descriptors in the interface extra-buffer on
        // certain host controllers.

        // ── Audio Control (AC) Interface ────────────────────────────────────
        w.interface(self.control_iface, 0, UAC_CLASS, UAC_SUBCLASS_CONTROL, 0x00)?;
        self.write_ac_header(w)?;

        if let Some(ref cfg) = self.in_cfg {
            self.write_terminal_descriptors(
                w,
                cfg,
                TERM_ID_MIC_IN,
                TERM_ID_MIC_FU,
                TERM_ID_MIC_USB,
                true,
            )?;
        }
        if let Some(ref cfg) = self.out_cfg {
            self.write_terminal_descriptors(
                w,
                cfg,
                TERM_ID_SPEAKER_USB,
                TERM_ID_SPEAKER_FU,
                TERM_ID_SPEAKER_OUT,
                false,
            )?;
        }

        // ── Audio Streaming (AS) Interfaces ─────────────────────────────────
        if let Some(ref cfg) = self.in_cfg {
            self.write_as_descriptors(
                w,
                cfg,
                self.in_iface,
                self.in_ep.as_ref().unwrap().address(),
                true,
            )?;
        }
        if let Some(ref cfg) = self.out_cfg {
            self.write_as_descriptors(
                w,
                cfg,
                self.out_iface,
                self.out_ep.as_ref().unwrap().address(),
                false,
            )?;
        }

        Ok(())
    }

    fn control_in(&mut self, xfer: ControlIn<'_, B>) {
        let req = xfer.request();

        // Only handle Class requests to the Interface recipient.
        if req.request_type != RequestType::Class || req.recipient != Recipient::Interface {
            return;
        }

        // wIndex = (UnitID << 8) | Interface
        let unit_id = (req.index >> 8) as u8;
        // wValue = (ControlSelector << 8) | ChannelNumber
        let cs = (req.value >> 8) as u8;

        // Check if the request targets one of our Feature Units.
        let channels = self.fu_channels(unit_id);
        if channels == 0 {
            return; // Not our unit — let the request fall through.
        }

        match req.request {
            CUR_GET if cs == FU_MUTE => {
                // Mute: return 0 = unmuted
                let _ = xfer.accept_with(&[0x00]);
            }
            CUR_GET if cs == FU_VOLUME => {
                // Volume CUR = 0x0000 (max / 0 dB), 16-bit signed LE
                let _ = xfer.accept_with(&[0x00, 0x00]);
            }
            MIN_GET if cs == FU_VOLUME => {
                // Volume MIN = 0x0080 (minimum, very quiet)
                let _ = xfer.accept_with(&[0x00, 0x80]);
            }
            MAX_GET if cs == FU_VOLUME => {
                // Volume MAX = 0x0000 (0 dB, max)
                let _ = xfer.accept_with(&[0x00, 0x00]);
            }
            RES_GET if cs == FU_VOLUME => {
                // Volume RES = 0x0100 (1 dB resolution)
                let _ = xfer.accept_with(&[0x00, 0x01]);
            }
            _ => {
                let _ = xfer.reject();
            }
        }
    }

    fn control_out(&mut self, xfer: ControlOut<'_, B>) {
        let req = xfer.request();

        if req.request_type != RequestType::Class || req.recipient != Recipient::Interface {
            return;
        }

        let unit_id = (req.index >> 8) as u8;
        let cs = (req.value >> 8) as u8;

        if self.fu_channels(unit_id) == 0 {
            return;
        }

        match req.request {
            CUR_SET if cs == FU_MUTE => {
                // Accept mute change (ignore the value for now).
                let _ = xfer.accept();
            }
            CUR_SET if cs == FU_VOLUME => {
                // Accept volume change (ignore the value for now).
                let _ = xfer.accept();
            }
            _ => {
                let _ = xfer.reject();
            }
        }
    }

    fn configure(&mut self) {
        self.configured = true;
    }

    fn deconfigure(&mut self) {
        self.configured = false;
        self.in_alt = 0;
        self.out_alt = 0;
    }

    fn reset(&mut self) {
        self.configured = false;
        self.in_alt = 0;
        self.out_alt = 0;
    }

    fn alt_setting(&mut self, iface: InterfaceNumber) -> Option<u8> {
        if self.in_cfg.is_some() && self.in_iface == iface {
            return Some(self.in_alt);
        }
        if self.out_cfg.is_some() && self.out_iface == iface {
            return Some(self.out_alt);
        }
        None
    }

    fn set_alt_setting(&mut self, iface: InterfaceNumber, alt: u8) -> bool {
        if self.in_cfg.is_some() && self.in_iface == iface {
            self.in_alt = alt;
            return true;
        }
        if self.out_cfg.is_some() && self.out_iface == iface {
            self.out_alt = alt;
            return true;
        }
        false
    }
}