xref: /freebsd/sys/dev/usb/controller/ehci.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
1d2b99310SHans Petter Selasky /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
3*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4*718cf2ccSPedro F. Giffuni  *
502ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
602ac6454SAndrew Thompson  * Copyright (c) 2004 The NetBSD Foundation, Inc. All rights reserved.
702ac6454SAndrew Thompson  * Copyright (c) 2004 Lennart Augustsson. All rights reserved.
802ac6454SAndrew Thompson  * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
902ac6454SAndrew Thompson  *
1002ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1102ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1202ac6454SAndrew Thompson  * are met:
1302ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1502ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1602ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1702ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1802ac6454SAndrew Thompson  *
1902ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2002ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2102ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2202ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2302ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2402ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2502ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2602ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2702ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2802ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2902ac6454SAndrew Thompson  * SUCH DAMAGE.
3002ac6454SAndrew Thompson  */
3102ac6454SAndrew Thompson 
3202ac6454SAndrew Thompson /*
3302ac6454SAndrew Thompson  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
3402ac6454SAndrew Thompson  *
3502ac6454SAndrew Thompson  * The EHCI 0.96 spec can be found at
3602ac6454SAndrew Thompson  * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
3702ac6454SAndrew Thompson  * The EHCI 1.0 spec can be found at
3802ac6454SAndrew Thompson  * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
3902ac6454SAndrew Thompson  * and the USB 2.0 spec at
4002ac6454SAndrew Thompson  * http://www.usb.org/developers/docs/usb_20.zip
4102ac6454SAndrew Thompson  *
4202ac6454SAndrew Thompson  */
4302ac6454SAndrew Thompson 
4402ac6454SAndrew Thompson /*
4502ac6454SAndrew Thompson  * TODO:
4602ac6454SAndrew Thompson  * 1) command failures are not recovered correctly
4702ac6454SAndrew Thompson  */
4802ac6454SAndrew Thompson 
49d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
50d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
51d2b99310SHans Petter Selasky #else
52ed6d949aSAndrew Thompson #include <sys/stdint.h>
53ed6d949aSAndrew Thompson #include <sys/stddef.h>
54ed6d949aSAndrew Thompson #include <sys/param.h>
55ed6d949aSAndrew Thompson #include <sys/queue.h>
56ed6d949aSAndrew Thompson #include <sys/types.h>
57ed6d949aSAndrew Thompson #include <sys/systm.h>
58ed6d949aSAndrew Thompson #include <sys/kernel.h>
59ed6d949aSAndrew Thompson #include <sys/bus.h>
60ed6d949aSAndrew Thompson #include <sys/module.h>
61ed6d949aSAndrew Thompson #include <sys/lock.h>
62ed6d949aSAndrew Thompson #include <sys/mutex.h>
63ed6d949aSAndrew Thompson #include <sys/condvar.h>
64ed6d949aSAndrew Thompson #include <sys/sysctl.h>
65ed6d949aSAndrew Thompson #include <sys/sx.h>
66ed6d949aSAndrew Thompson #include <sys/unistd.h>
67ed6d949aSAndrew Thompson #include <sys/callout.h>
68ed6d949aSAndrew Thompson #include <sys/malloc.h>
69ed6d949aSAndrew Thompson #include <sys/priv.h>
70ed6d949aSAndrew Thompson 
7102ac6454SAndrew Thompson #include <dev/usb/usb.h>
72ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson #define	USB_DEBUG_VAR ehcidebug
7502ac6454SAndrew Thompson 
7602ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7802ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
7902ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
8002ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
8102ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
8202ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
8302ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
8602ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
87d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
88d2b99310SHans Petter Selasky 
8902ac6454SAndrew Thompson #include <dev/usb/controller/ehci.h>
901def609aSAndrew Thompson #include <dev/usb/controller/ehcireg.h>
9102ac6454SAndrew Thompson 
92578d0effSAndrew Thompson #define	EHCI_BUS2SC(bus) \
93578d0effSAndrew Thompson    ((ehci_softc_t *)(((uint8_t *)(bus)) - \
94578d0effSAndrew Thompson     ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))
9502ac6454SAndrew Thompson 
96b850ecc1SAndrew Thompson #ifdef USB_DEBUG
9702ac6454SAndrew Thompson static int ehcidebug = 0;
9802ac6454SAndrew Thompson static int ehcinohighspeed = 0;
99cf223a88SAndrew Thompson static int ehciiaadbug = 0;
100cf223a88SAndrew Thompson static int ehcilostintrbug = 0;
10102ac6454SAndrew Thompson 
1026472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
103af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RWTUN,
10402ac6454SAndrew Thompson     &ehcidebug, 0, "Debug level");
105af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RWTUN,
10683cadd7dSHans Petter Selasky     &ehcinohighspeed, 0, "Disable High Speed USB");
107af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, iaadbug, CTLFLAG_RWTUN,
10883cadd7dSHans Petter Selasky     &ehciiaadbug, 0, "Enable doorbell bug workaround");
109af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, lostintrbug, CTLFLAG_RWTUN,
11083cadd7dSHans Petter Selasky     &ehcilostintrbug, 0, "Enable lost interrupt bug workaround");
11183cadd7dSHans Petter Selasky 
11202ac6454SAndrew Thompson static void ehci_dump_regs(ehci_softc_t *sc);
11302ac6454SAndrew Thompson static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson #endif
11602ac6454SAndrew Thompson 
11702ac6454SAndrew Thompson #define	EHCI_INTR_ENDPT 1
11802ac6454SAndrew Thompson 
119e892b3feSHans Petter Selasky static const struct usb_bus_methods ehci_bus_methods;
120e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_bulk_methods;
121e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_ctrl_methods;
122e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_intr_methods;
123e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_fs_methods;
124e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_hs_methods;
12502ac6454SAndrew Thompson 
12630b22abeSAndrew Thompson static void ehci_do_poll(struct usb_bus *);
12730b22abeSAndrew Thompson static void ehci_device_done(struct usb_xfer *, usb_error_t);
12830b22abeSAndrew Thompson static uint8_t ehci_check_transfer(struct usb_xfer *);
12930b22abeSAndrew Thompson static void ehci_timeout(void *);
13030b22abeSAndrew Thompson static void ehci_poll_timeout(void *);
13130b22abeSAndrew Thompson 
13239307315SAndrew Thompson static void ehci_root_intr(ehci_softc_t *sc);
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson struct ehci_std_temp {
13502ac6454SAndrew Thompson 	ehci_softc_t *sc;
136760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
13702ac6454SAndrew Thompson 	ehci_qtd_t *td;
13802ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
13902ac6454SAndrew Thompson 	uint32_t average;
14002ac6454SAndrew Thompson 	uint32_t qtd_status;
14102ac6454SAndrew Thompson 	uint32_t len;
14202ac6454SAndrew Thompson 	uint16_t max_frame_size;
14302ac6454SAndrew Thompson 	uint8_t	shortpkt;
14402ac6454SAndrew Thompson 	uint8_t	auto_data_toggle;
14502ac6454SAndrew Thompson 	uint8_t	setup_alt_next;
1460f7d4548SAndrew Thompson 	uint8_t	last_frame;
14702ac6454SAndrew Thompson };
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson void
150e0a69b51SAndrew Thompson ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
15102ac6454SAndrew Thompson {
15202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
15302ac6454SAndrew Thompson 	uint32_t i;
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
15602ac6454SAndrew Thompson 	    sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
15702ac6454SAndrew Thompson 
15853e0bf6eSHans Petter Selasky 	cb(bus, &sc->sc_hw.terminate_pc, &sc->sc_hw.terminate_pg,
15953e0bf6eSHans Petter Selasky 	    sizeof(struct ehci_qh_sub), EHCI_QH_ALIGN);
16053e0bf6eSHans Petter Selasky 
16102ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
16202ac6454SAndrew Thompson 	    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
16502ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.intr_start_pc + i,
16602ac6454SAndrew Thompson 		    sc->sc_hw.intr_start_pg + i,
16702ac6454SAndrew Thompson 		    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
16802ac6454SAndrew Thompson 	}
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
17102ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
17202ac6454SAndrew Thompson 		    sc->sc_hw.isoc_hs_start_pg + i,
17302ac6454SAndrew Thompson 		    sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
17402ac6454SAndrew Thompson 	}
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
17702ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
17802ac6454SAndrew Thompson 		    sc->sc_hw.isoc_fs_start_pg + i,
17902ac6454SAndrew Thompson 		    sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
18002ac6454SAndrew Thompson 	}
18102ac6454SAndrew Thompson }
18202ac6454SAndrew Thompson 
183e0a69b51SAndrew Thompson usb_error_t
184e55e1ebcSAndrew Thompson ehci_reset(ehci_softc_t *sc)
18502ac6454SAndrew Thompson {
18602ac6454SAndrew Thompson 	uint32_t hcr;
187e55e1ebcSAndrew Thompson 	int i;
188e55e1ebcSAndrew Thompson 
189e55e1ebcSAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
190e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
1912e141748SHans Petter Selasky 		usb_pause_mtx(NULL, hz / 128);
192e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
193e55e1ebcSAndrew Thompson 		if (!hcr) {
194cdf4ec68SMichal Meloun 			if (sc->sc_vendor_post_reset != NULL)
195cdf4ec68SMichal Meloun 				sc->sc_vendor_post_reset(sc);
196e55e1ebcSAndrew Thompson 			return (0);
197e55e1ebcSAndrew Thompson 		}
198e55e1ebcSAndrew Thompson 	}
1997ae432c0SNick Hibma 	device_printf(sc->sc_bus.bdev, "reset timeout\n");
200e55e1ebcSAndrew Thompson 	return (USB_ERR_IOERROR);
201e55e1ebcSAndrew Thompson }
202e55e1ebcSAndrew Thompson 
203e0a69b51SAndrew Thompson static usb_error_t
204e55e1ebcSAndrew Thompson ehci_hcreset(ehci_softc_t *sc)
205e55e1ebcSAndrew Thompson {
206e55e1ebcSAndrew Thompson 	uint32_t hcr;
207e55e1ebcSAndrew Thompson 	int i;
20802ac6454SAndrew Thompson 
20902ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
210e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
2112e141748SHans Petter Selasky 		usb_pause_mtx(NULL, hz / 128);
212e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
213e55e1ebcSAndrew Thompson 		if (hcr)
21402ac6454SAndrew Thompson 			break;
21502ac6454SAndrew Thompson 	}
216e55e1ebcSAndrew Thompson 	if (!hcr)
21702ac6454SAndrew Thompson 		/*
21802ac6454SAndrew Thompson                  * Fall through and try reset anyway even though
21902ac6454SAndrew Thompson                  * Table 2-9 in the EHCI spec says this will result
22002ac6454SAndrew Thompson                  * in undefined behavior.
22102ac6454SAndrew Thompson                  */
222e55e1ebcSAndrew Thompson 		device_printf(sc->sc_bus.bdev, "stop timeout\n");
22302ac6454SAndrew Thompson 
2242e141748SHans Petter Selasky 	return (ehci_reset(sc));
2252e141748SHans Petter Selasky }
2262e141748SHans Petter Selasky 
2272e141748SHans Petter Selasky static int
2282e141748SHans Petter Selasky ehci_init_sub(struct ehci_softc *sc)
2292e141748SHans Petter Selasky {
2302e141748SHans Petter Selasky 	struct usb_page_search buf_res;
2312e141748SHans Petter Selasky 	uint32_t cparams;
2322e141748SHans Petter Selasky   	uint32_t hcr;
2332e141748SHans Petter Selasky 	uint8_t i;
2342e141748SHans Petter Selasky 
2352e141748SHans Petter Selasky 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
2362e141748SHans Petter Selasky 
2372e141748SHans Petter Selasky 	DPRINTF("cparams=0x%x\n", cparams);
2382e141748SHans Petter Selasky 
2392e141748SHans Petter Selasky 	if (EHCI_HCC_64BIT(cparams)) {
2402e141748SHans Petter Selasky 		DPRINTF("HCC uses 64-bit structures\n");
2412e141748SHans Petter Selasky 
2422e141748SHans Petter Selasky 		/* MUST clear segment register if 64 bit capable */
243fcd51bb4SHans Petter Selasky 		EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
2442e141748SHans Petter Selasky 	}
2452e141748SHans Petter Selasky 
2462e141748SHans Petter Selasky 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
2472e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
2482e141748SHans Petter Selasky 
2492e141748SHans Petter Selasky 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
2502e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
2512e141748SHans Petter Selasky 
2522e141748SHans Petter Selasky 	/* enable interrupts */
2532e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
2542e141748SHans Petter Selasky 
2552e141748SHans Petter Selasky 	/* turn on controller */
2562e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_USBCMD,
2572e141748SHans Petter Selasky 	    EHCI_CMD_ITC_1 |		/* 1 microframes interrupt delay */
2582e141748SHans Petter Selasky 	    (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
2592e141748SHans Petter Selasky 	    EHCI_CMD_ASE |
2602e141748SHans Petter Selasky 	    EHCI_CMD_PSE |
2612e141748SHans Petter Selasky 	    EHCI_CMD_RS);
2622e141748SHans Petter Selasky 
2632e141748SHans Petter Selasky 	/* Take over port ownership */
2642e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
2652e141748SHans Petter Selasky 
2662e141748SHans Petter Selasky 	for (i = 0; i < 100; i++) {
2672e141748SHans Petter Selasky 		usb_pause_mtx(NULL, hz / 128);
2682e141748SHans Petter Selasky 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
2692e141748SHans Petter Selasky 		if (!hcr) {
2702e141748SHans Petter Selasky 			break;
2712e141748SHans Petter Selasky 		}
2722e141748SHans Petter Selasky 	}
2732e141748SHans Petter Selasky 	if (hcr) {
2747ae432c0SNick Hibma 		device_printf(sc->sc_bus.bdev, "run timeout\n");
2752e141748SHans Petter Selasky 		return (USB_ERR_IOERROR);
2762e141748SHans Petter Selasky 	}
2772e141748SHans Petter Selasky 	return (USB_ERR_NORMAL_COMPLETION);
27802ac6454SAndrew Thompson }
27902ac6454SAndrew Thompson 
280e0a69b51SAndrew Thompson usb_error_t
28102ac6454SAndrew Thompson ehci_init(ehci_softc_t *sc)
28202ac6454SAndrew Thompson {
283760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
28402ac6454SAndrew Thompson 	uint32_t version;
28502ac6454SAndrew Thompson 	uint32_t sparams;
28602ac6454SAndrew Thompson 	uint16_t i;
28702ac6454SAndrew Thompson 	uint16_t x;
28802ac6454SAndrew Thompson 	uint16_t y;
28902ac6454SAndrew Thompson 	uint16_t bit;
290e0a69b51SAndrew Thompson 	usb_error_t err = 0;
29102ac6454SAndrew Thompson 
29202ac6454SAndrew Thompson 	DPRINTF("start\n");
29302ac6454SAndrew Thompson 
294a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
29530b22abeSAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_mtx, 0);
29602ac6454SAndrew Thompson 
29746873d15SHans Petter Selasky 	sc->sc_offs = EHCI_CAPLENGTH(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
29846873d15SHans Petter Selasky 
299b850ecc1SAndrew Thompson #ifdef USB_DEBUG
300cf223a88SAndrew Thompson 	if (ehciiaadbug)
301cf223a88SAndrew Thompson 		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
302cf223a88SAndrew Thompson 	if (ehcilostintrbug)
303cf223a88SAndrew Thompson 		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
30402ac6454SAndrew Thompson 	if (ehcidebug > 2) {
30502ac6454SAndrew Thompson 		ehci_dump_regs(sc);
30602ac6454SAndrew Thompson 	}
30702ac6454SAndrew Thompson #endif
30802ac6454SAndrew Thompson 
309495ed64cSNathan Whitehorn 	version = EHCI_HCIVERSION(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
31002ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
31102ac6454SAndrew Thompson 	    version >> 8, version & 0xff);
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
31402ac6454SAndrew Thompson 	DPRINTF("sparams=0x%x\n", sparams);
31502ac6454SAndrew Thompson 
31602ac6454SAndrew Thompson 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
31702ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_2_0;
31802ac6454SAndrew Thompson 
319db43ed37SMarcel Moolenaar 	if (!(sc->sc_flags & EHCI_SCFLG_DONTRESET)) {
32002ac6454SAndrew Thompson 		/* Reset the controller */
321db43ed37SMarcel Moolenaar 		DPRINTF("%s: resetting\n",
322db43ed37SMarcel Moolenaar 		    device_get_nameunit(sc->sc_bus.bdev));
32302ac6454SAndrew Thompson 
324e55e1ebcSAndrew Thompson 		err = ehci_hcreset(sc);
32502ac6454SAndrew Thompson 		if (err) {
32602ac6454SAndrew Thompson 			device_printf(sc->sc_bus.bdev, "reset timeout\n");
32702ac6454SAndrew Thompson 			return (err);
32802ac6454SAndrew Thompson 		}
329db43ed37SMarcel Moolenaar 	}
330db43ed37SMarcel Moolenaar 
33102ac6454SAndrew Thompson 	/*
33202ac6454SAndrew Thompson 	 * use current frame-list-size selection 0: 1024*4 bytes 1:  512*4
33302ac6454SAndrew Thompson 	 * bytes 2:  256*4 bytes 3:      unknown
33402ac6454SAndrew Thompson 	 */
33502ac6454SAndrew Thompson 	if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
33602ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
33702ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
33802ac6454SAndrew Thompson 	}
33902ac6454SAndrew Thompson 	/* set up the bus struct */
34002ac6454SAndrew Thompson 	sc->sc_bus.methods = &ehci_bus_methods;
34102ac6454SAndrew Thompson 
34202ac6454SAndrew Thompson 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
34302ac6454SAndrew Thompson 
34453e0bf6eSHans Petter Selasky 	if (1) {
34553e0bf6eSHans Petter Selasky 		struct ehci_qh_sub *qh;
34653e0bf6eSHans Petter Selasky 
34753e0bf6eSHans Petter Selasky 		usbd_get_page(&sc->sc_hw.terminate_pc, 0, &buf_res);
34853e0bf6eSHans Petter Selasky 
34953e0bf6eSHans Petter Selasky 		qh = buf_res.buffer;
35053e0bf6eSHans Petter Selasky 
35153e0bf6eSHans Petter Selasky 		sc->sc_terminate_self = htohc32(sc, buf_res.physaddr);
35253e0bf6eSHans Petter Selasky 
35353e0bf6eSHans Petter Selasky 		/* init terminate TD */
35453e0bf6eSHans Petter Selasky 		qh->qtd_next =
35553e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_LINK_TERMINATE);
35653e0bf6eSHans Petter Selasky 		qh->qtd_altnext =
35753e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_LINK_TERMINATE);
35853e0bf6eSHans Petter Selasky 		qh->qtd_status =
35953e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_QTD_HALTED);
36053e0bf6eSHans Petter Selasky 	}
36153e0bf6eSHans Petter Selasky 
36202ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
36302ac6454SAndrew Thompson 		ehci_qh_t *qh;
36402ac6454SAndrew Thompson 
365a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson 		qh = buf_res.buffer;
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson 		/* initialize page cache pointer */
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson 		qh->page_cache = sc->sc_hw.intr_start_pc + i;
37202ac6454SAndrew Thompson 
37302ac6454SAndrew Thompson 		/* store a pointer to queue head */
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 		sc->sc_intr_p_last[i] = qh;
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 		qh->qh_self =
378e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
379e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 		qh->qh_endp =
382e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
38302ac6454SAndrew Thompson 		qh->qh_endphub =
384e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_MULT(1));
38502ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
38602ac6454SAndrew Thompson 
38702ac6454SAndrew Thompson 		qh->qh_qtd.qtd_next =
388e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
38902ac6454SAndrew Thompson 		qh->qh_qtd.qtd_altnext =
390e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
39102ac6454SAndrew Thompson 		qh->qh_qtd.qtd_status =
392e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QTD_HALTED);
39302ac6454SAndrew Thompson 	}
39402ac6454SAndrew Thompson 
39502ac6454SAndrew Thompson 	/*
39602ac6454SAndrew Thompson 	 * the QHs are arranged to give poll intervals that are
39702ac6454SAndrew Thompson 	 * powers of 2 times 1ms
39802ac6454SAndrew Thompson 	 */
39902ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
40002ac6454SAndrew Thompson 	while (bit) {
40102ac6454SAndrew Thompson 		x = bit;
40202ac6454SAndrew Thompson 		while (x & bit) {
40302ac6454SAndrew Thompson 			ehci_qh_t *qh_x;
40402ac6454SAndrew Thompson 			ehci_qh_t *qh_y;
40502ac6454SAndrew Thompson 
40602ac6454SAndrew Thompson 			y = (x ^ bit) | (bit / 2);
40702ac6454SAndrew Thompson 
40802ac6454SAndrew Thompson 			qh_x = sc->sc_intr_p_last[x];
40902ac6454SAndrew Thompson 			qh_y = sc->sc_intr_p_last[y];
41002ac6454SAndrew Thompson 
41102ac6454SAndrew Thompson 			/*
41202ac6454SAndrew Thompson 			 * the next QH has half the poll interval
41302ac6454SAndrew Thompson 			 */
41402ac6454SAndrew Thompson 			qh_x->qh_link = qh_y->qh_self;
41502ac6454SAndrew Thompson 
41602ac6454SAndrew Thompson 			x++;
41702ac6454SAndrew Thompson 		}
41802ac6454SAndrew Thompson 		bit >>= 1;
41902ac6454SAndrew Thompson 	}
42002ac6454SAndrew Thompson 
42102ac6454SAndrew Thompson 	if (1) {
42202ac6454SAndrew Thompson 		ehci_qh_t *qh;
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson 		qh = sc->sc_intr_p_last[0];
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson 		/* the last (1ms) QH terminates */
427e55e1ebcSAndrew Thompson 		qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
42802ac6454SAndrew Thompson 	}
42902ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
43002ac6454SAndrew Thompson 		ehci_sitd_t *sitd;
43102ac6454SAndrew Thompson 		ehci_itd_t *itd;
43202ac6454SAndrew Thompson 
433a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson 		sitd = buf_res.buffer;
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 		/* initialize page cache pointer */
43802ac6454SAndrew Thompson 
43902ac6454SAndrew Thompson 		sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
44002ac6454SAndrew Thompson 
44102ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
44202ac6454SAndrew Thompson 
44302ac6454SAndrew Thompson 		sc->sc_isoc_fs_p_last[i] = sitd;
44402ac6454SAndrew Thompson 
44502ac6454SAndrew Thompson 		/* initialize full speed isochronous */
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson 		sitd->sitd_self =
448e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
449e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_SITD);
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 		sitd->sitd_back =
452e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 		sitd->sitd_next =
45502ac6454SAndrew Thompson 		    sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 
458a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
45902ac6454SAndrew Thompson 
46002ac6454SAndrew Thompson 		itd = buf_res.buffer;
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson 		/* initialize page cache pointer */
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 		itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
46502ac6454SAndrew Thompson 
46602ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
46702ac6454SAndrew Thompson 
46802ac6454SAndrew Thompson 		sc->sc_isoc_hs_p_last[i] = itd;
46902ac6454SAndrew Thompson 
47002ac6454SAndrew Thompson 		/* initialize high speed isochronous */
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 		itd->itd_self =
473e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
474e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_ITD);
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson 		itd->itd_next =
47702ac6454SAndrew Thompson 		    sitd->sitd_self;
47802ac6454SAndrew Thompson 	}
47902ac6454SAndrew Thompson 
480a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson 	if (1) {
48302ac6454SAndrew Thompson 		uint32_t *pframes;
48402ac6454SAndrew Thompson 
48502ac6454SAndrew Thompson 		pframes = buf_res.buffer;
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson 		/*
48802ac6454SAndrew Thompson 		 * execution order:
48902ac6454SAndrew Thompson 		 * pframes -> high speed isochronous ->
49002ac6454SAndrew Thompson 		 *    full speed isochronous -> interrupt QH's
49102ac6454SAndrew Thompson 		 */
49202ac6454SAndrew Thompson 		for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
49302ac6454SAndrew Thompson 			pframes[i] = sc->sc_isoc_hs_p_last
49402ac6454SAndrew Thompson 			    [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
49502ac6454SAndrew Thompson 		}
49602ac6454SAndrew Thompson 	}
497a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
49802ac6454SAndrew Thompson 
49902ac6454SAndrew Thompson 	if (1) {
50002ac6454SAndrew Thompson 
50102ac6454SAndrew Thompson 		ehci_qh_t *qh;
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 		qh = buf_res.buffer;
50402ac6454SAndrew Thompson 
50502ac6454SAndrew Thompson 		/* initialize page cache pointer */
50602ac6454SAndrew Thompson 
50702ac6454SAndrew Thompson 		qh->page_cache = &sc->sc_hw.async_start_pc;
50802ac6454SAndrew Thompson 
50902ac6454SAndrew Thompson 		/* store a pointer to the queue head */
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson 		sc->sc_async_p_last = qh;
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson 		/* init dummy QH that starts the async list */
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 		qh->qh_self =
516e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
517e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
51802ac6454SAndrew Thompson 
51902ac6454SAndrew Thompson 		/* fill the QH */
52002ac6454SAndrew Thompson 		qh->qh_endp =
521e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
522e55e1ebcSAndrew Thompson 		qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
52302ac6454SAndrew Thompson 		qh->qh_link = qh->qh_self;
52402ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
52502ac6454SAndrew Thompson 
52602ac6454SAndrew Thompson 		/* fill the overlay qTD */
527e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
528e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
529e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
53002ac6454SAndrew Thompson 	}
53102ac6454SAndrew Thompson 	/* flush all cache into memory */
53202ac6454SAndrew Thompson 
533a593f6b8SAndrew Thompson 	usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
53402ac6454SAndrew Thompson 
535b850ecc1SAndrew Thompson #ifdef USB_DEBUG
53602ac6454SAndrew Thompson 	if (ehcidebug) {
53702ac6454SAndrew Thompson 		ehci_dump_sqh(sc, sc->sc_async_p_last);
53802ac6454SAndrew Thompson 	}
53902ac6454SAndrew Thompson #endif
54002ac6454SAndrew Thompson 
5412e141748SHans Petter Selasky 	/* finial setup */
5422e141748SHans Petter Selasky  	err = ehci_init_sub(sc);
54302ac6454SAndrew Thompson 
54402ac6454SAndrew Thompson 	if (!err) {
54502ac6454SAndrew Thompson 		/* catch any lost interrupts */
54602ac6454SAndrew Thompson 		ehci_do_poll(&sc->sc_bus);
54702ac6454SAndrew Thompson 	}
54802ac6454SAndrew Thompson 	return (err);
54902ac6454SAndrew Thompson }
55002ac6454SAndrew Thompson 
55102ac6454SAndrew Thompson /*
55202ac6454SAndrew Thompson  * shut down the controller when the system is going down
55302ac6454SAndrew Thompson  */
55402ac6454SAndrew Thompson void
55502ac6454SAndrew Thompson ehci_detach(ehci_softc_t *sc)
55602ac6454SAndrew Thompson {
55702ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
55802ac6454SAndrew Thompson 
559a593f6b8SAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_pcd);
56030b22abeSAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_poll);
56102ac6454SAndrew Thompson 
562bda9babeSAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, 0);
563e55e1ebcSAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
56402ac6454SAndrew Thompson 
565e55e1ebcSAndrew Thompson 	if (ehci_hcreset(sc)) {
56602ac6454SAndrew Thompson 		DPRINTF("reset failed!\n");
56702ac6454SAndrew Thompson 	}
56802ac6454SAndrew Thompson 
56902ac6454SAndrew Thompson 	/* XXX let stray task complete */
570a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL, hz / 20);
57102ac6454SAndrew Thompson 
572a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_pcd);
57330b22abeSAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_poll);
57402ac6454SAndrew Thompson }
57502ac6454SAndrew Thompson 
5762e141748SHans Petter Selasky static void
57702ac6454SAndrew Thompson ehci_suspend(ehci_softc_t *sc)
57802ac6454SAndrew Thompson {
57902ac6454SAndrew Thompson 	DPRINTF("stopping the HC\n");
58002ac6454SAndrew Thompson 
5812e141748SHans Petter Selasky 	/* reset HC */
5822e141748SHans Petter Selasky 	ehci_hcreset(sc);
58302ac6454SAndrew Thompson }
5842e141748SHans Petter Selasky 
5852e141748SHans Petter Selasky static void
5862e141748SHans Petter Selasky ehci_resume(ehci_softc_t *sc)
5872e141748SHans Petter Selasky {
5882e141748SHans Petter Selasky 	/* reset HC */
5892e141748SHans Petter Selasky 	ehci_hcreset(sc);
5902e141748SHans Petter Selasky 
5912e141748SHans Petter Selasky 	/* setup HC */
5922e141748SHans Petter Selasky 	ehci_init_sub(sc);
5932e141748SHans Petter Selasky 
5942e141748SHans Petter Selasky 	/* catch any lost interrupts */
5952e141748SHans Petter Selasky 	ehci_do_poll(&sc->sc_bus);
59602ac6454SAndrew Thompson }
59702ac6454SAndrew Thompson 
598b850ecc1SAndrew Thompson #ifdef USB_DEBUG
59902ac6454SAndrew Thompson static void
60002ac6454SAndrew Thompson ehci_dump_regs(ehci_softc_t *sc)
60102ac6454SAndrew Thompson {
60202ac6454SAndrew Thompson 	uint32_t i;
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBCMD);
60502ac6454SAndrew Thompson 	printf("cmd=0x%08x\n", i);
60602ac6454SAndrew Thompson 
60702ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_1)
60802ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_1\n");
60902ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_2)
61002ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_2\n");
61102ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_4)
61202ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_4\n");
61302ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_8)
61402ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_8\n");
61502ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_16)
61602ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_16\n");
61702ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_32)
61802ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_32\n");
61902ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_64)
62002ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_64\n");
62102ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPME)
62202ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPME\n");
62302ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPMC)
62402ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPMC\n");
62502ac6454SAndrew Thompson 	if (i & EHCI_CMD_LHCR)
62602ac6454SAndrew Thompson 		printf(" EHCI_CMD_LHCR\n");
62702ac6454SAndrew Thompson 	if (i & EHCI_CMD_IAAD)
62802ac6454SAndrew Thompson 		printf(" EHCI_CMD_IAAD\n");
62902ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASE)
63002ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASE\n");
63102ac6454SAndrew Thompson 	if (i & EHCI_CMD_PSE)
63202ac6454SAndrew Thompson 		printf(" EHCI_CMD_PSE\n");
63302ac6454SAndrew Thompson 	if (i & EHCI_CMD_FLS_M)
63402ac6454SAndrew Thompson 		printf(" EHCI_CMD_FLS_M\n");
63502ac6454SAndrew Thompson 	if (i & EHCI_CMD_HCRESET)
63602ac6454SAndrew Thompson 		printf(" EHCI_CMD_HCRESET\n");
63702ac6454SAndrew Thompson 	if (i & EHCI_CMD_RS)
63802ac6454SAndrew Thompson 		printf(" EHCI_CMD_RS\n");
63902ac6454SAndrew Thompson 
64002ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBSTS);
64102ac6454SAndrew Thompson 
64202ac6454SAndrew Thompson 	printf("sts=0x%08x\n", i);
64302ac6454SAndrew Thompson 
64402ac6454SAndrew Thompson 	if (i & EHCI_STS_ASS)
64502ac6454SAndrew Thompson 		printf(" EHCI_STS_ASS\n");
64602ac6454SAndrew Thompson 	if (i & EHCI_STS_PSS)
64702ac6454SAndrew Thompson 		printf(" EHCI_STS_PSS\n");
64802ac6454SAndrew Thompson 	if (i & EHCI_STS_REC)
64902ac6454SAndrew Thompson 		printf(" EHCI_STS_REC\n");
65002ac6454SAndrew Thompson 	if (i & EHCI_STS_HCH)
65102ac6454SAndrew Thompson 		printf(" EHCI_STS_HCH\n");
65202ac6454SAndrew Thompson 	if (i & EHCI_STS_IAA)
65302ac6454SAndrew Thompson 		printf(" EHCI_STS_IAA\n");
65402ac6454SAndrew Thompson 	if (i & EHCI_STS_HSE)
65502ac6454SAndrew Thompson 		printf(" EHCI_STS_HSE\n");
65602ac6454SAndrew Thompson 	if (i & EHCI_STS_FLR)
65702ac6454SAndrew Thompson 		printf(" EHCI_STS_FLR\n");
65802ac6454SAndrew Thompson 	if (i & EHCI_STS_PCD)
65902ac6454SAndrew Thompson 		printf(" EHCI_STS_PCD\n");
66002ac6454SAndrew Thompson 	if (i & EHCI_STS_ERRINT)
66102ac6454SAndrew Thompson 		printf(" EHCI_STS_ERRINT\n");
66202ac6454SAndrew Thompson 	if (i & EHCI_STS_INT)
66302ac6454SAndrew Thompson 		printf(" EHCI_STS_INT\n");
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson 	printf("ien=0x%08x\n",
66602ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_USBINTR));
66702ac6454SAndrew Thompson 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
66802ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_FRINDEX),
66902ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
67002ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
67102ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
67202ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
67302ac6454SAndrew Thompson 		printf("port %d status=0x%08x\n", i,
67402ac6454SAndrew Thompson 		    EOREAD4(sc, EHCI_PORTSC(i)));
67502ac6454SAndrew Thompson 	}
67602ac6454SAndrew Thompson }
67702ac6454SAndrew Thompson 
67802ac6454SAndrew Thompson static void
67902ac6454SAndrew Thompson ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
68002ac6454SAndrew Thompson {
681e55e1ebcSAndrew Thompson 	link = hc32toh(sc, link);
68202ac6454SAndrew Thompson 	printf("0x%08x", link);
68302ac6454SAndrew Thompson 	if (link & EHCI_LINK_TERMINATE)
68402ac6454SAndrew Thompson 		printf("<T>");
68502ac6454SAndrew Thompson 	else {
68602ac6454SAndrew Thompson 		printf("<");
68702ac6454SAndrew Thompson 		if (type) {
68802ac6454SAndrew Thompson 			switch (EHCI_LINK_TYPE(link)) {
68902ac6454SAndrew Thompson 			case EHCI_LINK_ITD:
69002ac6454SAndrew Thompson 				printf("ITD");
69102ac6454SAndrew Thompson 				break;
69202ac6454SAndrew Thompson 			case EHCI_LINK_QH:
69302ac6454SAndrew Thompson 				printf("QH");
69402ac6454SAndrew Thompson 				break;
69502ac6454SAndrew Thompson 			case EHCI_LINK_SITD:
69602ac6454SAndrew Thompson 				printf("SITD");
69702ac6454SAndrew Thompson 				break;
69802ac6454SAndrew Thompson 			case EHCI_LINK_FSTN:
69902ac6454SAndrew Thompson 				printf("FSTN");
70002ac6454SAndrew Thompson 				break;
70102ac6454SAndrew Thompson 			}
70202ac6454SAndrew Thompson 		}
70302ac6454SAndrew Thompson 		printf(">");
70402ac6454SAndrew Thompson 	}
70502ac6454SAndrew Thompson }
70602ac6454SAndrew Thompson 
70702ac6454SAndrew Thompson static void
70802ac6454SAndrew Thompson ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
70902ac6454SAndrew Thompson {
71002ac6454SAndrew Thompson 	uint32_t s;
71102ac6454SAndrew Thompson 
71202ac6454SAndrew Thompson 	printf("  next=");
71302ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_next, 0);
71402ac6454SAndrew Thompson 	printf(" altnext=");
71502ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_altnext, 0);
71602ac6454SAndrew Thompson 	printf("\n");
717e55e1ebcSAndrew Thompson 	s = hc32toh(sc, qtd->qtd_status);
71802ac6454SAndrew Thompson 	printf("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
71902ac6454SAndrew Thompson 	    s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
72002ac6454SAndrew Thompson 	    EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
72102ac6454SAndrew Thompson 	printf("    cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
72202ac6454SAndrew Thompson 	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
72302ac6454SAndrew Thompson 	    (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
72402ac6454SAndrew Thompson 	    (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
72502ac6454SAndrew Thompson 	    (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
72602ac6454SAndrew Thompson 	    (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
72702ac6454SAndrew Thompson 	    (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
72802ac6454SAndrew Thompson 	    (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
72902ac6454SAndrew Thompson 	    (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
73002ac6454SAndrew Thompson 	    (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
73302ac6454SAndrew Thompson 		printf("  buffer[%d]=0x%08x\n", s,
734e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer[s]));
73502ac6454SAndrew Thompson 	}
73602ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
73702ac6454SAndrew Thompson 		printf("  buffer_hi[%d]=0x%08x\n", s,
738e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer_hi[s]));
73902ac6454SAndrew Thompson 	}
74002ac6454SAndrew Thompson }
74102ac6454SAndrew Thompson 
74202ac6454SAndrew Thompson static uint8_t
74302ac6454SAndrew Thompson ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
74402ac6454SAndrew Thompson {
74502ac6454SAndrew Thompson 	uint8_t temp;
74602ac6454SAndrew Thompson 
747a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sqtd->page_cache);
748e55e1ebcSAndrew Thompson 	printf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
74902ac6454SAndrew Thompson 	ehci_dump_qtd(sc, sqtd);
750e55e1ebcSAndrew Thompson 	temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
75102ac6454SAndrew Thompson 	return (temp);
75202ac6454SAndrew Thompson }
75302ac6454SAndrew Thompson 
75402ac6454SAndrew Thompson static void
75502ac6454SAndrew Thompson ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
75602ac6454SAndrew Thompson {
75702ac6454SAndrew Thompson 	uint16_t i;
75802ac6454SAndrew Thompson 	uint8_t stop;
75902ac6454SAndrew Thompson 
76002ac6454SAndrew Thompson 	stop = 0;
76102ac6454SAndrew Thompson 	for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
76202ac6454SAndrew Thompson 		stop = ehci_dump_sqtd(sc, sqtd);
76302ac6454SAndrew Thompson 	}
76402ac6454SAndrew Thompson 	if (sqtd) {
76502ac6454SAndrew Thompson 		printf("dump aborted, too many TDs\n");
76602ac6454SAndrew Thompson 	}
76702ac6454SAndrew Thompson }
76802ac6454SAndrew Thompson 
76902ac6454SAndrew Thompson static void
77002ac6454SAndrew Thompson ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
77102ac6454SAndrew Thompson {
77202ac6454SAndrew Thompson 	uint32_t endp;
77302ac6454SAndrew Thompson 	uint32_t endphub;
77402ac6454SAndrew Thompson 
775a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
776e55e1ebcSAndrew Thompson 	printf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
77702ac6454SAndrew Thompson 	printf("  link=");
77802ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_link, 1);
77902ac6454SAndrew Thompson 	printf("\n");
780e55e1ebcSAndrew Thompson 	endp = hc32toh(sc, qh->qh_endp);
78102ac6454SAndrew Thompson 	printf("  endp=0x%08x\n", endp);
78202ac6454SAndrew Thompson 	printf("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
78302ac6454SAndrew Thompson 	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
78402ac6454SAndrew Thompson 	    EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
78502ac6454SAndrew Thompson 	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
78602ac6454SAndrew Thompson 	printf("    mpl=0x%x ctl=%d nrl=%d\n",
78702ac6454SAndrew Thompson 	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
78802ac6454SAndrew Thompson 	    EHCI_QH_GET_NRL(endp));
789e55e1ebcSAndrew Thompson 	endphub = hc32toh(sc, qh->qh_endphub);
79002ac6454SAndrew Thompson 	printf("  endphub=0x%08x\n", endphub);
79102ac6454SAndrew Thompson 	printf("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
79202ac6454SAndrew Thompson 	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
79302ac6454SAndrew Thompson 	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
79402ac6454SAndrew Thompson 	    EHCI_QH_GET_MULT(endphub));
79502ac6454SAndrew Thompson 	printf("  curqtd=");
79602ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_curqtd, 0);
79702ac6454SAndrew Thompson 	printf("\n");
79802ac6454SAndrew Thompson 	printf("Overlay qTD:\n");
79902ac6454SAndrew Thompson 	ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
80002ac6454SAndrew Thompson }
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson static void
80302ac6454SAndrew Thompson ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
80402ac6454SAndrew Thompson {
805a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sitd->page_cache);
806e55e1ebcSAndrew Thompson 	printf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
807e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
80802ac6454SAndrew Thompson 	printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
809e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_portaddr),
810e55e1ebcSAndrew Thompson 	    (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
81102ac6454SAndrew Thompson 	    ? "in" : "out",
812e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
813e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
814e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
815e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
816e55e1ebcSAndrew Thompson 	printf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
817e55e1ebcSAndrew Thompson 	printf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
818e55e1ebcSAndrew Thompson 	    (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
819e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
82002ac6454SAndrew Thompson 	printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
821e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_back),
822e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[0]),
823e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[1]),
824e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[0]),
825e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[1]));
82602ac6454SAndrew Thompson }
82702ac6454SAndrew Thompson 
82802ac6454SAndrew Thompson static void
82902ac6454SAndrew Thompson ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
83002ac6454SAndrew Thompson {
831a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(itd->page_cache);
832e55e1ebcSAndrew Thompson 	printf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
833e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
834e55e1ebcSAndrew Thompson 	printf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
835e55e1ebcSAndrew Thompson 	    (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
836e55e1ebcSAndrew Thompson 	printf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
837e55e1ebcSAndrew Thompson 	    (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
838e55e1ebcSAndrew Thompson 	printf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
839e55e1ebcSAndrew Thompson 	    (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
840e55e1ebcSAndrew Thompson 	printf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
841e55e1ebcSAndrew Thompson 	    (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
842e55e1ebcSAndrew Thompson 	printf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
843e55e1ebcSAndrew Thompson 	    (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
844e55e1ebcSAndrew Thompson 	printf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
845e55e1ebcSAndrew Thompson 	    (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
846e55e1ebcSAndrew Thompson 	printf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
847e55e1ebcSAndrew Thompson 	    (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
848e55e1ebcSAndrew Thompson 	printf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
849e55e1ebcSAndrew Thompson 	    (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
850e55e1ebcSAndrew Thompson 	printf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
85102ac6454SAndrew Thompson 	printf("  addr=0x%02x; endpt=0x%01x\n",
852e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
853e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
854e55e1ebcSAndrew Thompson 	printf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
85502ac6454SAndrew Thompson 	printf(" dir=%s; mpl=0x%02x\n",
856e55e1ebcSAndrew Thompson 	    (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
857e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
85802ac6454SAndrew Thompson 	printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
859e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[2]),
860e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[3]),
861e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[4]),
862e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[5]),
863e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[6]));
86402ac6454SAndrew Thompson 	printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
86502ac6454SAndrew Thompson 	    "       0x%08x,0x%08x,0x%08x\n",
866e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[0]),
867e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[1]),
868e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[2]),
869e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[3]),
870e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[4]),
871e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[5]),
872e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[6]));
87302ac6454SAndrew Thompson }
87402ac6454SAndrew Thompson 
87502ac6454SAndrew Thompson static void
87602ac6454SAndrew Thompson ehci_dump_isoc(ehci_softc_t *sc)
87702ac6454SAndrew Thompson {
87802ac6454SAndrew Thompson 	ehci_itd_t *itd;
87902ac6454SAndrew Thompson 	ehci_sitd_t *sitd;
88002ac6454SAndrew Thompson 	uint16_t max = 1000;
88102ac6454SAndrew Thompson 	uint16_t pos;
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 	pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
88402ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson 	printf("%s: isochronous dump from frame 0x%03x:\n",
88702ac6454SAndrew Thompson 	    __FUNCTION__, pos);
88802ac6454SAndrew Thompson 
88902ac6454SAndrew Thompson 	itd = sc->sc_isoc_hs_p_last[pos];
89002ac6454SAndrew Thompson 	sitd = sc->sc_isoc_fs_p_last[pos];
89102ac6454SAndrew Thompson 
89202ac6454SAndrew Thompson 	while (itd && max && max--) {
89302ac6454SAndrew Thompson 		ehci_dump_itd(sc, itd);
89402ac6454SAndrew Thompson 		itd = itd->prev;
89502ac6454SAndrew Thompson 	}
89602ac6454SAndrew Thompson 
89702ac6454SAndrew Thompson 	while (sitd && max && max--) {
89802ac6454SAndrew Thompson 		ehci_dump_sitd(sc, sitd);
89902ac6454SAndrew Thompson 		sitd = sitd->prev;
90002ac6454SAndrew Thompson 	}
90102ac6454SAndrew Thompson }
90202ac6454SAndrew Thompson 
90302ac6454SAndrew Thompson #endif
90402ac6454SAndrew Thompson 
90502ac6454SAndrew Thompson static void
906760bc48eSAndrew Thompson ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
90702ac6454SAndrew Thompson {
90802ac6454SAndrew Thompson 	/* check for early completion */
90902ac6454SAndrew Thompson 	if (ehci_check_transfer(xfer)) {
91002ac6454SAndrew Thompson 		return;
91102ac6454SAndrew Thompson 	}
91202ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
913a593f6b8SAndrew Thompson 	usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
91402ac6454SAndrew Thompson 
91502ac6454SAndrew Thompson 	/* start timeout, if any */
91602ac6454SAndrew Thompson 	if (xfer->timeout != 0) {
917a593f6b8SAndrew Thompson 		usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
91802ac6454SAndrew Thompson 	}
91902ac6454SAndrew Thompson }
92002ac6454SAndrew Thompson 
92102ac6454SAndrew Thompson #define	EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
92202ac6454SAndrew Thompson static ehci_sitd_t *
92302ac6454SAndrew Thompson _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
92402ac6454SAndrew Thompson {
92502ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
92602ac6454SAndrew Thompson 
92702ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
92802ac6454SAndrew Thompson 
92902ac6454SAndrew Thompson 	std->next = last->next;
93002ac6454SAndrew Thompson 	std->sitd_next = last->sitd_next;
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 	std->prev = last;
93302ac6454SAndrew Thompson 
934a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
93502ac6454SAndrew Thompson 
93602ac6454SAndrew Thompson 	/*
93702ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
93802ac6454SAndrew Thompson 	 */
93902ac6454SAndrew Thompson 	last->next = std;
94002ac6454SAndrew Thompson 	last->sitd_next = std->sitd_self;
94102ac6454SAndrew Thompson 
942a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
94302ac6454SAndrew Thompson 
94402ac6454SAndrew Thompson 	return (std);
94502ac6454SAndrew Thompson }
94602ac6454SAndrew Thompson 
94702ac6454SAndrew Thompson #define	EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
94802ac6454SAndrew Thompson static ehci_itd_t *
94902ac6454SAndrew Thompson _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
95002ac6454SAndrew Thompson {
95102ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
95202ac6454SAndrew Thompson 
95302ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
95402ac6454SAndrew Thompson 
95502ac6454SAndrew Thompson 	std->next = last->next;
95602ac6454SAndrew Thompson 	std->itd_next = last->itd_next;
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson 	std->prev = last;
95902ac6454SAndrew Thompson 
960a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
96102ac6454SAndrew Thompson 
96202ac6454SAndrew Thompson 	/*
96302ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
96402ac6454SAndrew Thompson 	 */
96502ac6454SAndrew Thompson 	last->next = std;
96602ac6454SAndrew Thompson 	last->itd_next = std->itd_self;
96702ac6454SAndrew Thompson 
968a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
96902ac6454SAndrew Thompson 
97002ac6454SAndrew Thompson 	return (std);
97102ac6454SAndrew Thompson }
97202ac6454SAndrew Thompson 
97302ac6454SAndrew Thompson #define	EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
97402ac6454SAndrew Thompson static ehci_qh_t *
97502ac6454SAndrew Thompson _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
97602ac6454SAndrew Thompson {
97702ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", sqh, last);
97802ac6454SAndrew Thompson 
97902ac6454SAndrew Thompson 	if (sqh->prev != NULL) {
98002ac6454SAndrew Thompson 		/* should not happen */
98102ac6454SAndrew Thompson 		DPRINTFN(0, "QH already linked!\n");
98202ac6454SAndrew Thompson 		return (last);
98302ac6454SAndrew Thompson 	}
98402ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson 	sqh->next = last->next;
98702ac6454SAndrew Thompson 	sqh->qh_link = last->qh_link;
98802ac6454SAndrew Thompson 
98902ac6454SAndrew Thompson 	sqh->prev = last;
99002ac6454SAndrew Thompson 
991a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(sqh->page_cache);
99202ac6454SAndrew Thompson 
99302ac6454SAndrew Thompson 	/*
99402ac6454SAndrew Thompson 	 * the last->next->prev is never followed: sqh->next->prev = sqh;
99502ac6454SAndrew Thompson 	 */
99602ac6454SAndrew Thompson 
99702ac6454SAndrew Thompson 	last->next = sqh;
99802ac6454SAndrew Thompson 	last->qh_link = sqh->qh_self;
99902ac6454SAndrew Thompson 
1000a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
100102ac6454SAndrew Thompson 
100202ac6454SAndrew Thompson 	return (sqh);
100302ac6454SAndrew Thompson }
100402ac6454SAndrew Thompson 
100502ac6454SAndrew Thompson #define	EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
100602ac6454SAndrew Thompson static ehci_sitd_t *
100702ac6454SAndrew Thompson _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
100802ac6454SAndrew Thompson {
100902ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
101002ac6454SAndrew Thompson 
101102ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
101202ac6454SAndrew Thompson 
101302ac6454SAndrew Thompson 	std->prev->next = std->next;
101402ac6454SAndrew Thompson 	std->prev->sitd_next = std->sitd_next;
101502ac6454SAndrew Thompson 
1016a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
101702ac6454SAndrew Thompson 
101802ac6454SAndrew Thompson 	if (std->next) {
101902ac6454SAndrew Thompson 		std->next->prev = std->prev;
1020a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
102102ac6454SAndrew Thompson 	}
102202ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
102302ac6454SAndrew Thompson }
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson #define	EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
102602ac6454SAndrew Thompson static ehci_itd_t *
102702ac6454SAndrew Thompson _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
102802ac6454SAndrew Thompson {
102902ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
103002ac6454SAndrew Thompson 
103102ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
103202ac6454SAndrew Thompson 
103302ac6454SAndrew Thompson 	std->prev->next = std->next;
103402ac6454SAndrew Thompson 	std->prev->itd_next = std->itd_next;
103502ac6454SAndrew Thompson 
1036a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
103702ac6454SAndrew Thompson 
103802ac6454SAndrew Thompson 	if (std->next) {
103902ac6454SAndrew Thompson 		std->next->prev = std->prev;
1040a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
104102ac6454SAndrew Thompson 	}
104202ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
104302ac6454SAndrew Thompson }
104402ac6454SAndrew Thompson 
104502ac6454SAndrew Thompson #define	EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
104602ac6454SAndrew Thompson static ehci_qh_t *
104702ac6454SAndrew Thompson _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
104802ac6454SAndrew Thompson {
104902ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", sqh, last);
105002ac6454SAndrew Thompson 
105102ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
105202ac6454SAndrew Thompson 
105302ac6454SAndrew Thompson 	/* only remove if not removed from a queue */
105402ac6454SAndrew Thompson 	if (sqh->prev) {
105502ac6454SAndrew Thompson 
105602ac6454SAndrew Thompson 		sqh->prev->next = sqh->next;
105702ac6454SAndrew Thompson 		sqh->prev->qh_link = sqh->qh_link;
105802ac6454SAndrew Thompson 
1059a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->prev->page_cache);
106002ac6454SAndrew Thompson 
106102ac6454SAndrew Thompson 		if (sqh->next) {
106202ac6454SAndrew Thompson 			sqh->next->prev = sqh->prev;
1063a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(sqh->next->page_cache);
106402ac6454SAndrew Thompson 		}
106502ac6454SAndrew Thompson 		last = ((last == sqh) ? sqh->prev : last);
106602ac6454SAndrew Thompson 
106702ac6454SAndrew Thompson 		sqh->prev = 0;
106802ac6454SAndrew Thompson 
1069a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->page_cache);
107002ac6454SAndrew Thompson 	}
107102ac6454SAndrew Thompson 	return (last);
107202ac6454SAndrew Thompson }
107302ac6454SAndrew Thompson 
1074dbe63d3aSHans Petter Selasky static void
1075dbe63d3aSHans Petter Selasky ehci_data_toggle_update(struct usb_xfer *xfer, uint16_t actlen, uint16_t xlen)
1076dbe63d3aSHans Petter Selasky {
1077ca794e78SHans Petter Selasky 	uint16_t rem;
1078dbe63d3aSHans Petter Selasky 	uint8_t dt;
1079dbe63d3aSHans Petter Selasky 
1080dbe63d3aSHans Petter Selasky 	/* count number of full packets */
1081dbe63d3aSHans Petter Selasky 	dt = (actlen / xfer->max_packet_size) & 1;
1082dbe63d3aSHans Petter Selasky 
1083996c2735SHans Petter Selasky 	/* compute remainder */
1084ca794e78SHans Petter Selasky 	rem = actlen % xfer->max_packet_size;
1085dbe63d3aSHans Petter Selasky 
1086ca794e78SHans Petter Selasky 	if (rem > 0)
1087dbe63d3aSHans Petter Selasky 		dt ^= 1;	/* short packet at the end */
1088ca794e78SHans Petter Selasky 	else if (actlen != xlen)
1089dbe63d3aSHans Petter Selasky 		dt ^= 1;	/* zero length packet at the end */
1090ea719edeSHans Petter Selasky 	else if (xlen == 0)
1091ea719edeSHans Petter Selasky 		dt ^= 1;	/* zero length transfer */
1092dbe63d3aSHans Petter Selasky 
1093dbe63d3aSHans Petter Selasky 	xfer->endpoint->toggle_next ^= dt;
1094dbe63d3aSHans Petter Selasky }
1095dbe63d3aSHans Petter Selasky 
1096e0a69b51SAndrew Thompson static usb_error_t
1097760bc48eSAndrew Thompson ehci_non_isoc_done_sub(struct usb_xfer *xfer)
109802ac6454SAndrew Thompson {
109902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
110002ac6454SAndrew Thompson 	ehci_qtd_t *td;
110102ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
110202ac6454SAndrew Thompson 	uint32_t status;
110302ac6454SAndrew Thompson 	uint16_t len;
110402ac6454SAndrew Thompson 
110502ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
110602ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
110702ac6454SAndrew Thompson 
110802ac6454SAndrew Thompson 	if (xfer->aframes != xfer->nframes) {
1109ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
111002ac6454SAndrew Thompson 	}
111102ac6454SAndrew Thompson 	while (1) {
111202ac6454SAndrew Thompson 
1113a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1114e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->qtd_status);
111502ac6454SAndrew Thompson 
111602ac6454SAndrew Thompson 		len = EHCI_QTD_GET_BYTES(status);
111702ac6454SAndrew Thompson 
111802ac6454SAndrew Thompson 		/*
111902ac6454SAndrew Thompson 	         * Verify the status length and
112002ac6454SAndrew Thompson 		 * add the length to "frlengths[]":
112102ac6454SAndrew Thompson 	         */
112202ac6454SAndrew Thompson 		if (len > td->len) {
112302ac6454SAndrew Thompson 			/* should not happen */
112402ac6454SAndrew Thompson 			DPRINTF("Invalid status length, "
112502ac6454SAndrew Thompson 			    "0x%04x/0x%04x bytes\n", len, td->len);
112602ac6454SAndrew Thompson 			status |= EHCI_QTD_HALTED;
112702ac6454SAndrew Thompson 		} else if (xfer->aframes != xfer->nframes) {
112802ac6454SAndrew Thompson 			xfer->frlengths[xfer->aframes] += td->len - len;
1129dbe63d3aSHans Petter Selasky 			/* manually update data toggle */
1130dbe63d3aSHans Petter Selasky 			ehci_data_toggle_update(xfer, td->len - len, td->len);
113102ac6454SAndrew Thompson 		}
1132dbe63d3aSHans Petter Selasky 
113302ac6454SAndrew Thompson 		/* Check for last transfer */
113402ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
113502ac6454SAndrew Thompson 			td = NULL;
113602ac6454SAndrew Thompson 			break;
113702ac6454SAndrew Thompson 		}
113802ac6454SAndrew Thompson 		/* Check for transfer error */
113902ac6454SAndrew Thompson 		if (status & EHCI_QTD_HALTED) {
114002ac6454SAndrew Thompson 			/* the transfer is finished */
114102ac6454SAndrew Thompson 			td = NULL;
114202ac6454SAndrew Thompson 			break;
114302ac6454SAndrew Thompson 		}
114402ac6454SAndrew Thompson 		/* Check for short transfer */
114502ac6454SAndrew Thompson 		if (len > 0) {
114602ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
114702ac6454SAndrew Thompson 				/* follow alt next */
114802ac6454SAndrew Thompson 				td = td->alt_next;
114902ac6454SAndrew Thompson 			} else {
115002ac6454SAndrew Thompson 				/* the transfer is finished */
115102ac6454SAndrew Thompson 				td = NULL;
115202ac6454SAndrew Thompson 			}
115302ac6454SAndrew Thompson 			break;
115402ac6454SAndrew Thompson 		}
115502ac6454SAndrew Thompson 		td = td->obj_next;
115602ac6454SAndrew Thompson 
115702ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
115802ac6454SAndrew Thompson 			/* this USB frame is complete */
115902ac6454SAndrew Thompson 			break;
116002ac6454SAndrew Thompson 		}
116102ac6454SAndrew Thompson 	}
116202ac6454SAndrew Thompson 
116302ac6454SAndrew Thompson 	/* update transfer cache */
116402ac6454SAndrew Thompson 
116502ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
116602ac6454SAndrew Thompson 
1167b850ecc1SAndrew Thompson #ifdef USB_DEBUG
116802ac6454SAndrew Thompson 	if (status & EHCI_QTD_STATERRS) {
116902ac6454SAndrew Thompson 		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
117002ac6454SAndrew Thompson 		    "status=%s%s%s%s%s%s%s%s\n",
1171ae60fdfbSAndrew Thompson 		    xfer->address, xfer->endpointno, xfer->aframes,
117202ac6454SAndrew Thompson 		    (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
117302ac6454SAndrew Thompson 		    (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
117402ac6454SAndrew Thompson 		    (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
117502ac6454SAndrew Thompson 		    (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
117602ac6454SAndrew Thompson 		    (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
117702ac6454SAndrew Thompson 		    (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
117802ac6454SAndrew Thompson 		    (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
117902ac6454SAndrew Thompson 		    (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
118002ac6454SAndrew Thompson 	}
118102ac6454SAndrew Thompson #endif
1182a148d44fSHans Petter Selasky 	if (status & EHCI_QTD_HALTED) {
1183a148d44fSHans Petter Selasky 		if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
1184a148d44fSHans Petter Selasky 		    (xfer->xroot->udev->address != 0)) {
1185a148d44fSHans Petter Selasky 			/* try to separate I/O errors from STALL */
1186a148d44fSHans Petter Selasky 			if (EHCI_QTD_GET_CERR(status) == 0)
1187a148d44fSHans Petter Selasky 				return (USB_ERR_IOERROR);
1188a148d44fSHans Petter Selasky 		}
1189a148d44fSHans Petter Selasky 		return (USB_ERR_STALLED);
1190a148d44fSHans Petter Selasky 	}
1191a148d44fSHans Petter Selasky 	return (USB_ERR_NORMAL_COMPLETION);
119202ac6454SAndrew Thompson }
119302ac6454SAndrew Thompson 
119402ac6454SAndrew Thompson static void
1195760bc48eSAndrew Thompson ehci_non_isoc_done(struct usb_xfer *xfer)
119602ac6454SAndrew Thompson {
1197c8a14b91SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1198c8a14b91SAndrew Thompson 	ehci_qh_t *qh;
1199c8a14b91SAndrew Thompson 	uint32_t status;
1200e0a69b51SAndrew Thompson 	usb_error_t err = 0;
120102ac6454SAndrew Thompson 
1202ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1203ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
120402ac6454SAndrew Thompson 
1205b850ecc1SAndrew Thompson #ifdef USB_DEBUG
120602ac6454SAndrew Thompson 	if (ehcidebug > 10) {
120702ac6454SAndrew Thompson 		ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 		ehci_dump_sqtds(sc, xfer->td_transfer_first);
121002ac6454SAndrew Thompson 	}
121102ac6454SAndrew Thompson #endif
121202ac6454SAndrew Thompson 
1213c8a14b91SAndrew Thompson 	/* extract data toggle directly from the QH's overlay area */
1214c8a14b91SAndrew Thompson 
1215c8a14b91SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1216c8a14b91SAndrew Thompson 
1217c8a14b91SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
1218c8a14b91SAndrew Thompson 
1219c8a14b91SAndrew Thompson 	status = hc32toh(sc, qh->qh_qtd.qtd_status);
1220c8a14b91SAndrew Thompson 
122102ac6454SAndrew Thompson 	/* reset scanner */
122202ac6454SAndrew Thompson 
122302ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
122402ac6454SAndrew Thompson 
122502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
122602ac6454SAndrew Thompson 
122702ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
122802ac6454SAndrew Thompson 
122902ac6454SAndrew Thompson 			err = ehci_non_isoc_done_sub(xfer);
123002ac6454SAndrew Thompson 		}
123102ac6454SAndrew Thompson 		xfer->aframes = 1;
123202ac6454SAndrew Thompson 
123302ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
123402ac6454SAndrew Thompson 			goto done;
123502ac6454SAndrew Thompson 		}
123602ac6454SAndrew Thompson 	}
123702ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
123802ac6454SAndrew Thompson 
123902ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
124002ac6454SAndrew Thompson 		xfer->aframes++;
124102ac6454SAndrew Thompson 
124202ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
124302ac6454SAndrew Thompson 			goto done;
124402ac6454SAndrew Thompson 		}
124502ac6454SAndrew Thompson 	}
124602ac6454SAndrew Thompson 
124702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
124802ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
124902ac6454SAndrew Thompson 
125002ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
125102ac6454SAndrew Thompson 	}
125202ac6454SAndrew Thompson done:
125302ac6454SAndrew Thompson 	ehci_device_done(xfer, err);
125402ac6454SAndrew Thompson }
125502ac6454SAndrew Thompson 
125602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
125702ac6454SAndrew Thompson  *	ehci_check_transfer
125802ac6454SAndrew Thompson  *
125902ac6454SAndrew Thompson  * Return values:
126002ac6454SAndrew Thompson  *    0: USB transfer is not finished
126102ac6454SAndrew Thompson  * Else: USB transfer is finished
126202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
126302ac6454SAndrew Thompson static uint8_t
1264760bc48eSAndrew Thompson ehci_check_transfer(struct usb_xfer *xfer)
126502ac6454SAndrew Thompson {
1266e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods = xfer->endpoint->methods;
126702ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
126802ac6454SAndrew Thompson 
126902ac6454SAndrew Thompson 	uint32_t status;
127002ac6454SAndrew Thompson 
127102ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
127202ac6454SAndrew Thompson 
127302ac6454SAndrew Thompson 	if (methods == &ehci_device_isoc_fs_methods) {
127402ac6454SAndrew Thompson 		ehci_sitd_t *td;
127502ac6454SAndrew Thompson 
127602ac6454SAndrew Thompson 		/* isochronous full speed transfer */
127702ac6454SAndrew Thompson 
127802ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1279a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1280e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
128102ac6454SAndrew Thompson 
128202ac6454SAndrew Thompson 		/* also check if first is complete */
128302ac6454SAndrew Thompson 
128402ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1285a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1286e55e1ebcSAndrew Thompson 		status |= hc32toh(sc, td->sitd_status);
128702ac6454SAndrew Thompson 
128802ac6454SAndrew Thompson 		if (!(status & EHCI_SITD_ACTIVE)) {
128902ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
129002ac6454SAndrew Thompson 			goto transferred;
129102ac6454SAndrew Thompson 		}
129202ac6454SAndrew Thompson 	} else if (methods == &ehci_device_isoc_hs_methods) {
129302ac6454SAndrew Thompson 		ehci_itd_t *td;
129402ac6454SAndrew Thompson 
129502ac6454SAndrew Thompson 		/* isochronous high speed transfer */
129602ac6454SAndrew Thompson 
129789119752SAndrew Thompson 		/* check last transfer */
129802ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1299a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1300d1f7c4baSAndrew Thompson 		status = td->itd_status[0];
1301d1f7c4baSAndrew Thompson 		status |= td->itd_status[1];
1302d1f7c4baSAndrew Thompson 		status |= td->itd_status[2];
1303d1f7c4baSAndrew Thompson 		status |= td->itd_status[3];
1304d1f7c4baSAndrew Thompson 		status |= td->itd_status[4];
1305d1f7c4baSAndrew Thompson 		status |= td->itd_status[5];
1306d1f7c4baSAndrew Thompson 		status |= td->itd_status[6];
1307d1f7c4baSAndrew Thompson 		status |= td->itd_status[7];
130802ac6454SAndrew Thompson 
130902ac6454SAndrew Thompson 		/* also check first transfer */
131002ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1311a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
131289119752SAndrew Thompson 		status |= td->itd_status[0];
1313d1f7c4baSAndrew Thompson 		status |= td->itd_status[1];
1314d1f7c4baSAndrew Thompson 		status |= td->itd_status[2];
1315d1f7c4baSAndrew Thompson 		status |= td->itd_status[3];
1316d1f7c4baSAndrew Thompson 		status |= td->itd_status[4];
1317d1f7c4baSAndrew Thompson 		status |= td->itd_status[5];
1318d1f7c4baSAndrew Thompson 		status |= td->itd_status[6];
1319d1f7c4baSAndrew Thompson 		status |= td->itd_status[7];
132002ac6454SAndrew Thompson 
132102ac6454SAndrew Thompson 		/* if no transactions are active we continue */
1322e55e1ebcSAndrew Thompson 		if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
132302ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
132402ac6454SAndrew Thompson 			goto transferred;
132502ac6454SAndrew Thompson 		}
132602ac6454SAndrew Thompson 	} else {
132702ac6454SAndrew Thompson 		ehci_qtd_t *td;
1328c8a14b91SAndrew Thompson 		ehci_qh_t *qh;
132902ac6454SAndrew Thompson 
133002ac6454SAndrew Thompson 		/* non-isochronous transfer */
133102ac6454SAndrew Thompson 
133202ac6454SAndrew Thompson 		/*
133302ac6454SAndrew Thompson 		 * check whether there is an error somewhere in the middle,
133402ac6454SAndrew Thompson 		 * or whether there was a short packet (SPD and not ACTIVE)
133502ac6454SAndrew Thompson 		 */
133602ac6454SAndrew Thompson 		td = xfer->td_transfer_cache;
133702ac6454SAndrew Thompson 
1338c8a14b91SAndrew Thompson 		qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1339c8a14b91SAndrew Thompson 
1340c8a14b91SAndrew Thompson 		usb_pc_cpu_invalidate(qh->page_cache);
1341c8a14b91SAndrew Thompson 
1342c8a14b91SAndrew Thompson 		status = hc32toh(sc, qh->qh_qtd.qtd_status);
1343c8a14b91SAndrew Thompson 		if (status & EHCI_QTD_ACTIVE) {
1344c8a14b91SAndrew Thompson 			/* transfer is pending */
1345c8a14b91SAndrew Thompson 			goto done;
1346c8a14b91SAndrew Thompson 		}
1347c8a14b91SAndrew Thompson 
134802ac6454SAndrew Thompson 		while (1) {
1349a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(td->page_cache);
1350e55e1ebcSAndrew Thompson 			status = hc32toh(sc, td->qtd_status);
135102ac6454SAndrew Thompson 
135202ac6454SAndrew Thompson 			/*
1353c8a14b91SAndrew Thompson 			 * Check if there is an active TD which
1354c8a14b91SAndrew Thompson 			 * indicates that the transfer isn't done.
135502ac6454SAndrew Thompson 			 */
135602ac6454SAndrew Thompson 			if (status & EHCI_QTD_ACTIVE) {
135702ac6454SAndrew Thompson 				/* update cache */
135802ac6454SAndrew Thompson 				xfer->td_transfer_cache = td;
135902ac6454SAndrew Thompson 				goto done;
136002ac6454SAndrew Thompson 			}
136102ac6454SAndrew Thompson 			/*
136202ac6454SAndrew Thompson 			 * last transfer descriptor makes the transfer done
136302ac6454SAndrew Thompson 			 */
136402ac6454SAndrew Thompson 			if (((void *)td) == xfer->td_transfer_last) {
136502ac6454SAndrew Thompson 				break;
136602ac6454SAndrew Thompson 			}
136702ac6454SAndrew Thompson 			/*
136802ac6454SAndrew Thompson 			 * any kind of error makes the transfer done
136902ac6454SAndrew Thompson 			 */
137002ac6454SAndrew Thompson 			if (status & EHCI_QTD_HALTED) {
137102ac6454SAndrew Thompson 				break;
137202ac6454SAndrew Thompson 			}
137302ac6454SAndrew Thompson 			/*
137402ac6454SAndrew Thompson 			 * if there is no alternate next transfer, a short
137502ac6454SAndrew Thompson 			 * packet also makes the transfer done
137602ac6454SAndrew Thompson 			 */
137702ac6454SAndrew Thompson 			if (EHCI_QTD_GET_BYTES(status)) {
137802ac6454SAndrew Thompson 				if (xfer->flags_int.short_frames_ok) {
137902ac6454SAndrew Thompson 					/* follow alt next */
138002ac6454SAndrew Thompson 					if (td->alt_next) {
138102ac6454SAndrew Thompson 						td = td->alt_next;
138202ac6454SAndrew Thompson 						continue;
138302ac6454SAndrew Thompson 					}
138402ac6454SAndrew Thompson 				}
138502ac6454SAndrew Thompson 				/* transfer is done */
138602ac6454SAndrew Thompson 				break;
138702ac6454SAndrew Thompson 			}
138802ac6454SAndrew Thompson 			td = td->obj_next;
138902ac6454SAndrew Thompson 		}
139002ac6454SAndrew Thompson 		ehci_non_isoc_done(xfer);
139102ac6454SAndrew Thompson 		goto transferred;
139202ac6454SAndrew Thompson 	}
139302ac6454SAndrew Thompson 
139402ac6454SAndrew Thompson done:
139502ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
139602ac6454SAndrew Thompson 	return (0);
139702ac6454SAndrew Thompson 
139802ac6454SAndrew Thompson transferred:
139902ac6454SAndrew Thompson 	return (1);
140002ac6454SAndrew Thompson }
140102ac6454SAndrew Thompson 
140202ac6454SAndrew Thompson static void
140302ac6454SAndrew Thompson ehci_pcd_enable(ehci_softc_t *sc)
140402ac6454SAndrew Thompson {
140502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
140602ac6454SAndrew Thompson 
140702ac6454SAndrew Thompson 	sc->sc_eintrs |= EHCI_STS_PCD;
140802ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
140902ac6454SAndrew Thompson 
141002ac6454SAndrew Thompson 	/* acknowledge any PCD interrupt */
141102ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
141202ac6454SAndrew Thompson 
141339307315SAndrew Thompson 	ehci_root_intr(sc);
141402ac6454SAndrew Thompson }
141502ac6454SAndrew Thompson 
141602ac6454SAndrew Thompson static void
141702ac6454SAndrew Thompson ehci_interrupt_poll(ehci_softc_t *sc)
141802ac6454SAndrew Thompson {
1419760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
142002ac6454SAndrew Thompson 
142102ac6454SAndrew Thompson repeat:
142202ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
142302ac6454SAndrew Thompson 		/*
142402ac6454SAndrew Thompson 		 * check if transfer is transferred
142502ac6454SAndrew Thompson 		 */
142602ac6454SAndrew Thompson 		if (ehci_check_transfer(xfer)) {
142702ac6454SAndrew Thompson 			/* queue has been modified */
142802ac6454SAndrew Thompson 			goto repeat;
142902ac6454SAndrew Thompson 		}
143002ac6454SAndrew Thompson 	}
143102ac6454SAndrew Thompson }
143202ac6454SAndrew Thompson 
143330b22abeSAndrew Thompson /*
143430b22abeSAndrew Thompson  * Some EHCI chips from VIA / ATI seem to trigger interrupts before
143530b22abeSAndrew Thompson  * writing back the qTD status, or miss signalling occasionally under
143630b22abeSAndrew Thompson  * heavy load.  If the host machine is too fast, we can miss
143730b22abeSAndrew Thompson  * transaction completion - when we scan the active list the
143830b22abeSAndrew Thompson  * transaction still seems to be active. This generally exhibits
143930b22abeSAndrew Thompson  * itself as a umass stall that never recovers.
144030b22abeSAndrew Thompson  *
144130b22abeSAndrew Thompson  * We work around this behaviour by setting up this callback after any
144230b22abeSAndrew Thompson  * softintr that completes with transactions still pending, giving us
144330b22abeSAndrew Thompson  * another chance to check for completion after the writeback has
144430b22abeSAndrew Thompson  * taken place.
144530b22abeSAndrew Thompson  */
144630b22abeSAndrew Thompson static void
144730b22abeSAndrew Thompson ehci_poll_timeout(void *arg)
144830b22abeSAndrew Thompson {
144930b22abeSAndrew Thompson 	ehci_softc_t *sc = arg;
145030b22abeSAndrew Thompson 
145130b22abeSAndrew Thompson 	DPRINTFN(3, "\n");
145230b22abeSAndrew Thompson 	ehci_interrupt_poll(sc);
145330b22abeSAndrew Thompson }
145430b22abeSAndrew Thompson 
145502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
145602ac6454SAndrew Thompson  *	ehci_interrupt - EHCI interrupt handler
145702ac6454SAndrew Thompson  *
145802ac6454SAndrew Thompson  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
145902ac6454SAndrew Thompson  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
146002ac6454SAndrew Thompson  * is present !
146102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
146202ac6454SAndrew Thompson void
146302ac6454SAndrew Thompson ehci_interrupt(ehci_softc_t *sc)
146402ac6454SAndrew Thompson {
146502ac6454SAndrew Thompson 	uint32_t status;
146602ac6454SAndrew Thompson 
146702ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
146802ac6454SAndrew Thompson 
146902ac6454SAndrew Thompson 	DPRINTFN(16, "real interrupt\n");
147002ac6454SAndrew Thompson 
1471b850ecc1SAndrew Thompson #ifdef USB_DEBUG
147202ac6454SAndrew Thompson 	if (ehcidebug > 15) {
147302ac6454SAndrew Thompson 		ehci_dump_regs(sc);
147402ac6454SAndrew Thompson 	}
147502ac6454SAndrew Thompson #endif
147602ac6454SAndrew Thompson 
147702ac6454SAndrew Thompson 	status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
147802ac6454SAndrew Thompson 	if (status == 0) {
147902ac6454SAndrew Thompson 		/* the interrupt was not for us */
148002ac6454SAndrew Thompson 		goto done;
148102ac6454SAndrew Thompson 	}
148202ac6454SAndrew Thompson 	if (!(status & sc->sc_eintrs)) {
148302ac6454SAndrew Thompson 		goto done;
148402ac6454SAndrew Thompson 	}
148502ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, status);	/* acknowledge */
148602ac6454SAndrew Thompson 
148702ac6454SAndrew Thompson 	status &= sc->sc_eintrs;
148802ac6454SAndrew Thompson 
148902ac6454SAndrew Thompson 	if (status & EHCI_STS_HSE) {
149002ac6454SAndrew Thompson 		printf("%s: unrecoverable error, "
149102ac6454SAndrew Thompson 		    "controller halted\n", __FUNCTION__);
1492b850ecc1SAndrew Thompson #ifdef USB_DEBUG
149302ac6454SAndrew Thompson 		ehci_dump_regs(sc);
149402ac6454SAndrew Thompson 		ehci_dump_isoc(sc);
149502ac6454SAndrew Thompson #endif
149602ac6454SAndrew Thompson 	}
149702ac6454SAndrew Thompson 	if (status & EHCI_STS_PCD) {
149802ac6454SAndrew Thompson 		/*
149902ac6454SAndrew Thompson 		 * Disable PCD interrupt for now, because it will be
150002ac6454SAndrew Thompson 		 * on until the port has been reset.
150102ac6454SAndrew Thompson 		 */
150202ac6454SAndrew Thompson 		sc->sc_eintrs &= ~EHCI_STS_PCD;
150302ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
150402ac6454SAndrew Thompson 
150539307315SAndrew Thompson 		ehci_root_intr(sc);
150602ac6454SAndrew Thompson 
150702ac6454SAndrew Thompson 		/* do not allow RHSC interrupts > 1 per second */
1508a593f6b8SAndrew Thompson 		usb_callout_reset(&sc->sc_tmo_pcd, hz,
150902ac6454SAndrew Thompson 		    (void *)&ehci_pcd_enable, sc);
151002ac6454SAndrew Thompson 	}
151102ac6454SAndrew Thompson 	status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
151202ac6454SAndrew Thompson 
151302ac6454SAndrew Thompson 	if (status != 0) {
151402ac6454SAndrew Thompson 		/* block unprocessed interrupts */
151502ac6454SAndrew Thompson 		sc->sc_eintrs &= ~status;
151602ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
151702ac6454SAndrew Thompson 		printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
151802ac6454SAndrew Thompson 	}
151902ac6454SAndrew Thompson 	/* poll all the USB transfers */
152002ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
152102ac6454SAndrew Thompson 
152230b22abeSAndrew Thompson 	if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) {
152330b22abeSAndrew Thompson 		usb_callout_reset(&sc->sc_tmo_poll, hz / 128,
152430b22abeSAndrew Thompson 		    (void *)&ehci_poll_timeout, sc);
152530b22abeSAndrew Thompson 	}
152630b22abeSAndrew Thompson 
152702ac6454SAndrew Thompson done:
152802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
152902ac6454SAndrew Thompson }
153002ac6454SAndrew Thompson 
153102ac6454SAndrew Thompson /*
153202ac6454SAndrew Thompson  * called when a request does not complete
153302ac6454SAndrew Thompson  */
153402ac6454SAndrew Thompson static void
153502ac6454SAndrew Thompson ehci_timeout(void *arg)
153602ac6454SAndrew Thompson {
1537760bc48eSAndrew Thompson 	struct usb_xfer *xfer = arg;
153802ac6454SAndrew Thompson 
153902ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
154002ac6454SAndrew Thompson 
154102ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
154202ac6454SAndrew Thompson 
154302ac6454SAndrew Thompson 	/* transfer is transferred */
154402ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_TIMEOUT);
154502ac6454SAndrew Thompson }
154602ac6454SAndrew Thompson 
154702ac6454SAndrew Thompson static void
1548760bc48eSAndrew Thompson ehci_do_poll(struct usb_bus *bus)
154902ac6454SAndrew Thompson {
155002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
155102ac6454SAndrew Thompson 
155202ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
155302ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
155402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
155502ac6454SAndrew Thompson }
155602ac6454SAndrew Thompson 
155702ac6454SAndrew Thompson static void
155802ac6454SAndrew Thompson ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
155902ac6454SAndrew Thompson {
1560760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
156102ac6454SAndrew Thompson 	ehci_qtd_t *td;
156202ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
156302ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
156402ac6454SAndrew Thompson 	uint32_t buf_offset;
156502ac6454SAndrew Thompson 	uint32_t average;
156602ac6454SAndrew Thompson 	uint32_t len_old;
15670f7d4548SAndrew Thompson 	uint32_t terminate;
156853e0bf6eSHans Petter Selasky 	uint32_t qtd_altnext;
156902ac6454SAndrew Thompson 	uint8_t shortpkt_old;
157002ac6454SAndrew Thompson 	uint8_t precompute;
157102ac6454SAndrew Thompson 
157253e0bf6eSHans Petter Selasky 	terminate = temp->sc->sc_terminate_self;
157353e0bf6eSHans Petter Selasky 	qtd_altnext = temp->sc->sc_terminate_self;
157402ac6454SAndrew Thompson 	td_alt_next = NULL;
157502ac6454SAndrew Thompson 	buf_offset = 0;
157602ac6454SAndrew Thompson 	shortpkt_old = temp->shortpkt;
157702ac6454SAndrew Thompson 	len_old = temp->len;
157802ac6454SAndrew Thompson 	precompute = 1;
157902ac6454SAndrew Thompson 
158002ac6454SAndrew Thompson restart:
158102ac6454SAndrew Thompson 
158202ac6454SAndrew Thompson 	td = temp->td;
158302ac6454SAndrew Thompson 	td_next = temp->td_next;
158402ac6454SAndrew Thompson 
158502ac6454SAndrew Thompson 	while (1) {
158602ac6454SAndrew Thompson 
158702ac6454SAndrew Thompson 		if (temp->len == 0) {
158802ac6454SAndrew Thompson 
158902ac6454SAndrew Thompson 			if (temp->shortpkt) {
159002ac6454SAndrew Thompson 				break;
159102ac6454SAndrew Thompson 			}
159202ac6454SAndrew Thompson 			/* send a Zero Length Packet, ZLP, last */
159302ac6454SAndrew Thompson 
159402ac6454SAndrew Thompson 			temp->shortpkt = 1;
159502ac6454SAndrew Thompson 			average = 0;
159602ac6454SAndrew Thompson 
159702ac6454SAndrew Thompson 		} else {
159802ac6454SAndrew Thompson 
159902ac6454SAndrew Thompson 			average = temp->average;
160002ac6454SAndrew Thompson 
160102ac6454SAndrew Thompson 			if (temp->len < average) {
160202ac6454SAndrew Thompson 				if (temp->len % temp->max_frame_size) {
160302ac6454SAndrew Thompson 					temp->shortpkt = 1;
160402ac6454SAndrew Thompson 				}
160502ac6454SAndrew Thompson 				average = temp->len;
160602ac6454SAndrew Thompson 			}
160702ac6454SAndrew Thompson 		}
160802ac6454SAndrew Thompson 
160902ac6454SAndrew Thompson 		if (td_next == NULL) {
161002ac6454SAndrew Thompson 			panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
161102ac6454SAndrew Thompson 		}
161202ac6454SAndrew Thompson 		/* get next TD */
161302ac6454SAndrew Thompson 
161402ac6454SAndrew Thompson 		td = td_next;
161502ac6454SAndrew Thompson 		td_next = td->obj_next;
161602ac6454SAndrew Thompson 
161702ac6454SAndrew Thompson 		/* check if we are pre-computing */
161802ac6454SAndrew Thompson 
161902ac6454SAndrew Thompson 		if (precompute) {
162002ac6454SAndrew Thompson 
162102ac6454SAndrew Thompson 			/* update remaining length */
162202ac6454SAndrew Thompson 
162302ac6454SAndrew Thompson 			temp->len -= average;
162402ac6454SAndrew Thompson 
162502ac6454SAndrew Thompson 			continue;
162602ac6454SAndrew Thompson 		}
162702ac6454SAndrew Thompson 		/* fill out current TD */
162802ac6454SAndrew Thompson 
162902ac6454SAndrew Thompson 		td->qtd_status =
163002ac6454SAndrew Thompson 		    temp->qtd_status |
1631c8a14b91SAndrew Thompson 		    htohc32(temp->sc, EHCI_QTD_IOC |
1632c8a14b91SAndrew Thompson 			EHCI_QTD_SET_BYTES(average));
163302ac6454SAndrew Thompson 
163402ac6454SAndrew Thompson 		if (average == 0) {
163502ac6454SAndrew Thompson 
163602ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
163702ac6454SAndrew Thompson 
163802ac6454SAndrew Thompson 				/* update data toggle, ZLP case */
163902ac6454SAndrew Thompson 
164002ac6454SAndrew Thompson 				temp->qtd_status ^=
1641e55e1ebcSAndrew Thompson 				    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
164202ac6454SAndrew Thompson 			}
164302ac6454SAndrew Thompson 			td->len = 0;
164402ac6454SAndrew Thompson 
16459ae7f7c1SHans Petter Selasky 			/* properly reset reserved fields */
164602ac6454SAndrew Thompson 			td->qtd_buffer[0] = 0;
164702ac6454SAndrew Thompson 			td->qtd_buffer[1] = 0;
16489ae7f7c1SHans Petter Selasky 			td->qtd_buffer[2] = 0;
16499ae7f7c1SHans Petter Selasky 			td->qtd_buffer[3] = 0;
16509ae7f7c1SHans Petter Selasky 			td->qtd_buffer[4] = 0;
16519ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[0] = 0;
165202ac6454SAndrew Thompson 			td->qtd_buffer_hi[1] = 0;
16539ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[2] = 0;
16549ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[3] = 0;
16559ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[4] = 0;
165602ac6454SAndrew Thompson 		} else {
165702ac6454SAndrew Thompson 
165802ac6454SAndrew Thompson 			uint8_t x;
165902ac6454SAndrew Thompson 
166002ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
166102ac6454SAndrew Thompson 
166202ac6454SAndrew Thompson 				/* update data toggle */
166302ac6454SAndrew Thompson 
1664057b4402SPedro F. Giffuni 				if (howmany(average, temp->max_frame_size) & 1) {
166502ac6454SAndrew Thompson 					temp->qtd_status ^=
1666e55e1ebcSAndrew Thompson 					    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
166702ac6454SAndrew Thompson 				}
166802ac6454SAndrew Thompson 			}
166902ac6454SAndrew Thompson 			td->len = average;
167002ac6454SAndrew Thompson 
167102ac6454SAndrew Thompson 			/* update remaining length */
167202ac6454SAndrew Thompson 
167302ac6454SAndrew Thompson 			temp->len -= average;
167402ac6454SAndrew Thompson 
167502ac6454SAndrew Thompson 			/* fill out buffer pointers */
167602ac6454SAndrew Thompson 
1677a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset, &buf_res);
167802ac6454SAndrew Thompson 			td->qtd_buffer[0] =
1679e55e1ebcSAndrew Thompson 			    htohc32(temp->sc, buf_res.physaddr);
168002ac6454SAndrew Thompson 			td->qtd_buffer_hi[0] = 0;
168102ac6454SAndrew Thompson 
168202ac6454SAndrew Thompson 			x = 1;
168302ac6454SAndrew Thompson 
168402ac6454SAndrew Thompson 			while (average > EHCI_PAGE_SIZE) {
168502ac6454SAndrew Thompson 				average -= EHCI_PAGE_SIZE;
168602ac6454SAndrew Thompson 				buf_offset += EHCI_PAGE_SIZE;
1687a593f6b8SAndrew Thompson 				usbd_get_page(temp->pc, buf_offset, &buf_res);
168802ac6454SAndrew Thompson 				td->qtd_buffer[x] =
1689e55e1ebcSAndrew Thompson 				    htohc32(temp->sc,
169002ac6454SAndrew Thompson 				    buf_res.physaddr & (~0xFFF));
169102ac6454SAndrew Thompson 				td->qtd_buffer_hi[x] = 0;
169202ac6454SAndrew Thompson 				x++;
169302ac6454SAndrew Thompson 			}
169402ac6454SAndrew Thompson 
169502ac6454SAndrew Thompson 			/*
169602ac6454SAndrew Thompson 			 * NOTE: The "average" variable is never zero after
169702ac6454SAndrew Thompson 			 * exiting the loop above !
169802ac6454SAndrew Thompson 			 *
169902ac6454SAndrew Thompson 			 * NOTE: We have to subtract one from the offset to
170002ac6454SAndrew Thompson 			 * ensure that we are computing the physical address
170102ac6454SAndrew Thompson 			 * of a valid page !
170202ac6454SAndrew Thompson 			 */
170302ac6454SAndrew Thompson 			buf_offset += average;
1704a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
170502ac6454SAndrew Thompson 			td->qtd_buffer[x] =
1706e55e1ebcSAndrew Thompson 			    htohc32(temp->sc,
170702ac6454SAndrew Thompson 			    buf_res.physaddr & (~0xFFF));
170802ac6454SAndrew Thompson 			td->qtd_buffer_hi[x] = 0;
17099ae7f7c1SHans Petter Selasky 
17109ae7f7c1SHans Petter Selasky 			/* properly reset reserved fields */
17119ae7f7c1SHans Petter Selasky 			while (++x < EHCI_QTD_NBUFFERS) {
17129ae7f7c1SHans Petter Selasky 				td->qtd_buffer[x] = 0;
17139ae7f7c1SHans Petter Selasky 				td->qtd_buffer_hi[x] = 0;
17149ae7f7c1SHans Petter Selasky 			}
171502ac6454SAndrew Thompson 		}
171602ac6454SAndrew Thompson 
171702ac6454SAndrew Thompson 		if (td_next) {
171802ac6454SAndrew Thompson 			/* link the current TD with the next one */
171902ac6454SAndrew Thompson 			td->qtd_next = td_next->qtd_self;
172002ac6454SAndrew Thompson 		}
172153e0bf6eSHans Petter Selasky 		td->qtd_altnext = qtd_altnext;
172202ac6454SAndrew Thompson 		td->alt_next = td_alt_next;
172302ac6454SAndrew Thompson 
1724a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
172502ac6454SAndrew Thompson 	}
172602ac6454SAndrew Thompson 
172702ac6454SAndrew Thompson 	if (precompute) {
172802ac6454SAndrew Thompson 		precompute = 0;
172902ac6454SAndrew Thompson 
173002ac6454SAndrew Thompson 		/* setup alt next pointer, if any */
17310f7d4548SAndrew Thompson 		if (temp->last_frame) {
17320f7d4548SAndrew Thompson 			td_alt_next = NULL;
173353e0bf6eSHans Petter Selasky 			qtd_altnext = terminate;
173402ac6454SAndrew Thompson 		} else {
173502ac6454SAndrew Thompson 			/* we use this field internally */
173602ac6454SAndrew Thompson 			td_alt_next = td_next;
173753e0bf6eSHans Petter Selasky 			if (temp->setup_alt_next) {
173853e0bf6eSHans Petter Selasky 				qtd_altnext = td_next->qtd_self;
173953e0bf6eSHans Petter Selasky 			} else {
174053e0bf6eSHans Petter Selasky 				qtd_altnext = terminate;
174153e0bf6eSHans Petter Selasky 			}
174202ac6454SAndrew Thompson 		}
174302ac6454SAndrew Thompson 
174402ac6454SAndrew Thompson 		/* restore */
174502ac6454SAndrew Thompson 		temp->shortpkt = shortpkt_old;
174602ac6454SAndrew Thompson 		temp->len = len_old;
174702ac6454SAndrew Thompson 		goto restart;
174802ac6454SAndrew Thompson 	}
174902ac6454SAndrew Thompson 	temp->td = td;
175002ac6454SAndrew Thompson 	temp->td_next = td_next;
175102ac6454SAndrew Thompson }
175202ac6454SAndrew Thompson 
175302ac6454SAndrew Thompson static void
1754760bc48eSAndrew Thompson ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
175502ac6454SAndrew Thompson {
175602ac6454SAndrew Thompson 	struct ehci_std_temp temp;
1757e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
175802ac6454SAndrew Thompson 	ehci_qh_t *qh;
175902ac6454SAndrew Thompson 	ehci_qtd_t *td;
176002ac6454SAndrew Thompson 	uint32_t qh_endp;
176102ac6454SAndrew Thompson 	uint32_t qh_endphub;
176202ac6454SAndrew Thompson 	uint32_t x;
176302ac6454SAndrew Thompson 
176402ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1765ae60fdfbSAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
1766a593f6b8SAndrew Thompson 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
176702ac6454SAndrew Thompson 
1768578d0effSAndrew Thompson 	temp.average = xfer->max_hc_frame_size;
176902ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
177002ac6454SAndrew Thompson 	temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
177102ac6454SAndrew Thompson 
177202ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
177302ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
177402ac6454SAndrew Thompson 
177502ac6454SAndrew Thompson 	/* get next DMA set */
177602ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
177702ac6454SAndrew Thompson 
177802ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
177902ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
178002ac6454SAndrew Thompson 
178102ac6454SAndrew Thompson 	temp.td = NULL;
178202ac6454SAndrew Thompson 	temp.td_next = td;
178302ac6454SAndrew Thompson 	temp.qtd_status = 0;
17840f7d4548SAndrew Thompson 	temp.last_frame = 0;
178502ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
178602ac6454SAndrew Thompson 
178702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
1788ae60fdfbSAndrew Thompson 		if (xfer->endpoint->toggle_next) {
178902ac6454SAndrew Thompson 			/* DATA1 is next */
179002ac6454SAndrew Thompson 			temp.qtd_status |=
1791e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
179202ac6454SAndrew Thompson 		}
179302ac6454SAndrew Thompson 		temp.auto_data_toggle = 0;
179402ac6454SAndrew Thompson 	} else {
179502ac6454SAndrew Thompson 		temp.auto_data_toggle = 1;
179602ac6454SAndrew Thompson 	}
179702ac6454SAndrew Thompson 
179853e0bf6eSHans Petter Selasky 	if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
179953e0bf6eSHans Petter Selasky 	    (xfer->xroot->udev->address != 0)) {
180002ac6454SAndrew Thompson 		/* max 3 retries */
180102ac6454SAndrew Thompson 		temp.qtd_status |=
1802e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
180302ac6454SAndrew Thompson 	}
180402ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
180502ac6454SAndrew Thompson 
180602ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
180702ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
180802ac6454SAndrew Thompson 
1809dbe63d3aSHans Petter Selasky 			xfer->endpoint->toggle_next = 0;
1810dbe63d3aSHans Petter Selasky 
181102ac6454SAndrew Thompson 			temp.qtd_status &=
1812e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1813e55e1ebcSAndrew Thompson 			temp.qtd_status |= htohc32(temp.sc,
18145f128668SAndrew Thompson 			    EHCI_QTD_ACTIVE |
181502ac6454SAndrew Thompson 			    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
181602ac6454SAndrew Thompson 			    EHCI_QTD_SET_TOGGLE(0));
181702ac6454SAndrew Thompson 
181802ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
181902ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + 0;
182002ac6454SAndrew Thompson 			temp.shortpkt = temp.len ? 1 : 0;
18210f7d4548SAndrew Thompson 			/* check for last frame */
18220f7d4548SAndrew Thompson 			if (xfer->nframes == 1) {
18230f7d4548SAndrew Thompson 				/* no STATUS stage yet, SETUP is last */
18240f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
18250f7d4548SAndrew Thompson 					temp.last_frame = 1;
18260f7d4548SAndrew Thompson 					temp.setup_alt_next = 0;
18270f7d4548SAndrew Thompson 				}
18280f7d4548SAndrew Thompson 			}
182902ac6454SAndrew Thompson 			ehci_setup_standard_chain_sub(&temp);
183002ac6454SAndrew Thompson 		}
183102ac6454SAndrew Thompson 		x = 1;
183202ac6454SAndrew Thompson 	} else {
183302ac6454SAndrew Thompson 		x = 0;
183402ac6454SAndrew Thompson 	}
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson 	while (x != xfer->nframes) {
183702ac6454SAndrew Thompson 
183802ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
183902ac6454SAndrew Thompson 
184002ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
184102ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
184202ac6454SAndrew Thompson 
184302ac6454SAndrew Thompson 		x++;
184402ac6454SAndrew Thompson 
184502ac6454SAndrew Thompson 		if (x == xfer->nframes) {
18460f7d4548SAndrew Thompson 			if (xfer->flags_int.control_xfr) {
18470f7d4548SAndrew Thompson 				/* no STATUS stage yet, DATA is last */
18480f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
18490f7d4548SAndrew Thompson 					temp.last_frame = 1;
185002ac6454SAndrew Thompson 					temp.setup_alt_next = 0;
185102ac6454SAndrew Thompson 				}
18520f7d4548SAndrew Thompson 			} else {
18530f7d4548SAndrew Thompson 				temp.last_frame = 1;
18540f7d4548SAndrew Thompson 				temp.setup_alt_next = 0;
18550f7d4548SAndrew Thompson 			}
18560f7d4548SAndrew Thompson 		}
185702ac6454SAndrew Thompson 		/* keep previous data toggle and error count */
185802ac6454SAndrew Thompson 
185902ac6454SAndrew Thompson 		temp.qtd_status &=
1860e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
186102ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
186202ac6454SAndrew Thompson 
186302ac6454SAndrew Thompson 		if (temp.len == 0) {
186402ac6454SAndrew Thompson 
186502ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
186602ac6454SAndrew Thompson 
186702ac6454SAndrew Thompson 			temp.shortpkt = 0;
186802ac6454SAndrew Thompson 
186902ac6454SAndrew Thompson 		} else {
187002ac6454SAndrew Thompson 
187102ac6454SAndrew Thompson 			/* regular data transfer */
187202ac6454SAndrew Thompson 
187302ac6454SAndrew Thompson 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
187402ac6454SAndrew Thompson 		}
187502ac6454SAndrew Thompson 
187602ac6454SAndrew Thompson 		/* set endpoint direction */
187702ac6454SAndrew Thompson 
187802ac6454SAndrew Thompson 		temp.qtd_status |=
1879ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1880e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
188102ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1882e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
188302ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
188402ac6454SAndrew Thompson 
188502ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
188602ac6454SAndrew Thompson 	}
188702ac6454SAndrew Thompson 
188802ac6454SAndrew Thompson 	/* check if we should append a status stage */
188902ac6454SAndrew Thompson 
189002ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
189102ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
189202ac6454SAndrew Thompson 
189302ac6454SAndrew Thompson 		/*
189402ac6454SAndrew Thompson 		 * Send a DATA1 message and invert the current endpoint
189502ac6454SAndrew Thompson 		 * direction.
189602ac6454SAndrew Thompson 		 */
189702ac6454SAndrew Thompson 
1898e55e1ebcSAndrew Thompson 		temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
189902ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
190002ac6454SAndrew Thompson 		temp.qtd_status |=
1901ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1902e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
190302ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
190402ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1)) :
1905e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
190602ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
190702ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
190802ac6454SAndrew Thompson 
190902ac6454SAndrew Thompson 		temp.len = 0;
191002ac6454SAndrew Thompson 		temp.pc = NULL;
191102ac6454SAndrew Thompson 		temp.shortpkt = 0;
19120f7d4548SAndrew Thompson 		temp.last_frame = 1;
19130f7d4548SAndrew Thompson 		temp.setup_alt_next = 0;
191402ac6454SAndrew Thompson 
191502ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
191602ac6454SAndrew Thompson 	}
191702ac6454SAndrew Thompson 	td = temp.td;
191802ac6454SAndrew Thompson 
191902ac6454SAndrew Thompson 	/* the last TD terminates the transfer: */
1920e55e1ebcSAndrew Thompson 	td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1921e55e1ebcSAndrew Thompson 	td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
192202ac6454SAndrew Thompson 
1923a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(td->page_cache);
192402ac6454SAndrew Thompson 
192502ac6454SAndrew Thompson 	/* must have at least one frame! */
192602ac6454SAndrew Thompson 
192702ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
192802ac6454SAndrew Thompson 
1929b850ecc1SAndrew Thompson #ifdef USB_DEBUG
193002ac6454SAndrew Thompson 	if (ehcidebug > 8) {
193102ac6454SAndrew Thompson 		DPRINTF("nexttog=%d; data before transfer:\n",
1932ae60fdfbSAndrew Thompson 		    xfer->endpoint->toggle_next);
193302ac6454SAndrew Thompson 		ehci_dump_sqtds(temp.sc,
193402ac6454SAndrew Thompson 		    xfer->td_transfer_first);
193502ac6454SAndrew Thompson 	}
193602ac6454SAndrew Thompson #endif
193702ac6454SAndrew Thompson 
1938ae60fdfbSAndrew Thompson 	methods = xfer->endpoint->methods;
193902ac6454SAndrew Thompson 
194002ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
194102ac6454SAndrew Thompson 
194202ac6454SAndrew Thompson 	/* the "qh_link" field is filled when the QH is added */
194302ac6454SAndrew Thompson 
194402ac6454SAndrew Thompson 	qh_endp =
194502ac6454SAndrew Thompson 	    (EHCI_QH_SET_ADDR(xfer->address) |
1946ae60fdfbSAndrew Thompson 	    EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
194702ac6454SAndrew Thompson 	    EHCI_QH_SET_MPL(xfer->max_packet_size));
194802ac6454SAndrew Thompson 
1949a593f6b8SAndrew Thompson 	if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
19502094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
195102ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods)
195202ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(8);
195302ac6454SAndrew Thompson 	} else {
195402ac6454SAndrew Thompson 
1955a593f6b8SAndrew Thompson 		if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
19562094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
195702ac6454SAndrew Thompson 		} else {
19582094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
195902ac6454SAndrew Thompson 		}
196002ac6454SAndrew Thompson 
196102ac6454SAndrew Thompson 		if (methods == &ehci_device_ctrl_methods) {
196202ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_CTL;
196302ac6454SAndrew Thompson 		}
196402ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods) {
196502ac6454SAndrew Thompson 			/* Only try one time per microframe! */
196602ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(1);
196702ac6454SAndrew Thompson 		}
196802ac6454SAndrew Thompson 	}
196902ac6454SAndrew Thompson 
19702094ecdaSAndrew Thompson 	if (temp.auto_data_toggle == 0) {
19712094ecdaSAndrew Thompson 		/* software computes the data toggle */
19722094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_DTC;
19732094ecdaSAndrew Thompson 	}
19742094ecdaSAndrew Thompson 
1975e55e1ebcSAndrew Thompson 	qh->qh_endp = htohc32(temp.sc, qh_endp);
197602ac6454SAndrew Thompson 
197702ac6454SAndrew Thompson 	qh_endphub =
197802ac6454SAndrew Thompson 	    (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
1979f12c6c29SAndrew Thompson 	    EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) |
1980f12c6c29SAndrew Thompson 	    EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) |
198102ac6454SAndrew Thompson 	    EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
198202ac6454SAndrew Thompson 	    EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
198302ac6454SAndrew Thompson 
1984e55e1ebcSAndrew Thompson 	qh->qh_endphub = htohc32(temp.sc, qh_endphub);
19852094ecdaSAndrew Thompson 	qh->qh_curqtd = 0;
198602ac6454SAndrew Thompson 
198702ac6454SAndrew Thompson 	/* fill the overlay qTD */
198802ac6454SAndrew Thompson 
19892094ecdaSAndrew Thompson 	if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
199002ac6454SAndrew Thompson 		/* DATA1 is next */
19912094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
19922094ecdaSAndrew Thompson 	} else {
19932094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = 0;
199402ac6454SAndrew Thompson 	}
19952094ecdaSAndrew Thompson 
199602ac6454SAndrew Thompson 	td = xfer->td_transfer_first;
199702ac6454SAndrew Thompson 
199802ac6454SAndrew Thompson 	qh->qh_qtd.qtd_next = td->qtd_self;
199902ac6454SAndrew Thompson 	qh->qh_qtd.qtd_altnext =
2000e55e1ebcSAndrew Thompson 	    htohc32(temp.sc, EHCI_LINK_TERMINATE);
200102ac6454SAndrew Thompson 
20029ae7f7c1SHans Petter Selasky 	/* properly reset reserved fields */
20039ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[0] = 0;
20049ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[1] = 0;
20059ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[2] = 0;
20069ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[3] = 0;
20079ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[4] = 0;
20089ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[0] = 0;
20099ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[1] = 0;
20109ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[2] = 0;
20119ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[3] = 0;
20129ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[4] = 0;
20139ae7f7c1SHans Petter Selasky 
2014a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(qh->page_cache);
201502ac6454SAndrew Thompson 
2016ec8f3127SAndrew Thompson 	if (xfer->xroot->udev->flags.self_suspended == 0) {
201702ac6454SAndrew Thompson 		EHCI_APPEND_QH(qh, *qh_last);
201802ac6454SAndrew Thompson 	}
201902ac6454SAndrew Thompson }
202002ac6454SAndrew Thompson 
202102ac6454SAndrew Thompson static void
202239307315SAndrew Thompson ehci_root_intr(ehci_softc_t *sc)
202302ac6454SAndrew Thompson {
202402ac6454SAndrew Thompson 	uint16_t i;
202502ac6454SAndrew Thompson 	uint16_t m;
202602ac6454SAndrew Thompson 
202702ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
202802ac6454SAndrew Thompson 
202902ac6454SAndrew Thompson 	/* clear any old interrupt data */
203039307315SAndrew Thompson 	memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
203102ac6454SAndrew Thompson 
203202ac6454SAndrew Thompson 	/* set bits */
203302ac6454SAndrew Thompson 	m = (sc->sc_noport + 1);
203402ac6454SAndrew Thompson 	if (m > (8 * sizeof(sc->sc_hub_idata))) {
203502ac6454SAndrew Thompson 		m = (8 * sizeof(sc->sc_hub_idata));
203602ac6454SAndrew Thompson 	}
203702ac6454SAndrew Thompson 	for (i = 1; i < m; i++) {
203802ac6454SAndrew Thompson 		/* pick out CHANGE bits from the status register */
203902ac6454SAndrew Thompson 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
204002ac6454SAndrew Thompson 			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
204102ac6454SAndrew Thompson 			DPRINTF("port %d changed\n", i);
204202ac6454SAndrew Thompson 		}
204302ac6454SAndrew Thompson 	}
204439307315SAndrew Thompson 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
204539307315SAndrew Thompson 	    sizeof(sc->sc_hub_idata));
204602ac6454SAndrew Thompson }
204702ac6454SAndrew Thompson 
204802ac6454SAndrew Thompson static void
2049760bc48eSAndrew Thompson ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
205002ac6454SAndrew Thompson {
205102ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
205202ac6454SAndrew Thompson 	uint32_t status;
205302ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
205402ac6454SAndrew Thompson 	uint16_t len = 0;
205502ac6454SAndrew Thompson 	ehci_sitd_t *td = xfer->td_transfer_first;
205602ac6454SAndrew Thompson 	ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
205702ac6454SAndrew Thompson 
2058ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2059ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
206002ac6454SAndrew Thompson 
206102ac6454SAndrew Thompson 	while (nframes--) {
206202ac6454SAndrew Thompson 		if (td == NULL) {
206302ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
206402ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
206502ac6454SAndrew Thompson 		}
206602ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
206702ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
206802ac6454SAndrew Thompson 		}
2069b850ecc1SAndrew Thompson #ifdef USB_DEBUG
207002ac6454SAndrew Thompson 		if (ehcidebug > 15) {
207102ac6454SAndrew Thompson 			DPRINTF("isoc FS-TD\n");
207202ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
207302ac6454SAndrew Thompson 		}
207402ac6454SAndrew Thompson #endif
2075a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2076e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
207702ac6454SAndrew Thompson 
207802ac6454SAndrew Thompson 		len = EHCI_SITD_GET_LEN(status);
207902ac6454SAndrew Thompson 
2080c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2081c773c254SAndrew Thompson 
208202ac6454SAndrew Thompson 		if (*plen >= len) {
208302ac6454SAndrew Thompson 			len = *plen - len;
208402ac6454SAndrew Thompson 		} else {
208502ac6454SAndrew Thompson 			len = 0;
208602ac6454SAndrew Thompson 		}
208702ac6454SAndrew Thompson 
208802ac6454SAndrew Thompson 		*plen = len;
208902ac6454SAndrew Thompson 
209002ac6454SAndrew Thompson 		/* remove FS-TD from schedule */
209102ac6454SAndrew Thompson 		EHCI_REMOVE_FS_TD(td, *pp_last);
209202ac6454SAndrew Thompson 
209302ac6454SAndrew Thompson 		pp_last++;
209402ac6454SAndrew Thompson 		plen++;
209502ac6454SAndrew Thompson 		td = td->obj_next;
209602ac6454SAndrew Thompson 	}
209702ac6454SAndrew Thompson 
209802ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
209902ac6454SAndrew Thompson }
210002ac6454SAndrew Thompson 
210102ac6454SAndrew Thompson static void
2102760bc48eSAndrew Thompson ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
210302ac6454SAndrew Thompson {
210402ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
210502ac6454SAndrew Thompson 	uint32_t status;
210602ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
210702ac6454SAndrew Thompson 	uint16_t len = 0;
210802ac6454SAndrew Thompson 	uint8_t td_no = 0;
210902ac6454SAndrew Thompson 	ehci_itd_t *td = xfer->td_transfer_first;
211002ac6454SAndrew Thompson 	ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
211102ac6454SAndrew Thompson 
2112ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2113ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
211402ac6454SAndrew Thompson 
2115883bb300SAndrew Thompson 	while (nframes) {
211602ac6454SAndrew Thompson 		if (td == NULL) {
211702ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
211802ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
211902ac6454SAndrew Thompson 		}
212002ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
212102ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
212202ac6454SAndrew Thompson 		}
2123b850ecc1SAndrew Thompson #ifdef USB_DEBUG
212402ac6454SAndrew Thompson 		if (ehcidebug > 15) {
212502ac6454SAndrew Thompson 			DPRINTF("isoc HS-TD\n");
212602ac6454SAndrew Thompson 			ehci_dump_itd(sc, td);
212702ac6454SAndrew Thompson 		}
212802ac6454SAndrew Thompson #endif
212902ac6454SAndrew Thompson 
2130a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2131e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->itd_status[td_no]);
213202ac6454SAndrew Thompson 
213302ac6454SAndrew Thompson 		len = EHCI_ITD_GET_LEN(status);
213402ac6454SAndrew Thompson 
2135c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2136c773c254SAndrew Thompson 
2137f12c6c29SAndrew Thompson 		if (xfer->endpoint->usb_smask & (1 << td_no)) {
2138883bb300SAndrew Thompson 
213902ac6454SAndrew Thompson 			if (*plen >= len) {
214002ac6454SAndrew Thompson 				/*
2141883bb300SAndrew Thompson 				 * The length is valid. NOTE: The
2142883bb300SAndrew Thompson 				 * complete length is written back
2143883bb300SAndrew Thompson 				 * into the status field, and not the
2144883bb300SAndrew Thompson 				 * remainder like with other transfer
2145883bb300SAndrew Thompson 				 * descriptor types.
214602ac6454SAndrew Thompson 				 */
214702ac6454SAndrew Thompson 			} else {
214802ac6454SAndrew Thompson 				/* Invalid length - truncate */
214902ac6454SAndrew Thompson 				len = 0;
215002ac6454SAndrew Thompson 			}
215102ac6454SAndrew Thompson 
215202ac6454SAndrew Thompson 			*plen = len;
215302ac6454SAndrew Thompson 			plen++;
2154883bb300SAndrew Thompson 			nframes--;
2155883bb300SAndrew Thompson 		}
2156883bb300SAndrew Thompson 
215702ac6454SAndrew Thompson 		td_no++;
215802ac6454SAndrew Thompson 
215902ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
216002ac6454SAndrew Thompson 			/* remove HS-TD from schedule */
216102ac6454SAndrew Thompson 			EHCI_REMOVE_HS_TD(td, *pp_last);
216202ac6454SAndrew Thompson 			pp_last++;
216302ac6454SAndrew Thompson 
216402ac6454SAndrew Thompson 			td_no = 0;
216502ac6454SAndrew Thompson 			td = td->obj_next;
216602ac6454SAndrew Thompson 		}
216702ac6454SAndrew Thompson 	}
216802ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
216902ac6454SAndrew Thompson }
217002ac6454SAndrew Thompson 
217102ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row,
217202ac6454SAndrew Thompson  * from close and from interrupt
217302ac6454SAndrew Thompson  */
217402ac6454SAndrew Thompson static void
2175e0a69b51SAndrew Thompson ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
217602ac6454SAndrew Thompson {
2177e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods = xfer->endpoint->methods;
217802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
217902ac6454SAndrew Thompson 
218002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
218102ac6454SAndrew Thompson 
2182ae60fdfbSAndrew Thompson 	DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2183ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint, error);
218402ac6454SAndrew Thompson 
218502ac6454SAndrew Thompson 	if ((methods == &ehci_device_bulk_methods) ||
218602ac6454SAndrew Thompson 	    (methods == &ehci_device_ctrl_methods)) {
2187b850ecc1SAndrew Thompson #ifdef USB_DEBUG
218802ac6454SAndrew Thompson 		if (ehcidebug > 8) {
218902ac6454SAndrew Thompson 			DPRINTF("nexttog=%d; data after transfer:\n",
2190ae60fdfbSAndrew Thompson 			    xfer->endpoint->toggle_next);
219102ac6454SAndrew Thompson 			ehci_dump_sqtds(sc,
219202ac6454SAndrew Thompson 			    xfer->td_transfer_first);
219302ac6454SAndrew Thompson 		}
219402ac6454SAndrew Thompson #endif
219502ac6454SAndrew Thompson 
219602ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
219702ac6454SAndrew Thompson 		    sc->sc_async_p_last);
219802ac6454SAndrew Thompson 	}
219902ac6454SAndrew Thompson 	if (methods == &ehci_device_intr_methods) {
220002ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
220102ac6454SAndrew Thompson 		    sc->sc_intr_p_last[xfer->qh_pos]);
220202ac6454SAndrew Thompson 	}
220302ac6454SAndrew Thompson 	/*
220402ac6454SAndrew Thompson 	 * Only finish isochronous transfers once which will update
220502ac6454SAndrew Thompson 	 * "xfer->frlengths".
220602ac6454SAndrew Thompson 	 */
220702ac6454SAndrew Thompson 	if (xfer->td_transfer_first &&
220802ac6454SAndrew Thompson 	    xfer->td_transfer_last) {
220902ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_fs_methods) {
221002ac6454SAndrew Thompson 			ehci_isoc_fs_done(sc, xfer);
221102ac6454SAndrew Thompson 		}
221202ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_hs_methods) {
221302ac6454SAndrew Thompson 			ehci_isoc_hs_done(sc, xfer);
221402ac6454SAndrew Thompson 		}
221502ac6454SAndrew Thompson 		xfer->td_transfer_first = NULL;
221602ac6454SAndrew Thompson 		xfer->td_transfer_last = NULL;
221702ac6454SAndrew Thompson 	}
221802ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
2219a593f6b8SAndrew Thompson 	usbd_transfer_done(xfer, error);
222002ac6454SAndrew Thompson }
222102ac6454SAndrew Thompson 
222202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
222302ac6454SAndrew Thompson  * ehci bulk support
222402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
222502ac6454SAndrew Thompson static void
2226760bc48eSAndrew Thompson ehci_device_bulk_open(struct usb_xfer *xfer)
222702ac6454SAndrew Thompson {
222802ac6454SAndrew Thompson 	return;
222902ac6454SAndrew Thompson }
223002ac6454SAndrew Thompson 
223102ac6454SAndrew Thompson static void
2232760bc48eSAndrew Thompson ehci_device_bulk_close(struct usb_xfer *xfer)
223302ac6454SAndrew Thompson {
223402ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
223502ac6454SAndrew Thompson }
223602ac6454SAndrew Thompson 
223702ac6454SAndrew Thompson static void
2238760bc48eSAndrew Thompson ehci_device_bulk_enter(struct usb_xfer *xfer)
223902ac6454SAndrew Thompson {
224002ac6454SAndrew Thompson 	return;
224102ac6454SAndrew Thompson }
224202ac6454SAndrew Thompson 
224302ac6454SAndrew Thompson static void
22444a6af125SHans Petter Selasky ehci_doorbell_async(struct ehci_softc *sc)
22454a6af125SHans Petter Selasky {
22464a6af125SHans Petter Selasky 	uint32_t temp;
22474a6af125SHans Petter Selasky 
22484a6af125SHans Petter Selasky 	/*
22494a6af125SHans Petter Selasky 	 * XXX Performance quirk: Some Host Controllers have a too low
22504a6af125SHans Petter Selasky 	 * interrupt rate. Issue an IAAD to stimulate the Host
22514a6af125SHans Petter Selasky 	 * Controller after queueing the BULK transfer.
22524a6af125SHans Petter Selasky 	 *
22534a6af125SHans Petter Selasky 	 * XXX Force the host controller to refresh any QH caches.
22544a6af125SHans Petter Selasky 	 */
22554a6af125SHans Petter Selasky 	temp = EOREAD4(sc, EHCI_USBCMD);
22564a6af125SHans Petter Selasky 	if (!(temp & EHCI_CMD_IAAD))
22574a6af125SHans Petter Selasky 		EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
22584a6af125SHans Petter Selasky }
22594a6af125SHans Petter Selasky 
22604a6af125SHans Petter Selasky static void
2261760bc48eSAndrew Thompson ehci_device_bulk_start(struct usb_xfer *xfer)
226202ac6454SAndrew Thompson {
226302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
226402ac6454SAndrew Thompson 
226502ac6454SAndrew Thompson 	/* setup TD's and QH */
226602ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
226702ac6454SAndrew Thompson 
226802ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
226902ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
2270d1864afbSAndrew Thompson 
2271cf223a88SAndrew Thompson 	/*
2272cf223a88SAndrew Thompson 	 * XXX Certain nVidia chipsets choke when using the IAAD
2273cf223a88SAndrew Thompson 	 * feature too frequently.
2274cf223a88SAndrew Thompson 	 */
2275cf223a88SAndrew Thompson 	if (sc->sc_flags & EHCI_SCFLG_IAADBUG)
2276cf223a88SAndrew Thompson 		return;
2277cf223a88SAndrew Thompson 
22784a6af125SHans Petter Selasky 	ehci_doorbell_async(sc);
227902ac6454SAndrew Thompson }
228002ac6454SAndrew Thompson 
2281e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_bulk_methods =
228202ac6454SAndrew Thompson {
228302ac6454SAndrew Thompson 	.open = ehci_device_bulk_open,
228402ac6454SAndrew Thompson 	.close = ehci_device_bulk_close,
228502ac6454SAndrew Thompson 	.enter = ehci_device_bulk_enter,
228602ac6454SAndrew Thompson 	.start = ehci_device_bulk_start,
228702ac6454SAndrew Thompson };
228802ac6454SAndrew Thompson 
228902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
229002ac6454SAndrew Thompson  * ehci control support
229102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
229202ac6454SAndrew Thompson static void
2293760bc48eSAndrew Thompson ehci_device_ctrl_open(struct usb_xfer *xfer)
229402ac6454SAndrew Thompson {
229502ac6454SAndrew Thompson 	return;
229602ac6454SAndrew Thompson }
229702ac6454SAndrew Thompson 
229802ac6454SAndrew Thompson static void
2299760bc48eSAndrew Thompson ehci_device_ctrl_close(struct usb_xfer *xfer)
230002ac6454SAndrew Thompson {
230102ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
230202ac6454SAndrew Thompson }
230302ac6454SAndrew Thompson 
230402ac6454SAndrew Thompson static void
2305760bc48eSAndrew Thompson ehci_device_ctrl_enter(struct usb_xfer *xfer)
230602ac6454SAndrew Thompson {
230702ac6454SAndrew Thompson 	return;
230802ac6454SAndrew Thompson }
230902ac6454SAndrew Thompson 
231002ac6454SAndrew Thompson static void
2311760bc48eSAndrew Thompson ehci_device_ctrl_start(struct usb_xfer *xfer)
231202ac6454SAndrew Thompson {
231302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
231402ac6454SAndrew Thompson 
231502ac6454SAndrew Thompson 	/* setup TD's and QH */
231602ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
231702ac6454SAndrew Thompson 
231802ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
231902ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
232002ac6454SAndrew Thompson }
232102ac6454SAndrew Thompson 
2322e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_ctrl_methods =
232302ac6454SAndrew Thompson {
232402ac6454SAndrew Thompson 	.open = ehci_device_ctrl_open,
232502ac6454SAndrew Thompson 	.close = ehci_device_ctrl_close,
232602ac6454SAndrew Thompson 	.enter = ehci_device_ctrl_enter,
232702ac6454SAndrew Thompson 	.start = ehci_device_ctrl_start,
232802ac6454SAndrew Thompson };
232902ac6454SAndrew Thompson 
233002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
233102ac6454SAndrew Thompson  * ehci interrupt support
233202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
233302ac6454SAndrew Thompson static void
2334760bc48eSAndrew Thompson ehci_device_intr_open(struct usb_xfer *xfer)
233502ac6454SAndrew Thompson {
233602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
233702ac6454SAndrew Thompson 	uint16_t best;
233802ac6454SAndrew Thompson 	uint16_t bit;
233902ac6454SAndrew Thompson 	uint16_t x;
234002ac6454SAndrew Thompson 
2341f12c6c29SAndrew Thompson 	usb_hs_bandwidth_alloc(xfer);
234202ac6454SAndrew Thompson 
234302ac6454SAndrew Thompson 	/*
234402ac6454SAndrew Thompson 	 * Find the best QH position corresponding to the given interval:
234502ac6454SAndrew Thompson 	 */
234602ac6454SAndrew Thompson 
234702ac6454SAndrew Thompson 	best = 0;
234802ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
234902ac6454SAndrew Thompson 	while (bit) {
235002ac6454SAndrew Thompson 		if (xfer->interval >= bit) {
235102ac6454SAndrew Thompson 			x = bit;
235202ac6454SAndrew Thompson 			best = bit;
235302ac6454SAndrew Thompson 			while (x & bit) {
235402ac6454SAndrew Thompson 				if (sc->sc_intr_stat[x] <
235502ac6454SAndrew Thompson 				    sc->sc_intr_stat[best]) {
235602ac6454SAndrew Thompson 					best = x;
235702ac6454SAndrew Thompson 				}
235802ac6454SAndrew Thompson 				x++;
235902ac6454SAndrew Thompson 			}
236002ac6454SAndrew Thompson 			break;
236102ac6454SAndrew Thompson 		}
236202ac6454SAndrew Thompson 		bit >>= 1;
236302ac6454SAndrew Thompson 	}
236402ac6454SAndrew Thompson 
236502ac6454SAndrew Thompson 	sc->sc_intr_stat[best]++;
236602ac6454SAndrew Thompson 	xfer->qh_pos = best;
236702ac6454SAndrew Thompson 
236802ac6454SAndrew Thompson 	DPRINTFN(3, "best=%d interval=%d\n",
236902ac6454SAndrew Thompson 	    best, xfer->interval);
237002ac6454SAndrew Thompson }
237102ac6454SAndrew Thompson 
237202ac6454SAndrew Thompson static void
2373760bc48eSAndrew Thompson ehci_device_intr_close(struct usb_xfer *xfer)
237402ac6454SAndrew Thompson {
237502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
237602ac6454SAndrew Thompson 
237702ac6454SAndrew Thompson 	sc->sc_intr_stat[xfer->qh_pos]--;
237802ac6454SAndrew Thompson 
237902ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
2380f12c6c29SAndrew Thompson 
2381f12c6c29SAndrew Thompson 	/* bandwidth must be freed after device done */
2382f12c6c29SAndrew Thompson 	usb_hs_bandwidth_free(xfer);
238302ac6454SAndrew Thompson }
238402ac6454SAndrew Thompson 
238502ac6454SAndrew Thompson static void
2386760bc48eSAndrew Thompson ehci_device_intr_enter(struct usb_xfer *xfer)
238702ac6454SAndrew Thompson {
238802ac6454SAndrew Thompson 	return;
238902ac6454SAndrew Thompson }
239002ac6454SAndrew Thompson 
239102ac6454SAndrew Thompson static void
2392760bc48eSAndrew Thompson ehci_device_intr_start(struct usb_xfer *xfer)
239302ac6454SAndrew Thompson {
239402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
239502ac6454SAndrew Thompson 
239602ac6454SAndrew Thompson 	/* setup TD's and QH */
239702ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
239802ac6454SAndrew Thompson 
239902ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
240002ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
240102ac6454SAndrew Thompson }
240202ac6454SAndrew Thompson 
2403e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_intr_methods =
240402ac6454SAndrew Thompson {
240502ac6454SAndrew Thompson 	.open = ehci_device_intr_open,
240602ac6454SAndrew Thompson 	.close = ehci_device_intr_close,
240702ac6454SAndrew Thompson 	.enter = ehci_device_intr_enter,
240802ac6454SAndrew Thompson 	.start = ehci_device_intr_start,
240902ac6454SAndrew Thompson };
241002ac6454SAndrew Thompson 
241102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
241202ac6454SAndrew Thompson  * ehci full speed isochronous support
241302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
241402ac6454SAndrew Thompson static void
2415760bc48eSAndrew Thompson ehci_device_isoc_fs_open(struct usb_xfer *xfer)
241602ac6454SAndrew Thompson {
241702ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
241802ac6454SAndrew Thompson 	ehci_sitd_t *td;
241902ac6454SAndrew Thompson 	uint32_t sitd_portaddr;
242002ac6454SAndrew Thompson 	uint8_t ds;
242102ac6454SAndrew Thompson 
242202ac6454SAndrew Thompson 	sitd_portaddr =
242302ac6454SAndrew Thompson 	    EHCI_SITD_SET_ADDR(xfer->address) |
2424ae60fdfbSAndrew Thompson 	    EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
242502ac6454SAndrew Thompson 	    EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
242602ac6454SAndrew Thompson 	    EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
242702ac6454SAndrew Thompson 
24280a4cc48fSHans Petter Selasky 	if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
242902ac6454SAndrew Thompson 		sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
24300a4cc48fSHans Petter Selasky 
2431e55e1ebcSAndrew Thompson 	sitd_portaddr = htohc32(sc, sitd_portaddr);
243202ac6454SAndrew Thompson 
243302ac6454SAndrew Thompson 	/* initialize all TD's */
243402ac6454SAndrew Thompson 
243502ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
243602ac6454SAndrew Thompson 
243702ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
243802ac6454SAndrew Thompson 
243902ac6454SAndrew Thompson 			td->sitd_portaddr = sitd_portaddr;
244002ac6454SAndrew Thompson 
244102ac6454SAndrew Thompson 			/*
244202ac6454SAndrew Thompson 			 * TODO: make some kind of automatic
244302ac6454SAndrew Thompson 			 * SMASK/CMASK selection based on micro-frame
244402ac6454SAndrew Thompson 			 * usage
244502ac6454SAndrew Thompson 			 *
244602ac6454SAndrew Thompson 			 * micro-frame usage (8 microframes per 1ms)
244702ac6454SAndrew Thompson 			 */
2448e55e1ebcSAndrew Thompson 			td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
244902ac6454SAndrew Thompson 
2450a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
245102ac6454SAndrew Thompson 		}
245202ac6454SAndrew Thompson 	}
245302ac6454SAndrew Thompson }
245402ac6454SAndrew Thompson 
245502ac6454SAndrew Thompson static void
2456760bc48eSAndrew Thompson ehci_device_isoc_fs_close(struct usb_xfer *xfer)
245702ac6454SAndrew Thompson {
245802ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
245902ac6454SAndrew Thompson }
246002ac6454SAndrew Thompson 
246102ac6454SAndrew Thompson static void
2462760bc48eSAndrew Thompson ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
246302ac6454SAndrew Thompson {
2464760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
246502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
246602ac6454SAndrew Thompson 	ehci_sitd_t *td;
246702ac6454SAndrew Thompson 	ehci_sitd_t *td_last = NULL;
246802ac6454SAndrew Thompson 	ehci_sitd_t **pp_last;
246902ac6454SAndrew Thompson 	uint32_t *plen;
247002ac6454SAndrew Thompson 	uint32_t buf_offset;
247102ac6454SAndrew Thompson 	uint32_t nframes;
247202ac6454SAndrew Thompson 	uint32_t temp;
247302ac6454SAndrew Thompson 	uint32_t sitd_mask;
247402ac6454SAndrew Thompson 	uint16_t tlen;
247502ac6454SAndrew Thompson 	uint8_t sa;
247602ac6454SAndrew Thompson 	uint8_t sb;
247702ac6454SAndrew Thompson 
2478b850ecc1SAndrew Thompson #ifdef USB_DEBUG
247902ac6454SAndrew Thompson 	uint8_t once = 1;
248002ac6454SAndrew Thompson 
248102ac6454SAndrew Thompson #endif
248202ac6454SAndrew Thompson 
248302ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2484ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
248502ac6454SAndrew Thompson 
248602ac6454SAndrew Thompson 	/* get the current frame index */
248702ac6454SAndrew Thompson 
248802ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
248902ac6454SAndrew Thompson 
249002ac6454SAndrew Thompson 	/*
249102ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
249202ac6454SAndrew Thompson 	 * will be inserted
249302ac6454SAndrew Thompson 	 */
2494ae60fdfbSAndrew Thompson 	buf_offset = (nframes - xfer->endpoint->isoc_next) &
249502ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
249602ac6454SAndrew Thompson 
2497ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
249802ac6454SAndrew Thompson 	    (buf_offset < xfer->nframes)) {
249902ac6454SAndrew Thompson 		/*
250002ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
250102ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
250202ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
250302ac6454SAndrew Thompson 		 * overlap.
250402ac6454SAndrew Thompson 		 */
2505ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) &
250602ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2507ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
2508ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
250902ac6454SAndrew Thompson 	}
251002ac6454SAndrew Thompson 	/*
251102ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
251202ac6454SAndrew Thompson 	 * current frame position:
251302ac6454SAndrew Thompson 	 */
2514ae60fdfbSAndrew Thompson 	buf_offset = (xfer->endpoint->isoc_next - nframes) &
251502ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
251602ac6454SAndrew Thompson 
251702ac6454SAndrew Thompson 	/*
251802ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
251902ac6454SAndrew Thompson 	 */
252002ac6454SAndrew Thompson 	xfer->isoc_time_complete =
25210a4cc48fSHans Petter Selasky 	    usb_isoc_time_expand(&sc->sc_bus, nframes) +
25220a4cc48fSHans Petter Selasky 	    buf_offset + xfer->nframes;
252302ac6454SAndrew Thompson 
252402ac6454SAndrew Thompson 	/* get the real number of frames */
252502ac6454SAndrew Thompson 
252602ac6454SAndrew Thompson 	nframes = xfer->nframes;
252702ac6454SAndrew Thompson 
252802ac6454SAndrew Thompson 	buf_offset = 0;
252902ac6454SAndrew Thompson 
253002ac6454SAndrew Thompson 	plen = xfer->frlengths;
253102ac6454SAndrew Thompson 
253202ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
253302ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
253402ac6454SAndrew Thompson 
253502ac6454SAndrew Thompson 	/* get next DMA set */
253602ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
253702ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
253802ac6454SAndrew Thompson 
2539ae60fdfbSAndrew Thompson 	pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];
254002ac6454SAndrew Thompson 
254102ac6454SAndrew Thompson 	/* store starting position */
254202ac6454SAndrew Thompson 
2543ae60fdfbSAndrew Thompson 	xfer->qh_pos = xfer->endpoint->isoc_next;
254402ac6454SAndrew Thompson 
254502ac6454SAndrew Thompson 	while (nframes--) {
254602ac6454SAndrew Thompson 		if (td == NULL) {
254702ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
254802ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
254902ac6454SAndrew Thompson 		}
25500a4cc48fSHans Petter Selasky 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT])
255102ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
25520a4cc48fSHans Petter Selasky 
255302ac6454SAndrew Thompson 		/* reuse sitd_portaddr and sitd_back from last transfer */
255402ac6454SAndrew Thompson 
255502ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
2556b850ecc1SAndrew Thompson #ifdef USB_DEBUG
255702ac6454SAndrew Thompson 			if (once) {
255802ac6454SAndrew Thompson 				once = 0;
255902ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d "
256002ac6454SAndrew Thompson 				    "bytes (frame truncated)\n",
256102ac6454SAndrew Thompson 				    __FUNCTION__, *plen,
256202ac6454SAndrew Thompson 				    xfer->max_frame_size);
256302ac6454SAndrew Thompson 			}
256402ac6454SAndrew Thompson #endif
256502ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
256602ac6454SAndrew Thompson 		}
25670a4cc48fSHans Petter Selasky 
25680a4cc48fSHans Petter Selasky 		/* allocate a slot */
25690a4cc48fSHans Petter Selasky 
25700a4cc48fSHans Petter Selasky 		sa = usbd_fs_isoc_schedule_alloc_slot(xfer,
25710a4cc48fSHans Petter Selasky 		    xfer->isoc_time_complete - nframes - 1);
25720a4cc48fSHans Petter Selasky 
25730a4cc48fSHans Petter Selasky 		if (sa == 255) {
257402ac6454SAndrew Thompson 			/*
25750a4cc48fSHans Petter Selasky 			 * Schedule is FULL, set length to zero:
257602ac6454SAndrew Thompson 			 */
25770a4cc48fSHans Petter Selasky 
257802ac6454SAndrew Thompson 			*plen = 0;
25790a4cc48fSHans Petter Selasky 			sa = USB_FS_ISOC_UFRAME_MAX - 1;
258002ac6454SAndrew Thompson 		}
258102ac6454SAndrew Thompson 		if (*plen) {
258202ac6454SAndrew Thompson 			/*
2583a593f6b8SAndrew Thompson 			 * only call "usbd_get_page()" when we have a
258402ac6454SAndrew Thompson 			 * non-zero length
258502ac6454SAndrew Thompson 			 */
2586a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2587e55e1ebcSAndrew Thompson 			td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
258802ac6454SAndrew Thompson 			buf_offset += *plen;
258902ac6454SAndrew Thompson 			/*
259002ac6454SAndrew Thompson 			 * NOTE: We need to subtract one from the offset so
259102ac6454SAndrew Thompson 			 * that we are on a valid page!
259202ac6454SAndrew Thompson 			 */
2593a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset - 1,
259402ac6454SAndrew Thompson 			    &buf_res);
259502ac6454SAndrew Thompson 			temp = buf_res.physaddr & ~0xFFF;
259602ac6454SAndrew Thompson 		} else {
259702ac6454SAndrew Thompson 			td->sitd_bp[0] = 0;
259802ac6454SAndrew Thompson 			temp = 0;
259902ac6454SAndrew Thompson 		}
260002ac6454SAndrew Thompson 
2601ae60fdfbSAndrew Thompson 		if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
260202ac6454SAndrew Thompson 			tlen = *plen;
260302ac6454SAndrew Thompson 			if (tlen <= 188) {
260402ac6454SAndrew Thompson 				temp |= 1;	/* T-count = 1, TP = ALL */
260502ac6454SAndrew Thompson 				tlen = 1;
260602ac6454SAndrew Thompson 			} else {
260702ac6454SAndrew Thompson 				tlen += 187;
260802ac6454SAndrew Thompson 				tlen /= 188;
260902ac6454SAndrew Thompson 				temp |= tlen;	/* T-count = [1..6] */
261002ac6454SAndrew Thompson 				temp |= 8;	/* TP = Begin */
261102ac6454SAndrew Thompson 			}
261202ac6454SAndrew Thompson 
261302ac6454SAndrew Thompson 			tlen += sa;
261402ac6454SAndrew Thompson 
261502ac6454SAndrew Thompson 			if (tlen >= 8) {
261602ac6454SAndrew Thompson 				sb = 0;
261702ac6454SAndrew Thompson 			} else {
261802ac6454SAndrew Thompson 				sb = (1 << tlen);
261902ac6454SAndrew Thompson 			}
262002ac6454SAndrew Thompson 
262102ac6454SAndrew Thompson 			sa = (1 << sa);
262202ac6454SAndrew Thompson 			sa = (sb - sa) & 0x3F;
262302ac6454SAndrew Thompson 			sb = 0;
262402ac6454SAndrew Thompson 		} else {
262502ac6454SAndrew Thompson 			sb = (-(4 << sa)) & 0xFE;
262602ac6454SAndrew Thompson 			sa = (1 << sa) & 0x3F;
262702ac6454SAndrew Thompson 		}
262802ac6454SAndrew Thompson 
262902ac6454SAndrew Thompson 		sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
263002ac6454SAndrew Thompson 		    EHCI_SITD_SET_CMASK(sb));
263102ac6454SAndrew Thompson 
2632e55e1ebcSAndrew Thompson 		td->sitd_bp[1] = htohc32(sc, temp);
263302ac6454SAndrew Thompson 
2634e55e1ebcSAndrew Thompson 		td->sitd_mask = htohc32(sc, sitd_mask);
263502ac6454SAndrew Thompson 
263602ac6454SAndrew Thompson 		if (nframes == 0) {
2637e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
26385f128668SAndrew Thompson 			    EHCI_SITD_IOC |
263902ac6454SAndrew Thompson 			    EHCI_SITD_ACTIVE |
264002ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
264102ac6454SAndrew Thompson 		} else {
2642e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
26435f128668SAndrew Thompson 			    EHCI_SITD_ACTIVE |
264402ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
264502ac6454SAndrew Thompson 		}
2646a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
264702ac6454SAndrew Thompson 
2648b850ecc1SAndrew Thompson #ifdef USB_DEBUG
264902ac6454SAndrew Thompson 		if (ehcidebug > 15) {
265002ac6454SAndrew Thompson 			DPRINTF("FS-TD %d\n", nframes);
265102ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
265202ac6454SAndrew Thompson 		}
265302ac6454SAndrew Thompson #endif
265402ac6454SAndrew Thompson 		/* insert TD into schedule */
265502ac6454SAndrew Thompson 		EHCI_APPEND_FS_TD(td, *pp_last);
265602ac6454SAndrew Thompson 		pp_last++;
265702ac6454SAndrew Thompson 
265802ac6454SAndrew Thompson 		plen++;
265902ac6454SAndrew Thompson 		td_last = td;
266002ac6454SAndrew Thompson 		td = td->obj_next;
266102ac6454SAndrew Thompson 	}
266202ac6454SAndrew Thompson 
266302ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
266402ac6454SAndrew Thompson 
266502ac6454SAndrew Thompson 	/* update isoc_next */
2666ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
266702ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
26687e66ab7cSHans Petter Selasky 
26697e66ab7cSHans Petter Selasky 	/*
26707e66ab7cSHans Petter Selasky 	 * We don't allow cancelling of the SPLIT transaction USB FULL
26717e66ab7cSHans Petter Selasky 	 * speed transfer, because it disturbs the bandwidth
26727e66ab7cSHans Petter Selasky 	 * computation algorithm.
26737e66ab7cSHans Petter Selasky 	 */
26747e66ab7cSHans Petter Selasky 	xfer->flags_int.can_cancel_immed = 0;
267502ac6454SAndrew Thompson }
267602ac6454SAndrew Thompson 
267702ac6454SAndrew Thompson static void
2678760bc48eSAndrew Thompson ehci_device_isoc_fs_start(struct usb_xfer *xfer)
267902ac6454SAndrew Thompson {
26807e66ab7cSHans Petter Selasky 	/*
26817e66ab7cSHans Petter Selasky 	 * We don't allow cancelling of the SPLIT transaction USB FULL
26827e66ab7cSHans Petter Selasky 	 * speed transfer, because it disturbs the bandwidth
26837e66ab7cSHans Petter Selasky 	 * computation algorithm.
26847e66ab7cSHans Petter Selasky 	 */
26857e66ab7cSHans Petter Selasky 	xfer->flags_int.can_cancel_immed = 0;
26867e66ab7cSHans Petter Selasky 
26877e66ab7cSHans Petter Selasky 	/* set a default timeout */
26887e66ab7cSHans Petter Selasky 	if (xfer->timeout == 0)
26897e66ab7cSHans Petter Selasky 		xfer->timeout = 500; /* ms */
26907e66ab7cSHans Petter Selasky 
269102ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
269202ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
269302ac6454SAndrew Thompson }
269402ac6454SAndrew Thompson 
2695e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_fs_methods =
269602ac6454SAndrew Thompson {
269702ac6454SAndrew Thompson 	.open = ehci_device_isoc_fs_open,
269802ac6454SAndrew Thompson 	.close = ehci_device_isoc_fs_close,
269902ac6454SAndrew Thompson 	.enter = ehci_device_isoc_fs_enter,
270002ac6454SAndrew Thompson 	.start = ehci_device_isoc_fs_start,
270102ac6454SAndrew Thompson };
270202ac6454SAndrew Thompson 
270302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
270402ac6454SAndrew Thompson  * ehci high speed isochronous support
270502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
270602ac6454SAndrew Thompson static void
2707760bc48eSAndrew Thompson ehci_device_isoc_hs_open(struct usb_xfer *xfer)
270802ac6454SAndrew Thompson {
270902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
271002ac6454SAndrew Thompson 	ehci_itd_t *td;
271102ac6454SAndrew Thompson 	uint32_t temp;
271202ac6454SAndrew Thompson 	uint8_t ds;
2713883bb300SAndrew Thompson 
2714f12c6c29SAndrew Thompson 	usb_hs_bandwidth_alloc(xfer);
271502ac6454SAndrew Thompson 
271602ac6454SAndrew Thompson 	/* initialize all TD's */
271702ac6454SAndrew Thompson 
271802ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
271902ac6454SAndrew Thompson 
272002ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
272102ac6454SAndrew Thompson 
272202ac6454SAndrew Thompson 			/* set TD inactive */
272302ac6454SAndrew Thompson 			td->itd_status[0] = 0;
272402ac6454SAndrew Thompson 			td->itd_status[1] = 0;
272502ac6454SAndrew Thompson 			td->itd_status[2] = 0;
272602ac6454SAndrew Thompson 			td->itd_status[3] = 0;
272702ac6454SAndrew Thompson 			td->itd_status[4] = 0;
272802ac6454SAndrew Thompson 			td->itd_status[5] = 0;
272902ac6454SAndrew Thompson 			td->itd_status[6] = 0;
273002ac6454SAndrew Thompson 			td->itd_status[7] = 0;
273102ac6454SAndrew Thompson 
273202ac6454SAndrew Thompson 			/* set endpoint and address */
2733e55e1ebcSAndrew Thompson 			td->itd_bp[0] = htohc32(sc,
27345f128668SAndrew Thompson 			    EHCI_ITD_SET_ADDR(xfer->address) |
2735ae60fdfbSAndrew Thompson 			    EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
273602ac6454SAndrew Thompson 
273702ac6454SAndrew Thompson 			temp =
273802ac6454SAndrew Thompson 			    EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
273902ac6454SAndrew Thompson 
274002ac6454SAndrew Thompson 			/* set direction */
2741ae60fdfbSAndrew Thompson 			if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
274202ac6454SAndrew Thompson 				temp |= EHCI_ITD_SET_DIR_IN;
274302ac6454SAndrew Thompson 			}
274402ac6454SAndrew Thompson 			/* set maximum packet size */
2745e55e1ebcSAndrew Thompson 			td->itd_bp[1] = htohc32(sc, temp);
274602ac6454SAndrew Thompson 
274702ac6454SAndrew Thompson 			/* set transfer multiplier */
2748e55e1ebcSAndrew Thompson 			td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
274902ac6454SAndrew Thompson 
2750a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
275102ac6454SAndrew Thompson 		}
275202ac6454SAndrew Thompson 	}
275302ac6454SAndrew Thompson }
275402ac6454SAndrew Thompson 
275502ac6454SAndrew Thompson static void
2756760bc48eSAndrew Thompson ehci_device_isoc_hs_close(struct usb_xfer *xfer)
275702ac6454SAndrew Thompson {
275802ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
2759f12c6c29SAndrew Thompson 
2760f12c6c29SAndrew Thompson 	/* bandwidth must be freed after device done */
2761f12c6c29SAndrew Thompson 	usb_hs_bandwidth_free(xfer);
276202ac6454SAndrew Thompson }
276302ac6454SAndrew Thompson 
276402ac6454SAndrew Thompson static void
2765760bc48eSAndrew Thompson ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
276602ac6454SAndrew Thompson {
2767760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
276802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
276902ac6454SAndrew Thompson 	ehci_itd_t *td;
277002ac6454SAndrew Thompson 	ehci_itd_t *td_last = NULL;
277102ac6454SAndrew Thompson 	ehci_itd_t **pp_last;
277202ac6454SAndrew Thompson 	bus_size_t page_addr;
277302ac6454SAndrew Thompson 	uint32_t *plen;
277402ac6454SAndrew Thompson 	uint32_t status;
277502ac6454SAndrew Thompson 	uint32_t buf_offset;
277602ac6454SAndrew Thompson 	uint32_t nframes;
277702ac6454SAndrew Thompson 	uint32_t itd_offset[8 + 1];
277802ac6454SAndrew Thompson 	uint8_t x;
277902ac6454SAndrew Thompson 	uint8_t td_no;
278002ac6454SAndrew Thompson 	uint8_t page_no;
2781d1f7c4baSAndrew Thompson 	uint8_t shift = usbd_xfer_get_fps_shift(xfer);
278202ac6454SAndrew Thompson 
2783b850ecc1SAndrew Thompson #ifdef USB_DEBUG
278402ac6454SAndrew Thompson 	uint8_t once = 1;
278502ac6454SAndrew Thompson 
278602ac6454SAndrew Thompson #endif
278702ac6454SAndrew Thompson 
2788d1f7c4baSAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d shift=%d\n",
2789d1f7c4baSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes, (int)shift);
279002ac6454SAndrew Thompson 
279102ac6454SAndrew Thompson 	/* get the current frame index */
279202ac6454SAndrew Thompson 
279302ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
279402ac6454SAndrew Thompson 
279502ac6454SAndrew Thompson 	/*
279602ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
279702ac6454SAndrew Thompson 	 * will be inserted
279802ac6454SAndrew Thompson 	 */
2799ae60fdfbSAndrew Thompson 	buf_offset = (nframes - xfer->endpoint->isoc_next) &
280002ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
280102ac6454SAndrew Thompson 
2802ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
2803d1f7c4baSAndrew Thompson 	    (buf_offset < (((xfer->nframes << shift) + 7) / 8))) {
280402ac6454SAndrew Thompson 		/*
280502ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
280602ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
280702ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
280802ac6454SAndrew Thompson 		 * overlap.
280902ac6454SAndrew Thompson 		 */
2810ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) &
281102ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2812ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
2813ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
281402ac6454SAndrew Thompson 	}
281502ac6454SAndrew Thompson 	/*
281602ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
281702ac6454SAndrew Thompson 	 * current frame position:
281802ac6454SAndrew Thompson 	 */
2819ae60fdfbSAndrew Thompson 	buf_offset = (xfer->endpoint->isoc_next - nframes) &
282002ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
282102ac6454SAndrew Thompson 
282202ac6454SAndrew Thompson 	/*
282302ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
282402ac6454SAndrew Thompson 	 */
282502ac6454SAndrew Thompson 	xfer->isoc_time_complete =
2826a593f6b8SAndrew Thompson 	    usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
2827d1f7c4baSAndrew Thompson 	    (((xfer->nframes << shift) + 7) / 8);
282802ac6454SAndrew Thompson 
282902ac6454SAndrew Thompson 	/* get the real number of frames */
283002ac6454SAndrew Thompson 
283102ac6454SAndrew Thompson 	nframes = xfer->nframes;
283202ac6454SAndrew Thompson 
283302ac6454SAndrew Thompson 	buf_offset = 0;
283402ac6454SAndrew Thompson 	td_no = 0;
283502ac6454SAndrew Thompson 
283602ac6454SAndrew Thompson 	plen = xfer->frlengths;
283702ac6454SAndrew Thompson 
283802ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
283902ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
284002ac6454SAndrew Thompson 
284102ac6454SAndrew Thompson 	/* get next DMA set */
284202ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
284302ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
284402ac6454SAndrew Thompson 
2845ae60fdfbSAndrew Thompson 	pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];
284602ac6454SAndrew Thompson 
284702ac6454SAndrew Thompson 	/* store starting position */
284802ac6454SAndrew Thompson 
2849ae60fdfbSAndrew Thompson 	xfer->qh_pos = xfer->endpoint->isoc_next;
285002ac6454SAndrew Thompson 
2851883bb300SAndrew Thompson 	while (nframes) {
285202ac6454SAndrew Thompson 		if (td == NULL) {
285302ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
285402ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
285502ac6454SAndrew Thompson 		}
285602ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
285702ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
285802ac6454SAndrew Thompson 		}
285902ac6454SAndrew Thompson 		/* range check */
286002ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
2861b850ecc1SAndrew Thompson #ifdef USB_DEBUG
286202ac6454SAndrew Thompson 			if (once) {
286302ac6454SAndrew Thompson 				once = 0;
286402ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d bytes "
286502ac6454SAndrew Thompson 				    "(frame truncated)\n",
286602ac6454SAndrew Thompson 				    __FUNCTION__, *plen, xfer->max_frame_size);
286702ac6454SAndrew Thompson 			}
286802ac6454SAndrew Thompson #endif
286902ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
287002ac6454SAndrew Thompson 		}
2871883bb300SAndrew Thompson 
2872f12c6c29SAndrew Thompson 		if (xfer->endpoint->usb_smask & (1 << td_no)) {
287302ac6454SAndrew Thompson 			status = (EHCI_ITD_SET_LEN(*plen) |
287402ac6454SAndrew Thompson 			    EHCI_ITD_ACTIVE |
287502ac6454SAndrew Thompson 			    EHCI_ITD_SET_PG(0));
2876e55e1ebcSAndrew Thompson 			td->itd_status[td_no] = htohc32(sc, status);
287702ac6454SAndrew Thompson 			itd_offset[td_no] = buf_offset;
287802ac6454SAndrew Thompson 			buf_offset += *plen;
287902ac6454SAndrew Thompson 			plen++;
2880883bb300SAndrew Thompson 			nframes --;
2881883bb300SAndrew Thompson 		} else {
2882883bb300SAndrew Thompson 			td->itd_status[td_no] = 0;	/* not active */
2883883bb300SAndrew Thompson 			itd_offset[td_no] = buf_offset;
2884883bb300SAndrew Thompson 		}
2885883bb300SAndrew Thompson 
288602ac6454SAndrew Thompson 		td_no++;
288702ac6454SAndrew Thompson 
288802ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
288902ac6454SAndrew Thompson 
289002ac6454SAndrew Thompson 			/* the rest of the transfers are not active, if any */
289102ac6454SAndrew Thompson 			for (x = td_no; x != 8; x++) {
289202ac6454SAndrew Thompson 				td->itd_status[x] = 0;	/* not active */
289302ac6454SAndrew Thompson 			}
289402ac6454SAndrew Thompson 
289502ac6454SAndrew Thompson 			/* check if there is any data to be transferred */
289602ac6454SAndrew Thompson 			if (itd_offset[0] != buf_offset) {
289702ac6454SAndrew Thompson 				page_no = 0;
289802ac6454SAndrew Thompson 				itd_offset[td_no] = buf_offset;
289902ac6454SAndrew Thompson 
290002ac6454SAndrew Thompson 				/* get first page offset */
2901a593f6b8SAndrew Thompson 				usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
290202ac6454SAndrew Thompson 				/* get page address */
290302ac6454SAndrew Thompson 				page_addr = buf_res.physaddr & ~0xFFF;
290402ac6454SAndrew Thompson 				/* update page address */
2905e55e1ebcSAndrew Thompson 				td->itd_bp[0] &= htohc32(sc, 0xFFF);
2906e55e1ebcSAndrew Thompson 				td->itd_bp[0] |= htohc32(sc, page_addr);
290702ac6454SAndrew Thompson 
290802ac6454SAndrew Thompson 				for (x = 0; x != td_no; x++) {
290902ac6454SAndrew Thompson 					/* set page number and page offset */
291002ac6454SAndrew Thompson 					status = (EHCI_ITD_SET_PG(page_no) |
291102ac6454SAndrew Thompson 					    (buf_res.physaddr & 0xFFF));
2912e55e1ebcSAndrew Thompson 					td->itd_status[x] |= htohc32(sc, status);
291302ac6454SAndrew Thompson 
291402ac6454SAndrew Thompson 					/* get next page offset */
291502ac6454SAndrew Thompson 					if (itd_offset[x + 1] == buf_offset) {
291602ac6454SAndrew Thompson 						/*
291702ac6454SAndrew Thompson 						 * We subtract one so that
291802ac6454SAndrew Thompson 						 * we don't go off the last
291902ac6454SAndrew Thompson 						 * page!
292002ac6454SAndrew Thompson 						 */
2921a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
292202ac6454SAndrew Thompson 					} else {
2923a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
292402ac6454SAndrew Thompson 					}
292502ac6454SAndrew Thompson 
292602ac6454SAndrew Thompson 					/* check if we need a new page */
292702ac6454SAndrew Thompson 					if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
292802ac6454SAndrew Thompson 						/* new page needed */
292902ac6454SAndrew Thompson 						page_addr = buf_res.physaddr & ~0xFFF;
293002ac6454SAndrew Thompson 						if (page_no == 6) {
293102ac6454SAndrew Thompson 							panic("%s: too many pages\n", __FUNCTION__);
293202ac6454SAndrew Thompson 						}
293302ac6454SAndrew Thompson 						page_no++;
293402ac6454SAndrew Thompson 						/* update page address */
2935e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2936e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] |= htohc32(sc, page_addr);
293702ac6454SAndrew Thompson 					}
293802ac6454SAndrew Thompson 				}
293902ac6454SAndrew Thompson 			}
294002ac6454SAndrew Thompson 			/* set IOC bit if we are complete */
294102ac6454SAndrew Thompson 			if (nframes == 0) {
2942883bb300SAndrew Thompson 				td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC);
294302ac6454SAndrew Thompson 			}
2944a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
2945b850ecc1SAndrew Thompson #ifdef USB_DEBUG
294602ac6454SAndrew Thompson 			if (ehcidebug > 15) {
294702ac6454SAndrew Thompson 				DPRINTF("HS-TD %d\n", nframes);
294802ac6454SAndrew Thompson 				ehci_dump_itd(sc, td);
294902ac6454SAndrew Thompson 			}
295002ac6454SAndrew Thompson #endif
295102ac6454SAndrew Thompson 			/* insert TD into schedule */
295202ac6454SAndrew Thompson 			EHCI_APPEND_HS_TD(td, *pp_last);
295302ac6454SAndrew Thompson 			pp_last++;
295402ac6454SAndrew Thompson 
295502ac6454SAndrew Thompson 			td_no = 0;
295602ac6454SAndrew Thompson 			td_last = td;
295702ac6454SAndrew Thompson 			td = td->obj_next;
295802ac6454SAndrew Thompson 		}
295902ac6454SAndrew Thompson 	}
296002ac6454SAndrew Thompson 
296102ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
296202ac6454SAndrew Thompson 
296302ac6454SAndrew Thompson 	/* update isoc_next */
2964ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
296502ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
296602ac6454SAndrew Thompson }
296702ac6454SAndrew Thompson 
296802ac6454SAndrew Thompson static void
2969760bc48eSAndrew Thompson ehci_device_isoc_hs_start(struct usb_xfer *xfer)
297002ac6454SAndrew Thompson {
297102ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
297202ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
297302ac6454SAndrew Thompson }
297402ac6454SAndrew Thompson 
2975e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_hs_methods =
297602ac6454SAndrew Thompson {
297702ac6454SAndrew Thompson 	.open = ehci_device_isoc_hs_open,
297802ac6454SAndrew Thompson 	.close = ehci_device_isoc_hs_close,
297902ac6454SAndrew Thompson 	.enter = ehci_device_isoc_hs_enter,
298002ac6454SAndrew Thompson 	.start = ehci_device_isoc_hs_start,
298102ac6454SAndrew Thompson };
298202ac6454SAndrew Thompson 
298302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
298402ac6454SAndrew Thompson  * ehci root control support
298502ac6454SAndrew Thompson  *------------------------------------------------------------------------*
298639307315SAndrew Thompson  * Simulate a hardware hub by handling all the necessary requests.
298702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
298802ac6454SAndrew Thompson 
298902ac6454SAndrew Thompson static const
2990760bc48eSAndrew Thompson struct usb_device_descriptor ehci_devd =
299102ac6454SAndrew Thompson {
2992760bc48eSAndrew Thompson 	sizeof(struct usb_device_descriptor),
299302ac6454SAndrew Thompson 	UDESC_DEVICE,			/* type */
299402ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
299502ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
299602ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
299702ac6454SAndrew Thompson 	UDPROTO_HSHUBSTT,		/* protocol */
299802ac6454SAndrew Thompson 	64,				/* max packet */
299902ac6454SAndrew Thompson 	{0}, {0}, {0x00, 0x01},		/* device id */
300020733245SPedro F. Giffuni 	1, 2, 0,			/* string indexes */
300102ac6454SAndrew Thompson 	1				/* # of configurations */
300202ac6454SAndrew Thompson };
300302ac6454SAndrew Thompson 
300402ac6454SAndrew Thompson static const
3005760bc48eSAndrew Thompson struct usb_device_qualifier ehci_odevd =
300602ac6454SAndrew Thompson {
3007760bc48eSAndrew Thompson 	sizeof(struct usb_device_qualifier),
300802ac6454SAndrew Thompson 	UDESC_DEVICE_QUALIFIER,		/* type */
300902ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
301002ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
301102ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
301202ac6454SAndrew Thompson 	UDPROTO_FSHUB,			/* protocol */
301302ac6454SAndrew Thompson 	0,				/* max packet */
301402ac6454SAndrew Thompson 	0,				/* # of configurations */
301502ac6454SAndrew Thompson 	0
301602ac6454SAndrew Thompson };
301702ac6454SAndrew Thompson 
301802ac6454SAndrew Thompson static const struct ehci_config_desc ehci_confd = {
301902ac6454SAndrew Thompson 	.confd = {
3020760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_config_descriptor),
302102ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
302202ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(ehci_confd),
302302ac6454SAndrew Thompson 		.bNumInterface = 1,
302402ac6454SAndrew Thompson 		.bConfigurationValue = 1,
302502ac6454SAndrew Thompson 		.iConfiguration = 0,
302602ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
302702ac6454SAndrew Thompson 		.bMaxPower = 0		/* max power */
302802ac6454SAndrew Thompson 	},
302902ac6454SAndrew Thompson 	.ifcd = {
3030760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_interface_descriptor),
303102ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
303202ac6454SAndrew Thompson 		.bNumEndpoints = 1,
303302ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
303402ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
30351678e135SHans Petter Selasky 		.bInterfaceProtocol = 0,
303602ac6454SAndrew Thompson 	},
303702ac6454SAndrew Thompson 	.endpd = {
3038760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_endpoint_descriptor),
303902ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
304002ac6454SAndrew Thompson 		.bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
304102ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
304202ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
304302ac6454SAndrew Thompson 		.bInterval = 255,
304402ac6454SAndrew Thompson 	},
304502ac6454SAndrew Thompson };
304602ac6454SAndrew Thompson 
304702ac6454SAndrew Thompson static const
3048760bc48eSAndrew Thompson struct usb_hub_descriptor ehci_hubd =
304902ac6454SAndrew Thompson {
30506d917491SHans Petter Selasky 	.bDescLength = 0,		/* dynamic length */
30516d917491SHans Petter Selasky 	.bDescriptorType = UDESC_HUB,
305202ac6454SAndrew Thompson };
305302ac6454SAndrew Thompson 
3054cdf4ec68SMichal Meloun uint16_t
3055cdf4ec68SMichal Meloun ehci_get_port_speed_portsc(struct ehci_softc *sc, uint16_t index)
3056cdf4ec68SMichal Meloun {
3057cdf4ec68SMichal Meloun 	uint32_t v;
3058cdf4ec68SMichal Meloun 
3059cdf4ec68SMichal Meloun 	v = EOREAD4(sc, EHCI_PORTSC(index));
3060cdf4ec68SMichal Meloun 	v = (v >> EHCI_PORTSC_PSPD_SHIFT) & EHCI_PORTSC_PSPD_MASK;
3061cdf4ec68SMichal Meloun 
3062cdf4ec68SMichal Meloun 	if (v == EHCI_PORT_SPEED_HIGH)
3063cdf4ec68SMichal Meloun 		return (UPS_HIGH_SPEED);
3064cdf4ec68SMichal Meloun 	if (v == EHCI_PORT_SPEED_LOW)
3065cdf4ec68SMichal Meloun 		return (UPS_LOW_SPEED);
3066cdf4ec68SMichal Meloun 	return (0);
3067cdf4ec68SMichal Meloun }
3068cdf4ec68SMichal Meloun 
3069cdf4ec68SMichal Meloun uint16_t
3070cdf4ec68SMichal Meloun ehci_get_port_speed_hostc(struct ehci_softc *sc, uint16_t index)
3071cdf4ec68SMichal Meloun {
3072cdf4ec68SMichal Meloun 	uint32_t v;
3073cdf4ec68SMichal Meloun 
3074cdf4ec68SMichal Meloun 	v = EOREAD4(sc, EHCI_HOSTC(index));
3075cdf4ec68SMichal Meloun 	v = (v >> EHCI_HOSTC_PSPD_SHIFT) & EHCI_HOSTC_PSPD_MASK;
3076cdf4ec68SMichal Meloun 
3077cdf4ec68SMichal Meloun 	if (v == EHCI_PORT_SPEED_HIGH)
3078cdf4ec68SMichal Meloun 		return (UPS_HIGH_SPEED);
3079cdf4ec68SMichal Meloun 	if (v == EHCI_PORT_SPEED_LOW)
3080cdf4ec68SMichal Meloun 		return (UPS_LOW_SPEED);
3081cdf4ec68SMichal Meloun 	return (0);
3082cdf4ec68SMichal Meloun }
3083cdf4ec68SMichal Meloun 
308402ac6454SAndrew Thompson static void
308502ac6454SAndrew Thompson ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
308602ac6454SAndrew Thompson {
308702ac6454SAndrew Thompson 	uint32_t port;
308802ac6454SAndrew Thompson 	uint32_t v;
308902ac6454SAndrew Thompson 
309002ac6454SAndrew Thompson 	DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
309102ac6454SAndrew Thompson 
309202ac6454SAndrew Thompson 	port = EHCI_PORTSC(index);
309302ac6454SAndrew Thompson 	v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
309402ac6454SAndrew Thompson 	EOWRITE4(sc, port, v | EHCI_PS_PO);
309502ac6454SAndrew Thompson }
309602ac6454SAndrew Thompson 
3097e0a69b51SAndrew Thompson static usb_error_t
3098760bc48eSAndrew Thompson ehci_roothub_exec(struct usb_device *udev,
3099760bc48eSAndrew Thompson     struct usb_device_request *req, const void **pptr, uint16_t *plength)
310002ac6454SAndrew Thompson {
3101459d369eSAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3102459d369eSAndrew Thompson 	const char *str_ptr;
3103459d369eSAndrew Thompson 	const void *ptr;
310402ac6454SAndrew Thompson 	uint32_t port;
310502ac6454SAndrew Thompson 	uint32_t v;
3106459d369eSAndrew Thompson 	uint16_t len;
310702ac6454SAndrew Thompson 	uint16_t i;
310802ac6454SAndrew Thompson 	uint16_t value;
310902ac6454SAndrew Thompson 	uint16_t index;
3110e0a69b51SAndrew Thompson 	usb_error_t err;
311102ac6454SAndrew Thompson 
311202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
311302ac6454SAndrew Thompson 
311402ac6454SAndrew Thompson 	/* buffer reset */
3115459d369eSAndrew Thompson 	ptr = (const void *)&sc->sc_hub_desc;
3116459d369eSAndrew Thompson 	len = 0;
3117459d369eSAndrew Thompson 	err = 0;
311802ac6454SAndrew Thompson 
3119459d369eSAndrew Thompson 	value = UGETW(req->wValue);
3120459d369eSAndrew Thompson 	index = UGETW(req->wIndex);
312102ac6454SAndrew Thompson 
312202ac6454SAndrew Thompson 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
312302ac6454SAndrew Thompson 	    "wValue=0x%04x wIndex=0x%04x\n",
3124459d369eSAndrew Thompson 	    req->bmRequestType, req->bRequest,
3125459d369eSAndrew Thompson 	    UGETW(req->wLength), value, index);
312602ac6454SAndrew Thompson 
312702ac6454SAndrew Thompson #define	C(x,y) ((x) | ((y) << 8))
3128459d369eSAndrew Thompson 	switch (C(req->bRequest, req->bmRequestType)) {
312902ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
313002ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
313102ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
313202ac6454SAndrew Thompson 		/*
313302ac6454SAndrew Thompson 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
313402ac6454SAndrew Thompson 		 * for the integrated root hub.
313502ac6454SAndrew Thompson 		 */
313602ac6454SAndrew Thompson 		break;
313702ac6454SAndrew Thompson 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
3138459d369eSAndrew Thompson 		len = 1;
313902ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
314002ac6454SAndrew Thompson 		break;
314102ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
314202ac6454SAndrew Thompson 		switch (value >> 8) {
314302ac6454SAndrew Thompson 		case UDESC_DEVICE:
314402ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3145459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
314602ac6454SAndrew Thompson 				goto done;
314702ac6454SAndrew Thompson 			}
3148459d369eSAndrew Thompson 			len = sizeof(ehci_devd);
3149459d369eSAndrew Thompson 			ptr = (const void *)&ehci_devd;
315002ac6454SAndrew Thompson 			break;
315102ac6454SAndrew Thompson 			/*
315202ac6454SAndrew Thompson 			 * We can't really operate at another speed,
315302ac6454SAndrew Thompson 			 * but the specification says we need this
315402ac6454SAndrew Thompson 			 * descriptor:
315502ac6454SAndrew Thompson 			 */
315602ac6454SAndrew Thompson 		case UDESC_DEVICE_QUALIFIER:
315702ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3158459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
315902ac6454SAndrew Thompson 				goto done;
316002ac6454SAndrew Thompson 			}
3161459d369eSAndrew Thompson 			len = sizeof(ehci_odevd);
3162459d369eSAndrew Thompson 			ptr = (const void *)&ehci_odevd;
316302ac6454SAndrew Thompson 			break;
316402ac6454SAndrew Thompson 
316502ac6454SAndrew Thompson 		case UDESC_CONFIG:
316602ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3167459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
316802ac6454SAndrew Thompson 				goto done;
316902ac6454SAndrew Thompson 			}
3170459d369eSAndrew Thompson 			len = sizeof(ehci_confd);
3171459d369eSAndrew Thompson 			ptr = (const void *)&ehci_confd;
317202ac6454SAndrew Thompson 			break;
317302ac6454SAndrew Thompson 
317402ac6454SAndrew Thompson 		case UDESC_STRING:
317502ac6454SAndrew Thompson 			switch (value & 0xff) {
317602ac6454SAndrew Thompson 			case 0:	/* Language table */
3177459d369eSAndrew Thompson 				str_ptr = "\001";
317802ac6454SAndrew Thompson 				break;
317902ac6454SAndrew Thompson 
318002ac6454SAndrew Thompson 			case 1:	/* Vendor */
3181459d369eSAndrew Thompson 				str_ptr = sc->sc_vendor;
318202ac6454SAndrew Thompson 				break;
318302ac6454SAndrew Thompson 
318402ac6454SAndrew Thompson 			case 2:	/* Product */
3185459d369eSAndrew Thompson 				str_ptr = "EHCI root HUB";
318602ac6454SAndrew Thompson 				break;
318702ac6454SAndrew Thompson 
318802ac6454SAndrew Thompson 			default:
3189459d369eSAndrew Thompson 				str_ptr = "";
319002ac6454SAndrew Thompson 				break;
319102ac6454SAndrew Thompson 			}
319202ac6454SAndrew Thompson 
3193a593f6b8SAndrew Thompson 			len = usb_make_str_desc(
3194459d369eSAndrew Thompson 			    sc->sc_hub_desc.temp,
319502ac6454SAndrew Thompson 			    sizeof(sc->sc_hub_desc.temp),
3196459d369eSAndrew Thompson 			    str_ptr);
319702ac6454SAndrew Thompson 			break;
319802ac6454SAndrew Thompson 		default:
3199459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
320002ac6454SAndrew Thompson 			goto done;
320102ac6454SAndrew Thompson 		}
320202ac6454SAndrew Thompson 		break;
320302ac6454SAndrew Thompson 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3204459d369eSAndrew Thompson 		len = 1;
320502ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = 0;
320602ac6454SAndrew Thompson 		break;
320702ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_DEVICE):
3208459d369eSAndrew Thompson 		len = 2;
320902ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
321002ac6454SAndrew Thompson 		break;
321102ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
321202ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3213459d369eSAndrew Thompson 		len = 2;
321402ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
321502ac6454SAndrew Thompson 		break;
321602ac6454SAndrew Thompson 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3217ab42e8b2SAndrew Thompson 		if (value >= EHCI_MAX_DEVICES) {
3218459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
321902ac6454SAndrew Thompson 			goto done;
322002ac6454SAndrew Thompson 		}
322102ac6454SAndrew Thompson 		sc->sc_addr = value;
322202ac6454SAndrew Thompson 		break;
322302ac6454SAndrew Thompson 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
322402ac6454SAndrew Thompson 		if ((value != 0) && (value != 1)) {
3225459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
322602ac6454SAndrew Thompson 			goto done;
322702ac6454SAndrew Thompson 		}
322802ac6454SAndrew Thompson 		sc->sc_conf = value;
322902ac6454SAndrew Thompson 		break;
323002ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
323102ac6454SAndrew Thompson 		break;
323202ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
323302ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
323402ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3235459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
323602ac6454SAndrew Thompson 		goto done;
323702ac6454SAndrew Thompson 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
323802ac6454SAndrew Thompson 		break;
323902ac6454SAndrew Thompson 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
324002ac6454SAndrew Thompson 		break;
324102ac6454SAndrew Thompson 		/* Hub requests */
324202ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
324302ac6454SAndrew Thompson 		break;
324402ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
324502ac6454SAndrew Thompson 		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
324602ac6454SAndrew Thompson 
324702ac6454SAndrew Thompson 		if ((index < 1) ||
324802ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3249459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
325002ac6454SAndrew Thompson 			goto done;
325102ac6454SAndrew Thompson 		}
325202ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
325302ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
325402ac6454SAndrew Thompson 		switch (value) {
325502ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
325602ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PE);
325702ac6454SAndrew Thompson 			break;
325802ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
325902ac6454SAndrew Thompson 			if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
326002ac6454SAndrew Thompson 
326102ac6454SAndrew Thompson 				/*
326202ac6454SAndrew Thompson 				 * waking up a High Speed device is rather
326302ac6454SAndrew Thompson 				 * complicated if
326402ac6454SAndrew Thompson 				 */
326502ac6454SAndrew Thompson 				EOWRITE4(sc, port, v | EHCI_PS_FPR);
326602ac6454SAndrew Thompson 			}
326702ac6454SAndrew Thompson 			/* wait 20ms for resume sequence to complete */
3268a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
326902ac6454SAndrew Thompson 
327002ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
327102ac6454SAndrew Thompson 			    EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
327202ac6454SAndrew Thompson 
32734a35cda7SAndrew Thompson 			/* 4ms settle time */
3274a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
327502ac6454SAndrew Thompson 			break;
327602ac6454SAndrew Thompson 		case UHF_PORT_POWER:
327702ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PP);
327802ac6454SAndrew Thompson 			break;
327902ac6454SAndrew Thompson 		case UHF_PORT_TEST:
328002ac6454SAndrew Thompson 			DPRINTFN(3, "clear port test "
328102ac6454SAndrew Thompson 			    "%d\n", index);
328202ac6454SAndrew Thompson 			break;
328302ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
328402ac6454SAndrew Thompson 			DPRINTFN(3, "clear port ind "
328502ac6454SAndrew Thompson 			    "%d\n", index);
328602ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
328702ac6454SAndrew Thompson 			break;
328802ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
328902ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
329002ac6454SAndrew Thompson 			break;
329102ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
329202ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
329302ac6454SAndrew Thompson 			break;
329402ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
329502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
329602ac6454SAndrew Thompson 			break;
329702ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
329802ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
329902ac6454SAndrew Thompson 			break;
330002ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
330102ac6454SAndrew Thompson 			sc->sc_isreset = 0;
330202ac6454SAndrew Thompson 			break;
330302ac6454SAndrew Thompson 		default:
3304459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
330502ac6454SAndrew Thompson 			goto done;
330602ac6454SAndrew Thompson 		}
330702ac6454SAndrew Thompson 		break;
330802ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
330902ac6454SAndrew Thompson 		if ((value & 0xff) != 0) {
3310459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
331102ac6454SAndrew Thompson 			goto done;
331202ac6454SAndrew Thompson 		}
3313b494261cSHans Petter Selasky 		v = EREAD4(sc, EHCI_HCSPARAMS);
331402ac6454SAndrew Thompson 
331502ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd = ehci_hubd;
331602ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
331753e0bf6eSHans Petter Selasky 
331853e0bf6eSHans Petter Selasky 		if (EHCI_HCS_PPC(v))
331953e0bf6eSHans Petter Selasky 			i = UHD_PWR_INDIVIDUAL;
332053e0bf6eSHans Petter Selasky 		else
332153e0bf6eSHans Petter Selasky 			i = UHD_PWR_NO_SWITCH;
332253e0bf6eSHans Petter Selasky 
332353e0bf6eSHans Petter Selasky 		if (EHCI_HCS_P_INDICATOR(v))
332453e0bf6eSHans Petter Selasky 			i |= UHD_PORT_IND;
332553e0bf6eSHans Petter Selasky 
332653e0bf6eSHans Petter Selasky 		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
332702ac6454SAndrew Thompson 		/* XXX can't find out? */
332802ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
332953e0bf6eSHans Petter Selasky 		/* XXX don't know if ports are removable or not */
333002ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bDescLength =
333102ac6454SAndrew Thompson 		    8 + ((sc->sc_noport + 7) / 8);
3332459d369eSAndrew Thompson 		len = sc->sc_hub_desc.hubd.bDescLength;
333302ac6454SAndrew Thompson 		break;
333402ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3335459d369eSAndrew Thompson 		len = 16;
3336271ae033SHans Petter Selasky 		memset(sc->sc_hub_desc.temp, 0, 16);
333702ac6454SAndrew Thompson 		break;
333802ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
333902ac6454SAndrew Thompson 		DPRINTFN(9, "get port status i=%d\n",
334002ac6454SAndrew Thompson 		    index);
334102ac6454SAndrew Thompson 		if ((index < 1) ||
334202ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3343459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
334402ac6454SAndrew Thompson 			goto done;
334502ac6454SAndrew Thompson 		}
334602ac6454SAndrew Thompson 		v = EOREAD4(sc, EHCI_PORTSC(index));
334702ac6454SAndrew Thompson 		DPRINTFN(9, "port status=0x%04x\n", v);
3348cdf4ec68SMichal Meloun 		if (sc->sc_flags & EHCI_SCFLG_TT) {
3349cdf4ec68SMichal Meloun 			if (sc->sc_vendor_get_port_speed != NULL) {
3350cdf4ec68SMichal Meloun 				i = sc->sc_vendor_get_port_speed(sc, index);
3351cdf4ec68SMichal Meloun 			} else {
3352cdf4ec68SMichal Meloun 				device_printf(sc->sc_bus.bdev,
3353cdf4ec68SMichal Meloun 				    "EHCI_SCFLG_TT quirk is set but "
3354cdf4ec68SMichal Meloun 				    "sc_vendor_get_hub_speed() is NULL\n");
335502ac6454SAndrew Thompson 				i = UPS_HIGH_SPEED;
3356cdf4ec68SMichal Meloun 			}
335702ac6454SAndrew Thompson 		} else {
335802ac6454SAndrew Thompson 			i = UPS_HIGH_SPEED;
335902ac6454SAndrew Thompson 		}
336002ac6454SAndrew Thompson 		if (v & EHCI_PS_CS)
336102ac6454SAndrew Thompson 			i |= UPS_CURRENT_CONNECT_STATUS;
336202ac6454SAndrew Thompson 		if (v & EHCI_PS_PE)
336302ac6454SAndrew Thompson 			i |= UPS_PORT_ENABLED;
336402ac6454SAndrew Thompson 		if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
336502ac6454SAndrew Thompson 			i |= UPS_SUSPEND;
336602ac6454SAndrew Thompson 		if (v & EHCI_PS_OCA)
336702ac6454SAndrew Thompson 			i |= UPS_OVERCURRENT_INDICATOR;
336802ac6454SAndrew Thompson 		if (v & EHCI_PS_PR)
336902ac6454SAndrew Thompson 			i |= UPS_RESET;
337002ac6454SAndrew Thompson 		if (v & EHCI_PS_PP)
337102ac6454SAndrew Thompson 			i |= UPS_PORT_POWER;
337202ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortStatus, i);
337302ac6454SAndrew Thompson 		i = 0;
337402ac6454SAndrew Thompson 		if (v & EHCI_PS_CSC)
337502ac6454SAndrew Thompson 			i |= UPS_C_CONNECT_STATUS;
337602ac6454SAndrew Thompson 		if (v & EHCI_PS_PEC)
337702ac6454SAndrew Thompson 			i |= UPS_C_PORT_ENABLED;
337802ac6454SAndrew Thompson 		if (v & EHCI_PS_OCC)
337902ac6454SAndrew Thompson 			i |= UPS_C_OVERCURRENT_INDICATOR;
338002ac6454SAndrew Thompson 		if (v & EHCI_PS_FPR)
338102ac6454SAndrew Thompson 			i |= UPS_C_SUSPEND;
338202ac6454SAndrew Thompson 		if (sc->sc_isreset)
338302ac6454SAndrew Thompson 			i |= UPS_C_PORT_RESET;
338402ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortChange, i);
3385459d369eSAndrew Thompson 		len = sizeof(sc->sc_hub_desc.ps);
338602ac6454SAndrew Thompson 		break;
338702ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3388459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
338902ac6454SAndrew Thompson 		goto done;
339002ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
339102ac6454SAndrew Thompson 		break;
339202ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
339302ac6454SAndrew Thompson 		if ((index < 1) ||
339402ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3395459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
339602ac6454SAndrew Thompson 			goto done;
339702ac6454SAndrew Thompson 		}
339802ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
339902ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
340002ac6454SAndrew Thompson 		switch (value) {
340102ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
340202ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PE);
340302ac6454SAndrew Thompson 			break;
340402ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
340502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
340602ac6454SAndrew Thompson 			break;
340702ac6454SAndrew Thompson 		case UHF_PORT_RESET:
340802ac6454SAndrew Thompson 			DPRINTFN(6, "reset port %d\n", index);
3409b850ecc1SAndrew Thompson #ifdef USB_DEBUG
341002ac6454SAndrew Thompson 			if (ehcinohighspeed) {
341102ac6454SAndrew Thompson 				/*
341202ac6454SAndrew Thompson 				 * Connect USB device to companion
341302ac6454SAndrew Thompson 				 * controller.
341402ac6454SAndrew Thompson 				 */
341502ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
341602ac6454SAndrew Thompson 				break;
341702ac6454SAndrew Thompson 			}
341802ac6454SAndrew Thompson #endif
3419e55e1ebcSAndrew Thompson 			if (EHCI_PS_IS_LOWSPEED(v) &&
3420e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
342102ac6454SAndrew Thompson 				/* Low speed device, give up ownership. */
342202ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
342302ac6454SAndrew Thompson 				break;
342402ac6454SAndrew Thompson 			}
342502ac6454SAndrew Thompson 			/* Start reset sequence. */
342602ac6454SAndrew Thompson 			v &= ~(EHCI_PS_PE | EHCI_PS_PR);
342702ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PR);
342802ac6454SAndrew Thompson 
342902ac6454SAndrew Thompson 			/* Wait for reset to complete. */
3430a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
343137506412SHans Petter Selasky 			    USB_MS_TO_TICKS(usb_port_root_reset_delay));
343202ac6454SAndrew Thompson 
343302ac6454SAndrew Thompson 			/* Terminate reset sequence. */
343402ac6454SAndrew Thompson 			if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
343502ac6454SAndrew Thompson 				EOWRITE4(sc, port, v);
343602ac6454SAndrew Thompson 
343702ac6454SAndrew Thompson 			/* Wait for HC to complete reset. */
3438a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
343902ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
344002ac6454SAndrew Thompson 
344102ac6454SAndrew Thompson 			v = EOREAD4(sc, port);
344202ac6454SAndrew Thompson 			DPRINTF("ehci after reset, status=0x%08x\n", v);
344302ac6454SAndrew Thompson 			if (v & EHCI_PS_PR) {
344402ac6454SAndrew Thompson 				device_printf(sc->sc_bus.bdev,
344502ac6454SAndrew Thompson 				    "port reset timeout\n");
3446459d369eSAndrew Thompson 				err = USB_ERR_TIMEOUT;
344702ac6454SAndrew Thompson 				goto done;
344802ac6454SAndrew Thompson 			}
3449e55e1ebcSAndrew Thompson 			if (!(v & EHCI_PS_PE) &&
3450e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3451e55e1ebcSAndrew Thompson 				/* Not a high speed device, give up ownership.*/
345202ac6454SAndrew Thompson 				ehci_disown(sc, index, 0);
345302ac6454SAndrew Thompson 				break;
345402ac6454SAndrew Thompson 			}
345502ac6454SAndrew Thompson 			sc->sc_isreset = 1;
345602ac6454SAndrew Thompson 			DPRINTF("ehci port %d reset, status = 0x%08x\n",
345702ac6454SAndrew Thompson 			    index, v);
345802ac6454SAndrew Thompson 			break;
345902ac6454SAndrew Thompson 
346002ac6454SAndrew Thompson 		case UHF_PORT_POWER:
346102ac6454SAndrew Thompson 			DPRINTFN(3, "set port power %d\n", index);
346202ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PP);
346302ac6454SAndrew Thompson 			break;
346402ac6454SAndrew Thompson 
346502ac6454SAndrew Thompson 		case UHF_PORT_TEST:
346602ac6454SAndrew Thompson 			DPRINTFN(3, "set port test %d\n", index);
346702ac6454SAndrew Thompson 			break;
346802ac6454SAndrew Thompson 
346902ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
347002ac6454SAndrew Thompson 			DPRINTFN(3, "set port ind %d\n", index);
347102ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
347202ac6454SAndrew Thompson 			break;
347302ac6454SAndrew Thompson 
347402ac6454SAndrew Thompson 		default:
3475459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
347602ac6454SAndrew Thompson 			goto done;
347702ac6454SAndrew Thompson 		}
347802ac6454SAndrew Thompson 		break;
347902ac6454SAndrew Thompson 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
348002ac6454SAndrew Thompson 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
348102ac6454SAndrew Thompson 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
348202ac6454SAndrew Thompson 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
348302ac6454SAndrew Thompson 		break;
348402ac6454SAndrew Thompson 	default:
3485459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
348602ac6454SAndrew Thompson 		goto done;
348702ac6454SAndrew Thompson 	}
348802ac6454SAndrew Thompson done:
3489459d369eSAndrew Thompson 	*plength = len;
3490459d369eSAndrew Thompson 	*pptr = ptr;
3491459d369eSAndrew Thompson 	return (err);
349202ac6454SAndrew Thompson }
349302ac6454SAndrew Thompson 
349402ac6454SAndrew Thompson static void
3495760bc48eSAndrew Thompson ehci_xfer_setup(struct usb_setup_params *parm)
349602ac6454SAndrew Thompson {
3497760bc48eSAndrew Thompson 	struct usb_page_search page_info;
3498760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
349902ac6454SAndrew Thompson 	ehci_softc_t *sc;
3500760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
350102ac6454SAndrew Thompson 	void *last_obj;
350202ac6454SAndrew Thompson 	uint32_t nqtd;
350302ac6454SAndrew Thompson 	uint32_t nqh;
350402ac6454SAndrew Thompson 	uint32_t nsitd;
350502ac6454SAndrew Thompson 	uint32_t nitd;
350602ac6454SAndrew Thompson 	uint32_t n;
350702ac6454SAndrew Thompson 
350802ac6454SAndrew Thompson 	sc = EHCI_BUS2SC(parm->udev->bus);
350902ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
351002ac6454SAndrew Thompson 
351102ac6454SAndrew Thompson 	nqtd = 0;
351202ac6454SAndrew Thompson 	nqh = 0;
351302ac6454SAndrew Thompson 	nsitd = 0;
351402ac6454SAndrew Thompson 	nitd = 0;
351502ac6454SAndrew Thompson 
351602ac6454SAndrew Thompson 	/*
351702ac6454SAndrew Thompson 	 * compute maximum number of some structures
351802ac6454SAndrew Thompson 	 */
351902ac6454SAndrew Thompson 	if (parm->methods == &ehci_device_ctrl_methods) {
352002ac6454SAndrew Thompson 
352102ac6454SAndrew Thompson 		/*
352202ac6454SAndrew Thompson 		 * The proof for the "nqtd" formula is illustrated like
352302ac6454SAndrew Thompson 		 * this:
352402ac6454SAndrew Thompson 		 *
352502ac6454SAndrew Thompson 		 * +------------------------------------+
352602ac6454SAndrew Thompson 		 * |                                    |
352702ac6454SAndrew Thompson 		 * |         |remainder ->              |
352802ac6454SAndrew Thompson 		 * |   +-----+---+                      |
352902ac6454SAndrew Thompson 		 * |   | xxx | x | frm 0                |
353002ac6454SAndrew Thompson 		 * |   +-----+---++                     |
353102ac6454SAndrew Thompson 		 * |   | xxx | xx | frm 1               |
353202ac6454SAndrew Thompson 		 * |   +-----+----+                     |
353302ac6454SAndrew Thompson 		 * |            ...                     |
353402ac6454SAndrew Thompson 		 * +------------------------------------+
353502ac6454SAndrew Thompson 		 *
353602ac6454SAndrew Thompson 		 * "xxx" means a completely full USB transfer descriptor
353702ac6454SAndrew Thompson 		 *
353802ac6454SAndrew Thompson 		 * "x" and "xx" means a short USB packet
353902ac6454SAndrew Thompson 		 *
354002ac6454SAndrew Thompson 		 * For the remainder of an USB transfer modulo
354102ac6454SAndrew Thompson 		 * "max_data_length" we need two USB transfer descriptors.
354202ac6454SAndrew Thompson 		 * One to transfer the remaining data and one to finalise
354302ac6454SAndrew Thompson 		 * with a zero length packet in case the "force_short_xfer"
354402ac6454SAndrew Thompson 		 * flag is set. We only need two USB transfer descriptors in
354502ac6454SAndrew Thompson 		 * the case where the transfer length of the first one is a
354602ac6454SAndrew Thompson 		 * factor of "max_frame_size". The rest of the needed USB
354702ac6454SAndrew Thompson 		 * transfer descriptors is given by the buffer size divided
354802ac6454SAndrew Thompson 		 * by the maximum data payload.
354902ac6454SAndrew Thompson 		 */
355002ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
355102ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
355202ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
355302ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
355402ac6454SAndrew Thompson 
3555a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
355602ac6454SAndrew Thompson 
355702ac6454SAndrew Thompson 		nqh = 1;
355802ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes) + 1	/* STATUS */
3559578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
356002ac6454SAndrew Thompson 
356102ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_bulk_methods) {
356202ac6454SAndrew Thompson 
356302ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
356402ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
356502ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
356602ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
356702ac6454SAndrew Thompson 
3568a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
356902ac6454SAndrew Thompson 
357002ac6454SAndrew Thompson 		nqh = 1;
357102ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3572578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
357302ac6454SAndrew Thompson 
357402ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_intr_methods) {
357502ac6454SAndrew Thompson 
357602ac6454SAndrew Thompson 		if (parm->speed == USB_SPEED_HIGH) {
357702ac6454SAndrew Thompson 			parm->hc_max_packet_size = 0x400;
357802ac6454SAndrew Thompson 			parm->hc_max_packet_count = 3;
357902ac6454SAndrew Thompson 		} else if (parm->speed == USB_SPEED_FULL) {
358002ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
358102ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
358202ac6454SAndrew Thompson 		} else {
358302ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
358402ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
358502ac6454SAndrew Thompson 		}
358602ac6454SAndrew Thompson 
358702ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
358802ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
358902ac6454SAndrew Thompson 
3590a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
359102ac6454SAndrew Thompson 
359202ac6454SAndrew Thompson 		nqh = 1;
359302ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3594578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
359502ac6454SAndrew Thompson 
359602ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_fs_methods) {
359702ac6454SAndrew Thompson 
359802ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x3FF;
359902ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
360002ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x3FF;
360102ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
360202ac6454SAndrew Thompson 
3603a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
360402ac6454SAndrew Thompson 
360502ac6454SAndrew Thompson 		nsitd = xfer->nframes;
360602ac6454SAndrew Thompson 
360702ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_hs_methods) {
360802ac6454SAndrew Thompson 
360902ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
361002ac6454SAndrew Thompson 		parm->hc_max_packet_count = 3;
361102ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0xC00;
361202ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
361302ac6454SAndrew Thompson 
3614a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
361502ac6454SAndrew Thompson 
3616883bb300SAndrew Thompson 		nitd = ((xfer->nframes + 7) / 8) <<
3617883bb300SAndrew Thompson 		    usbd_xfer_get_fps_shift(xfer);
361802ac6454SAndrew Thompson 
361902ac6454SAndrew Thompson 	} else {
362002ac6454SAndrew Thompson 
362102ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
362202ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
362302ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x400;
362402ac6454SAndrew Thompson 
3625a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
362602ac6454SAndrew Thompson 	}
362702ac6454SAndrew Thompson 
362802ac6454SAndrew Thompson alloc_dma_set:
362902ac6454SAndrew Thompson 
363002ac6454SAndrew Thompson 	if (parm->err) {
363102ac6454SAndrew Thompson 		return;
363202ac6454SAndrew Thompson 	}
363302ac6454SAndrew Thompson 	/*
363402ac6454SAndrew Thompson 	 * Allocate queue heads and transfer descriptors
363502ac6454SAndrew Thompson 	 */
363602ac6454SAndrew Thompson 	last_obj = NULL;
363702ac6454SAndrew Thompson 
3638a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
363902ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_itd_t),
364002ac6454SAndrew Thompson 	    EHCI_ITD_ALIGN, nitd)) {
364102ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
364202ac6454SAndrew Thompson 		return;
364302ac6454SAndrew Thompson 	}
364402ac6454SAndrew Thompson 	if (parm->buf) {
364502ac6454SAndrew Thompson 		for (n = 0; n != nitd; n++) {
364602ac6454SAndrew Thompson 			ehci_itd_t *td;
364702ac6454SAndrew Thompson 
3648a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
364902ac6454SAndrew Thompson 
365002ac6454SAndrew Thompson 			td = page_info.buffer;
365102ac6454SAndrew Thompson 
365202ac6454SAndrew Thompson 			/* init TD */
3653e55e1ebcSAndrew Thompson 			td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
365402ac6454SAndrew Thompson 			td->obj_next = last_obj;
365502ac6454SAndrew Thompson 			td->page_cache = pc + n;
365602ac6454SAndrew Thompson 
365702ac6454SAndrew Thompson 			last_obj = td;
365802ac6454SAndrew Thompson 
3659a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
366002ac6454SAndrew Thompson 		}
366102ac6454SAndrew Thompson 	}
3662a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
366302ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_sitd_t),
366402ac6454SAndrew Thompson 	    EHCI_SITD_ALIGN, nsitd)) {
366502ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
366602ac6454SAndrew Thompson 		return;
366702ac6454SAndrew Thompson 	}
366802ac6454SAndrew Thompson 	if (parm->buf) {
366902ac6454SAndrew Thompson 		for (n = 0; n != nsitd; n++) {
367002ac6454SAndrew Thompson 			ehci_sitd_t *td;
367102ac6454SAndrew Thompson 
3672a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
367302ac6454SAndrew Thompson 
367402ac6454SAndrew Thompson 			td = page_info.buffer;
367502ac6454SAndrew Thompson 
367602ac6454SAndrew Thompson 			/* init TD */
3677e55e1ebcSAndrew Thompson 			td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
367802ac6454SAndrew Thompson 			td->obj_next = last_obj;
367902ac6454SAndrew Thompson 			td->page_cache = pc + n;
368002ac6454SAndrew Thompson 
368102ac6454SAndrew Thompson 			last_obj = td;
368202ac6454SAndrew Thompson 
3683a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
368402ac6454SAndrew Thompson 		}
368502ac6454SAndrew Thompson 	}
3686a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
368702ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qtd_t),
368802ac6454SAndrew Thompson 	    EHCI_QTD_ALIGN, nqtd)) {
368902ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
369002ac6454SAndrew Thompson 		return;
369102ac6454SAndrew Thompson 	}
369202ac6454SAndrew Thompson 	if (parm->buf) {
369302ac6454SAndrew Thompson 		for (n = 0; n != nqtd; n++) {
369402ac6454SAndrew Thompson 			ehci_qtd_t *qtd;
369502ac6454SAndrew Thompson 
3696a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
369702ac6454SAndrew Thompson 
369802ac6454SAndrew Thompson 			qtd = page_info.buffer;
369902ac6454SAndrew Thompson 
370002ac6454SAndrew Thompson 			/* init TD */
3701e55e1ebcSAndrew Thompson 			qtd->qtd_self = htohc32(sc, page_info.physaddr);
370202ac6454SAndrew Thompson 			qtd->obj_next = last_obj;
370302ac6454SAndrew Thompson 			qtd->page_cache = pc + n;
370402ac6454SAndrew Thompson 
370502ac6454SAndrew Thompson 			last_obj = qtd;
370602ac6454SAndrew Thompson 
3707a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
370802ac6454SAndrew Thompson 		}
370902ac6454SAndrew Thompson 	}
371002ac6454SAndrew Thompson 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
371102ac6454SAndrew Thompson 
371202ac6454SAndrew Thompson 	last_obj = NULL;
371302ac6454SAndrew Thompson 
3714a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
371502ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qh_t),
371602ac6454SAndrew Thompson 	    EHCI_QH_ALIGN, nqh)) {
371702ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
371802ac6454SAndrew Thompson 		return;
371902ac6454SAndrew Thompson 	}
372002ac6454SAndrew Thompson 	if (parm->buf) {
372102ac6454SAndrew Thompson 		for (n = 0; n != nqh; n++) {
372202ac6454SAndrew Thompson 			ehci_qh_t *qh;
372302ac6454SAndrew Thompson 
3724a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
372502ac6454SAndrew Thompson 
372602ac6454SAndrew Thompson 			qh = page_info.buffer;
372702ac6454SAndrew Thompson 
372802ac6454SAndrew Thompson 			/* init QH */
3729e55e1ebcSAndrew Thompson 			qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
373002ac6454SAndrew Thompson 			qh->obj_next = last_obj;
373102ac6454SAndrew Thompson 			qh->page_cache = pc + n;
373202ac6454SAndrew Thompson 
373302ac6454SAndrew Thompson 			last_obj = qh;
373402ac6454SAndrew Thompson 
3735a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
373602ac6454SAndrew Thompson 		}
373702ac6454SAndrew Thompson 	}
373802ac6454SAndrew Thompson 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
373902ac6454SAndrew Thompson 
374002ac6454SAndrew Thompson 	if (!xfer->flags_int.curr_dma_set) {
374102ac6454SAndrew Thompson 		xfer->flags_int.curr_dma_set = 1;
374202ac6454SAndrew Thompson 		goto alloc_dma_set;
374302ac6454SAndrew Thompson 	}
374402ac6454SAndrew Thompson }
374502ac6454SAndrew Thompson 
374602ac6454SAndrew Thompson static void
3747760bc48eSAndrew Thompson ehci_xfer_unsetup(struct usb_xfer *xfer)
374802ac6454SAndrew Thompson {
374902ac6454SAndrew Thompson 	return;
375002ac6454SAndrew Thompson }
375102ac6454SAndrew Thompson 
375202ac6454SAndrew Thompson static void
3753ae60fdfbSAndrew Thompson ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3754ae60fdfbSAndrew Thompson     struct usb_endpoint *ep)
375502ac6454SAndrew Thompson {
375602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
375702ac6454SAndrew Thompson 
3758ae60fdfbSAndrew Thompson 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3759ae60fdfbSAndrew Thompson 	    ep, udev->address,
3760f29a0724SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb_mode,
376102ac6454SAndrew Thompson 	    sc->sc_addr);
376202ac6454SAndrew Thompson 
376339307315SAndrew Thompson 	if (udev->device_index != sc->sc_addr) {
376439307315SAndrew Thompson 
376502ac6454SAndrew Thompson 		if ((udev->speed != USB_SPEED_HIGH) &&
376602ac6454SAndrew Thompson 		    ((udev->hs_hub_addr == 0) ||
376702ac6454SAndrew Thompson 		    (udev->hs_port_no == 0) ||
3768495f25ceSAndrew Thompson 		    (udev->parent_hs_hub == NULL) ||
3769495f25ceSAndrew Thompson 		    (udev->parent_hs_hub->hub == NULL))) {
377002ac6454SAndrew Thompson 			/* We need a transaction translator */
377102ac6454SAndrew Thompson 			goto done;
377202ac6454SAndrew Thompson 		}
377302ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
377402ac6454SAndrew Thompson 		case UE_CONTROL:
3775ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_ctrl_methods;
377602ac6454SAndrew Thompson 			break;
377702ac6454SAndrew Thompson 		case UE_INTERRUPT:
3778ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_intr_methods;
377902ac6454SAndrew Thompson 			break;
378002ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
378102ac6454SAndrew Thompson 			if (udev->speed == USB_SPEED_HIGH) {
3782ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_hs_methods;
378302ac6454SAndrew Thompson 			} else if (udev->speed == USB_SPEED_FULL) {
3784ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_fs_methods;
378502ac6454SAndrew Thompson 			}
378602ac6454SAndrew Thompson 			break;
378702ac6454SAndrew Thompson 		case UE_BULK:
3788ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_bulk_methods;
378902ac6454SAndrew Thompson 			break;
379002ac6454SAndrew Thompson 		default:
379102ac6454SAndrew Thompson 			/* do nothing */
379202ac6454SAndrew Thompson 			break;
379302ac6454SAndrew Thompson 		}
379402ac6454SAndrew Thompson 	}
379502ac6454SAndrew Thompson done:
379602ac6454SAndrew Thompson 	return;
379702ac6454SAndrew Thompson }
379802ac6454SAndrew Thompson 
379902ac6454SAndrew Thompson static void
3800527aa7b2SAndrew Thompson ehci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
380102ac6454SAndrew Thompson {
380202ac6454SAndrew Thompson 	/*
380302ac6454SAndrew Thompson 	 * Wait until the hardware has finished any possible use of
380402ac6454SAndrew Thompson 	 * the transfer descriptor(s) and QH
380502ac6454SAndrew Thompson 	 */
380634485480SHans Petter Selasky 	*pus = (1125);			/* microseconds */
380702ac6454SAndrew Thompson }
380802ac6454SAndrew Thompson 
380902ac6454SAndrew Thompson static void
3810760bc48eSAndrew Thompson ehci_device_resume(struct usb_device *udev)
381102ac6454SAndrew Thompson {
381202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3813760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3814e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
381502ac6454SAndrew Thompson 
381602ac6454SAndrew Thompson 	DPRINTF("\n");
381702ac6454SAndrew Thompson 
381802ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
381902ac6454SAndrew Thompson 
382002ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
382102ac6454SAndrew Thompson 
382202ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
382302ac6454SAndrew Thompson 
3824ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
382502ac6454SAndrew Thompson 
382602ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
382702ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
382802ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
382902ac6454SAndrew Thompson 				    sc->sc_async_p_last);
383002ac6454SAndrew Thompson 			}
383102ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
383202ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
383302ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
383402ac6454SAndrew Thompson 			}
383502ac6454SAndrew Thompson 		}
383602ac6454SAndrew Thompson 	}
383702ac6454SAndrew Thompson 
383802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
383902ac6454SAndrew Thompson 
384002ac6454SAndrew Thompson 	return;
384102ac6454SAndrew Thompson }
384202ac6454SAndrew Thompson 
384302ac6454SAndrew Thompson static void
3844760bc48eSAndrew Thompson ehci_device_suspend(struct usb_device *udev)
384502ac6454SAndrew Thompson {
384602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3847760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3848e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
384902ac6454SAndrew Thompson 
385002ac6454SAndrew Thompson 	DPRINTF("\n");
385102ac6454SAndrew Thompson 
385202ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
385302ac6454SAndrew Thompson 
385402ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
385502ac6454SAndrew Thompson 
385602ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
385702ac6454SAndrew Thompson 
3858ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
385902ac6454SAndrew Thompson 
386002ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
386102ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
386202ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
386302ac6454SAndrew Thompson 				    sc->sc_async_p_last);
386402ac6454SAndrew Thompson 			}
386502ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
386602ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
386702ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
386802ac6454SAndrew Thompson 			}
386902ac6454SAndrew Thompson 		}
387002ac6454SAndrew Thompson 	}
387102ac6454SAndrew Thompson 
387202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
38732e141748SHans Petter Selasky }
387402ac6454SAndrew Thompson 
38752e141748SHans Petter Selasky static void
38762e141748SHans Petter Selasky ehci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
38772e141748SHans Petter Selasky {
38782e141748SHans Petter Selasky 	struct ehci_softc *sc = EHCI_BUS2SC(bus);
38792e141748SHans Petter Selasky 
38802e141748SHans Petter Selasky 	switch (state) {
38812e141748SHans Petter Selasky 	case USB_HW_POWER_SUSPEND:
38822e141748SHans Petter Selasky 	case USB_HW_POWER_SHUTDOWN:
38832e141748SHans Petter Selasky 		ehci_suspend(sc);
38842e141748SHans Petter Selasky 		break;
38852e141748SHans Petter Selasky 	case USB_HW_POWER_RESUME:
38862e141748SHans Petter Selasky 		ehci_resume(sc);
38872e141748SHans Petter Selasky 		break;
38882e141748SHans Petter Selasky 	default:
38892e141748SHans Petter Selasky 		break;
38902e141748SHans Petter Selasky 	}
389102ac6454SAndrew Thompson }
389202ac6454SAndrew Thompson 
389302ac6454SAndrew Thompson static void
3894760bc48eSAndrew Thompson ehci_set_hw_power(struct usb_bus *bus)
389502ac6454SAndrew Thompson {
389602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
389702ac6454SAndrew Thompson 	uint32_t temp;
389802ac6454SAndrew Thompson 	uint32_t flags;
389902ac6454SAndrew Thompson 
390002ac6454SAndrew Thompson 	DPRINTF("\n");
390102ac6454SAndrew Thompson 
390202ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
390302ac6454SAndrew Thompson 
390402ac6454SAndrew Thompson 	flags = bus->hw_power_state;
390502ac6454SAndrew Thompson 
390602ac6454SAndrew Thompson 	temp = EOREAD4(sc, EHCI_USBCMD);
390702ac6454SAndrew Thompson 
390802ac6454SAndrew Thompson 	temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
390902ac6454SAndrew Thompson 
391002ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_CONTROL |
391102ac6454SAndrew Thompson 	    USB_HW_POWER_BULK)) {
391202ac6454SAndrew Thompson 		DPRINTF("Async is active\n");
391302ac6454SAndrew Thompson 		temp |= EHCI_CMD_ASE;
391402ac6454SAndrew Thompson 	}
391502ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_INTERRUPT |
391602ac6454SAndrew Thompson 	    USB_HW_POWER_ISOC)) {
391702ac6454SAndrew Thompson 		DPRINTF("Periodic is active\n");
391802ac6454SAndrew Thompson 		temp |= EHCI_CMD_PSE;
391902ac6454SAndrew Thompson 	}
392002ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, temp);
392102ac6454SAndrew Thompson 
392202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
392302ac6454SAndrew Thompson 
392402ac6454SAndrew Thompson 	return;
392502ac6454SAndrew Thompson }
392602ac6454SAndrew Thompson 
39274a6af125SHans Petter Selasky static void
39284a6af125SHans Petter Selasky ehci_start_dma_delay_second(struct usb_xfer *xfer)
39294a6af125SHans Petter Selasky {
39304a6af125SHans Petter Selasky 	struct ehci_softc *sc = EHCI_BUS2SC(xfer->xroot->bus);
39314a6af125SHans Petter Selasky 
39324a6af125SHans Petter Selasky 	DPRINTF("\n");
39334a6af125SHans Petter Selasky 
39344a6af125SHans Petter Selasky 	/* trigger doorbell */
39354a6af125SHans Petter Selasky 	ehci_doorbell_async(sc);
39364a6af125SHans Petter Selasky 
39374a6af125SHans Petter Selasky 	/* give the doorbell 4ms */
39384a6af125SHans Petter Selasky 	usbd_transfer_timeout_ms(xfer,
39394a6af125SHans Petter Selasky 	    (void (*)(void *))&usb_dma_delay_done_cb, 4);
39404a6af125SHans Petter Selasky }
39414a6af125SHans Petter Selasky 
39424a6af125SHans Petter Selasky /*
39434a6af125SHans Petter Selasky  * Ring the doorbell twice before freeing any DMA descriptors. Some host
39444a6af125SHans Petter Selasky  * controllers apparently cache the QH descriptors and need a message
39454a6af125SHans Petter Selasky  * that the cache needs to be discarded.
39464a6af125SHans Petter Selasky  */
39474a6af125SHans Petter Selasky static void
39484a6af125SHans Petter Selasky ehci_start_dma_delay(struct usb_xfer *xfer)
39494a6af125SHans Petter Selasky {
39504a6af125SHans Petter Selasky 	struct ehci_softc *sc = EHCI_BUS2SC(xfer->xroot->bus);
39514a6af125SHans Petter Selasky 
39524a6af125SHans Petter Selasky 	DPRINTF("\n");
39534a6af125SHans Petter Selasky 
39544a6af125SHans Petter Selasky 	/* trigger doorbell */
39554a6af125SHans Petter Selasky 	ehci_doorbell_async(sc);
39564a6af125SHans Petter Selasky 
39574a6af125SHans Petter Selasky 	/* give the doorbell 4ms */
39584a6af125SHans Petter Selasky 	usbd_transfer_timeout_ms(xfer,
39594a6af125SHans Petter Selasky 	    (void (*)(void *))&ehci_start_dma_delay_second, 4);
39604a6af125SHans Petter Selasky }
39614a6af125SHans Petter Selasky 
3962e892b3feSHans Petter Selasky static const struct usb_bus_methods ehci_bus_methods =
396302ac6454SAndrew Thompson {
3964ae60fdfbSAndrew Thompson 	.endpoint_init = ehci_ep_init,
396502ac6454SAndrew Thompson 	.xfer_setup = ehci_xfer_setup,
396602ac6454SAndrew Thompson 	.xfer_unsetup = ehci_xfer_unsetup,
396702ac6454SAndrew Thompson 	.get_dma_delay = ehci_get_dma_delay,
396802ac6454SAndrew Thompson 	.device_resume = ehci_device_resume,
396902ac6454SAndrew Thompson 	.device_suspend = ehci_device_suspend,
397002ac6454SAndrew Thompson 	.set_hw_power = ehci_set_hw_power,
39712e141748SHans Petter Selasky 	.set_hw_power_sleep = ehci_set_hw_power_sleep,
397239307315SAndrew Thompson 	.roothub_exec = ehci_roothub_exec,
3973dddb25f9SAlfred Perlstein 	.xfer_poll = ehci_do_poll,
39744a6af125SHans Petter Selasky 	.start_dma_delay = ehci_start_dma_delay,
397502ac6454SAndrew Thompson };
3976