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