audio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 "mc97.h"
  30. #include <no2usb/usb.h>
  31. #include <no2usb/usb_ac_proto.h>
  32. #include <no2usb/usb_dfu_rt.h>
  33. #include <no2usb/usb_hw.h>
  34. #include <no2usb/usb_priv.h>
  35. #include "config.h"
  36. #define INTF_AUDIO_CONTROL 1
  37. #define INTF_AUDIO_DATA_IN 2
  38. #define INTF_AUDIO_DATA_OUT 3
  39. #define UNIT_FEAT_PCM_IN 2
  40. #define UNIT_FEAT_PCM_OUT 5
  41. #define PKT_SIZE_SAMP 60
  42. #define PKT_SIZE_BYTE 120
  43. // PCM Audio
  44. // ---------------------------------------------------------------------------
  45. static struct {
  46. bool active;
  47. uint8_t bdi;
  48. } g_pcm[2];
  49. static void
  50. pcm_init(void)
  51. {
  52. /* Local state */
  53. memset(&g_pcm, 0x00, sizeof(g_pcm));
  54. /* Init MC97 */
  55. mc97_init();
  56. }
  57. // PCM Audio USB helpers
  58. // ---------------------------------------------------------------------------
  59. static int
  60. _idx_from_req(struct usb_ctrl_req *req)
  61. {
  62. int unit_id = (req->wIndex >> 8) & 0xff;
  63. int chan = req->wValue & 0xff;
  64. if (chan != 0)
  65. return -1;
  66. switch (unit_id)
  67. {
  68. case UNIT_FEAT_PCM_IN: return 0;
  69. case UNIT_FEAT_PCM_OUT: return 1;
  70. }
  71. return -1;
  72. }
  73. // PCM Audio USB data
  74. // ---------------------------------------------------------------------------
  75. static void
  76. pcm_usb_configure(const struct usb_conf_desc *conf)
  77. {
  78. const struct usb_intf_desc *intf;
  79. /* Reset state */
  80. g_pcm[0].bdi = 0;
  81. g_pcm[1].bdi = 0;
  82. /* Boot PCM input */
  83. intf = usb_desc_find_intf(conf, INTF_AUDIO_DATA_IN, 0, NULL);
  84. usb_ep_boot(intf, 0x81, true);
  85. /* Boot PCM output */
  86. intf = usb_desc_find_intf(conf, INTF_AUDIO_DATA_OUT, 0, NULL);
  87. usb_ep_boot(intf, 0x01, true);
  88. usb_ep_boot(intf, 0x82, false);
  89. /* MC97 flow reset */
  90. mc97_flow_rx_reset();
  91. mc97_flow_tx_reset();
  92. }
  93. static bool
  94. pcm_usb_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
  95. {
  96. switch (base->bInterfaceNumber)
  97. {
  98. case INTF_AUDIO_DATA_IN:
  99. /* If same state, don't do anything */
  100. if (sel->bAlternateSetting == g_pcm[0].active)
  101. break;
  102. g_pcm[0].active = sel->bAlternateSetting;
  103. /* Reset BDI and reconfigure EPs */
  104. g_pcm[0].bdi = 0;
  105. usb_ep_reconf(sel, 0x81);
  106. /* MC97 data flow */
  107. if (!g_pcm[0].active)
  108. mc97_flow_rx_reset();
  109. else
  110. mc97_flow_rx_start();
  111. break;
  112. case INTF_AUDIO_DATA_OUT:
  113. /* If same state, don't do anything */
  114. if (sel->bAlternateSetting == g_pcm[1].active)
  115. break;
  116. g_pcm[1].active = sel->bAlternateSetting;
  117. /* Reset BDI and reconfigure EPs */
  118. g_pcm[1].bdi = 0;
  119. usb_ep_reconf(sel, 0x01);
  120. usb_ep_reconf(sel, 0x82);
  121. /* MC97 data flow */
  122. if (!g_pcm[1].active)
  123. mc97_flow_tx_reset();
  124. /* If active, pre-queue two buffers */
  125. if (g_pcm[1].active) {
  126. usb_ep_regs[1].out.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(PKT_SIZE_BYTE);
  127. usb_ep_regs[1].out.bd[1].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(PKT_SIZE_BYTE);
  128. }
  129. break;
  130. default:
  131. return false;
  132. }
  133. return true;
  134. }
  135. static bool
  136. pcm_usb_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
  137. {
  138. switch (base->bInterfaceNumber)
  139. {
  140. case INTF_AUDIO_DATA_IN:
  141. *alt = g_pcm[0].active;
  142. break;
  143. case INTF_AUDIO_DATA_OUT:
  144. *alt = g_pcm[1].active;
  145. break;
  146. default:
  147. return false;
  148. }
  149. return true;
  150. }
  151. static void
  152. pcm_poll_feedback_ep(void)
  153. {
  154. static int rate;
  155. uint32_t val;
  156. /* If not active, reset state */
  157. if (!g_pcm[1].active) {
  158. rate = 8 * 16384;
  159. return;
  160. }
  161. /* Fetch current level and flow active status */
  162. int lvl = mc97_flow_tx_level();
  163. bool active = mc97_flow_tx_active();
  164. /* If flow isn't running, don't run the algo */
  165. if (!active)
  166. return;
  167. /* If previous packet isn't sent, don't run the algo */
  168. if ((usb_ep_regs[2].in.bd[0].csr & USB_BD_STATE_MSK) == USB_BD_STATE_RDY_DATA)
  169. return;
  170. /* Level alerts */
  171. if ((lvl < 32) || (lvl > 224))
  172. printf("LEVEL ALERT: %d (%d)\n", lvl, (rate >> 14));
  173. /* Adapt the rate depending on fifo level */
  174. rate += ((MC97_FIFO_SIZE / 2) - lvl) << 4;
  175. if (rate > (9 * 16384))
  176. rate = 9 * 16384;
  177. else if (rate < (7 * 16384))
  178. rate = 7 * 16384;
  179. /* Set rate */
  180. val = rate;
  181. /* Prepare buffer */
  182. usb_data_write(usb_ep_regs[2].in.bd[0].ptr, &val, 4);
  183. usb_ep_regs[2].in.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(3);
  184. }
  185. static void
  186. pcm_poll_in(void)
  187. {
  188. int16_t buf[PKT_SIZE_SAMP];
  189. /* Active ? */
  190. if (!g_pcm[0].active)
  191. return;
  192. /* Fill as many BDs as we can */
  193. while (1) {
  194. uint32_t csr = usb_ep_regs[1].in.bd[g_pcm[0].bdi].csr;
  195. uint32_t ptr = usb_ep_regs[1].in.bd[g_pcm[0].bdi].ptr;
  196. /* Is that BD free ? */
  197. if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_RDY_DATA)
  198. break;
  199. /* Read data from MC97 link */
  200. int n = mc97_flow_rx_pull(buf, PKT_SIZE_SAMP);
  201. if (!n)
  202. break;
  203. /* Submit what we got */
  204. usb_data_write(ptr, buf, PKT_SIZE_BYTE);
  205. usb_ep_regs[1].in.bd[g_pcm[0].bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(n*2);
  206. g_pcm[0].bdi ^= 1;
  207. /* If packet wasn't full, wait for the next iteration */
  208. if (n < PKT_SIZE_SAMP)
  209. break;
  210. }
  211. }
  212. static void
  213. pcm_poll_out(void)
  214. {
  215. int16_t buf[PKT_SIZE_SAMP];
  216. /* Active ? */
  217. if (!g_pcm[1].active)
  218. return;
  219. /* Starting level */
  220. int lvl = mc97_flow_tx_level();
  221. bool active = mc97_flow_tx_active();
  222. /* Refill process ? */
  223. if (!lvl & active)
  224. mc97_flow_tx_stop();
  225. /* Empty as many BDs as we can */
  226. while (1) {
  227. uint32_t csr = usb_ep_regs[1].out.bd[g_pcm[1].bdi].csr;
  228. uint32_t ptr = usb_ep_regs[1].out.bd[g_pcm[1].bdi].ptr;
  229. /* Is that BD pending ? */
  230. if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_RDY_DATA)
  231. break;
  232. /* Pull valid data */
  233. if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_OK)
  234. {
  235. int n = ((csr & USB_BD_LEN_MSK) - 2) / 2; /* Reported length includes CRC */
  236. /* If it doesn't fit, we're done for now */
  237. if ((lvl + n) > MC97_FIFO_SIZE)
  238. break;
  239. lvl += n;
  240. /* Read and push */
  241. if (n) {
  242. usb_data_read(buf, ptr, PKT_SIZE_BYTE);
  243. mc97_flow_tx_push(buf, n);
  244. }
  245. }
  246. /* Reprepare and move on */
  247. usb_ep_regs[1].out.bd[g_pcm[1].bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(PKT_SIZE_BYTE);
  248. g_pcm[1].bdi ^= 1;
  249. }
  250. /* Delayed enable */
  251. if ((lvl > (MC97_FIFO_SIZE/2)) && !active)
  252. mc97_flow_tx_start();
  253. }
  254. static void
  255. pcm_poll(void)
  256. {
  257. pcm_poll_in();
  258. pcm_poll_out();
  259. pcm_poll_feedback_ep();
  260. }
  261. // PCM Audio USB control
  262. // ---------------------------------------------------------------------------
  263. static bool
  264. pcm_usb_mute_set(struct usb_ctrl_req *req, uint8_t *data, int *len)
  265. {
  266. int idx = _idx_from_req(req);
  267. switch (idx) {
  268. case 0: mc97_set_rx_mute(data[0]); return true;
  269. case 1: mc97_set_tx_mute(data[0]); return true;
  270. }
  271. return false;
  272. }
  273. static bool
  274. pcm_usb_mute_get(struct usb_ctrl_req *req, uint8_t *data, int *len)
  275. {
  276. int idx = _idx_from_req(req);
  277. switch (idx) {
  278. case 0: data[0] = mc97_get_rx_mute(); return true;
  279. case 1: data[0] = mc97_get_tx_mute(); return true;
  280. }
  281. return false;
  282. }
  283. #define BOUND(x, a, b) (((x)<(a))?(a):(((x)>(b))?(b):(x)))
  284. static bool
  285. pcm_usb_volume_set(struct usb_ctrl_req *req, uint8_t *data, int *len)
  286. {
  287. int idx = _idx_from_req(req);
  288. int16_t vol = *((int16_t*)data);
  289. switch (idx) {
  290. case 0:
  291. vol = BOUND(vol, 0, 5760) >> 7;
  292. mc97_set_rx_gain(vol);
  293. return true;
  294. case 1:
  295. vol = BOUND(-vol, 0, 5760) >> 7;
  296. mc97_set_tx_attenuation(vol);
  297. return true;
  298. }
  299. return false;
  300. }
  301. static bool
  302. pcm_usb_volume_get(struct usb_ctrl_req *req, uint8_t *data, int *len)
  303. {
  304. int idx = _idx_from_req(req);
  305. switch (idx) {
  306. case 0: *((int16_t*)data) = (mc97_get_rx_gain() << 7); return true;
  307. case 1: *((int16_t*)data) = -(mc97_get_tx_attenuation() << 7); return true;
  308. }
  309. return false;
  310. }
  311. static bool
  312. pcm_usb_volume_min(struct usb_ctrl_req *req, uint8_t *data, int *len)
  313. {
  314. const int16_t max[] = { 0 /* 0 dB gain */, -5760 /* 22.5 dB attenuation */ };
  315. int idx;
  316. if ((idx = _idx_from_req(req)) < 0)
  317. return false;
  318. *((int16_t*)data) = max[idx];
  319. return true;
  320. }
  321. static bool
  322. pcm_usb_volume_max(struct usb_ctrl_req *req, uint8_t *data, int *len)
  323. {
  324. const int16_t max[] = { 5760 /* 22.5 dB gain */, 0 /* 0 dB attenuation */ };
  325. int idx;
  326. if ((idx = _idx_from_req(req)) < 0)
  327. return false;
  328. *((int16_t*)data) = max[idx];
  329. return true;
  330. }
  331. static bool
  332. pcm_usb_volume_res(struct usb_ctrl_req *req, uint8_t *data, int *len)
  333. {
  334. const int16_t res[] = { 384, 384 }; /* 1.5 dB resolution */
  335. int idx;
  336. if ((idx = _idx_from_req(req)) < 0)
  337. return false;
  338. *((int16_t*)data) = res[idx];
  339. return true;
  340. }
  341. // Shared USB driver
  342. // ---------------------------------------------------------------------------
  343. /* Control handler structs */
  344. typedef bool (*usb_audio_control_fn)(struct usb_ctrl_req *req, uint8_t *data, int *len);
  345. struct usb_audio_control_handler {
  346. int len;
  347. usb_audio_control_fn set_cur;
  348. usb_audio_control_fn get_cur;
  349. usb_audio_control_fn get_min;
  350. usb_audio_control_fn get_max;
  351. usb_audio_control_fn get_res;
  352. };
  353. struct usb_audio_req_handler {
  354. uint8_t rcpt; /* USB_REQ_RCPT_INTF or USB_REQ_RCPT_EP */
  355. uint8_t idx; /* Interface or EP index */
  356. uint8_t entity_id;
  357. uint16_t val_match;
  358. uint16_t val_mask;
  359. const struct usb_audio_control_handler *h;
  360. };
  361. /* Control handlers for this implementation */
  362. static const struct usb_audio_control_handler _uac_mute = { /* USB_AC_FU_CONTROL_MUTE */
  363. .len = 1,
  364. .set_cur = pcm_usb_mute_set,
  365. .get_cur = pcm_usb_mute_get,
  366. };
  367. static const struct usb_audio_control_handler _uac_volume = { /* USB_AC_FU_CONTROL_VOLUME */
  368. .len = 2,
  369. .set_cur = pcm_usb_volume_set,
  370. .get_cur = pcm_usb_volume_get,
  371. .get_min = pcm_usb_volume_min,
  372. .get_max = pcm_usb_volume_max,
  373. .get_res = pcm_usb_volume_res,
  374. };
  375. static const struct usb_audio_req_handler _uac_handlers[] = {
  376. { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEAT_PCM_IN, (USB_AC_FU_CONTROL_MUTE << 8), 0xff00, &_uac_mute },
  377. { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEAT_PCM_IN, (USB_AC_FU_CONTROL_VOLUME << 8), 0xff00, &_uac_volume },
  378. { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEAT_PCM_OUT, (USB_AC_FU_CONTROL_MUTE << 8), 0xff00, &_uac_mute },
  379. { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEAT_PCM_OUT, (USB_AC_FU_CONTROL_VOLUME << 8), 0xff00, &_uac_volume },
  380. { 0 }
  381. };
  382. /* USB driver implemntation (including control handler dispatch */
  383. static struct {
  384. struct usb_ctrl_req *req;
  385. usb_audio_control_fn fn;
  386. } g_cb_ctx;
  387. static bool
  388. audio_ctrl_req_cb(struct usb_xfer *xfer)
  389. {
  390. struct usb_ctrl_req *req = g_cb_ctx.req;
  391. usb_audio_control_fn fn = g_cb_ctx.fn;
  392. return fn(req, xfer->data, &xfer->len);
  393. }
  394. static enum usb_fnd_resp
  395. audio_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
  396. {
  397. const struct usb_audio_req_handler *rh;
  398. /* Check it's a class request */
  399. if (USB_REQ_TYPE(req) != USB_REQ_TYPE_CLASS)
  400. return USB_FND_CONTINUE;
  401. /* Check R/W consitency */
  402. /* The control request ID mirrors the read flag in the MSB */
  403. if ((req->bmRequestType ^ req->bRequest) & 0x80)
  404. return USB_FND_ERROR;
  405. /* Find a matching handler */
  406. for (rh=&_uac_handlers[0]; rh->rcpt; rh++)
  407. {
  408. usb_audio_control_fn fn = NULL;
  409. /* Check recipient type and index */
  410. if (USB_REQ_RCPT(req) != rh->rcpt)
  411. continue;
  412. if ((req->wIndex & 0xff) != rh->idx)
  413. continue;
  414. /* Check Entity ID */
  415. if ((req->wIndex >> 8) != rh->entity_id)
  416. continue;
  417. /* Check control */
  418. if ((req->wValue & rh->val_mask) != rh->val_match)
  419. continue;
  420. /* We have a match, first check it's not a NOP and check length */
  421. if (!rh->h)
  422. return USB_FND_ERROR;
  423. if ((rh->h->len != -1) && (rh->h->len != req->wLength))
  424. return USB_FND_ERROR;
  425. /* Then grab appropriate function */
  426. switch (req->bRequest)
  427. {
  428. case USB_REQ_AC_SET_CUR:
  429. fn = rh->h->set_cur;
  430. break;
  431. case USB_REQ_AC_GET_CUR:
  432. fn = rh->h->get_cur;
  433. break;
  434. case USB_REQ_AC_GET_MIN:
  435. fn = rh->h->get_min;
  436. break;
  437. case USB_REQ_AC_GET_MAX:
  438. fn = rh->h->get_max;
  439. break;
  440. case USB_REQ_AC_GET_RES:
  441. fn = rh->h->get_res;
  442. break;
  443. default:
  444. fn = NULL;
  445. }
  446. if (!fn)
  447. return USB_FND_ERROR;
  448. /* And try to call it */
  449. if (USB_REQ_IS_READ(req)) {
  450. /* Request is a read, we can call handler immediately */
  451. xfer->len = req->wLength;
  452. return fn(req, xfer->data, &xfer->len) ? USB_FND_SUCCESS : USB_FND_ERROR;
  453. } else {
  454. /* Request is a write, we need to hold off until end of data phase */
  455. g_cb_ctx.req = req;
  456. g_cb_ctx.fn = fn;
  457. xfer->len = req->wLength;
  458. xfer->cb_done = audio_ctrl_req_cb;
  459. return USB_FND_SUCCESS;
  460. }
  461. }
  462. return USB_FND_ERROR;
  463. }
  464. static enum usb_fnd_resp
  465. audio_set_conf(const struct usb_conf_desc *conf)
  466. {
  467. /* Default PCM interface is inactive */
  468. pcm_usb_configure(conf);
  469. return USB_FND_SUCCESS;
  470. }
  471. static enum usb_fnd_resp
  472. audio_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
  473. {
  474. /* Check it's audio class */
  475. if (base->bInterfaceClass != USB_CLS_AUDIO)
  476. return USB_FND_CONTINUE;
  477. /* Sub class */
  478. switch (base->bInterfaceSubClass)
  479. {
  480. case USB_AC_SCLS_AUDIOCONTROL:
  481. return USB_FND_SUCCESS;
  482. case USB_AC_SCLS_AUDIOSTREAMING:
  483. return pcm_usb_set_intf(base, sel) ? USB_FND_SUCCESS : USB_FND_ERROR;
  484. default:
  485. return USB_FND_ERROR;
  486. }
  487. }
  488. static enum usb_fnd_resp
  489. audio_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
  490. {
  491. /* Check it's audio class */
  492. if (base->bInterfaceClass != USB_CLS_AUDIO)
  493. return USB_FND_CONTINUE;
  494. /* Sub class */
  495. switch (base->bInterfaceSubClass)
  496. {
  497. case USB_AC_SCLS_AUDIOCONTROL:
  498. *alt = 0;
  499. return USB_FND_SUCCESS;
  500. case USB_AC_SCLS_AUDIOSTREAMING:
  501. return pcm_usb_get_intf(base, alt) ? USB_FND_SUCCESS : USB_FND_ERROR;
  502. default:
  503. return USB_FND_ERROR;
  504. }
  505. }
  506. static struct usb_fn_drv _audio_drv = {
  507. .ctrl_req = audio_ctrl_req,
  508. .set_conf = audio_set_conf,
  509. .set_intf = audio_set_intf,
  510. .get_intf = audio_get_intf,
  511. };
  512. // Exposed API
  513. // ---------------------------------------------------------------------------
  514. void
  515. audio_init(void)
  516. {
  517. /* Init hardware */
  518. pcm_init();
  519. /* Register function driver */
  520. usb_register_function_driver(&_audio_drv);
  521. }
  522. void
  523. audio_poll(void)
  524. {
  525. pcm_poll();
  526. }