xref: /openbsd-src/sys/dev/usb/dwc2/dwc2.c (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /*	$OpenBSD: dwc2.c,v 1.46 2017/06/29 17:36:16 deraadt Exp $	*/
2 /*	$NetBSD: dwc2.c,v 1.32 2014/09/02 23:26:20 macallan Exp $	*/
3 
4 /*-
5  * Copyright (c) 2013 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Nick Hudson
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #if 0
34 #include "opt_usb.h"
35 #endif
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.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/endian.h>
46 #if 0
47 #include <sys/cpu.h>
48 #endif
49 
50 #include <machine/bus.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdivar.h>
55 #include <dev/usb/usb_mem.h>
56 
57 #if 0
58 #include <dev/usb/usbroothub_subr.h>
59 #endif
60 
61 #include <dev/usb/dwc2/dwc2.h>
62 #include <dev/usb/dwc2/dwc2var.h>
63 
64 #include <dev/usb/dwc2/dwc2_core.h>
65 #include <dev/usb/dwc2/dwc2_hcd.h>
66 
67 #ifdef DWC2_COUNTERS
68 #define	DWC2_EVCNT_ADD(a,b)	((void)((a).ev_count += (b)))
69 #else
70 #define	DWC2_EVCNT_ADD(a,b)	do { } while (/*CONSTCOND*/0)
71 #endif
72 #define	DWC2_EVCNT_INCR(a)	DWC2_EVCNT_ADD((a), 1)
73 
74 #ifdef DWC2_DEBUG
75 #define	DPRINTFN(n,fmt,...) do {			\
76 	if (dwc2debug >= (n)) {			\
77 		printf("%s: " fmt,			\
78 		__FUNCTION__,## __VA_ARGS__);		\
79 	}						\
80 } while (0)
81 #define	DPRINTF(...)	DPRINTFN(1, __VA_ARGS__)
82 int dwc2debug = 0;
83 #else
84 #define	DPRINTF(...) do { } while (0)
85 #define	DPRINTFN(...) do { } while (0)
86 #endif
87 
88 STATIC usbd_status	dwc2_open(struct usbd_pipe *);
89 STATIC int		dwc2_setaddr(struct usbd_device *, int);
90 STATIC void		dwc2_poll(struct usbd_bus *);
91 STATIC void		dwc2_softintr(void *);
92 
93 #if 0
94 STATIC usbd_status	dwc2_allocm(struct usbd_bus *, struct usb_dma *, uint32_t);
95 STATIC void		dwc2_freem(struct usbd_bus *, struct usb_dma *);
96 #endif
97 
98 STATIC struct usbd_xfer	*dwc2_allocx(struct usbd_bus *);
99 STATIC void		dwc2_freex(struct usbd_bus *, struct usbd_xfer *);
100 #if 0
101 STATIC void		dwc2_get_lock(struct usbd_bus *, struct mutex **);
102 #endif
103 
104 STATIC usbd_status	dwc2_root_ctrl_transfer(struct usbd_xfer *);
105 STATIC usbd_status	dwc2_root_ctrl_start(struct usbd_xfer *);
106 STATIC void		dwc2_root_ctrl_abort(struct usbd_xfer *);
107 STATIC void		dwc2_root_ctrl_close(struct usbd_pipe *);
108 STATIC void		dwc2_root_ctrl_done(struct usbd_xfer *);
109 
110 STATIC usbd_status	dwc2_root_intr_transfer(struct usbd_xfer *);
111 STATIC usbd_status	dwc2_root_intr_start(struct usbd_xfer *);
112 STATIC void		dwc2_root_intr_abort(struct usbd_xfer *);
113 STATIC void		dwc2_root_intr_close(struct usbd_pipe *);
114 STATIC void		dwc2_root_intr_done(struct usbd_xfer *);
115 
116 STATIC usbd_status	dwc2_device_ctrl_transfer(struct usbd_xfer *);
117 STATIC usbd_status	dwc2_device_ctrl_start(struct usbd_xfer *);
118 STATIC void		dwc2_device_ctrl_abort(struct usbd_xfer *);
119 STATIC void		dwc2_device_ctrl_close(struct usbd_pipe *);
120 STATIC void		dwc2_device_ctrl_done(struct usbd_xfer *);
121 
122 STATIC usbd_status	dwc2_device_bulk_transfer(struct usbd_xfer *);
123 STATIC usbd_status	dwc2_device_bulk_start(struct usbd_xfer *);
124 STATIC void		dwc2_device_bulk_abort(struct usbd_xfer *);
125 STATIC void		dwc2_device_bulk_close(struct usbd_pipe *);
126 STATIC void		dwc2_device_bulk_done(struct usbd_xfer *);
127 
128 STATIC usbd_status	dwc2_device_intr_transfer(struct usbd_xfer *);
129 STATIC usbd_status	dwc2_device_intr_start(struct usbd_xfer *);
130 STATIC void		dwc2_device_intr_abort(struct usbd_xfer *);
131 STATIC void		dwc2_device_intr_close(struct usbd_pipe *);
132 STATIC void		dwc2_device_intr_done(struct usbd_xfer *);
133 
134 STATIC usbd_status	dwc2_device_isoc_transfer(struct usbd_xfer *);
135 STATIC usbd_status	dwc2_device_isoc_start(struct usbd_xfer *);
136 STATIC void		dwc2_device_isoc_abort(struct usbd_xfer *);
137 STATIC void		dwc2_device_isoc_close(struct usbd_pipe *);
138 STATIC void		dwc2_device_isoc_done(struct usbd_xfer *);
139 
140 STATIC usbd_status	dwc2_device_start(struct usbd_xfer *);
141 
142 STATIC void		dwc2_close_pipe(struct usbd_pipe *);
143 STATIC void		dwc2_abort_xfer(struct usbd_xfer *, usbd_status);
144 
145 STATIC void		dwc2_device_clear_toggle(struct usbd_pipe *);
146 STATIC void		dwc2_noop(struct usbd_pipe *pipe);
147 
148 STATIC int		dwc2_interrupt(struct dwc2_softc *);
149 STATIC void		dwc2_rhc(void *);
150 
151 STATIC void		dwc2_timeout(void *);
152 STATIC void		dwc2_timeout_task(void *);
153 
154 STATIC_INLINE void
155 dwc2_allocate_bus_bandwidth(struct dwc2_hsotg *hsotg, u16 bw,
156 			    struct usbd_xfer *xfer)
157 {
158 }
159 
160 STATIC_INLINE void
161 dwc2_free_bus_bandwidth(struct dwc2_hsotg *hsotg, u16 bw,
162 			struct usbd_xfer *xfer)
163 {
164 }
165 
166 #define DWC2_INTR_ENDPT 1
167 
168 STATIC struct usbd_bus_methods dwc2_bus_methods = {
169 	.open_pipe =	dwc2_open,
170 	.dev_setaddr =	dwc2_setaddr,
171 	.soft_intr =	dwc2_softintr,
172 	.do_poll =	dwc2_poll,
173 #if 0
174 	.allocm =	dwc2_allocm,
175 	.freem =	dwc2_freem,
176 #endif
177 	.allocx =	dwc2_allocx,
178 	.freex =	dwc2_freex,
179 #if 0
180 	.get_lock =	dwc2_get_lock,
181 #endif
182 };
183 
184 STATIC struct usbd_pipe_methods dwc2_root_ctrl_methods = {
185 	.transfer =	dwc2_root_ctrl_transfer,
186 	.start =	dwc2_root_ctrl_start,
187 	.abort =	dwc2_root_ctrl_abort,
188 	.close =	dwc2_root_ctrl_close,
189 	.cleartoggle =	dwc2_noop,
190 	.done =		dwc2_root_ctrl_done,
191 };
192 
193 STATIC struct usbd_pipe_methods dwc2_root_intr_methods = {
194 	.transfer =	dwc2_root_intr_transfer,
195 	.start =	dwc2_root_intr_start,
196 	.abort =	dwc2_root_intr_abort,
197 	.close =	dwc2_root_intr_close,
198 	.cleartoggle =	dwc2_noop,
199 	.done =		dwc2_root_intr_done,
200 };
201 
202 STATIC struct usbd_pipe_methods dwc2_device_ctrl_methods = {
203 	.transfer =	dwc2_device_ctrl_transfer,
204 	.start =	dwc2_device_ctrl_start,
205 	.abort =	dwc2_device_ctrl_abort,
206 	.close =	dwc2_device_ctrl_close,
207 	.cleartoggle =	dwc2_noop,
208 	.done =		dwc2_device_ctrl_done,
209 };
210 
211 STATIC struct usbd_pipe_methods dwc2_device_intr_methods = {
212 	.transfer =	dwc2_device_intr_transfer,
213 	.start =	dwc2_device_intr_start,
214 	.abort =	dwc2_device_intr_abort,
215 	.close =	dwc2_device_intr_close,
216 	.cleartoggle =	dwc2_device_clear_toggle,
217 	.done =		dwc2_device_intr_done,
218 };
219 
220 STATIC struct usbd_pipe_methods dwc2_device_bulk_methods = {
221 	.transfer =	dwc2_device_bulk_transfer,
222 	.start =	dwc2_device_bulk_start,
223 	.abort =	dwc2_device_bulk_abort,
224 	.close =	dwc2_device_bulk_close,
225 	.cleartoggle =	dwc2_device_clear_toggle,
226 	.done =		dwc2_device_bulk_done,
227 };
228 
229 STATIC struct usbd_pipe_methods dwc2_device_isoc_methods = {
230 	.transfer =	dwc2_device_isoc_transfer,
231 	.start =	dwc2_device_isoc_start,
232 	.abort =	dwc2_device_isoc_abort,
233 	.close =	dwc2_device_isoc_close,
234 	.cleartoggle =	dwc2_noop,
235 	.done =		dwc2_device_isoc_done,
236 };
237 
238 #if 0
239 STATIC usbd_status
240 dwc2_allocm(struct usbd_bus *bus, struct usb_dma *dma, uint32_t size)
241 {
242 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
243 	usbd_status status;
244 
245 	DPRINTFN(10, "\n");
246 
247 	status = usb_allocmem(&sc->sc_bus, size, 0, dma);
248 	if (status == USBD_NOMEM)
249 		status = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size);
250 	return status;
251 }
252 
253 STATIC void
254 dwc2_freem(struct usbd_bus *bus, struct usb_dma *dma)
255 {
256 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
257 
258 	DPRINTFN(10, "\n");
259 
260 	if (dma->block->flags & USB_DMA_RESERVE) {
261 		usb_reserve_freem(&sc->sc_dma_reserve, dma);
262 		return;
263 	}
264 	usb_freemem(&sc->sc_bus, dma);
265 }
266 #endif
267 
268 /*
269  * Work around the half configured control (default) pipe when setting
270  * the address of a device.
271  */
272 STATIC int
273 dwc2_setaddr(struct usbd_device *dev, int addr)
274 {
275 	if (usbd_set_address(dev, addr))
276 		return (1);
277 
278 	dev->address = addr;
279 
280 	/*
281 	 * Re-establish the default pipe with the new address and the
282 	 * new max packet size.
283 	 */
284 	dwc2_close_pipe(dev->default_pipe);
285 	if (dwc2_open(dev->default_pipe))
286 		return (EINVAL);
287 
288 	return (0);
289 }
290 
291 struct usbd_xfer *
292 dwc2_allocx(struct usbd_bus *bus)
293 {
294 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
295 	struct dwc2_xfer *dxfer;
296 
297 	DPRINTFN(10, "\n");
298 
299 	DWC2_EVCNT_INCR(sc->sc_ev_xferpoolget);
300 	dxfer = pool_get(&sc->sc_xferpool, PR_NOWAIT);
301 	if (dxfer != NULL) {
302 		memset(dxfer, 0, sizeof(*dxfer));
303 
304 		dxfer->urb = dwc2_hcd_urb_alloc(sc->sc_hsotg,
305 		    DWC2_MAXISOCPACKETS, GFP_KERNEL);
306 
307 #ifdef DWC2_DEBUG
308 		dxfer->xfer.busy_free = XFER_ONQU;
309 #endif
310 	}
311 	return (struct usbd_xfer *)dxfer;
312 }
313 
314 void
315 dwc2_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
316 {
317 	struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
318 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
319 
320 	DPRINTFN(10, "\n");
321 
322 #ifdef DWC2_DEBUG
323 	if (xfer->busy_free != XFER_ONQU) {
324 		DPRINTF("xfer=%p not busy, 0x%08x\n", xfer, xfer->busy_free);
325 	}
326 	xfer->busy_free = XFER_FREE;
327 #endif
328 	DWC2_EVCNT_INCR(sc->sc_ev_xferpoolput);
329 	dwc2_hcd_urb_free(sc->sc_hsotg, dxfer->urb, DWC2_MAXISOCPACKETS);
330 	pool_put(&sc->sc_xferpool, xfer);
331 }
332 
333 #if 0
334 STATIC void
335 dwc2_get_lock(struct usbd_bus *bus, struct mutex **lock)
336 {
337 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
338 
339 	*lock = &sc->sc_lock;
340 }
341 #endif
342 
343 STATIC void
344 dwc2_rhc(void *addr)
345 {
346 	struct dwc2_softc *sc = addr;
347 	struct usbd_xfer *xfer;
348 	u_char *p;
349 
350 	DPRINTF("\n");
351 	xfer = sc->sc_intrxfer;
352 
353 	if (xfer == NULL) {
354 		/* Just ignore the change. */
355 		return;
356 
357 	}
358 	/* set port bit */
359 	p = KERNADDR(&xfer->dmabuf, 0);
360 
361 	p[0] = 0x02;	/* we only have one port (1 << 1) */
362 
363 	xfer->actlen = xfer->length;
364 	xfer->status = USBD_NORMAL_COMPLETION;
365 
366 	usb_transfer_complete(xfer);
367 }
368 
369 STATIC void
370 dwc2_softintr(void *v)
371 {
372 	struct usbd_bus *bus = v;
373 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
374 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
375 	struct dwc2_xfer *dxfer;
376 
377 	mtx_enter(&hsotg->lock);
378 	while ((dxfer = TAILQ_FIRST(&sc->sc_complete)) != NULL) {
379 
380 		KASSERTMSG(!timeout_pending(&dxfer->xfer.timeout_handle),
381 		    "xfer %p pipe %p\n", dxfer, dxfer->xfer.pipe);
382 
383 		/*
384 		 * dwc2_abort_xfer will remove this transfer from the
385 		 * sc_complete queue
386 		 */
387 		/*XXXNH not tested */
388 		if (dxfer->flags & DWC2_XFER_ABORTING) {
389 			wakeup(&dxfer->flags);
390 			continue;
391 		}
392 
393 		TAILQ_REMOVE(&sc->sc_complete, dxfer, xnext);
394 
395 		mtx_leave(&hsotg->lock);
396 		usb_transfer_complete(&dxfer->xfer);
397 		mtx_enter(&hsotg->lock);
398 	}
399 	mtx_leave(&hsotg->lock);
400 }
401 
402 STATIC void
403 dwc2_timeout(void *addr)
404 {
405 	struct usbd_xfer *xfer = addr;
406  	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
407 
408 	DPRINTF("xfer=%p\n", xfer);
409 
410 	if (sc->sc_dying) {
411 		dwc2_timeout_task(addr);
412 		return;
413 	}
414 
415 	/* Execute the abort in a process context. */
416 	usb_init_task(&xfer->abort_task, dwc2_timeout_task, addr,
417 	    USB_TASK_TYPE_ABORT);
418 	usb_add_task(xfer->device, &xfer->abort_task);
419 }
420 
421 STATIC void
422 dwc2_timeout_task(void *addr)
423 {
424 	struct usbd_xfer *xfer = addr;
425 	int s;
426 
427 	DPRINTF("xfer=%p\n", xfer);
428 
429 	s = splusb();
430 	dwc2_abort_xfer(xfer, USBD_TIMEOUT);
431 	splx(s);
432 }
433 
434 usbd_status
435 dwc2_open(struct usbd_pipe *pipe)
436 {
437 	struct usbd_device *dev = pipe->device;
438 	struct dwc2_softc *sc = DWC2_PIPE2SC(pipe);
439 	struct dwc2_pipe *dpipe = DWC2_PIPE2DPIPE(pipe);
440 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
441 	uint8_t addr = dev->address;
442 	uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
443 	usbd_status err;
444 
445 	DPRINTF("pipe %p addr %d xfertype %d dir %s\n", pipe, addr, xfertype,
446 	    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in" : "out");
447 
448 	if (sc->sc_dying) {
449 		return USBD_IOERROR;
450 	}
451 
452 	if (addr == sc->sc_addr) {
453 		switch (ed->bEndpointAddress) {
454 		case USB_CONTROL_ENDPOINT:
455 			pipe->methods = &dwc2_root_ctrl_methods;
456 			break;
457 		case UE_DIR_IN | DWC2_INTR_ENDPT:
458 			pipe->methods = &dwc2_root_intr_methods;
459 			break;
460 		default:
461 			DPRINTF("bad bEndpointAddress 0x%02x\n",
462 			    ed->bEndpointAddress);
463 			return USBD_INVAL;
464 		}
465 		DPRINTF("root hub pipe open\n");
466 		return USBD_NORMAL_COMPLETION;
467 	}
468 
469 	switch (xfertype) {
470 	case UE_CONTROL:
471 		pipe->methods = &dwc2_device_ctrl_methods;
472 		err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
473 		    0, &dpipe->req_dma);
474 		if (err)
475 			return err;
476 		break;
477 	case UE_INTERRUPT:
478 		pipe->methods = &dwc2_device_intr_methods;
479 		break;
480 	case UE_ISOCHRONOUS:
481 		pipe->methods = &dwc2_device_isoc_methods;
482 		break;
483 	case UE_BULK:
484 		pipe->methods = &dwc2_device_bulk_methods;
485 		break;
486 	default:
487 		DPRINTF("bad xfer type %d\n", xfertype);
488 		return USBD_INVAL;
489 	}
490 
491 	dpipe->priv = NULL;	/* QH */
492 
493 	return USBD_NORMAL_COMPLETION;
494 }
495 
496 STATIC void
497 dwc2_poll(struct usbd_bus *bus)
498 {
499 	struct dwc2_softc *sc = DWC2_BUS2SC(bus);
500 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
501 
502 	mtx_enter(&hsotg->lock);
503 	dwc2_interrupt(sc);
504 	mtx_leave(&hsotg->lock);
505 }
506 
507 /*
508  * Close a reqular pipe.
509  * Assumes that there are no pending transactions.
510  */
511 STATIC void
512 dwc2_close_pipe(struct usbd_pipe *pipe)
513 {
514 	/* nothing */
515 }
516 
517 /*
518  * Abort a device request.
519  */
520 STATIC void
521 dwc2_abort_xfer(struct usbd_xfer *xfer, usbd_status status)
522 {
523 	struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
524 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
525 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
526 	struct dwc2_xfer *d, *tmp;
527 	bool wake;
528 	int err;
529 
530 	splsoftassert(IPL_SOFTUSB);
531 
532 	DPRINTF("xfer=%p\n", xfer);
533 
534 	if (sc->sc_dying) {
535 		xfer->status = status;
536 		timeout_del(&xfer->timeout_handle);
537 		usb_rem_task(xfer->device, &xfer->abort_task);
538 		usb_transfer_complete(xfer);
539 		return;
540 	}
541 
542 	/*
543 	 * If an abort is already in progress then just wait for it to
544 	 * complete and return.
545 	 */
546 	if (dxfer->flags & DWC2_XFER_ABORTING) {
547 		xfer->status = status;
548 		dxfer->flags |= DWC2_XFER_ABORTWAIT;
549 		while (dxfer->flags & DWC2_XFER_ABORTING)
550 			tsleep(&dxfer->flags, PZERO, "dwc2xfer", 0);
551 		return;
552 	}
553 
554 	mtx_enter(&hsotg->lock);
555 
556 	/* The transfer might have been completed already. */
557 	if (xfer->status != USBD_IN_PROGRESS) {
558 		DPRINTF("xfer=%p already completed\n", xfer);
559 		mtx_leave(&hsotg->lock);
560 		return;
561 	}
562 
563 	/*
564 	 * Step 1: Make the stack ignore it and stop the timeout.
565 	 */
566 	dxfer->flags |= DWC2_XFER_ABORTING;
567 
568 	xfer->status = status;	/* make software ignore it */
569 	timeout_del(&xfer->timeout_handle);
570 	usb_rem_task(xfer->device, &xfer->abort_task);
571 
572 	/* XXXNH suboptimal */
573 	TAILQ_FOREACH_SAFE(d, &sc->sc_complete, xnext, tmp) {
574 		if (d == dxfer) {
575 			TAILQ_REMOVE(&sc->sc_complete, dxfer, xnext);
576 		}
577 	}
578 
579 	err = dwc2_hcd_urb_dequeue(hsotg, dxfer->urb);
580 	if (err) {
581 		DPRINTF("dwc2_hcd_urb_dequeue failed\n");
582 	}
583 
584 	mtx_leave(&hsotg->lock);
585 
586 	/*
587 	 * Step 2: Execute callback.
588 	 */
589 	wake = dxfer->flags & DWC2_XFER_ABORTWAIT;
590 	dxfer->flags &= ~(DWC2_XFER_ABORTING | DWC2_XFER_ABORTWAIT);
591 
592 	usb_transfer_complete(xfer);
593 	if (wake) {
594 		wakeup(&dxfer->flags);
595 	}
596 }
597 
598 STATIC void
599 dwc2_noop(struct usbd_pipe *pipe)
600 {
601 
602 }
603 
604 STATIC void
605 dwc2_device_clear_toggle(struct usbd_pipe *pipe)
606 {
607 
608 	DPRINTF("toggle %d -> 0", pipe->endpoint->savedtoggle);
609 }
610 
611 /***********************************************************************/
612 
613 /*
614  * Data structures and routines to emulate the root hub.
615  */
616 
617 STATIC const usb_device_descriptor_t dwc2_devd = {
618 	.bLength = sizeof(usb_device_descriptor_t),
619 	.bDescriptorType = UDESC_DEVICE,
620 	.bcdUSB = {0x00, 0x02},
621 	.bDeviceClass = UDCLASS_HUB,
622 	.bDeviceSubClass = UDSUBCLASS_HUB,
623 	.bDeviceProtocol = UDPROTO_HSHUBSTT,
624 	.bMaxPacketSize = 64,
625 	.bcdDevice = {0x00, 0x01},
626 	.iManufacturer = 1,
627 	.iProduct = 2,
628 	.bNumConfigurations = 1,
629 };
630 
631 struct dwc2_config_desc {
632 	usb_config_descriptor_t confd;
633 	usb_interface_descriptor_t ifcd;
634 	usb_endpoint_descriptor_t endpd;
635 } __packed;
636 
637 STATIC const struct dwc2_config_desc dwc2_confd = {
638 	.confd = {
639 		.bLength = USB_CONFIG_DESCRIPTOR_SIZE,
640 		.bDescriptorType = UDESC_CONFIG,
641 		.wTotalLength[0] = sizeof(dwc2_confd),
642 		.bNumInterface = 1,
643 		.bConfigurationValue = 1,
644 		.iConfiguration = 0,
645 		.bmAttributes = UC_SELF_POWERED,
646 		.bMaxPower = 0,
647 	},
648 	.ifcd = {
649 		.bLength = USB_INTERFACE_DESCRIPTOR_SIZE,
650 		.bDescriptorType = UDESC_INTERFACE,
651 		.bInterfaceNumber = 0,
652 		.bAlternateSetting = 0,
653 		.bNumEndpoints = 1,
654 		.bInterfaceClass = UICLASS_HUB,
655 		.bInterfaceSubClass = UISUBCLASS_HUB,
656 		.bInterfaceProtocol = UIPROTO_HSHUBSTT,
657 		.iInterface = 0
658 	},
659 	.endpd = {
660 		.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE,
661 		.bDescriptorType = UDESC_ENDPOINT,
662 		.bEndpointAddress = UE_DIR_IN | DWC2_INTR_ENDPT,
663 		.bmAttributes = UE_INTERRUPT,
664 		.wMaxPacketSize = {8, 0},			/* max packet */
665 		.bInterval = 255,
666 	},
667 };
668 
669 #define	HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
670 #if 0
671 /* appears to be unused */
672 STATIC const usb_hub_descriptor_t dwc2_hubd = {
673 	.bDescLength = USB_HUB_DESCRIPTOR_SIZE,
674 	.bDescriptorType = UDESC_HUB,
675 	.bNbrPorts = 1,
676 	HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
677 	.bPwrOn2PwrGood = 50,
678 	.bHubContrCurrent = 0,
679 	.DeviceRemovable = {0},		/* port is removable */
680 };
681 #endif
682 
683 STATIC usbd_status
684 dwc2_root_ctrl_transfer(struct usbd_xfer *xfer)
685 {
686 	usbd_status err;
687 
688 	err = usb_insert_transfer(xfer);
689 	if (err)
690 		return err;
691 
692 	return dwc2_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
693 }
694 
695 STATIC usbd_status
696 dwc2_root_ctrl_start(struct usbd_xfer *xfer)
697 {
698 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
699 	usb_device_request_t *req;
700 	uint8_t *buf;
701 	uint16_t len;
702 	int value, index, l, s, totlen;
703 	usbd_status err = USBD_IOERROR;
704 
705 	if (sc->sc_dying)
706 		return USBD_IOERROR;
707 
708 	req = &xfer->request;
709 
710 	DPRINTFN(4, "type=0x%02x request=%02x\n",
711 	    req->bmRequestType, req->bRequest);
712 
713 	len = UGETW(req->wLength);
714 	value = UGETW(req->wValue);
715 	index = UGETW(req->wIndex);
716 
717 	buf = len ? KERNADDR(&xfer->dmabuf, 0) : NULL;
718 
719 	totlen = 0;
720 
721 #define C(x,y) ((x) | ((y) << 8))
722 	switch (C(req->bRequest, req->bmRequestType)) {
723 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
724 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
725 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
726 		/*
727 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
728 		 * for the integrated root hub.
729 		 */
730 		break;
731 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
732 		if (len > 0) {
733 			*buf = sc->sc_conf;
734 			totlen = 1;
735 		}
736 		break;
737 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
738 		DPRINTFN(8, "wValue=0x%04x\n", value);
739 
740 		if (len == 0)
741 			break;
742 		switch (value) {
743 		case C(0, UDESC_DEVICE):
744 			l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
745 // 			USETW(dwc2_devd.idVendor, sc->sc_id_vendor);
746 			memcpy(buf, &dwc2_devd, l);
747 			buf += l;
748 			len -= l;
749 			totlen += l;
750 
751 			break;
752 		case C(0, UDESC_CONFIG):
753 			l = min(len, sizeof(dwc2_confd));
754 			memcpy(buf, &dwc2_confd, l);
755 			buf += l;
756 			len -= l;
757 			totlen += l;
758 
759 			break;
760 #define sd ((usb_string_descriptor_t *)buf)
761 		case C(0, UDESC_STRING):
762 			totlen = usbd_str(sd, len, "\001");
763 			break;
764 		case C(1, UDESC_STRING):
765 			totlen = usbd_str(sd, len, sc->sc_vendor);
766 			break;
767 		case C(2, UDESC_STRING):
768 			totlen = usbd_str(sd, len, "DWC2 root hub");
769 			break;
770 #undef sd
771 		default:
772 			goto fail;
773 		}
774 		break;
775 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
776 		if (len > 0) {
777 			*buf = 0;
778 			totlen = 1;
779 		}
780 		break;
781 	case C(UR_GET_STATUS, UT_READ_DEVICE):
782 		if (len > 1) {
783 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
784 			totlen = 2;
785 		}
786 		break;
787 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
788 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
789 		if (len > 1) {
790 			USETW(((usb_status_t *)buf)->wStatus, 0);
791 			totlen = 2;
792 		}
793 		break;
794 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
795 		DPRINTF("UR_SET_ADDRESS, UT_WRITE_DEVICE: addr %d\n",
796 		    value);
797 		if (value >= USB_MAX_DEVICES)
798 			goto fail;
799 
800 		sc->sc_addr = value;
801 		break;
802 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
803 		if (value != 0 && value != 1)
804 			goto fail;
805 
806 		sc->sc_conf = value;
807 		break;
808 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
809 		break;
810 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
811 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
812 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
813 		err = USBD_IOERROR;
814 		goto fail;
815 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
816 		break;
817 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
818 		break;
819 	default:
820 		/* Hub requests - XXXNH len check? */
821 		err = dwc2_hcd_hub_control(sc->sc_hsotg,
822 		    C(req->bRequest, req->bmRequestType), value, index,
823 		    buf, len);
824 		if (err) {
825 			err = USBD_IOERROR;
826 			goto fail;
827 		}
828 		totlen = len;
829 	}
830 	xfer->actlen = totlen;
831 	err = USBD_NORMAL_COMPLETION;
832 
833 fail:
834 	s = splusb();
835 	xfer->status = err;
836 	usb_transfer_complete(xfer);
837 	splx(s);
838 
839 	return err;
840 }
841 
842 STATIC void
843 dwc2_root_ctrl_abort(struct usbd_xfer *xfer)
844 {
845 	DPRINTFN(10, "\n");
846 
847 	/* Nothing to do, all transfers are synchronous. */
848 }
849 
850 STATIC void
851 dwc2_root_ctrl_close(struct usbd_pipe *pipe)
852 {
853 	DPRINTFN(10, "\n");
854 
855 	/* Nothing to do. */
856 }
857 
858 STATIC void
859 dwc2_root_ctrl_done(struct usbd_xfer *xfer)
860 {
861 	DPRINTFN(10, "\n");
862 
863 	/* Nothing to do. */
864 }
865 
866 STATIC usbd_status
867 dwc2_root_intr_transfer(struct usbd_xfer *xfer)
868 {
869 	usbd_status err;
870 
871 	DPRINTF("\n");
872 
873 	/* Insert last in queue. */
874 	err = usb_insert_transfer(xfer);
875 	if (err)
876 		return err;
877 
878 	/* Pipe isn't running, start first */
879 	return dwc2_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
880 }
881 
882 STATIC usbd_status
883 dwc2_root_intr_start(struct usbd_xfer *xfer)
884 {
885 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
886 
887 	DPRINTF("\n");
888 
889 	if (sc->sc_dying)
890 		return USBD_IOERROR;
891 
892 	KASSERT(sc->sc_intrxfer == NULL);
893 	sc->sc_intrxfer = xfer;
894 
895 	return USBD_IN_PROGRESS;
896 }
897 
898 /* Abort a root interrupt request. */
899 STATIC void
900 dwc2_root_intr_abort(struct usbd_xfer *xfer)
901 {
902 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
903 
904 	DPRINTF("xfer=%p\n", xfer);
905 
906 	KASSERT(xfer->pipe->intrxfer == xfer);
907 
908 	sc->sc_intrxfer = NULL;
909 
910 	xfer->status = USBD_CANCELLED;
911 	usb_transfer_complete(xfer);
912 }
913 
914 STATIC void
915 dwc2_root_intr_close(struct usbd_pipe *pipe)
916 {
917 	struct dwc2_softc *sc = DWC2_PIPE2SC(pipe);
918 
919 	DPRINTF("\n");
920 
921 	sc->sc_intrxfer = NULL;
922 }
923 
924 STATIC void
925 dwc2_root_intr_done(struct usbd_xfer *xfer)
926 {
927 
928 	DPRINTF("\n");
929 }
930 
931 /***********************************************************************/
932 
933 STATIC usbd_status
934 dwc2_device_ctrl_transfer(struct usbd_xfer *xfer)
935 {
936 	usbd_status err;
937 
938 	DPRINTF("\n");
939 
940 	/* Insert last in queue. */
941 	err = usb_insert_transfer(xfer);
942 	if (err)
943 		return err;
944 
945 	/* Pipe isn't running, start first */
946 	return dwc2_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
947 }
948 
949 STATIC usbd_status
950 dwc2_device_ctrl_start(struct usbd_xfer *xfer)
951 {
952 	usbd_status err;
953 
954 	DPRINTF("\n");
955 
956 	xfer->status = USBD_IN_PROGRESS;
957 	err = dwc2_device_start(xfer);
958 
959 	return err;
960 }
961 
962 STATIC void
963 dwc2_device_ctrl_abort(struct usbd_xfer *xfer)
964 {
965 	DPRINTF("xfer=%p\n", xfer);
966 	dwc2_abort_xfer(xfer, USBD_CANCELLED);
967 }
968 
969 STATIC void
970 dwc2_device_ctrl_close(struct usbd_pipe *pipe)
971 {
972 
973 	DPRINTF("pipe=%p\n", pipe);
974 	dwc2_close_pipe(pipe);
975 }
976 
977 STATIC void
978 dwc2_device_ctrl_done(struct usbd_xfer *xfer)
979 {
980 
981 	DPRINTF("xfer=%p\n", xfer);
982 }
983 
984 /***********************************************************************/
985 
986 STATIC usbd_status
987 dwc2_device_bulk_transfer(struct usbd_xfer *xfer)
988 {
989 	usbd_status err;
990 
991 	DPRINTF("xfer=%p\n", xfer);
992 
993 	/* Insert last in queue. */
994 	err = usb_insert_transfer(xfer);
995 	if (err)
996 		return err;
997 
998 	/* Pipe isn't running, start first */
999 	return dwc2_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1000 }
1001 
1002 STATIC usbd_status
1003 dwc2_device_bulk_start(struct usbd_xfer *xfer)
1004 {
1005 	usbd_status err;
1006 
1007 	DPRINTF("xfer=%p\n", xfer);
1008 
1009 	xfer->status = USBD_IN_PROGRESS;
1010 	err = dwc2_device_start(xfer);
1011 
1012 	return err;
1013 }
1014 
1015 STATIC void
1016 dwc2_device_bulk_abort(struct usbd_xfer *xfer)
1017 {
1018 	DPRINTF("xfer=%p\n", xfer);
1019 
1020 	dwc2_abort_xfer(xfer, USBD_CANCELLED);
1021 }
1022 
1023 STATIC void
1024 dwc2_device_bulk_close(struct usbd_pipe *pipe)
1025 {
1026 
1027 	DPRINTF("pipe=%p\n", pipe);
1028 
1029 	dwc2_close_pipe(pipe);
1030 }
1031 
1032 STATIC void
1033 dwc2_device_bulk_done(struct usbd_xfer *xfer)
1034 {
1035 
1036 	DPRINTF("xfer=%p\n", xfer);
1037 }
1038 
1039 /***********************************************************************/
1040 
1041 STATIC usbd_status
1042 dwc2_device_intr_transfer(struct usbd_xfer *xfer)
1043 {
1044 	usbd_status err;
1045 
1046 	DPRINTF("xfer=%p\n", xfer);
1047 
1048 	/* Insert last in queue. */
1049 	err = usb_insert_transfer(xfer);
1050 	if (err)
1051 		return err;
1052 
1053 	/* Pipe isn't running, start first */
1054 	return dwc2_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1055 }
1056 
1057 STATIC usbd_status
1058 dwc2_device_intr_start(struct usbd_xfer *xfer)
1059 {
1060 	usbd_status err;
1061 
1062 	xfer->status = USBD_IN_PROGRESS;
1063 	err = dwc2_device_start(xfer);
1064 
1065 	return err;
1066 }
1067 
1068 /* Abort a device interrupt request. */
1069 STATIC void
1070 dwc2_device_intr_abort(struct usbd_xfer *xfer)
1071 {
1072 	KASSERT(xfer->pipe->intrxfer == xfer);
1073 
1074 	DPRINTF("xfer=%p\n", xfer);
1075 
1076 	dwc2_abort_xfer(xfer, USBD_CANCELLED);
1077 }
1078 
1079 STATIC void
1080 dwc2_device_intr_close(struct usbd_pipe *pipe)
1081 {
1082 
1083 	DPRINTF("pipe=%p\n", pipe);
1084 
1085 	dwc2_close_pipe(pipe);
1086 }
1087 
1088 STATIC void
1089 dwc2_device_intr_done(struct usbd_xfer *xfer)
1090 {
1091 
1092 	DPRINTF("\n");
1093 
1094 	if (xfer->pipe->repeat) {
1095 		xfer->status = USBD_IN_PROGRESS;
1096 		dwc2_device_start(xfer);
1097 	}
1098 }
1099 
1100 /***********************************************************************/
1101 
1102 usbd_status
1103 dwc2_device_isoc_transfer(struct usbd_xfer *xfer)
1104 {
1105 	usbd_status err;
1106 
1107 	DPRINTF("xfer=%p\n", xfer);
1108 
1109 	/* Insert last in queue. */
1110 	err = usb_insert_transfer(xfer);
1111 	if (err)
1112 		return err;
1113 
1114 	/* Pipe isn't running, start first */
1115 	return dwc2_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1116 }
1117 
1118 usbd_status
1119 dwc2_device_isoc_start(struct usbd_xfer *xfer)
1120 {
1121 	struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1122 	struct dwc2_softc *sc = DWC2_DPIPE2SC(dpipe);
1123 	usbd_status err;
1124 
1125 	/* Why would you do that anyway? */
1126 	if (sc->sc_bus.use_polling)
1127 		return (USBD_INVAL);
1128 
1129 	xfer->status = USBD_IN_PROGRESS;
1130 	err = dwc2_device_start(xfer);
1131 
1132 	return err;
1133 }
1134 
1135 void
1136 dwc2_device_isoc_abort(struct usbd_xfer *xfer)
1137 {
1138 	DPRINTF("xfer=%p\n", xfer);
1139 
1140 	dwc2_abort_xfer(xfer, USBD_CANCELLED);
1141 }
1142 
1143 void
1144 dwc2_device_isoc_close(struct usbd_pipe *pipe)
1145 {
1146 	DPRINTF("\n");
1147 
1148 	dwc2_close_pipe(pipe);
1149 }
1150 
1151 void
1152 dwc2_device_isoc_done(struct usbd_xfer *xfer)
1153 {
1154 
1155 	DPRINTF("\n");
1156 }
1157 
1158 
1159 usbd_status
1160 dwc2_device_start(struct usbd_xfer *xfer)
1161 {
1162  	struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
1163 	struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1164 	struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1165 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
1166 	struct dwc2_hcd_urb *dwc2_urb;
1167 
1168 	struct usbd_device *dev = xfer->pipe->device;
1169 	usb_endpoint_descriptor_t *ed = xfer->pipe->endpoint->edesc;
1170 	uint8_t addr = dev->address;
1171 	uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1172 	uint8_t epnum = UE_GET_ADDR(ed->bEndpointAddress);
1173 	uint8_t dir = UE_GET_DIR(ed->bEndpointAddress);
1174 	uint16_t mps = UE_GET_SIZE(UGETW(ed->wMaxPacketSize));
1175 	uint32_t len;
1176 
1177 	uint32_t flags = 0;
1178 	uint32_t off = 0;
1179 	int retval, err = USBD_IN_PROGRESS;
1180 	int alloc_bandwidth = 0;
1181 	int i;
1182 
1183 	DPRINTFN(1, "xfer=%p pipe=%p\n", xfer, xfer->pipe);
1184 
1185 	if (xfertype == UE_ISOCHRONOUS ||
1186 	    xfertype == UE_INTERRUPT) {
1187 		mtx_enter(&hsotg->lock);
1188 		if (!dwc2_hcd_is_bandwidth_allocated(hsotg, xfer))
1189 			alloc_bandwidth = 1;
1190 		mtx_leave(&hsotg->lock);
1191 	}
1192 
1193 	/*
1194 	 * For Control pipe the direction is from the request, all other
1195 	 * transfers have been set correctly at pipe open time.
1196 	 */
1197 	if (xfertype == UE_CONTROL) {
1198 		usb_device_request_t *req = &xfer->request;
1199 
1200 		DPRINTFN(3, "xfer=%p type=0x%02x request=0x%02x wValue=0x%04x "
1201 		    "wIndex=0x%04x len=%d addr=%d endpt=%d dir=%s speed=%d "
1202 		    "mps=%d\n",
1203 		    xfer, req->bmRequestType, req->bRequest, UGETW(req->wValue),
1204 		    UGETW(req->wIndex), UGETW(req->wLength), dev->address,
1205 		    epnum, dir == UT_READ ? "in" :"out", dev->speed, mps);
1206 
1207 		/* Copy request packet to our DMA buffer */
1208 		memcpy(KERNADDR(&dpipe->req_dma, 0), req, sizeof(*req));
1209 		usb_syncmem(&dpipe->req_dma, 0, sizeof(*req),
1210 			    BUS_DMASYNC_PREWRITE);
1211 		len = UGETW(req->wLength);
1212 		if ((req->bmRequestType & UT_READ) == UT_READ) {
1213 			dir = UE_DIR_IN;
1214 		} else {
1215 			dir = UE_DIR_OUT;
1216 		}
1217 
1218 		DPRINTFN(3, "req = %p dma = %llx len %d dir %s\n",
1219 		    KERNADDR(&dpipe->req_dma, 0),
1220 		    (long long)DMAADDR(&dpipe->req_dma, 0),
1221 		    len, dir == UE_DIR_IN ? "in" : "out");
1222 	} else {
1223 		DPRINTFN(3, "xfer=%p len=%d flags=%d addr=%d endpt=%d,"
1224 		    " mps=%d dir %s\n", xfer, xfer->length, xfer->flags, addr,
1225 		    epnum, mps, dir == UT_READ ? "in" :"out");
1226 
1227 		len = xfer->length;
1228 	}
1229 
1230 	dwc2_urb = dxfer->urb;
1231 	if (!dwc2_urb)
1232 		return USBD_NOMEM;
1233 
1234 	memset(dwc2_urb, 0, sizeof(*dwc2_urb) +
1235 	    sizeof(dwc2_urb->iso_descs[0]) * DWC2_MAXISOCPACKETS);
1236 
1237 	dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, addr, epnum, xfertype, dir,
1238 				  mps);
1239 
1240 	if (xfertype == UE_CONTROL) {
1241 		dwc2_urb->setup_usbdma = &dpipe->req_dma;
1242 		dwc2_urb->setup_packet = KERNADDR(&dpipe->req_dma, 0);
1243 		dwc2_urb->setup_dma = DMAADDR(&dpipe->req_dma, 0);
1244 	} else {
1245 		/* XXXNH - % mps required? */
1246 		if ((xfer->flags & USBD_FORCE_SHORT_XFER) && (len % mps) == 0)
1247 			flags |= URB_SEND_ZERO_PACKET;
1248 	}
1249 	flags |= URB_GIVEBACK_ASAP;
1250 
1251 	/*
1252 	 * control transfers with no data phase don't touch usbdma, but
1253 	 * everything else does.
1254 	 */
1255 	if (!(xfertype == UE_CONTROL && len == 0)) {
1256 		dwc2_urb->usbdma = &xfer->dmabuf;
1257 		dwc2_urb->buf = KERNADDR(dwc2_urb->usbdma, 0);
1258 		dwc2_urb->dma = DMAADDR(dwc2_urb->usbdma, 0);
1259  	}
1260 	dwc2_urb->length = len;
1261  	dwc2_urb->flags = flags;
1262 	dwc2_urb->status = -EINPROGRESS;
1263 	dwc2_urb->packet_count = xfer->nframes;
1264 
1265 	if (xfertype == UE_INTERRUPT ||
1266 	    xfertype == UE_ISOCHRONOUS) {
1267 		uint16_t ival;
1268 
1269 		if (xfertype == UE_INTERRUPT &&
1270 		    dpipe->pipe.interval != USBD_DEFAULT_INTERVAL) {
1271 			ival = dpipe->pipe.interval;
1272 		} else {
1273 			ival = ed->bInterval;
1274 		}
1275 
1276 		if (ival < 1) {
1277 			retval = -ENODEV;
1278 			goto fail;
1279 		}
1280 		if (dev->speed == USB_SPEED_HIGH ||
1281 		   (dev->speed == USB_SPEED_FULL && xfertype == UE_ISOCHRONOUS)) {
1282 			if (ival > 16) {
1283 				/*
1284 				 * illegal with HS/FS, but there were
1285 				 * documentation bugs in the spec
1286 				 */
1287 				ival = 256;
1288 			} else {
1289 				ival = (1 << (ival - 1));
1290 			}
1291 		} else {
1292 			if (xfertype == UE_INTERRUPT && ival < 10)
1293 				ival = 10;
1294 		}
1295 		dwc2_urb->interval = ival;
1296 	}
1297 
1298 	xfer->actlen = 0;
1299 
1300 	KASSERT(xfertype != UE_ISOCHRONOUS ||
1301 	    xfer->nframes < DWC2_MAXISOCPACKETS);
1302 	KASSERTMSG(xfer->nframes == 0 || xfertype == UE_ISOCHRONOUS,
1303 	    "nframes %d xfertype %d\n", xfer->nframes, xfertype);
1304 
1305 	for (off = i = 0; i < xfer->nframes; ++i) {
1306 		DPRINTFN(3, "xfer=%p frame=%d offset=%d length=%d\n", xfer, i,
1307 		    off, xfer->frlengths[i]);
1308 
1309 		dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i, off,
1310 		    xfer->frlengths[i]);
1311 		off += xfer->frlengths[i];
1312 	}
1313 
1314 	/* might need to check cpu_intr_p */
1315 	mtx_enter(&hsotg->lock);
1316 
1317 	if (xfer->timeout && !sc->sc_bus.use_polling) {
1318 		timeout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
1319 		    dwc2_timeout, xfer);
1320 	}
1321 
1322 	dwc2_urb->priv = xfer;
1323 	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, &dpipe->priv, 0);
1324 	if (retval)
1325 		goto fail;
1326 
1327 	if (alloc_bandwidth) {
1328 		dwc2_allocate_bus_bandwidth(hsotg,
1329 				dwc2_hcd_get_ep_bandwidth(hsotg, dpipe),
1330 				xfer);
1331 	}
1332 
1333 fail:
1334 	mtx_leave(&hsotg->lock);
1335 
1336 	switch (retval) {
1337 	case 0:
1338 		break;
1339 	case -ENODEV:
1340 		err = USBD_INVAL;
1341 		break;
1342 	case -ENOMEM:
1343 		err = USBD_NOMEM;
1344 		break;
1345 	default:
1346 		err = USBD_IOERROR;
1347 	}
1348 
1349 	return err;
1350 
1351 }
1352 
1353 void
1354 dwc2_worker(struct task *wk, void *priv)
1355 {
1356 	struct dwc2_softc *sc = priv;
1357 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
1358 
1359 /* db_enter(); */
1360 #if 0
1361 	struct usbd_xfer *xfer = dwork->xfer;
1362 	struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
1363 
1364 	dwc2_hcd_endpoint_disable(sc->dwc_dev.hcd, dpipe->priv, 250);
1365 	dwc_free(NULL, dpipe->urb);
1366 #endif
1367 
1368 	if (wk == &hsotg->wf_otg) {
1369 		dwc2_conn_id_status_change(wk);
1370 	} else if (wk == &hsotg->start_work.work) {
1371 		dwc2_hcd_start_func(wk);
1372 	} else if (wk == &hsotg->reset_work.work) {
1373 		dwc2_hcd_reset_func(wk);
1374 	} else {
1375 #if 0
1376 		KASSERT(dwork->xfer != NULL);
1377 		KASSERT(dxfer->queued == true);
1378 
1379 		if (!(dxfer->flags & DWC2_XFER_ABORTING)) {
1380 			dwc2_start_standard_chain(xfer);
1381 		}
1382 		dxfer->queued = false;
1383 		wakeup(&dxfer->flags);
1384 #endif
1385 	}
1386 }
1387 
1388 int
1389 dwc2_intr(void *p)
1390 {
1391 	struct dwc2_softc *sc = p;
1392 	struct dwc2_hsotg *hsotg = sc->sc_hsotg;
1393 	int ret = 0;
1394 
1395 	mtx_enter(&hsotg->lock);
1396 
1397 	if (sc->sc_dying)
1398 		goto done;
1399 
1400 	if (sc->sc_bus.use_polling) {
1401 		uint32_t intrs;
1402 
1403 		intrs = dwc2_read_core_intr(hsotg);
1404 		DWC2_WRITE_4(hsotg, GINTSTS, intrs);
1405 	} else {
1406 		ret = dwc2_interrupt(sc);
1407 	}
1408 
1409 done:
1410 	mtx_leave(&hsotg->lock);
1411 
1412 	return ret;
1413 }
1414 
1415 int
1416 dwc2_interrupt(struct dwc2_softc *sc)
1417 {
1418 	int ret = 0;
1419 
1420 	if (sc->sc_hcdenabled) {
1421 		ret |= dwc2_handle_hcd_intr(sc->sc_hsotg);
1422 	}
1423 
1424 	ret |= dwc2_handle_common_intr(sc->sc_hsotg);
1425 
1426 	return ret;
1427 }
1428 
1429 /***********************************************************************/
1430 
1431 int
1432 dwc2_detach(struct dwc2_softc *sc, int flags)
1433 {
1434 	int rv = 0;
1435 
1436 	if (sc->sc_child != NULL)
1437 		rv = config_detach(sc->sc_child, flags);
1438 
1439 	return rv;
1440 }
1441 
1442 /***********************************************************************/
1443 int
1444 dwc2_init(struct dwc2_softc *sc)
1445 {
1446 	int err = 0;
1447 
1448 	sc->sc_bus.usbrev = USBREV_2_0;
1449 	sc->sc_bus.methods = &dwc2_bus_methods;
1450 	sc->sc_bus.pipe_size = sizeof(struct dwc2_pipe);
1451 	sc->sc_hcdenabled = false;
1452 
1453 	TAILQ_INIT(&sc->sc_complete);
1454 
1455 	sc->sc_rhc_si = softintr_establish(IPL_SOFTUSB, dwc2_rhc, sc);
1456 
1457 #if 0
1458 	usb_setup_reserve(&sc->sc_bus, &sc->sc_dma_reserve, sc->sc_bus.dmatag,
1459 	    USB_MEM_RESERVE);
1460 #endif
1461 
1462 	pool_init(&sc->sc_xferpool, sizeof(struct dwc2_xfer), 0, IPL_USB, 0,
1463 	    "dwc2xfer", NULL);
1464 	pool_init(&sc->sc_qhpool, sizeof(struct dwc2_qh), 0, IPL_USB, 0,
1465 	    "dwc2qh", NULL);
1466 	pool_init(&sc->sc_qtdpool, sizeof(struct dwc2_qtd), 0, IPL_USB, 0,
1467 	    "dwc2qtd", NULL);
1468 
1469 	sc->sc_hsotg = malloc(sizeof(struct dwc2_hsotg), M_DEVBUF,
1470 	    M_ZERO | M_WAITOK);
1471 
1472 	sc->sc_hsotg->hsotg_sc = sc;
1473 	sc->sc_hsotg->dev = &sc->sc_bus.bdev;
1474 	sc->sc_hcdenabled = true;
1475 
1476 	err = dwc2_hcd_init(sc->sc_hsotg, sc->sc_params);
1477 	if (err) {
1478 		err = -err;
1479 		goto fail;
1480 	}
1481 
1482 	return 0;
1483 
1484 fail:
1485 	free(sc->sc_hsotg, M_DEVBUF, sizeof(struct dwc2_hsotg));
1486 	softintr_disestablish(sc->sc_rhc_si);
1487 
1488 	return err;
1489 }
1490 
1491 int
1492 dwc2_dma_config(struct dwc2_softc *sc, struct dwc2_core_dma_config *config)
1493 {
1494 	sc->sc_dma_config = config;
1495 	return dwc2_hcd_dma_config(sc->sc_hsotg, sc->sc_dma_config);
1496 }
1497 
1498 #if 0
1499 /*
1500  * curmode is a mode indication bit 0 = device, 1 = host
1501  */
1502 STATIC const char * const intnames[32] = {
1503 	"curmode",	"modemis",	"otgint",	"sof",
1504 	"rxflvl",	"nptxfemp",	"ginnakeff",	"goutnakeff",
1505 	"ulpickint",	"i2cint",	"erlysusp",	"usbsusp",
1506 	"usbrst",	"enumdone",	"isooutdrop",	"eopf",
1507 	"restore_done",	"epmis",	"iepint",	"oepint",
1508 	"incompisoin",	"incomplp",	"fetsusp",	"resetdet",
1509 	"prtint",	"hchint",	"ptxfemp",	"lpm",
1510 	"conidstschng",	"disconnint",	"sessreqint",	"wkupint"
1511 };
1512 
1513 
1514 /***********************************************************************/
1515 
1516 #endif
1517 
1518 
1519 void
1520 dw_timeout(void *arg)
1521 {
1522 	struct delayed_work *dw = arg;
1523 
1524 	task_set(&dw->work, dw->dw_fn, dw->dw_arg);
1525 	task_add(dw->dw_wq, &dw->work);
1526 }
1527 
1528 void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, int *hub_addr,
1529 			int *hub_port)
1530 {
1531 	struct usbd_xfer *xfer = context;
1532 	struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1533 	struct usbd_device *dev = dpipe->pipe.device;
1534 
1535 	if (dev->myhsport != NULL) {
1536 		*hub_addr = dev->myhsport->parent->address;
1537  		*hub_port = dev->myhsport->portno;
1538 	}
1539 }
1540 
1541 int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
1542 {
1543 	struct usbd_xfer *xfer = context;
1544 	struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1545 	struct usbd_device *dev = dpipe->pipe.device;
1546 
1547 	return dev->speed;
1548 }
1549 
1550 /*
1551  * Sets the final status of an URB and returns it to the upper layer. Any
1552  * required cleanup of the URB is performed.
1553  *
1554  * Must be called with interrupt disabled and spinlock held
1555  */
1556 void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
1557 			int status)
1558 {
1559 	struct usbd_xfer *xfer;
1560 	struct dwc2_xfer *dxfer;
1561 	struct dwc2_softc *sc;
1562 	usb_endpoint_descriptor_t *ed;
1563 	uint8_t xfertype;
1564 
1565 	if (!qtd) {
1566 		dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
1567 		return;
1568 	}
1569 
1570 	if (!qtd->urb) {
1571 		dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
1572 		return;
1573 	}
1574 
1575 	xfer = qtd->urb->priv;
1576 	if (!xfer) {
1577 		dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
1578 		return;
1579 	}
1580 
1581 	dxfer = DWC2_XFER2DXFER(xfer);
1582 	sc = DWC2_XFER2SC(xfer);
1583 	ed = xfer->pipe->endpoint->edesc;
1584 	xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1585 
1586 	xfer->actlen = dwc2_hcd_urb_get_actual_length(qtd->urb);
1587 
1588 	DPRINTFN(3, "xfer=%p actlen=%d\n", xfer, xfer->actlen);
1589 
1590 	if (xfertype == UE_ISOCHRONOUS && dbg_perio()) {
1591 		int i;
1592 
1593 		for (i = 0; i < xfer->nframes; i++)
1594 			dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
1595 				 i, qtd->urb->iso_descs[i].status);
1596 	}
1597 
1598 	if (xfertype == UE_ISOCHRONOUS) {
1599 		int i;
1600 
1601 		xfer->actlen = 0;
1602 		for (i = 0; i < xfer->nframes; ++i) {
1603 			xfer->frlengths[i] =
1604 				dwc2_hcd_urb_get_iso_desc_actual_length(
1605 						qtd->urb, i);
1606 			xfer->actlen += xfer->frlengths[i];
1607 		}
1608 	}
1609 
1610 	if (!status) {
1611 		if (!(xfer->flags & USBD_SHORT_XFER_OK) &&
1612 		    xfer->actlen < xfer->length)
1613 			status = -EIO;
1614 	}
1615 
1616 	switch (status) {
1617 	case 0:
1618 		xfer->status = USBD_NORMAL_COMPLETION;
1619 		break;
1620 	case -EPIPE:
1621 		xfer->status = USBD_STALLED;
1622 		break;
1623 	case -ETIMEDOUT:
1624 		xfer->status = USBD_TIMEOUT;
1625 		break;
1626 	case -EPROTO:
1627 		xfer->status = USBD_INVAL;
1628 		break;
1629 	case -EIO:
1630 		xfer->status = USBD_IOERROR;
1631 		break;
1632 	case -EOVERFLOW:
1633 		xfer->status = USBD_IOERROR;
1634 		break;
1635 	default:
1636 		printf("%s: unknown error status %d\n", __func__, status);
1637 	}
1638 
1639 	if (xfertype == UE_ISOCHRONOUS ||
1640 	    xfertype == UE_INTERRUPT) {
1641 		struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1642 
1643 		dwc2_free_bus_bandwidth(hsotg,
1644 					dwc2_hcd_get_ep_bandwidth(hsotg, dpipe),
1645 					xfer);
1646 	}
1647 
1648 	qtd->urb = NULL;
1649 	timeout_del(&xfer->timeout_handle);
1650 	usb_rem_task(xfer->device, &xfer->abort_task);
1651 
1652 	MUTEX_ASSERT_LOCKED(&hsotg->lock);
1653 
1654 	TAILQ_INSERT_TAIL(&sc->sc_complete, dxfer, xnext);
1655 
1656 	mtx_leave(&hsotg->lock);
1657 	usb_schedsoftintr(&sc->sc_bus);
1658 	mtx_enter(&hsotg->lock);
1659 }
1660 
1661 
1662 int
1663 _dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1664 {
1665 	dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
1666 
1667 	mtx_enter(&hsotg->lock);
1668 
1669 	hsotg->op_state = OTG_STATE_A_HOST;
1670 
1671 	dwc2_hcd_reinit(hsotg);
1672 
1673 	/* Try to enable port. */
1674 	dwc2_handle_hcd_intr(hsotg);
1675 
1676 	/*XXXNH*/
1677 	delay(50);
1678 
1679 	mtx_leave(&hsotg->lock);
1680 	return 0;
1681 }
1682 
1683 int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
1684 {
1685 
1686 	return false;
1687 }
1688