Sylvain Munaut | f5d7bf2 | 2020-09-14 10:23:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * usb_e1.c |
| 3 | * |
| 4 | * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com> |
| 5 | * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | #include <stdbool.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include <no2usb/usb_hw.h> |
| 13 | #include <no2usb/usb_priv.h> |
| 14 | |
| 15 | #include "console.h" |
| 16 | #include "e1.h" |
| 17 | #include "misc.h" |
| 18 | |
| 19 | struct { |
| 20 | bool running; |
| 21 | int in_bdi[2]; |
| 22 | } g_usb_e1; |
| 23 | |
| 24 | |
| 25 | /* Hack */ |
| 26 | unsigned int e1_rx_need_data(int chan, unsigned int usb_addr, unsigned int max_len, unsigned int *pos); |
| 27 | unsigned int e1_rx_level(int chan); |
| 28 | uint8_t e1_get_pending_flags(int chan); |
| 29 | /* ---- */ |
| 30 | |
Sylvain Munaut | f5d7bf2 | 2020-09-14 10:23:50 +0200 | [diff] [blame] | 31 | |
| 32 | void |
| 33 | usb_e1_run(void) |
| 34 | { |
| 35 | int chan; |
| 36 | int bdi; |
| 37 | |
| 38 | if (!g_usb_e1.running) |
| 39 | return; |
| 40 | |
| 41 | /* EP[1-2] IN */ |
| 42 | for (chan=0; chan<2; chan++) |
| 43 | { |
| 44 | bdi = g_usb_e1.in_bdi[chan]; |
| 45 | |
| 46 | while ((usb_ep_regs[1+chan].in.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA) |
| 47 | { |
| 48 | uint32_t ptr = usb_ep_regs[1+chan].in.bd[bdi].ptr; |
| 49 | uint32_t hdr; |
| 50 | unsigned int pos; |
| 51 | |
| 52 | /* Error check */ |
| 53 | if ((usb_ep_regs[1+chan].in.bd[bdi].csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR) |
| 54 | printf("Err EP%d IN\n", 1+chan); |
| 55 | |
| 56 | /* Get some data from E1 */ |
| 57 | int n = e1_rx_level(chan); |
| 58 | |
| 59 | // if (n > 64) |
| 60 | // n = 12; |
| 61 | // else if (n > 32) |
| 62 | // n = 10; |
| 63 | // else if (n > 8) |
| 64 | // n = 8; |
| 65 | if (n > 12) |
| 66 | n = 12; |
| 67 | else if (!n) |
| 68 | break; |
| 69 | |
| 70 | n = e1_rx_need_data(chan, (ptr >> 2) + 1, n, &pos); |
| 71 | |
| 72 | /* Write header */ |
| 73 | /* [31:12] (reserved) */ |
| 74 | /* [11:10] CRC results (first new multiframe present in packet) */ |
| 75 | /* [ 9: 8] CRC results (second new multiframe present in packet) */ |
| 76 | /* [ 7: 5] Multiframe sequence number (first frame of packet) */ |
| 77 | /* [ 4: 0] Position in multi-frame (first frame of packet) */ |
| 78 | hdr = (pos & 0xff) | (e1_get_pending_flags(chan) << 24); |
| 79 | usb_data_write(ptr, &hdr, 4); |
| 80 | usb_ep_regs[1+chan].in.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN((n * 32) + 4); |
| 81 | |
| 82 | /* Next BDI */ |
| 83 | bdi ^= 1; |
| 84 | g_usb_e1.in_bdi[chan] = bdi; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static const struct usb_intf_desc * |
| 90 | _find_intf(const struct usb_conf_desc *conf, uint8_t idx) |
| 91 | { |
| 92 | const struct usb_intf_desc *intf = NULL; |
| 93 | const void *sod, *eod; |
| 94 | |
| 95 | if (!conf) |
| 96 | return NULL; |
| 97 | |
| 98 | sod = conf; |
| 99 | eod = sod + conf->wTotalLength; |
| 100 | |
| 101 | while (1) { |
| 102 | sod = usb_desc_find(sod, eod, USB_DT_INTF); |
| 103 | if (!sod) |
| 104 | break; |
| 105 | |
| 106 | intf = (void*)sod; |
| 107 | if (intf->bInterfaceNumber == idx) |
| 108 | return intf; |
| 109 | |
| 110 | sod = usb_desc_next(sod); |
| 111 | } |
| 112 | |
| 113 | return NULL; |
| 114 | } |
| 115 | enum usb_fnd_resp |
| 116 | _e1_set_conf(const struct usb_conf_desc *conf) |
| 117 | { |
| 118 | const struct usb_intf_desc *intf; |
| 119 | |
| 120 | printf("e1 set_conf %08x\n", conf); |
| 121 | if (!conf) |
| 122 | return USB_FND_SUCCESS; |
| 123 | |
| 124 | intf = _find_intf(conf, 0); |
| 125 | if (!intf) |
| 126 | return USB_FND_ERROR; |
| 127 | |
| 128 | printf("e1 set_conf %08x\n", intf); |
| 129 | |
| 130 | usb_ep_boot(intf, 0x81, true); |
| 131 | usb_ep_boot(intf, 0x82, true); |
| 132 | |
| 133 | return USB_FND_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | enum usb_fnd_resp |
| 137 | _e1_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel) |
| 138 | { |
| 139 | if (base->bInterfaceNumber != 0) |
| 140 | return USB_FND_CONTINUE; |
| 141 | |
| 142 | if (sel->bAlternateSetting == 0) |
| 143 | { |
| 144 | /* Already stopped ? */ |
| 145 | if (!g_usb_e1.running) |
| 146 | return USB_FND_SUCCESS; |
| 147 | |
| 148 | /* Update state */ |
| 149 | g_usb_e1.running = false; |
| 150 | |
| 151 | /* Stop E1 */ |
| 152 | e1_stop(); |
| 153 | |
| 154 | /* Disable end-points */ |
| 155 | usb_ep_regs[1].in.status = 0; |
| 156 | usb_ep_regs[2].in.status = 0; |
| 157 | } |
| 158 | else if (sel->bAlternateSetting == 1) |
| 159 | { |
| 160 | /* Already running ? */ |
| 161 | if (g_usb_e1.running) |
| 162 | return USB_FND_SUCCESS; |
| 163 | |
| 164 | /* Update state */ |
| 165 | g_usb_e1.running = true; |
| 166 | |
| 167 | /* Reset buffer pointers */ |
| 168 | g_usb_e1.in_bdi[0] = 0; |
| 169 | g_usb_e1.in_bdi[1] = 0; |
| 170 | |
| 171 | /* Configure EP1 IN / EP2 IN */ |
| 172 | usb_ep_regs[1].in.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */ |
| 173 | usb_ep_regs[2].in.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */ |
| 174 | |
| 175 | /* EP1 IN: Prepare two buffers */ |
| 176 | usb_ep_regs[1].in.bd[0].ptr = 256 + 0 * 388; |
| 177 | usb_ep_regs[1].in.bd[0].csr = 0; |
| 178 | |
| 179 | usb_ep_regs[1].in.bd[1].ptr = 256 + 1 * 388; |
| 180 | usb_ep_regs[1].in.bd[1].csr = 0; |
| 181 | |
| 182 | /* EP2 IN: Prepare two buffers */ |
| 183 | usb_ep_regs[2].in.bd[0].ptr = 256 + 2 * 388; |
| 184 | usb_ep_regs[2].in.bd[0].csr = 0; |
| 185 | |
| 186 | usb_ep_regs[2].in.bd[1].ptr = 256 + 3 * 388; |
| 187 | usb_ep_regs[2].in.bd[1].csr = 0; |
| 188 | |
| 189 | /* Start E1 */ |
| 190 | e1_start(); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | /* Unknown */ |
| 195 | return USB_FND_ERROR; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | return USB_FND_SUCCESS; |
| 200 | } |
| 201 | |
| 202 | enum usb_fnd_resp |
| 203 | _e1_get_intf(const struct usb_intf_desc *base, uint8_t *alt) |
| 204 | { |
| 205 | if (base->bInterfaceNumber != 0) |
| 206 | return USB_FND_CONTINUE; |
| 207 | |
| 208 | *alt = g_usb_e1.running ? 1 : 0; |
| 209 | |
| 210 | return USB_FND_SUCCESS; |
| 211 | } |
| 212 | |
| 213 | static struct usb_fn_drv _e1_drv = { |
| 214 | .set_conf = _e1_set_conf, |
| 215 | .set_intf = _e1_set_intf, |
| 216 | .get_intf = _e1_get_intf, |
| 217 | }; |
| 218 | |
| 219 | void |
| 220 | usb_e1_init(void) |
| 221 | { |
| 222 | /* Clear state */ |
| 223 | memset(&g_usb_e1, 0x00, sizeof(g_usb_e1)); |
| 224 | |
| 225 | /* Install driver */ |
| 226 | usb_register_function_driver(&_e1_drv); |
| 227 | } |