blob: f3f1538e342cebbc6bb8f80747e6c2daeccec13e [file] [log] [blame]
Sylvain Munautf5d7bf22020-09-14 10:23:50 +02001/*
2 * usb_e1.c
3 *
4 * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
Harald Weltef9280b02022-11-01 21:58:34 +01005 * Copyright (C) 2022 Harald Welte <laforge@osmocom.org>
Sylvain Munautf5d7bf22020-09-14 10:23:50 +02006 * 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"
19
20struct {
Harald Weltef9280b02022-11-01 21:58:34 +010021 bool running[2];
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020022 int in_bdi[2];
23} g_usb_e1;
24
25
26/* Hack */
27unsigned int e1_rx_need_data(int chan, unsigned int usb_addr, unsigned int max_len, unsigned int *pos);
28unsigned int e1_rx_level(int chan);
29uint8_t e1_get_pending_flags(int chan);
30/* ---- */
31
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020032
33void
34usb_e1_run(void)
35{
36 int chan;
37 int bdi;
38
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020039 /* EP[1-2] IN */
40 for (chan=0; chan<2; chan++)
41 {
Harald Weltef9280b02022-11-01 21:58:34 +010042 if (!g_usb_e1.running[chan])
43 continue;
44
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020045 bdi = g_usb_e1.in_bdi[chan];
46
47 while ((usb_ep_regs[1+chan].in.bd[bdi].csr & USB_BD_STATE_MSK) != USB_BD_STATE_RDY_DATA)
48 {
49 uint32_t ptr = usb_ep_regs[1+chan].in.bd[bdi].ptr;
50 uint32_t hdr;
51 unsigned int pos;
52
53 /* Error check */
54 if ((usb_ep_regs[1+chan].in.bd[bdi].csr & USB_BD_STATE_MSK) == USB_BD_STATE_DONE_ERR)
55 printf("Err EP%d IN\n", 1+chan);
56
57 /* Get some data from E1 */
58 int n = e1_rx_level(chan);
59
60// if (n > 64)
61// n = 12;
62// else if (n > 32)
63// n = 10;
64// else if (n > 8)
65// n = 8;
66 if (n > 12)
67 n = 12;
68 else if (!n)
69 break;
70
71 n = e1_rx_need_data(chan, (ptr >> 2) + 1, n, &pos);
72
73 /* Write header */
74 /* [31:12] (reserved) */
75 /* [11:10] CRC results (first new multiframe present in packet) */
76 /* [ 9: 8] CRC results (second new multiframe present in packet) */
77 /* [ 7: 5] Multiframe sequence number (first frame of packet) */
78 /* [ 4: 0] Position in multi-frame (first frame of packet) */
79 hdr = (pos & 0xff) | (e1_get_pending_flags(chan) << 24);
80 usb_data_write(ptr, &hdr, 4);
81 usb_ep_regs[1+chan].in.bd[bdi].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN((n * 32) + 4);
82
83 /* Next BDI */
84 bdi ^= 1;
85 g_usb_e1.in_bdi[chan] = bdi;
86 }
87 }
88}
89
90static const struct usb_intf_desc *
91_find_intf(const struct usb_conf_desc *conf, uint8_t idx)
92{
93 const struct usb_intf_desc *intf = NULL;
94 const void *sod, *eod;
95
96 if (!conf)
97 return NULL;
98
99 sod = conf;
100 eod = sod + conf->wTotalLength;
101
102 while (1) {
103 sod = usb_desc_find(sod, eod, USB_DT_INTF);
104 if (!sod)
105 break;
106
107 intf = (void*)sod;
108 if (intf->bInterfaceNumber == idx)
109 return intf;
110
111 sod = usb_desc_next(sod);
112 }
113
114 return NULL;
115}
Harald Weltef9280b02022-11-01 21:58:34 +0100116
117static const struct usb_conf_desc *last_conf;
118
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200119enum usb_fnd_resp
120_e1_set_conf(const struct usb_conf_desc *conf)
121{
122 const struct usb_intf_desc *intf;
123
124 printf("e1 set_conf %08x\n", conf);
Harald Weltef9280b02022-11-01 21:58:34 +0100125
126 last_conf = conf;
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200127 if (!conf)
128 return USB_FND_SUCCESS;
129
130 intf = _find_intf(conf, 0);
131 if (!intf)
132 return USB_FND_ERROR;
133
134 printf("e1 set_conf %08x\n", intf);
135
136 usb_ep_boot(intf, 0x81, true);
137 usb_ep_boot(intf, 0x82, true);
138
139 return USB_FND_SUCCESS;
140}
141
Harald Weltef9280b02022-11-01 21:58:34 +0100142static void
143disable_chan(int chan)
144{
145 /* Already stopped ? */
146 if (!g_usb_e1.running[chan])
147 return;
148
149 /* Update state */
150 g_usb_e1.running[chan] = false;
151
152 /* Stop E1 */
153 e1_stop(chan);
154
155 /* Disable end-points */
156 usb_ep_regs[chan+1].in.status = 0;
157}
158
159static void
160enable_chan(int chan)
161{
162 /* Already running ? */
163 if (g_usb_e1.running[chan])
164 return;
165
166 /* Update state */
167 g_usb_e1.running[chan] = true;
168
169 /* Reset buffer pointers */
170 g_usb_e1.in_bdi[chan] = 0;
171
172 /* Configure EP1 IN / EP2 IN */
173 usb_ep_regs[chan+1].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[chan+1].in.bd[0].ptr = 256 + (chan * 2 + 0) * 388;
177 usb_ep_regs[chan+1].in.bd[0].csr = 0;
178
179 usb_ep_regs[chan+1].in.bd[1].ptr = 256 + (chan * 2 + 1) * 388;
180 usb_ep_regs[chan+1].in.bd[1].csr = 0;
181
182 /* Start E1 */
183 e1_start(chan);
184}
185
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200186enum usb_fnd_resp
187_e1_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
188{
Harald Weltef9280b02022-11-01 21:58:34 +0100189 if (!last_conf || last_conf->bConfigurationValue == 1) {
190 /* Legacy Configuration */
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200191
Harald Weltef9280b02022-11-01 21:58:34 +0100192 if (base->bInterfaceNumber != 0)
193 return USB_FND_CONTINUE;
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200194
Harald Weltef9280b02022-11-01 21:58:34 +0100195 if (sel->bAlternateSetting == 0) {
196 disable_chan(0);
197 disable_chan(1);
198 } else if (sel->bAlternateSetting == 1) {
199 enable_chan(0);
200 enable_chan(1);
201 } else {
202 /* Unknown */
203 return USB_FND_ERROR;
204 }
205 } else if (last_conf && last_conf->bConfigurationValue == 2) {
206 /* e1d compatible configuration */
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200207
Harald Weltef9280b02022-11-01 21:58:34 +0100208 switch (base->bInterfaceNumber) {
209 case 0:
210 case 1:
211 switch (sel->bAlternateSetting) {
212 case 0:
213 disable_chan(base->bInterfaceNumber);
214 break;
215 case 1:
216 enable_chan(base->bInterfaceNumber);
217 break;
218 default:
219 /* Unknown */
220 return USB_FND_ERROR;
221 }
222 break;
223 default:
224 return USB_FND_CONTINUE;
225 }
226 } else {
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200227 return USB_FND_ERROR;
228 }
229
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200230 return USB_FND_SUCCESS;
231}
232
233enum usb_fnd_resp
234_e1_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
235{
Harald Weltef9280b02022-11-01 21:58:34 +0100236 if (!last_conf || last_conf->bConfigurationValue == 1) {
237 /* Legacy configuration */
238 if (base->bInterfaceNumber != 0)
239 return USB_FND_CONTINUE;
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200240
Harald Weltef9280b02022-11-01 21:58:34 +0100241 *alt = g_usb_e1.running[0] && g_usb_e1.running[1] ? 1 : 0;
242 } else if (last_conf && last_conf->bConfigurationValue == 2) {
243 /* e1d compatible configuration */
244 switch (base->bInterfaceNumber) {
245 case 0:
246 case 1:
247 *alt = g_usb_e1.running[base->bInterfaceNumber] ? 1 : 0;
248 break;
249 default:
250 return USB_FND_CONTINUE;
251 }
252 } else
253 return USB_FND_CONTINUE;
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200254
255 return USB_FND_SUCCESS;
256}
257
258static struct usb_fn_drv _e1_drv = {
259 .set_conf = _e1_set_conf,
260 .set_intf = _e1_set_intf,
261 .get_intf = _e1_get_intf,
262};
263
264void
265usb_e1_init(void)
266{
267 /* Clear state */
268 memset(&g_usb_e1, 0x00, sizeof(g_usb_e1));
269
270 /* Install driver */
271 usb_register_function_driver(&_e1_drv);
272}