blob: 3507b82042035ec737211432a840ce6435e2f70d [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"
Harald Welte6cb6e6e2022-11-01 16:07:03 +010019#include "idt82v2081.h"
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020020
21struct {
Harald Weltef9280b02022-11-01 21:58:34 +010022 bool running[2];
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020023 int in_bdi[2];
Harald Welte6cb6e6e2022-11-01 16:07:03 +010024 struct idt82 idt82[2];
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020025} g_usb_e1;
26
27
28/* Hack */
29unsigned int e1_rx_need_data(int chan, unsigned int usb_addr, unsigned int max_len, unsigned int *pos);
30unsigned int e1_rx_level(int chan);
31uint8_t e1_get_pending_flags(int chan);
32/* ---- */
33
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020034
35void
36usb_e1_run(void)
37{
38 int chan;
39 int bdi;
40
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020041 /* EP[1-2] IN */
42 for (chan=0; chan<2; chan++)
43 {
Harald Weltef9280b02022-11-01 21:58:34 +010044 if (!g_usb_e1.running[chan])
45 continue;
46
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020047 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
92static 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 Weltef9280b02022-11-01 21:58:34 +0100118
119static const struct usb_conf_desc *last_conf;
120
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200121enum 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 Weltef9280b02022-11-01 21:58:34 +0100127
128 last_conf = conf;
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200129 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 Weltef9280b02022-11-01 21:58:34 +0100144static void
145disable_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
161static void
162enable_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 Munautf5d7bf22020-09-14 10:23:50 +0200188enum usb_fnd_resp
189_e1_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
190{
Harald Weltef9280b02022-11-01 21:58:34 +0100191 if (!last_conf || last_conf->bConfigurationValue == 1) {
192 /* Legacy Configuration */
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200193
Harald Weltef9280b02022-11-01 21:58:34 +0100194 if (base->bInterfaceNumber != 0)
195 return USB_FND_CONTINUE;
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200196
Harald Weltef9280b02022-11-01 21:58:34 +0100197 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 Munautf5d7bf22020-09-14 10:23:50 +0200209
Harald Weltef9280b02022-11-01 21:58:34 +0100210 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 Welte6cb6e6e2022-11-01 16:07:03 +0100218 idt82_init(&g_usb_e1.idt82[base->bInterfaceNumber], true);
Harald Weltef9280b02022-11-01 21:58:34 +0100219 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 Munautf5d7bf22020-09-14 10:23:50 +0200230 return USB_FND_ERROR;
231 }
232
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200233 return USB_FND_SUCCESS;
234}
235
236enum usb_fnd_resp
237_e1_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
238{
Harald Weltef9280b02022-11-01 21:58:34 +0100239 if (!last_conf || last_conf->bConfigurationValue == 1) {
240 /* Legacy configuration */
241 if (base->bInterfaceNumber != 0)
242 return USB_FND_CONTINUE;
Sylvain Munautf5d7bf22020-09-14 10:23:50 +0200243
Harald Weltef9280b02022-11-01 21:58:34 +0100244 *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 Munautf5d7bf22020-09-14 10:23:50 +0200257
258 return USB_FND_SUCCESS;
259}
260
261static 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
267void
268usb_e1_init(void)
269{
270 /* Clear state */
271 memset(&g_usb_e1, 0x00, sizeof(g_usb_e1));
Harald Welte6cb6e6e2022-11-01 16:07:03 +0100272 /* 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 Munautf5d7bf22020-09-14 10:23:50 +0200275
276 /* Install driver */
277 usb_register_function_driver(&_e1_drv);
278}