123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680 |
- #include <stdint.h>
- #include <stdbool.h>
- #include <string.h>
- #include "console.h"
- #include "mc97.h"
- #include <no2usb/usb.h>
- #include <no2usb/usb_ac_proto.h>
- #include <no2usb/usb_dfu_rt.h>
- #include <no2usb/usb_hw.h>
- #include <no2usb/usb_priv.h>
- #include "config.h"
- #define INTF_AUDIO_CONTROL 1
- #define INTF_AUDIO_DATA_IN 2
- #define INTF_AUDIO_DATA_OUT 3
- #define UNIT_FEAT_PCM_IN 2
- #define UNIT_FEAT_PCM_OUT 5
- #define PKT_SIZE_SAMP 60
- #define PKT_SIZE_BYTE 120
- static struct {
- bool active;
- uint8_t bdi;
- } g_pcm[2];
- static void
- pcm_init(void)
- {
-
- memset(&g_pcm, 0x00, sizeof(g_pcm));
-
- mc97_init();
- }
- static int
- _idx_from_req(struct usb_ctrl_req *req)
- {
- int unit_id = (req->wIndex >> 8) & 0xff;
- int chan = req->wValue & 0xff;
- if (chan != 0)
- return -1;
- switch (unit_id)
- {
- case UNIT_FEAT_PCM_IN: return 0;
- case UNIT_FEAT_PCM_OUT: return 1;
- }
- return -1;
- }
- static void
- pcm_usb_configure(const struct usb_conf_desc *conf)
- {
- const struct usb_intf_desc *intf;
-
- g_pcm[0].bdi = 0;
- g_pcm[1].bdi = 0;
-
- intf = usb_desc_find_intf(conf, INTF_AUDIO_DATA_IN, 0, NULL);
- usb_ep_boot(intf, 0x81, true);
-
- intf = usb_desc_find_intf(conf, INTF_AUDIO_DATA_OUT, 0, NULL);
- usb_ep_boot(intf, 0x01, true);
- usb_ep_boot(intf, 0x82, false);
-
- mc97_flow_rx_reset();
- mc97_flow_tx_reset();
- }
- static bool
- pcm_usb_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
- {
- switch (base->bInterfaceNumber)
- {
- case INTF_AUDIO_DATA_IN:
-
- if (sel->bAlternateSetting == g_pcm[0].active)
- break;
- g_pcm[0].active = sel->bAlternateSetting;
-
- g_pcm[0].bdi = 0;
- usb_ep_reconf(sel, 0x81);
-
- if (!g_pcm[0].active)
- mc97_flow_rx_reset();
- else
- mc97_flow_rx_start();
- break;
- case INTF_AUDIO_DATA_OUT:
-
- if (sel->bAlternateSetting == g_pcm[1].active)
- break;
- g_pcm[1].active = sel->bAlternateSetting;
-
- g_pcm[1].bdi = 0;
- usb_ep_reconf(sel, 0x01);
- usb_ep_reconf(sel, 0x82);
-
- if (!g_pcm[1].active)
- mc97_flow_tx_reset();
-
- if (g_pcm[1].active) {
- usb_ep_regs[1].out.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(PKT_SIZE_BYTE);
- usb_ep_regs[1].out.bd[1].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(PKT_SIZE_BYTE);
- }
- break;
- default:
- return false;
- }
- return true;
- }
- static bool
- pcm_usb_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
- {
- switch (base->bInterfaceNumber)
- {
- case INTF_AUDIO_DATA_IN:
- *alt = g_pcm[0].active;
- break;
- case INTF_AUDIO_DATA_OUT:
- *alt = g_pcm[1].active;
- break;
- default:
- return false;
- }
- return true;
- }
- static void
- pcm_poll_feedback_ep(void)
- {
- static int rate;
- uint32_t val;
-
- if (!g_pcm[1].active) {
- rate = 8 * 16384;
- return;
- }
-
- int lvl = mc97_flow_tx_level();
- bool active = mc97_flow_tx_active();
-
- if (!active)
- return;
-
- if ((usb_ep_regs[2].in.bd[0].csr & USB_BD_STATE_MSK) == USB_BD_STATE_RDY_DATA)
- return;
-
- if ((lvl < 32) || (lvl > 224))
- printf("LEVEL ALERT: %d (%d)\n", lvl, (rate >> 14));
-
- rate += ((MC97_FIFO_SIZE / 2) - lvl) << 4;
- if (rate > (9 * 16384))
- rate = 9 * 16384;
- else if (rate < (7 * 16384))
- rate = 7 * 16384;
-
- val = rate;
-
- usb_data_write(usb_ep_regs[2].in.bd[0].ptr, &val, 4);
- usb_ep_regs[2].in.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(3);
- }
- static void
- pcm_poll_in(void)
- {
- int16_t buf[PKT_SIZE_SAMP];
-
- if (!g_pcm[0].active)
- return;
-
- while (1) {
- uint32_t csr = usb_ep_regs[1].in.bd[g_pcm[0].bdi].csr;
- uint32_t ptr = usb_ep_regs[1].in.bd[g_pcm[0].bdi].ptr;
-
- if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_RDY_DATA)
- break;
-
- int n = mc97_flow_rx_pull(buf, PKT_SIZE_SAMP);
- if (!n)
- break;
-
- usb_data_write(ptr, buf, PKT_SIZE_BYTE);
- usb_ep_regs[1].in.bd[g_pcm[0].bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(n*2);
- g_pcm[0].bdi ^= 1;
-
- if (n < PKT_SIZE_SAMP)
- break;
- }
- }
- static void
- pcm_poll_out(void)
- {
- int16_t buf[PKT_SIZE_SAMP];
-
- if (!g_pcm[1].active)
- return;
-
- int lvl = mc97_flow_tx_level();
- bool active = mc97_flow_tx_active();
-
- if (!lvl & active)
- mc97_flow_tx_stop();
-
- while (1) {
- uint32_t csr = usb_ep_regs[1].out.bd[g_pcm[1].bdi].csr;
- uint32_t ptr = usb_ep_regs[1].out.bd[g_pcm[1].bdi].ptr;
-
- if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_RDY_DATA)
- break;
-
- if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_OK)
- {
- int n = ((csr & USB_BD_LEN_MSK) - 2) / 2;
-
- if ((lvl + n) > MC97_FIFO_SIZE)
- break;
- lvl += n;
-
- if (n) {
- usb_data_read(buf, ptr, PKT_SIZE_BYTE);
- mc97_flow_tx_push(buf, n);
- }
- }
-
- usb_ep_regs[1].out.bd[g_pcm[1].bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(PKT_SIZE_BYTE);
- g_pcm[1].bdi ^= 1;
- }
-
- if ((lvl > (MC97_FIFO_SIZE/2)) && !active)
- mc97_flow_tx_start();
- }
- static void
- pcm_poll(void)
- {
- pcm_poll_in();
- pcm_poll_out();
- pcm_poll_feedback_ep();
- }
- static bool
- pcm_usb_mute_set(struct usb_ctrl_req *req, uint8_t *data, int *len)
- {
- int idx = _idx_from_req(req);
- switch (idx) {
- case 0: mc97_set_rx_mute(data[0]); return true;
- case 1: mc97_set_tx_mute(data[0]); return true;
- }
- return false;
- }
- static bool
- pcm_usb_mute_get(struct usb_ctrl_req *req, uint8_t *data, int *len)
- {
- int idx = _idx_from_req(req);
- switch (idx) {
- case 0: data[0] = mc97_get_rx_mute(); return true;
- case 1: data[0] = mc97_get_tx_mute(); return true;
- }
- return false;
- }
- #define BOUND(x, a, b) (((x)<(a))?(a):(((x)>(b))?(b):(x)))
- static bool
- pcm_usb_volume_set(struct usb_ctrl_req *req, uint8_t *data, int *len)
- {
- int idx = _idx_from_req(req);
- int16_t vol = *((int16_t*)data);
- switch (idx) {
- case 0:
- vol = BOUND(vol, 0, 5760) >> 7;
- mc97_set_rx_gain(vol);
- return true;
- case 1:
- vol = BOUND(-vol, 0, 5760) >> 7;
- mc97_set_tx_attenuation(vol);
- return true;
- }
- return false;
- }
- static bool
- pcm_usb_volume_get(struct usb_ctrl_req *req, uint8_t *data, int *len)
- {
- int idx = _idx_from_req(req);
- switch (idx) {
- case 0: *((int16_t*)data) = (mc97_get_rx_gain() << 7); return true;
- case 1: *((int16_t*)data) = -(mc97_get_tx_attenuation() << 7); return true;
- }
- return false;
- }
- static bool
- pcm_usb_volume_min(struct usb_ctrl_req *req, uint8_t *data, int *len)
- {
- const int16_t max[] = { 0 , -5760 };
- int idx;
- if ((idx = _idx_from_req(req)) < 0)
- return false;
- *((int16_t*)data) = max[idx];
- return true;
- }
- static bool
- pcm_usb_volume_max(struct usb_ctrl_req *req, uint8_t *data, int *len)
- {
- const int16_t max[] = { 5760 , 0 };
- int idx;
- if ((idx = _idx_from_req(req)) < 0)
- return false;
- *((int16_t*)data) = max[idx];
- return true;
- }
- static bool
- pcm_usb_volume_res(struct usb_ctrl_req *req, uint8_t *data, int *len)
- {
- const int16_t res[] = { 384, 384 };
- int idx;
- if ((idx = _idx_from_req(req)) < 0)
- return false;
- *((int16_t*)data) = res[idx];
- return true;
- }
- typedef bool (*usb_audio_control_fn)(struct usb_ctrl_req *req, uint8_t *data, int *len);
- struct usb_audio_control_handler {
- int len;
- usb_audio_control_fn set_cur;
- usb_audio_control_fn get_cur;
- usb_audio_control_fn get_min;
- usb_audio_control_fn get_max;
- usb_audio_control_fn get_res;
- };
- struct usb_audio_req_handler {
- uint8_t rcpt;
- uint8_t idx;
- uint8_t entity_id;
- uint16_t val_match;
- uint16_t val_mask;
- const struct usb_audio_control_handler *h;
- };
- static const struct usb_audio_control_handler _uac_mute = {
- .len = 1,
- .set_cur = pcm_usb_mute_set,
- .get_cur = pcm_usb_mute_get,
- };
- static const struct usb_audio_control_handler _uac_volume = {
- .len = 2,
- .set_cur = pcm_usb_volume_set,
- .get_cur = pcm_usb_volume_get,
- .get_min = pcm_usb_volume_min,
- .get_max = pcm_usb_volume_max,
- .get_res = pcm_usb_volume_res,
- };
- static const struct usb_audio_req_handler _uac_handlers[] = {
- { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEAT_PCM_IN, (USB_AC_FU_CONTROL_MUTE << 8), 0xff00, &_uac_mute },
- { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEAT_PCM_IN, (USB_AC_FU_CONTROL_VOLUME << 8), 0xff00, &_uac_volume },
- { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEAT_PCM_OUT, (USB_AC_FU_CONTROL_MUTE << 8), 0xff00, &_uac_mute },
- { USB_REQ_RCPT_INTF, INTF_AUDIO_CONTROL, UNIT_FEAT_PCM_OUT, (USB_AC_FU_CONTROL_VOLUME << 8), 0xff00, &_uac_volume },
- { 0 }
- };
- static struct {
- struct usb_ctrl_req *req;
- usb_audio_control_fn fn;
- } g_cb_ctx;
- static bool
- audio_ctrl_req_cb(struct usb_xfer *xfer)
- {
- struct usb_ctrl_req *req = g_cb_ctx.req;
- usb_audio_control_fn fn = g_cb_ctx.fn;
- return fn(req, xfer->data, &xfer->len);
- }
- static enum usb_fnd_resp
- audio_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
- {
- const struct usb_audio_req_handler *rh;
-
- if (USB_REQ_TYPE(req) != USB_REQ_TYPE_CLASS)
- return USB_FND_CONTINUE;
-
-
- if ((req->bmRequestType ^ req->bRequest) & 0x80)
- return USB_FND_ERROR;
-
- for (rh=&_uac_handlers[0]; rh->rcpt; rh++)
- {
- usb_audio_control_fn fn = NULL;
-
- if (USB_REQ_RCPT(req) != rh->rcpt)
- continue;
- if ((req->wIndex & 0xff) != rh->idx)
- continue;
-
- if ((req->wIndex >> 8) != rh->entity_id)
- continue;
-
- if ((req->wValue & rh->val_mask) != rh->val_match)
- continue;
-
- if (!rh->h)
- return USB_FND_ERROR;
- if ((rh->h->len != -1) && (rh->h->len != req->wLength))
- return USB_FND_ERROR;
-
- switch (req->bRequest)
- {
- case USB_REQ_AC_SET_CUR:
- fn = rh->h->set_cur;
- break;
- case USB_REQ_AC_GET_CUR:
- fn = rh->h->get_cur;
- break;
- case USB_REQ_AC_GET_MIN:
- fn = rh->h->get_min;
- break;
- case USB_REQ_AC_GET_MAX:
- fn = rh->h->get_max;
- break;
- case USB_REQ_AC_GET_RES:
- fn = rh->h->get_res;
- break;
- default:
- fn = NULL;
- }
- if (!fn)
- return USB_FND_ERROR;
-
- if (USB_REQ_IS_READ(req)) {
-
- xfer->len = req->wLength;
- return fn(req, xfer->data, &xfer->len) ? USB_FND_SUCCESS : USB_FND_ERROR;
- } else {
-
- g_cb_ctx.req = req;
- g_cb_ctx.fn = fn;
- xfer->len = req->wLength;
- xfer->cb_done = audio_ctrl_req_cb;
- return USB_FND_SUCCESS;
- }
- }
- return USB_FND_ERROR;
- }
- static enum usb_fnd_resp
- audio_set_conf(const struct usb_conf_desc *conf)
- {
-
- pcm_usb_configure(conf);
- return USB_FND_SUCCESS;
- }
- static enum usb_fnd_resp
- audio_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
- {
-
- if (base->bInterfaceClass != USB_CLS_AUDIO)
- return USB_FND_CONTINUE;
-
- switch (base->bInterfaceSubClass)
- {
- case USB_AC_SCLS_AUDIOCONTROL:
- return USB_FND_SUCCESS;
- case USB_AC_SCLS_AUDIOSTREAMING:
- return pcm_usb_set_intf(base, sel) ? USB_FND_SUCCESS : USB_FND_ERROR;
- default:
- return USB_FND_ERROR;
- }
- }
- static enum usb_fnd_resp
- audio_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
- {
-
- if (base->bInterfaceClass != USB_CLS_AUDIO)
- return USB_FND_CONTINUE;
-
- switch (base->bInterfaceSubClass)
- {
- case USB_AC_SCLS_AUDIOCONTROL:
- *alt = 0;
- return USB_FND_SUCCESS;
- case USB_AC_SCLS_AUDIOSTREAMING:
- return pcm_usb_get_intf(base, alt) ? USB_FND_SUCCESS : USB_FND_ERROR;
- default:
- return USB_FND_ERROR;
- }
- }
- static struct usb_fn_drv _audio_drv = {
- .ctrl_req = audio_ctrl_req,
- .set_conf = audio_set_conf,
- .set_intf = audio_set_intf,
- .get_intf = audio_get_intf,
- };
- void
- audio_init(void)
- {
-
- pcm_init();
-
- usb_register_function_driver(&_audio_drv);
- }
- void
- audio_poll(void)
- {
- pcm_poll();
- }
|