blob: e37edc3f3e2f27883db9148831de8c47b169554f [file] [log] [blame]
Sylvain Munautbc9f5c42020-09-14 10:22:29 +02001/*
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 Welte2df1f802020-12-14 17:46:53 +010017#include "e1.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020018
19struct {
Harald Welte30fc5602020-12-14 15:56:28 +010020 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 Munautbc9f5c42020-09-14 10:22:29 +020023} g_usb_e1;
24
25
26/* Hack */
Harald Weltedaff4f62020-12-14 17:39:23 +010027unsigned int e1_rx_need_data(unsigned int usb_addr, unsigned int max_len, unsigned int *pos);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020028unsigned int e1_tx_feed_data(unsigned int usb_addr, unsigned int len);
29unsigned int e1_tx_level(void);
30unsigned int e1_rx_level(void);
31/* ---- */
32
33bool
34usb_ep_boot(const struct usb_intf_desc *intf, uint8_t ep_addr, bool dual_bd);
35
36static 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 Welte2b7dadf2020-12-14 15:56:15 +010044 /* Compute real E1 tick count (with safety against bad values) */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020045 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
65void
66usb_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 Welte2df1f802020-12-14 17:46:53 +010080 unsigned int pos;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020081
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 Welte2df1f802020-12-14 17:46:53 +010098 n = e1_rx_need_data((ptr >> 2) + 1, n, &pos);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020099
Harald Welte2df1f802020-12-14 17:46:53 +0100100 /* Write header: currently version and pos (mfr/fr number) */
101 hdr = (0 << 28) | (pos & 0xff);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200102 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
134refill:
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
154static 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 Weltec8271852020-12-14 13:48:09 +0100180
181static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200182_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 Weltec8271852020-12-14 13:48:09 +0100203static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200204_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 Weltec8271852020-12-14 13:48:09 +0100245static enum usb_fnd_resp
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200246_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
256static 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
262void
263usb_e1_init(void)
264{
265 memset(&g_usb_e1, 0x00, sizeof(g_usb_e1));
266 usb_register_function_driver(&_e1_drv);
267}