xref: /freebsd/sys/dev/usb/controller/dwc_otg.c (revision 0572ccaa4543b0abef8ef81e384c1d04de9f3da1)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2012 Hans Petter Selasky. All rights reserved.
4  * Copyright (c) 2010-2011 Aleksandr Rybalko. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * This file contains the driver for the DesignWare series USB 2.0 OTG
30  * Controller.
31  */
32 
33 /*
34  * LIMITATION: Drivers must be bound to all OUT endpoints in the
35  * active configuration for this driver to work properly. Blocking any
36  * OUT endpoint will block all OUT endpoints including the control
37  * endpoint. Usually this is not a problem.
38  */
39 
40 /*
41  * NOTE: Writing to non-existing registers appears to cause an
42  * internal reset.
43  */
44 
45 #ifdef USB_GLOBAL_INCLUDE_FILE
46 #include USB_GLOBAL_INCLUDE_FILE
47 #else
48 #include <sys/stdint.h>
49 #include <sys/stddef.h>
50 #include <sys/param.h>
51 #include <sys/queue.h>
52 #include <sys/types.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/bus.h>
56 #include <sys/module.h>
57 #include <sys/lock.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/sysctl.h>
61 #include <sys/sx.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
65 #include <sys/priv.h>
66 
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 
70 #define	USB_DEBUG_VAR dwc_otg_debug
71 
72 #include <dev/usb/usb_core.h>
73 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/usb_busdma.h>
75 #include <dev/usb/usb_process.h>
76 #include <dev/usb/usb_transfer.h>
77 #include <dev/usb/usb_device.h>
78 #include <dev/usb/usb_hub.h>
79 #include <dev/usb/usb_util.h>
80 
81 #include <dev/usb/usb_controller.h>
82 #include <dev/usb/usb_bus.h>
83 #endif			/* USB_GLOBAL_INCLUDE_FILE */
84 
85 #include <dev/usb/controller/dwc_otg.h>
86 #include <dev/usb/controller/dwc_otgreg.h>
87 
88 #define	DWC_OTG_BUS2SC(bus) \
89    ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
90     ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
91 
92 #define	DWC_OTG_PC2UDEV(pc) \
93    (USB_DMATAG_TO_XROOT((pc)->tag_parent)->udev)
94 
95 #define	DWC_OTG_MSK_GINT_ENABLED	\
96    (GINTMSK_ENUMDONEMSK |		\
97    GINTMSK_USBRSTMSK |			\
98    GINTMSK_USBSUSPMSK |			\
99    GINTMSK_IEPINTMSK |			\
100    GINTMSK_SESSREQINTMSK |		\
101    GINTMSK_OTGINTMSK |			\
102    GINTMSK_PRTINTMSK)
103 
104 #define	DWC_OTG_MSK_GINT_THREAD_IRQ				\
105    (GINTSTS_USBRST | GINTSTS_ENUMDONE | GINTSTS_PRTINT |	\
106    GINTSTS_WKUPINT | GINTSTS_USBSUSP | GINTMSK_OTGINTMSK |	\
107    GINTSTS_SESSREQINT)
108 
109 static int dwc_otg_use_hsic;
110 
111 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW, 0, "USB DWC OTG");
112 
113 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, use_hsic, CTLFLAG_RD | CTLFLAG_TUN,
114     &dwc_otg_use_hsic, 0, "DWC OTG uses HSIC interface");
115 TUNABLE_INT("hw.usb.dwc_otg.use_hsic", &dwc_otg_use_hsic);
116 
117 #ifdef USB_DEBUG
118 static int dwc_otg_debug;
119 
120 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RW,
121     &dwc_otg_debug, 0, "DWC OTG debug level");
122 #endif
123 
124 #define	DWC_OTG_INTR_ENDPT 1
125 
126 /* prototypes */
127 
128 static const struct usb_bus_methods dwc_otg_bus_methods;
129 static const struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
130 static const struct usb_pipe_methods dwc_otg_device_isoc_methods;
131 
132 static dwc_otg_cmd_t dwc_otg_setup_rx;
133 static dwc_otg_cmd_t dwc_otg_data_rx;
134 static dwc_otg_cmd_t dwc_otg_data_tx;
135 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
136 
137 static dwc_otg_cmd_t dwc_otg_host_setup_tx;
138 static dwc_otg_cmd_t dwc_otg_host_data_tx;
139 static dwc_otg_cmd_t dwc_otg_host_data_rx;
140 
141 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
142 static void dwc_otg_do_poll(struct usb_bus *);
143 static void dwc_otg_standard_done(struct usb_xfer *);
144 static void dwc_otg_root_intr(struct dwc_otg_softc *);
145 static void dwc_otg_interrupt_poll(struct dwc_otg_softc *);
146 static void dwc_otg_host_channel_disable(struct dwc_otg_softc *, uint8_t);
147 
148 /*
149  * Here is a configuration that the chip supports.
150  */
151 static const struct usb_hw_ep_profile dwc_otg_ep_profile[1] = {
152 
153 	[0] = {
154 		.max_in_frame_size = 64,/* fixed */
155 		.max_out_frame_size = 64,	/* fixed */
156 		.is_simplex = 1,
157 		.support_control = 1,
158 	}
159 };
160 
161 static void
162 dwc_otg_get_hw_ep_profile(struct usb_device *udev,
163     const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
164 {
165 	struct dwc_otg_softc *sc;
166 
167 	sc = DWC_OTG_BUS2SC(udev->bus);
168 
169 	if (ep_addr < sc->sc_dev_ep_max)
170 		*ppf = &sc->sc_hw_ep_profile[ep_addr].usb;
171 	else
172 		*ppf = NULL;
173 }
174 
175 static int
176 dwc_otg_init_fifo(struct dwc_otg_softc *sc, uint8_t mode)
177 {
178 	struct dwc_otg_profile *pf;
179 	uint32_t fifo_size;
180 	uint32_t fifo_regs;
181 	uint32_t tx_start;
182 	uint8_t x;
183 
184 	fifo_size = sc->sc_fifo_size;
185 
186 	/*
187 	 * NOTE: Reserved fixed size area at end of RAM, which must
188 	 * not be allocated to the FIFOs:
189 	 */
190 	fifo_regs = 4 * 16;
191 
192 	if (fifo_size < fifo_regs) {
193 		DPRINTF("Too little FIFO\n");
194 		return (EINVAL);
195 	}
196 
197 	/* subtract FIFO regs from total once */
198 	fifo_size -= fifo_regs;
199 
200 	/* split equally for IN and OUT */
201 	fifo_size /= 2;
202 
203 	/* align to 4 bytes boundary */
204 	fifo_size &= ~3;
205 
206 	/* set global receive FIFO size */
207 	DWC_OTG_WRITE_4(sc, DOTG_GRXFSIZ, fifo_size / 4);
208 
209 	tx_start = fifo_size;
210 
211 	if (fifo_size < 64) {
212 		DPRINTFN(-1, "Not enough data space for EP0 FIFO.\n");
213 		return (EINVAL);
214 	}
215 
216 	/* disable any leftover host channels */
217 	for (x = 0; x != sc->sc_host_ch_max; x++) {
218 		if (sc->sc_chan_state[x].wait_sof == 0)
219 			continue;
220 		dwc_otg_host_channel_disable(sc, x);
221 	}
222 
223 	if (mode == DWC_MODE_HOST) {
224 
225 		/* reset active endpoints */
226 		sc->sc_active_rx_ep = 0;
227 
228 		/* split equally for periodic and non-periodic */
229 		fifo_size /= 2;
230 
231 		/* align to 4 bytes boundary */
232 		fifo_size &= ~3;
233 
234 		DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
235 		    ((fifo_size / 4) << 16) |
236 		    (tx_start / 4));
237 
238 		tx_start += fifo_size;
239 
240 		for (x = 0; x != sc->sc_host_ch_max; x++) {
241 			/* disable all host interrupts */
242 			DWC_OTG_WRITE_4(sc, DOTG_HCINTMSK(x), 0);
243 		}
244 
245 		DWC_OTG_WRITE_4(sc, DOTG_HPTXFSIZ,
246 		    ((fifo_size / 4) << 16) |
247 		    (tx_start / 4));
248 
249 		/* reset host channel state */
250 		memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
251 
252 		/* reset FIFO TX levels */
253 		sc->sc_tx_cur_p_level = 0;
254 		sc->sc_tx_cur_np_level = 0;
255 
256 		/* store maximum periodic and non-periodic FIFO TX size */
257 		sc->sc_tx_max_size = fifo_size;
258 
259 		/* disable all host channel interrupts */
260 		DWC_OTG_WRITE_4(sc, DOTG_HAINTMSK, 0);
261 	}
262 
263 	if (mode == DWC_MODE_DEVICE) {
264 
265 	    DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
266 		(0x10 << 16) | (tx_start / 4));
267 	    fifo_size -= 0x40;
268 	    tx_start += 0x40;
269 
270 	    /* setup control endpoint profile */
271 	    sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
272 
273 	    /* reset active endpoints */
274 	    sc->sc_active_rx_ep = 1;
275 
276 	    for (x = 1; x != sc->sc_dev_ep_max; x++) {
277 
278 		pf = sc->sc_hw_ep_profile + x;
279 
280 		pf->usb.max_out_frame_size = 1024 * 3;
281 		pf->usb.is_simplex = 0;	/* assume duplex */
282 		pf->usb.support_bulk = 1;
283 		pf->usb.support_interrupt = 1;
284 		pf->usb.support_isochronous = 1;
285 		pf->usb.support_out = 1;
286 
287 		if (x < sc->sc_dev_in_ep_max) {
288 			uint32_t limit;
289 
290 			limit = (x == 1) ? DWC_OTG_MAX_TXN :
291 			    (DWC_OTG_MAX_TXN / 2);
292 
293 			if (fifo_size >= limit) {
294 				DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
295 				    ((limit / 4) << 16) |
296 				    (tx_start / 4));
297 				tx_start += limit;
298 				fifo_size -= limit;
299 				pf->usb.max_in_frame_size = 0x200;
300 				pf->usb.support_in = 1;
301 				pf->max_buffer = limit;
302 
303 			} else if (fifo_size >= 0x80) {
304 				DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
305 				    ((0x80 / 4) << 16) | (tx_start / 4));
306 				tx_start += 0x80;
307 				fifo_size -= 0x80;
308 				pf->usb.max_in_frame_size = 0x40;
309 				pf->usb.support_in = 1;
310 
311 			} else {
312 				pf->usb.is_simplex = 1;
313 				DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
314 				    (0x0 << 16) | (tx_start / 4));
315 			}
316 		} else {
317 			pf->usb.is_simplex = 1;
318 		}
319 
320 		DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
321 		    pf->usb.max_in_frame_size,
322 		    pf->usb.max_out_frame_size);
323 	    }
324 	}
325 
326 	/* reset RX FIFO */
327 	DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
328 	    GRSTCTL_RXFFLSH);
329 
330 	if (mode != DWC_MODE_OTG) {
331 		/* reset all TX FIFOs */
332 		DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
333 		    GRSTCTL_TXFIFO(0x10) |
334 		    GRSTCTL_TXFFLSH);
335 	} else {
336 		/* reset active endpoints */
337 		sc->sc_active_rx_ep = 0;
338 
339 		/* reset periodic and non-periodic FIFO TX size */
340 		sc->sc_tx_max_size = fifo_size;
341 
342 		/* reset host channel state */
343 		memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
344 
345 		/* reset FIFO TX levels */
346 		sc->sc_tx_cur_p_level = 0;
347 		sc->sc_tx_cur_np_level = 0;
348 	}
349 	return (0);
350 }
351 
352 static void
353 dwc_otg_update_host_frame_interval(struct dwc_otg_softc *sc)
354 {
355 
356   /*
357    * Disabled until further. Assuming that the register is already
358    * programmed correctly by the boot loader.
359    */
360 #if 0
361 	uint32_t temp;
362 
363 	/* setup HOST frame interval register, based on existing value */
364 	temp = DWC_OTG_READ_4(sc, DOTG_HFIR) & HFIR_FRINT_MASK;
365 	if (temp >= 10000)
366 		temp /= 1000;
367 	else
368 		temp /= 125;
369 
370 	/* figure out nearest X-tal value */
371 	if (temp >= 54)
372 		temp = 60;	/* MHz */
373 	else if (temp >= 39)
374 		temp = 48;	/* MHz */
375 	else
376 		temp = 30;	/* MHz */
377 
378 	if (sc->sc_flags.status_high_speed)
379 		temp *= 125;
380 	else
381 		temp *= 1000;
382 
383 	DPRINTF("HFIR=0x%08x\n", temp);
384 
385 	DWC_OTG_WRITE_4(sc, DOTG_HFIR, temp);
386 #endif
387 }
388 
389 static void
390 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
391 {
392 	if (sc->sc_flags.clocks_off &&
393 	    sc->sc_flags.port_powered) {
394 
395 		DPRINTFN(5, "\n");
396 
397 		/* TODO - platform specific */
398 
399 		sc->sc_flags.clocks_off = 0;
400 	}
401 }
402 
403 static void
404 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
405 {
406 	if (!sc->sc_flags.clocks_off) {
407 
408 		DPRINTFN(5, "\n");
409 
410 		/* TODO - platform specific */
411 
412 		sc->sc_flags.clocks_off = 1;
413 	}
414 }
415 
416 static void
417 dwc_otg_pull_up(struct dwc_otg_softc *sc)
418 {
419 	uint32_t temp;
420 
421 	/* pullup D+, if possible */
422 
423 	if (!sc->sc_flags.d_pulled_up &&
424 	    sc->sc_flags.port_powered) {
425 		sc->sc_flags.d_pulled_up = 1;
426 
427 		temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
428 		temp &= ~DCTL_SFTDISCON;
429 		DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
430 	}
431 }
432 
433 static void
434 dwc_otg_pull_down(struct dwc_otg_softc *sc)
435 {
436 	uint32_t temp;
437 
438 	/* pulldown D+, if possible */
439 
440 	if (sc->sc_flags.d_pulled_up) {
441 		sc->sc_flags.d_pulled_up = 0;
442 
443 		temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
444 		temp |= DCTL_SFTDISCON;
445 		DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
446 	}
447 }
448 
449 static void
450 dwc_otg_enable_sof_irq(struct dwc_otg_softc *sc)
451 {
452 	/* In device mode we don't use the SOF interrupt */
453 	if (sc->sc_flags.status_device_mode != 0 ||
454 	    (sc->sc_irq_mask & GINTMSK_SOFMSK) != 0)
455 		return;
456 	sc->sc_irq_mask |= GINTMSK_SOFMSK;
457 	DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
458 }
459 
460 static void
461 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
462 {
463 	if (sc->sc_flags.status_suspend) {
464 		/* update status bits */
465 		sc->sc_flags.status_suspend = 0;
466 		sc->sc_flags.change_suspend = 1;
467 
468 		if (sc->sc_flags.status_device_mode) {
469 			/*
470 			 * Disable resume interrupt and enable suspend
471 			 * interrupt:
472 			 */
473 			sc->sc_irq_mask &= ~GINTMSK_WKUPINTMSK;
474 			sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
475 			DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
476 		}
477 
478 		/* complete root HUB interrupt endpoint */
479 		dwc_otg_root_intr(sc);
480 	}
481 }
482 
483 static void
484 dwc_otg_suspend_irq(struct dwc_otg_softc *sc)
485 {
486 	if (!sc->sc_flags.status_suspend) {
487 		/* update status bits */
488 		sc->sc_flags.status_suspend = 1;
489 		sc->sc_flags.change_suspend = 1;
490 
491 		if (sc->sc_flags.status_device_mode) {
492 			/*
493 			 * Disable suspend interrupt and enable resume
494 			 * interrupt:
495 			 */
496 			sc->sc_irq_mask &= ~GINTMSK_USBSUSPMSK;
497 			sc->sc_irq_mask |= GINTMSK_WKUPINTMSK;
498 			DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
499 		}
500 
501 		/* complete root HUB interrupt endpoint */
502 		dwc_otg_root_intr(sc);
503 	}
504 }
505 
506 static void
507 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
508 {
509 	if (!sc->sc_flags.status_suspend)
510 		return;
511 
512 	DPRINTFN(5, "Remote wakeup\n");
513 
514 	if (sc->sc_flags.status_device_mode) {
515 		uint32_t temp;
516 
517 		/* enable remote wakeup signalling */
518 		temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
519 		temp |= DCTL_RMTWKUPSIG;
520 		DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
521 
522 		/* Wait 8ms for remote wakeup to complete. */
523 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
524 
525 		temp &= ~DCTL_RMTWKUPSIG;
526 		DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
527 	} else {
528 		/* enable USB port */
529 		DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
530 
531 		/* wait 10ms */
532 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
533 
534 		/* resume port */
535 		sc->sc_hprt_val |= HPRT_PRTRES;
536 		DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
537 
538 		/* Wait 100ms for resume signalling to complete. */
539 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
540 
541 		/* clear suspend and resume */
542 		sc->sc_hprt_val &= ~(HPRT_PRTSUSP | HPRT_PRTRES);
543 		DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
544 
545 		/* Wait 4ms */
546 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
547 	}
548 
549 	/* need to fake resume IRQ */
550 	dwc_otg_resume_irq(sc);
551 }
552 
553 static void
554 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
555 {
556 	uint32_t temp;
557 
558 	DPRINTFN(5, "addr=%d\n", addr);
559 
560 	temp = DWC_OTG_READ_4(sc, DOTG_DCFG);
561 	temp &= ~DCFG_DEVADDR_SET(0x7F);
562 	temp |= DCFG_DEVADDR_SET(addr);
563 	DWC_OTG_WRITE_4(sc, DOTG_DCFG, temp);
564 }
565 
566 static void
567 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
568 {
569 	DPRINTFN(5, "RX status clear\n");
570 
571 	if (sc->sc_flags.status_device_mode != 0) {
572 		/* enable RX FIFO level interrupt */
573 		sc->sc_irq_mask |= GINTMSK_RXFLVLMSK;
574 		DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
575 	}
576 
577 	/* clear cached status */
578 	sc->sc_last_rx_status = 0;
579 }
580 
581 static void
582 dwc_otg_clear_hcint(struct dwc_otg_softc *sc, uint8_t x)
583 {
584 	uint32_t hcint;
585 
586 	/* clear all pending interrupts */
587 	hcint = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
588 	DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), hcint);
589 
590 	/* clear buffered interrupts */
591 	sc->sc_chan_state[x].hcint = 0;
592 }
593 
594 static uint8_t
595 dwc_otg_host_channel_alloc(struct dwc_otg_softc *sc, struct dwc_otg_td *td, uint8_t which, uint8_t is_out)
596 {
597 	uint32_t tx_p_size;
598 	uint32_t tx_np_size;
599 	uint8_t x;
600 
601 	if (td->channel[which] < DWC_OTG_MAX_CHANNELS)
602 		return (0);		/* already allocated */
603 
604 	/* check if device is suspended */
605 	if (DWC_OTG_PC2UDEV(td->pc)->flags.self_suspended != 0)
606 		return (1);		/* busy - cannot transfer data */
607 
608 	/* compute needed TX FIFO size */
609 	if (is_out != 0) {
610 		if (td->ep_type == UE_INTERRUPT ||
611 		    td->ep_type == UE_ISOCHRONOUS) {
612 			tx_p_size = td->max_packet_size;
613 			tx_np_size = 0;
614 			if (td->hcsplt != 0 && tx_p_size > HCSPLT_XACTLEN_BURST)
615 				tx_p_size = HCSPLT_XACTLEN_BURST;
616 			if ((sc->sc_tx_cur_p_level + tx_p_size) > sc->sc_tx_max_size) {
617 				DPRINTF("Too little FIFO space\n");
618 				return (1);	/* too little FIFO */
619 			}
620 		} else {
621 			tx_p_size = 0;
622 			tx_np_size = td->max_packet_size;
623 			if (td->hcsplt != 0 && tx_np_size > HCSPLT_XACTLEN_BURST)
624 				tx_np_size = HCSPLT_XACTLEN_BURST;
625 			if ((sc->sc_tx_cur_np_level + tx_np_size) > sc->sc_tx_max_size) {
626 				DPRINTF("Too little FIFO space\n");
627 				return (1);	/* too little FIFO */
628 			}
629 		}
630 	} else {
631 		/* not a TX transaction */
632 		tx_p_size = 0;
633 		tx_np_size = 0;
634 	}
635 
636 	for (x = 0; x != sc->sc_host_ch_max; x++) {
637 		if (sc->sc_chan_state[x].allocated != 0)
638 			continue;
639 		/* check if channel is still enabled */
640 		if (sc->sc_chan_state[x].wait_sof != 0)
641 			continue;
642 
643 		sc->sc_chan_state[x].allocated = 1;
644 		sc->sc_chan_state[x].tx_p_size = tx_p_size;
645 		sc->sc_chan_state[x].tx_np_size = tx_np_size;
646 
647 		/* keep track of used TX FIFO, if any */
648 		sc->sc_tx_cur_p_level += tx_p_size;
649 		sc->sc_tx_cur_np_level += tx_np_size;
650 
651 		/* clear interrupts */
652 		dwc_otg_clear_hcint(sc, x);
653 
654 		DPRINTF("CH=%d HCCHAR=0x%08x "
655 		    "HCSPLT=0x%08x\n", x, td->hcchar, td->hcsplt);
656 
657 		/* set active channel */
658 		sc->sc_active_rx_ep |= (1 << x);
659 
660 		/* set channel */
661 		td->channel[which] = x;
662 
663 		return (0);	/* allocated */
664 	}
665 	/* wait a bit */
666 	dwc_otg_enable_sof_irq(sc);
667 	return (1);	/* busy */
668 }
669 
670 static void
671 dwc_otg_host_channel_free(struct dwc_otg_softc *sc, struct dwc_otg_td *td, uint8_t which)
672 {
673 	uint8_t x;
674 
675 	if (td->channel[which] >= DWC_OTG_MAX_CHANNELS)
676 		return;		/* already freed */
677 
678 	/* free channel */
679 	x = td->channel[which];
680 	td->channel[which] = DWC_OTG_MAX_CHANNELS;
681 
682 	DPRINTF("CH=%d\n", x);
683 
684 	/*
685 	 * We need to let programmed host channels run till complete
686 	 * else the host channel will stop functioning. Assume that
687 	 * after a fixed given amount of time the host channel is no
688 	 * longer doing any USB traffic:
689 	 */
690 	if (td->ep_type == UE_ISOCHRONOUS || td->ep_type == UE_INTERRUPT) {
691 		/* double buffered */
692 		sc->sc_chan_state[x].wait_sof = DWC_OTG_SLOT_IDLE_MAX;
693 	} else {
694 		/* single buffered */
695 		sc->sc_chan_state[x].wait_sof = DWC_OTG_SLOT_IDLE_MIN;
696 	}
697 
698 	sc->sc_chan_state[x].allocated = 0;
699 
700 	/* ack any pending messages */
701 	if (sc->sc_last_rx_status != 0 &&
702 	    GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) == x) {
703 		dwc_otg_common_rx_ack(sc);
704 	}
705 
706 	/* clear active channel */
707 	sc->sc_active_rx_ep &= ~(1 << x);
708 }
709 
710 static uint8_t
711 dwc_otg_host_setup_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
712 {
713 	struct usb_device_request req __aligned(4);
714 	uint32_t hcint;
715 	uint32_t hcchar;
716 	uint8_t delta;
717 
718 	if (td->channel[0] < DWC_OTG_MAX_CHANNELS) {
719 		hcint = sc->sc_chan_state[td->channel[0]].hcint;
720 
721 		DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
722 		    td->channel[0], td->state, hcint,
723 		    DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel[0])),
724 		    DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel[0])));
725 	} else {
726 		hcint = 0;
727 		goto check_state;
728 	}
729 
730 	if (hcint & (HCINT_RETRY |
731 	    HCINT_ACK | HCINT_NYET)) {
732 		/* give success bits priority over failure bits */
733 	} else if (hcint & HCINT_STALL) {
734 		DPRINTF("CH=%d STALL\n", td->channel[0]);
735 		td->error_stall = 1;
736 		td->error_any = 1;
737 		goto complete;
738 	} else if (hcint & HCINT_ERRORS) {
739 		DPRINTF("CH=%d ERROR\n", td->channel[0]);
740 		td->errcnt++;
741 		if (td->hcsplt != 0 || td->errcnt >= 3) {
742 			td->error_any = 1;
743 			goto complete;
744 		}
745 	}
746 
747 	if (hcint & (HCINT_ERRORS | HCINT_RETRY |
748 	    HCINT_ACK | HCINT_NYET)) {
749 		if (!(hcint & HCINT_ERRORS))
750 			td->errcnt = 0;
751 	}
752 
753 check_state:
754 	switch (td->state) {
755 	case DWC_CHAN_ST_START:
756 		goto send_pkt;
757 
758 	case DWC_CHAN_ST_WAIT_ANE:
759 		if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
760 			td->did_nak++;
761 			td->tt_scheduled = 0;
762 			goto send_pkt;
763 		} else if (hcint & (HCINT_ACK | HCINT_NYET)) {
764 			td->offset += td->tx_bytes;
765 			td->remainder -= td->tx_bytes;
766 			td->toggle = 1;
767 			td->tt_scheduled = 0;
768 			goto complete;
769 		}
770 		break;
771 
772 	case DWC_CHAN_ST_WAIT_S_ANE:
773 		if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
774 			td->did_nak++;
775 			td->tt_scheduled = 0;
776 			goto send_pkt;
777 		} else if (hcint & (HCINT_ACK | HCINT_NYET)) {
778 			goto send_cpkt;
779 		}
780 		break;
781 
782 	case DWC_CHAN_ST_WAIT_C_ANE:
783 		if (hcint & HCINT_NYET) {
784 			goto send_cpkt;
785 		} else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
786 			td->did_nak++;
787 			td->tt_scheduled = 0;
788 			goto send_pkt;
789 		} else if (hcint & HCINT_ACK) {
790 			td->offset += td->tx_bytes;
791 			td->remainder -= td->tx_bytes;
792 			td->toggle = 1;
793 			goto complete;
794 		}
795 		break;
796 
797 	case DWC_CHAN_ST_WAIT_C_PKT:
798 		goto send_cpkt;
799 
800 	default:
801 		break;
802 	}
803 	goto busy;
804 
805 send_pkt:
806 	/* free existing channel, if any */
807 	dwc_otg_host_channel_free(sc, td, 0);
808 
809 	if (sizeof(req) != td->remainder) {
810 		td->error_any = 1;
811 		goto complete;
812 	}
813 
814 	if (td->hcsplt != 0) {
815 		delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
816 		if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
817 			td->state = DWC_CHAN_ST_START;
818 			goto busy;
819 		}
820 		delta = sc->sc_last_frame_num - td->tt_start_slot;
821 		if (delta > 5) {
822 			/* missed it */
823 			td->tt_scheduled = 0;
824 			td->state = DWC_CHAN_ST_START;
825 			goto busy;
826 		}
827 	}
828 
829 	/* allocate a new channel */
830 	if (dwc_otg_host_channel_alloc(sc, td, 0, 1)) {
831 		td->state = DWC_CHAN_ST_START;
832 		goto busy;
833 	}
834 
835 	if (td->hcsplt != 0) {
836 		td->hcsplt &= ~HCSPLT_COMPSPLT;
837 		td->state = DWC_CHAN_ST_WAIT_S_ANE;
838 	} else {
839 		td->state = DWC_CHAN_ST_WAIT_ANE;
840 	}
841 
842 	usbd_copy_out(td->pc, 0, &req, sizeof(req));
843 
844 	DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
845 	    (sizeof(req) << HCTSIZ_XFERSIZE_SHIFT) |
846 	    (1 << HCTSIZ_PKTCNT_SHIFT) |
847 	    (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
848 
849 	DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
850 
851 	hcchar = td->hcchar;
852 	hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
853 	hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
854 
855 	/* must enable channel before writing data to FIFO */
856 	DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
857 
858 	/* transfer data into FIFO */
859 	bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
860 	    DOTG_DFIFO(td->channel[0]), (uint32_t *)&req, sizeof(req) / 4);
861 
862 	/* store number of bytes transmitted */
863 	td->tx_bytes = sizeof(req);
864 	goto busy;
865 
866 send_cpkt:
867 	/* free existing channel, if any */
868 	dwc_otg_host_channel_free(sc, td, 0);
869 
870 	delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
871 	if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
872 		td->state = DWC_CHAN_ST_WAIT_C_PKT;
873 		goto busy;
874 	}
875 	delta = sc->sc_last_frame_num - td->tt_start_slot;
876 	if (delta > DWC_OTG_TT_SLOT_MAX) {
877 		/* we missed the service interval */
878 		if (td->ep_type != UE_ISOCHRONOUS)
879 			td->error_any = 1;
880 		goto complete;
881 	}
882 	/* allocate a new channel */
883 	if (dwc_otg_host_channel_alloc(sc, td, 0, 0)) {
884 		td->state = DWC_CHAN_ST_WAIT_C_PKT;
885 		goto busy;
886 	}
887 
888 	/* wait until next slot before trying again */
889 	td->tt_complete_slot++;
890 
891 	td->hcsplt |= HCSPLT_COMPSPLT;
892 	td->state = DWC_CHAN_ST_WAIT_C_ANE;
893 
894 	DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
895 	    (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
896 
897 	DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
898 
899 	hcchar = td->hcchar;
900 	hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
901 	hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
902 
903 	/* must enable channel before writing data to FIFO */
904 	DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
905 
906 busy:
907 	return (1);	/* busy */
908 
909 complete:
910 	dwc_otg_host_channel_free(sc, td, 0);
911 	return (0);	/* complete */
912 }
913 
914 static uint8_t
915 dwc_otg_setup_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
916 {
917 	struct usb_device_request req __aligned(4);
918 	uint32_t temp;
919 	uint16_t count;
920 
921 	/* check endpoint status */
922 
923 	if (sc->sc_last_rx_status == 0)
924 		goto not_complete;
925 
926 	if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != 0)
927 		goto not_complete;
928 
929 	if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
930 	    GRXSTSRD_DPID_DATA0) {
931 		/* release FIFO */
932 		dwc_otg_common_rx_ack(sc);
933 		goto not_complete;
934 	}
935 
936 	if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
937 	    GRXSTSRD_STP_DATA) {
938 		/* release FIFO */
939 		dwc_otg_common_rx_ack(sc);
940 		goto not_complete;
941 	}
942 
943 	DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
944 
945 	/* clear did stall */
946 	td->did_stall = 0;
947 
948 	/* get the packet byte count */
949 	count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
950 
951 	/* verify data length */
952 	if (count != td->remainder) {
953 		DPRINTFN(0, "Invalid SETUP packet "
954 		    "length, %d bytes\n", count);
955 		/* release FIFO */
956 		dwc_otg_common_rx_ack(sc);
957 		goto not_complete;
958 	}
959 	if (count != sizeof(req)) {
960 		DPRINTFN(0, "Unsupported SETUP packet "
961 		    "length, %d bytes\n", count);
962 		/* release FIFO */
963 		dwc_otg_common_rx_ack(sc);
964 		goto not_complete;
965 	}
966 
967 	/* copy in control request */
968 	memcpy(&req, sc->sc_rx_bounce_buffer, sizeof(req));
969 
970 	/* copy data into real buffer */
971 	usbd_copy_in(td->pc, 0, &req, sizeof(req));
972 
973 	td->offset = sizeof(req);
974 	td->remainder = 0;
975 
976 	/* sneak peek the set address */
977 	if ((req.bmRequestType == UT_WRITE_DEVICE) &&
978 	    (req.bRequest == UR_SET_ADDRESS)) {
979 		/* must write address before ZLP */
980 		dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
981 	}
982 
983 	/* don't send any data by default */
984 	DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(0),
985 	    DXEPTSIZ_SET_NPKT(0) |
986 	    DXEPTSIZ_SET_NBYTES(0));
987 
988 	temp = sc->sc_in_ctl[0];
989 
990 	/* enable IN endpoint */
991 	DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
992 	    temp | DIEPCTL_EPENA);
993 	DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
994 	    temp | DIEPCTL_SNAK);
995 
996 	/* reset IN endpoint buffer */
997 	DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
998 	    GRSTCTL_TXFIFO(0) |
999 	    GRSTCTL_TXFFLSH);
1000 
1001 	/* acknowledge RX status */
1002 	dwc_otg_common_rx_ack(sc);
1003 	return (0);			/* complete */
1004 
1005 not_complete:
1006 	/* abort any ongoing transfer, before enabling again */
1007 
1008 	temp = sc->sc_out_ctl[0];
1009 
1010 	temp |= DOEPCTL_EPENA |
1011 	    DOEPCTL_SNAK;
1012 
1013 	/* enable OUT endpoint */
1014 	DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0), temp);
1015 
1016 	if (!td->did_stall) {
1017 		td->did_stall = 1;
1018 
1019 		DPRINTFN(5, "stalling IN and OUT direction\n");
1020 
1021 		/* set stall after enabling endpoint */
1022 		DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0),
1023 		    temp | DOEPCTL_STALL);
1024 
1025 		temp = sc->sc_in_ctl[0];
1026 
1027 		/* set stall assuming endpoint is enabled */
1028 		DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
1029 		    temp | DIEPCTL_STALL);
1030 	}
1031 
1032 	/* setup number of buffers to receive */
1033 	DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
1034 	    DXEPTSIZ_SET_MULTI(3) |
1035 	    DXEPTSIZ_SET_NPKT(1) |
1036 	    DXEPTSIZ_SET_NBYTES(sizeof(req)));
1037 
1038 	return (1);			/* not complete */
1039 }
1040 
1041 static uint8_t
1042 dwc_otg_host_rate_check_interrupt(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1043 {
1044 	uint8_t delta;
1045 
1046 	delta = sc->sc_tmr_val - td->tmr_val;
1047 	if (delta >= 128)
1048 		return (1);	/* busy */
1049 
1050 	td->tmr_val = sc->sc_tmr_val + td->tmr_res;
1051 
1052 	/* set toggle, if any */
1053 	if (td->set_toggle) {
1054 		td->set_toggle = 0;
1055 		td->toggle = 1;
1056 	}
1057 	return (0);
1058 }
1059 
1060 static uint8_t
1061 dwc_otg_host_rate_check(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1062 {
1063 	if (td->ep_type == UE_ISOCHRONOUS) {
1064 		/* non TT isochronous traffic */
1065 		if ((td->tmr_val != 0) ||
1066 		    (sc->sc_last_frame_num & (td->tmr_res - 1))) {
1067 			goto busy;
1068 		}
1069 		td->tmr_val = 1;	/* executed */
1070 		td->toggle = 0;
1071 
1072 	} else if (td->ep_type == UE_INTERRUPT) {
1073 		if (!td->tt_scheduled)
1074 			goto busy;
1075 		td->tt_scheduled = 0;
1076 	} else if (td->did_nak >= DWC_OTG_NAK_MAX) {
1077 		goto busy;
1078 	} else if (td->set_toggle) {
1079 		td->set_toggle = 0;
1080 		td->toggle = 1;
1081 	}
1082 	return (0);
1083 busy:
1084 	return (1);
1085 }
1086 
1087 static uint8_t
1088 dwc_otg_host_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1089 {
1090 	uint32_t hcint;
1091 	uint32_t hcchar;
1092 	uint32_t count;
1093 	uint8_t delta;
1094 	uint8_t channel;
1095 
1096 	channel = td->channel[td->tt_channel_tog];
1097 
1098 	if (channel < DWC_OTG_MAX_CHANNELS) {
1099 		hcint = sc->sc_chan_state[channel].hcint;
1100 
1101 		DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1102 		    channel, td->state, hcint,
1103 		    DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1104 		    DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1105 	} else {
1106 		hcint = 0;
1107 		goto check_state;
1108 	}
1109 
1110 	/* check interrupt bits */
1111 
1112 	if (hcint & (HCINT_RETRY |
1113 	    HCINT_ACK | HCINT_NYET)) {
1114 		/* give success bits priority over failure bits */
1115 	} else if (hcint & HCINT_STALL) {
1116 		DPRINTF("CH=%d STALL\n", channel);
1117 		td->error_stall = 1;
1118 		td->error_any = 1;
1119 		goto complete;
1120 	} else if (hcint & HCINT_ERRORS) {
1121 		DPRINTF("CH=%d ERROR\n", channel);
1122 		td->errcnt++;
1123 		if (td->hcsplt != 0 || td->errcnt >= 3) {
1124 			if (td->ep_type != UE_ISOCHRONOUS) {
1125 				td->error_any = 1;
1126 				goto complete;
1127 			}
1128 		}
1129 	}
1130 
1131 	/* check endpoint status */
1132 	if (sc->sc_last_rx_status == 0)
1133 		goto check_state;
1134 
1135 	if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != channel)
1136 		goto check_state;
1137 
1138 	switch (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) {
1139 	case GRXSTSRH_IN_DATA:
1140 
1141 		DPRINTF("DATA ST=%d STATUS=0x%08x\n",
1142 		    (int)td->state, (int)sc->sc_last_rx_status);
1143 
1144 		if (hcint & HCINT_SOFTWARE_ONLY) {
1145 			/*
1146 			 * When using SPLIT transactions on interrupt
1147 			 * endpoints, sometimes data occurs twice.
1148 			 */
1149 			DPRINTF("Data already received\n");
1150 			break;
1151 		}
1152 
1153 		/* get the packet byte count */
1154 		count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1155 
1156 		/* check for isochronous transfer or high-speed bandwidth endpoint */
1157 		if (td->ep_type == UE_ISOCHRONOUS || td->max_packet_count > 1) {
1158 			if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) != GRXSTSRD_DPID_DATA0) {
1159 				td->tt_xactpos = HCSPLT_XACTPOS_MIDDLE;
1160 			} else {
1161 				td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
1162 
1163 				/* verify the packet byte count */
1164 				if (count < td->max_packet_size) {
1165 					/* we have a short packet */
1166 					td->short_pkt = 1;
1167 					td->got_short = 1;
1168 				}
1169 			}
1170 			td->toggle = 0;
1171 		} else {
1172 			/* verify the packet byte count */
1173 			if (count != td->max_packet_size) {
1174 				if (count < td->max_packet_size) {
1175 					/* we have a short packet */
1176 					td->short_pkt = 1;
1177 					td->got_short = 1;
1178 				} else {
1179 					/* invalid USB packet */
1180 					td->error_any = 1;
1181 
1182 					/* release FIFO */
1183 					dwc_otg_common_rx_ack(sc);
1184 					goto complete;
1185 				}
1186 			}
1187 			td->toggle ^= 1;
1188 			td->tt_scheduled = 0;
1189 		}
1190 
1191 		/* verify the packet byte count */
1192 		if (count > td->remainder) {
1193 			/* invalid USB packet */
1194 			td->error_any = 1;
1195 
1196 			/* release FIFO */
1197 			dwc_otg_common_rx_ack(sc);
1198 			goto complete;
1199 		}
1200 
1201 		usbd_copy_in(td->pc, td->offset,
1202 		    sc->sc_rx_bounce_buffer, count);
1203 
1204 		td->remainder -= count;
1205 		td->offset += count;
1206 		hcint |= HCINT_SOFTWARE_ONLY;
1207 		sc->sc_chan_state[channel].hcint = hcint;
1208 		break;
1209 
1210 	default:
1211 		break;
1212 	}
1213 	/* release FIFO */
1214 	dwc_otg_common_rx_ack(sc);
1215 
1216 check_state:
1217 	if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1218 	    HCINT_ACK | HCINT_NYET)) {
1219 		if (!(hcint & HCINT_ERRORS))
1220 			td->errcnt = 0;
1221 	}
1222 
1223 	switch (td->state) {
1224 	case DWC_CHAN_ST_START:
1225 		if (td->hcsplt != 0)
1226 			goto receive_spkt;
1227 		else
1228 			goto receive_pkt;
1229 
1230 	case DWC_CHAN_ST_WAIT_ANE:
1231 		if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1232 			td->did_nak++;
1233 			td->tt_scheduled = 0;
1234 			if (td->hcsplt != 0)
1235 				goto receive_spkt;
1236 			else
1237 				goto receive_pkt;
1238 		} else if (hcint & HCINT_NYET) {
1239 			if (td->hcsplt != 0) {
1240 				/* try again */
1241 				goto receive_pkt;
1242 			} else {
1243 				/* not a valid token for IN endpoints */
1244 				td->error_any = 1;
1245 				goto complete;
1246 			}
1247 		} else if (hcint & HCINT_ACK) {
1248 			/* wait for data - ACK arrived first */
1249 			if (!(hcint & HCINT_SOFTWARE_ONLY))
1250 				goto busy;
1251 
1252 			if (td->ep_type == UE_ISOCHRONOUS) {
1253 				/* check if we are complete */
1254 				if ((td->remainder == 0) ||
1255 				    (td->tt_xactpos == HCSPLT_XACTPOS_BEGIN)) {
1256 					goto complete;
1257 				}
1258 				/* get another packet */
1259 				goto receive_pkt;
1260 			} else {
1261 				/* check if we are complete */
1262 				if ((td->remainder == 0) || (td->got_short != 0)) {
1263 					if (td->short_pkt)
1264 						goto complete;
1265 
1266 					/*
1267 					 * Else need to receive a zero length
1268 					 * packet.
1269 					 */
1270 				}
1271 				td->tt_scheduled = 0;
1272 				td->did_nak = 0;
1273 				if (td->hcsplt != 0)
1274 					goto receive_spkt;
1275 				else
1276 					goto receive_pkt;
1277 			}
1278 		}
1279 		break;
1280 
1281 	case DWC_CHAN_ST_WAIT_S_ANE:
1282 		/*
1283 		 * NOTE: The DWC OTG hardware provides a fake ACK in
1284 		 * case of interrupt and isochronous transfers:
1285 		 */
1286 		if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1287 			td->did_nak++;
1288 			td->tt_scheduled = 0;
1289 			goto receive_spkt;
1290 		} else if (hcint & HCINT_NYET) {
1291 			td->tt_scheduled = 0;
1292 			goto receive_spkt;
1293 		} else if (hcint & HCINT_ACK) {
1294 			td->did_nak = 0;
1295 			goto receive_pkt;
1296 		}
1297 		break;
1298 
1299 	case DWC_CHAN_ST_WAIT_C_PKT:
1300 		goto receive_pkt;
1301 
1302 	default:
1303 		break;
1304 	}
1305 	goto busy;
1306 
1307 receive_pkt:
1308 	/* free existing channel, if any */
1309 	dwc_otg_host_channel_free(sc, td, td->tt_channel_tog);
1310 
1311   	if (td->hcsplt != 0) {
1312 		delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1313 		if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1314 			td->state = DWC_CHAN_ST_WAIT_C_PKT;
1315 			goto busy;
1316 		}
1317 		delta = sc->sc_last_frame_num - td->tt_start_slot;
1318 		if (delta > DWC_OTG_TT_SLOT_MAX) {
1319 			/* we missed the service interval */
1320 			if (td->ep_type != UE_ISOCHRONOUS)
1321 				td->error_any = 1;
1322 			goto complete;
1323 		}
1324 		/* complete split */
1325 		td->hcsplt |= HCSPLT_COMPSPLT;
1326 	} else if (td->tt_xactpos == HCSPLT_XACTPOS_BEGIN &&
1327 	    dwc_otg_host_rate_check(sc, td)) {
1328 		td->state = DWC_CHAN_ST_WAIT_C_PKT;
1329 		goto busy;
1330 	}
1331 
1332 	/* allocate a new channel */
1333 	if (dwc_otg_host_channel_alloc(sc, td, td->tt_channel_tog, 0)) {
1334 		td->state = DWC_CHAN_ST_WAIT_C_PKT;
1335 		goto busy;
1336 	}
1337 
1338 	channel = td->channel[td->tt_channel_tog];
1339 
1340 	/* set toggle, if any */
1341 	if (td->set_toggle) {
1342 		td->set_toggle = 0;
1343 		td->toggle = 1;
1344 	}
1345 
1346 	td->state = DWC_CHAN_ST_WAIT_ANE;
1347 
1348 	/* receive one packet */
1349 	DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1350 	    (td->max_packet_size << HCTSIZ_XFERSIZE_SHIFT) |
1351 	    (1 << HCTSIZ_PKTCNT_SHIFT) |
1352 	    (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1353 	    (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1354 
1355 	DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1356 
1357 	hcchar = td->hcchar;
1358 	hcchar |= HCCHAR_EPDIR_IN;
1359 
1360 	/* check if other channel is allocated */
1361 	if (td->channel[td->tt_channel_tog ^ 1] < DWC_OTG_MAX_CHANNELS) {
1362 		/* second - receive after next SOF event */
1363 		if ((sc->sc_last_frame_num & 1) != 0)
1364 			hcchar |= HCCHAR_ODDFRM;
1365 		else
1366 			hcchar &= ~HCCHAR_ODDFRM;
1367 
1368 		/* other channel is next */
1369 		td->tt_channel_tog ^= 1;
1370 		td->tt_complete_slot++;
1371 
1372 		/* must enable channel before data can be received */
1373 		DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1374 	} else {
1375 		/* first - receive after second next SOF event */
1376 		if ((sc->sc_last_frame_num & 1) == 0)
1377 			hcchar |= HCCHAR_ODDFRM;
1378 		else
1379 			hcchar &= ~HCCHAR_ODDFRM;
1380 
1381 		/* must enable channel before data can be received */
1382 		DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1383 
1384 		if (td->hcsplt != 0) {
1385 			if (td->ep_type == UE_ISOCHRONOUS || td->ep_type == UE_INTERRUPT) {
1386 				/* allocate a second channel */
1387 				td->tt_channel_tog ^= 1;
1388 				goto receive_pkt;
1389 			} else {
1390 				td->tt_complete_slot++;
1391 			}
1392 		}
1393 	}
1394 	goto busy;
1395 
1396 receive_spkt:
1397 	/* free existing channel(s), if any */
1398 	dwc_otg_host_channel_free(sc, td, 0);
1399 	dwc_otg_host_channel_free(sc, td, 1);
1400 
1401 	delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1402 	if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1403 		td->state = DWC_CHAN_ST_START;
1404 		goto busy;
1405 	}
1406 	delta = sc->sc_last_frame_num - td->tt_start_slot;
1407 	if (delta > 5) {
1408 		/* missed it */
1409 		td->tt_scheduled = 0;
1410 		td->state = DWC_CHAN_ST_START;
1411 		goto busy;
1412 	}
1413 
1414 	/* allocate a new channel */
1415 	if (dwc_otg_host_channel_alloc(sc, td, 0, 0)) {
1416 		td->state = DWC_CHAN_ST_START;
1417 		goto busy;
1418 	}
1419 
1420 	channel = td->channel[0];
1421 
1422 	td->hcsplt &= ~HCSPLT_COMPSPLT;
1423 	td->state = DWC_CHAN_ST_WAIT_S_ANE;
1424 
1425 	/* reset channel toggle */
1426 	td->tt_channel_tog = 0;
1427 
1428 	/* receive one packet */
1429 	DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1430 	    (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1431 
1432 	DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1433 
1434 	/* send after next SOF event */
1435 	if ((sc->sc_last_frame_num & 1) == 0)
1436 		td->hcchar |= HCCHAR_ODDFRM;
1437 	else
1438 		td->hcchar &= ~HCCHAR_ODDFRM;
1439 
1440 	hcchar = td->hcchar;
1441 	hcchar |= HCCHAR_EPDIR_IN;
1442 
1443 	/* must enable channel before data can be received */
1444 	DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1445 busy:
1446 	return (1);	/* busy */
1447 
1448 complete:
1449 	dwc_otg_host_channel_free(sc, td, 0);
1450 	dwc_otg_host_channel_free(sc, td, 1);
1451 	return (0);	/* complete */
1452 }
1453 
1454 static uint8_t
1455 dwc_otg_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1456 {
1457 	uint32_t temp;
1458 	uint16_t count;
1459 	uint8_t got_short;
1460 
1461 	got_short = 0;
1462 
1463 	/* check endpoint status */
1464 	if (sc->sc_last_rx_status == 0)
1465 		goto not_complete;
1466 
1467 	if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != td->ep_no)
1468 		goto not_complete;
1469 
1470 	/* check for SETUP packet */
1471 	if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1472 	    GRXSTSRD_STP_DATA) {
1473 		if (td->remainder == 0) {
1474 			/*
1475 			 * We are actually complete and have
1476 			 * received the next SETUP
1477 			 */
1478 			DPRINTFN(5, "faking complete\n");
1479 			return (0);	/* complete */
1480 		}
1481 		/*
1482 		 * USB Host Aborted the transfer.
1483 		 */
1484 		td->error_any = 1;
1485 		return (0);		/* complete */
1486 	}
1487 
1488 	if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1489 	    GRXSTSRD_OUT_DATA) {
1490 		/* release FIFO */
1491 		dwc_otg_common_rx_ack(sc);
1492 		goto not_complete;
1493 	}
1494 
1495 	/* get the packet byte count */
1496 	count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1497 
1498 	/* verify the packet byte count */
1499 	if (count != td->max_packet_size) {
1500 		if (count < td->max_packet_size) {
1501 			/* we have a short packet */
1502 			td->short_pkt = 1;
1503 			got_short = 1;
1504 		} else {
1505 			/* invalid USB packet */
1506 			td->error_any = 1;
1507 
1508 			/* release FIFO */
1509 			dwc_otg_common_rx_ack(sc);
1510 			return (0);	/* we are complete */
1511 		}
1512 	}
1513 	/* verify the packet byte count */
1514 	if (count > td->remainder) {
1515 		/* invalid USB packet */
1516 		td->error_any = 1;
1517 
1518 		/* release FIFO */
1519 		dwc_otg_common_rx_ack(sc);
1520 		return (0);		/* we are complete */
1521 	}
1522 
1523 	usbd_copy_in(td->pc, td->offset, sc->sc_rx_bounce_buffer, count);
1524 	td->remainder -= count;
1525 	td->offset += count;
1526 
1527 	/* release FIFO */
1528 	dwc_otg_common_rx_ack(sc);
1529 
1530 	/* check if we are complete */
1531 	if ((td->remainder == 0) || got_short) {
1532 		if (td->short_pkt) {
1533 			/* we are complete */
1534 			return (0);
1535 		}
1536 		/* else need to receive a zero length packet */
1537 	}
1538 
1539 not_complete:
1540 
1541 	temp = sc->sc_out_ctl[td->ep_no];
1542 
1543 	temp |= DOEPCTL_EPENA | DOEPCTL_CNAK;
1544 
1545 	DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(td->ep_no), temp);
1546 
1547 	/* enable SETUP and transfer complete interrupt */
1548 	if (td->ep_no == 0) {
1549 		DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
1550 		    DXEPTSIZ_SET_NPKT(1) |
1551 		    DXEPTSIZ_SET_NBYTES(td->max_packet_size));
1552 	} else {
1553 		/* allow reception of multiple packets */
1554 		DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(td->ep_no),
1555 		    DXEPTSIZ_SET_MULTI(1) |
1556 		    DXEPTSIZ_SET_NPKT(4) |
1557 		    DXEPTSIZ_SET_NBYTES(4 *
1558 		    ((td->max_packet_size + 3) & ~3)));
1559 	}
1560 	return (1);			/* not complete */
1561 }
1562 
1563 static uint8_t
1564 dwc_otg_host_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1565 {
1566 	uint32_t count;
1567 	uint32_t hcint;
1568 	uint32_t hcchar;
1569 	uint8_t delta;
1570 	uint8_t channel;
1571 
1572 	channel = td->channel[td->tt_channel_tog];
1573 
1574 	if (channel < DWC_OTG_MAX_CHANNELS) {
1575 		hcint = sc->sc_chan_state[channel].hcint;
1576 
1577 		DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1578 		    channel, td->state, hcint,
1579 		    DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1580 		    DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1581 	} else {
1582 		hcint = 0;
1583 		goto check_state;
1584 	}
1585 
1586 	if (hcint & (HCINT_RETRY |
1587 	    HCINT_ACK | HCINT_NYET)) {
1588 		/* give success bits priority over failure bits */
1589 	} else if (hcint & HCINT_STALL) {
1590 		DPRINTF("CH=%d STALL\n", channel);
1591 		td->error_stall = 1;
1592 		td->error_any = 1;
1593 		goto complete;
1594 	} else if (hcint & HCINT_ERRORS) {
1595 		DPRINTF("CH=%d ERROR\n", channel);
1596 		td->errcnt++;
1597 		if (td->hcsplt != 0 || td->errcnt >= 3) {
1598 			td->error_any = 1;
1599 			goto complete;
1600 		}
1601 	}
1602 
1603 	if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1604 	    HCINT_ACK | HCINT_NYET)) {
1605 
1606 		if (!(hcint & HCINT_ERRORS))
1607 			td->errcnt = 0;
1608 	}
1609 
1610 check_state:
1611 	switch (td->state) {
1612 	case DWC_CHAN_ST_START:
1613 		goto send_pkt;
1614 
1615 	case DWC_CHAN_ST_WAIT_ANE:
1616 		if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1617 			td->did_nak++;
1618 			td->tt_scheduled = 0;
1619 			goto send_pkt;
1620 		} else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1621 			td->offset += td->tx_bytes;
1622 			td->remainder -= td->tx_bytes;
1623 			td->toggle ^= 1;
1624 			td->did_nak = 0;
1625 			td->tt_scheduled = 0;
1626 
1627 			/* check remainder */
1628 			if (td->remainder == 0) {
1629 				if (td->short_pkt)
1630 					goto complete;
1631 
1632 				/*
1633 				 * Else we need to transmit a short
1634 				 * packet:
1635 				 */
1636 			}
1637 			goto send_pkt;
1638 		}
1639 		break;
1640 
1641 	case DWC_CHAN_ST_WAIT_S_ANE:
1642 		if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1643 			td->did_nak++;
1644 			td->tt_scheduled = 0;
1645 			goto send_pkt;
1646 		} else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1647 			td->did_nak = 0;
1648 			goto send_cpkt;
1649 		}
1650 		break;
1651 
1652 	case DWC_CHAN_ST_WAIT_C_ANE:
1653 		if (hcint & HCINT_NYET) {
1654 			goto send_cpkt;
1655 		} else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1656 			td->did_nak++;
1657 			td->tt_scheduled = 0;
1658 			goto send_pkt;
1659 		} else if (hcint & HCINT_ACK) {
1660 			td->offset += td->tx_bytes;
1661 			td->remainder -= td->tx_bytes;
1662 			td->toggle ^= 1;
1663 			td->did_nak = 0;
1664 			td->tt_scheduled = 0;
1665 
1666 			/* check remainder */
1667 			if (td->remainder == 0) {
1668 				if (td->short_pkt)
1669 					goto complete;
1670 
1671 				/* else we need to transmit a short packet */
1672 			}
1673 			goto send_pkt;
1674 		}
1675 		break;
1676 
1677 	case DWC_CHAN_ST_WAIT_C_PKT:
1678 		goto send_cpkt;
1679 
1680 	case DWC_CHAN_ST_TX_WAIT_ISOC:
1681 
1682 		/* Check if isochronous OUT traffic is complete */
1683 		if ((hcint & HCINT_HCH_DONE_MASK) == 0)
1684 			break;
1685 
1686 		td->offset += td->tx_bytes;
1687 		td->remainder -= td->tx_bytes;
1688 
1689 		if (td->hcsplt != 0 || td->remainder == 0)
1690 			goto complete;
1691 
1692 		/* check for next packet */
1693 		if (td->max_packet_count > 1)
1694 			td->tt_xactpos++;
1695 
1696 		/* free existing channel, if any */
1697 		dwc_otg_host_channel_free(sc, td, td->tt_channel_tog);
1698 
1699 		td->state = DWC_CHAN_ST_TX_PKT_ISOC;
1700 
1701 		/* FALLTHROUGH */
1702 
1703 	case DWC_CHAN_ST_TX_PKT_ISOC:
1704 		if (dwc_otg_host_channel_alloc(sc, td, 0, 1))
1705 			break;
1706 		channel = td->channel[0];
1707 		goto send_isoc_pkt;
1708 	default:
1709 		break;
1710 	}
1711 	goto busy;
1712 
1713 send_pkt:
1714 	/* free existing channel(s), if any */
1715 	dwc_otg_host_channel_free(sc, td, 0);
1716 	dwc_otg_host_channel_free(sc, td, 1);
1717 
1718 	if (td->hcsplt != 0) {
1719 		delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1720 		if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1721 			td->state = DWC_CHAN_ST_START;
1722 			goto busy;
1723 		}
1724 		delta = sc->sc_last_frame_num - td->tt_start_slot;
1725 		if (delta > 5) {
1726 			/* missed it */
1727 			td->tt_scheduled = 0;
1728 			td->state = DWC_CHAN_ST_START;
1729 			goto busy;
1730 		}
1731 	} else if (dwc_otg_host_rate_check(sc, td)) {
1732 		td->state = DWC_CHAN_ST_START;
1733 		goto busy;
1734 	}
1735 
1736 	/* allocate a new channel */
1737 	if (dwc_otg_host_channel_alloc(sc, td, 0, 1)) {
1738 		td->state = DWC_CHAN_ST_START;
1739 		goto busy;
1740 	}
1741 
1742 	channel = td->channel[0];
1743 
1744 	/* set toggle, if any */
1745 	if (td->set_toggle) {
1746 		td->set_toggle = 0;
1747 		td->toggle = 1;
1748 	}
1749 
1750 	if (td->ep_type == UE_ISOCHRONOUS) {
1751 send_isoc_pkt:
1752 		/* Isochronous OUT transfers don't have any ACKs */
1753 		td->state = DWC_CHAN_ST_TX_WAIT_ISOC;
1754 		td->hcsplt &= ~HCSPLT_COMPSPLT;
1755 		if (td->hcsplt != 0) {
1756 			/* get maximum transfer length */
1757 			count = td->remainder;
1758 			if (count > HCSPLT_XACTLEN_BURST) {
1759 				DPRINTF("TT overflow\n");
1760 				td->error = 1;
1761 				goto complete;
1762 			}
1763 			/* Update transaction position */
1764 			td->hcsplt &= ~HCSPLT_XACTPOS_MASK;
1765 			td->hcsplt |= (HCSPLT_XACTPOS_ALL << HCSPLT_XACTPOS_SHIFT);
1766 		} else {
1767 			/* send one packet at a time */
1768 			count = td->max_packet_size;
1769 			if (td->remainder < count) {
1770 				/* we have a short packet */
1771 				td->short_pkt = 1;
1772 				count = td->remainder;
1773 			}
1774 		}
1775 	} else if (td->hcsplt != 0) {
1776 
1777 		td->hcsplt &= ~HCSPLT_COMPSPLT;
1778 
1779 		/* Wait for ACK/NAK/ERR from TT */
1780 		td->state = DWC_CHAN_ST_WAIT_S_ANE;
1781 
1782 		/* send one packet at a time */
1783 		count = td->max_packet_size;
1784 		if (td->remainder < count) {
1785 			/* we have a short packet */
1786 			td->short_pkt = 1;
1787 			count = td->remainder;
1788 		}
1789 	} else {
1790 		/* Wait for ACK/NAK/STALL from device */
1791 		td->state = DWC_CHAN_ST_WAIT_ANE;
1792 
1793 		/* send one packet at a time */
1794 		count = td->max_packet_size;
1795 		if (td->remainder < count) {
1796 			/* we have a short packet */
1797 			td->short_pkt = 1;
1798 			count = td->remainder;
1799 		}
1800 	}
1801 
1802 	/* check for High-Speed multi-packets */
1803 	if ((td->hcsplt == 0) && (td->max_packet_count > 1)) {
1804 		if (td->npkt == 0) {
1805 			if (td->remainder >= (3 * td->max_packet_size))
1806 				td->npkt = 3;
1807 			else if (td->remainder >= (2 * td->max_packet_size))
1808 				td->npkt = 2;
1809 			else
1810 				td->npkt = 1;
1811 
1812 			if (td->npkt > td->max_packet_count)
1813 				td->npkt = td->max_packet_count;
1814 
1815 			td->tt_xactpos = 1;	/* overload */
1816 		}
1817 		if (td->tt_xactpos == td->npkt) {
1818 			if (td->npkt == 1) {
1819 				DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1820 				    (count << HCTSIZ_XFERSIZE_SHIFT) |
1821 				    (1 << HCTSIZ_PKTCNT_SHIFT) |
1822 				    (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1823 			} else if (td->npkt == 2) {
1824 				DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1825 				    (count << HCTSIZ_XFERSIZE_SHIFT) |
1826 				    (1 << HCTSIZ_PKTCNT_SHIFT) |
1827 				    (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT));
1828 			} else {
1829 				DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1830 				    (count << HCTSIZ_XFERSIZE_SHIFT) |
1831 				    (1 << HCTSIZ_PKTCNT_SHIFT) |
1832 				    (HCTSIZ_PID_DATA2 << HCTSIZ_PID_SHIFT));
1833 			}
1834 			td->npkt = 0;
1835 		} else {
1836 			DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1837 			    (count << HCTSIZ_XFERSIZE_SHIFT) |
1838 			    (1 << HCTSIZ_PKTCNT_SHIFT) |
1839 			    (HCTSIZ_PID_MDATA << HCTSIZ_PID_SHIFT));
1840 		}
1841 	} else {
1842 		/* TODO: HCTSIZ_DOPNG */
1843 
1844 		DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1845 		    (count << HCTSIZ_XFERSIZE_SHIFT) |
1846 		    (1 << HCTSIZ_PKTCNT_SHIFT) |
1847 		    (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1848 		    (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1849 	}
1850 
1851 	DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1852 
1853 	hcchar = td->hcchar;
1854 	hcchar &= ~HCCHAR_EPDIR_IN;
1855 
1856 	/* send after next SOF event */
1857 	if ((sc->sc_last_frame_num & 1) == 0)
1858 		hcchar |= HCCHAR_ODDFRM;
1859 	else
1860 		hcchar &= ~HCCHAR_ODDFRM;
1861 
1862 	/* must enable before writing data to FIFO */
1863 	DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1864 
1865 	if (count != 0) {
1866 
1867 		/* clear topmost word before copy */
1868 		sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
1869 
1870 		/* copy out data */
1871 		usbd_copy_out(td->pc, td->offset,
1872 		    sc->sc_tx_bounce_buffer, count);
1873 
1874 		/* transfer data into FIFO */
1875 		bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
1876 		    DOTG_DFIFO(channel),
1877 		    sc->sc_tx_bounce_buffer, (count + 3) / 4);
1878 	}
1879 
1880 	/* store number of bytes transmitted */
1881 	td->tx_bytes = count;
1882 	goto busy;
1883 
1884 send_cpkt:
1885 	/* free existing channel, if any */
1886 	dwc_otg_host_channel_free(sc, td, td->tt_channel_tog);
1887 
1888 	delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1889 	if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1890 		td->state = DWC_CHAN_ST_WAIT_C_PKT;
1891 		goto busy;
1892 	}
1893 	delta = sc->sc_last_frame_num - td->tt_start_slot;
1894 	if (delta > DWC_OTG_TT_SLOT_MAX) {
1895 		/* we missed the service interval */
1896 		if (td->ep_type != UE_ISOCHRONOUS)
1897 			td->error_any = 1;
1898 		goto complete;
1899 	}
1900 
1901 	/* allocate a new channel */
1902 	if (dwc_otg_host_channel_alloc(sc, td, td->tt_channel_tog, 0)) {
1903 		td->state = DWC_CHAN_ST_WAIT_C_PKT;
1904 		goto busy;
1905 	}
1906 
1907 	channel = td->channel[td->tt_channel_tog];
1908 
1909  	td->hcsplt |= HCSPLT_COMPSPLT;
1910 	td->state = DWC_CHAN_ST_WAIT_C_ANE;
1911 
1912 	DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1913 	    (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1914 
1915 	DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1916 
1917 	hcchar = td->hcchar;
1918 	hcchar &= ~HCCHAR_EPDIR_IN;
1919 
1920 	/* check if other channel is allocated */
1921 	if (td->channel[td->tt_channel_tog ^ 1] < DWC_OTG_MAX_CHANNELS) {
1922 		/* second - receive after next SOF event */
1923 		if ((sc->sc_last_frame_num & 1) != 0)
1924 			hcchar |= HCCHAR_ODDFRM;
1925 		else
1926 			hcchar &= ~HCCHAR_ODDFRM;
1927 
1928 		/* other channel is next */
1929 		td->tt_channel_tog ^= 1;
1930 		/* wait until next slot before trying again */
1931 		td->tt_complete_slot++;
1932 
1933 		/* must enable channel before data can be received */
1934 		DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1935 	} else {
1936 		/* first - receive after second next SOF event */
1937 		if ((sc->sc_last_frame_num & 1) == 0)
1938 			hcchar |= HCCHAR_ODDFRM;
1939 		else
1940 			hcchar &= ~HCCHAR_ODDFRM;
1941 
1942 		/* must enable channel before data can be received */
1943 		DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1944 
1945 		if (td->hcsplt != 0) {
1946 			if (td->ep_type == UE_ISOCHRONOUS || td->ep_type == UE_INTERRUPT) {
1947 				/* allocate a second channel */
1948 				td->tt_channel_tog ^= 1;
1949 				goto send_cpkt;
1950 			} else {
1951 				/* wait until next slot before trying again */
1952 				td->tt_complete_slot++;
1953 			}
1954 		}
1955 	}
1956 busy:
1957 	return (1);	/* busy */
1958 
1959 complete:
1960 	dwc_otg_host_channel_free(sc, td, 0);
1961 	dwc_otg_host_channel_free(sc, td, 1);
1962 	return (0);	/* complete */
1963 }
1964 
1965 static uint8_t
1966 dwc_otg_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1967 {
1968 	uint32_t max_buffer;
1969 	uint32_t count;
1970 	uint32_t fifo_left;
1971 	uint32_t mpkt;
1972 	uint32_t temp;
1973 	uint8_t to;
1974 
1975 	to = 3;				/* don't loop forever! */
1976 
1977 	max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
1978 
1979 repeat:
1980 	/* check for for endpoint 0 data */
1981 
1982 	temp = sc->sc_last_rx_status;
1983 
1984 	if ((td->ep_no == 0) && (temp != 0) &&
1985 	    (GRXSTSRD_CHNUM_GET(temp) == 0)) {
1986 
1987 		if ((temp & GRXSTSRD_PKTSTS_MASK) !=
1988 		    GRXSTSRD_STP_DATA) {
1989 
1990 			/* dump data - wrong direction */
1991 			dwc_otg_common_rx_ack(sc);
1992 		} else {
1993 			/*
1994 			 * The current transfer was cancelled
1995 			 * by the USB Host:
1996 			 */
1997 			td->error_any = 1;
1998 			return (0);		/* complete */
1999 		}
2000 	}
2001 
2002 	/* fill in more TX data, if possible */
2003 	if (td->tx_bytes != 0) {
2004 
2005 		uint16_t cpkt;
2006 
2007 		/* check if packets have been transferred */
2008 		temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2009 
2010 		/* get current packet number */
2011 		cpkt = DXEPTSIZ_GET_NPKT(temp);
2012 
2013 		if (cpkt >= td->npkt) {
2014 			fifo_left = 0;
2015 		} else {
2016 			if (max_buffer != 0) {
2017 				fifo_left = (td->npkt - cpkt) *
2018 				    td->max_packet_size;
2019 
2020 				if (fifo_left > max_buffer)
2021 					fifo_left = max_buffer;
2022 			} else {
2023 				fifo_left = td->max_packet_size;
2024 			}
2025 		}
2026 
2027 		count = td->tx_bytes;
2028 		if (count > fifo_left)
2029 			count = fifo_left;
2030 
2031 		if (count != 0) {
2032 
2033 			/* clear topmost word before copy */
2034 			sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
2035 
2036 			/* copy out data */
2037 			usbd_copy_out(td->pc, td->offset,
2038 			    sc->sc_tx_bounce_buffer, count);
2039 
2040 			/* transfer data into FIFO */
2041 			bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
2042 			    DOTG_DFIFO(td->ep_no),
2043 			    sc->sc_tx_bounce_buffer, (count + 3) / 4);
2044 
2045 			td->tx_bytes -= count;
2046 			td->remainder -= count;
2047 			td->offset += count;
2048 			td->npkt = cpkt;
2049 		}
2050 		if (td->tx_bytes != 0)
2051 			goto not_complete;
2052 
2053 		/* check remainder */
2054 		if (td->remainder == 0) {
2055 			if (td->short_pkt)
2056 				return (0);	/* complete */
2057 
2058 			/* else we need to transmit a short packet */
2059 		}
2060 	}
2061 
2062 	if (!to--)
2063 		goto not_complete;
2064 
2065 	/* check if not all packets have been transferred */
2066 	temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2067 
2068 	if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2069 
2070 		DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
2071 		    "DIEPCTL=0x%08x\n", td->ep_no,
2072 		    DXEPTSIZ_GET_NPKT(temp),
2073 		    temp, DWC_OTG_READ_4(sc, DOTG_DIEPCTL(td->ep_no)));
2074 
2075 		goto not_complete;
2076 	}
2077 
2078 	DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
2079 
2080 	/* try to optimise by sending more data */
2081 	if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
2082 
2083 		/* send multiple packets at the same time */
2084 		mpkt = max_buffer / td->max_packet_size;
2085 
2086 		if (mpkt > 0x3FE)
2087 			mpkt = 0x3FE;
2088 
2089 		count = td->remainder;
2090 		if (count > 0x7FFFFF)
2091 			count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
2092 
2093 		td->npkt = count / td->max_packet_size;
2094 
2095 		/*
2096 		 * NOTE: We could use 0x3FE instead of "mpkt" in the
2097 		 * check below to get more throughput, but then we
2098 		 * have a dependency towards non-generic chip features
2099 		 * to disable the TX-FIFO-EMPTY interrupts on a per
2100 		 * endpoint basis. Increase the maximum buffer size of
2101 		 * the IN endpoint to increase the performance.
2102 		 */
2103 		if (td->npkt > mpkt) {
2104 			td->npkt = mpkt;
2105 			count = td->max_packet_size * mpkt;
2106 		} else if ((count == 0) || (count % td->max_packet_size)) {
2107 			/* we are transmitting a short packet */
2108 			td->npkt++;
2109 			td->short_pkt = 1;
2110 		}
2111 	} else {
2112 		/* send one packet at a time */
2113 		mpkt = 1;
2114 		count = td->max_packet_size;
2115 		if (td->remainder < count) {
2116 			/* we have a short packet */
2117 			td->short_pkt = 1;
2118 			count = td->remainder;
2119 		}
2120 		td->npkt = 1;
2121 	}
2122 	DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(td->ep_no),
2123 	    DXEPTSIZ_SET_MULTI(1) |
2124 	    DXEPTSIZ_SET_NPKT(td->npkt) |
2125 	    DXEPTSIZ_SET_NBYTES(count));
2126 
2127 	/* make room for buffering */
2128 	td->npkt += mpkt;
2129 
2130 	temp = sc->sc_in_ctl[td->ep_no];
2131 
2132 	/* must enable before writing data to FIFO */
2133 	DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(td->ep_no), temp |
2134 	    DIEPCTL_EPENA |
2135 	    DIEPCTL_CNAK);
2136 
2137 	td->tx_bytes = count;
2138 
2139 	/* check remainder */
2140 	if (td->tx_bytes == 0 &&
2141 	    td->remainder == 0) {
2142 		if (td->short_pkt)
2143 			return (0);	/* complete */
2144 
2145 		/* else we need to transmit a short packet */
2146 	}
2147 	goto repeat;
2148 
2149 not_complete:
2150 	return (1);			/* not complete */
2151 }
2152 
2153 static uint8_t
2154 dwc_otg_data_tx_sync(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2155 {
2156 	uint32_t temp;
2157 
2158 	/*
2159 	 * If all packets are transferred we are complete:
2160 	 */
2161 	temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2162 
2163 	/* check that all packets have been transferred */
2164 	if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2165 		DPRINTFN(5, "busy ep=%d\n", td->ep_no);
2166 		goto not_complete;
2167 	}
2168 	return (0);
2169 
2170 not_complete:
2171 
2172 	/* we only want to know if there is a SETUP packet or free IN packet */
2173 
2174 	temp = sc->sc_last_rx_status;
2175 
2176 	if ((td->ep_no == 0) && (temp != 0) &&
2177 	    (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2178 
2179 		if ((temp & GRXSTSRD_PKTSTS_MASK) ==
2180 		    GRXSTSRD_STP_DATA) {
2181 			DPRINTFN(5, "faking complete\n");
2182 			/*
2183 			 * Race condition: We are complete!
2184 			 */
2185 			return (0);
2186 		} else {
2187 			/* dump data - wrong direction */
2188 			dwc_otg_common_rx_ack(sc);
2189 		}
2190 	}
2191 	return (1);			/* not complete */
2192 }
2193 
2194 static void
2195 dwc_otg_xfer_do_fifo(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2196 {
2197 	struct dwc_otg_td *td;
2198 	uint8_t toggle;
2199 	uint8_t tmr_val;
2200 	uint8_t tmr_res;
2201 
2202 	DPRINTFN(9, "\n");
2203 
2204 	td = xfer->td_transfer_cache;
2205 	if (td == NULL)
2206 		return;
2207 
2208 	while (1) {
2209 		if ((td->func) (sc, td)) {
2210 			/* operation in progress */
2211 			break;
2212 		}
2213 		if (((void *)td) == xfer->td_transfer_last) {
2214 			goto done;
2215 		}
2216 		if (td->error_any) {
2217 			goto done;
2218 		} else if (td->remainder > 0) {
2219 			/*
2220 			 * We had a short transfer. If there is no alternate
2221 			 * next, stop processing !
2222 			 */
2223 			if (!td->alt_next)
2224 				goto done;
2225 		}
2226 
2227 		/*
2228 		 * Fetch the next transfer descriptor and transfer
2229 		 * some flags to the next transfer descriptor
2230 		 */
2231 		tmr_res = td->tmr_res;
2232 		tmr_val = td->tmr_val;
2233 		toggle = td->toggle;
2234 		td = td->obj_next;
2235 		xfer->td_transfer_cache = td;
2236 		td->toggle = toggle;	/* transfer toggle */
2237 		td->tmr_res = tmr_res;
2238 		td->tmr_val = tmr_val;
2239 	}
2240 	return;
2241 
2242 done:
2243 	xfer->td_transfer_cache = NULL;
2244 	sc->sc_xfer_complete = 1;
2245 }
2246 
2247 static uint8_t
2248 dwc_otg_xfer_do_complete(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2249 {
2250 	struct dwc_otg_td *td;
2251 
2252 	DPRINTFN(9, "\n");
2253 
2254 	td = xfer->td_transfer_cache;
2255 	if (td == NULL) {
2256 		/* compute all actual lengths */
2257 		dwc_otg_standard_done(xfer);
2258 		return (1);
2259 	}
2260 	return (0);
2261 }
2262 
2263 static void
2264 dwc_otg_timer(void *_sc)
2265 {
2266 	struct dwc_otg_softc *sc = _sc;
2267 	struct usb_xfer *xfer;
2268 	struct dwc_otg_td *td;
2269 
2270 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2271 
2272 	DPRINTF("\n");
2273 
2274 	USB_BUS_SPIN_LOCK(&sc->sc_bus);
2275 
2276 	/* increment timer value */
2277 	sc->sc_tmr_val++;
2278 
2279 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2280 		td = xfer->td_transfer_cache;
2281 		if (td != NULL) {
2282 			/* reset NAK counter */
2283 			td->did_nak = 0;
2284 		}
2285 	}
2286 
2287 	/* enable SOF interrupt, which will poll jobs */
2288 	dwc_otg_enable_sof_irq(sc);
2289 
2290 	USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2291 
2292 	if (sc->sc_timer_active) {
2293 		/* restart timer */
2294 		usb_callout_reset(&sc->sc_timer,
2295 		    hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2296 		    &dwc_otg_timer, sc);
2297 	}
2298 }
2299 
2300 static void
2301 dwc_otg_timer_start(struct dwc_otg_softc *sc)
2302 {
2303 	if (sc->sc_timer_active != 0)
2304 		return;
2305 
2306 	sc->sc_timer_active = 1;
2307 
2308 	/* restart timer */
2309 	usb_callout_reset(&sc->sc_timer,
2310 	    hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2311 	    &dwc_otg_timer, sc);
2312 }
2313 
2314 static void
2315 dwc_otg_timer_stop(struct dwc_otg_softc *sc)
2316 {
2317 	if (sc->sc_timer_active == 0)
2318 		return;
2319 
2320 	sc->sc_timer_active = 0;
2321 
2322 	/* stop timer */
2323 	usb_callout_stop(&sc->sc_timer);
2324 }
2325 
2326 static void
2327 dwc_otg_host_channel_disable(struct dwc_otg_softc *sc, uint8_t x)
2328 {
2329 	uint32_t hcchar;
2330 
2331 	hcchar = DWC_OTG_READ_4(sc, DOTG_HCCHAR(x));
2332 
2333 	/* disable host channel, if any */
2334 	if (hcchar & (HCCHAR_CHENA | HCCHAR_CHDIS)) {
2335 		/* disable channel */
2336 		DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(x),
2337 		    HCCHAR_CHENA | HCCHAR_CHDIS);
2338 		/* wait for chip to get its brains in order */
2339 		sc->sc_chan_state[x].wait_sof = 2;
2340 	}
2341 
2342 	/* release TX FIFO usage, if any */
2343 	sc->sc_tx_cur_p_level -= sc->sc_chan_state[x].tx_p_size;
2344 	sc->sc_tx_cur_np_level -= sc->sc_chan_state[x].tx_np_size;
2345 
2346 	/* don't release TX FIFO usage twice */
2347 	sc->sc_chan_state[x].tx_p_size = 0;
2348 	sc->sc_chan_state[x].tx_np_size = 0;
2349 }
2350 
2351 static uint8_t
2352 dwc_otg_update_host_transfer_schedule(struct dwc_otg_softc *sc)
2353 {
2354 	TAILQ_HEAD(, usb_xfer) head;
2355 	struct usb_xfer *xfer;
2356 	struct usb_xfer *xfer_next;
2357 	struct dwc_otg_td *td;
2358 	uint16_t temp;
2359 	uint8_t x;
2360 
2361 	temp = DWC_OTG_READ_4(sc, DOTG_HFNUM) & DWC_OTG_FRAME_MASK;
2362 
2363 	if (sc->sc_last_frame_num == temp)
2364 		return (0);
2365 
2366 	sc->sc_last_frame_num = temp;
2367 
2368 	TAILQ_INIT(&head);
2369 
2370 	for (x = 0; x != sc->sc_host_ch_max; x++) {
2371 		if (sc->sc_chan_state[x].wait_sof == 0)
2372 			continue;
2373 
2374 		sc->sc_needsof = 1;
2375 		if (--(sc->sc_chan_state[x].wait_sof) == 0)
2376 			dwc_otg_host_channel_disable(sc, x);
2377 	}
2378 
2379 	if ((temp & 7) == 0) {
2380 		TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2381 			td = xfer->td_transfer_cache;
2382 			if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2383 				continue;
2384 
2385 			/* execute more frames */
2386 			td->tmr_val = 0;
2387 
2388 			sc->sc_needsof = 1;
2389 
2390 			if (td->hcsplt == 0 || td->tt_scheduled != 0)
2391 				continue;
2392 
2393 			/* Start ASAP */
2394 			td->tt_start_slot = temp + 0;
2395 			td->tt_complete_slot = temp + 2;
2396 			td->tt_scheduled = 1;
2397 			TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2398 			TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2399 		}
2400 
2401 		TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2402 			td = xfer->td_transfer_cache;
2403 			if (td == NULL || td->ep_type != UE_INTERRUPT)
2404 				continue;
2405 
2406 			if (td->tt_scheduled != 0) {
2407 				sc->sc_needsof = 1;
2408 				continue;
2409 			}
2410 
2411 			if (dwc_otg_host_rate_check_interrupt(sc, td))
2412 				continue;
2413 
2414 			if (td->hcsplt == 0) {
2415 				sc->sc_needsof = 1;
2416 				td->tt_scheduled = 1;
2417 				continue;
2418 			}
2419 
2420 			/* start ASAP */
2421 			td->tt_start_slot = temp + 0;
2422 			td->tt_complete_slot = temp + 2;
2423 			sc->sc_needsof = 1;
2424 			td->tt_scheduled = 1;
2425 			TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2426 			TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2427 		}
2428 
2429 		TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2430 			td = xfer->td_transfer_cache;
2431 			if (td == NULL ||
2432 			    td->ep_type != UE_CONTROL ||
2433 			    td->did_nak >= DWC_OTG_NAK_MAX) {
2434 				continue;
2435 			}
2436 
2437 			sc->sc_needsof = 1;
2438 
2439 			if (td->hcsplt == 0 || td->tt_scheduled != 0)
2440 				continue;
2441 
2442 			/* start ASAP */
2443 			td->tt_start_slot = temp + 0;
2444 			td->tt_complete_slot = temp + 1;
2445 			td->tt_scheduled = 1;
2446 			TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2447 			TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2448 		}
2449 	}
2450 	if ((temp & 7) < 6) {
2451 		TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2452 			td = xfer->td_transfer_cache;
2453 			if (td == NULL ||
2454 			    td->ep_type != UE_BULK ||
2455 			    td->did_nak >= DWC_OTG_NAK_MAX) {
2456 				continue;
2457 			}
2458 
2459 			sc->sc_needsof = 1;
2460 
2461 			if (td->hcsplt == 0 || td->tt_scheduled != 0)
2462 				continue;
2463 
2464 			/* start ASAP */
2465 			td->tt_start_slot = temp + 0;
2466 			td->tt_complete_slot = temp + 1;
2467 			td->tt_scheduled = 1;
2468 			TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2469 			TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2470 		}
2471 	}
2472 
2473 	/* Put TT transfers in execution order at the end */
2474 	TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2475 
2476 	/* move all TT transfers in front, keeping the current order */
2477 	TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2478 		td = xfer->td_transfer_cache;
2479 		if (td == NULL || td->hcsplt == 0)
2480 			continue;
2481 		TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2482 		TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2483 	}
2484 	TAILQ_CONCAT(&head, &sc->sc_bus.intr_q.head, wait_entry);
2485 	TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2486 
2487 	/* put non-TT BULK transfers last */
2488 	TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2489 		td = xfer->td_transfer_cache;
2490 		if (td == NULL || td->hcsplt != 0 || td->ep_type != UE_BULK)
2491 			continue;
2492 		TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2493 		TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2494 	}
2495 	TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2496 
2497 	if ((temp & 7) == 0) {
2498 
2499 		DPRINTFN(12, "SOF interrupt #%d, needsof=%d\n",
2500 		    (int)temp, (int)sc->sc_needsof);
2501 
2502 		/* update SOF IRQ mask */
2503 		if (sc->sc_irq_mask & GINTMSK_SOFMSK) {
2504 			if (sc->sc_needsof == 0) {
2505 				sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2506 				DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2507 			}
2508 		} else {
2509 			if (sc->sc_needsof != 0) {
2510 				sc->sc_irq_mask |= GINTMSK_SOFMSK;
2511 				DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2512 			}
2513 		}
2514 
2515 		/* clear need SOF flag */
2516 		sc->sc_needsof = 0;
2517 	}
2518 	return (1);
2519 }
2520 
2521 static void
2522 dwc_otg_interrupt_poll(struct dwc_otg_softc *sc)
2523 {
2524 	struct usb_xfer *xfer;
2525 	uint32_t temp;
2526 	uint8_t got_rx_status;
2527 	uint8_t x;
2528 
2529 repeat:
2530 	/* get all channel interrupts */
2531 	for (x = 0; x != sc->sc_host_ch_max; x++) {
2532 		temp = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
2533 		if (temp != 0) {
2534 			DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), temp);
2535 			temp &= ~HCINT_SOFTWARE_ONLY;
2536 			sc->sc_chan_state[x].hcint |= temp;
2537 		}
2538 	}
2539 
2540 	if (sc->sc_last_rx_status == 0) {
2541 
2542 		temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2543 		if (temp & GINTSTS_RXFLVL) {
2544 			/* pop current status */
2545 			sc->sc_last_rx_status =
2546 			    DWC_OTG_READ_4(sc, DOTG_GRXSTSPD);
2547 		}
2548 
2549 		if (sc->sc_last_rx_status != 0) {
2550 
2551 			uint8_t ep_no;
2552 
2553 			temp = sc->sc_last_rx_status &
2554 			    GRXSTSRD_PKTSTS_MASK;
2555 
2556 			/* non-data messages we simply skip */
2557 			if (temp != GRXSTSRD_STP_DATA &&
2558 			    temp != GRXSTSRD_OUT_DATA) {
2559 				dwc_otg_common_rx_ack(sc);
2560 				goto repeat;
2561 			}
2562 
2563 			temp = GRXSTSRD_BCNT_GET(
2564 			    sc->sc_last_rx_status);
2565 			ep_no = GRXSTSRD_CHNUM_GET(
2566 			    sc->sc_last_rx_status);
2567 
2568 			/* receive data, if any */
2569 			if (temp != 0) {
2570 				DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
2571 				bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
2572 				    DOTG_DFIFO(ep_no),
2573 				    sc->sc_rx_bounce_buffer, (temp + 3) / 4);
2574 			}
2575 
2576 			/* check if we should dump the data */
2577 			if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2578 				dwc_otg_common_rx_ack(sc);
2579 				goto repeat;
2580 			}
2581 
2582 			got_rx_status = 1;
2583 
2584 			DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
2585 			    sc->sc_last_rx_status, ep_no,
2586 			    (sc->sc_last_rx_status >> 15) & 3,
2587 			    GRXSTSRD_BCNT_GET(sc->sc_last_rx_status),
2588 			    (sc->sc_last_rx_status >> 17) & 15);
2589 		} else {
2590 			got_rx_status = 0;
2591 		}
2592 	} else {
2593 		uint8_t ep_no;
2594 
2595 		ep_no = GRXSTSRD_CHNUM_GET(
2596 		    sc->sc_last_rx_status);
2597 
2598 		/* check if we should dump the data */
2599 		if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2600 			dwc_otg_common_rx_ack(sc);
2601 			goto repeat;
2602 		}
2603 
2604 		got_rx_status = 1;
2605 	}
2606 
2607 	/* execute FIFOs */
2608 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
2609 		dwc_otg_xfer_do_fifo(sc, xfer);
2610 
2611 	if (got_rx_status) {
2612 		/* check if data was consumed */
2613 		if (sc->sc_last_rx_status == 0)
2614 			goto repeat;
2615 
2616 		/* disable RX FIFO level interrupt */
2617 		sc->sc_irq_mask &= ~GINTMSK_RXFLVLMSK;
2618 		DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2619 	}
2620 
2621 	if (sc->sc_flags.status_device_mode == 0 && sc->sc_xfer_complete == 0) {
2622 		/* update host transfer schedule, so that new transfers can be issued */
2623 		if (dwc_otg_update_host_transfer_schedule(sc))
2624 			goto repeat;
2625 	}
2626 }
2627 
2628 static void
2629 dwc_otg_interrupt_complete(struct dwc_otg_softc *sc)
2630 {
2631 	struct usb_xfer *xfer;
2632 repeat:
2633 	/* scan for completion events */
2634 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2635 		if (dwc_otg_xfer_do_complete(sc, xfer))
2636 			goto repeat;
2637 	}
2638 }
2639 
2640 static void
2641 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
2642 {
2643 	DPRINTFN(5, "vbus = %u\n", is_on);
2644 
2645 	/*
2646 	 * If the USB host mode is forced, then assume VBUS is always
2647 	 * present else rely on the input to this function:
2648 	 */
2649 	if ((is_on != 0) || (sc->sc_mode == DWC_MODE_HOST)) {
2650 
2651 		if (!sc->sc_flags.status_vbus) {
2652 			sc->sc_flags.status_vbus = 1;
2653 
2654 			/* complete root HUB interrupt endpoint */
2655 
2656 			dwc_otg_root_intr(sc);
2657 		}
2658 	} else {
2659 		if (sc->sc_flags.status_vbus) {
2660 			sc->sc_flags.status_vbus = 0;
2661 			sc->sc_flags.status_bus_reset = 0;
2662 			sc->sc_flags.status_suspend = 0;
2663 			sc->sc_flags.change_suspend = 0;
2664 			sc->sc_flags.change_connect = 1;
2665 
2666 			/* complete root HUB interrupt endpoint */
2667 
2668 			dwc_otg_root_intr(sc);
2669 		}
2670 	}
2671 }
2672 
2673 int
2674 dwc_otg_filter_interrupt(void *arg)
2675 {
2676 	struct dwc_otg_softc *sc = arg;
2677 	int retval = FILTER_HANDLED;
2678 	uint32_t status;
2679 
2680 	/* read and clear interrupt status */
2681 	status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2682 
2683 	/* clear interrupts we are handling here */
2684 	DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & ~DWC_OTG_MSK_GINT_THREAD_IRQ);
2685 
2686 	/* check for USB state change interrupts */
2687 	if ((status & DWC_OTG_MSK_GINT_THREAD_IRQ) != 0)
2688 		retval = FILTER_SCHEDULE_THREAD;
2689 
2690 	/* clear all IN endpoint interrupts */
2691 	if (status & GINTSTS_IEPINT) {
2692 		uint32_t temp;
2693 		uint8_t x;
2694 
2695 		for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
2696 			temp = DWC_OTG_READ_4(sc, DOTG_DIEPINT(x));
2697 			if (temp & DIEPMSK_XFERCOMPLMSK) {
2698 				DWC_OTG_WRITE_4(sc, DOTG_DIEPINT(x),
2699 				    DIEPMSK_XFERCOMPLMSK);
2700 			}
2701 		}
2702 	}
2703 
2704 	USB_BUS_SPIN_LOCK(&sc->sc_bus);
2705 
2706 	/* poll FIFOs, if any */
2707 	dwc_otg_interrupt_poll(sc);
2708 
2709 	if (sc->sc_xfer_complete != 0)
2710 		retval = FILTER_SCHEDULE_THREAD;
2711 
2712 	USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2713 
2714 	return (retval);
2715 }
2716 
2717 void
2718 dwc_otg_interrupt(void *arg)
2719 {
2720 	struct dwc_otg_softc *sc = arg;
2721 	uint32_t status;
2722 
2723 	USB_BUS_LOCK(&sc->sc_bus);
2724 	USB_BUS_SPIN_LOCK(&sc->sc_bus);
2725 
2726 	/* read and clear interrupt status */
2727 	status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2728 
2729 	/* clear interrupts we are handling here */
2730 	DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & DWC_OTG_MSK_GINT_THREAD_IRQ);
2731 
2732 	DPRINTFN(14, "GINTSTS=0x%08x HAINT=0x%08x HFNUM=0x%08x\n",
2733 	    status, DWC_OTG_READ_4(sc, DOTG_HAINT),
2734 	    DWC_OTG_READ_4(sc, DOTG_HFNUM));
2735 
2736 	if (status & GINTSTS_USBRST) {
2737 
2738 		/* set correct state */
2739 		sc->sc_flags.status_device_mode = 1;
2740 		sc->sc_flags.status_bus_reset = 0;
2741 		sc->sc_flags.status_suspend = 0;
2742 		sc->sc_flags.change_suspend = 0;
2743 		sc->sc_flags.change_connect = 1;
2744 
2745 		/* Disable SOF interrupt */
2746 		sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2747 		/* Enable RX frame interrupt */
2748 		sc->sc_irq_mask |= GINTMSK_RXFLVLMSK;
2749 		DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2750 
2751 		/* complete root HUB interrupt endpoint */
2752 		dwc_otg_root_intr(sc);
2753 	}
2754 
2755 	/* check for any bus state change interrupts */
2756 	if (status & GINTSTS_ENUMDONE) {
2757 
2758 		uint32_t temp;
2759 
2760 		DPRINTFN(5, "end of reset\n");
2761 
2762 		/* set correct state */
2763 		sc->sc_flags.status_device_mode = 1;
2764 		sc->sc_flags.status_bus_reset = 1;
2765 		sc->sc_flags.status_suspend = 0;
2766 		sc->sc_flags.change_suspend = 0;
2767 		sc->sc_flags.change_connect = 1;
2768 		sc->sc_flags.status_low_speed = 0;
2769 		sc->sc_flags.port_enabled = 1;
2770 
2771 		/* reset FIFOs */
2772 		(void) dwc_otg_init_fifo(sc, DWC_MODE_DEVICE);
2773 
2774 		/* reset function address */
2775 		dwc_otg_set_address(sc, 0);
2776 
2777 		/* figure out enumeration speed */
2778 		temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
2779 		if (DSTS_ENUMSPD_GET(temp) == DSTS_ENUMSPD_HI)
2780 			sc->sc_flags.status_high_speed = 1;
2781 		else
2782 			sc->sc_flags.status_high_speed = 0;
2783 
2784 		/*
2785 		 * Disable resume and SOF interrupt, and enable
2786 		 * suspend and RX frame interrupt:
2787 		 */
2788 		sc->sc_irq_mask &= ~(GINTMSK_WKUPINTMSK | GINTMSK_SOFMSK);
2789 		sc->sc_irq_mask |= (GINTMSK_USBSUSPMSK | GINTMSK_RXFLVLMSK);
2790 		DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2791 
2792 		/* complete root HUB interrupt endpoint */
2793 		dwc_otg_root_intr(sc);
2794 	}
2795 
2796 	if (status & GINTSTS_PRTINT) {
2797 		uint32_t hprt;
2798 
2799 		hprt = DWC_OTG_READ_4(sc, DOTG_HPRT);
2800 
2801 		/* clear change bits */
2802 		DWC_OTG_WRITE_4(sc, DOTG_HPRT, (hprt & (
2803 		    HPRT_PRTPWR | HPRT_PRTENCHNG |
2804 		    HPRT_PRTCONNDET | HPRT_PRTOVRCURRCHNG)) |
2805 		    sc->sc_hprt_val);
2806 
2807 		DPRINTFN(12, "GINTSTS=0x%08x, HPRT=0x%08x\n", status, hprt);
2808 
2809 		sc->sc_flags.status_device_mode = 0;
2810 
2811 		if (hprt & HPRT_PRTCONNSTS)
2812 			sc->sc_flags.status_bus_reset = 1;
2813 		else
2814 			sc->sc_flags.status_bus_reset = 0;
2815 
2816 		if (hprt & HPRT_PRTENCHNG)
2817 			sc->sc_flags.change_enabled = 1;
2818 
2819 		if (hprt & HPRT_PRTENA)
2820 			sc->sc_flags.port_enabled = 1;
2821 		else
2822 			sc->sc_flags.port_enabled = 0;
2823 
2824 		if (hprt & HPRT_PRTOVRCURRCHNG)
2825 			sc->sc_flags.change_over_current = 1;
2826 
2827 		if (hprt & HPRT_PRTOVRCURRACT)
2828 			sc->sc_flags.port_over_current = 1;
2829 		else
2830 			sc->sc_flags.port_over_current = 0;
2831 
2832 		if (hprt & HPRT_PRTPWR)
2833 			sc->sc_flags.port_powered = 1;
2834 		else
2835 			sc->sc_flags.port_powered = 0;
2836 
2837 		if (((hprt & HPRT_PRTSPD_MASK)
2838 		    >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_LOW)
2839 			sc->sc_flags.status_low_speed = 1;
2840 		else
2841 			sc->sc_flags.status_low_speed = 0;
2842 
2843 		if (((hprt & HPRT_PRTSPD_MASK)
2844 		    >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_HIGH)
2845 			sc->sc_flags.status_high_speed = 1;
2846 		else
2847 			sc->sc_flags.status_high_speed = 0;
2848 
2849 		if (hprt & HPRT_PRTCONNDET)
2850 			sc->sc_flags.change_connect = 1;
2851 
2852 		if (hprt & HPRT_PRTSUSP)
2853 			dwc_otg_suspend_irq(sc);
2854 		else
2855 			dwc_otg_resume_irq(sc);
2856 
2857 		/* complete root HUB interrupt endpoint */
2858 		dwc_otg_root_intr(sc);
2859 
2860 		/* disable RX FIFO level interrupt */
2861 		sc->sc_irq_mask &= ~GINTMSK_RXFLVLMSK;
2862 		DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2863 
2864 		/* update host frame interval */
2865 		dwc_otg_update_host_frame_interval(sc);
2866 	}
2867 
2868 	/*
2869 	 * If resume and suspend is set at the same time we interpret
2870 	 * that like RESUME. Resume is set when there is at least 3
2871 	 * milliseconds of inactivity on the USB BUS.
2872 	 */
2873 	if (status & GINTSTS_WKUPINT) {
2874 
2875 		DPRINTFN(5, "resume interrupt\n");
2876 
2877 		dwc_otg_resume_irq(sc);
2878 
2879 	} else if (status & GINTSTS_USBSUSP) {
2880 
2881 		DPRINTFN(5, "suspend interrupt\n");
2882 
2883 		dwc_otg_suspend_irq(sc);
2884 	}
2885 	/* check VBUS */
2886 	if (status & (GINTSTS_USBSUSP |
2887 	    GINTSTS_USBRST |
2888 	    GINTMSK_OTGINTMSK |
2889 	    GINTSTS_SESSREQINT)) {
2890 		uint32_t temp;
2891 
2892 		temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
2893 
2894 		DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
2895 
2896 		dwc_otg_vbus_interrupt(sc,
2897 		    (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
2898 	}
2899 
2900 	if (sc->sc_xfer_complete != 0) {
2901 		sc->sc_xfer_complete = 0;
2902 
2903 		/* complete FIFOs, if any */
2904 		dwc_otg_interrupt_complete(sc);
2905 
2906 		if (sc->sc_flags.status_device_mode == 0) {
2907 			/* update host transfer schedule, so that new transfers can be issued */
2908 			if (dwc_otg_update_host_transfer_schedule(sc))
2909 				dwc_otg_interrupt_poll(sc);
2910 		}
2911 	}
2912 
2913 	USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2914 	USB_BUS_UNLOCK(&sc->sc_bus);
2915 }
2916 
2917 static void
2918 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
2919 {
2920 	struct dwc_otg_td *td;
2921 
2922 	/* get current Transfer Descriptor */
2923 	td = temp->td_next;
2924 	temp->td = td;
2925 
2926 	/* prepare for next TD */
2927 	temp->td_next = td->obj_next;
2928 
2929 	/* fill out the Transfer Descriptor */
2930 	td->func = temp->func;
2931 	td->pc = temp->pc;
2932 	td->offset = temp->offset;
2933 	td->remainder = temp->len;
2934 	td->tx_bytes = 0;
2935 	td->error_any = 0;
2936 	td->error_stall = 0;
2937 	td->npkt = 0;
2938 	td->did_stall = temp->did_stall;
2939 	td->short_pkt = temp->short_pkt;
2940 	td->alt_next = temp->setup_alt_next;
2941 	td->set_toggle = 0;
2942 	td->got_short = 0;
2943 	td->did_nak = 0;
2944 	td->channel[0] = DWC_OTG_MAX_CHANNELS;
2945 	td->channel[1] = DWC_OTG_MAX_CHANNELS;
2946 	td->state = 0;
2947 	td->errcnt = 0;
2948 	td->tt_scheduled = 0;
2949 	td->tt_channel_tog = 0;
2950 	td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
2951 }
2952 
2953 static void
2954 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
2955 {
2956 	struct dwc_otg_std_temp temp;
2957 	struct dwc_otg_td *td;
2958 	uint32_t x;
2959 	uint8_t need_sync;
2960 	uint8_t is_host;
2961 
2962 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
2963 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
2964 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
2965 
2966 	temp.max_frame_size = xfer->max_frame_size;
2967 
2968 	td = xfer->td_start[0];
2969 	xfer->td_transfer_first = td;
2970 	xfer->td_transfer_cache = td;
2971 
2972 	/* setup temp */
2973 
2974 	temp.pc = NULL;
2975 	temp.td = NULL;
2976 	temp.td_next = xfer->td_start[0];
2977 	temp.offset = 0;
2978 	temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
2979 	    xfer->flags_int.isochronous_xfr;
2980 	temp.did_stall = !xfer->flags_int.control_stall;
2981 
2982 	is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
2983 
2984 	/* check if we should prepend a setup message */
2985 
2986 	if (xfer->flags_int.control_xfr) {
2987 		if (xfer->flags_int.control_hdr) {
2988 
2989 			if (is_host)
2990 				temp.func = &dwc_otg_host_setup_tx;
2991 			else
2992 				temp.func = &dwc_otg_setup_rx;
2993 
2994 			temp.len = xfer->frlengths[0];
2995 			temp.pc = xfer->frbuffers + 0;
2996 			temp.short_pkt = temp.len ? 1 : 0;
2997 
2998 			/* check for last frame */
2999 			if (xfer->nframes == 1) {
3000 				/* no STATUS stage yet, SETUP is last */
3001 				if (xfer->flags_int.control_act)
3002 					temp.setup_alt_next = 0;
3003 			}
3004 
3005 			dwc_otg_setup_standard_chain_sub(&temp);
3006 		}
3007 		x = 1;
3008 	} else {
3009 		x = 0;
3010 	}
3011 
3012 	if (x != xfer->nframes) {
3013 		if (xfer->endpointno & UE_DIR_IN) {
3014 			if (is_host) {
3015 				temp.func = &dwc_otg_host_data_rx;
3016 				need_sync = 0;
3017 			} else {
3018 				temp.func = &dwc_otg_data_tx;
3019 				need_sync = 1;
3020 			}
3021 		} else {
3022 			if (is_host) {
3023 				temp.func = &dwc_otg_host_data_tx;
3024 				need_sync = 0;
3025 			} else {
3026 				temp.func = &dwc_otg_data_rx;
3027 				need_sync = 0;
3028 			}
3029 		}
3030 
3031 		/* setup "pc" pointer */
3032 		temp.pc = xfer->frbuffers + x;
3033 	} else {
3034 		need_sync = 0;
3035 	}
3036 	while (x != xfer->nframes) {
3037 
3038 		/* DATA0 / DATA1 message */
3039 
3040 		temp.len = xfer->frlengths[x];
3041 
3042 		x++;
3043 
3044 		if (x == xfer->nframes) {
3045 			if (xfer->flags_int.control_xfr) {
3046 				if (xfer->flags_int.control_act) {
3047 					temp.setup_alt_next = 0;
3048 				}
3049 			} else {
3050 				temp.setup_alt_next = 0;
3051 			}
3052 		}
3053 		if (temp.len == 0) {
3054 
3055 			/* make sure that we send an USB packet */
3056 
3057 			temp.short_pkt = 0;
3058 
3059 		} else {
3060 
3061 			/* regular data transfer */
3062 
3063 			temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
3064 		}
3065 
3066 		dwc_otg_setup_standard_chain_sub(&temp);
3067 
3068 		if (xfer->flags_int.isochronous_xfr) {
3069 			temp.offset += temp.len;
3070 		} else {
3071 			/* get next Page Cache pointer */
3072 			temp.pc = xfer->frbuffers + x;
3073 		}
3074 	}
3075 
3076 	if (xfer->flags_int.control_xfr) {
3077 
3078 		/* always setup a valid "pc" pointer for status and sync */
3079 		temp.pc = xfer->frbuffers + 0;
3080 		temp.len = 0;
3081 		temp.short_pkt = 0;
3082 		temp.setup_alt_next = 0;
3083 
3084 		/* check if we need to sync */
3085 		if (need_sync) {
3086 			/* we need a SYNC point after TX */
3087 			temp.func = &dwc_otg_data_tx_sync;
3088 			dwc_otg_setup_standard_chain_sub(&temp);
3089 		}
3090 
3091 		/* check if we should append a status stage */
3092 		if (!xfer->flags_int.control_act) {
3093 
3094 			/*
3095 			 * Send a DATA1 message and invert the current
3096 			 * endpoint direction.
3097 			 */
3098 			if (xfer->endpointno & UE_DIR_IN) {
3099 				if (is_host) {
3100 					temp.func = &dwc_otg_host_data_tx;
3101 					need_sync = 0;
3102 				} else {
3103 					temp.func = &dwc_otg_data_rx;
3104 					need_sync = 0;
3105 				}
3106 			} else {
3107 				if (is_host) {
3108 					temp.func = &dwc_otg_host_data_rx;
3109 					need_sync = 0;
3110 				} else {
3111 					temp.func = &dwc_otg_data_tx;
3112 					need_sync = 1;
3113 				}
3114 			}
3115 
3116 			dwc_otg_setup_standard_chain_sub(&temp);
3117 
3118 			/* data toggle should be DATA1 */
3119 			td = temp.td;
3120 			td->set_toggle = 1;
3121 
3122 			if (need_sync) {
3123 				/* we need a SYNC point after TX */
3124 				temp.func = &dwc_otg_data_tx_sync;
3125 				dwc_otg_setup_standard_chain_sub(&temp);
3126 			}
3127 		}
3128 	} else {
3129 		/* check if we need to sync */
3130 		if (need_sync) {
3131 
3132 			temp.pc = xfer->frbuffers + 0;
3133 			temp.len = 0;
3134 			temp.short_pkt = 0;
3135 			temp.setup_alt_next = 0;
3136 
3137 			/* we need a SYNC point after TX */
3138 			temp.func = &dwc_otg_data_tx_sync;
3139 			dwc_otg_setup_standard_chain_sub(&temp);
3140 		}
3141 	}
3142 
3143 	/* must have at least one frame! */
3144 	td = temp.td;
3145 	xfer->td_transfer_last = td;
3146 
3147 	if (is_host) {
3148 
3149 		struct dwc_otg_softc *sc;
3150 		uint32_t hcchar;
3151 		uint32_t hcsplt;
3152 
3153 		sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3154 
3155 		/* get first again */
3156 		td = xfer->td_transfer_first;
3157 		td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
3158 
3159 		hcchar =
3160 			(xfer->address << HCCHAR_DEVADDR_SHIFT) |
3161 			((xfer->endpointno & UE_ADDR) << HCCHAR_EPNUM_SHIFT) |
3162 			(xfer->max_packet_size << HCCHAR_MPS_SHIFT) |
3163 			HCCHAR_CHENA;
3164 
3165 		/* XXX stability hack - possible HW issue */
3166 		if (td->ep_type == UE_CONTROL)
3167 			hcchar |= (UE_BULK << HCCHAR_EPTYPE_SHIFT);
3168 		else
3169 			hcchar |= (td->ep_type << HCCHAR_EPTYPE_SHIFT);
3170 
3171 		if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_LOW)
3172 			hcchar |= HCCHAR_LSPDDEV;
3173 		if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
3174 			hcchar |= HCCHAR_EPDIR_IN;
3175 
3176 		switch (xfer->xroot->udev->speed) {
3177 		case USB_SPEED_FULL:
3178 		case USB_SPEED_LOW:
3179 			/* check if root HUB port is running High Speed */
3180 			if (xfer->xroot->udev->parent_hs_hub != NULL) {
3181 				hcsplt = HCSPLT_SPLTENA |
3182 				    (xfer->xroot->udev->hs_port_no <<
3183 				    HCSPLT_PRTADDR_SHIFT) |
3184 				    (xfer->xroot->udev->hs_hub_addr <<
3185 				    HCSPLT_HUBADDR_SHIFT);
3186 			} else {
3187 				hcsplt = 0;
3188 			}
3189 			if (td->ep_type == UE_INTERRUPT) {
3190 				uint32_t ival;
3191 				ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3192 				if (ival == 0)
3193 					ival = 1;
3194 				else if (ival > 127)
3195 					ival = 127;
3196 				td->tmr_val = sc->sc_tmr_val + ival;
3197 				td->tmr_res = ival;
3198 			} else if (td->ep_type == UE_ISOCHRONOUS) {
3199 				td->tmr_val = 0;
3200 				td->tmr_res = 1;
3201 			} else {
3202 				td->tmr_val = 0;
3203 				td->tmr_res = 0;
3204 			}
3205 			break;
3206 		case USB_SPEED_HIGH:
3207 			hcsplt = 0;
3208 			if (td->ep_type == UE_ISOCHRONOUS ||
3209 			    td->ep_type == UE_INTERRUPT) {
3210 				hcchar |= ((xfer->max_packet_count & 3)
3211 				    << HCCHAR_MC_SHIFT);
3212 			}
3213 			if (td->ep_type == UE_INTERRUPT) {
3214 				uint32_t ival;
3215 				ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3216 				if (ival == 0)
3217 					ival = 1;
3218 				else if (ival > 127)
3219 					ival = 127;
3220 				td->tmr_val = sc->sc_tmr_val + ival;
3221 				td->tmr_res = ival;
3222 			} else if (td->ep_type == UE_ISOCHRONOUS) {
3223 				td->tmr_val = 0;
3224 				td->tmr_res = 1 << usbd_xfer_get_fps_shift(xfer);
3225 			} else {
3226 				td->tmr_val = 0;
3227 				td->tmr_res = 0;
3228 			}
3229 			break;
3230 		default:
3231 			hcsplt = 0;
3232 			td->tmr_val = 0;
3233 			td->tmr_res = 0;
3234 			break;
3235 		}
3236 
3237 		/* store configuration in all TD's */
3238 		while (1) {
3239 			td->hcchar = hcchar;
3240 			td->hcsplt = hcsplt;
3241 
3242 			if (((void *)td) == xfer->td_transfer_last)
3243 				break;
3244 
3245 			td = td->obj_next;
3246 		}
3247 	}
3248 }
3249 
3250 static void
3251 dwc_otg_timeout(void *arg)
3252 {
3253 	struct usb_xfer *xfer = arg;
3254 
3255 	DPRINTF("xfer=%p\n", xfer);
3256 
3257 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
3258 
3259 	/* transfer is transferred */
3260 	dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
3261 }
3262 
3263 static void
3264 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
3265 {
3266 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3267 	struct usb_xfer_root *xroot;
3268 	struct dwc_otg_td *td;
3269 
3270 	DPRINTFN(9, "\n");
3271 
3272 	/*
3273 	 * Poll one time in device mode, which will turn on the
3274 	 * endpoint interrupts. Else wait for SOF interrupt in host
3275 	 * mode.
3276 	 */
3277 	if (sc->sc_flags.status_device_mode != 0) {
3278 		dwc_otg_xfer_do_fifo(sc, xfer);
3279 		if (dwc_otg_xfer_do_complete(sc, xfer))
3280 			return;
3281 	}
3282 	USB_BUS_SPIN_LOCK(&sc->sc_bus);
3283 
3284 	/* put transfer on interrupt queue */
3285 	usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3286 
3287 	/* start timeout, if any */
3288 	if (xfer->timeout != 0) {
3289 		usbd_transfer_timeout_ms(xfer,
3290 		    &dwc_otg_timeout, xfer->timeout);
3291 	}
3292 
3293 	if (sc->sc_flags.status_device_mode != 0)
3294 		goto done;
3295 
3296 	/* enable SOF interrupt, if any */
3297 	dwc_otg_enable_sof_irq(sc);
3298 
3299 	td = xfer->td_transfer_cache;
3300 	if (td->ep_type != UE_BULK)
3301 		goto done;
3302 
3303 	xroot = xfer->xroot;
3304 
3305 	/*
3306 	 * Optimise the ping-pong effect by waking up other BULK
3307 	 * transfers belonging to the same device group:
3308 	 */
3309 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3310 		td = xfer->td_transfer_cache;
3311 		if (td == NULL || td->ep_type != UE_BULK || xfer->xroot != xroot)
3312 			continue;
3313 		/* reset NAK counter */
3314 		td->did_nak = 0;
3315 	}
3316 done:
3317 	USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3318 }
3319 
3320 static void
3321 dwc_otg_root_intr(struct dwc_otg_softc *sc)
3322 {
3323 	DPRINTFN(9, "\n");
3324 
3325 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3326 
3327 	/* set port bit */
3328 	sc->sc_hub_idata[0] = 0x02;	/* we only have one port */
3329 
3330 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3331 	    sizeof(sc->sc_hub_idata));
3332 }
3333 
3334 static usb_error_t
3335 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
3336 {
3337 	struct dwc_otg_td *td;
3338 	uint32_t len;
3339 	usb_error_t error;
3340 
3341 	DPRINTFN(9, "\n");
3342 
3343 	td = xfer->td_transfer_cache;
3344 
3345 	do {
3346 		len = td->remainder;
3347 
3348 		/* store last data toggle */
3349 		xfer->endpoint->toggle_next = td->toggle;
3350 
3351 		if (xfer->aframes != xfer->nframes) {
3352 			/*
3353 			 * Verify the length and subtract
3354 			 * the remainder from "frlengths[]":
3355 			 */
3356 			if (len > xfer->frlengths[xfer->aframes]) {
3357 				td->error_any = 1;
3358 			} else {
3359 				xfer->frlengths[xfer->aframes] -= len;
3360 			}
3361 		}
3362 		/* Check for transfer error */
3363 		if (td->error_any) {
3364 			/* the transfer is finished */
3365 			error = (td->error_stall ?
3366 			    USB_ERR_STALLED : USB_ERR_IOERROR);
3367 			td = NULL;
3368 			break;
3369 		}
3370 		/* Check for short transfer */
3371 		if (len > 0) {
3372 			if (xfer->flags_int.short_frames_ok ||
3373 			    xfer->flags_int.isochronous_xfr) {
3374 				/* follow alt next */
3375 				if (td->alt_next) {
3376 					td = td->obj_next;
3377 				} else {
3378 					td = NULL;
3379 				}
3380 			} else {
3381 				/* the transfer is finished */
3382 				td = NULL;
3383 			}
3384 			error = 0;
3385 			break;
3386 		}
3387 		td = td->obj_next;
3388 
3389 		/* this USB frame is complete */
3390 		error = 0;
3391 		break;
3392 
3393 	} while (0);
3394 
3395 	/* update transfer cache */
3396 
3397 	xfer->td_transfer_cache = td;
3398 
3399 	return (error);
3400 }
3401 
3402 static void
3403 dwc_otg_standard_done(struct usb_xfer *xfer)
3404 {
3405 	usb_error_t err = 0;
3406 
3407 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
3408 	    xfer, xfer->endpoint);
3409 
3410 	/* reset scanner */
3411 
3412 	xfer->td_transfer_cache = xfer->td_transfer_first;
3413 
3414 	if (xfer->flags_int.control_xfr) {
3415 
3416 		if (xfer->flags_int.control_hdr) {
3417 
3418 			err = dwc_otg_standard_done_sub(xfer);
3419 		}
3420 		xfer->aframes = 1;
3421 
3422 		if (xfer->td_transfer_cache == NULL) {
3423 			goto done;
3424 		}
3425 	}
3426 	while (xfer->aframes != xfer->nframes) {
3427 
3428 		err = dwc_otg_standard_done_sub(xfer);
3429 		xfer->aframes++;
3430 
3431 		if (xfer->td_transfer_cache == NULL) {
3432 			goto done;
3433 		}
3434 	}
3435 
3436 	if (xfer->flags_int.control_xfr &&
3437 	    !xfer->flags_int.control_act) {
3438 
3439 		err = dwc_otg_standard_done_sub(xfer);
3440 	}
3441 done:
3442 	dwc_otg_device_done(xfer, err);
3443 }
3444 
3445 /*------------------------------------------------------------------------*
3446  *	dwc_otg_device_done
3447  *
3448  * NOTE: this function can be called more than one time on the
3449  * same USB transfer!
3450  *------------------------------------------------------------------------*/
3451 static void
3452 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
3453 {
3454 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3455 
3456 	DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
3457 	    xfer, xfer->endpoint, error);
3458 
3459 	if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
3460 		/* Interrupts are cleared by the interrupt handler */
3461 	} else {
3462 		struct dwc_otg_td *td;
3463 
3464 		td = xfer->td_transfer_first;
3465 
3466 		if (td != NULL) {
3467 			dwc_otg_host_channel_free(sc, td, 0);
3468 			dwc_otg_host_channel_free(sc, td, 1);
3469 		}
3470 	}
3471 	/* dequeue transfer and start next transfer */
3472 	usbd_transfer_done(xfer, error);
3473 }
3474 
3475 static void
3476 dwc_otg_xfer_stall(struct usb_xfer *xfer)
3477 {
3478 	dwc_otg_device_done(xfer, USB_ERR_STALLED);
3479 }
3480 
3481 static void
3482 dwc_otg_set_stall(struct usb_device *udev,
3483     struct usb_endpoint *ep, uint8_t *did_stall)
3484 {
3485 	struct dwc_otg_softc *sc;
3486 	uint32_t temp;
3487 	uint32_t reg;
3488 	uint8_t ep_no;
3489 
3490 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3491 
3492 	/* check mode */
3493 	if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3494 		/* not supported */
3495 		return;
3496 	}
3497 
3498 	sc = DWC_OTG_BUS2SC(udev->bus);
3499 
3500 	/* get endpoint address */
3501 	ep_no = ep->edesc->bEndpointAddress;
3502 
3503 	DPRINTFN(5, "endpoint=0x%x\n", ep_no);
3504 
3505 	if (ep_no & UE_DIR_IN) {
3506 		reg = DOTG_DIEPCTL(ep_no & UE_ADDR);
3507 		temp = sc->sc_in_ctl[ep_no & UE_ADDR];
3508 	} else {
3509 		reg = DOTG_DOEPCTL(ep_no & UE_ADDR);
3510 		temp = sc->sc_out_ctl[ep_no & UE_ADDR];
3511 	}
3512 
3513 	/* disable and stall endpoint */
3514 	DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3515 	DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_STALL);
3516 
3517 	/* clear active OUT ep */
3518 	if (!(ep_no & UE_DIR_IN)) {
3519 
3520 		sc->sc_active_rx_ep &= ~(1U << (ep_no & UE_ADDR));
3521 
3522 		if (sc->sc_last_rx_status != 0 &&
3523 		    (ep_no & UE_ADDR) == GRXSTSRD_CHNUM_GET(
3524 		    sc->sc_last_rx_status)) {
3525 			/* dump data */
3526 			dwc_otg_common_rx_ack(sc);
3527 			/* poll interrupt */
3528 			dwc_otg_interrupt_poll(sc);
3529 			dwc_otg_interrupt_complete(sc);
3530 		}
3531 	}
3532 }
3533 
3534 static void
3535 dwc_otg_clear_stall_sub(struct dwc_otg_softc *sc, uint32_t mps,
3536     uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
3537 {
3538 	uint32_t reg;
3539 	uint32_t temp;
3540 
3541 	if (ep_type == UE_CONTROL) {
3542 		/* clearing stall is not needed */
3543 		return;
3544 	}
3545 
3546 	if (ep_dir) {
3547 		reg = DOTG_DIEPCTL(ep_no);
3548 	} else {
3549 		reg = DOTG_DOEPCTL(ep_no);
3550 		sc->sc_active_rx_ep |= (1U << ep_no);
3551 	}
3552 
3553 	/* round up and mask away the multiplier count */
3554 	mps = (mps + 3) & 0x7FC;
3555 
3556 	if (ep_type == UE_BULK) {
3557 		temp = DIEPCTL_EPTYPE_SET(
3558 		    DIEPCTL_EPTYPE_BULK) |
3559 		    DIEPCTL_USBACTEP;
3560 	} else if (ep_type == UE_INTERRUPT) {
3561 		temp = DIEPCTL_EPTYPE_SET(
3562 		    DIEPCTL_EPTYPE_INTERRUPT) |
3563 		    DIEPCTL_USBACTEP;
3564 	} else {
3565 		temp = DIEPCTL_EPTYPE_SET(
3566 		    DIEPCTL_EPTYPE_ISOC) |
3567 		    DIEPCTL_USBACTEP;
3568 	}
3569 
3570 	temp |= DIEPCTL_MPS_SET(mps);
3571 	temp |= DIEPCTL_TXFNUM_SET(ep_no);
3572 
3573 	if (ep_dir)
3574 		sc->sc_in_ctl[ep_no] = temp;
3575 	else
3576 		sc->sc_out_ctl[ep_no] = temp;
3577 
3578 	DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3579 	DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_SETD0PID);
3580 	DWC_OTG_WRITE_4(sc, reg, temp | DIEPCTL_SNAK);
3581 
3582 	/* we only reset the transmit FIFO */
3583 	if (ep_dir) {
3584 		DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
3585 		    GRSTCTL_TXFIFO(ep_no) |
3586 		    GRSTCTL_TXFFLSH);
3587 
3588 		DWC_OTG_WRITE_4(sc,
3589 		    DOTG_DIEPTSIZ(ep_no), 0);
3590 	}
3591 
3592 	/* poll interrupt */
3593 	dwc_otg_interrupt_poll(sc);
3594 	dwc_otg_interrupt_complete(sc);
3595 }
3596 
3597 static void
3598 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3599 {
3600 	struct dwc_otg_softc *sc;
3601 	struct usb_endpoint_descriptor *ed;
3602 
3603 	DPRINTFN(5, "endpoint=%p\n", ep);
3604 
3605 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3606 
3607 	/* check mode */
3608 	if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3609 		/* not supported */
3610 		return;
3611 	}
3612 	/* get softc */
3613 	sc = DWC_OTG_BUS2SC(udev->bus);
3614 
3615 	/* get endpoint descriptor */
3616 	ed = ep->edesc;
3617 
3618 	/* reset endpoint */
3619 	dwc_otg_clear_stall_sub(sc,
3620 	    UGETW(ed->wMaxPacketSize),
3621 	    (ed->bEndpointAddress & UE_ADDR),
3622 	    (ed->bmAttributes & UE_XFERTYPE),
3623 	    (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3624 }
3625 
3626 static void
3627 dwc_otg_device_state_change(struct usb_device *udev)
3628 {
3629 	struct dwc_otg_softc *sc;
3630 	uint8_t x;
3631 
3632 	/* check mode */
3633 	if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3634 		/* not supported */
3635 		return;
3636 	}
3637 
3638 	/* get softc */
3639 	sc = DWC_OTG_BUS2SC(udev->bus);
3640 
3641 	/* deactivate all other endpoint but the control endpoint */
3642 	if (udev->state == USB_STATE_CONFIGURED ||
3643 	    udev->state == USB_STATE_ADDRESSED) {
3644 
3645 		USB_BUS_LOCK(&sc->sc_bus);
3646 
3647 		for (x = 1; x != sc->sc_dev_ep_max; x++) {
3648 
3649 			if (x < sc->sc_dev_in_ep_max) {
3650 				DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x),
3651 				    DIEPCTL_EPDIS);
3652 				DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x), 0);
3653 			}
3654 
3655 			DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x),
3656 			    DOEPCTL_EPDIS);
3657 			DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x), 0);
3658 		}
3659 		USB_BUS_UNLOCK(&sc->sc_bus);
3660 	}
3661 }
3662 
3663 int
3664 dwc_otg_init(struct dwc_otg_softc *sc)
3665 {
3666 	uint32_t temp;
3667 
3668 	DPRINTF("start\n");
3669 
3670 	/* set up the bus structure */
3671 	sc->sc_bus.usbrev = USB_REV_2_0;
3672 	sc->sc_bus.methods = &dwc_otg_bus_methods;
3673 
3674 	usb_callout_init_mtx(&sc->sc_timer,
3675 	    &sc->sc_bus.bus_mtx, 0);
3676 
3677 	USB_BUS_LOCK(&sc->sc_bus);
3678 
3679 	/* turn on clocks */
3680 	dwc_otg_clocks_on(sc);
3681 
3682 	temp = DWC_OTG_READ_4(sc, DOTG_GSNPSID);
3683 	DPRINTF("Version = 0x%08x\n", temp);
3684 
3685 	/* disconnect */
3686 	DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3687 	    DCTL_SFTDISCON);
3688 
3689 	/* wait for host to detect disconnect */
3690 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
3691 
3692 	DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
3693 	    GRSTCTL_CSFTRST);
3694 
3695 	/* wait a little bit for block to reset */
3696 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
3697 
3698 	switch (sc->sc_mode) {
3699 	case DWC_MODE_DEVICE:
3700 		temp = GUSBCFG_FORCEDEVMODE;
3701 		break;
3702 	case DWC_MODE_HOST:
3703 		temp = GUSBCFG_FORCEHOSTMODE;
3704 		break;
3705 	default:
3706 		temp = 0;
3707 		break;
3708 	}
3709 
3710 	/* select HSIC or non-HSIC mode */
3711 	if (dwc_otg_use_hsic) {
3712 		DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3713 		    GUSBCFG_PHYIF |
3714 		    GUSBCFG_TRD_TIM_SET(5) | temp);
3715 		DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL,
3716 		    0x000000EC);
3717 
3718 		temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3719 		DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3720 		    temp & ~GLPMCFG_HSIC_CONN);
3721 		DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3722 		    temp | GLPMCFG_HSIC_CONN);
3723 	} else {
3724 		DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3725 		    GUSBCFG_ULPI_UTMI_SEL |
3726 		    GUSBCFG_TRD_TIM_SET(5) | temp);
3727 		DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3728 
3729 		temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3730 		DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3731 		    temp & ~GLPMCFG_HSIC_CONN);
3732 	}
3733 
3734 	/* clear global nak */
3735 	DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3736 	    DCTL_CGOUTNAK |
3737 	    DCTL_CGNPINNAK);
3738 
3739 	/* disable USB port */
3740 	DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0xFFFFFFFF);
3741 
3742 	/* wait 10ms */
3743 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3744 
3745 	/* enable USB port */
3746 	DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
3747 
3748 	/* wait 10ms */
3749 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3750 
3751 	/* pull up D+ */
3752 	dwc_otg_pull_up(sc);
3753 
3754 	temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG3);
3755 
3756 	sc->sc_fifo_size = 4 * GHWCFG3_DFIFODEPTH_GET(temp);
3757 
3758 	temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
3759 
3760 	sc->sc_dev_ep_max = GHWCFG2_NUMDEVEPS_GET(temp);
3761 
3762 	if (sc->sc_dev_ep_max > DWC_OTG_MAX_ENDPOINTS)
3763 		sc->sc_dev_ep_max = DWC_OTG_MAX_ENDPOINTS;
3764 
3765 	sc->sc_host_ch_max = GHWCFG2_NUMHSTCHNL_GET(temp);
3766 
3767 	if (sc->sc_host_ch_max > DWC_OTG_MAX_CHANNELS)
3768 		sc->sc_host_ch_max = DWC_OTG_MAX_CHANNELS;
3769 
3770 	temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG4);
3771 
3772 	sc->sc_dev_in_ep_max = GHWCFG4_NUM_IN_EP_GET(temp);
3773 
3774 	DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d Host CHs = %d\n",
3775 	    sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max,
3776 	    sc->sc_host_ch_max);
3777 
3778 	/* setup FIFO */
3779 	if (dwc_otg_init_fifo(sc, sc->sc_mode)) {
3780 		USB_BUS_UNLOCK(&sc->sc_bus);
3781 		return (EINVAL);
3782 	}
3783 
3784 	/* enable interrupts */
3785 	sc->sc_irq_mask = DWC_OTG_MSK_GINT_ENABLED;
3786 	DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
3787 
3788 	if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_DEVICE) {
3789 
3790 		/* enable all endpoint interrupts */
3791 		temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
3792 		if (temp & GHWCFG2_MPI) {
3793 			uint8_t x;
3794 
3795 			DPRINTF("Multi Process Interrupts\n");
3796 
3797 			for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
3798 				DWC_OTG_WRITE_4(sc, DOTG_DIEPEACHINTMSK(x),
3799 				    DIEPMSK_XFERCOMPLMSK);
3800 				DWC_OTG_WRITE_4(sc, DOTG_DOEPEACHINTMSK(x), 0);
3801 			}
3802 			DWC_OTG_WRITE_4(sc, DOTG_DEACHINTMSK, 0xFFFF);
3803 		} else {
3804 			DWC_OTG_WRITE_4(sc, DOTG_DIEPMSK,
3805 			    DIEPMSK_XFERCOMPLMSK);
3806 			DWC_OTG_WRITE_4(sc, DOTG_DOEPMSK, 0);
3807 			DWC_OTG_WRITE_4(sc, DOTG_DAINTMSK, 0xFFFF);
3808 		}
3809 	}
3810 
3811 	if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_HOST) {
3812 		/* setup clocks */
3813 		temp = DWC_OTG_READ_4(sc, DOTG_HCFG);
3814 		temp &= ~(HCFG_FSLSSUPP | HCFG_FSLSPCLKSEL_MASK);
3815 		temp |= (1 << HCFG_FSLSPCLKSEL_SHIFT);
3816 		DWC_OTG_WRITE_4(sc, DOTG_HCFG, temp);
3817 	}
3818 
3819 	/* only enable global IRQ */
3820 	DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG,
3821 	    GAHBCFG_GLBLINTRMSK);
3822 
3823 	/* turn off clocks */
3824 	dwc_otg_clocks_off(sc);
3825 
3826 	/* read initial VBUS state */
3827 
3828 	temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
3829 
3830 	DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
3831 
3832 	dwc_otg_vbus_interrupt(sc,
3833 	    (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
3834 
3835 	USB_BUS_UNLOCK(&sc->sc_bus);
3836 
3837 	/* catch any lost interrupts */
3838 
3839 	dwc_otg_do_poll(&sc->sc_bus);
3840 
3841 	return (0);			/* success */
3842 }
3843 
3844 void
3845 dwc_otg_uninit(struct dwc_otg_softc *sc)
3846 {
3847 	USB_BUS_LOCK(&sc->sc_bus);
3848 
3849 	/* stop host timer */
3850 	dwc_otg_timer_stop(sc);
3851 
3852 	/* set disconnect */
3853 	DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3854 	    DCTL_SFTDISCON);
3855 
3856 	/* turn off global IRQ */
3857 	DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG, 0);
3858 
3859 	sc->sc_flags.port_enabled = 0;
3860 	sc->sc_flags.port_powered = 0;
3861 	sc->sc_flags.status_vbus = 0;
3862 	sc->sc_flags.status_bus_reset = 0;
3863 	sc->sc_flags.status_suspend = 0;
3864 	sc->sc_flags.change_suspend = 0;
3865 	sc->sc_flags.change_connect = 1;
3866 
3867 	dwc_otg_pull_down(sc);
3868 	dwc_otg_clocks_off(sc);
3869 
3870 	USB_BUS_UNLOCK(&sc->sc_bus);
3871 
3872 	usb_callout_drain(&sc->sc_timer);
3873 }
3874 
3875 static void
3876 dwc_otg_suspend(struct dwc_otg_softc *sc)
3877 {
3878 	return;
3879 }
3880 
3881 static void
3882 dwc_otg_resume(struct dwc_otg_softc *sc)
3883 {
3884 	return;
3885 }
3886 
3887 static void
3888 dwc_otg_do_poll(struct usb_bus *bus)
3889 {
3890 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
3891 
3892 	USB_BUS_LOCK(&sc->sc_bus);
3893 	USB_BUS_SPIN_LOCK(&sc->sc_bus);
3894 	dwc_otg_interrupt_poll(sc);
3895 	dwc_otg_interrupt_complete(sc);
3896 	if (sc->sc_flags.status_device_mode == 0) {
3897 		/* update host transfer schedule, so that new transfers can be issued */
3898 		if (dwc_otg_update_host_transfer_schedule(sc))
3899 			dwc_otg_interrupt_poll(sc);
3900 	}
3901 	USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3902 	USB_BUS_UNLOCK(&sc->sc_bus);
3903 }
3904 
3905 /*------------------------------------------------------------------------*
3906  * DWC OTG bulk support
3907  * DWC OTG control support
3908  * DWC OTG interrupt support
3909  *------------------------------------------------------------------------*/
3910 static void
3911 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
3912 {
3913 }
3914 
3915 static void
3916 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
3917 {
3918 	dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
3919 }
3920 
3921 static void
3922 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
3923 {
3924 }
3925 
3926 static void
3927 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
3928 {
3929 	/* setup TDs */
3930 	dwc_otg_setup_standard_chain(xfer);
3931 	dwc_otg_start_standard_chain(xfer);
3932 }
3933 
3934 static const struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
3935 {
3936 	.open = dwc_otg_device_non_isoc_open,
3937 	.close = dwc_otg_device_non_isoc_close,
3938 	.enter = dwc_otg_device_non_isoc_enter,
3939 	.start = dwc_otg_device_non_isoc_start,
3940 };
3941 
3942 /*------------------------------------------------------------------------*
3943  * DWC OTG full speed isochronous support
3944  *------------------------------------------------------------------------*/
3945 static void
3946 dwc_otg_device_isoc_open(struct usb_xfer *xfer)
3947 {
3948 }
3949 
3950 static void
3951 dwc_otg_device_isoc_close(struct usb_xfer *xfer)
3952 {
3953 	dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
3954 }
3955 
3956 static void
3957 dwc_otg_device_isoc_enter(struct usb_xfer *xfer)
3958 {
3959 }
3960 
3961 static void
3962 dwc_otg_device_isoc_start(struct usb_xfer *xfer)
3963 {
3964 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3965 	uint32_t temp;
3966 	uint32_t msframes;
3967 	uint32_t framenum;
3968 	uint8_t shift = usbd_xfer_get_fps_shift(xfer);
3969 
3970 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
3971 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
3972 
3973 	if (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST) {
3974 		temp = DWC_OTG_READ_4(sc, DOTG_HFNUM);
3975 
3976 		/* get the current frame index */
3977 		framenum = (temp & HFNUM_FRNUM_MASK);
3978 	} else {
3979 		temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
3980 
3981 		/* get the current frame index */
3982 		framenum = DSTS_SOFFN_GET(temp);
3983 	}
3984 
3985 	if (xfer->xroot->udev->parent_hs_hub != NULL)
3986 		framenum /= 8;
3987 
3988 	framenum &= DWC_OTG_FRAME_MASK;
3989 
3990 	/*
3991 	 * Compute number of milliseconds worth of data traffic for
3992 	 * this USB transfer:
3993 	 */
3994 	if (xfer->xroot->udev->speed == USB_SPEED_HIGH)
3995 		msframes = ((xfer->nframes << shift) + 7) / 8;
3996 	else
3997 		msframes = xfer->nframes;
3998 
3999 	/*
4000 	 * check if the frame index is within the window where the frames
4001 	 * will be inserted
4002 	 */
4003 	temp = (framenum - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
4004 
4005 	if ((xfer->endpoint->is_synced == 0) || (temp < msframes)) {
4006 		/*
4007 		 * If there is data underflow or the pipe queue is
4008 		 * empty we schedule the transfer a few frames ahead
4009 		 * of the current frame position. Else two isochronous
4010 		 * transfers might overlap.
4011 		 */
4012 		xfer->endpoint->isoc_next = (framenum + 3) & DWC_OTG_FRAME_MASK;
4013 		xfer->endpoint->is_synced = 1;
4014 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
4015 	}
4016 	/*
4017 	 * compute how many milliseconds the insertion is ahead of the
4018 	 * current frame position:
4019 	 */
4020 	temp = (xfer->endpoint->isoc_next - framenum) & DWC_OTG_FRAME_MASK;
4021 
4022 	/*
4023 	 * pre-compute when the isochronous transfer will be finished:
4024 	 */
4025 	xfer->isoc_time_complete =
4026 		usb_isoc_time_expand(&sc->sc_bus, framenum) + temp + msframes;
4027 
4028 	/* setup TDs */
4029 	dwc_otg_setup_standard_chain(xfer);
4030 
4031 	/* compute frame number for next insertion */
4032 	xfer->endpoint->isoc_next += msframes;
4033 
4034 	/* start TD chain */
4035 	dwc_otg_start_standard_chain(xfer);
4036 }
4037 
4038 static const struct usb_pipe_methods dwc_otg_device_isoc_methods =
4039 {
4040 	.open = dwc_otg_device_isoc_open,
4041 	.close = dwc_otg_device_isoc_close,
4042 	.enter = dwc_otg_device_isoc_enter,
4043 	.start = dwc_otg_device_isoc_start,
4044 };
4045 
4046 /*------------------------------------------------------------------------*
4047  * DWC OTG root control support
4048  *------------------------------------------------------------------------*
4049  * Simulate a hardware HUB by handling all the necessary requests.
4050  *------------------------------------------------------------------------*/
4051 
4052 static const struct usb_device_descriptor dwc_otg_devd = {
4053 	.bLength = sizeof(struct usb_device_descriptor),
4054 	.bDescriptorType = UDESC_DEVICE,
4055 	.bcdUSB = {0x00, 0x02},
4056 	.bDeviceClass = UDCLASS_HUB,
4057 	.bDeviceSubClass = UDSUBCLASS_HUB,
4058 	.bDeviceProtocol = UDPROTO_HSHUBSTT,
4059 	.bMaxPacketSize = 64,
4060 	.bcdDevice = {0x00, 0x01},
4061 	.iManufacturer = 1,
4062 	.iProduct = 2,
4063 	.bNumConfigurations = 1,
4064 };
4065 
4066 static const struct dwc_otg_config_desc dwc_otg_confd = {
4067 	.confd = {
4068 		.bLength = sizeof(struct usb_config_descriptor),
4069 		.bDescriptorType = UDESC_CONFIG,
4070 		.wTotalLength[0] = sizeof(dwc_otg_confd),
4071 		.bNumInterface = 1,
4072 		.bConfigurationValue = 1,
4073 		.iConfiguration = 0,
4074 		.bmAttributes = UC_SELF_POWERED,
4075 		.bMaxPower = 0,
4076 	},
4077 	.ifcd = {
4078 		.bLength = sizeof(struct usb_interface_descriptor),
4079 		.bDescriptorType = UDESC_INTERFACE,
4080 		.bNumEndpoints = 1,
4081 		.bInterfaceClass = UICLASS_HUB,
4082 		.bInterfaceSubClass = UISUBCLASS_HUB,
4083 		.bInterfaceProtocol = 0,
4084 	},
4085 	.endpd = {
4086 		.bLength = sizeof(struct usb_endpoint_descriptor),
4087 		.bDescriptorType = UDESC_ENDPOINT,
4088 		.bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
4089 		.bmAttributes = UE_INTERRUPT,
4090 		.wMaxPacketSize[0] = 8,
4091 		.bInterval = 255,
4092 	},
4093 };
4094 
4095 #define	HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
4096 
4097 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
4098 	.bDescLength = sizeof(dwc_otg_hubd),
4099 	.bDescriptorType = UDESC_HUB,
4100 	.bNbrPorts = 1,
4101 	HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
4102 	.bPwrOn2PwrGood = 50,
4103 	.bHubContrCurrent = 0,
4104 	.DeviceRemovable = {0},		/* port is removable */
4105 };
4106 
4107 #define	STRING_VENDOR \
4108   "D\0W\0C\0O\0T\0G"
4109 
4110 #define	STRING_PRODUCT \
4111   "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
4112 
4113 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
4114 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
4115 
4116 static usb_error_t
4117 dwc_otg_roothub_exec(struct usb_device *udev,
4118     struct usb_device_request *req, const void **pptr, uint16_t *plength)
4119 {
4120 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4121 	const void *ptr;
4122 	uint16_t len;
4123 	uint16_t value;
4124 	uint16_t index;
4125 	usb_error_t err;
4126 
4127 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
4128 
4129 	/* buffer reset */
4130 	ptr = (const void *)&sc->sc_hub_temp;
4131 	len = 0;
4132 	err = 0;
4133 
4134 	value = UGETW(req->wValue);
4135 	index = UGETW(req->wIndex);
4136 
4137 	/* demultiplex the control request */
4138 
4139 	switch (req->bmRequestType) {
4140 	case UT_READ_DEVICE:
4141 		switch (req->bRequest) {
4142 		case UR_GET_DESCRIPTOR:
4143 			goto tr_handle_get_descriptor;
4144 		case UR_GET_CONFIG:
4145 			goto tr_handle_get_config;
4146 		case UR_GET_STATUS:
4147 			goto tr_handle_get_status;
4148 		default:
4149 			goto tr_stalled;
4150 		}
4151 		break;
4152 
4153 	case UT_WRITE_DEVICE:
4154 		switch (req->bRequest) {
4155 		case UR_SET_ADDRESS:
4156 			goto tr_handle_set_address;
4157 		case UR_SET_CONFIG:
4158 			goto tr_handle_set_config;
4159 		case UR_CLEAR_FEATURE:
4160 			goto tr_valid;	/* nop */
4161 		case UR_SET_DESCRIPTOR:
4162 			goto tr_valid;	/* nop */
4163 		case UR_SET_FEATURE:
4164 		default:
4165 			goto tr_stalled;
4166 		}
4167 		break;
4168 
4169 	case UT_WRITE_ENDPOINT:
4170 		switch (req->bRequest) {
4171 		case UR_CLEAR_FEATURE:
4172 			switch (UGETW(req->wValue)) {
4173 			case UF_ENDPOINT_HALT:
4174 				goto tr_handle_clear_halt;
4175 			case UF_DEVICE_REMOTE_WAKEUP:
4176 				goto tr_handle_clear_wakeup;
4177 			default:
4178 				goto tr_stalled;
4179 			}
4180 			break;
4181 		case UR_SET_FEATURE:
4182 			switch (UGETW(req->wValue)) {
4183 			case UF_ENDPOINT_HALT:
4184 				goto tr_handle_set_halt;
4185 			case UF_DEVICE_REMOTE_WAKEUP:
4186 				goto tr_handle_set_wakeup;
4187 			default:
4188 				goto tr_stalled;
4189 			}
4190 			break;
4191 		case UR_SYNCH_FRAME:
4192 			goto tr_valid;	/* nop */
4193 		default:
4194 			goto tr_stalled;
4195 		}
4196 		break;
4197 
4198 	case UT_READ_ENDPOINT:
4199 		switch (req->bRequest) {
4200 		case UR_GET_STATUS:
4201 			goto tr_handle_get_ep_status;
4202 		default:
4203 			goto tr_stalled;
4204 		}
4205 		break;
4206 
4207 	case UT_WRITE_INTERFACE:
4208 		switch (req->bRequest) {
4209 		case UR_SET_INTERFACE:
4210 			goto tr_handle_set_interface;
4211 		case UR_CLEAR_FEATURE:
4212 			goto tr_valid;	/* nop */
4213 		case UR_SET_FEATURE:
4214 		default:
4215 			goto tr_stalled;
4216 		}
4217 		break;
4218 
4219 	case UT_READ_INTERFACE:
4220 		switch (req->bRequest) {
4221 		case UR_GET_INTERFACE:
4222 			goto tr_handle_get_interface;
4223 		case UR_GET_STATUS:
4224 			goto tr_handle_get_iface_status;
4225 		default:
4226 			goto tr_stalled;
4227 		}
4228 		break;
4229 
4230 	case UT_WRITE_CLASS_INTERFACE:
4231 	case UT_WRITE_VENDOR_INTERFACE:
4232 		/* XXX forward */
4233 		break;
4234 
4235 	case UT_READ_CLASS_INTERFACE:
4236 	case UT_READ_VENDOR_INTERFACE:
4237 		/* XXX forward */
4238 		break;
4239 
4240 	case UT_WRITE_CLASS_DEVICE:
4241 		switch (req->bRequest) {
4242 		case UR_CLEAR_FEATURE:
4243 			goto tr_valid;
4244 		case UR_SET_DESCRIPTOR:
4245 		case UR_SET_FEATURE:
4246 			break;
4247 		default:
4248 			goto tr_stalled;
4249 		}
4250 		break;
4251 
4252 	case UT_WRITE_CLASS_OTHER:
4253 		switch (req->bRequest) {
4254 		case UR_CLEAR_FEATURE:
4255 			goto tr_handle_clear_port_feature;
4256 		case UR_SET_FEATURE:
4257 			goto tr_handle_set_port_feature;
4258 		case UR_CLEAR_TT_BUFFER:
4259 		case UR_RESET_TT:
4260 		case UR_STOP_TT:
4261 			goto tr_valid;
4262 
4263 		default:
4264 			goto tr_stalled;
4265 		}
4266 		break;
4267 
4268 	case UT_READ_CLASS_OTHER:
4269 		switch (req->bRequest) {
4270 		case UR_GET_TT_STATE:
4271 			goto tr_handle_get_tt_state;
4272 		case UR_GET_STATUS:
4273 			goto tr_handle_get_port_status;
4274 		default:
4275 			goto tr_stalled;
4276 		}
4277 		break;
4278 
4279 	case UT_READ_CLASS_DEVICE:
4280 		switch (req->bRequest) {
4281 		case UR_GET_DESCRIPTOR:
4282 			goto tr_handle_get_class_descriptor;
4283 		case UR_GET_STATUS:
4284 			goto tr_handle_get_class_status;
4285 
4286 		default:
4287 			goto tr_stalled;
4288 		}
4289 		break;
4290 	default:
4291 		goto tr_stalled;
4292 	}
4293 	goto tr_valid;
4294 
4295 tr_handle_get_descriptor:
4296 	switch (value >> 8) {
4297 	case UDESC_DEVICE:
4298 		if (value & 0xff) {
4299 			goto tr_stalled;
4300 		}
4301 		len = sizeof(dwc_otg_devd);
4302 		ptr = (const void *)&dwc_otg_devd;
4303 		goto tr_valid;
4304 	case UDESC_CONFIG:
4305 		if (value & 0xff) {
4306 			goto tr_stalled;
4307 		}
4308 		len = sizeof(dwc_otg_confd);
4309 		ptr = (const void *)&dwc_otg_confd;
4310 		goto tr_valid;
4311 	case UDESC_STRING:
4312 		switch (value & 0xff) {
4313 		case 0:		/* Language table */
4314 			len = sizeof(usb_string_lang_en);
4315 			ptr = (const void *)&usb_string_lang_en;
4316 			goto tr_valid;
4317 
4318 		case 1:		/* Vendor */
4319 			len = sizeof(dwc_otg_vendor);
4320 			ptr = (const void *)&dwc_otg_vendor;
4321 			goto tr_valid;
4322 
4323 		case 2:		/* Product */
4324 			len = sizeof(dwc_otg_product);
4325 			ptr = (const void *)&dwc_otg_product;
4326 			goto tr_valid;
4327 		default:
4328 			break;
4329 		}
4330 		break;
4331 	default:
4332 		goto tr_stalled;
4333 	}
4334 	goto tr_stalled;
4335 
4336 tr_handle_get_config:
4337 	len = 1;
4338 	sc->sc_hub_temp.wValue[0] = sc->sc_conf;
4339 	goto tr_valid;
4340 
4341 tr_handle_get_status:
4342 	len = 2;
4343 	USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
4344 	goto tr_valid;
4345 
4346 tr_handle_set_address:
4347 	if (value & 0xFF00) {
4348 		goto tr_stalled;
4349 	}
4350 	sc->sc_rt_addr = value;
4351 	goto tr_valid;
4352 
4353 tr_handle_set_config:
4354 	if (value >= 2) {
4355 		goto tr_stalled;
4356 	}
4357 	sc->sc_conf = value;
4358 	goto tr_valid;
4359 
4360 tr_handle_get_interface:
4361 	len = 1;
4362 	sc->sc_hub_temp.wValue[0] = 0;
4363 	goto tr_valid;
4364 
4365 tr_handle_get_tt_state:
4366 tr_handle_get_class_status:
4367 tr_handle_get_iface_status:
4368 tr_handle_get_ep_status:
4369 	len = 2;
4370 	USETW(sc->sc_hub_temp.wValue, 0);
4371 	goto tr_valid;
4372 
4373 tr_handle_set_halt:
4374 tr_handle_set_interface:
4375 tr_handle_set_wakeup:
4376 tr_handle_clear_wakeup:
4377 tr_handle_clear_halt:
4378 	goto tr_valid;
4379 
4380 tr_handle_clear_port_feature:
4381 	if (index != 1)
4382 		goto tr_stalled;
4383 
4384 	DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
4385 
4386 	switch (value) {
4387 	case UHF_PORT_SUSPEND:
4388 		dwc_otg_wakeup_peer(sc);
4389 		break;
4390 
4391 	case UHF_PORT_ENABLE:
4392 		if (sc->sc_flags.status_device_mode == 0) {
4393 			DWC_OTG_WRITE_4(sc, DOTG_HPRT,
4394 			    sc->sc_hprt_val | HPRT_PRTENA);
4395 		}
4396 		sc->sc_flags.port_enabled = 0;
4397 		break;
4398 
4399 	case UHF_C_PORT_RESET:
4400 		sc->sc_flags.change_reset = 0;
4401 		break;
4402 
4403 	case UHF_C_PORT_ENABLE:
4404 		sc->sc_flags.change_enabled = 0;
4405 		break;
4406 
4407 	case UHF_C_PORT_OVER_CURRENT:
4408 		sc->sc_flags.change_over_current = 0;
4409 		break;
4410 
4411 	case UHF_PORT_TEST:
4412 	case UHF_PORT_INDICATOR:
4413 		/* nops */
4414 		break;
4415 
4416 	case UHF_PORT_POWER:
4417 		sc->sc_flags.port_powered = 0;
4418 		if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4419 			sc->sc_hprt_val = 0;
4420 			DWC_OTG_WRITE_4(sc, DOTG_HPRT, HPRT_PRTENA);
4421 		}
4422 		dwc_otg_pull_down(sc);
4423 		dwc_otg_clocks_off(sc);
4424 		break;
4425 
4426 	case UHF_C_PORT_CONNECTION:
4427 		/* clear connect change flag */
4428 		sc->sc_flags.change_connect = 0;
4429 		break;
4430 
4431 	case UHF_C_PORT_SUSPEND:
4432 		sc->sc_flags.change_suspend = 0;
4433 		break;
4434 
4435 	default:
4436 		err = USB_ERR_IOERROR;
4437 		goto done;
4438 	}
4439 	goto tr_valid;
4440 
4441 tr_handle_set_port_feature:
4442 	if (index != 1) {
4443 		goto tr_stalled;
4444 	}
4445 	DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
4446 
4447 	switch (value) {
4448 	case UHF_PORT_ENABLE:
4449 		break;
4450 
4451 	case UHF_PORT_SUSPEND:
4452 		if (sc->sc_flags.status_device_mode == 0) {
4453 			/* set suspend BIT */
4454 			sc->sc_hprt_val |= HPRT_PRTSUSP;
4455 			DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4456 
4457 			/* generate HUB suspend event */
4458 			dwc_otg_suspend_irq(sc);
4459 		}
4460 		break;
4461 
4462 	case UHF_PORT_RESET:
4463 		if (sc->sc_flags.status_device_mode == 0) {
4464 
4465 			DPRINTF("PORT RESET\n");
4466 
4467 			/* enable PORT reset */
4468 			DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val | HPRT_PRTRST);
4469 
4470 			/* Wait 62.5ms for reset to complete */
4471 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4472 
4473 			DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4474 
4475 			/* Wait 62.5ms for reset to complete */
4476 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4477 
4478 			/* reset FIFOs */
4479 			(void) dwc_otg_init_fifo(sc, DWC_MODE_HOST);
4480 
4481 			sc->sc_flags.change_reset = 1;
4482 		} else {
4483 			err = USB_ERR_IOERROR;
4484 		}
4485 		break;
4486 
4487 	case UHF_PORT_TEST:
4488 	case UHF_PORT_INDICATOR:
4489 		/* nops */
4490 		break;
4491 	case UHF_PORT_POWER:
4492 		if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4493 			sc->sc_hprt_val |= HPRT_PRTPWR;
4494 			DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4495 		}
4496 		sc->sc_flags.port_powered = 1;
4497 		break;
4498 	default:
4499 		err = USB_ERR_IOERROR;
4500 		goto done;
4501 	}
4502 	goto tr_valid;
4503 
4504 tr_handle_get_port_status:
4505 
4506 	DPRINTFN(9, "UR_GET_PORT_STATUS\n");
4507 
4508 	if (index != 1)
4509 		goto tr_stalled;
4510 
4511 	if (sc->sc_flags.status_vbus)
4512 		dwc_otg_clocks_on(sc);
4513 	else
4514 		dwc_otg_clocks_off(sc);
4515 
4516 	/* Select Device Side Mode */
4517 
4518 	if (sc->sc_flags.status_device_mode) {
4519 		value = UPS_PORT_MODE_DEVICE;
4520 		dwc_otg_timer_stop(sc);
4521 	} else {
4522 		value = 0;
4523 		dwc_otg_timer_start(sc);
4524 	}
4525 
4526 	if (sc->sc_flags.status_high_speed)
4527 		value |= UPS_HIGH_SPEED;
4528 	else if (sc->sc_flags.status_low_speed)
4529 		value |= UPS_LOW_SPEED;
4530 
4531 	if (sc->sc_flags.port_powered)
4532 		value |= UPS_PORT_POWER;
4533 
4534 	if (sc->sc_flags.port_enabled)
4535 		value |= UPS_PORT_ENABLED;
4536 
4537 	if (sc->sc_flags.port_over_current)
4538 		value |= UPS_OVERCURRENT_INDICATOR;
4539 
4540 	if (sc->sc_flags.status_vbus &&
4541 	    sc->sc_flags.status_bus_reset)
4542 		value |= UPS_CURRENT_CONNECT_STATUS;
4543 
4544 	if (sc->sc_flags.status_suspend)
4545 		value |= UPS_SUSPEND;
4546 
4547 	USETW(sc->sc_hub_temp.ps.wPortStatus, value);
4548 
4549 	value = 0;
4550 
4551 	if (sc->sc_flags.change_connect)
4552 		value |= UPS_C_CONNECT_STATUS;
4553 	if (sc->sc_flags.change_suspend)
4554 		value |= UPS_C_SUSPEND;
4555 	if (sc->sc_flags.change_reset)
4556 		value |= UPS_C_PORT_RESET;
4557 	if (sc->sc_flags.change_over_current)
4558 		value |= UPS_C_OVERCURRENT_INDICATOR;
4559 
4560 	USETW(sc->sc_hub_temp.ps.wPortChange, value);
4561 	len = sizeof(sc->sc_hub_temp.ps);
4562 	goto tr_valid;
4563 
4564 tr_handle_get_class_descriptor:
4565 	if (value & 0xFF) {
4566 		goto tr_stalled;
4567 	}
4568 	ptr = (const void *)&dwc_otg_hubd;
4569 	len = sizeof(dwc_otg_hubd);
4570 	goto tr_valid;
4571 
4572 tr_stalled:
4573 	err = USB_ERR_STALLED;
4574 tr_valid:
4575 done:
4576 	*plength = len;
4577 	*pptr = ptr;
4578 	return (err);
4579 }
4580 
4581 static void
4582 dwc_otg_xfer_setup(struct usb_setup_params *parm)
4583 {
4584 	struct usb_xfer *xfer;
4585 	void *last_obj;
4586 	uint32_t ntd;
4587 	uint32_t n;
4588 	uint8_t ep_no;
4589 	uint8_t ep_type;
4590 
4591 	xfer = parm->curr_xfer;
4592 
4593 	/*
4594 	 * NOTE: This driver does not use any of the parameters that
4595 	 * are computed from the following values. Just set some
4596 	 * reasonable dummies:
4597 	 */
4598 	parm->hc_max_packet_size = 0x500;
4599 	parm->hc_max_packet_count = 3;
4600 	parm->hc_max_frame_size = 3 * 0x500;
4601 
4602 	usbd_transfer_setup_sub(parm);
4603 
4604 	/*
4605 	 * compute maximum number of TDs
4606 	 */
4607 	ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
4608 
4609 	if (ep_type == UE_CONTROL) {
4610 
4611 		ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
4612 		    + 1 /* SYNC 2 */ + 1 /* SYNC 3 */;
4613 	} else {
4614 
4615 		ntd = xfer->nframes + 1 /* SYNC */ ;
4616 	}
4617 
4618 	/*
4619 	 * check if "usbd_transfer_setup_sub" set an error
4620 	 */
4621 	if (parm->err)
4622 		return;
4623 
4624 	/*
4625 	 * allocate transfer descriptors
4626 	 */
4627 	last_obj = NULL;
4628 
4629 	ep_no = xfer->endpointno & UE_ADDR;
4630 
4631 	/*
4632 	 * Check for a valid endpoint profile in USB device mode:
4633 	 */
4634 	if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4635 		const struct usb_hw_ep_profile *pf;
4636 
4637 		dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4638 
4639 		if (pf == NULL) {
4640 			/* should not happen */
4641 			parm->err = USB_ERR_INVAL;
4642 			return;
4643 		}
4644 	}
4645 
4646 	/* align data */
4647 	parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4648 
4649 	for (n = 0; n != ntd; n++) {
4650 
4651 		struct dwc_otg_td *td;
4652 
4653 		if (parm->buf) {
4654 
4655 			td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4656 
4657 			/* init TD */
4658 			td->max_packet_size = xfer->max_packet_size;
4659 			td->max_packet_count = xfer->max_packet_count;
4660 			td->ep_no = ep_no;
4661 			td->ep_type = ep_type;
4662 			td->obj_next = last_obj;
4663 
4664 			last_obj = td;
4665 		}
4666 		parm->size[0] += sizeof(*td);
4667 	}
4668 
4669 	xfer->td_start[0] = last_obj;
4670 }
4671 
4672 static void
4673 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
4674 {
4675 	return;
4676 }
4677 
4678 static void
4679 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4680     struct usb_endpoint *ep)
4681 {
4682 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4683 
4684 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
4685 	    ep, udev->address,
4686 	    edesc->bEndpointAddress, udev->flags.usb_mode,
4687 	    sc->sc_rt_addr, udev->device_index);
4688 
4689 	if (udev->device_index != sc->sc_rt_addr) {
4690 
4691 		if (udev->flags.usb_mode == USB_MODE_DEVICE) {
4692 			if (udev->speed != USB_SPEED_FULL &&
4693 			    udev->speed != USB_SPEED_HIGH) {
4694 				/* not supported */
4695 				return;
4696 			}
4697 		} else {
4698 			if (udev->speed == USB_SPEED_HIGH) {
4699 				if ((UGETW(edesc->wMaxPacketSize) >> 11) & 3) {
4700 					/* high bandwidth endpoint - not tested */
4701 					DPRINTF("High Bandwidth Endpoint - not tested\n");
4702 					return;
4703 				}
4704 			}
4705 		}
4706 		if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
4707 			ep->methods = &dwc_otg_device_isoc_methods;
4708 		else
4709 			ep->methods = &dwc_otg_device_non_isoc_methods;
4710 	}
4711 }
4712 
4713 static void
4714 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
4715 {
4716 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4717 
4718 	switch (state) {
4719 	case USB_HW_POWER_SUSPEND:
4720 		dwc_otg_suspend(sc);
4721 		break;
4722 	case USB_HW_POWER_SHUTDOWN:
4723 		dwc_otg_uninit(sc);
4724 		break;
4725 	case USB_HW_POWER_RESUME:
4726 		dwc_otg_resume(sc);
4727 		break;
4728 	default:
4729 		break;
4730 	}
4731 }
4732 
4733 static void
4734 dwc_otg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4735 {
4736 	/* DMA delay - wait until any use of memory is finished */
4737 	*pus = (2125);			/* microseconds */
4738 }
4739 
4740 static void
4741 dwc_otg_device_resume(struct usb_device *udev)
4742 {
4743 	DPRINTF("\n");
4744 
4745 	/* poll all transfers again to restart resumed ones */
4746 	dwc_otg_do_poll(udev->bus);
4747 }
4748 
4749 static void
4750 dwc_otg_device_suspend(struct usb_device *udev)
4751 {
4752 	DPRINTF("\n");
4753 }
4754 
4755 static const struct usb_bus_methods dwc_otg_bus_methods =
4756 {
4757 	.endpoint_init = &dwc_otg_ep_init,
4758 	.xfer_setup = &dwc_otg_xfer_setup,
4759 	.xfer_unsetup = &dwc_otg_xfer_unsetup,
4760 	.get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
4761 	.xfer_stall = &dwc_otg_xfer_stall,
4762 	.set_stall = &dwc_otg_set_stall,
4763 	.clear_stall = &dwc_otg_clear_stall,
4764 	.roothub_exec = &dwc_otg_roothub_exec,
4765 	.xfer_poll = &dwc_otg_do_poll,
4766 	.device_state_change = &dwc_otg_device_state_change,
4767 	.set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,
4768 	.get_dma_delay = &dwc_otg_get_dma_delay,
4769 	.device_resume = &dwc_otg_device_resume,
4770 	.device_suspend = &dwc_otg_device_suspend,
4771 };
4772