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