motg.c revision 1.31 1 /* $NetBSD: motg.c,v 1.31 2020/02/19 16:01:37 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2004, 2011, 2012, 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology, Jared D. McNeill (jmcneill (at) invisible.ca),
10 * Matthew R. Green (mrg (at) eterna.com.au), and Manuel Bouyer (bouyer (at) netbsd.org).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34
35 /*
36 * This file contains the driver for the Mentor Graphics Inventra USB
37 * 2.0 High Speed Dual-Role controller.
38 *
39 * NOTE: The current implementation only supports Device Side Mode!
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: motg.c,v 1.31 2020/02/19 16:01:37 riastradh Exp $");
44
45 #ifdef _KERNEL_OPT
46 #include "opt_usb.h"
47 #endif
48
49 #include <sys/param.h>
50
51 #include <sys/bus.h>
52 #include <sys/cpu.h>
53 #include <sys/device.h>
54 #include <sys/kernel.h>
55 #include <sys/kmem.h>
56 #include <sys/proc.h>
57 #include <sys/queue.h>
58 #include <sys/select.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61
62 #include <machine/endian.h>
63
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdivar.h>
67 #include <dev/usb/usb_mem.h>
68 #include <dev/usb/usbhist.h>
69
70 #include <dev/usb/motgreg.h>
71 #include <dev/usb/motgvar.h>
72 #include <dev/usb/usbroothub.h>
73
74 #ifdef USB_DEBUG
75 #ifndef MOTG_DEBUG
76 #define motgdebug 0
77 #else
78 int motgdebug = 0;
79
80 SYSCTL_SETUP(sysctl_hw_motg_setup, "sysctl hw.motg setup")
81 {
82 int err;
83 const struct sysctlnode *rnode;
84 const struct sysctlnode *cnode;
85
86 err = sysctl_createv(clog, 0, NULL, &rnode,
87 CTLFLAG_PERMANENT, CTLTYPE_NODE, "motg",
88 SYSCTL_DESCR("motg global controls"),
89 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
90
91 if (err)
92 goto fail;
93
94 /* control debugging printfs */
95 err = sysctl_createv(clog, 0, &rnode, &cnode,
96 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
97 "debug", SYSCTL_DESCR("Enable debugging output"),
98 NULL, 0, &motgdebug, sizeof(motgdebug), CTL_CREATE, CTL_EOL);
99 if (err)
100 goto fail;
101
102 return;
103 fail:
104 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
105 }
106
107 #endif /* MOTG_DEBUG */
108 #endif /* USB_DEBUG */
109
110 #define MD_ROOT 0x0002
111 #define MD_CTRL 0x0004
112 #define MD_BULK 0x0008
113
114 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(motgdebug,1,FMT,A,B,C,D)
115 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGM(motgdebug,N,FMT,A,B,C,D)
116 #define MOTGHIST_FUNC() USBHIST_FUNC()
117 #define MOTGHIST_CALLED(name) USBHIST_CALLED(motgdebug)
118
119
120 /* various timeouts, for various speeds */
121 /* control NAK timeouts */
122 #define NAK_TO_CTRL 10 /* 1024 frames, about 1s */
123 #define NAK_TO_CTRL_HIGH 13 /* 8k microframes, about 0.8s */
124
125 /* intr/iso polling intervals */
126 #define POLL_TO 100 /* 100 frames, about 0.1s */
127 #define POLL_TO_HIGH 10 /* 100 microframes, about 0.12s */
128
129 /* bulk NAK timeouts */
130 #define NAK_TO_BULK 0 /* disabled */
131 #define NAK_TO_BULK_HIGH 0
132
133 static void motg_hub_change(struct motg_softc *);
134
135 static usbd_status motg_root_intr_transfer(struct usbd_xfer *);
136 static usbd_status motg_root_intr_start(struct usbd_xfer *);
137 static void motg_root_intr_abort(struct usbd_xfer *);
138 static void motg_root_intr_close(struct usbd_pipe *);
139 static void motg_root_intr_done(struct usbd_xfer *);
140
141 static usbd_status motg_open(struct usbd_pipe *);
142 static void motg_poll(struct usbd_bus *);
143 static void motg_softintr(void *);
144 static struct usbd_xfer *
145 motg_allocx(struct usbd_bus *, unsigned int);
146 static void motg_freex(struct usbd_bus *, struct usbd_xfer *);
147 static bool motg_dying(struct usbd_bus *);
148 static void motg_get_lock(struct usbd_bus *, kmutex_t **);
149 static int motg_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
150 void *, int);
151
152 static void motg_noop(struct usbd_pipe *pipe);
153 static usbd_status motg_portreset(struct motg_softc*);
154
155 static usbd_status motg_device_ctrl_transfer(struct usbd_xfer *);
156 static usbd_status motg_device_ctrl_start(struct usbd_xfer *);
157 static void motg_device_ctrl_abort(struct usbd_xfer *);
158 static void motg_device_ctrl_close(struct usbd_pipe *);
159 static void motg_device_ctrl_done(struct usbd_xfer *);
160 static usbd_status motg_device_ctrl_start1(struct motg_softc *);
161 static void motg_device_ctrl_read(struct usbd_xfer *);
162 static void motg_device_ctrl_intr_rx(struct motg_softc *);
163 static void motg_device_ctrl_intr_tx(struct motg_softc *);
164
165 static usbd_status motg_device_data_transfer(struct usbd_xfer *);
166 static usbd_status motg_device_data_start(struct usbd_xfer *);
167 static usbd_status motg_device_data_start1(struct motg_softc *,
168 struct motg_hw_ep *);
169 static void motg_device_data_abort(struct usbd_xfer *);
170 static void motg_device_data_close(struct usbd_pipe *);
171 static void motg_device_data_done(struct usbd_xfer *);
172 static void motg_device_intr_rx(struct motg_softc *, int);
173 static void motg_device_intr_tx(struct motg_softc *, int);
174 static void motg_device_data_read(struct usbd_xfer *);
175 static void motg_device_data_write(struct usbd_xfer *);
176
177 static void motg_device_clear_toggle(struct usbd_pipe *);
178 static void motg_abortx(struct usbd_xfer *);
179
180 #define UBARR(sc) bus_space_barrier((sc)->sc_iot, (sc)->sc_ioh, 0, (sc)->sc_size, \
181 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
182 #define UWRITE1(sc, r, x) \
183 do { UBARR(sc); bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \
184 } while (/*CONSTCOND*/0)
185 #define UWRITE2(sc, r, x) \
186 do { UBARR(sc); bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \
187 } while (/*CONSTCOND*/0)
188 #define UWRITE4(sc, r, x) \
189 do { UBARR(sc); bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \
190 } while (/*CONSTCOND*/0)
191
192 static __inline uint32_t
193 UREAD1(struct motg_softc *sc, bus_size_t r)
194 {
195
196 UBARR(sc);
197 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, r);
198 }
199 static __inline uint32_t
200 UREAD2(struct motg_softc *sc, bus_size_t r)
201 {
202
203 UBARR(sc);
204 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, r);
205 }
206
207 #if 0
208 static __inline uint32_t
209 UREAD4(struct motg_softc *sc, bus_size_t r)
210 {
211
212 UBARR(sc);
213 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
214 }
215 #endif
216
217 static void
218 musbotg_pull_common(struct motg_softc *sc, uint8_t on)
219 {
220 uint8_t val;
221
222 val = UREAD1(sc, MUSB2_REG_POWER);
223 if (on)
224 val |= MUSB2_MASK_SOFTC;
225 else
226 val &= ~MUSB2_MASK_SOFTC;
227
228 UWRITE1(sc, MUSB2_REG_POWER, val);
229 }
230
231 const struct usbd_bus_methods motg_bus_methods = {
232 .ubm_open = motg_open,
233 .ubm_softint = motg_softintr,
234 .ubm_dopoll = motg_poll,
235 .ubm_allocx = motg_allocx,
236 .ubm_freex = motg_freex,
237 .ubm_abortx = motg_abortx,
238 .ubm_dying = motg_dying,
239 .ubm_getlock = motg_get_lock,
240 .ubm_rhctrl = motg_roothub_ctrl,
241 };
242
243 const struct usbd_pipe_methods motg_root_intr_methods = {
244 .upm_transfer = motg_root_intr_transfer,
245 .upm_start = motg_root_intr_start,
246 .upm_abort = motg_root_intr_abort,
247 .upm_close = motg_root_intr_close,
248 .upm_cleartoggle = motg_noop,
249 .upm_done = motg_root_intr_done,
250 };
251
252 const struct usbd_pipe_methods motg_device_ctrl_methods = {
253 .upm_transfer = motg_device_ctrl_transfer,
254 .upm_start = motg_device_ctrl_start,
255 .upm_abort = motg_device_ctrl_abort,
256 .upm_close = motg_device_ctrl_close,
257 .upm_cleartoggle = motg_noop,
258 .upm_done = motg_device_ctrl_done,
259 };
260
261 const struct usbd_pipe_methods motg_device_data_methods = {
262 .upm_transfer = motg_device_data_transfer,
263 .upm_start = motg_device_data_start,
264 .upm_abort = motg_device_data_abort,
265 .upm_close = motg_device_data_close,
266 .upm_cleartoggle = motg_device_clear_toggle,
267 .upm_done = motg_device_data_done,
268 };
269
270 int
271 motg_init(struct motg_softc *sc)
272 {
273 uint32_t nrx, ntx, val;
274 int dynfifo;
275 int offset, i;
276
277 MOTGHIST_FUNC(); MOTGHIST_CALLED();
278
279 if (sc->sc_mode == MOTG_MODE_DEVICE)
280 return ENOTSUP; /* not supported */
281
282 /* disable all interrupts */
283 UWRITE1(sc, MUSB2_REG_INTUSBE, 0);
284 UWRITE2(sc, MUSB2_REG_INTTXE, 0);
285 UWRITE2(sc, MUSB2_REG_INTRXE, 0);
286 /* disable pullup */
287
288 musbotg_pull_common(sc, 0);
289
290 #ifdef MUSB2_REG_RXDBDIS
291 /* disable double packet buffering XXX what's this ? */
292 UWRITE2(sc, MUSB2_REG_RXDBDIS, 0xFFFF);
293 UWRITE2(sc, MUSB2_REG_TXDBDIS, 0xFFFF);
294 #endif
295
296 /* enable HighSpeed and ISO Update flags */
297
298 UWRITE1(sc, MUSB2_REG_POWER,
299 MUSB2_MASK_HSENAB | MUSB2_MASK_ISOUPD);
300
301 if (sc->sc_mode == MOTG_MODE_DEVICE) {
302 /* clear Session bit, if set */
303 val = UREAD1(sc, MUSB2_REG_DEVCTL);
304 val &= ~MUSB2_MASK_SESS;
305 UWRITE1(sc, MUSB2_REG_DEVCTL, val);
306 } else {
307 /* Enter session for Host mode */
308 val = UREAD1(sc, MUSB2_REG_DEVCTL);
309 val |= MUSB2_MASK_SESS;
310 UWRITE1(sc, MUSB2_REG_DEVCTL, val);
311 }
312 delay(1000);
313 DPRINTF("DEVCTL 0x%jx", UREAD1(sc, MUSB2_REG_DEVCTL), 0, 0, 0);
314
315 /* disable testmode */
316
317 UWRITE1(sc, MUSB2_REG_TESTMODE, 0);
318
319 #ifdef MUSB2_REG_MISC
320 /* set default value */
321
322 UWRITE1(sc, MUSB2_REG_MISC, 0);
323 #endif
324
325 /* select endpoint index 0 */
326
327 UWRITE1(sc, MUSB2_REG_EPINDEX, 0);
328
329 if (sc->sc_ep_max == 0) {
330 /* read out number of endpoints */
331 nrx = (UREAD1(sc, MUSB2_REG_EPINFO) / 16);
332
333 ntx = (UREAD1(sc, MUSB2_REG_EPINFO) % 16);
334
335 /* these numbers exclude the control endpoint */
336
337 DPRINTFN(1,"RX/TX endpoints: %ju/%ju", nrx, ntx, 0, 0);
338
339 sc->sc_ep_max = MAX(nrx, ntx);
340 } else {
341 nrx = ntx = sc->sc_ep_max;
342 }
343 if (sc->sc_ep_max == 0) {
344 aprint_error_dev(sc->sc_dev, " no endpoints\n");
345 return -1;
346 }
347 KASSERT(sc->sc_ep_max <= MOTG_MAX_HW_EP);
348 /* read out configuration data */
349 val = UREAD1(sc, MUSB2_REG_CONFDATA);
350
351 DPRINTF("Config Data: 0x%02jx", val, 0, 0, 0);
352
353 dynfifo = (val & MUSB2_MASK_CD_DYNFIFOSZ) ? 1 : 0;
354
355 if (dynfifo) {
356 aprint_normal_dev(sc->sc_dev, "Dynamic FIFO sizing detected, "
357 "assuming 16Kbytes of FIFO RAM\n");
358 }
359
360 DPRINTF("HW version: 0x%04jx\n", UREAD1(sc, MUSB2_REG_HWVERS), 0, 0, 0);
361
362 /* initialise endpoint profiles */
363 sc->sc_in_ep[0].ep_fifo_size = 64;
364 sc->sc_out_ep[0].ep_fifo_size = 0; /* not used */
365 sc->sc_out_ep[0].ep_number = sc->sc_in_ep[0].ep_number = 0;
366 SIMPLEQ_INIT(&sc->sc_in_ep[0].ep_pipes);
367 offset = 64;
368
369 for (i = 1; i <= sc->sc_ep_max; i++) {
370 int fiforx_size, fifotx_size, fifo_size;
371
372 /* select endpoint */
373 UWRITE1(sc, MUSB2_REG_EPINDEX, i);
374
375 if (sc->sc_ep_fifosize) {
376 fiforx_size = fifotx_size = sc->sc_ep_fifosize;
377 } else {
378 val = UREAD1(sc, MUSB2_REG_FSIZE);
379 fiforx_size = (val & MUSB2_MASK_RX_FSIZE) >> 4;
380 fifotx_size = (val & MUSB2_MASK_TX_FSIZE);
381 }
382
383 DPRINTF("Endpoint %ju FIFO size: IN=%ju, OUT=%ju, DYN=%jd",
384 i, fifotx_size, fiforx_size, dynfifo);
385
386 if (dynfifo) {
387 if (sc->sc_ep_fifosize) {
388 fifo_size = ffs(sc->sc_ep_fifosize) - 1;
389 } else {
390 if (i < 3) {
391 fifo_size = 12; /* 4K */
392 } else if (i < 10) {
393 fifo_size = 10; /* 1K */
394 } else {
395 fifo_size = 7; /* 128 bytes */
396 }
397 }
398 if (fiforx_size && (i <= nrx)) {
399 fiforx_size = fifo_size;
400 if (fifo_size > 7) {
401 #if 0
402 UWRITE1(sc, MUSB2_REG_RXFIFOSZ,
403 MUSB2_VAL_FIFOSZ(fifo_size) |
404 MUSB2_MASK_FIFODB);
405 #else
406 UWRITE1(sc, MUSB2_REG_RXFIFOSZ,
407 MUSB2_VAL_FIFOSZ(fifo_size));
408 #endif
409 } else {
410 UWRITE1(sc, MUSB2_REG_RXFIFOSZ,
411 MUSB2_VAL_FIFOSZ(fifo_size));
412 }
413 UWRITE2(sc, MUSB2_REG_RXFIFOADD,
414 offset >> 3);
415 offset += (1 << fiforx_size);
416 }
417 if (fifotx_size && (i <= ntx)) {
418 fifotx_size = fifo_size;
419 if (fifo_size > 7) {
420 #if 0
421 UWRITE1(sc, MUSB2_REG_TXFIFOSZ,
422 MUSB2_VAL_FIFOSZ(fifo_size) |
423 MUSB2_MASK_FIFODB);
424 #else
425 UWRITE1(sc, MUSB2_REG_TXFIFOSZ,
426 MUSB2_VAL_FIFOSZ(fifo_size));
427 #endif
428 } else {
429 UWRITE1(sc, MUSB2_REG_TXFIFOSZ,
430 MUSB2_VAL_FIFOSZ(fifo_size));
431 }
432
433 UWRITE2(sc, MUSB2_REG_TXFIFOADD,
434 offset >> 3);
435
436 offset += (1 << fifotx_size);
437 }
438 }
439 if (fiforx_size && (i <= nrx)) {
440 sc->sc_in_ep[i].ep_fifo_size = (1 << fiforx_size);
441 SIMPLEQ_INIT(&sc->sc_in_ep[i].ep_pipes);
442 }
443 if (fifotx_size && (i <= ntx)) {
444 sc->sc_out_ep[i].ep_fifo_size = (1 << fifotx_size);
445 SIMPLEQ_INIT(&sc->sc_out_ep[i].ep_pipes);
446 }
447 sc->sc_out_ep[i].ep_number = sc->sc_in_ep[i].ep_number = i;
448 }
449
450
451 DPRINTF("Dynamic FIFO size = %jd bytes", offset, 0, 0, 0);
452
453 /* turn on default interrupts */
454
455 if (sc->sc_mode == MOTG_MODE_HOST) {
456 UWRITE1(sc, MUSB2_REG_INTUSBE, 0xff);
457 UWRITE2(sc, MUSB2_REG_INTTXE, 0xffff);
458 UWRITE2(sc, MUSB2_REG_INTRXE, 0xffff);
459 } else
460 UWRITE1(sc, MUSB2_REG_INTUSBE, MUSB2_MASK_IRESET);
461
462 sc->sc_xferpool = pool_cache_init(sizeof(struct motg_xfer), 0, 0, 0,
463 "motgxfer", NULL, IPL_USB, NULL, NULL, NULL);
464
465 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
466 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
467
468 /* Set up the bus struct. */
469 sc->sc_bus.ub_methods = &motg_bus_methods;
470 sc->sc_bus.ub_pipesize= sizeof(struct motg_pipe);
471 sc->sc_bus.ub_revision = USBREV_2_0;
472 sc->sc_bus.ub_usedma = false;
473 sc->sc_bus.ub_hcpriv = sc;
474 sc->sc_child = config_found(sc->sc_dev, &sc->sc_bus, usbctlprint);
475 return 0;
476 }
477
478 static int
479 motg_select_ep(struct motg_softc *sc, struct usbd_pipe *pipe)
480 {
481 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe);
482 usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
483 struct motg_hw_ep *ep;
484 int i, size;
485
486 MOTGHIST_FUNC(); MOTGHIST_CALLED();
487
488 ep = (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) ?
489 sc->sc_in_ep : sc->sc_out_ep;
490 size = UE_GET_SIZE(UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize));
491
492 for (i = sc->sc_ep_max; i >= 1; i--) {
493 DPRINTF(UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ?
494 "in_ep[%jd].ep_fifo_size %jd size %jd ref %jd" :
495 "out_ep[%jd].ep_fifo_size %jd size %jd ref %jd", i,
496 ep[i].ep_fifo_size, size, ep[i].refcount);
497 if (ep[i].ep_fifo_size >= size) {
498 /* found a suitable endpoint */
499 otgpipe->hw_ep = &ep[i];
500 mutex_enter(&sc->sc_lock);
501 if (otgpipe->hw_ep->refcount > 0) {
502 /* no luck, try next */
503 mutex_exit(&sc->sc_lock);
504 otgpipe->hw_ep = NULL;
505 } else {
506 otgpipe->hw_ep->refcount++;
507 SIMPLEQ_INSERT_TAIL(&otgpipe->hw_ep->ep_pipes,
508 otgpipe, ep_pipe_list);
509 mutex_exit(&sc->sc_lock);
510 return 0;
511 }
512 }
513 }
514 return -1;
515 }
516
517 /* Open a new pipe. */
518 usbd_status
519 motg_open(struct usbd_pipe *pipe)
520 {
521 struct motg_softc *sc = MOTG_PIPE2SC(pipe);
522 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe);
523 usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
524 uint8_t rhaddr = pipe->up_dev->ud_bus->ub_rhaddr;
525
526 MOTGHIST_FUNC(); MOTGHIST_CALLED();
527
528 DPRINTF("pipe=%#jx, addr=%jd, endpt=%jd (%jd)", (uintptr_t)pipe,
529 pipe->up_dev->ud_addr, ed->bEndpointAddress, rhaddr);
530
531 if (sc->sc_dying)
532 return USBD_IOERROR;
533
534 /* toggle state needed for bulk endpoints */
535 otgpipe->nexttoggle = pipe->up_endpoint->ue_toggle;
536
537 if (pipe->up_dev->ud_addr == rhaddr) {
538 switch (ed->bEndpointAddress) {
539 case USB_CONTROL_ENDPOINT:
540 pipe->up_methods = &roothub_ctrl_methods;
541 break;
542 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
543 pipe->up_methods = &motg_root_intr_methods;
544 break;
545 default:
546 return USBD_INVAL;
547 }
548 } else {
549 switch (ed->bmAttributes & UE_XFERTYPE) {
550 case UE_CONTROL:
551 pipe->up_methods = &motg_device_ctrl_methods;
552 /* always use sc_in_ep[0] for in and out */
553 otgpipe->hw_ep = &sc->sc_in_ep[0];
554 mutex_enter(&sc->sc_lock);
555 otgpipe->hw_ep->refcount++;
556 SIMPLEQ_INSERT_TAIL(&otgpipe->hw_ep->ep_pipes,
557 otgpipe, ep_pipe_list);
558 mutex_exit(&sc->sc_lock);
559 break;
560 case UE_BULK:
561 case UE_INTERRUPT:
562 DPRINTFN(MD_BULK,
563 "type %jd dir %jd pipe wMaxPacketSize %jd",
564 UE_GET_XFERTYPE(ed->bmAttributes),
565 UE_GET_DIR(pipe->up_endpoint->ue_edesc->bEndpointAddress),
566 UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize), 0);
567 if (motg_select_ep(sc, pipe) != 0)
568 goto bad;
569 KASSERT(otgpipe->hw_ep != NULL);
570 pipe->up_methods = &motg_device_data_methods;
571 otgpipe->nexttoggle = pipe->up_endpoint->ue_toggle;
572 break;
573 default:
574 goto bad;
575 #ifdef notyet
576 case UE_ISOCHRONOUS:
577 ...
578 break;
579 #endif /* notyet */
580 }
581 }
582 return USBD_NORMAL_COMPLETION;
583
584 bad:
585 return USBD_NOMEM;
586 }
587
588 void
589 motg_softintr(void *v)
590 {
591 struct usbd_bus *bus = v;
592 struct motg_softc *sc = MOTG_BUS2SC(bus);
593 uint16_t rx_status, tx_status;
594 uint8_t ctrl_status;
595 uint32_t val;
596 int i;
597
598 MOTGHIST_FUNC(); MOTGHIST_CALLED();
599
600 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
601
602 DPRINTFN(MD_ROOT | MD_CTRL, "sc %#jx", (uintptr_t)sc, 0 ,0 ,0);
603
604 mutex_spin_enter(&sc->sc_intr_lock);
605 rx_status = sc->sc_intr_rx_ep;
606 sc->sc_intr_rx_ep = 0;
607 tx_status = sc->sc_intr_tx_ep;
608 sc->sc_intr_tx_ep = 0;
609 ctrl_status = sc->sc_intr_ctrl;
610 sc->sc_intr_ctrl = 0;
611 mutex_spin_exit(&sc->sc_intr_lock);
612
613 ctrl_status |= UREAD1(sc, MUSB2_REG_INTUSB);
614
615 if (ctrl_status & (MUSB2_MASK_IRESET |
616 MUSB2_MASK_IRESUME | MUSB2_MASK_ISUSP |
617 MUSB2_MASK_ICONN | MUSB2_MASK_IDISC)) {
618 DPRINTFN(MD_ROOT | MD_CTRL, "bus 0x%jx", ctrl_status, 0, 0, 0);
619
620 if (ctrl_status & MUSB2_MASK_IRESET) {
621 sc->sc_isreset = 1;
622 sc->sc_port_suspended = 0;
623 sc->sc_port_suspended_change = 1;
624 sc->sc_connected_changed = 1;
625 sc->sc_port_enabled = 1;
626
627 val = UREAD1(sc, MUSB2_REG_POWER);
628 if (val & MUSB2_MASK_HSMODE)
629 sc->sc_high_speed = 1;
630 else
631 sc->sc_high_speed = 0;
632 DPRINTFN(MD_ROOT | MD_CTRL, "speed %jd", sc->sc_high_speed,
633 0, 0, 0);
634
635 /* turn off interrupts */
636 val = MUSB2_MASK_IRESET;
637 val &= ~MUSB2_MASK_IRESUME;
638 val |= MUSB2_MASK_ISUSP;
639 UWRITE1(sc, MUSB2_REG_INTUSBE, val);
640 UWRITE2(sc, MUSB2_REG_INTTXE, 0);
641 UWRITE2(sc, MUSB2_REG_INTRXE, 0);
642 }
643 if (ctrl_status & MUSB2_MASK_IRESUME) {
644 if (sc->sc_port_suspended) {
645 sc->sc_port_suspended = 0;
646 sc->sc_port_suspended_change = 1;
647 val = UREAD1(sc, MUSB2_REG_INTUSBE);
648 /* disable resume interrupt */
649 val &= ~MUSB2_MASK_IRESUME;
650 /* enable suspend interrupt */
651 val |= MUSB2_MASK_ISUSP;
652 UWRITE1(sc, MUSB2_REG_INTUSBE, val);
653 }
654 } else if (ctrl_status & MUSB2_MASK_ISUSP) {
655 if (!sc->sc_port_suspended) {
656 sc->sc_port_suspended = 1;
657 sc->sc_port_suspended_change = 1;
658
659 val = UREAD1(sc, MUSB2_REG_INTUSBE);
660 /* disable suspend interrupt */
661 val &= ~MUSB2_MASK_ISUSP;
662 /* enable resume interrupt */
663 val |= MUSB2_MASK_IRESUME;
664 UWRITE1(sc, MUSB2_REG_INTUSBE, val);
665 }
666 }
667 if (ctrl_status & MUSB2_MASK_ICONN) {
668 sc->sc_connected = 1;
669 sc->sc_connected_changed = 1;
670 sc->sc_isreset = 1;
671 sc->sc_port_enabled = 1;
672 } else if (ctrl_status & MUSB2_MASK_IDISC) {
673 sc->sc_connected = 0;
674 sc->sc_connected_changed = 1;
675 sc->sc_isreset = 0;
676 sc->sc_port_enabled = 0;
677 }
678
679 /* complete root HUB interrupt endpoint */
680
681 motg_hub_change(sc);
682 }
683 /*
684 * read in interrupt status and mix with the status we
685 * got from the wrapper
686 */
687 rx_status |= UREAD2(sc, MUSB2_REG_INTRX);
688 tx_status |= UREAD2(sc, MUSB2_REG_INTTX);
689
690 KASSERTMSG((rx_status & 0x01) == 0, "ctrl_rx %08x", rx_status);
691 if (tx_status & 0x01)
692 motg_device_ctrl_intr_tx(sc);
693 for (i = 1; i <= sc->sc_ep_max; i++) {
694 if (rx_status & (0x01 << i))
695 motg_device_intr_rx(sc, i);
696 if (tx_status & (0x01 << i))
697 motg_device_intr_tx(sc, i);
698 }
699 return;
700 }
701
702 void
703 motg_poll(struct usbd_bus *bus)
704 {
705 struct motg_softc *sc = MOTG_BUS2SC(bus);
706
707 sc->sc_intr_poll(sc->sc_intr_poll_arg);
708 mutex_enter(&sc->sc_lock);
709 motg_softintr(bus);
710 mutex_exit(&sc->sc_lock);
711 }
712
713 int
714 motg_intr(struct motg_softc *sc, uint16_t rx_ep, uint16_t tx_ep,
715 uint8_t ctrl)
716 {
717 KASSERT(mutex_owned(&sc->sc_intr_lock));
718 sc->sc_intr_tx_ep = tx_ep;
719 sc->sc_intr_rx_ep = rx_ep;
720 sc->sc_intr_ctrl = ctrl;
721
722 if (!sc->sc_bus.ub_usepolling) {
723 usb_schedsoftintr(&sc->sc_bus);
724 }
725 return 1;
726 }
727
728 int
729 motg_intr_vbus(struct motg_softc *sc, int vbus)
730 {
731 uint8_t val;
732 MOTGHIST_FUNC(); MOTGHIST_CALLED();
733
734 if (sc->sc_mode == MOTG_MODE_HOST && vbus == 0) {
735 DPRINTF("vbus down, try to re-enable", 0, 0, 0, 0);
736 /* try to re-enter session for Host mode */
737 val = UREAD1(sc, MUSB2_REG_DEVCTL);
738 val |= MUSB2_MASK_SESS;
739 UWRITE1(sc, MUSB2_REG_DEVCTL, val);
740 }
741 return 1;
742 }
743
744 struct usbd_xfer *
745 motg_allocx(struct usbd_bus *bus, unsigned int nframes)
746 {
747 struct motg_softc *sc = MOTG_BUS2SC(bus);
748 struct usbd_xfer *xfer;
749
750 xfer = pool_cache_get(sc->sc_xferpool, PR_WAITOK);
751 if (xfer != NULL) {
752 memset(xfer, 0, sizeof(struct motg_xfer));
753 #ifdef DIAGNOSTIC
754 xfer->ux_state = XFER_BUSY;
755 #endif
756 }
757 return xfer;
758 }
759
760 void
761 motg_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
762 {
763 struct motg_softc *sc = MOTG_BUS2SC(bus);
764
765 #ifdef DIAGNOSTIC
766 if (xfer->ux_state != XFER_BUSY &&
767 xfer->ux_status != USBD_NOT_STARTED) {
768 printf("motg_freex: xfer=%p not busy, 0x%08x\n", xfer,
769 xfer->ux_state);
770 }
771 xfer->ux_state = XFER_FREE;
772 #endif
773 pool_cache_put(sc->sc_xferpool, xfer);
774 }
775
776 static bool
777 motg_dying(struct usbd_bus *bus)
778 {
779 struct motg_softc *sc = MOTG_BUS2SC(bus);
780
781 return sc->sc_dying;
782 }
783
784 static void
785 motg_get_lock(struct usbd_bus *bus, kmutex_t **lock)
786 {
787 struct motg_softc *sc = MOTG_BUS2SC(bus);
788
789 *lock = &sc->sc_lock;
790 }
791
792 /*
793 * Routines to emulate the root hub.
794 */
795 Static int
796 motg_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
797 void *buf, int buflen)
798 {
799 struct motg_softc *sc = MOTG_BUS2SC(bus);
800 int status, change, totlen = 0;
801 uint16_t len, value, index;
802 usb_port_status_t ps;
803 usbd_status err;
804 uint32_t val;
805
806 MOTGHIST_FUNC(); MOTGHIST_CALLED();
807
808 if (sc->sc_dying)
809 return -1;
810
811 DPRINTFN(MD_ROOT, "type=0x%02jx request=%02jx", req->bmRequestType,
812 req->bRequest, 0, 0);
813
814 len = UGETW(req->wLength);
815 value = UGETW(req->wValue);
816 index = UGETW(req->wIndex);
817
818 #define C(x,y) ((x) | ((y) << 8))
819 switch (C(req->bRequest, req->bmRequestType)) {
820 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
821 DPRINTFN(MD_ROOT, "wValue=0x%04jx", value, 0, 0, 0);
822 switch (value) {
823 #define sd ((usb_string_descriptor_t *)buf)
824 case C(2, UDESC_STRING):
825 /* Product */
826 totlen = usb_makestrdesc(sd, len, "MOTG root hub");
827 break;
828 #undef sd
829 default:
830 /* default from usbroothub */
831 return buflen;
832 }
833 break;
834 /* Hub requests */
835 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
836 break;
837 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
838 DPRINTFN(MD_ROOT,
839 "UR_CLEAR_PORT_FEATURE port=%jd feature=%jd", index, value,
840 0, 0);
841 if (index != 1) {
842 return -1;
843 }
844 switch (value) {
845 case UHF_PORT_ENABLE:
846 sc->sc_port_enabled = 0;
847 break;
848 case UHF_PORT_SUSPEND:
849 if (sc->sc_port_suspended != 0) {
850 val = UREAD1(sc, MUSB2_REG_POWER);
851 val &= ~MUSB2_MASK_SUSPMODE;
852 val |= MUSB2_MASK_RESUME;
853 UWRITE1(sc, MUSB2_REG_POWER, val);
854 /* wait 20 milliseconds */
855 usb_delay_ms(&sc->sc_bus, 20);
856 val = UREAD1(sc, MUSB2_REG_POWER);
857 val &= ~MUSB2_MASK_RESUME;
858 UWRITE1(sc, MUSB2_REG_POWER, val);
859 sc->sc_port_suspended = 0;
860 sc->sc_port_suspended_change = 1;
861 }
862 break;
863 case UHF_PORT_RESET:
864 break;
865 case UHF_C_PORT_CONNECTION:
866 break;
867 case UHF_C_PORT_ENABLE:
868 break;
869 case UHF_C_PORT_OVER_CURRENT:
870 break;
871 case UHF_C_PORT_RESET:
872 sc->sc_isreset = 0;
873 break;
874 case UHF_PORT_POWER:
875 /* XXX todo */
876 break;
877 case UHF_PORT_CONNECTION:
878 case UHF_PORT_OVER_CURRENT:
879 case UHF_PORT_LOW_SPEED:
880 case UHF_C_PORT_SUSPEND:
881 default:
882 return -1;
883 }
884 break;
885 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
886 return -1;
887 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
888 if (len == 0)
889 break;
890 if ((value & 0xff) != 0) {
891 return -1;
892 }
893 totlen = buflen;
894 break;
895 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
896 if (len != 4) {
897 return -1;
898 }
899 memset(buf, 0, len);
900 totlen = len;
901 break;
902 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
903 if (index != 1) {
904 return -1;
905 }
906 if (len != 4) {
907 return -1;
908 }
909 status = change = 0;
910 if (sc->sc_connected)
911 status |= UPS_CURRENT_CONNECT_STATUS;
912 if (sc->sc_connected_changed) {
913 change |= UPS_C_CONNECT_STATUS;
914 sc->sc_connected_changed = 0;
915 }
916 if (sc->sc_port_enabled)
917 status |= UPS_PORT_ENABLED;
918 if (sc->sc_port_enabled_changed) {
919 change |= UPS_C_PORT_ENABLED;
920 sc->sc_port_enabled_changed = 0;
921 }
922 if (sc->sc_port_suspended)
923 status |= UPS_SUSPEND;
924 if (sc->sc_high_speed)
925 status |= UPS_HIGH_SPEED;
926 status |= UPS_PORT_POWER; /* XXX */
927 if (sc->sc_isreset)
928 change |= UPS_C_PORT_RESET;
929 USETW(ps.wPortStatus, status);
930 USETW(ps.wPortChange, change);
931 totlen = uimin(len, sizeof(ps));
932 memcpy(buf, &ps, totlen);
933 break;
934 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
935 return -1;
936 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
937 break;
938 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
939 if (index != 1) {
940 return -1;
941 }
942 switch(value) {
943 case UHF_PORT_ENABLE:
944 sc->sc_port_enabled = 1;
945 break;
946 case UHF_PORT_SUSPEND:
947 if (sc->sc_port_suspended == 0) {
948 val = UREAD1(sc, MUSB2_REG_POWER);
949 val |= MUSB2_MASK_SUSPMODE;
950 UWRITE1(sc, MUSB2_REG_POWER, val);
951 /* wait 20 milliseconds */
952 usb_delay_ms(&sc->sc_bus, 20);
953 sc->sc_port_suspended = 1;
954 sc->sc_port_suspended_change = 1;
955 }
956 break;
957 case UHF_PORT_RESET:
958 err = motg_portreset(sc);
959 if (err != USBD_NORMAL_COMPLETION)
960 return -1;
961 return 0;
962 case UHF_PORT_POWER:
963 /* XXX todo */
964 return 0;
965 case UHF_C_PORT_CONNECTION:
966 case UHF_C_PORT_ENABLE:
967 case UHF_C_PORT_OVER_CURRENT:
968 case UHF_PORT_CONNECTION:
969 case UHF_PORT_OVER_CURRENT:
970 case UHF_PORT_LOW_SPEED:
971 case UHF_C_PORT_SUSPEND:
972 case UHF_C_PORT_RESET:
973 default:
974 return -1;
975 }
976 break;
977 default:
978 /* default from usbroothub */
979 return buflen;
980 }
981
982 return totlen;
983 }
984
985 /* Abort a root interrupt request. */
986 void
987 motg_root_intr_abort(struct usbd_xfer *xfer)
988 {
989 struct motg_softc *sc = MOTG_XFER2SC(xfer);
990
991 KASSERT(mutex_owned(&sc->sc_lock));
992 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
993
994 /* If xfer has already completed, nothing to do here. */
995 if (sc->sc_intr_xfer == NULL)
996 return;
997
998 /*
999 * Otherwise, sc->sc_intr_xfer had better be this transfer.
1000 * Cancel it.
1001 */
1002 KASSERT(sc->sc_intr_xfer == xfer);
1003 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
1004 xfer->ux_status = USBD_CANCELLED;
1005 usb_transfer_complete(xfer);
1006 }
1007
1008 usbd_status
1009 motg_root_intr_transfer(struct usbd_xfer *xfer)
1010 {
1011 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1012 usbd_status err;
1013
1014 /* Insert last in queue. */
1015 mutex_enter(&sc->sc_lock);
1016 err = usb_insert_transfer(xfer);
1017 mutex_exit(&sc->sc_lock);
1018 if (err)
1019 return err;
1020
1021 /*
1022 * Pipe isn't running (otherwise err would be USBD_INPROG),
1023 * start first
1024 */
1025 return motg_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1026 }
1027
1028 /* Start a transfer on the root interrupt pipe */
1029 usbd_status
1030 motg_root_intr_start(struct usbd_xfer *xfer)
1031 {
1032 struct usbd_pipe *pipe = xfer->ux_pipe;
1033 struct motg_softc *sc = MOTG_PIPE2SC(pipe);
1034 const bool polling = sc->sc_bus.ub_usepolling;
1035
1036 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1037
1038 DPRINTFN(MD_ROOT, "xfer=%#jx len=%jd flags=%jd", (uintptr_t)xfer,
1039 xfer->ux_length, xfer->ux_flags, 0);
1040
1041 if (sc->sc_dying)
1042 return USBD_IOERROR;
1043
1044 if (!polling)
1045 mutex_enter(&sc->sc_lock);
1046 KASSERT(sc->sc_intr_xfer == NULL);
1047 sc->sc_intr_xfer = xfer;
1048 xfer->ux_status = USBD_IN_PROGRESS;
1049 if (!polling)
1050 mutex_exit(&sc->sc_lock);
1051
1052 return USBD_IN_PROGRESS;
1053 }
1054
1055 /* Close the root interrupt pipe. */
1056 void
1057 motg_root_intr_close(struct usbd_pipe *pipe)
1058 {
1059 struct motg_softc *sc __diagused = MOTG_PIPE2SC(pipe);
1060 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1061
1062 KASSERT(mutex_owned(&sc->sc_lock));
1063
1064 /*
1065 * Caller must guarantee the xfer has completed first, by
1066 * closing the pipe only after normal completion or an abort.
1067 */
1068 KASSERT(sc->sc_intr_xfer == NULL);
1069 }
1070
1071 void
1072 motg_root_intr_done(struct usbd_xfer *xfer)
1073 {
1074 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1075 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1076
1077 KASSERT(mutex_owned(&sc->sc_lock));
1078
1079 /* Claim the xfer so it doesn't get completed again. */
1080 KASSERT(sc->sc_intr_xfer == xfer);
1081 KASSERT(xfer->ux_status != USBD_IN_PROGRESS);
1082 sc->sc_intr_xfer = NULL;
1083 }
1084
1085 void
1086 motg_noop(struct usbd_pipe *pipe)
1087 {
1088 }
1089
1090 static usbd_status
1091 motg_portreset(struct motg_softc *sc)
1092 {
1093 uint32_t val;
1094 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1095
1096 val = UREAD1(sc, MUSB2_REG_POWER);
1097 val |= MUSB2_MASK_RESET;
1098 UWRITE1(sc, MUSB2_REG_POWER, val);
1099 /* Wait for 20 msec */
1100 usb_delay_ms(&sc->sc_bus, 20);
1101
1102 val = UREAD1(sc, MUSB2_REG_POWER);
1103 val &= ~MUSB2_MASK_RESET;
1104 UWRITE1(sc, MUSB2_REG_POWER, val);
1105
1106 /* determine line speed */
1107 val = UREAD1(sc, MUSB2_REG_POWER);
1108 if (val & MUSB2_MASK_HSMODE)
1109 sc->sc_high_speed = 1;
1110 else
1111 sc->sc_high_speed = 0;
1112 DPRINTFN(MD_ROOT | MD_CTRL, "speed %jd", sc->sc_high_speed, 0, 0, 0);
1113
1114 sc->sc_isreset = 1;
1115 sc->sc_port_enabled = 1;
1116 return USBD_NORMAL_COMPLETION;
1117 }
1118
1119 /*
1120 * This routine is executed when an interrupt on the root hub is detected
1121 */
1122 static void
1123 motg_hub_change(struct motg_softc *sc)
1124 {
1125 struct usbd_xfer *xfer = sc->sc_intr_xfer;
1126 struct usbd_pipe *pipe;
1127 u_char *p;
1128
1129 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1130
1131 if (xfer == NULL)
1132 return; /* the interrupt pipe is not open */
1133 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
1134
1135 pipe = xfer->ux_pipe;
1136 if (pipe->up_dev == NULL || pipe->up_dev->ud_bus == NULL)
1137 return; /* device has detached */
1138
1139 p = xfer->ux_buf;
1140 p[0] = 1<<1;
1141 xfer->ux_actlen = 1;
1142 xfer->ux_status = USBD_NORMAL_COMPLETION;
1143 usb_transfer_complete(xfer);
1144 }
1145
1146 static uint8_t
1147 motg_speed(uint8_t speed)
1148 {
1149 switch(speed) {
1150 case USB_SPEED_LOW:
1151 return MUSB2_MASK_TI_SPEED_LO;
1152 case USB_SPEED_FULL:
1153 return MUSB2_MASK_TI_SPEED_FS;
1154 case USB_SPEED_HIGH:
1155 return MUSB2_MASK_TI_SPEED_HS;
1156 default:
1157 panic("motg: unknown speed %d", speed);
1158 /* NOTREACHED */
1159 }
1160 }
1161
1162 static uint8_t
1163 motg_type(uint8_t type)
1164 {
1165 switch(type) {
1166 case UE_CONTROL:
1167 return MUSB2_MASK_TI_PROTO_CTRL;
1168 case UE_ISOCHRONOUS:
1169 return MUSB2_MASK_TI_PROTO_ISOC;
1170 case UE_BULK:
1171 return MUSB2_MASK_TI_PROTO_BULK;
1172 case UE_INTERRUPT:
1173 return MUSB2_MASK_TI_PROTO_INTR;
1174 default:
1175 panic("motg: unknown type %d", type);
1176 /* NOTREACHED */
1177 }
1178 }
1179
1180 static void
1181 motg_setup_endpoint_tx(struct usbd_xfer *xfer)
1182 {
1183 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1184 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe);
1185 struct usbd_device *dev = otgpipe->pipe.up_dev;
1186 int epnumber = otgpipe->hw_ep->ep_number;
1187
1188 UWRITE1(sc, MUSB2_REG_TXFADDR(epnumber), dev->ud_addr);
1189 if (dev->ud_myhsport) {
1190 UWRITE1(sc, MUSB2_REG_TXHADDR(epnumber),
1191 dev->ud_myhsport->up_parent->ud_addr);
1192 UWRITE1(sc, MUSB2_REG_TXHUBPORT(epnumber),
1193 dev->ud_myhsport->up_portno);
1194 } else {
1195 UWRITE1(sc, MUSB2_REG_TXHADDR(epnumber), 0);
1196 UWRITE1(sc, MUSB2_REG_TXHUBPORT(epnumber), 0);
1197 }
1198 UWRITE1(sc, MUSB2_REG_TXTI,
1199 motg_speed(dev->ud_speed) |
1200 UE_GET_ADDR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) |
1201 motg_type(UE_GET_XFERTYPE(xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes))
1202 );
1203 if (epnumber == 0) {
1204 if (sc->sc_high_speed) {
1205 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT,
1206 NAK_TO_CTRL_HIGH);
1207 } else {
1208 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, NAK_TO_CTRL);
1209 }
1210 } else {
1211 if ((xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE)
1212 == UE_BULK) {
1213 if (sc->sc_high_speed) {
1214 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT,
1215 NAK_TO_BULK_HIGH);
1216 } else {
1217 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, NAK_TO_BULK);
1218 }
1219 } else {
1220 if (sc->sc_high_speed) {
1221 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, POLL_TO_HIGH);
1222 } else {
1223 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, POLL_TO);
1224 }
1225 }
1226 }
1227 }
1228
1229 static void
1230 motg_setup_endpoint_rx(struct usbd_xfer *xfer)
1231 {
1232 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1233 struct usbd_device *dev = xfer->ux_pipe->up_dev;
1234 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe);
1235 int epnumber = otgpipe->hw_ep->ep_number;
1236
1237 UWRITE1(sc, MUSB2_REG_RXFADDR(epnumber), dev->ud_addr);
1238 if (dev->ud_myhsport) {
1239 UWRITE1(sc, MUSB2_REG_RXHADDR(epnumber),
1240 dev->ud_myhsport->up_parent->ud_addr);
1241 UWRITE1(sc, MUSB2_REG_RXHUBPORT(epnumber),
1242 dev->ud_myhsport->up_portno);
1243 } else {
1244 UWRITE1(sc, MUSB2_REG_RXHADDR(epnumber), 0);
1245 UWRITE1(sc, MUSB2_REG_RXHUBPORT(epnumber), 0);
1246 }
1247 UWRITE1(sc, MUSB2_REG_RXTI,
1248 motg_speed(dev->ud_speed) |
1249 UE_GET_ADDR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) |
1250 motg_type(UE_GET_XFERTYPE(xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes))
1251 );
1252 if (epnumber == 0) {
1253 if (sc->sc_high_speed) {
1254 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT,
1255 NAK_TO_CTRL_HIGH);
1256 } else {
1257 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, NAK_TO_CTRL);
1258 }
1259 } else {
1260 if ((xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE)
1261 == UE_BULK) {
1262 if (sc->sc_high_speed) {
1263 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT,
1264 NAK_TO_BULK_HIGH);
1265 } else {
1266 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, NAK_TO_BULK);
1267 }
1268 } else {
1269 if (sc->sc_high_speed) {
1270 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, POLL_TO_HIGH);
1271 } else {
1272 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, POLL_TO);
1273 }
1274 }
1275 }
1276 }
1277
1278 static usbd_status
1279 motg_device_ctrl_transfer(struct usbd_xfer *xfer)
1280 {
1281 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1282 usbd_status err;
1283
1284 /* Insert last in queue. */
1285 mutex_enter(&sc->sc_lock);
1286 err = usb_insert_transfer(xfer);
1287 xfer->ux_status = USBD_NOT_STARTED;
1288 mutex_exit(&sc->sc_lock);
1289 if (err)
1290 return err;
1291
1292 /*
1293 * Pipe isn't running (otherwise err would be USBD_INPROG),
1294 * so start it first.
1295 */
1296 return motg_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1297 }
1298
1299 static usbd_status
1300 motg_device_ctrl_start(struct usbd_xfer *xfer)
1301 {
1302 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1303 usbd_status err;
1304 mutex_enter(&sc->sc_lock);
1305 err = motg_device_ctrl_start1(sc);
1306 mutex_exit(&sc->sc_lock);
1307 if (err != USBD_IN_PROGRESS)
1308 return err;
1309 return USBD_IN_PROGRESS;
1310 }
1311
1312 static usbd_status
1313 motg_device_ctrl_start1(struct motg_softc *sc)
1314 {
1315 struct motg_hw_ep *ep = &sc->sc_in_ep[0];
1316 struct usbd_xfer *xfer = NULL;
1317 struct motg_pipe *otgpipe;
1318 usbd_status err = 0;
1319
1320 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1321
1322 KASSERT(mutex_owned(&sc->sc_lock));
1323 if (sc->sc_dying)
1324 return USBD_IOERROR;
1325
1326 if (!sc->sc_connected)
1327 return USBD_IOERROR;
1328
1329 if (ep->xfer != NULL) {
1330 err = USBD_IN_PROGRESS;
1331 goto end;
1332 }
1333 /* locate the first pipe with work to do */
1334 SIMPLEQ_FOREACH(otgpipe, &ep->ep_pipes, ep_pipe_list) {
1335 xfer = SIMPLEQ_FIRST(&otgpipe->pipe.up_queue);
1336 DPRINTFN(MD_CTRL, "pipe %#jx xfer %#jx status %jd",
1337 (uintptr_t)otgpipe, (uintptr_t)xfer,
1338 (xfer != NULL) ? xfer->ux_status : 0, 0);
1339
1340 if (xfer != NULL) {
1341 /* move this pipe to the end of the list */
1342 SIMPLEQ_REMOVE(&ep->ep_pipes, otgpipe,
1343 motg_pipe, ep_pipe_list);
1344 SIMPLEQ_INSERT_TAIL(&ep->ep_pipes,
1345 otgpipe, ep_pipe_list);
1346 break;
1347 }
1348 }
1349 if (xfer == NULL) {
1350 err = USBD_NOT_STARTED;
1351 goto end;
1352 }
1353 usbd_xfer_schedule_timeout(xfer);
1354 xfer->ux_status = USBD_IN_PROGRESS;
1355 KASSERT(otgpipe == MOTG_PIPE2MPIPE(xfer->ux_pipe));
1356 KASSERT(otgpipe->hw_ep == ep);
1357 KASSERT(xfer->ux_rqflags & URQ_REQUEST);
1358 // KASSERT(xfer->ux_actlen == 0);
1359 xfer->ux_actlen = 0;
1360
1361 ep->xfer = xfer;
1362 ep->datalen = xfer->ux_length;
1363 if (ep->datalen > 0)
1364 ep->data = xfer->ux_buf;
1365 else
1366 ep->data = NULL;
1367 if ((xfer->ux_flags & USBD_FORCE_SHORT_XFER) &&
1368 (ep->datalen % 64) == 0)
1369 ep->need_short_xfer = 1;
1370 else
1371 ep->need_short_xfer = 0;
1372 /* now we need send this request */
1373 DPRINTFN(MD_CTRL,
1374 "xfer %#jx send data %#jx len %jd short %jd",
1375 (uintptr_t)xfer, (uintptr_t)ep->data, ep->datalen,
1376 ep->need_short_xfer);
1377 DPRINTFN(MD_CTRL,
1378 "xfer %#jx ... speed %jd to %jd", (uintptr_t)xfer,
1379 xfer->ux_pipe->up_dev->ud_speed,
1380 xfer->ux_pipe->up_dev->ud_addr, 0);
1381 KASSERT(ep->phase == IDLE);
1382 ep->phase = SETUP;
1383 /* select endpoint 0 */
1384 UWRITE1(sc, MUSB2_REG_EPINDEX, 0);
1385 /* fifo should be empty at this point */
1386 KASSERT((UREAD1(sc, MUSB2_REG_TXCSRL) & MUSB2_MASK_CSR0L_TXPKTRDY) == 0);
1387 /* send data */
1388 // KASSERT(((vaddr_t)(&xfer->ux_request) & 3) == 0);
1389 KASSERT(sizeof(xfer->ux_request) == 8);
1390 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_EPFIFO(0),
1391 (void *)&xfer->ux_request, sizeof(xfer->ux_request));
1392
1393 motg_setup_endpoint_tx(xfer);
1394 /* start transaction */
1395 UWRITE1(sc, MUSB2_REG_TXCSRL,
1396 MUSB2_MASK_CSR0L_TXPKTRDY | MUSB2_MASK_CSR0L_SETUPPKT);
1397
1398 end:
1399 if (err)
1400 return err;
1401
1402 return USBD_IN_PROGRESS;
1403 }
1404
1405 static void
1406 motg_device_ctrl_read(struct usbd_xfer *xfer)
1407 {
1408 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1409 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe);
1410 /* assume endpoint already selected */
1411 motg_setup_endpoint_rx(xfer);
1412 /* start transaction */
1413 UWRITE1(sc, MUSB2_REG_TXCSRL, MUSB2_MASK_CSR0L_REQPKT);
1414 otgpipe->hw_ep->phase = DATA_IN;
1415 }
1416
1417 static void
1418 motg_device_ctrl_intr_rx(struct motg_softc *sc)
1419 {
1420 struct motg_hw_ep *ep = &sc->sc_in_ep[0];
1421 struct usbd_xfer *xfer = ep->xfer;
1422 uint8_t csr;
1423 int datalen, max_datalen;
1424 char *data;
1425 bool got_short;
1426 usbd_status new_status = USBD_IN_PROGRESS;
1427
1428 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1429
1430 KASSERT(mutex_owned(&sc->sc_lock));
1431 KASSERT(ep->phase == DATA_IN || ep->phase == STATUS_IN);
1432
1433 /* select endpoint 0 */
1434 UWRITE1(sc, MUSB2_REG_EPINDEX, 0);
1435
1436 /* read out FIFO status */
1437 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
1438 DPRINTFN(MD_CTRL, "phase %jd csr 0x%jx xfer %#jx status %jd",
1439 ep->phase, csr, (uintptr_t)xfer,
1440 (xfer != NULL) ? xfer->ux_status : 0);
1441
1442 if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
1443 csr &= ~MUSB2_MASK_CSR0L_REQPKT;
1444 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
1445
1446 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
1447 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
1448 new_status = USBD_TIMEOUT; /* XXX */
1449 goto complete;
1450 }
1451 if (csr & (MUSB2_MASK_CSR0L_RXSTALL | MUSB2_MASK_CSR0L_ERROR)) {
1452 if (csr & MUSB2_MASK_CSR0L_RXSTALL)
1453 new_status = USBD_STALLED;
1454 else
1455 new_status = USBD_IOERROR;
1456 /* clear status */
1457 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1458 goto complete;
1459 }
1460 if ((csr & MUSB2_MASK_CSR0L_RXPKTRDY) == 0)
1461 return; /* no data yet */
1462
1463 if (xfer == NULL || xfer->ux_status != USBD_IN_PROGRESS)
1464 goto complete;
1465
1466 if (ep->phase == STATUS_IN) {
1467 new_status = USBD_NORMAL_COMPLETION;
1468 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1469 goto complete;
1470 }
1471 datalen = UREAD2(sc, MUSB2_REG_RXCOUNT);
1472 DPRINTFN(MD_CTRL, "phase %jd datalen %jd", ep->phase, datalen, 0, 0);
1473 KASSERT(UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize) > 0);
1474 max_datalen = uimin(UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize),
1475 ep->datalen);
1476 if (datalen > max_datalen) {
1477 new_status = USBD_IOERROR;
1478 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1479 goto complete;
1480 }
1481 got_short = (datalen < max_datalen);
1482 if (datalen > 0) {
1483 KASSERT(ep->phase == DATA_IN);
1484 data = ep->data;
1485 ep->data += datalen;
1486 ep->datalen -= datalen;
1487 xfer->ux_actlen += datalen;
1488 if (((vaddr_t)data & 0x3) == 0 &&
1489 (datalen >> 2) > 0) {
1490 DPRINTFN(MD_CTRL, "r4 data %#jx len %jd",
1491 (uintptr_t)data, datalen, 0, 0);
1492 bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
1493 MUSB2_REG_EPFIFO(0), (void *)data, datalen >> 2);
1494 data += (datalen & ~0x3);
1495 datalen -= (datalen & ~0x3);
1496 }
1497 DPRINTFN(MD_CTRL, "r1 data %#jx len %jd", (uintptr_t)data,
1498 datalen, 0, 0);
1499 if (datalen) {
1500 bus_space_read_multi_1(sc->sc_iot, sc->sc_ioh,
1501 MUSB2_REG_EPFIFO(0), data, datalen);
1502 }
1503 }
1504 UWRITE1(sc, MUSB2_REG_TXCSRL, csr & ~MUSB2_MASK_CSR0L_RXPKTRDY);
1505 KASSERT(ep->phase == DATA_IN);
1506 if (got_short || (ep->datalen == 0)) {
1507 if (ep->need_short_xfer == 0) {
1508 ep->phase = STATUS_OUT;
1509 UWRITE1(sc, MUSB2_REG_TXCSRH,
1510 UREAD1(sc, MUSB2_REG_TXCSRH) |
1511 MUSB2_MASK_CSR0H_PING_DIS);
1512 motg_setup_endpoint_tx(xfer);
1513 UWRITE1(sc, MUSB2_REG_TXCSRL,
1514 MUSB2_MASK_CSR0L_STATUSPKT |
1515 MUSB2_MASK_CSR0L_TXPKTRDY);
1516 return;
1517 }
1518 ep->need_short_xfer = 0;
1519 }
1520 motg_device_ctrl_read(xfer);
1521 return;
1522 complete:
1523 ep->phase = IDLE;
1524 ep->xfer = NULL;
1525 /*
1526 * Try to claim this xfer for completion. If it has already
1527 * completed or aborted, drop it on the floor.
1528 */
1529 if (xfer && usbd_xfer_trycomplete(xfer)) {
1530 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
1531 KASSERT(new_status != USBD_IN_PROGRESS);
1532 xfer->ux_status = new_status;
1533 usb_transfer_complete(xfer);
1534 }
1535 motg_device_ctrl_start1(sc);
1536 }
1537
1538 static void
1539 motg_device_ctrl_intr_tx(struct motg_softc *sc)
1540 {
1541 struct motg_hw_ep *ep = &sc->sc_in_ep[0];
1542 struct usbd_xfer *xfer = ep->xfer;
1543 uint8_t csr;
1544 int datalen;
1545 char *data;
1546 usbd_status new_status = USBD_IN_PROGRESS;
1547
1548 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1549
1550 KASSERT(mutex_owned(&sc->sc_lock));
1551
1552 if (ep->phase == DATA_IN || ep->phase == STATUS_IN) {
1553 motg_device_ctrl_intr_rx(sc);
1554 return;
1555 }
1556
1557 KASSERT(ep->phase == SETUP || ep->phase == DATA_OUT ||
1558 ep->phase == STATUS_OUT);
1559
1560 /* select endpoint 0 */
1561 UWRITE1(sc, MUSB2_REG_EPINDEX, 0);
1562
1563 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
1564 DPRINTFN(MD_CTRL, "phase %jd csr 0x%jx xfer %#jx status %jd",
1565 ep->phase, csr, (uintptr_t)xfer,
1566 (xfer != NULL) ? xfer->ux_status : 0);
1567
1568 if (csr & MUSB2_MASK_CSR0L_RXSTALL) {
1569 /* command not accepted */
1570 new_status = USBD_STALLED;
1571 /* clear status */
1572 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1573 goto complete;
1574 }
1575 if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
1576 new_status = USBD_TIMEOUT; /* XXX */
1577 /* flush fifo */
1578 while (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
1579 UWRITE1(sc, MUSB2_REG_TXCSRH,
1580 UREAD1(sc, MUSB2_REG_TXCSRH) |
1581 MUSB2_MASK_CSR0H_FFLUSH);
1582 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
1583 }
1584 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
1585 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
1586 goto complete;
1587 }
1588 if (csr & MUSB2_MASK_CSR0L_ERROR) {
1589 new_status = USBD_IOERROR;
1590 /* clear status */
1591 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1592 goto complete;
1593 }
1594 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
1595 /* data still not sent */
1596 return;
1597 }
1598 if (xfer == NULL || xfer->ux_status != USBD_IN_PROGRESS)
1599 goto complete;
1600 if (ep->phase == STATUS_OUT) {
1601 /*
1602 * we have sent status and got no error;
1603 * declare transfer complete
1604 */
1605 DPRINTFN(MD_CTRL, "xfer %#jx status %jd complete",
1606 (uintptr_t)xfer, xfer->ux_status, 0, 0);
1607 new_status = USBD_NORMAL_COMPLETION;
1608 goto complete;
1609 }
1610 if (ep->datalen == 0) {
1611 if (ep->need_short_xfer) {
1612 ep->need_short_xfer = 0;
1613 /* one more data phase */
1614 if (xfer->ux_request.bmRequestType & UT_READ) {
1615 DPRINTFN(MD_CTRL, "xfer %#jx to DATA_IN",
1616 (uintptr_t)xfer, 0, 0, 0);
1617 motg_device_ctrl_read(xfer);
1618 return;
1619 } /* else fall back to DATA_OUT */
1620 } else {
1621 DPRINTFN(MD_CTRL, "xfer %#jx to STATUS_IN, csrh 0x%jx",
1622 (uintptr_t)xfer, UREAD1(sc, MUSB2_REG_TXCSRH),
1623 0, 0);
1624 ep->phase = STATUS_IN;
1625 UWRITE1(sc, MUSB2_REG_RXCSRH,
1626 UREAD1(sc, MUSB2_REG_RXCSRH) |
1627 MUSB2_MASK_CSR0H_PING_DIS);
1628 motg_setup_endpoint_rx(xfer);
1629 UWRITE1(sc, MUSB2_REG_TXCSRL,
1630 MUSB2_MASK_CSR0L_STATUSPKT |
1631 MUSB2_MASK_CSR0L_REQPKT);
1632 return;
1633 }
1634 }
1635 if (xfer->ux_request.bmRequestType & UT_READ) {
1636 motg_device_ctrl_read(xfer);
1637 return;
1638 }
1639 /* setup a dataout phase */
1640 datalen = uimin(ep->datalen,
1641 UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize));
1642 ep->phase = DATA_OUT;
1643 DPRINTFN(MD_CTRL, "xfer %#jx to DATA_OUT, csrh 0x%jx", (uintptr_t)xfer,
1644 UREAD1(sc, MUSB2_REG_TXCSRH), 0, 0);
1645 if (datalen) {
1646 data = ep->data;
1647 ep->data += datalen;
1648 ep->datalen -= datalen;
1649 xfer->ux_actlen += datalen;
1650 if (((vaddr_t)data & 0x3) == 0 &&
1651 (datalen >> 2) > 0) {
1652 bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
1653 MUSB2_REG_EPFIFO(0), (void *)data, datalen >> 2);
1654 data += (datalen & ~0x3);
1655 datalen -= (datalen & ~0x3);
1656 }
1657 if (datalen) {
1658 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh,
1659 MUSB2_REG_EPFIFO(0), data, datalen);
1660 }
1661 }
1662 /* send data */
1663 motg_setup_endpoint_tx(xfer);
1664 UWRITE1(sc, MUSB2_REG_TXCSRL, MUSB2_MASK_CSR0L_TXPKTRDY);
1665 return;
1666
1667 complete:
1668 ep->phase = IDLE;
1669 ep->xfer = NULL;
1670 /*
1671 * Try to claim this xfer for completion. If it has already
1672 * completed or aborted, drop it on the floor.
1673 */
1674 if (xfer && usbd_xfer_trycomplete(xfer)) {
1675 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
1676 KASSERT(new_status != USBD_IN_PROGRESS);
1677 xfer->ux_status = new_status;
1678 usb_transfer_complete(xfer);
1679 }
1680 motg_device_ctrl_start1(sc);
1681 }
1682
1683 /* Abort a device control request. */
1684 void
1685 motg_device_ctrl_abort(struct usbd_xfer *xfer)
1686 {
1687 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1688
1689 usbd_xfer_abort(xfer);
1690 }
1691
1692 /* Close a device control pipe */
1693 void
1694 motg_device_ctrl_close(struct usbd_pipe *pipe)
1695 {
1696 struct motg_softc *sc __diagused = MOTG_PIPE2SC(pipe);
1697 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe);
1698 struct motg_pipe *otgpipeiter;
1699
1700 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1701
1702 KASSERT(mutex_owned(&sc->sc_lock));
1703 KASSERT(otgpipe->hw_ep->xfer == NULL ||
1704 otgpipe->hw_ep->xfer->ux_pipe != pipe);
1705
1706 SIMPLEQ_FOREACH(otgpipeiter, &otgpipe->hw_ep->ep_pipes, ep_pipe_list) {
1707 if (otgpipeiter == otgpipe) {
1708 /* remove from list */
1709 SIMPLEQ_REMOVE(&otgpipe->hw_ep->ep_pipes, otgpipe,
1710 motg_pipe, ep_pipe_list);
1711 otgpipe->hw_ep->refcount--;
1712 /* we're done */
1713 return;
1714 }
1715 }
1716 panic("motg_device_ctrl_close: not found");
1717 }
1718
1719 void
1720 motg_device_ctrl_done(struct usbd_xfer *xfer)
1721 {
1722 struct motg_pipe *otgpipe __diagused = MOTG_PIPE2MPIPE(xfer->ux_pipe);
1723 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1724
1725 KASSERT(otgpipe->hw_ep->xfer != xfer);
1726 }
1727
1728 static usbd_status
1729 motg_device_data_transfer(struct usbd_xfer *xfer)
1730 {
1731 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1732 usbd_status err;
1733
1734 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1735
1736 /* Insert last in queue. */
1737 mutex_enter(&sc->sc_lock);
1738 DPRINTF("xfer %#jx status %jd", (uintptr_t)xfer, xfer->ux_status, 0, 0);
1739 err = usb_insert_transfer(xfer);
1740 xfer->ux_status = USBD_NOT_STARTED;
1741 mutex_exit(&sc->sc_lock);
1742 if (err)
1743 return err;
1744
1745 /*
1746 * Pipe isn't running (otherwise err would be USBD_INPROG),
1747 * so start it first.
1748 */
1749 return motg_device_data_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
1750 }
1751
1752 static usbd_status
1753 motg_device_data_start(struct usbd_xfer *xfer)
1754 {
1755 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1756 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe);
1757 usbd_status err;
1758
1759 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1760
1761 mutex_enter(&sc->sc_lock);
1762 DPRINTF("xfer %#jx status %jd", (uintptr_t)xfer, xfer->ux_status, 0, 0);
1763 err = motg_device_data_start1(sc, otgpipe->hw_ep);
1764 mutex_exit(&sc->sc_lock);
1765 if (err != USBD_IN_PROGRESS)
1766 return err;
1767 return USBD_IN_PROGRESS;
1768 }
1769
1770 static usbd_status
1771 motg_device_data_start1(struct motg_softc *sc, struct motg_hw_ep *ep)
1772 {
1773 struct usbd_xfer *xfer = NULL;
1774 struct motg_pipe *otgpipe;
1775 usbd_status err = 0;
1776 uint32_t val __diagused;
1777
1778 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1779
1780 KASSERT(mutex_owned(&sc->sc_lock));
1781 if (sc->sc_dying)
1782 return USBD_IOERROR;
1783
1784 if (!sc->sc_connected)
1785 return USBD_IOERROR;
1786
1787 if (ep->xfer != NULL) {
1788 err = USBD_IN_PROGRESS;
1789 goto end;
1790 }
1791 /* locate the first pipe with work to do */
1792 SIMPLEQ_FOREACH(otgpipe, &ep->ep_pipes, ep_pipe_list) {
1793 xfer = SIMPLEQ_FIRST(&otgpipe->pipe.up_queue);
1794 DPRINTFN(MD_BULK, "pipe %#jx xfer %#jx status %jd",
1795 (uintptr_t)otgpipe, (uintptr_t)xfer,
1796 (xfer != NULL) ? xfer->ux_status : 0, 0);
1797 if (xfer != NULL) {
1798 /* move this pipe to the end of the list */
1799 SIMPLEQ_REMOVE(&ep->ep_pipes, otgpipe,
1800 motg_pipe, ep_pipe_list);
1801 SIMPLEQ_INSERT_TAIL(&ep->ep_pipes,
1802 otgpipe, ep_pipe_list);
1803 break;
1804 }
1805 }
1806 if (xfer == NULL) {
1807 err = USBD_NOT_STARTED;
1808 goto end;
1809 }
1810 usbd_xfer_schedule_timeout(xfer);
1811 xfer->ux_status = USBD_IN_PROGRESS;
1812 KASSERT(otgpipe == MOTG_PIPE2MPIPE(xfer->ux_pipe));
1813 KASSERT(otgpipe->hw_ep == ep);
1814 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
1815 // KASSERT(xfer->ux_actlen == 0);
1816 xfer->ux_actlen = 0;
1817
1818 ep->xfer = xfer;
1819 ep->datalen = xfer->ux_length;
1820 KASSERT(ep->datalen > 0);
1821 ep->data = xfer->ux_buf;
1822 if ((xfer->ux_flags & USBD_FORCE_SHORT_XFER) &&
1823 (ep->datalen % 64) == 0)
1824 ep->need_short_xfer = 1;
1825 else
1826 ep->need_short_xfer = 0;
1827 /* now we need send this request */
1828 DPRINTFN(MD_BULK,
1829 UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN ?
1830 "xfer %#jx in data %#jx len %jd short %jd" :
1831 "xfer %#jx out data %#jx len %jd short %jd",
1832 (uintptr_t)xfer, (uintptr_t)ep->data, ep->datalen,
1833 ep->need_short_xfer);
1834 DPRINTFN(MD_BULK, "... speed %jd to %jd",
1835 xfer->ux_pipe->up_dev->ud_speed,
1836 xfer->ux_pipe->up_dev->ud_addr, 0, 0);
1837 KASSERT(ep->phase == IDLE);
1838 /* select endpoint */
1839 UWRITE1(sc, MUSB2_REG_EPINDEX, ep->ep_number);
1840 if (UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress)
1841 == UE_DIR_IN) {
1842 val = UREAD1(sc, MUSB2_REG_RXCSRL);
1843 KASSERT((val & MUSB2_MASK_CSRL_RXPKTRDY) == 0);
1844 motg_device_data_read(xfer);
1845 } else {
1846 ep->phase = DATA_OUT;
1847 val = UREAD1(sc, MUSB2_REG_TXCSRL);
1848 KASSERT((val & MUSB2_MASK_CSRL_TXPKTRDY) == 0);
1849 motg_device_data_write(xfer);
1850 }
1851 end:
1852 if (err)
1853 return err;
1854
1855 return USBD_IN_PROGRESS;
1856 }
1857
1858 static void
1859 motg_device_data_read(struct usbd_xfer *xfer)
1860 {
1861 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1862 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe);
1863 uint32_t val;
1864
1865 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1866
1867 KASSERT(mutex_owned(&sc->sc_lock));
1868 /* assume endpoint already selected */
1869 motg_setup_endpoint_rx(xfer);
1870 /* Max packet size */
1871 UWRITE2(sc, MUSB2_REG_RXMAXP,
1872 UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize));
1873 /* Data Toggle */
1874 val = UREAD1(sc, MUSB2_REG_RXCSRH);
1875 val |= MUSB2_MASK_CSRH_RXDT_WREN;
1876 if (otgpipe->nexttoggle)
1877 val |= MUSB2_MASK_CSRH_RXDT_VAL;
1878 else
1879 val &= ~MUSB2_MASK_CSRH_RXDT_VAL;
1880 UWRITE1(sc, MUSB2_REG_RXCSRH, val);
1881
1882 DPRINTFN(MD_BULK, "%#jx to DATA_IN on ep %jd, csrh 0x%jx",
1883 (uintptr_t)xfer, otgpipe->hw_ep->ep_number,
1884 UREAD1(sc, MUSB2_REG_RXCSRH), 0);
1885 /* start transaction */
1886 UWRITE1(sc, MUSB2_REG_RXCSRL, MUSB2_MASK_CSRL_RXREQPKT);
1887 otgpipe->hw_ep->phase = DATA_IN;
1888 }
1889
1890 static void
1891 motg_device_data_write(struct usbd_xfer *xfer)
1892 {
1893 struct motg_softc *sc = MOTG_XFER2SC(xfer);
1894 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe);
1895 struct motg_hw_ep *ep = otgpipe->hw_ep;
1896 int datalen;
1897 char *data;
1898 uint32_t val;
1899
1900 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1901
1902 KASSERT(xfer!=NULL);
1903 KASSERT(mutex_owned(&sc->sc_lock));
1904
1905 datalen = uimin(ep->datalen,
1906 UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize));
1907 ep->phase = DATA_OUT;
1908 DPRINTFN(MD_BULK, "%#jx to DATA_OUT on ep %jd, len %jd csrh 0x%jx",
1909 (uintptr_t)xfer, ep->ep_number, datalen,
1910 UREAD1(sc, MUSB2_REG_TXCSRH));
1911
1912 /* assume endpoint already selected */
1913 /* write data to fifo */
1914 data = ep->data;
1915 ep->data += datalen;
1916 ep->datalen -= datalen;
1917 xfer->ux_actlen += datalen;
1918 if (((vaddr_t)data & 0x3) == 0 &&
1919 (datalen >> 2) > 0) {
1920 bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
1921 MUSB2_REG_EPFIFO(ep->ep_number),
1922 (void *)data, datalen >> 2);
1923 data += (datalen & ~0x3);
1924 datalen -= (datalen & ~0x3);
1925 }
1926 if (datalen) {
1927 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh,
1928 MUSB2_REG_EPFIFO(ep->ep_number), data, datalen);
1929 }
1930
1931 motg_setup_endpoint_tx(xfer);
1932 /* Max packet size */
1933 UWRITE2(sc, MUSB2_REG_TXMAXP,
1934 UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize));
1935 /* Data Toggle */
1936 val = UREAD1(sc, MUSB2_REG_TXCSRH);
1937 val |= MUSB2_MASK_CSRH_TXDT_WREN;
1938 if (otgpipe->nexttoggle)
1939 val |= MUSB2_MASK_CSRH_TXDT_VAL;
1940 else
1941 val &= ~MUSB2_MASK_CSRH_TXDT_VAL;
1942 UWRITE1(sc, MUSB2_REG_TXCSRH, val);
1943
1944 /* start transaction */
1945 UWRITE1(sc, MUSB2_REG_TXCSRL, MUSB2_MASK_CSRL_TXPKTRDY);
1946 }
1947
1948 static void
1949 motg_device_intr_rx(struct motg_softc *sc, int epnumber)
1950 {
1951 struct motg_hw_ep *ep = &sc->sc_in_ep[epnumber];
1952 struct usbd_xfer *xfer = ep->xfer;
1953 uint8_t csr;
1954 int datalen, max_datalen;
1955 char *data;
1956 bool got_short;
1957 usbd_status new_status = USBD_IN_PROGRESS;
1958
1959 MOTGHIST_FUNC(); MOTGHIST_CALLED();
1960
1961 KASSERT(mutex_owned(&sc->sc_lock));
1962 KASSERT(ep->ep_number == epnumber);
1963
1964 DPRINTFN(MD_BULK, "on ep %jd", epnumber, 0, 0, 0);
1965 /* select endpoint */
1966 UWRITE1(sc, MUSB2_REG_EPINDEX, epnumber);
1967
1968 /* read out FIFO status */
1969 csr = UREAD1(sc, MUSB2_REG_RXCSRL);
1970 DPRINTFN(MD_BULK, "phase %jd csr 0x%jx", ep->phase, csr ,0 ,0);
1971
1972 if ((csr & (MUSB2_MASK_CSRL_RXNAKTO | MUSB2_MASK_CSRL_RXSTALL |
1973 MUSB2_MASK_CSRL_RXERROR | MUSB2_MASK_CSRL_RXPKTRDY)) == 0)
1974 return;
1975
1976 KASSERTMSG(ep->phase == DATA_IN, "phase %d", ep->phase);
1977 if (csr & MUSB2_MASK_CSRL_RXNAKTO) {
1978 csr &= ~MUSB2_MASK_CSRL_RXREQPKT;
1979 UWRITE1(sc, MUSB2_REG_RXCSRL, csr);
1980
1981 csr &= ~MUSB2_MASK_CSRL_RXNAKTO;
1982 UWRITE1(sc, MUSB2_REG_RXCSRL, csr);
1983 new_status = USBD_TIMEOUT; /* XXX */
1984 goto complete;
1985 }
1986 if (csr & (MUSB2_MASK_CSRL_RXSTALL | MUSB2_MASK_CSRL_RXERROR)) {
1987 if (csr & MUSB2_MASK_CSRL_RXSTALL)
1988 new_status = USBD_STALLED;
1989 else
1990 new_status = USBD_IOERROR;
1991 /* clear status */
1992 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
1993 goto complete;
1994 }
1995 KASSERT(csr & MUSB2_MASK_CSRL_RXPKTRDY);
1996
1997 if (xfer == NULL || xfer->ux_status != USBD_IN_PROGRESS) {
1998 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
1999 goto complete;
2000 }
2001
2002 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe);
2003 otgpipe->nexttoggle = otgpipe->nexttoggle ^ 1;
2004
2005 datalen = UREAD2(sc, MUSB2_REG_RXCOUNT);
2006 DPRINTFN(MD_BULK, "phase %jd datalen %jd", ep->phase, datalen ,0 ,0);
2007 KASSERT(UE_GET_SIZE(UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize)) > 0);
2008 max_datalen = uimin(
2009 UE_GET_SIZE(UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize)),
2010 ep->datalen);
2011 if (datalen > max_datalen) {
2012 new_status = USBD_IOERROR;
2013 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
2014 goto complete;
2015 }
2016 got_short = (datalen < max_datalen);
2017 if (datalen > 0) {
2018 KASSERT(ep->phase == DATA_IN);
2019 data = ep->data;
2020 ep->data += datalen;
2021 ep->datalen -= datalen;
2022 xfer->ux_actlen += datalen;
2023 if (((vaddr_t)data & 0x3) == 0 &&
2024 (datalen >> 2) > 0) {
2025 DPRINTFN(MD_BULK, "r4 data %#jx len %jd",
2026 (uintptr_t)data, datalen, 0, 0);
2027 bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
2028 MUSB2_REG_EPFIFO(ep->ep_number),
2029 (void *)data, datalen >> 2);
2030 data += (datalen & ~0x3);
2031 datalen -= (datalen & ~0x3);
2032 }
2033 DPRINTFN(MD_BULK, "r1 data %#jx len %jd", (uintptr_t)data,
2034 datalen ,0 ,0);
2035 if (datalen) {
2036 bus_space_read_multi_1(sc->sc_iot, sc->sc_ioh,
2037 MUSB2_REG_EPFIFO(ep->ep_number), data, datalen);
2038 }
2039 }
2040 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
2041 KASSERT(ep->phase == DATA_IN);
2042 if (got_short || (ep->datalen == 0)) {
2043 if (ep->need_short_xfer == 0) {
2044 new_status = USBD_NORMAL_COMPLETION;
2045 goto complete;
2046 }
2047 ep->need_short_xfer = 0;
2048 }
2049 motg_device_data_read(xfer);
2050 return;
2051 complete:
2052 DPRINTFN(MD_BULK, "xfer %#jx complete, status %jd", (uintptr_t)xfer,
2053 (xfer != NULL) ? xfer->ux_status : 0, 0, 0);
2054 ep->phase = IDLE;
2055 ep->xfer = NULL;
2056 /*
2057 * Try to claim this xfer for completion. If it has already
2058 * completed or aborted, drop it on the floor.
2059 */
2060 if (xfer && usbd_xfer_trycomplete(xfer)) {
2061 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
2062 KASSERT(new_status != USBD_IN_PROGRESS);
2063 xfer->ux_status = new_status;
2064 usb_transfer_complete(xfer);
2065 }
2066 motg_device_data_start1(sc, ep);
2067 }
2068
2069 static void
2070 motg_device_intr_tx(struct motg_softc *sc, int epnumber)
2071 {
2072 struct motg_hw_ep *ep = &sc->sc_out_ep[epnumber];
2073 struct usbd_xfer *xfer = ep->xfer;
2074 uint8_t csr;
2075 struct motg_pipe *otgpipe;
2076 usbd_status new_status = USBD_IN_PROGRESS;
2077
2078 MOTGHIST_FUNC(); MOTGHIST_CALLED();
2079
2080 KASSERT(mutex_owned(&sc->sc_lock));
2081 KASSERT(ep->ep_number == epnumber);
2082
2083 DPRINTFN(MD_BULK, " on ep %jd", epnumber, 0, 0, 0);
2084 /* select endpoint */
2085 UWRITE1(sc, MUSB2_REG_EPINDEX, epnumber);
2086
2087 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
2088 DPRINTFN(MD_BULK, "phase %jd csr 0x%jx", ep->phase, csr, 0, 0);
2089
2090 if (csr & (MUSB2_MASK_CSRL_TXSTALLED|MUSB2_MASK_CSRL_TXERROR)) {
2091 /* command not accepted */
2092 if (csr & MUSB2_MASK_CSRL_TXSTALLED)
2093 new_status = USBD_STALLED;
2094 else
2095 new_status = USBD_IOERROR;
2096 /* clear status */
2097 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
2098 goto complete;
2099 }
2100 if (csr & MUSB2_MASK_CSRL_TXNAKTO) {
2101 new_status = USBD_TIMEOUT; /* XXX */
2102 csr &= ~MUSB2_MASK_CSRL_TXNAKTO;
2103 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
2104 /* flush fifo */
2105 while (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
2106 csr |= MUSB2_MASK_CSRL_TXFFLUSH;
2107 csr &= ~MUSB2_MASK_CSRL_TXNAKTO;
2108 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
2109 delay(1000);
2110 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
2111 DPRINTFN(MD_BULK, "TX fifo flush ep %jd CSR 0x%jx",
2112 epnumber, csr, 0, 0);
2113 }
2114 goto complete;
2115 }
2116 if (csr & (MUSB2_MASK_CSRL_TXFIFONEMPTY|MUSB2_MASK_CSRL_TXPKTRDY)) {
2117 /* data still not sent */
2118 return;
2119 }
2120 if (xfer == NULL || xfer->ux_status != USBD_IN_PROGRESS)
2121 goto complete;
2122 KASSERT(ep->phase == DATA_OUT);
2123
2124 otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe);
2125 otgpipe->nexttoggle = otgpipe->nexttoggle ^ 1;
2126
2127 if (ep->datalen == 0) {
2128 if (ep->need_short_xfer) {
2129 ep->need_short_xfer = 0;
2130 /* one more data phase */
2131 } else {
2132 new_status = USBD_NORMAL_COMPLETION;
2133 goto complete;
2134 }
2135 }
2136 motg_device_data_write(xfer);
2137 return;
2138
2139 complete:
2140 DPRINTFN(MD_BULK, "xfer %#jx complete, status %jd", (uintptr_t)xfer,
2141 (xfer != NULL) ? xfer->ux_status : 0, 0, 0);
2142 ep->phase = IDLE;
2143 ep->xfer = NULL;
2144 /*
2145 * Try to claim this xfer for completion. If it has already
2146 * completed or aborted, drop it on the floor.
2147 */
2148 if (xfer && usbd_xfer_trycomplete(xfer)) {
2149 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
2150 KASSERT(new_status != USBD_IN_PROGRESS);
2151 xfer->ux_status = new_status;
2152 usb_transfer_complete(xfer);
2153 }
2154 motg_device_data_start1(sc, ep);
2155 }
2156
2157 /* Abort a device control request. */
2158 void
2159 motg_device_data_abort(struct usbd_xfer *xfer)
2160 {
2161 struct motg_softc __diagused *sc = MOTG_XFER2SC(xfer);
2162 KASSERT(mutex_owned(&sc->sc_lock));
2163
2164 MOTGHIST_FUNC(); MOTGHIST_CALLED();
2165
2166 usbd_xfer_abort(xfer);
2167 }
2168
2169 /* Close a device control pipe */
2170 void
2171 motg_device_data_close(struct usbd_pipe *pipe)
2172 {
2173 struct motg_softc *sc __diagused = MOTG_PIPE2SC(pipe);
2174 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe);
2175 struct motg_pipe *otgpipeiter;
2176
2177 MOTGHIST_FUNC(); MOTGHIST_CALLED();
2178
2179 KASSERT(mutex_owned(&sc->sc_lock));
2180 KASSERT(otgpipe->hw_ep->xfer == NULL ||
2181 otgpipe->hw_ep->xfer->ux_pipe != pipe);
2182
2183 pipe->up_endpoint->ue_toggle = otgpipe->nexttoggle;
2184 SIMPLEQ_FOREACH(otgpipeiter, &otgpipe->hw_ep->ep_pipes, ep_pipe_list) {
2185 if (otgpipeiter == otgpipe) {
2186 /* remove from list */
2187 SIMPLEQ_REMOVE(&otgpipe->hw_ep->ep_pipes, otgpipe,
2188 motg_pipe, ep_pipe_list);
2189 otgpipe->hw_ep->refcount--;
2190 /* we're done */
2191 return;
2192 }
2193 }
2194 panic("motg_device_data_close: not found");
2195 }
2196
2197 void
2198 motg_device_data_done(struct usbd_xfer *xfer)
2199 {
2200 struct motg_pipe *otgpipe __diagused = MOTG_PIPE2MPIPE(xfer->ux_pipe);
2201 MOTGHIST_FUNC(); MOTGHIST_CALLED();
2202
2203 KASSERT(otgpipe->hw_ep->xfer != xfer);
2204 }
2205
2206 void
2207 motg_device_clear_toggle(struct usbd_pipe *pipe)
2208 {
2209 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe);
2210 otgpipe->nexttoggle = 0;
2211 }
2212
2213 /* Abort a device control request. */
2214 static void
2215 motg_abortx(struct usbd_xfer *xfer)
2216 {
2217 MOTGHIST_FUNC(); MOTGHIST_CALLED();
2218 uint8_t csr;
2219 struct motg_softc *sc = MOTG_XFER2SC(xfer);
2220 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe);
2221
2222 KASSERT(mutex_owned(&sc->sc_lock));
2223 ASSERT_SLEEPABLE();
2224
2225 /*
2226 * If we're dying, skip the hardware action and just notify the
2227 * software that we're done.
2228 */
2229 if (sc->sc_dying) {
2230 goto dying;
2231 }
2232
2233 if (otgpipe->hw_ep->xfer == xfer) {
2234 otgpipe->hw_ep->xfer = NULL;
2235 if (otgpipe->hw_ep->ep_number > 0) {
2236 /* select endpoint */
2237 UWRITE1(sc, MUSB2_REG_EPINDEX,
2238 otgpipe->hw_ep->ep_number);
2239 if (otgpipe->hw_ep->phase == DATA_OUT) {
2240 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
2241 while (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
2242 csr |= MUSB2_MASK_CSRL_TXFFLUSH;
2243 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
2244 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
2245 }
2246 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
2247 } else if (otgpipe->hw_ep->phase == DATA_IN) {
2248 csr = UREAD1(sc, MUSB2_REG_RXCSRL);
2249 while (csr & MUSB2_MASK_CSRL_RXPKTRDY) {
2250 csr |= MUSB2_MASK_CSRL_RXFFLUSH;
2251 UWRITE1(sc, MUSB2_REG_RXCSRL, csr);
2252 csr = UREAD1(sc, MUSB2_REG_RXCSRL);
2253 }
2254 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
2255 }
2256 otgpipe->hw_ep->phase = IDLE;
2257 }
2258 }
2259 dying:
2260 usb_transfer_complete(xfer);
2261 KASSERT(mutex_owned(&sc->sc_lock));
2262 }
2263