ehci.c revision 192053
1802Ssundar/*-
2802Ssundar * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
3970Sattila * Copyright (c) 2004 The NetBSD Foundation, Inc. All rights reserved.
4802Ssundar * Copyright (c) 2004 Lennart Augustsson. All rights reserved.
5802Ssundar * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
6802Ssundar *
7970Sattila * Redistribution and use in source and binary forms, with or without
8802Ssundar * modification, are permitted provided that the following conditions
9802Ssundar * are met:
10970Sattila * 1. Redistributions of source code must retain the above copyright
11802Ssundar *    notice, this list of conditions and the following disclaimer.
12802Ssundar * 2. Redistributions in binary form must reproduce the above copyright
13802Ssundar *    notice, this list of conditions and the following disclaimer in the
14970Sattila *    documentation and/or other materials provided with the distribution.
15802Ssundar *
16802Ssundar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17802Ssundar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18970Sattila * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19802Ssundar * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20802Ssundar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21802Ssundar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22802Ssundar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23802Ssundar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24802Ssundar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25802Ssundar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26802Ssundar * SUCH DAMAGE.
27802Ssundar */
28802Ssundar
29802Ssundar/*
30802Ssundar * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
31802Ssundar *
32802Ssundar * The EHCI 0.96 spec can be found at
33802Ssundar * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
34970Sattila * The EHCI 1.0 spec can be found at
35802Ssundar * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
36802Ssundar * and the USB 2.0 spec at
37802Ssundar * http://www.usb.org/developers/docs/usb_20.zip
38802Ssundar *
39802Ssundar */
40802Ssundar
41802Ssundar/*
42802Ssundar * TODO:
43802Ssundar * 1) command failures are not recovered correctly
44802Ssundar */
45802Ssundar
46802Ssundar#include <sys/cdefs.h>
47802Ssundar__FBSDID("$FreeBSD: head/sys/dev/usb/controller/ehci.c 192053 2009-05-13 18:04:51Z thompsa $");
48802Ssundar
49#include <dev/usb/usb.h>
50#include <dev/usb/usb_mfunc.h>
51#include <dev/usb/usb_error.h>
52
53#define	USB_DEBUG_VAR ehcidebug
54
55#include <dev/usb/usb_core.h>
56#include <dev/usb/usb_debug.h>
57#include <dev/usb/usb_busdma.h>
58#include <dev/usb/usb_process.h>
59#include <dev/usb/usb_transfer.h>
60#include <dev/usb/usb_device.h>
61#include <dev/usb/usb_hub.h>
62#include <dev/usb/usb_util.h>
63
64#include <dev/usb/usb_controller.h>
65#include <dev/usb/usb_bus.h>
66#include <dev/usb/controller/ehci.h>
67
68#define	EHCI_BUS2SC(bus) \
69   ((ehci_softc_t *)(((uint8_t *)(bus)) - \
70    ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))
71
72#if USB_DEBUG
73static int ehcidebug = 0;
74static int ehcinohighspeed = 0;
75
76SYSCTL_NODE(_hw_usb2, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
77SYSCTL_INT(_hw_usb2_ehci, OID_AUTO, debug, CTLFLAG_RW,
78    &ehcidebug, 0, "Debug level");
79SYSCTL_INT(_hw_usb2_ehci, OID_AUTO, no_hs, CTLFLAG_RW,
80    &ehcinohighspeed, 0, "Disable High Speed USB");
81
82static void ehci_dump_regs(ehci_softc_t *sc);
83static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
84
85#endif
86
87#define	EHCI_INTR_ENDPT 1
88
89extern struct usb2_bus_methods ehci_bus_methods;
90extern struct usb2_pipe_methods ehci_device_bulk_methods;
91extern struct usb2_pipe_methods ehci_device_ctrl_methods;
92extern struct usb2_pipe_methods ehci_device_intr_methods;
93extern struct usb2_pipe_methods ehci_device_isoc_fs_methods;
94extern struct usb2_pipe_methods ehci_device_isoc_hs_methods;
95
96static void ehci_do_poll(struct usb2_bus *bus);
97static void ehci_device_done(struct usb2_xfer *xfer, usb2_error_t error);
98static uint8_t ehci_check_transfer(struct usb2_xfer *xfer);
99static void ehci_timeout(void *arg);
100static void ehci_root_intr(ehci_softc_t *sc);
101
102struct ehci_std_temp {
103	ehci_softc_t *sc;
104	struct usb2_page_cache *pc;
105	ehci_qtd_t *td;
106	ehci_qtd_t *td_next;
107	uint32_t average;
108	uint32_t qtd_status;
109	uint32_t len;
110	uint16_t max_frame_size;
111	uint8_t	shortpkt;
112	uint8_t	auto_data_toggle;
113	uint8_t	setup_alt_next;
114	uint8_t	last_frame;
115};
116
117void
118ehci_iterate_hw_softc(struct usb2_bus *bus, usb2_bus_mem_sub_cb_t *cb)
119{
120	ehci_softc_t *sc = EHCI_BUS2SC(bus);
121	uint32_t i;
122
123	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
124	    sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
125
126	cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
127	    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
128
129	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
130		cb(bus, sc->sc_hw.intr_start_pc + i,
131		    sc->sc_hw.intr_start_pg + i,
132		    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
133	}
134
135	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
136		cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
137		    sc->sc_hw.isoc_hs_start_pg + i,
138		    sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
139	}
140
141	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
142		cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
143		    sc->sc_hw.isoc_fs_start_pg + i,
144		    sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
145	}
146}
147
148usb2_error_t
149ehci_reset(ehci_softc_t *sc)
150{
151	uint32_t hcr;
152	int i;
153
154	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
155	for (i = 0; i < 100; i++) {
156		usb2_pause_mtx(NULL, hz / 1000);
157		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
158		if (!hcr) {
159			if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) {
160				/*
161				 * Force USBMODE as requested.  Controllers
162				 * may have multiple operating modes.
163				 */
164				uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE);
165				if (sc->sc_flags & EHCI_SCFLG_SETMODE) {
166					usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST;
167					device_printf(sc->sc_bus.bdev,
168					    "set host controller mode\n");
169				}
170				if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) {
171					usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE;
172					device_printf(sc->sc_bus.bdev,
173					    "set big-endian mode\n");
174				}
175				EOWRITE4(sc,  EHCI_USBMODE, usbmode);
176			}
177			return (0);
178		}
179	}
180	device_printf(sc->sc_bus.bdev, "reset timeout\n");
181	return (USB_ERR_IOERROR);
182}
183
184static usb2_error_t
185ehci_hcreset(ehci_softc_t *sc)
186{
187	uint32_t hcr;
188	int i;
189
190	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
191	for (i = 0; i < 100; i++) {
192		usb2_pause_mtx(NULL, hz / 1000);
193		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
194		if (hcr)
195			break;
196	}
197	if (!hcr)
198		/*
199                 * Fall through and try reset anyway even though
200                 * Table 2-9 in the EHCI spec says this will result
201                 * in undefined behavior.
202                 */
203		device_printf(sc->sc_bus.bdev, "stop timeout\n");
204
205	return ehci_reset(sc);
206}
207
208usb2_error_t
209ehci_init(ehci_softc_t *sc)
210{
211	struct usb2_page_search buf_res;
212	uint32_t version;
213	uint32_t sparams;
214	uint32_t cparams;
215	uint32_t hcr;
216	uint16_t i;
217	uint16_t x;
218	uint16_t y;
219	uint16_t bit;
220	usb2_error_t err = 0;
221
222	DPRINTF("start\n");
223
224	usb2_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
225
226#if USB_DEBUG
227	if (ehcidebug > 2) {
228		ehci_dump_regs(sc);
229	}
230#endif
231
232	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
233
234	version = EREAD2(sc, EHCI_HCIVERSION);
235	device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
236	    version >> 8, version & 0xff);
237
238	sparams = EREAD4(sc, EHCI_HCSPARAMS);
239	DPRINTF("sparams=0x%x\n", sparams);
240
241	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
242	cparams = EREAD4(sc, EHCI_HCCPARAMS);
243	DPRINTF("cparams=0x%x\n", cparams);
244
245	if (EHCI_HCC_64BIT(cparams)) {
246		DPRINTF("HCC uses 64-bit structures\n");
247
248		/* MUST clear segment register if 64 bit capable */
249		EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
250	}
251	sc->sc_bus.usbrev = USB_REV_2_0;
252
253	/* Reset the controller */
254	DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev));
255
256	err = ehci_hcreset(sc);
257	if (err) {
258		device_printf(sc->sc_bus.bdev, "reset timeout\n");
259		return (err);
260	}
261	/*
262	 * use current frame-list-size selection 0: 1024*4 bytes 1:  512*4
263	 * bytes 2:  256*4 bytes 3:      unknown
264	 */
265	if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
266		device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
267		return (USB_ERR_IOERROR);
268	}
269	/* set up the bus struct */
270	sc->sc_bus.methods = &ehci_bus_methods;
271
272	sc->sc_eintrs = EHCI_NORMAL_INTRS;
273
274	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
275		ehci_qh_t *qh;
276
277		usb2_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
278
279		qh = buf_res.buffer;
280
281		/* initialize page cache pointer */
282
283		qh->page_cache = sc->sc_hw.intr_start_pc + i;
284
285		/* store a pointer to queue head */
286
287		sc->sc_intr_p_last[i] = qh;
288
289		qh->qh_self =
290		    htohc32(sc, buf_res.physaddr) |
291		    htohc32(sc, EHCI_LINK_QH);
292
293		qh->qh_endp =
294		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
295		qh->qh_endphub =
296		    htohc32(sc, EHCI_QH_SET_MULT(1));
297		qh->qh_curqtd = 0;
298
299		qh->qh_qtd.qtd_next =
300		    htohc32(sc, EHCI_LINK_TERMINATE);
301		qh->qh_qtd.qtd_altnext =
302		    htohc32(sc, EHCI_LINK_TERMINATE);
303		qh->qh_qtd.qtd_status =
304		    htohc32(sc, EHCI_QTD_HALTED);
305	}
306
307	/*
308	 * the QHs are arranged to give poll intervals that are
309	 * powers of 2 times 1ms
310	 */
311	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
312	while (bit) {
313		x = bit;
314		while (x & bit) {
315			ehci_qh_t *qh_x;
316			ehci_qh_t *qh_y;
317
318			y = (x ^ bit) | (bit / 2);
319
320			qh_x = sc->sc_intr_p_last[x];
321			qh_y = sc->sc_intr_p_last[y];
322
323			/*
324			 * the next QH has half the poll interval
325			 */
326			qh_x->qh_link = qh_y->qh_self;
327
328			x++;
329		}
330		bit >>= 1;
331	}
332
333	if (1) {
334		ehci_qh_t *qh;
335
336		qh = sc->sc_intr_p_last[0];
337
338		/* the last (1ms) QH terminates */
339		qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
340	}
341	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
342		ehci_sitd_t *sitd;
343		ehci_itd_t *itd;
344
345		usb2_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
346
347		sitd = buf_res.buffer;
348
349		/* initialize page cache pointer */
350
351		sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
352
353		/* store a pointer to the transfer descriptor */
354
355		sc->sc_isoc_fs_p_last[i] = sitd;
356
357		/* initialize full speed isochronous */
358
359		sitd->sitd_self =
360		    htohc32(sc, buf_res.physaddr) |
361		    htohc32(sc, EHCI_LINK_SITD);
362
363		sitd->sitd_back =
364		    htohc32(sc, EHCI_LINK_TERMINATE);
365
366		sitd->sitd_next =
367		    sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
368
369
370		usb2_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
371
372		itd = buf_res.buffer;
373
374		/* initialize page cache pointer */
375
376		itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
377
378		/* store a pointer to the transfer descriptor */
379
380		sc->sc_isoc_hs_p_last[i] = itd;
381
382		/* initialize high speed isochronous */
383
384		itd->itd_self =
385		    htohc32(sc, buf_res.physaddr) |
386		    htohc32(sc, EHCI_LINK_ITD);
387
388		itd->itd_next =
389		    sitd->sitd_self;
390	}
391
392	usb2_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
393
394	if (1) {
395		uint32_t *pframes;
396
397		pframes = buf_res.buffer;
398
399		/*
400		 * execution order:
401		 * pframes -> high speed isochronous ->
402		 *    full speed isochronous -> interrupt QH's
403		 */
404		for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
405			pframes[i] = sc->sc_isoc_hs_p_last
406			    [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
407		}
408	}
409	/* setup sync list pointer */
410	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
411
412	usb2_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
413
414	if (1) {
415
416		ehci_qh_t *qh;
417
418		qh = buf_res.buffer;
419
420		/* initialize page cache pointer */
421
422		qh->page_cache = &sc->sc_hw.async_start_pc;
423
424		/* store a pointer to the queue head */
425
426		sc->sc_async_p_last = qh;
427
428		/* init dummy QH that starts the async list */
429
430		qh->qh_self =
431		    htohc32(sc, buf_res.physaddr) |
432		    htohc32(sc, EHCI_LINK_QH);
433
434		/* fill the QH */
435		qh->qh_endp =
436		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
437		qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
438		qh->qh_link = qh->qh_self;
439		qh->qh_curqtd = 0;
440
441		/* fill the overlay qTD */
442		qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
443		qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
444		qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
445	}
446	/* flush all cache into memory */
447
448	usb2_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
449
450#if USB_DEBUG
451	if (ehcidebug) {
452		ehci_dump_sqh(sc, sc->sc_async_p_last);
453	}
454#endif
455
456	/* setup async list pointer */
457	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
458
459
460	/* enable interrupts */
461	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
462
463	/* turn on controller */
464	EOWRITE4(sc, EHCI_USBCMD,
465	    EHCI_CMD_ITC_1 |		/* 1 microframes interrupt delay */
466	    (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
467	    EHCI_CMD_ASE |
468	    EHCI_CMD_PSE |
469	    EHCI_CMD_RS);
470
471	/* Take over port ownership */
472	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
473
474	for (i = 0; i < 100; i++) {
475		usb2_pause_mtx(NULL, hz / 1000);
476		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
477		if (!hcr) {
478			break;
479		}
480	}
481	if (hcr) {
482		device_printf(sc->sc_bus.bdev, "run timeout\n");
483		return (USB_ERR_IOERROR);
484	}
485
486	if (!err) {
487		/* catch any lost interrupts */
488		ehci_do_poll(&sc->sc_bus);
489	}
490	return (err);
491}
492
493/*
494 * shut down the controller when the system is going down
495 */
496void
497ehci_detach(ehci_softc_t *sc)
498{
499	USB_BUS_LOCK(&sc->sc_bus);
500
501	usb2_callout_stop(&sc->sc_tmo_pcd);
502
503	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
504	USB_BUS_UNLOCK(&sc->sc_bus);
505
506	if (ehci_hcreset(sc)) {
507		DPRINTF("reset failed!\n");
508	}
509
510	/* XXX let stray task complete */
511	usb2_pause_mtx(NULL, hz / 20);
512
513	usb2_callout_drain(&sc->sc_tmo_pcd);
514}
515
516void
517ehci_suspend(ehci_softc_t *sc)
518{
519	uint32_t cmd;
520	uint32_t hcr;
521	uint8_t i;
522
523	USB_BUS_LOCK(&sc->sc_bus);
524
525	for (i = 1; i <= sc->sc_noport; i++) {
526		cmd = EOREAD4(sc, EHCI_PORTSC(i));
527		if (((cmd & EHCI_PS_PO) == 0) &&
528		    ((cmd & EHCI_PS_PE) == EHCI_PS_PE)) {
529			EOWRITE4(sc, EHCI_PORTSC(i),
530			    cmd | EHCI_PS_SUSP);
531		}
532	}
533
534	sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
535
536	cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
537	EOWRITE4(sc, EHCI_USBCMD, cmd);
538
539	for (i = 0; i < 100; i++) {
540		hcr = EOREAD4(sc, EHCI_USBSTS) &
541		    (EHCI_STS_ASS | EHCI_STS_PSS);
542
543		if (hcr == 0) {
544			break;
545		}
546		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
547	}
548
549	if (hcr != 0) {
550		device_printf(sc->sc_bus.bdev, "reset timeout\n");
551	}
552	cmd &= ~EHCI_CMD_RS;
553	EOWRITE4(sc, EHCI_USBCMD, cmd);
554
555	for (i = 0; i < 100; i++) {
556		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
557		if (hcr == EHCI_STS_HCH) {
558			break;
559		}
560		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
561	}
562
563	if (hcr != EHCI_STS_HCH) {
564		device_printf(sc->sc_bus.bdev,
565		    "config timeout\n");
566	}
567	USB_BUS_UNLOCK(&sc->sc_bus);
568}
569
570void
571ehci_resume(ehci_softc_t *sc)
572{
573	struct usb2_page_search buf_res;
574	uint32_t cmd;
575	uint32_t hcr;
576	uint8_t i;
577
578	USB_BUS_LOCK(&sc->sc_bus);
579
580	/* restore things in case the bios doesn't */
581	EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
582
583	usb2_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
584	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
585
586	usb2_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
587	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
588
589	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
590
591	hcr = 0;
592	for (i = 1; i <= sc->sc_noport; i++) {
593		cmd = EOREAD4(sc, EHCI_PORTSC(i));
594		if (((cmd & EHCI_PS_PO) == 0) &&
595		    ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
596			EOWRITE4(sc, EHCI_PORTSC(i),
597			    cmd | EHCI_PS_FPR);
598			hcr = 1;
599		}
600	}
601
602	if (hcr) {
603		usb2_pause_mtx(&sc->sc_bus.bus_mtx,
604		    USB_MS_TO_TICKS(USB_RESUME_WAIT));
605
606		for (i = 1; i <= sc->sc_noport; i++) {
607			cmd = EOREAD4(sc, EHCI_PORTSC(i));
608			if (((cmd & EHCI_PS_PO) == 0) &&
609			    ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
610				EOWRITE4(sc, EHCI_PORTSC(i),
611				    cmd & ~EHCI_PS_FPR);
612			}
613		}
614	}
615	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
616
617	for (i = 0; i < 100; i++) {
618		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
619		if (hcr != EHCI_STS_HCH) {
620			break;
621		}
622		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
623	}
624	if (hcr == EHCI_STS_HCH) {
625		device_printf(sc->sc_bus.bdev, "config timeout\n");
626	}
627
628	USB_BUS_UNLOCK(&sc->sc_bus);
629
630	usb2_pause_mtx(NULL,
631	    USB_MS_TO_TICKS(USB_RESUME_WAIT));
632
633	/* catch any lost interrupts */
634	ehci_do_poll(&sc->sc_bus);
635}
636
637void
638ehci_shutdown(ehci_softc_t *sc)
639{
640	DPRINTF("stopping the HC\n");
641
642	if (ehci_hcreset(sc)) {
643		DPRINTF("reset failed!\n");
644	}
645}
646
647#if USB_DEBUG
648static void
649ehci_dump_regs(ehci_softc_t *sc)
650{
651	uint32_t i;
652
653	i = EOREAD4(sc, EHCI_USBCMD);
654	printf("cmd=0x%08x\n", i);
655
656	if (i & EHCI_CMD_ITC_1)
657		printf(" EHCI_CMD_ITC_1\n");
658	if (i & EHCI_CMD_ITC_2)
659		printf(" EHCI_CMD_ITC_2\n");
660	if (i & EHCI_CMD_ITC_4)
661		printf(" EHCI_CMD_ITC_4\n");
662	if (i & EHCI_CMD_ITC_8)
663		printf(" EHCI_CMD_ITC_8\n");
664	if (i & EHCI_CMD_ITC_16)
665		printf(" EHCI_CMD_ITC_16\n");
666	if (i & EHCI_CMD_ITC_32)
667		printf(" EHCI_CMD_ITC_32\n");
668	if (i & EHCI_CMD_ITC_64)
669		printf(" EHCI_CMD_ITC_64\n");
670	if (i & EHCI_CMD_ASPME)
671		printf(" EHCI_CMD_ASPME\n");
672	if (i & EHCI_CMD_ASPMC)
673		printf(" EHCI_CMD_ASPMC\n");
674	if (i & EHCI_CMD_LHCR)
675		printf(" EHCI_CMD_LHCR\n");
676	if (i & EHCI_CMD_IAAD)
677		printf(" EHCI_CMD_IAAD\n");
678	if (i & EHCI_CMD_ASE)
679		printf(" EHCI_CMD_ASE\n");
680	if (i & EHCI_CMD_PSE)
681		printf(" EHCI_CMD_PSE\n");
682	if (i & EHCI_CMD_FLS_M)
683		printf(" EHCI_CMD_FLS_M\n");
684	if (i & EHCI_CMD_HCRESET)
685		printf(" EHCI_CMD_HCRESET\n");
686	if (i & EHCI_CMD_RS)
687		printf(" EHCI_CMD_RS\n");
688
689	i = EOREAD4(sc, EHCI_USBSTS);
690
691	printf("sts=0x%08x\n", i);
692
693	if (i & EHCI_STS_ASS)
694		printf(" EHCI_STS_ASS\n");
695	if (i & EHCI_STS_PSS)
696		printf(" EHCI_STS_PSS\n");
697	if (i & EHCI_STS_REC)
698		printf(" EHCI_STS_REC\n");
699	if (i & EHCI_STS_HCH)
700		printf(" EHCI_STS_HCH\n");
701	if (i & EHCI_STS_IAA)
702		printf(" EHCI_STS_IAA\n");
703	if (i & EHCI_STS_HSE)
704		printf(" EHCI_STS_HSE\n");
705	if (i & EHCI_STS_FLR)
706		printf(" EHCI_STS_FLR\n");
707	if (i & EHCI_STS_PCD)
708		printf(" EHCI_STS_PCD\n");
709	if (i & EHCI_STS_ERRINT)
710		printf(" EHCI_STS_ERRINT\n");
711	if (i & EHCI_STS_INT)
712		printf(" EHCI_STS_INT\n");
713
714	printf("ien=0x%08x\n",
715	    EOREAD4(sc, EHCI_USBINTR));
716	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
717	    EOREAD4(sc, EHCI_FRINDEX),
718	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
719	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
720	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
721	for (i = 1; i <= sc->sc_noport; i++) {
722		printf("port %d status=0x%08x\n", i,
723		    EOREAD4(sc, EHCI_PORTSC(i)));
724	}
725}
726
727static void
728ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
729{
730	link = hc32toh(sc, link);
731	printf("0x%08x", link);
732	if (link & EHCI_LINK_TERMINATE)
733		printf("<T>");
734	else {
735		printf("<");
736		if (type) {
737			switch (EHCI_LINK_TYPE(link)) {
738			case EHCI_LINK_ITD:
739				printf("ITD");
740				break;
741			case EHCI_LINK_QH:
742				printf("QH");
743				break;
744			case EHCI_LINK_SITD:
745				printf("SITD");
746				break;
747			case EHCI_LINK_FSTN:
748				printf("FSTN");
749				break;
750			}
751		}
752		printf(">");
753	}
754}
755
756static void
757ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
758{
759	uint32_t s;
760
761	printf("  next=");
762	ehci_dump_link(sc, qtd->qtd_next, 0);
763	printf(" altnext=");
764	ehci_dump_link(sc, qtd->qtd_altnext, 0);
765	printf("\n");
766	s = hc32toh(sc, qtd->qtd_status);
767	printf("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
768	    s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
769	    EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
770	printf("    cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
771	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
772	    (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
773	    (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
774	    (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
775	    (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
776	    (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
777	    (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
778	    (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
779	    (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
780
781	for (s = 0; s < 5; s++) {
782		printf("  buffer[%d]=0x%08x\n", s,
783		    hc32toh(sc, qtd->qtd_buffer[s]));
784	}
785	for (s = 0; s < 5; s++) {
786		printf("  buffer_hi[%d]=0x%08x\n", s,
787		    hc32toh(sc, qtd->qtd_buffer_hi[s]));
788	}
789}
790
791static uint8_t
792ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
793{
794	uint8_t temp;
795
796	usb2_pc_cpu_invalidate(sqtd->page_cache);
797	printf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
798	ehci_dump_qtd(sc, sqtd);
799	temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
800	return (temp);
801}
802
803static void
804ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
805{
806	uint16_t i;
807	uint8_t stop;
808
809	stop = 0;
810	for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
811		stop = ehci_dump_sqtd(sc, sqtd);
812	}
813	if (sqtd) {
814		printf("dump aborted, too many TDs\n");
815	}
816}
817
818static void
819ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
820{
821	uint32_t endp;
822	uint32_t endphub;
823
824	usb2_pc_cpu_invalidate(qh->page_cache);
825	printf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
826	printf("  link=");
827	ehci_dump_link(sc, qh->qh_link, 1);
828	printf("\n");
829	endp = hc32toh(sc, qh->qh_endp);
830	printf("  endp=0x%08x\n", endp);
831	printf("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
832	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
833	    EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
834	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
835	printf("    mpl=0x%x ctl=%d nrl=%d\n",
836	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
837	    EHCI_QH_GET_NRL(endp));
838	endphub = hc32toh(sc, qh->qh_endphub);
839	printf("  endphub=0x%08x\n", endphub);
840	printf("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
841	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
842	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
843	    EHCI_QH_GET_MULT(endphub));
844	printf("  curqtd=");
845	ehci_dump_link(sc, qh->qh_curqtd, 0);
846	printf("\n");
847	printf("Overlay qTD:\n");
848	ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
849}
850
851static void
852ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
853{
854	usb2_pc_cpu_invalidate(sitd->page_cache);
855	printf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
856	printf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
857	printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
858	    hc32toh(sc, sitd->sitd_portaddr),
859	    (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
860	    ? "in" : "out",
861	    EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
862	    EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
863	    EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
864	    EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
865	printf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
866	printf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
867	    (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
868	    EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
869	printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
870	    hc32toh(sc, sitd->sitd_back),
871	    hc32toh(sc, sitd->sitd_bp[0]),
872	    hc32toh(sc, sitd->sitd_bp[1]),
873	    hc32toh(sc, sitd->sitd_bp_hi[0]),
874	    hc32toh(sc, sitd->sitd_bp_hi[1]));
875}
876
877static void
878ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
879{
880	usb2_pc_cpu_invalidate(itd->page_cache);
881	printf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
882	printf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
883	printf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
884	    (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
885	printf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
886	    (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
887	printf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
888	    (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
889	printf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
890	    (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
891	printf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
892	    (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
893	printf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
894	    (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
895	printf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
896	    (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
897	printf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
898	    (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
899	printf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
900	printf("  addr=0x%02x; endpt=0x%01x\n",
901	    EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
902	    EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
903	printf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
904	printf(" dir=%s; mpl=0x%02x\n",
905	    (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
906	    EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
907	printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
908	    hc32toh(sc, itd->itd_bp[2]),
909	    hc32toh(sc, itd->itd_bp[3]),
910	    hc32toh(sc, itd->itd_bp[4]),
911	    hc32toh(sc, itd->itd_bp[5]),
912	    hc32toh(sc, itd->itd_bp[6]));
913	printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
914	    "       0x%08x,0x%08x,0x%08x\n",
915	    hc32toh(sc, itd->itd_bp_hi[0]),
916	    hc32toh(sc, itd->itd_bp_hi[1]),
917	    hc32toh(sc, itd->itd_bp_hi[2]),
918	    hc32toh(sc, itd->itd_bp_hi[3]),
919	    hc32toh(sc, itd->itd_bp_hi[4]),
920	    hc32toh(sc, itd->itd_bp_hi[5]),
921	    hc32toh(sc, itd->itd_bp_hi[6]));
922}
923
924static void
925ehci_dump_isoc(ehci_softc_t *sc)
926{
927	ehci_itd_t *itd;
928	ehci_sitd_t *sitd;
929	uint16_t max = 1000;
930	uint16_t pos;
931
932	pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
933	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
934
935	printf("%s: isochronous dump from frame 0x%03x:\n",
936	    __FUNCTION__, pos);
937
938	itd = sc->sc_isoc_hs_p_last[pos];
939	sitd = sc->sc_isoc_fs_p_last[pos];
940
941	while (itd && max && max--) {
942		ehci_dump_itd(sc, itd);
943		itd = itd->prev;
944	}
945
946	while (sitd && max && max--) {
947		ehci_dump_sitd(sc, sitd);
948		sitd = sitd->prev;
949	}
950}
951
952#endif
953
954static void
955ehci_transfer_intr_enqueue(struct usb2_xfer *xfer)
956{
957	/* check for early completion */
958	if (ehci_check_transfer(xfer)) {
959		return;
960	}
961	/* put transfer on interrupt queue */
962	usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
963
964	/* start timeout, if any */
965	if (xfer->timeout != 0) {
966		usb2_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
967	}
968}
969
970#define	EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
971static ehci_sitd_t *
972_ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
973{
974	DPRINTFN(11, "%p to %p\n", std, last);
975
976	/* (sc->sc_bus.mtx) must be locked */
977
978	std->next = last->next;
979	std->sitd_next = last->sitd_next;
980
981	std->prev = last;
982
983	usb2_pc_cpu_flush(std->page_cache);
984
985	/*
986	 * the last->next->prev is never followed: std->next->prev = std;
987	 */
988	last->next = std;
989	last->sitd_next = std->sitd_self;
990
991	usb2_pc_cpu_flush(last->page_cache);
992
993	return (std);
994}
995
996#define	EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
997static ehci_itd_t *
998_ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
999{
1000	DPRINTFN(11, "%p to %p\n", std, last);
1001
1002	/* (sc->sc_bus.mtx) must be locked */
1003
1004	std->next = last->next;
1005	std->itd_next = last->itd_next;
1006
1007	std->prev = last;
1008
1009	usb2_pc_cpu_flush(std->page_cache);
1010
1011	/*
1012	 * the last->next->prev is never followed: std->next->prev = std;
1013	 */
1014	last->next = std;
1015	last->itd_next = std->itd_self;
1016
1017	usb2_pc_cpu_flush(last->page_cache);
1018
1019	return (std);
1020}
1021
1022#define	EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
1023static ehci_qh_t *
1024_ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
1025{
1026	DPRINTFN(11, "%p to %p\n", sqh, last);
1027
1028	if (sqh->prev != NULL) {
1029		/* should not happen */
1030		DPRINTFN(0, "QH already linked!\n");
1031		return (last);
1032	}
1033	/* (sc->sc_bus.mtx) must be locked */
1034
1035	sqh->next = last->next;
1036	sqh->qh_link = last->qh_link;
1037
1038	sqh->prev = last;
1039
1040	usb2_pc_cpu_flush(sqh->page_cache);
1041
1042	/*
1043	 * the last->next->prev is never followed: sqh->next->prev = sqh;
1044	 */
1045
1046	last->next = sqh;
1047	last->qh_link = sqh->qh_self;
1048
1049	usb2_pc_cpu_flush(last->page_cache);
1050
1051	return (sqh);
1052}
1053
1054#define	EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
1055static ehci_sitd_t *
1056_ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
1057{
1058	DPRINTFN(11, "%p from %p\n", std, last);
1059
1060	/* (sc->sc_bus.mtx) must be locked */
1061
1062	std->prev->next = std->next;
1063	std->prev->sitd_next = std->sitd_next;
1064
1065	usb2_pc_cpu_flush(std->prev->page_cache);
1066
1067	if (std->next) {
1068		std->next->prev = std->prev;
1069		usb2_pc_cpu_flush(std->next->page_cache);
1070	}
1071	return ((last == std) ? std->prev : last);
1072}
1073
1074#define	EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
1075static ehci_itd_t *
1076_ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
1077{
1078	DPRINTFN(11, "%p from %p\n", std, last);
1079
1080	/* (sc->sc_bus.mtx) must be locked */
1081
1082	std->prev->next = std->next;
1083	std->prev->itd_next = std->itd_next;
1084
1085	usb2_pc_cpu_flush(std->prev->page_cache);
1086
1087	if (std->next) {
1088		std->next->prev = std->prev;
1089		usb2_pc_cpu_flush(std->next->page_cache);
1090	}
1091	return ((last == std) ? std->prev : last);
1092}
1093
1094#define	EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
1095static ehci_qh_t *
1096_ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
1097{
1098	DPRINTFN(11, "%p from %p\n", sqh, last);
1099
1100	/* (sc->sc_bus.mtx) must be locked */
1101
1102	/* only remove if not removed from a queue */
1103	if (sqh->prev) {
1104
1105		sqh->prev->next = sqh->next;
1106		sqh->prev->qh_link = sqh->qh_link;
1107
1108		usb2_pc_cpu_flush(sqh->prev->page_cache);
1109
1110		if (sqh->next) {
1111			sqh->next->prev = sqh->prev;
1112			usb2_pc_cpu_flush(sqh->next->page_cache);
1113		}
1114		last = ((last == sqh) ? sqh->prev : last);
1115
1116		sqh->prev = 0;
1117
1118		usb2_pc_cpu_flush(sqh->page_cache);
1119	}
1120	return (last);
1121}
1122
1123static usb2_error_t
1124ehci_non_isoc_done_sub(struct usb2_xfer *xfer)
1125{
1126	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1127	ehci_qtd_t *td;
1128	ehci_qtd_t *td_alt_next;
1129	uint32_t status;
1130	uint16_t len;
1131
1132	td = xfer->td_transfer_cache;
1133	td_alt_next = td->alt_next;
1134
1135	if (xfer->aframes != xfer->nframes) {
1136		xfer->frlengths[xfer->aframes] = 0;
1137	}
1138	while (1) {
1139
1140		usb2_pc_cpu_invalidate(td->page_cache);
1141		status = hc32toh(sc, td->qtd_status);
1142
1143		len = EHCI_QTD_GET_BYTES(status);
1144
1145		/*
1146	         * Verify the status length and
1147		 * add the length to "frlengths[]":
1148	         */
1149		if (len > td->len) {
1150			/* should not happen */
1151			DPRINTF("Invalid status length, "
1152			    "0x%04x/0x%04x bytes\n", len, td->len);
1153			status |= EHCI_QTD_HALTED;
1154		} else if (xfer->aframes != xfer->nframes) {
1155			xfer->frlengths[xfer->aframes] += td->len - len;
1156		}
1157		/* Check for last transfer */
1158		if (((void *)td) == xfer->td_transfer_last) {
1159			if (len == 0) {
1160				/*
1161			         * Halt is ok if descriptor is last,
1162			         * and complete:
1163			         */
1164				status &= ~EHCI_QTD_HALTED;
1165			}
1166			td = NULL;
1167			break;
1168		}
1169		/* Check for transfer error */
1170		if (status & EHCI_QTD_HALTED) {
1171			/* the transfer is finished */
1172			td = NULL;
1173			break;
1174		}
1175		/* Check for short transfer */
1176		if (len > 0) {
1177			if (xfer->flags_int.short_frames_ok) {
1178				/* follow alt next */
1179				td = td->alt_next;
1180			} else {
1181				/* the transfer is finished */
1182				td = NULL;
1183			}
1184			break;
1185		}
1186		td = td->obj_next;
1187
1188		if (td->alt_next != td_alt_next) {
1189			/* this USB frame is complete */
1190			break;
1191		}
1192	}
1193
1194	/* update transfer cache */
1195
1196	xfer->td_transfer_cache = td;
1197
1198	/* update data toggle */
1199
1200	xfer->pipe->toggle_next =
1201	    (status & EHCI_QTD_TOGGLE_MASK) ? 1 : 0;
1202
1203#if USB_DEBUG
1204	if (status & EHCI_QTD_STATERRS) {
1205		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
1206		    "status=%s%s%s%s%s%s%s%s\n",
1207		    xfer->address, xfer->endpoint, xfer->aframes,
1208		    (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1209		    (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
1210		    (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
1211		    (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
1212		    (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
1213		    (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
1214		    (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
1215		    (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
1216	}
1217#endif
1218
1219	return ((status & EHCI_QTD_HALTED) ?
1220	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1221}
1222
1223static void
1224ehci_non_isoc_done(struct usb2_xfer *xfer)
1225{
1226	usb2_error_t err = 0;
1227
1228	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
1229	    xfer, xfer->pipe);
1230
1231#if USB_DEBUG
1232	if (ehcidebug > 10) {
1233		ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1234
1235		ehci_dump_sqtds(sc, xfer->td_transfer_first);
1236	}
1237#endif
1238
1239	/* reset scanner */
1240
1241	xfer->td_transfer_cache = xfer->td_transfer_first;
1242
1243	if (xfer->flags_int.control_xfr) {
1244
1245		if (xfer->flags_int.control_hdr) {
1246
1247			err = ehci_non_isoc_done_sub(xfer);
1248		}
1249		xfer->aframes = 1;
1250
1251		if (xfer->td_transfer_cache == NULL) {
1252			goto done;
1253		}
1254	}
1255	while (xfer->aframes != xfer->nframes) {
1256
1257		err = ehci_non_isoc_done_sub(xfer);
1258		xfer->aframes++;
1259
1260		if (xfer->td_transfer_cache == NULL) {
1261			goto done;
1262		}
1263	}
1264
1265	if (xfer->flags_int.control_xfr &&
1266	    !xfer->flags_int.control_act) {
1267
1268		err = ehci_non_isoc_done_sub(xfer);
1269	}
1270done:
1271	ehci_device_done(xfer, err);
1272}
1273
1274/*------------------------------------------------------------------------*
1275 *	ehci_check_transfer
1276 *
1277 * Return values:
1278 *    0: USB transfer is not finished
1279 * Else: USB transfer is finished
1280 *------------------------------------------------------------------------*/
1281static uint8_t
1282ehci_check_transfer(struct usb2_xfer *xfer)
1283{
1284	struct usb2_pipe_methods *methods = xfer->pipe->methods;
1285	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1286
1287	uint32_t status;
1288
1289	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
1290
1291	if (methods == &ehci_device_isoc_fs_methods) {
1292		ehci_sitd_t *td;
1293
1294		/* isochronous full speed transfer */
1295
1296		td = xfer->td_transfer_last;
1297		usb2_pc_cpu_invalidate(td->page_cache);
1298		status = hc32toh(sc, td->sitd_status);
1299
1300		/* also check if first is complete */
1301
1302		td = xfer->td_transfer_first;
1303		usb2_pc_cpu_invalidate(td->page_cache);
1304		status |= hc32toh(sc, td->sitd_status);
1305
1306		if (!(status & EHCI_SITD_ACTIVE)) {
1307			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1308			goto transferred;
1309		}
1310	} else if (methods == &ehci_device_isoc_hs_methods) {
1311		ehci_itd_t *td;
1312
1313		/* isochronous high speed transfer */
1314
1315		td = xfer->td_transfer_last;
1316		usb2_pc_cpu_invalidate(td->page_cache);
1317		status =
1318		    td->itd_status[0] | td->itd_status[1] |
1319		    td->itd_status[2] | td->itd_status[3] |
1320		    td->itd_status[4] | td->itd_status[5] |
1321		    td->itd_status[6] | td->itd_status[7];
1322
1323		/* also check first transfer */
1324		td = xfer->td_transfer_first;
1325		usb2_pc_cpu_invalidate(td->page_cache);
1326		status |=
1327		    td->itd_status[0] | td->itd_status[1] |
1328		    td->itd_status[2] | td->itd_status[3] |
1329		    td->itd_status[4] | td->itd_status[5] |
1330		    td->itd_status[6] | td->itd_status[7];
1331
1332		/* if no transactions are active we continue */
1333		if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
1334			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1335			goto transferred;
1336		}
1337	} else {
1338		ehci_qtd_t *td;
1339
1340		/* non-isochronous transfer */
1341
1342		/*
1343		 * check whether there is an error somewhere in the middle,
1344		 * or whether there was a short packet (SPD and not ACTIVE)
1345		 */
1346		td = xfer->td_transfer_cache;
1347
1348		while (1) {
1349			usb2_pc_cpu_invalidate(td->page_cache);
1350			status = hc32toh(sc, td->qtd_status);
1351
1352			/*
1353			 * if there is an active TD the transfer isn't done
1354			 */
1355			if (status & EHCI_QTD_ACTIVE) {
1356				/* update cache */
1357				xfer->td_transfer_cache = td;
1358				goto done;
1359			}
1360			/*
1361			 * last transfer descriptor makes the transfer done
1362			 */
1363			if (((void *)td) == xfer->td_transfer_last) {
1364				break;
1365			}
1366			/*
1367			 * any kind of error makes the transfer done
1368			 */
1369			if (status & EHCI_QTD_HALTED) {
1370				break;
1371			}
1372			/*
1373			 * if there is no alternate next transfer, a short
1374			 * packet also makes the transfer done
1375			 */
1376			if (EHCI_QTD_GET_BYTES(status)) {
1377				if (xfer->flags_int.short_frames_ok) {
1378					/* follow alt next */
1379					if (td->alt_next) {
1380						td = td->alt_next;
1381						continue;
1382					}
1383				}
1384				/* transfer is done */
1385				break;
1386			}
1387			td = td->obj_next;
1388		}
1389		ehci_non_isoc_done(xfer);
1390		goto transferred;
1391	}
1392
1393done:
1394	DPRINTFN(13, "xfer=%p is still active\n", xfer);
1395	return (0);
1396
1397transferred:
1398	return (1);
1399}
1400
1401static void
1402ehci_pcd_enable(ehci_softc_t *sc)
1403{
1404	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1405
1406	sc->sc_eintrs |= EHCI_STS_PCD;
1407	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1408
1409	/* acknowledge any PCD interrupt */
1410	EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
1411
1412	ehci_root_intr(sc);
1413}
1414
1415static void
1416ehci_interrupt_poll(ehci_softc_t *sc)
1417{
1418	struct usb2_xfer *xfer;
1419
1420repeat:
1421	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1422		/*
1423		 * check if transfer is transferred
1424		 */
1425		if (ehci_check_transfer(xfer)) {
1426			/* queue has been modified */
1427			goto repeat;
1428		}
1429	}
1430}
1431
1432/*------------------------------------------------------------------------*
1433 *	ehci_interrupt - EHCI interrupt handler
1434 *
1435 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1436 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1437 * is present !
1438 *------------------------------------------------------------------------*/
1439void
1440ehci_interrupt(ehci_softc_t *sc)
1441{
1442	uint32_t status;
1443
1444	USB_BUS_LOCK(&sc->sc_bus);
1445
1446	DPRINTFN(16, "real interrupt\n");
1447
1448#if USB_DEBUG
1449	if (ehcidebug > 15) {
1450		ehci_dump_regs(sc);
1451	}
1452#endif
1453
1454	status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
1455	if (status == 0) {
1456		/* the interrupt was not for us */
1457		goto done;
1458	}
1459	if (!(status & sc->sc_eintrs)) {
1460		goto done;
1461	}
1462	EOWRITE4(sc, EHCI_USBSTS, status);	/* acknowledge */
1463
1464	status &= sc->sc_eintrs;
1465
1466	if (status & EHCI_STS_HSE) {
1467		printf("%s: unrecoverable error, "
1468		    "controller halted\n", __FUNCTION__);
1469#if USB_DEBUG
1470		ehci_dump_regs(sc);
1471		ehci_dump_isoc(sc);
1472#endif
1473	}
1474	if (status & EHCI_STS_PCD) {
1475		/*
1476		 * Disable PCD interrupt for now, because it will be
1477		 * on until the port has been reset.
1478		 */
1479		sc->sc_eintrs &= ~EHCI_STS_PCD;
1480		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1481
1482		ehci_root_intr(sc);
1483
1484		/* do not allow RHSC interrupts > 1 per second */
1485		usb2_callout_reset(&sc->sc_tmo_pcd, hz,
1486		    (void *)&ehci_pcd_enable, sc);
1487	}
1488	status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
1489
1490	if (status != 0) {
1491		/* block unprocessed interrupts */
1492		sc->sc_eintrs &= ~status;
1493		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1494		printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
1495	}
1496	/* poll all the USB transfers */
1497	ehci_interrupt_poll(sc);
1498
1499done:
1500	USB_BUS_UNLOCK(&sc->sc_bus);
1501}
1502
1503/*
1504 * called when a request does not complete
1505 */
1506static void
1507ehci_timeout(void *arg)
1508{
1509	struct usb2_xfer *xfer = arg;
1510
1511	DPRINTF("xfer=%p\n", xfer);
1512
1513	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1514
1515	/* transfer is transferred */
1516	ehci_device_done(xfer, USB_ERR_TIMEOUT);
1517}
1518
1519static void
1520ehci_do_poll(struct usb2_bus *bus)
1521{
1522	ehci_softc_t *sc = EHCI_BUS2SC(bus);
1523
1524	USB_BUS_LOCK(&sc->sc_bus);
1525	ehci_interrupt_poll(sc);
1526	USB_BUS_UNLOCK(&sc->sc_bus);
1527}
1528
1529static void
1530ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
1531{
1532	struct usb2_page_search buf_res;
1533	ehci_qtd_t *td;
1534	ehci_qtd_t *td_next;
1535	ehci_qtd_t *td_alt_next;
1536	uint32_t qtd_altnext;
1537	uint32_t buf_offset;
1538	uint32_t average;
1539	uint32_t len_old;
1540	uint32_t terminate;
1541	uint8_t shortpkt_old;
1542	uint8_t precompute;
1543
1544	terminate = htohc32(temp->sc, EHCI_LINK_TERMINATE);
1545	qtd_altnext = terminate;
1546	td_alt_next = NULL;
1547	buf_offset = 0;
1548	shortpkt_old = temp->shortpkt;
1549	len_old = temp->len;
1550	precompute = 1;
1551
1552restart:
1553
1554	td = temp->td;
1555	td_next = temp->td_next;
1556
1557	while (1) {
1558
1559		if (temp->len == 0) {
1560
1561			if (temp->shortpkt) {
1562				break;
1563			}
1564			/* send a Zero Length Packet, ZLP, last */
1565
1566			temp->shortpkt = 1;
1567			average = 0;
1568
1569		} else {
1570
1571			average = temp->average;
1572
1573			if (temp->len < average) {
1574				if (temp->len % temp->max_frame_size) {
1575					temp->shortpkt = 1;
1576				}
1577				average = temp->len;
1578			}
1579		}
1580
1581		if (td_next == NULL) {
1582			panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
1583		}
1584		/* get next TD */
1585
1586		td = td_next;
1587		td_next = td->obj_next;
1588
1589		/* check if we are pre-computing */
1590
1591		if (precompute) {
1592
1593			/* update remaining length */
1594
1595			temp->len -= average;
1596
1597			continue;
1598		}
1599		/* fill out current TD */
1600
1601		td->qtd_status =
1602		    temp->qtd_status |
1603		    htohc32(temp->sc, EHCI_QTD_SET_BYTES(average));
1604
1605		if (average == 0) {
1606
1607			if (temp->auto_data_toggle == 0) {
1608
1609				/* update data toggle, ZLP case */
1610
1611				temp->qtd_status ^=
1612				    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1613			}
1614			td->len = 0;
1615
1616			td->qtd_buffer[0] = 0;
1617			td->qtd_buffer_hi[0] = 0;
1618
1619			td->qtd_buffer[1] = 0;
1620			td->qtd_buffer_hi[1] = 0;
1621
1622		} else {
1623
1624			uint8_t x;
1625
1626			if (temp->auto_data_toggle == 0) {
1627
1628				/* update data toggle */
1629
1630				if (((average + temp->max_frame_size - 1) /
1631				    temp->max_frame_size) & 1) {
1632					temp->qtd_status ^=
1633					    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
1634				}
1635			}
1636			td->len = average;
1637
1638			/* update remaining length */
1639
1640			temp->len -= average;
1641
1642			/* fill out buffer pointers */
1643
1644			usb2_get_page(temp->pc, buf_offset, &buf_res);
1645			td->qtd_buffer[0] =
1646			    htohc32(temp->sc, buf_res.physaddr);
1647			td->qtd_buffer_hi[0] = 0;
1648
1649			x = 1;
1650
1651			while (average > EHCI_PAGE_SIZE) {
1652				average -= EHCI_PAGE_SIZE;
1653				buf_offset += EHCI_PAGE_SIZE;
1654				usb2_get_page(temp->pc, buf_offset, &buf_res);
1655				td->qtd_buffer[x] =
1656				    htohc32(temp->sc,
1657				    buf_res.physaddr & (~0xFFF));
1658				td->qtd_buffer_hi[x] = 0;
1659				x++;
1660			}
1661
1662			/*
1663			 * NOTE: The "average" variable is never zero after
1664			 * exiting the loop above !
1665			 *
1666			 * NOTE: We have to subtract one from the offset to
1667			 * ensure that we are computing the physical address
1668			 * of a valid page !
1669			 */
1670			buf_offset += average;
1671			usb2_get_page(temp->pc, buf_offset - 1, &buf_res);
1672			td->qtd_buffer[x] =
1673			    htohc32(temp->sc,
1674			    buf_res.physaddr & (~0xFFF));
1675			td->qtd_buffer_hi[x] = 0;
1676		}
1677
1678		if (td_next) {
1679			/* link the current TD with the next one */
1680			td->qtd_next = td_next->qtd_self;
1681		}
1682		td->qtd_altnext = qtd_altnext;
1683		td->alt_next = td_alt_next;
1684
1685		usb2_pc_cpu_flush(td->page_cache);
1686	}
1687
1688	if (precompute) {
1689		precompute = 0;
1690
1691		/* setup alt next pointer, if any */
1692		if (temp->last_frame) {
1693			td_alt_next = NULL;
1694			qtd_altnext = terminate;
1695		} else {
1696			/* we use this field internally */
1697			td_alt_next = td_next;
1698			if (temp->setup_alt_next) {
1699				qtd_altnext = td_next->qtd_self;
1700			} else {
1701				qtd_altnext = terminate;
1702			}
1703		}
1704
1705		/* restore */
1706		temp->shortpkt = shortpkt_old;
1707		temp->len = len_old;
1708		goto restart;
1709	}
1710	temp->td = td;
1711	temp->td_next = td_next;
1712}
1713
1714static void
1715ehci_setup_standard_chain(struct usb2_xfer *xfer, ehci_qh_t **qh_last)
1716{
1717	struct ehci_std_temp temp;
1718	struct usb2_pipe_methods *methods;
1719	ehci_qh_t *qh;
1720	ehci_qtd_t *td;
1721	uint32_t qh_endp;
1722	uint32_t qh_endphub;
1723	uint32_t x;
1724
1725	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1726	    xfer->address, UE_GET_ADDR(xfer->endpoint),
1727	    xfer->sumlen, usb2_get_speed(xfer->xroot->udev));
1728
1729	temp.average = xfer->max_hc_frame_size;
1730	temp.max_frame_size = xfer->max_frame_size;
1731	temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
1732
1733	/* toggle the DMA set we are using */
1734	xfer->flags_int.curr_dma_set ^= 1;
1735
1736	/* get next DMA set */
1737	td = xfer->td_start[xfer->flags_int.curr_dma_set];
1738
1739	xfer->td_transfer_first = td;
1740	xfer->td_transfer_cache = td;
1741
1742	temp.td = NULL;
1743	temp.td_next = td;
1744	temp.qtd_status = 0;
1745	temp.last_frame = 0;
1746	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1747
1748	if (xfer->flags_int.control_xfr) {
1749		if (xfer->pipe->toggle_next) {
1750			/* DATA1 is next */
1751			temp.qtd_status |=
1752			    htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
1753		}
1754		temp.auto_data_toggle = 0;
1755	} else {
1756		temp.auto_data_toggle = 1;
1757	}
1758
1759	if (usb2_get_speed(xfer->xroot->udev) != USB_SPEED_HIGH) {
1760		/* max 3 retries */
1761		temp.qtd_status |=
1762		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1763	}
1764	/* check if we should prepend a setup message */
1765
1766	if (xfer->flags_int.control_xfr) {
1767		if (xfer->flags_int.control_hdr) {
1768
1769			temp.qtd_status &=
1770			    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1771			temp.qtd_status |= htohc32(temp.sc,
1772			    EHCI_QTD_ACTIVE |
1773			    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
1774			    EHCI_QTD_SET_TOGGLE(0));
1775
1776			temp.len = xfer->frlengths[0];
1777			temp.pc = xfer->frbuffers + 0;
1778			temp.shortpkt = temp.len ? 1 : 0;
1779			/* check for last frame */
1780			if (xfer->nframes == 1) {
1781				/* no STATUS stage yet, SETUP is last */
1782				if (xfer->flags_int.control_act) {
1783					temp.last_frame = 1;
1784					temp.setup_alt_next = 0;
1785				}
1786			}
1787			ehci_setup_standard_chain_sub(&temp);
1788		}
1789		x = 1;
1790	} else {
1791		x = 0;
1792	}
1793
1794	while (x != xfer->nframes) {
1795
1796		/* DATA0 / DATA1 message */
1797
1798		temp.len = xfer->frlengths[x];
1799		temp.pc = xfer->frbuffers + x;
1800
1801		x++;
1802
1803		if (x == xfer->nframes) {
1804			if (xfer->flags_int.control_xfr) {
1805				/* no STATUS stage yet, DATA is last */
1806				if (xfer->flags_int.control_act) {
1807					temp.last_frame = 1;
1808					temp.setup_alt_next = 0;
1809				}
1810			} else {
1811				temp.last_frame = 1;
1812				temp.setup_alt_next = 0;
1813			}
1814		}
1815		/* keep previous data toggle and error count */
1816
1817		temp.qtd_status &=
1818		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1819		    EHCI_QTD_SET_TOGGLE(1));
1820
1821		if (temp.len == 0) {
1822
1823			/* make sure that we send an USB packet */
1824
1825			temp.shortpkt = 0;
1826
1827		} else {
1828
1829			/* regular data transfer */
1830
1831			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1832		}
1833
1834		/* set endpoint direction */
1835
1836		temp.qtd_status |=
1837		    (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) ?
1838		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
1839		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1840		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
1841		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
1842
1843		ehci_setup_standard_chain_sub(&temp);
1844	}
1845
1846	/* check if we should append a status stage */
1847
1848	if (xfer->flags_int.control_xfr &&
1849	    !xfer->flags_int.control_act) {
1850
1851		/*
1852		 * Send a DATA1 message and invert the current endpoint
1853		 * direction.
1854		 */
1855
1856		temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
1857		    EHCI_QTD_SET_TOGGLE(1));
1858		temp.qtd_status |=
1859		    (UE_GET_DIR(xfer->endpoint) == UE_DIR_OUT) ?
1860		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
1861		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
1862		    EHCI_QTD_SET_TOGGLE(1)) :
1863		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
1864		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
1865		    EHCI_QTD_SET_TOGGLE(1));
1866
1867		temp.len = 0;
1868		temp.pc = NULL;
1869		temp.shortpkt = 0;
1870		temp.last_frame = 1;
1871		temp.setup_alt_next = 0;
1872
1873		ehci_setup_standard_chain_sub(&temp);
1874	}
1875	td = temp.td;
1876
1877	/* the last TD terminates the transfer: */
1878	td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1879	td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1880	td->qtd_status |= htohc32(temp.sc, EHCI_QTD_IOC);
1881
1882	usb2_pc_cpu_flush(td->page_cache);
1883
1884	/* must have at least one frame! */
1885
1886	xfer->td_transfer_last = td;
1887
1888#if USB_DEBUG
1889	if (ehcidebug > 8) {
1890		DPRINTF("nexttog=%d; data before transfer:\n",
1891		    xfer->pipe->toggle_next);
1892		ehci_dump_sqtds(temp.sc,
1893		    xfer->td_transfer_first);
1894	}
1895#endif
1896
1897	methods = xfer->pipe->methods;
1898
1899	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1900
1901	/* the "qh_link" field is filled when the QH is added */
1902
1903	qh_endp =
1904	    (EHCI_QH_SET_ADDR(xfer->address) |
1905	    EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpoint)) |
1906	    EHCI_QH_SET_MPL(xfer->max_packet_size));
1907
1908	if (usb2_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
1909		qh_endp |= (EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) |
1910		    EHCI_QH_DTC);
1911		if (methods != &ehci_device_intr_methods)
1912			qh_endp |= EHCI_QH_SET_NRL(8);
1913	} else {
1914
1915		if (usb2_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
1916			qh_endp |= (EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL) |
1917			    EHCI_QH_DTC);
1918		} else {
1919			qh_endp |= (EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW) |
1920			    EHCI_QH_DTC);
1921		}
1922
1923		if (methods == &ehci_device_ctrl_methods) {
1924			qh_endp |= EHCI_QH_CTL;
1925		}
1926		if (methods != &ehci_device_intr_methods) {
1927			/* Only try one time per microframe! */
1928			qh_endp |= EHCI_QH_SET_NRL(1);
1929		}
1930	}
1931
1932	qh->qh_endp = htohc32(temp.sc, qh_endp);
1933
1934	qh_endphub =
1935	    (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
1936	    EHCI_QH_SET_CMASK(xfer->usb2_cmask) |
1937	    EHCI_QH_SET_SMASK(xfer->usb2_smask) |
1938	    EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
1939	    EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
1940
1941	qh->qh_endphub = htohc32(temp.sc, qh_endphub);
1942	qh->qh_curqtd = htohc32(temp.sc, 0);
1943
1944	/* fill the overlay qTD */
1945	qh->qh_qtd.qtd_status = htohc32(temp.sc, 0);
1946
1947	if (temp.auto_data_toggle) {
1948
1949		/* let the hardware compute the data toggle */
1950
1951		qh->qh_endp &= htohc32(temp.sc, ~EHCI_QH_DTC);
1952
1953		if (xfer->pipe->toggle_next) {
1954			/* DATA1 is next */
1955			qh->qh_qtd.qtd_status |=
1956			    htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
1957		}
1958	}
1959	td = xfer->td_transfer_first;
1960
1961	qh->qh_qtd.qtd_next = td->qtd_self;
1962	qh->qh_qtd.qtd_altnext =
1963	    htohc32(temp.sc, EHCI_LINK_TERMINATE);
1964
1965	usb2_pc_cpu_flush(qh->page_cache);
1966
1967	if (xfer->xroot->udev->flags.self_suspended == 0) {
1968		EHCI_APPEND_QH(qh, *qh_last);
1969	}
1970}
1971
1972static void
1973ehci_root_intr(ehci_softc_t *sc)
1974{
1975	uint16_t i;
1976	uint16_t m;
1977
1978	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1979
1980	/* clear any old interrupt data */
1981	memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
1982
1983	/* set bits */
1984	m = (sc->sc_noport + 1);
1985	if (m > (8 * sizeof(sc->sc_hub_idata))) {
1986		m = (8 * sizeof(sc->sc_hub_idata));
1987	}
1988	for (i = 1; i < m; i++) {
1989		/* pick out CHANGE bits from the status register */
1990		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
1991			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
1992			DPRINTF("port %d changed\n", i);
1993		}
1994	}
1995	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
1996	    sizeof(sc->sc_hub_idata));
1997}
1998
1999static void
2000ehci_isoc_fs_done(ehci_softc_t *sc, struct usb2_xfer *xfer)
2001{
2002	uint32_t nframes = xfer->nframes;
2003	uint32_t status;
2004	uint32_t *plen = xfer->frlengths;
2005	uint16_t len = 0;
2006	ehci_sitd_t *td = xfer->td_transfer_first;
2007	ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
2008
2009	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
2010	    xfer, xfer->pipe);
2011
2012	while (nframes--) {
2013		if (td == NULL) {
2014			panic("%s:%d: out of TD's\n",
2015			    __FUNCTION__, __LINE__);
2016		}
2017		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2018			pp_last = &sc->sc_isoc_fs_p_last[0];
2019		}
2020#if USB_DEBUG
2021		if (ehcidebug > 15) {
2022			DPRINTF("isoc FS-TD\n");
2023			ehci_dump_sitd(sc, td);
2024		}
2025#endif
2026		usb2_pc_cpu_invalidate(td->page_cache);
2027		status = hc32toh(sc, td->sitd_status);
2028
2029		len = EHCI_SITD_GET_LEN(status);
2030
2031		DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2032
2033		if (*plen >= len) {
2034			len = *plen - len;
2035		} else {
2036			len = 0;
2037		}
2038
2039		*plen = len;
2040
2041		/* remove FS-TD from schedule */
2042		EHCI_REMOVE_FS_TD(td, *pp_last);
2043
2044		pp_last++;
2045		plen++;
2046		td = td->obj_next;
2047	}
2048
2049	xfer->aframes = xfer->nframes;
2050}
2051
2052static void
2053ehci_isoc_hs_done(ehci_softc_t *sc, struct usb2_xfer *xfer)
2054{
2055	uint32_t nframes = xfer->nframes;
2056	uint32_t status;
2057	uint32_t *plen = xfer->frlengths;
2058	uint16_t len = 0;
2059	uint8_t td_no = 0;
2060	ehci_itd_t *td = xfer->td_transfer_first;
2061	ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
2062
2063	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
2064	    xfer, xfer->pipe);
2065
2066	while (nframes--) {
2067		if (td == NULL) {
2068			panic("%s:%d: out of TD's\n",
2069			    __FUNCTION__, __LINE__);
2070		}
2071		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2072			pp_last = &sc->sc_isoc_hs_p_last[0];
2073		}
2074#if USB_DEBUG
2075		if (ehcidebug > 15) {
2076			DPRINTF("isoc HS-TD\n");
2077			ehci_dump_itd(sc, td);
2078		}
2079#endif
2080
2081		usb2_pc_cpu_invalidate(td->page_cache);
2082		status = hc32toh(sc, td->itd_status[td_no]);
2083
2084		len = EHCI_ITD_GET_LEN(status);
2085
2086		DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2087
2088		if (*plen >= len) {
2089			/*
2090			 * The length is valid. NOTE: The complete
2091			 * length is written back into the status
2092			 * field, and not the remainder like with
2093			 * other transfer descriptor types.
2094			 */
2095		} else {
2096			/* Invalid length - truncate */
2097			len = 0;
2098		}
2099
2100		*plen = len;
2101
2102		plen++;
2103		td_no++;
2104
2105		if ((td_no == 8) || (nframes == 0)) {
2106			/* remove HS-TD from schedule */
2107			EHCI_REMOVE_HS_TD(td, *pp_last);
2108			pp_last++;
2109
2110			td_no = 0;
2111			td = td->obj_next;
2112		}
2113	}
2114	xfer->aframes = xfer->nframes;
2115}
2116
2117/* NOTE: "done" can be run two times in a row,
2118 * from close and from interrupt
2119 */
2120static void
2121ehci_device_done(struct usb2_xfer *xfer, usb2_error_t error)
2122{
2123	struct usb2_pipe_methods *methods = xfer->pipe->methods;
2124	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2125
2126	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2127
2128	DPRINTFN(2, "xfer=%p, pipe=%p, error=%d\n",
2129	    xfer, xfer->pipe, error);
2130
2131	if ((methods == &ehci_device_bulk_methods) ||
2132	    (methods == &ehci_device_ctrl_methods)) {
2133#if USB_DEBUG
2134		if (ehcidebug > 8) {
2135			DPRINTF("nexttog=%d; data after transfer:\n",
2136			    xfer->pipe->toggle_next);
2137			ehci_dump_sqtds(sc,
2138			    xfer->td_transfer_first);
2139		}
2140#endif
2141
2142		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2143		    sc->sc_async_p_last);
2144	}
2145	if (methods == &ehci_device_intr_methods) {
2146		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
2147		    sc->sc_intr_p_last[xfer->qh_pos]);
2148	}
2149	/*
2150	 * Only finish isochronous transfers once which will update
2151	 * "xfer->frlengths".
2152	 */
2153	if (xfer->td_transfer_first &&
2154	    xfer->td_transfer_last) {
2155		if (methods == &ehci_device_isoc_fs_methods) {
2156			ehci_isoc_fs_done(sc, xfer);
2157		}
2158		if (methods == &ehci_device_isoc_hs_methods) {
2159			ehci_isoc_hs_done(sc, xfer);
2160		}
2161		xfer->td_transfer_first = NULL;
2162		xfer->td_transfer_last = NULL;
2163	}
2164	/* dequeue transfer and start next transfer */
2165	usb2_transfer_done(xfer, error);
2166}
2167
2168/*------------------------------------------------------------------------*
2169 * ehci bulk support
2170 *------------------------------------------------------------------------*/
2171static void
2172ehci_device_bulk_open(struct usb2_xfer *xfer)
2173{
2174	return;
2175}
2176
2177static void
2178ehci_device_bulk_close(struct usb2_xfer *xfer)
2179{
2180	ehci_device_done(xfer, USB_ERR_CANCELLED);
2181}
2182
2183static void
2184ehci_device_bulk_enter(struct usb2_xfer *xfer)
2185{
2186	return;
2187}
2188
2189static void
2190ehci_device_bulk_start(struct usb2_xfer *xfer)
2191{
2192	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2193	uint32_t temp;
2194
2195	/* setup TD's and QH */
2196	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2197
2198	/* put transfer on interrupt queue */
2199	ehci_transfer_intr_enqueue(xfer);
2200
2201	/* XXX Performance quirk: Some Host Controllers have a too low
2202	 * interrupt rate. Issue an IAAD to stimulate the Host
2203	 * Controller after queueing the BULK transfer.
2204	 */
2205	temp = EOREAD4(sc, EHCI_USBCMD);
2206	if (!(temp & EHCI_CMD_IAAD))
2207		EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
2208}
2209
2210struct usb2_pipe_methods ehci_device_bulk_methods =
2211{
2212	.open = ehci_device_bulk_open,
2213	.close = ehci_device_bulk_close,
2214	.enter = ehci_device_bulk_enter,
2215	.start = ehci_device_bulk_start,
2216};
2217
2218/*------------------------------------------------------------------------*
2219 * ehci control support
2220 *------------------------------------------------------------------------*/
2221static void
2222ehci_device_ctrl_open(struct usb2_xfer *xfer)
2223{
2224	return;
2225}
2226
2227static void
2228ehci_device_ctrl_close(struct usb2_xfer *xfer)
2229{
2230	ehci_device_done(xfer, USB_ERR_CANCELLED);
2231}
2232
2233static void
2234ehci_device_ctrl_enter(struct usb2_xfer *xfer)
2235{
2236	return;
2237}
2238
2239static void
2240ehci_device_ctrl_start(struct usb2_xfer *xfer)
2241{
2242	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2243
2244	/* setup TD's and QH */
2245	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
2246
2247	/* put transfer on interrupt queue */
2248	ehci_transfer_intr_enqueue(xfer);
2249}
2250
2251struct usb2_pipe_methods ehci_device_ctrl_methods =
2252{
2253	.open = ehci_device_ctrl_open,
2254	.close = ehci_device_ctrl_close,
2255	.enter = ehci_device_ctrl_enter,
2256	.start = ehci_device_ctrl_start,
2257};
2258
2259/*------------------------------------------------------------------------*
2260 * ehci interrupt support
2261 *------------------------------------------------------------------------*/
2262static void
2263ehci_device_intr_open(struct usb2_xfer *xfer)
2264{
2265	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2266	uint16_t best;
2267	uint16_t bit;
2268	uint16_t x;
2269	uint8_t slot;
2270
2271	/* Allocate a microframe slot first: */
2272
2273	slot = usb2_intr_schedule_adjust
2274	    (xfer->xroot->udev, xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX);
2275
2276	if (usb2_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
2277		xfer->usb2_uframe = slot;
2278		xfer->usb2_smask = (1 << slot) & 0xFF;
2279		xfer->usb2_cmask = 0;
2280	} else {
2281		xfer->usb2_uframe = slot;
2282		xfer->usb2_smask = (1 << slot) & 0x3F;
2283		xfer->usb2_cmask = (-(4 << slot)) & 0xFE;
2284	}
2285
2286	/*
2287	 * Find the best QH position corresponding to the given interval:
2288	 */
2289
2290	best = 0;
2291	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
2292	while (bit) {
2293		if (xfer->interval >= bit) {
2294			x = bit;
2295			best = bit;
2296			while (x & bit) {
2297				if (sc->sc_intr_stat[x] <
2298				    sc->sc_intr_stat[best]) {
2299					best = x;
2300				}
2301				x++;
2302			}
2303			break;
2304		}
2305		bit >>= 1;
2306	}
2307
2308	sc->sc_intr_stat[best]++;
2309	xfer->qh_pos = best;
2310
2311	DPRINTFN(3, "best=%d interval=%d\n",
2312	    best, xfer->interval);
2313}
2314
2315static void
2316ehci_device_intr_close(struct usb2_xfer *xfer)
2317{
2318	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2319	uint8_t slot;
2320
2321	slot = usb2_intr_schedule_adjust
2322	    (xfer->xroot->udev, -(xfer->max_frame_size), xfer->usb2_uframe);
2323
2324	sc->sc_intr_stat[xfer->qh_pos]--;
2325
2326	ehci_device_done(xfer, USB_ERR_CANCELLED);
2327}
2328
2329static void
2330ehci_device_intr_enter(struct usb2_xfer *xfer)
2331{
2332	return;
2333}
2334
2335static void
2336ehci_device_intr_start(struct usb2_xfer *xfer)
2337{
2338	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2339
2340	/* setup TD's and QH */
2341	ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
2342
2343	/* put transfer on interrupt queue */
2344	ehci_transfer_intr_enqueue(xfer);
2345}
2346
2347struct usb2_pipe_methods ehci_device_intr_methods =
2348{
2349	.open = ehci_device_intr_open,
2350	.close = ehci_device_intr_close,
2351	.enter = ehci_device_intr_enter,
2352	.start = ehci_device_intr_start,
2353};
2354
2355/*------------------------------------------------------------------------*
2356 * ehci full speed isochronous support
2357 *------------------------------------------------------------------------*/
2358static void
2359ehci_device_isoc_fs_open(struct usb2_xfer *xfer)
2360{
2361	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2362	ehci_sitd_t *td;
2363	uint32_t sitd_portaddr;
2364	uint8_t ds;
2365
2366	sitd_portaddr =
2367	    EHCI_SITD_SET_ADDR(xfer->address) |
2368	    EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpoint)) |
2369	    EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
2370	    EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
2371
2372	if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) {
2373		sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
2374	}
2375	sitd_portaddr = htohc32(sc, sitd_portaddr);
2376
2377	/* initialize all TD's */
2378
2379	for (ds = 0; ds != 2; ds++) {
2380
2381		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2382
2383			td->sitd_portaddr = sitd_portaddr;
2384
2385			/*
2386			 * TODO: make some kind of automatic
2387			 * SMASK/CMASK selection based on micro-frame
2388			 * usage
2389			 *
2390			 * micro-frame usage (8 microframes per 1ms)
2391			 */
2392			td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
2393
2394			usb2_pc_cpu_flush(td->page_cache);
2395		}
2396	}
2397}
2398
2399static void
2400ehci_device_isoc_fs_close(struct usb2_xfer *xfer)
2401{
2402	ehci_device_done(xfer, USB_ERR_CANCELLED);
2403}
2404
2405static void
2406ehci_device_isoc_fs_enter(struct usb2_xfer *xfer)
2407{
2408	struct usb2_page_search buf_res;
2409	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2410	struct usb2_fs_isoc_schedule *fss_start;
2411	struct usb2_fs_isoc_schedule *fss_end;
2412	struct usb2_fs_isoc_schedule *fss;
2413	ehci_sitd_t *td;
2414	ehci_sitd_t *td_last = NULL;
2415	ehci_sitd_t **pp_last;
2416	uint32_t *plen;
2417	uint32_t buf_offset;
2418	uint32_t nframes;
2419	uint32_t temp;
2420	uint32_t sitd_mask;
2421	uint16_t tlen;
2422	uint8_t sa;
2423	uint8_t sb;
2424	uint8_t error;
2425
2426#if USB_DEBUG
2427	uint8_t once = 1;
2428
2429#endif
2430
2431	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2432	    xfer, xfer->pipe->isoc_next, xfer->nframes);
2433
2434	/* get the current frame index */
2435
2436	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2437
2438	/*
2439	 * check if the frame index is within the window where the frames
2440	 * will be inserted
2441	 */
2442	buf_offset = (nframes - xfer->pipe->isoc_next) &
2443	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2444
2445	if ((xfer->pipe->is_synced == 0) ||
2446	    (buf_offset < xfer->nframes)) {
2447		/*
2448		 * If there is data underflow or the pipe queue is empty we
2449		 * schedule the transfer a few frames ahead of the current
2450		 * frame position. Else two isochronous transfers might
2451		 * overlap.
2452		 */
2453		xfer->pipe->isoc_next = (nframes + 3) &
2454		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2455		xfer->pipe->is_synced = 1;
2456		DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
2457	}
2458	/*
2459	 * compute how many milliseconds the insertion is ahead of the
2460	 * current frame position:
2461	 */
2462	buf_offset = (xfer->pipe->isoc_next - nframes) &
2463	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2464
2465	/*
2466	 * pre-compute when the isochronous transfer will be finished:
2467	 */
2468	xfer->isoc_time_complete =
2469	    usb2_fs_isoc_schedule_isoc_time_expand
2470	    (xfer->xroot->udev, &fss_start, &fss_end, nframes) + buf_offset +
2471	    xfer->nframes;
2472
2473	/* get the real number of frames */
2474
2475	nframes = xfer->nframes;
2476
2477	buf_offset = 0;
2478
2479	plen = xfer->frlengths;
2480
2481	/* toggle the DMA set we are using */
2482	xfer->flags_int.curr_dma_set ^= 1;
2483
2484	/* get next DMA set */
2485	td = xfer->td_start[xfer->flags_int.curr_dma_set];
2486	xfer->td_transfer_first = td;
2487
2488	pp_last = &sc->sc_isoc_fs_p_last[xfer->pipe->isoc_next];
2489
2490	/* store starting position */
2491
2492	xfer->qh_pos = xfer->pipe->isoc_next;
2493
2494	fss = fss_start + (xfer->qh_pos % USB_ISOC_TIME_MAX);
2495
2496	while (nframes--) {
2497		if (td == NULL) {
2498			panic("%s:%d: out of TD's\n",
2499			    __FUNCTION__, __LINE__);
2500		}
2501		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2502			pp_last = &sc->sc_isoc_fs_p_last[0];
2503		}
2504		if (fss >= fss_end) {
2505			fss = fss_start;
2506		}
2507		/* reuse sitd_portaddr and sitd_back from last transfer */
2508
2509		if (*plen > xfer->max_frame_size) {
2510#if USB_DEBUG
2511			if (once) {
2512				once = 0;
2513				printf("%s: frame length(%d) exceeds %d "
2514				    "bytes (frame truncated)\n",
2515				    __FUNCTION__, *plen,
2516				    xfer->max_frame_size);
2517			}
2518#endif
2519			*plen = xfer->max_frame_size;
2520		}
2521		/*
2522		 * We currently don't care if the ISOCHRONOUS schedule is
2523		 * full!
2524		 */
2525		error = usb2_fs_isoc_schedule_alloc(fss, &sa, *plen);
2526		if (error) {
2527			/*
2528			 * The FULL speed schedule is FULL! Set length
2529			 * to zero.
2530			 */
2531			*plen = 0;
2532		}
2533		if (*plen) {
2534			/*
2535			 * only call "usb2_get_page()" when we have a
2536			 * non-zero length
2537			 */
2538			usb2_get_page(xfer->frbuffers, buf_offset, &buf_res);
2539			td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
2540			buf_offset += *plen;
2541			/*
2542			 * NOTE: We need to subtract one from the offset so
2543			 * that we are on a valid page!
2544			 */
2545			usb2_get_page(xfer->frbuffers, buf_offset - 1,
2546			    &buf_res);
2547			temp = buf_res.physaddr & ~0xFFF;
2548		} else {
2549			td->sitd_bp[0] = 0;
2550			temp = 0;
2551		}
2552
2553		if (UE_GET_DIR(xfer->endpoint) == UE_DIR_OUT) {
2554			tlen = *plen;
2555			if (tlen <= 188) {
2556				temp |= 1;	/* T-count = 1, TP = ALL */
2557				tlen = 1;
2558			} else {
2559				tlen += 187;
2560				tlen /= 188;
2561				temp |= tlen;	/* T-count = [1..6] */
2562				temp |= 8;	/* TP = Begin */
2563			}
2564
2565			tlen += sa;
2566
2567			if (tlen >= 8) {
2568				sb = 0;
2569			} else {
2570				sb = (1 << tlen);
2571			}
2572
2573			sa = (1 << sa);
2574			sa = (sb - sa) & 0x3F;
2575			sb = 0;
2576		} else {
2577			sb = (-(4 << sa)) & 0xFE;
2578			sa = (1 << sa) & 0x3F;
2579		}
2580
2581		sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
2582		    EHCI_SITD_SET_CMASK(sb));
2583
2584		td->sitd_bp[1] = htohc32(sc, temp);
2585
2586		td->sitd_mask = htohc32(sc, sitd_mask);
2587
2588		if (nframes == 0) {
2589			td->sitd_status = htohc32(sc,
2590			    EHCI_SITD_IOC |
2591			    EHCI_SITD_ACTIVE |
2592			    EHCI_SITD_SET_LEN(*plen));
2593		} else {
2594			td->sitd_status = htohc32(sc,
2595			    EHCI_SITD_ACTIVE |
2596			    EHCI_SITD_SET_LEN(*plen));
2597		}
2598		usb2_pc_cpu_flush(td->page_cache);
2599
2600#if USB_DEBUG
2601		if (ehcidebug > 15) {
2602			DPRINTF("FS-TD %d\n", nframes);
2603			ehci_dump_sitd(sc, td);
2604		}
2605#endif
2606		/* insert TD into schedule */
2607		EHCI_APPEND_FS_TD(td, *pp_last);
2608		pp_last++;
2609
2610		plen++;
2611		fss++;
2612		td_last = td;
2613		td = td->obj_next;
2614	}
2615
2616	xfer->td_transfer_last = td_last;
2617
2618	/* update isoc_next */
2619	xfer->pipe->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
2620	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2621}
2622
2623static void
2624ehci_device_isoc_fs_start(struct usb2_xfer *xfer)
2625{
2626	/* put transfer on interrupt queue */
2627	ehci_transfer_intr_enqueue(xfer);
2628}
2629
2630struct usb2_pipe_methods ehci_device_isoc_fs_methods =
2631{
2632	.open = ehci_device_isoc_fs_open,
2633	.close = ehci_device_isoc_fs_close,
2634	.enter = ehci_device_isoc_fs_enter,
2635	.start = ehci_device_isoc_fs_start,
2636};
2637
2638/*------------------------------------------------------------------------*
2639 * ehci high speed isochronous support
2640 *------------------------------------------------------------------------*/
2641static void
2642ehci_device_isoc_hs_open(struct usb2_xfer *xfer)
2643{
2644	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2645	ehci_itd_t *td;
2646	uint32_t temp;
2647	uint8_t ds;
2648
2649	/* initialize all TD's */
2650
2651	for (ds = 0; ds != 2; ds++) {
2652
2653		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2654
2655			/* set TD inactive */
2656			td->itd_status[0] = 0;
2657			td->itd_status[1] = 0;
2658			td->itd_status[2] = 0;
2659			td->itd_status[3] = 0;
2660			td->itd_status[4] = 0;
2661			td->itd_status[5] = 0;
2662			td->itd_status[6] = 0;
2663			td->itd_status[7] = 0;
2664
2665			/* set endpoint and address */
2666			td->itd_bp[0] = htohc32(sc,
2667			    EHCI_ITD_SET_ADDR(xfer->address) |
2668			    EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpoint)));
2669
2670			temp =
2671			    EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
2672
2673			/* set direction */
2674			if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) {
2675				temp |= EHCI_ITD_SET_DIR_IN;
2676			}
2677			/* set maximum packet size */
2678			td->itd_bp[1] = htohc32(sc, temp);
2679
2680			/* set transfer multiplier */
2681			td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
2682
2683			usb2_pc_cpu_flush(td->page_cache);
2684		}
2685	}
2686}
2687
2688static void
2689ehci_device_isoc_hs_close(struct usb2_xfer *xfer)
2690{
2691	ehci_device_done(xfer, USB_ERR_CANCELLED);
2692}
2693
2694static void
2695ehci_device_isoc_hs_enter(struct usb2_xfer *xfer)
2696{
2697	struct usb2_page_search buf_res;
2698	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2699	ehci_itd_t *td;
2700	ehci_itd_t *td_last = NULL;
2701	ehci_itd_t **pp_last;
2702	bus_size_t page_addr;
2703	uint32_t *plen;
2704	uint32_t status;
2705	uint32_t buf_offset;
2706	uint32_t nframes;
2707	uint32_t itd_offset[8 + 1];
2708	uint8_t x;
2709	uint8_t td_no;
2710	uint8_t page_no;
2711
2712#if USB_DEBUG
2713	uint8_t once = 1;
2714
2715#endif
2716
2717	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2718	    xfer, xfer->pipe->isoc_next, xfer->nframes);
2719
2720	/* get the current frame index */
2721
2722	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
2723
2724	/*
2725	 * check if the frame index is within the window where the frames
2726	 * will be inserted
2727	 */
2728	buf_offset = (nframes - xfer->pipe->isoc_next) &
2729	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2730
2731	if ((xfer->pipe->is_synced == 0) ||
2732	    (buf_offset < ((xfer->nframes + 7) / 8))) {
2733		/*
2734		 * If there is data underflow or the pipe queue is empty we
2735		 * schedule the transfer a few frames ahead of the current
2736		 * frame position. Else two isochronous transfers might
2737		 * overlap.
2738		 */
2739		xfer->pipe->isoc_next = (nframes + 3) &
2740		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2741		xfer->pipe->is_synced = 1;
2742		DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
2743	}
2744	/*
2745	 * compute how many milliseconds the insertion is ahead of the
2746	 * current frame position:
2747	 */
2748	buf_offset = (xfer->pipe->isoc_next - nframes) &
2749	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2750
2751	/*
2752	 * pre-compute when the isochronous transfer will be finished:
2753	 */
2754	xfer->isoc_time_complete =
2755	    usb2_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
2756	    ((xfer->nframes + 7) / 8);
2757
2758	/* get the real number of frames */
2759
2760	nframes = xfer->nframes;
2761
2762	buf_offset = 0;
2763	td_no = 0;
2764
2765	plen = xfer->frlengths;
2766
2767	/* toggle the DMA set we are using */
2768	xfer->flags_int.curr_dma_set ^= 1;
2769
2770	/* get next DMA set */
2771	td = xfer->td_start[xfer->flags_int.curr_dma_set];
2772	xfer->td_transfer_first = td;
2773
2774	pp_last = &sc->sc_isoc_hs_p_last[xfer->pipe->isoc_next];
2775
2776	/* store starting position */
2777
2778	xfer->qh_pos = xfer->pipe->isoc_next;
2779
2780	while (nframes--) {
2781		if (td == NULL) {
2782			panic("%s:%d: out of TD's\n",
2783			    __FUNCTION__, __LINE__);
2784		}
2785		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
2786			pp_last = &sc->sc_isoc_hs_p_last[0];
2787		}
2788		/* range check */
2789		if (*plen > xfer->max_frame_size) {
2790#if USB_DEBUG
2791			if (once) {
2792				once = 0;
2793				printf("%s: frame length(%d) exceeds %d bytes "
2794				    "(frame truncated)\n",
2795				    __FUNCTION__, *plen, xfer->max_frame_size);
2796			}
2797#endif
2798			*plen = xfer->max_frame_size;
2799		}
2800		status = (EHCI_ITD_SET_LEN(*plen) |
2801		    EHCI_ITD_ACTIVE |
2802		    EHCI_ITD_SET_PG(0));
2803		td->itd_status[td_no] = htohc32(sc, status);
2804		itd_offset[td_no] = buf_offset;
2805		buf_offset += *plen;
2806		plen++;
2807		td_no++;
2808
2809		if ((td_no == 8) || (nframes == 0)) {
2810
2811			/* the rest of the transfers are not active, if any */
2812			for (x = td_no; x != 8; x++) {
2813				td->itd_status[x] = 0;	/* not active */
2814			}
2815
2816			/* check if there is any data to be transferred */
2817			if (itd_offset[0] != buf_offset) {
2818				page_no = 0;
2819				itd_offset[td_no] = buf_offset;
2820
2821				/* get first page offset */
2822				usb2_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
2823				/* get page address */
2824				page_addr = buf_res.physaddr & ~0xFFF;
2825				/* update page address */
2826				td->itd_bp[0] &= htohc32(sc, 0xFFF);
2827				td->itd_bp[0] |= htohc32(sc, page_addr);
2828
2829				for (x = 0; x != td_no; x++) {
2830					/* set page number and page offset */
2831					status = (EHCI_ITD_SET_PG(page_no) |
2832					    (buf_res.physaddr & 0xFFF));
2833					td->itd_status[x] |= htohc32(sc, status);
2834
2835					/* get next page offset */
2836					if (itd_offset[x + 1] == buf_offset) {
2837						/*
2838						 * We subtract one so that
2839						 * we don't go off the last
2840						 * page!
2841						 */
2842						usb2_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
2843					} else {
2844						usb2_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
2845					}
2846
2847					/* check if we need a new page */
2848					if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
2849						/* new page needed */
2850						page_addr = buf_res.physaddr & ~0xFFF;
2851						if (page_no == 6) {
2852							panic("%s: too many pages\n", __FUNCTION__);
2853						}
2854						page_no++;
2855						/* update page address */
2856						td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2857						td->itd_bp[page_no] |= htohc32(sc, page_addr);
2858					}
2859				}
2860			}
2861			/* set IOC bit if we are complete */
2862			if (nframes == 0) {
2863				td->itd_status[7] |= htohc32(sc, EHCI_ITD_IOC);
2864			}
2865			usb2_pc_cpu_flush(td->page_cache);
2866#if USB_DEBUG
2867			if (ehcidebug > 15) {
2868				DPRINTF("HS-TD %d\n", nframes);
2869				ehci_dump_itd(sc, td);
2870			}
2871#endif
2872			/* insert TD into schedule */
2873			EHCI_APPEND_HS_TD(td, *pp_last);
2874			pp_last++;
2875
2876			td_no = 0;
2877			td_last = td;
2878			td = td->obj_next;
2879		}
2880	}
2881
2882	xfer->td_transfer_last = td_last;
2883
2884	/* update isoc_next */
2885	xfer->pipe->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
2886	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2887}
2888
2889static void
2890ehci_device_isoc_hs_start(struct usb2_xfer *xfer)
2891{
2892	/* put transfer on interrupt queue */
2893	ehci_transfer_intr_enqueue(xfer);
2894}
2895
2896struct usb2_pipe_methods ehci_device_isoc_hs_methods =
2897{
2898	.open = ehci_device_isoc_hs_open,
2899	.close = ehci_device_isoc_hs_close,
2900	.enter = ehci_device_isoc_hs_enter,
2901	.start = ehci_device_isoc_hs_start,
2902};
2903
2904/*------------------------------------------------------------------------*
2905 * ehci root control support
2906 *------------------------------------------------------------------------*
2907 * Simulate a hardware hub by handling all the necessary requests.
2908 *------------------------------------------------------------------------*/
2909
2910static const
2911struct usb2_device_descriptor ehci_devd =
2912{
2913	sizeof(struct usb2_device_descriptor),
2914	UDESC_DEVICE,			/* type */
2915	{0x00, 0x02},			/* USB version */
2916	UDCLASS_HUB,			/* class */
2917	UDSUBCLASS_HUB,			/* subclass */
2918	UDPROTO_HSHUBSTT,		/* protocol */
2919	64,				/* max packet */
2920	{0}, {0}, {0x00, 0x01},		/* device id */
2921	1, 2, 0,			/* string indicies */
2922	1				/* # of configurations */
2923};
2924
2925static const
2926struct usb2_device_qualifier ehci_odevd =
2927{
2928	sizeof(struct usb2_device_qualifier),
2929	UDESC_DEVICE_QUALIFIER,		/* type */
2930	{0x00, 0x02},			/* USB version */
2931	UDCLASS_HUB,			/* class */
2932	UDSUBCLASS_HUB,			/* subclass */
2933	UDPROTO_FSHUB,			/* protocol */
2934	0,				/* max packet */
2935	0,				/* # of configurations */
2936	0
2937};
2938
2939static const struct ehci_config_desc ehci_confd = {
2940	.confd = {
2941		.bLength = sizeof(struct usb2_config_descriptor),
2942		.bDescriptorType = UDESC_CONFIG,
2943		.wTotalLength[0] = sizeof(ehci_confd),
2944		.bNumInterface = 1,
2945		.bConfigurationValue = 1,
2946		.iConfiguration = 0,
2947		.bmAttributes = UC_SELF_POWERED,
2948		.bMaxPower = 0		/* max power */
2949	},
2950	.ifcd = {
2951		.bLength = sizeof(struct usb2_interface_descriptor),
2952		.bDescriptorType = UDESC_INTERFACE,
2953		.bNumEndpoints = 1,
2954		.bInterfaceClass = UICLASS_HUB,
2955		.bInterfaceSubClass = UISUBCLASS_HUB,
2956		.bInterfaceProtocol = UIPROTO_HSHUBSTT,
2957		0
2958	},
2959	.endpd = {
2960		.bLength = sizeof(struct usb2_endpoint_descriptor),
2961		.bDescriptorType = UDESC_ENDPOINT,
2962		.bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
2963		.bmAttributes = UE_INTERRUPT,
2964		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
2965		.bInterval = 255,
2966	},
2967};
2968
2969static const
2970struct usb2_hub_descriptor ehci_hubd =
2971{
2972	0,				/* dynamic length */
2973	UDESC_HUB,
2974	0,
2975	{0, 0},
2976	0,
2977	0,
2978	{0},
2979};
2980
2981static void
2982ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
2983{
2984	uint32_t port;
2985	uint32_t v;
2986
2987	DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
2988
2989	port = EHCI_PORTSC(index);
2990	v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
2991	EOWRITE4(sc, port, v | EHCI_PS_PO);
2992}
2993
2994static usb2_error_t
2995ehci_roothub_exec(struct usb2_device *udev,
2996    struct usb2_device_request *req, const void **pptr, uint16_t *plength)
2997{
2998	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
2999	const char *str_ptr;
3000	const void *ptr;
3001	uint32_t port;
3002	uint32_t v;
3003	uint16_t len;
3004	uint16_t i;
3005	uint16_t value;
3006	uint16_t index;
3007	uint8_t l;
3008	usb2_error_t err;
3009
3010	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3011
3012	/* buffer reset */
3013	ptr = (const void *)&sc->sc_hub_desc;
3014	len = 0;
3015	err = 0;
3016
3017	value = UGETW(req->wValue);
3018	index = UGETW(req->wIndex);
3019
3020	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3021	    "wValue=0x%04x wIndex=0x%04x\n",
3022	    req->bmRequestType, req->bRequest,
3023	    UGETW(req->wLength), value, index);
3024
3025#define	C(x,y) ((x) | ((y) << 8))
3026	switch (C(req->bRequest, req->bmRequestType)) {
3027	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3028	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3029	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3030		/*
3031		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3032		 * for the integrated root hub.
3033		 */
3034		break;
3035	case C(UR_GET_CONFIG, UT_READ_DEVICE):
3036		len = 1;
3037		sc->sc_hub_desc.temp[0] = sc->sc_conf;
3038		break;
3039	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3040		switch (value >> 8) {
3041		case UDESC_DEVICE:
3042			if ((value & 0xff) != 0) {
3043				err = USB_ERR_IOERROR;
3044				goto done;
3045			}
3046			len = sizeof(ehci_devd);
3047			ptr = (const void *)&ehci_devd;
3048			break;
3049			/*
3050			 * We can't really operate at another speed,
3051			 * but the specification says we need this
3052			 * descriptor:
3053			 */
3054		case UDESC_DEVICE_QUALIFIER:
3055			if ((value & 0xff) != 0) {
3056				err = USB_ERR_IOERROR;
3057				goto done;
3058			}
3059			len = sizeof(ehci_odevd);
3060			ptr = (const void *)&ehci_odevd;
3061			break;
3062
3063		case UDESC_CONFIG:
3064			if ((value & 0xff) != 0) {
3065				err = USB_ERR_IOERROR;
3066				goto done;
3067			}
3068			len = sizeof(ehci_confd);
3069			ptr = (const void *)&ehci_confd;
3070			break;
3071
3072		case UDESC_STRING:
3073			switch (value & 0xff) {
3074			case 0:	/* Language table */
3075				str_ptr = "\001";
3076				break;
3077
3078			case 1:	/* Vendor */
3079				str_ptr = sc->sc_vendor;
3080				break;
3081
3082			case 2:	/* Product */
3083				str_ptr = "EHCI root HUB";
3084				break;
3085
3086			default:
3087				str_ptr = "";
3088				break;
3089			}
3090
3091			len = usb2_make_str_desc(
3092			    sc->sc_hub_desc.temp,
3093			    sizeof(sc->sc_hub_desc.temp),
3094			    str_ptr);
3095			break;
3096		default:
3097			err = USB_ERR_IOERROR;
3098			goto done;
3099		}
3100		break;
3101	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3102		len = 1;
3103		sc->sc_hub_desc.temp[0] = 0;
3104		break;
3105	case C(UR_GET_STATUS, UT_READ_DEVICE):
3106		len = 2;
3107		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3108		break;
3109	case C(UR_GET_STATUS, UT_READ_INTERFACE):
3110	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3111		len = 2;
3112		USETW(sc->sc_hub_desc.stat.wStatus, 0);
3113		break;
3114	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3115		if (value >= EHCI_MAX_DEVICES) {
3116			err = USB_ERR_IOERROR;
3117			goto done;
3118		}
3119		sc->sc_addr = value;
3120		break;
3121	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3122		if ((value != 0) && (value != 1)) {
3123			err = USB_ERR_IOERROR;
3124			goto done;
3125		}
3126		sc->sc_conf = value;
3127		break;
3128	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3129		break;
3130	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3131	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3132	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3133		err = USB_ERR_IOERROR;
3134		goto done;
3135	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3136		break;
3137	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3138		break;
3139		/* Hub requests */
3140	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3141		break;
3142	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3143		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3144
3145		if ((index < 1) ||
3146		    (index > sc->sc_noport)) {
3147			err = USB_ERR_IOERROR;
3148			goto done;
3149		}
3150		port = EHCI_PORTSC(index);
3151		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3152		switch (value) {
3153		case UHF_PORT_ENABLE:
3154			EOWRITE4(sc, port, v & ~EHCI_PS_PE);
3155			break;
3156		case UHF_PORT_SUSPEND:
3157			if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
3158
3159				/*
3160				 * waking up a High Speed device is rather
3161				 * complicated if
3162				 */
3163				EOWRITE4(sc, port, v | EHCI_PS_FPR);
3164			}
3165			/* wait 20ms for resume sequence to complete */
3166			usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3167
3168			EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
3169			    EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
3170
3171			/* 4ms settle time */
3172			usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3173			break;
3174		case UHF_PORT_POWER:
3175			EOWRITE4(sc, port, v & ~EHCI_PS_PP);
3176			break;
3177		case UHF_PORT_TEST:
3178			DPRINTFN(3, "clear port test "
3179			    "%d\n", index);
3180			break;
3181		case UHF_PORT_INDICATOR:
3182			DPRINTFN(3, "clear port ind "
3183			    "%d\n", index);
3184			EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
3185			break;
3186		case UHF_C_PORT_CONNECTION:
3187			EOWRITE4(sc, port, v | EHCI_PS_CSC);
3188			break;
3189		case UHF_C_PORT_ENABLE:
3190			EOWRITE4(sc, port, v | EHCI_PS_PEC);
3191			break;
3192		case UHF_C_PORT_SUSPEND:
3193			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3194			break;
3195		case UHF_C_PORT_OVER_CURRENT:
3196			EOWRITE4(sc, port, v | EHCI_PS_OCC);
3197			break;
3198		case UHF_C_PORT_RESET:
3199			sc->sc_isreset = 0;
3200			break;
3201		default:
3202			err = USB_ERR_IOERROR;
3203			goto done;
3204		}
3205		break;
3206	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3207		if ((value & 0xff) != 0) {
3208			err = USB_ERR_IOERROR;
3209			goto done;
3210		}
3211		v = EOREAD4(sc, EHCI_HCSPARAMS);
3212
3213		sc->sc_hub_desc.hubd = ehci_hubd;
3214		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3215		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics,
3216		    (EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH) |
3217		    (EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS)) ?
3218		    UHD_PORT_IND : 0));
3219		/* XXX can't find out? */
3220		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
3221		for (l = 0; l < sc->sc_noport; l++) {
3222			/* XXX can't find out? */
3223			sc->sc_hub_desc.hubd.DeviceRemovable[l / 8] &= ~(1 << (l % 8));
3224		}
3225		sc->sc_hub_desc.hubd.bDescLength =
3226		    8 + ((sc->sc_noport + 7) / 8);
3227		len = sc->sc_hub_desc.hubd.bDescLength;
3228		break;
3229	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3230		len = 16;
3231		bzero(sc->sc_hub_desc.temp, 16);
3232		break;
3233	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3234		DPRINTFN(9, "get port status i=%d\n",
3235		    index);
3236		if ((index < 1) ||
3237		    (index > sc->sc_noport)) {
3238			err = USB_ERR_IOERROR;
3239			goto done;
3240		}
3241		v = EOREAD4(sc, EHCI_PORTSC(index));
3242		DPRINTFN(9, "port status=0x%04x\n", v);
3243		if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) {
3244			if ((v & 0xc000000) == 0x8000000)
3245				i = UPS_HIGH_SPEED;
3246			else if ((v & 0xc000000) == 0x4000000)
3247				i = UPS_LOW_SPEED;
3248			else
3249				i = 0;
3250		} else {
3251			i = UPS_HIGH_SPEED;
3252		}
3253		if (v & EHCI_PS_CS)
3254			i |= UPS_CURRENT_CONNECT_STATUS;
3255		if (v & EHCI_PS_PE)
3256			i |= UPS_PORT_ENABLED;
3257		if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
3258			i |= UPS_SUSPEND;
3259		if (v & EHCI_PS_OCA)
3260			i |= UPS_OVERCURRENT_INDICATOR;
3261		if (v & EHCI_PS_PR)
3262			i |= UPS_RESET;
3263		if (v & EHCI_PS_PP)
3264			i |= UPS_PORT_POWER;
3265		USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3266		i = 0;
3267		if (v & EHCI_PS_CSC)
3268			i |= UPS_C_CONNECT_STATUS;
3269		if (v & EHCI_PS_PEC)
3270			i |= UPS_C_PORT_ENABLED;
3271		if (v & EHCI_PS_OCC)
3272			i |= UPS_C_OVERCURRENT_INDICATOR;
3273		if (v & EHCI_PS_FPR)
3274			i |= UPS_C_SUSPEND;
3275		if (sc->sc_isreset)
3276			i |= UPS_C_PORT_RESET;
3277		USETW(sc->sc_hub_desc.ps.wPortChange, i);
3278		len = sizeof(sc->sc_hub_desc.ps);
3279		break;
3280	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3281		err = USB_ERR_IOERROR;
3282		goto done;
3283	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3284		break;
3285	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3286		if ((index < 1) ||
3287		    (index > sc->sc_noport)) {
3288			err = USB_ERR_IOERROR;
3289			goto done;
3290		}
3291		port = EHCI_PORTSC(index);
3292		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
3293		switch (value) {
3294		case UHF_PORT_ENABLE:
3295			EOWRITE4(sc, port, v | EHCI_PS_PE);
3296			break;
3297		case UHF_PORT_SUSPEND:
3298			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
3299			break;
3300		case UHF_PORT_RESET:
3301			DPRINTFN(6, "reset port %d\n", index);
3302#if USB_DEBUG
3303			if (ehcinohighspeed) {
3304				/*
3305				 * Connect USB device to companion
3306				 * controller.
3307				 */
3308				ehci_disown(sc, index, 1);
3309				break;
3310			}
3311#endif
3312			if (EHCI_PS_IS_LOWSPEED(v) &&
3313			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3314				/* Low speed device, give up ownership. */
3315				ehci_disown(sc, index, 1);
3316				break;
3317			}
3318			/* Start reset sequence. */
3319			v &= ~(EHCI_PS_PE | EHCI_PS_PR);
3320			EOWRITE4(sc, port, v | EHCI_PS_PR);
3321
3322			/* Wait for reset to complete. */
3323			usb2_pause_mtx(&sc->sc_bus.bus_mtx,
3324			    USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
3325
3326			/* Terminate reset sequence. */
3327			if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
3328				EOWRITE4(sc, port, v);
3329
3330			/* Wait for HC to complete reset. */
3331			usb2_pause_mtx(&sc->sc_bus.bus_mtx,
3332			    USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
3333
3334			v = EOREAD4(sc, port);
3335			DPRINTF("ehci after reset, status=0x%08x\n", v);
3336			if (v & EHCI_PS_PR) {
3337				device_printf(sc->sc_bus.bdev,
3338				    "port reset timeout\n");
3339				err = USB_ERR_TIMEOUT;
3340				goto done;
3341			}
3342			if (!(v & EHCI_PS_PE) &&
3343			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3344				/* Not a high speed device, give up ownership.*/
3345				ehci_disown(sc, index, 0);
3346				break;
3347			}
3348			sc->sc_isreset = 1;
3349			DPRINTF("ehci port %d reset, status = 0x%08x\n",
3350			    index, v);
3351			break;
3352
3353		case UHF_PORT_POWER:
3354			DPRINTFN(3, "set port power %d\n", index);
3355			EOWRITE4(sc, port, v | EHCI_PS_PP);
3356			break;
3357
3358		case UHF_PORT_TEST:
3359			DPRINTFN(3, "set port test %d\n", index);
3360			break;
3361
3362		case UHF_PORT_INDICATOR:
3363			DPRINTFN(3, "set port ind %d\n", index);
3364			EOWRITE4(sc, port, v | EHCI_PS_PIC);
3365			break;
3366
3367		default:
3368			err = USB_ERR_IOERROR;
3369			goto done;
3370		}
3371		break;
3372	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3373	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3374	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3375	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3376		break;
3377	default:
3378		err = USB_ERR_IOERROR;
3379		goto done;
3380	}
3381done:
3382	*plength = len;
3383	*pptr = ptr;
3384	return (err);
3385}
3386
3387static void
3388ehci_xfer_setup(struct usb2_setup_params *parm)
3389{
3390	struct usb2_page_search page_info;
3391	struct usb2_page_cache *pc;
3392	ehci_softc_t *sc;
3393	struct usb2_xfer *xfer;
3394	void *last_obj;
3395	uint32_t nqtd;
3396	uint32_t nqh;
3397	uint32_t nsitd;
3398	uint32_t nitd;
3399	uint32_t n;
3400
3401	sc = EHCI_BUS2SC(parm->udev->bus);
3402	xfer = parm->curr_xfer;
3403
3404	nqtd = 0;
3405	nqh = 0;
3406	nsitd = 0;
3407	nitd = 0;
3408
3409	/*
3410	 * compute maximum number of some structures
3411	 */
3412	if (parm->methods == &ehci_device_ctrl_methods) {
3413
3414		/*
3415		 * The proof for the "nqtd" formula is illustrated like
3416		 * this:
3417		 *
3418		 * +------------------------------------+
3419		 * |                                    |
3420		 * |         |remainder ->              |
3421		 * |   +-----+---+                      |
3422		 * |   | xxx | x | frm 0                |
3423		 * |   +-----+---++                     |
3424		 * |   | xxx | xx | frm 1               |
3425		 * |   +-----+----+                     |
3426		 * |            ...                     |
3427		 * +------------------------------------+
3428		 *
3429		 * "xxx" means a completely full USB transfer descriptor
3430		 *
3431		 * "x" and "xx" means a short USB packet
3432		 *
3433		 * For the remainder of an USB transfer modulo
3434		 * "max_data_length" we need two USB transfer descriptors.
3435		 * One to transfer the remaining data and one to finalise
3436		 * with a zero length packet in case the "force_short_xfer"
3437		 * flag is set. We only need two USB transfer descriptors in
3438		 * the case where the transfer length of the first one is a
3439		 * factor of "max_frame_size". The rest of the needed USB
3440		 * transfer descriptors is given by the buffer size divided
3441		 * by the maximum data payload.
3442		 */
3443		parm->hc_max_packet_size = 0x400;
3444		parm->hc_max_packet_count = 1;
3445		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3446		xfer->flags_int.bdma_enable = 1;
3447
3448		usb2_transfer_setup_sub(parm);
3449
3450		nqh = 1;
3451		nqtd = ((2 * xfer->nframes) + 1	/* STATUS */
3452		    + (xfer->max_data_length / xfer->max_hc_frame_size));
3453
3454	} else if (parm->methods == &ehci_device_bulk_methods) {
3455
3456		parm->hc_max_packet_size = 0x400;
3457		parm->hc_max_packet_count = 1;
3458		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3459		xfer->flags_int.bdma_enable = 1;
3460
3461		usb2_transfer_setup_sub(parm);
3462
3463		nqh = 1;
3464		nqtd = ((2 * xfer->nframes)
3465		    + (xfer->max_data_length / xfer->max_hc_frame_size));
3466
3467	} else if (parm->methods == &ehci_device_intr_methods) {
3468
3469		if (parm->speed == USB_SPEED_HIGH) {
3470			parm->hc_max_packet_size = 0x400;
3471			parm->hc_max_packet_count = 3;
3472		} else if (parm->speed == USB_SPEED_FULL) {
3473			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
3474			parm->hc_max_packet_count = 1;
3475		} else {
3476			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
3477			parm->hc_max_packet_count = 1;
3478		}
3479
3480		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
3481		xfer->flags_int.bdma_enable = 1;
3482
3483		usb2_transfer_setup_sub(parm);
3484
3485		nqh = 1;
3486		nqtd = ((2 * xfer->nframes)
3487		    + (xfer->max_data_length / xfer->max_hc_frame_size));
3488
3489	} else if (parm->methods == &ehci_device_isoc_fs_methods) {
3490
3491		parm->hc_max_packet_size = 0x3FF;
3492		parm->hc_max_packet_count = 1;
3493		parm->hc_max_frame_size = 0x3FF;
3494		xfer->flags_int.bdma_enable = 1;
3495
3496		usb2_transfer_setup_sub(parm);
3497
3498		nsitd = xfer->nframes;
3499
3500	} else if (parm->methods == &ehci_device_isoc_hs_methods) {
3501
3502		parm->hc_max_packet_size = 0x400;
3503		parm->hc_max_packet_count = 3;
3504		parm->hc_max_frame_size = 0xC00;
3505		xfer->flags_int.bdma_enable = 1;
3506
3507		usb2_transfer_setup_sub(parm);
3508
3509		nitd = (xfer->nframes + 7) / 8;
3510
3511	} else {
3512
3513		parm->hc_max_packet_size = 0x400;
3514		parm->hc_max_packet_count = 1;
3515		parm->hc_max_frame_size = 0x400;
3516
3517		usb2_transfer_setup_sub(parm);
3518	}
3519
3520alloc_dma_set:
3521
3522	if (parm->err) {
3523		return;
3524	}
3525	/*
3526	 * Allocate queue heads and transfer descriptors
3527	 */
3528	last_obj = NULL;
3529
3530	if (usb2_transfer_setup_sub_malloc(
3531	    parm, &pc, sizeof(ehci_itd_t),
3532	    EHCI_ITD_ALIGN, nitd)) {
3533		parm->err = USB_ERR_NOMEM;
3534		return;
3535	}
3536	if (parm->buf) {
3537		for (n = 0; n != nitd; n++) {
3538			ehci_itd_t *td;
3539
3540			usb2_get_page(pc + n, 0, &page_info);
3541
3542			td = page_info.buffer;
3543
3544			/* init TD */
3545			td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
3546			td->obj_next = last_obj;
3547			td->page_cache = pc + n;
3548
3549			last_obj = td;
3550
3551			usb2_pc_cpu_flush(pc + n);
3552		}
3553	}
3554	if (usb2_transfer_setup_sub_malloc(
3555	    parm, &pc, sizeof(ehci_sitd_t),
3556	    EHCI_SITD_ALIGN, nsitd)) {
3557		parm->err = USB_ERR_NOMEM;
3558		return;
3559	}
3560	if (parm->buf) {
3561		for (n = 0; n != nsitd; n++) {
3562			ehci_sitd_t *td;
3563
3564			usb2_get_page(pc + n, 0, &page_info);
3565
3566			td = page_info.buffer;
3567
3568			/* init TD */
3569			td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
3570			td->obj_next = last_obj;
3571			td->page_cache = pc + n;
3572
3573			last_obj = td;
3574
3575			usb2_pc_cpu_flush(pc + n);
3576		}
3577	}
3578	if (usb2_transfer_setup_sub_malloc(
3579	    parm, &pc, sizeof(ehci_qtd_t),
3580	    EHCI_QTD_ALIGN, nqtd)) {
3581		parm->err = USB_ERR_NOMEM;
3582		return;
3583	}
3584	if (parm->buf) {
3585		for (n = 0; n != nqtd; n++) {
3586			ehci_qtd_t *qtd;
3587
3588			usb2_get_page(pc + n, 0, &page_info);
3589
3590			qtd = page_info.buffer;
3591
3592			/* init TD */
3593			qtd->qtd_self = htohc32(sc, page_info.physaddr);
3594			qtd->obj_next = last_obj;
3595			qtd->page_cache = pc + n;
3596
3597			last_obj = qtd;
3598
3599			usb2_pc_cpu_flush(pc + n);
3600		}
3601	}
3602	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3603
3604	last_obj = NULL;
3605
3606	if (usb2_transfer_setup_sub_malloc(
3607	    parm, &pc, sizeof(ehci_qh_t),
3608	    EHCI_QH_ALIGN, nqh)) {
3609		parm->err = USB_ERR_NOMEM;
3610		return;
3611	}
3612	if (parm->buf) {
3613		for (n = 0; n != nqh; n++) {
3614			ehci_qh_t *qh;
3615
3616			usb2_get_page(pc + n, 0, &page_info);
3617
3618			qh = page_info.buffer;
3619
3620			/* init QH */
3621			qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
3622			qh->obj_next = last_obj;
3623			qh->page_cache = pc + n;
3624
3625			last_obj = qh;
3626
3627			usb2_pc_cpu_flush(pc + n);
3628		}
3629	}
3630	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3631
3632	if (!xfer->flags_int.curr_dma_set) {
3633		xfer->flags_int.curr_dma_set = 1;
3634		goto alloc_dma_set;
3635	}
3636}
3637
3638static void
3639ehci_xfer_unsetup(struct usb2_xfer *xfer)
3640{
3641	return;
3642}
3643
3644static void
3645ehci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc,
3646    struct usb2_pipe *pipe)
3647{
3648	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3649
3650	DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3651	    pipe, udev->address,
3652	    edesc->bEndpointAddress, udev->flags.usb2_mode,
3653	    sc->sc_addr);
3654
3655	if (udev->flags.usb2_mode != USB_MODE_HOST) {
3656		/* not supported */
3657		return;
3658	}
3659	if (udev->device_index != sc->sc_addr) {
3660
3661		if ((udev->speed != USB_SPEED_HIGH) &&
3662		    ((udev->hs_hub_addr == 0) ||
3663		    (udev->hs_port_no == 0) ||
3664		    (udev->parent_hs_hub == NULL) ||
3665		    (udev->parent_hs_hub->hub == NULL))) {
3666			/* We need a transaction translator */
3667			goto done;
3668		}
3669		switch (edesc->bmAttributes & UE_XFERTYPE) {
3670		case UE_CONTROL:
3671			pipe->methods = &ehci_device_ctrl_methods;
3672			break;
3673		case UE_INTERRUPT:
3674			pipe->methods = &ehci_device_intr_methods;
3675			break;
3676		case UE_ISOCHRONOUS:
3677			if (udev->speed == USB_SPEED_HIGH) {
3678				pipe->methods = &ehci_device_isoc_hs_methods;
3679			} else if (udev->speed == USB_SPEED_FULL) {
3680				pipe->methods = &ehci_device_isoc_fs_methods;
3681			}
3682			break;
3683		case UE_BULK:
3684			if (udev->speed != USB_SPEED_LOW) {
3685				pipe->methods = &ehci_device_bulk_methods;
3686			}
3687			break;
3688		default:
3689			/* do nothing */
3690			break;
3691		}
3692	}
3693done:
3694	return;
3695}
3696
3697static void
3698ehci_get_dma_delay(struct usb2_bus *bus, uint32_t *pus)
3699{
3700	/*
3701	 * Wait until the hardware has finished any possible use of
3702	 * the transfer descriptor(s) and QH
3703	 */
3704	*pus = (188);			/* microseconds */
3705}
3706
3707static void
3708ehci_device_resume(struct usb2_device *udev)
3709{
3710	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3711	struct usb2_xfer *xfer;
3712	struct usb2_pipe_methods *methods;
3713
3714	DPRINTF("\n");
3715
3716	USB_BUS_LOCK(udev->bus);
3717
3718	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3719
3720		if (xfer->xroot->udev == udev) {
3721
3722			methods = xfer->pipe->methods;
3723
3724			if ((methods == &ehci_device_bulk_methods) ||
3725			    (methods == &ehci_device_ctrl_methods)) {
3726				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3727				    sc->sc_async_p_last);
3728			}
3729			if (methods == &ehci_device_intr_methods) {
3730				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3731				    sc->sc_intr_p_last[xfer->qh_pos]);
3732			}
3733		}
3734	}
3735
3736	USB_BUS_UNLOCK(udev->bus);
3737
3738	return;
3739}
3740
3741static void
3742ehci_device_suspend(struct usb2_device *udev)
3743{
3744	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3745	struct usb2_xfer *xfer;
3746	struct usb2_pipe_methods *methods;
3747
3748	DPRINTF("\n");
3749
3750	USB_BUS_LOCK(udev->bus);
3751
3752	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3753
3754		if (xfer->xroot->udev == udev) {
3755
3756			methods = xfer->pipe->methods;
3757
3758			if ((methods == &ehci_device_bulk_methods) ||
3759			    (methods == &ehci_device_ctrl_methods)) {
3760				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3761				    sc->sc_async_p_last);
3762			}
3763			if (methods == &ehci_device_intr_methods) {
3764				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
3765				    sc->sc_intr_p_last[xfer->qh_pos]);
3766			}
3767		}
3768	}
3769
3770	USB_BUS_UNLOCK(udev->bus);
3771
3772	return;
3773}
3774
3775static void
3776ehci_set_hw_power(struct usb2_bus *bus)
3777{
3778	ehci_softc_t *sc = EHCI_BUS2SC(bus);
3779	uint32_t temp;
3780	uint32_t flags;
3781
3782	DPRINTF("\n");
3783
3784	USB_BUS_LOCK(bus);
3785
3786	flags = bus->hw_power_state;
3787
3788	temp = EOREAD4(sc, EHCI_USBCMD);
3789
3790	temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
3791
3792	if (flags & (USB_HW_POWER_CONTROL |
3793	    USB_HW_POWER_BULK)) {
3794		DPRINTF("Async is active\n");
3795		temp |= EHCI_CMD_ASE;
3796	}
3797	if (flags & (USB_HW_POWER_INTERRUPT |
3798	    USB_HW_POWER_ISOC)) {
3799		DPRINTF("Periodic is active\n");
3800		temp |= EHCI_CMD_PSE;
3801	}
3802	EOWRITE4(sc, EHCI_USBCMD, temp);
3803
3804	USB_BUS_UNLOCK(bus);
3805
3806	return;
3807}
3808
3809struct usb2_bus_methods ehci_bus_methods =
3810{
3811	.pipe_init = ehci_pipe_init,
3812	.xfer_setup = ehci_xfer_setup,
3813	.xfer_unsetup = ehci_xfer_unsetup,
3814	.get_dma_delay = ehci_get_dma_delay,
3815	.device_resume = ehci_device_resume,
3816	.device_suspend = ehci_device_suspend,
3817	.set_hw_power = ehci_set_hw_power,
3818	.roothub_exec = ehci_roothub_exec,
3819};
3820