Home | History | Annotate | Line # | Download | only in dwc2
dwc2.c revision 1.65
      1 /*	$NetBSD: dwc2.c,v 1.65 2019/12/03 22:22:36 jmcneill Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nick Hudson
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: dwc2.c,v 1.65 2019/12/03 22:22:36 jmcneill Exp $");
     34 
     35 #include "opt_usb.h"
     36 
     37 #include <sys/param.h>
     38 
     39 #include <sys/cpu.h>
     40 #include <sys/device.h>
     41 #include <sys/kernel.h>
     42 #include <sys/kmem.h>
     43 #include <sys/proc.h>
     44 #include <sys/queue.h>
     45 #include <sys/select.h>
     46 #include <sys/sysctl.h>
     47 #include <sys/systm.h>
     48 
     49 #include <machine/endian.h>
     50 
     51 #include <dev/usb/usb.h>
     52 #include <dev/usb/usbdi.h>
     53 #include <dev/usb/usbdivar.h>
     54 #include <dev/usb/usb_mem.h>
     55 #include <dev/usb/usbroothub.h>
     56 
     57 #include <dwc2/dwc2.h>
     58 #include <dwc2/dwc2var.h>
     59 
     60 #include "dwc2_core.h"
     61 #include "dwc2_hcd.h"
     62 
     63 #ifdef DWC2_COUNTERS
     64 #define	DWC2_EVCNT_ADD(a,b)	((void)((a).ev_count += (b)))
     65 #else
     66 #define	DWC2_EVCNT_ADD(a,b)	do { } while (/*CONSTCOND*/0)
     67 #endif
     68 #define	DWC2_EVCNT_INCR(a)	DWC2_EVCNT_ADD((a), 1)
     69 
     70 #ifdef DWC2_DEBUG
     71 #define	DPRINTFN(n,fmt,...) do {			\
     72 	if (dwc2debug >= (n)) {			\
     73 		printf("%s: " fmt,			\
     74 		__FUNCTION__,## __VA_ARGS__);		\
     75 	}						\
     76 } while (0)
     77 #define	DPRINTF(...)	DPRINTFN(1, __VA_ARGS__)
     78 int dwc2debug = 0;
     79 
     80 SYSCTL_SETUP(sysctl_hw_dwc2_setup, "sysctl hw.dwc2 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, "dwc2",
     88 	    SYSCTL_DESCR("dwc2 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, &dwc2debug, sizeof(dwc2debug), 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 #else
    107 #define	DPRINTF(...) do { } while (0)
    108 #define	DPRINTFN(...) do { } while (0)
    109 #endif
    110 
    111 Static usbd_status	dwc2_open(struct usbd_pipe *);
    112 Static void		dwc2_poll(struct usbd_bus *);
    113 Static void		dwc2_softintr(void *);
    114 
    115 Static struct usbd_xfer *
    116 			dwc2_allocx(struct usbd_bus *, unsigned int);
    117 Static void		dwc2_freex(struct usbd_bus *, struct usbd_xfer *);
    118 Static void		dwc2_get_lock(struct usbd_bus *, kmutex_t **);
    119 Static int		dwc2_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
    120 			    void *, int);
    121 
    122 Static usbd_status	dwc2_root_intr_transfer(struct usbd_xfer *);
    123 Static usbd_status	dwc2_root_intr_start(struct usbd_xfer *);
    124 Static void		dwc2_root_intr_abort(struct usbd_xfer *);
    125 Static void		dwc2_root_intr_close(struct usbd_pipe *);
    126 Static void		dwc2_root_intr_done(struct usbd_xfer *);
    127 
    128 Static usbd_status	dwc2_device_ctrl_transfer(struct usbd_xfer *);
    129 Static usbd_status	dwc2_device_ctrl_start(struct usbd_xfer *);
    130 Static void		dwc2_device_ctrl_abort(struct usbd_xfer *);
    131 Static void		dwc2_device_ctrl_close(struct usbd_pipe *);
    132 Static void		dwc2_device_ctrl_done(struct usbd_xfer *);
    133 
    134 Static usbd_status	dwc2_device_bulk_transfer(struct usbd_xfer *);
    135 Static void		dwc2_device_bulk_abort(struct usbd_xfer *);
    136 Static void		dwc2_device_bulk_close(struct usbd_pipe *);
    137 Static void		dwc2_device_bulk_done(struct usbd_xfer *);
    138 
    139 Static usbd_status	dwc2_device_intr_transfer(struct usbd_xfer *);
    140 Static usbd_status	dwc2_device_intr_start(struct usbd_xfer *);
    141 Static void		dwc2_device_intr_abort(struct usbd_xfer *);
    142 Static void		dwc2_device_intr_close(struct usbd_pipe *);
    143 Static void		dwc2_device_intr_done(struct usbd_xfer *);
    144 
    145 Static usbd_status	dwc2_device_isoc_transfer(struct usbd_xfer *);
    146 Static void		dwc2_device_isoc_abort(struct usbd_xfer *);
    147 Static void		dwc2_device_isoc_close(struct usbd_pipe *);
    148 Static void		dwc2_device_isoc_done(struct usbd_xfer *);
    149 
    150 Static usbd_status	dwc2_device_start(struct usbd_xfer *);
    151 
    152 Static void		dwc2_close_pipe(struct usbd_pipe *);
    153 Static void		dwc2_abort_xfer(struct usbd_xfer *, usbd_status);
    154 
    155 Static void		dwc2_device_clear_toggle(struct usbd_pipe *);
    156 Static void		dwc2_noop(struct usbd_pipe *pipe);
    157 
    158 Static int		dwc2_interrupt(struct dwc2_softc *);
    159 Static void		dwc2_rhc(void *);
    160 
    161 Static void		dwc2_timeout(void *);
    162 Static void		dwc2_timeout_task(void *);
    163 
    164 
    165 static inline void
    166 dwc2_allocate_bus_bandwidth(struct dwc2_hsotg *hsotg, u16 bw,
    167 			    struct usbd_xfer *xfer)
    168 {
    169 }
    170 
    171 static inline void
    172 dwc2_free_bus_bandwidth(struct dwc2_hsotg *hsotg, u16 bw,
    173 			struct usbd_xfer *xfer)
    174 {
    175 }
    176 
    177 Static const struct usbd_bus_methods dwc2_bus_methods = {
    178 	.ubm_open =	dwc2_open,
    179 	.ubm_softint =	dwc2_softintr,
    180 	.ubm_dopoll =	dwc2_poll,
    181 	.ubm_allocx =	dwc2_allocx,
    182 	.ubm_freex =	dwc2_freex,
    183 	.ubm_getlock =	dwc2_get_lock,
    184 	.ubm_rhctrl =	dwc2_roothub_ctrl,
    185 };
    186 
    187 Static const struct usbd_pipe_methods dwc2_root_intr_methods = {
    188 	.upm_transfer =	dwc2_root_intr_transfer,
    189 	.upm_start =	dwc2_root_intr_start,
    190 	.upm_abort =	dwc2_root_intr_abort,
    191 	.upm_close =	dwc2_root_intr_close,
    192 	.upm_cleartoggle =	dwc2_noop,
    193 	.upm_done =	dwc2_root_intr_done,
    194 };
    195 
    196 Static const struct usbd_pipe_methods dwc2_device_ctrl_methods = {
    197 	.upm_transfer =	dwc2_device_ctrl_transfer,
    198 	.upm_start =	dwc2_device_ctrl_start,
    199 	.upm_abort =	dwc2_device_ctrl_abort,
    200 	.upm_close =	dwc2_device_ctrl_close,
    201 	.upm_cleartoggle =	dwc2_noop,
    202 	.upm_done =	dwc2_device_ctrl_done,
    203 };
    204 
    205 Static const struct usbd_pipe_methods dwc2_device_intr_methods = {
    206 	.upm_transfer =	dwc2_device_intr_transfer,
    207 	.upm_start =	dwc2_device_intr_start,
    208 	.upm_abort =	dwc2_device_intr_abort,
    209 	.upm_close =	dwc2_device_intr_close,
    210 	.upm_cleartoggle =	dwc2_device_clear_toggle,
    211 	.upm_done =	dwc2_device_intr_done,
    212 };
    213 
    214 Static const struct usbd_pipe_methods dwc2_device_bulk_methods = {
    215 	.upm_transfer =	dwc2_device_bulk_transfer,
    216 	.upm_abort =	dwc2_device_bulk_abort,
    217 	.upm_close =	dwc2_device_bulk_close,
    218 	.upm_cleartoggle =	dwc2_device_clear_toggle,
    219 	.upm_done =	dwc2_device_bulk_done,
    220 };
    221 
    222 Static const struct usbd_pipe_methods dwc2_device_isoc_methods = {
    223 	.upm_transfer =	dwc2_device_isoc_transfer,
    224 	.upm_abort =	dwc2_device_isoc_abort,
    225 	.upm_close =	dwc2_device_isoc_close,
    226 	.upm_cleartoggle =	dwc2_noop,
    227 	.upm_done =	dwc2_device_isoc_done,
    228 };
    229 
    230 struct usbd_xfer *
    231 dwc2_allocx(struct usbd_bus *bus, unsigned int nframes)
    232 {
    233 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
    234 	struct dwc2_xfer *dxfer;
    235 	struct usbd_xfer *xfer;
    236 
    237 	DPRINTFN(10, "\n");
    238 
    239 	DWC2_EVCNT_INCR(sc->sc_ev_xferpoolget);
    240 	dxfer = pool_cache_get(sc->sc_xferpool, PR_WAITOK);
    241 	xfer = (struct usbd_xfer *)dxfer;
    242 	if (dxfer != NULL) {
    243 		memset(dxfer, 0, sizeof(*dxfer));
    244 
    245 		dxfer->urb = dwc2_hcd_urb_alloc(sc->sc_hsotg,
    246 		    nframes, GFP_KERNEL);
    247 
    248 		/* Initialise this always so we can call remove on it. */
    249 		usb_init_task(&xfer->ux_aborttask, dwc2_timeout_task, xfer,
    250 		    USB_TASKQ_MPSAFE);
    251 #ifdef DIAGNOSTIC
    252 		dxfer->xfer.ux_state = XFER_BUSY;
    253 #endif
    254 	}
    255 	return (struct usbd_xfer *)dxfer;
    256 }
    257 
    258 void
    259 dwc2_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
    260 {
    261 	struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
    262 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
    263 
    264 	DPRINTFN(10, "\n");
    265 
    266 #ifdef DIAGNOSTIC
    267 	if (xfer->ux_state != XFER_BUSY &&
    268 	    xfer->ux_status != USBD_NOT_STARTED) {
    269 		DPRINTF("xfer=%p not busy, 0x%08x\n", xfer, xfer->ux_state);
    270 	}
    271 	xfer->ux_state = XFER_FREE;
    272 #endif
    273 	DWC2_EVCNT_INCR(sc->sc_ev_xferpoolput);
    274 	dwc2_hcd_urb_free(sc->sc_hsotg, dxfer->urb, dxfer->urb->packet_count);
    275 	pool_cache_put(sc->sc_xferpool, xfer);
    276 }
    277 
    278 Static void
    279 dwc2_get_lock(struct usbd_bus *bus, kmutex_t **lock)
    280 {
    281 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
    282 
    283 	*lock = &sc->sc_lock;
    284 }
    285 
    286 Static void
    287 dwc2_rhc(void *addr)
    288 {
    289 	struct dwc2_softc *sc = addr;
    290 	struct usbd_xfer *xfer;
    291 	u_char *p;
    292 
    293 	DPRINTF("\n");
    294 	mutex_enter(&sc->sc_lock);
    295 	xfer = sc->sc_intrxfer;
    296 
    297 	if (xfer == NULL) {
    298 		/* Just ignore the change. */
    299 		mutex_exit(&sc->sc_lock);
    300 		return;
    301 
    302 	}
    303 	/* set port bit */
    304 	p = KERNADDR(&xfer->ux_dmabuf, 0);
    305 
    306 	p[0] = 0x02;	/* we only have one port (1 << 1) */
    307 
    308 	xfer->ux_actlen = xfer->ux_length;
    309 	xfer->ux_status = USBD_NORMAL_COMPLETION;
    310 
    311 	usb_transfer_complete(xfer);
    312 	mutex_exit(&sc->sc_lock);
    313 }
    314 
    315 Static void
    316 dwc2_softintr(void *v)
    317 {
    318 	struct usbd_bus *bus = v;
    319 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
    320 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
    321 	struct dwc2_xfer *dxfer;
    322 
    323 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
    324 
    325 	mutex_spin_enter(&hsotg->lock);
    326 	while ((dxfer = TAILQ_FIRST(&sc->sc_complete)) != NULL) {
    327 
    328 		KASSERTMSG(!callout_pending(&dxfer->xfer.ux_callout),
    329 		    "xfer %p pipe %p\n", dxfer, dxfer->xfer.ux_pipe);
    330 
    331 		/*
    332 		 * dwc2_abort_xfer will remove this transfer from the
    333 		 * sc_complete queue
    334 		 */
    335 		/*XXXNH not tested */
    336 		if (dxfer->xfer.ux_status == USBD_CANCELLED ||
    337 		    dxfer->xfer.ux_status == USBD_TIMEOUT) {
    338 			continue;
    339 		}
    340 
    341 		TAILQ_REMOVE(&sc->sc_complete, dxfer, xnext);
    342 
    343 		mutex_spin_exit(&hsotg->lock);
    344 		usb_transfer_complete(&dxfer->xfer);
    345 		mutex_spin_enter(&hsotg->lock);
    346 	}
    347 	mutex_spin_exit(&hsotg->lock);
    348 }
    349 
    350 Static void
    351 dwc2_timeout(void *addr)
    352 {
    353 	struct usbd_xfer *xfer = addr;
    354  	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    355 	struct usbd_device *dev = xfer->ux_pipe->up_dev;
    356 
    357 	DPRINTF("xfer=%p\n", xfer);
    358 
    359 	mutex_enter(&sc->sc_lock);
    360 	if (!sc->sc_dying && xfer->ux_status == USBD_IN_PROGRESS)
    361 		usb_add_task(dev, &xfer->ux_aborttask, USB_TASKQ_HC);
    362 	mutex_exit(&sc->sc_lock);
    363 }
    364 
    365 Static void
    366 dwc2_timeout_task(void *addr)
    367 {
    368 	struct usbd_xfer *xfer = addr;
    369  	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    370 
    371 	DPRINTF("xfer=%p\n", xfer);
    372 
    373 	mutex_enter(&sc->sc_lock);
    374 	dwc2_abort_xfer(xfer, USBD_TIMEOUT);
    375 	mutex_exit(&sc->sc_lock);
    376 }
    377 
    378 usbd_status
    379 dwc2_open(struct usbd_pipe *pipe)
    380 {
    381 	struct usbd_device *dev = pipe->up_dev;
    382 	struct dwc2_softc *sc = DWC2_PIPE2SC(pipe);
    383 	struct dwc2_pipe *dpipe = DWC2_PIPE2DPIPE(pipe);
    384 	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
    385 	uint8_t addr = dev->ud_addr;
    386 	uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
    387 	usbd_status err;
    388 
    389 	DPRINTF("pipe %p addr %d xfertype %d dir %s\n", pipe, addr, xfertype,
    390 	    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in" : "out");
    391 
    392 	if (sc->sc_dying) {
    393 		return USBD_IOERROR;
    394 	}
    395 
    396 	if (addr == dev->ud_bus->ub_rhaddr) {
    397 		switch (ed->bEndpointAddress) {
    398 		case USB_CONTROL_ENDPOINT:
    399 			pipe->up_methods = &roothub_ctrl_methods;
    400 			break;
    401 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
    402 			pipe->up_methods = &dwc2_root_intr_methods;
    403 			break;
    404 		default:
    405 			DPRINTF("bad bEndpointAddress 0x%02x\n",
    406 			    ed->bEndpointAddress);
    407 			return USBD_INVAL;
    408 		}
    409 		DPRINTF("root hub pipe open\n");
    410 		return USBD_NORMAL_COMPLETION;
    411 	}
    412 
    413 	switch (xfertype) {
    414 	case UE_CONTROL:
    415 		pipe->up_methods = &dwc2_device_ctrl_methods;
    416 		err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
    417 		    0, &dpipe->req_dma);
    418 		if (err)
    419 			return err;
    420 		break;
    421 	case UE_INTERRUPT:
    422 		pipe->up_methods = &dwc2_device_intr_methods;
    423 		break;
    424 	case UE_ISOCHRONOUS:
    425 		pipe->up_serialise = false;
    426 		pipe->up_methods = &dwc2_device_isoc_methods;
    427 		break;
    428 	case UE_BULK:
    429 		pipe->up_serialise = false;
    430 		pipe->up_methods = &dwc2_device_bulk_methods;
    431 		break;
    432 	default:
    433 		DPRINTF("bad xfer type %d\n", xfertype);
    434 		return USBD_INVAL;
    435 	}
    436 
    437 	/* QH */
    438 	dpipe->priv = NULL;
    439 
    440 	return USBD_NORMAL_COMPLETION;
    441 }
    442 
    443 Static void
    444 dwc2_poll(struct usbd_bus *bus)
    445 {
    446 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
    447 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
    448 
    449 	mutex_spin_enter(&hsotg->lock);
    450 	dwc2_interrupt(sc);
    451 	mutex_spin_exit(&hsotg->lock);
    452 }
    453 
    454 /*
    455  * Close a reqular pipe.
    456  * Assumes that there are no pending transactions.
    457  */
    458 Static void
    459 dwc2_close_pipe(struct usbd_pipe *pipe)
    460 {
    461 #ifdef DIAGNOSTIC
    462 	struct dwc2_softc *sc = pipe->up_dev->ud_bus->ub_hcpriv;
    463 #endif
    464 
    465 	KASSERT(mutex_owned(&sc->sc_lock));
    466 }
    467 
    468 /*
    469  * Abort a device request.
    470  */
    471 Static void
    472 dwc2_abort_xfer(struct usbd_xfer *xfer, usbd_status status)
    473 {
    474 	struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
    475 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    476 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
    477 	struct dwc2_xfer *d, *tmp;
    478 	int err;
    479 
    480 	KASSERTMSG((status == USBD_CANCELLED || status == USBD_TIMEOUT),
    481 	    "invalid status for abort: %d", (int)status);
    482 
    483 	DPRINTF("xfer %p pipe %p status 0x%08x", xfer, xfer->ux_pipe, status);
    484 
    485 	KASSERT(mutex_owned(&sc->sc_lock));
    486 	ASSERT_SLEEPABLE();
    487 
    488 	if (status == USBD_CANCELLED) {
    489 		/*
    490 		 * We are synchronously aborting.  Try to stop the
    491 		 * callout and task, but if we can't, wait for them to
    492 		 * complete.
    493 		 */
    494 		callout_halt(&xfer->ux_callout, &sc->sc_lock);
    495 		usb_rem_task_wait(xfer->ux_pipe->up_dev, &xfer->ux_aborttask,
    496 		    USB_TASKQ_HC, &sc->sc_lock);
    497 	} else {
    498 		/* Otherwise, we are timing out.  */
    499 		KASSERT(status == USBD_TIMEOUT);
    500 	}
    501 
    502 	/*
    503 	 * The xfer cannot have been cancelled already.  It is the
    504 	 * responsibility of the caller of usbd_abort_pipe not to try
    505 	 * to abort a pipe multiple times, whether concurrently or
    506 	 * sequentially.
    507 	 */
    508 	KASSERT(xfer->ux_status != USBD_CANCELLED);
    509 
    510 	/* Only the timeout, which runs only once, can time it out.  */
    511 	KASSERT(xfer->ux_status != USBD_TIMEOUT);
    512 
    513 	/* If anyone else beat us, we're done.  */
    514 	if (xfer->ux_status != USBD_IN_PROGRESS)
    515 		return;
    516 
    517 	/* We beat everyone else.  Claim the status.  */
    518 	xfer->ux_status = status;
    519 
    520 	/*
    521 	 * If we're dying, skip the hardware action and just notify the
    522 	 * software that we're done.
    523 	 */
    524 	if (sc->sc_dying) {
    525 		DPRINTFN(4, "xfer %p dying 0x%08x", xfer, xfer->ux_status);
    526 		goto dying;
    527 	}
    528 
    529 	/*
    530 	 * HC Step 1: Handle the hardware.
    531 	 */
    532 	mutex_spin_enter(&hsotg->lock);
    533 	/* XXXNH suboptimal */
    534 	TAILQ_FOREACH_SAFE(d, &sc->sc_complete, xnext, tmp) {
    535 		if (d == dxfer) {
    536 			TAILQ_REMOVE(&sc->sc_complete, dxfer, xnext);
    537 			break;
    538 		}
    539 	}
    540 
    541 	err = dwc2_hcd_urb_dequeue(hsotg, dxfer->urb);
    542 	if (err) {
    543 		DPRINTF("dwc2_hcd_urb_dequeue failed\n");
    544 	}
    545 
    546 	mutex_spin_exit(&hsotg->lock);
    547 
    548 	/*
    549 	 * Final Step: Notify completion to waiting xfers.
    550 	 */
    551 dying:
    552 	usb_transfer_complete(xfer);
    553 	KASSERT(mutex_owned(&sc->sc_lock));
    554 }
    555 
    556 Static void
    557 dwc2_noop(struct usbd_pipe *pipe)
    558 {
    559 
    560 }
    561 
    562 Static void
    563 dwc2_device_clear_toggle(struct usbd_pipe *pipe)
    564 {
    565 
    566 	DPRINTF("toggle %d -> 0", pipe->up_endpoint->ue_toggle);
    567 }
    568 
    569 /***********************************************************************/
    570 
    571 Static int
    572 dwc2_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
    573     void *buf, int buflen)
    574 {
    575 	struct dwc2_softc *sc = bus->ub_hcpriv;
    576 	usbd_status err = USBD_IOERROR;
    577 	uint16_t len, value, index;
    578 	int totlen = 0;
    579 
    580 	if (sc->sc_dying)
    581 		return -1;
    582 
    583 	DPRINTFN(4, "type=0x%02x request=%02x\n",
    584 	    req->bmRequestType, req->bRequest);
    585 
    586 	len = UGETW(req->wLength);
    587 	value = UGETW(req->wValue);
    588 	index = UGETW(req->wIndex);
    589 
    590 #define C(x,y) ((x) | ((y) << 8))
    591 	switch (C(req->bRequest, req->bmRequestType)) {
    592 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
    593 		DPRINTFN(8, "wValue=0x%04x\n", value);
    594 
    595 		if (len == 0)
    596 			break;
    597 		switch (value) {
    598 #define sd ((usb_string_descriptor_t *)buf)
    599 		case C(2, UDESC_STRING):
    600 			/* Product */
    601 			totlen = usb_makestrdesc(sd, len, "DWC2 root hub");
    602 			break;
    603 #undef sd
    604 		default:
    605 			/* default from usbroothub */
    606 			return buflen;
    607 		}
    608 		break;
    609 
    610 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
    611 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
    612 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
    613 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
    614 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
    615 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
    616 		/* default from usbroothub */
    617 		DPRINTFN(4, "returning %d (usbroothub default)", buflen);
    618 
    619 		return buflen;
    620 
    621 	default:
    622 		/* Hub requests */
    623 		err = dwc2_hcd_hub_control(sc->sc_hsotg,
    624 		    C(req->bRequest, req->bmRequestType), value, index,
    625 		    buf, len);
    626 		if (err) {
    627 			return -1;
    628 		}
    629 		totlen = len;
    630 	}
    631 
    632 	return totlen;
    633 }
    634 
    635 Static usbd_status
    636 dwc2_root_intr_transfer(struct usbd_xfer *xfer)
    637 {
    638 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    639 	usbd_status err;
    640 
    641 	DPRINTF("\n");
    642 
    643 	/* Insert last in queue. */
    644 	mutex_enter(&sc->sc_lock);
    645 	err = usb_insert_transfer(xfer);
    646 	mutex_exit(&sc->sc_lock);
    647 	if (err)
    648 		return err;
    649 
    650 	/* Pipe isn't running, start first */
    651 	return dwc2_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    652 }
    653 
    654 Static usbd_status
    655 dwc2_root_intr_start(struct usbd_xfer *xfer)
    656 {
    657 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    658 	const bool polling = sc->sc_bus.ub_usepolling;
    659 
    660 	DPRINTF("\n");
    661 
    662 	if (sc->sc_dying)
    663 		return USBD_IOERROR;
    664 
    665 	if (!polling)
    666 		mutex_enter(&sc->sc_lock);
    667 	KASSERT(sc->sc_intrxfer == NULL);
    668 	sc->sc_intrxfer = xfer;
    669 	if (!polling)
    670 		mutex_exit(&sc->sc_lock);
    671 
    672 	return USBD_IN_PROGRESS;
    673 }
    674 
    675 /* Abort a root interrupt request. */
    676 Static void
    677 dwc2_root_intr_abort(struct usbd_xfer *xfer)
    678 {
    679 	struct dwc2_softc *sc __diagused = DWC2_XFER2SC(xfer);
    680 
    681 	DPRINTF("xfer=%p\n", xfer);
    682 
    683 	KASSERT(mutex_owned(&sc->sc_lock));
    684 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
    685 
    686 	xfer->ux_status = USBD_CANCELLED;
    687 	usb_transfer_complete(xfer);
    688 }
    689 
    690 Static void
    691 dwc2_root_intr_close(struct usbd_pipe *pipe)
    692 {
    693 	struct dwc2_softc *sc = DWC2_PIPE2SC(pipe);
    694 
    695 	DPRINTF("\n");
    696 
    697 	KASSERT(mutex_owned(&sc->sc_lock));
    698 
    699 	sc->sc_intrxfer = NULL;
    700 }
    701 
    702 Static void
    703 dwc2_root_intr_done(struct usbd_xfer *xfer)
    704 {
    705 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    706 
    707 	KASSERT(sc->sc_intrxfer != NULL);
    708 	sc->sc_intrxfer = NULL;
    709 	DPRINTF("\n");
    710 }
    711 
    712 /***********************************************************************/
    713 
    714 Static usbd_status
    715 dwc2_device_ctrl_transfer(struct usbd_xfer *xfer)
    716 {
    717 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    718 	usbd_status err;
    719 
    720 	DPRINTF("\n");
    721 
    722 	/* Insert last in queue. */
    723 	mutex_enter(&sc->sc_lock);
    724 	err = usb_insert_transfer(xfer);
    725 	mutex_exit(&sc->sc_lock);
    726 	if (err)
    727 		return err;
    728 
    729 	/* Pipe isn't running, start first */
    730 	return dwc2_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    731 }
    732 
    733 Static usbd_status
    734 dwc2_device_ctrl_start(struct usbd_xfer *xfer)
    735 {
    736 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    737 	usbd_status err;
    738 	const bool polling = sc->sc_bus.ub_usepolling;
    739 
    740 	DPRINTF("\n");
    741 
    742 	if (!polling)
    743 		mutex_enter(&sc->sc_lock);
    744 	xfer->ux_status = USBD_IN_PROGRESS;
    745 	err = dwc2_device_start(xfer);
    746 	if (!polling)
    747 		mutex_exit(&sc->sc_lock);
    748 
    749 	if (err)
    750 		return err;
    751 
    752 	return USBD_IN_PROGRESS;
    753 }
    754 
    755 Static void
    756 dwc2_device_ctrl_abort(struct usbd_xfer *xfer)
    757 {
    758 #ifdef DIAGNOSTIC
    759 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    760 #endif
    761 	KASSERT(mutex_owned(&sc->sc_lock));
    762 
    763 	DPRINTF("xfer=%p\n", xfer);
    764 	dwc2_abort_xfer(xfer, USBD_CANCELLED);
    765 }
    766 
    767 Static void
    768 dwc2_device_ctrl_close(struct usbd_pipe *pipe)
    769 {
    770 
    771 	DPRINTF("pipe=%p\n", pipe);
    772 	dwc2_close_pipe(pipe);
    773 }
    774 
    775 Static void
    776 dwc2_device_ctrl_done(struct usbd_xfer *xfer)
    777 {
    778 
    779 	DPRINTF("xfer=%p\n", xfer);
    780 }
    781 
    782 /***********************************************************************/
    783 
    784 Static usbd_status
    785 dwc2_device_bulk_transfer(struct usbd_xfer *xfer)
    786 {
    787 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    788 	usbd_status err;
    789 
    790 	DPRINTF("xfer=%p\n", xfer);
    791 
    792 	/* Insert last in queue. */
    793 	mutex_enter(&sc->sc_lock);
    794 	err = usb_insert_transfer(xfer);
    795 
    796 	KASSERT(err == USBD_NORMAL_COMPLETION);
    797 
    798 	xfer->ux_status = USBD_IN_PROGRESS;
    799 	err = dwc2_device_start(xfer);
    800 	mutex_exit(&sc->sc_lock);
    801 
    802 	return err;
    803 }
    804 
    805 Static void
    806 dwc2_device_bulk_abort(struct usbd_xfer *xfer)
    807 {
    808 #ifdef DIAGNOSTIC
    809 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    810 #endif
    811 	KASSERT(mutex_owned(&sc->sc_lock));
    812 
    813 	DPRINTF("xfer=%p\n", xfer);
    814 	dwc2_abort_xfer(xfer, USBD_CANCELLED);
    815 }
    816 
    817 Static void
    818 dwc2_device_bulk_close(struct usbd_pipe *pipe)
    819 {
    820 
    821 	DPRINTF("pipe=%p\n", pipe);
    822 
    823 	dwc2_close_pipe(pipe);
    824 }
    825 
    826 Static void
    827 dwc2_device_bulk_done(struct usbd_xfer *xfer)
    828 {
    829 
    830 	DPRINTF("xfer=%p\n", xfer);
    831 }
    832 
    833 /***********************************************************************/
    834 
    835 Static usbd_status
    836 dwc2_device_intr_transfer(struct usbd_xfer *xfer)
    837 {
    838 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    839 	usbd_status err;
    840 
    841 	DPRINTF("xfer=%p\n", xfer);
    842 
    843 	/* Insert last in queue. */
    844 	mutex_enter(&sc->sc_lock);
    845 	err = usb_insert_transfer(xfer);
    846 	mutex_exit(&sc->sc_lock);
    847 	if (err)
    848 		return err;
    849 
    850 	/* Pipe isn't running, start first */
    851 	return dwc2_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    852 }
    853 
    854 Static usbd_status
    855 dwc2_device_intr_start(struct usbd_xfer *xfer)
    856 {
    857 	struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer)
    858 	struct usbd_device *dev = dpipe->pipe.up_dev;
    859 	struct dwc2_softc *sc = dev->ud_bus->ub_hcpriv;
    860 	usbd_status err;
    861 	const bool polling = sc->sc_bus.ub_usepolling;
    862 
    863 	if (!polling)
    864 		mutex_enter(&sc->sc_lock);
    865 	xfer->ux_status = USBD_IN_PROGRESS;
    866 	err = dwc2_device_start(xfer);
    867 	if (!polling)
    868 		mutex_exit(&sc->sc_lock);
    869 
    870 	if (err)
    871 		return err;
    872 
    873 	return USBD_IN_PROGRESS;
    874 }
    875 
    876 /* Abort a device interrupt request. */
    877 Static void
    878 dwc2_device_intr_abort(struct usbd_xfer *xfer)
    879 {
    880 #ifdef DIAGNOSTIC
    881 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    882 #endif
    883 
    884 	KASSERT(mutex_owned(&sc->sc_lock));
    885 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
    886 
    887 	DPRINTF("xfer=%p\n", xfer);
    888 
    889 	dwc2_abort_xfer(xfer, USBD_CANCELLED);
    890 }
    891 
    892 Static void
    893 dwc2_device_intr_close(struct usbd_pipe *pipe)
    894 {
    895 
    896 	DPRINTF("pipe=%p\n", pipe);
    897 
    898 	dwc2_close_pipe(pipe);
    899 }
    900 
    901 Static void
    902 dwc2_device_intr_done(struct usbd_xfer *xfer)
    903 {
    904 
    905 	DPRINTF("\n");
    906 }
    907 
    908 /***********************************************************************/
    909 
    910 usbd_status
    911 dwc2_device_isoc_transfer(struct usbd_xfer *xfer)
    912 {
    913 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    914 	usbd_status err;
    915 
    916 	DPRINTF("xfer=%p\n", xfer);
    917 
    918 	/* Insert last in queue. */
    919 	mutex_enter(&sc->sc_lock);
    920 	err = usb_insert_transfer(xfer);
    921 
    922 	KASSERT(err == USBD_NORMAL_COMPLETION);
    923 
    924 	xfer->ux_status = USBD_IN_PROGRESS;
    925 	err = dwc2_device_start(xfer);
    926 	mutex_exit(&sc->sc_lock);
    927 
    928 	return err;
    929 }
    930 
    931 void
    932 dwc2_device_isoc_abort(struct usbd_xfer *xfer)
    933 {
    934 	struct dwc2_softc *sc __diagused = DWC2_XFER2SC(xfer);
    935 	KASSERT(mutex_owned(&sc->sc_lock));
    936 
    937 	DPRINTF("xfer=%p\n", xfer);
    938 	dwc2_abort_xfer(xfer, USBD_CANCELLED);
    939 }
    940 
    941 void
    942 dwc2_device_isoc_close(struct usbd_pipe *pipe)
    943 {
    944 	DPRINTF("\n");
    945 
    946 	dwc2_close_pipe(pipe);
    947 }
    948 
    949 void
    950 dwc2_device_isoc_done(struct usbd_xfer *xfer)
    951 {
    952 
    953 	DPRINTF("\n");
    954 }
    955 
    956 
    957 usbd_status
    958 dwc2_device_start(struct usbd_xfer *xfer)
    959 {
    960  	struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
    961 	struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
    962 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
    963 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
    964 	struct dwc2_hcd_urb *dwc2_urb;
    965 
    966 	struct usbd_device *dev = xfer->ux_pipe->up_dev;
    967 	usb_endpoint_descriptor_t *ed = xfer->ux_pipe->up_endpoint->ue_edesc;
    968 	uint8_t addr = dev->ud_addr;
    969 	uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
    970 	uint8_t epnum = UE_GET_ADDR(ed->bEndpointAddress);
    971 	uint8_t dir = UE_GET_DIR(ed->bEndpointAddress);
    972 	uint16_t mps = UE_GET_SIZE(UGETW(ed->wMaxPacketSize));
    973 	uint32_t len;
    974 
    975 	uint32_t flags = 0;
    976 	uint32_t off = 0;
    977 	int retval, err;
    978 	int alloc_bandwidth = 0;
    979 
    980 	DPRINTFN(1, "xfer=%p pipe=%p\n", xfer, xfer->ux_pipe);
    981 
    982 	if (xfertype == UE_ISOCHRONOUS ||
    983 	    xfertype == UE_INTERRUPT) {
    984 		mutex_spin_enter(&hsotg->lock);
    985 		if (!dwc2_hcd_is_bandwidth_allocated(hsotg, xfer))
    986 			alloc_bandwidth = 1;
    987 		mutex_spin_exit(&hsotg->lock);
    988 	}
    989 
    990 	/*
    991 	 * For Control pipe the direction is from the request, all other
    992 	 * transfers have been set correctly at pipe open time.
    993 	 */
    994 	if (xfertype == UE_CONTROL) {
    995 		usb_device_request_t *req = &xfer->ux_request;
    996 
    997 		DPRINTFN(3, "xfer=%p type=0x%02x request=0x%02x wValue=0x%04x "
    998 		    "wIndex=0x%04x len=%d addr=%d endpt=%d dir=%s speed=%d "
    999 		    "mps=%d\n",
   1000 		    xfer, req->bmRequestType, req->bRequest, UGETW(req->wValue),
   1001 		    UGETW(req->wIndex), UGETW(req->wLength), dev->ud_addr,
   1002 		    epnum, dir == UT_READ ? "in" :"out", dev->ud_speed, mps);
   1003 
   1004 		/* Copy request packet to our DMA buffer */
   1005 		memcpy(KERNADDR(&dpipe->req_dma, 0), req, sizeof(*req));
   1006 		usb_syncmem(&dpipe->req_dma, 0, sizeof(*req),
   1007 		    BUS_DMASYNC_PREWRITE);
   1008 		len = UGETW(req->wLength);
   1009 		if ((req->bmRequestType & UT_READ) == UT_READ) {
   1010 			dir = UE_DIR_IN;
   1011 		} else {
   1012 			dir = UE_DIR_OUT;
   1013 		}
   1014 
   1015 		DPRINTFN(3, "req = %p dma = %" PRIxBUSADDR " len %d dir %s\n",
   1016 		    KERNADDR(&dpipe->req_dma, 0), DMAADDR(&dpipe->req_dma, 0),
   1017 		    len, dir == UE_DIR_IN ? "in" : "out");
   1018 	} else if (xfertype == UE_ISOCHRONOUS) {
   1019 		DPRINTFN(3, "xfer=%p nframes=%d flags=%d addr=%d endpt=%d,"
   1020 		    " mps=%d dir %s\n", xfer, xfer->ux_nframes, xfer->ux_flags, addr,
   1021 		    epnum, mps, dir == UT_READ ? "in" :"out");
   1022 
   1023 		len = 0;
   1024 		for (int i = 0; i < xfer->ux_nframes; i++)
   1025 			len += xfer->ux_frlengths[i];
   1026 	} else {
   1027 		DPRINTFN(3, "xfer=%p len=%d flags=%d addr=%d endpt=%d,"
   1028 		    " mps=%d dir %s\n", xfer, xfer->ux_length, xfer->ux_flags, addr,
   1029 		    epnum, mps, dir == UT_READ ? "in" :"out");
   1030 
   1031 		len = xfer->ux_length;
   1032 	}
   1033 
   1034 	dwc2_urb = dxfer->urb;
   1035 	if (!dwc2_urb)
   1036 		return USBD_NOMEM;
   1037 
   1038 	KASSERT(dwc2_urb->packet_count == xfer->ux_nframes);
   1039 	memset(dwc2_urb, 0, sizeof(*dwc2_urb) +
   1040 	    sizeof(dwc2_urb->iso_descs[0]) * dwc2_urb->packet_count);
   1041 
   1042 	dwc2_urb->priv = xfer;
   1043 	dwc2_urb->packet_count = xfer->ux_nframes;
   1044 
   1045 	dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, addr, epnum, xfertype, dir,
   1046 	    mps);
   1047 
   1048 	if (xfertype == UE_CONTROL) {
   1049 		dwc2_urb->setup_usbdma = &dpipe->req_dma;
   1050 		dwc2_urb->setup_packet = KERNADDR(&dpipe->req_dma, 0);
   1051 		dwc2_urb->setup_dma = DMAADDR(&dpipe->req_dma, 0);
   1052 	} else {
   1053 		/* XXXNH - % mps required? */
   1054 		if ((xfer->ux_flags & USBD_FORCE_SHORT_XFER) && (len % mps) == 0)
   1055 		    flags |= URB_SEND_ZERO_PACKET;
   1056 	}
   1057 	flags |= URB_GIVEBACK_ASAP;
   1058 
   1059 	/*
   1060 	 * control transfers with no data phase don't touch usbdma, but
   1061 	 * everything else does.
   1062 	 */
   1063 	if (!(xfertype == UE_CONTROL && len == 0)) {
   1064 		dwc2_urb->usbdma = &xfer->ux_dmabuf;
   1065 		dwc2_urb->buf = KERNADDR(dwc2_urb->usbdma, 0);
   1066 		dwc2_urb->dma = DMAADDR(dwc2_urb->usbdma, 0);
   1067 
   1068 		usb_syncmem(&xfer->ux_dmabuf, 0, len,
   1069 		    dir == UE_DIR_IN ?
   1070 			BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1071  	}
   1072 	dwc2_urb->length = len;
   1073  	dwc2_urb->flags = flags;
   1074 	dwc2_urb->status = -EINPROGRESS;
   1075 
   1076 	if (xfertype == UE_INTERRUPT ||
   1077 	    xfertype == UE_ISOCHRONOUS) {
   1078 		uint16_t ival;
   1079 
   1080 		if (xfertype == UE_INTERRUPT &&
   1081 		    dpipe->pipe.up_interval != USBD_DEFAULT_INTERVAL) {
   1082 			ival = dpipe->pipe.up_interval;
   1083 		} else {
   1084 			ival = ed->bInterval;
   1085 		}
   1086 
   1087 		if (ival < 1) {
   1088 			retval = -ENODEV;
   1089 			goto fail;
   1090 		}
   1091 		if (dev->ud_speed == USB_SPEED_HIGH ||
   1092 		   (dev->ud_speed == USB_SPEED_FULL && xfertype == UE_ISOCHRONOUS)) {
   1093 			if (ival > 16) {
   1094 				/*
   1095 				 * illegal with HS/FS, but there were
   1096 				 * documentation bugs in the spec
   1097 				 */
   1098 				ival = 256;
   1099 			} else {
   1100 				ival = (1 << (ival - 1));
   1101 			}
   1102 		} else {
   1103 			if (xfertype == UE_INTERRUPT && ival < 10)
   1104 				ival = 10;
   1105 		}
   1106 		dwc2_urb->interval = ival;
   1107 	}
   1108 
   1109 	/* XXXNH bring down from callers?? */
   1110 // 	mutex_enter(&sc->sc_lock);
   1111 
   1112 	xfer->ux_actlen = 0;
   1113 
   1114 	KASSERT(xfertype != UE_ISOCHRONOUS ||
   1115 	    xfer->ux_nframes <= dwc2_urb->packet_count);
   1116 	KASSERTMSG(xfer->ux_nframes == 0 || xfertype == UE_ISOCHRONOUS,
   1117 	    "nframes %d xfertype %d\n", xfer->ux_nframes, xfertype);
   1118 
   1119 	off = 0;
   1120 	for (size_t i = 0; i < xfer->ux_nframes; ++i) {
   1121 		DPRINTFN(3, "xfer=%p frame=%zd offset=%d length=%d\n", xfer, i,
   1122 		    off, xfer->ux_frlengths[i]);
   1123 
   1124 		dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i, off,
   1125 		    xfer->ux_frlengths[i]);
   1126 		off += xfer->ux_frlengths[i];
   1127 	}
   1128 
   1129 	struct dwc2_qh *qh = dpipe->priv;
   1130 	struct dwc2_qtd *qtd;
   1131 	bool qh_allocated = false;
   1132 
   1133 	/* Create QH for the endpoint if it doesn't exist */
   1134 	if (!qh) {
   1135 		qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, GFP_ATOMIC);
   1136 		if (!qh) {
   1137 			retval = -ENOMEM;
   1138 			goto fail;
   1139 		}
   1140 		dpipe->priv = qh;
   1141 		qh_allocated = true;
   1142 	}
   1143 
   1144 	qtd = pool_cache_get(sc->sc_qtdpool, PR_NOWAIT);
   1145 	if (!qtd) {
   1146 		retval = -ENOMEM;
   1147 		goto fail1;
   1148 	}
   1149 	memset(qtd, 0, sizeof(*qtd));
   1150 
   1151 	/* might need to check cpu_intr_p */
   1152 	mutex_spin_enter(&hsotg->lock);
   1153 
   1154 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
   1155 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   1156 		    dwc2_timeout, xfer);
   1157 	}
   1158 	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
   1159 	if (retval)
   1160 		goto fail2;
   1161 
   1162 	if (alloc_bandwidth) {
   1163 		dwc2_allocate_bus_bandwidth(hsotg,
   1164 				dwc2_hcd_get_ep_bandwidth(hsotg, dpipe),
   1165 				xfer);
   1166 	}
   1167 
   1168 	mutex_spin_exit(&hsotg->lock);
   1169 // 	mutex_exit(&sc->sc_lock);
   1170 
   1171 	return USBD_IN_PROGRESS;
   1172 
   1173 fail2:
   1174 	callout_halt(&xfer->ux_callout, &hsotg->lock);
   1175 	dwc2_urb->priv = NULL;
   1176 	mutex_spin_exit(&hsotg->lock);
   1177 	pool_cache_put(sc->sc_qtdpool, qtd);
   1178 
   1179 fail1:
   1180 	if (qh_allocated) {
   1181 		dpipe->priv = NULL;
   1182 		dwc2_hcd_qh_free(hsotg, qh);
   1183 	}
   1184 fail:
   1185 
   1186 	switch (retval) {
   1187 	case -EINVAL:
   1188 	case -ENODEV:
   1189 		err = USBD_INVAL;
   1190 		break;
   1191 	case -ENOMEM:
   1192 		err = USBD_NOMEM;
   1193 		break;
   1194 	default:
   1195 		err = USBD_IOERROR;
   1196 	}
   1197 
   1198 	return err;
   1199 
   1200 }
   1201 
   1202 int dwc2_intr(void *p)
   1203 {
   1204 	struct dwc2_softc *sc = p;
   1205 	struct dwc2_hsotg *hsotg;
   1206 	int ret = 0;
   1207 
   1208 	if (sc == NULL)
   1209 		return 0;
   1210 
   1211 	hsotg = sc->sc_hsotg;
   1212 	mutex_spin_enter(&hsotg->lock);
   1213 
   1214 	if (sc->sc_dying || !device_has_power(sc->sc_dev))
   1215 		goto done;
   1216 
   1217 	if (sc->sc_bus.ub_usepolling) {
   1218 		uint32_t intrs;
   1219 
   1220 		intrs = dwc2_read_core_intr(hsotg);
   1221 		DWC2_WRITE_4(hsotg, GINTSTS, intrs);
   1222 	} else {
   1223 		ret = dwc2_interrupt(sc);
   1224 	}
   1225 
   1226 done:
   1227 	mutex_spin_exit(&hsotg->lock);
   1228 
   1229 	return ret;
   1230 }
   1231 
   1232 int
   1233 dwc2_interrupt(struct dwc2_softc *sc)
   1234 {
   1235 	int ret = 0;
   1236 
   1237 	if (sc->sc_hcdenabled) {
   1238 		ret |= dwc2_handle_hcd_intr(sc->sc_hsotg);
   1239 	}
   1240 
   1241 	ret |= dwc2_handle_common_intr(sc->sc_hsotg);
   1242 
   1243 	return ret;
   1244 }
   1245 
   1246 /***********************************************************************/
   1247 
   1248 int
   1249 dwc2_detach(struct dwc2_softc *sc, int flags)
   1250 {
   1251 	int rv = 0;
   1252 
   1253 	if (sc->sc_child != NULL)
   1254 		rv = config_detach(sc->sc_child, flags);
   1255 
   1256 	return rv;
   1257 }
   1258 
   1259 bool
   1260 dwc2_shutdown(device_t self, int flags)
   1261 {
   1262 	struct dwc2_softc *sc = device_private(self);
   1263 
   1264 	sc = sc;
   1265 
   1266 	return true;
   1267 }
   1268 
   1269 void
   1270 dwc2_childdet(device_t self, device_t child)
   1271 {
   1272 	struct dwc2_softc *sc = device_private(self);
   1273 
   1274 	sc = sc;
   1275 }
   1276 
   1277 int
   1278 dwc2_activate(device_t self, enum devact act)
   1279 {
   1280 	struct dwc2_softc *sc = device_private(self);
   1281 
   1282 	sc = sc;
   1283 
   1284 	return 0;
   1285 }
   1286 
   1287 bool
   1288 dwc2_resume(device_t dv, const pmf_qual_t *qual)
   1289 {
   1290 	struct dwc2_softc *sc = device_private(dv);
   1291 
   1292 	sc = sc;
   1293 
   1294 	return true;
   1295 }
   1296 
   1297 bool
   1298 dwc2_suspend(device_t dv, const pmf_qual_t *qual)
   1299 {
   1300 	struct dwc2_softc *sc = device_private(dv);
   1301 
   1302 	sc = sc;
   1303 
   1304 	return true;
   1305 }
   1306 
   1307 /***********************************************************************/
   1308 int
   1309 dwc2_init(struct dwc2_softc *sc)
   1310 {
   1311 	int err = 0;
   1312 
   1313 	err = linux_workqueue_init();
   1314 	if (err)
   1315 		return err;
   1316 
   1317 	sc->sc_bus.ub_hcpriv = sc;
   1318 	sc->sc_bus.ub_revision = USBREV_2_0;
   1319 	sc->sc_bus.ub_methods = &dwc2_bus_methods;
   1320 	sc->sc_bus.ub_pipesize = sizeof(struct dwc2_pipe);
   1321 	sc->sc_bus.ub_usedma = true;
   1322 	sc->sc_hcdenabled = false;
   1323 
   1324 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1325 
   1326 	TAILQ_INIT(&sc->sc_complete);
   1327 
   1328 	sc->sc_rhc_si = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
   1329 	    dwc2_rhc, sc);
   1330 
   1331 	sc->sc_xferpool = pool_cache_init(sizeof(struct dwc2_xfer), 0, 0, 0,
   1332 	    "dwc2xfer", NULL, IPL_USB, NULL, NULL, NULL);
   1333 	sc->sc_qhpool = pool_cache_init(sizeof(struct dwc2_qh), 0, 0, 0,
   1334 	    "dwc2qh", NULL, IPL_USB, NULL, NULL, NULL);
   1335 	sc->sc_qtdpool = pool_cache_init(sizeof(struct dwc2_qtd), 0, 0, 0,
   1336 	    "dwc2qtd", NULL, IPL_USB, NULL, NULL, NULL);
   1337 
   1338 	sc->sc_hsotg = kmem_zalloc(sizeof(struct dwc2_hsotg), KM_SLEEP);
   1339 	sc->sc_hsotg->hsotg_sc = sc;
   1340 	sc->sc_hsotg->dev = sc->sc_dev;
   1341 	sc->sc_hcdenabled = true;
   1342 
   1343 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
   1344 	struct dwc2_core_params defparams;
   1345 	int retval;
   1346 
   1347 	if (sc->sc_params == NULL) {
   1348 		/* Default all params to autodetect */
   1349 		dwc2_set_all_params(&defparams, -1);
   1350 		sc->sc_params = &defparams;
   1351 
   1352 		/*
   1353 		 * Disable descriptor dma mode by default as the HW can support
   1354 		 * it, but does not support it for SPLIT transactions.
   1355 		 */
   1356 		defparams.dma_desc_enable = 0;
   1357 	}
   1358 	hsotg->dr_mode = USB_DR_MODE_HOST;
   1359 
   1360 	/* Detect config values from hardware */
   1361 	retval = dwc2_get_hwparams(hsotg);
   1362 	if (retval) {
   1363 		goto fail2;
   1364 	}
   1365 
   1366 	hsotg->core_params = kmem_zalloc(sizeof(*hsotg->core_params), KM_SLEEP);
   1367 	dwc2_set_all_params(hsotg->core_params, -1);
   1368 
   1369 	/* Validate parameter values */
   1370 	dwc2_set_parameters(hsotg, sc->sc_params);
   1371 
   1372 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
   1373     IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
   1374 	if (hsotg->dr_mode != USB_DR_MODE_HOST) {
   1375 		retval = dwc2_gadget_init(hsotg);
   1376 		if (retval)
   1377 			goto fail2;
   1378 		hsotg->gadget_enabled = 1;
   1379 	}
   1380 #endif
   1381 #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || \
   1382     IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
   1383 	if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) {
   1384 		retval = dwc2_hcd_init(hsotg);
   1385 		if (retval) {
   1386 			if (hsotg->gadget_enabled)
   1387 				dwc2_hsotg_remove(hsotg);
   1388 			goto fail2;
   1389 		}
   1390 	    hsotg->hcd_enabled = 1;
   1391         }
   1392 #endif
   1393 
   1394 	uint32_t snpsid = hsotg->hw_params.snpsid;
   1395 	aprint_verbose_dev(sc->sc_dev, "Core Release: %x.%x%x%x (snpsid=%x)\n",
   1396 	    snpsid >> 12 & 0xf, snpsid >> 8 & 0xf,
   1397 	    snpsid >> 4 & 0xf, snpsid & 0xf, snpsid);
   1398 
   1399 	return 0;
   1400 
   1401 fail2:
   1402 	err = -retval;
   1403 	kmem_free(sc->sc_hsotg, sizeof(struct dwc2_hsotg));
   1404 	softint_disestablish(sc->sc_rhc_si);
   1405 
   1406 	return err;
   1407 }
   1408 
   1409 #if 0
   1410 /*
   1411  * curmode is a mode indication bit 0 = device, 1 = host
   1412  */
   1413 static const char * const intnames[32] = {
   1414 	"curmode",	"modemis",	"otgint",	"sof",
   1415 	"rxflvl",	"nptxfemp",	"ginnakeff",	"goutnakeff",
   1416 	"ulpickint",	"i2cint",	"erlysusp",	"usbsusp",
   1417 	"usbrst",	"enumdone",	"isooutdrop",	"eopf",
   1418 	"restore_done",	"epmis",	"iepint",	"oepint",
   1419 	"incompisoin",	"incomplp",	"fetsusp",	"resetdet",
   1420 	"prtint",	"hchint",	"ptxfemp",	"lpm",
   1421 	"conidstschng",	"disconnint",	"sessreqint",	"wkupint"
   1422 };
   1423 
   1424 
   1425 /***********************************************************************/
   1426 
   1427 #endif
   1428 
   1429 void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, int *hub_addr,
   1430 			int *hub_port)
   1431 {
   1432 	struct usbd_xfer *xfer = context;
   1433 	struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
   1434 	struct usbd_device *dev = dpipe->pipe.up_dev;
   1435 
   1436 	*hub_addr = dev->ud_myhsport->up_parent->ud_addr;
   1437  	*hub_port = dev->ud_myhsport->up_portno;
   1438 }
   1439 
   1440 int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
   1441 {
   1442 	struct usbd_xfer *xfer = context;
   1443 	struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
   1444 	struct usbd_device *dev = dpipe->pipe.up_dev;
   1445 
   1446 	return dev->ud_speed;
   1447 }
   1448 
   1449 /*
   1450  * Sets the final status of an URB and returns it to the upper layer. Any
   1451  * required cleanup of the URB is performed.
   1452  *
   1453  * Must be called with interrupt disabled and spinlock held
   1454  */
   1455 void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
   1456     int status)
   1457 {
   1458 	struct usbd_xfer *xfer;
   1459 	struct dwc2_xfer *dxfer;
   1460 	struct dwc2_softc *sc;
   1461 	usb_endpoint_descriptor_t *ed;
   1462 	uint8_t xfertype;
   1463 
   1464 	if (!qtd) {
   1465 		dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
   1466 		return;
   1467 	}
   1468 
   1469 	if (!qtd->urb) {
   1470 		dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
   1471 		return;
   1472 	}
   1473 
   1474 	xfer = qtd->urb->priv;
   1475 	if (!xfer) {
   1476 		dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
   1477 		return;
   1478 	}
   1479 
   1480 	/*
   1481 	 * If software has completed it, either by cancellation
   1482 	 * or timeout, drop it on the floor.
   1483 	 */
   1484 	if (xfer->ux_status != USBD_IN_PROGRESS) {
   1485 		KASSERT(xfer->ux_status == USBD_CANCELLED ||
   1486 		    xfer->ux_status == USBD_TIMEOUT);
   1487 		return;
   1488 	}
   1489 
   1490 	/*
   1491 	 * Cancel the timeout and the task, which have not yet
   1492 	 * run.  If they have already fired, at worst they are
   1493 	 * waiting for the lock.  They will see that the xfer
   1494 	 * is no longer in progress and give up.
   1495 	 */
   1496 	callout_stop(&xfer->ux_callout);
   1497 	usb_rem_task(xfer->ux_pipe->up_dev, &xfer->ux_aborttask);
   1498 
   1499 	dxfer = DWC2_XFER2DXFER(xfer);
   1500 	sc = DWC2_XFER2SC(xfer);
   1501 	ed = xfer->ux_pipe->up_endpoint->ue_edesc;
   1502 	xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
   1503 
   1504 	struct dwc2_hcd_urb *urb = qtd->urb;
   1505 	xfer->ux_actlen = dwc2_hcd_urb_get_actual_length(urb);
   1506 
   1507 	DPRINTFN(3, "xfer=%p actlen=%d\n", xfer, xfer->ux_actlen);
   1508 
   1509 	if (xfertype == UE_ISOCHRONOUS) {
   1510 		xfer->ux_actlen = 0;
   1511 		for (size_t i = 0; i < xfer->ux_nframes; ++i) {
   1512 			xfer->ux_frlengths[i] =
   1513 				dwc2_hcd_urb_get_iso_desc_actual_length(
   1514 						urb, i);
   1515 			DPRINTFN(1, "xfer=%p frame=%zu length=%d\n", xfer, i,
   1516 			    xfer->ux_frlengths[i]);
   1517 			xfer->ux_actlen += xfer->ux_frlengths[i];
   1518 		}
   1519 		DPRINTFN(1, "xfer=%p actlen=%d (isoc)\n", xfer, xfer->ux_actlen);
   1520 	}
   1521 
   1522 	if (xfertype == UE_ISOCHRONOUS && dbg_perio()) {
   1523 		for (size_t i = 0; i < xfer->ux_nframes; i++)
   1524 			dev_vdbg(hsotg->dev, " ISO Desc %zu status %d\n",
   1525 				 i, urb->iso_descs[i].status);
   1526 	}
   1527 
   1528 	if (!status) {
   1529 		if (!(xfer->ux_flags & USBD_SHORT_XFER_OK) &&
   1530 		    xfer->ux_actlen < xfer->ux_length)
   1531 			status = -EIO;
   1532 	}
   1533 
   1534 	switch (status) {
   1535 	case 0:
   1536 		xfer->ux_status = USBD_NORMAL_COMPLETION;
   1537 		break;
   1538 	case -EPIPE:
   1539 		xfer->ux_status = USBD_STALLED;
   1540 		break;
   1541 	case -ETIMEDOUT:
   1542 		xfer->ux_status = USBD_TIMEOUT;
   1543 		break;
   1544 	case -EPROTO:
   1545 		xfer->ux_status = USBD_INVAL;
   1546 		break;
   1547 	case -EIO:
   1548 		xfer->ux_status = USBD_IOERROR;
   1549 		break;
   1550 	case -EOVERFLOW:
   1551 		xfer->ux_status = USBD_IOERROR;
   1552 		break;
   1553 	default:
   1554 		xfer->ux_status = USBD_IOERROR;
   1555 		printf("%s: unknown error status %d\n", __func__, status);
   1556 	}
   1557 
   1558 	if (xfer->ux_status == USBD_NORMAL_COMPLETION) {
   1559 		/*
   1560 		 * control transfers with no data phase don't touch dmabuf, but
   1561 		 * everything else does.
   1562 		 */
   1563 		if (!(xfertype == UE_CONTROL &&
   1564 		    UGETW(xfer->ux_request.wLength) == 0) &&
   1565 		    xfer->ux_actlen > 0	/* XXX PR/53503 */
   1566 		    ) {
   1567 			int rd = usbd_xfer_isread(xfer);
   1568 
   1569 			usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_actlen,
   1570 			    rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1571 		}
   1572 	}
   1573 
   1574 	if (xfertype == UE_ISOCHRONOUS ||
   1575 	    xfertype == UE_INTERRUPT) {
   1576 		struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
   1577 
   1578 		dwc2_free_bus_bandwidth(hsotg,
   1579 					dwc2_hcd_get_ep_bandwidth(hsotg, dpipe),
   1580 					xfer);
   1581 	}
   1582 
   1583 	qtd->urb = NULL;
   1584 	KASSERT(mutex_owned(&hsotg->lock));
   1585 
   1586 	TAILQ_INSERT_TAIL(&sc->sc_complete, dxfer, xnext);
   1587 
   1588 	mutex_spin_exit(&hsotg->lock);
   1589 	usb_schedsoftintr(&sc->sc_bus);
   1590 	mutex_spin_enter(&hsotg->lock);
   1591 }
   1592 
   1593 
   1594 int
   1595 _dwc2_hcd_start(struct dwc2_hsotg *hsotg)
   1596 {
   1597 	dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
   1598 
   1599 	mutex_spin_enter(&hsotg->lock);
   1600 
   1601 	hsotg->lx_state = DWC2_L0;
   1602 
   1603 	if (dwc2_is_device_mode(hsotg)) {
   1604 		mutex_spin_exit(&hsotg->lock);
   1605 		return 0;	/* why 0 ?? */
   1606 	}
   1607 
   1608 	dwc2_hcd_reinit(hsotg);
   1609 
   1610 	mutex_spin_exit(&hsotg->lock);
   1611 	return 0;
   1612 }
   1613 
   1614 int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
   1615 {
   1616 
   1617 	return false;
   1618 }
   1619