audio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * audio.c
  3. *
  4. * USB Audio class firmware
  5. *
  6. * Copyright (C) 2020 Sylvain Munaut
  7. * All rights reserved.
  8. *
  9. * LGPL v3+, see LICENSE.lgpl3
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include <string.h>
  28. #include "console.h"
  29. #include "led.h"
  30. #include "mini-printf.h"
  31. #include "spi.h"
  32. #include <no2usb/usb.h>
  33. #include <no2usb/usb_ac_proto.h>
  34. #include <no2usb/usb_dfu_rt.h>
  35. #include <no2usb/usb_hw.h>
  36. #include <no2usb/usb_priv.h>
  37. #include "utils.h"
  38. #include "config.h"
  39. // Volume helpers
  40. // ---------------------------------------------------------------------------
  41. /* [round(256*(math.pow(2,i/256.0)-1)) for i in range(256)] */
  42. static const uint8_t vol_log2lin_lut[] = {
  43. 0x00, 0x01, 0x01, 0x02, 0x03, 0x03, 0x04, 0x05,
  44. 0x06, 0x06, 0x07, 0x08, 0x08, 0x09, 0x0a, 0x0b,
  45. 0x0b, 0x0c, 0x0d, 0x0e, 0x0e, 0x0f, 0x10, 0x10,
  46. 0x11, 0x12, 0x13, 0x13, 0x14, 0x15, 0x16, 0x16,
  47. 0x17, 0x18, 0x19, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
  48. 0x1d, 0x1e, 0x1f, 0x20, 0x20, 0x21, 0x22, 0x23,
  49. 0x24, 0x24, 0x25, 0x26, 0x27, 0x28, 0x28, 0x29,
  50. 0x2a, 0x2b, 0x2c, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
  51. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x35, 0x36,
  52. 0x37, 0x38, 0x39, 0x3a, 0x3a, 0x3b, 0x3c, 0x3d,
  53. 0x3e, 0x3f, 0x40, 0x41, 0x41, 0x42, 0x43, 0x44,
  54. 0x45, 0x46, 0x47, 0x48, 0x48, 0x49, 0x4a, 0x4b,
  55. 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x51, 0x52,
  56. 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
  57. 0x5b, 0x5c, 0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x61,
  58. 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  59. 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71,
  60. 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  61. 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81,
  62. 0x82, 0x83, 0x84, 0x85, 0x87, 0x88, 0x89, 0x8a,
  63. 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92,
  64. 0x93, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  65. 0x9c, 0x9d, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
  66. 0xa5, 0xa6, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad,
  67. 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb6, 0xb7,
  68. 0xb8, 0xb9, 0xba, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0,
  69. 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc8, 0xc9, 0xca,
  70. 0xcb, 0xcd, 0xce, 0xcf, 0xd0, 0xd2, 0xd3, 0xd4,
  71. 0xd6, 0xd7, 0xd8, 0xd9, 0xdb, 0xdc, 0xdd, 0xde,
  72. 0xe0, 0xe1, 0xe2, 0xe4, 0xe5, 0xe6, 0xe8, 0xe9,
  73. 0xea, 0xec, 0xed, 0xee, 0xf0, 0xf1, 0xf2, 0xf4,
  74. 0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfc, 0xfd, 0xff,
  75. };
  76. /* [round(math.log2(1.0 + x / 256.0) * 256) for x in range(256)] */
  77. static const uint8_t vol_lin2log_lut[] = {
  78. 0x00, 0x01, 0x03, 0x04, 0x06, 0x07, 0x09, 0x0a,
  79. 0x0b, 0x0d, 0x0e, 0x10, 0x11, 0x12, 0x14, 0x15,
  80. 0x16, 0x18, 0x19, 0x1a, 0x1c, 0x1d, 0x1e, 0x20,
  81. 0x21, 0x22, 0x24, 0x25, 0x26, 0x28, 0x29, 0x2a,
  82. 0x2c, 0x2d, 0x2e, 0x2f, 0x31, 0x32, 0x33, 0x34,
  83. 0x36, 0x37, 0x38, 0x39, 0x3b, 0x3c, 0x3d, 0x3e,
  84. 0x3f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x47, 0x48,
  85. 0x49, 0x4a, 0x4b, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
  86. 0x52, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
  87. 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
  88. 0x64, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
  89. 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x74, 0x75,
  90. 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d,
  91. 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
  92. 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d,
  93. 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
  94. 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9b, 0x9c,
  95. 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
  96. 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xa9, 0xaa, 0xab,
  97. 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb2,
  98. 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xb9,
  99. 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc0,
  100. 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc6, 0xc7,
  101. 0xc8, 0xc9, 0xca, 0xcb, 0xcb, 0xcc, 0xcd, 0xce,
  102. 0xcf, 0xd0, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd4,
  103. 0xd5, 0xd6, 0xd7, 0xd8, 0xd8, 0xd9, 0xda, 0xdb,
  104. 0xdc, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe0, 0xe1,
  105. 0xe2, 0xe3, 0xe4, 0xe4, 0xe5, 0xe6, 0xe7, 0xe7,
  106. 0xe8, 0xe9, 0xea, 0xea, 0xeb, 0xec, 0xed, 0xee,
  107. 0xee, 0xef, 0xf0, 0xf1, 0xf1, 0xf2, 0xf3, 0xf4,
  108. 0xf4, 0xf5, 0xf6, 0xf7, 0xf7, 0xf8, 0xf9, 0xf9,
  109. 0xfa, 0xfb, 0xfc, 0xfc, 0xfd, 0xfe, 0xff, 0xff,
  110. };
  111. #define VOL_INVALID (-32768)
  112. /* 16384 * math.pow(10, x/(20*256)) */
  113. static int16_t
  114. vol_log2lin(int16_t log)
  115. {
  116. uint16_t lin;
  117. int s = 0;
  118. /* Special cases */
  119. if (log == VOL_INVALID) /* Special value */
  120. return 0x0000;
  121. if (log >= 1541) /* Max is ~6 dB */
  122. return 0x7fff;
  123. /* Integer part */
  124. while (log < 0) {
  125. log += 1541;
  126. s += 1;
  127. }
  128. /* LUT */
  129. lin = vol_log2lin_lut[(log * 680) >> 12];
  130. /* Scaling */
  131. lin = (lin << 6) | (lin >> 2) | 0x4000;
  132. lin >>= s;
  133. return lin;
  134. }
  135. /* 20 * 256 * math.log10(lin / 16384) */
  136. static int16_t
  137. vol_lin2log(int16_t lin)
  138. {
  139. int32_t l = 0;
  140. /* Special cases */
  141. if (lin <= 0)
  142. return VOL_INVALID;
  143. /* Integer part */
  144. while (lin < 0x4000) {
  145. lin <<= 1;
  146. l = l - 256;
  147. }
  148. /* LUT correct */
  149. l += vol_lin2log_lut[(lin >> 6) & 0xff];
  150. /* Final scaling */
  151. l = (l * 1541) >> 8;
  152. return (int16_t) l;
  153. }
  154. // PCM Audio
  155. // ---------------------------------------------------------------------------
  156. struct wb_audio_pcm {
  157. uint32_t csr;
  158. uint32_t volume;
  159. uint32_t fifo;
  160. } __attribute__((packed,aligned(4)));
  161. static volatile struct wb_audio_pcm * const pcm_regs = (void*)(AUDIO_PCM_BASE);
  162. static struct {
  163. bool active;
  164. bool mute_all;
  165. struct {
  166. bool mute;
  167. int16_t vol_log;
  168. uint16_t vol_lin;
  169. } chan[2];
  170. uint8_t bdi;
  171. } g_pcm;
  172. static void
  173. pcm_hw_update_volume(void)
  174. {
  175. pcm_regs->volume =
  176. (((!g_pcm.mute_all && !g_pcm.chan[1].mute) ?
  177. g_pcm.chan[1].vol_lin : 0) << 16) |
  178. (((!g_pcm.mute_all && !g_pcm.chan[0].mute) ?
  179. g_pcm.chan[0].vol_lin : 0));
  180. }
  181. static void
  182. pcm_set_volume(uint8_t chan, int16_t vol_log)
  183. {
  184. printf("Volume set %d to %d\n", chan, vol_log);
  185. if (g_pcm.chan[chan].vol_log == vol_log)
  186. return;
  187. g_pcm.chan[chan].vol_lin = vol_log2lin(vol_log);
  188. g_pcm.chan[chan].vol_log = vol_lin2log(g_pcm.chan[chan].vol_lin);
  189. pcm_hw_update_volume();
  190. }
  191. static void
  192. pcm_init(void)
  193. {
  194. /* Local state */
  195. memset(&g_pcm, 0x00, sizeof(g_pcm));
  196. /* Audio enabled at -6 dB by default */
  197. pcm_set_volume(0, -6*256);
  198. pcm_set_volume(1, -6*256);
  199. }
  200. static int
  201. pcm_level(void)
  202. {
  203. return (pcm_regs->csr >> 4) & 0xfff;
  204. }
  205. // Audio USB data
  206. // ---------------------------------------------------------------------------
  207. static void
  208. pcm_usb_fill_feedback_ep(void)
  209. {
  210. /* FIXME figure this out */
  211. #if 0
  212. uint32_t val = 8192;
  213. /* Prepare buffer */
  214. usb_data_write(64, &val, 4);
  215. usb_ep_regs[1].in.bd[0].ptr = 64;
  216. usb_ep_regs[1].in.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(3);
  217. #endif
  218. }
  219. static void
  220. pcm_usb_flow_start(void)
  221. {
  222. /* Reset Buffer index */
  223. g_pcm.bdi = 0;
  224. /* EP 1 OUT: Type=Isochronous, dual buffered */
  225. usb_ep_regs[1].out.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL;
  226. /* EP1 OUT: Queue two buffers */
  227. usb_ep_regs[1].out.bd[0].ptr = 1024;
  228. usb_ep_regs[1].out.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(288);
  229. usb_ep_regs[1].out.bd[1].ptr = 1024 + 288;
  230. usb_ep_regs[1].out.bd[1].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(288);
  231. /* EP1 IN: Type=Isochronous, single buffered */
  232. usb_ep_regs[1].in.status = USB_EP_TYPE_ISOC;
  233. pcm_usb_fill_feedback_ep();
  234. }
  235. static void
  236. pcm_usb_flow_stop(void)
  237. {
  238. /* EP 1 OUT: Disable */
  239. usb_ep_regs[1].out.status = 0;
  240. /* EP 1 IN: Disable */
  241. usb_ep_regs[1].in.status = 0;
  242. /* Stop playing audio */
  243. pcm_regs->csr = 0;
  244. }
  245. static void
  246. pcm_usb_set_active(bool active)
  247. {
  248. if (g_pcm.active == active)
  249. return;
  250. g_pcm.active = active;
  251. if (active)
  252. pcm_usb_flow_start();
  253. else
  254. pcm_usb_flow_stop();
  255. }
  256. static void
  257. pcm_poll(void)
  258. {
  259. /* Check if enough space in FIFO */
  260. if (pcm_level() >= 440)
  261. return;
  262. /* EP BD Status */
  263. uint32_t ptr = usb_ep_regs[1].out.bd[g_pcm.bdi].ptr;
  264. uint32_t csr = usb_ep_regs[1].out.bd[g_pcm.bdi].csr;
  265. /* Check if we have a USB packet */
  266. if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_RDY_DATA)
  267. return;
  268. /* Valid data ? */
  269. if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_OK)
  270. {
  271. static uint32_t lt;
  272. uint32_t ct;
  273. volatile uint32_t __attribute__((aligned(4))) *src_u32 = (volatile uint32_t *)((USB_DATA_BASE) + ptr);
  274. int len = (csr & USB_BD_LEN_MSK) - 2; /* Reported length includes CRC */
  275. for (int i=0; i<len; i+=4)
  276. pcm_regs->fifo = *src_u32++;
  277. ct = usb_get_tick();
  278. if ((ct-lt) > 1)
  279. printf("%d %d %d %d\n", len, pcm_level(), ct-lt, ct);
  280. lt = ct;
  281. /* If we have enough in the FIFO, enable core */
  282. if ((pcm_level() > 200) && !(pcm_regs->csr & 1))
  283. pcm_regs->csr = 1;
  284. }
  285. /* Next transfer */
  286. usb_ep_regs[1].out.bd[g_pcm.bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(288);
  287. g_pcm.bdi ^= 1;
  288. }
  289. // PCM Audio USB control
  290. // ---------------------------------------------------------------------------
  291. static bool
  292. pcm_usb_mute_set(uint16_t wValue, uint8_t *data, int *len)
  293. {
  294. uint8_t chan = wValue & 0xff;
  295. if (chan >= 3)
  296. return false;
  297. if (chan == 0) {
  298. g_pcm.mute_all = data[0];
  299. pcm_hw_update_volume();
  300. } else {
  301. g_pcm.chan[chan-1].mute =data[0];
  302. pcm_hw_update_volume();
  303. }
  304. return true;
  305. }
  306. static bool
  307. pcm_usb_mute_get(uint16_t wValue, uint8_t *data, int *len)
  308. {
  309. uint8_t chan = wValue & 0xff;
  310. if (chan >= 3)
  311. return false;
  312. if (chan == 0) {
  313. data[0] = g_pcm.mute_all;
  314. } else {
  315. data[0] = g_pcm.chan[chan-1].mute;
  316. }
  317. return true;
  318. }
  319. static bool
  320. pcm_usb_volume_set(uint16_t wValue, uint8_t *data, int *len)
  321. {
  322. uint8_t chan = wValue & 0xff;
  323. if ((chan == 0) || (chan >= 3))
  324. return false;
  325. pcm_set_volume(chan, *((int16_t*)data));
  326. return true;
  327. }
  328. static bool
  329. pcm_usb_volume_get(uint16_t wValue, uint8_t *data, int *len)
  330. {
  331. uint8_t chan = wValue & 0xff;
  332. if ((chan == 0) || (chan >= 3))
  333. return false;
  334. *((int16_t*)data) = g_pcm.chan[chan-1].vol_log;
  335. return true;
  336. }
  337. static bool
  338. pcm_usb_volume_min(uint16_t wValue, uint8_t *data, int *len)
  339. {
  340. uint8_t chan = wValue & 0xff;
  341. if ((chan == 0) || (chan >= 3))
  342. return false;
  343. *((int16_t*)data) = (-80 * 256);
  344. return true;
  345. }
  346. static bool
  347. pcm_usb_volume_max(uint16_t wValue, uint8_t *data, int *len)
  348. {
  349. uint8_t chan = wValue & 0xff;
  350. if ((chan == 0) || (chan >= 3))
  351. return false;
  352. *((int16_t*)data) = (5 * 256);
  353. return true;
  354. }
  355. static bool
  356. pcm_usb_volume_res(uint16_t wValue, uint8_t *data, int *len)
  357. {
  358. uint8_t chan = wValue & 0xff;
  359. if ((chan == 0) || (chan >= 3))
  360. return false;
  361. *((int16_t*)data) = (256 / 2);
  362. return true;
  363. }
  364. // MIDI
  365. // ---------------------------------------------------------------------------
  366. struct wb_uart {
  367. uint32_t data;
  368. uint32_t clkdiv;
  369. } __attribute__((packed,aligned(4)));
  370. static volatile struct wb_uart * const midi_regs = (void*)(MIDI_BASE);
  371. void
  372. midi_usb_set_conf(void)
  373. {
  374. /* EP 2 OUT: Type=Bulk, single buffered */
  375. usb_ep_regs[2].out.status = USB_EP_TYPE_BULK;
  376. /* Fill a buffer */
  377. usb_ep_regs[2].out.bd[0].ptr = 1536;
  378. usb_ep_regs[2].out.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(64);
  379. }
  380. static const int
  381. midi_pkt[16] = {
  382. -1, /* 0x0 Miscellaneous function codes. Reserved for future extensions */
  383. -1, /* 0x1 Cable events. Reserved for future expansion */
  384. 2, /* 0x2 Two-byte System Common messages like MTC, SongSelect, etc */
  385. 3, /* 0x3 Three-byte System Common messages like SPP, etc */
  386. 3, /* 0x4 SysEx starts or continues */
  387. 1, /* 0x5 SysEx ends with following single byte */
  388. 2, /* 0x6 SysEx ends with following two bytes */
  389. 3, /* 0x7 SysEx ends with following three bytes */
  390. 3, /* 0x8 Note-off */
  391. 3, /* 0x9 Note-on */
  392. 3, /* 0xa Poly-KeyPress */
  393. 3, /* 0xb Control Change */
  394. 2, /* 0xc Program Change */
  395. 2, /* 0xd Channel Pressure */
  396. 3, /* 0xe PitchBend Change */
  397. 1, /* 0xf Single Byte */
  398. };
  399. static void
  400. midi_poll(void)
  401. {
  402. /* EP BD Status */
  403. uint32_t ptr = usb_ep_regs[2].out.bd[0].ptr;
  404. uint32_t csr = usb_ep_regs[2].out.bd[0].csr;
  405. /* Check if we have a USB packet */
  406. if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_RDY_DATA)
  407. return;
  408. /* Valid data ? */
  409. if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_OK)
  410. {
  411. uint32_t midi[64];
  412. int len = (csr & USB_BD_LEN_MSK) - 2; /* Reported length includes CRC */
  413. usb_data_read(midi, ptr, len);
  414. for (int i=0; i<(len>>2); i++) {
  415. uint32_t w = midi[i];
  416. int bl = midi_pkt[w & 0xf];
  417. w >>= 8;
  418. while (bl-- > 0) {
  419. midi_regs->data = (w & 0xff);
  420. w >>= 8;
  421. }
  422. }
  423. }
  424. /* Next transfer */
  425. usb_ep_regs[2].out.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(64);
  426. }
  427. static void
  428. midi_init(void)
  429. {
  430. /* 31250 baud with 24MHz system clk */
  431. midi_regs->clkdiv = 768;
  432. }
  433. // Shared USB driver
  434. // ---------------------------------------------------------------------------
  435. /* Control handler structs */
  436. typedef bool (*usb_audio_control_fn)(uint16_t wValue, uint8_t *data, int *len);
  437. struct usb_audio_control_handler {
  438. int len;
  439. usb_audio_control_fn set_cur;
  440. usb_audio_control_fn get_cur;
  441. usb_audio_control_fn get_min;
  442. usb_audio_control_fn get_max;
  443. usb_audio_control_fn get_res;
  444. };
  445. struct usb_audio_req_handler {
  446. uint8_t rcpt; /* USB_REQ_RCPT_INTF or USB_REQ_RCPT_EP */
  447. uint8_t idx; /* Interface or EP index */
  448. uint8_t entity_id;
  449. uint16_t val_match;
  450. uint16_t val_mask;
  451. const struct usb_audio_control_handler *h;
  452. };
  453. /* Control handlers for this implementation */
  454. static const struct usb_audio_control_handler _uac_mute = { /* USB_AC_FU_CONTROL_MUTE */
  455. .len = 1,
  456. .set_cur = pcm_usb_mute_set,
  457. .get_cur = pcm_usb_mute_get,
  458. };
  459. static const struct usb_audio_control_handler _uac_volume = { /* USB_AC_FU_CONTROL_VOLUME */
  460. .len = 2,
  461. .set_cur = pcm_usb_volume_set,
  462. .get_cur = pcm_usb_volume_get,
  463. .get_min = pcm_usb_volume_min,
  464. .get_max = pcm_usb_volume_max,
  465. .get_res = pcm_usb_volume_res,
  466. };
  467. #define INTF_AUDIO_CONTROL 1
  468. #define UNIT_FEATURE 2
  469. static const struct usb_audio_req_handler _uac_handlers[] = {
  470. { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEATURE, (USB_AC_FU_CONTROL_MUTE << 8), 0xff00, &_uac_mute },
  471. { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEATURE, (USB_AC_FU_CONTROL_VOLUME << 8), 0xff00, &_uac_volume },
  472. { 0 }
  473. };
  474. /* USB driver implemntation (including control handler dispatch */
  475. static struct {
  476. struct usb_ctrl_req *req;
  477. usb_audio_control_fn fn;
  478. } g_cb_ctx;
  479. static bool
  480. audio_ctrl_req_cb(struct usb_xfer *xfer)
  481. {
  482. struct usb_ctrl_req *req = g_cb_ctx.req;
  483. usb_audio_control_fn fn = g_cb_ctx.fn;
  484. return fn(req->wValue, xfer->data, &xfer->len);
  485. }
  486. static enum usb_fnd_resp
  487. audio_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
  488. {
  489. const struct usb_audio_req_handler *rh;
  490. /* Check it's a class request to an interface */
  491. if (USB_REQ_TYPE(req) != USB_REQ_TYPE_CLASS)
  492. return USB_FND_CONTINUE;
  493. /* Check R/W consitency */
  494. /* The control request ID mirrors the read flag in the MSB */
  495. if ((req->bmRequestType ^ req->bRequest) & 0x80)
  496. return USB_FND_ERROR;
  497. /* Find a matching handler */
  498. for (rh=&_uac_handlers[0]; rh->rcpt; rh++)
  499. {
  500. usb_audio_control_fn fn = NULL;
  501. /* Check recipient type and index */
  502. if (USB_REQ_RCPT(req) != rh->rcpt)
  503. continue;
  504. if ((req->wIndex & 0xff) != rh->idx)
  505. continue;
  506. /* Check Entity ID */
  507. if ((req->wIndex >> 8) != rh->entity_id)
  508. continue;
  509. /* Check control */
  510. if ((req->wValue & rh->val_mask) != rh->val_match)
  511. continue;
  512. /* We have a match, first check it's not a NOP and check length */
  513. if (!rh->h)
  514. return USB_FND_ERROR;
  515. if ((rh->h->len != -1) && (rh->h->len != req->wLength))
  516. return USB_FND_ERROR;
  517. /* Then grab appropriate function */
  518. switch (req->bRequest)
  519. {
  520. case USB_REQ_AC_SET_CUR:
  521. fn = rh->h->set_cur;
  522. break;
  523. case USB_REQ_AC_GET_CUR:
  524. fn = rh->h->get_cur;
  525. break;
  526. case USB_REQ_AC_GET_MIN:
  527. fn = rh->h->get_min;
  528. break;
  529. case USB_REQ_AC_GET_MAX:
  530. fn = rh->h->get_max;
  531. break;
  532. case USB_REQ_AC_GET_RES:
  533. fn = rh->h->get_res;
  534. break;
  535. default:
  536. fn = NULL;
  537. }
  538. if (!fn)
  539. return USB_FND_ERROR;
  540. /* And try to call it */
  541. if (USB_REQ_IS_READ(req)) {
  542. /* Request is a read, we can call handler immediately */
  543. xfer->len = req->wLength;
  544. return fn(req->wValue, xfer->data, &xfer->len) ? USB_FND_SUCCESS : USB_FND_ERROR;
  545. } else {
  546. /* Request is a write, we need to hold off until end of data phase */
  547. g_cb_ctx.req = req;
  548. g_cb_ctx.fn = fn;
  549. xfer->len = req->wLength;
  550. xfer->cb_done = audio_ctrl_req_cb;
  551. return USB_FND_SUCCESS;
  552. }
  553. }
  554. return USB_FND_ERROR;
  555. }
  556. static enum usb_fnd_resp
  557. audio_set_conf(const struct usb_conf_desc *conf)
  558. {
  559. /* Default PCM interface is inactive */
  560. pcm_usb_set_active(false);
  561. /* MIDI EP config */
  562. midi_usb_set_conf();
  563. return USB_FND_SUCCESS;
  564. }
  565. static enum usb_fnd_resp
  566. audio_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
  567. {
  568. /* Check it's audio class */
  569. if (base->bInterfaceClass != 0x01)
  570. return USB_FND_CONTINUE;
  571. /* Sub class */
  572. switch (base->bInterfaceSubClass)
  573. {
  574. case USB_AC_SCLS_AUDIOCONTROL:
  575. case USB_AC_SCLS_MIDISTREAMING:
  576. return USB_FND_SUCCESS;
  577. case USB_AC_SCLS_AUDIOSTREAMING:
  578. pcm_usb_set_active(sel->bAlternateSetting != 0);
  579. return USB_FND_SUCCESS;
  580. default:
  581. return USB_FND_ERROR;
  582. }
  583. }
  584. static enum usb_fnd_resp
  585. audio_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
  586. {
  587. /* Check it's audio class */
  588. if (base->bInterfaceClass != 0x01)
  589. return USB_FND_CONTINUE;
  590. /* Sub class */
  591. switch (base->bInterfaceSubClass)
  592. {
  593. case USB_AC_SCLS_AUDIOCONTROL:
  594. case USB_AC_SCLS_MIDISTREAMING:
  595. *alt = 0;
  596. return USB_FND_SUCCESS;
  597. case USB_AC_SCLS_AUDIOSTREAMING:
  598. *alt = g_pcm.active ? 1 : 0;
  599. return USB_FND_SUCCESS;
  600. default:
  601. return USB_FND_ERROR;
  602. }
  603. }
  604. static struct usb_fn_drv _audio_drv = {
  605. .ctrl_req = audio_ctrl_req,
  606. .set_conf = audio_set_conf,
  607. .set_intf = audio_set_intf,
  608. .get_intf = audio_get_intf,
  609. };
  610. // Exposed API
  611. // ---------------------------------------------------------------------------
  612. void
  613. audio_init(void)
  614. {
  615. /* Init hardware */
  616. pcm_init();
  617. midi_init();
  618. /* Register function driver */
  619. usb_register_function_driver(&_audio_drv);
  620. }
  621. void
  622. audio_poll(void)
  623. {
  624. pcm_poll();
  625. midi_poll();
  626. }
  627. void
  628. audio_debug_print(void)
  629. {
  630. uint32_t csr = pcm_regs->csr;
  631. printf("Audio PCM tick : %04x\n", csr >> 16);
  632. printf("Audio PCM FIFO level : %d\n", (csr >> 4) & 0xfff);
  633. printf("Audio PCM State : %d\n", csr & 3);
  634. }