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