xref: /freebsd/sys/dev/usb/controller/dwc_otg.c (revision e39e854e27f53a784c3982cbeb68f4ad1cfd9162)
1 /*-
2  * Copyright (c) 2012 Hans Petter Selasky. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 /*
27  * This file contains the driver for the DesignWare series USB 2.0 OTG
28  * Controller. This driver currently only supports the device mode of
29  * the USB hardware.
30  */
31 
32 /*
33  * LIMITATION: Drivers must be bound to all OUT endpoints in the
34  * active configuration for this driver to work properly. Blocking any
35  * OUT endpoint will block all OUT endpoints including the control
36  * endpoint. Usually this is not a problem.
37  */
38 
39 /*
40  * NOTE: Writing to non-existing registers appears to cause an
41  * internal reset.
42  */
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/stdint.h>
48 #include <sys/stddef.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/types.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/bus.h>
55 #include <sys/module.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/sx.h>
61 #include <sys/unistd.h>
62 #include <sys/callout.h>
63 #include <sys/malloc.h>
64 #include <sys/priv.h>
65 
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 
69 #define	USB_DEBUG_VAR dwc_otg_debug
70 
71 #include <dev/usb/usb_core.h>
72 #include <dev/usb/usb_debug.h>
73 #include <dev/usb/usb_busdma.h>
74 #include <dev/usb/usb_process.h>
75 #include <dev/usb/usb_transfer.h>
76 #include <dev/usb/usb_device.h>
77 #include <dev/usb/usb_hub.h>
78 #include <dev/usb/usb_util.h>
79 
80 #include <dev/usb/usb_controller.h>
81 #include <dev/usb/usb_bus.h>
82 
83 #include <dev/usb/controller/dwc_otg.h>
84 
85 #define	DWC_OTG_BUS2SC(bus) \
86    ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
87     ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
88 
89 #define	DWC_OTG_PC2SC(pc) \
90    DWC_OTG_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
91 
92 #define	DWC_OTG_MSK_GINT_ENABLED	\
93    (DWC_OTG_MSK_GINT_ENUM_DONE |	\
94    DWC_OTG_MSK_GINT_USB_RESET |		\
95    DWC_OTG_MSK_GINT_USB_SUSPEND |	\
96    DWC_OTG_MSK_GINT_INEP |		\
97    DWC_OTG_MSK_GINT_RXFLVL |		\
98    DWC_OTG_MSK_GINT_SESSREQINT)
99 
100 #define DWC_OTG_USE_HSIC 0
101 
102 #ifdef USB_DEBUG
103 static int dwc_otg_debug = 0;
104 
105 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW, 0, "USB DWC OTG");
106 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RW,
107     &dwc_otg_debug, 0, "DWC OTG debug level");
108 #endif
109 
110 #define	DWC_OTG_INTR_ENDPT 1
111 
112 /* prototypes */
113 
114 struct usb_bus_methods dwc_otg_bus_methods;
115 struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
116 struct usb_pipe_methods dwc_otg_device_isoc_fs_methods;
117 
118 static dwc_otg_cmd_t dwc_otg_setup_rx;
119 static dwc_otg_cmd_t dwc_otg_data_rx;
120 static dwc_otg_cmd_t dwc_otg_data_tx;
121 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
122 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
123 static void dwc_otg_do_poll(struct usb_bus *);
124 static void dwc_otg_standard_done(struct usb_xfer *);
125 static void dwc_otg_root_intr(struct dwc_otg_softc *sc);
126 
127 /*
128  * Here is a configuration that the chip supports.
129  */
130 static const struct usb_hw_ep_profile dwc_otg_ep_profile[1] = {
131 
132 	[0] = {
133 		.max_in_frame_size = 64,/* fixed */
134 		.max_out_frame_size = 64,	/* fixed */
135 		.is_simplex = 1,
136 		.support_control = 1,
137 	}
138 };
139 
140 static void
141 dwc_otg_get_hw_ep_profile(struct usb_device *udev,
142     const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
143 {
144 	struct dwc_otg_softc *sc;
145 
146 	sc = DWC_OTG_BUS2SC(udev->bus);
147 
148 	if (ep_addr < sc->sc_dev_ep_max)
149 		*ppf = &sc->sc_hw_ep_profile[ep_addr].usb;
150 	else
151 		*ppf = NULL;
152 }
153 
154 static int
155 dwc_otg_init_fifo(struct dwc_otg_softc *sc)
156 {
157 	struct dwc_otg_profile *pf;
158 	uint32_t fifo_size;
159 	uint32_t fifo_regs;
160 	uint32_t tx_start;
161 	uint8_t x;
162 
163 	fifo_size = sc->sc_fifo_size;
164 
165 	fifo_regs = 4 * (sc->sc_dev_ep_max + sc->sc_dev_in_ep_max);
166 
167 	if (fifo_size >= fifo_regs)
168 		fifo_size -= fifo_regs;
169 	else
170 		fifo_size = 0;
171 
172 	/* split equally for IN and OUT */
173 	fifo_size /= 2;
174 
175 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRXFSIZ, fifo_size / 4);
176 
177 	/* align to 4-bytes */
178 	fifo_size &= ~3;
179 
180 	tx_start = fifo_size;
181 
182 	if (fifo_size < 0x40) {
183 		DPRINTFN(-1, "Not enough data space for EP0 FIFO.\n");
184 		USB_BUS_UNLOCK(&sc->sc_bus);
185 		return (EINVAL);
186 	}
187 
188 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GNPTXFSIZ, (0x10 << 16) | (tx_start / 4));
189 	fifo_size -= 0x40;
190 	tx_start += 0x40;
191 
192 	/* setup control endpoint profile */
193 	sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
194 
195 	for (x = 1; x != sc->sc_dev_ep_max; x++) {
196 
197 		pf = sc->sc_hw_ep_profile + x;
198 
199 		pf->usb.max_out_frame_size = 1024 * 3;
200 		pf->usb.is_simplex = 0;	/* assume duplex */
201 		pf->usb.support_bulk = 1;
202 		pf->usb.support_interrupt = 1;
203 		pf->usb.support_isochronous = 1;
204 		pf->usb.support_out = 1;
205 
206 		if (x < sc->sc_dev_in_ep_max) {
207 			uint32_t limit;
208 
209 			limit = (x == 1) ? DWC_OTG_MAX_TXN :
210 			    (DWC_OTG_MAX_TXN / 2);
211 
212 			if (fifo_size >= limit) {
213 				DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTXF(x),
214 				    ((limit / 4) << 16) |
215 				    (tx_start / 4));
216 				tx_start += limit;
217 				fifo_size -= limit;
218 				pf->usb.max_in_frame_size = 0x200;
219 				pf->usb.support_in = 1;
220 				pf->max_buffer = limit;
221 
222 			} else if (fifo_size >= 0x80) {
223 				DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTXF(x),
224 				    ((0x80 / 4) << 16) | (tx_start / 4));
225 				tx_start += 0x80;
226 				fifo_size -= 0x80;
227 				pf->usb.max_in_frame_size = 0x40;
228 				pf->usb.support_in = 1;
229 
230 			} else {
231 				pf->usb.is_simplex = 1;
232 				DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTXF(x),
233 				    (0x0 << 16) | (tx_start / 4));
234 			}
235 		} else {
236 			pf->usb.is_simplex = 1;
237 		}
238 
239 		DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
240 		    pf->usb.max_in_frame_size,
241 		    pf->usb.max_out_frame_size);
242 	}
243 
244 	/* reset RX FIFO */
245 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
246 	    DWC_OTG_MSK_GRSTCTL_RXFFLUSH);
247 
248 	/* reset all TX FIFOs */
249 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
250 	   DWC_OTG_MSK_GRSTCTL_TXFIFO(0x10) |
251 	   DWC_OTG_MSK_GRSTCTL_TXFFLUSH);
252 
253 	return (0);
254 }
255 
256 static void
257 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
258 {
259 	if (sc->sc_flags.clocks_off &&
260 	    sc->sc_flags.port_powered) {
261 
262 		DPRINTFN(5, "\n");
263 
264 		/* TODO - platform specific */
265 
266 		sc->sc_flags.clocks_off = 0;
267 	}
268 }
269 
270 static void
271 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
272 {
273 	if (!sc->sc_flags.clocks_off) {
274 
275 		DPRINTFN(5, "\n");
276 
277 		/* TODO - platform specific */
278 
279 		sc->sc_flags.clocks_off = 1;
280 	}
281 }
282 
283 static void
284 dwc_otg_pull_up(struct dwc_otg_softc *sc)
285 {
286 	uint32_t temp;
287 
288 	/* pullup D+, if possible */
289 
290 	if (!sc->sc_flags.d_pulled_up &&
291 	    sc->sc_flags.port_powered) {
292 		sc->sc_flags.d_pulled_up = 1;
293 
294 		temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DCTL);
295 		temp &= ~DWC_OTG_MSK_DCTL_SOFT_DISC;
296 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL, temp);
297 	}
298 }
299 
300 static void
301 dwc_otg_pull_down(struct dwc_otg_softc *sc)
302 {
303 	uint32_t temp;
304 
305 	/* pulldown D+, if possible */
306 
307 	if (sc->sc_flags.d_pulled_up) {
308 		sc->sc_flags.d_pulled_up = 0;
309 
310 		temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DCTL);
311 		temp |= DWC_OTG_MSK_DCTL_SOFT_DISC;
312 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL, temp);
313 	}
314 }
315 
316 static void
317 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
318 {
319 	if (sc->sc_flags.status_suspend) {
320 		/* update status bits */
321 		sc->sc_flags.status_suspend = 0;
322 		sc->sc_flags.change_suspend = 1;
323 
324 		/*
325 		 * Disable resume interrupt and enable suspend
326 		 * interrupt:
327 		 */
328 		sc->sc_irq_mask &= ~DWC_OTG_MSK_GINT_WKUPINT;
329 		sc->sc_irq_mask |= DWC_OTG_MSK_GINT_USB_SUSPEND;
330 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
331 
332 		/* complete root HUB interrupt endpoint */
333 		dwc_otg_root_intr(sc);
334 	}
335 }
336 
337 static void
338 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
339 {
340 	uint32_t temp;
341 
342 	if (!sc->sc_flags.status_suspend)
343 		return;
344 
345 	DPRINTFN(5, "Remote wakeup\n");
346 
347 	/* enable remote wakeup signalling */
348 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DCTL);
349 	temp |= DWC_OTG_MSK_DCTL_REMOTE_WAKEUP;
350 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL, temp);
351 
352 	/* Wait 8ms for remote wakeup to complete. */
353 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
354 
355 	temp &= ~DWC_OTG_MSK_DCTL_REMOTE_WAKEUP;
356 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL, temp);
357 
358 	/* need to fake resume IRQ */
359 	dwc_otg_resume_irq(sc);
360 }
361 
362 static void
363 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
364 {
365 	uint32_t temp;
366 
367 	DPRINTFN(5, "addr=%d\n", addr);
368 
369 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DCFG);
370 	temp &= ~DWC_OTG_MSK_DCFG_SET_DEV_ADDR(0x7F);
371 	temp |= DWC_OTG_MSK_DCFG_SET_DEV_ADDR(addr);
372 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCFG, temp);
373 }
374 
375 static void
376 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
377 {
378 	DPRINTFN(5, "RX status clear\n");
379 
380 	/* enable RX FIFO level interrupt */
381 	sc->sc_irq_mask |= DWC_OTG_MSK_GINT_RXFLVL;
382 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
383 
384 	/* clear cached status */
385 	sc->sc_last_rx_status = 0;
386 }
387 
388 static uint8_t
389 dwc_otg_setup_rx(struct dwc_otg_td *td)
390 {
391 	struct dwc_otg_softc *sc;
392 	struct usb_device_request req __aligned(4);
393 	uint32_t temp;
394 	uint16_t count;
395 
396 	/* get pointer to softc */
397 	sc = DWC_OTG_PC2SC(td->pc);
398 
399 	/* check endpoint status */
400 
401 	if (sc->sc_last_rx_status == 0)
402 		goto not_complete;
403 
404 	if (DWC_OTG_MSK_GRXSTS_GET_CHANNEL(sc->sc_last_rx_status) != 0)
405 		goto not_complete;
406 
407 	if ((sc->sc_last_rx_status & DWC_OTG_MSK_GRXSTS_PID) !=
408 	    DWC_OTG_MSK_GRXSTS_PID_DATA0) {
409 		/* release FIFO */
410 		dwc_otg_common_rx_ack(sc);
411 		goto not_complete;
412 	}
413 
414 	if ((sc->sc_last_rx_status & DWC_OTG_MSK_GRXSTS_PACKET_STS) !=
415 	    DWC_OTG_MSK_GRXSTS_DEV_STP_DATA) {
416 		/* release FIFO */
417 		dwc_otg_common_rx_ack(sc);
418 		goto not_complete;
419 	}
420 
421 	DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
422 
423 	/* clear did stall */
424 	td->did_stall = 0;
425 
426 	/* get the packet byte count */
427 	count = DWC_OTG_MSK_GRXSTS_GET_BYTE_CNT(sc->sc_last_rx_status);
428 
429 	/* verify data length */
430 	if (count != td->remainder) {
431 		DPRINTFN(0, "Invalid SETUP packet "
432 		    "length, %d bytes\n", count);
433 		/* release FIFO */
434 		dwc_otg_common_rx_ack(sc);
435 		goto not_complete;
436 	}
437 	if (count != sizeof(req)) {
438 		DPRINTFN(0, "Unsupported SETUP packet "
439 		    "length, %d bytes\n", count);
440 		/* release FIFO */
441 		dwc_otg_common_rx_ack(sc);
442 		goto not_complete;
443 	}
444 
445 	/* copy in control request */
446 	memcpy(&req, sc->sc_rx_bounce_buffer, sizeof(req));
447 
448 	/* copy data into real buffer */
449 	usbd_copy_in(td->pc, 0, &req, sizeof(req));
450 
451 	td->offset = sizeof(req);
452 	td->remainder = 0;
453 
454 	/* sneak peek the set address */
455 	if ((req.bmRequestType == UT_WRITE_DEVICE) &&
456 	    (req.bRequest == UR_SET_ADDRESS)) {
457 		/* must write address before ZLP */
458 		dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
459 	}
460 
461 	/* don't send any data by default */
462 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTSIZ(0),
463 	    DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(0) |
464 	    DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(0));
465 
466 	temp = sc->sc_in_ctl[0];
467 
468 	/* enable IN endpoint */
469 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(0),
470 	    temp | DWC_OTG_MSK_DIEPCTL_ENABLE);
471 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(0),
472 	    temp | DWC_OTG_MSK_DIEPCTL_SET_NAK);
473 
474 	/* reset IN endpoint buffer */
475 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
476 	    DWC_OTG_MSK_GRSTCTL_TXFIFO(0) |
477 	    DWC_OTG_MSK_GRSTCTL_TXFFLUSH);
478 
479 	/* acknowledge RX status */
480 	dwc_otg_common_rx_ack(sc);
481 	return (0);			/* complete */
482 
483 not_complete:
484 	/* abort any ongoing transfer, before enabling again */
485 
486 	temp = sc->sc_out_ctl[0];
487 
488 	temp |= DWC_OTG_MSK_DOEPCTL_ENABLE |
489 	    DWC_OTG_MSK_DOEPCTL_SET_NAK;
490 
491 	/* enable OUT endpoint */
492 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(0), temp);
493 
494 	if (!td->did_stall) {
495 		td->did_stall = 1;
496 
497 		DPRINTFN(5, "stalling IN and OUT direction\n");
498 
499 		/* set stall after enabling endpoint */
500 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(0),
501 		    temp | DWC_OTG_MSK_DOEPCTL_STALL);
502 
503 		temp = sc->sc_in_ctl[0];
504 
505 		/* set stall assuming endpoint is enabled */
506 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(0),
507 		    temp | DWC_OTG_MSK_DIEPCTL_STALL);
508 	}
509 
510 	/* setup number of buffers to receive */
511 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPTSIZ(0),
512 	    DWC_OTG_MSK_DXEPTSIZ_SET_MULTI(3) |
513 	    DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(1) |
514 	    DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(sizeof(req)));
515 
516 	return (1);			/* not complete */
517 }
518 
519 static uint8_t
520 dwc_otg_data_rx(struct dwc_otg_td *td)
521 {
522 	struct dwc_otg_softc *sc;
523 	uint32_t temp;
524 	uint16_t count;
525 	uint8_t got_short;
526 
527 	got_short = 0;
528 
529 	/* get pointer to softc */
530 	sc = DWC_OTG_PC2SC(td->pc);
531 
532 	/* check endpoint status */
533 	if (sc->sc_last_rx_status == 0)
534 		goto not_complete;
535 
536 	if (DWC_OTG_MSK_GRXSTS_GET_CHANNEL(sc->sc_last_rx_status) != td->ep_no)
537 		goto not_complete;
538 
539 	/* check for SETUP packet */
540 	if ((sc->sc_last_rx_status & DWC_OTG_MSK_GRXSTS_PACKET_STS) ==
541 	    DWC_OTG_MSK_GRXSTS_DEV_STP_DATA) {
542 		if (td->remainder == 0) {
543 			/*
544 			 * We are actually complete and have
545 			 * received the next SETUP
546 			 */
547 			DPRINTFN(5, "faking complete\n");
548 			return (0);	/* complete */
549 		}
550 		/*
551 		 * USB Host Aborted the transfer.
552 		 */
553 		td->error = 1;
554 		return (0);		/* complete */
555 	}
556 
557 	if ((sc->sc_last_rx_status & DWC_OTG_MSK_GRXSTS_PACKET_STS) !=
558 	    DWC_OTG_MSK_GRXSTS_DEV_OUT_DATA) {
559 		/* release FIFO */
560 		dwc_otg_common_rx_ack(sc);
561 		goto not_complete;
562 	}
563 
564 	/* get the packet byte count */
565 	count = DWC_OTG_MSK_GRXSTS_GET_BYTE_CNT(sc->sc_last_rx_status);
566 
567 	/* verify the packet byte count */
568 	if (count != td->max_packet_size) {
569 		if (count < td->max_packet_size) {
570 			/* we have a short packet */
571 			td->short_pkt = 1;
572 			got_short = 1;
573 		} else {
574 			/* invalid USB packet */
575 			td->error = 1;
576 
577 			/* release FIFO */
578 			dwc_otg_common_rx_ack(sc);
579 			return (0);	/* we are complete */
580 		}
581 	}
582 	/* verify the packet byte count */
583 	if (count > td->remainder) {
584 		/* invalid USB packet */
585 		td->error = 1;
586 
587 		/* release FIFO */
588 		dwc_otg_common_rx_ack(sc);
589 		return (0);		/* we are complete */
590 	}
591 
592 	usbd_copy_in(td->pc, td->offset, sc->sc_rx_bounce_buffer, count);
593 	td->remainder -= count;
594 	td->offset += count;
595 
596 	/* release FIFO */
597 	dwc_otg_common_rx_ack(sc);
598 
599 	/* check if we are complete */
600 	if ((td->remainder == 0) || got_short) {
601 		if (td->short_pkt) {
602 			/* we are complete */
603 			return (0);
604 		}
605 		/* else need to receive a zero length packet */
606 	}
607 
608 not_complete:
609 
610 	temp = sc->sc_out_ctl[td->ep_no];
611 
612 	temp |= DWC_OTG_MSK_DOEPCTL_ENABLE |
613 	    DWC_OTG_MSK_DOEPCTL_CLR_NAK;
614 
615 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(td->ep_no), temp);
616 
617 	/* enable SETUP and transfer complete interrupt */
618 	if (td->ep_no == 0) {
619 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPTSIZ(0),
620 		    DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(1) |
621 		    DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(td->max_packet_size));
622 	} else {
623 		/* allow reception of multiple packets */
624 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPTSIZ(td->ep_no),
625 		    DWC_OTG_MSK_DXEPTSIZ_SET_MULTI(1) |
626 		    DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(4) |
627 		    DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(4 *
628 		    ((td->max_packet_size + 3) & ~3)));
629 	}
630 	return (1);			/* not complete */
631 }
632 
633 static uint8_t
634 dwc_otg_data_tx(struct dwc_otg_td *td)
635 {
636 	struct dwc_otg_softc *sc;
637 	uint32_t max_buffer;
638 	uint32_t count;
639 	uint32_t fifo_left;
640 	uint32_t mpkt;
641 	uint32_t temp;
642 	uint8_t to;
643 
644 	to = 3;				/* don't loop forever! */
645 
646 	/* get pointer to softc */
647 	sc = DWC_OTG_PC2SC(td->pc);
648 
649 	max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
650 
651 repeat:
652 	/* check for for endpoint 0 data */
653 
654 	temp = sc->sc_last_rx_status;
655 
656 	if ((td->ep_no == 0) && (temp != 0) &&
657 	    (DWC_OTG_MSK_GRXSTS_GET_CHANNEL(temp) == 0)) {
658 
659 		if ((temp & DWC_OTG_MSK_GRXSTS_PACKET_STS) !=
660 		    DWC_OTG_MSK_GRXSTS_DEV_STP_DATA) {
661 
662 			/* dump data - wrong direction */
663 			dwc_otg_common_rx_ack(sc);
664 		} else {
665 			/*
666 			 * The current transfer was cancelled
667 			 * by the USB Host:
668 			 */
669 			td->error = 1;
670 			return (0);		/* complete */
671 		}
672 	}
673 
674 	/* fill in more TX data, if possible */
675 	if (td->tx_bytes != 0) {
676 
677 		uint16_t cpkt;
678 
679 		/* check if packets have been transferred */
680 		temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPTSIZ(td->ep_no));
681 
682 		/* get current packet number */
683 		cpkt = DWC_OTG_MSK_DXEPTSIZ_GET_NPKT(temp);
684 
685 		if (cpkt >= td->npkt) {
686 			fifo_left = 0;
687 		} else {
688 			if (max_buffer != 0) {
689 				fifo_left = (td->npkt - cpkt) *
690 				    td->max_packet_size;
691 
692 				if (fifo_left > max_buffer)
693 					fifo_left = max_buffer;
694 			} else {
695 				fifo_left = td->max_packet_size;
696 			}
697 		}
698 
699 		count = td->tx_bytes;
700 		if (count > fifo_left)
701 			count = fifo_left;
702 
703 		if (count != 0) {
704 
705 			/* clear topmost word before copy */
706 			sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
707 
708 			/* copy out data */
709 			usbd_copy_out(td->pc, td->offset,
710 			    sc->sc_tx_bounce_buffer, count);
711 
712 			/* transfer data into FIFO */
713 			bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
714 			    DWC_OTG_REG_DFIFO(td->ep_no),
715 			    sc->sc_tx_bounce_buffer, (count + 3) / 4);
716 
717 			td->tx_bytes -= count;
718 			td->remainder -= count;
719 			td->offset += count;
720 			td->npkt = cpkt;
721 		}
722 		if (td->tx_bytes != 0)
723 			goto not_complete;
724 
725 		/* check remainder */
726 		if (td->remainder == 0) {
727 			if (td->short_pkt)
728 				return (0);	/* complete */
729 
730 			/* else we need to transmit a short packet */
731 		}
732 	}
733 
734 	if (!to--)
735 		goto not_complete;
736 
737 	/* check if not all packets have been transferred */
738 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPTSIZ(td->ep_no));
739 
740 	if (DWC_OTG_MSK_DXEPTSIZ_GET_NPKT(temp) != 0) {
741 
742 		DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
743 		    "DIEPCTL=0x%08x\n", td->ep_no,
744 		    DWC_OTG_MSK_DXEPTSIZ_GET_NPKT(temp),
745 		    temp, DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPCTL(td->ep_no)));
746 
747 		goto not_complete;
748 	}
749 
750 	DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
751 
752 	/* try to optimise by sending more data */
753 	if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
754 
755 		/* send multiple packets at the same time */
756 		mpkt = max_buffer / td->max_packet_size;
757 
758 		if (mpkt > 0x3FE)
759 			mpkt = 0x3FE;
760 
761 		count = td->remainder;
762 		if (count > 0x7FFFFF)
763 			count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
764 
765 		td->npkt = count / td->max_packet_size;
766 
767 		/*
768 		 * NOTE: We could use 0x3FE instead of "mpkt" in the
769 		 * check below to get more throughput, but then we
770 		 * have a dependency towards non-generic chip features
771 		 * to disable the TX-FIFO-EMPTY interrupts on a per
772 		 * endpoint basis. Increase the maximum buffer size of
773 		 * the IN endpoint to increase the performance.
774 		 */
775 		if (td->npkt > mpkt) {
776 			td->npkt = mpkt;
777 			count = td->max_packet_size * mpkt;
778 		} else if ((count == 0) || (count % td->max_packet_size)) {
779 			/* we are transmitting a short packet */
780 			td->npkt++;
781 			td->short_pkt = 1;
782 		}
783 	} else {
784 		/* send one packet at a time */
785 		mpkt = 1;
786 		count = td->max_packet_size;
787 		if (td->remainder < count) {
788 			/* we have a short packet */
789 			td->short_pkt = 1;
790 			count = td->remainder;
791 		}
792 		td->npkt = 1;
793 	}
794 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTSIZ(td->ep_no),
795 	    DWC_OTG_MSK_DXEPTSIZ_SET_MULTI(1) |
796 	    DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(td->npkt) |
797 	    DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(count));
798 
799 	/* make room for buffering */
800 	td->npkt += mpkt;
801 
802 	temp = sc->sc_in_ctl[td->ep_no];
803 
804 	/* must enable before writing data to FIFO */
805 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(td->ep_no), temp |
806 	    DWC_OTG_MSK_DIEPCTL_ENABLE |
807 	    DWC_OTG_MSK_DIEPCTL_CLR_NAK);
808 
809 	td->tx_bytes = count;
810 
811 	/* check remainder */
812 	if (td->tx_bytes == 0 &&
813 	    td->remainder == 0) {
814 		if (td->short_pkt)
815 			return (0);	/* complete */
816 
817 		/* else we need to transmit a short packet */
818 	}
819 	goto repeat;
820 
821 not_complete:
822 	return (1);			/* not complete */
823 }
824 
825 static uint8_t
826 dwc_otg_data_tx_sync(struct dwc_otg_td *td)
827 {
828 	struct dwc_otg_softc *sc;
829 	uint32_t temp;
830 
831 	/* get pointer to softc */
832 	sc = DWC_OTG_PC2SC(td->pc);
833 
834 	/*
835 	 * If all packets are transferred we are complete:
836 	 */
837 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPTSIZ(td->ep_no));
838 
839 	/* check that all packets have been transferred */
840 	if (DWC_OTG_MSK_DXEPTSIZ_GET_NPKT(temp) != 0) {
841 		DPRINTFN(5, "busy ep=%d\n", td->ep_no);
842 		goto not_complete;
843 	}
844 	return (0);
845 
846 not_complete:
847 
848 	/* we only want to know if there is a SETUP packet or free IN packet */
849 
850 	temp = sc->sc_last_rx_status;
851 
852 	if ((td->ep_no == 0) && (temp != 0) &&
853 	    (DWC_OTG_MSK_GRXSTS_GET_CHANNEL(temp) == 0)) {
854 
855 		if ((temp & DWC_OTG_MSK_GRXSTS_PACKET_STS) ==
856 		    DWC_OTG_MSK_GRXSTS_DEV_STP_DATA) {
857 			DPRINTFN(5, "faking complete\n");
858 			/*
859 			 * Race condition: We are complete!
860 			 */
861 			return (0);
862 		} else {
863 			/* dump data - wrong direction */
864 			dwc_otg_common_rx_ack(sc);
865 		}
866 	}
867 	return (1);			/* not complete */
868 }
869 
870 static uint8_t
871 dwc_otg_xfer_do_fifo(struct usb_xfer *xfer)
872 {
873 	struct dwc_otg_td *td;
874 
875 	DPRINTFN(9, "\n");
876 
877 	td = xfer->td_transfer_cache;
878 	while (1) {
879 		if ((td->func) (td)) {
880 			/* operation in progress */
881 			break;
882 		}
883 		if (((void *)td) == xfer->td_transfer_last) {
884 			goto done;
885 		}
886 		if (td->error) {
887 			goto done;
888 		} else if (td->remainder > 0) {
889 			/*
890 			 * We had a short transfer. If there is no alternate
891 			 * next, stop processing !
892 			 */
893 			if (!td->alt_next)
894 				goto done;
895 		}
896 
897 		/*
898 		 * Fetch the next transfer descriptor and transfer
899 		 * some flags to the next transfer descriptor
900 		 */
901 		td = td->obj_next;
902 		xfer->td_transfer_cache = td;
903 	}
904 	return (1);			/* not complete */
905 
906 done:
907 	/* compute all actual lengths */
908 
909 	dwc_otg_standard_done(xfer);
910 	return (0);			/* complete */
911 }
912 
913 static void
914 dwc_otg_interrupt_poll(struct dwc_otg_softc *sc)
915 {
916 	struct usb_xfer *xfer;
917 	uint32_t temp;
918 	uint8_t got_rx_status;
919 
920 repeat:
921 	if (sc->sc_last_rx_status == 0) {
922 
923 		temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GINTSTS);
924 		if (temp & DWC_OTG_MSK_GINT_RXFLVL) {
925 			/* pop current status */
926 			sc->sc_last_rx_status =
927 			    DWC_OTG_READ_4(sc, DWC_OTG_REG_GRXSTSP);
928 		}
929 
930 		if (sc->sc_last_rx_status != 0) {
931 
932 			uint8_t ep_no;
933 
934 			temp = DWC_OTG_MSK_GRXSTS_GET_BYTE_CNT(
935 			    sc->sc_last_rx_status);
936 			ep_no = DWC_OTG_MSK_GRXSTS_GET_CHANNEL(
937 			    sc->sc_last_rx_status);
938 
939 			/* receive data, if any */
940 			if (temp != 0) {
941 				DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
942 				bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
943 				    DWC_OTG_REG_DFIFO(ep_no),
944 				    sc->sc_rx_bounce_buffer, (temp + 3) / 4);
945 			}
946 
947 			temp = sc->sc_last_rx_status &
948 			    DWC_OTG_MSK_GRXSTS_PACKET_STS;
949 
950 			/* non-data messages we simply skip */
951 			if (temp != DWC_OTG_MSK_GRXSTS_DEV_STP_DATA &&
952 			    temp != DWC_OTG_MSK_GRXSTS_DEV_OUT_DATA) {
953 				dwc_otg_common_rx_ack(sc);
954 				goto repeat;
955 			}
956 
957 			/* check if we should dump the data */
958 			if (!(sc->sc_active_out_ep & (1U << ep_no))) {
959 				dwc_otg_common_rx_ack(sc);
960 				goto repeat;
961 			}
962 
963 			got_rx_status = 1;
964 
965 			DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
966 			    sc->sc_last_rx_status, ep_no,
967 			    (sc->sc_last_rx_status >> 15) & 3,
968 			    DWC_OTG_MSK_GRXSTS_GET_BYTE_CNT(sc->sc_last_rx_status),
969 			    (sc->sc_last_rx_status >> 17) & 15);
970 		} else {
971 			got_rx_status = 0;
972 		}
973 	} else {
974 		uint8_t ep_no;
975 
976 		ep_no = DWC_OTG_MSK_GRXSTS_GET_CHANNEL(
977 		    sc->sc_last_rx_status);
978 
979 		/* check if we should dump the data */
980 		if (!(sc->sc_active_out_ep & (1U << ep_no))) {
981 			dwc_otg_common_rx_ack(sc);
982 			goto repeat;
983 		}
984 
985 		got_rx_status = 1;
986 	}
987 
988 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
989 		if (!dwc_otg_xfer_do_fifo(xfer)) {
990 			/* queue has been modified */
991 			goto repeat;
992 		}
993 	}
994 
995 	if (got_rx_status) {
996 		if (sc->sc_last_rx_status == 0)
997 			goto repeat;
998 
999 		/* disable RX FIFO level interrupt */
1000 		sc->sc_irq_mask &= ~DWC_OTG_MSK_GINT_RXFLVL;
1001 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
1002 	}
1003 }
1004 
1005 static void
1006 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
1007 {
1008 	DPRINTFN(5, "vbus = %u\n", is_on);
1009 
1010 	if (is_on) {
1011 		if (!sc->sc_flags.status_vbus) {
1012 			sc->sc_flags.status_vbus = 1;
1013 
1014 			/* complete root HUB interrupt endpoint */
1015 
1016 			dwc_otg_root_intr(sc);
1017 		}
1018 	} else {
1019 		if (sc->sc_flags.status_vbus) {
1020 			sc->sc_flags.status_vbus = 0;
1021 			sc->sc_flags.status_bus_reset = 0;
1022 			sc->sc_flags.status_suspend = 0;
1023 			sc->sc_flags.change_suspend = 0;
1024 			sc->sc_flags.change_connect = 1;
1025 
1026 			/* complete root HUB interrupt endpoint */
1027 
1028 			dwc_otg_root_intr(sc);
1029 		}
1030 	}
1031 }
1032 
1033 void
1034 dwc_otg_interrupt(struct dwc_otg_softc *sc)
1035 {
1036 	uint32_t status;
1037 
1038 	USB_BUS_LOCK(&sc->sc_bus);
1039 
1040 	/* read and clear interrupt status */
1041 	status = DWC_OTG_READ_4(sc, DWC_OTG_REG_GINTSTS);
1042 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTSTS, status);
1043 
1044 	DPRINTFN(14, "GINTSTS=0x%08x\n", status);
1045 
1046 	if (status & DWC_OTG_MSK_GINT_USB_RESET) {
1047 
1048 		/* set correct state */
1049 		sc->sc_flags.status_bus_reset = 0;
1050 		sc->sc_flags.status_suspend = 0;
1051 		sc->sc_flags.change_suspend = 0;
1052 		sc->sc_flags.change_connect = 1;
1053 
1054 		/* complete root HUB interrupt endpoint */
1055 		dwc_otg_root_intr(sc);
1056 	}
1057 
1058 	/* check for any bus state change interrupts */
1059 	if (status & DWC_OTG_MSK_GINT_ENUM_DONE) {
1060 
1061 		uint32_t temp;
1062 
1063 		DPRINTFN(5, "end of reset\n");
1064 
1065 		/* set correct state */
1066 		sc->sc_flags.status_bus_reset = 1;
1067 		sc->sc_flags.status_suspend = 0;
1068 		sc->sc_flags.change_suspend = 0;
1069 		sc->sc_flags.change_connect = 1;
1070 
1071 		/* reset FIFOs */
1072 		dwc_otg_init_fifo(sc);
1073 
1074 		/* reset function address */
1075 		dwc_otg_set_address(sc, 0);
1076 
1077 		/* reset active endpoints */
1078 		sc->sc_active_out_ep = 1;
1079 
1080 		/* figure out enumeration speed */
1081 		temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DSTS);
1082 		if (DWC_OTG_MSK_DSTS_GET_ENUM_SPEED(temp) ==
1083 		    DWC_OTG_MSK_DSTS_ENUM_SPEED_HI)
1084 			sc->sc_flags.status_high_speed = 1;
1085 		else
1086 			sc->sc_flags.status_high_speed = 0;
1087 
1088 		/* disable resume interrupt and enable suspend interrupt */
1089 
1090 		sc->sc_irq_mask &= ~DWC_OTG_MSK_GINT_WKUPINT;
1091 		sc->sc_irq_mask |= DWC_OTG_MSK_GINT_USB_SUSPEND;
1092 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
1093 
1094 		/* complete root HUB interrupt endpoint */
1095 		dwc_otg_root_intr(sc);
1096 	}
1097 	/*
1098 	 * If resume and suspend is set at the same time we interpret
1099 	 * that like RESUME. Resume is set when there is at least 3
1100 	 * milliseconds of inactivity on the USB BUS.
1101 	 */
1102 	if (status & DWC_OTG_MSK_GINT_WKUPINT) {
1103 
1104 		DPRINTFN(5, "resume interrupt\n");
1105 
1106 		dwc_otg_resume_irq(sc);
1107 
1108 	} else if (status & DWC_OTG_MSK_GINT_USB_SUSPEND) {
1109 
1110 		DPRINTFN(5, "suspend interrupt\n");
1111 
1112 		if (!sc->sc_flags.status_suspend) {
1113 			/* update status bits */
1114 			sc->sc_flags.status_suspend = 1;
1115 			sc->sc_flags.change_suspend = 1;
1116 
1117 			/*
1118 			 * Disable suspend interrupt and enable resume
1119 			 * interrupt:
1120 			 */
1121 			sc->sc_irq_mask &= ~DWC_OTG_MSK_GINT_USB_SUSPEND;
1122 			sc->sc_irq_mask |= DWC_OTG_MSK_GINT_WKUPINT;
1123 			DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
1124 
1125 			/* complete root HUB interrupt endpoint */
1126 			dwc_otg_root_intr(sc);
1127 		}
1128 	}
1129 	/* check VBUS */
1130 	if (status & (DWC_OTG_MSK_GINT_USB_SUSPEND |
1131 	    DWC_OTG_MSK_GINT_USB_RESET |
1132 	    DWC_OTG_MSK_GINT_SESSREQINT)) {
1133 		uint32_t temp;
1134 
1135 		temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GOTGCTL);
1136 
1137 		DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
1138 
1139 		dwc_otg_vbus_interrupt(sc,
1140 		    (temp & DWC_OTG_MSK_GOTGCTL_BSESS_VALID) ? 1 : 0);
1141 	}
1142 
1143 	/* clear all IN endpoint interrupts */
1144 	if (status & DWC_OTG_MSK_GINT_INEP) {
1145 		uint32_t temp;
1146 		uint8_t x;
1147 
1148 		for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
1149 			temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPINT(x));
1150 			if (temp & DWC_OTG_MSK_DIEP_XFER_COMPLETE) {
1151 				DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPINT(x),
1152 				    DWC_OTG_MSK_DIEP_XFER_COMPLETE);
1153 			}
1154 		}
1155 	}
1156 
1157 #if 0
1158 	/* check if we should poll the FIFOs */
1159 	if (status & (DWC_OTG_MSK_GINT_RXFLVL | DWC_OTG_MSK_GINT_INEP))
1160 #endif
1161 		/* poll FIFO(s) */
1162 		dwc_otg_interrupt_poll(sc);
1163 
1164 	USB_BUS_UNLOCK(&sc->sc_bus);
1165 }
1166 
1167 static void
1168 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
1169 {
1170 	struct dwc_otg_td *td;
1171 
1172 	/* get current Transfer Descriptor */
1173 	td = temp->td_next;
1174 	temp->td = td;
1175 
1176 	/* prepare for next TD */
1177 	temp->td_next = td->obj_next;
1178 
1179 	/* fill out the Transfer Descriptor */
1180 	td->func = temp->func;
1181 	td->pc = temp->pc;
1182 	td->offset = temp->offset;
1183 	td->remainder = temp->len;
1184 	td->tx_bytes = 0;
1185 	td->error = 0;
1186 	td->npkt = 1;
1187 	td->did_stall = temp->did_stall;
1188 	td->short_pkt = temp->short_pkt;
1189 	td->alt_next = temp->setup_alt_next;
1190 }
1191 
1192 static void
1193 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
1194 {
1195 	struct dwc_otg_std_temp temp;
1196 	struct dwc_otg_td *td;
1197 	uint32_t x;
1198 	uint8_t need_sync;
1199 
1200 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1201 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
1202 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1203 
1204 	temp.max_frame_size = xfer->max_frame_size;
1205 
1206 	td = xfer->td_start[0];
1207 	xfer->td_transfer_first = td;
1208 	xfer->td_transfer_cache = td;
1209 
1210 	/* setup temp */
1211 
1212 	temp.pc = NULL;
1213 	temp.td = NULL;
1214 	temp.td_next = xfer->td_start[0];
1215 	temp.offset = 0;
1216 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1217 	temp.did_stall = !xfer->flags_int.control_stall;
1218 
1219 	/* check if we should prepend a setup message */
1220 
1221 	if (xfer->flags_int.control_xfr) {
1222 		if (xfer->flags_int.control_hdr) {
1223 
1224 			temp.func = &dwc_otg_setup_rx;
1225 			temp.len = xfer->frlengths[0];
1226 			temp.pc = xfer->frbuffers + 0;
1227 			temp.short_pkt = temp.len ? 1 : 0;
1228 
1229 			/* check for last frame */
1230 			if (xfer->nframes == 1) {
1231 				/* no STATUS stage yet, SETUP is last */
1232 				if (xfer->flags_int.control_act)
1233 					temp.setup_alt_next = 0;
1234 			}
1235 
1236 			dwc_otg_setup_standard_chain_sub(&temp);
1237 		}
1238 		x = 1;
1239 	} else {
1240 		x = 0;
1241 	}
1242 
1243 	if (x != xfer->nframes) {
1244 		if (xfer->endpointno & UE_DIR_IN) {
1245 			temp.func = &dwc_otg_data_tx;
1246 			need_sync = 1;
1247 		} else {
1248 			temp.func = &dwc_otg_data_rx;
1249 			need_sync = 0;
1250 		}
1251 
1252 		/* setup "pc" pointer */
1253 		temp.pc = xfer->frbuffers + x;
1254 	} else {
1255 		need_sync = 0;
1256 	}
1257 	while (x != xfer->nframes) {
1258 
1259 		/* DATA0 / DATA1 message */
1260 
1261 		temp.len = xfer->frlengths[x];
1262 
1263 		x++;
1264 
1265 		if (x == xfer->nframes) {
1266 			if (xfer->flags_int.control_xfr) {
1267 				if (xfer->flags_int.control_act) {
1268 					temp.setup_alt_next = 0;
1269 				}
1270 			} else {
1271 				temp.setup_alt_next = 0;
1272 			}
1273 		}
1274 		if (temp.len == 0) {
1275 
1276 			/* make sure that we send an USB packet */
1277 
1278 			temp.short_pkt = 0;
1279 
1280 		} else {
1281 
1282 			/* regular data transfer */
1283 
1284 			temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
1285 		}
1286 
1287 		dwc_otg_setup_standard_chain_sub(&temp);
1288 
1289 		if (xfer->flags_int.isochronous_xfr) {
1290 			temp.offset += temp.len;
1291 		} else {
1292 			/* get next Page Cache pointer */
1293 			temp.pc = xfer->frbuffers + x;
1294 		}
1295 	}
1296 
1297 	if (xfer->flags_int.control_xfr) {
1298 
1299 		/* always setup a valid "pc" pointer for status and sync */
1300 		temp.pc = xfer->frbuffers + 0;
1301 		temp.len = 0;
1302 		temp.short_pkt = 0;
1303 		temp.setup_alt_next = 0;
1304 
1305 		/* check if we need to sync */
1306 		if (need_sync) {
1307 			/* we need a SYNC point after TX */
1308 			temp.func = &dwc_otg_data_tx_sync;
1309 			dwc_otg_setup_standard_chain_sub(&temp);
1310 		}
1311 
1312 		/* check if we should append a status stage */
1313 		if (!xfer->flags_int.control_act) {
1314 
1315 			/*
1316 			 * Send a DATA1 message and invert the current
1317 			 * endpoint direction.
1318 			 */
1319 			if (xfer->endpointno & UE_DIR_IN) {
1320 				temp.func = &dwc_otg_data_rx;
1321 				need_sync = 0;
1322 			} else {
1323 				temp.func = &dwc_otg_data_tx;
1324 				need_sync = 1;
1325 			}
1326 
1327 			dwc_otg_setup_standard_chain_sub(&temp);
1328 			if (need_sync) {
1329 				/* we need a SYNC point after TX */
1330 				temp.func = &dwc_otg_data_tx_sync;
1331 				dwc_otg_setup_standard_chain_sub(&temp);
1332 			}
1333 		}
1334 	} else {
1335 		/* check if we need to sync */
1336 		if (need_sync) {
1337 
1338 			temp.pc = xfer->frbuffers + 0;
1339 			temp.len = 0;
1340 			temp.short_pkt = 0;
1341 			temp.setup_alt_next = 0;
1342 
1343 			/* we need a SYNC point after TX */
1344 			temp.func = &dwc_otg_data_tx_sync;
1345 			dwc_otg_setup_standard_chain_sub(&temp);
1346 		}
1347 	}
1348 
1349 	/* must have at least one frame! */
1350 	td = temp.td;
1351 	xfer->td_transfer_last = td;
1352 }
1353 
1354 static void
1355 dwc_otg_timeout(void *arg)
1356 {
1357 	struct usb_xfer *xfer = arg;
1358 
1359 	DPRINTF("xfer=%p\n", xfer);
1360 
1361 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1362 
1363 	/* transfer is transferred */
1364 	dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
1365 }
1366 
1367 static void
1368 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
1369 {
1370 	DPRINTFN(9, "\n");
1371 
1372 	/* poll one time - will turn on interrupts */
1373 	if (dwc_otg_xfer_do_fifo(xfer)) {
1374 
1375 		/* put transfer on interrupt queue */
1376 		usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
1377 
1378 		/* start timeout, if any */
1379 		if (xfer->timeout != 0) {
1380 			usbd_transfer_timeout_ms(xfer,
1381 			    &dwc_otg_timeout, xfer->timeout);
1382 		}
1383 	}
1384 }
1385 
1386 static void
1387 dwc_otg_root_intr(struct dwc_otg_softc *sc)
1388 {
1389 	DPRINTFN(9, "\n");
1390 
1391 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1392 
1393 	/* set port bit */
1394 	sc->sc_hub_idata[0] = 0x02;	/* we only have one port */
1395 
1396 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
1397 	    sizeof(sc->sc_hub_idata));
1398 }
1399 
1400 static usb_error_t
1401 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
1402 {
1403 	struct dwc_otg_td *td;
1404 	uint32_t len;
1405 	uint8_t error;
1406 
1407 	DPRINTFN(9, "\n");
1408 
1409 	td = xfer->td_transfer_cache;
1410 
1411 	do {
1412 		len = td->remainder;
1413 
1414 		if (xfer->aframes != xfer->nframes) {
1415 			/*
1416 			 * Verify the length and subtract
1417 			 * the remainder from "frlengths[]":
1418 			 */
1419 			if (len > xfer->frlengths[xfer->aframes]) {
1420 				td->error = 1;
1421 			} else {
1422 				xfer->frlengths[xfer->aframes] -= len;
1423 			}
1424 		}
1425 		/* Check for transfer error */
1426 		if (td->error) {
1427 			/* the transfer is finished */
1428 			error = 1;
1429 			td = NULL;
1430 			break;
1431 		}
1432 		/* Check for short transfer */
1433 		if (len > 0) {
1434 			if (xfer->flags_int.short_frames_ok) {
1435 				/* follow alt next */
1436 				if (td->alt_next) {
1437 					td = td->obj_next;
1438 				} else {
1439 					td = NULL;
1440 				}
1441 			} else {
1442 				/* the transfer is finished */
1443 				td = NULL;
1444 			}
1445 			error = 0;
1446 			break;
1447 		}
1448 		td = td->obj_next;
1449 
1450 		/* this USB frame is complete */
1451 		error = 0;
1452 		break;
1453 
1454 	} while (0);
1455 
1456 	/* update transfer cache */
1457 
1458 	xfer->td_transfer_cache = td;
1459 
1460 	return (error ?
1461 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1462 }
1463 
1464 static void
1465 dwc_otg_standard_done(struct usb_xfer *xfer)
1466 {
1467 	usb_error_t err = 0;
1468 
1469 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1470 	    xfer, xfer->endpoint);
1471 
1472 	/* reset scanner */
1473 
1474 	xfer->td_transfer_cache = xfer->td_transfer_first;
1475 
1476 	if (xfer->flags_int.control_xfr) {
1477 
1478 		if (xfer->flags_int.control_hdr) {
1479 
1480 			err = dwc_otg_standard_done_sub(xfer);
1481 		}
1482 		xfer->aframes = 1;
1483 
1484 		if (xfer->td_transfer_cache == NULL) {
1485 			goto done;
1486 		}
1487 	}
1488 	while (xfer->aframes != xfer->nframes) {
1489 
1490 		err = dwc_otg_standard_done_sub(xfer);
1491 		xfer->aframes++;
1492 
1493 		if (xfer->td_transfer_cache == NULL) {
1494 			goto done;
1495 		}
1496 	}
1497 
1498 	if (xfer->flags_int.control_xfr &&
1499 	    !xfer->flags_int.control_act) {
1500 
1501 		err = dwc_otg_standard_done_sub(xfer);
1502 	}
1503 done:
1504 	dwc_otg_device_done(xfer, err);
1505 }
1506 
1507 /*------------------------------------------------------------------------*
1508  *	dwc_otg_device_done
1509  *
1510  * NOTE: this function can be called more than one time on the
1511  * same USB transfer!
1512  *------------------------------------------------------------------------*/
1513 static void
1514 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
1515 {
1516 	DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
1517 	    xfer, xfer->endpoint, error);
1518 
1519 	if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1520 		DPRINTFN(15, "disabled interrupts!\n");
1521 	}
1522 	/* dequeue transfer and start next transfer */
1523 	usbd_transfer_done(xfer, error);
1524 }
1525 
1526 static void
1527 dwc_otg_set_stall(struct usb_device *udev, struct usb_xfer *xfer,
1528     struct usb_endpoint *ep, uint8_t *did_stall)
1529 {
1530 	struct dwc_otg_softc *sc;
1531 	uint32_t temp;
1532 	uint32_t reg;
1533 	uint8_t ep_no;
1534 
1535 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1536 
1537 	if (xfer) {
1538 		/* cancel any ongoing transfers */
1539 		dwc_otg_device_done(xfer, USB_ERR_STALLED);
1540 	}
1541 	sc = DWC_OTG_BUS2SC(udev->bus);
1542 
1543 	/* get endpoint address */
1544 	ep_no = ep->edesc->bEndpointAddress;
1545 
1546 	DPRINTFN(5, "endpoint=0x%x\n", ep_no);
1547 
1548 	if (ep_no & UE_DIR_IN) {
1549 		reg = DWC_OTG_REG_DIEPCTL(ep_no & UE_ADDR);
1550 		temp = sc->sc_in_ctl[ep_no & UE_ADDR];
1551 	} else {
1552 		reg = DWC_OTG_REG_DOEPCTL(ep_no & UE_ADDR);
1553 		temp = sc->sc_out_ctl[ep_no & UE_ADDR];
1554 	}
1555 
1556 	/* disable and stall endpoint */
1557 	DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DOEPCTL_DISABLE);
1558 	DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DOEPCTL_STALL);
1559 
1560 	/* clear active OUT ep */
1561 	if (!(ep_no & UE_DIR_IN)) {
1562 
1563 		sc->sc_active_out_ep &= ~(1U << (ep_no & UE_ADDR));
1564 
1565 		if (sc->sc_last_rx_status != 0 &&
1566 		    (ep_no & UE_ADDR) == DWC_OTG_MSK_GRXSTS_GET_CHANNEL(
1567 		    sc->sc_last_rx_status)) {
1568 			/* dump data */
1569 			dwc_otg_common_rx_ack(sc);
1570 			/* poll interrupt */
1571 			dwc_otg_interrupt_poll(sc);
1572 		}
1573 	}
1574 }
1575 
1576 static void
1577 dwc_otg_clear_stall_sub(struct dwc_otg_softc *sc, uint32_t mps,
1578     uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
1579 {
1580 	uint32_t reg;
1581 	uint32_t temp;
1582 
1583 	if (ep_type == UE_CONTROL) {
1584 		/* clearing stall is not needed */
1585 		return;
1586 	}
1587 
1588 	if (ep_dir) {
1589 		reg = DWC_OTG_REG_DIEPCTL(ep_no);
1590 	} else {
1591 		reg = DWC_OTG_REG_DOEPCTL(ep_no);
1592 		sc->sc_active_out_ep |= (1U << ep_no);
1593 	}
1594 
1595 	/* round up and mask away the multiplier count */
1596 	mps = (mps + 3) & 0x7FC;
1597 
1598 	if (ep_type == UE_BULK) {
1599 		temp = DWC_OTG_MSK_EP_SET_TYPE(
1600 		    DWC_OTG_MSK_EP_TYPE_BULK) |
1601 		    DWC_OTG_MSK_DIEPCTL_USB_AEP;
1602 	} else if (ep_type == UE_INTERRUPT) {
1603 		temp = DWC_OTG_MSK_EP_SET_TYPE(
1604 		    DWC_OTG_MSK_EP_TYPE_INTERRUPT) |
1605 		    DWC_OTG_MSK_DIEPCTL_USB_AEP;
1606 	} else {
1607 		temp = DWC_OTG_MSK_EP_SET_TYPE(
1608 		    DWC_OTG_MSK_EP_TYPE_ISOC) |
1609 		    DWC_OTG_MSK_DIEPCTL_USB_AEP;
1610 	}
1611 
1612 	temp |= DWC_OTG_MSK_DIEPCTL_MPS(mps);
1613 	temp |= DWC_OTG_MSK_DIEPCTL_FNUM(ep_no);
1614 
1615 	if (ep_dir)
1616 		sc->sc_in_ctl[ep_no] = temp;
1617 	else
1618 		sc->sc_out_ctl[ep_no] = temp;
1619 
1620 	DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DOEPCTL_DISABLE);
1621 	DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DOEPCTL_SET_DATA0);
1622 	DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DIEPCTL_SET_NAK);
1623 
1624 	/* we only reset the transmit FIFO */
1625 	if (ep_dir) {
1626 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
1627 		    DWC_OTG_MSK_GRSTCTL_TXFIFO(ep_no) |
1628 		    DWC_OTG_MSK_GRSTCTL_TXFFLUSH);
1629 
1630 		DWC_OTG_WRITE_4(sc,
1631 		    DWC_OTG_REG_DIEPTSIZ(ep_no), 0);
1632 	}
1633 
1634 	/* poll interrupt */
1635 	dwc_otg_interrupt_poll(sc);
1636 }
1637 
1638 static void
1639 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
1640 {
1641 	struct dwc_otg_softc *sc;
1642 	struct usb_endpoint_descriptor *ed;
1643 
1644 	DPRINTFN(5, "endpoint=%p\n", ep);
1645 
1646 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1647 
1648 	/* check mode */
1649 	if (udev->flags.usb_mode != USB_MODE_DEVICE) {
1650 		/* not supported */
1651 		return;
1652 	}
1653 	/* get softc */
1654 	sc = DWC_OTG_BUS2SC(udev->bus);
1655 
1656 	/* get endpoint descriptor */
1657 	ed = ep->edesc;
1658 
1659 	/* reset endpoint */
1660 	dwc_otg_clear_stall_sub(sc,
1661 	    UGETW(ed->wMaxPacketSize),
1662 	    (ed->bEndpointAddress & UE_ADDR),
1663 	    (ed->bmAttributes & UE_XFERTYPE),
1664 	    (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
1665 }
1666 
1667 static void
1668 dwc_otg_device_state_change(struct usb_device *udev)
1669 {
1670 	struct dwc_otg_softc *sc;
1671 	uint8_t x;
1672 
1673 	/* get softc */
1674 	sc = DWC_OTG_BUS2SC(udev->bus);
1675 
1676 	/* deactivate all other endpoint but the control endpoint */
1677 	if (udev->state == USB_STATE_CONFIGURED ||
1678 	    udev->state == USB_STATE_ADDRESSED) {
1679 
1680 		USB_BUS_LOCK(&sc->sc_bus);
1681 
1682 		for (x = 1; x != sc->sc_dev_ep_max; x++) {
1683 
1684 			if (x < sc->sc_dev_in_ep_max) {
1685 				DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(x),
1686 				    DWC_OTG_MSK_DIEPCTL_DISABLE);
1687 				DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(x), 0);
1688 			}
1689 
1690 			DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(x),
1691 			    DWC_OTG_MSK_DOEPCTL_DISABLE);
1692 			DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(x), 0);
1693 		}
1694 		USB_BUS_UNLOCK(&sc->sc_bus);
1695 	}
1696 }
1697 
1698 int
1699 dwc_otg_init(struct dwc_otg_softc *sc)
1700 {
1701 	uint32_t temp;
1702 
1703 	DPRINTF("start\n");
1704 
1705 	/* set up the bus structure */
1706 	sc->sc_bus.usbrev = USB_REV_2_0;
1707 	sc->sc_bus.methods = &dwc_otg_bus_methods;
1708 
1709 	/* reset active endpoints */
1710 	sc->sc_active_out_ep = 1;
1711 
1712 	USB_BUS_LOCK(&sc->sc_bus);
1713 
1714 	/* turn on clocks */
1715 	dwc_otg_clocks_on(sc);
1716 
1717 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GSNPSID);
1718 	DPRINTF("Version = 0x%08x\n", temp);
1719 
1720 	/* disconnect */
1721 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL,
1722 	    DWC_OTG_MSK_DCTL_SOFT_DISC);
1723 
1724 	/* wait for host to detect disconnect */
1725 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
1726 
1727 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
1728 	    DWC_OTG_MSK_GRSTCTL_CSFTRST);
1729 
1730 	/* wait a little bit for block to reset */
1731 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
1732 
1733 	/* select HSIC or non-HSIC mode */
1734 	if (DWC_OTG_USE_HSIC) {
1735 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GUSBCFG,
1736 		    DWC_OTG_MSK_GUSBCFG_PHY_INTF |
1737 		    DWC_OTG_MSK_GUSBCFG_TRD_TIM(5));
1738 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GOTGCTL,
1739 		    0x000000EC);
1740 
1741 		temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GLPMCFG);
1742 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GLPMCFG,
1743 		    temp & ~DWC_OTG_MSK_GLPMCFG_HSIC_CONN);
1744 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GLPMCFG,
1745 		    temp | DWC_OTG_MSK_GLPMCFG_HSIC_CONN);
1746 	} else {
1747 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GUSBCFG,
1748 		    DWC_OTG_MSK_GUSBCFG_ULPI_UMTI_SEL |
1749 		    DWC_OTG_MSK_GUSBCFG_TRD_TIM(5));
1750 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GOTGCTL, 0);
1751 
1752 		temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GLPMCFG);
1753 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GLPMCFG,
1754 		    temp & ~DWC_OTG_MSK_GLPMCFG_HSIC_CONN);
1755 	}
1756 
1757 	/* clear global nak */
1758 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL,
1759 	    DWC_OTG_MSK_DCTL_CGOUT_NAK |
1760 	    DWC_OTG_MSK_DCTL_CGNPIN_NAK);
1761 
1762 	/* enable USB port */
1763 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_PCGCCTL, 0);
1764 
1765 	/* pull up D+ */
1766 	dwc_otg_pull_up(sc);
1767 
1768 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GHWCFG3);
1769 
1770 	sc->sc_fifo_size = 4 * DWC_OTG_MSK_GHWCFG3_GET_DFIFO(temp);
1771 
1772 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GHWCFG2);
1773 
1774 	sc->sc_dev_ep_max = DWC_OTG_MSK_GHWCFG2_NUM_DEV_EP(temp);
1775 
1776 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GHWCFG4);
1777 
1778 	sc->sc_dev_in_ep_max = DWC_OTG_MSK_GHWCFG4_NUM_IN_EPS(temp);
1779 
1780 	DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d\n",
1781 	    sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max);
1782 
1783 	/* setup FIFO */
1784 	if (dwc_otg_init_fifo(sc))
1785 		return (EINVAL);
1786 
1787 	/* enable interrupts */
1788 	sc->sc_irq_mask = DWC_OTG_MSK_GINT_ENABLED;
1789 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
1790 
1791 	/* enable all endpoint interrupts */
1792 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GHWCFG2);
1793 	if (temp & DWC_OTG_MSK_GHWCFG2_MPI) {
1794 		uint8_t x;
1795 
1796 		DPRINTF("Multi Process Interrupts\n");
1797 
1798 		for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
1799 			DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPEACHMSK(x),
1800 			    DWC_OTG_MSK_DIEP_XFER_COMPLETE);
1801 			DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPEACHMSK(x), 0);
1802 		}
1803 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DEACHINTMSK, 0xFFFF);
1804 	} else {
1805 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPMSK,
1806 		    DWC_OTG_MSK_DIEP_XFER_COMPLETE);
1807 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPMSK, 0);
1808 		DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DAINTMSK, 0xFFFF);
1809 	}
1810 
1811 	/* enable global IRQ */
1812 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GAHBCFG,
1813 	    DWC_OTG_MSK_GAHBCFG_GLOBAL_IRQ);
1814 
1815 	/* turn off clocks */
1816 	dwc_otg_clocks_off(sc);
1817 
1818 	/* read initial VBUS state */
1819 
1820 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GOTGCTL);
1821 
1822 	DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
1823 
1824 	dwc_otg_vbus_interrupt(sc,
1825 	    (temp & DWC_OTG_MSK_GOTGCTL_BSESS_VALID) ? 1 : 0);
1826 
1827 	USB_BUS_UNLOCK(&sc->sc_bus);
1828 
1829 	/* catch any lost interrupts */
1830 
1831 	dwc_otg_do_poll(&sc->sc_bus);
1832 
1833 	return (0);			/* success */
1834 }
1835 
1836 void
1837 dwc_otg_uninit(struct dwc_otg_softc *sc)
1838 {
1839 	USB_BUS_LOCK(&sc->sc_bus);
1840 
1841 	/* set disconnect */
1842 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL,
1843 	    DWC_OTG_MSK_DCTL_SOFT_DISC);
1844 
1845 	/* turn off global IRQ */
1846 	DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GAHBCFG, 0);
1847 
1848 	sc->sc_flags.port_powered = 0;
1849 	sc->sc_flags.status_vbus = 0;
1850 	sc->sc_flags.status_bus_reset = 0;
1851 	sc->sc_flags.status_suspend = 0;
1852 	sc->sc_flags.change_suspend = 0;
1853 	sc->sc_flags.change_connect = 1;
1854 
1855 	dwc_otg_pull_down(sc);
1856 	dwc_otg_clocks_off(sc);
1857 
1858 	USB_BUS_UNLOCK(&sc->sc_bus);
1859 }
1860 
1861 static void
1862 dwc_otg_suspend(struct dwc_otg_softc *sc)
1863 {
1864 	return;
1865 }
1866 
1867 static void
1868 dwc_otg_resume(struct dwc_otg_softc *sc)
1869 {
1870 	return;
1871 }
1872 
1873 static void
1874 dwc_otg_do_poll(struct usb_bus *bus)
1875 {
1876 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
1877 
1878 	USB_BUS_LOCK(&sc->sc_bus);
1879 	dwc_otg_interrupt_poll(sc);
1880 	USB_BUS_UNLOCK(&sc->sc_bus);
1881 }
1882 
1883 /*------------------------------------------------------------------------*
1884  * at91dci bulk support
1885  * at91dci control support
1886  * at91dci interrupt support
1887  *------------------------------------------------------------------------*/
1888 static void
1889 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
1890 {
1891 	return;
1892 }
1893 
1894 static void
1895 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
1896 {
1897 	dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
1898 }
1899 
1900 static void
1901 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
1902 {
1903 	return;
1904 }
1905 
1906 static void
1907 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
1908 {
1909 	/* setup TDs */
1910 	dwc_otg_setup_standard_chain(xfer);
1911 	dwc_otg_start_standard_chain(xfer);
1912 }
1913 
1914 struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
1915 {
1916 	.open = dwc_otg_device_non_isoc_open,
1917 	.close = dwc_otg_device_non_isoc_close,
1918 	.enter = dwc_otg_device_non_isoc_enter,
1919 	.start = dwc_otg_device_non_isoc_start,
1920 };
1921 
1922 /*------------------------------------------------------------------------*
1923  * at91dci full speed isochronous support
1924  *------------------------------------------------------------------------*/
1925 static void
1926 dwc_otg_device_isoc_fs_open(struct usb_xfer *xfer)
1927 {
1928 	return;
1929 }
1930 
1931 static void
1932 dwc_otg_device_isoc_fs_close(struct usb_xfer *xfer)
1933 {
1934 	dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
1935 }
1936 
1937 static void
1938 dwc_otg_device_isoc_fs_enter(struct usb_xfer *xfer)
1939 {
1940 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
1941 	uint32_t temp;
1942 	uint32_t nframes;
1943 
1944 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1945 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
1946 
1947 	temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DSTS);
1948 
1949 	/* get the current frame index */
1950 
1951 	nframes = DWC_OTG_MSK_DSTS_GET_FNUM(temp);
1952 
1953 	if (sc->sc_flags.status_high_speed)
1954 		nframes /= 8;
1955 
1956 	nframes &= DWC_OTG_FRAME_MASK;
1957 
1958 	/*
1959 	 * check if the frame index is within the window where the frames
1960 	 * will be inserted
1961 	 */
1962 	temp = (nframes - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
1963 
1964 	if ((xfer->endpoint->is_synced == 0) ||
1965 	    (temp < xfer->nframes)) {
1966 		/*
1967 		 * If there is data underflow or the pipe queue is
1968 		 * empty we schedule the transfer a few frames ahead
1969 		 * of the current frame position. Else two isochronous
1970 		 * transfers might overlap.
1971 		 */
1972 		xfer->endpoint->isoc_next = (nframes + 3) & DWC_OTG_FRAME_MASK;
1973 		xfer->endpoint->is_synced = 1;
1974 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1975 	}
1976 	/*
1977 	 * compute how many milliseconds the insertion is ahead of the
1978 	 * current frame position:
1979 	 */
1980 	temp = (xfer->endpoint->isoc_next - nframes) & DWC_OTG_FRAME_MASK;
1981 
1982 	/*
1983 	 * pre-compute when the isochronous transfer will be finished:
1984 	 */
1985 	xfer->isoc_time_complete =
1986 	    usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
1987 	    xfer->nframes;
1988 
1989 	/* compute frame number for next insertion */
1990 	xfer->endpoint->isoc_next += xfer->nframes;
1991 
1992 	/* setup TDs */
1993 	dwc_otg_setup_standard_chain(xfer);
1994 }
1995 
1996 static void
1997 dwc_otg_device_isoc_fs_start(struct usb_xfer *xfer)
1998 {
1999 	/* start TD chain */
2000 	dwc_otg_start_standard_chain(xfer);
2001 }
2002 
2003 struct usb_pipe_methods dwc_otg_device_isoc_fs_methods =
2004 {
2005 	.open = dwc_otg_device_isoc_fs_open,
2006 	.close = dwc_otg_device_isoc_fs_close,
2007 	.enter = dwc_otg_device_isoc_fs_enter,
2008 	.start = dwc_otg_device_isoc_fs_start,
2009 };
2010 
2011 /*------------------------------------------------------------------------*
2012  * at91dci root control support
2013  *------------------------------------------------------------------------*
2014  * Simulate a hardware HUB by handling all the necessary requests.
2015  *------------------------------------------------------------------------*/
2016 
2017 static const struct usb_device_descriptor dwc_otg_devd = {
2018 	.bLength = sizeof(struct usb_device_descriptor),
2019 	.bDescriptorType = UDESC_DEVICE,
2020 	.bcdUSB = {0x00, 0x02},
2021 	.bDeviceClass = UDCLASS_HUB,
2022 	.bDeviceSubClass = UDSUBCLASS_HUB,
2023 	.bDeviceProtocol = UDPROTO_HSHUBSTT,
2024 	.bMaxPacketSize = 64,
2025 	.bcdDevice = {0x00, 0x01},
2026 	.iManufacturer = 1,
2027 	.iProduct = 2,
2028 	.bNumConfigurations = 1,
2029 };
2030 
2031 static const struct dwc_otg_config_desc dwc_otg_confd = {
2032 	.confd = {
2033 		.bLength = sizeof(struct usb_config_descriptor),
2034 		.bDescriptorType = UDESC_CONFIG,
2035 		.wTotalLength[0] = sizeof(dwc_otg_confd),
2036 		.bNumInterface = 1,
2037 		.bConfigurationValue = 1,
2038 		.iConfiguration = 0,
2039 		.bmAttributes = UC_SELF_POWERED,
2040 		.bMaxPower = 0,
2041 	},
2042 	.ifcd = {
2043 		.bLength = sizeof(struct usb_interface_descriptor),
2044 		.bDescriptorType = UDESC_INTERFACE,
2045 		.bNumEndpoints = 1,
2046 		.bInterfaceClass = UICLASS_HUB,
2047 		.bInterfaceSubClass = UISUBCLASS_HUB,
2048 		.bInterfaceProtocol = 0,
2049 	},
2050 	.endpd = {
2051 		.bLength = sizeof(struct usb_endpoint_descriptor),
2052 		.bDescriptorType = UDESC_ENDPOINT,
2053 		.bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
2054 		.bmAttributes = UE_INTERRUPT,
2055 		.wMaxPacketSize[0] = 8,
2056 		.bInterval = 255,
2057 	},
2058 };
2059 
2060 #define	HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
2061 
2062 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
2063 	.bDescLength = sizeof(dwc_otg_hubd),
2064 	.bDescriptorType = UDESC_HUB,
2065 	.bNbrPorts = 1,
2066 	HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
2067 	.bPwrOn2PwrGood = 50,
2068 	.bHubContrCurrent = 0,
2069 	.DeviceRemovable = {0},		/* port is removable */
2070 };
2071 
2072 #define	STRING_LANG \
2073   0x09, 0x04,				/* American English */
2074 
2075 #define	STRING_VENDOR \
2076   'D', 0, 'W', 0, 'C', 0, 'O', 0, 'T', 0, 'G', 0
2077 
2078 #define	STRING_PRODUCT \
2079   'D', 0, 'C', 0, 'I', 0, ' ', 0, 'R', 0, \
2080   'o', 0, 'o', 0, 't', 0, ' ', 0, 'H', 0, \
2081   'U', 0, 'B', 0,
2082 
2083 USB_MAKE_STRING_DESC(STRING_LANG, dwc_otg_langtab);
2084 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
2085 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
2086 
2087 static usb_error_t
2088 dwc_otg_roothub_exec(struct usb_device *udev,
2089     struct usb_device_request *req, const void **pptr, uint16_t *plength)
2090 {
2091 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
2092 	const void *ptr;
2093 	uint16_t len;
2094 	uint16_t value;
2095 	uint16_t index;
2096 	usb_error_t err;
2097 
2098 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2099 
2100 	/* buffer reset */
2101 	ptr = (const void *)&sc->sc_hub_temp;
2102 	len = 0;
2103 	err = 0;
2104 
2105 	value = UGETW(req->wValue);
2106 	index = UGETW(req->wIndex);
2107 
2108 	/* demultiplex the control request */
2109 
2110 	switch (req->bmRequestType) {
2111 	case UT_READ_DEVICE:
2112 		switch (req->bRequest) {
2113 		case UR_GET_DESCRIPTOR:
2114 			goto tr_handle_get_descriptor;
2115 		case UR_GET_CONFIG:
2116 			goto tr_handle_get_config;
2117 		case UR_GET_STATUS:
2118 			goto tr_handle_get_status;
2119 		default:
2120 			goto tr_stalled;
2121 		}
2122 		break;
2123 
2124 	case UT_WRITE_DEVICE:
2125 		switch (req->bRequest) {
2126 		case UR_SET_ADDRESS:
2127 			goto tr_handle_set_address;
2128 		case UR_SET_CONFIG:
2129 			goto tr_handle_set_config;
2130 		case UR_CLEAR_FEATURE:
2131 			goto tr_valid;	/* nop */
2132 		case UR_SET_DESCRIPTOR:
2133 			goto tr_valid;	/* nop */
2134 		case UR_SET_FEATURE:
2135 		default:
2136 			goto tr_stalled;
2137 		}
2138 		break;
2139 
2140 	case UT_WRITE_ENDPOINT:
2141 		switch (req->bRequest) {
2142 		case UR_CLEAR_FEATURE:
2143 			switch (UGETW(req->wValue)) {
2144 			case UF_ENDPOINT_HALT:
2145 				goto tr_handle_clear_halt;
2146 			case UF_DEVICE_REMOTE_WAKEUP:
2147 				goto tr_handle_clear_wakeup;
2148 			default:
2149 				goto tr_stalled;
2150 			}
2151 			break;
2152 		case UR_SET_FEATURE:
2153 			switch (UGETW(req->wValue)) {
2154 			case UF_ENDPOINT_HALT:
2155 				goto tr_handle_set_halt;
2156 			case UF_DEVICE_REMOTE_WAKEUP:
2157 				goto tr_handle_set_wakeup;
2158 			default:
2159 				goto tr_stalled;
2160 			}
2161 			break;
2162 		case UR_SYNCH_FRAME:
2163 			goto tr_valid;	/* nop */
2164 		default:
2165 			goto tr_stalled;
2166 		}
2167 		break;
2168 
2169 	case UT_READ_ENDPOINT:
2170 		switch (req->bRequest) {
2171 		case UR_GET_STATUS:
2172 			goto tr_handle_get_ep_status;
2173 		default:
2174 			goto tr_stalled;
2175 		}
2176 		break;
2177 
2178 	case UT_WRITE_INTERFACE:
2179 		switch (req->bRequest) {
2180 		case UR_SET_INTERFACE:
2181 			goto tr_handle_set_interface;
2182 		case UR_CLEAR_FEATURE:
2183 			goto tr_valid;	/* nop */
2184 		case UR_SET_FEATURE:
2185 		default:
2186 			goto tr_stalled;
2187 		}
2188 		break;
2189 
2190 	case UT_READ_INTERFACE:
2191 		switch (req->bRequest) {
2192 		case UR_GET_INTERFACE:
2193 			goto tr_handle_get_interface;
2194 		case UR_GET_STATUS:
2195 			goto tr_handle_get_iface_status;
2196 		default:
2197 			goto tr_stalled;
2198 		}
2199 		break;
2200 
2201 	case UT_WRITE_CLASS_INTERFACE:
2202 	case UT_WRITE_VENDOR_INTERFACE:
2203 		/* XXX forward */
2204 		break;
2205 
2206 	case UT_READ_CLASS_INTERFACE:
2207 	case UT_READ_VENDOR_INTERFACE:
2208 		/* XXX forward */
2209 		break;
2210 
2211 	case UT_WRITE_CLASS_DEVICE:
2212 		switch (req->bRequest) {
2213 		case UR_CLEAR_FEATURE:
2214 			goto tr_valid;
2215 		case UR_SET_DESCRIPTOR:
2216 		case UR_SET_FEATURE:
2217 			break;
2218 		default:
2219 			goto tr_stalled;
2220 		}
2221 		break;
2222 
2223 	case UT_WRITE_CLASS_OTHER:
2224 		switch (req->bRequest) {
2225 		case UR_CLEAR_FEATURE:
2226 			goto tr_handle_clear_port_feature;
2227 		case UR_SET_FEATURE:
2228 			goto tr_handle_set_port_feature;
2229 		case UR_CLEAR_TT_BUFFER:
2230 		case UR_RESET_TT:
2231 		case UR_STOP_TT:
2232 			goto tr_valid;
2233 
2234 		default:
2235 			goto tr_stalled;
2236 		}
2237 		break;
2238 
2239 	case UT_READ_CLASS_OTHER:
2240 		switch (req->bRequest) {
2241 		case UR_GET_TT_STATE:
2242 			goto tr_handle_get_tt_state;
2243 		case UR_GET_STATUS:
2244 			goto tr_handle_get_port_status;
2245 		default:
2246 			goto tr_stalled;
2247 		}
2248 		break;
2249 
2250 	case UT_READ_CLASS_DEVICE:
2251 		switch (req->bRequest) {
2252 		case UR_GET_DESCRIPTOR:
2253 			goto tr_handle_get_class_descriptor;
2254 		case UR_GET_STATUS:
2255 			goto tr_handle_get_class_status;
2256 
2257 		default:
2258 			goto tr_stalled;
2259 		}
2260 		break;
2261 	default:
2262 		goto tr_stalled;
2263 	}
2264 	goto tr_valid;
2265 
2266 tr_handle_get_descriptor:
2267 	switch (value >> 8) {
2268 	case UDESC_DEVICE:
2269 		if (value & 0xff) {
2270 			goto tr_stalled;
2271 		}
2272 		len = sizeof(dwc_otg_devd);
2273 		ptr = (const void *)&dwc_otg_devd;
2274 		goto tr_valid;
2275 	case UDESC_CONFIG:
2276 		if (value & 0xff) {
2277 			goto tr_stalled;
2278 		}
2279 		len = sizeof(dwc_otg_confd);
2280 		ptr = (const void *)&dwc_otg_confd;
2281 		goto tr_valid;
2282 	case UDESC_STRING:
2283 		switch (value & 0xff) {
2284 		case 0:		/* Language table */
2285 			len = sizeof(dwc_otg_langtab);
2286 			ptr = (const void *)&dwc_otg_langtab;
2287 			goto tr_valid;
2288 
2289 		case 1:		/* Vendor */
2290 			len = sizeof(dwc_otg_vendor);
2291 			ptr = (const void *)&dwc_otg_vendor;
2292 			goto tr_valid;
2293 
2294 		case 2:		/* Product */
2295 			len = sizeof(dwc_otg_product);
2296 			ptr = (const void *)&dwc_otg_product;
2297 			goto tr_valid;
2298 		default:
2299 			break;
2300 		}
2301 		break;
2302 	default:
2303 		goto tr_stalled;
2304 	}
2305 	goto tr_stalled;
2306 
2307 tr_handle_get_config:
2308 	len = 1;
2309 	sc->sc_hub_temp.wValue[0] = sc->sc_conf;
2310 	goto tr_valid;
2311 
2312 tr_handle_get_status:
2313 	len = 2;
2314 	USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
2315 	goto tr_valid;
2316 
2317 tr_handle_set_address:
2318 	if (value & 0xFF00) {
2319 		goto tr_stalled;
2320 	}
2321 	sc->sc_rt_addr = value;
2322 	goto tr_valid;
2323 
2324 tr_handle_set_config:
2325 	if (value >= 2) {
2326 		goto tr_stalled;
2327 	}
2328 	sc->sc_conf = value;
2329 	goto tr_valid;
2330 
2331 tr_handle_get_interface:
2332 	len = 1;
2333 	sc->sc_hub_temp.wValue[0] = 0;
2334 	goto tr_valid;
2335 
2336 tr_handle_get_tt_state:
2337 tr_handle_get_class_status:
2338 tr_handle_get_iface_status:
2339 tr_handle_get_ep_status:
2340 	len = 2;
2341 	USETW(sc->sc_hub_temp.wValue, 0);
2342 	goto tr_valid;
2343 
2344 tr_handle_set_halt:
2345 tr_handle_set_interface:
2346 tr_handle_set_wakeup:
2347 tr_handle_clear_wakeup:
2348 tr_handle_clear_halt:
2349 	goto tr_valid;
2350 
2351 tr_handle_clear_port_feature:
2352 	if (index != 1) {
2353 		goto tr_stalled;
2354 	}
2355 	DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
2356 
2357 	switch (value) {
2358 	case UHF_PORT_SUSPEND:
2359 		dwc_otg_wakeup_peer(sc);
2360 		break;
2361 
2362 	case UHF_PORT_ENABLE:
2363 		sc->sc_flags.port_enabled = 0;
2364 		break;
2365 
2366 	case UHF_PORT_TEST:
2367 	case UHF_PORT_INDICATOR:
2368 	case UHF_C_PORT_ENABLE:
2369 	case UHF_C_PORT_OVER_CURRENT:
2370 	case UHF_C_PORT_RESET:
2371 		/* nops */
2372 		break;
2373 	case UHF_PORT_POWER:
2374 		sc->sc_flags.port_powered = 0;
2375 		dwc_otg_pull_down(sc);
2376 		dwc_otg_clocks_off(sc);
2377 		break;
2378 	case UHF_C_PORT_CONNECTION:
2379 		/* clear connect change flag */
2380 		sc->sc_flags.change_connect = 0;
2381 
2382 		if (!sc->sc_flags.status_bus_reset) {
2383 			/* we are not connected */
2384 			break;
2385 		}
2386 		break;
2387 	case UHF_C_PORT_SUSPEND:
2388 		sc->sc_flags.change_suspend = 0;
2389 		break;
2390 	default:
2391 		err = USB_ERR_IOERROR;
2392 		goto done;
2393 	}
2394 	goto tr_valid;
2395 
2396 tr_handle_set_port_feature:
2397 	if (index != 1) {
2398 		goto tr_stalled;
2399 	}
2400 	DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
2401 
2402 	switch (value) {
2403 	case UHF_PORT_ENABLE:
2404 		sc->sc_flags.port_enabled = 1;
2405 		break;
2406 	case UHF_PORT_SUSPEND:
2407 	case UHF_PORT_RESET:
2408 	case UHF_PORT_TEST:
2409 	case UHF_PORT_INDICATOR:
2410 		/* nops */
2411 		break;
2412 	case UHF_PORT_POWER:
2413 		sc->sc_flags.port_powered = 1;
2414 		break;
2415 	default:
2416 		err = USB_ERR_IOERROR;
2417 		goto done;
2418 	}
2419 	goto tr_valid;
2420 
2421 tr_handle_get_port_status:
2422 
2423 	DPRINTFN(9, "UR_GET_PORT_STATUS\n");
2424 
2425 	if (index != 1) {
2426 		goto tr_stalled;
2427 	}
2428 	if (sc->sc_flags.status_vbus) {
2429 		dwc_otg_clocks_on(sc);
2430 	} else {
2431 		dwc_otg_clocks_off(sc);
2432 	}
2433 
2434 	/* Select Device Side Mode */
2435 
2436 	value = UPS_PORT_MODE_DEVICE;
2437 
2438 	if (sc->sc_flags.status_high_speed) {
2439 		value |= UPS_HIGH_SPEED;
2440 	}
2441 	if (sc->sc_flags.port_powered) {
2442 		value |= UPS_PORT_POWER;
2443 	}
2444 	if (sc->sc_flags.port_enabled) {
2445 		value |= UPS_PORT_ENABLED;
2446 	}
2447 	if (sc->sc_flags.status_vbus &&
2448 	    sc->sc_flags.status_bus_reset) {
2449 		value |= UPS_CURRENT_CONNECT_STATUS;
2450 	}
2451 	if (sc->sc_flags.status_suspend) {
2452 		value |= UPS_SUSPEND;
2453 	}
2454 	USETW(sc->sc_hub_temp.ps.wPortStatus, value);
2455 
2456 	value = 0;
2457 
2458 	if (sc->sc_flags.change_connect) {
2459 		value |= UPS_C_CONNECT_STATUS;
2460 	}
2461 	if (sc->sc_flags.change_suspend) {
2462 		value |= UPS_C_SUSPEND;
2463 	}
2464 	USETW(sc->sc_hub_temp.ps.wPortChange, value);
2465 	len = sizeof(sc->sc_hub_temp.ps);
2466 	goto tr_valid;
2467 
2468 tr_handle_get_class_descriptor:
2469 	if (value & 0xFF) {
2470 		goto tr_stalled;
2471 	}
2472 	ptr = (const void *)&dwc_otg_hubd;
2473 	len = sizeof(dwc_otg_hubd);
2474 	goto tr_valid;
2475 
2476 tr_stalled:
2477 	err = USB_ERR_STALLED;
2478 tr_valid:
2479 done:
2480 	*plength = len;
2481 	*pptr = ptr;
2482 	return (err);
2483 }
2484 
2485 static void
2486 dwc_otg_xfer_setup(struct usb_setup_params *parm)
2487 {
2488 	const struct usb_hw_ep_profile *pf;
2489 	struct usb_xfer *xfer;
2490 	void *last_obj;
2491 	uint32_t ntd;
2492 	uint32_t n;
2493 	uint8_t ep_no;
2494 
2495 	xfer = parm->curr_xfer;
2496 
2497 	/*
2498 	 * NOTE: This driver does not use any of the parameters that
2499 	 * are computed from the following values. Just set some
2500 	 * reasonable dummies:
2501 	 */
2502 	parm->hc_max_packet_size = 0x500;
2503 	parm->hc_max_packet_count = 1;
2504 	parm->hc_max_frame_size = 0x500;
2505 
2506 	usbd_transfer_setup_sub(parm);
2507 
2508 	/*
2509 	 * compute maximum number of TDs
2510 	 */
2511 	if ((xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL) {
2512 
2513 		ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
2514 		    + 1 /* SYNC 2 */ ;
2515 	} else {
2516 
2517 		ntd = xfer->nframes + 1 /* SYNC */ ;
2518 	}
2519 
2520 	/*
2521 	 * check if "usbd_transfer_setup_sub" set an error
2522 	 */
2523 	if (parm->err)
2524 		return;
2525 
2526 	/*
2527 	 * allocate transfer descriptors
2528 	 */
2529 	last_obj = NULL;
2530 
2531 	/*
2532 	 * get profile stuff
2533 	 */
2534 	ep_no = xfer->endpointno & UE_ADDR;
2535 	dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
2536 
2537 	if (pf == NULL) {
2538 		/* should not happen */
2539 		parm->err = USB_ERR_INVAL;
2540 		return;
2541 	}
2542 
2543 	/* align data */
2544 	parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
2545 
2546 	for (n = 0; n != ntd; n++) {
2547 
2548 		struct dwc_otg_td *td;
2549 
2550 		if (parm->buf) {
2551 
2552 			td = USB_ADD_BYTES(parm->buf, parm->size[0]);
2553 
2554 			/* init TD */
2555 			td->max_packet_size = xfer->max_packet_size;
2556 			td->ep_no = ep_no;
2557 			td->obj_next = last_obj;
2558 
2559 			last_obj = td;
2560 		}
2561 		parm->size[0] += sizeof(*td);
2562 	}
2563 
2564 	xfer->td_start[0] = last_obj;
2565 }
2566 
2567 static void
2568 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
2569 {
2570 	return;
2571 }
2572 
2573 static void
2574 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2575     struct usb_endpoint *ep)
2576 {
2577 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
2578 
2579 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
2580 	    ep, udev->address,
2581 	    edesc->bEndpointAddress, udev->flags.usb_mode,
2582 	    sc->sc_rt_addr, udev->device_index);
2583 
2584 	if (udev->device_index != sc->sc_rt_addr) {
2585 
2586 		if (udev->flags.usb_mode != USB_MODE_DEVICE) {
2587 			/* not supported */
2588 			return;
2589 		}
2590 		if (udev->speed != USB_SPEED_FULL &&
2591 		    udev->speed != USB_SPEED_HIGH) {
2592 			/* not supported */
2593 			return;
2594 		}
2595 		if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
2596 			ep->methods = &dwc_otg_device_isoc_fs_methods;
2597 		else
2598 			ep->methods = &dwc_otg_device_non_isoc_methods;
2599 	}
2600 }
2601 
2602 static void
2603 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
2604 {
2605 	struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
2606 
2607 	switch (state) {
2608 	case USB_HW_POWER_SUSPEND:
2609 		dwc_otg_suspend(sc);
2610 		break;
2611 	case USB_HW_POWER_SHUTDOWN:
2612 		dwc_otg_uninit(sc);
2613 		break;
2614 	case USB_HW_POWER_RESUME:
2615 		dwc_otg_resume(sc);
2616 		break;
2617 	default:
2618 		break;
2619 	}
2620 }
2621 
2622 struct usb_bus_methods dwc_otg_bus_methods =
2623 {
2624 	.endpoint_init = &dwc_otg_ep_init,
2625 	.xfer_setup = &dwc_otg_xfer_setup,
2626 	.xfer_unsetup = &dwc_otg_xfer_unsetup,
2627 	.get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
2628 	.set_stall = &dwc_otg_set_stall,
2629 	.clear_stall = &dwc_otg_clear_stall,
2630 	.roothub_exec = &dwc_otg_roothub_exec,
2631 	.xfer_poll = &dwc_otg_do_poll,
2632 	.device_state_change = &dwc_otg_device_state_change,
2633 	.set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,
2634 };
2635