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