xref: /freebsd/sys/dev/usb/controller/ehci.c (revision af3b2549c4ba2ef00a7cbb4cb6836598bf0aefbe)
1d2b99310SHans Petter Selasky /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  * Copyright (c) 2004 The NetBSD Foundation, Inc. All rights reserved.
502ac6454SAndrew Thompson  * Copyright (c) 2004 Lennart Augustsson. All rights reserved.
602ac6454SAndrew Thompson  * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
902ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1002ac6454SAndrew Thompson  * are met:
1102ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1302ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1502ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1602ac6454SAndrew Thompson  *
1702ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1802ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1902ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2002ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2102ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2202ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2302ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2402ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2502ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2602ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2702ac6454SAndrew Thompson  * SUCH DAMAGE.
2802ac6454SAndrew Thompson  */
2902ac6454SAndrew Thompson 
3002ac6454SAndrew Thompson /*
3102ac6454SAndrew Thompson  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
3202ac6454SAndrew Thompson  *
3302ac6454SAndrew Thompson  * The EHCI 0.96 spec can be found at
3402ac6454SAndrew Thompson  * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
3502ac6454SAndrew Thompson  * The EHCI 1.0 spec can be found at
3602ac6454SAndrew Thompson  * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
3702ac6454SAndrew Thompson  * and the USB 2.0 spec at
3802ac6454SAndrew Thompson  * http://www.usb.org/developers/docs/usb_20.zip
3902ac6454SAndrew Thompson  *
4002ac6454SAndrew Thompson  */
4102ac6454SAndrew Thompson 
4202ac6454SAndrew Thompson /*
4302ac6454SAndrew Thompson  * TODO:
4402ac6454SAndrew Thompson  * 1) command failures are not recovered correctly
4502ac6454SAndrew Thompson  */
4602ac6454SAndrew Thompson 
47d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
48d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
49d2b99310SHans Petter Selasky #else
50ed6d949aSAndrew Thompson #include <sys/stdint.h>
51ed6d949aSAndrew Thompson #include <sys/stddef.h>
52ed6d949aSAndrew Thompson #include <sys/param.h>
53ed6d949aSAndrew Thompson #include <sys/queue.h>
54ed6d949aSAndrew Thompson #include <sys/types.h>
55ed6d949aSAndrew Thompson #include <sys/systm.h>
56ed6d949aSAndrew Thompson #include <sys/kernel.h>
57ed6d949aSAndrew Thompson #include <sys/bus.h>
58ed6d949aSAndrew Thompson #include <sys/module.h>
59ed6d949aSAndrew Thompson #include <sys/lock.h>
60ed6d949aSAndrew Thompson #include <sys/mutex.h>
61ed6d949aSAndrew Thompson #include <sys/condvar.h>
62ed6d949aSAndrew Thompson #include <sys/sysctl.h>
63ed6d949aSAndrew Thompson #include <sys/sx.h>
64ed6d949aSAndrew Thompson #include <sys/unistd.h>
65ed6d949aSAndrew Thompson #include <sys/callout.h>
66ed6d949aSAndrew Thompson #include <sys/malloc.h>
67ed6d949aSAndrew Thompson #include <sys/priv.h>
68ed6d949aSAndrew Thompson 
6902ac6454SAndrew Thompson #include <dev/usb/usb.h>
70ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson #define	USB_DEBUG_VAR ehcidebug
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
7502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7802ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
7902ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
8002ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
8102ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
8402ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
85d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
86d2b99310SHans Petter Selasky 
8702ac6454SAndrew Thompson #include <dev/usb/controller/ehci.h>
881def609aSAndrew Thompson #include <dev/usb/controller/ehcireg.h>
8902ac6454SAndrew Thompson 
90578d0effSAndrew Thompson #define	EHCI_BUS2SC(bus) \
91578d0effSAndrew Thompson    ((ehci_softc_t *)(((uint8_t *)(bus)) - \
92578d0effSAndrew Thompson     ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))
9302ac6454SAndrew Thompson 
94b850ecc1SAndrew Thompson #ifdef USB_DEBUG
9502ac6454SAndrew Thompson static int ehcidebug = 0;
9602ac6454SAndrew Thompson static int ehcinohighspeed = 0;
97cf223a88SAndrew Thompson static int ehciiaadbug = 0;
98cf223a88SAndrew Thompson static int ehcilostintrbug = 0;
9902ac6454SAndrew Thompson 
1006472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
101*af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RWTUN,
10202ac6454SAndrew Thompson     &ehcidebug, 0, "Debug level");
103*af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RWTUN,
10483cadd7dSHans Petter Selasky     &ehcinohighspeed, 0, "Disable High Speed USB");
105*af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, iaadbug, CTLFLAG_RWTUN,
10683cadd7dSHans Petter Selasky     &ehciiaadbug, 0, "Enable doorbell bug workaround");
107*af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, lostintrbug, CTLFLAG_RWTUN,
10883cadd7dSHans Petter Selasky     &ehcilostintrbug, 0, "Enable lost interrupt bug workaround");
10983cadd7dSHans Petter Selasky 
11002ac6454SAndrew Thompson static void ehci_dump_regs(ehci_softc_t *sc);
11102ac6454SAndrew Thompson static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson #endif
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson #define	EHCI_INTR_ENDPT 1
11602ac6454SAndrew Thompson 
117e892b3feSHans Petter Selasky static const struct usb_bus_methods ehci_bus_methods;
118e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_bulk_methods;
119e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_ctrl_methods;
120e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_intr_methods;
121e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_fs_methods;
122e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_hs_methods;
12302ac6454SAndrew Thompson 
12430b22abeSAndrew Thompson static void ehci_do_poll(struct usb_bus *);
12530b22abeSAndrew Thompson static void ehci_device_done(struct usb_xfer *, usb_error_t);
12630b22abeSAndrew Thompson static uint8_t ehci_check_transfer(struct usb_xfer *);
12730b22abeSAndrew Thompson static void ehci_timeout(void *);
12830b22abeSAndrew Thompson static void ehci_poll_timeout(void *);
12930b22abeSAndrew Thompson 
13039307315SAndrew Thompson static void ehci_root_intr(ehci_softc_t *sc);
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson struct ehci_std_temp {
13302ac6454SAndrew Thompson 	ehci_softc_t *sc;
134760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
13502ac6454SAndrew Thompson 	ehci_qtd_t *td;
13602ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
13702ac6454SAndrew Thompson 	uint32_t average;
13802ac6454SAndrew Thompson 	uint32_t qtd_status;
13902ac6454SAndrew Thompson 	uint32_t len;
14002ac6454SAndrew Thompson 	uint16_t max_frame_size;
14102ac6454SAndrew Thompson 	uint8_t	shortpkt;
14202ac6454SAndrew Thompson 	uint8_t	auto_data_toggle;
14302ac6454SAndrew Thompson 	uint8_t	setup_alt_next;
1440f7d4548SAndrew Thompson 	uint8_t	last_frame;
14502ac6454SAndrew Thompson };
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson void
148e0a69b51SAndrew Thompson ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
14902ac6454SAndrew Thompson {
15002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
15102ac6454SAndrew Thompson 	uint32_t i;
15202ac6454SAndrew Thompson 
15302ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
15402ac6454SAndrew Thompson 	    sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
15502ac6454SAndrew Thompson 
15653e0bf6eSHans Petter Selasky 	cb(bus, &sc->sc_hw.terminate_pc, &sc->sc_hw.terminate_pg,
15753e0bf6eSHans Petter Selasky 	    sizeof(struct ehci_qh_sub), EHCI_QH_ALIGN);
15853e0bf6eSHans Petter Selasky 
15902ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
16002ac6454SAndrew Thompson 	    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
16102ac6454SAndrew Thompson 
16202ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
16302ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.intr_start_pc + i,
16402ac6454SAndrew Thompson 		    sc->sc_hw.intr_start_pg + i,
16502ac6454SAndrew Thompson 		    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
16602ac6454SAndrew Thompson 	}
16702ac6454SAndrew Thompson 
16802ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
16902ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
17002ac6454SAndrew Thompson 		    sc->sc_hw.isoc_hs_start_pg + i,
17102ac6454SAndrew Thompson 		    sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
17202ac6454SAndrew Thompson 	}
17302ac6454SAndrew Thompson 
17402ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
17502ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
17602ac6454SAndrew Thompson 		    sc->sc_hw.isoc_fs_start_pg + i,
17702ac6454SAndrew Thompson 		    sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
17802ac6454SAndrew Thompson 	}
17902ac6454SAndrew Thompson }
18002ac6454SAndrew Thompson 
181e0a69b51SAndrew Thompson usb_error_t
182e55e1ebcSAndrew Thompson ehci_reset(ehci_softc_t *sc)
18302ac6454SAndrew Thompson {
18402ac6454SAndrew Thompson 	uint32_t hcr;
185e55e1ebcSAndrew Thompson 	int i;
186e55e1ebcSAndrew Thompson 
187e55e1ebcSAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
188e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
1892e141748SHans Petter Selasky 		usb_pause_mtx(NULL, hz / 128);
190e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
191e55e1ebcSAndrew Thompson 		if (!hcr) {
192e55e1ebcSAndrew Thompson 			if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) {
193e55e1ebcSAndrew Thompson 				/*
194e55e1ebcSAndrew Thompson 				 * Force USBMODE as requested.  Controllers
195e55e1ebcSAndrew Thompson 				 * may have multiple operating modes.
196e55e1ebcSAndrew Thompson 				 */
197e55e1ebcSAndrew Thompson 				uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE);
198e55e1ebcSAndrew Thompson 				if (sc->sc_flags & EHCI_SCFLG_SETMODE) {
199e55e1ebcSAndrew Thompson 					usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST;
200e55e1ebcSAndrew Thompson 					device_printf(sc->sc_bus.bdev,
201e55e1ebcSAndrew Thompson 					    "set host controller mode\n");
202e55e1ebcSAndrew Thompson 				}
203e55e1ebcSAndrew Thompson 				if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) {
204e55e1ebcSAndrew Thompson 					usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE;
205e55e1ebcSAndrew Thompson 					device_printf(sc->sc_bus.bdev,
206e55e1ebcSAndrew Thompson 					    "set big-endian mode\n");
207e55e1ebcSAndrew Thompson 				}
208e55e1ebcSAndrew Thompson 				EOWRITE4(sc,  EHCI_USBMODE, usbmode);
209e55e1ebcSAndrew Thompson 			}
210e55e1ebcSAndrew Thompson 			return (0);
211e55e1ebcSAndrew Thompson 		}
212e55e1ebcSAndrew Thompson 	}
2132e141748SHans Petter Selasky 	device_printf(sc->sc_bus.bdev, "Reset timeout\n");
214e55e1ebcSAndrew Thompson 	return (USB_ERR_IOERROR);
215e55e1ebcSAndrew Thompson }
216e55e1ebcSAndrew Thompson 
217e0a69b51SAndrew Thompson static usb_error_t
218e55e1ebcSAndrew Thompson ehci_hcreset(ehci_softc_t *sc)
219e55e1ebcSAndrew Thompson {
220e55e1ebcSAndrew Thompson 	uint32_t hcr;
221e55e1ebcSAndrew Thompson 	int i;
22202ac6454SAndrew Thompson 
22302ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
224e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
2252e141748SHans Petter Selasky 		usb_pause_mtx(NULL, hz / 128);
226e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
227e55e1ebcSAndrew Thompson 		if (hcr)
22802ac6454SAndrew Thompson 			break;
22902ac6454SAndrew Thompson 	}
230e55e1ebcSAndrew Thompson 	if (!hcr)
23102ac6454SAndrew Thompson 		/*
23202ac6454SAndrew Thompson                  * Fall through and try reset anyway even though
23302ac6454SAndrew Thompson                  * Table 2-9 in the EHCI spec says this will result
23402ac6454SAndrew Thompson                  * in undefined behavior.
23502ac6454SAndrew Thompson                  */
236e55e1ebcSAndrew Thompson 		device_printf(sc->sc_bus.bdev, "stop timeout\n");
23702ac6454SAndrew Thompson 
2382e141748SHans Petter Selasky 	return (ehci_reset(sc));
2392e141748SHans Petter Selasky }
2402e141748SHans Petter Selasky 
2412e141748SHans Petter Selasky static int
2422e141748SHans Petter Selasky ehci_init_sub(struct ehci_softc *sc)
2432e141748SHans Petter Selasky {
2442e141748SHans Petter Selasky 	struct usb_page_search buf_res;
2452e141748SHans Petter Selasky 	uint32_t cparams;
2462e141748SHans Petter Selasky   	uint32_t hcr;
2472e141748SHans Petter Selasky 	uint8_t i;
2482e141748SHans Petter Selasky 
2492e141748SHans Petter Selasky 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
2502e141748SHans Petter Selasky 
2512e141748SHans Petter Selasky 	DPRINTF("cparams=0x%x\n", cparams);
2522e141748SHans Petter Selasky 
2532e141748SHans Petter Selasky 	if (EHCI_HCC_64BIT(cparams)) {
2542e141748SHans Petter Selasky 		DPRINTF("HCC uses 64-bit structures\n");
2552e141748SHans Petter Selasky 
2562e141748SHans Petter Selasky 		/* MUST clear segment register if 64 bit capable */
257fcd51bb4SHans Petter Selasky 		EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
2582e141748SHans Petter Selasky 	}
2592e141748SHans Petter Selasky 
2602e141748SHans Petter Selasky 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
2612e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
2622e141748SHans Petter Selasky 
2632e141748SHans Petter Selasky 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
2642e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
2652e141748SHans Petter Selasky 
2662e141748SHans Petter Selasky 	/* enable interrupts */
2672e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
2682e141748SHans Petter Selasky 
2692e141748SHans Petter Selasky 	/* turn on controller */
2702e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_USBCMD,
2712e141748SHans Petter Selasky 	    EHCI_CMD_ITC_1 |		/* 1 microframes interrupt delay */
2722e141748SHans Petter Selasky 	    (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
2732e141748SHans Petter Selasky 	    EHCI_CMD_ASE |
2742e141748SHans Petter Selasky 	    EHCI_CMD_PSE |
2752e141748SHans Petter Selasky 	    EHCI_CMD_RS);
2762e141748SHans Petter Selasky 
2772e141748SHans Petter Selasky 	/* Take over port ownership */
2782e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
2792e141748SHans Petter Selasky 
2802e141748SHans Petter Selasky 	for (i = 0; i < 100; i++) {
2812e141748SHans Petter Selasky 		usb_pause_mtx(NULL, hz / 128);
2822e141748SHans Petter Selasky 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
2832e141748SHans Petter Selasky 		if (!hcr) {
2842e141748SHans Petter Selasky 			break;
2852e141748SHans Petter Selasky 		}
2862e141748SHans Petter Selasky 	}
2872e141748SHans Petter Selasky 	if (hcr) {
2882e141748SHans Petter Selasky 		device_printf(sc->sc_bus.bdev, "Run timeout\n");
2892e141748SHans Petter Selasky 		return (USB_ERR_IOERROR);
2902e141748SHans Petter Selasky 	}
2912e141748SHans Petter Selasky 	return (USB_ERR_NORMAL_COMPLETION);
29202ac6454SAndrew Thompson }
29302ac6454SAndrew Thompson 
294e0a69b51SAndrew Thompson usb_error_t
29502ac6454SAndrew Thompson ehci_init(ehci_softc_t *sc)
29602ac6454SAndrew Thompson {
297760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
29802ac6454SAndrew Thompson 	uint32_t version;
29902ac6454SAndrew Thompson 	uint32_t sparams;
30002ac6454SAndrew Thompson 	uint16_t i;
30102ac6454SAndrew Thompson 	uint16_t x;
30202ac6454SAndrew Thompson 	uint16_t y;
30302ac6454SAndrew Thompson 	uint16_t bit;
304e0a69b51SAndrew Thompson 	usb_error_t err = 0;
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson 	DPRINTF("start\n");
30702ac6454SAndrew Thompson 
308a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
30930b22abeSAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_mtx, 0);
31002ac6454SAndrew Thompson 
31146873d15SHans Petter Selasky 	sc->sc_offs = EHCI_CAPLENGTH(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
31246873d15SHans Petter Selasky 
313b850ecc1SAndrew Thompson #ifdef USB_DEBUG
314cf223a88SAndrew Thompson 	if (ehciiaadbug)
315cf223a88SAndrew Thompson 		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
316cf223a88SAndrew Thompson 	if (ehcilostintrbug)
317cf223a88SAndrew Thompson 		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
31802ac6454SAndrew Thompson 	if (ehcidebug > 2) {
31902ac6454SAndrew Thompson 		ehci_dump_regs(sc);
32002ac6454SAndrew Thompson 	}
32102ac6454SAndrew Thompson #endif
32202ac6454SAndrew Thompson 
323495ed64cSNathan Whitehorn 	version = EHCI_HCIVERSION(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
32402ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
32502ac6454SAndrew Thompson 	    version >> 8, version & 0xff);
32602ac6454SAndrew Thompson 
32702ac6454SAndrew Thompson 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
32802ac6454SAndrew Thompson 	DPRINTF("sparams=0x%x\n", sparams);
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
33102ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_2_0;
33202ac6454SAndrew Thompson 
333db43ed37SMarcel Moolenaar 	if (!(sc->sc_flags & EHCI_SCFLG_DONTRESET)) {
33402ac6454SAndrew Thompson 		/* Reset the controller */
335db43ed37SMarcel Moolenaar 		DPRINTF("%s: resetting\n",
336db43ed37SMarcel Moolenaar 		    device_get_nameunit(sc->sc_bus.bdev));
33702ac6454SAndrew Thompson 
338e55e1ebcSAndrew Thompson 		err = ehci_hcreset(sc);
33902ac6454SAndrew Thompson 		if (err) {
34002ac6454SAndrew Thompson 			device_printf(sc->sc_bus.bdev, "reset timeout\n");
34102ac6454SAndrew Thompson 			return (err);
34202ac6454SAndrew Thompson 		}
343db43ed37SMarcel Moolenaar 	}
344db43ed37SMarcel Moolenaar 
34502ac6454SAndrew Thompson 	/*
34602ac6454SAndrew Thompson 	 * use current frame-list-size selection 0: 1024*4 bytes 1:  512*4
34702ac6454SAndrew Thompson 	 * bytes 2:  256*4 bytes 3:      unknown
34802ac6454SAndrew Thompson 	 */
34902ac6454SAndrew Thompson 	if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
35002ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
35102ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
35202ac6454SAndrew Thompson 	}
35302ac6454SAndrew Thompson 	/* set up the bus struct */
35402ac6454SAndrew Thompson 	sc->sc_bus.methods = &ehci_bus_methods;
35502ac6454SAndrew Thompson 
35602ac6454SAndrew Thompson 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
35702ac6454SAndrew Thompson 
35853e0bf6eSHans Petter Selasky 	if (1) {
35953e0bf6eSHans Petter Selasky 		struct ehci_qh_sub *qh;
36053e0bf6eSHans Petter Selasky 
36153e0bf6eSHans Petter Selasky 		usbd_get_page(&sc->sc_hw.terminate_pc, 0, &buf_res);
36253e0bf6eSHans Petter Selasky 
36353e0bf6eSHans Petter Selasky 		qh = buf_res.buffer;
36453e0bf6eSHans Petter Selasky 
36553e0bf6eSHans Petter Selasky 		sc->sc_terminate_self = htohc32(sc, buf_res.physaddr);
36653e0bf6eSHans Petter Selasky 
36753e0bf6eSHans Petter Selasky 		/* init terminate TD */
36853e0bf6eSHans Petter Selasky 		qh->qtd_next =
36953e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_LINK_TERMINATE);
37053e0bf6eSHans Petter Selasky 		qh->qtd_altnext =
37153e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_LINK_TERMINATE);
37253e0bf6eSHans Petter Selasky 		qh->qtd_status =
37353e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_QTD_HALTED);
37453e0bf6eSHans Petter Selasky 	}
37553e0bf6eSHans Petter Selasky 
37602ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
37702ac6454SAndrew Thompson 		ehci_qh_t *qh;
37802ac6454SAndrew Thompson 
379a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 		qh = buf_res.buffer;
38202ac6454SAndrew Thompson 
38302ac6454SAndrew Thompson 		/* initialize page cache pointer */
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 		qh->page_cache = sc->sc_hw.intr_start_pc + i;
38602ac6454SAndrew Thompson 
38702ac6454SAndrew Thompson 		/* store a pointer to queue head */
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 		sc->sc_intr_p_last[i] = qh;
39002ac6454SAndrew Thompson 
39102ac6454SAndrew Thompson 		qh->qh_self =
392e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
393e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
39402ac6454SAndrew Thompson 
39502ac6454SAndrew Thompson 		qh->qh_endp =
396e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
39702ac6454SAndrew Thompson 		qh->qh_endphub =
398e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_MULT(1));
39902ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson 		qh->qh_qtd.qtd_next =
402e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
40302ac6454SAndrew Thompson 		qh->qh_qtd.qtd_altnext =
404e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
40502ac6454SAndrew Thompson 		qh->qh_qtd.qtd_status =
406e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QTD_HALTED);
40702ac6454SAndrew Thompson 	}
40802ac6454SAndrew Thompson 
40902ac6454SAndrew Thompson 	/*
41002ac6454SAndrew Thompson 	 * the QHs are arranged to give poll intervals that are
41102ac6454SAndrew Thompson 	 * powers of 2 times 1ms
41202ac6454SAndrew Thompson 	 */
41302ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
41402ac6454SAndrew Thompson 	while (bit) {
41502ac6454SAndrew Thompson 		x = bit;
41602ac6454SAndrew Thompson 		while (x & bit) {
41702ac6454SAndrew Thompson 			ehci_qh_t *qh_x;
41802ac6454SAndrew Thompson 			ehci_qh_t *qh_y;
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson 			y = (x ^ bit) | (bit / 2);
42102ac6454SAndrew Thompson 
42202ac6454SAndrew Thompson 			qh_x = sc->sc_intr_p_last[x];
42302ac6454SAndrew Thompson 			qh_y = sc->sc_intr_p_last[y];
42402ac6454SAndrew Thompson 
42502ac6454SAndrew Thompson 			/*
42602ac6454SAndrew Thompson 			 * the next QH has half the poll interval
42702ac6454SAndrew Thompson 			 */
42802ac6454SAndrew Thompson 			qh_x->qh_link = qh_y->qh_self;
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson 			x++;
43102ac6454SAndrew Thompson 		}
43202ac6454SAndrew Thompson 		bit >>= 1;
43302ac6454SAndrew Thompson 	}
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson 	if (1) {
43602ac6454SAndrew Thompson 		ehci_qh_t *qh;
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson 		qh = sc->sc_intr_p_last[0];
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 		/* the last (1ms) QH terminates */
441e55e1ebcSAndrew Thompson 		qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
44202ac6454SAndrew Thompson 	}
44302ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
44402ac6454SAndrew Thompson 		ehci_sitd_t *sitd;
44502ac6454SAndrew Thompson 		ehci_itd_t *itd;
44602ac6454SAndrew Thompson 
447a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 		sitd = buf_res.buffer;
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 		/* initialize page cache pointer */
45202ac6454SAndrew Thompson 
45302ac6454SAndrew Thompson 		sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
45402ac6454SAndrew Thompson 
45502ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 		sc->sc_isoc_fs_p_last[i] = sitd;
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 		/* initialize full speed isochronous */
46002ac6454SAndrew Thompson 
46102ac6454SAndrew Thompson 		sitd->sitd_self =
462e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
463e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_SITD);
46402ac6454SAndrew Thompson 
46502ac6454SAndrew Thompson 		sitd->sitd_back =
466e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
46702ac6454SAndrew Thompson 
46802ac6454SAndrew Thompson 		sitd->sitd_next =
46902ac6454SAndrew Thompson 		    sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson 
472a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson 		itd = buf_res.buffer;
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson 		/* initialize page cache pointer */
47702ac6454SAndrew Thompson 
47802ac6454SAndrew Thompson 		itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson 		sc->sc_isoc_hs_p_last[i] = itd;
48302ac6454SAndrew Thompson 
48402ac6454SAndrew Thompson 		/* initialize high speed isochronous */
48502ac6454SAndrew Thompson 
48602ac6454SAndrew Thompson 		itd->itd_self =
487e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
488e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_ITD);
48902ac6454SAndrew Thompson 
49002ac6454SAndrew Thompson 		itd->itd_next =
49102ac6454SAndrew Thompson 		    sitd->sitd_self;
49202ac6454SAndrew Thompson 	}
49302ac6454SAndrew Thompson 
494a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson 	if (1) {
49702ac6454SAndrew Thompson 		uint32_t *pframes;
49802ac6454SAndrew Thompson 
49902ac6454SAndrew Thompson 		pframes = buf_res.buffer;
50002ac6454SAndrew Thompson 
50102ac6454SAndrew Thompson 		/*
50202ac6454SAndrew Thompson 		 * execution order:
50302ac6454SAndrew Thompson 		 * pframes -> high speed isochronous ->
50402ac6454SAndrew Thompson 		 *    full speed isochronous -> interrupt QH's
50502ac6454SAndrew Thompson 		 */
50602ac6454SAndrew Thompson 		for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
50702ac6454SAndrew Thompson 			pframes[i] = sc->sc_isoc_hs_p_last
50802ac6454SAndrew Thompson 			    [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
50902ac6454SAndrew Thompson 		}
51002ac6454SAndrew Thompson 	}
511a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson 	if (1) {
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 		ehci_qh_t *qh;
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 		qh = buf_res.buffer;
51802ac6454SAndrew Thompson 
51902ac6454SAndrew Thompson 		/* initialize page cache pointer */
52002ac6454SAndrew Thompson 
52102ac6454SAndrew Thompson 		qh->page_cache = &sc->sc_hw.async_start_pc;
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson 		/* store a pointer to the queue head */
52402ac6454SAndrew Thompson 
52502ac6454SAndrew Thompson 		sc->sc_async_p_last = qh;
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson 		/* init dummy QH that starts the async list */
52802ac6454SAndrew Thompson 
52902ac6454SAndrew Thompson 		qh->qh_self =
530e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
531e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
53202ac6454SAndrew Thompson 
53302ac6454SAndrew Thompson 		/* fill the QH */
53402ac6454SAndrew Thompson 		qh->qh_endp =
535e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
536e55e1ebcSAndrew Thompson 		qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
53702ac6454SAndrew Thompson 		qh->qh_link = qh->qh_self;
53802ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
53902ac6454SAndrew Thompson 
54002ac6454SAndrew Thompson 		/* fill the overlay qTD */
541e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
542e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
543e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
54402ac6454SAndrew Thompson 	}
54502ac6454SAndrew Thompson 	/* flush all cache into memory */
54602ac6454SAndrew Thompson 
547a593f6b8SAndrew Thompson 	usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
54802ac6454SAndrew Thompson 
549b850ecc1SAndrew Thompson #ifdef USB_DEBUG
55002ac6454SAndrew Thompson 	if (ehcidebug) {
55102ac6454SAndrew Thompson 		ehci_dump_sqh(sc, sc->sc_async_p_last);
55202ac6454SAndrew Thompson 	}
55302ac6454SAndrew Thompson #endif
55402ac6454SAndrew Thompson 
5552e141748SHans Petter Selasky 	/* finial setup */
5562e141748SHans Petter Selasky  	err = ehci_init_sub(sc);
55702ac6454SAndrew Thompson 
55802ac6454SAndrew Thompson 	if (!err) {
55902ac6454SAndrew Thompson 		/* catch any lost interrupts */
56002ac6454SAndrew Thompson 		ehci_do_poll(&sc->sc_bus);
56102ac6454SAndrew Thompson 	}
56202ac6454SAndrew Thompson 	return (err);
56302ac6454SAndrew Thompson }
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson /*
56602ac6454SAndrew Thompson  * shut down the controller when the system is going down
56702ac6454SAndrew Thompson  */
56802ac6454SAndrew Thompson void
56902ac6454SAndrew Thompson ehci_detach(ehci_softc_t *sc)
57002ac6454SAndrew Thompson {
57102ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
57202ac6454SAndrew Thompson 
573a593f6b8SAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_pcd);
57430b22abeSAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_poll);
57502ac6454SAndrew Thompson 
576bda9babeSAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, 0);
577e55e1ebcSAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
57802ac6454SAndrew Thompson 
579e55e1ebcSAndrew Thompson 	if (ehci_hcreset(sc)) {
58002ac6454SAndrew Thompson 		DPRINTF("reset failed!\n");
58102ac6454SAndrew Thompson 	}
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson 	/* XXX let stray task complete */
584a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL, hz / 20);
58502ac6454SAndrew Thompson 
586a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_pcd);
58730b22abeSAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_poll);
58802ac6454SAndrew Thompson }
58902ac6454SAndrew Thompson 
5902e141748SHans Petter Selasky static void
59102ac6454SAndrew Thompson ehci_suspend(ehci_softc_t *sc)
59202ac6454SAndrew Thompson {
59302ac6454SAndrew Thompson 	DPRINTF("stopping the HC\n");
59402ac6454SAndrew Thompson 
5952e141748SHans Petter Selasky 	/* reset HC */
5962e141748SHans Petter Selasky 	ehci_hcreset(sc);
59702ac6454SAndrew Thompson }
5982e141748SHans Petter Selasky 
5992e141748SHans Petter Selasky static void
6002e141748SHans Petter Selasky ehci_resume(ehci_softc_t *sc)
6012e141748SHans Petter Selasky {
6022e141748SHans Petter Selasky 	/* reset HC */
6032e141748SHans Petter Selasky 	ehci_hcreset(sc);
6042e141748SHans Petter Selasky 
6052e141748SHans Petter Selasky 	/* setup HC */
6062e141748SHans Petter Selasky 	ehci_init_sub(sc);
6072e141748SHans Petter Selasky 
6082e141748SHans Petter Selasky 	/* catch any lost interrupts */
6092e141748SHans Petter Selasky 	ehci_do_poll(&sc->sc_bus);
61002ac6454SAndrew Thompson }
61102ac6454SAndrew Thompson 
612b850ecc1SAndrew Thompson #ifdef USB_DEBUG
61302ac6454SAndrew Thompson static void
61402ac6454SAndrew Thompson ehci_dump_regs(ehci_softc_t *sc)
61502ac6454SAndrew Thompson {
61602ac6454SAndrew Thompson 	uint32_t i;
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBCMD);
61902ac6454SAndrew Thompson 	printf("cmd=0x%08x\n", i);
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_1)
62202ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_1\n");
62302ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_2)
62402ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_2\n");
62502ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_4)
62602ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_4\n");
62702ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_8)
62802ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_8\n");
62902ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_16)
63002ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_16\n");
63102ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_32)
63202ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_32\n");
63302ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_64)
63402ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_64\n");
63502ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPME)
63602ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPME\n");
63702ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPMC)
63802ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPMC\n");
63902ac6454SAndrew Thompson 	if (i & EHCI_CMD_LHCR)
64002ac6454SAndrew Thompson 		printf(" EHCI_CMD_LHCR\n");
64102ac6454SAndrew Thompson 	if (i & EHCI_CMD_IAAD)
64202ac6454SAndrew Thompson 		printf(" EHCI_CMD_IAAD\n");
64302ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASE)
64402ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASE\n");
64502ac6454SAndrew Thompson 	if (i & EHCI_CMD_PSE)
64602ac6454SAndrew Thompson 		printf(" EHCI_CMD_PSE\n");
64702ac6454SAndrew Thompson 	if (i & EHCI_CMD_FLS_M)
64802ac6454SAndrew Thompson 		printf(" EHCI_CMD_FLS_M\n");
64902ac6454SAndrew Thompson 	if (i & EHCI_CMD_HCRESET)
65002ac6454SAndrew Thompson 		printf(" EHCI_CMD_HCRESET\n");
65102ac6454SAndrew Thompson 	if (i & EHCI_CMD_RS)
65202ac6454SAndrew Thompson 		printf(" EHCI_CMD_RS\n");
65302ac6454SAndrew Thompson 
65402ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBSTS);
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 	printf("sts=0x%08x\n", i);
65702ac6454SAndrew Thompson 
65802ac6454SAndrew Thompson 	if (i & EHCI_STS_ASS)
65902ac6454SAndrew Thompson 		printf(" EHCI_STS_ASS\n");
66002ac6454SAndrew Thompson 	if (i & EHCI_STS_PSS)
66102ac6454SAndrew Thompson 		printf(" EHCI_STS_PSS\n");
66202ac6454SAndrew Thompson 	if (i & EHCI_STS_REC)
66302ac6454SAndrew Thompson 		printf(" EHCI_STS_REC\n");
66402ac6454SAndrew Thompson 	if (i & EHCI_STS_HCH)
66502ac6454SAndrew Thompson 		printf(" EHCI_STS_HCH\n");
66602ac6454SAndrew Thompson 	if (i & EHCI_STS_IAA)
66702ac6454SAndrew Thompson 		printf(" EHCI_STS_IAA\n");
66802ac6454SAndrew Thompson 	if (i & EHCI_STS_HSE)
66902ac6454SAndrew Thompson 		printf(" EHCI_STS_HSE\n");
67002ac6454SAndrew Thompson 	if (i & EHCI_STS_FLR)
67102ac6454SAndrew Thompson 		printf(" EHCI_STS_FLR\n");
67202ac6454SAndrew Thompson 	if (i & EHCI_STS_PCD)
67302ac6454SAndrew Thompson 		printf(" EHCI_STS_PCD\n");
67402ac6454SAndrew Thompson 	if (i & EHCI_STS_ERRINT)
67502ac6454SAndrew Thompson 		printf(" EHCI_STS_ERRINT\n");
67602ac6454SAndrew Thompson 	if (i & EHCI_STS_INT)
67702ac6454SAndrew Thompson 		printf(" EHCI_STS_INT\n");
67802ac6454SAndrew Thompson 
67902ac6454SAndrew Thompson 	printf("ien=0x%08x\n",
68002ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_USBINTR));
68102ac6454SAndrew Thompson 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
68202ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_FRINDEX),
68302ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
68402ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
68502ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
68602ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
68702ac6454SAndrew Thompson 		printf("port %d status=0x%08x\n", i,
68802ac6454SAndrew Thompson 		    EOREAD4(sc, EHCI_PORTSC(i)));
68902ac6454SAndrew Thompson 	}
69002ac6454SAndrew Thompson }
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson static void
69302ac6454SAndrew Thompson ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
69402ac6454SAndrew Thompson {
695e55e1ebcSAndrew Thompson 	link = hc32toh(sc, link);
69602ac6454SAndrew Thompson 	printf("0x%08x", link);
69702ac6454SAndrew Thompson 	if (link & EHCI_LINK_TERMINATE)
69802ac6454SAndrew Thompson 		printf("<T>");
69902ac6454SAndrew Thompson 	else {
70002ac6454SAndrew Thompson 		printf("<");
70102ac6454SAndrew Thompson 		if (type) {
70202ac6454SAndrew Thompson 			switch (EHCI_LINK_TYPE(link)) {
70302ac6454SAndrew Thompson 			case EHCI_LINK_ITD:
70402ac6454SAndrew Thompson 				printf("ITD");
70502ac6454SAndrew Thompson 				break;
70602ac6454SAndrew Thompson 			case EHCI_LINK_QH:
70702ac6454SAndrew Thompson 				printf("QH");
70802ac6454SAndrew Thompson 				break;
70902ac6454SAndrew Thompson 			case EHCI_LINK_SITD:
71002ac6454SAndrew Thompson 				printf("SITD");
71102ac6454SAndrew Thompson 				break;
71202ac6454SAndrew Thompson 			case EHCI_LINK_FSTN:
71302ac6454SAndrew Thompson 				printf("FSTN");
71402ac6454SAndrew Thompson 				break;
71502ac6454SAndrew Thompson 			}
71602ac6454SAndrew Thompson 		}
71702ac6454SAndrew Thompson 		printf(">");
71802ac6454SAndrew Thompson 	}
71902ac6454SAndrew Thompson }
72002ac6454SAndrew Thompson 
72102ac6454SAndrew Thompson static void
72202ac6454SAndrew Thompson ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
72302ac6454SAndrew Thompson {
72402ac6454SAndrew Thompson 	uint32_t s;
72502ac6454SAndrew Thompson 
72602ac6454SAndrew Thompson 	printf("  next=");
72702ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_next, 0);
72802ac6454SAndrew Thompson 	printf(" altnext=");
72902ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_altnext, 0);
73002ac6454SAndrew Thompson 	printf("\n");
731e55e1ebcSAndrew Thompson 	s = hc32toh(sc, qtd->qtd_status);
73202ac6454SAndrew Thompson 	printf("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
73302ac6454SAndrew Thompson 	    s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
73402ac6454SAndrew Thompson 	    EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
73502ac6454SAndrew Thompson 	printf("    cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
73602ac6454SAndrew Thompson 	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
73702ac6454SAndrew Thompson 	    (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
73802ac6454SAndrew Thompson 	    (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
73902ac6454SAndrew Thompson 	    (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
74002ac6454SAndrew Thompson 	    (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
74102ac6454SAndrew Thompson 	    (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
74202ac6454SAndrew Thompson 	    (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
74302ac6454SAndrew Thompson 	    (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
74402ac6454SAndrew Thompson 	    (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
74502ac6454SAndrew Thompson 
74602ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
74702ac6454SAndrew Thompson 		printf("  buffer[%d]=0x%08x\n", s,
748e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer[s]));
74902ac6454SAndrew Thompson 	}
75002ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
75102ac6454SAndrew Thompson 		printf("  buffer_hi[%d]=0x%08x\n", s,
752e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer_hi[s]));
75302ac6454SAndrew Thompson 	}
75402ac6454SAndrew Thompson }
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson static uint8_t
75702ac6454SAndrew Thompson ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
75802ac6454SAndrew Thompson {
75902ac6454SAndrew Thompson 	uint8_t temp;
76002ac6454SAndrew Thompson 
761a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sqtd->page_cache);
762e55e1ebcSAndrew Thompson 	printf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
76302ac6454SAndrew Thompson 	ehci_dump_qtd(sc, sqtd);
764e55e1ebcSAndrew Thompson 	temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
76502ac6454SAndrew Thompson 	return (temp);
76602ac6454SAndrew Thompson }
76702ac6454SAndrew Thompson 
76802ac6454SAndrew Thompson static void
76902ac6454SAndrew Thompson ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
77002ac6454SAndrew Thompson {
77102ac6454SAndrew Thompson 	uint16_t i;
77202ac6454SAndrew Thompson 	uint8_t stop;
77302ac6454SAndrew Thompson 
77402ac6454SAndrew Thompson 	stop = 0;
77502ac6454SAndrew Thompson 	for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
77602ac6454SAndrew Thompson 		stop = ehci_dump_sqtd(sc, sqtd);
77702ac6454SAndrew Thompson 	}
77802ac6454SAndrew Thompson 	if (sqtd) {
77902ac6454SAndrew Thompson 		printf("dump aborted, too many TDs\n");
78002ac6454SAndrew Thompson 	}
78102ac6454SAndrew Thompson }
78202ac6454SAndrew Thompson 
78302ac6454SAndrew Thompson static void
78402ac6454SAndrew Thompson ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
78502ac6454SAndrew Thompson {
78602ac6454SAndrew Thompson 	uint32_t endp;
78702ac6454SAndrew Thompson 	uint32_t endphub;
78802ac6454SAndrew Thompson 
789a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
790e55e1ebcSAndrew Thompson 	printf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
79102ac6454SAndrew Thompson 	printf("  link=");
79202ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_link, 1);
79302ac6454SAndrew Thompson 	printf("\n");
794e55e1ebcSAndrew Thompson 	endp = hc32toh(sc, qh->qh_endp);
79502ac6454SAndrew Thompson 	printf("  endp=0x%08x\n", endp);
79602ac6454SAndrew Thompson 	printf("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
79702ac6454SAndrew Thompson 	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
79802ac6454SAndrew Thompson 	    EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
79902ac6454SAndrew Thompson 	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
80002ac6454SAndrew Thompson 	printf("    mpl=0x%x ctl=%d nrl=%d\n",
80102ac6454SAndrew Thompson 	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
80202ac6454SAndrew Thompson 	    EHCI_QH_GET_NRL(endp));
803e55e1ebcSAndrew Thompson 	endphub = hc32toh(sc, qh->qh_endphub);
80402ac6454SAndrew Thompson 	printf("  endphub=0x%08x\n", endphub);
80502ac6454SAndrew Thompson 	printf("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
80602ac6454SAndrew Thompson 	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
80702ac6454SAndrew Thompson 	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
80802ac6454SAndrew Thompson 	    EHCI_QH_GET_MULT(endphub));
80902ac6454SAndrew Thompson 	printf("  curqtd=");
81002ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_curqtd, 0);
81102ac6454SAndrew Thompson 	printf("\n");
81202ac6454SAndrew Thompson 	printf("Overlay qTD:\n");
81302ac6454SAndrew Thompson 	ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
81402ac6454SAndrew Thompson }
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson static void
81702ac6454SAndrew Thompson ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
81802ac6454SAndrew Thompson {
819a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sitd->page_cache);
820e55e1ebcSAndrew Thompson 	printf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
821e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
82202ac6454SAndrew Thompson 	printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
823e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_portaddr),
824e55e1ebcSAndrew Thompson 	    (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
82502ac6454SAndrew Thompson 	    ? "in" : "out",
826e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
827e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
828e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
829e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
830e55e1ebcSAndrew Thompson 	printf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
831e55e1ebcSAndrew Thompson 	printf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
832e55e1ebcSAndrew Thompson 	    (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
833e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
83402ac6454SAndrew Thompson 	printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
835e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_back),
836e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[0]),
837e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[1]),
838e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[0]),
839e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[1]));
84002ac6454SAndrew Thompson }
84102ac6454SAndrew Thompson 
84202ac6454SAndrew Thompson static void
84302ac6454SAndrew Thompson ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
84402ac6454SAndrew Thompson {
845a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(itd->page_cache);
846e55e1ebcSAndrew Thompson 	printf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
847e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
848e55e1ebcSAndrew Thompson 	printf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
849e55e1ebcSAndrew Thompson 	    (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
850e55e1ebcSAndrew Thompson 	printf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
851e55e1ebcSAndrew Thompson 	    (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
852e55e1ebcSAndrew Thompson 	printf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
853e55e1ebcSAndrew Thompson 	    (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
854e55e1ebcSAndrew Thompson 	printf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
855e55e1ebcSAndrew Thompson 	    (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
856e55e1ebcSAndrew Thompson 	printf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
857e55e1ebcSAndrew Thompson 	    (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
858e55e1ebcSAndrew Thompson 	printf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
859e55e1ebcSAndrew Thompson 	    (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
860e55e1ebcSAndrew Thompson 	printf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
861e55e1ebcSAndrew Thompson 	    (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
862e55e1ebcSAndrew Thompson 	printf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
863e55e1ebcSAndrew Thompson 	    (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
864e55e1ebcSAndrew Thompson 	printf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
86502ac6454SAndrew Thompson 	printf("  addr=0x%02x; endpt=0x%01x\n",
866e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
867e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
868e55e1ebcSAndrew Thompson 	printf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
86902ac6454SAndrew Thompson 	printf(" dir=%s; mpl=0x%02x\n",
870e55e1ebcSAndrew Thompson 	    (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
871e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
87202ac6454SAndrew Thompson 	printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
873e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[2]),
874e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[3]),
875e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[4]),
876e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[5]),
877e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[6]));
87802ac6454SAndrew Thompson 	printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
87902ac6454SAndrew Thompson 	    "       0x%08x,0x%08x,0x%08x\n",
880e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[0]),
881e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[1]),
882e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[2]),
883e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[3]),
884e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[4]),
885e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[5]),
886e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[6]));
88702ac6454SAndrew Thompson }
88802ac6454SAndrew Thompson 
88902ac6454SAndrew Thompson static void
89002ac6454SAndrew Thompson ehci_dump_isoc(ehci_softc_t *sc)
89102ac6454SAndrew Thompson {
89202ac6454SAndrew Thompson 	ehci_itd_t *itd;
89302ac6454SAndrew Thompson 	ehci_sitd_t *sitd;
89402ac6454SAndrew Thompson 	uint16_t max = 1000;
89502ac6454SAndrew Thompson 	uint16_t pos;
89602ac6454SAndrew Thompson 
89702ac6454SAndrew Thompson 	pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
89802ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
89902ac6454SAndrew Thompson 
90002ac6454SAndrew Thompson 	printf("%s: isochronous dump from frame 0x%03x:\n",
90102ac6454SAndrew Thompson 	    __FUNCTION__, pos);
90202ac6454SAndrew Thompson 
90302ac6454SAndrew Thompson 	itd = sc->sc_isoc_hs_p_last[pos];
90402ac6454SAndrew Thompson 	sitd = sc->sc_isoc_fs_p_last[pos];
90502ac6454SAndrew Thompson 
90602ac6454SAndrew Thompson 	while (itd && max && max--) {
90702ac6454SAndrew Thompson 		ehci_dump_itd(sc, itd);
90802ac6454SAndrew Thompson 		itd = itd->prev;
90902ac6454SAndrew Thompson 	}
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson 	while (sitd && max && max--) {
91202ac6454SAndrew Thompson 		ehci_dump_sitd(sc, sitd);
91302ac6454SAndrew Thompson 		sitd = sitd->prev;
91402ac6454SAndrew Thompson 	}
91502ac6454SAndrew Thompson }
91602ac6454SAndrew Thompson 
91702ac6454SAndrew Thompson #endif
91802ac6454SAndrew Thompson 
91902ac6454SAndrew Thompson static void
920760bc48eSAndrew Thompson ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
92102ac6454SAndrew Thompson {
92202ac6454SAndrew Thompson 	/* check for early completion */
92302ac6454SAndrew Thompson 	if (ehci_check_transfer(xfer)) {
92402ac6454SAndrew Thompson 		return;
92502ac6454SAndrew Thompson 	}
92602ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
927a593f6b8SAndrew Thompson 	usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
92802ac6454SAndrew Thompson 
92902ac6454SAndrew Thompson 	/* start timeout, if any */
93002ac6454SAndrew Thompson 	if (xfer->timeout != 0) {
931a593f6b8SAndrew Thompson 		usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
93202ac6454SAndrew Thompson 	}
93302ac6454SAndrew Thompson }
93402ac6454SAndrew Thompson 
93502ac6454SAndrew Thompson #define	EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
93602ac6454SAndrew Thompson static ehci_sitd_t *
93702ac6454SAndrew Thompson _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
93802ac6454SAndrew Thompson {
93902ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
94002ac6454SAndrew Thompson 
94102ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 	std->next = last->next;
94402ac6454SAndrew Thompson 	std->sitd_next = last->sitd_next;
94502ac6454SAndrew Thompson 
94602ac6454SAndrew Thompson 	std->prev = last;
94702ac6454SAndrew Thompson 
948a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
94902ac6454SAndrew Thompson 
95002ac6454SAndrew Thompson 	/*
95102ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
95202ac6454SAndrew Thompson 	 */
95302ac6454SAndrew Thompson 	last->next = std;
95402ac6454SAndrew Thompson 	last->sitd_next = std->sitd_self;
95502ac6454SAndrew Thompson 
956a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson 	return (std);
95902ac6454SAndrew Thompson }
96002ac6454SAndrew Thompson 
96102ac6454SAndrew Thompson #define	EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
96202ac6454SAndrew Thompson static ehci_itd_t *
96302ac6454SAndrew Thompson _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
96402ac6454SAndrew Thompson {
96502ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
96602ac6454SAndrew Thompson 
96702ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
96802ac6454SAndrew Thompson 
96902ac6454SAndrew Thompson 	std->next = last->next;
97002ac6454SAndrew Thompson 	std->itd_next = last->itd_next;
97102ac6454SAndrew Thompson 
97202ac6454SAndrew Thompson 	std->prev = last;
97302ac6454SAndrew Thompson 
974a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
97502ac6454SAndrew Thompson 
97602ac6454SAndrew Thompson 	/*
97702ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
97802ac6454SAndrew Thompson 	 */
97902ac6454SAndrew Thompson 	last->next = std;
98002ac6454SAndrew Thompson 	last->itd_next = std->itd_self;
98102ac6454SAndrew Thompson 
982a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 	return (std);
98502ac6454SAndrew Thompson }
98602ac6454SAndrew Thompson 
98702ac6454SAndrew Thompson #define	EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
98802ac6454SAndrew Thompson static ehci_qh_t *
98902ac6454SAndrew Thompson _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
99002ac6454SAndrew Thompson {
99102ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", sqh, last);
99202ac6454SAndrew Thompson 
99302ac6454SAndrew Thompson 	if (sqh->prev != NULL) {
99402ac6454SAndrew Thompson 		/* should not happen */
99502ac6454SAndrew Thompson 		DPRINTFN(0, "QH already linked!\n");
99602ac6454SAndrew Thompson 		return (last);
99702ac6454SAndrew Thompson 	}
99802ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
99902ac6454SAndrew Thompson 
100002ac6454SAndrew Thompson 	sqh->next = last->next;
100102ac6454SAndrew Thompson 	sqh->qh_link = last->qh_link;
100202ac6454SAndrew Thompson 
100302ac6454SAndrew Thompson 	sqh->prev = last;
100402ac6454SAndrew Thompson 
1005a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(sqh->page_cache);
100602ac6454SAndrew Thompson 
100702ac6454SAndrew Thompson 	/*
100802ac6454SAndrew Thompson 	 * the last->next->prev is never followed: sqh->next->prev = sqh;
100902ac6454SAndrew Thompson 	 */
101002ac6454SAndrew Thompson 
101102ac6454SAndrew Thompson 	last->next = sqh;
101202ac6454SAndrew Thompson 	last->qh_link = sqh->qh_self;
101302ac6454SAndrew Thompson 
1014a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
101502ac6454SAndrew Thompson 
101602ac6454SAndrew Thompson 	return (sqh);
101702ac6454SAndrew Thompson }
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson #define	EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
102002ac6454SAndrew Thompson static ehci_sitd_t *
102102ac6454SAndrew Thompson _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
102202ac6454SAndrew Thompson {
102302ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson 	std->prev->next = std->next;
102802ac6454SAndrew Thompson 	std->prev->sitd_next = std->sitd_next;
102902ac6454SAndrew Thompson 
1030a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
103102ac6454SAndrew Thompson 
103202ac6454SAndrew Thompson 	if (std->next) {
103302ac6454SAndrew Thompson 		std->next->prev = std->prev;
1034a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
103502ac6454SAndrew Thompson 	}
103602ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
103702ac6454SAndrew Thompson }
103802ac6454SAndrew Thompson 
103902ac6454SAndrew Thompson #define	EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
104002ac6454SAndrew Thompson static ehci_itd_t *
104102ac6454SAndrew Thompson _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
104202ac6454SAndrew Thompson {
104302ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
104402ac6454SAndrew Thompson 
104502ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
104602ac6454SAndrew Thompson 
104702ac6454SAndrew Thompson 	std->prev->next = std->next;
104802ac6454SAndrew Thompson 	std->prev->itd_next = std->itd_next;
104902ac6454SAndrew Thompson 
1050a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
105102ac6454SAndrew Thompson 
105202ac6454SAndrew Thompson 	if (std->next) {
105302ac6454SAndrew Thompson 		std->next->prev = std->prev;
1054a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
105502ac6454SAndrew Thompson 	}
105602ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
105702ac6454SAndrew Thompson }
105802ac6454SAndrew Thompson 
105902ac6454SAndrew Thompson #define	EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
106002ac6454SAndrew Thompson static ehci_qh_t *
106102ac6454SAndrew Thompson _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
106202ac6454SAndrew Thompson {
106302ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", sqh, last);
106402ac6454SAndrew Thompson 
106502ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
106602ac6454SAndrew Thompson 
106702ac6454SAndrew Thompson 	/* only remove if not removed from a queue */
106802ac6454SAndrew Thompson 	if (sqh->prev) {
106902ac6454SAndrew Thompson 
107002ac6454SAndrew Thompson 		sqh->prev->next = sqh->next;
107102ac6454SAndrew Thompson 		sqh->prev->qh_link = sqh->qh_link;
107202ac6454SAndrew Thompson 
1073a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->prev->page_cache);
107402ac6454SAndrew Thompson 
107502ac6454SAndrew Thompson 		if (sqh->next) {
107602ac6454SAndrew Thompson 			sqh->next->prev = sqh->prev;
1077a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(sqh->next->page_cache);
107802ac6454SAndrew Thompson 		}
107902ac6454SAndrew Thompson 		last = ((last == sqh) ? sqh->prev : last);
108002ac6454SAndrew Thompson 
108102ac6454SAndrew Thompson 		sqh->prev = 0;
108202ac6454SAndrew Thompson 
1083a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->page_cache);
108402ac6454SAndrew Thompson 	}
108502ac6454SAndrew Thompson 	return (last);
108602ac6454SAndrew Thompson }
108702ac6454SAndrew Thompson 
1088dbe63d3aSHans Petter Selasky static void
1089dbe63d3aSHans Petter Selasky ehci_data_toggle_update(struct usb_xfer *xfer, uint16_t actlen, uint16_t xlen)
1090dbe63d3aSHans Petter Selasky {
1091ca794e78SHans Petter Selasky 	uint16_t rem;
1092dbe63d3aSHans Petter Selasky 	uint8_t dt;
1093dbe63d3aSHans Petter Selasky 
1094dbe63d3aSHans Petter Selasky 	/* count number of full packets */
1095dbe63d3aSHans Petter Selasky 	dt = (actlen / xfer->max_packet_size) & 1;
1096dbe63d3aSHans Petter Selasky 
1097996c2735SHans Petter Selasky 	/* compute remainder */
1098ca794e78SHans Petter Selasky 	rem = actlen % xfer->max_packet_size;
1099dbe63d3aSHans Petter Selasky 
1100ca794e78SHans Petter Selasky 	if (rem > 0)
1101dbe63d3aSHans Petter Selasky 		dt ^= 1;	/* short packet at the end */
1102ca794e78SHans Petter Selasky 	else if (actlen != xlen)
1103dbe63d3aSHans Petter Selasky 		dt ^= 1;	/* zero length packet at the end */
1104ea719edeSHans Petter Selasky 	else if (xlen == 0)
1105ea719edeSHans Petter Selasky 		dt ^= 1;	/* zero length transfer */
1106dbe63d3aSHans Petter Selasky 
1107dbe63d3aSHans Petter Selasky 	xfer->endpoint->toggle_next ^= dt;
1108dbe63d3aSHans Petter Selasky }
1109dbe63d3aSHans Petter Selasky 
1110e0a69b51SAndrew Thompson static usb_error_t
1111760bc48eSAndrew Thompson ehci_non_isoc_done_sub(struct usb_xfer *xfer)
111202ac6454SAndrew Thompson {
111302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
111402ac6454SAndrew Thompson 	ehci_qtd_t *td;
111502ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
111602ac6454SAndrew Thompson 	uint32_t status;
111702ac6454SAndrew Thompson 	uint16_t len;
111802ac6454SAndrew Thompson 
111902ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
112002ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
112102ac6454SAndrew Thompson 
112202ac6454SAndrew Thompson 	if (xfer->aframes != xfer->nframes) {
1123ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
112402ac6454SAndrew Thompson 	}
112502ac6454SAndrew Thompson 	while (1) {
112602ac6454SAndrew Thompson 
1127a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1128e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->qtd_status);
112902ac6454SAndrew Thompson 
113002ac6454SAndrew Thompson 		len = EHCI_QTD_GET_BYTES(status);
113102ac6454SAndrew Thompson 
113202ac6454SAndrew Thompson 		/*
113302ac6454SAndrew Thompson 	         * Verify the status length and
113402ac6454SAndrew Thompson 		 * add the length to "frlengths[]":
113502ac6454SAndrew Thompson 	         */
113602ac6454SAndrew Thompson 		if (len > td->len) {
113702ac6454SAndrew Thompson 			/* should not happen */
113802ac6454SAndrew Thompson 			DPRINTF("Invalid status length, "
113902ac6454SAndrew Thompson 			    "0x%04x/0x%04x bytes\n", len, td->len);
114002ac6454SAndrew Thompson 			status |= EHCI_QTD_HALTED;
114102ac6454SAndrew Thompson 		} else if (xfer->aframes != xfer->nframes) {
114202ac6454SAndrew Thompson 			xfer->frlengths[xfer->aframes] += td->len - len;
1143dbe63d3aSHans Petter Selasky 			/* manually update data toggle */
1144dbe63d3aSHans Petter Selasky 			ehci_data_toggle_update(xfer, td->len - len, td->len);
114502ac6454SAndrew Thompson 		}
1146dbe63d3aSHans Petter Selasky 
114702ac6454SAndrew Thompson 		/* Check for last transfer */
114802ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
114902ac6454SAndrew Thompson 			td = NULL;
115002ac6454SAndrew Thompson 			break;
115102ac6454SAndrew Thompson 		}
115202ac6454SAndrew Thompson 		/* Check for transfer error */
115302ac6454SAndrew Thompson 		if (status & EHCI_QTD_HALTED) {
115402ac6454SAndrew Thompson 			/* the transfer is finished */
115502ac6454SAndrew Thompson 			td = NULL;
115602ac6454SAndrew Thompson 			break;
115702ac6454SAndrew Thompson 		}
115802ac6454SAndrew Thompson 		/* Check for short transfer */
115902ac6454SAndrew Thompson 		if (len > 0) {
116002ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
116102ac6454SAndrew Thompson 				/* follow alt next */
116202ac6454SAndrew Thompson 				td = td->alt_next;
116302ac6454SAndrew Thompson 			} else {
116402ac6454SAndrew Thompson 				/* the transfer is finished */
116502ac6454SAndrew Thompson 				td = NULL;
116602ac6454SAndrew Thompson 			}
116702ac6454SAndrew Thompson 			break;
116802ac6454SAndrew Thompson 		}
116902ac6454SAndrew Thompson 		td = td->obj_next;
117002ac6454SAndrew Thompson 
117102ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
117202ac6454SAndrew Thompson 			/* this USB frame is complete */
117302ac6454SAndrew Thompson 			break;
117402ac6454SAndrew Thompson 		}
117502ac6454SAndrew Thompson 	}
117602ac6454SAndrew Thompson 
117702ac6454SAndrew Thompson 	/* update transfer cache */
117802ac6454SAndrew Thompson 
117902ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
118002ac6454SAndrew Thompson 
1181b850ecc1SAndrew Thompson #ifdef USB_DEBUG
118202ac6454SAndrew Thompson 	if (status & EHCI_QTD_STATERRS) {
118302ac6454SAndrew Thompson 		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
118402ac6454SAndrew Thompson 		    "status=%s%s%s%s%s%s%s%s\n",
1185ae60fdfbSAndrew Thompson 		    xfer->address, xfer->endpointno, xfer->aframes,
118602ac6454SAndrew Thompson 		    (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
118702ac6454SAndrew Thompson 		    (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
118802ac6454SAndrew Thompson 		    (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
118902ac6454SAndrew Thompson 		    (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
119002ac6454SAndrew Thompson 		    (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
119102ac6454SAndrew Thompson 		    (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
119202ac6454SAndrew Thompson 		    (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
119302ac6454SAndrew Thompson 		    (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
119402ac6454SAndrew Thompson 	}
119502ac6454SAndrew Thompson #endif
1196a148d44fSHans Petter Selasky 	if (status & EHCI_QTD_HALTED) {
1197a148d44fSHans Petter Selasky 		if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
1198a148d44fSHans Petter Selasky 		    (xfer->xroot->udev->address != 0)) {
1199a148d44fSHans Petter Selasky 			/* try to separate I/O errors from STALL */
1200a148d44fSHans Petter Selasky 			if (EHCI_QTD_GET_CERR(status) == 0)
1201a148d44fSHans Petter Selasky 				return (USB_ERR_IOERROR);
1202a148d44fSHans Petter Selasky 		}
1203a148d44fSHans Petter Selasky 		return (USB_ERR_STALLED);
1204a148d44fSHans Petter Selasky 	}
1205a148d44fSHans Petter Selasky 	return (USB_ERR_NORMAL_COMPLETION);
120602ac6454SAndrew Thompson }
120702ac6454SAndrew Thompson 
120802ac6454SAndrew Thompson static void
1209760bc48eSAndrew Thompson ehci_non_isoc_done(struct usb_xfer *xfer)
121002ac6454SAndrew Thompson {
1211c8a14b91SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1212c8a14b91SAndrew Thompson 	ehci_qh_t *qh;
1213c8a14b91SAndrew Thompson 	uint32_t status;
1214e0a69b51SAndrew Thompson 	usb_error_t err = 0;
121502ac6454SAndrew Thompson 
1216ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1217ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
121802ac6454SAndrew Thompson 
1219b850ecc1SAndrew Thompson #ifdef USB_DEBUG
122002ac6454SAndrew Thompson 	if (ehcidebug > 10) {
122102ac6454SAndrew Thompson 		ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
122202ac6454SAndrew Thompson 
122302ac6454SAndrew Thompson 		ehci_dump_sqtds(sc, xfer->td_transfer_first);
122402ac6454SAndrew Thompson 	}
122502ac6454SAndrew Thompson #endif
122602ac6454SAndrew Thompson 
1227c8a14b91SAndrew Thompson 	/* extract data toggle directly from the QH's overlay area */
1228c8a14b91SAndrew Thompson 
1229c8a14b91SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1230c8a14b91SAndrew Thompson 
1231c8a14b91SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
1232c8a14b91SAndrew Thompson 
1233c8a14b91SAndrew Thompson 	status = hc32toh(sc, qh->qh_qtd.qtd_status);
1234c8a14b91SAndrew Thompson 
123502ac6454SAndrew Thompson 	/* reset scanner */
123602ac6454SAndrew Thompson 
123702ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
123802ac6454SAndrew Thompson 
123902ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
124002ac6454SAndrew Thompson 
124102ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
124202ac6454SAndrew Thompson 
124302ac6454SAndrew Thompson 			err = ehci_non_isoc_done_sub(xfer);
124402ac6454SAndrew Thompson 		}
124502ac6454SAndrew Thompson 		xfer->aframes = 1;
124602ac6454SAndrew Thompson 
124702ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
124802ac6454SAndrew Thompson 			goto done;
124902ac6454SAndrew Thompson 		}
125002ac6454SAndrew Thompson 	}
125102ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
125202ac6454SAndrew Thompson 
125302ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
125402ac6454SAndrew Thompson 		xfer->aframes++;
125502ac6454SAndrew Thompson 
125602ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
125702ac6454SAndrew Thompson 			goto done;
125802ac6454SAndrew Thompson 		}
125902ac6454SAndrew Thompson 	}
126002ac6454SAndrew Thompson 
126102ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
126202ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
126302ac6454SAndrew Thompson 
126402ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
126502ac6454SAndrew Thompson 	}
126602ac6454SAndrew Thompson done:
126702ac6454SAndrew Thompson 	ehci_device_done(xfer, err);
126802ac6454SAndrew Thompson }
126902ac6454SAndrew Thompson 
127002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
127102ac6454SAndrew Thompson  *	ehci_check_transfer
127202ac6454SAndrew Thompson  *
127302ac6454SAndrew Thompson  * Return values:
127402ac6454SAndrew Thompson  *    0: USB transfer is not finished
127502ac6454SAndrew Thompson  * Else: USB transfer is finished
127602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
127702ac6454SAndrew Thompson static uint8_t
1278760bc48eSAndrew Thompson ehci_check_transfer(struct usb_xfer *xfer)
127902ac6454SAndrew Thompson {
1280e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods = xfer->endpoint->methods;
128102ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
128202ac6454SAndrew Thompson 
128302ac6454SAndrew Thompson 	uint32_t status;
128402ac6454SAndrew Thompson 
128502ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
128602ac6454SAndrew Thompson 
128702ac6454SAndrew Thompson 	if (methods == &ehci_device_isoc_fs_methods) {
128802ac6454SAndrew Thompson 		ehci_sitd_t *td;
128902ac6454SAndrew Thompson 
129002ac6454SAndrew Thompson 		/* isochronous full speed transfer */
129102ac6454SAndrew Thompson 
129202ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1293a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1294e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson 		/* also check if first is complete */
129702ac6454SAndrew Thompson 
129802ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1299a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1300e55e1ebcSAndrew Thompson 		status |= hc32toh(sc, td->sitd_status);
130102ac6454SAndrew Thompson 
130202ac6454SAndrew Thompson 		if (!(status & EHCI_SITD_ACTIVE)) {
130302ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
130402ac6454SAndrew Thompson 			goto transferred;
130502ac6454SAndrew Thompson 		}
130602ac6454SAndrew Thompson 	} else if (methods == &ehci_device_isoc_hs_methods) {
130702ac6454SAndrew Thompson 		ehci_itd_t *td;
130802ac6454SAndrew Thompson 
130902ac6454SAndrew Thompson 		/* isochronous high speed transfer */
131002ac6454SAndrew Thompson 
131189119752SAndrew Thompson 		/* check last transfer */
131202ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1313a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1314d1f7c4baSAndrew Thompson 		status = td->itd_status[0];
1315d1f7c4baSAndrew Thompson 		status |= td->itd_status[1];
1316d1f7c4baSAndrew Thompson 		status |= td->itd_status[2];
1317d1f7c4baSAndrew Thompson 		status |= td->itd_status[3];
1318d1f7c4baSAndrew Thompson 		status |= td->itd_status[4];
1319d1f7c4baSAndrew Thompson 		status |= td->itd_status[5];
1320d1f7c4baSAndrew Thompson 		status |= td->itd_status[6];
1321d1f7c4baSAndrew Thompson 		status |= td->itd_status[7];
132202ac6454SAndrew Thompson 
132302ac6454SAndrew Thompson 		/* also check first transfer */
132402ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1325a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
132689119752SAndrew Thompson 		status |= td->itd_status[0];
1327d1f7c4baSAndrew Thompson 		status |= td->itd_status[1];
1328d1f7c4baSAndrew Thompson 		status |= td->itd_status[2];
1329d1f7c4baSAndrew Thompson 		status |= td->itd_status[3];
1330d1f7c4baSAndrew Thompson 		status |= td->itd_status[4];
1331d1f7c4baSAndrew Thompson 		status |= td->itd_status[5];
1332d1f7c4baSAndrew Thompson 		status |= td->itd_status[6];
1333d1f7c4baSAndrew Thompson 		status |= td->itd_status[7];
133402ac6454SAndrew Thompson 
133502ac6454SAndrew Thompson 		/* if no transactions are active we continue */
1336e55e1ebcSAndrew Thompson 		if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
133702ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
133802ac6454SAndrew Thompson 			goto transferred;
133902ac6454SAndrew Thompson 		}
134002ac6454SAndrew Thompson 	} else {
134102ac6454SAndrew Thompson 		ehci_qtd_t *td;
1342c8a14b91SAndrew Thompson 		ehci_qh_t *qh;
134302ac6454SAndrew Thompson 
134402ac6454SAndrew Thompson 		/* non-isochronous transfer */
134502ac6454SAndrew Thompson 
134602ac6454SAndrew Thompson 		/*
134702ac6454SAndrew Thompson 		 * check whether there is an error somewhere in the middle,
134802ac6454SAndrew Thompson 		 * or whether there was a short packet (SPD and not ACTIVE)
134902ac6454SAndrew Thompson 		 */
135002ac6454SAndrew Thompson 		td = xfer->td_transfer_cache;
135102ac6454SAndrew Thompson 
1352c8a14b91SAndrew Thompson 		qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1353c8a14b91SAndrew Thompson 
1354c8a14b91SAndrew Thompson 		usb_pc_cpu_invalidate(qh->page_cache);
1355c8a14b91SAndrew Thompson 
1356c8a14b91SAndrew Thompson 		status = hc32toh(sc, qh->qh_qtd.qtd_status);
1357c8a14b91SAndrew Thompson 		if (status & EHCI_QTD_ACTIVE) {
1358c8a14b91SAndrew Thompson 			/* transfer is pending */
1359c8a14b91SAndrew Thompson 			goto done;
1360c8a14b91SAndrew Thompson 		}
1361c8a14b91SAndrew Thompson 
136202ac6454SAndrew Thompson 		while (1) {
1363a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(td->page_cache);
1364e55e1ebcSAndrew Thompson 			status = hc32toh(sc, td->qtd_status);
136502ac6454SAndrew Thompson 
136602ac6454SAndrew Thompson 			/*
1367c8a14b91SAndrew Thompson 			 * Check if there is an active TD which
1368c8a14b91SAndrew Thompson 			 * indicates that the transfer isn't done.
136902ac6454SAndrew Thompson 			 */
137002ac6454SAndrew Thompson 			if (status & EHCI_QTD_ACTIVE) {
137102ac6454SAndrew Thompson 				/* update cache */
137202ac6454SAndrew Thompson 				xfer->td_transfer_cache = td;
137302ac6454SAndrew Thompson 				goto done;
137402ac6454SAndrew Thompson 			}
137502ac6454SAndrew Thompson 			/*
137602ac6454SAndrew Thompson 			 * last transfer descriptor makes the transfer done
137702ac6454SAndrew Thompson 			 */
137802ac6454SAndrew Thompson 			if (((void *)td) == xfer->td_transfer_last) {
137902ac6454SAndrew Thompson 				break;
138002ac6454SAndrew Thompson 			}
138102ac6454SAndrew Thompson 			/*
138202ac6454SAndrew Thompson 			 * any kind of error makes the transfer done
138302ac6454SAndrew Thompson 			 */
138402ac6454SAndrew Thompson 			if (status & EHCI_QTD_HALTED) {
138502ac6454SAndrew Thompson 				break;
138602ac6454SAndrew Thompson 			}
138702ac6454SAndrew Thompson 			/*
138802ac6454SAndrew Thompson 			 * if there is no alternate next transfer, a short
138902ac6454SAndrew Thompson 			 * packet also makes the transfer done
139002ac6454SAndrew Thompson 			 */
139102ac6454SAndrew Thompson 			if (EHCI_QTD_GET_BYTES(status)) {
139202ac6454SAndrew Thompson 				if (xfer->flags_int.short_frames_ok) {
139302ac6454SAndrew Thompson 					/* follow alt next */
139402ac6454SAndrew Thompson 					if (td->alt_next) {
139502ac6454SAndrew Thompson 						td = td->alt_next;
139602ac6454SAndrew Thompson 						continue;
139702ac6454SAndrew Thompson 					}
139802ac6454SAndrew Thompson 				}
139902ac6454SAndrew Thompson 				/* transfer is done */
140002ac6454SAndrew Thompson 				break;
140102ac6454SAndrew Thompson 			}
140202ac6454SAndrew Thompson 			td = td->obj_next;
140302ac6454SAndrew Thompson 		}
140402ac6454SAndrew Thompson 		ehci_non_isoc_done(xfer);
140502ac6454SAndrew Thompson 		goto transferred;
140602ac6454SAndrew Thompson 	}
140702ac6454SAndrew Thompson 
140802ac6454SAndrew Thompson done:
140902ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
141002ac6454SAndrew Thompson 	return (0);
141102ac6454SAndrew Thompson 
141202ac6454SAndrew Thompson transferred:
141302ac6454SAndrew Thompson 	return (1);
141402ac6454SAndrew Thompson }
141502ac6454SAndrew Thompson 
141602ac6454SAndrew Thompson static void
141702ac6454SAndrew Thompson ehci_pcd_enable(ehci_softc_t *sc)
141802ac6454SAndrew Thompson {
141902ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
142002ac6454SAndrew Thompson 
142102ac6454SAndrew Thompson 	sc->sc_eintrs |= EHCI_STS_PCD;
142202ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
142302ac6454SAndrew Thompson 
142402ac6454SAndrew Thompson 	/* acknowledge any PCD interrupt */
142502ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
142602ac6454SAndrew Thompson 
142739307315SAndrew Thompson 	ehci_root_intr(sc);
142802ac6454SAndrew Thompson }
142902ac6454SAndrew Thompson 
143002ac6454SAndrew Thompson static void
143102ac6454SAndrew Thompson ehci_interrupt_poll(ehci_softc_t *sc)
143202ac6454SAndrew Thompson {
1433760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
143402ac6454SAndrew Thompson 
143502ac6454SAndrew Thompson repeat:
143602ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
143702ac6454SAndrew Thompson 		/*
143802ac6454SAndrew Thompson 		 * check if transfer is transferred
143902ac6454SAndrew Thompson 		 */
144002ac6454SAndrew Thompson 		if (ehci_check_transfer(xfer)) {
144102ac6454SAndrew Thompson 			/* queue has been modified */
144202ac6454SAndrew Thompson 			goto repeat;
144302ac6454SAndrew Thompson 		}
144402ac6454SAndrew Thompson 	}
144502ac6454SAndrew Thompson }
144602ac6454SAndrew Thompson 
144730b22abeSAndrew Thompson /*
144830b22abeSAndrew Thompson  * Some EHCI chips from VIA / ATI seem to trigger interrupts before
144930b22abeSAndrew Thompson  * writing back the qTD status, or miss signalling occasionally under
145030b22abeSAndrew Thompson  * heavy load.  If the host machine is too fast, we can miss
145130b22abeSAndrew Thompson  * transaction completion - when we scan the active list the
145230b22abeSAndrew Thompson  * transaction still seems to be active. This generally exhibits
145330b22abeSAndrew Thompson  * itself as a umass stall that never recovers.
145430b22abeSAndrew Thompson  *
145530b22abeSAndrew Thompson  * We work around this behaviour by setting up this callback after any
145630b22abeSAndrew Thompson  * softintr that completes with transactions still pending, giving us
145730b22abeSAndrew Thompson  * another chance to check for completion after the writeback has
145830b22abeSAndrew Thompson  * taken place.
145930b22abeSAndrew Thompson  */
146030b22abeSAndrew Thompson static void
146130b22abeSAndrew Thompson ehci_poll_timeout(void *arg)
146230b22abeSAndrew Thompson {
146330b22abeSAndrew Thompson 	ehci_softc_t *sc = arg;
146430b22abeSAndrew Thompson 
146530b22abeSAndrew Thompson 	DPRINTFN(3, "\n");
146630b22abeSAndrew Thompson 	ehci_interrupt_poll(sc);
146730b22abeSAndrew Thompson }
146830b22abeSAndrew Thompson 
146902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
147002ac6454SAndrew Thompson  *	ehci_interrupt - EHCI interrupt handler
147102ac6454SAndrew Thompson  *
147202ac6454SAndrew Thompson  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
147302ac6454SAndrew Thompson  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
147402ac6454SAndrew Thompson  * is present !
147502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
147602ac6454SAndrew Thompson void
147702ac6454SAndrew Thompson ehci_interrupt(ehci_softc_t *sc)
147802ac6454SAndrew Thompson {
147902ac6454SAndrew Thompson 	uint32_t status;
148002ac6454SAndrew Thompson 
148102ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
148202ac6454SAndrew Thompson 
148302ac6454SAndrew Thompson 	DPRINTFN(16, "real interrupt\n");
148402ac6454SAndrew Thompson 
1485b850ecc1SAndrew Thompson #ifdef USB_DEBUG
148602ac6454SAndrew Thompson 	if (ehcidebug > 15) {
148702ac6454SAndrew Thompson 		ehci_dump_regs(sc);
148802ac6454SAndrew Thompson 	}
148902ac6454SAndrew Thompson #endif
149002ac6454SAndrew Thompson 
149102ac6454SAndrew Thompson 	status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
149202ac6454SAndrew Thompson 	if (status == 0) {
149302ac6454SAndrew Thompson 		/* the interrupt was not for us */
149402ac6454SAndrew Thompson 		goto done;
149502ac6454SAndrew Thompson 	}
149602ac6454SAndrew Thompson 	if (!(status & sc->sc_eintrs)) {
149702ac6454SAndrew Thompson 		goto done;
149802ac6454SAndrew Thompson 	}
149902ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, status);	/* acknowledge */
150002ac6454SAndrew Thompson 
150102ac6454SAndrew Thompson 	status &= sc->sc_eintrs;
150202ac6454SAndrew Thompson 
150302ac6454SAndrew Thompson 	if (status & EHCI_STS_HSE) {
150402ac6454SAndrew Thompson 		printf("%s: unrecoverable error, "
150502ac6454SAndrew Thompson 		    "controller halted\n", __FUNCTION__);
1506b850ecc1SAndrew Thompson #ifdef USB_DEBUG
150702ac6454SAndrew Thompson 		ehci_dump_regs(sc);
150802ac6454SAndrew Thompson 		ehci_dump_isoc(sc);
150902ac6454SAndrew Thompson #endif
151002ac6454SAndrew Thompson 	}
151102ac6454SAndrew Thompson 	if (status & EHCI_STS_PCD) {
151202ac6454SAndrew Thompson 		/*
151302ac6454SAndrew Thompson 		 * Disable PCD interrupt for now, because it will be
151402ac6454SAndrew Thompson 		 * on until the port has been reset.
151502ac6454SAndrew Thompson 		 */
151602ac6454SAndrew Thompson 		sc->sc_eintrs &= ~EHCI_STS_PCD;
151702ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
151802ac6454SAndrew Thompson 
151939307315SAndrew Thompson 		ehci_root_intr(sc);
152002ac6454SAndrew Thompson 
152102ac6454SAndrew Thompson 		/* do not allow RHSC interrupts > 1 per second */
1522a593f6b8SAndrew Thompson 		usb_callout_reset(&sc->sc_tmo_pcd, hz,
152302ac6454SAndrew Thompson 		    (void *)&ehci_pcd_enable, sc);
152402ac6454SAndrew Thompson 	}
152502ac6454SAndrew Thompson 	status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
152602ac6454SAndrew Thompson 
152702ac6454SAndrew Thompson 	if (status != 0) {
152802ac6454SAndrew Thompson 		/* block unprocessed interrupts */
152902ac6454SAndrew Thompson 		sc->sc_eintrs &= ~status;
153002ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
153102ac6454SAndrew Thompson 		printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
153202ac6454SAndrew Thompson 	}
153302ac6454SAndrew Thompson 	/* poll all the USB transfers */
153402ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
153502ac6454SAndrew Thompson 
153630b22abeSAndrew Thompson 	if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) {
153730b22abeSAndrew Thompson 		usb_callout_reset(&sc->sc_tmo_poll, hz / 128,
153830b22abeSAndrew Thompson 		    (void *)&ehci_poll_timeout, sc);
153930b22abeSAndrew Thompson 	}
154030b22abeSAndrew Thompson 
154102ac6454SAndrew Thompson done:
154202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
154302ac6454SAndrew Thompson }
154402ac6454SAndrew Thompson 
154502ac6454SAndrew Thompson /*
154602ac6454SAndrew Thompson  * called when a request does not complete
154702ac6454SAndrew Thompson  */
154802ac6454SAndrew Thompson static void
154902ac6454SAndrew Thompson ehci_timeout(void *arg)
155002ac6454SAndrew Thompson {
1551760bc48eSAndrew Thompson 	struct usb_xfer *xfer = arg;
155202ac6454SAndrew Thompson 
155302ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
155402ac6454SAndrew Thompson 
155502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
155602ac6454SAndrew Thompson 
155702ac6454SAndrew Thompson 	/* transfer is transferred */
155802ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_TIMEOUT);
155902ac6454SAndrew Thompson }
156002ac6454SAndrew Thompson 
156102ac6454SAndrew Thompson static void
1562760bc48eSAndrew Thompson ehci_do_poll(struct usb_bus *bus)
156302ac6454SAndrew Thompson {
156402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
156502ac6454SAndrew Thompson 
156602ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
156702ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
156802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
156902ac6454SAndrew Thompson }
157002ac6454SAndrew Thompson 
157102ac6454SAndrew Thompson static void
157202ac6454SAndrew Thompson ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
157302ac6454SAndrew Thompson {
1574760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
157502ac6454SAndrew Thompson 	ehci_qtd_t *td;
157602ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
157702ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
157802ac6454SAndrew Thompson 	uint32_t buf_offset;
157902ac6454SAndrew Thompson 	uint32_t average;
158002ac6454SAndrew Thompson 	uint32_t len_old;
15810f7d4548SAndrew Thompson 	uint32_t terminate;
158253e0bf6eSHans Petter Selasky 	uint32_t qtd_altnext;
158302ac6454SAndrew Thompson 	uint8_t shortpkt_old;
158402ac6454SAndrew Thompson 	uint8_t precompute;
158502ac6454SAndrew Thompson 
158653e0bf6eSHans Petter Selasky 	terminate = temp->sc->sc_terminate_self;
158753e0bf6eSHans Petter Selasky 	qtd_altnext = temp->sc->sc_terminate_self;
158802ac6454SAndrew Thompson 	td_alt_next = NULL;
158902ac6454SAndrew Thompson 	buf_offset = 0;
159002ac6454SAndrew Thompson 	shortpkt_old = temp->shortpkt;
159102ac6454SAndrew Thompson 	len_old = temp->len;
159202ac6454SAndrew Thompson 	precompute = 1;
159302ac6454SAndrew Thompson 
159402ac6454SAndrew Thompson restart:
159502ac6454SAndrew Thompson 
159602ac6454SAndrew Thompson 	td = temp->td;
159702ac6454SAndrew Thompson 	td_next = temp->td_next;
159802ac6454SAndrew Thompson 
159902ac6454SAndrew Thompson 	while (1) {
160002ac6454SAndrew Thompson 
160102ac6454SAndrew Thompson 		if (temp->len == 0) {
160202ac6454SAndrew Thompson 
160302ac6454SAndrew Thompson 			if (temp->shortpkt) {
160402ac6454SAndrew Thompson 				break;
160502ac6454SAndrew Thompson 			}
160602ac6454SAndrew Thompson 			/* send a Zero Length Packet, ZLP, last */
160702ac6454SAndrew Thompson 
160802ac6454SAndrew Thompson 			temp->shortpkt = 1;
160902ac6454SAndrew Thompson 			average = 0;
161002ac6454SAndrew Thompson 
161102ac6454SAndrew Thompson 		} else {
161202ac6454SAndrew Thompson 
161302ac6454SAndrew Thompson 			average = temp->average;
161402ac6454SAndrew Thompson 
161502ac6454SAndrew Thompson 			if (temp->len < average) {
161602ac6454SAndrew Thompson 				if (temp->len % temp->max_frame_size) {
161702ac6454SAndrew Thompson 					temp->shortpkt = 1;
161802ac6454SAndrew Thompson 				}
161902ac6454SAndrew Thompson 				average = temp->len;
162002ac6454SAndrew Thompson 			}
162102ac6454SAndrew Thompson 		}
162202ac6454SAndrew Thompson 
162302ac6454SAndrew Thompson 		if (td_next == NULL) {
162402ac6454SAndrew Thompson 			panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
162502ac6454SAndrew Thompson 		}
162602ac6454SAndrew Thompson 		/* get next TD */
162702ac6454SAndrew Thompson 
162802ac6454SAndrew Thompson 		td = td_next;
162902ac6454SAndrew Thompson 		td_next = td->obj_next;
163002ac6454SAndrew Thompson 
163102ac6454SAndrew Thompson 		/* check if we are pre-computing */
163202ac6454SAndrew Thompson 
163302ac6454SAndrew Thompson 		if (precompute) {
163402ac6454SAndrew Thompson 
163502ac6454SAndrew Thompson 			/* update remaining length */
163602ac6454SAndrew Thompson 
163702ac6454SAndrew Thompson 			temp->len -= average;
163802ac6454SAndrew Thompson 
163902ac6454SAndrew Thompson 			continue;
164002ac6454SAndrew Thompson 		}
164102ac6454SAndrew Thompson 		/* fill out current TD */
164202ac6454SAndrew Thompson 
164302ac6454SAndrew Thompson 		td->qtd_status =
164402ac6454SAndrew Thompson 		    temp->qtd_status |
1645c8a14b91SAndrew Thompson 		    htohc32(temp->sc, EHCI_QTD_IOC |
1646c8a14b91SAndrew Thompson 			EHCI_QTD_SET_BYTES(average));
164702ac6454SAndrew Thompson 
164802ac6454SAndrew Thompson 		if (average == 0) {
164902ac6454SAndrew Thompson 
165002ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
165102ac6454SAndrew Thompson 
165202ac6454SAndrew Thompson 				/* update data toggle, ZLP case */
165302ac6454SAndrew Thompson 
165402ac6454SAndrew Thompson 				temp->qtd_status ^=
1655e55e1ebcSAndrew Thompson 				    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
165602ac6454SAndrew Thompson 			}
165702ac6454SAndrew Thompson 			td->len = 0;
165802ac6454SAndrew Thompson 
16599ae7f7c1SHans Petter Selasky 			/* properly reset reserved fields */
166002ac6454SAndrew Thompson 			td->qtd_buffer[0] = 0;
166102ac6454SAndrew Thompson 			td->qtd_buffer[1] = 0;
16629ae7f7c1SHans Petter Selasky 			td->qtd_buffer[2] = 0;
16639ae7f7c1SHans Petter Selasky 			td->qtd_buffer[3] = 0;
16649ae7f7c1SHans Petter Selasky 			td->qtd_buffer[4] = 0;
16659ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[0] = 0;
166602ac6454SAndrew Thompson 			td->qtd_buffer_hi[1] = 0;
16679ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[2] = 0;
16689ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[3] = 0;
16699ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[4] = 0;
167002ac6454SAndrew Thompson 		} else {
167102ac6454SAndrew Thompson 
167202ac6454SAndrew Thompson 			uint8_t x;
167302ac6454SAndrew Thompson 
167402ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
167502ac6454SAndrew Thompson 
167602ac6454SAndrew Thompson 				/* update data toggle */
167702ac6454SAndrew Thompson 
167802ac6454SAndrew Thompson 				if (((average + temp->max_frame_size - 1) /
167902ac6454SAndrew Thompson 				    temp->max_frame_size) & 1) {
168002ac6454SAndrew Thompson 					temp->qtd_status ^=
1681e55e1ebcSAndrew Thompson 					    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
168202ac6454SAndrew Thompson 				}
168302ac6454SAndrew Thompson 			}
168402ac6454SAndrew Thompson 			td->len = average;
168502ac6454SAndrew Thompson 
168602ac6454SAndrew Thompson 			/* update remaining length */
168702ac6454SAndrew Thompson 
168802ac6454SAndrew Thompson 			temp->len -= average;
168902ac6454SAndrew Thompson 
169002ac6454SAndrew Thompson 			/* fill out buffer pointers */
169102ac6454SAndrew Thompson 
1692a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset, &buf_res);
169302ac6454SAndrew Thompson 			td->qtd_buffer[0] =
1694e55e1ebcSAndrew Thompson 			    htohc32(temp->sc, buf_res.physaddr);
169502ac6454SAndrew Thompson 			td->qtd_buffer_hi[0] = 0;
169602ac6454SAndrew Thompson 
169702ac6454SAndrew Thompson 			x = 1;
169802ac6454SAndrew Thompson 
169902ac6454SAndrew Thompson 			while (average > EHCI_PAGE_SIZE) {
170002ac6454SAndrew Thompson 				average -= EHCI_PAGE_SIZE;
170102ac6454SAndrew Thompson 				buf_offset += EHCI_PAGE_SIZE;
1702a593f6b8SAndrew Thompson 				usbd_get_page(temp->pc, buf_offset, &buf_res);
170302ac6454SAndrew Thompson 				td->qtd_buffer[x] =
1704e55e1ebcSAndrew Thompson 				    htohc32(temp->sc,
170502ac6454SAndrew Thompson 				    buf_res.physaddr & (~0xFFF));
170602ac6454SAndrew Thompson 				td->qtd_buffer_hi[x] = 0;
170702ac6454SAndrew Thompson 				x++;
170802ac6454SAndrew Thompson 			}
170902ac6454SAndrew Thompson 
171002ac6454SAndrew Thompson 			/*
171102ac6454SAndrew Thompson 			 * NOTE: The "average" variable is never zero after
171202ac6454SAndrew Thompson 			 * exiting the loop above !
171302ac6454SAndrew Thompson 			 *
171402ac6454SAndrew Thompson 			 * NOTE: We have to subtract one from the offset to
171502ac6454SAndrew Thompson 			 * ensure that we are computing the physical address
171602ac6454SAndrew Thompson 			 * of a valid page !
171702ac6454SAndrew Thompson 			 */
171802ac6454SAndrew Thompson 			buf_offset += average;
1719a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
172002ac6454SAndrew Thompson 			td->qtd_buffer[x] =
1721e55e1ebcSAndrew Thompson 			    htohc32(temp->sc,
172202ac6454SAndrew Thompson 			    buf_res.physaddr & (~0xFFF));
172302ac6454SAndrew Thompson 			td->qtd_buffer_hi[x] = 0;
17249ae7f7c1SHans Petter Selasky 
17259ae7f7c1SHans Petter Selasky 			/* properly reset reserved fields */
17269ae7f7c1SHans Petter Selasky 			while (++x < EHCI_QTD_NBUFFERS) {
17279ae7f7c1SHans Petter Selasky 				td->qtd_buffer[x] = 0;
17289ae7f7c1SHans Petter Selasky 				td->qtd_buffer_hi[x] = 0;
17299ae7f7c1SHans Petter Selasky 			}
173002ac6454SAndrew Thompson 		}
173102ac6454SAndrew Thompson 
173202ac6454SAndrew Thompson 		if (td_next) {
173302ac6454SAndrew Thompson 			/* link the current TD with the next one */
173402ac6454SAndrew Thompson 			td->qtd_next = td_next->qtd_self;
173502ac6454SAndrew Thompson 		}
173653e0bf6eSHans Petter Selasky 		td->qtd_altnext = qtd_altnext;
173702ac6454SAndrew Thompson 		td->alt_next = td_alt_next;
173802ac6454SAndrew Thompson 
1739a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
174002ac6454SAndrew Thompson 	}
174102ac6454SAndrew Thompson 
174202ac6454SAndrew Thompson 	if (precompute) {
174302ac6454SAndrew Thompson 		precompute = 0;
174402ac6454SAndrew Thompson 
174502ac6454SAndrew Thompson 		/* setup alt next pointer, if any */
17460f7d4548SAndrew Thompson 		if (temp->last_frame) {
17470f7d4548SAndrew Thompson 			td_alt_next = NULL;
174853e0bf6eSHans Petter Selasky 			qtd_altnext = terminate;
174902ac6454SAndrew Thompson 		} else {
175002ac6454SAndrew Thompson 			/* we use this field internally */
175102ac6454SAndrew Thompson 			td_alt_next = td_next;
175253e0bf6eSHans Petter Selasky 			if (temp->setup_alt_next) {
175353e0bf6eSHans Petter Selasky 				qtd_altnext = td_next->qtd_self;
175453e0bf6eSHans Petter Selasky 			} else {
175553e0bf6eSHans Petter Selasky 				qtd_altnext = terminate;
175653e0bf6eSHans Petter Selasky 			}
175702ac6454SAndrew Thompson 		}
175802ac6454SAndrew Thompson 
175902ac6454SAndrew Thompson 		/* restore */
176002ac6454SAndrew Thompson 		temp->shortpkt = shortpkt_old;
176102ac6454SAndrew Thompson 		temp->len = len_old;
176202ac6454SAndrew Thompson 		goto restart;
176302ac6454SAndrew Thompson 	}
176402ac6454SAndrew Thompson 	temp->td = td;
176502ac6454SAndrew Thompson 	temp->td_next = td_next;
176602ac6454SAndrew Thompson }
176702ac6454SAndrew Thompson 
176802ac6454SAndrew Thompson static void
1769760bc48eSAndrew Thompson ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
177002ac6454SAndrew Thompson {
177102ac6454SAndrew Thompson 	struct ehci_std_temp temp;
1772e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
177302ac6454SAndrew Thompson 	ehci_qh_t *qh;
177402ac6454SAndrew Thompson 	ehci_qtd_t *td;
177502ac6454SAndrew Thompson 	uint32_t qh_endp;
177602ac6454SAndrew Thompson 	uint32_t qh_endphub;
177702ac6454SAndrew Thompson 	uint32_t x;
177802ac6454SAndrew Thompson 
177902ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1780ae60fdfbSAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
1781a593f6b8SAndrew Thompson 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
178202ac6454SAndrew Thompson 
1783578d0effSAndrew Thompson 	temp.average = xfer->max_hc_frame_size;
178402ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
178502ac6454SAndrew Thompson 	temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
178602ac6454SAndrew Thompson 
178702ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
178802ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
178902ac6454SAndrew Thompson 
179002ac6454SAndrew Thompson 	/* get next DMA set */
179102ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
179202ac6454SAndrew Thompson 
179302ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
179402ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
179502ac6454SAndrew Thompson 
179602ac6454SAndrew Thompson 	temp.td = NULL;
179702ac6454SAndrew Thompson 	temp.td_next = td;
179802ac6454SAndrew Thompson 	temp.qtd_status = 0;
17990f7d4548SAndrew Thompson 	temp.last_frame = 0;
180002ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
180102ac6454SAndrew Thompson 
180202ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
1803ae60fdfbSAndrew Thompson 		if (xfer->endpoint->toggle_next) {
180402ac6454SAndrew Thompson 			/* DATA1 is next */
180502ac6454SAndrew Thompson 			temp.qtd_status |=
1806e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
180702ac6454SAndrew Thompson 		}
180802ac6454SAndrew Thompson 		temp.auto_data_toggle = 0;
180902ac6454SAndrew Thompson 	} else {
181002ac6454SAndrew Thompson 		temp.auto_data_toggle = 1;
181102ac6454SAndrew Thompson 	}
181202ac6454SAndrew Thompson 
181353e0bf6eSHans Petter Selasky 	if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
181453e0bf6eSHans Petter Selasky 	    (xfer->xroot->udev->address != 0)) {
181502ac6454SAndrew Thompson 		/* max 3 retries */
181602ac6454SAndrew Thompson 		temp.qtd_status |=
1817e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
181802ac6454SAndrew Thompson 	}
181902ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
182002ac6454SAndrew Thompson 
182102ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
182202ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
182302ac6454SAndrew Thompson 
1824dbe63d3aSHans Petter Selasky 			xfer->endpoint->toggle_next = 0;
1825dbe63d3aSHans Petter Selasky 
182602ac6454SAndrew Thompson 			temp.qtd_status &=
1827e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1828e55e1ebcSAndrew Thompson 			temp.qtd_status |= htohc32(temp.sc,
18295f128668SAndrew Thompson 			    EHCI_QTD_ACTIVE |
183002ac6454SAndrew Thompson 			    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
183102ac6454SAndrew Thompson 			    EHCI_QTD_SET_TOGGLE(0));
183202ac6454SAndrew Thompson 
183302ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
183402ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + 0;
183502ac6454SAndrew Thompson 			temp.shortpkt = temp.len ? 1 : 0;
18360f7d4548SAndrew Thompson 			/* check for last frame */
18370f7d4548SAndrew Thompson 			if (xfer->nframes == 1) {
18380f7d4548SAndrew Thompson 				/* no STATUS stage yet, SETUP is last */
18390f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
18400f7d4548SAndrew Thompson 					temp.last_frame = 1;
18410f7d4548SAndrew Thompson 					temp.setup_alt_next = 0;
18420f7d4548SAndrew Thompson 				}
18430f7d4548SAndrew Thompson 			}
184402ac6454SAndrew Thompson 			ehci_setup_standard_chain_sub(&temp);
184502ac6454SAndrew Thompson 		}
184602ac6454SAndrew Thompson 		x = 1;
184702ac6454SAndrew Thompson 	} else {
184802ac6454SAndrew Thompson 		x = 0;
184902ac6454SAndrew Thompson 	}
185002ac6454SAndrew Thompson 
185102ac6454SAndrew Thompson 	while (x != xfer->nframes) {
185202ac6454SAndrew Thompson 
185302ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
185402ac6454SAndrew Thompson 
185502ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
185602ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
185702ac6454SAndrew Thompson 
185802ac6454SAndrew Thompson 		x++;
185902ac6454SAndrew Thompson 
186002ac6454SAndrew Thompson 		if (x == xfer->nframes) {
18610f7d4548SAndrew Thompson 			if (xfer->flags_int.control_xfr) {
18620f7d4548SAndrew Thompson 				/* no STATUS stage yet, DATA is last */
18630f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
18640f7d4548SAndrew Thompson 					temp.last_frame = 1;
186502ac6454SAndrew Thompson 					temp.setup_alt_next = 0;
186602ac6454SAndrew Thompson 				}
18670f7d4548SAndrew Thompson 			} else {
18680f7d4548SAndrew Thompson 				temp.last_frame = 1;
18690f7d4548SAndrew Thompson 				temp.setup_alt_next = 0;
18700f7d4548SAndrew Thompson 			}
18710f7d4548SAndrew Thompson 		}
187202ac6454SAndrew Thompson 		/* keep previous data toggle and error count */
187302ac6454SAndrew Thompson 
187402ac6454SAndrew Thompson 		temp.qtd_status &=
1875e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
187602ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
187702ac6454SAndrew Thompson 
187802ac6454SAndrew Thompson 		if (temp.len == 0) {
187902ac6454SAndrew Thompson 
188002ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
188102ac6454SAndrew Thompson 
188202ac6454SAndrew Thompson 			temp.shortpkt = 0;
188302ac6454SAndrew Thompson 
188402ac6454SAndrew Thompson 		} else {
188502ac6454SAndrew Thompson 
188602ac6454SAndrew Thompson 			/* regular data transfer */
188702ac6454SAndrew Thompson 
188802ac6454SAndrew Thompson 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
188902ac6454SAndrew Thompson 		}
189002ac6454SAndrew Thompson 
189102ac6454SAndrew Thompson 		/* set endpoint direction */
189202ac6454SAndrew Thompson 
189302ac6454SAndrew Thompson 		temp.qtd_status |=
1894ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1895e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
189602ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1897e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
189802ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
189902ac6454SAndrew Thompson 
190002ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
190102ac6454SAndrew Thompson 	}
190202ac6454SAndrew Thompson 
190302ac6454SAndrew Thompson 	/* check if we should append a status stage */
190402ac6454SAndrew Thompson 
190502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
190602ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
190702ac6454SAndrew Thompson 
190802ac6454SAndrew Thompson 		/*
190902ac6454SAndrew Thompson 		 * Send a DATA1 message and invert the current endpoint
191002ac6454SAndrew Thompson 		 * direction.
191102ac6454SAndrew Thompson 		 */
191202ac6454SAndrew Thompson 
1913e55e1ebcSAndrew Thompson 		temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
191402ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
191502ac6454SAndrew Thompson 		temp.qtd_status |=
1916ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1917e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
191802ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
191902ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1)) :
1920e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
192102ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
192202ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
192302ac6454SAndrew Thompson 
192402ac6454SAndrew Thompson 		temp.len = 0;
192502ac6454SAndrew Thompson 		temp.pc = NULL;
192602ac6454SAndrew Thompson 		temp.shortpkt = 0;
19270f7d4548SAndrew Thompson 		temp.last_frame = 1;
19280f7d4548SAndrew Thompson 		temp.setup_alt_next = 0;
192902ac6454SAndrew Thompson 
193002ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
193102ac6454SAndrew Thompson 	}
193202ac6454SAndrew Thompson 	td = temp.td;
193302ac6454SAndrew Thompson 
193402ac6454SAndrew Thompson 	/* the last TD terminates the transfer: */
1935e55e1ebcSAndrew Thompson 	td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1936e55e1ebcSAndrew Thompson 	td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
193702ac6454SAndrew Thompson 
1938a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(td->page_cache);
193902ac6454SAndrew Thompson 
194002ac6454SAndrew Thompson 	/* must have at least one frame! */
194102ac6454SAndrew Thompson 
194202ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
194302ac6454SAndrew Thompson 
1944b850ecc1SAndrew Thompson #ifdef USB_DEBUG
194502ac6454SAndrew Thompson 	if (ehcidebug > 8) {
194602ac6454SAndrew Thompson 		DPRINTF("nexttog=%d; data before transfer:\n",
1947ae60fdfbSAndrew Thompson 		    xfer->endpoint->toggle_next);
194802ac6454SAndrew Thompson 		ehci_dump_sqtds(temp.sc,
194902ac6454SAndrew Thompson 		    xfer->td_transfer_first);
195002ac6454SAndrew Thompson 	}
195102ac6454SAndrew Thompson #endif
195202ac6454SAndrew Thompson 
1953ae60fdfbSAndrew Thompson 	methods = xfer->endpoint->methods;
195402ac6454SAndrew Thompson 
195502ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
195602ac6454SAndrew Thompson 
195702ac6454SAndrew Thompson 	/* the "qh_link" field is filled when the QH is added */
195802ac6454SAndrew Thompson 
195902ac6454SAndrew Thompson 	qh_endp =
196002ac6454SAndrew Thompson 	    (EHCI_QH_SET_ADDR(xfer->address) |
1961ae60fdfbSAndrew Thompson 	    EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
196202ac6454SAndrew Thompson 	    EHCI_QH_SET_MPL(xfer->max_packet_size));
196302ac6454SAndrew Thompson 
1964a593f6b8SAndrew Thompson 	if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
19652094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
196602ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods)
196702ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(8);
196802ac6454SAndrew Thompson 	} else {
196902ac6454SAndrew Thompson 
1970a593f6b8SAndrew Thompson 		if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
19712094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
197202ac6454SAndrew Thompson 		} else {
19732094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
197402ac6454SAndrew Thompson 		}
197502ac6454SAndrew Thompson 
197602ac6454SAndrew Thompson 		if (methods == &ehci_device_ctrl_methods) {
197702ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_CTL;
197802ac6454SAndrew Thompson 		}
197902ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods) {
198002ac6454SAndrew Thompson 			/* Only try one time per microframe! */
198102ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(1);
198202ac6454SAndrew Thompson 		}
198302ac6454SAndrew Thompson 	}
198402ac6454SAndrew Thompson 
19852094ecdaSAndrew Thompson 	if (temp.auto_data_toggle == 0) {
19862094ecdaSAndrew Thompson 		/* software computes the data toggle */
19872094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_DTC;
19882094ecdaSAndrew Thompson 	}
19892094ecdaSAndrew Thompson 
1990e55e1ebcSAndrew Thompson 	qh->qh_endp = htohc32(temp.sc, qh_endp);
199102ac6454SAndrew Thompson 
199202ac6454SAndrew Thompson 	qh_endphub =
199302ac6454SAndrew Thompson 	    (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
1994f12c6c29SAndrew Thompson 	    EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) |
1995f12c6c29SAndrew Thompson 	    EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) |
199602ac6454SAndrew Thompson 	    EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
199702ac6454SAndrew Thompson 	    EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
199802ac6454SAndrew Thompson 
1999e55e1ebcSAndrew Thompson 	qh->qh_endphub = htohc32(temp.sc, qh_endphub);
20002094ecdaSAndrew Thompson 	qh->qh_curqtd = 0;
200102ac6454SAndrew Thompson 
200202ac6454SAndrew Thompson 	/* fill the overlay qTD */
200302ac6454SAndrew Thompson 
20042094ecdaSAndrew Thompson 	if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
200502ac6454SAndrew Thompson 		/* DATA1 is next */
20062094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
20072094ecdaSAndrew Thompson 	} else {
20082094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = 0;
200902ac6454SAndrew Thompson 	}
20102094ecdaSAndrew Thompson 
201102ac6454SAndrew Thompson 	td = xfer->td_transfer_first;
201202ac6454SAndrew Thompson 
201302ac6454SAndrew Thompson 	qh->qh_qtd.qtd_next = td->qtd_self;
201402ac6454SAndrew Thompson 	qh->qh_qtd.qtd_altnext =
2015e55e1ebcSAndrew Thompson 	    htohc32(temp.sc, EHCI_LINK_TERMINATE);
201602ac6454SAndrew Thompson 
20179ae7f7c1SHans Petter Selasky 	/* properly reset reserved fields */
20189ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[0] = 0;
20199ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[1] = 0;
20209ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[2] = 0;
20219ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[3] = 0;
20229ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[4] = 0;
20239ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[0] = 0;
20249ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[1] = 0;
20259ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[2] = 0;
20269ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[3] = 0;
20279ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[4] = 0;
20289ae7f7c1SHans Petter Selasky 
2029a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(qh->page_cache);
203002ac6454SAndrew Thompson 
2031ec8f3127SAndrew Thompson 	if (xfer->xroot->udev->flags.self_suspended == 0) {
203202ac6454SAndrew Thompson 		EHCI_APPEND_QH(qh, *qh_last);
203302ac6454SAndrew Thompson 	}
203402ac6454SAndrew Thompson }
203502ac6454SAndrew Thompson 
203602ac6454SAndrew Thompson static void
203739307315SAndrew Thompson ehci_root_intr(ehci_softc_t *sc)
203802ac6454SAndrew Thompson {
203902ac6454SAndrew Thompson 	uint16_t i;
204002ac6454SAndrew Thompson 	uint16_t m;
204102ac6454SAndrew Thompson 
204202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
204302ac6454SAndrew Thompson 
204402ac6454SAndrew Thompson 	/* clear any old interrupt data */
204539307315SAndrew Thompson 	memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
204602ac6454SAndrew Thompson 
204702ac6454SAndrew Thompson 	/* set bits */
204802ac6454SAndrew Thompson 	m = (sc->sc_noport + 1);
204902ac6454SAndrew Thompson 	if (m > (8 * sizeof(sc->sc_hub_idata))) {
205002ac6454SAndrew Thompson 		m = (8 * sizeof(sc->sc_hub_idata));
205102ac6454SAndrew Thompson 	}
205202ac6454SAndrew Thompson 	for (i = 1; i < m; i++) {
205302ac6454SAndrew Thompson 		/* pick out CHANGE bits from the status register */
205402ac6454SAndrew Thompson 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
205502ac6454SAndrew Thompson 			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
205602ac6454SAndrew Thompson 			DPRINTF("port %d changed\n", i);
205702ac6454SAndrew Thompson 		}
205802ac6454SAndrew Thompson 	}
205939307315SAndrew Thompson 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
206039307315SAndrew Thompson 	    sizeof(sc->sc_hub_idata));
206102ac6454SAndrew Thompson }
206202ac6454SAndrew Thompson 
206302ac6454SAndrew Thompson static void
2064760bc48eSAndrew Thompson ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
206502ac6454SAndrew Thompson {
206602ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
206702ac6454SAndrew Thompson 	uint32_t status;
206802ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
206902ac6454SAndrew Thompson 	uint16_t len = 0;
207002ac6454SAndrew Thompson 	ehci_sitd_t *td = xfer->td_transfer_first;
207102ac6454SAndrew Thompson 	ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
207202ac6454SAndrew Thompson 
2073ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2074ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
207502ac6454SAndrew Thompson 
207602ac6454SAndrew Thompson 	while (nframes--) {
207702ac6454SAndrew Thompson 		if (td == NULL) {
207802ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
207902ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
208002ac6454SAndrew Thompson 		}
208102ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
208202ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
208302ac6454SAndrew Thompson 		}
2084b850ecc1SAndrew Thompson #ifdef USB_DEBUG
208502ac6454SAndrew Thompson 		if (ehcidebug > 15) {
208602ac6454SAndrew Thompson 			DPRINTF("isoc FS-TD\n");
208702ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
208802ac6454SAndrew Thompson 		}
208902ac6454SAndrew Thompson #endif
2090a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2091e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
209202ac6454SAndrew Thompson 
209302ac6454SAndrew Thompson 		len = EHCI_SITD_GET_LEN(status);
209402ac6454SAndrew Thompson 
2095c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2096c773c254SAndrew Thompson 
209702ac6454SAndrew Thompson 		if (*plen >= len) {
209802ac6454SAndrew Thompson 			len = *plen - len;
209902ac6454SAndrew Thompson 		} else {
210002ac6454SAndrew Thompson 			len = 0;
210102ac6454SAndrew Thompson 		}
210202ac6454SAndrew Thompson 
210302ac6454SAndrew Thompson 		*plen = len;
210402ac6454SAndrew Thompson 
210502ac6454SAndrew Thompson 		/* remove FS-TD from schedule */
210602ac6454SAndrew Thompson 		EHCI_REMOVE_FS_TD(td, *pp_last);
210702ac6454SAndrew Thompson 
210802ac6454SAndrew Thompson 		pp_last++;
210902ac6454SAndrew Thompson 		plen++;
211002ac6454SAndrew Thompson 		td = td->obj_next;
211102ac6454SAndrew Thompson 	}
211202ac6454SAndrew Thompson 
211302ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
211402ac6454SAndrew Thompson }
211502ac6454SAndrew Thompson 
211602ac6454SAndrew Thompson static void
2117760bc48eSAndrew Thompson ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
211802ac6454SAndrew Thompson {
211902ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
212002ac6454SAndrew Thompson 	uint32_t status;
212102ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
212202ac6454SAndrew Thompson 	uint16_t len = 0;
212302ac6454SAndrew Thompson 	uint8_t td_no = 0;
212402ac6454SAndrew Thompson 	ehci_itd_t *td = xfer->td_transfer_first;
212502ac6454SAndrew Thompson 	ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
212602ac6454SAndrew Thompson 
2127ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2128ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
212902ac6454SAndrew Thompson 
2130883bb300SAndrew Thompson 	while (nframes) {
213102ac6454SAndrew Thompson 		if (td == NULL) {
213202ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
213302ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
213402ac6454SAndrew Thompson 		}
213502ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
213602ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
213702ac6454SAndrew Thompson 		}
2138b850ecc1SAndrew Thompson #ifdef USB_DEBUG
213902ac6454SAndrew Thompson 		if (ehcidebug > 15) {
214002ac6454SAndrew Thompson 			DPRINTF("isoc HS-TD\n");
214102ac6454SAndrew Thompson 			ehci_dump_itd(sc, td);
214202ac6454SAndrew Thompson 		}
214302ac6454SAndrew Thompson #endif
214402ac6454SAndrew Thompson 
2145a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2146e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->itd_status[td_no]);
214702ac6454SAndrew Thompson 
214802ac6454SAndrew Thompson 		len = EHCI_ITD_GET_LEN(status);
214902ac6454SAndrew Thompson 
2150c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2151c773c254SAndrew Thompson 
2152f12c6c29SAndrew Thompson 		if (xfer->endpoint->usb_smask & (1 << td_no)) {
2153883bb300SAndrew Thompson 
215402ac6454SAndrew Thompson 			if (*plen >= len) {
215502ac6454SAndrew Thompson 				/*
2156883bb300SAndrew Thompson 				 * The length is valid. NOTE: The
2157883bb300SAndrew Thompson 				 * complete length is written back
2158883bb300SAndrew Thompson 				 * into the status field, and not the
2159883bb300SAndrew Thompson 				 * remainder like with other transfer
2160883bb300SAndrew Thompson 				 * descriptor types.
216102ac6454SAndrew Thompson 				 */
216202ac6454SAndrew Thompson 			} else {
216302ac6454SAndrew Thompson 				/* Invalid length - truncate */
216402ac6454SAndrew Thompson 				len = 0;
216502ac6454SAndrew Thompson 			}
216602ac6454SAndrew Thompson 
216702ac6454SAndrew Thompson 			*plen = len;
216802ac6454SAndrew Thompson 			plen++;
2169883bb300SAndrew Thompson 			nframes--;
2170883bb300SAndrew Thompson 		}
2171883bb300SAndrew Thompson 
217202ac6454SAndrew Thompson 		td_no++;
217302ac6454SAndrew Thompson 
217402ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
217502ac6454SAndrew Thompson 			/* remove HS-TD from schedule */
217602ac6454SAndrew Thompson 			EHCI_REMOVE_HS_TD(td, *pp_last);
217702ac6454SAndrew Thompson 			pp_last++;
217802ac6454SAndrew Thompson 
217902ac6454SAndrew Thompson 			td_no = 0;
218002ac6454SAndrew Thompson 			td = td->obj_next;
218102ac6454SAndrew Thompson 		}
218202ac6454SAndrew Thompson 	}
218302ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
218402ac6454SAndrew Thompson }
218502ac6454SAndrew Thompson 
218602ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row,
218702ac6454SAndrew Thompson  * from close and from interrupt
218802ac6454SAndrew Thompson  */
218902ac6454SAndrew Thompson static void
2190e0a69b51SAndrew Thompson ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
219102ac6454SAndrew Thompson {
2192e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods = xfer->endpoint->methods;
219302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
219402ac6454SAndrew Thompson 
219502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
219602ac6454SAndrew Thompson 
2197ae60fdfbSAndrew Thompson 	DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2198ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint, error);
219902ac6454SAndrew Thompson 
220002ac6454SAndrew Thompson 	if ((methods == &ehci_device_bulk_methods) ||
220102ac6454SAndrew Thompson 	    (methods == &ehci_device_ctrl_methods)) {
2202b850ecc1SAndrew Thompson #ifdef USB_DEBUG
220302ac6454SAndrew Thompson 		if (ehcidebug > 8) {
220402ac6454SAndrew Thompson 			DPRINTF("nexttog=%d; data after transfer:\n",
2205ae60fdfbSAndrew Thompson 			    xfer->endpoint->toggle_next);
220602ac6454SAndrew Thompson 			ehci_dump_sqtds(sc,
220702ac6454SAndrew Thompson 			    xfer->td_transfer_first);
220802ac6454SAndrew Thompson 		}
220902ac6454SAndrew Thompson #endif
221002ac6454SAndrew Thompson 
221102ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
221202ac6454SAndrew Thompson 		    sc->sc_async_p_last);
221302ac6454SAndrew Thompson 	}
221402ac6454SAndrew Thompson 	if (methods == &ehci_device_intr_methods) {
221502ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
221602ac6454SAndrew Thompson 		    sc->sc_intr_p_last[xfer->qh_pos]);
221702ac6454SAndrew Thompson 	}
221802ac6454SAndrew Thompson 	/*
221902ac6454SAndrew Thompson 	 * Only finish isochronous transfers once which will update
222002ac6454SAndrew Thompson 	 * "xfer->frlengths".
222102ac6454SAndrew Thompson 	 */
222202ac6454SAndrew Thompson 	if (xfer->td_transfer_first &&
222302ac6454SAndrew Thompson 	    xfer->td_transfer_last) {
222402ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_fs_methods) {
222502ac6454SAndrew Thompson 			ehci_isoc_fs_done(sc, xfer);
222602ac6454SAndrew Thompson 		}
222702ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_hs_methods) {
222802ac6454SAndrew Thompson 			ehci_isoc_hs_done(sc, xfer);
222902ac6454SAndrew Thompson 		}
223002ac6454SAndrew Thompson 		xfer->td_transfer_first = NULL;
223102ac6454SAndrew Thompson 		xfer->td_transfer_last = NULL;
223202ac6454SAndrew Thompson 	}
223302ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
2234a593f6b8SAndrew Thompson 	usbd_transfer_done(xfer, error);
223502ac6454SAndrew Thompson }
223602ac6454SAndrew Thompson 
223702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
223802ac6454SAndrew Thompson  * ehci bulk support
223902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
224002ac6454SAndrew Thompson static void
2241760bc48eSAndrew Thompson ehci_device_bulk_open(struct usb_xfer *xfer)
224202ac6454SAndrew Thompson {
224302ac6454SAndrew Thompson 	return;
224402ac6454SAndrew Thompson }
224502ac6454SAndrew Thompson 
224602ac6454SAndrew Thompson static void
2247760bc48eSAndrew Thompson ehci_device_bulk_close(struct usb_xfer *xfer)
224802ac6454SAndrew Thompson {
224902ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
225002ac6454SAndrew Thompson }
225102ac6454SAndrew Thompson 
225202ac6454SAndrew Thompson static void
2253760bc48eSAndrew Thompson ehci_device_bulk_enter(struct usb_xfer *xfer)
225402ac6454SAndrew Thompson {
225502ac6454SAndrew Thompson 	return;
225602ac6454SAndrew Thompson }
225702ac6454SAndrew Thompson 
225802ac6454SAndrew Thompson static void
22594a6af125SHans Petter Selasky ehci_doorbell_async(struct ehci_softc *sc)
22604a6af125SHans Petter Selasky {
22614a6af125SHans Petter Selasky 	uint32_t temp;
22624a6af125SHans Petter Selasky 
22634a6af125SHans Petter Selasky 	/*
22644a6af125SHans Petter Selasky 	 * XXX Performance quirk: Some Host Controllers have a too low
22654a6af125SHans Petter Selasky 	 * interrupt rate. Issue an IAAD to stimulate the Host
22664a6af125SHans Petter Selasky 	 * Controller after queueing the BULK transfer.
22674a6af125SHans Petter Selasky 	 *
22684a6af125SHans Petter Selasky 	 * XXX Force the host controller to refresh any QH caches.
22694a6af125SHans Petter Selasky 	 */
22704a6af125SHans Petter Selasky 	temp = EOREAD4(sc, EHCI_USBCMD);
22714a6af125SHans Petter Selasky 	if (!(temp & EHCI_CMD_IAAD))
22724a6af125SHans Petter Selasky 		EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
22734a6af125SHans Petter Selasky }
22744a6af125SHans Petter Selasky 
22754a6af125SHans Petter Selasky static void
2276760bc48eSAndrew Thompson ehci_device_bulk_start(struct usb_xfer *xfer)
227702ac6454SAndrew Thompson {
227802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
227902ac6454SAndrew Thompson 
228002ac6454SAndrew Thompson 	/* setup TD's and QH */
228102ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
228202ac6454SAndrew Thompson 
228302ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
228402ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
2285d1864afbSAndrew Thompson 
2286cf223a88SAndrew Thompson 	/*
2287cf223a88SAndrew Thompson 	 * XXX Certain nVidia chipsets choke when using the IAAD
2288cf223a88SAndrew Thompson 	 * feature too frequently.
2289cf223a88SAndrew Thompson 	 */
2290cf223a88SAndrew Thompson 	if (sc->sc_flags & EHCI_SCFLG_IAADBUG)
2291cf223a88SAndrew Thompson 		return;
2292cf223a88SAndrew Thompson 
22934a6af125SHans Petter Selasky 	ehci_doorbell_async(sc);
229402ac6454SAndrew Thompson }
229502ac6454SAndrew Thompson 
2296e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_bulk_methods =
229702ac6454SAndrew Thompson {
229802ac6454SAndrew Thompson 	.open = ehci_device_bulk_open,
229902ac6454SAndrew Thompson 	.close = ehci_device_bulk_close,
230002ac6454SAndrew Thompson 	.enter = ehci_device_bulk_enter,
230102ac6454SAndrew Thompson 	.start = ehci_device_bulk_start,
230202ac6454SAndrew Thompson };
230302ac6454SAndrew Thompson 
230402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
230502ac6454SAndrew Thompson  * ehci control support
230602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
230702ac6454SAndrew Thompson static void
2308760bc48eSAndrew Thompson ehci_device_ctrl_open(struct usb_xfer *xfer)
230902ac6454SAndrew Thompson {
231002ac6454SAndrew Thompson 	return;
231102ac6454SAndrew Thompson }
231202ac6454SAndrew Thompson 
231302ac6454SAndrew Thompson static void
2314760bc48eSAndrew Thompson ehci_device_ctrl_close(struct usb_xfer *xfer)
231502ac6454SAndrew Thompson {
231602ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
231702ac6454SAndrew Thompson }
231802ac6454SAndrew Thompson 
231902ac6454SAndrew Thompson static void
2320760bc48eSAndrew Thompson ehci_device_ctrl_enter(struct usb_xfer *xfer)
232102ac6454SAndrew Thompson {
232202ac6454SAndrew Thompson 	return;
232302ac6454SAndrew Thompson }
232402ac6454SAndrew Thompson 
232502ac6454SAndrew Thompson static void
2326760bc48eSAndrew Thompson ehci_device_ctrl_start(struct usb_xfer *xfer)
232702ac6454SAndrew Thompson {
232802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
232902ac6454SAndrew Thompson 
233002ac6454SAndrew Thompson 	/* setup TD's and QH */
233102ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
233202ac6454SAndrew Thompson 
233302ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
233402ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
233502ac6454SAndrew Thompson }
233602ac6454SAndrew Thompson 
2337e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_ctrl_methods =
233802ac6454SAndrew Thompson {
233902ac6454SAndrew Thompson 	.open = ehci_device_ctrl_open,
234002ac6454SAndrew Thompson 	.close = ehci_device_ctrl_close,
234102ac6454SAndrew Thompson 	.enter = ehci_device_ctrl_enter,
234202ac6454SAndrew Thompson 	.start = ehci_device_ctrl_start,
234302ac6454SAndrew Thompson };
234402ac6454SAndrew Thompson 
234502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
234602ac6454SAndrew Thompson  * ehci interrupt support
234702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
234802ac6454SAndrew Thompson static void
2349760bc48eSAndrew Thompson ehci_device_intr_open(struct usb_xfer *xfer)
235002ac6454SAndrew Thompson {
235102ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
235202ac6454SAndrew Thompson 	uint16_t best;
235302ac6454SAndrew Thompson 	uint16_t bit;
235402ac6454SAndrew Thompson 	uint16_t x;
235502ac6454SAndrew Thompson 
2356f12c6c29SAndrew Thompson 	usb_hs_bandwidth_alloc(xfer);
235702ac6454SAndrew Thompson 
235802ac6454SAndrew Thompson 	/*
235902ac6454SAndrew Thompson 	 * Find the best QH position corresponding to the given interval:
236002ac6454SAndrew Thompson 	 */
236102ac6454SAndrew Thompson 
236202ac6454SAndrew Thompson 	best = 0;
236302ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
236402ac6454SAndrew Thompson 	while (bit) {
236502ac6454SAndrew Thompson 		if (xfer->interval >= bit) {
236602ac6454SAndrew Thompson 			x = bit;
236702ac6454SAndrew Thompson 			best = bit;
236802ac6454SAndrew Thompson 			while (x & bit) {
236902ac6454SAndrew Thompson 				if (sc->sc_intr_stat[x] <
237002ac6454SAndrew Thompson 				    sc->sc_intr_stat[best]) {
237102ac6454SAndrew Thompson 					best = x;
237202ac6454SAndrew Thompson 				}
237302ac6454SAndrew Thompson 				x++;
237402ac6454SAndrew Thompson 			}
237502ac6454SAndrew Thompson 			break;
237602ac6454SAndrew Thompson 		}
237702ac6454SAndrew Thompson 		bit >>= 1;
237802ac6454SAndrew Thompson 	}
237902ac6454SAndrew Thompson 
238002ac6454SAndrew Thompson 	sc->sc_intr_stat[best]++;
238102ac6454SAndrew Thompson 	xfer->qh_pos = best;
238202ac6454SAndrew Thompson 
238302ac6454SAndrew Thompson 	DPRINTFN(3, "best=%d interval=%d\n",
238402ac6454SAndrew Thompson 	    best, xfer->interval);
238502ac6454SAndrew Thompson }
238602ac6454SAndrew Thompson 
238702ac6454SAndrew Thompson static void
2388760bc48eSAndrew Thompson ehci_device_intr_close(struct usb_xfer *xfer)
238902ac6454SAndrew Thompson {
239002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
239102ac6454SAndrew Thompson 
239202ac6454SAndrew Thompson 	sc->sc_intr_stat[xfer->qh_pos]--;
239302ac6454SAndrew Thompson 
239402ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
2395f12c6c29SAndrew Thompson 
2396f12c6c29SAndrew Thompson 	/* bandwidth must be freed after device done */
2397f12c6c29SAndrew Thompson 	usb_hs_bandwidth_free(xfer);
239802ac6454SAndrew Thompson }
239902ac6454SAndrew Thompson 
240002ac6454SAndrew Thompson static void
2401760bc48eSAndrew Thompson ehci_device_intr_enter(struct usb_xfer *xfer)
240202ac6454SAndrew Thompson {
240302ac6454SAndrew Thompson 	return;
240402ac6454SAndrew Thompson }
240502ac6454SAndrew Thompson 
240602ac6454SAndrew Thompson static void
2407760bc48eSAndrew Thompson ehci_device_intr_start(struct usb_xfer *xfer)
240802ac6454SAndrew Thompson {
240902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
241002ac6454SAndrew Thompson 
241102ac6454SAndrew Thompson 	/* setup TD's and QH */
241202ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
241302ac6454SAndrew Thompson 
241402ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
241502ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
241602ac6454SAndrew Thompson }
241702ac6454SAndrew Thompson 
2418e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_intr_methods =
241902ac6454SAndrew Thompson {
242002ac6454SAndrew Thompson 	.open = ehci_device_intr_open,
242102ac6454SAndrew Thompson 	.close = ehci_device_intr_close,
242202ac6454SAndrew Thompson 	.enter = ehci_device_intr_enter,
242302ac6454SAndrew Thompson 	.start = ehci_device_intr_start,
242402ac6454SAndrew Thompson };
242502ac6454SAndrew Thompson 
242602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
242702ac6454SAndrew Thompson  * ehci full speed isochronous support
242802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
242902ac6454SAndrew Thompson static void
2430760bc48eSAndrew Thompson ehci_device_isoc_fs_open(struct usb_xfer *xfer)
243102ac6454SAndrew Thompson {
243202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
243302ac6454SAndrew Thompson 	ehci_sitd_t *td;
243402ac6454SAndrew Thompson 	uint32_t sitd_portaddr;
243502ac6454SAndrew Thompson 	uint8_t ds;
243602ac6454SAndrew Thompson 
243702ac6454SAndrew Thompson 	sitd_portaddr =
243802ac6454SAndrew Thompson 	    EHCI_SITD_SET_ADDR(xfer->address) |
2439ae60fdfbSAndrew Thompson 	    EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
244002ac6454SAndrew Thompson 	    EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
244102ac6454SAndrew Thompson 	    EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
244202ac6454SAndrew Thompson 
24430a4cc48fSHans Petter Selasky 	if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
244402ac6454SAndrew Thompson 		sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
24450a4cc48fSHans Petter Selasky 
2446e55e1ebcSAndrew Thompson 	sitd_portaddr = htohc32(sc, sitd_portaddr);
244702ac6454SAndrew Thompson 
244802ac6454SAndrew Thompson 	/* initialize all TD's */
244902ac6454SAndrew Thompson 
245002ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
245102ac6454SAndrew Thompson 
245202ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
245302ac6454SAndrew Thompson 
245402ac6454SAndrew Thompson 			td->sitd_portaddr = sitd_portaddr;
245502ac6454SAndrew Thompson 
245602ac6454SAndrew Thompson 			/*
245702ac6454SAndrew Thompson 			 * TODO: make some kind of automatic
245802ac6454SAndrew Thompson 			 * SMASK/CMASK selection based on micro-frame
245902ac6454SAndrew Thompson 			 * usage
246002ac6454SAndrew Thompson 			 *
246102ac6454SAndrew Thompson 			 * micro-frame usage (8 microframes per 1ms)
246202ac6454SAndrew Thompson 			 */
2463e55e1ebcSAndrew Thompson 			td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
246402ac6454SAndrew Thompson 
2465a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
246602ac6454SAndrew Thompson 		}
246702ac6454SAndrew Thompson 	}
246802ac6454SAndrew Thompson }
246902ac6454SAndrew Thompson 
247002ac6454SAndrew Thompson static void
2471760bc48eSAndrew Thompson ehci_device_isoc_fs_close(struct usb_xfer *xfer)
247202ac6454SAndrew Thompson {
247302ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
247402ac6454SAndrew Thompson }
247502ac6454SAndrew Thompson 
247602ac6454SAndrew Thompson static void
2477760bc48eSAndrew Thompson ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
247802ac6454SAndrew Thompson {
2479760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
248002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
248102ac6454SAndrew Thompson 	ehci_sitd_t *td;
248202ac6454SAndrew Thompson 	ehci_sitd_t *td_last = NULL;
248302ac6454SAndrew Thompson 	ehci_sitd_t **pp_last;
248402ac6454SAndrew Thompson 	uint32_t *plen;
248502ac6454SAndrew Thompson 	uint32_t buf_offset;
248602ac6454SAndrew Thompson 	uint32_t nframes;
248702ac6454SAndrew Thompson 	uint32_t temp;
248802ac6454SAndrew Thompson 	uint32_t sitd_mask;
248902ac6454SAndrew Thompson 	uint16_t tlen;
249002ac6454SAndrew Thompson 	uint8_t sa;
249102ac6454SAndrew Thompson 	uint8_t sb;
249202ac6454SAndrew Thompson 
2493b850ecc1SAndrew Thompson #ifdef USB_DEBUG
249402ac6454SAndrew Thompson 	uint8_t once = 1;
249502ac6454SAndrew Thompson 
249602ac6454SAndrew Thompson #endif
249702ac6454SAndrew Thompson 
249802ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2499ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
250002ac6454SAndrew Thompson 
250102ac6454SAndrew Thompson 	/* get the current frame index */
250202ac6454SAndrew Thompson 
250302ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
250402ac6454SAndrew Thompson 
250502ac6454SAndrew Thompson 	/*
250602ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
250702ac6454SAndrew Thompson 	 * will be inserted
250802ac6454SAndrew Thompson 	 */
2509ae60fdfbSAndrew Thompson 	buf_offset = (nframes - xfer->endpoint->isoc_next) &
251002ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
251102ac6454SAndrew Thompson 
2512ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
251302ac6454SAndrew Thompson 	    (buf_offset < xfer->nframes)) {
251402ac6454SAndrew Thompson 		/*
251502ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
251602ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
251702ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
251802ac6454SAndrew Thompson 		 * overlap.
251902ac6454SAndrew Thompson 		 */
2520ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) &
252102ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2522ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
2523ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
252402ac6454SAndrew Thompson 	}
252502ac6454SAndrew Thompson 	/*
252602ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
252702ac6454SAndrew Thompson 	 * current frame position:
252802ac6454SAndrew Thompson 	 */
2529ae60fdfbSAndrew Thompson 	buf_offset = (xfer->endpoint->isoc_next - nframes) &
253002ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
253102ac6454SAndrew Thompson 
253202ac6454SAndrew Thompson 	/*
253302ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
253402ac6454SAndrew Thompson 	 */
253502ac6454SAndrew Thompson 	xfer->isoc_time_complete =
25360a4cc48fSHans Petter Selasky 	    usb_isoc_time_expand(&sc->sc_bus, nframes) +
25370a4cc48fSHans Petter Selasky 	    buf_offset + xfer->nframes;
253802ac6454SAndrew Thompson 
253902ac6454SAndrew Thompson 	/* get the real number of frames */
254002ac6454SAndrew Thompson 
254102ac6454SAndrew Thompson 	nframes = xfer->nframes;
254202ac6454SAndrew Thompson 
254302ac6454SAndrew Thompson 	buf_offset = 0;
254402ac6454SAndrew Thompson 
254502ac6454SAndrew Thompson 	plen = xfer->frlengths;
254602ac6454SAndrew Thompson 
254702ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
254802ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
254902ac6454SAndrew Thompson 
255002ac6454SAndrew Thompson 	/* get next DMA set */
255102ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
255202ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
255302ac6454SAndrew Thompson 
2554ae60fdfbSAndrew Thompson 	pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];
255502ac6454SAndrew Thompson 
255602ac6454SAndrew Thompson 	/* store starting position */
255702ac6454SAndrew Thompson 
2558ae60fdfbSAndrew Thompson 	xfer->qh_pos = xfer->endpoint->isoc_next;
255902ac6454SAndrew Thompson 
256002ac6454SAndrew Thompson 	while (nframes--) {
256102ac6454SAndrew Thompson 		if (td == NULL) {
256202ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
256302ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
256402ac6454SAndrew Thompson 		}
25650a4cc48fSHans Petter Selasky 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT])
256602ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
25670a4cc48fSHans Petter Selasky 
256802ac6454SAndrew Thompson 		/* reuse sitd_portaddr and sitd_back from last transfer */
256902ac6454SAndrew Thompson 
257002ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
2571b850ecc1SAndrew Thompson #ifdef USB_DEBUG
257202ac6454SAndrew Thompson 			if (once) {
257302ac6454SAndrew Thompson 				once = 0;
257402ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d "
257502ac6454SAndrew Thompson 				    "bytes (frame truncated)\n",
257602ac6454SAndrew Thompson 				    __FUNCTION__, *plen,
257702ac6454SAndrew Thompson 				    xfer->max_frame_size);
257802ac6454SAndrew Thompson 			}
257902ac6454SAndrew Thompson #endif
258002ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
258102ac6454SAndrew Thompson 		}
25820a4cc48fSHans Petter Selasky 
25830a4cc48fSHans Petter Selasky 		/* allocate a slot */
25840a4cc48fSHans Petter Selasky 
25850a4cc48fSHans Petter Selasky 		sa = usbd_fs_isoc_schedule_alloc_slot(xfer,
25860a4cc48fSHans Petter Selasky 		    xfer->isoc_time_complete - nframes - 1);
25870a4cc48fSHans Petter Selasky 
25880a4cc48fSHans Petter Selasky 		if (sa == 255) {
258902ac6454SAndrew Thompson 			/*
25900a4cc48fSHans Petter Selasky 			 * Schedule is FULL, set length to zero:
259102ac6454SAndrew Thompson 			 */
25920a4cc48fSHans Petter Selasky 
259302ac6454SAndrew Thompson 			*plen = 0;
25940a4cc48fSHans Petter Selasky 			sa = USB_FS_ISOC_UFRAME_MAX - 1;
259502ac6454SAndrew Thompson 		}
259602ac6454SAndrew Thompson 		if (*plen) {
259702ac6454SAndrew Thompson 			/*
2598a593f6b8SAndrew Thompson 			 * only call "usbd_get_page()" when we have a
259902ac6454SAndrew Thompson 			 * non-zero length
260002ac6454SAndrew Thompson 			 */
2601a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2602e55e1ebcSAndrew Thompson 			td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
260302ac6454SAndrew Thompson 			buf_offset += *plen;
260402ac6454SAndrew Thompson 			/*
260502ac6454SAndrew Thompson 			 * NOTE: We need to subtract one from the offset so
260602ac6454SAndrew Thompson 			 * that we are on a valid page!
260702ac6454SAndrew Thompson 			 */
2608a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset - 1,
260902ac6454SAndrew Thompson 			    &buf_res);
261002ac6454SAndrew Thompson 			temp = buf_res.physaddr & ~0xFFF;
261102ac6454SAndrew Thompson 		} else {
261202ac6454SAndrew Thompson 			td->sitd_bp[0] = 0;
261302ac6454SAndrew Thompson 			temp = 0;
261402ac6454SAndrew Thompson 		}
261502ac6454SAndrew Thompson 
2616ae60fdfbSAndrew Thompson 		if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
261702ac6454SAndrew Thompson 			tlen = *plen;
261802ac6454SAndrew Thompson 			if (tlen <= 188) {
261902ac6454SAndrew Thompson 				temp |= 1;	/* T-count = 1, TP = ALL */
262002ac6454SAndrew Thompson 				tlen = 1;
262102ac6454SAndrew Thompson 			} else {
262202ac6454SAndrew Thompson 				tlen += 187;
262302ac6454SAndrew Thompson 				tlen /= 188;
262402ac6454SAndrew Thompson 				temp |= tlen;	/* T-count = [1..6] */
262502ac6454SAndrew Thompson 				temp |= 8;	/* TP = Begin */
262602ac6454SAndrew Thompson 			}
262702ac6454SAndrew Thompson 
262802ac6454SAndrew Thompson 			tlen += sa;
262902ac6454SAndrew Thompson 
263002ac6454SAndrew Thompson 			if (tlen >= 8) {
263102ac6454SAndrew Thompson 				sb = 0;
263202ac6454SAndrew Thompson 			} else {
263302ac6454SAndrew Thompson 				sb = (1 << tlen);
263402ac6454SAndrew Thompson 			}
263502ac6454SAndrew Thompson 
263602ac6454SAndrew Thompson 			sa = (1 << sa);
263702ac6454SAndrew Thompson 			sa = (sb - sa) & 0x3F;
263802ac6454SAndrew Thompson 			sb = 0;
263902ac6454SAndrew Thompson 		} else {
264002ac6454SAndrew Thompson 			sb = (-(4 << sa)) & 0xFE;
264102ac6454SAndrew Thompson 			sa = (1 << sa) & 0x3F;
264202ac6454SAndrew Thompson 		}
264302ac6454SAndrew Thompson 
264402ac6454SAndrew Thompson 		sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
264502ac6454SAndrew Thompson 		    EHCI_SITD_SET_CMASK(sb));
264602ac6454SAndrew Thompson 
2647e55e1ebcSAndrew Thompson 		td->sitd_bp[1] = htohc32(sc, temp);
264802ac6454SAndrew Thompson 
2649e55e1ebcSAndrew Thompson 		td->sitd_mask = htohc32(sc, sitd_mask);
265002ac6454SAndrew Thompson 
265102ac6454SAndrew Thompson 		if (nframes == 0) {
2652e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
26535f128668SAndrew Thompson 			    EHCI_SITD_IOC |
265402ac6454SAndrew Thompson 			    EHCI_SITD_ACTIVE |
265502ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
265602ac6454SAndrew Thompson 		} else {
2657e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
26585f128668SAndrew Thompson 			    EHCI_SITD_ACTIVE |
265902ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
266002ac6454SAndrew Thompson 		}
2661a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
266202ac6454SAndrew Thompson 
2663b850ecc1SAndrew Thompson #ifdef USB_DEBUG
266402ac6454SAndrew Thompson 		if (ehcidebug > 15) {
266502ac6454SAndrew Thompson 			DPRINTF("FS-TD %d\n", nframes);
266602ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
266702ac6454SAndrew Thompson 		}
266802ac6454SAndrew Thompson #endif
266902ac6454SAndrew Thompson 		/* insert TD into schedule */
267002ac6454SAndrew Thompson 		EHCI_APPEND_FS_TD(td, *pp_last);
267102ac6454SAndrew Thompson 		pp_last++;
267202ac6454SAndrew Thompson 
267302ac6454SAndrew Thompson 		plen++;
267402ac6454SAndrew Thompson 		td_last = td;
267502ac6454SAndrew Thompson 		td = td->obj_next;
267602ac6454SAndrew Thompson 	}
267702ac6454SAndrew Thompson 
267802ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
267902ac6454SAndrew Thompson 
268002ac6454SAndrew Thompson 	/* update isoc_next */
2681ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
268202ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
26837e66ab7cSHans Petter Selasky 
26847e66ab7cSHans Petter Selasky 	/*
26857e66ab7cSHans Petter Selasky 	 * We don't allow cancelling of the SPLIT transaction USB FULL
26867e66ab7cSHans Petter Selasky 	 * speed transfer, because it disturbs the bandwidth
26877e66ab7cSHans Petter Selasky 	 * computation algorithm.
26887e66ab7cSHans Petter Selasky 	 */
26897e66ab7cSHans Petter Selasky 	xfer->flags_int.can_cancel_immed = 0;
269002ac6454SAndrew Thompson }
269102ac6454SAndrew Thompson 
269202ac6454SAndrew Thompson static void
2693760bc48eSAndrew Thompson ehci_device_isoc_fs_start(struct usb_xfer *xfer)
269402ac6454SAndrew Thompson {
26957e66ab7cSHans Petter Selasky 	/*
26967e66ab7cSHans Petter Selasky 	 * We don't allow cancelling of the SPLIT transaction USB FULL
26977e66ab7cSHans Petter Selasky 	 * speed transfer, because it disturbs the bandwidth
26987e66ab7cSHans Petter Selasky 	 * computation algorithm.
26997e66ab7cSHans Petter Selasky 	 */
27007e66ab7cSHans Petter Selasky 	xfer->flags_int.can_cancel_immed = 0;
27017e66ab7cSHans Petter Selasky 
27027e66ab7cSHans Petter Selasky 	/* set a default timeout */
27037e66ab7cSHans Petter Selasky 	if (xfer->timeout == 0)
27047e66ab7cSHans Petter Selasky 		xfer->timeout = 500; /* ms */
27057e66ab7cSHans Petter Selasky 
270602ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
270702ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
270802ac6454SAndrew Thompson }
270902ac6454SAndrew Thompson 
2710e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_fs_methods =
271102ac6454SAndrew Thompson {
271202ac6454SAndrew Thompson 	.open = ehci_device_isoc_fs_open,
271302ac6454SAndrew Thompson 	.close = ehci_device_isoc_fs_close,
271402ac6454SAndrew Thompson 	.enter = ehci_device_isoc_fs_enter,
271502ac6454SAndrew Thompson 	.start = ehci_device_isoc_fs_start,
271602ac6454SAndrew Thompson };
271702ac6454SAndrew Thompson 
271802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
271902ac6454SAndrew Thompson  * ehci high speed isochronous support
272002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
272102ac6454SAndrew Thompson static void
2722760bc48eSAndrew Thompson ehci_device_isoc_hs_open(struct usb_xfer *xfer)
272302ac6454SAndrew Thompson {
272402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
272502ac6454SAndrew Thompson 	ehci_itd_t *td;
272602ac6454SAndrew Thompson 	uint32_t temp;
272702ac6454SAndrew Thompson 	uint8_t ds;
2728883bb300SAndrew Thompson 
2729f12c6c29SAndrew Thompson 	usb_hs_bandwidth_alloc(xfer);
273002ac6454SAndrew Thompson 
273102ac6454SAndrew Thompson 	/* initialize all TD's */
273202ac6454SAndrew Thompson 
273302ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
273402ac6454SAndrew Thompson 
273502ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
273602ac6454SAndrew Thompson 
273702ac6454SAndrew Thompson 			/* set TD inactive */
273802ac6454SAndrew Thompson 			td->itd_status[0] = 0;
273902ac6454SAndrew Thompson 			td->itd_status[1] = 0;
274002ac6454SAndrew Thompson 			td->itd_status[2] = 0;
274102ac6454SAndrew Thompson 			td->itd_status[3] = 0;
274202ac6454SAndrew Thompson 			td->itd_status[4] = 0;
274302ac6454SAndrew Thompson 			td->itd_status[5] = 0;
274402ac6454SAndrew Thompson 			td->itd_status[6] = 0;
274502ac6454SAndrew Thompson 			td->itd_status[7] = 0;
274602ac6454SAndrew Thompson 
274702ac6454SAndrew Thompson 			/* set endpoint and address */
2748e55e1ebcSAndrew Thompson 			td->itd_bp[0] = htohc32(sc,
27495f128668SAndrew Thompson 			    EHCI_ITD_SET_ADDR(xfer->address) |
2750ae60fdfbSAndrew Thompson 			    EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
275102ac6454SAndrew Thompson 
275202ac6454SAndrew Thompson 			temp =
275302ac6454SAndrew Thompson 			    EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
275402ac6454SAndrew Thompson 
275502ac6454SAndrew Thompson 			/* set direction */
2756ae60fdfbSAndrew Thompson 			if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
275702ac6454SAndrew Thompson 				temp |= EHCI_ITD_SET_DIR_IN;
275802ac6454SAndrew Thompson 			}
275902ac6454SAndrew Thompson 			/* set maximum packet size */
2760e55e1ebcSAndrew Thompson 			td->itd_bp[1] = htohc32(sc, temp);
276102ac6454SAndrew Thompson 
276202ac6454SAndrew Thompson 			/* set transfer multiplier */
2763e55e1ebcSAndrew Thompson 			td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
276402ac6454SAndrew Thompson 
2765a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
276602ac6454SAndrew Thompson 		}
276702ac6454SAndrew Thompson 	}
276802ac6454SAndrew Thompson }
276902ac6454SAndrew Thompson 
277002ac6454SAndrew Thompson static void
2771760bc48eSAndrew Thompson ehci_device_isoc_hs_close(struct usb_xfer *xfer)
277202ac6454SAndrew Thompson {
277302ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
2774f12c6c29SAndrew Thompson 
2775f12c6c29SAndrew Thompson 	/* bandwidth must be freed after device done */
2776f12c6c29SAndrew Thompson 	usb_hs_bandwidth_free(xfer);
277702ac6454SAndrew Thompson }
277802ac6454SAndrew Thompson 
277902ac6454SAndrew Thompson static void
2780760bc48eSAndrew Thompson ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
278102ac6454SAndrew Thompson {
2782760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
278302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
278402ac6454SAndrew Thompson 	ehci_itd_t *td;
278502ac6454SAndrew Thompson 	ehci_itd_t *td_last = NULL;
278602ac6454SAndrew Thompson 	ehci_itd_t **pp_last;
278702ac6454SAndrew Thompson 	bus_size_t page_addr;
278802ac6454SAndrew Thompson 	uint32_t *plen;
278902ac6454SAndrew Thompson 	uint32_t status;
279002ac6454SAndrew Thompson 	uint32_t buf_offset;
279102ac6454SAndrew Thompson 	uint32_t nframes;
279202ac6454SAndrew Thompson 	uint32_t itd_offset[8 + 1];
279302ac6454SAndrew Thompson 	uint8_t x;
279402ac6454SAndrew Thompson 	uint8_t td_no;
279502ac6454SAndrew Thompson 	uint8_t page_no;
2796d1f7c4baSAndrew Thompson 	uint8_t shift = usbd_xfer_get_fps_shift(xfer);
279702ac6454SAndrew Thompson 
2798b850ecc1SAndrew Thompson #ifdef USB_DEBUG
279902ac6454SAndrew Thompson 	uint8_t once = 1;
280002ac6454SAndrew Thompson 
280102ac6454SAndrew Thompson #endif
280202ac6454SAndrew Thompson 
2803d1f7c4baSAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d shift=%d\n",
2804d1f7c4baSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes, (int)shift);
280502ac6454SAndrew Thompson 
280602ac6454SAndrew Thompson 	/* get the current frame index */
280702ac6454SAndrew Thompson 
280802ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
280902ac6454SAndrew Thompson 
281002ac6454SAndrew Thompson 	/*
281102ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
281202ac6454SAndrew Thompson 	 * will be inserted
281302ac6454SAndrew Thompson 	 */
2814ae60fdfbSAndrew Thompson 	buf_offset = (nframes - xfer->endpoint->isoc_next) &
281502ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
281602ac6454SAndrew Thompson 
2817ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
2818d1f7c4baSAndrew Thompson 	    (buf_offset < (((xfer->nframes << shift) + 7) / 8))) {
281902ac6454SAndrew Thompson 		/*
282002ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
282102ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
282202ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
282302ac6454SAndrew Thompson 		 * overlap.
282402ac6454SAndrew Thompson 		 */
2825ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) &
282602ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2827ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
2828ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
282902ac6454SAndrew Thompson 	}
283002ac6454SAndrew Thompson 	/*
283102ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
283202ac6454SAndrew Thompson 	 * current frame position:
283302ac6454SAndrew Thompson 	 */
2834ae60fdfbSAndrew Thompson 	buf_offset = (xfer->endpoint->isoc_next - nframes) &
283502ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
283602ac6454SAndrew Thompson 
283702ac6454SAndrew Thompson 	/*
283802ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
283902ac6454SAndrew Thompson 	 */
284002ac6454SAndrew Thompson 	xfer->isoc_time_complete =
2841a593f6b8SAndrew Thompson 	    usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
2842d1f7c4baSAndrew Thompson 	    (((xfer->nframes << shift) + 7) / 8);
284302ac6454SAndrew Thompson 
284402ac6454SAndrew Thompson 	/* get the real number of frames */
284502ac6454SAndrew Thompson 
284602ac6454SAndrew Thompson 	nframes = xfer->nframes;
284702ac6454SAndrew Thompson 
284802ac6454SAndrew Thompson 	buf_offset = 0;
284902ac6454SAndrew Thompson 	td_no = 0;
285002ac6454SAndrew Thompson 
285102ac6454SAndrew Thompson 	plen = xfer->frlengths;
285202ac6454SAndrew Thompson 
285302ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
285402ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
285502ac6454SAndrew Thompson 
285602ac6454SAndrew Thompson 	/* get next DMA set */
285702ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
285802ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
285902ac6454SAndrew Thompson 
2860ae60fdfbSAndrew Thompson 	pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];
286102ac6454SAndrew Thompson 
286202ac6454SAndrew Thompson 	/* store starting position */
286302ac6454SAndrew Thompson 
2864ae60fdfbSAndrew Thompson 	xfer->qh_pos = xfer->endpoint->isoc_next;
286502ac6454SAndrew Thompson 
2866883bb300SAndrew Thompson 	while (nframes) {
286702ac6454SAndrew Thompson 		if (td == NULL) {
286802ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
286902ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
287002ac6454SAndrew Thompson 		}
287102ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
287202ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
287302ac6454SAndrew Thompson 		}
287402ac6454SAndrew Thompson 		/* range check */
287502ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
2876b850ecc1SAndrew Thompson #ifdef USB_DEBUG
287702ac6454SAndrew Thompson 			if (once) {
287802ac6454SAndrew Thompson 				once = 0;
287902ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d bytes "
288002ac6454SAndrew Thompson 				    "(frame truncated)\n",
288102ac6454SAndrew Thompson 				    __FUNCTION__, *plen, xfer->max_frame_size);
288202ac6454SAndrew Thompson 			}
288302ac6454SAndrew Thompson #endif
288402ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
288502ac6454SAndrew Thompson 		}
2886883bb300SAndrew Thompson 
2887f12c6c29SAndrew Thompson 		if (xfer->endpoint->usb_smask & (1 << td_no)) {
288802ac6454SAndrew Thompson 			status = (EHCI_ITD_SET_LEN(*plen) |
288902ac6454SAndrew Thompson 			    EHCI_ITD_ACTIVE |
289002ac6454SAndrew Thompson 			    EHCI_ITD_SET_PG(0));
2891e55e1ebcSAndrew Thompson 			td->itd_status[td_no] = htohc32(sc, status);
289202ac6454SAndrew Thompson 			itd_offset[td_no] = buf_offset;
289302ac6454SAndrew Thompson 			buf_offset += *plen;
289402ac6454SAndrew Thompson 			plen++;
2895883bb300SAndrew Thompson 			nframes --;
2896883bb300SAndrew Thompson 		} else {
2897883bb300SAndrew Thompson 			td->itd_status[td_no] = 0;	/* not active */
2898883bb300SAndrew Thompson 			itd_offset[td_no] = buf_offset;
2899883bb300SAndrew Thompson 		}
2900883bb300SAndrew Thompson 
290102ac6454SAndrew Thompson 		td_no++;
290202ac6454SAndrew Thompson 
290302ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
290402ac6454SAndrew Thompson 
290502ac6454SAndrew Thompson 			/* the rest of the transfers are not active, if any */
290602ac6454SAndrew Thompson 			for (x = td_no; x != 8; x++) {
290702ac6454SAndrew Thompson 				td->itd_status[x] = 0;	/* not active */
290802ac6454SAndrew Thompson 			}
290902ac6454SAndrew Thompson 
291002ac6454SAndrew Thompson 			/* check if there is any data to be transferred */
291102ac6454SAndrew Thompson 			if (itd_offset[0] != buf_offset) {
291202ac6454SAndrew Thompson 				page_no = 0;
291302ac6454SAndrew Thompson 				itd_offset[td_no] = buf_offset;
291402ac6454SAndrew Thompson 
291502ac6454SAndrew Thompson 				/* get first page offset */
2916a593f6b8SAndrew Thompson 				usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
291702ac6454SAndrew Thompson 				/* get page address */
291802ac6454SAndrew Thompson 				page_addr = buf_res.physaddr & ~0xFFF;
291902ac6454SAndrew Thompson 				/* update page address */
2920e55e1ebcSAndrew Thompson 				td->itd_bp[0] &= htohc32(sc, 0xFFF);
2921e55e1ebcSAndrew Thompson 				td->itd_bp[0] |= htohc32(sc, page_addr);
292202ac6454SAndrew Thompson 
292302ac6454SAndrew Thompson 				for (x = 0; x != td_no; x++) {
292402ac6454SAndrew Thompson 					/* set page number and page offset */
292502ac6454SAndrew Thompson 					status = (EHCI_ITD_SET_PG(page_no) |
292602ac6454SAndrew Thompson 					    (buf_res.physaddr & 0xFFF));
2927e55e1ebcSAndrew Thompson 					td->itd_status[x] |= htohc32(sc, status);
292802ac6454SAndrew Thompson 
292902ac6454SAndrew Thompson 					/* get next page offset */
293002ac6454SAndrew Thompson 					if (itd_offset[x + 1] == buf_offset) {
293102ac6454SAndrew Thompson 						/*
293202ac6454SAndrew Thompson 						 * We subtract one so that
293302ac6454SAndrew Thompson 						 * we don't go off the last
293402ac6454SAndrew Thompson 						 * page!
293502ac6454SAndrew Thompson 						 */
2936a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
293702ac6454SAndrew Thompson 					} else {
2938a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
293902ac6454SAndrew Thompson 					}
294002ac6454SAndrew Thompson 
294102ac6454SAndrew Thompson 					/* check if we need a new page */
294202ac6454SAndrew Thompson 					if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
294302ac6454SAndrew Thompson 						/* new page needed */
294402ac6454SAndrew Thompson 						page_addr = buf_res.physaddr & ~0xFFF;
294502ac6454SAndrew Thompson 						if (page_no == 6) {
294602ac6454SAndrew Thompson 							panic("%s: too many pages\n", __FUNCTION__);
294702ac6454SAndrew Thompson 						}
294802ac6454SAndrew Thompson 						page_no++;
294902ac6454SAndrew Thompson 						/* update page address */
2950e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2951e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] |= htohc32(sc, page_addr);
295202ac6454SAndrew Thompson 					}
295302ac6454SAndrew Thompson 				}
295402ac6454SAndrew Thompson 			}
295502ac6454SAndrew Thompson 			/* set IOC bit if we are complete */
295602ac6454SAndrew Thompson 			if (nframes == 0) {
2957883bb300SAndrew Thompson 				td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC);
295802ac6454SAndrew Thompson 			}
2959a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
2960b850ecc1SAndrew Thompson #ifdef USB_DEBUG
296102ac6454SAndrew Thompson 			if (ehcidebug > 15) {
296202ac6454SAndrew Thompson 				DPRINTF("HS-TD %d\n", nframes);
296302ac6454SAndrew Thompson 				ehci_dump_itd(sc, td);
296402ac6454SAndrew Thompson 			}
296502ac6454SAndrew Thompson #endif
296602ac6454SAndrew Thompson 			/* insert TD into schedule */
296702ac6454SAndrew Thompson 			EHCI_APPEND_HS_TD(td, *pp_last);
296802ac6454SAndrew Thompson 			pp_last++;
296902ac6454SAndrew Thompson 
297002ac6454SAndrew Thompson 			td_no = 0;
297102ac6454SAndrew Thompson 			td_last = td;
297202ac6454SAndrew Thompson 			td = td->obj_next;
297302ac6454SAndrew Thompson 		}
297402ac6454SAndrew Thompson 	}
297502ac6454SAndrew Thompson 
297602ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
297702ac6454SAndrew Thompson 
297802ac6454SAndrew Thompson 	/* update isoc_next */
2979ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
298002ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
298102ac6454SAndrew Thompson }
298202ac6454SAndrew Thompson 
298302ac6454SAndrew Thompson static void
2984760bc48eSAndrew Thompson ehci_device_isoc_hs_start(struct usb_xfer *xfer)
298502ac6454SAndrew Thompson {
298602ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
298702ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
298802ac6454SAndrew Thompson }
298902ac6454SAndrew Thompson 
2990e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_hs_methods =
299102ac6454SAndrew Thompson {
299202ac6454SAndrew Thompson 	.open = ehci_device_isoc_hs_open,
299302ac6454SAndrew Thompson 	.close = ehci_device_isoc_hs_close,
299402ac6454SAndrew Thompson 	.enter = ehci_device_isoc_hs_enter,
299502ac6454SAndrew Thompson 	.start = ehci_device_isoc_hs_start,
299602ac6454SAndrew Thompson };
299702ac6454SAndrew Thompson 
299802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
299902ac6454SAndrew Thompson  * ehci root control support
300002ac6454SAndrew Thompson  *------------------------------------------------------------------------*
300139307315SAndrew Thompson  * Simulate a hardware hub by handling all the necessary requests.
300202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
300302ac6454SAndrew Thompson 
300402ac6454SAndrew Thompson static const
3005760bc48eSAndrew Thompson struct usb_device_descriptor ehci_devd =
300602ac6454SAndrew Thompson {
3007760bc48eSAndrew Thompson 	sizeof(struct usb_device_descriptor),
300802ac6454SAndrew Thompson 	UDESC_DEVICE,			/* type */
300902ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
301002ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
301102ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
301202ac6454SAndrew Thompson 	UDPROTO_HSHUBSTT,		/* protocol */
301302ac6454SAndrew Thompson 	64,				/* max packet */
301402ac6454SAndrew Thompson 	{0}, {0}, {0x00, 0x01},		/* device id */
301502ac6454SAndrew Thompson 	1, 2, 0,			/* string indicies */
301602ac6454SAndrew Thompson 	1				/* # of configurations */
301702ac6454SAndrew Thompson };
301802ac6454SAndrew Thompson 
301902ac6454SAndrew Thompson static const
3020760bc48eSAndrew Thompson struct usb_device_qualifier ehci_odevd =
302102ac6454SAndrew Thompson {
3022760bc48eSAndrew Thompson 	sizeof(struct usb_device_qualifier),
302302ac6454SAndrew Thompson 	UDESC_DEVICE_QUALIFIER,		/* type */
302402ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
302502ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
302602ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
302702ac6454SAndrew Thompson 	UDPROTO_FSHUB,			/* protocol */
302802ac6454SAndrew Thompson 	0,				/* max packet */
302902ac6454SAndrew Thompson 	0,				/* # of configurations */
303002ac6454SAndrew Thompson 	0
303102ac6454SAndrew Thompson };
303202ac6454SAndrew Thompson 
303302ac6454SAndrew Thompson static const struct ehci_config_desc ehci_confd = {
303402ac6454SAndrew Thompson 	.confd = {
3035760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_config_descriptor),
303602ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
303702ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(ehci_confd),
303802ac6454SAndrew Thompson 		.bNumInterface = 1,
303902ac6454SAndrew Thompson 		.bConfigurationValue = 1,
304002ac6454SAndrew Thompson 		.iConfiguration = 0,
304102ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
304202ac6454SAndrew Thompson 		.bMaxPower = 0		/* max power */
304302ac6454SAndrew Thompson 	},
304402ac6454SAndrew Thompson 	.ifcd = {
3045760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_interface_descriptor),
304602ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
304702ac6454SAndrew Thompson 		.bNumEndpoints = 1,
304802ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
304902ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
30501678e135SHans Petter Selasky 		.bInterfaceProtocol = 0,
305102ac6454SAndrew Thompson 	},
305202ac6454SAndrew Thompson 	.endpd = {
3053760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_endpoint_descriptor),
305402ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
305502ac6454SAndrew Thompson 		.bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
305602ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
305702ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
305802ac6454SAndrew Thompson 		.bInterval = 255,
305902ac6454SAndrew Thompson 	},
306002ac6454SAndrew Thompson };
306102ac6454SAndrew Thompson 
306202ac6454SAndrew Thompson static const
3063760bc48eSAndrew Thompson struct usb_hub_descriptor ehci_hubd =
306402ac6454SAndrew Thompson {
30656d917491SHans Petter Selasky 	.bDescLength = 0,		/* dynamic length */
30666d917491SHans Petter Selasky 	.bDescriptorType = UDESC_HUB,
306702ac6454SAndrew Thompson };
306802ac6454SAndrew Thompson 
306902ac6454SAndrew Thompson static void
307002ac6454SAndrew Thompson ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
307102ac6454SAndrew Thompson {
307202ac6454SAndrew Thompson 	uint32_t port;
307302ac6454SAndrew Thompson 	uint32_t v;
307402ac6454SAndrew Thompson 
307502ac6454SAndrew Thompson 	DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
307602ac6454SAndrew Thompson 
307702ac6454SAndrew Thompson 	port = EHCI_PORTSC(index);
307802ac6454SAndrew Thompson 	v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
307902ac6454SAndrew Thompson 	EOWRITE4(sc, port, v | EHCI_PS_PO);
308002ac6454SAndrew Thompson }
308102ac6454SAndrew Thompson 
3082e0a69b51SAndrew Thompson static usb_error_t
3083760bc48eSAndrew Thompson ehci_roothub_exec(struct usb_device *udev,
3084760bc48eSAndrew Thompson     struct usb_device_request *req, const void **pptr, uint16_t *plength)
308502ac6454SAndrew Thompson {
3086459d369eSAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3087459d369eSAndrew Thompson 	const char *str_ptr;
3088459d369eSAndrew Thompson 	const void *ptr;
308902ac6454SAndrew Thompson 	uint32_t port;
309002ac6454SAndrew Thompson 	uint32_t v;
3091459d369eSAndrew Thompson 	uint16_t len;
309202ac6454SAndrew Thompson 	uint16_t i;
309302ac6454SAndrew Thompson 	uint16_t value;
309402ac6454SAndrew Thompson 	uint16_t index;
3095e0a69b51SAndrew Thompson 	usb_error_t err;
309602ac6454SAndrew Thompson 
309702ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
309802ac6454SAndrew Thompson 
309902ac6454SAndrew Thompson 	/* buffer reset */
3100459d369eSAndrew Thompson 	ptr = (const void *)&sc->sc_hub_desc;
3101459d369eSAndrew Thompson 	len = 0;
3102459d369eSAndrew Thompson 	err = 0;
310302ac6454SAndrew Thompson 
3104459d369eSAndrew Thompson 	value = UGETW(req->wValue);
3105459d369eSAndrew Thompson 	index = UGETW(req->wIndex);
310602ac6454SAndrew Thompson 
310702ac6454SAndrew Thompson 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
310802ac6454SAndrew Thompson 	    "wValue=0x%04x wIndex=0x%04x\n",
3109459d369eSAndrew Thompson 	    req->bmRequestType, req->bRequest,
3110459d369eSAndrew Thompson 	    UGETW(req->wLength), value, index);
311102ac6454SAndrew Thompson 
311202ac6454SAndrew Thompson #define	C(x,y) ((x) | ((y) << 8))
3113459d369eSAndrew Thompson 	switch (C(req->bRequest, req->bmRequestType)) {
311402ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
311502ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
311602ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
311702ac6454SAndrew Thompson 		/*
311802ac6454SAndrew Thompson 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
311902ac6454SAndrew Thompson 		 * for the integrated root hub.
312002ac6454SAndrew Thompson 		 */
312102ac6454SAndrew Thompson 		break;
312202ac6454SAndrew Thompson 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
3123459d369eSAndrew Thompson 		len = 1;
312402ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
312502ac6454SAndrew Thompson 		break;
312602ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
312702ac6454SAndrew Thompson 		switch (value >> 8) {
312802ac6454SAndrew Thompson 		case UDESC_DEVICE:
312902ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3130459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
313102ac6454SAndrew Thompson 				goto done;
313202ac6454SAndrew Thompson 			}
3133459d369eSAndrew Thompson 			len = sizeof(ehci_devd);
3134459d369eSAndrew Thompson 			ptr = (const void *)&ehci_devd;
313502ac6454SAndrew Thompson 			break;
313602ac6454SAndrew Thompson 			/*
313702ac6454SAndrew Thompson 			 * We can't really operate at another speed,
313802ac6454SAndrew Thompson 			 * but the specification says we need this
313902ac6454SAndrew Thompson 			 * descriptor:
314002ac6454SAndrew Thompson 			 */
314102ac6454SAndrew Thompson 		case UDESC_DEVICE_QUALIFIER:
314202ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3143459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
314402ac6454SAndrew Thompson 				goto done;
314502ac6454SAndrew Thompson 			}
3146459d369eSAndrew Thompson 			len = sizeof(ehci_odevd);
3147459d369eSAndrew Thompson 			ptr = (const void *)&ehci_odevd;
314802ac6454SAndrew Thompson 			break;
314902ac6454SAndrew Thompson 
315002ac6454SAndrew Thompson 		case UDESC_CONFIG:
315102ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3152459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
315302ac6454SAndrew Thompson 				goto done;
315402ac6454SAndrew Thompson 			}
3155459d369eSAndrew Thompson 			len = sizeof(ehci_confd);
3156459d369eSAndrew Thompson 			ptr = (const void *)&ehci_confd;
315702ac6454SAndrew Thompson 			break;
315802ac6454SAndrew Thompson 
315902ac6454SAndrew Thompson 		case UDESC_STRING:
316002ac6454SAndrew Thompson 			switch (value & 0xff) {
316102ac6454SAndrew Thompson 			case 0:	/* Language table */
3162459d369eSAndrew Thompson 				str_ptr = "\001";
316302ac6454SAndrew Thompson 				break;
316402ac6454SAndrew Thompson 
316502ac6454SAndrew Thompson 			case 1:	/* Vendor */
3166459d369eSAndrew Thompson 				str_ptr = sc->sc_vendor;
316702ac6454SAndrew Thompson 				break;
316802ac6454SAndrew Thompson 
316902ac6454SAndrew Thompson 			case 2:	/* Product */
3170459d369eSAndrew Thompson 				str_ptr = "EHCI root HUB";
317102ac6454SAndrew Thompson 				break;
317202ac6454SAndrew Thompson 
317302ac6454SAndrew Thompson 			default:
3174459d369eSAndrew Thompson 				str_ptr = "";
317502ac6454SAndrew Thompson 				break;
317602ac6454SAndrew Thompson 			}
317702ac6454SAndrew Thompson 
3178a593f6b8SAndrew Thompson 			len = usb_make_str_desc(
3179459d369eSAndrew Thompson 			    sc->sc_hub_desc.temp,
318002ac6454SAndrew Thompson 			    sizeof(sc->sc_hub_desc.temp),
3181459d369eSAndrew Thompson 			    str_ptr);
318202ac6454SAndrew Thompson 			break;
318302ac6454SAndrew Thompson 		default:
3184459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
318502ac6454SAndrew Thompson 			goto done;
318602ac6454SAndrew Thompson 		}
318702ac6454SAndrew Thompson 		break;
318802ac6454SAndrew Thompson 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3189459d369eSAndrew Thompson 		len = 1;
319002ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = 0;
319102ac6454SAndrew Thompson 		break;
319202ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_DEVICE):
3193459d369eSAndrew Thompson 		len = 2;
319402ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
319502ac6454SAndrew Thompson 		break;
319602ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
319702ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3198459d369eSAndrew Thompson 		len = 2;
319902ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
320002ac6454SAndrew Thompson 		break;
320102ac6454SAndrew Thompson 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3202ab42e8b2SAndrew Thompson 		if (value >= EHCI_MAX_DEVICES) {
3203459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
320402ac6454SAndrew Thompson 			goto done;
320502ac6454SAndrew Thompson 		}
320602ac6454SAndrew Thompson 		sc->sc_addr = value;
320702ac6454SAndrew Thompson 		break;
320802ac6454SAndrew Thompson 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
320902ac6454SAndrew Thompson 		if ((value != 0) && (value != 1)) {
3210459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
321102ac6454SAndrew Thompson 			goto done;
321202ac6454SAndrew Thompson 		}
321302ac6454SAndrew Thompson 		sc->sc_conf = value;
321402ac6454SAndrew Thompson 		break;
321502ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
321602ac6454SAndrew Thompson 		break;
321702ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
321802ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
321902ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3220459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
322102ac6454SAndrew Thompson 		goto done;
322202ac6454SAndrew Thompson 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
322302ac6454SAndrew Thompson 		break;
322402ac6454SAndrew Thompson 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
322502ac6454SAndrew Thompson 		break;
322602ac6454SAndrew Thompson 		/* Hub requests */
322702ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
322802ac6454SAndrew Thompson 		break;
322902ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
323002ac6454SAndrew Thompson 		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
323102ac6454SAndrew Thompson 
323202ac6454SAndrew Thompson 		if ((index < 1) ||
323302ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3234459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
323502ac6454SAndrew Thompson 			goto done;
323602ac6454SAndrew Thompson 		}
323702ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
323802ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
323902ac6454SAndrew Thompson 		switch (value) {
324002ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
324102ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PE);
324202ac6454SAndrew Thompson 			break;
324302ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
324402ac6454SAndrew Thompson 			if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
324502ac6454SAndrew Thompson 
324602ac6454SAndrew Thompson 				/*
324702ac6454SAndrew Thompson 				 * waking up a High Speed device is rather
324802ac6454SAndrew Thompson 				 * complicated if
324902ac6454SAndrew Thompson 				 */
325002ac6454SAndrew Thompson 				EOWRITE4(sc, port, v | EHCI_PS_FPR);
325102ac6454SAndrew Thompson 			}
325202ac6454SAndrew Thompson 			/* wait 20ms for resume sequence to complete */
3253a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
325402ac6454SAndrew Thompson 
325502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
325602ac6454SAndrew Thompson 			    EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
325702ac6454SAndrew Thompson 
32584a35cda7SAndrew Thompson 			/* 4ms settle time */
3259a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
326002ac6454SAndrew Thompson 			break;
326102ac6454SAndrew Thompson 		case UHF_PORT_POWER:
326202ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PP);
326302ac6454SAndrew Thompson 			break;
326402ac6454SAndrew Thompson 		case UHF_PORT_TEST:
326502ac6454SAndrew Thompson 			DPRINTFN(3, "clear port test "
326602ac6454SAndrew Thompson 			    "%d\n", index);
326702ac6454SAndrew Thompson 			break;
326802ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
326902ac6454SAndrew Thompson 			DPRINTFN(3, "clear port ind "
327002ac6454SAndrew Thompson 			    "%d\n", index);
327102ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
327202ac6454SAndrew Thompson 			break;
327302ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
327402ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
327502ac6454SAndrew Thompson 			break;
327602ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
327702ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
327802ac6454SAndrew Thompson 			break;
327902ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
328002ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
328102ac6454SAndrew Thompson 			break;
328202ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
328302ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
328402ac6454SAndrew Thompson 			break;
328502ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
328602ac6454SAndrew Thompson 			sc->sc_isreset = 0;
328702ac6454SAndrew Thompson 			break;
328802ac6454SAndrew Thompson 		default:
3289459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
329002ac6454SAndrew Thompson 			goto done;
329102ac6454SAndrew Thompson 		}
329202ac6454SAndrew Thompson 		break;
329302ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
329402ac6454SAndrew Thompson 		if ((value & 0xff) != 0) {
3295459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
329602ac6454SAndrew Thompson 			goto done;
329702ac6454SAndrew Thompson 		}
3298b494261cSHans Petter Selasky 		v = EREAD4(sc, EHCI_HCSPARAMS);
329902ac6454SAndrew Thompson 
330002ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd = ehci_hubd;
330102ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
330253e0bf6eSHans Petter Selasky 
330353e0bf6eSHans Petter Selasky 		if (EHCI_HCS_PPC(v))
330453e0bf6eSHans Petter Selasky 			i = UHD_PWR_INDIVIDUAL;
330553e0bf6eSHans Petter Selasky 		else
330653e0bf6eSHans Petter Selasky 			i = UHD_PWR_NO_SWITCH;
330753e0bf6eSHans Petter Selasky 
330853e0bf6eSHans Petter Selasky 		if (EHCI_HCS_P_INDICATOR(v))
330953e0bf6eSHans Petter Selasky 			i |= UHD_PORT_IND;
331053e0bf6eSHans Petter Selasky 
331153e0bf6eSHans Petter Selasky 		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
331202ac6454SAndrew Thompson 		/* XXX can't find out? */
331302ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
331453e0bf6eSHans Petter Selasky 		/* XXX don't know if ports are removable or not */
331502ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bDescLength =
331602ac6454SAndrew Thompson 		    8 + ((sc->sc_noport + 7) / 8);
3317459d369eSAndrew Thompson 		len = sc->sc_hub_desc.hubd.bDescLength;
331802ac6454SAndrew Thompson 		break;
331902ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3320459d369eSAndrew Thompson 		len = 16;
3321271ae033SHans Petter Selasky 		memset(sc->sc_hub_desc.temp, 0, 16);
332202ac6454SAndrew Thompson 		break;
332302ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
332402ac6454SAndrew Thompson 		DPRINTFN(9, "get port status i=%d\n",
332502ac6454SAndrew Thompson 		    index);
332602ac6454SAndrew Thompson 		if ((index < 1) ||
332702ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3328459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
332902ac6454SAndrew Thompson 			goto done;
333002ac6454SAndrew Thompson 		}
333102ac6454SAndrew Thompson 		v = EOREAD4(sc, EHCI_PORTSC(index));
333202ac6454SAndrew Thompson 		DPRINTFN(9, "port status=0x%04x\n", v);
3333e55e1ebcSAndrew Thompson 		if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) {
333402ac6454SAndrew Thompson 			if ((v & 0xc000000) == 0x8000000)
333502ac6454SAndrew Thompson 				i = UPS_HIGH_SPEED;
333602ac6454SAndrew Thompson 			else if ((v & 0xc000000) == 0x4000000)
333702ac6454SAndrew Thompson 				i = UPS_LOW_SPEED;
333802ac6454SAndrew Thompson 			else
333902ac6454SAndrew Thompson 				i = 0;
334002ac6454SAndrew Thompson 		} else {
334102ac6454SAndrew Thompson 			i = UPS_HIGH_SPEED;
334202ac6454SAndrew Thompson 		}
334302ac6454SAndrew Thompson 		if (v & EHCI_PS_CS)
334402ac6454SAndrew Thompson 			i |= UPS_CURRENT_CONNECT_STATUS;
334502ac6454SAndrew Thompson 		if (v & EHCI_PS_PE)
334602ac6454SAndrew Thompson 			i |= UPS_PORT_ENABLED;
334702ac6454SAndrew Thompson 		if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
334802ac6454SAndrew Thompson 			i |= UPS_SUSPEND;
334902ac6454SAndrew Thompson 		if (v & EHCI_PS_OCA)
335002ac6454SAndrew Thompson 			i |= UPS_OVERCURRENT_INDICATOR;
335102ac6454SAndrew Thompson 		if (v & EHCI_PS_PR)
335202ac6454SAndrew Thompson 			i |= UPS_RESET;
335302ac6454SAndrew Thompson 		if (v & EHCI_PS_PP)
335402ac6454SAndrew Thompson 			i |= UPS_PORT_POWER;
335502ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortStatus, i);
335602ac6454SAndrew Thompson 		i = 0;
335702ac6454SAndrew Thompson 		if (v & EHCI_PS_CSC)
335802ac6454SAndrew Thompson 			i |= UPS_C_CONNECT_STATUS;
335902ac6454SAndrew Thompson 		if (v & EHCI_PS_PEC)
336002ac6454SAndrew Thompson 			i |= UPS_C_PORT_ENABLED;
336102ac6454SAndrew Thompson 		if (v & EHCI_PS_OCC)
336202ac6454SAndrew Thompson 			i |= UPS_C_OVERCURRENT_INDICATOR;
336302ac6454SAndrew Thompson 		if (v & EHCI_PS_FPR)
336402ac6454SAndrew Thompson 			i |= UPS_C_SUSPEND;
336502ac6454SAndrew Thompson 		if (sc->sc_isreset)
336602ac6454SAndrew Thompson 			i |= UPS_C_PORT_RESET;
336702ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortChange, i);
3368459d369eSAndrew Thompson 		len = sizeof(sc->sc_hub_desc.ps);
336902ac6454SAndrew Thompson 		break;
337002ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3371459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
337202ac6454SAndrew Thompson 		goto done;
337302ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
337402ac6454SAndrew Thompson 		break;
337502ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
337602ac6454SAndrew Thompson 		if ((index < 1) ||
337702ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3378459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
337902ac6454SAndrew Thompson 			goto done;
338002ac6454SAndrew Thompson 		}
338102ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
338202ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
338302ac6454SAndrew Thompson 		switch (value) {
338402ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
338502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PE);
338602ac6454SAndrew Thompson 			break;
338702ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
338802ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
338902ac6454SAndrew Thompson 			break;
339002ac6454SAndrew Thompson 		case UHF_PORT_RESET:
339102ac6454SAndrew Thompson 			DPRINTFN(6, "reset port %d\n", index);
3392b850ecc1SAndrew Thompson #ifdef USB_DEBUG
339302ac6454SAndrew Thompson 			if (ehcinohighspeed) {
339402ac6454SAndrew Thompson 				/*
339502ac6454SAndrew Thompson 				 * Connect USB device to companion
339602ac6454SAndrew Thompson 				 * controller.
339702ac6454SAndrew Thompson 				 */
339802ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
339902ac6454SAndrew Thompson 				break;
340002ac6454SAndrew Thompson 			}
340102ac6454SAndrew Thompson #endif
3402e55e1ebcSAndrew Thompson 			if (EHCI_PS_IS_LOWSPEED(v) &&
3403e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
340402ac6454SAndrew Thompson 				/* Low speed device, give up ownership. */
340502ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
340602ac6454SAndrew Thompson 				break;
340702ac6454SAndrew Thompson 			}
340802ac6454SAndrew Thompson 			/* Start reset sequence. */
340902ac6454SAndrew Thompson 			v &= ~(EHCI_PS_PE | EHCI_PS_PR);
341002ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PR);
341102ac6454SAndrew Thompson 
341202ac6454SAndrew Thompson 			/* Wait for reset to complete. */
3413a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
341437506412SHans Petter Selasky 			    USB_MS_TO_TICKS(usb_port_root_reset_delay));
341502ac6454SAndrew Thompson 
341602ac6454SAndrew Thompson 			/* Terminate reset sequence. */
341702ac6454SAndrew Thompson 			if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
341802ac6454SAndrew Thompson 				EOWRITE4(sc, port, v);
341902ac6454SAndrew Thompson 
342002ac6454SAndrew Thompson 			/* Wait for HC to complete reset. */
3421a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
342202ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
342302ac6454SAndrew Thompson 
342402ac6454SAndrew Thompson 			v = EOREAD4(sc, port);
342502ac6454SAndrew Thompson 			DPRINTF("ehci after reset, status=0x%08x\n", v);
342602ac6454SAndrew Thompson 			if (v & EHCI_PS_PR) {
342702ac6454SAndrew Thompson 				device_printf(sc->sc_bus.bdev,
342802ac6454SAndrew Thompson 				    "port reset timeout\n");
3429459d369eSAndrew Thompson 				err = USB_ERR_TIMEOUT;
343002ac6454SAndrew Thompson 				goto done;
343102ac6454SAndrew Thompson 			}
3432e55e1ebcSAndrew Thompson 			if (!(v & EHCI_PS_PE) &&
3433e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3434e55e1ebcSAndrew Thompson 				/* Not a high speed device, give up ownership.*/
343502ac6454SAndrew Thompson 				ehci_disown(sc, index, 0);
343602ac6454SAndrew Thompson 				break;
343702ac6454SAndrew Thompson 			}
343802ac6454SAndrew Thompson 			sc->sc_isreset = 1;
343902ac6454SAndrew Thompson 			DPRINTF("ehci port %d reset, status = 0x%08x\n",
344002ac6454SAndrew Thompson 			    index, v);
344102ac6454SAndrew Thompson 			break;
344202ac6454SAndrew Thompson 
344302ac6454SAndrew Thompson 		case UHF_PORT_POWER:
344402ac6454SAndrew Thompson 			DPRINTFN(3, "set port power %d\n", index);
344502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PP);
344602ac6454SAndrew Thompson 			break;
344702ac6454SAndrew Thompson 
344802ac6454SAndrew Thompson 		case UHF_PORT_TEST:
344902ac6454SAndrew Thompson 			DPRINTFN(3, "set port test %d\n", index);
345002ac6454SAndrew Thompson 			break;
345102ac6454SAndrew Thompson 
345202ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
345302ac6454SAndrew Thompson 			DPRINTFN(3, "set port ind %d\n", index);
345402ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
345502ac6454SAndrew Thompson 			break;
345602ac6454SAndrew Thompson 
345702ac6454SAndrew Thompson 		default:
3458459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
345902ac6454SAndrew Thompson 			goto done;
346002ac6454SAndrew Thompson 		}
346102ac6454SAndrew Thompson 		break;
346202ac6454SAndrew Thompson 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
346302ac6454SAndrew Thompson 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
346402ac6454SAndrew Thompson 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
346502ac6454SAndrew Thompson 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
346602ac6454SAndrew Thompson 		break;
346702ac6454SAndrew Thompson 	default:
3468459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
346902ac6454SAndrew Thompson 		goto done;
347002ac6454SAndrew Thompson 	}
347102ac6454SAndrew Thompson done:
3472459d369eSAndrew Thompson 	*plength = len;
3473459d369eSAndrew Thompson 	*pptr = ptr;
3474459d369eSAndrew Thompson 	return (err);
347502ac6454SAndrew Thompson }
347602ac6454SAndrew Thompson 
347702ac6454SAndrew Thompson static void
3478760bc48eSAndrew Thompson ehci_xfer_setup(struct usb_setup_params *parm)
347902ac6454SAndrew Thompson {
3480760bc48eSAndrew Thompson 	struct usb_page_search page_info;
3481760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
348202ac6454SAndrew Thompson 	ehci_softc_t *sc;
3483760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
348402ac6454SAndrew Thompson 	void *last_obj;
348502ac6454SAndrew Thompson 	uint32_t nqtd;
348602ac6454SAndrew Thompson 	uint32_t nqh;
348702ac6454SAndrew Thompson 	uint32_t nsitd;
348802ac6454SAndrew Thompson 	uint32_t nitd;
348902ac6454SAndrew Thompson 	uint32_t n;
349002ac6454SAndrew Thompson 
349102ac6454SAndrew Thompson 	sc = EHCI_BUS2SC(parm->udev->bus);
349202ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
349302ac6454SAndrew Thompson 
349402ac6454SAndrew Thompson 	nqtd = 0;
349502ac6454SAndrew Thompson 	nqh = 0;
349602ac6454SAndrew Thompson 	nsitd = 0;
349702ac6454SAndrew Thompson 	nitd = 0;
349802ac6454SAndrew Thompson 
349902ac6454SAndrew Thompson 	/*
350002ac6454SAndrew Thompson 	 * compute maximum number of some structures
350102ac6454SAndrew Thompson 	 */
350202ac6454SAndrew Thompson 	if (parm->methods == &ehci_device_ctrl_methods) {
350302ac6454SAndrew Thompson 
350402ac6454SAndrew Thompson 		/*
350502ac6454SAndrew Thompson 		 * The proof for the "nqtd" formula is illustrated like
350602ac6454SAndrew Thompson 		 * this:
350702ac6454SAndrew Thompson 		 *
350802ac6454SAndrew Thompson 		 * +------------------------------------+
350902ac6454SAndrew Thompson 		 * |                                    |
351002ac6454SAndrew Thompson 		 * |         |remainder ->              |
351102ac6454SAndrew Thompson 		 * |   +-----+---+                      |
351202ac6454SAndrew Thompson 		 * |   | xxx | x | frm 0                |
351302ac6454SAndrew Thompson 		 * |   +-----+---++                     |
351402ac6454SAndrew Thompson 		 * |   | xxx | xx | frm 1               |
351502ac6454SAndrew Thompson 		 * |   +-----+----+                     |
351602ac6454SAndrew Thompson 		 * |            ...                     |
351702ac6454SAndrew Thompson 		 * +------------------------------------+
351802ac6454SAndrew Thompson 		 *
351902ac6454SAndrew Thompson 		 * "xxx" means a completely full USB transfer descriptor
352002ac6454SAndrew Thompson 		 *
352102ac6454SAndrew Thompson 		 * "x" and "xx" means a short USB packet
352202ac6454SAndrew Thompson 		 *
352302ac6454SAndrew Thompson 		 * For the remainder of an USB transfer modulo
352402ac6454SAndrew Thompson 		 * "max_data_length" we need two USB transfer descriptors.
352502ac6454SAndrew Thompson 		 * One to transfer the remaining data and one to finalise
352602ac6454SAndrew Thompson 		 * with a zero length packet in case the "force_short_xfer"
352702ac6454SAndrew Thompson 		 * flag is set. We only need two USB transfer descriptors in
352802ac6454SAndrew Thompson 		 * the case where the transfer length of the first one is a
352902ac6454SAndrew Thompson 		 * factor of "max_frame_size". The rest of the needed USB
353002ac6454SAndrew Thompson 		 * transfer descriptors is given by the buffer size divided
353102ac6454SAndrew Thompson 		 * by the maximum data payload.
353202ac6454SAndrew Thompson 		 */
353302ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
353402ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
353502ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
353602ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
353702ac6454SAndrew Thompson 
3538a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
353902ac6454SAndrew Thompson 
354002ac6454SAndrew Thompson 		nqh = 1;
354102ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes) + 1	/* STATUS */
3542578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
354302ac6454SAndrew Thompson 
354402ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_bulk_methods) {
354502ac6454SAndrew Thompson 
354602ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
354702ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
354802ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
354902ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
355002ac6454SAndrew Thompson 
3551a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
355202ac6454SAndrew Thompson 
355302ac6454SAndrew Thompson 		nqh = 1;
355402ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3555578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
355602ac6454SAndrew Thompson 
355702ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_intr_methods) {
355802ac6454SAndrew Thompson 
355902ac6454SAndrew Thompson 		if (parm->speed == USB_SPEED_HIGH) {
356002ac6454SAndrew Thompson 			parm->hc_max_packet_size = 0x400;
356102ac6454SAndrew Thompson 			parm->hc_max_packet_count = 3;
356202ac6454SAndrew Thompson 		} else if (parm->speed == USB_SPEED_FULL) {
356302ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
356402ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
356502ac6454SAndrew Thompson 		} else {
356602ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
356702ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
356802ac6454SAndrew Thompson 		}
356902ac6454SAndrew Thompson 
357002ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
357102ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
357202ac6454SAndrew Thompson 
3573a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
357402ac6454SAndrew Thompson 
357502ac6454SAndrew Thompson 		nqh = 1;
357602ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3577578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
357802ac6454SAndrew Thompson 
357902ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_fs_methods) {
358002ac6454SAndrew Thompson 
358102ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x3FF;
358202ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
358302ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x3FF;
358402ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
358502ac6454SAndrew Thompson 
3586a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
358702ac6454SAndrew Thompson 
358802ac6454SAndrew Thompson 		nsitd = xfer->nframes;
358902ac6454SAndrew Thompson 
359002ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_hs_methods) {
359102ac6454SAndrew Thompson 
359202ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
359302ac6454SAndrew Thompson 		parm->hc_max_packet_count = 3;
359402ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0xC00;
359502ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
359602ac6454SAndrew Thompson 
3597a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
359802ac6454SAndrew Thompson 
3599883bb300SAndrew Thompson 		nitd = ((xfer->nframes + 7) / 8) <<
3600883bb300SAndrew Thompson 		    usbd_xfer_get_fps_shift(xfer);
360102ac6454SAndrew Thompson 
360202ac6454SAndrew Thompson 	} else {
360302ac6454SAndrew Thompson 
360402ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
360502ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
360602ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x400;
360702ac6454SAndrew Thompson 
3608a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
360902ac6454SAndrew Thompson 	}
361002ac6454SAndrew Thompson 
361102ac6454SAndrew Thompson alloc_dma_set:
361202ac6454SAndrew Thompson 
361302ac6454SAndrew Thompson 	if (parm->err) {
361402ac6454SAndrew Thompson 		return;
361502ac6454SAndrew Thompson 	}
361602ac6454SAndrew Thompson 	/*
361702ac6454SAndrew Thompson 	 * Allocate queue heads and transfer descriptors
361802ac6454SAndrew Thompson 	 */
361902ac6454SAndrew Thompson 	last_obj = NULL;
362002ac6454SAndrew Thompson 
3621a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
362202ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_itd_t),
362302ac6454SAndrew Thompson 	    EHCI_ITD_ALIGN, nitd)) {
362402ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
362502ac6454SAndrew Thompson 		return;
362602ac6454SAndrew Thompson 	}
362702ac6454SAndrew Thompson 	if (parm->buf) {
362802ac6454SAndrew Thompson 		for (n = 0; n != nitd; n++) {
362902ac6454SAndrew Thompson 			ehci_itd_t *td;
363002ac6454SAndrew Thompson 
3631a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
363202ac6454SAndrew Thompson 
363302ac6454SAndrew Thompson 			td = page_info.buffer;
363402ac6454SAndrew Thompson 
363502ac6454SAndrew Thompson 			/* init TD */
3636e55e1ebcSAndrew Thompson 			td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
363702ac6454SAndrew Thompson 			td->obj_next = last_obj;
363802ac6454SAndrew Thompson 			td->page_cache = pc + n;
363902ac6454SAndrew Thompson 
364002ac6454SAndrew Thompson 			last_obj = td;
364102ac6454SAndrew Thompson 
3642a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
364302ac6454SAndrew Thompson 		}
364402ac6454SAndrew Thompson 	}
3645a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
364602ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_sitd_t),
364702ac6454SAndrew Thompson 	    EHCI_SITD_ALIGN, nsitd)) {
364802ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
364902ac6454SAndrew Thompson 		return;
365002ac6454SAndrew Thompson 	}
365102ac6454SAndrew Thompson 	if (parm->buf) {
365202ac6454SAndrew Thompson 		for (n = 0; n != nsitd; n++) {
365302ac6454SAndrew Thompson 			ehci_sitd_t *td;
365402ac6454SAndrew Thompson 
3655a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
365602ac6454SAndrew Thompson 
365702ac6454SAndrew Thompson 			td = page_info.buffer;
365802ac6454SAndrew Thompson 
365902ac6454SAndrew Thompson 			/* init TD */
3660e55e1ebcSAndrew Thompson 			td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
366102ac6454SAndrew Thompson 			td->obj_next = last_obj;
366202ac6454SAndrew Thompson 			td->page_cache = pc + n;
366302ac6454SAndrew Thompson 
366402ac6454SAndrew Thompson 			last_obj = td;
366502ac6454SAndrew Thompson 
3666a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
366702ac6454SAndrew Thompson 		}
366802ac6454SAndrew Thompson 	}
3669a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
367002ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qtd_t),
367102ac6454SAndrew Thompson 	    EHCI_QTD_ALIGN, nqtd)) {
367202ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
367302ac6454SAndrew Thompson 		return;
367402ac6454SAndrew Thompson 	}
367502ac6454SAndrew Thompson 	if (parm->buf) {
367602ac6454SAndrew Thompson 		for (n = 0; n != nqtd; n++) {
367702ac6454SAndrew Thompson 			ehci_qtd_t *qtd;
367802ac6454SAndrew Thompson 
3679a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
368002ac6454SAndrew Thompson 
368102ac6454SAndrew Thompson 			qtd = page_info.buffer;
368202ac6454SAndrew Thompson 
368302ac6454SAndrew Thompson 			/* init TD */
3684e55e1ebcSAndrew Thompson 			qtd->qtd_self = htohc32(sc, page_info.physaddr);
368502ac6454SAndrew Thompson 			qtd->obj_next = last_obj;
368602ac6454SAndrew Thompson 			qtd->page_cache = pc + n;
368702ac6454SAndrew Thompson 
368802ac6454SAndrew Thompson 			last_obj = qtd;
368902ac6454SAndrew Thompson 
3690a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
369102ac6454SAndrew Thompson 		}
369202ac6454SAndrew Thompson 	}
369302ac6454SAndrew Thompson 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
369402ac6454SAndrew Thompson 
369502ac6454SAndrew Thompson 	last_obj = NULL;
369602ac6454SAndrew Thompson 
3697a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
369802ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qh_t),
369902ac6454SAndrew Thompson 	    EHCI_QH_ALIGN, nqh)) {
370002ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
370102ac6454SAndrew Thompson 		return;
370202ac6454SAndrew Thompson 	}
370302ac6454SAndrew Thompson 	if (parm->buf) {
370402ac6454SAndrew Thompson 		for (n = 0; n != nqh; n++) {
370502ac6454SAndrew Thompson 			ehci_qh_t *qh;
370602ac6454SAndrew Thompson 
3707a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
370802ac6454SAndrew Thompson 
370902ac6454SAndrew Thompson 			qh = page_info.buffer;
371002ac6454SAndrew Thompson 
371102ac6454SAndrew Thompson 			/* init QH */
3712e55e1ebcSAndrew Thompson 			qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
371302ac6454SAndrew Thompson 			qh->obj_next = last_obj;
371402ac6454SAndrew Thompson 			qh->page_cache = pc + n;
371502ac6454SAndrew Thompson 
371602ac6454SAndrew Thompson 			last_obj = qh;
371702ac6454SAndrew Thompson 
3718a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
371902ac6454SAndrew Thompson 		}
372002ac6454SAndrew Thompson 	}
372102ac6454SAndrew Thompson 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
372202ac6454SAndrew Thompson 
372302ac6454SAndrew Thompson 	if (!xfer->flags_int.curr_dma_set) {
372402ac6454SAndrew Thompson 		xfer->flags_int.curr_dma_set = 1;
372502ac6454SAndrew Thompson 		goto alloc_dma_set;
372602ac6454SAndrew Thompson 	}
372702ac6454SAndrew Thompson }
372802ac6454SAndrew Thompson 
372902ac6454SAndrew Thompson static void
3730760bc48eSAndrew Thompson ehci_xfer_unsetup(struct usb_xfer *xfer)
373102ac6454SAndrew Thompson {
373202ac6454SAndrew Thompson 	return;
373302ac6454SAndrew Thompson }
373402ac6454SAndrew Thompson 
373502ac6454SAndrew Thompson static void
3736ae60fdfbSAndrew Thompson ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3737ae60fdfbSAndrew Thompson     struct usb_endpoint *ep)
373802ac6454SAndrew Thompson {
373902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
374002ac6454SAndrew Thompson 
3741ae60fdfbSAndrew Thompson 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3742ae60fdfbSAndrew Thompson 	    ep, udev->address,
3743f29a0724SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb_mode,
374402ac6454SAndrew Thompson 	    sc->sc_addr);
374502ac6454SAndrew Thompson 
374639307315SAndrew Thompson 	if (udev->device_index != sc->sc_addr) {
374739307315SAndrew Thompson 
374802ac6454SAndrew Thompson 		if ((udev->speed != USB_SPEED_HIGH) &&
374902ac6454SAndrew Thompson 		    ((udev->hs_hub_addr == 0) ||
375002ac6454SAndrew Thompson 		    (udev->hs_port_no == 0) ||
3751495f25ceSAndrew Thompson 		    (udev->parent_hs_hub == NULL) ||
3752495f25ceSAndrew Thompson 		    (udev->parent_hs_hub->hub == NULL))) {
375302ac6454SAndrew Thompson 			/* We need a transaction translator */
375402ac6454SAndrew Thompson 			goto done;
375502ac6454SAndrew Thompson 		}
375602ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
375702ac6454SAndrew Thompson 		case UE_CONTROL:
3758ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_ctrl_methods;
375902ac6454SAndrew Thompson 			break;
376002ac6454SAndrew Thompson 		case UE_INTERRUPT:
3761ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_intr_methods;
376202ac6454SAndrew Thompson 			break;
376302ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
376402ac6454SAndrew Thompson 			if (udev->speed == USB_SPEED_HIGH) {
3765ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_hs_methods;
376602ac6454SAndrew Thompson 			} else if (udev->speed == USB_SPEED_FULL) {
3767ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_fs_methods;
376802ac6454SAndrew Thompson 			}
376902ac6454SAndrew Thompson 			break;
377002ac6454SAndrew Thompson 		case UE_BULK:
3771ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_bulk_methods;
377202ac6454SAndrew Thompson 			break;
377302ac6454SAndrew Thompson 		default:
377402ac6454SAndrew Thompson 			/* do nothing */
377502ac6454SAndrew Thompson 			break;
377602ac6454SAndrew Thompson 		}
377702ac6454SAndrew Thompson 	}
377802ac6454SAndrew Thompson done:
377902ac6454SAndrew Thompson 	return;
378002ac6454SAndrew Thompson }
378102ac6454SAndrew Thompson 
378202ac6454SAndrew Thompson static void
3783527aa7b2SAndrew Thompson ehci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
378402ac6454SAndrew Thompson {
378502ac6454SAndrew Thompson 	/*
378602ac6454SAndrew Thompson 	 * Wait until the hardware has finished any possible use of
378702ac6454SAndrew Thompson 	 * the transfer descriptor(s) and QH
378802ac6454SAndrew Thompson 	 */
378934485480SHans Petter Selasky 	*pus = (1125);			/* microseconds */
379002ac6454SAndrew Thompson }
379102ac6454SAndrew Thompson 
379202ac6454SAndrew Thompson static void
3793760bc48eSAndrew Thompson ehci_device_resume(struct usb_device *udev)
379402ac6454SAndrew Thompson {
379502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3796760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3797e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
379802ac6454SAndrew Thompson 
379902ac6454SAndrew Thompson 	DPRINTF("\n");
380002ac6454SAndrew Thompson 
380102ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
380202ac6454SAndrew Thompson 
380302ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
380402ac6454SAndrew Thompson 
380502ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
380602ac6454SAndrew Thompson 
3807ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
380802ac6454SAndrew Thompson 
380902ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
381002ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
381102ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
381202ac6454SAndrew Thompson 				    sc->sc_async_p_last);
381302ac6454SAndrew Thompson 			}
381402ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
381502ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
381602ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
381702ac6454SAndrew Thompson 			}
381802ac6454SAndrew Thompson 		}
381902ac6454SAndrew Thompson 	}
382002ac6454SAndrew Thompson 
382102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
382202ac6454SAndrew Thompson 
382302ac6454SAndrew Thompson 	return;
382402ac6454SAndrew Thompson }
382502ac6454SAndrew Thompson 
382602ac6454SAndrew Thompson static void
3827760bc48eSAndrew Thompson ehci_device_suspend(struct usb_device *udev)
382802ac6454SAndrew Thompson {
382902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3830760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3831e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
383202ac6454SAndrew Thompson 
383302ac6454SAndrew Thompson 	DPRINTF("\n");
383402ac6454SAndrew Thompson 
383502ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
383602ac6454SAndrew Thompson 
383702ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
383802ac6454SAndrew Thompson 
383902ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
384002ac6454SAndrew Thompson 
3841ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
384202ac6454SAndrew Thompson 
384302ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
384402ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
384502ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
384602ac6454SAndrew Thompson 				    sc->sc_async_p_last);
384702ac6454SAndrew Thompson 			}
384802ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
384902ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
385002ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
385102ac6454SAndrew Thompson 			}
385202ac6454SAndrew Thompson 		}
385302ac6454SAndrew Thompson 	}
385402ac6454SAndrew Thompson 
385502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
38562e141748SHans Petter Selasky }
385702ac6454SAndrew Thompson 
38582e141748SHans Petter Selasky static void
38592e141748SHans Petter Selasky ehci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
38602e141748SHans Petter Selasky {
38612e141748SHans Petter Selasky 	struct ehci_softc *sc = EHCI_BUS2SC(bus);
38622e141748SHans Petter Selasky 
38632e141748SHans Petter Selasky 	switch (state) {
38642e141748SHans Petter Selasky 	case USB_HW_POWER_SUSPEND:
38652e141748SHans Petter Selasky 	case USB_HW_POWER_SHUTDOWN:
38662e141748SHans Petter Selasky 		ehci_suspend(sc);
38672e141748SHans Petter Selasky 		break;
38682e141748SHans Petter Selasky 	case USB_HW_POWER_RESUME:
38692e141748SHans Petter Selasky 		ehci_resume(sc);
38702e141748SHans Petter Selasky 		break;
38712e141748SHans Petter Selasky 	default:
38722e141748SHans Petter Selasky 		break;
38732e141748SHans Petter Selasky 	}
387402ac6454SAndrew Thompson }
387502ac6454SAndrew Thompson 
387602ac6454SAndrew Thompson static void
3877760bc48eSAndrew Thompson ehci_set_hw_power(struct usb_bus *bus)
387802ac6454SAndrew Thompson {
387902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
388002ac6454SAndrew Thompson 	uint32_t temp;
388102ac6454SAndrew Thompson 	uint32_t flags;
388202ac6454SAndrew Thompson 
388302ac6454SAndrew Thompson 	DPRINTF("\n");
388402ac6454SAndrew Thompson 
388502ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
388602ac6454SAndrew Thompson 
388702ac6454SAndrew Thompson 	flags = bus->hw_power_state;
388802ac6454SAndrew Thompson 
388902ac6454SAndrew Thompson 	temp = EOREAD4(sc, EHCI_USBCMD);
389002ac6454SAndrew Thompson 
389102ac6454SAndrew Thompson 	temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
389202ac6454SAndrew Thompson 
389302ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_CONTROL |
389402ac6454SAndrew Thompson 	    USB_HW_POWER_BULK)) {
389502ac6454SAndrew Thompson 		DPRINTF("Async is active\n");
389602ac6454SAndrew Thompson 		temp |= EHCI_CMD_ASE;
389702ac6454SAndrew Thompson 	}
389802ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_INTERRUPT |
389902ac6454SAndrew Thompson 	    USB_HW_POWER_ISOC)) {
390002ac6454SAndrew Thompson 		DPRINTF("Periodic is active\n");
390102ac6454SAndrew Thompson 		temp |= EHCI_CMD_PSE;
390202ac6454SAndrew Thompson 	}
390302ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, temp);
390402ac6454SAndrew Thompson 
390502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
390602ac6454SAndrew Thompson 
390702ac6454SAndrew Thompson 	return;
390802ac6454SAndrew Thompson }
390902ac6454SAndrew Thompson 
39104a6af125SHans Petter Selasky static void
39114a6af125SHans Petter Selasky ehci_start_dma_delay_second(struct usb_xfer *xfer)
39124a6af125SHans Petter Selasky {
39134a6af125SHans Petter Selasky 	struct ehci_softc *sc = EHCI_BUS2SC(xfer->xroot->bus);
39144a6af125SHans Petter Selasky 
39154a6af125SHans Petter Selasky 	DPRINTF("\n");
39164a6af125SHans Petter Selasky 
39174a6af125SHans Petter Selasky 	/* trigger doorbell */
39184a6af125SHans Petter Selasky 	ehci_doorbell_async(sc);
39194a6af125SHans Petter Selasky 
39204a6af125SHans Petter Selasky 	/* give the doorbell 4ms */
39214a6af125SHans Petter Selasky 	usbd_transfer_timeout_ms(xfer,
39224a6af125SHans Petter Selasky 	    (void (*)(void *))&usb_dma_delay_done_cb, 4);
39234a6af125SHans Petter Selasky }
39244a6af125SHans Petter Selasky 
39254a6af125SHans Petter Selasky /*
39264a6af125SHans Petter Selasky  * Ring the doorbell twice before freeing any DMA descriptors. Some host
39274a6af125SHans Petter Selasky  * controllers apparently cache the QH descriptors and need a message
39284a6af125SHans Petter Selasky  * that the cache needs to be discarded.
39294a6af125SHans Petter Selasky  */
39304a6af125SHans Petter Selasky static void
39314a6af125SHans Petter Selasky ehci_start_dma_delay(struct usb_xfer *xfer)
39324a6af125SHans Petter Selasky {
39334a6af125SHans Petter Selasky 	struct ehci_softc *sc = EHCI_BUS2SC(xfer->xroot->bus);
39344a6af125SHans Petter Selasky 
39354a6af125SHans Petter Selasky 	DPRINTF("\n");
39364a6af125SHans Petter Selasky 
39374a6af125SHans Petter Selasky 	/* trigger doorbell */
39384a6af125SHans Petter Selasky 	ehci_doorbell_async(sc);
39394a6af125SHans Petter Selasky 
39404a6af125SHans Petter Selasky 	/* give the doorbell 4ms */
39414a6af125SHans Petter Selasky 	usbd_transfer_timeout_ms(xfer,
39424a6af125SHans Petter Selasky 	    (void (*)(void *))&ehci_start_dma_delay_second, 4);
39434a6af125SHans Petter Selasky }
39444a6af125SHans Petter Selasky 
3945e892b3feSHans Petter Selasky static const struct usb_bus_methods ehci_bus_methods =
394602ac6454SAndrew Thompson {
3947ae60fdfbSAndrew Thompson 	.endpoint_init = ehci_ep_init,
394802ac6454SAndrew Thompson 	.xfer_setup = ehci_xfer_setup,
394902ac6454SAndrew Thompson 	.xfer_unsetup = ehci_xfer_unsetup,
395002ac6454SAndrew Thompson 	.get_dma_delay = ehci_get_dma_delay,
395102ac6454SAndrew Thompson 	.device_resume = ehci_device_resume,
395202ac6454SAndrew Thompson 	.device_suspend = ehci_device_suspend,
395302ac6454SAndrew Thompson 	.set_hw_power = ehci_set_hw_power,
39542e141748SHans Petter Selasky 	.set_hw_power_sleep = ehci_set_hw_power_sleep,
395539307315SAndrew Thompson 	.roothub_exec = ehci_roothub_exec,
3956dddb25f9SAlfred Perlstein 	.xfer_poll = ehci_do_poll,
39574a6af125SHans Petter Selasky 	.start_dma_delay = ehci_start_dma_delay,
395802ac6454SAndrew Thompson };
3959