Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/usb_audio.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / usb_audio.c` 251 lines ยท 9725 bytes Raw Blame History 1 /* usb_audio.c -- audio.output pure logic (see usb_audio.h). Freestanding. */ 2 #include "usb_audio.h" 3 4 #define D_CONFIG 2u 5 #define D_INTERFACE 4u 6 #define D_ENDPOINT 5u 7 #define D_CS_INTERFACE 0x24u 8 9 static uint32_t le24(const uint8_t *b) { return (uint32_t)b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16); } 10 11 static int usable(const ck_uac_stream *s) 12 { 13 if (s->channels < 2 || s->subframe_bytes < 2 || s->bit_resolution < 16) return 0; 14 if (s->rate_count == 0) return 1; 15 for (unsigned i = 0; i < s->rate_count; i++) 16 if (s->sample_rates[i] == 48000u) return 1; 17 return 0; 18 } 19 20 /* Lexicographic preference key: lower is better. */ 21 static int better(const ck_uac_stream *a, const ck_uac_stream *b) 22 { 23 int ac = a->channels != 2, bc = b->channels != 2; 24 if (ac != bc) return ac < bc; 25 int ab = a->bit_resolution != 16, bb = b->bit_resolution != 16; 26 if (ab != bb) return ab < bb; 27 return a->max_packet < b->max_packet; 28 } 29 30 int ck_uac_select_output(const uint8_t *b, size_t len, ck_uac_stream *out) 31 { 32 if (!b || !out || len < 9 || b[0] < 9 || b[1] != D_CONFIG) return -1; 33 size_t total = (size_t)b[2] | ((size_t)b[3] << 8); 34 if (total > len) total = len; 35 36 uint8_t version = CK_UAC_NONE; 37 int have_control = 0, have_iface = 0, have_fmt = 0; 38 uint8_t control = 0, if_num = 0, if_alt = 0, if_as = 0; 39 uint8_t f_ch = 0, f_sub = 0, f_bits = 0, f_count = 0; 40 uint32_t f_rates[CK_UAC_MAX_RATES] = {0}; 41 uint16_t clock = CK_UAC_NO_CLOCK; 42 ck_uac_stream best; 43 int have_best = 0; 44 unsigned n = 0; 45 46 size_t at = 0; 47 while (at + 2 <= total) { 48 size_t l = b[at]; 49 if (l < 2 || at + l > total) return -1; 50 const uint8_t *d = b + at; 51 if (d[1] == D_INTERFACE && l >= 9) { 52 have_iface = 1; 53 if_num = d[2]; 54 if_alt = d[3]; 55 if_as = d[5] == 1 && d[6] == 2; 56 have_fmt = 0; 57 if (d[5] == 1 && d[6] == 1) { 58 have_control = 1; 59 control = d[2]; 60 } 61 } else if (d[1] == D_CS_INTERFACE && l >= 3) { 62 int in_as = have_iface && if_as; 63 int in_other = have_iface && !if_as; 64 if (d[2] == 1 && in_as) { 65 /* UAC2 AS_GENERAL: bNrChannels at offset 10. UAC1 has none 66 * there (offset 4 is bDelay); its channels come from 67 * FORMAT_TYPE_I. */ 68 if (version == CK_UAC2 && l >= 11) { 69 if (!have_fmt) { 70 f_sub = 0; 71 f_bits = 0; 72 f_count = 0; 73 for (unsigned i = 0; i < CK_UAC_MAX_RATES; i++) f_rates[i] = 0; 74 have_fmt = 1; 75 } 76 f_ch = d[10]; 77 } 78 } else if (d[2] == 1 && l >= 8 && in_other) { 79 uint16_t bcd = (uint16_t)(d[3] | (d[4] << 8)); 80 version = bcd >= 0x0200 ? CK_UAC2 : CK_UAC1; 81 if (version == CK_UAC2) clock = d[7]; 82 } else if (d[2] == 2 && in_as && l >= (version == CK_UAC2 ? 6u : 8u)) { 83 /* FORMAT_TYPE_I. UAC1: bNrChannels[4] bSubframeSize[5] 84 * bBitResolution[6] bSamFreqType[7]. UAC2: bSubslotSize[4] 85 * bBitResolution[5]. */ 86 uint8_t ch, sub, bits; 87 if (version == CK_UAC2) { 88 ch = have_fmt ? f_ch : 0; 89 sub = d[4]; 90 bits = d[5]; 91 } else { 92 ch = d[4]; 93 sub = d[5]; 94 bits = d[6]; 95 } 96 uint32_t rates[CK_UAC_MAX_RATES] = {0}; 97 uint8_t count = 0; 98 if (version == CK_UAC1) { 99 size_t nr = d[7]; 100 if (nr != 0 && l >= 8 + nr * 3) { 101 count = (uint8_t)(nr < CK_UAC_MAX_RATES ? nr : CK_UAC_MAX_RATES); 102 for (unsigned i = 0; i < count; i++) rates[i] = le24(d + 8 + i * 3); 103 } 104 } else if (version == CK_UAC2) { 105 /* Rates belong to the UAC2 clock entity (not queried). */ 106 rates[0] = 48000u; 107 count = 1; 108 } 109 have_fmt = 1; 110 f_ch = ch; 111 f_sub = sub; 112 f_bits = bits; 113 f_count = count; 114 for (unsigned i = 0; i < CK_UAC_MAX_RATES; i++) f_rates[i] = rates[i]; 115 } else if (d[2] == 0x0a && l >= 4 && version == CK_UAC2) { 116 clock = d[3]; /* CLOCK_SOURCE bClockID */ 117 } 118 } else if (d[1] == D_ENDPOINT && l >= 7) { 119 if (have_iface && if_as && have_fmt && version != CK_UAC_NONE && have_control) { 120 if (if_alt != 0 && (d[2] & 0x80u) == 0 && (d[3] & 3u) == 1u && n < CK_UAC_MAX_CANDIDATES) { 121 ck_uac_stream s; 122 s.version = version; 123 s.control_interface = control; 124 s.interface = if_num; 125 s.alternate = if_alt; 126 s.endpoint_address = d[2]; 127 s.max_packet = (uint16_t)((d[4] | (d[5] << 8)) & 0x7ff); 128 s.interval = d[6]; 129 s.channels = f_ch; 130 s.subframe_bytes = f_sub; 131 s.bit_resolution = f_bits; 132 s.clock_source = clock; 133 for (unsigned i = 0; i < CK_UAC_MAX_RATES; i++) s.sample_rates[i] = f_rates[i]; 134 s.rate_count = f_count; 135 if (version == CK_UAC2 && f_count == 1) s.sample_rates[0] = 48000u; 136 n++; 137 if (usable(&s) && (!have_best || better(&s, &best))) { 138 best = s; 139 have_best = 1; 140 } 141 } 142 } 143 } 144 at += l; 145 } 146 if (!have_best) return -1; 147 *out = best; 148 return 0; 149 } 150 151 uint32_t ck_uac_interval_us(int high_speed, uint8_t interval) 152 { 153 if (interval == 0) return 0; 154 if (!high_speed) return 1000u; 155 return interval <= 16 ? 125u << (interval - 1) : 0; 156 } 157 158 int ck_pcm_framer_init(ck_pcm_framer *f, uint32_t rate, uint8_t channels, uint8_t sample_bytes, uint32_t interval_us) 159 { 160 if (!f) return -1; 161 f->rate = rate; 162 f->channels = channels; 163 f->sample_bytes = sample_bytes; 164 f->interval_us = interval_us; 165 f->remainder = 0; 166 if (!rate || !channels || !sample_bytes || !interval_us) { 167 f->rate = 0; /* unusable: every packet is 0 bytes */ 168 return -1; 169 } 170 return 0; 171 } 172 173 size_t ck_pcm_peek_bytes(const ck_pcm_framer *f) 174 { 175 uint64_t num = (uint64_t)f->rate * f->interval_us + f->remainder; 176 return (size_t)(num / 1000000u) * f->channels * f->sample_bytes; 177 } 178 179 size_t ck_pcm_next_bytes(ck_pcm_framer *f) 180 { 181 uint64_t num = (uint64_t)f->rate * f->interval_us + f->remainder; 182 f->remainder = num % 1000000u; 183 return (size_t)(num / 1000000u) * f->channels * f->sample_bytes; 184 } 185 186 size_t ck_pcm_fill_i16(ck_pcm_framer *f, const int16_t *ring, size_t ring_len, size_t *cursor, int16_t *out, 187 size_t out_len) 188 { 189 size_t samples = ck_pcm_next_bytes(f) / 2; 190 if (samples > out_len) samples = out_len; 191 if (!ring || ring_len == 0) { 192 for (size_t i = 0; i < samples; i++) out[i] = 0; 193 return samples; 194 } 195 for (size_t i = 0; i < samples; i++) { 196 out[i] = ring[*cursor % ring_len]; 197 *cursor = (*cursor + 1) % ring_len; 198 } 199 return samples; 200 } 201 202 static ck_uac_trb isoch_trb(uint64_t buf, uint32_t length, uint16_t frame_id, int ioc) 203 { 204 ck_uac_trb t; 205 t.d[0] = (uint32_t)buf; 206 t.d[1] = (uint32_t)(buf >> 32); 207 t.d[2] = length & 0x1ffffu; 208 /* One packet per TD (no CHAIN), TBC (bits 8:7) zero, SIA clear. */ 209 t.d[3] = ((uint32_t)(frame_id & 0x7ffu) << 20) | (CK_UAC_TRB_ISOCH << 10) | (ioc ? CK_UAC_TRB_IOC : 0u); 210 return t; 211 } 212 213 size_t ck_uac_schedule(ck_pcm_framer *f, uint16_t controller_frame, uint16_t lead_frames, const ck_uac_packet *packets, 214 size_t npackets, const int16_t *ring, size_t ring_len, size_t *cursor, int16_t *pcm, 215 size_t pcm_len, size_t stride, ck_uac_trb *trbs, size_t ntrbs) 216 { 217 if (!f || !packets || !cursor || !pcm || !trbs || stride == 0) return 0; 218 size_t slots = npackets < ntrbs ? npackets : ntrbs; 219 if (pcm_len / stride < slots) slots = pcm_len / stride; 220 size_t count = 0; 221 while (count < slots) { 222 /* Check before consuming PCM: never truncate a packet. */ 223 size_t need = ck_pcm_peek_bytes(f); 224 if (need > packets[count].capacity || need > stride * 2) break; 225 size_t samples = ck_pcm_fill_i16(f, ring, ring_len, cursor, pcm + count * stride, stride); 226 uint16_t frame = (uint16_t)((controller_frame + lead_frames + count) & 0x7ffu); 227 trbs[count] = isoch_trb(packets[count].dma, (uint32_t)(samples * 2), frame, 0); 228 count++; 229 } 230 if (count > 0) trbs[count - 1].d[3] |= CK_UAC_TRB_IOC; 231 return count; 232 } 233 234 int ck_test_tone_init(ck_test_tone *t, uint32_t frequency_hz, uint32_t sample_rate) 235 { 236 if (!t || sample_rate == 0 || frequency_hz >= sample_rate / 2) return -1; 237 t->phase = 0; 238 t->step = (uint32_t)(((uint64_t)frequency_hz << 32) / sample_rate); 239 return 0; 240 } 241 242 int16_t ck_test_tone_next(ck_test_tone *t) 243 { 244 t->phase += t->step; 245 int32_t x = (int32_t)(t->phase >> 16); 246 int32_t tri = x < 32768 ? x : 65535 - x; 247 int64_t y = (4 * (int64_t)tri * (int64_t)(32767 - tri) * 2) / 32767 - 32767; 248 if (y > 32767) y = 32767; 249 if (y < -32767) y = -32767; 250 return (int16_t)y; 251 }