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