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" |
Harald Welte | 2df1f80 | 2020-12-14 17:46:53 +0100 | [diff] [blame] | 16 | #include "e1.h" |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 17 | #include "misc.h" |
| 18 | #include "usb_desc_ids.h" |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 19 | #include "utils.h" |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 20 | |
| 21 | #include "ice1usb_proto.h" |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 22 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 23 | struct usb_e1_state { |
Harald Welte | 30fc560 | 2020-12-14 15:56:28 +0100 | [diff] [blame] | 24 | bool running; /* are we running (transceiving USB data)? */ |
| 25 | int out_bdi; /* buffer descriptor index for OUT EP */ |
| 26 | int in_bdi; /* buffer descriptor index for IN EP */ |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 27 | struct ice1usb_tx_config tx_cfg; |
| 28 | struct ice1usb_rx_config rx_cfg; |
Harald Welte | 805f2cf | 2020-12-14 17:31:03 +0100 | [diff] [blame] | 29 | struct e1_error_count last_err; |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static struct usb_e1_state g_usb_e1[2]; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 33 | |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 34 | /* default configuration at power-up */ |
| 35 | static const struct ice1usb_tx_config tx_cfg_default = { |
| 36 | .mode = ICE1USB_TX_MODE_TS0_CRC4_E, |
| 37 | .timing = ICE1USB_TX_TIME_SRC_LOCAL, |
| 38 | .ext_loopback = ICE1USB_TX_EXT_LOOPBACK_OFF, |
| 39 | .alarm = 0, |
| 40 | }; |
| 41 | |
| 42 | static const struct ice1usb_rx_config rx_cfg_default = { |
| 43 | .mode = ICE1USB_RX_MODE_MULTIFRAME, |
| 44 | }; |
| 45 | |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 46 | |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 47 | static volatile struct usb_ep * |
| 48 | _get_ep_regs(uint8_t ep) |
| 49 | { |
| 50 | return (ep & 0x80) ? &usb_ep_regs[ep & 0x1f].in : &usb_ep_regs[ep & 0x1f].out; |
| 51 | } |
| 52 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 53 | static struct usb_e1_state * |
| 54 | _get_state(int port) |
| 55 | { |
| 56 | if ((port < 0) || (port > 1)) |
| 57 | panic("_get_state invalid port %d", port); |
| 58 | return &g_usb_e1[port]; |
| 59 | } |
| 60 | |
| 61 | static int |
| 62 | _ifnum2port(uint8_t bInterfaceNumber) |
| 63 | { |
| 64 | switch (bInterfaceNumber) { |
| 65 | case USB_INTF_E1(0): return 0; |
| 66 | case USB_INTF_E1(1): return 1; |
| 67 | default: |
| 68 | /* Don't panic since this will be handled as USB STALL */ |
| 69 | return -1; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 74 | static void |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 75 | _usb_fill_feedback_ep(int port) |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 76 | { |
Sylvain Munaut | 2de84f8 | 2022-01-06 08:12:04 +0100 | [diff] [blame] | 77 | volatile struct usb_ep *ep_regs = _get_ep_regs(USB_EP_E1_FB(port)); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 78 | |
Sylvain Munaut | 2de84f8 | 2022-01-06 08:12:04 +0100 | [diff] [blame] | 79 | /* Always ensure we're ready to send */ |
| 80 | if ((ep_regs->bd[0].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA) |
| 81 | { |
| 82 | uint32_t val = 8192; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 83 | |
Sylvain Munaut | 2de84f8 | 2022-01-06 08:12:04 +0100 | [diff] [blame] | 84 | /* Add instant bias depending on TX fifo level */ |
| 85 | unsigned int level = e1_tx_level(port); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 86 | |
Sylvain Munaut | 2de84f8 | 2022-01-06 08:12:04 +0100 | [diff] [blame] | 87 | if (level < (4 * 16)) |
| 88 | val += 256; |
| 89 | else if (level > (6 * 16)) |
| 90 | val -= 256; |
| 91 | |
| 92 | /* Fill buffer and submit it */ |
| 93 | usb_data_write(ep_regs->bd[0].ptr, &val, 4); |
| 94 | ep_regs->bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(3); |
| 95 | } |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Harald Welte | 1b89f3b | 2022-01-13 10:03:42 +0100 | [diff] [blame] | 98 | static void |
| 99 | _fill_irq_err(struct ice1usb_irq_err *out, const struct e1_error_count *cur_err) |
| 100 | { |
| 101 | out->crc = cur_err->crc; |
| 102 | out->align = cur_err->align; |
| 103 | out->ovfl = cur_err->ovfl; |
| 104 | out->unfl = cur_err->unfl; |
| 105 | out->flags = cur_err->flags; |
| 106 | } |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 107 | |
Sylvain Munaut | c9c22a6 | 2022-01-12 11:47:08 +0100 | [diff] [blame] | 108 | static void |
| 109 | _usb_e1_run(int port) |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 110 | { |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 111 | struct usb_e1_state *usb_e1 = _get_state(port); |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 112 | volatile struct usb_ep *ep_regs; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 113 | int bdi; |
| 114 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 115 | if (!usb_e1->running) |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 116 | return; |
| 117 | |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 118 | /* Interrupt endpoint */ |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 119 | ep_regs = _get_ep_regs(USB_EP_E1_INT(port)); |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 120 | |
| 121 | if ((ep_regs->bd[0].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA) { |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 122 | const struct e1_error_count *cur_err = e1_get_error_count(port); |
| 123 | if (memcmp(cur_err, &usb_e1->last_err, sizeof(*cur_err))) { |
Harald Welte | 1b89f3b | 2022-01-13 10:03:42 +0100 | [diff] [blame] | 124 | struct ice1usb_irq errmsg; |
| 125 | errmsg.type = ICE1USB_IRQ_T_ERRCNT; |
| 126 | _fill_irq_err(&errmsg.u.errors, cur_err); |
Harald Welte | 805f2cf | 2020-12-14 17:31:03 +0100 | [diff] [blame] | 127 | printf("E"); |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 128 | usb_data_write(ep_regs->bd[0].ptr, &errmsg, sizeof(errmsg)); |
| 129 | ep_regs->bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(sizeof(errmsg)); |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 130 | usb_e1->last_err = *cur_err; |
Harald Welte | 805f2cf | 2020-12-14 17:31:03 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 134 | /* Data IN endpoint */ |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 135 | ep_regs = _get_ep_regs(USB_EP_E1_IN(port)); |
| 136 | bdi = usb_e1->in_bdi; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 137 | |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 138 | while ((ep_regs->bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA) |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 139 | { |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 140 | uint32_t ptr = ep_regs->bd[bdi].ptr; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 141 | uint32_t hdr; |
Harald Welte | 2df1f80 | 2020-12-14 17:46:53 +0100 | [diff] [blame] | 142 | unsigned int pos; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 143 | |
| 144 | /* Error check */ |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 145 | if ((ep_regs->bd[bdi].csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR) |
| 146 | puts("Err EP IN\n"); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 147 | |
| 148 | /* Get some data from E1 */ |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 149 | int n = e1_rx_level(port); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 150 | |
Sylvain Munaut | 1d98709 | 2022-01-05 21:04:09 +0100 | [diff] [blame] | 151 | if (n > 32) |
| 152 | n = 9; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 153 | else if (n > 8) |
| 154 | n = 8; |
| 155 | else if (!n) |
| 156 | break; |
| 157 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 158 | n = e1_rx_need_data(port, (ptr >> 2) + 1, n, &pos); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 159 | |
Harald Welte | 2df1f80 | 2020-12-14 17:46:53 +0100 | [diff] [blame] | 160 | /* Write header: currently version and pos (mfr/fr number) */ |
| 161 | hdr = (0 << 28) | (pos & 0xff); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 162 | usb_data_write(ptr, &hdr, 4); |
| 163 | |
| 164 | /* Resubmit */ |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 165 | ep_regs->bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN((n * 32) + 4); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 166 | |
| 167 | /* Next BDI */ |
| 168 | bdi ^= 1; |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 169 | usb_e1->in_bdi = bdi; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 172 | /* Data OUT endpoint */ |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 173 | ep_regs = _get_ep_regs(USB_EP_E1_OUT(port)); |
| 174 | bdi = usb_e1->out_bdi; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 175 | |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 176 | while ((ep_regs->bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA) |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 177 | { |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 178 | uint32_t ptr = ep_regs->bd[bdi].ptr; |
| 179 | uint32_t csr = ep_regs->bd[bdi].csr; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 180 | uint32_t hdr; |
| 181 | |
| 182 | /* Error check */ |
| 183 | if ((csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR) { |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 184 | puts("Err EP OUT\n"); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 185 | goto refill; |
| 186 | } |
| 187 | |
| 188 | /* Grab header */ |
| 189 | usb_data_read(&hdr, ptr, 4); |
| 190 | |
| 191 | /* Empty data into the FIFO */ |
Sylvain Munaut | d108dfb | 2022-01-03 21:11:05 +0100 | [diff] [blame] | 192 | int n = ((int)(csr & USB_BD_LEN_MSK) - 6) / 32; |
| 193 | if (n > 0) |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 194 | e1_tx_feed_data(port, (ptr >> 2) + 1, n); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 195 | |
| 196 | refill: |
| 197 | /* Refill it */ |
Sylvain Munaut | 1d98709 | 2022-01-05 21:04:09 +0100 | [diff] [blame] | 198 | ep_regs->bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(292); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 199 | |
| 200 | /* Next BDI */ |
| 201 | bdi ^= 1; |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 202 | usb_e1->out_bdi = bdi; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 203 | |
| 204 | static int x = 0; |
| 205 | if ((x++ & 0xff) == 0xff) |
| 206 | puts("."); |
| 207 | } |
| 208 | |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 209 | /* Feedback endpoint */ |
Sylvain Munaut | 2de84f8 | 2022-01-06 08:12:04 +0100 | [diff] [blame] | 210 | _usb_fill_feedback_ep(port); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Sylvain Munaut | c9c22a6 | 2022-01-12 11:47:08 +0100 | [diff] [blame] | 213 | void |
| 214 | usb_e1_poll(void) |
| 215 | { |
| 216 | for (int i=0; i<2; i++) { |
| 217 | e1_poll(i); |
| 218 | _usb_e1_run(i); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
Harald Welte | c827185 | 2020-12-14 13:48:09 +0100 | [diff] [blame] | 223 | static enum usb_fnd_resp |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 224 | _e1_set_conf(const struct usb_conf_desc *conf) |
| 225 | { |
| 226 | const struct usb_intf_desc *intf; |
| 227 | |
| 228 | printf("e1 set_conf %08x\n", conf); |
| 229 | if (!conf) |
| 230 | return USB_FND_SUCCESS; |
| 231 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 232 | for (int port=0; port<2; port++) |
| 233 | { |
| 234 | intf = usb_desc_find_intf(conf, USB_INTF_E1(port), 0, NULL); |
| 235 | if (!intf) |
| 236 | return USB_FND_ERROR; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 237 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 238 | printf("e1 set_conf[%d] %08x\n", port, intf); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 239 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 240 | usb_ep_boot(intf, USB_EP_E1_IN(port), true); |
| 241 | usb_ep_boot(intf, USB_EP_E1_OUT(port), true); |
| 242 | usb_ep_boot(intf, USB_EP_E1_FB(port), false); |
| 243 | usb_ep_boot(intf, USB_EP_E1_INT(port), false); |
| 244 | } |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 245 | |
| 246 | return USB_FND_SUCCESS; |
| 247 | } |
| 248 | |
Sylvain Munaut | ace0932 | 2022-01-10 12:41:49 +0100 | [diff] [blame] | 249 | static uint32_t |
| 250 | _tx_config_reg(const struct ice1usb_tx_config *cfg) |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 251 | { |
Sylvain Munaut | ace0932 | 2022-01-10 12:41:49 +0100 | [diff] [blame] | 252 | return ((cfg->mode & 3) << 1) | |
Sylvain Munaut | 3da5151 | 2022-01-03 22:12:59 +0100 | [diff] [blame] | 253 | ((cfg->timing & 1) << 3) | |
| 254 | ((cfg->alarm & 1) << 4) | |
Sylvain Munaut | ace0932 | 2022-01-10 12:41:49 +0100 | [diff] [blame] | 255 | ((cfg->ext_loopback & 3) << 5); |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Sylvain Munaut | ace0932 | 2022-01-10 12:41:49 +0100 | [diff] [blame] | 258 | static uint32_t |
| 259 | _rx_config_reg(const struct ice1usb_rx_config *cfg) |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 260 | { |
Sylvain Munaut | ace0932 | 2022-01-10 12:41:49 +0100 | [diff] [blame] | 261 | return (cfg->mode << 1); |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Harald Welte | c827185 | 2020-12-14 13:48:09 +0100 | [diff] [blame] | 264 | static enum usb_fnd_resp |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 265 | _e1_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel) |
| 266 | { |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 267 | volatile struct usb_ep *ep_regs; |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 268 | struct usb_e1_state *usb_e1; |
| 269 | int port; |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 270 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 271 | /* Is it for E1 interface ? */ |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 272 | if ((base->bInterfaceClass != 0xff) || (base->bInterfaceSubClass != 0xe1)) |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 273 | return USB_FND_CONTINUE; |
| 274 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 275 | /* Get matching port (if any) */ |
| 276 | port = _ifnum2port(base->bInterfaceNumber); |
| 277 | if (port < 0) |
| 278 | return USB_FND_ERROR; |
| 279 | |
| 280 | usb_e1 = _get_state(port); |
| 281 | |
| 282 | /* Valid setting ? */ |
Sylvain Munaut | 9674436 | 2022-01-03 17:03:26 +0100 | [diff] [blame] | 283 | if (sel->bAlternateSetting > 1) |
| 284 | return USB_FND_ERROR; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 285 | |
Sylvain Munaut | 9674436 | 2022-01-03 17:03:26 +0100 | [diff] [blame] | 286 | /* Don't do anything if no change */ |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 287 | if (usb_e1->running == (sel->bAlternateSetting != 0)) |
Sylvain Munaut | 9674436 | 2022-01-03 17:03:26 +0100 | [diff] [blame] | 288 | return USB_FND_SUCCESS; |
| 289 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 290 | usb_e1->running = (sel->bAlternateSetting != 0); |
Sylvain Munaut | 9674436 | 2022-01-03 17:03:26 +0100 | [diff] [blame] | 291 | |
| 292 | /* Reconfigure the endpoints */ |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 293 | usb_ep_reconf(sel, USB_EP_E1_IN(port)); |
| 294 | usb_ep_reconf(sel, USB_EP_E1_OUT(port)); |
| 295 | usb_ep_reconf(sel, USB_EP_E1_FB(port)); |
| 296 | usb_ep_reconf(sel, USB_EP_E1_INT(port)); |
Sylvain Munaut | 9674436 | 2022-01-03 17:03:26 +0100 | [diff] [blame] | 297 | |
| 298 | /* Update E1 and USB state */ |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 299 | switch (usb_e1->running) { |
Sylvain Munaut | 9674436 | 2022-01-03 17:03:26 +0100 | [diff] [blame] | 300 | case false: |
| 301 | /* Disable E1 rx/tx */ |
Sylvain Munaut | 0cc1613 | 2022-01-10 13:26:15 +0100 | [diff] [blame] | 302 | e1_stop(port); |
Harald Welte | e6b0fe8 | 2020-12-17 22:22:05 +0100 | [diff] [blame] | 303 | break; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 304 | |
Sylvain Munaut | 9674436 | 2022-01-03 17:03:26 +0100 | [diff] [blame] | 305 | case true: |
| 306 | /* Reset and Re-Enable E1 */ |
Sylvain Munaut | 0cc1613 | 2022-01-10 13:26:15 +0100 | [diff] [blame] | 307 | e1_start(port); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 308 | |
Sylvain Munaut | 9674436 | 2022-01-03 17:03:26 +0100 | [diff] [blame] | 309 | /* Reset BDI */ |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 310 | usb_e1->in_bdi = 0; |
| 311 | usb_e1->out_bdi = 0; |
Harald Welte | e6b0fe8 | 2020-12-17 22:22:05 +0100 | [diff] [blame] | 312 | |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 313 | /* EP OUT: Queue two buffers */ |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 314 | ep_regs = _get_ep_regs(USB_EP_E1_OUT(port)); |
Sylvain Munaut | 1d98709 | 2022-01-05 21:04:09 +0100 | [diff] [blame] | 315 | ep_regs->bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(292); |
| 316 | ep_regs->bd[1].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(292); |
Harald Welte | e6b0fe8 | 2020-12-17 22:22:05 +0100 | [diff] [blame] | 317 | |
Harald Welte | e6b0fe8 | 2020-12-17 22:22:05 +0100 | [diff] [blame] | 318 | break; |
Harald Welte | e6b0fe8 | 2020-12-17 22:22:05 +0100 | [diff] [blame] | 319 | } |
Harald Welte | 805f2cf | 2020-12-14 17:31:03 +0100 | [diff] [blame] | 320 | |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 321 | return USB_FND_SUCCESS; |
| 322 | } |
| 323 | |
Harald Welte | c827185 | 2020-12-14 13:48:09 +0100 | [diff] [blame] | 324 | static enum usb_fnd_resp |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 325 | _e1_get_intf(const struct usb_intf_desc *base, uint8_t *alt) |
| 326 | { |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 327 | struct usb_e1_state *usb_e1; |
| 328 | int port; |
| 329 | |
| 330 | /* Is it for E1 interface ? */ |
Sylvain Munaut | ff44f14 | 2022-01-03 20:34:23 +0100 | [diff] [blame] | 331 | if ((base->bInterfaceClass != 0xff) || (base->bInterfaceSubClass != 0xe1)) |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 332 | return USB_FND_CONTINUE; |
| 333 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 334 | /* Get matching port (if any) */ |
| 335 | port = _ifnum2port(base->bInterfaceNumber); |
| 336 | if (port < 0) |
| 337 | return USB_FND_ERROR; |
| 338 | |
| 339 | usb_e1 = _get_state(port); |
| 340 | |
| 341 | /* Return current alt-setting */ |
| 342 | *alt = usb_e1->running ? 1 : 0; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 343 | |
| 344 | return USB_FND_SUCCESS; |
| 345 | } |
| 346 | |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 347 | static bool |
| 348 | _set_tx_mode_done(struct usb_xfer *xfer) |
| 349 | { |
| 350 | const struct ice1usb_tx_config *cfg = (const struct ice1usb_tx_config *) xfer->data; |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 351 | struct usb_ctrl_req *req = xfer->cb_ctx; |
| 352 | int port = _ifnum2port(req->wIndex); |
| 353 | struct usb_e1_state *usb_e1 = _get_state(port); |
| 354 | printf("set_tx_mode[%d] %02x%02x%02x%02x\r\n", port, |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 355 | xfer->data[0], xfer->data[1], xfer->data[2], xfer->data[3]); |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 356 | usb_e1->tx_cfg = *cfg; |
Sylvain Munaut | ace0932 | 2022-01-10 12:41:49 +0100 | [diff] [blame] | 357 | e1_tx_config(port, _tx_config_reg(cfg)); |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 358 | return true; |
| 359 | } |
| 360 | |
| 361 | static bool |
| 362 | _set_rx_mode_done(struct usb_xfer *xfer) |
| 363 | { |
| 364 | const struct ice1usb_rx_config *cfg = (const struct ice1usb_rx_config *) xfer->data; |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 365 | struct usb_ctrl_req *req = xfer->cb_ctx; |
| 366 | int port = _ifnum2port(req->wIndex); |
| 367 | struct usb_e1_state *usb_e1 = _get_state(port); |
| 368 | printf("set_rx_mode[%d] %02x\r\n", port, xfer->data[0]); |
| 369 | usb_e1->rx_cfg = *cfg; |
Sylvain Munaut | ace0932 | 2022-01-10 12:41:49 +0100 | [diff] [blame] | 370 | e1_rx_config(port, _rx_config_reg(cfg)); |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 371 | return true; |
| 372 | } |
| 373 | |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 374 | static enum usb_fnd_resp |
Sylvain Munaut | caf8cf9 | 2022-01-12 13:35:12 +0100 | [diff] [blame] | 375 | _e1_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer) |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 376 | { |
Harald Welte | 1b89f3b | 2022-01-13 10:03:42 +0100 | [diff] [blame] | 377 | const struct e1_error_count *cur_err; |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 378 | struct usb_e1_state *usb_e1; |
| 379 | int port; |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 380 | |
Sylvain Munaut | caf8cf9 | 2022-01-12 13:35:12 +0100 | [diff] [blame] | 381 | /* Check it's for an E1 interface */ |
| 382 | if (USB_REQ_TYPE_RCPT(req) != (USB_REQ_TYPE_VENDOR | USB_REQ_RCPT_INTF)) |
| 383 | return USB_FND_CONTINUE; |
| 384 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 385 | /* Get matching port (if any) */ |
| 386 | port = _ifnum2port(req->wIndex); |
| 387 | if (port < 0) |
| 388 | return USB_FND_CONTINUE; |
| 389 | |
| 390 | usb_e1 = _get_state(port); |
| 391 | |
| 392 | /* Process request */ |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 393 | switch (req->bRequest) { |
| 394 | case ICE1USB_INTF_GET_CAPABILITIES: |
| 395 | /* no optional capabilities yet */ |
| 396 | xfer->len = 0; |
| 397 | break; |
| 398 | case ICE1USB_INTF_SET_TX_CFG: |
| 399 | if (req->wLength < sizeof(struct ice1usb_tx_config)) |
| 400 | return USB_FND_ERROR; |
| 401 | xfer->cb_done = _set_tx_mode_done; |
| 402 | xfer->cb_ctx = req; |
| 403 | xfer->len = sizeof(struct ice1usb_tx_config); |
| 404 | break; |
| 405 | case ICE1USB_INTF_GET_TX_CFG: |
| 406 | if (req->wLength < sizeof(struct ice1usb_tx_config)) |
| 407 | return USB_FND_ERROR; |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 408 | memcpy(xfer->data, &usb_e1->tx_cfg, sizeof(struct ice1usb_tx_config)); |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 409 | xfer->len = sizeof(struct ice1usb_tx_config); |
| 410 | break; |
| 411 | case ICE1USB_INTF_SET_RX_CFG: |
| 412 | if (req->wLength < sizeof(struct ice1usb_rx_config)) |
| 413 | return USB_FND_ERROR; |
| 414 | xfer->cb_done = _set_rx_mode_done; |
| 415 | xfer->cb_ctx = req; |
| 416 | xfer->len = sizeof(struct ice1usb_rx_config); |
| 417 | break; |
| 418 | case ICE1USB_INTF_GET_RX_CFG: |
| 419 | if (req->wLength < sizeof(struct ice1usb_rx_config)) |
| 420 | return USB_FND_ERROR; |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 421 | memcpy(xfer->data, &usb_e1->rx_cfg, sizeof(struct ice1usb_rx_config)); |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 422 | xfer->len = sizeof(struct ice1usb_rx_config); |
| 423 | break; |
Harald Welte | 1b89f3b | 2022-01-13 10:03:42 +0100 | [diff] [blame] | 424 | case ICE1USB_INTF_GET_ERRORS: |
| 425 | if (req->wLength < sizeof(struct ice1usb_irq_err)) |
| 426 | return USB_FND_ERROR; |
| 427 | cur_err = e1_get_error_count(port); |
| 428 | _fill_irq_err((struct ice1usb_irq_err *)xfer->data, cur_err); |
| 429 | xfer->len = sizeof(struct ice1usb_irq_err); |
| 430 | break; |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 431 | default: |
| 432 | return USB_FND_ERROR; |
| 433 | } |
| 434 | |
| 435 | return USB_FND_SUCCESS; |
| 436 | } |
| 437 | |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 438 | |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 439 | static struct usb_fn_drv _e1_drv = { |
| 440 | .set_conf = _e1_set_conf, |
| 441 | .set_intf = _e1_set_intf, |
| 442 | .get_intf = _e1_get_intf, |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 443 | .ctrl_req = _e1_ctrl_req, |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | void |
| 447 | usb_e1_init(void) |
| 448 | { |
Sylvain Munaut | 0cc1613 | 2022-01-10 13:26:15 +0100 | [diff] [blame] | 449 | uint32_t rx_cr = _rx_config_reg(&rx_cfg_default); |
| 450 | uint32_t tx_cr = _tx_config_reg(&tx_cfg_default); |
| 451 | |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 452 | for (int i=0; i<2; i++) { |
| 453 | struct usb_e1_state *usb_e1 = _get_state(i); |
| 454 | memset(usb_e1, 0x00, sizeof(struct usb_e1_state)); |
| 455 | usb_e1->tx_cfg = tx_cfg_default; |
| 456 | usb_e1->rx_cfg = rx_cfg_default; |
Sylvain Munaut | 0cc1613 | 2022-01-10 13:26:15 +0100 | [diff] [blame] | 457 | e1_init(i, rx_cr, tx_cr); |
Sylvain Munaut | 41c98b6 | 2022-01-05 21:11:35 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 460 | usb_register_function_driver(&_e1_drv); |
| 461 | } |