aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / usb_audio.h
| 1 | /* usb_audio.h -- audio.output (issue #85, ADR 0012 rule 15), C kernel slice 1: |
| 2 | * the pure logic of USB Audio Class output. No MMIO, no DMA, no controller |
| 3 | * access; host tested in dev/tests/usb_audio_test.c (make usb-audio-test). |
| 4 | * |
| 5 | * Port of the Rust reference crates/aienos-kernel/src/usb/audio.rs (#96, |
| 6 | * #102), which the C kernel (ADR 0024) does not link: |
| 7 | * - ck_uac_select_output: walk one configuration descriptor (UAC1 or UAC2) |
| 8 | * and pick the best isochronous OUT stream for 48 kHz 16-bit stereo; |
| 9 | * - ck_uac_interval_us: endpoint service interval from bInterval; |
| 10 | * - ck_pcm_framer: bytes per service interval with the fractional frame |
| 11 | * carried, so 44.1 kHz at 1 ms totals exactly 44100 frames per second; |
| 12 | * - ck_uac_schedule: one isochronous TD (one Isoch TRB) per packet, Frame |
| 13 | * IDs wrapping at the 11-bit field, IOC on the last scheduled packet only; |
| 14 | * - ck_test_tone: deterministic integer sine-like tone (no floating point). |
| 15 | * |
| 16 | * Behaviour matches the Rust reference, including its known limit: UAC2 |
| 17 | * sample rates belong to the clock entity and are not queried here, so a UAC2 |
| 18 | * stream is taken to offer 48 kHz. Every descriptor length is bounds checked |
| 19 | * and the walk never reads past min(len, wTotalLength). |
| 20 | * |
| 21 | * Not in this slice (later #85 cuts): the xHCI isochronous endpoint inside |
| 22 | * the DMA fence, the QEMU usb-audio gate, and any speaker policy. Output |
| 23 | * only: nothing here records audio or touches a microphone. |
| 24 | * Freestanding. QEMU is not hardware: nothing here is physically qualified. */ |
| 25 | #ifndef AIENOS_CK_USB_AUDIO_H |
| 26 | #define AIENOS_CK_USB_AUDIO_H |
| 27 | #include <stddef.h> |
| 28 | #include <stdint.h> |
| 29 | |
| 30 | enum ck_uac_version { CK_UAC_NONE = 0, CK_UAC1 = 1, CK_UAC2 = 2 }; |
| 31 | |
| 32 | #define CK_UAC_MAX_RATES 8u |
| 33 | #define CK_UAC_MAX_CANDIDATES 16u |
| 34 | #define CK_UAC_NO_CLOCK 0xffffu |
| 35 | |
| 36 | typedef struct { |
| 37 | uint8_t version; /* enum ck_uac_version */ |
| 38 | uint8_t control_interface; /* AudioControl bInterfaceNumber */ |
| 39 | uint8_t interface; /* AudioStreaming bInterfaceNumber */ |
| 40 | uint8_t alternate; /* bAlternateSetting carrying the endpoint */ |
| 41 | uint8_t endpoint_address; /* isochronous OUT bEndpointAddress */ |
| 42 | uint16_t max_packet; /* wMaxPacketSize bits 10:0 */ |
| 43 | uint8_t interval; /* bInterval */ |
| 44 | uint8_t channels; |
| 45 | uint8_t subframe_bytes; |
| 46 | uint8_t bit_resolution; |
| 47 | uint16_t clock_source; /* UAC2 clock entity id, or CK_UAC_NO_CLOCK */ |
| 48 | uint32_t sample_rates[CK_UAC_MAX_RATES]; /* rate_count 0 = continuous */ |
| 49 | uint8_t rate_count; |
| 50 | } ck_uac_stream; |
| 51 | |
| 52 | /* 0 and *out filled, or -1: no usable stream (at least 2 channels, 2-byte |
| 53 | * subframes, 16-bit resolution, 48 kHz offered or continuous) or a malformed |
| 54 | * descriptor. Preference: exactly 2 channels, then exactly 16 bits, then the |
| 55 | * smallest max_packet; ties keep the first stream found. */ |
| 56 | int ck_uac_select_output(const uint8_t *cfg, size_t len, ck_uac_stream *out); |
| 57 | |
| 58 | /* Service interval in microseconds: full speed 1000, high speed |
| 59 | * 125 << (bInterval - 1) for bInterval 1..16. 0 when bInterval is 0 or out |
| 60 | * of range. */ |
| 61 | uint32_t ck_uac_interval_us(int high_speed, uint8_t interval); |
| 62 | |
| 63 | typedef struct { |
| 64 | uint32_t rate; |
| 65 | uint8_t channels; |
| 66 | uint8_t sample_bytes; |
| 67 | uint32_t interval_us; |
| 68 | uint64_t remainder; |
| 69 | } ck_pcm_framer; |
| 70 | /* 0, or -1 when any argument is zero (the framer is then unusable). */ |
| 71 | int ck_pcm_framer_init(ck_pcm_framer *f, uint32_t rate, uint8_t channels, uint8_t sample_bytes, uint32_t interval_us); |
| 72 | /* Bytes of the next packet, advancing the fractional carry. */ |
| 73 | size_t ck_pcm_next_bytes(ck_pcm_framer *f); |
| 74 | /* Bytes the next packet will need, without advancing. */ |
| 75 | size_t ck_pcm_peek_bytes(const ck_pcm_framer *f); |
| 76 | /* Advance one packet and copy its samples (at most out_len) from the |
| 77 | * interleaved ring, wrapping *cursor. An empty ring writes silence. Returns |
| 78 | * the samples written. */ |
| 79 | size_t ck_pcm_fill_i16(ck_pcm_framer *f, const int16_t *ring, size_t ring_len, size_t *cursor, int16_t *out, |
| 80 | size_t out_len); |
| 81 | |
| 82 | /* One isochronous packet buffer: DMA address and capacity in bytes. */ |
| 83 | typedef struct { |
| 84 | uint64_t dma; |
| 85 | uint16_t capacity; |
| 86 | } ck_uac_packet; |
| 87 | /* Isoch TRB dwords (xHCI 1.2 section 6.4.1.3); the cycle bit is left 0 for |
| 88 | * the ring writer to set. */ |
| 89 | typedef struct { |
| 90 | uint32_t d[4]; |
| 91 | } ck_uac_trb; |
| 92 | #define CK_UAC_TRB_ISOCH 5u |
| 93 | #define CK_UAC_TRB_IOC (1u << 5) |
| 94 | #define CK_UAC_TRB_SIA (1u << 31) |
| 95 | /* Prepare up to min(npackets, ntrbs, pcm_len / stride) packets, Frame ID |
| 96 | * (controller_frame + lead_frames + i) & 0x7ff. Stops, without consuming any |
| 97 | * PCM, at the first packet that would not fit its buffer capacity or its |
| 98 | * stride-sample PCM chunk (never truncates). stride 0 schedules nothing. |
| 99 | * Returns the number of TRBs written; the last one carries IOC. */ |
| 100 | size_t ck_uac_schedule(ck_pcm_framer *f, uint16_t controller_frame, uint16_t lead_frames, const ck_uac_packet *packets, |
| 101 | size_t npackets, const int16_t *ring, size_t ring_len, size_t *cursor, int16_t *pcm, |
| 102 | size_t pcm_len, size_t stride, ck_uac_trb *trbs, size_t ntrbs); |
| 103 | |
| 104 | typedef struct { |
| 105 | uint32_t phase; |
| 106 | uint32_t step; |
| 107 | } ck_test_tone; |
| 108 | /* 0, or -1 when sample_rate is 0 or frequency_hz is not below Nyquist. */ |
| 109 | int ck_test_tone_init(ck_test_tone *t, uint32_t frequency_hz, uint32_t sample_rate); |
| 110 | /* Next sample, always within [-32767, 32767]. */ |
| 111 | int16_t ck_test_tone_next(ck_test_tone *t); |
| 112 | #endif |