blob: 421305b598d34e80953e1e5e816d0989ab8b2c71 [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>
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 "e1.h"
17#include "misc.h"
18
19struct {
20 bool running;
21 int in_bdi[2];
22} g_usb_e1;
23
24
25/* Hack */
26unsigned int e1_rx_need_data(int chan, unsigned int usb_addr, unsigned int max_len, unsigned int *pos);
27unsigned int e1_rx_level(int chan);
28uint8_t e1_get_pending_flags(int chan);
29/* ---- */
30
31bool
32usb_ep_boot(const struct usb_intf_desc *intf, uint8_t ep_addr, bool dual_bd);
33
34
35void
36usb_e1_run(void)
37{
38 int chan;
39 int bdi;
40
41 if (!g_usb_e1.running)
42 return;
43
44 /* EP[1-2] IN */
45 for (chan=0; chan<2; chan++)
46 {
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
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}
118enum usb_fnd_resp
119_e1_set_conf(const struct usb_conf_desc *conf)
120{
121 const struct usb_intf_desc *intf;
122
123 printf("e1 set_conf %08x\n", conf);
124 if (!conf)
125 return USB_FND_SUCCESS;
126
127 intf = _find_intf(conf, 0);
128 if (!intf)
129 return USB_FND_ERROR;
130
131 printf("e1 set_conf %08x\n", intf);
132
133 usb_ep_boot(intf, 0x81, true);
134 usb_ep_boot(intf, 0x82, true);
135
136 return USB_FND_SUCCESS;
137}
138
139enum usb_fnd_resp
140_e1_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel)
141{
142 if (base->bInterfaceNumber != 0)
143 return USB_FND_CONTINUE;
144
145 if (sel->bAlternateSetting == 0)
146 {
147 /* Already stopped ? */
148 if (!g_usb_e1.running)
149 return USB_FND_SUCCESS;
150
151 /* Update state */
152 g_usb_e1.running = false;
153
154 /* Stop E1 */
155 e1_stop();
156
157 /* Disable end-points */
158 usb_ep_regs[1].in.status = 0;
159 usb_ep_regs[2].in.status = 0;
160 }
161 else if (sel->bAlternateSetting == 1)
162 {
163 /* Already running ? */
164 if (g_usb_e1.running)
165 return USB_FND_SUCCESS;
166
167 /* Update state */
168 g_usb_e1.running = true;
169
170 /* Reset buffer pointers */
171 g_usb_e1.in_bdi[0] = 0;
172 g_usb_e1.in_bdi[1] = 0;
173
174 /* Configure EP1 IN / EP2 IN */
175 usb_ep_regs[1].in.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */
176 usb_ep_regs[2].in.status = USB_EP_TYPE_ISOC | USB_EP_BD_DUAL; /* Type=Isochronous, dual buffered */
177
178 /* EP1 IN: Prepare two buffers */
179 usb_ep_regs[1].in.bd[0].ptr = 256 + 0 * 388;
180 usb_ep_regs[1].in.bd[0].csr = 0;
181
182 usb_ep_regs[1].in.bd[1].ptr = 256 + 1 * 388;
183 usb_ep_regs[1].in.bd[1].csr = 0;
184
185 /* EP2 IN: Prepare two buffers */
186 usb_ep_regs[2].in.bd[0].ptr = 256 + 2 * 388;
187 usb_ep_regs[2].in.bd[0].csr = 0;
188
189 usb_ep_regs[2].in.bd[1].ptr = 256 + 3 * 388;
190 usb_ep_regs[2].in.bd[1].csr = 0;
191
192 /* Start E1 */
193 e1_start();
194 }
195 else
196 {
197 /* Unknown */
198 return USB_FND_ERROR;
199 }
200
201
202 return USB_FND_SUCCESS;
203}
204
205enum usb_fnd_resp
206_e1_get_intf(const struct usb_intf_desc *base, uint8_t *alt)
207{
208 if (base->bInterfaceNumber != 0)
209 return USB_FND_CONTINUE;
210
211 *alt = g_usb_e1.running ? 1 : 0;
212
213 return USB_FND_SUCCESS;
214}
215
216static struct usb_fn_drv _e1_drv = {
217 .set_conf = _e1_set_conf,
218 .set_intf = _e1_set_intf,
219 .get_intf = _e1_get_intf,
220};
221
222void
223usb_e1_init(void)
224{
225 /* Clear state */
226 memset(&g_usb_e1, 0x00, sizeof(g_usb_e1));
227
228 /* Install driver */
229 usb_register_function_driver(&_e1_drv);
230}