Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +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 "misc.h" |
Harald Welte | 2df1f80 | 2020-12-14 17:46:53 +0100 | [diff] [blame] | 17 | #include "e1.h" |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 18 | |
| 19 | struct { |
Harald Welte | 30fc560 | 2020-12-14 15:56:28 +0100 | [diff] [blame] | 20 | bool running; /* are we running (transceiving USB data)? */ |
| 21 | int out_bdi; /* buffer descriptor index for OUT EP */ |
| 22 | int in_bdi; /* buffer descriptor index for IN EP */ |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 23 | } g_usb_e1; |
| 24 | |
| 25 | |
| 26 | /* Hack */ |
Harald Welte | daff4f6 | 2020-12-14 17:39:23 +0100 | [diff] [blame] | 27 | unsigned int e1_rx_need_data(unsigned int usb_addr, unsigned int max_len, unsigned int *pos); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 28 | unsigned int e1_tx_feed_data(unsigned int usb_addr, unsigned int len); |
| 29 | unsigned int e1_tx_level(void); |
| 30 | unsigned int e1_rx_level(void); |
| 31 | /* ---- */ |
| 32 | |
| 33 | bool |
| 34 | usb_ep_boot(const struct usb_intf_desc *intf, uint8_t ep_addr, bool dual_bd); |
| 35 | |
| 36 | static void |
| 37 | _usb_fill_feedback_ep(void) |
| 38 | { |
| 39 | static uint16_t ticks_prev = 0; |
| 40 | uint16_t ticks; |
| 41 | uint32_t val = 8192; |
| 42 | unsigned int level; |
| 43 | |
Harald Welte | 2b7dadf | 2020-12-14 15:56:15 +0100 | [diff] [blame] | 44 | /* Compute real E1 tick count (with safety against bad values) */ |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 45 | ticks = e1_tick_read(); |
| 46 | val = (ticks - ticks_prev) & 0xffff; |
| 47 | ticks_prev = ticks; |
| 48 | if ((val < 7168) | (val > 9216)) |
| 49 | val = 8192; |
| 50 | |
| 51 | /* Bias depending on TX fifo level */ |
| 52 | level = e1_tx_level(); |
| 53 | if (level < (3 * 16)) |
| 54 | val += 256; |
| 55 | else if (level > (8 * 16)) |
| 56 | val -= 256; |
| 57 | |
| 58 | /* Prepare buffer */ |
| 59 | usb_data_write(64, &val, 4); |
| 60 | usb_ep_regs[1].in.bd[0].ptr = 64; |
| 61 | usb_ep_regs[1].in.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(3); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | void |
| 66 | usb_e1_run(void) |
| 67 | { |
| 68 | int bdi; |
| 69 | |
| 70 | if (!g_usb_e1.running) |
| 71 | return; |
| 72 | |
| 73 | /* EP2 IN */ |
| 74 | bdi = g_usb_e1.in_bdi; |
| 75 | |
| 76 | while ((usb_ep_regs[2].in.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA) |
| 77 | { |
| 78 | uint32_t ptr = usb_ep_regs[2].in.bd[bdi].ptr; |
| 79 | uint32_t hdr; |
Harald Welte | 2df1f80 | 2020-12-14 17:46:53 +0100 | [diff] [blame] | 80 | unsigned int pos; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 81 | |
| 82 | /* Error check */ |
| 83 | if ((usb_ep_regs[2].in.bd[bdi].csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR) |
| 84 | puts("Err EP2 IN\n"); |
| 85 | |
| 86 | /* Get some data from E1 */ |
| 87 | int n = e1_rx_level(); |
| 88 | |
| 89 | if (n > 64) |
| 90 | n = 12; |
| 91 | else if (n > 32) |
| 92 | n = 10; |
| 93 | else if (n > 8) |
| 94 | n = 8; |
| 95 | else if (!n) |
| 96 | break; |
| 97 | |
Harald Welte | 2df1f80 | 2020-12-14 17:46:53 +0100 | [diff] [blame] | 98 | n = e1_rx_need_data((ptr >> 2) + 1, n, &pos); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 99 | |
Harald Welte | 2df1f80 | 2020-12-14 17:46:53 +0100 | [diff] [blame] | 100 | /* Write header: currently version and pos (mfr/fr number) */ |
| 101 | hdr = (0 << 28) | (pos & 0xff); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 102 | usb_data_write(ptr, &hdr, 4); |
| 103 | |
| 104 | /* Resubmit */ |
| 105 | usb_ep_regs[2].in.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN((n * 32) + 4); |
| 106 | |
| 107 | /* Next BDI */ |
| 108 | bdi ^= 1; |
| 109 | g_usb_e1.in_bdi = bdi; |
| 110 | } |
| 111 | |
| 112 | /* EP1 OUT */ |
| 113 | bdi = g_usb_e1.out_bdi; |
| 114 | |
| 115 | while ((usb_ep_regs[1].out.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA) |
| 116 | { |
| 117 | uint32_t ptr = usb_ep_regs[1].out.bd[bdi].ptr; |
| 118 | uint32_t csr = usb_ep_regs[1].out.bd[bdi].csr; |
| 119 | uint32_t hdr; |
| 120 | |
| 121 | /* Error check */ |
| 122 | if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR) { |
| 123 | puts("Err EP1 OUT\n"); |
| 124 | goto refill; |
| 125 | } |
| 126 | |
| 127 | /* Grab header */ |
| 128 | usb_data_read(&hdr, ptr, 4); |
| 129 | |
| 130 | /* Empty data into the FIFO */ |
| 131 | int n = ((csr & USB_BD_LEN_MSK) - 4) / 32; |
| 132 | n = e1_tx_feed_data((ptr >> 2) + 1, n); |
| 133 | |
| 134 | refill: |
| 135 | /* Refill it */ |
| 136 | usb_ep_regs[1].out.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388); |
| 137 | |
| 138 | /* Next BDI */ |
| 139 | bdi ^= 1; |
| 140 | g_usb_e1.out_bdi = bdi; |
| 141 | |
| 142 | static int x = 0; |
| 143 | if ((x++ & 0xff) == 0xff) |
| 144 | puts("."); |
| 145 | } |
| 146 | |
| 147 | /* EP1 IN */ |
| 148 | if ((usb_ep_regs[1].in.bd[0].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA) |
| 149 | { |
| 150 | _usb_fill_feedback_ep(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static const struct usb_intf_desc * |
| 155 | _find_intf(const struct usb_conf_desc *conf, uint8_t idx) |
| 156 | { |
| 157 | const struct usb_intf_desc *intf = NULL; |
| 158 | const void *sod, *eod; |
| 159 | |
| 160 | if (!conf) |
| 161 | return NULL; |
| 162 | |
| 163 | sod = conf; |
| 164 | eod = sod + conf->wTotalLength; |
| 165 | |
| 166 | while (1) { |
| 167 | sod = usb_desc_find(sod, eod, USB_DT_INTF); |
| 168 | if (!sod) |
| 169 | break; |
| 170 | |
| 171 | intf = (void*)sod; |
| 172 | if (intf->bInterfaceNumber == idx) |
| 173 | return intf; |
| 174 | |
| 175 | sod = usb_desc_next(sod); |
| 176 | } |
| 177 | |
| 178 | return NULL; |
| 179 | } |
Harald Welte | c827185 | 2020-12-14 13:48:09 +0100 | [diff] [blame] | 180 | |
| 181 | static enum usb_fnd_resp |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 182 | _e1_set_conf(const struct usb_conf_desc *conf) |
| 183 | { |
| 184 | const struct usb_intf_desc *intf; |
| 185 | |
| 186 | printf("e1 set_conf %08x\n", conf); |
| 187 | if (!conf) |
| 188 | return USB_FND_SUCCESS; |
| 189 | |
| 190 | intf = _find_intf(conf, 0); |
| 191 | if (!intf) |
| 192 | return USB_FND_ERROR; |
| 193 | |
| 194 | printf("e1 set_conf %08x\n", intf); |
| 195 | |
| 196 | usb_ep_boot(intf, 0x01, true); |
| 197 | usb_ep_boot(intf, 0x81, true); |
| 198 | usb_ep_boot(intf, 0x82, true); |
| 199 | |
| 200 | return USB_FND_SUCCESS; |
| 201 | } |
| 202 | |
Harald Welte | c827185 | 2020-12-14 13:48:09 +0100 | [diff] [blame] | 203 | static enum usb_fnd_resp |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 204 | _e1_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel) |
| 205 | { |
| 206 | if (base->bInterfaceNumber != 0) |
| 207 | return USB_FND_CONTINUE; |
| 208 | |
| 209 | if (sel->bAlternateSetting != 1) |
| 210 | return USB_FND_SUCCESS; |
| 211 | |
| 212 | /* Hack to avoid re-setting while running ... avoid BD desync */ |
| 213 | if (g_usb_e1.running) |
| 214 | return USB_FND_SUCCESS; |
| 215 | |
| 216 | g_usb_e1.running = true; |
| 217 | |
| 218 | /* Configure EP1 OUT / EP2 IN */ |
| 219 | usb_ep_regs[1].out.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */ |
| 220 | usb_ep_regs[2].in.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */ |
| 221 | |
| 222 | /* Configure EP1 IN (feedback) */ |
| 223 | usb_ep_regs[1].in.status = USB_EP_TYPE_ISOC; /* Type=Isochronous, single buffered */ |
| 224 | |
| 225 | /* EP2 IN: Prepare two buffers */ |
| 226 | usb_ep_regs[2].in.bd[0].ptr = 1024; |
| 227 | usb_ep_regs[2].in.bd[0].csr = 0; |
| 228 | |
| 229 | usb_ep_regs[2].in.bd[1].ptr = 1536; |
| 230 | usb_ep_regs[2].in.bd[1].csr = 0; |
| 231 | |
| 232 | /* EP1 OUT: Queue two buffers */ |
| 233 | usb_ep_regs[1].out.bd[0].ptr = 1024; |
| 234 | usb_ep_regs[1].out.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388); |
| 235 | |
| 236 | usb_ep_regs[1].out.bd[1].ptr = 1536; |
| 237 | usb_ep_regs[1].out.bd[1].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(388); |
| 238 | |
| 239 | /* EP1 IN: Queue buffer */ |
| 240 | _usb_fill_feedback_ep(); |
| 241 | |
| 242 | return USB_FND_SUCCESS; |
| 243 | } |
| 244 | |
Harald Welte | c827185 | 2020-12-14 13:48:09 +0100 | [diff] [blame] | 245 | static enum usb_fnd_resp |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 246 | _e1_get_intf(const struct usb_intf_desc *base, uint8_t *alt) |
| 247 | { |
| 248 | if (base->bInterfaceNumber != 0) |
| 249 | return USB_FND_CONTINUE; |
| 250 | |
| 251 | *alt = g_usb_e1.running ? 1 : 0; |
| 252 | |
| 253 | return USB_FND_SUCCESS; |
| 254 | } |
| 255 | |
| 256 | static struct usb_fn_drv _e1_drv = { |
| 257 | .set_conf = _e1_set_conf, |
| 258 | .set_intf = _e1_set_intf, |
| 259 | .get_intf = _e1_get_intf, |
| 260 | }; |
| 261 | |
| 262 | void |
| 263 | usb_e1_init(void) |
| 264 | { |
| 265 | memset(&g_usb_e1, 0x00, sizeof(g_usb_e1)); |
| 266 | usb_register_function_driver(&_e1_drv); |
| 267 | } |