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