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