xref: /freebsd/sys/dev/usb/controller/ehci.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
502ac6454SAndrew Thompson  * Copyright (c) 2004 The NetBSD Foundation, Inc. All rights reserved.
602ac6454SAndrew Thompson  * Copyright (c) 2004 Lennart Augustsson. All rights reserved.
702ac6454SAndrew Thompson  * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
802ac6454SAndrew Thompson  *
902ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1002ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1102ac6454SAndrew Thompson  * are met:
1202ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1402ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1602ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1702ac6454SAndrew Thompson  *
1802ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1902ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2002ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2102ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2202ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2302ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2402ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2502ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2602ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2702ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2802ac6454SAndrew Thompson  * SUCH DAMAGE.
2902ac6454SAndrew Thompson  */
3002ac6454SAndrew Thompson 
3102ac6454SAndrew Thompson /*
3202ac6454SAndrew Thompson  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
3302ac6454SAndrew Thompson  *
3402ac6454SAndrew Thompson  * The EHCI 0.96 spec can be found at
3502ac6454SAndrew Thompson  * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
3602ac6454SAndrew Thompson  * The EHCI 1.0 spec can be found at
3702ac6454SAndrew Thompson  * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
3802ac6454SAndrew Thompson  * and the USB 2.0 spec at
3902ac6454SAndrew Thompson  * http://www.usb.org/developers/docs/usb_20.zip
4002ac6454SAndrew Thompson  *
4102ac6454SAndrew Thompson  */
4202ac6454SAndrew Thompson 
4302ac6454SAndrew Thompson /*
4402ac6454SAndrew Thompson  * TODO:
4502ac6454SAndrew Thompson  * 1) command failures are not recovered correctly
4602ac6454SAndrew Thompson  */
4702ac6454SAndrew Thompson 
48d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
49d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
50d2b99310SHans Petter Selasky #else
51ed6d949aSAndrew Thompson #include <sys/stdint.h>
52ed6d949aSAndrew Thompson #include <sys/stddef.h>
53ed6d949aSAndrew Thompson #include <sys/param.h>
54ed6d949aSAndrew Thompson #include <sys/queue.h>
55ed6d949aSAndrew Thompson #include <sys/types.h>
56ed6d949aSAndrew Thompson #include <sys/systm.h>
57ed6d949aSAndrew Thompson #include <sys/kernel.h>
58ed6d949aSAndrew Thompson #include <sys/bus.h>
59ed6d949aSAndrew Thompson #include <sys/module.h>
60ed6d949aSAndrew Thompson #include <sys/lock.h>
61ed6d949aSAndrew Thompson #include <sys/mutex.h>
62ed6d949aSAndrew Thompson #include <sys/condvar.h>
63ed6d949aSAndrew Thompson #include <sys/sysctl.h>
64ed6d949aSAndrew Thompson #include <sys/sx.h>
65ed6d949aSAndrew Thompson #include <sys/unistd.h>
66ed6d949aSAndrew Thompson #include <sys/callout.h>
67ed6d949aSAndrew Thompson #include <sys/malloc.h>
68ed6d949aSAndrew Thompson #include <sys/priv.h>
69ed6d949aSAndrew Thompson 
7002ac6454SAndrew Thompson #include <dev/usb/usb.h>
71ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #define	USB_DEBUG_VAR ehcidebug
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
7602ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
7802ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7902ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
8002ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
8102ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
8202ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
8302ac6454SAndrew Thompson 
8402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
8502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
86d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
87d2b99310SHans Petter Selasky 
8802ac6454SAndrew Thompson #include <dev/usb/controller/ehci.h>
891def609aSAndrew Thompson #include <dev/usb/controller/ehcireg.h>
9002ac6454SAndrew Thompson 
91578d0effSAndrew Thompson #define	EHCI_BUS2SC(bus) \
9252ab576dSJohn Baldwin 	__containerof(bus, ehci_softc_t, sc_bus)
9302ac6454SAndrew Thompson 
94b850ecc1SAndrew Thompson #ifdef USB_DEBUG
9502ac6454SAndrew Thompson static int ehcidebug = 0;
9602ac6454SAndrew Thompson static int ehcinohighspeed = 0;
97cf223a88SAndrew Thompson static int ehciiaadbug = 0;
98cf223a88SAndrew Thompson static int ehcilostintrbug = 0;
9902ac6454SAndrew Thompson 
100f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
101f8d2b1f3SPawel Biernacki     "USB ehci");
102af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RWTUN,
10302ac6454SAndrew Thompson     &ehcidebug, 0, "Debug level");
104af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RWTUN,
10583cadd7dSHans Petter Selasky     &ehcinohighspeed, 0, "Disable High Speed USB");
106af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, iaadbug, CTLFLAG_RWTUN,
10783cadd7dSHans Petter Selasky     &ehciiaadbug, 0, "Enable doorbell bug workaround");
108af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ehci, OID_AUTO, lostintrbug, CTLFLAG_RWTUN,
10983cadd7dSHans Petter Selasky     &ehcilostintrbug, 0, "Enable lost interrupt bug workaround");
11083cadd7dSHans Petter Selasky 
11102ac6454SAndrew Thompson static void ehci_dump_regs(ehci_softc_t *sc);
11202ac6454SAndrew Thompson static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
11302ac6454SAndrew Thompson 
11402ac6454SAndrew Thompson #endif
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson #define	EHCI_INTR_ENDPT 1
11702ac6454SAndrew Thompson 
118e892b3feSHans Petter Selasky static const struct usb_bus_methods ehci_bus_methods;
119e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_bulk_methods;
120e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_ctrl_methods;
121e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_intr_methods;
122e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_fs_methods;
123e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_hs_methods;
12402ac6454SAndrew Thompson 
12530b22abeSAndrew Thompson static void ehci_do_poll(struct usb_bus *);
12630b22abeSAndrew Thompson static void ehci_device_done(struct usb_xfer *, usb_error_t);
12730b22abeSAndrew Thompson static uint8_t ehci_check_transfer(struct usb_xfer *);
12830b22abeSAndrew Thompson static void ehci_timeout(void *);
12930b22abeSAndrew Thompson static void ehci_poll_timeout(void *);
13030b22abeSAndrew Thompson 
13139307315SAndrew Thompson static void ehci_root_intr(ehci_softc_t *sc);
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson struct ehci_std_temp {
13402ac6454SAndrew Thompson 	ehci_softc_t *sc;
135760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
13602ac6454SAndrew Thompson 	ehci_qtd_t *td;
13702ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
13802ac6454SAndrew Thompson 	uint32_t average;
13902ac6454SAndrew Thompson 	uint32_t qtd_status;
14002ac6454SAndrew Thompson 	uint32_t len;
14102ac6454SAndrew Thompson 	uint16_t max_frame_size;
14202ac6454SAndrew Thompson 	uint8_t	shortpkt;
14302ac6454SAndrew Thompson 	uint8_t	auto_data_toggle;
14402ac6454SAndrew Thompson 	uint8_t	setup_alt_next;
1450f7d4548SAndrew Thompson 	uint8_t	last_frame;
14602ac6454SAndrew Thompson };
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson void
ehci_iterate_hw_softc(struct usb_bus * bus,usb_bus_mem_sub_cb_t * cb)149e0a69b51SAndrew Thompson ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
15002ac6454SAndrew Thompson {
15102ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
15202ac6454SAndrew Thompson 	uint32_t i;
15302ac6454SAndrew Thompson 
15402ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
15502ac6454SAndrew Thompson 	    sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
15602ac6454SAndrew Thompson 
15753e0bf6eSHans Petter Selasky 	cb(bus, &sc->sc_hw.terminate_pc, &sc->sc_hw.terminate_pg,
15853e0bf6eSHans Petter Selasky 	    sizeof(struct ehci_qh_sub), EHCI_QH_ALIGN);
15953e0bf6eSHans Petter Selasky 
16002ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
16102ac6454SAndrew Thompson 	    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
16402ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.intr_start_pc + i,
16502ac6454SAndrew Thompson 		    sc->sc_hw.intr_start_pg + i,
16602ac6454SAndrew Thompson 		    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
16702ac6454SAndrew Thompson 	}
16802ac6454SAndrew Thompson 
16902ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
17002ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
17102ac6454SAndrew Thompson 		    sc->sc_hw.isoc_hs_start_pg + i,
17202ac6454SAndrew Thompson 		    sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
17302ac6454SAndrew Thompson 	}
17402ac6454SAndrew Thompson 
17502ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
17602ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
17702ac6454SAndrew Thompson 		    sc->sc_hw.isoc_fs_start_pg + i,
17802ac6454SAndrew Thompson 		    sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
17902ac6454SAndrew Thompson 	}
18002ac6454SAndrew Thompson }
18102ac6454SAndrew Thompson 
182e0a69b51SAndrew Thompson usb_error_t
ehci_reset(ehci_softc_t * sc)183e55e1ebcSAndrew Thompson ehci_reset(ehci_softc_t *sc)
18402ac6454SAndrew Thompson {
18502ac6454SAndrew Thompson 	uint32_t hcr;
186e55e1ebcSAndrew Thompson 	int i;
187e55e1ebcSAndrew Thompson 
188e55e1ebcSAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
189e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
1902e141748SHans Petter Selasky 		usb_pause_mtx(NULL, hz / 128);
191e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
192e55e1ebcSAndrew Thompson 		if (!hcr) {
193cdf4ec68SMichal Meloun 			if (sc->sc_vendor_post_reset != NULL)
194cdf4ec68SMichal Meloun 				sc->sc_vendor_post_reset(sc);
195e55e1ebcSAndrew Thompson 			return (0);
196e55e1ebcSAndrew Thompson 		}
197e55e1ebcSAndrew Thompson 	}
1987ae432c0SNick Hibma 	device_printf(sc->sc_bus.bdev, "reset timeout\n");
199e55e1ebcSAndrew Thompson 	return (USB_ERR_IOERROR);
200e55e1ebcSAndrew Thompson }
201e55e1ebcSAndrew Thompson 
202e0a69b51SAndrew Thompson static usb_error_t
ehci_hcreset(ehci_softc_t * sc)203e55e1ebcSAndrew Thompson ehci_hcreset(ehci_softc_t *sc)
204e55e1ebcSAndrew Thompson {
205e55e1ebcSAndrew Thompson 	uint32_t hcr;
206e55e1ebcSAndrew Thompson 	int i;
20702ac6454SAndrew Thompson 
20802ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
209e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
2102e141748SHans Petter Selasky 		usb_pause_mtx(NULL, hz / 128);
211e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
212e55e1ebcSAndrew Thompson 		if (hcr)
21302ac6454SAndrew Thompson 			break;
21402ac6454SAndrew Thompson 	}
215e55e1ebcSAndrew Thompson 	if (!hcr)
21602ac6454SAndrew Thompson 		/*
21702ac6454SAndrew Thompson                  * Fall through and try reset anyway even though
21802ac6454SAndrew Thompson                  * Table 2-9 in the EHCI spec says this will result
21902ac6454SAndrew Thompson                  * in undefined behavior.
22002ac6454SAndrew Thompson                  */
221e55e1ebcSAndrew Thompson 		device_printf(sc->sc_bus.bdev, "stop timeout\n");
22202ac6454SAndrew Thompson 
2232e141748SHans Petter Selasky 	return (ehci_reset(sc));
2242e141748SHans Petter Selasky }
2252e141748SHans Petter Selasky 
2262e141748SHans Petter Selasky static int
ehci_init_sub(struct ehci_softc * sc)2272e141748SHans Petter Selasky ehci_init_sub(struct ehci_softc *sc)
2282e141748SHans Petter Selasky {
2292e141748SHans Petter Selasky 	struct usb_page_search buf_res;
2302e141748SHans Petter Selasky 	uint32_t cparams;
2312e141748SHans Petter Selasky   	uint32_t hcr;
2322e141748SHans Petter Selasky 	uint8_t i;
2332e141748SHans Petter Selasky 
2342e141748SHans Petter Selasky 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
2352e141748SHans Petter Selasky 
2362e141748SHans Petter Selasky 	DPRINTF("cparams=0x%x\n", cparams);
2372e141748SHans Petter Selasky 
2382e141748SHans Petter Selasky 	if (EHCI_HCC_64BIT(cparams)) {
2392e141748SHans Petter Selasky 		DPRINTF("HCC uses 64-bit structures\n");
2402e141748SHans Petter Selasky 
2412e141748SHans Petter Selasky 		/* MUST clear segment register if 64 bit capable */
242fcd51bb4SHans Petter Selasky 		EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
2432e141748SHans Petter Selasky 	}
2442e141748SHans Petter Selasky 
2452e141748SHans Petter Selasky 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
2462e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
2472e141748SHans Petter Selasky 
2482e141748SHans Petter Selasky 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
2492e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
2502e141748SHans Petter Selasky 
2512e141748SHans Petter Selasky 	/* enable interrupts */
2522e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
2532e141748SHans Petter Selasky 
2542e141748SHans Petter Selasky 	/* turn on controller */
2552e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_USBCMD,
2562e141748SHans Petter Selasky 	    EHCI_CMD_ITC_1 |		/* 1 microframes interrupt delay */
2572e141748SHans Petter Selasky 	    (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
2582e141748SHans Petter Selasky 	    EHCI_CMD_ASE |
2592e141748SHans Petter Selasky 	    EHCI_CMD_PSE |
2602e141748SHans Petter Selasky 	    EHCI_CMD_RS);
2612e141748SHans Petter Selasky 
2622e141748SHans Petter Selasky 	/* Take over port ownership */
2632e141748SHans Petter Selasky 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
2642e141748SHans Petter Selasky 
2652e141748SHans Petter Selasky 	for (i = 0; i < 100; i++) {
2662e141748SHans Petter Selasky 		usb_pause_mtx(NULL, hz / 128);
2672e141748SHans Petter Selasky 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
2682e141748SHans Petter Selasky 		if (!hcr) {
2692e141748SHans Petter Selasky 			break;
2702e141748SHans Petter Selasky 		}
2712e141748SHans Petter Selasky 	}
2722e141748SHans Petter Selasky 	if (hcr) {
2737ae432c0SNick Hibma 		device_printf(sc->sc_bus.bdev, "run timeout\n");
2742e141748SHans Petter Selasky 		return (USB_ERR_IOERROR);
2752e141748SHans Petter Selasky 	}
2762e141748SHans Petter Selasky 	return (USB_ERR_NORMAL_COMPLETION);
27702ac6454SAndrew Thompson }
27802ac6454SAndrew Thompson 
279e0a69b51SAndrew Thompson usb_error_t
ehci_init(ehci_softc_t * sc)28002ac6454SAndrew Thompson ehci_init(ehci_softc_t *sc)
28102ac6454SAndrew Thompson {
282760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
28302ac6454SAndrew Thompson 	uint32_t version;
28402ac6454SAndrew Thompson 	uint32_t sparams;
28502ac6454SAndrew Thompson 	uint16_t i;
28602ac6454SAndrew Thompson 	uint16_t x;
28702ac6454SAndrew Thompson 	uint16_t y;
28802ac6454SAndrew Thompson 	uint16_t bit;
289e0a69b51SAndrew Thompson 	usb_error_t err = 0;
29002ac6454SAndrew Thompson 
29102ac6454SAndrew Thompson 	DPRINTF("start\n");
29202ac6454SAndrew Thompson 
293a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
29430b22abeSAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_mtx, 0);
29502ac6454SAndrew Thompson 
29646873d15SHans Petter Selasky 	sc->sc_offs = EHCI_CAPLENGTH(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
29746873d15SHans Petter Selasky 
298b850ecc1SAndrew Thompson #ifdef USB_DEBUG
299cf223a88SAndrew Thompson 	if (ehciiaadbug)
300cf223a88SAndrew Thompson 		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
301cf223a88SAndrew Thompson 	if (ehcilostintrbug)
302cf223a88SAndrew Thompson 		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
30302ac6454SAndrew Thompson 	if (ehcidebug > 2) {
30402ac6454SAndrew Thompson 		ehci_dump_regs(sc);
30502ac6454SAndrew Thompson 	}
30602ac6454SAndrew Thompson #endif
30702ac6454SAndrew Thompson 
308495ed64cSNathan Whitehorn 	version = EHCI_HCIVERSION(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
30902ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
31002ac6454SAndrew Thompson 	    version >> 8, version & 0xff);
31102ac6454SAndrew Thompson 
31202ac6454SAndrew Thompson 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
31302ac6454SAndrew Thompson 	DPRINTF("sparams=0x%x\n", sparams);
31402ac6454SAndrew Thompson 
31502ac6454SAndrew Thompson 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
31602ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_2_0;
31702ac6454SAndrew Thompson 
318db43ed37SMarcel Moolenaar 	if (!(sc->sc_flags & EHCI_SCFLG_DONTRESET)) {
31902ac6454SAndrew Thompson 		/* Reset the controller */
320db43ed37SMarcel Moolenaar 		DPRINTF("%s: resetting\n",
321db43ed37SMarcel Moolenaar 		    device_get_nameunit(sc->sc_bus.bdev));
32202ac6454SAndrew Thompson 
323e55e1ebcSAndrew Thompson 		err = ehci_hcreset(sc);
32402ac6454SAndrew Thompson 		if (err) {
32502ac6454SAndrew Thompson 			device_printf(sc->sc_bus.bdev, "reset timeout\n");
32602ac6454SAndrew Thompson 			return (err);
32702ac6454SAndrew Thompson 		}
328db43ed37SMarcel Moolenaar 	}
329db43ed37SMarcel Moolenaar 
33002ac6454SAndrew Thompson 	/*
33102ac6454SAndrew Thompson 	 * use current frame-list-size selection 0: 1024*4 bytes 1:  512*4
33202ac6454SAndrew Thompson 	 * bytes 2:  256*4 bytes 3:      unknown
33302ac6454SAndrew Thompson 	 */
33402ac6454SAndrew Thompson 	if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
33502ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
33602ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
33702ac6454SAndrew Thompson 	}
33802ac6454SAndrew Thompson 	/* set up the bus struct */
33902ac6454SAndrew Thompson 	sc->sc_bus.methods = &ehci_bus_methods;
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
34202ac6454SAndrew Thompson 
34353e0bf6eSHans Petter Selasky 	if (1) {
34453e0bf6eSHans Petter Selasky 		struct ehci_qh_sub *qh;
34553e0bf6eSHans Petter Selasky 
34653e0bf6eSHans Petter Selasky 		usbd_get_page(&sc->sc_hw.terminate_pc, 0, &buf_res);
34753e0bf6eSHans Petter Selasky 
34853e0bf6eSHans Petter Selasky 		qh = buf_res.buffer;
34953e0bf6eSHans Petter Selasky 
35053e0bf6eSHans Petter Selasky 		sc->sc_terminate_self = htohc32(sc, buf_res.physaddr);
35153e0bf6eSHans Petter Selasky 
35253e0bf6eSHans Petter Selasky 		/* init terminate TD */
35353e0bf6eSHans Petter Selasky 		qh->qtd_next =
35453e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_LINK_TERMINATE);
35553e0bf6eSHans Petter Selasky 		qh->qtd_altnext =
35653e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_LINK_TERMINATE);
35753e0bf6eSHans Petter Selasky 		qh->qtd_status =
35853e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_QTD_HALTED);
35953e0bf6eSHans Petter Selasky 	}
36053e0bf6eSHans Petter Selasky 
36102ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
36202ac6454SAndrew Thompson 		ehci_qh_t *qh;
36302ac6454SAndrew Thompson 
364a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
36502ac6454SAndrew Thompson 
36602ac6454SAndrew Thompson 		qh = buf_res.buffer;
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 		/* initialize page cache pointer */
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 		qh->page_cache = sc->sc_hw.intr_start_pc + i;
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson 		/* store a pointer to queue head */
37302ac6454SAndrew Thompson 
37402ac6454SAndrew Thompson 		sc->sc_intr_p_last[i] = qh;
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 		qh->qh_self =
377e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
378e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson 		qh->qh_endp =
381e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
38202ac6454SAndrew Thompson 		qh->qh_endphub =
383e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_MULT(1));
38402ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
38502ac6454SAndrew Thompson 
38602ac6454SAndrew Thompson 		qh->qh_qtd.qtd_next =
387e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
38802ac6454SAndrew Thompson 		qh->qh_qtd.qtd_altnext =
389e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
39002ac6454SAndrew Thompson 		qh->qh_qtd.qtd_status =
391e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QTD_HALTED);
39202ac6454SAndrew Thompson 	}
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 	/*
39502ac6454SAndrew Thompson 	 * the QHs are arranged to give poll intervals that are
39602ac6454SAndrew Thompson 	 * powers of 2 times 1ms
39702ac6454SAndrew Thompson 	 */
39802ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
39902ac6454SAndrew Thompson 	while (bit) {
40002ac6454SAndrew Thompson 		x = bit;
40102ac6454SAndrew Thompson 		while (x & bit) {
40202ac6454SAndrew Thompson 			ehci_qh_t *qh_x;
40302ac6454SAndrew Thompson 			ehci_qh_t *qh_y;
40402ac6454SAndrew Thompson 
40502ac6454SAndrew Thompson 			y = (x ^ bit) | (bit / 2);
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson 			qh_x = sc->sc_intr_p_last[x];
40802ac6454SAndrew Thompson 			qh_y = sc->sc_intr_p_last[y];
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson 			/*
41102ac6454SAndrew Thompson 			 * the next QH has half the poll interval
41202ac6454SAndrew Thompson 			 */
41302ac6454SAndrew Thompson 			qh_x->qh_link = qh_y->qh_self;
41402ac6454SAndrew Thompson 
41502ac6454SAndrew Thompson 			x++;
41602ac6454SAndrew Thompson 		}
41702ac6454SAndrew Thompson 		bit >>= 1;
41802ac6454SAndrew Thompson 	}
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson 	if (1) {
42102ac6454SAndrew Thompson 		ehci_qh_t *qh;
42202ac6454SAndrew Thompson 
42302ac6454SAndrew Thompson 		qh = sc->sc_intr_p_last[0];
42402ac6454SAndrew Thompson 
42502ac6454SAndrew Thompson 		/* the last (1ms) QH terminates */
426e55e1ebcSAndrew Thompson 		qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
42702ac6454SAndrew Thompson 	}
42802ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
42902ac6454SAndrew Thompson 		ehci_sitd_t *sitd;
43002ac6454SAndrew Thompson 		ehci_itd_t *itd;
43102ac6454SAndrew Thompson 
432a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
43302ac6454SAndrew Thompson 
43402ac6454SAndrew Thompson 		sitd = buf_res.buffer;
43502ac6454SAndrew Thompson 
43602ac6454SAndrew Thompson 		/* initialize page cache pointer */
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson 		sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
44102ac6454SAndrew Thompson 
44202ac6454SAndrew Thompson 		sc->sc_isoc_fs_p_last[i] = sitd;
44302ac6454SAndrew Thompson 
44402ac6454SAndrew Thompson 		/* initialize full speed isochronous */
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson 		sitd->sitd_self =
447e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
448e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_SITD);
44902ac6454SAndrew Thompson 
45002ac6454SAndrew Thompson 		sitd->sitd_back =
451e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
45202ac6454SAndrew Thompson 
45302ac6454SAndrew Thompson 		sitd->sitd_next =
45402ac6454SAndrew Thompson 		    sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
45502ac6454SAndrew Thompson 
456a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
45702ac6454SAndrew Thompson 
45802ac6454SAndrew Thompson 		itd = buf_res.buffer;
45902ac6454SAndrew Thompson 
46002ac6454SAndrew Thompson 		/* initialize page cache pointer */
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson 		itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
46502ac6454SAndrew Thompson 
46602ac6454SAndrew Thompson 		sc->sc_isoc_hs_p_last[i] = itd;
46702ac6454SAndrew Thompson 
46802ac6454SAndrew Thompson 		/* initialize high speed isochronous */
46902ac6454SAndrew Thompson 
47002ac6454SAndrew Thompson 		itd->itd_self =
471e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
472e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_ITD);
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson 		itd->itd_next =
47502ac6454SAndrew Thompson 		    sitd->sitd_self;
47602ac6454SAndrew Thompson 	}
47702ac6454SAndrew Thompson 
478a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 	if (1) {
48102ac6454SAndrew Thompson 		uint32_t *pframes;
48202ac6454SAndrew Thompson 
48302ac6454SAndrew Thompson 		pframes = buf_res.buffer;
48402ac6454SAndrew Thompson 
48502ac6454SAndrew Thompson 		/*
48602ac6454SAndrew Thompson 		 * execution order:
48702ac6454SAndrew Thompson 		 * pframes -> high speed isochronous ->
48802ac6454SAndrew Thompson 		 *    full speed isochronous -> interrupt QH's
48902ac6454SAndrew Thompson 		 */
49002ac6454SAndrew Thompson 		for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
49102ac6454SAndrew Thompson 			pframes[i] = sc->sc_isoc_hs_p_last
49202ac6454SAndrew Thompson 			    [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
49302ac6454SAndrew Thompson 		}
49402ac6454SAndrew Thompson 	}
495a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
49602ac6454SAndrew Thompson 
49702ac6454SAndrew Thompson 	if (1) {
49802ac6454SAndrew Thompson 		ehci_qh_t *qh;
49902ac6454SAndrew Thompson 
50002ac6454SAndrew Thompson 		qh = buf_res.buffer;
50102ac6454SAndrew Thompson 
50202ac6454SAndrew Thompson 		/* initialize page cache pointer */
50302ac6454SAndrew Thompson 
50402ac6454SAndrew Thompson 		qh->page_cache = &sc->sc_hw.async_start_pc;
50502ac6454SAndrew Thompson 
50602ac6454SAndrew Thompson 		/* store a pointer to the queue head */
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 		sc->sc_async_p_last = qh;
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson 		/* init dummy QH that starts the async list */
51102ac6454SAndrew Thompson 
51202ac6454SAndrew Thompson 		qh->qh_self =
513e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
514e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
51502ac6454SAndrew Thompson 
51602ac6454SAndrew Thompson 		/* fill the QH */
51702ac6454SAndrew Thompson 		qh->qh_endp =
518e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
519e55e1ebcSAndrew Thompson 		qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
52002ac6454SAndrew Thompson 		qh->qh_link = qh->qh_self;
52102ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson 		/* fill the overlay qTD */
524e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
525e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
526e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
52702ac6454SAndrew Thompson 	}
52802ac6454SAndrew Thompson 	/* flush all cache into memory */
52902ac6454SAndrew Thompson 
530a593f6b8SAndrew Thompson 	usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
53102ac6454SAndrew Thompson 
532b850ecc1SAndrew Thompson #ifdef USB_DEBUG
53302ac6454SAndrew Thompson 	if (ehcidebug) {
53402ac6454SAndrew Thompson 		ehci_dump_sqh(sc, sc->sc_async_p_last);
53502ac6454SAndrew Thompson 	}
53602ac6454SAndrew Thompson #endif
53702ac6454SAndrew Thompson 
5382e141748SHans Petter Selasky 	/* finial setup */
5392e141748SHans Petter Selasky  	err = ehci_init_sub(sc);
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 	if (!err) {
54202ac6454SAndrew Thompson 		/* catch any lost interrupts */
54302ac6454SAndrew Thompson 		ehci_do_poll(&sc->sc_bus);
54402ac6454SAndrew Thompson 	}
54502ac6454SAndrew Thompson 	return (err);
54602ac6454SAndrew Thompson }
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson /*
54902ac6454SAndrew Thompson  * shut down the controller when the system is going down
55002ac6454SAndrew Thompson  */
55102ac6454SAndrew Thompson void
ehci_detach(ehci_softc_t * sc)55202ac6454SAndrew Thompson ehci_detach(ehci_softc_t *sc)
55302ac6454SAndrew Thompson {
55402ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
55502ac6454SAndrew Thompson 
556a593f6b8SAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_pcd);
55730b22abeSAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_poll);
55802ac6454SAndrew Thompson 
559bda9babeSAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, 0);
560e55e1ebcSAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
56102ac6454SAndrew Thompson 
562e55e1ebcSAndrew Thompson 	if (ehci_hcreset(sc)) {
56302ac6454SAndrew Thompson 		DPRINTF("reset failed!\n");
56402ac6454SAndrew Thompson 	}
56502ac6454SAndrew Thompson 
56602ac6454SAndrew Thompson 	/* XXX let stray task complete */
567a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL, hz / 20);
56802ac6454SAndrew Thompson 
569a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_pcd);
57030b22abeSAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_poll);
57102ac6454SAndrew Thompson }
57202ac6454SAndrew Thompson 
5732e141748SHans Petter Selasky static void
ehci_suspend(ehci_softc_t * sc)57402ac6454SAndrew Thompson ehci_suspend(ehci_softc_t *sc)
57502ac6454SAndrew Thompson {
57602ac6454SAndrew Thompson 	DPRINTF("stopping the HC\n");
57702ac6454SAndrew Thompson 
5782e141748SHans Petter Selasky 	/* reset HC */
5792e141748SHans Petter Selasky 	ehci_hcreset(sc);
58002ac6454SAndrew Thompson }
5812e141748SHans Petter Selasky 
5822e141748SHans Petter Selasky static void
ehci_resume(ehci_softc_t * sc)5832e141748SHans Petter Selasky ehci_resume(ehci_softc_t *sc)
5842e141748SHans Petter Selasky {
5852e141748SHans Petter Selasky 	/* reset HC */
5862e141748SHans Petter Selasky 	ehci_hcreset(sc);
5872e141748SHans Petter Selasky 
5882e141748SHans Petter Selasky 	/* setup HC */
5892e141748SHans Petter Selasky 	ehci_init_sub(sc);
5902e141748SHans Petter Selasky 
5912e141748SHans Petter Selasky 	/* catch any lost interrupts */
5922e141748SHans Petter Selasky 	ehci_do_poll(&sc->sc_bus);
59302ac6454SAndrew Thompson }
59402ac6454SAndrew Thompson 
595b850ecc1SAndrew Thompson #ifdef USB_DEBUG
59602ac6454SAndrew Thompson static void
ehci_dump_regs(ehci_softc_t * sc)59702ac6454SAndrew Thompson ehci_dump_regs(ehci_softc_t *sc)
59802ac6454SAndrew Thompson {
59902ac6454SAndrew Thompson 	uint32_t i;
60002ac6454SAndrew Thompson 
60102ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBCMD);
60202ac6454SAndrew Thompson 	printf("cmd=0x%08x\n", i);
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_1)
60502ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_1\n");
60602ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_2)
60702ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_2\n");
60802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_4)
60902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_4\n");
61002ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_8)
61102ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_8\n");
61202ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_16)
61302ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_16\n");
61402ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_32)
61502ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_32\n");
61602ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_64)
61702ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_64\n");
61802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPME)
61902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPME\n");
62002ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPMC)
62102ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPMC\n");
62202ac6454SAndrew Thompson 	if (i & EHCI_CMD_LHCR)
62302ac6454SAndrew Thompson 		printf(" EHCI_CMD_LHCR\n");
62402ac6454SAndrew Thompson 	if (i & EHCI_CMD_IAAD)
62502ac6454SAndrew Thompson 		printf(" EHCI_CMD_IAAD\n");
62602ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASE)
62702ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASE\n");
62802ac6454SAndrew Thompson 	if (i & EHCI_CMD_PSE)
62902ac6454SAndrew Thompson 		printf(" EHCI_CMD_PSE\n");
63002ac6454SAndrew Thompson 	if (i & EHCI_CMD_FLS_M)
63102ac6454SAndrew Thompson 		printf(" EHCI_CMD_FLS_M\n");
63202ac6454SAndrew Thompson 	if (i & EHCI_CMD_HCRESET)
63302ac6454SAndrew Thompson 		printf(" EHCI_CMD_HCRESET\n");
63402ac6454SAndrew Thompson 	if (i & EHCI_CMD_RS)
63502ac6454SAndrew Thompson 		printf(" EHCI_CMD_RS\n");
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBSTS);
63802ac6454SAndrew Thompson 
63902ac6454SAndrew Thompson 	printf("sts=0x%08x\n", i);
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson 	if (i & EHCI_STS_ASS)
64202ac6454SAndrew Thompson 		printf(" EHCI_STS_ASS\n");
64302ac6454SAndrew Thompson 	if (i & EHCI_STS_PSS)
64402ac6454SAndrew Thompson 		printf(" EHCI_STS_PSS\n");
64502ac6454SAndrew Thompson 	if (i & EHCI_STS_REC)
64602ac6454SAndrew Thompson 		printf(" EHCI_STS_REC\n");
64702ac6454SAndrew Thompson 	if (i & EHCI_STS_HCH)
64802ac6454SAndrew Thompson 		printf(" EHCI_STS_HCH\n");
64902ac6454SAndrew Thompson 	if (i & EHCI_STS_IAA)
65002ac6454SAndrew Thompson 		printf(" EHCI_STS_IAA\n");
65102ac6454SAndrew Thompson 	if (i & EHCI_STS_HSE)
65202ac6454SAndrew Thompson 		printf(" EHCI_STS_HSE\n");
65302ac6454SAndrew Thompson 	if (i & EHCI_STS_FLR)
65402ac6454SAndrew Thompson 		printf(" EHCI_STS_FLR\n");
65502ac6454SAndrew Thompson 	if (i & EHCI_STS_PCD)
65602ac6454SAndrew Thompson 		printf(" EHCI_STS_PCD\n");
65702ac6454SAndrew Thompson 	if (i & EHCI_STS_ERRINT)
65802ac6454SAndrew Thompson 		printf(" EHCI_STS_ERRINT\n");
65902ac6454SAndrew Thompson 	if (i & EHCI_STS_INT)
66002ac6454SAndrew Thompson 		printf(" EHCI_STS_INT\n");
66102ac6454SAndrew Thompson 
66202ac6454SAndrew Thompson 	printf("ien=0x%08x\n",
66302ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_USBINTR));
66402ac6454SAndrew Thompson 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
66502ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_FRINDEX),
66602ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
66702ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
66802ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
66902ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
67002ac6454SAndrew Thompson 		printf("port %d status=0x%08x\n", i,
67102ac6454SAndrew Thompson 		    EOREAD4(sc, EHCI_PORTSC(i)));
67202ac6454SAndrew Thompson 	}
67302ac6454SAndrew Thompson }
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson static void
ehci_dump_link(ehci_softc_t * sc,uint32_t link,int type)67602ac6454SAndrew Thompson ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
67702ac6454SAndrew Thompson {
678e55e1ebcSAndrew Thompson 	link = hc32toh(sc, link);
67902ac6454SAndrew Thompson 	printf("0x%08x", link);
68002ac6454SAndrew Thompson 	if (link & EHCI_LINK_TERMINATE)
68102ac6454SAndrew Thompson 		printf("<T>");
68202ac6454SAndrew Thompson 	else {
68302ac6454SAndrew Thompson 		printf("<");
68402ac6454SAndrew Thompson 		if (type) {
68502ac6454SAndrew Thompson 			switch (EHCI_LINK_TYPE(link)) {
68602ac6454SAndrew Thompson 			case EHCI_LINK_ITD:
68702ac6454SAndrew Thompson 				printf("ITD");
68802ac6454SAndrew Thompson 				break;
68902ac6454SAndrew Thompson 			case EHCI_LINK_QH:
69002ac6454SAndrew Thompson 				printf("QH");
69102ac6454SAndrew Thompson 				break;
69202ac6454SAndrew Thompson 			case EHCI_LINK_SITD:
69302ac6454SAndrew Thompson 				printf("SITD");
69402ac6454SAndrew Thompson 				break;
69502ac6454SAndrew Thompson 			case EHCI_LINK_FSTN:
69602ac6454SAndrew Thompson 				printf("FSTN");
69702ac6454SAndrew Thompson 				break;
69802ac6454SAndrew Thompson 			}
69902ac6454SAndrew Thompson 		}
70002ac6454SAndrew Thompson 		printf(">");
70102ac6454SAndrew Thompson 	}
70202ac6454SAndrew Thompson }
70302ac6454SAndrew Thompson 
70402ac6454SAndrew Thompson static void
ehci_dump_qtd(ehci_softc_t * sc,ehci_qtd_t * qtd)70502ac6454SAndrew Thompson ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
70602ac6454SAndrew Thompson {
70702ac6454SAndrew Thompson 	uint32_t s;
70802ac6454SAndrew Thompson 
70902ac6454SAndrew Thompson 	printf("  next=");
71002ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_next, 0);
71102ac6454SAndrew Thompson 	printf(" altnext=");
71202ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_altnext, 0);
71302ac6454SAndrew Thompson 	printf("\n");
714e55e1ebcSAndrew Thompson 	s = hc32toh(sc, qtd->qtd_status);
71502ac6454SAndrew Thompson 	printf("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
71602ac6454SAndrew Thompson 	    s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
71702ac6454SAndrew Thompson 	    EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
71802ac6454SAndrew Thompson 	printf("    cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
71902ac6454SAndrew Thompson 	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
72002ac6454SAndrew Thompson 	    (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
72102ac6454SAndrew Thompson 	    (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
72202ac6454SAndrew Thompson 	    (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
72302ac6454SAndrew Thompson 	    (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
72402ac6454SAndrew Thompson 	    (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
72502ac6454SAndrew Thompson 	    (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
72602ac6454SAndrew Thompson 	    (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
72702ac6454SAndrew Thompson 	    (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
72802ac6454SAndrew Thompson 
72902ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
73002ac6454SAndrew Thompson 		printf("  buffer[%d]=0x%08x\n", s,
731e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer[s]));
73202ac6454SAndrew Thompson 	}
73302ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
73402ac6454SAndrew Thompson 		printf("  buffer_hi[%d]=0x%08x\n", s,
735e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer_hi[s]));
73602ac6454SAndrew Thompson 	}
73702ac6454SAndrew Thompson }
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson static uint8_t
ehci_dump_sqtd(ehci_softc_t * sc,ehci_qtd_t * sqtd)74002ac6454SAndrew Thompson ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
74102ac6454SAndrew Thompson {
74202ac6454SAndrew Thompson 	uint8_t temp;
74302ac6454SAndrew Thompson 
744a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sqtd->page_cache);
745e55e1ebcSAndrew Thompson 	printf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
74602ac6454SAndrew Thompson 	ehci_dump_qtd(sc, sqtd);
747e55e1ebcSAndrew Thompson 	temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
74802ac6454SAndrew Thompson 	return (temp);
74902ac6454SAndrew Thompson }
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson static void
ehci_dump_sqtds(ehci_softc_t * sc,ehci_qtd_t * sqtd)75202ac6454SAndrew Thompson ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
75302ac6454SAndrew Thompson {
75402ac6454SAndrew Thompson 	uint16_t i;
75502ac6454SAndrew Thompson 	uint8_t stop;
75602ac6454SAndrew Thompson 
75702ac6454SAndrew Thompson 	stop = 0;
75802ac6454SAndrew Thompson 	for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
75902ac6454SAndrew Thompson 		stop = ehci_dump_sqtd(sc, sqtd);
76002ac6454SAndrew Thompson 	}
76102ac6454SAndrew Thompson 	if (sqtd) {
76202ac6454SAndrew Thompson 		printf("dump aborted, too many TDs\n");
76302ac6454SAndrew Thompson 	}
76402ac6454SAndrew Thompson }
76502ac6454SAndrew Thompson 
76602ac6454SAndrew Thompson static void
ehci_dump_sqh(ehci_softc_t * sc,ehci_qh_t * qh)76702ac6454SAndrew Thompson ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
76802ac6454SAndrew Thompson {
76902ac6454SAndrew Thompson 	uint32_t endp;
77002ac6454SAndrew Thompson 	uint32_t endphub;
77102ac6454SAndrew Thompson 
772a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
773e55e1ebcSAndrew Thompson 	printf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
77402ac6454SAndrew Thompson 	printf("  link=");
77502ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_link, 1);
77602ac6454SAndrew Thompson 	printf("\n");
777e55e1ebcSAndrew Thompson 	endp = hc32toh(sc, qh->qh_endp);
77802ac6454SAndrew Thompson 	printf("  endp=0x%08x\n", endp);
77902ac6454SAndrew Thompson 	printf("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
78002ac6454SAndrew Thompson 	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
78102ac6454SAndrew Thompson 	    EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
78202ac6454SAndrew Thompson 	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
78302ac6454SAndrew Thompson 	printf("    mpl=0x%x ctl=%d nrl=%d\n",
78402ac6454SAndrew Thompson 	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
78502ac6454SAndrew Thompson 	    EHCI_QH_GET_NRL(endp));
786e55e1ebcSAndrew Thompson 	endphub = hc32toh(sc, qh->qh_endphub);
78702ac6454SAndrew Thompson 	printf("  endphub=0x%08x\n", endphub);
78802ac6454SAndrew Thompson 	printf("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
78902ac6454SAndrew Thompson 	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
79002ac6454SAndrew Thompson 	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
79102ac6454SAndrew Thompson 	    EHCI_QH_GET_MULT(endphub));
79202ac6454SAndrew Thompson 	printf("  curqtd=");
79302ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_curqtd, 0);
79402ac6454SAndrew Thompson 	printf("\n");
79502ac6454SAndrew Thompson 	printf("Overlay qTD:\n");
79602ac6454SAndrew Thompson 	ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
79702ac6454SAndrew Thompson }
79802ac6454SAndrew Thompson 
79902ac6454SAndrew Thompson static void
ehci_dump_sitd(ehci_softc_t * sc,ehci_sitd_t * sitd)80002ac6454SAndrew Thompson ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
80102ac6454SAndrew Thompson {
802a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sitd->page_cache);
803e55e1ebcSAndrew Thompson 	printf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
804e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
80502ac6454SAndrew Thompson 	printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
806e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_portaddr),
807e55e1ebcSAndrew Thompson 	    (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
80802ac6454SAndrew Thompson 	    ? "in" : "out",
809e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
810e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
811e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
812e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
813e55e1ebcSAndrew Thompson 	printf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
814e55e1ebcSAndrew Thompson 	printf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
815e55e1ebcSAndrew Thompson 	    (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
816e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
81702ac6454SAndrew Thompson 	printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
818e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_back),
819e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[0]),
820e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[1]),
821e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[0]),
822e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[1]));
82302ac6454SAndrew Thompson }
82402ac6454SAndrew Thompson 
82502ac6454SAndrew Thompson static void
ehci_dump_itd(ehci_softc_t * sc,ehci_itd_t * itd)82602ac6454SAndrew Thompson ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
82702ac6454SAndrew Thompson {
828a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(itd->page_cache);
829e55e1ebcSAndrew Thompson 	printf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
830e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
831e55e1ebcSAndrew Thompson 	printf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
832e55e1ebcSAndrew Thompson 	    (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
833e55e1ebcSAndrew Thompson 	printf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
834e55e1ebcSAndrew Thompson 	    (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
835e55e1ebcSAndrew Thompson 	printf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
836e55e1ebcSAndrew Thompson 	    (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
837e55e1ebcSAndrew Thompson 	printf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
838e55e1ebcSAndrew Thompson 	    (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
839e55e1ebcSAndrew Thompson 	printf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
840e55e1ebcSAndrew Thompson 	    (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
841e55e1ebcSAndrew Thompson 	printf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
842e55e1ebcSAndrew Thompson 	    (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
843e55e1ebcSAndrew Thompson 	printf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
844e55e1ebcSAndrew Thompson 	    (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
845e55e1ebcSAndrew Thompson 	printf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
846e55e1ebcSAndrew Thompson 	    (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
847e55e1ebcSAndrew Thompson 	printf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
84802ac6454SAndrew Thompson 	printf("  addr=0x%02x; endpt=0x%01x\n",
849e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
850e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
851e55e1ebcSAndrew Thompson 	printf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
85202ac6454SAndrew Thompson 	printf(" dir=%s; mpl=0x%02x\n",
853e55e1ebcSAndrew Thompson 	    (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
854e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
85502ac6454SAndrew Thompson 	printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
856e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[2]),
857e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[3]),
858e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[4]),
859e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[5]),
860e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[6]));
86102ac6454SAndrew Thompson 	printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
86202ac6454SAndrew Thompson 	    "       0x%08x,0x%08x,0x%08x\n",
863e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[0]),
864e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[1]),
865e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[2]),
866e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[3]),
867e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[4]),
868e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[5]),
869e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[6]));
87002ac6454SAndrew Thompson }
87102ac6454SAndrew Thompson 
87202ac6454SAndrew Thompson static void
ehci_dump_isoc(ehci_softc_t * sc)87302ac6454SAndrew Thompson ehci_dump_isoc(ehci_softc_t *sc)
87402ac6454SAndrew Thompson {
87502ac6454SAndrew Thompson 	ehci_itd_t *itd;
87602ac6454SAndrew Thompson 	ehci_sitd_t *sitd;
87702ac6454SAndrew Thompson 	uint16_t max = 1000;
87802ac6454SAndrew Thompson 	uint16_t pos;
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson 	pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
88102ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 	printf("%s: isochronous dump from frame 0x%03x:\n",
88402ac6454SAndrew Thompson 	    __FUNCTION__, pos);
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson 	itd = sc->sc_isoc_hs_p_last[pos];
88702ac6454SAndrew Thompson 	sitd = sc->sc_isoc_fs_p_last[pos];
88802ac6454SAndrew Thompson 
88902ac6454SAndrew Thompson 	while (itd && max && max--) {
89002ac6454SAndrew Thompson 		ehci_dump_itd(sc, itd);
89102ac6454SAndrew Thompson 		itd = itd->prev;
89202ac6454SAndrew Thompson 	}
89302ac6454SAndrew Thompson 
89402ac6454SAndrew Thompson 	while (sitd && max && max--) {
89502ac6454SAndrew Thompson 		ehci_dump_sitd(sc, sitd);
89602ac6454SAndrew Thompson 		sitd = sitd->prev;
89702ac6454SAndrew Thompson 	}
89802ac6454SAndrew Thompson }
89902ac6454SAndrew Thompson 
90002ac6454SAndrew Thompson #endif
90102ac6454SAndrew Thompson 
90202ac6454SAndrew Thompson static void
ehci_transfer_intr_enqueue(struct usb_xfer * xfer)903760bc48eSAndrew Thompson ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
90402ac6454SAndrew Thompson {
90502ac6454SAndrew Thompson 	/* check for early completion */
90602ac6454SAndrew Thompson 	if (ehci_check_transfer(xfer)) {
90702ac6454SAndrew Thompson 		return;
90802ac6454SAndrew Thompson 	}
90902ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
910a593f6b8SAndrew Thompson 	usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
91102ac6454SAndrew Thompson 
91202ac6454SAndrew Thompson 	/* start timeout, if any */
91302ac6454SAndrew Thompson 	if (xfer->timeout != 0) {
914a593f6b8SAndrew Thompson 		usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
91502ac6454SAndrew Thompson 	}
91602ac6454SAndrew Thompson }
91702ac6454SAndrew Thompson 
91802ac6454SAndrew Thompson #define	EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
91902ac6454SAndrew Thompson static ehci_sitd_t *
_ehci_append_fs_td(ehci_sitd_t * std,ehci_sitd_t * last)92002ac6454SAndrew Thompson _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
92102ac6454SAndrew Thompson {
92202ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
92302ac6454SAndrew Thompson 
92402ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
92502ac6454SAndrew Thompson 
92602ac6454SAndrew Thompson 	std->next = last->next;
92702ac6454SAndrew Thompson 	std->sitd_next = last->sitd_next;
92802ac6454SAndrew Thompson 
92902ac6454SAndrew Thompson 	std->prev = last;
93002ac6454SAndrew Thompson 
931a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
93202ac6454SAndrew Thompson 
93302ac6454SAndrew Thompson 	/*
93402ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
93502ac6454SAndrew Thompson 	 */
93602ac6454SAndrew Thompson 	last->next = std;
93702ac6454SAndrew Thompson 	last->sitd_next = std->sitd_self;
93802ac6454SAndrew Thompson 
939a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
94002ac6454SAndrew Thompson 
94102ac6454SAndrew Thompson 	return (std);
94202ac6454SAndrew Thompson }
94302ac6454SAndrew Thompson 
94402ac6454SAndrew Thompson #define	EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
94502ac6454SAndrew Thompson static ehci_itd_t *
_ehci_append_hs_td(ehci_itd_t * std,ehci_itd_t * last)94602ac6454SAndrew Thompson _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
94702ac6454SAndrew Thompson {
94802ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
94902ac6454SAndrew Thompson 
95002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
95102ac6454SAndrew Thompson 
95202ac6454SAndrew Thompson 	std->next = last->next;
95302ac6454SAndrew Thompson 	std->itd_next = last->itd_next;
95402ac6454SAndrew Thompson 
95502ac6454SAndrew Thompson 	std->prev = last;
95602ac6454SAndrew Thompson 
957a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
95802ac6454SAndrew Thompson 
95902ac6454SAndrew Thompson 	/*
96002ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
96102ac6454SAndrew Thompson 	 */
96202ac6454SAndrew Thompson 	last->next = std;
96302ac6454SAndrew Thompson 	last->itd_next = std->itd_self;
96402ac6454SAndrew Thompson 
965a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
96602ac6454SAndrew Thompson 
96702ac6454SAndrew Thompson 	return (std);
96802ac6454SAndrew Thompson }
96902ac6454SAndrew Thompson 
97002ac6454SAndrew Thompson #define	EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
97102ac6454SAndrew Thompson static ehci_qh_t *
_ehci_append_qh(ehci_qh_t * sqh,ehci_qh_t * last)97202ac6454SAndrew Thompson _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
97302ac6454SAndrew Thompson {
97402ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", sqh, last);
97502ac6454SAndrew Thompson 
97602ac6454SAndrew Thompson 	if (sqh->prev != NULL) {
97702ac6454SAndrew Thompson 		/* should not happen */
97802ac6454SAndrew Thompson 		DPRINTFN(0, "QH already linked!\n");
97902ac6454SAndrew Thompson 		return (last);
98002ac6454SAndrew Thompson 	}
98102ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
98202ac6454SAndrew Thompson 
98302ac6454SAndrew Thompson 	sqh->next = last->next;
98402ac6454SAndrew Thompson 	sqh->qh_link = last->qh_link;
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson 	sqh->prev = last;
98702ac6454SAndrew Thompson 
988a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(sqh->page_cache);
98902ac6454SAndrew Thompson 
99002ac6454SAndrew Thompson 	/*
99102ac6454SAndrew Thompson 	 * the last->next->prev is never followed: sqh->next->prev = sqh;
99202ac6454SAndrew Thompson 	 */
99302ac6454SAndrew Thompson 
99402ac6454SAndrew Thompson 	last->next = sqh;
99502ac6454SAndrew Thompson 	last->qh_link = sqh->qh_self;
99602ac6454SAndrew Thompson 
997a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
99802ac6454SAndrew Thompson 
99902ac6454SAndrew Thompson 	return (sqh);
100002ac6454SAndrew Thompson }
100102ac6454SAndrew Thompson 
100202ac6454SAndrew Thompson #define	EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
100302ac6454SAndrew Thompson static ehci_sitd_t *
_ehci_remove_fs_td(ehci_sitd_t * std,ehci_sitd_t * last)100402ac6454SAndrew Thompson _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
100502ac6454SAndrew Thompson {
100602ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
100702ac6454SAndrew Thompson 
100802ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
100902ac6454SAndrew Thompson 
101002ac6454SAndrew Thompson 	std->prev->next = std->next;
101102ac6454SAndrew Thompson 	std->prev->sitd_next = std->sitd_next;
101202ac6454SAndrew Thompson 
1013a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
101402ac6454SAndrew Thompson 
101502ac6454SAndrew Thompson 	if (std->next) {
101602ac6454SAndrew Thompson 		std->next->prev = std->prev;
1017a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
101802ac6454SAndrew Thompson 	}
101902ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
102002ac6454SAndrew Thompson }
102102ac6454SAndrew Thompson 
102202ac6454SAndrew Thompson #define	EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
102302ac6454SAndrew Thompson static ehci_itd_t *
_ehci_remove_hs_td(ehci_itd_t * std,ehci_itd_t * last)102402ac6454SAndrew Thompson _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
102502ac6454SAndrew Thompson {
102602ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
102702ac6454SAndrew Thompson 
102802ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
102902ac6454SAndrew Thompson 
103002ac6454SAndrew Thompson 	std->prev->next = std->next;
103102ac6454SAndrew Thompson 	std->prev->itd_next = std->itd_next;
103202ac6454SAndrew Thompson 
1033a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
103402ac6454SAndrew Thompson 
103502ac6454SAndrew Thompson 	if (std->next) {
103602ac6454SAndrew Thompson 		std->next->prev = std->prev;
1037a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
103802ac6454SAndrew Thompson 	}
103902ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
104002ac6454SAndrew Thompson }
104102ac6454SAndrew Thompson 
104202ac6454SAndrew Thompson #define	EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
104302ac6454SAndrew Thompson static ehci_qh_t *
_ehci_remove_qh(ehci_qh_t * sqh,ehci_qh_t * last)104402ac6454SAndrew Thompson _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
104502ac6454SAndrew Thompson {
104602ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", sqh, last);
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
104902ac6454SAndrew Thompson 
105002ac6454SAndrew Thompson 	/* only remove if not removed from a queue */
105102ac6454SAndrew Thompson 	if (sqh->prev) {
105202ac6454SAndrew Thompson 		sqh->prev->next = sqh->next;
105302ac6454SAndrew Thompson 		sqh->prev->qh_link = sqh->qh_link;
105402ac6454SAndrew Thompson 
1055a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->prev->page_cache);
105602ac6454SAndrew Thompson 
105702ac6454SAndrew Thompson 		if (sqh->next) {
105802ac6454SAndrew Thompson 			sqh->next->prev = sqh->prev;
1059a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(sqh->next->page_cache);
106002ac6454SAndrew Thompson 		}
106102ac6454SAndrew Thompson 		last = ((last == sqh) ? sqh->prev : last);
106202ac6454SAndrew Thompson 
106302ac6454SAndrew Thompson 		sqh->prev = 0;
106402ac6454SAndrew Thompson 
1065a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->page_cache);
106602ac6454SAndrew Thompson 	}
106702ac6454SAndrew Thompson 	return (last);
106802ac6454SAndrew Thompson }
106902ac6454SAndrew Thompson 
1070dbe63d3aSHans Petter Selasky static void
ehci_data_toggle_update(struct usb_xfer * xfer,uint16_t actlen,uint16_t xlen)1071dbe63d3aSHans Petter Selasky ehci_data_toggle_update(struct usb_xfer *xfer, uint16_t actlen, uint16_t xlen)
1072dbe63d3aSHans Petter Selasky {
1073ca794e78SHans Petter Selasky 	uint16_t rem;
1074dbe63d3aSHans Petter Selasky 	uint8_t dt;
1075dbe63d3aSHans Petter Selasky 
1076dbe63d3aSHans Petter Selasky 	/* count number of full packets */
1077dbe63d3aSHans Petter Selasky 	dt = (actlen / xfer->max_packet_size) & 1;
1078dbe63d3aSHans Petter Selasky 
1079996c2735SHans Petter Selasky 	/* compute remainder */
1080ca794e78SHans Petter Selasky 	rem = actlen % xfer->max_packet_size;
1081dbe63d3aSHans Petter Selasky 
1082ca794e78SHans Petter Selasky 	if (rem > 0)
1083dbe63d3aSHans Petter Selasky 		dt ^= 1;	/* short packet at the end */
1084ca794e78SHans Petter Selasky 	else if (actlen != xlen)
1085dbe63d3aSHans Petter Selasky 		dt ^= 1;	/* zero length packet at the end */
1086ea719edeSHans Petter Selasky 	else if (xlen == 0)
1087ea719edeSHans Petter Selasky 		dt ^= 1;	/* zero length transfer */
1088dbe63d3aSHans Petter Selasky 
1089dbe63d3aSHans Petter Selasky 	xfer->endpoint->toggle_next ^= dt;
1090dbe63d3aSHans Petter Selasky }
1091dbe63d3aSHans Petter Selasky 
1092e0a69b51SAndrew Thompson static usb_error_t
ehci_non_isoc_done_sub(struct usb_xfer * xfer)1093760bc48eSAndrew Thompson ehci_non_isoc_done_sub(struct usb_xfer *xfer)
109402ac6454SAndrew Thompson {
109502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
109602ac6454SAndrew Thompson 	ehci_qtd_t *td;
109702ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
109802ac6454SAndrew Thompson 	uint32_t status;
109902ac6454SAndrew Thompson 	uint16_t len;
110002ac6454SAndrew Thompson 
110102ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
110202ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
110302ac6454SAndrew Thompson 
110402ac6454SAndrew Thompson 	if (xfer->aframes != xfer->nframes) {
1105ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
110602ac6454SAndrew Thompson 	}
110702ac6454SAndrew Thompson 	while (1) {
1108a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1109e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->qtd_status);
111002ac6454SAndrew Thompson 
111102ac6454SAndrew Thompson 		len = EHCI_QTD_GET_BYTES(status);
111202ac6454SAndrew Thompson 
111302ac6454SAndrew Thompson 		/*
111402ac6454SAndrew Thompson 	         * Verify the status length and
111502ac6454SAndrew Thompson 		 * add the length to "frlengths[]":
111602ac6454SAndrew Thompson 	         */
111702ac6454SAndrew Thompson 		if (len > td->len) {
111802ac6454SAndrew Thompson 			/* should not happen */
111902ac6454SAndrew Thompson 			DPRINTF("Invalid status length, "
112002ac6454SAndrew Thompson 			    "0x%04x/0x%04x bytes\n", len, td->len);
112102ac6454SAndrew Thompson 			status |= EHCI_QTD_HALTED;
112202ac6454SAndrew Thompson 		} else if (xfer->aframes != xfer->nframes) {
112302ac6454SAndrew Thompson 			xfer->frlengths[xfer->aframes] += td->len - len;
1124dbe63d3aSHans Petter Selasky 			/* manually update data toggle */
1125dbe63d3aSHans Petter Selasky 			ehci_data_toggle_update(xfer, td->len - len, td->len);
112602ac6454SAndrew Thompson 		}
1127dbe63d3aSHans Petter Selasky 
112802ac6454SAndrew Thompson 		/* Check for last transfer */
112902ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
113002ac6454SAndrew Thompson 			td = NULL;
113102ac6454SAndrew Thompson 			break;
113202ac6454SAndrew Thompson 		}
113302ac6454SAndrew Thompson 		/* Check for transfer error */
113402ac6454SAndrew Thompson 		if (status & EHCI_QTD_HALTED) {
113502ac6454SAndrew Thompson 			/* the transfer is finished */
113602ac6454SAndrew Thompson 			td = NULL;
113702ac6454SAndrew Thompson 			break;
113802ac6454SAndrew Thompson 		}
113902ac6454SAndrew Thompson 		/* Check for short transfer */
114002ac6454SAndrew Thompson 		if (len > 0) {
114102ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
114202ac6454SAndrew Thompson 				/* follow alt next */
114302ac6454SAndrew Thompson 				td = td->alt_next;
114402ac6454SAndrew Thompson 			} else {
114502ac6454SAndrew Thompson 				/* the transfer is finished */
114602ac6454SAndrew Thompson 				td = NULL;
114702ac6454SAndrew Thompson 			}
114802ac6454SAndrew Thompson 			break;
114902ac6454SAndrew Thompson 		}
115002ac6454SAndrew Thompson 		td = td->obj_next;
115102ac6454SAndrew Thompson 
115202ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
115302ac6454SAndrew Thompson 			/* this USB frame is complete */
115402ac6454SAndrew Thompson 			break;
115502ac6454SAndrew Thompson 		}
115602ac6454SAndrew Thompson 	}
115702ac6454SAndrew Thompson 
115802ac6454SAndrew Thompson 	/* update transfer cache */
115902ac6454SAndrew Thompson 
116002ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
116102ac6454SAndrew Thompson 
1162b850ecc1SAndrew Thompson #ifdef USB_DEBUG
116302ac6454SAndrew Thompson 	if (status & EHCI_QTD_STATERRS) {
116402ac6454SAndrew Thompson 		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
116502ac6454SAndrew Thompson 		    "status=%s%s%s%s%s%s%s%s\n",
1166ae60fdfbSAndrew Thompson 		    xfer->address, xfer->endpointno, xfer->aframes,
116702ac6454SAndrew Thompson 		    (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
116802ac6454SAndrew Thompson 		    (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
116902ac6454SAndrew Thompson 		    (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
117002ac6454SAndrew Thompson 		    (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
117102ac6454SAndrew Thompson 		    (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
117202ac6454SAndrew Thompson 		    (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
117302ac6454SAndrew Thompson 		    (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
117402ac6454SAndrew Thompson 		    (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
117502ac6454SAndrew Thompson 	}
117602ac6454SAndrew Thompson #endif
1177a148d44fSHans Petter Selasky 	if (status & EHCI_QTD_HALTED) {
1178a148d44fSHans Petter Selasky 		if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
1179a148d44fSHans Petter Selasky 		    (xfer->xroot->udev->address != 0)) {
1180a148d44fSHans Petter Selasky 			/* try to separate I/O errors from STALL */
1181a148d44fSHans Petter Selasky 			if (EHCI_QTD_GET_CERR(status) == 0)
1182a148d44fSHans Petter Selasky 				return (USB_ERR_IOERROR);
1183a148d44fSHans Petter Selasky 		}
1184a148d44fSHans Petter Selasky 		return (USB_ERR_STALLED);
1185a148d44fSHans Petter Selasky 	}
1186a148d44fSHans Petter Selasky 	return (USB_ERR_NORMAL_COMPLETION);
118702ac6454SAndrew Thompson }
118802ac6454SAndrew Thompson 
118902ac6454SAndrew Thompson static void
ehci_non_isoc_done(struct usb_xfer * xfer)1190760bc48eSAndrew Thompson ehci_non_isoc_done(struct usb_xfer *xfer)
119102ac6454SAndrew Thompson {
1192c8a14b91SAndrew Thompson 	ehci_qh_t *qh;
1193e0a69b51SAndrew Thompson 	usb_error_t err = 0;
119402ac6454SAndrew Thompson 
1195ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1196ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
119702ac6454SAndrew Thompson 
1198b850ecc1SAndrew Thompson #ifdef USB_DEBUG
119902ac6454SAndrew Thompson 	if (ehcidebug > 10) {
120002ac6454SAndrew Thompson 		ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
120102ac6454SAndrew Thompson 
120202ac6454SAndrew Thompson 		ehci_dump_sqtds(sc, xfer->td_transfer_first);
120302ac6454SAndrew Thompson 	}
120402ac6454SAndrew Thompson #endif
120502ac6454SAndrew Thompson 
1206c8a14b91SAndrew Thompson 	/* extract data toggle directly from the QH's overlay area */
1207c8a14b91SAndrew Thompson 
1208c8a14b91SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1209c8a14b91SAndrew Thompson 
1210c8a14b91SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
1211c8a14b91SAndrew Thompson 
121202ac6454SAndrew Thompson 	/* reset scanner */
121302ac6454SAndrew Thompson 
121402ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
121502ac6454SAndrew Thompson 
121602ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
121702ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
121802ac6454SAndrew Thompson 			err = ehci_non_isoc_done_sub(xfer);
121902ac6454SAndrew Thompson 		}
122002ac6454SAndrew Thompson 		xfer->aframes = 1;
122102ac6454SAndrew Thompson 
122202ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
122302ac6454SAndrew Thompson 			goto done;
122402ac6454SAndrew Thompson 		}
122502ac6454SAndrew Thompson 	}
122602ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
122702ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
122802ac6454SAndrew Thompson 		xfer->aframes++;
122902ac6454SAndrew Thompson 
123002ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
123102ac6454SAndrew Thompson 			goto done;
123202ac6454SAndrew Thompson 		}
123302ac6454SAndrew Thompson 	}
123402ac6454SAndrew Thompson 
123502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
123602ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
123702ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
123802ac6454SAndrew Thompson 	}
123902ac6454SAndrew Thompson done:
124002ac6454SAndrew Thompson 	ehci_device_done(xfer, err);
124102ac6454SAndrew Thompson }
124202ac6454SAndrew Thompson 
124302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
124402ac6454SAndrew Thompson  *	ehci_check_transfer
124502ac6454SAndrew Thompson  *
124602ac6454SAndrew Thompson  * Return values:
124702ac6454SAndrew Thompson  *    0: USB transfer is not finished
124802ac6454SAndrew Thompson  * Else: USB transfer is finished
124902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
125002ac6454SAndrew Thompson static uint8_t
ehci_check_transfer(struct usb_xfer * xfer)1251760bc48eSAndrew Thompson ehci_check_transfer(struct usb_xfer *xfer)
125202ac6454SAndrew Thompson {
1253e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods = xfer->endpoint->methods;
125402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
125502ac6454SAndrew Thompson 
125602ac6454SAndrew Thompson 	uint32_t status;
125702ac6454SAndrew Thompson 
125802ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
125902ac6454SAndrew Thompson 
126002ac6454SAndrew Thompson 	if (methods == &ehci_device_isoc_fs_methods) {
126102ac6454SAndrew Thompson 		ehci_sitd_t *td;
126202ac6454SAndrew Thompson 
126302ac6454SAndrew Thompson 		/* isochronous full speed transfer */
126402ac6454SAndrew Thompson 
126502ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1266a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1267e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
126802ac6454SAndrew Thompson 
126902ac6454SAndrew Thompson 		/* also check if first is complete */
127002ac6454SAndrew Thompson 
127102ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1272a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1273e55e1ebcSAndrew Thompson 		status |= hc32toh(sc, td->sitd_status);
127402ac6454SAndrew Thompson 
127502ac6454SAndrew Thompson 		if (!(status & EHCI_SITD_ACTIVE)) {
127602ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
127702ac6454SAndrew Thompson 			goto transferred;
127802ac6454SAndrew Thompson 		}
127902ac6454SAndrew Thompson 	} else if (methods == &ehci_device_isoc_hs_methods) {
128002ac6454SAndrew Thompson 		ehci_itd_t *td;
128102ac6454SAndrew Thompson 
128202ac6454SAndrew Thompson 		/* isochronous high speed transfer */
128302ac6454SAndrew Thompson 
128489119752SAndrew Thompson 		/* check last transfer */
128502ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1286a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1287d1f7c4baSAndrew Thompson 		status = td->itd_status[0];
1288d1f7c4baSAndrew Thompson 		status |= td->itd_status[1];
1289d1f7c4baSAndrew Thompson 		status |= td->itd_status[2];
1290d1f7c4baSAndrew Thompson 		status |= td->itd_status[3];
1291d1f7c4baSAndrew Thompson 		status |= td->itd_status[4];
1292d1f7c4baSAndrew Thompson 		status |= td->itd_status[5];
1293d1f7c4baSAndrew Thompson 		status |= td->itd_status[6];
1294d1f7c4baSAndrew Thompson 		status |= td->itd_status[7];
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson 		/* also check first transfer */
129702ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1298a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
129989119752SAndrew Thompson 		status |= td->itd_status[0];
1300d1f7c4baSAndrew Thompson 		status |= td->itd_status[1];
1301d1f7c4baSAndrew Thompson 		status |= td->itd_status[2];
1302d1f7c4baSAndrew Thompson 		status |= td->itd_status[3];
1303d1f7c4baSAndrew Thompson 		status |= td->itd_status[4];
1304d1f7c4baSAndrew Thompson 		status |= td->itd_status[5];
1305d1f7c4baSAndrew Thompson 		status |= td->itd_status[6];
1306d1f7c4baSAndrew Thompson 		status |= td->itd_status[7];
130702ac6454SAndrew Thompson 
130802ac6454SAndrew Thompson 		/* if no transactions are active we continue */
1309e55e1ebcSAndrew Thompson 		if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
131002ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
131102ac6454SAndrew Thompson 			goto transferred;
131202ac6454SAndrew Thompson 		}
131302ac6454SAndrew Thompson 	} else {
131402ac6454SAndrew Thompson 		ehci_qtd_t *td;
1315c8a14b91SAndrew Thompson 		ehci_qh_t *qh;
131602ac6454SAndrew Thompson 
131702ac6454SAndrew Thompson 		/* non-isochronous transfer */
131802ac6454SAndrew Thompson 
131902ac6454SAndrew Thompson 		/*
132002ac6454SAndrew Thompson 		 * check whether there is an error somewhere in the middle,
132102ac6454SAndrew Thompson 		 * or whether there was a short packet (SPD and not ACTIVE)
132202ac6454SAndrew Thompson 		 */
132302ac6454SAndrew Thompson 		td = xfer->td_transfer_cache;
132402ac6454SAndrew Thompson 
1325c8a14b91SAndrew Thompson 		qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1326c8a14b91SAndrew Thompson 
1327c8a14b91SAndrew Thompson 		usb_pc_cpu_invalidate(qh->page_cache);
1328c8a14b91SAndrew Thompson 
1329c8a14b91SAndrew Thompson 		status = hc32toh(sc, qh->qh_qtd.qtd_status);
1330c8a14b91SAndrew Thompson 		if (status & EHCI_QTD_ACTIVE) {
1331c8a14b91SAndrew Thompson 			/* transfer is pending */
1332c8a14b91SAndrew Thompson 			goto done;
1333c8a14b91SAndrew Thompson 		}
1334c8a14b91SAndrew Thompson 
133502ac6454SAndrew Thompson 		while (1) {
1336a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(td->page_cache);
1337e55e1ebcSAndrew Thompson 			status = hc32toh(sc, td->qtd_status);
133802ac6454SAndrew Thompson 
133902ac6454SAndrew Thompson 			/*
1340c8a14b91SAndrew Thompson 			 * Check if there is an active TD which
1341c8a14b91SAndrew Thompson 			 * indicates that the transfer isn't done.
134202ac6454SAndrew Thompson 			 */
134302ac6454SAndrew Thompson 			if (status & EHCI_QTD_ACTIVE) {
134402ac6454SAndrew Thompson 				/* update cache */
134502ac6454SAndrew Thompson 				xfer->td_transfer_cache = td;
134602ac6454SAndrew Thompson 				goto done;
134702ac6454SAndrew Thompson 			}
134802ac6454SAndrew Thompson 			/*
134902ac6454SAndrew Thompson 			 * last transfer descriptor makes the transfer done
135002ac6454SAndrew Thompson 			 */
135102ac6454SAndrew Thompson 			if (((void *)td) == xfer->td_transfer_last) {
135202ac6454SAndrew Thompson 				break;
135302ac6454SAndrew Thompson 			}
135402ac6454SAndrew Thompson 			/*
135502ac6454SAndrew Thompson 			 * any kind of error makes the transfer done
135602ac6454SAndrew Thompson 			 */
135702ac6454SAndrew Thompson 			if (status & EHCI_QTD_HALTED) {
135802ac6454SAndrew Thompson 				break;
135902ac6454SAndrew Thompson 			}
136002ac6454SAndrew Thompson 			/*
136102ac6454SAndrew Thompson 			 * if there is no alternate next transfer, a short
136202ac6454SAndrew Thompson 			 * packet also makes the transfer done
136302ac6454SAndrew Thompson 			 */
136402ac6454SAndrew Thompson 			if (EHCI_QTD_GET_BYTES(status)) {
136502ac6454SAndrew Thompson 				if (xfer->flags_int.short_frames_ok) {
136602ac6454SAndrew Thompson 					/* follow alt next */
136702ac6454SAndrew Thompson 					if (td->alt_next) {
136802ac6454SAndrew Thompson 						td = td->alt_next;
136902ac6454SAndrew Thompson 						continue;
137002ac6454SAndrew Thompson 					}
137102ac6454SAndrew Thompson 				}
137202ac6454SAndrew Thompson 				/* transfer is done */
137302ac6454SAndrew Thompson 				break;
137402ac6454SAndrew Thompson 			}
137502ac6454SAndrew Thompson 			td = td->obj_next;
137602ac6454SAndrew Thompson 		}
137702ac6454SAndrew Thompson 		ehci_non_isoc_done(xfer);
137802ac6454SAndrew Thompson 		goto transferred;
137902ac6454SAndrew Thompson 	}
138002ac6454SAndrew Thompson 
138102ac6454SAndrew Thompson done:
138202ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
138302ac6454SAndrew Thompson 	return (0);
138402ac6454SAndrew Thompson 
138502ac6454SAndrew Thompson transferred:
138602ac6454SAndrew Thompson 	return (1);
138702ac6454SAndrew Thompson }
138802ac6454SAndrew Thompson 
138902ac6454SAndrew Thompson static void
ehci_pcd_enable(ehci_softc_t * sc)139002ac6454SAndrew Thompson ehci_pcd_enable(ehci_softc_t *sc)
139102ac6454SAndrew Thompson {
139202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
139302ac6454SAndrew Thompson 
139402ac6454SAndrew Thompson 	sc->sc_eintrs |= EHCI_STS_PCD;
139502ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
139602ac6454SAndrew Thompson 
139702ac6454SAndrew Thompson 	/* acknowledge any PCD interrupt */
139802ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
139902ac6454SAndrew Thompson 
140039307315SAndrew Thompson 	ehci_root_intr(sc);
140102ac6454SAndrew Thompson }
140202ac6454SAndrew Thompson 
140302ac6454SAndrew Thompson static void
ehci_interrupt_poll(ehci_softc_t * sc)140402ac6454SAndrew Thompson ehci_interrupt_poll(ehci_softc_t *sc)
140502ac6454SAndrew Thompson {
1406760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
140702ac6454SAndrew Thompson 
140802ac6454SAndrew Thompson repeat:
140902ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
141002ac6454SAndrew Thompson 		/*
141102ac6454SAndrew Thompson 		 * check if transfer is transferred
141202ac6454SAndrew Thompson 		 */
141302ac6454SAndrew Thompson 		if (ehci_check_transfer(xfer)) {
141402ac6454SAndrew Thompson 			/* queue has been modified */
141502ac6454SAndrew Thompson 			goto repeat;
141602ac6454SAndrew Thompson 		}
141702ac6454SAndrew Thompson 	}
141802ac6454SAndrew Thompson }
141902ac6454SAndrew Thompson 
142030b22abeSAndrew Thompson /*
142130b22abeSAndrew Thompson  * Some EHCI chips from VIA / ATI seem to trigger interrupts before
142230b22abeSAndrew Thompson  * writing back the qTD status, or miss signalling occasionally under
142330b22abeSAndrew Thompson  * heavy load.  If the host machine is too fast, we can miss
142430b22abeSAndrew Thompson  * transaction completion - when we scan the active list the
142530b22abeSAndrew Thompson  * transaction still seems to be active. This generally exhibits
142630b22abeSAndrew Thompson  * itself as a umass stall that never recovers.
142730b22abeSAndrew Thompson  *
142830b22abeSAndrew Thompson  * We work around this behaviour by setting up this callback after any
142930b22abeSAndrew Thompson  * softintr that completes with transactions still pending, giving us
143030b22abeSAndrew Thompson  * another chance to check for completion after the writeback has
143130b22abeSAndrew Thompson  * taken place.
143230b22abeSAndrew Thompson  */
143330b22abeSAndrew Thompson static void
ehci_poll_timeout(void * arg)143430b22abeSAndrew Thompson ehci_poll_timeout(void *arg)
143530b22abeSAndrew Thompson {
143630b22abeSAndrew Thompson 	ehci_softc_t *sc = arg;
143730b22abeSAndrew Thompson 
143830b22abeSAndrew Thompson 	DPRINTFN(3, "\n");
143930b22abeSAndrew Thompson 	ehci_interrupt_poll(sc);
144030b22abeSAndrew Thompson }
144130b22abeSAndrew Thompson 
144202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
144302ac6454SAndrew Thompson  *	ehci_interrupt - EHCI interrupt handler
144402ac6454SAndrew Thompson  *
144502ac6454SAndrew Thompson  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
144602ac6454SAndrew Thompson  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
144702ac6454SAndrew Thompson  * is present !
144802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
144902ac6454SAndrew Thompson void
ehci_interrupt(ehci_softc_t * sc)145002ac6454SAndrew Thompson ehci_interrupt(ehci_softc_t *sc)
145102ac6454SAndrew Thompson {
145202ac6454SAndrew Thompson 	uint32_t status;
145302ac6454SAndrew Thompson 
145402ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
145502ac6454SAndrew Thompson 
145602ac6454SAndrew Thompson 	DPRINTFN(16, "real interrupt\n");
145702ac6454SAndrew Thompson 
1458b850ecc1SAndrew Thompson #ifdef USB_DEBUG
145902ac6454SAndrew Thompson 	if (ehcidebug > 15) {
146002ac6454SAndrew Thompson 		ehci_dump_regs(sc);
146102ac6454SAndrew Thompson 	}
146202ac6454SAndrew Thompson #endif
146302ac6454SAndrew Thompson 
146402ac6454SAndrew Thompson 	status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
146502ac6454SAndrew Thompson 	if (status == 0) {
146602ac6454SAndrew Thompson 		/* the interrupt was not for us */
146702ac6454SAndrew Thompson 		goto done;
146802ac6454SAndrew Thompson 	}
146902ac6454SAndrew Thompson 	if (!(status & sc->sc_eintrs)) {
147002ac6454SAndrew Thompson 		goto done;
147102ac6454SAndrew Thompson 	}
147202ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, status);	/* acknowledge */
147302ac6454SAndrew Thompson 
147402ac6454SAndrew Thompson 	status &= sc->sc_eintrs;
147502ac6454SAndrew Thompson 
147602ac6454SAndrew Thompson 	if (status & EHCI_STS_HSE) {
147702ac6454SAndrew Thompson 		printf("%s: unrecoverable error, "
147802ac6454SAndrew Thompson 		    "controller halted\n", __FUNCTION__);
1479b850ecc1SAndrew Thompson #ifdef USB_DEBUG
148002ac6454SAndrew Thompson 		ehci_dump_regs(sc);
148102ac6454SAndrew Thompson 		ehci_dump_isoc(sc);
148202ac6454SAndrew Thompson #endif
148302ac6454SAndrew Thompson 	}
148402ac6454SAndrew Thompson 	if (status & EHCI_STS_PCD) {
148502ac6454SAndrew Thompson 		/*
148602ac6454SAndrew Thompson 		 * Disable PCD interrupt for now, because it will be
148702ac6454SAndrew Thompson 		 * on until the port has been reset.
148802ac6454SAndrew Thompson 		 */
148902ac6454SAndrew Thompson 		sc->sc_eintrs &= ~EHCI_STS_PCD;
149002ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
149102ac6454SAndrew Thompson 
149239307315SAndrew Thompson 		ehci_root_intr(sc);
149302ac6454SAndrew Thompson 
149402ac6454SAndrew Thompson 		/* do not allow RHSC interrupts > 1 per second */
1495a593f6b8SAndrew Thompson 		usb_callout_reset(&sc->sc_tmo_pcd, hz,
149602ac6454SAndrew Thompson 		    (void *)&ehci_pcd_enable, sc);
149702ac6454SAndrew Thompson 	}
149802ac6454SAndrew Thompson 	status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
149902ac6454SAndrew Thompson 
150002ac6454SAndrew Thompson 	if (status != 0) {
150102ac6454SAndrew Thompson 		/* block unprocessed interrupts */
150202ac6454SAndrew Thompson 		sc->sc_eintrs &= ~status;
150302ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
150402ac6454SAndrew Thompson 		printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
150502ac6454SAndrew Thompson 	}
150602ac6454SAndrew Thompson 	/* poll all the USB transfers */
150702ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
150802ac6454SAndrew Thompson 
150930b22abeSAndrew Thompson 	if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) {
151030b22abeSAndrew Thompson 		usb_callout_reset(&sc->sc_tmo_poll, hz / 128,
151130b22abeSAndrew Thompson 		    (void *)&ehci_poll_timeout, sc);
151230b22abeSAndrew Thompson 	}
151330b22abeSAndrew Thompson 
151402ac6454SAndrew Thompson done:
151502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
151602ac6454SAndrew Thompson }
151702ac6454SAndrew Thompson 
151802ac6454SAndrew Thompson /*
151902ac6454SAndrew Thompson  * called when a request does not complete
152002ac6454SAndrew Thompson  */
152102ac6454SAndrew Thompson static void
ehci_timeout(void * arg)152202ac6454SAndrew Thompson ehci_timeout(void *arg)
152302ac6454SAndrew Thompson {
1524760bc48eSAndrew Thompson 	struct usb_xfer *xfer = arg;
152502ac6454SAndrew Thompson 
152602ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
152702ac6454SAndrew Thompson 
152802ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
152902ac6454SAndrew Thompson 
153002ac6454SAndrew Thompson 	/* transfer is transferred */
153102ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_TIMEOUT);
153202ac6454SAndrew Thompson }
153302ac6454SAndrew Thompson 
153402ac6454SAndrew Thompson static void
ehci_do_poll(struct usb_bus * bus)1535760bc48eSAndrew Thompson ehci_do_poll(struct usb_bus *bus)
153602ac6454SAndrew Thompson {
153702ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
153802ac6454SAndrew Thompson 
153902ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
154002ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
154102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
154202ac6454SAndrew Thompson }
154302ac6454SAndrew Thompson 
154402ac6454SAndrew Thompson static void
ehci_setup_standard_chain_sub(struct ehci_std_temp * temp)154502ac6454SAndrew Thompson ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
154602ac6454SAndrew Thompson {
1547760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
154802ac6454SAndrew Thompson 	ehci_qtd_t *td;
154902ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
155002ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
155102ac6454SAndrew Thompson 	uint32_t buf_offset;
155202ac6454SAndrew Thompson 	uint32_t average;
155302ac6454SAndrew Thompson 	uint32_t len_old;
15540f7d4548SAndrew Thompson 	uint32_t terminate;
155553e0bf6eSHans Petter Selasky 	uint32_t qtd_altnext;
155602ac6454SAndrew Thompson 	uint8_t shortpkt_old;
155702ac6454SAndrew Thompson 	uint8_t precompute;
155802ac6454SAndrew Thompson 
155953e0bf6eSHans Petter Selasky 	terminate = temp->sc->sc_terminate_self;
156053e0bf6eSHans Petter Selasky 	qtd_altnext = temp->sc->sc_terminate_self;
156102ac6454SAndrew Thompson 	td_alt_next = NULL;
156202ac6454SAndrew Thompson 	buf_offset = 0;
156302ac6454SAndrew Thompson 	shortpkt_old = temp->shortpkt;
156402ac6454SAndrew Thompson 	len_old = temp->len;
156502ac6454SAndrew Thompson 	precompute = 1;
156602ac6454SAndrew Thompson 
156702ac6454SAndrew Thompson restart:
156802ac6454SAndrew Thompson 
156902ac6454SAndrew Thompson 	td = temp->td;
157002ac6454SAndrew Thompson 	td_next = temp->td_next;
157102ac6454SAndrew Thompson 
157202ac6454SAndrew Thompson 	while (1) {
157302ac6454SAndrew Thompson 		if (temp->len == 0) {
157402ac6454SAndrew Thompson 			if (temp->shortpkt) {
157502ac6454SAndrew Thompson 				break;
157602ac6454SAndrew Thompson 			}
157702ac6454SAndrew Thompson 			/* send a Zero Length Packet, ZLP, last */
157802ac6454SAndrew Thompson 
157902ac6454SAndrew Thompson 			temp->shortpkt = 1;
158002ac6454SAndrew Thompson 			average = 0;
158102ac6454SAndrew Thompson 
158202ac6454SAndrew Thompson 		} else {
158302ac6454SAndrew Thompson 			average = temp->average;
158402ac6454SAndrew Thompson 
158502ac6454SAndrew Thompson 			if (temp->len < average) {
158602ac6454SAndrew Thompson 				if (temp->len % temp->max_frame_size) {
158702ac6454SAndrew Thompson 					temp->shortpkt = 1;
158802ac6454SAndrew Thompson 				}
158902ac6454SAndrew Thompson 				average = temp->len;
159002ac6454SAndrew Thompson 			}
159102ac6454SAndrew Thompson 		}
159202ac6454SAndrew Thompson 
159302ac6454SAndrew Thompson 		if (td_next == NULL) {
159402ac6454SAndrew Thompson 			panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
159502ac6454SAndrew Thompson 		}
159602ac6454SAndrew Thompson 		/* get next TD */
159702ac6454SAndrew Thompson 
159802ac6454SAndrew Thompson 		td = td_next;
159902ac6454SAndrew Thompson 		td_next = td->obj_next;
160002ac6454SAndrew Thompson 
160102ac6454SAndrew Thompson 		/* check if we are pre-computing */
160202ac6454SAndrew Thompson 
160302ac6454SAndrew Thompson 		if (precompute) {
160402ac6454SAndrew Thompson 			/* update remaining length */
160502ac6454SAndrew Thompson 
160602ac6454SAndrew Thompson 			temp->len -= average;
160702ac6454SAndrew Thompson 
160802ac6454SAndrew Thompson 			continue;
160902ac6454SAndrew Thompson 		}
161002ac6454SAndrew Thompson 		/* fill out current TD */
161102ac6454SAndrew Thompson 
161202ac6454SAndrew Thompson 		td->qtd_status =
161302ac6454SAndrew Thompson 		    temp->qtd_status |
1614c8a14b91SAndrew Thompson 		    htohc32(temp->sc, EHCI_QTD_IOC |
1615c8a14b91SAndrew Thompson 			EHCI_QTD_SET_BYTES(average));
161602ac6454SAndrew Thompson 
161702ac6454SAndrew Thompson 		if (average == 0) {
161802ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
161902ac6454SAndrew Thompson 				/* update data toggle, ZLP case */
162002ac6454SAndrew Thompson 
162102ac6454SAndrew Thompson 				temp->qtd_status ^=
1622e55e1ebcSAndrew Thompson 				    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
162302ac6454SAndrew Thompson 			}
162402ac6454SAndrew Thompson 			td->len = 0;
162502ac6454SAndrew Thompson 
16269ae7f7c1SHans Petter Selasky 			/* properly reset reserved fields */
162702ac6454SAndrew Thompson 			td->qtd_buffer[0] = 0;
162802ac6454SAndrew Thompson 			td->qtd_buffer[1] = 0;
16299ae7f7c1SHans Petter Selasky 			td->qtd_buffer[2] = 0;
16309ae7f7c1SHans Petter Selasky 			td->qtd_buffer[3] = 0;
16319ae7f7c1SHans Petter Selasky 			td->qtd_buffer[4] = 0;
16329ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[0] = 0;
163302ac6454SAndrew Thompson 			td->qtd_buffer_hi[1] = 0;
16349ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[2] = 0;
16359ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[3] = 0;
16369ae7f7c1SHans Petter Selasky 			td->qtd_buffer_hi[4] = 0;
163702ac6454SAndrew Thompson 		} else {
163802ac6454SAndrew Thompson 			uint8_t x;
163902ac6454SAndrew Thompson 
164002ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
164102ac6454SAndrew Thompson 				/* update data toggle */
164202ac6454SAndrew Thompson 
1643057b4402SPedro F. Giffuni 				if (howmany(average, temp->max_frame_size) & 1) {
164402ac6454SAndrew Thompson 					temp->qtd_status ^=
1645e55e1ebcSAndrew Thompson 					    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
164602ac6454SAndrew Thompson 				}
164702ac6454SAndrew Thompson 			}
164802ac6454SAndrew Thompson 			td->len = average;
164902ac6454SAndrew Thompson 
165002ac6454SAndrew Thompson 			/* update remaining length */
165102ac6454SAndrew Thompson 
165202ac6454SAndrew Thompson 			temp->len -= average;
165302ac6454SAndrew Thompson 
165402ac6454SAndrew Thompson 			/* fill out buffer pointers */
165502ac6454SAndrew Thompson 
1656a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset, &buf_res);
165702ac6454SAndrew Thompson 			td->qtd_buffer[0] =
1658e55e1ebcSAndrew Thompson 			    htohc32(temp->sc, buf_res.physaddr);
165902ac6454SAndrew Thompson 			td->qtd_buffer_hi[0] = 0;
166002ac6454SAndrew Thompson 
166102ac6454SAndrew Thompson 			x = 1;
166202ac6454SAndrew Thompson 
166302ac6454SAndrew Thompson 			while (average > EHCI_PAGE_SIZE) {
166402ac6454SAndrew Thompson 				average -= EHCI_PAGE_SIZE;
166502ac6454SAndrew Thompson 				buf_offset += EHCI_PAGE_SIZE;
1666a593f6b8SAndrew Thompson 				usbd_get_page(temp->pc, buf_offset, &buf_res);
166702ac6454SAndrew Thompson 				td->qtd_buffer[x] =
1668e55e1ebcSAndrew Thompson 				    htohc32(temp->sc,
166902ac6454SAndrew Thompson 				    buf_res.physaddr & (~0xFFF));
167002ac6454SAndrew Thompson 				td->qtd_buffer_hi[x] = 0;
167102ac6454SAndrew Thompson 				x++;
167202ac6454SAndrew Thompson 			}
167302ac6454SAndrew Thompson 
167402ac6454SAndrew Thompson 			/*
167502ac6454SAndrew Thompson 			 * NOTE: The "average" variable is never zero after
167602ac6454SAndrew Thompson 			 * exiting the loop above !
167702ac6454SAndrew Thompson 			 *
167802ac6454SAndrew Thompson 			 * NOTE: We have to subtract one from the offset to
167902ac6454SAndrew Thompson 			 * ensure that we are computing the physical address
168002ac6454SAndrew Thompson 			 * of a valid page !
168102ac6454SAndrew Thompson 			 */
168202ac6454SAndrew Thompson 			buf_offset += average;
1683a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
168402ac6454SAndrew Thompson 			td->qtd_buffer[x] =
1685e55e1ebcSAndrew Thompson 			    htohc32(temp->sc,
168602ac6454SAndrew Thompson 			    buf_res.physaddr & (~0xFFF));
168702ac6454SAndrew Thompson 			td->qtd_buffer_hi[x] = 0;
16889ae7f7c1SHans Petter Selasky 
16899ae7f7c1SHans Petter Selasky 			/* properly reset reserved fields */
16909ae7f7c1SHans Petter Selasky 			while (++x < EHCI_QTD_NBUFFERS) {
16919ae7f7c1SHans Petter Selasky 				td->qtd_buffer[x] = 0;
16929ae7f7c1SHans Petter Selasky 				td->qtd_buffer_hi[x] = 0;
16939ae7f7c1SHans Petter Selasky 			}
169402ac6454SAndrew Thompson 		}
169502ac6454SAndrew Thompson 
169602ac6454SAndrew Thompson 		if (td_next) {
169702ac6454SAndrew Thompson 			/* link the current TD with the next one */
169802ac6454SAndrew Thompson 			td->qtd_next = td_next->qtd_self;
169902ac6454SAndrew Thompson 		}
170053e0bf6eSHans Petter Selasky 		td->qtd_altnext = qtd_altnext;
170102ac6454SAndrew Thompson 		td->alt_next = td_alt_next;
170202ac6454SAndrew Thompson 
1703a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
170402ac6454SAndrew Thompson 	}
170502ac6454SAndrew Thompson 
170602ac6454SAndrew Thompson 	if (precompute) {
170702ac6454SAndrew Thompson 		precompute = 0;
170802ac6454SAndrew Thompson 
170902ac6454SAndrew Thompson 		/* setup alt next pointer, if any */
17100f7d4548SAndrew Thompson 		if (temp->last_frame) {
17110f7d4548SAndrew Thompson 			td_alt_next = NULL;
171253e0bf6eSHans Petter Selasky 			qtd_altnext = terminate;
171302ac6454SAndrew Thompson 		} else {
171402ac6454SAndrew Thompson 			/* we use this field internally */
171502ac6454SAndrew Thompson 			td_alt_next = td_next;
171653e0bf6eSHans Petter Selasky 			if (temp->setup_alt_next) {
171753e0bf6eSHans Petter Selasky 				qtd_altnext = td_next->qtd_self;
171853e0bf6eSHans Petter Selasky 			} else {
171953e0bf6eSHans Petter Selasky 				qtd_altnext = terminate;
172053e0bf6eSHans Petter Selasky 			}
172102ac6454SAndrew Thompson 		}
172202ac6454SAndrew Thompson 
172302ac6454SAndrew Thompson 		/* restore */
172402ac6454SAndrew Thompson 		temp->shortpkt = shortpkt_old;
172502ac6454SAndrew Thompson 		temp->len = len_old;
172602ac6454SAndrew Thompson 		goto restart;
172702ac6454SAndrew Thompson 	}
172802ac6454SAndrew Thompson 	temp->td = td;
172902ac6454SAndrew Thompson 	temp->td_next = td_next;
173002ac6454SAndrew Thompson }
173102ac6454SAndrew Thompson 
173202ac6454SAndrew Thompson static void
ehci_setup_standard_chain(struct usb_xfer * xfer,ehci_qh_t ** qh_last)1733760bc48eSAndrew Thompson ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
173402ac6454SAndrew Thompson {
173502ac6454SAndrew Thompson 	struct ehci_std_temp temp;
1736e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
173702ac6454SAndrew Thompson 	ehci_qh_t *qh;
173802ac6454SAndrew Thompson 	ehci_qtd_t *td;
173902ac6454SAndrew Thompson 	uint32_t qh_endp;
174002ac6454SAndrew Thompson 	uint32_t qh_endphub;
174102ac6454SAndrew Thompson 	uint32_t x;
174202ac6454SAndrew Thompson 
174302ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1744ae60fdfbSAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
1745a593f6b8SAndrew Thompson 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
174602ac6454SAndrew Thompson 
1747578d0effSAndrew Thompson 	temp.average = xfer->max_hc_frame_size;
174802ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
174902ac6454SAndrew Thompson 	temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
175002ac6454SAndrew Thompson 
175102ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
175202ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
175302ac6454SAndrew Thompson 
175402ac6454SAndrew Thompson 	/* get next DMA set */
175502ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
175602ac6454SAndrew Thompson 
175702ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
175802ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
175902ac6454SAndrew Thompson 
176002ac6454SAndrew Thompson 	temp.td = NULL;
176102ac6454SAndrew Thompson 	temp.td_next = td;
176202ac6454SAndrew Thompson 	temp.qtd_status = 0;
17630f7d4548SAndrew Thompson 	temp.last_frame = 0;
176402ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
176502ac6454SAndrew Thompson 
176602ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
1767ae60fdfbSAndrew Thompson 		if (xfer->endpoint->toggle_next) {
176802ac6454SAndrew Thompson 			/* DATA1 is next */
176902ac6454SAndrew Thompson 			temp.qtd_status |=
1770e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
177102ac6454SAndrew Thompson 		}
177202ac6454SAndrew Thompson 		temp.auto_data_toggle = 0;
177302ac6454SAndrew Thompson 	} else {
177402ac6454SAndrew Thompson 		temp.auto_data_toggle = 1;
177502ac6454SAndrew Thompson 	}
177602ac6454SAndrew Thompson 
177753e0bf6eSHans Petter Selasky 	if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
177853e0bf6eSHans Petter Selasky 	    (xfer->xroot->udev->address != 0)) {
177902ac6454SAndrew Thompson 		/* max 3 retries */
178002ac6454SAndrew Thompson 		temp.qtd_status |=
1781e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
178202ac6454SAndrew Thompson 	}
178302ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
178402ac6454SAndrew Thompson 
178502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
178602ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
1787dbe63d3aSHans Petter Selasky 			xfer->endpoint->toggle_next = 0;
1788dbe63d3aSHans Petter Selasky 
178902ac6454SAndrew Thompson 			temp.qtd_status &=
1790e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1791e55e1ebcSAndrew Thompson 			temp.qtd_status |= htohc32(temp.sc,
17925f128668SAndrew Thompson 			    EHCI_QTD_ACTIVE |
179302ac6454SAndrew Thompson 			    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
179402ac6454SAndrew Thompson 			    EHCI_QTD_SET_TOGGLE(0));
179502ac6454SAndrew Thompson 
179602ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
179702ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + 0;
179802ac6454SAndrew Thompson 			temp.shortpkt = temp.len ? 1 : 0;
17990f7d4548SAndrew Thompson 			/* check for last frame */
18000f7d4548SAndrew Thompson 			if (xfer->nframes == 1) {
18010f7d4548SAndrew Thompson 				/* no STATUS stage yet, SETUP is last */
18020f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
18030f7d4548SAndrew Thompson 					temp.last_frame = 1;
18040f7d4548SAndrew Thompson 					temp.setup_alt_next = 0;
18050f7d4548SAndrew Thompson 				}
18060f7d4548SAndrew Thompson 			}
180702ac6454SAndrew Thompson 			ehci_setup_standard_chain_sub(&temp);
180802ac6454SAndrew Thompson 		}
180902ac6454SAndrew Thompson 		x = 1;
181002ac6454SAndrew Thompson 	} else {
181102ac6454SAndrew Thompson 		x = 0;
181202ac6454SAndrew Thompson 	}
181302ac6454SAndrew Thompson 
181402ac6454SAndrew Thompson 	while (x != xfer->nframes) {
181502ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
181602ac6454SAndrew Thompson 
181702ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
181802ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
181902ac6454SAndrew Thompson 
182002ac6454SAndrew Thompson 		x++;
182102ac6454SAndrew Thompson 
182202ac6454SAndrew Thompson 		if (x == xfer->nframes) {
18230f7d4548SAndrew Thompson 			if (xfer->flags_int.control_xfr) {
18240f7d4548SAndrew Thompson 				/* no STATUS stage yet, DATA is last */
18250f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
18260f7d4548SAndrew Thompson 					temp.last_frame = 1;
182702ac6454SAndrew Thompson 					temp.setup_alt_next = 0;
182802ac6454SAndrew Thompson 				}
18290f7d4548SAndrew Thompson 			} else {
18300f7d4548SAndrew Thompson 				temp.last_frame = 1;
18310f7d4548SAndrew Thompson 				temp.setup_alt_next = 0;
18320f7d4548SAndrew Thompson 			}
18330f7d4548SAndrew Thompson 		}
183402ac6454SAndrew Thompson 		/* keep previous data toggle and error count */
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson 		temp.qtd_status &=
1837e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
183802ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
183902ac6454SAndrew Thompson 
184002ac6454SAndrew Thompson 		if (temp.len == 0) {
184102ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
184202ac6454SAndrew Thompson 
184302ac6454SAndrew Thompson 			temp.shortpkt = 0;
184402ac6454SAndrew Thompson 
184502ac6454SAndrew Thompson 		} else {
184602ac6454SAndrew Thompson 			/* regular data transfer */
184702ac6454SAndrew Thompson 
184802ac6454SAndrew Thompson 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
184902ac6454SAndrew Thompson 		}
185002ac6454SAndrew Thompson 
185102ac6454SAndrew Thompson 		/* set endpoint direction */
185202ac6454SAndrew Thompson 
185302ac6454SAndrew Thompson 		temp.qtd_status |=
1854ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1855e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
185602ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1857e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
185802ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
185902ac6454SAndrew Thompson 
186002ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
186102ac6454SAndrew Thompson 	}
186202ac6454SAndrew Thompson 
186302ac6454SAndrew Thompson 	/* check if we should append a status stage */
186402ac6454SAndrew Thompson 
186502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
186602ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
186702ac6454SAndrew Thompson 		/*
186802ac6454SAndrew Thompson 		 * Send a DATA1 message and invert the current endpoint
186902ac6454SAndrew Thompson 		 * direction.
187002ac6454SAndrew Thompson 		 */
187102ac6454SAndrew Thompson 
1872e55e1ebcSAndrew Thompson 		temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
187302ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
187402ac6454SAndrew Thompson 		temp.qtd_status |=
1875ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1876e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
187702ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
187802ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1)) :
1879e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
188002ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
188102ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
188202ac6454SAndrew Thompson 
188302ac6454SAndrew Thompson 		temp.len = 0;
188402ac6454SAndrew Thompson 		temp.pc = NULL;
188502ac6454SAndrew Thompson 		temp.shortpkt = 0;
18860f7d4548SAndrew Thompson 		temp.last_frame = 1;
18870f7d4548SAndrew Thompson 		temp.setup_alt_next = 0;
188802ac6454SAndrew Thompson 
188902ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
189002ac6454SAndrew Thompson 	}
189102ac6454SAndrew Thompson 	td = temp.td;
189202ac6454SAndrew Thompson 
189302ac6454SAndrew Thompson 	/* the last TD terminates the transfer: */
1894e55e1ebcSAndrew Thompson 	td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1895e55e1ebcSAndrew Thompson 	td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
189602ac6454SAndrew Thompson 
1897a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(td->page_cache);
189802ac6454SAndrew Thompson 
189902ac6454SAndrew Thompson 	/* must have at least one frame! */
190002ac6454SAndrew Thompson 
190102ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
190202ac6454SAndrew Thompson 
1903b850ecc1SAndrew Thompson #ifdef USB_DEBUG
190402ac6454SAndrew Thompson 	if (ehcidebug > 8) {
190502ac6454SAndrew Thompson 		DPRINTF("nexttog=%d; data before transfer:\n",
1906ae60fdfbSAndrew Thompson 		    xfer->endpoint->toggle_next);
190702ac6454SAndrew Thompson 		ehci_dump_sqtds(temp.sc,
190802ac6454SAndrew Thompson 		    xfer->td_transfer_first);
190902ac6454SAndrew Thompson 	}
191002ac6454SAndrew Thompson #endif
191102ac6454SAndrew Thompson 
1912ae60fdfbSAndrew Thompson 	methods = xfer->endpoint->methods;
191302ac6454SAndrew Thompson 
191402ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
191502ac6454SAndrew Thompson 
191602ac6454SAndrew Thompson 	/* the "qh_link" field is filled when the QH is added */
191702ac6454SAndrew Thompson 
191802ac6454SAndrew Thompson 	qh_endp =
191902ac6454SAndrew Thompson 	    (EHCI_QH_SET_ADDR(xfer->address) |
1920ae60fdfbSAndrew Thompson 	    EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
192102ac6454SAndrew Thompson 	    EHCI_QH_SET_MPL(xfer->max_packet_size));
192202ac6454SAndrew Thompson 
1923a593f6b8SAndrew Thompson 	if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
19242094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
192502ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods)
192602ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(8);
192702ac6454SAndrew Thompson 	} else {
1928a593f6b8SAndrew Thompson 		if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
19292094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
193002ac6454SAndrew Thompson 		} else {
19312094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
193202ac6454SAndrew Thompson 		}
193302ac6454SAndrew Thompson 
193402ac6454SAndrew Thompson 		if (methods == &ehci_device_ctrl_methods) {
193502ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_CTL;
193602ac6454SAndrew Thompson 		}
193702ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods) {
193802ac6454SAndrew Thompson 			/* Only try one time per microframe! */
193902ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(1);
194002ac6454SAndrew Thompson 		}
194102ac6454SAndrew Thompson 	}
194202ac6454SAndrew Thompson 
19432094ecdaSAndrew Thompson 	if (temp.auto_data_toggle == 0) {
19442094ecdaSAndrew Thompson 		/* software computes the data toggle */
19452094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_DTC;
19462094ecdaSAndrew Thompson 	}
19472094ecdaSAndrew Thompson 
1948e55e1ebcSAndrew Thompson 	qh->qh_endp = htohc32(temp.sc, qh_endp);
194902ac6454SAndrew Thompson 
195002ac6454SAndrew Thompson 	qh_endphub =
195102ac6454SAndrew Thompson 	    (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
1952f12c6c29SAndrew Thompson 	    EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) |
1953f12c6c29SAndrew Thompson 	    EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) |
195402ac6454SAndrew Thompson 	    EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
195502ac6454SAndrew Thompson 	    EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
195602ac6454SAndrew Thompson 
1957e55e1ebcSAndrew Thompson 	qh->qh_endphub = htohc32(temp.sc, qh_endphub);
19582094ecdaSAndrew Thompson 	qh->qh_curqtd = 0;
195902ac6454SAndrew Thompson 
196002ac6454SAndrew Thompson 	/* fill the overlay qTD */
196102ac6454SAndrew Thompson 
19622094ecdaSAndrew Thompson 	if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
196302ac6454SAndrew Thompson 		/* DATA1 is next */
19642094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
19652094ecdaSAndrew Thompson 	} else {
19662094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = 0;
196702ac6454SAndrew Thompson 	}
19682094ecdaSAndrew Thompson 
196902ac6454SAndrew Thompson 	td = xfer->td_transfer_first;
197002ac6454SAndrew Thompson 
197102ac6454SAndrew Thompson 	qh->qh_qtd.qtd_next = td->qtd_self;
197202ac6454SAndrew Thompson 	qh->qh_qtd.qtd_altnext =
1973e55e1ebcSAndrew Thompson 	    htohc32(temp.sc, EHCI_LINK_TERMINATE);
197402ac6454SAndrew Thompson 
19759ae7f7c1SHans Petter Selasky 	/* properly reset reserved fields */
19769ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[0] = 0;
19779ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[1] = 0;
19789ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[2] = 0;
19799ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[3] = 0;
19809ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer[4] = 0;
19819ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[0] = 0;
19829ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[1] = 0;
19839ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[2] = 0;
19849ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[3] = 0;
19859ae7f7c1SHans Petter Selasky 	qh->qh_qtd.qtd_buffer_hi[4] = 0;
19869ae7f7c1SHans Petter Selasky 
1987a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(qh->page_cache);
198802ac6454SAndrew Thompson 
1989ec8f3127SAndrew Thompson 	if (xfer->xroot->udev->flags.self_suspended == 0) {
199002ac6454SAndrew Thompson 		EHCI_APPEND_QH(qh, *qh_last);
199102ac6454SAndrew Thompson 	}
199202ac6454SAndrew Thompson }
199302ac6454SAndrew Thompson 
199402ac6454SAndrew Thompson static void
ehci_root_intr(ehci_softc_t * sc)199539307315SAndrew Thompson ehci_root_intr(ehci_softc_t *sc)
199602ac6454SAndrew Thompson {
199702ac6454SAndrew Thompson 	uint16_t i;
199802ac6454SAndrew Thompson 	uint16_t m;
199902ac6454SAndrew Thompson 
200002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
200102ac6454SAndrew Thompson 
200202ac6454SAndrew Thompson 	/* clear any old interrupt data */
200339307315SAndrew Thompson 	memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
200402ac6454SAndrew Thompson 
200502ac6454SAndrew Thompson 	/* set bits */
200602ac6454SAndrew Thompson 	m = (sc->sc_noport + 1);
200702ac6454SAndrew Thompson 	if (m > (8 * sizeof(sc->sc_hub_idata))) {
200802ac6454SAndrew Thompson 		m = (8 * sizeof(sc->sc_hub_idata));
200902ac6454SAndrew Thompson 	}
201002ac6454SAndrew Thompson 	for (i = 1; i < m; i++) {
201102ac6454SAndrew Thompson 		/* pick out CHANGE bits from the status register */
201202ac6454SAndrew Thompson 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
201302ac6454SAndrew Thompson 			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
201402ac6454SAndrew Thompson 			DPRINTF("port %d changed\n", i);
201502ac6454SAndrew Thompson 		}
201602ac6454SAndrew Thompson 	}
201739307315SAndrew Thompson 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
201839307315SAndrew Thompson 	    sizeof(sc->sc_hub_idata));
201902ac6454SAndrew Thompson }
202002ac6454SAndrew Thompson 
202102ac6454SAndrew Thompson static void
ehci_isoc_fs_done(ehci_softc_t * sc,struct usb_xfer * xfer)2022760bc48eSAndrew Thompson ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
202302ac6454SAndrew Thompson {
202402ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
202502ac6454SAndrew Thompson 	uint32_t status;
202602ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
202702ac6454SAndrew Thompson 	uint16_t len = 0;
202802ac6454SAndrew Thompson 	ehci_sitd_t *td = xfer->td_transfer_first;
202902ac6454SAndrew Thompson 	ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
203002ac6454SAndrew Thompson 
2031ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2032ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
203302ac6454SAndrew Thompson 
203402ac6454SAndrew Thompson 	while (nframes--) {
203502ac6454SAndrew Thompson 		if (td == NULL) {
203602ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
203702ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
203802ac6454SAndrew Thompson 		}
203902ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
204002ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
204102ac6454SAndrew Thompson 		}
2042b850ecc1SAndrew Thompson #ifdef USB_DEBUG
204302ac6454SAndrew Thompson 		if (ehcidebug > 15) {
204402ac6454SAndrew Thompson 			DPRINTF("isoc FS-TD\n");
204502ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
204602ac6454SAndrew Thompson 		}
204702ac6454SAndrew Thompson #endif
2048a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2049e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
205002ac6454SAndrew Thompson 
205102ac6454SAndrew Thompson 		len = EHCI_SITD_GET_LEN(status);
205202ac6454SAndrew Thompson 
2053c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2054c773c254SAndrew Thompson 
205502ac6454SAndrew Thompson 		if (*plen >= len) {
205602ac6454SAndrew Thompson 			len = *plen - len;
205702ac6454SAndrew Thompson 		} else {
205802ac6454SAndrew Thompson 			len = 0;
205902ac6454SAndrew Thompson 		}
206002ac6454SAndrew Thompson 
206102ac6454SAndrew Thompson 		*plen = len;
206202ac6454SAndrew Thompson 
206302ac6454SAndrew Thompson 		/* remove FS-TD from schedule */
206402ac6454SAndrew Thompson 		EHCI_REMOVE_FS_TD(td, *pp_last);
206502ac6454SAndrew Thompson 
206602ac6454SAndrew Thompson 		pp_last++;
206702ac6454SAndrew Thompson 		plen++;
206802ac6454SAndrew Thompson 		td = td->obj_next;
206902ac6454SAndrew Thompson 	}
207002ac6454SAndrew Thompson 
207102ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
207202ac6454SAndrew Thompson }
207302ac6454SAndrew Thompson 
207402ac6454SAndrew Thompson static void
ehci_isoc_hs_done(ehci_softc_t * sc,struct usb_xfer * xfer)2075760bc48eSAndrew Thompson ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
207602ac6454SAndrew Thompson {
207702ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
207802ac6454SAndrew Thompson 	uint32_t status;
207902ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
208002ac6454SAndrew Thompson 	uint16_t len = 0;
208102ac6454SAndrew Thompson 	uint8_t td_no = 0;
208202ac6454SAndrew Thompson 	ehci_itd_t *td = xfer->td_transfer_first;
208302ac6454SAndrew Thompson 	ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
208402ac6454SAndrew Thompson 
2085ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2086ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
208702ac6454SAndrew Thompson 
2088883bb300SAndrew Thompson 	while (nframes) {
208902ac6454SAndrew Thompson 		if (td == NULL) {
209002ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
209102ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
209202ac6454SAndrew Thompson 		}
209302ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
209402ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
209502ac6454SAndrew Thompson 		}
2096b850ecc1SAndrew Thompson #ifdef USB_DEBUG
209702ac6454SAndrew Thompson 		if (ehcidebug > 15) {
209802ac6454SAndrew Thompson 			DPRINTF("isoc HS-TD\n");
209902ac6454SAndrew Thompson 			ehci_dump_itd(sc, td);
210002ac6454SAndrew Thompson 		}
210102ac6454SAndrew Thompson #endif
210202ac6454SAndrew Thompson 
2103a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2104e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->itd_status[td_no]);
210502ac6454SAndrew Thompson 
210602ac6454SAndrew Thompson 		len = EHCI_ITD_GET_LEN(status);
210702ac6454SAndrew Thompson 
2108c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2109c773c254SAndrew Thompson 
2110f12c6c29SAndrew Thompson 		if (xfer->endpoint->usb_smask & (1 << td_no)) {
211102ac6454SAndrew Thompson 			if (*plen >= len) {
211202ac6454SAndrew Thompson 				/*
2113883bb300SAndrew Thompson 				 * The length is valid. NOTE: The
2114883bb300SAndrew Thompson 				 * complete length is written back
2115883bb300SAndrew Thompson 				 * into the status field, and not the
2116883bb300SAndrew Thompson 				 * remainder like with other transfer
2117883bb300SAndrew Thompson 				 * descriptor types.
211802ac6454SAndrew Thompson 				 */
211902ac6454SAndrew Thompson 			} else {
212002ac6454SAndrew Thompson 				/* Invalid length - truncate */
212102ac6454SAndrew Thompson 				len = 0;
212202ac6454SAndrew Thompson 			}
212302ac6454SAndrew Thompson 
212402ac6454SAndrew Thompson 			*plen = len;
212502ac6454SAndrew Thompson 			plen++;
2126883bb300SAndrew Thompson 			nframes--;
2127883bb300SAndrew Thompson 		}
2128883bb300SAndrew Thompson 
212902ac6454SAndrew Thompson 		td_no++;
213002ac6454SAndrew Thompson 
213102ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
213202ac6454SAndrew Thompson 			/* remove HS-TD from schedule */
213302ac6454SAndrew Thompson 			EHCI_REMOVE_HS_TD(td, *pp_last);
213402ac6454SAndrew Thompson 			pp_last++;
213502ac6454SAndrew Thompson 
213602ac6454SAndrew Thompson 			td_no = 0;
213702ac6454SAndrew Thompson 			td = td->obj_next;
213802ac6454SAndrew Thompson 		}
213902ac6454SAndrew Thompson 	}
214002ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
214102ac6454SAndrew Thompson }
214202ac6454SAndrew Thompson 
214302ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row,
214402ac6454SAndrew Thompson  * from close and from interrupt
214502ac6454SAndrew Thompson  */
214602ac6454SAndrew Thompson static void
ehci_device_done(struct usb_xfer * xfer,usb_error_t error)2147e0a69b51SAndrew Thompson ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
214802ac6454SAndrew Thompson {
2149e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods = xfer->endpoint->methods;
215002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
215102ac6454SAndrew Thompson 
215202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
215302ac6454SAndrew Thompson 
2154ae60fdfbSAndrew Thompson 	DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2155ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint, error);
215602ac6454SAndrew Thompson 
215702ac6454SAndrew Thompson 	if ((methods == &ehci_device_bulk_methods) ||
215802ac6454SAndrew Thompson 	    (methods == &ehci_device_ctrl_methods)) {
2159b850ecc1SAndrew Thompson #ifdef USB_DEBUG
216002ac6454SAndrew Thompson 		if (ehcidebug > 8) {
216102ac6454SAndrew Thompson 			DPRINTF("nexttog=%d; data after transfer:\n",
2162ae60fdfbSAndrew Thompson 			    xfer->endpoint->toggle_next);
216302ac6454SAndrew Thompson 			ehci_dump_sqtds(sc,
216402ac6454SAndrew Thompson 			    xfer->td_transfer_first);
216502ac6454SAndrew Thompson 		}
216602ac6454SAndrew Thompson #endif
216702ac6454SAndrew Thompson 
216802ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
216902ac6454SAndrew Thompson 		    sc->sc_async_p_last);
217002ac6454SAndrew Thompson 	}
217102ac6454SAndrew Thompson 	if (methods == &ehci_device_intr_methods) {
217202ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
217302ac6454SAndrew Thompson 		    sc->sc_intr_p_last[xfer->qh_pos]);
217402ac6454SAndrew Thompson 	}
217502ac6454SAndrew Thompson 	/*
217602ac6454SAndrew Thompson 	 * Only finish isochronous transfers once which will update
217702ac6454SAndrew Thompson 	 * "xfer->frlengths".
217802ac6454SAndrew Thompson 	 */
217902ac6454SAndrew Thompson 	if (xfer->td_transfer_first &&
218002ac6454SAndrew Thompson 	    xfer->td_transfer_last) {
218102ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_fs_methods) {
218202ac6454SAndrew Thompson 			ehci_isoc_fs_done(sc, xfer);
218302ac6454SAndrew Thompson 		}
218402ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_hs_methods) {
218502ac6454SAndrew Thompson 			ehci_isoc_hs_done(sc, xfer);
218602ac6454SAndrew Thompson 		}
218702ac6454SAndrew Thompson 		xfer->td_transfer_first = NULL;
218802ac6454SAndrew Thompson 		xfer->td_transfer_last = NULL;
218902ac6454SAndrew Thompson 	}
219002ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
2191a593f6b8SAndrew Thompson 	usbd_transfer_done(xfer, error);
219202ac6454SAndrew Thompson }
219302ac6454SAndrew Thompson 
219402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
219502ac6454SAndrew Thompson  * ehci bulk support
219602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
219702ac6454SAndrew Thompson static void
ehci_device_bulk_open(struct usb_xfer * xfer)2198760bc48eSAndrew Thompson ehci_device_bulk_open(struct usb_xfer *xfer)
219902ac6454SAndrew Thompson {
220002ac6454SAndrew Thompson 	return;
220102ac6454SAndrew Thompson }
220202ac6454SAndrew Thompson 
220302ac6454SAndrew Thompson static void
ehci_device_bulk_close(struct usb_xfer * xfer)2204760bc48eSAndrew Thompson ehci_device_bulk_close(struct usb_xfer *xfer)
220502ac6454SAndrew Thompson {
220602ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
220702ac6454SAndrew Thompson }
220802ac6454SAndrew Thompson 
220902ac6454SAndrew Thompson static void
ehci_device_bulk_enter(struct usb_xfer * xfer)2210760bc48eSAndrew Thompson ehci_device_bulk_enter(struct usb_xfer *xfer)
221102ac6454SAndrew Thompson {
221202ac6454SAndrew Thompson 	return;
221302ac6454SAndrew Thompson }
221402ac6454SAndrew Thompson 
221502ac6454SAndrew Thompson static void
ehci_doorbell_async(struct ehci_softc * sc)22164a6af125SHans Petter Selasky ehci_doorbell_async(struct ehci_softc *sc)
22174a6af125SHans Petter Selasky {
22184a6af125SHans Petter Selasky 	uint32_t temp;
22194a6af125SHans Petter Selasky 
22204a6af125SHans Petter Selasky 	/*
22214a6af125SHans Petter Selasky 	 * XXX Performance quirk: Some Host Controllers have a too low
22224a6af125SHans Petter Selasky 	 * interrupt rate. Issue an IAAD to stimulate the Host
22234a6af125SHans Petter Selasky 	 * Controller after queueing the BULK transfer.
22244a6af125SHans Petter Selasky 	 *
22254a6af125SHans Petter Selasky 	 * XXX Force the host controller to refresh any QH caches.
22264a6af125SHans Petter Selasky 	 */
22274a6af125SHans Petter Selasky 	temp = EOREAD4(sc, EHCI_USBCMD);
22284a6af125SHans Petter Selasky 	if (!(temp & EHCI_CMD_IAAD))
22294a6af125SHans Petter Selasky 		EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
22304a6af125SHans Petter Selasky }
22314a6af125SHans Petter Selasky 
22324a6af125SHans Petter Selasky static void
ehci_device_bulk_start(struct usb_xfer * xfer)2233760bc48eSAndrew Thompson ehci_device_bulk_start(struct usb_xfer *xfer)
223402ac6454SAndrew Thompson {
223502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
223602ac6454SAndrew Thompson 
223702ac6454SAndrew Thompson 	/* setup TD's and QH */
223802ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
223902ac6454SAndrew Thompson 
224002ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
224102ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
2242d1864afbSAndrew Thompson 
2243cf223a88SAndrew Thompson 	/*
2244cf223a88SAndrew Thompson 	 * XXX Certain nVidia chipsets choke when using the IAAD
2245cf223a88SAndrew Thompson 	 * feature too frequently.
2246cf223a88SAndrew Thompson 	 */
2247cf223a88SAndrew Thompson 	if (sc->sc_flags & EHCI_SCFLG_IAADBUG)
2248cf223a88SAndrew Thompson 		return;
2249cf223a88SAndrew Thompson 
22504a6af125SHans Petter Selasky 	ehci_doorbell_async(sc);
225102ac6454SAndrew Thompson }
225202ac6454SAndrew Thompson 
2253e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_bulk_methods =
225402ac6454SAndrew Thompson {
225502ac6454SAndrew Thompson 	.open = ehci_device_bulk_open,
225602ac6454SAndrew Thompson 	.close = ehci_device_bulk_close,
225702ac6454SAndrew Thompson 	.enter = ehci_device_bulk_enter,
225802ac6454SAndrew Thompson 	.start = ehci_device_bulk_start,
225902ac6454SAndrew Thompson };
226002ac6454SAndrew Thompson 
226102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
226202ac6454SAndrew Thompson  * ehci control support
226302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
226402ac6454SAndrew Thompson static void
ehci_device_ctrl_open(struct usb_xfer * xfer)2265760bc48eSAndrew Thompson ehci_device_ctrl_open(struct usb_xfer *xfer)
226602ac6454SAndrew Thompson {
226702ac6454SAndrew Thompson 	return;
226802ac6454SAndrew Thompson }
226902ac6454SAndrew Thompson 
227002ac6454SAndrew Thompson static void
ehci_device_ctrl_close(struct usb_xfer * xfer)2271760bc48eSAndrew Thompson ehci_device_ctrl_close(struct usb_xfer *xfer)
227202ac6454SAndrew Thompson {
227302ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
227402ac6454SAndrew Thompson }
227502ac6454SAndrew Thompson 
227602ac6454SAndrew Thompson static void
ehci_device_ctrl_enter(struct usb_xfer * xfer)2277760bc48eSAndrew Thompson ehci_device_ctrl_enter(struct usb_xfer *xfer)
227802ac6454SAndrew Thompson {
227902ac6454SAndrew Thompson 	return;
228002ac6454SAndrew Thompson }
228102ac6454SAndrew Thompson 
228202ac6454SAndrew Thompson static void
ehci_device_ctrl_start(struct usb_xfer * xfer)2283760bc48eSAndrew Thompson ehci_device_ctrl_start(struct usb_xfer *xfer)
228402ac6454SAndrew Thompson {
228502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
228602ac6454SAndrew Thompson 
228702ac6454SAndrew Thompson 	/* setup TD's and QH */
228802ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
228902ac6454SAndrew Thompson 
229002ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
229102ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
229202ac6454SAndrew Thompson }
229302ac6454SAndrew Thompson 
2294e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_ctrl_methods =
229502ac6454SAndrew Thompson {
229602ac6454SAndrew Thompson 	.open = ehci_device_ctrl_open,
229702ac6454SAndrew Thompson 	.close = ehci_device_ctrl_close,
229802ac6454SAndrew Thompson 	.enter = ehci_device_ctrl_enter,
229902ac6454SAndrew Thompson 	.start = ehci_device_ctrl_start,
230002ac6454SAndrew Thompson };
230102ac6454SAndrew Thompson 
230202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
230302ac6454SAndrew Thompson  * ehci interrupt support
230402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
230502ac6454SAndrew Thompson static void
ehci_device_intr_open(struct usb_xfer * xfer)2306760bc48eSAndrew Thompson ehci_device_intr_open(struct usb_xfer *xfer)
230702ac6454SAndrew Thompson {
230802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
230902ac6454SAndrew Thompson 	uint16_t best;
231002ac6454SAndrew Thompson 	uint16_t bit;
231102ac6454SAndrew Thompson 	uint16_t x;
231202ac6454SAndrew Thompson 
2313f12c6c29SAndrew Thompson 	usb_hs_bandwidth_alloc(xfer);
231402ac6454SAndrew Thompson 
231502ac6454SAndrew Thompson 	/*
231602ac6454SAndrew Thompson 	 * Find the best QH position corresponding to the given interval:
231702ac6454SAndrew Thompson 	 */
231802ac6454SAndrew Thompson 
231902ac6454SAndrew Thompson 	best = 0;
232002ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
232102ac6454SAndrew Thompson 	while (bit) {
232202ac6454SAndrew Thompson 		if (xfer->interval >= bit) {
232302ac6454SAndrew Thompson 			x = bit;
232402ac6454SAndrew Thompson 			best = bit;
232502ac6454SAndrew Thompson 			while (x & bit) {
232602ac6454SAndrew Thompson 				if (sc->sc_intr_stat[x] <
232702ac6454SAndrew Thompson 				    sc->sc_intr_stat[best]) {
232802ac6454SAndrew Thompson 					best = x;
232902ac6454SAndrew Thompson 				}
233002ac6454SAndrew Thompson 				x++;
233102ac6454SAndrew Thompson 			}
233202ac6454SAndrew Thompson 			break;
233302ac6454SAndrew Thompson 		}
233402ac6454SAndrew Thompson 		bit >>= 1;
233502ac6454SAndrew Thompson 	}
233602ac6454SAndrew Thompson 
233702ac6454SAndrew Thompson 	sc->sc_intr_stat[best]++;
233802ac6454SAndrew Thompson 	xfer->qh_pos = best;
233902ac6454SAndrew Thompson 
234002ac6454SAndrew Thompson 	DPRINTFN(3, "best=%d interval=%d\n",
234102ac6454SAndrew Thompson 	    best, xfer->interval);
234202ac6454SAndrew Thompson }
234302ac6454SAndrew Thompson 
234402ac6454SAndrew Thompson static void
ehci_device_intr_close(struct usb_xfer * xfer)2345760bc48eSAndrew Thompson ehci_device_intr_close(struct usb_xfer *xfer)
234602ac6454SAndrew Thompson {
234702ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
234802ac6454SAndrew Thompson 
234902ac6454SAndrew Thompson 	sc->sc_intr_stat[xfer->qh_pos]--;
235002ac6454SAndrew Thompson 
235102ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
2352f12c6c29SAndrew Thompson 
2353f12c6c29SAndrew Thompson 	/* bandwidth must be freed after device done */
2354f12c6c29SAndrew Thompson 	usb_hs_bandwidth_free(xfer);
235502ac6454SAndrew Thompson }
235602ac6454SAndrew Thompson 
235702ac6454SAndrew Thompson static void
ehci_device_intr_enter(struct usb_xfer * xfer)2358760bc48eSAndrew Thompson ehci_device_intr_enter(struct usb_xfer *xfer)
235902ac6454SAndrew Thompson {
236002ac6454SAndrew Thompson 	return;
236102ac6454SAndrew Thompson }
236202ac6454SAndrew Thompson 
236302ac6454SAndrew Thompson static void
ehci_device_intr_start(struct usb_xfer * xfer)2364760bc48eSAndrew Thompson ehci_device_intr_start(struct usb_xfer *xfer)
236502ac6454SAndrew Thompson {
236602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
236702ac6454SAndrew Thompson 
236802ac6454SAndrew Thompson 	/* setup TD's and QH */
236902ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
237002ac6454SAndrew Thompson 
237102ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
237202ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
237302ac6454SAndrew Thompson }
237402ac6454SAndrew Thompson 
2375e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_intr_methods =
237602ac6454SAndrew Thompson {
237702ac6454SAndrew Thompson 	.open = ehci_device_intr_open,
237802ac6454SAndrew Thompson 	.close = ehci_device_intr_close,
237902ac6454SAndrew Thompson 	.enter = ehci_device_intr_enter,
238002ac6454SAndrew Thompson 	.start = ehci_device_intr_start,
238102ac6454SAndrew Thompson };
238202ac6454SAndrew Thompson 
238302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
238402ac6454SAndrew Thompson  * ehci full speed isochronous support
238502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
238602ac6454SAndrew Thompson static void
ehci_device_isoc_fs_open(struct usb_xfer * xfer)2387760bc48eSAndrew Thompson ehci_device_isoc_fs_open(struct usb_xfer *xfer)
238802ac6454SAndrew Thompson {
238902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
239002ac6454SAndrew Thompson 	ehci_sitd_t *td;
239102ac6454SAndrew Thompson 	uint32_t sitd_portaddr;
239202ac6454SAndrew Thompson 	uint8_t ds;
239302ac6454SAndrew Thompson 
239402ac6454SAndrew Thompson 	sitd_portaddr =
239502ac6454SAndrew Thompson 	    EHCI_SITD_SET_ADDR(xfer->address) |
2396ae60fdfbSAndrew Thompson 	    EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
239702ac6454SAndrew Thompson 	    EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
239802ac6454SAndrew Thompson 	    EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
239902ac6454SAndrew Thompson 
24000a4cc48fSHans Petter Selasky 	if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
240102ac6454SAndrew Thompson 		sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
24020a4cc48fSHans Petter Selasky 
2403e55e1ebcSAndrew Thompson 	sitd_portaddr = htohc32(sc, sitd_portaddr);
240402ac6454SAndrew Thompson 
240502ac6454SAndrew Thompson 	/* initialize all TD's */
240602ac6454SAndrew Thompson 
240702ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
240802ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
240902ac6454SAndrew Thompson 			td->sitd_portaddr = sitd_portaddr;
241002ac6454SAndrew Thompson 
241102ac6454SAndrew Thompson 			/*
241202ac6454SAndrew Thompson 			 * TODO: make some kind of automatic
241302ac6454SAndrew Thompson 			 * SMASK/CMASK selection based on micro-frame
241402ac6454SAndrew Thompson 			 * usage
241502ac6454SAndrew Thompson 			 *
241602ac6454SAndrew Thompson 			 * micro-frame usage (8 microframes per 1ms)
241702ac6454SAndrew Thompson 			 */
2418e55e1ebcSAndrew Thompson 			td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
241902ac6454SAndrew Thompson 
2420a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
242102ac6454SAndrew Thompson 		}
242202ac6454SAndrew Thompson 	}
242302ac6454SAndrew Thompson }
242402ac6454SAndrew Thompson 
242502ac6454SAndrew Thompson static void
ehci_device_isoc_fs_close(struct usb_xfer * xfer)2426760bc48eSAndrew Thompson ehci_device_isoc_fs_close(struct usb_xfer *xfer)
242702ac6454SAndrew Thompson {
242802ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
242902ac6454SAndrew Thompson }
243002ac6454SAndrew Thompson 
243102ac6454SAndrew Thompson static void
ehci_device_isoc_fs_enter(struct usb_xfer * xfer)2432760bc48eSAndrew Thompson ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
243302ac6454SAndrew Thompson {
2434760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
243502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
243602ac6454SAndrew Thompson 	ehci_sitd_t *td;
243702ac6454SAndrew Thompson 	ehci_sitd_t *td_last = NULL;
243802ac6454SAndrew Thompson 	ehci_sitd_t **pp_last;
243902ac6454SAndrew Thompson 	uint32_t *plen;
244002ac6454SAndrew Thompson 	uint32_t buf_offset;
244102ac6454SAndrew Thompson 	uint32_t nframes;
24428fc2a3c4SHans Petter Selasky 	uint32_t startframe;
244302ac6454SAndrew Thompson 	uint32_t temp;
244402ac6454SAndrew Thompson 	uint32_t sitd_mask;
244502ac6454SAndrew Thompson 	uint16_t tlen;
244602ac6454SAndrew Thompson 	uint8_t sa;
244702ac6454SAndrew Thompson 	uint8_t sb;
244802ac6454SAndrew Thompson 
2449b850ecc1SAndrew Thompson #ifdef USB_DEBUG
245002ac6454SAndrew Thompson 	uint8_t once = 1;
245102ac6454SAndrew Thompson 
245202ac6454SAndrew Thompson #endif
245302ac6454SAndrew Thompson 
245402ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2455ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
245602ac6454SAndrew Thompson 
245702ac6454SAndrew Thompson 	/* get the current frame index */
245802ac6454SAndrew Thompson 
245902ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
246002ac6454SAndrew Thompson 
24618fc2a3c4SHans Petter Selasky 	if (usbd_xfer_get_isochronous_start_frame(
24628fc2a3c4SHans Petter Selasky 	    xfer, nframes, 0, 1, EHCI_VIRTUAL_FRAMELIST_COUNT - 1, &startframe))
24638fc2a3c4SHans Petter Selasky 		DPRINTFN(3, "start next=%d\n", startframe);
246402ac6454SAndrew Thompson 
246502ac6454SAndrew Thompson 	/* get the real number of frames */
246602ac6454SAndrew Thompson 
246702ac6454SAndrew Thompson 	nframes = xfer->nframes;
246802ac6454SAndrew Thompson 
246902ac6454SAndrew Thompson 	buf_offset = 0;
247002ac6454SAndrew Thompson 
247102ac6454SAndrew Thompson 	plen = xfer->frlengths;
247202ac6454SAndrew Thompson 
247302ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
247402ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
247502ac6454SAndrew Thompson 
247602ac6454SAndrew Thompson 	/* get next DMA set */
247702ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
247802ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
247902ac6454SAndrew Thompson 
24808fc2a3c4SHans Petter Selasky 	pp_last = &sc->sc_isoc_fs_p_last[startframe];
248102ac6454SAndrew Thompson 
248202ac6454SAndrew Thompson 	/* store starting position */
248302ac6454SAndrew Thompson 
24848fc2a3c4SHans Petter Selasky 	xfer->qh_pos = startframe;
248502ac6454SAndrew Thompson 
248602ac6454SAndrew Thompson 	while (nframes--) {
248702ac6454SAndrew Thompson 		if (td == NULL) {
248802ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
248902ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
249002ac6454SAndrew Thompson 		}
24910a4cc48fSHans Petter Selasky 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT])
249202ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
24930a4cc48fSHans Petter Selasky 
249402ac6454SAndrew Thompson 		/* reuse sitd_portaddr and sitd_back from last transfer */
249502ac6454SAndrew Thompson 
249602ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
2497b850ecc1SAndrew Thompson #ifdef USB_DEBUG
249802ac6454SAndrew Thompson 			if (once) {
249902ac6454SAndrew Thompson 				once = 0;
250002ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d "
250102ac6454SAndrew Thompson 				    "bytes (frame truncated)\n",
250202ac6454SAndrew Thompson 				    __FUNCTION__, *plen,
250302ac6454SAndrew Thompson 				    xfer->max_frame_size);
250402ac6454SAndrew Thompson 			}
250502ac6454SAndrew Thompson #endif
250602ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
250702ac6454SAndrew Thompson 		}
25080a4cc48fSHans Petter Selasky 
25090a4cc48fSHans Petter Selasky 		/* allocate a slot */
25100a4cc48fSHans Petter Selasky 
25110a4cc48fSHans Petter Selasky 		sa = usbd_fs_isoc_schedule_alloc_slot(xfer,
25120a4cc48fSHans Petter Selasky 		    xfer->isoc_time_complete - nframes - 1);
25130a4cc48fSHans Petter Selasky 
25140a4cc48fSHans Petter Selasky 		if (sa == 255) {
251502ac6454SAndrew Thompson 			/*
25160a4cc48fSHans Petter Selasky 			 * Schedule is FULL, set length to zero:
251702ac6454SAndrew Thompson 			 */
25180a4cc48fSHans Petter Selasky 
251902ac6454SAndrew Thompson 			*plen = 0;
25200a4cc48fSHans Petter Selasky 			sa = USB_FS_ISOC_UFRAME_MAX - 1;
252102ac6454SAndrew Thompson 		}
252202ac6454SAndrew Thompson 		if (*plen) {
252302ac6454SAndrew Thompson 			/*
2524a593f6b8SAndrew Thompson 			 * only call "usbd_get_page()" when we have a
252502ac6454SAndrew Thompson 			 * non-zero length
252602ac6454SAndrew Thompson 			 */
2527a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2528e55e1ebcSAndrew Thompson 			td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
252902ac6454SAndrew Thompson 			buf_offset += *plen;
253002ac6454SAndrew Thompson 			/*
253102ac6454SAndrew Thompson 			 * NOTE: We need to subtract one from the offset so
253202ac6454SAndrew Thompson 			 * that we are on a valid page!
253302ac6454SAndrew Thompson 			 */
2534a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset - 1,
253502ac6454SAndrew Thompson 			    &buf_res);
253602ac6454SAndrew Thompson 			temp = buf_res.physaddr & ~0xFFF;
253702ac6454SAndrew Thompson 		} else {
253802ac6454SAndrew Thompson 			td->sitd_bp[0] = 0;
253902ac6454SAndrew Thompson 			temp = 0;
254002ac6454SAndrew Thompson 		}
254102ac6454SAndrew Thompson 
2542ae60fdfbSAndrew Thompson 		if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
254302ac6454SAndrew Thompson 			tlen = *plen;
254402ac6454SAndrew Thompson 			if (tlen <= 188) {
254502ac6454SAndrew Thompson 				temp |= 1;	/* T-count = 1, TP = ALL */
254602ac6454SAndrew Thompson 				tlen = 1;
254702ac6454SAndrew Thompson 			} else {
254802ac6454SAndrew Thompson 				tlen += 187;
254902ac6454SAndrew Thompson 				tlen /= 188;
255002ac6454SAndrew Thompson 				temp |= tlen;	/* T-count = [1..6] */
255102ac6454SAndrew Thompson 				temp |= 8;	/* TP = Begin */
255202ac6454SAndrew Thompson 			}
255302ac6454SAndrew Thompson 
255402ac6454SAndrew Thompson 			tlen += sa;
255502ac6454SAndrew Thompson 
255602ac6454SAndrew Thompson 			if (tlen >= 8) {
255702ac6454SAndrew Thompson 				sb = 0;
255802ac6454SAndrew Thompson 			} else {
255902ac6454SAndrew Thompson 				sb = (1 << tlen);
256002ac6454SAndrew Thompson 			}
256102ac6454SAndrew Thompson 
256202ac6454SAndrew Thompson 			sa = (1 << sa);
256302ac6454SAndrew Thompson 			sa = (sb - sa) & 0x3F;
256402ac6454SAndrew Thompson 			sb = 0;
256502ac6454SAndrew Thompson 		} else {
256602ac6454SAndrew Thompson 			sb = (-(4 << sa)) & 0xFE;
256702ac6454SAndrew Thompson 			sa = (1 << sa) & 0x3F;
256802ac6454SAndrew Thompson 		}
256902ac6454SAndrew Thompson 
257002ac6454SAndrew Thompson 		sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
257102ac6454SAndrew Thompson 		    EHCI_SITD_SET_CMASK(sb));
257202ac6454SAndrew Thompson 
2573e55e1ebcSAndrew Thompson 		td->sitd_bp[1] = htohc32(sc, temp);
257402ac6454SAndrew Thompson 
2575e55e1ebcSAndrew Thompson 		td->sitd_mask = htohc32(sc, sitd_mask);
257602ac6454SAndrew Thompson 
257702ac6454SAndrew Thompson 		if (nframes == 0) {
2578e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
25795f128668SAndrew Thompson 			    EHCI_SITD_IOC |
258002ac6454SAndrew Thompson 			    EHCI_SITD_ACTIVE |
258102ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
258202ac6454SAndrew Thompson 		} else {
2583e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
25845f128668SAndrew Thompson 			    EHCI_SITD_ACTIVE |
258502ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
258602ac6454SAndrew Thompson 		}
2587a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
258802ac6454SAndrew Thompson 
2589b850ecc1SAndrew Thompson #ifdef USB_DEBUG
259002ac6454SAndrew Thompson 		if (ehcidebug > 15) {
259102ac6454SAndrew Thompson 			DPRINTF("FS-TD %d\n", nframes);
259202ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
259302ac6454SAndrew Thompson 		}
259402ac6454SAndrew Thompson #endif
259502ac6454SAndrew Thompson 		/* insert TD into schedule */
259602ac6454SAndrew Thompson 		EHCI_APPEND_FS_TD(td, *pp_last);
259702ac6454SAndrew Thompson 		pp_last++;
259802ac6454SAndrew Thompson 
259902ac6454SAndrew Thompson 		plen++;
260002ac6454SAndrew Thompson 		td_last = td;
260102ac6454SAndrew Thompson 		td = td->obj_next;
260202ac6454SAndrew Thompson 	}
260302ac6454SAndrew Thompson 
260402ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
260502ac6454SAndrew Thompson 
26067e66ab7cSHans Petter Selasky 	/*
26077e66ab7cSHans Petter Selasky 	 * We don't allow cancelling of the SPLIT transaction USB FULL
26087e66ab7cSHans Petter Selasky 	 * speed transfer, because it disturbs the bandwidth
26097e66ab7cSHans Petter Selasky 	 * computation algorithm.
26107e66ab7cSHans Petter Selasky 	 */
26117e66ab7cSHans Petter Selasky 	xfer->flags_int.can_cancel_immed = 0;
261202ac6454SAndrew Thompson }
261302ac6454SAndrew Thompson 
261402ac6454SAndrew Thompson static void
ehci_device_isoc_fs_start(struct usb_xfer * xfer)2615760bc48eSAndrew Thompson ehci_device_isoc_fs_start(struct usb_xfer *xfer)
261602ac6454SAndrew Thompson {
26177e66ab7cSHans Petter Selasky 	/*
26187e66ab7cSHans Petter Selasky 	 * We don't allow cancelling of the SPLIT transaction USB FULL
26197e66ab7cSHans Petter Selasky 	 * speed transfer, because it disturbs the bandwidth
26207e66ab7cSHans Petter Selasky 	 * computation algorithm.
26217e66ab7cSHans Petter Selasky 	 */
26227e66ab7cSHans Petter Selasky 	xfer->flags_int.can_cancel_immed = 0;
26237e66ab7cSHans Petter Selasky 
26247e66ab7cSHans Petter Selasky 	/* set a default timeout */
26257e66ab7cSHans Petter Selasky 	if (xfer->timeout == 0)
26267e66ab7cSHans Petter Selasky 		xfer->timeout = 500; /* ms */
26277e66ab7cSHans Petter Selasky 
262802ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
262902ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
263002ac6454SAndrew Thompson }
263102ac6454SAndrew Thompson 
2632e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_fs_methods =
263302ac6454SAndrew Thompson {
263402ac6454SAndrew Thompson 	.open = ehci_device_isoc_fs_open,
263502ac6454SAndrew Thompson 	.close = ehci_device_isoc_fs_close,
263602ac6454SAndrew Thompson 	.enter = ehci_device_isoc_fs_enter,
263702ac6454SAndrew Thompson 	.start = ehci_device_isoc_fs_start,
263802ac6454SAndrew Thompson };
263902ac6454SAndrew Thompson 
264002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
264102ac6454SAndrew Thompson  * ehci high speed isochronous support
264202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
264302ac6454SAndrew Thompson static void
ehci_device_isoc_hs_open(struct usb_xfer * xfer)2644760bc48eSAndrew Thompson ehci_device_isoc_hs_open(struct usb_xfer *xfer)
264502ac6454SAndrew Thompson {
264602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
264702ac6454SAndrew Thompson 	ehci_itd_t *td;
264802ac6454SAndrew Thompson 	uint32_t temp;
264902ac6454SAndrew Thompson 	uint8_t ds;
2650883bb300SAndrew Thompson 
2651f12c6c29SAndrew Thompson 	usb_hs_bandwidth_alloc(xfer);
265202ac6454SAndrew Thompson 
265302ac6454SAndrew Thompson 	/* initialize all TD's */
265402ac6454SAndrew Thompson 
265502ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
265602ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
265702ac6454SAndrew Thompson 			/* set TD inactive */
265802ac6454SAndrew Thompson 			td->itd_status[0] = 0;
265902ac6454SAndrew Thompson 			td->itd_status[1] = 0;
266002ac6454SAndrew Thompson 			td->itd_status[2] = 0;
266102ac6454SAndrew Thompson 			td->itd_status[3] = 0;
266202ac6454SAndrew Thompson 			td->itd_status[4] = 0;
266302ac6454SAndrew Thompson 			td->itd_status[5] = 0;
266402ac6454SAndrew Thompson 			td->itd_status[6] = 0;
266502ac6454SAndrew Thompson 			td->itd_status[7] = 0;
266602ac6454SAndrew Thompson 
266702ac6454SAndrew Thompson 			/* set endpoint and address */
2668e55e1ebcSAndrew Thompson 			td->itd_bp[0] = htohc32(sc,
26695f128668SAndrew Thompson 			    EHCI_ITD_SET_ADDR(xfer->address) |
2670ae60fdfbSAndrew Thompson 			    EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
267102ac6454SAndrew Thompson 
267202ac6454SAndrew Thompson 			temp =
267302ac6454SAndrew Thompson 			    EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
267402ac6454SAndrew Thompson 
267502ac6454SAndrew Thompson 			/* set direction */
2676ae60fdfbSAndrew Thompson 			if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
267702ac6454SAndrew Thompson 				temp |= EHCI_ITD_SET_DIR_IN;
267802ac6454SAndrew Thompson 			}
267902ac6454SAndrew Thompson 			/* set maximum packet size */
2680e55e1ebcSAndrew Thompson 			td->itd_bp[1] = htohc32(sc, temp);
268102ac6454SAndrew Thompson 
268202ac6454SAndrew Thompson 			/* set transfer multiplier */
2683e55e1ebcSAndrew Thompson 			td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
268402ac6454SAndrew Thompson 
2685a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
268602ac6454SAndrew Thompson 		}
268702ac6454SAndrew Thompson 	}
268802ac6454SAndrew Thompson }
268902ac6454SAndrew Thompson 
269002ac6454SAndrew Thompson static void
ehci_device_isoc_hs_close(struct usb_xfer * xfer)2691760bc48eSAndrew Thompson ehci_device_isoc_hs_close(struct usb_xfer *xfer)
269202ac6454SAndrew Thompson {
269302ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
2694f12c6c29SAndrew Thompson 
2695f12c6c29SAndrew Thompson 	/* bandwidth must be freed after device done */
2696f12c6c29SAndrew Thompson 	usb_hs_bandwidth_free(xfer);
269702ac6454SAndrew Thompson }
269802ac6454SAndrew Thompson 
269902ac6454SAndrew Thompson static void
ehci_device_isoc_hs_enter(struct usb_xfer * xfer)2700760bc48eSAndrew Thompson ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
270102ac6454SAndrew Thompson {
2702760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
270302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
270402ac6454SAndrew Thompson 	ehci_itd_t *td;
270502ac6454SAndrew Thompson 	ehci_itd_t *td_last = NULL;
270602ac6454SAndrew Thompson 	ehci_itd_t **pp_last;
270702ac6454SAndrew Thompson 	bus_size_t page_addr;
270802ac6454SAndrew Thompson 	uint32_t *plen;
270902ac6454SAndrew Thompson 	uint32_t status;
271002ac6454SAndrew Thompson 	uint32_t buf_offset;
271102ac6454SAndrew Thompson 	uint32_t nframes;
27128fc2a3c4SHans Petter Selasky 	uint32_t startframe;
271302ac6454SAndrew Thompson 	uint32_t itd_offset[8 + 1];
271402ac6454SAndrew Thompson 	uint8_t x;
271502ac6454SAndrew Thompson 	uint8_t td_no;
271602ac6454SAndrew Thompson 	uint8_t page_no;
271702ac6454SAndrew Thompson 
2718b850ecc1SAndrew Thompson #ifdef USB_DEBUG
271902ac6454SAndrew Thompson 	uint8_t once = 1;
272002ac6454SAndrew Thompson 
272102ac6454SAndrew Thompson #endif
272202ac6454SAndrew Thompson 
2723d1f7c4baSAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d shift=%d\n",
27248fc2a3c4SHans Petter Selasky 	    xfer, xfer->endpoint->isoc_next, xfer->nframes,
27258fc2a3c4SHans Petter Selasky 	    usbd_xfer_get_fps_shift(xfer));
272602ac6454SAndrew Thompson 
272702ac6454SAndrew Thompson 	/* get the current frame index */
272802ac6454SAndrew Thompson 
272902ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
273002ac6454SAndrew Thompson 
27318fc2a3c4SHans Petter Selasky 	if (usbd_xfer_get_isochronous_start_frame(
27328fc2a3c4SHans Petter Selasky 	    xfer, nframes, 0, 1, EHCI_VIRTUAL_FRAMELIST_COUNT - 1, &startframe))
27338fc2a3c4SHans Petter Selasky 		DPRINTFN(3, "start next=%d\n", startframe);
273402ac6454SAndrew Thompson 
273502ac6454SAndrew Thompson 	nframes = xfer->nframes;
273602ac6454SAndrew Thompson 
273702ac6454SAndrew Thompson 	buf_offset = 0;
273802ac6454SAndrew Thompson 	td_no = 0;
273902ac6454SAndrew Thompson 
274002ac6454SAndrew Thompson 	plen = xfer->frlengths;
274102ac6454SAndrew Thompson 
274202ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
274302ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
274402ac6454SAndrew Thompson 
274502ac6454SAndrew Thompson 	/* get next DMA set */
274602ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
274702ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
274802ac6454SAndrew Thompson 
27498fc2a3c4SHans Petter Selasky 	pp_last = &sc->sc_isoc_hs_p_last[startframe];
275002ac6454SAndrew Thompson 
275102ac6454SAndrew Thompson 	/* store starting position */
275202ac6454SAndrew Thompson 
27538fc2a3c4SHans Petter Selasky 	xfer->qh_pos = startframe;
275402ac6454SAndrew Thompson 
2755883bb300SAndrew Thompson 	while (nframes) {
275602ac6454SAndrew Thompson 		if (td == NULL) {
275702ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
275802ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
275902ac6454SAndrew Thompson 		}
276002ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
276102ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
276202ac6454SAndrew Thompson 		}
276302ac6454SAndrew Thompson 		/* range check */
276402ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
2765b850ecc1SAndrew Thompson #ifdef USB_DEBUG
276602ac6454SAndrew Thompson 			if (once) {
276702ac6454SAndrew Thompson 				once = 0;
276802ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d bytes "
276902ac6454SAndrew Thompson 				    "(frame truncated)\n",
277002ac6454SAndrew Thompson 				    __FUNCTION__, *plen, xfer->max_frame_size);
277102ac6454SAndrew Thompson 			}
277202ac6454SAndrew Thompson #endif
277302ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
277402ac6454SAndrew Thompson 		}
2775883bb300SAndrew Thompson 
2776f12c6c29SAndrew Thompson 		if (xfer->endpoint->usb_smask & (1 << td_no)) {
277702ac6454SAndrew Thompson 			status = (EHCI_ITD_SET_LEN(*plen) |
277802ac6454SAndrew Thompson 			    EHCI_ITD_ACTIVE |
277902ac6454SAndrew Thompson 			    EHCI_ITD_SET_PG(0));
2780e55e1ebcSAndrew Thompson 			td->itd_status[td_no] = htohc32(sc, status);
278102ac6454SAndrew Thompson 			itd_offset[td_no] = buf_offset;
278202ac6454SAndrew Thompson 			buf_offset += *plen;
278302ac6454SAndrew Thompson 			plen++;
2784883bb300SAndrew Thompson 			nframes --;
2785883bb300SAndrew Thompson 		} else {
2786883bb300SAndrew Thompson 			td->itd_status[td_no] = 0;	/* not active */
2787883bb300SAndrew Thompson 			itd_offset[td_no] = buf_offset;
2788883bb300SAndrew Thompson 		}
2789883bb300SAndrew Thompson 
279002ac6454SAndrew Thompson 		td_no++;
279102ac6454SAndrew Thompson 
279202ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
279302ac6454SAndrew Thompson 			/* the rest of the transfers are not active, if any */
279402ac6454SAndrew Thompson 			for (x = td_no; x != 8; x++) {
279502ac6454SAndrew Thompson 				td->itd_status[x] = 0;	/* not active */
279602ac6454SAndrew Thompson 			}
279702ac6454SAndrew Thompson 
279802ac6454SAndrew Thompson 			/* check if there is any data to be transferred */
279902ac6454SAndrew Thompson 			if (itd_offset[0] != buf_offset) {
280002ac6454SAndrew Thompson 				page_no = 0;
280102ac6454SAndrew Thompson 				itd_offset[td_no] = buf_offset;
280202ac6454SAndrew Thompson 
280302ac6454SAndrew Thompson 				/* get first page offset */
2804a593f6b8SAndrew Thompson 				usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
280502ac6454SAndrew Thompson 				/* get page address */
280602ac6454SAndrew Thompson 				page_addr = buf_res.physaddr & ~0xFFF;
280702ac6454SAndrew Thompson 				/* update page address */
2808e55e1ebcSAndrew Thompson 				td->itd_bp[0] &= htohc32(sc, 0xFFF);
2809e55e1ebcSAndrew Thompson 				td->itd_bp[0] |= htohc32(sc, page_addr);
281002ac6454SAndrew Thompson 
281102ac6454SAndrew Thompson 				for (x = 0; x != td_no; x++) {
281202ac6454SAndrew Thompson 					/* set page number and page offset */
281302ac6454SAndrew Thompson 					status = (EHCI_ITD_SET_PG(page_no) |
281402ac6454SAndrew Thompson 					    (buf_res.physaddr & 0xFFF));
2815e55e1ebcSAndrew Thompson 					td->itd_status[x] |= htohc32(sc, status);
281602ac6454SAndrew Thompson 
281702ac6454SAndrew Thompson 					/* get next page offset */
281802ac6454SAndrew Thompson 					if (itd_offset[x + 1] == buf_offset) {
281902ac6454SAndrew Thompson 						/*
282002ac6454SAndrew Thompson 						 * We subtract one so that
282102ac6454SAndrew Thompson 						 * we don't go off the last
282202ac6454SAndrew Thompson 						 * page!
282302ac6454SAndrew Thompson 						 */
2824a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
282502ac6454SAndrew Thompson 					} else {
2826a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
282702ac6454SAndrew Thompson 					}
282802ac6454SAndrew Thompson 
282902ac6454SAndrew Thompson 					/* check if we need a new page */
283002ac6454SAndrew Thompson 					if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
283102ac6454SAndrew Thompson 						/* new page needed */
283202ac6454SAndrew Thompson 						page_addr = buf_res.physaddr & ~0xFFF;
283302ac6454SAndrew Thompson 						if (page_no == 6) {
283402ac6454SAndrew Thompson 							panic("%s: too many pages\n", __FUNCTION__);
283502ac6454SAndrew Thompson 						}
283602ac6454SAndrew Thompson 						page_no++;
283702ac6454SAndrew Thompson 						/* update page address */
2838e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2839e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] |= htohc32(sc, page_addr);
284002ac6454SAndrew Thompson 					}
284102ac6454SAndrew Thompson 				}
284202ac6454SAndrew Thompson 			}
284302ac6454SAndrew Thompson 			/* set IOC bit if we are complete */
284402ac6454SAndrew Thompson 			if (nframes == 0) {
2845883bb300SAndrew Thompson 				td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC);
284602ac6454SAndrew Thompson 			}
2847a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
2848b850ecc1SAndrew Thompson #ifdef USB_DEBUG
284902ac6454SAndrew Thompson 			if (ehcidebug > 15) {
285002ac6454SAndrew Thompson 				DPRINTF("HS-TD %d\n", nframes);
285102ac6454SAndrew Thompson 				ehci_dump_itd(sc, td);
285202ac6454SAndrew Thompson 			}
285302ac6454SAndrew Thompson #endif
285402ac6454SAndrew Thompson 			/* insert TD into schedule */
285502ac6454SAndrew Thompson 			EHCI_APPEND_HS_TD(td, *pp_last);
285602ac6454SAndrew Thompson 			pp_last++;
285702ac6454SAndrew Thompson 
285802ac6454SAndrew Thompson 			td_no = 0;
285902ac6454SAndrew Thompson 			td_last = td;
286002ac6454SAndrew Thompson 			td = td->obj_next;
286102ac6454SAndrew Thompson 		}
286202ac6454SAndrew Thompson 	}
286302ac6454SAndrew Thompson 
286402ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
286502ac6454SAndrew Thompson }
286602ac6454SAndrew Thompson 
286702ac6454SAndrew Thompson static void
ehci_device_isoc_hs_start(struct usb_xfer * xfer)2868760bc48eSAndrew Thompson ehci_device_isoc_hs_start(struct usb_xfer *xfer)
286902ac6454SAndrew Thompson {
287002ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
287102ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
287202ac6454SAndrew Thompson }
287302ac6454SAndrew Thompson 
2874e892b3feSHans Petter Selasky static const struct usb_pipe_methods ehci_device_isoc_hs_methods =
287502ac6454SAndrew Thompson {
287602ac6454SAndrew Thompson 	.open = ehci_device_isoc_hs_open,
287702ac6454SAndrew Thompson 	.close = ehci_device_isoc_hs_close,
287802ac6454SAndrew Thompson 	.enter = ehci_device_isoc_hs_enter,
287902ac6454SAndrew Thompson 	.start = ehci_device_isoc_hs_start,
288002ac6454SAndrew Thompson };
288102ac6454SAndrew Thompson 
288202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
288302ac6454SAndrew Thompson  * ehci root control support
288402ac6454SAndrew Thompson  *------------------------------------------------------------------------*
288539307315SAndrew Thompson  * Simulate a hardware hub by handling all the necessary requests.
288602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
288702ac6454SAndrew Thompson 
288802ac6454SAndrew Thompson static const
2889760bc48eSAndrew Thompson struct usb_device_descriptor ehci_devd =
289002ac6454SAndrew Thompson {
2891760bc48eSAndrew Thompson 	sizeof(struct usb_device_descriptor),
289202ac6454SAndrew Thompson 	UDESC_DEVICE,			/* type */
289302ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
289402ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
289502ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
289602ac6454SAndrew Thompson 	UDPROTO_HSHUBSTT,		/* protocol */
289702ac6454SAndrew Thompson 	64,				/* max packet */
289802ac6454SAndrew Thompson 	{0}, {0}, {0x00, 0x01},		/* device id */
289920733245SPedro F. Giffuni 	1, 2, 0,			/* string indexes */
290002ac6454SAndrew Thompson 	1				/* # of configurations */
290102ac6454SAndrew Thompson };
290202ac6454SAndrew Thompson 
290302ac6454SAndrew Thompson static const
2904760bc48eSAndrew Thompson struct usb_device_qualifier ehci_odevd =
290502ac6454SAndrew Thompson {
2906760bc48eSAndrew Thompson 	sizeof(struct usb_device_qualifier),
290702ac6454SAndrew Thompson 	UDESC_DEVICE_QUALIFIER,		/* type */
290802ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
290902ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
291002ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
291102ac6454SAndrew Thompson 	UDPROTO_FSHUB,			/* protocol */
291202ac6454SAndrew Thompson 	0,				/* max packet */
291302ac6454SAndrew Thompson 	0,				/* # of configurations */
291402ac6454SAndrew Thompson 	0
291502ac6454SAndrew Thompson };
291602ac6454SAndrew Thompson 
291702ac6454SAndrew Thompson static const struct ehci_config_desc ehci_confd = {
291802ac6454SAndrew Thompson 	.confd = {
2919760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_config_descriptor),
292002ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
292102ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(ehci_confd),
292202ac6454SAndrew Thompson 		.bNumInterface = 1,
292302ac6454SAndrew Thompson 		.bConfigurationValue = 1,
292402ac6454SAndrew Thompson 		.iConfiguration = 0,
292502ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
292602ac6454SAndrew Thompson 		.bMaxPower = 0		/* max power */
292702ac6454SAndrew Thompson 	},
292802ac6454SAndrew Thompson 	.ifcd = {
2929760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_interface_descriptor),
293002ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
293102ac6454SAndrew Thompson 		.bNumEndpoints = 1,
293202ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
293302ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
29341678e135SHans Petter Selasky 		.bInterfaceProtocol = 0,
293502ac6454SAndrew Thompson 	},
293602ac6454SAndrew Thompson 	.endpd = {
2937760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_endpoint_descriptor),
293802ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
293902ac6454SAndrew Thompson 		.bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
294002ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
294102ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
294202ac6454SAndrew Thompson 		.bInterval = 255,
294302ac6454SAndrew Thompson 	},
294402ac6454SAndrew Thompson };
294502ac6454SAndrew Thompson 
294602ac6454SAndrew Thompson static const
2947760bc48eSAndrew Thompson struct usb_hub_descriptor ehci_hubd =
294802ac6454SAndrew Thompson {
29496d917491SHans Petter Selasky 	.bDescLength = 0,		/* dynamic length */
29506d917491SHans Petter Selasky 	.bDescriptorType = UDESC_HUB,
295102ac6454SAndrew Thompson };
295202ac6454SAndrew Thompson 
2953cdf4ec68SMichal Meloun uint16_t
ehci_get_port_speed_portsc(struct ehci_softc * sc,uint16_t index)2954cdf4ec68SMichal Meloun ehci_get_port_speed_portsc(struct ehci_softc *sc, uint16_t index)
2955cdf4ec68SMichal Meloun {
2956cdf4ec68SMichal Meloun 	uint32_t v;
2957cdf4ec68SMichal Meloun 
2958cdf4ec68SMichal Meloun 	v = EOREAD4(sc, EHCI_PORTSC(index));
2959cdf4ec68SMichal Meloun 	v = (v >> EHCI_PORTSC_PSPD_SHIFT) & EHCI_PORTSC_PSPD_MASK;
2960cdf4ec68SMichal Meloun 
2961cdf4ec68SMichal Meloun 	if (v == EHCI_PORT_SPEED_HIGH)
2962cdf4ec68SMichal Meloun 		return (UPS_HIGH_SPEED);
2963cdf4ec68SMichal Meloun 	if (v == EHCI_PORT_SPEED_LOW)
2964cdf4ec68SMichal Meloun 		return (UPS_LOW_SPEED);
2965cdf4ec68SMichal Meloun 	return (0);
2966cdf4ec68SMichal Meloun }
2967cdf4ec68SMichal Meloun 
2968cdf4ec68SMichal Meloun uint16_t
ehci_get_port_speed_hostc(struct ehci_softc * sc,uint16_t index)2969cdf4ec68SMichal Meloun ehci_get_port_speed_hostc(struct ehci_softc *sc, uint16_t index)
2970cdf4ec68SMichal Meloun {
2971cdf4ec68SMichal Meloun 	uint32_t v;
2972cdf4ec68SMichal Meloun 
2973cdf4ec68SMichal Meloun 	v = EOREAD4(sc, EHCI_HOSTC(index));
2974cdf4ec68SMichal Meloun 	v = (v >> EHCI_HOSTC_PSPD_SHIFT) & EHCI_HOSTC_PSPD_MASK;
2975cdf4ec68SMichal Meloun 
2976cdf4ec68SMichal Meloun 	if (v == EHCI_PORT_SPEED_HIGH)
2977cdf4ec68SMichal Meloun 		return (UPS_HIGH_SPEED);
2978cdf4ec68SMichal Meloun 	if (v == EHCI_PORT_SPEED_LOW)
2979cdf4ec68SMichal Meloun 		return (UPS_LOW_SPEED);
2980cdf4ec68SMichal Meloun 	return (0);
2981cdf4ec68SMichal Meloun }
2982cdf4ec68SMichal Meloun 
298302ac6454SAndrew Thompson static void
ehci_disown(ehci_softc_t * sc,uint16_t index,uint8_t lowspeed)298402ac6454SAndrew Thompson ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
298502ac6454SAndrew Thompson {
298602ac6454SAndrew Thompson 	uint32_t port;
298702ac6454SAndrew Thompson 	uint32_t v;
298802ac6454SAndrew Thompson 
298902ac6454SAndrew Thompson 	DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
299002ac6454SAndrew Thompson 
299102ac6454SAndrew Thompson 	port = EHCI_PORTSC(index);
299202ac6454SAndrew Thompson 	v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
299302ac6454SAndrew Thompson 	EOWRITE4(sc, port, v | EHCI_PS_PO);
299402ac6454SAndrew Thompson }
299502ac6454SAndrew Thompson 
2996e0a69b51SAndrew Thompson static usb_error_t
ehci_roothub_exec(struct usb_device * udev,struct usb_device_request * req,const void ** pptr,uint16_t * plength)2997760bc48eSAndrew Thompson ehci_roothub_exec(struct usb_device *udev,
2998760bc48eSAndrew Thompson     struct usb_device_request *req, const void **pptr, uint16_t *plength)
299902ac6454SAndrew Thompson {
3000459d369eSAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3001459d369eSAndrew Thompson 	const char *str_ptr;
3002459d369eSAndrew Thompson 	const void *ptr;
300302ac6454SAndrew Thompson 	uint32_t port;
300402ac6454SAndrew Thompson 	uint32_t v;
3005459d369eSAndrew Thompson 	uint16_t len;
300602ac6454SAndrew Thompson 	uint16_t i;
300702ac6454SAndrew Thompson 	uint16_t value;
300802ac6454SAndrew Thompson 	uint16_t index;
3009e0a69b51SAndrew Thompson 	usb_error_t err;
301002ac6454SAndrew Thompson 
301102ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
301202ac6454SAndrew Thompson 
301302ac6454SAndrew Thompson 	/* buffer reset */
3014459d369eSAndrew Thompson 	ptr = (const void *)&sc->sc_hub_desc;
3015459d369eSAndrew Thompson 	len = 0;
3016459d369eSAndrew Thompson 	err = 0;
301702ac6454SAndrew Thompson 
3018459d369eSAndrew Thompson 	value = UGETW(req->wValue);
3019459d369eSAndrew Thompson 	index = UGETW(req->wIndex);
302002ac6454SAndrew Thompson 
302102ac6454SAndrew Thompson 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
302202ac6454SAndrew Thompson 	    "wValue=0x%04x wIndex=0x%04x\n",
3023459d369eSAndrew Thompson 	    req->bmRequestType, req->bRequest,
3024459d369eSAndrew Thompson 	    UGETW(req->wLength), value, index);
302502ac6454SAndrew Thompson 
302602ac6454SAndrew Thompson #define	C(x,y) ((x) | ((y) << 8))
3027459d369eSAndrew Thompson 	switch (C(req->bRequest, req->bmRequestType)) {
302802ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
302902ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
303002ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
303102ac6454SAndrew Thompson 		/*
303202ac6454SAndrew Thompson 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
303302ac6454SAndrew Thompson 		 * for the integrated root hub.
303402ac6454SAndrew Thompson 		 */
303502ac6454SAndrew Thompson 		break;
303602ac6454SAndrew Thompson 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
3037459d369eSAndrew Thompson 		len = 1;
303802ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
303902ac6454SAndrew Thompson 		break;
304002ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
304102ac6454SAndrew Thompson 		switch (value >> 8) {
304202ac6454SAndrew Thompson 		case UDESC_DEVICE:
304302ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3044459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
304502ac6454SAndrew Thompson 				goto done;
304602ac6454SAndrew Thompson 			}
3047459d369eSAndrew Thompson 			len = sizeof(ehci_devd);
3048459d369eSAndrew Thompson 			ptr = (const void *)&ehci_devd;
304902ac6454SAndrew Thompson 			break;
305002ac6454SAndrew Thompson 			/*
305102ac6454SAndrew Thompson 			 * We can't really operate at another speed,
305202ac6454SAndrew Thompson 			 * but the specification says we need this
305302ac6454SAndrew Thompson 			 * descriptor:
305402ac6454SAndrew Thompson 			 */
305502ac6454SAndrew Thompson 		case UDESC_DEVICE_QUALIFIER:
305602ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3057459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
305802ac6454SAndrew Thompson 				goto done;
305902ac6454SAndrew Thompson 			}
3060459d369eSAndrew Thompson 			len = sizeof(ehci_odevd);
3061459d369eSAndrew Thompson 			ptr = (const void *)&ehci_odevd;
306202ac6454SAndrew Thompson 			break;
306302ac6454SAndrew Thompson 
306402ac6454SAndrew Thompson 		case UDESC_CONFIG:
306502ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3066459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
306702ac6454SAndrew Thompson 				goto done;
306802ac6454SAndrew Thompson 			}
3069459d369eSAndrew Thompson 			len = sizeof(ehci_confd);
3070459d369eSAndrew Thompson 			ptr = (const void *)&ehci_confd;
307102ac6454SAndrew Thompson 			break;
307202ac6454SAndrew Thompson 
307302ac6454SAndrew Thompson 		case UDESC_STRING:
307402ac6454SAndrew Thompson 			switch (value & 0xff) {
307502ac6454SAndrew Thompson 			case 0:	/* Language table */
3076459d369eSAndrew Thompson 				str_ptr = "\001";
307702ac6454SAndrew Thompson 				break;
307802ac6454SAndrew Thompson 
307902ac6454SAndrew Thompson 			case 1:	/* Vendor */
3080459d369eSAndrew Thompson 				str_ptr = sc->sc_vendor;
308102ac6454SAndrew Thompson 				break;
308202ac6454SAndrew Thompson 
308302ac6454SAndrew Thompson 			case 2:	/* Product */
3084459d369eSAndrew Thompson 				str_ptr = "EHCI root HUB";
308502ac6454SAndrew Thompson 				break;
308602ac6454SAndrew Thompson 
308702ac6454SAndrew Thompson 			default:
3088459d369eSAndrew Thompson 				str_ptr = "";
308902ac6454SAndrew Thompson 				break;
309002ac6454SAndrew Thompson 			}
309102ac6454SAndrew Thompson 
3092a593f6b8SAndrew Thompson 			len = usb_make_str_desc(
3093459d369eSAndrew Thompson 			    sc->sc_hub_desc.temp,
309402ac6454SAndrew Thompson 			    sizeof(sc->sc_hub_desc.temp),
3095459d369eSAndrew Thompson 			    str_ptr);
309602ac6454SAndrew Thompson 			break;
309702ac6454SAndrew Thompson 		default:
3098459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
309902ac6454SAndrew Thompson 			goto done;
310002ac6454SAndrew Thompson 		}
310102ac6454SAndrew Thompson 		break;
310202ac6454SAndrew Thompson 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3103459d369eSAndrew Thompson 		len = 1;
310402ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = 0;
310502ac6454SAndrew Thompson 		break;
310602ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_DEVICE):
3107459d369eSAndrew Thompson 		len = 2;
310802ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
310902ac6454SAndrew Thompson 		break;
311002ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
311102ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3112459d369eSAndrew Thompson 		len = 2;
311302ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
311402ac6454SAndrew Thompson 		break;
311502ac6454SAndrew Thompson 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3116ab42e8b2SAndrew Thompson 		if (value >= EHCI_MAX_DEVICES) {
3117459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
311802ac6454SAndrew Thompson 			goto done;
311902ac6454SAndrew Thompson 		}
312002ac6454SAndrew Thompson 		sc->sc_addr = value;
312102ac6454SAndrew Thompson 		break;
312202ac6454SAndrew Thompson 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
312302ac6454SAndrew Thompson 		if ((value != 0) && (value != 1)) {
3124459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
312502ac6454SAndrew Thompson 			goto done;
312602ac6454SAndrew Thompson 		}
312702ac6454SAndrew Thompson 		sc->sc_conf = value;
312802ac6454SAndrew Thompson 		break;
312902ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
313002ac6454SAndrew Thompson 		break;
313102ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
313202ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
313302ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3134459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
313502ac6454SAndrew Thompson 		goto done;
313602ac6454SAndrew Thompson 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
313702ac6454SAndrew Thompson 		break;
313802ac6454SAndrew Thompson 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
313902ac6454SAndrew Thompson 		break;
314002ac6454SAndrew Thompson 		/* Hub requests */
314102ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
314202ac6454SAndrew Thompson 		break;
314302ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
314402ac6454SAndrew Thompson 		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
314502ac6454SAndrew Thompson 
314602ac6454SAndrew Thompson 		if ((index < 1) ||
314702ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3148459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
314902ac6454SAndrew Thompson 			goto done;
315002ac6454SAndrew Thompson 		}
315102ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
315202ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
315302ac6454SAndrew Thompson 		switch (value) {
315402ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
315502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PE);
315602ac6454SAndrew Thompson 			break;
315702ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
315802ac6454SAndrew Thompson 			if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
315902ac6454SAndrew Thompson 				/*
316002ac6454SAndrew Thompson 				 * waking up a High Speed device is rather
316102ac6454SAndrew Thompson 				 * complicated if
316202ac6454SAndrew Thompson 				 */
316302ac6454SAndrew Thompson 				EOWRITE4(sc, port, v | EHCI_PS_FPR);
316402ac6454SAndrew Thompson 			}
316502ac6454SAndrew Thompson 			/* wait 20ms for resume sequence to complete */
3166a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
316702ac6454SAndrew Thompson 
316802ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
316902ac6454SAndrew Thompson 			    EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
317002ac6454SAndrew Thompson 
31714a35cda7SAndrew Thompson 			/* 4ms settle time */
3172a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
317302ac6454SAndrew Thompson 			break;
317402ac6454SAndrew Thompson 		case UHF_PORT_POWER:
317502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PP);
317602ac6454SAndrew Thompson 			break;
317702ac6454SAndrew Thompson 		case UHF_PORT_TEST:
317802ac6454SAndrew Thompson 			DPRINTFN(3, "clear port test "
317902ac6454SAndrew Thompson 			    "%d\n", index);
318002ac6454SAndrew Thompson 			break;
318102ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
318202ac6454SAndrew Thompson 			DPRINTFN(3, "clear port ind "
318302ac6454SAndrew Thompson 			    "%d\n", index);
318402ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
318502ac6454SAndrew Thompson 			break;
318602ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
318702ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
318802ac6454SAndrew Thompson 			break;
318902ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
319002ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
319102ac6454SAndrew Thompson 			break;
319202ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
319302ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
319402ac6454SAndrew Thompson 			break;
319502ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
319602ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
319702ac6454SAndrew Thompson 			break;
319802ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
319902ac6454SAndrew Thompson 			sc->sc_isreset = 0;
320002ac6454SAndrew Thompson 			break;
320102ac6454SAndrew Thompson 		default:
3202459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
320302ac6454SAndrew Thompson 			goto done;
320402ac6454SAndrew Thompson 		}
320502ac6454SAndrew Thompson 		break;
320602ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
320702ac6454SAndrew Thompson 		if ((value & 0xff) != 0) {
3208459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
320902ac6454SAndrew Thompson 			goto done;
321002ac6454SAndrew Thompson 		}
3211b494261cSHans Petter Selasky 		v = EREAD4(sc, EHCI_HCSPARAMS);
321202ac6454SAndrew Thompson 
321302ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd = ehci_hubd;
321402ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
321553e0bf6eSHans Petter Selasky 
321653e0bf6eSHans Petter Selasky 		if (EHCI_HCS_PPC(v))
321753e0bf6eSHans Petter Selasky 			i = UHD_PWR_INDIVIDUAL;
321853e0bf6eSHans Petter Selasky 		else
321953e0bf6eSHans Petter Selasky 			i = UHD_PWR_NO_SWITCH;
322053e0bf6eSHans Petter Selasky 
322153e0bf6eSHans Petter Selasky 		if (EHCI_HCS_P_INDICATOR(v))
322253e0bf6eSHans Petter Selasky 			i |= UHD_PORT_IND;
322353e0bf6eSHans Petter Selasky 
322453e0bf6eSHans Petter Selasky 		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
322502ac6454SAndrew Thompson 		/* XXX can't find out? */
322602ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
322753e0bf6eSHans Petter Selasky 		/* XXX don't know if ports are removable or not */
322802ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bDescLength =
322902ac6454SAndrew Thompson 		    8 + ((sc->sc_noport + 7) / 8);
3230459d369eSAndrew Thompson 		len = sc->sc_hub_desc.hubd.bDescLength;
323102ac6454SAndrew Thompson 		break;
323202ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3233459d369eSAndrew Thompson 		len = 16;
3234271ae033SHans Petter Selasky 		memset(sc->sc_hub_desc.temp, 0, 16);
323502ac6454SAndrew Thompson 		break;
323602ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
323702ac6454SAndrew Thompson 		DPRINTFN(9, "get port status i=%d\n",
323802ac6454SAndrew Thompson 		    index);
323902ac6454SAndrew Thompson 		if ((index < 1) ||
324002ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3241459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
324202ac6454SAndrew Thompson 			goto done;
324302ac6454SAndrew Thompson 		}
324402ac6454SAndrew Thompson 		v = EOREAD4(sc, EHCI_PORTSC(index));
324502ac6454SAndrew Thompson 		DPRINTFN(9, "port status=0x%04x\n", v);
3246cdf4ec68SMichal Meloun 		if (sc->sc_flags & EHCI_SCFLG_TT) {
3247cdf4ec68SMichal Meloun 			if (sc->sc_vendor_get_port_speed != NULL) {
3248cdf4ec68SMichal Meloun 				i = sc->sc_vendor_get_port_speed(sc, index);
3249cdf4ec68SMichal Meloun 			} else {
3250cdf4ec68SMichal Meloun 				device_printf(sc->sc_bus.bdev,
3251cdf4ec68SMichal Meloun 				    "EHCI_SCFLG_TT quirk is set but "
3252cdf4ec68SMichal Meloun 				    "sc_vendor_get_hub_speed() is NULL\n");
325302ac6454SAndrew Thompson 				i = UPS_HIGH_SPEED;
3254cdf4ec68SMichal Meloun 			}
325502ac6454SAndrew Thompson 		} else {
325602ac6454SAndrew Thompson 			i = UPS_HIGH_SPEED;
325702ac6454SAndrew Thompson 		}
325802ac6454SAndrew Thompson 		if (v & EHCI_PS_CS)
325902ac6454SAndrew Thompson 			i |= UPS_CURRENT_CONNECT_STATUS;
326002ac6454SAndrew Thompson 		if (v & EHCI_PS_PE)
326102ac6454SAndrew Thompson 			i |= UPS_PORT_ENABLED;
326202ac6454SAndrew Thompson 		if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
326302ac6454SAndrew Thompson 			i |= UPS_SUSPEND;
326402ac6454SAndrew Thompson 		if (v & EHCI_PS_OCA)
326502ac6454SAndrew Thompson 			i |= UPS_OVERCURRENT_INDICATOR;
326602ac6454SAndrew Thompson 		if (v & EHCI_PS_PR)
326702ac6454SAndrew Thompson 			i |= UPS_RESET;
326802ac6454SAndrew Thompson 		if (v & EHCI_PS_PP)
326902ac6454SAndrew Thompson 			i |= UPS_PORT_POWER;
327002ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortStatus, i);
327102ac6454SAndrew Thompson 		i = 0;
327202ac6454SAndrew Thompson 		if (v & EHCI_PS_CSC)
327302ac6454SAndrew Thompson 			i |= UPS_C_CONNECT_STATUS;
327402ac6454SAndrew Thompson 		if (v & EHCI_PS_PEC)
327502ac6454SAndrew Thompson 			i |= UPS_C_PORT_ENABLED;
327602ac6454SAndrew Thompson 		if (v & EHCI_PS_OCC)
327702ac6454SAndrew Thompson 			i |= UPS_C_OVERCURRENT_INDICATOR;
327802ac6454SAndrew Thompson 		if (v & EHCI_PS_FPR)
327902ac6454SAndrew Thompson 			i |= UPS_C_SUSPEND;
328002ac6454SAndrew Thompson 		if (sc->sc_isreset)
328102ac6454SAndrew Thompson 			i |= UPS_C_PORT_RESET;
328202ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortChange, i);
3283459d369eSAndrew Thompson 		len = sizeof(sc->sc_hub_desc.ps);
328402ac6454SAndrew Thompson 		break;
328502ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3286459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
328702ac6454SAndrew Thompson 		goto done;
328802ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
328902ac6454SAndrew Thompson 		break;
329002ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
329102ac6454SAndrew Thompson 		if ((index < 1) ||
329202ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3293459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
329402ac6454SAndrew Thompson 			goto done;
329502ac6454SAndrew Thompson 		}
329602ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
329702ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
329802ac6454SAndrew Thompson 		switch (value) {
329902ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
330002ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PE);
330102ac6454SAndrew Thompson 			break;
330202ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
330302ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
330402ac6454SAndrew Thompson 			break;
330502ac6454SAndrew Thompson 		case UHF_PORT_RESET:
330602ac6454SAndrew Thompson 			DPRINTFN(6, "reset port %d\n", index);
3307b850ecc1SAndrew Thompson #ifdef USB_DEBUG
330802ac6454SAndrew Thompson 			if (ehcinohighspeed) {
330902ac6454SAndrew Thompson 				/*
331002ac6454SAndrew Thompson 				 * Connect USB device to companion
331102ac6454SAndrew Thompson 				 * controller.
331202ac6454SAndrew Thompson 				 */
331302ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
331402ac6454SAndrew Thompson 				break;
331502ac6454SAndrew Thompson 			}
331602ac6454SAndrew Thompson #endif
3317e55e1ebcSAndrew Thompson 			if (EHCI_PS_IS_LOWSPEED(v) &&
3318e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
331902ac6454SAndrew Thompson 				/* Low speed device, give up ownership. */
332002ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
332102ac6454SAndrew Thompson 				break;
332202ac6454SAndrew Thompson 			}
332302ac6454SAndrew Thompson 			/* Start reset sequence. */
332402ac6454SAndrew Thompson 			v &= ~(EHCI_PS_PE | EHCI_PS_PR);
332502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PR);
332602ac6454SAndrew Thompson 
332702ac6454SAndrew Thompson 			/* Wait for reset to complete. */
3328a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
332937506412SHans Petter Selasky 			    USB_MS_TO_TICKS(usb_port_root_reset_delay));
333002ac6454SAndrew Thompson 
333102ac6454SAndrew Thompson 			/* Terminate reset sequence. */
333202ac6454SAndrew Thompson 			if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
333302ac6454SAndrew Thompson 				EOWRITE4(sc, port, v);
333402ac6454SAndrew Thompson 
333502ac6454SAndrew Thompson 			/* Wait for HC to complete reset. */
3336a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
333702ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
333802ac6454SAndrew Thompson 
333902ac6454SAndrew Thompson 			v = EOREAD4(sc, port);
334002ac6454SAndrew Thompson 			DPRINTF("ehci after reset, status=0x%08x\n", v);
334102ac6454SAndrew Thompson 			if (v & EHCI_PS_PR) {
334202ac6454SAndrew Thompson 				device_printf(sc->sc_bus.bdev,
334302ac6454SAndrew Thompson 				    "port reset timeout\n");
3344459d369eSAndrew Thompson 				err = USB_ERR_TIMEOUT;
334502ac6454SAndrew Thompson 				goto done;
334602ac6454SAndrew Thompson 			}
3347e55e1ebcSAndrew Thompson 			if (!(v & EHCI_PS_PE) &&
3348e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3349e55e1ebcSAndrew Thompson 				/* Not a high speed device, give up ownership.*/
335002ac6454SAndrew Thompson 				ehci_disown(sc, index, 0);
335102ac6454SAndrew Thompson 				break;
335202ac6454SAndrew Thompson 			}
335302ac6454SAndrew Thompson 			sc->sc_isreset = 1;
335402ac6454SAndrew Thompson 			DPRINTF("ehci port %d reset, status = 0x%08x\n",
335502ac6454SAndrew Thompson 			    index, v);
335602ac6454SAndrew Thompson 			break;
335702ac6454SAndrew Thompson 
335802ac6454SAndrew Thompson 		case UHF_PORT_POWER:
335902ac6454SAndrew Thompson 			DPRINTFN(3, "set port power %d\n", index);
336002ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PP);
336102ac6454SAndrew Thompson 			break;
336202ac6454SAndrew Thompson 
336302ac6454SAndrew Thompson 		case UHF_PORT_TEST:
336402ac6454SAndrew Thompson 			DPRINTFN(3, "set port test %d\n", index);
336502ac6454SAndrew Thompson 			break;
336602ac6454SAndrew Thompson 
336702ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
336802ac6454SAndrew Thompson 			DPRINTFN(3, "set port ind %d\n", index);
336902ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
337002ac6454SAndrew Thompson 			break;
337102ac6454SAndrew Thompson 
337202ac6454SAndrew Thompson 		default:
3373459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
337402ac6454SAndrew Thompson 			goto done;
337502ac6454SAndrew Thompson 		}
337602ac6454SAndrew Thompson 		break;
337702ac6454SAndrew Thompson 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
337802ac6454SAndrew Thompson 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
337902ac6454SAndrew Thompson 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
338002ac6454SAndrew Thompson 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
338102ac6454SAndrew Thompson 		break;
338202ac6454SAndrew Thompson 	default:
3383459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
338402ac6454SAndrew Thompson 		goto done;
338502ac6454SAndrew Thompson 	}
338602ac6454SAndrew Thompson done:
3387459d369eSAndrew Thompson 	*plength = len;
3388459d369eSAndrew Thompson 	*pptr = ptr;
3389459d369eSAndrew Thompson 	return (err);
339002ac6454SAndrew Thompson }
339102ac6454SAndrew Thompson 
339202ac6454SAndrew Thompson static void
ehci_xfer_setup(struct usb_setup_params * parm)3393760bc48eSAndrew Thompson ehci_xfer_setup(struct usb_setup_params *parm)
339402ac6454SAndrew Thompson {
3395760bc48eSAndrew Thompson 	struct usb_page_search page_info;
3396760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
339702ac6454SAndrew Thompson 	ehci_softc_t *sc;
3398760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
339902ac6454SAndrew Thompson 	void *last_obj;
340002ac6454SAndrew Thompson 	uint32_t nqtd;
340102ac6454SAndrew Thompson 	uint32_t nqh;
340202ac6454SAndrew Thompson 	uint32_t nsitd;
340302ac6454SAndrew Thompson 	uint32_t nitd;
340402ac6454SAndrew Thompson 	uint32_t n;
340502ac6454SAndrew Thompson 
340602ac6454SAndrew Thompson 	sc = EHCI_BUS2SC(parm->udev->bus);
340702ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
340802ac6454SAndrew Thompson 
340902ac6454SAndrew Thompson 	nqtd = 0;
341002ac6454SAndrew Thompson 	nqh = 0;
341102ac6454SAndrew Thompson 	nsitd = 0;
341202ac6454SAndrew Thompson 	nitd = 0;
341302ac6454SAndrew Thompson 
341402ac6454SAndrew Thompson 	/*
341502ac6454SAndrew Thompson 	 * compute maximum number of some structures
341602ac6454SAndrew Thompson 	 */
341702ac6454SAndrew Thompson 	if (parm->methods == &ehci_device_ctrl_methods) {
341802ac6454SAndrew Thompson 		/*
341902ac6454SAndrew Thompson 		 * The proof for the "nqtd" formula is illustrated like
342002ac6454SAndrew Thompson 		 * this:
342102ac6454SAndrew Thompson 		 *
342202ac6454SAndrew Thompson 		 * +------------------------------------+
342302ac6454SAndrew Thompson 		 * |                                    |
342402ac6454SAndrew Thompson 		 * |         |remainder ->              |
342502ac6454SAndrew Thompson 		 * |   +-----+---+                      |
342602ac6454SAndrew Thompson 		 * |   | xxx | x | frm 0                |
342702ac6454SAndrew Thompson 		 * |   +-----+---++                     |
342802ac6454SAndrew Thompson 		 * |   | xxx | xx | frm 1               |
342902ac6454SAndrew Thompson 		 * |   +-----+----+                     |
343002ac6454SAndrew Thompson 		 * |            ...                     |
343102ac6454SAndrew Thompson 		 * +------------------------------------+
343202ac6454SAndrew Thompson 		 *
343302ac6454SAndrew Thompson 		 * "xxx" means a completely full USB transfer descriptor
343402ac6454SAndrew Thompson 		 *
343502ac6454SAndrew Thompson 		 * "x" and "xx" means a short USB packet
343602ac6454SAndrew Thompson 		 *
343702ac6454SAndrew Thompson 		 * For the remainder of an USB transfer modulo
343802ac6454SAndrew Thompson 		 * "max_data_length" we need two USB transfer descriptors.
343902ac6454SAndrew Thompson 		 * One to transfer the remaining data and one to finalise
344002ac6454SAndrew Thompson 		 * with a zero length packet in case the "force_short_xfer"
344102ac6454SAndrew Thompson 		 * flag is set. We only need two USB transfer descriptors in
344202ac6454SAndrew Thompson 		 * the case where the transfer length of the first one is a
344302ac6454SAndrew Thompson 		 * factor of "max_frame_size". The rest of the needed USB
344402ac6454SAndrew Thompson 		 * transfer descriptors is given by the buffer size divided
344502ac6454SAndrew Thompson 		 * by the maximum data payload.
344602ac6454SAndrew Thompson 		 */
344702ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
344802ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
344902ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
345002ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
345102ac6454SAndrew Thompson 
3452a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
345302ac6454SAndrew Thompson 
345402ac6454SAndrew Thompson 		nqh = 1;
345502ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes) + 1	/* STATUS */
3456578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
345702ac6454SAndrew Thompson 
345802ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_bulk_methods) {
345902ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
346002ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
346102ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
346202ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
346302ac6454SAndrew Thompson 
3464a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
346502ac6454SAndrew Thompson 
346602ac6454SAndrew Thompson 		nqh = 1;
346702ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3468578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
346902ac6454SAndrew Thompson 
347002ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_intr_methods) {
347102ac6454SAndrew Thompson 		if (parm->speed == USB_SPEED_HIGH) {
347202ac6454SAndrew Thompson 			parm->hc_max_packet_size = 0x400;
347302ac6454SAndrew Thompson 			parm->hc_max_packet_count = 3;
347402ac6454SAndrew Thompson 		} else if (parm->speed == USB_SPEED_FULL) {
347502ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
347602ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
347702ac6454SAndrew Thompson 		} else {
347802ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
347902ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
348002ac6454SAndrew Thompson 		}
348102ac6454SAndrew Thompson 
348202ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
348302ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
348402ac6454SAndrew Thompson 
3485a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
348602ac6454SAndrew Thompson 
348702ac6454SAndrew Thompson 		nqh = 1;
348802ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3489578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
349002ac6454SAndrew Thompson 
349102ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_fs_methods) {
349202ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x3FF;
349302ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
349402ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x3FF;
349502ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
349602ac6454SAndrew Thompson 
3497a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
349802ac6454SAndrew Thompson 
349902ac6454SAndrew Thompson 		nsitd = xfer->nframes;
350002ac6454SAndrew Thompson 
350102ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_hs_methods) {
350202ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
350302ac6454SAndrew Thompson 		parm->hc_max_packet_count = 3;
350402ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0xC00;
350502ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
350602ac6454SAndrew Thompson 
3507a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
350802ac6454SAndrew Thompson 
3509883bb300SAndrew Thompson 		nitd = ((xfer->nframes + 7) / 8) <<
3510883bb300SAndrew Thompson 		    usbd_xfer_get_fps_shift(xfer);
351102ac6454SAndrew Thompson 
351202ac6454SAndrew Thompson 	} else {
351302ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
351402ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
351502ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x400;
351602ac6454SAndrew Thompson 
3517a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
351802ac6454SAndrew Thompson 	}
351902ac6454SAndrew Thompson 
352002ac6454SAndrew Thompson alloc_dma_set:
352102ac6454SAndrew Thompson 
352202ac6454SAndrew Thompson 	if (parm->err) {
352302ac6454SAndrew Thompson 		return;
352402ac6454SAndrew Thompson 	}
352502ac6454SAndrew Thompson 	/*
352602ac6454SAndrew Thompson 	 * Allocate queue heads and transfer descriptors
352702ac6454SAndrew Thompson 	 */
352802ac6454SAndrew Thompson 	last_obj = NULL;
352902ac6454SAndrew Thompson 
3530a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
353102ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_itd_t),
353202ac6454SAndrew Thompson 	    EHCI_ITD_ALIGN, nitd)) {
353302ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
353402ac6454SAndrew Thompson 		return;
353502ac6454SAndrew Thompson 	}
353602ac6454SAndrew Thompson 	if (parm->buf) {
353702ac6454SAndrew Thompson 		for (n = 0; n != nitd; n++) {
353802ac6454SAndrew Thompson 			ehci_itd_t *td;
353902ac6454SAndrew Thompson 
3540a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
354102ac6454SAndrew Thompson 
354202ac6454SAndrew Thompson 			td = page_info.buffer;
354302ac6454SAndrew Thompson 
354402ac6454SAndrew Thompson 			/* init TD */
3545e55e1ebcSAndrew Thompson 			td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
354602ac6454SAndrew Thompson 			td->obj_next = last_obj;
354702ac6454SAndrew Thompson 			td->page_cache = pc + n;
354802ac6454SAndrew Thompson 
354902ac6454SAndrew Thompson 			last_obj = td;
355002ac6454SAndrew Thompson 
3551a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
355202ac6454SAndrew Thompson 		}
355302ac6454SAndrew Thompson 	}
3554a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
355502ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_sitd_t),
355602ac6454SAndrew Thompson 	    EHCI_SITD_ALIGN, nsitd)) {
355702ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
355802ac6454SAndrew Thompson 		return;
355902ac6454SAndrew Thompson 	}
356002ac6454SAndrew Thompson 	if (parm->buf) {
356102ac6454SAndrew Thompson 		for (n = 0; n != nsitd; n++) {
356202ac6454SAndrew Thompson 			ehci_sitd_t *td;
356302ac6454SAndrew Thompson 
3564a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
356502ac6454SAndrew Thompson 
356602ac6454SAndrew Thompson 			td = page_info.buffer;
356702ac6454SAndrew Thompson 
356802ac6454SAndrew Thompson 			/* init TD */
3569e55e1ebcSAndrew Thompson 			td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
357002ac6454SAndrew Thompson 			td->obj_next = last_obj;
357102ac6454SAndrew Thompson 			td->page_cache = pc + n;
357202ac6454SAndrew Thompson 
357302ac6454SAndrew Thompson 			last_obj = td;
357402ac6454SAndrew Thompson 
3575a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
357602ac6454SAndrew Thompson 		}
357702ac6454SAndrew Thompson 	}
3578a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
357902ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qtd_t),
358002ac6454SAndrew Thompson 	    EHCI_QTD_ALIGN, nqtd)) {
358102ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
358202ac6454SAndrew Thompson 		return;
358302ac6454SAndrew Thompson 	}
358402ac6454SAndrew Thompson 	if (parm->buf) {
358502ac6454SAndrew Thompson 		for (n = 0; n != nqtd; n++) {
358602ac6454SAndrew Thompson 			ehci_qtd_t *qtd;
358702ac6454SAndrew Thompson 
3588a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
358902ac6454SAndrew Thompson 
359002ac6454SAndrew Thompson 			qtd = page_info.buffer;
359102ac6454SAndrew Thompson 
359202ac6454SAndrew Thompson 			/* init TD */
3593e55e1ebcSAndrew Thompson 			qtd->qtd_self = htohc32(sc, page_info.physaddr);
359402ac6454SAndrew Thompson 			qtd->obj_next = last_obj;
359502ac6454SAndrew Thompson 			qtd->page_cache = pc + n;
359602ac6454SAndrew Thompson 
359702ac6454SAndrew Thompson 			last_obj = qtd;
359802ac6454SAndrew Thompson 
3599a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
360002ac6454SAndrew Thompson 		}
360102ac6454SAndrew Thompson 	}
360202ac6454SAndrew Thompson 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
360302ac6454SAndrew Thompson 
360402ac6454SAndrew Thompson 	last_obj = NULL;
360502ac6454SAndrew Thompson 
3606a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
360702ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qh_t),
360802ac6454SAndrew Thompson 	    EHCI_QH_ALIGN, nqh)) {
360902ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
361002ac6454SAndrew Thompson 		return;
361102ac6454SAndrew Thompson 	}
361202ac6454SAndrew Thompson 	if (parm->buf) {
361302ac6454SAndrew Thompson 		for (n = 0; n != nqh; n++) {
361402ac6454SAndrew Thompson 			ehci_qh_t *qh;
361502ac6454SAndrew Thompson 
3616a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
361702ac6454SAndrew Thompson 
361802ac6454SAndrew Thompson 			qh = page_info.buffer;
361902ac6454SAndrew Thompson 
362002ac6454SAndrew Thompson 			/* init QH */
3621e55e1ebcSAndrew Thompson 			qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
362202ac6454SAndrew Thompson 			qh->obj_next = last_obj;
362302ac6454SAndrew Thompson 			qh->page_cache = pc + n;
362402ac6454SAndrew Thompson 
362502ac6454SAndrew Thompson 			last_obj = qh;
362602ac6454SAndrew Thompson 
3627a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
362802ac6454SAndrew Thompson 		}
362902ac6454SAndrew Thompson 	}
363002ac6454SAndrew Thompson 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
363102ac6454SAndrew Thompson 
363202ac6454SAndrew Thompson 	if (!xfer->flags_int.curr_dma_set) {
363302ac6454SAndrew Thompson 		xfer->flags_int.curr_dma_set = 1;
363402ac6454SAndrew Thompson 		goto alloc_dma_set;
363502ac6454SAndrew Thompson 	}
363602ac6454SAndrew Thompson }
363702ac6454SAndrew Thompson 
363802ac6454SAndrew Thompson static void
ehci_xfer_unsetup(struct usb_xfer * xfer)3639760bc48eSAndrew Thompson ehci_xfer_unsetup(struct usb_xfer *xfer)
364002ac6454SAndrew Thompson {
364102ac6454SAndrew Thompson 	return;
364202ac6454SAndrew Thompson }
364302ac6454SAndrew Thompson 
364402ac6454SAndrew Thompson static void
ehci_ep_init(struct usb_device * udev,struct usb_endpoint_descriptor * edesc,struct usb_endpoint * ep)3645ae60fdfbSAndrew Thompson ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3646ae60fdfbSAndrew Thompson     struct usb_endpoint *ep)
364702ac6454SAndrew Thompson {
364802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
364902ac6454SAndrew Thompson 
3650ae60fdfbSAndrew Thompson 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3651ae60fdfbSAndrew Thompson 	    ep, udev->address,
3652f29a0724SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb_mode,
365302ac6454SAndrew Thompson 	    sc->sc_addr);
365402ac6454SAndrew Thompson 
365539307315SAndrew Thompson 	if (udev->device_index != sc->sc_addr) {
365602ac6454SAndrew Thompson 		if ((udev->speed != USB_SPEED_HIGH) &&
365702ac6454SAndrew Thompson 		    ((udev->hs_hub_addr == 0) ||
365802ac6454SAndrew Thompson 		    (udev->hs_port_no == 0) ||
3659495f25ceSAndrew Thompson 		    (udev->parent_hs_hub == NULL) ||
3660495f25ceSAndrew Thompson 		    (udev->parent_hs_hub->hub == NULL))) {
366102ac6454SAndrew Thompson 			/* We need a transaction translator */
366202ac6454SAndrew Thompson 			goto done;
366302ac6454SAndrew Thompson 		}
366402ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
366502ac6454SAndrew Thompson 		case UE_CONTROL:
3666ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_ctrl_methods;
366702ac6454SAndrew Thompson 			break;
366802ac6454SAndrew Thompson 		case UE_INTERRUPT:
3669ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_intr_methods;
367002ac6454SAndrew Thompson 			break;
367102ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
367202ac6454SAndrew Thompson 			if (udev->speed == USB_SPEED_HIGH) {
3673ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_hs_methods;
367402ac6454SAndrew Thompson 			} else if (udev->speed == USB_SPEED_FULL) {
3675ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_fs_methods;
367602ac6454SAndrew Thompson 			}
367702ac6454SAndrew Thompson 			break;
367802ac6454SAndrew Thompson 		case UE_BULK:
3679ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_bulk_methods;
368002ac6454SAndrew Thompson 			break;
368102ac6454SAndrew Thompson 		default:
368202ac6454SAndrew Thompson 			/* do nothing */
368302ac6454SAndrew Thompson 			break;
368402ac6454SAndrew Thompson 		}
368502ac6454SAndrew Thompson 	}
368602ac6454SAndrew Thompson done:
368702ac6454SAndrew Thompson 	return;
368802ac6454SAndrew Thompson }
368902ac6454SAndrew Thompson 
369002ac6454SAndrew Thompson static void
ehci_get_dma_delay(struct usb_device * udev,uint32_t * pus)3691527aa7b2SAndrew Thompson ehci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
369202ac6454SAndrew Thompson {
369302ac6454SAndrew Thompson 	/*
369402ac6454SAndrew Thompson 	 * Wait until the hardware has finished any possible use of
369502ac6454SAndrew Thompson 	 * the transfer descriptor(s) and QH
369602ac6454SAndrew Thompson 	 */
369734485480SHans Petter Selasky 	*pus = (1125);			/* microseconds */
369802ac6454SAndrew Thompson }
369902ac6454SAndrew Thompson 
370002ac6454SAndrew Thompson static void
ehci_device_resume(struct usb_device * udev)3701760bc48eSAndrew Thompson ehci_device_resume(struct usb_device *udev)
370202ac6454SAndrew Thompson {
370302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3704760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3705e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
370602ac6454SAndrew Thompson 
370702ac6454SAndrew Thompson 	DPRINTF("\n");
370802ac6454SAndrew Thompson 
370902ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
371002ac6454SAndrew Thompson 
371102ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
371202ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
3713ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
371402ac6454SAndrew Thompson 
371502ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
371602ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
371702ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
371802ac6454SAndrew Thompson 				    sc->sc_async_p_last);
371902ac6454SAndrew Thompson 			}
372002ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
372102ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
372202ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
372302ac6454SAndrew Thompson 			}
372402ac6454SAndrew Thompson 		}
372502ac6454SAndrew Thompson 	}
372602ac6454SAndrew Thompson 
372702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
372802ac6454SAndrew Thompson 
372902ac6454SAndrew Thompson 	return;
373002ac6454SAndrew Thompson }
373102ac6454SAndrew Thompson 
373202ac6454SAndrew Thompson static void
ehci_device_suspend(struct usb_device * udev)3733760bc48eSAndrew Thompson ehci_device_suspend(struct usb_device *udev)
373402ac6454SAndrew Thompson {
373502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3736760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3737e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
373802ac6454SAndrew Thompson 
373902ac6454SAndrew Thompson 	DPRINTF("\n");
374002ac6454SAndrew Thompson 
374102ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
374202ac6454SAndrew Thompson 
374302ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
374402ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
3745ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
374602ac6454SAndrew Thompson 
374702ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
374802ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
374902ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
375002ac6454SAndrew Thompson 				    sc->sc_async_p_last);
375102ac6454SAndrew Thompson 			}
375202ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
375302ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
375402ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
375502ac6454SAndrew Thompson 			}
375602ac6454SAndrew Thompson 		}
375702ac6454SAndrew Thompson 	}
375802ac6454SAndrew Thompson 
375902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
37602e141748SHans Petter Selasky }
376102ac6454SAndrew Thompson 
37622e141748SHans Petter Selasky static void
ehci_set_hw_power_sleep(struct usb_bus * bus,uint32_t state)37632e141748SHans Petter Selasky ehci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
37642e141748SHans Petter Selasky {
37652e141748SHans Petter Selasky 	struct ehci_softc *sc = EHCI_BUS2SC(bus);
37662e141748SHans Petter Selasky 
37672e141748SHans Petter Selasky 	switch (state) {
37682e141748SHans Petter Selasky 	case USB_HW_POWER_SUSPEND:
37692e141748SHans Petter Selasky 	case USB_HW_POWER_SHUTDOWN:
37702e141748SHans Petter Selasky 		ehci_suspend(sc);
37712e141748SHans Petter Selasky 		break;
37722e141748SHans Petter Selasky 	case USB_HW_POWER_RESUME:
37732e141748SHans Petter Selasky 		ehci_resume(sc);
37742e141748SHans Petter Selasky 		break;
37752e141748SHans Petter Selasky 	default:
37762e141748SHans Petter Selasky 		break;
37772e141748SHans Petter Selasky 	}
377802ac6454SAndrew Thompson }
377902ac6454SAndrew Thompson 
378002ac6454SAndrew Thompson static void
ehci_set_hw_power(struct usb_bus * bus)3781760bc48eSAndrew Thompson ehci_set_hw_power(struct usb_bus *bus)
378202ac6454SAndrew Thompson {
378302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
378402ac6454SAndrew Thompson 	uint32_t temp;
378502ac6454SAndrew Thompson 	uint32_t flags;
378602ac6454SAndrew Thompson 
378702ac6454SAndrew Thompson 	DPRINTF("\n");
378802ac6454SAndrew Thompson 
378902ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
379002ac6454SAndrew Thompson 
379102ac6454SAndrew Thompson 	flags = bus->hw_power_state;
379202ac6454SAndrew Thompson 
379302ac6454SAndrew Thompson 	temp = EOREAD4(sc, EHCI_USBCMD);
379402ac6454SAndrew Thompson 
379502ac6454SAndrew Thompson 	temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
379602ac6454SAndrew Thompson 
379702ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_CONTROL |
379802ac6454SAndrew Thompson 	    USB_HW_POWER_BULK)) {
379902ac6454SAndrew Thompson 		DPRINTF("Async is active\n");
380002ac6454SAndrew Thompson 		temp |= EHCI_CMD_ASE;
380102ac6454SAndrew Thompson 	}
380202ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_INTERRUPT |
380302ac6454SAndrew Thompson 	    USB_HW_POWER_ISOC)) {
380402ac6454SAndrew Thompson 		DPRINTF("Periodic is active\n");
380502ac6454SAndrew Thompson 		temp |= EHCI_CMD_PSE;
380602ac6454SAndrew Thompson 	}
380702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, temp);
380802ac6454SAndrew Thompson 
380902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
381002ac6454SAndrew Thompson 
381102ac6454SAndrew Thompson 	return;
381202ac6454SAndrew Thompson }
381302ac6454SAndrew Thompson 
38144a6af125SHans Petter Selasky static void
ehci_start_dma_delay_second(struct usb_xfer * xfer)38154a6af125SHans Petter Selasky ehci_start_dma_delay_second(struct usb_xfer *xfer)
38164a6af125SHans Petter Selasky {
38174a6af125SHans Petter Selasky 	struct ehci_softc *sc = EHCI_BUS2SC(xfer->xroot->bus);
38184a6af125SHans Petter Selasky 
38194a6af125SHans Petter Selasky 	DPRINTF("\n");
38204a6af125SHans Petter Selasky 
38214a6af125SHans Petter Selasky 	/* trigger doorbell */
38224a6af125SHans Petter Selasky 	ehci_doorbell_async(sc);
38234a6af125SHans Petter Selasky 
38244a6af125SHans Petter Selasky 	/* give the doorbell 4ms */
38254a6af125SHans Petter Selasky 	usbd_transfer_timeout_ms(xfer,
38264a6af125SHans Petter Selasky 	    (void (*)(void *))&usb_dma_delay_done_cb, 4);
38274a6af125SHans Petter Selasky }
38284a6af125SHans Petter Selasky 
38294a6af125SHans Petter Selasky /*
38304a6af125SHans Petter Selasky  * Ring the doorbell twice before freeing any DMA descriptors. Some host
38314a6af125SHans Petter Selasky  * controllers apparently cache the QH descriptors and need a message
38324a6af125SHans Petter Selasky  * that the cache needs to be discarded.
38334a6af125SHans Petter Selasky  */
38344a6af125SHans Petter Selasky static void
ehci_start_dma_delay(struct usb_xfer * xfer)38354a6af125SHans Petter Selasky ehci_start_dma_delay(struct usb_xfer *xfer)
38364a6af125SHans Petter Selasky {
38374a6af125SHans Petter Selasky 	struct ehci_softc *sc = EHCI_BUS2SC(xfer->xroot->bus);
38384a6af125SHans Petter Selasky 
38394a6af125SHans Petter Selasky 	DPRINTF("\n");
38404a6af125SHans Petter Selasky 
38414a6af125SHans Petter Selasky 	/* trigger doorbell */
38424a6af125SHans Petter Selasky 	ehci_doorbell_async(sc);
38434a6af125SHans Petter Selasky 
38444a6af125SHans Petter Selasky 	/* give the doorbell 4ms */
38454a6af125SHans Petter Selasky 	usbd_transfer_timeout_ms(xfer,
38464a6af125SHans Petter Selasky 	    (void (*)(void *))&ehci_start_dma_delay_second, 4);
38474a6af125SHans Petter Selasky }
38484a6af125SHans Petter Selasky 
3849e892b3feSHans Petter Selasky static const struct usb_bus_methods ehci_bus_methods =
385002ac6454SAndrew Thompson {
3851ae60fdfbSAndrew Thompson 	.endpoint_init = ehci_ep_init,
385202ac6454SAndrew Thompson 	.xfer_setup = ehci_xfer_setup,
385302ac6454SAndrew Thompson 	.xfer_unsetup = ehci_xfer_unsetup,
385402ac6454SAndrew Thompson 	.get_dma_delay = ehci_get_dma_delay,
385502ac6454SAndrew Thompson 	.device_resume = ehci_device_resume,
385602ac6454SAndrew Thompson 	.device_suspend = ehci_device_suspend,
385702ac6454SAndrew Thompson 	.set_hw_power = ehci_set_hw_power,
38582e141748SHans Petter Selasky 	.set_hw_power_sleep = ehci_set_hw_power_sleep,
385939307315SAndrew Thompson 	.roothub_exec = ehci_roothub_exec,
3860dddb25f9SAlfred Perlstein 	.xfer_poll = ehci_do_poll,
38614a6af125SHans Petter Selasky 	.start_dma_delay = ehci_start_dma_delay,
386202ac6454SAndrew Thompson };
3863