xref: /freebsd/sys/dev/usb/controller/ehci.c (revision ea719ede0267abd75b3d73d436a36ddb12a6bb12)
102ac6454SAndrew Thompson /*-
202ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
302ac6454SAndrew Thompson  * Copyright (c) 2004 The NetBSD Foundation, Inc. All rights reserved.
402ac6454SAndrew Thompson  * Copyright (c) 2004 Lennart Augustsson. All rights reserved.
502ac6454SAndrew Thompson  * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
902ac6454SAndrew Thompson  * are met:
1002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1702ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1802ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1902ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2002ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2102ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2202ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2302ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2402ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2502ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2602ac6454SAndrew Thompson  * SUCH DAMAGE.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
2902ac6454SAndrew Thompson /*
3002ac6454SAndrew Thompson  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
3102ac6454SAndrew Thompson  *
3202ac6454SAndrew Thompson  * The EHCI 0.96 spec can be found at
3302ac6454SAndrew Thompson  * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
3402ac6454SAndrew Thompson  * The EHCI 1.0 spec can be found at
3502ac6454SAndrew Thompson  * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
3602ac6454SAndrew Thompson  * and the USB 2.0 spec at
3702ac6454SAndrew Thompson  * http://www.usb.org/developers/docs/usb_20.zip
3802ac6454SAndrew Thompson  *
3902ac6454SAndrew Thompson  */
4002ac6454SAndrew Thompson 
4102ac6454SAndrew Thompson /*
4202ac6454SAndrew Thompson  * TODO:
4302ac6454SAndrew Thompson  * 1) command failures are not recovered correctly
4402ac6454SAndrew Thompson  */
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson #include <sys/cdefs.h>
4702ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
4802ac6454SAndrew Thompson 
49ed6d949aSAndrew Thompson #include <sys/stdint.h>
50ed6d949aSAndrew Thompson #include <sys/stddef.h>
51ed6d949aSAndrew Thompson #include <sys/param.h>
52ed6d949aSAndrew Thompson #include <sys/queue.h>
53ed6d949aSAndrew Thompson #include <sys/types.h>
54ed6d949aSAndrew Thompson #include <sys/systm.h>
55ed6d949aSAndrew Thompson #include <sys/kernel.h>
56ed6d949aSAndrew Thompson #include <sys/bus.h>
57ed6d949aSAndrew Thompson #include <sys/module.h>
58ed6d949aSAndrew Thompson #include <sys/lock.h>
59ed6d949aSAndrew Thompson #include <sys/mutex.h>
60ed6d949aSAndrew Thompson #include <sys/condvar.h>
61ed6d949aSAndrew Thompson #include <sys/sysctl.h>
62ed6d949aSAndrew Thompson #include <sys/sx.h>
63ed6d949aSAndrew Thompson #include <sys/unistd.h>
64ed6d949aSAndrew Thompson #include <sys/callout.h>
65ed6d949aSAndrew Thompson #include <sys/malloc.h>
66ed6d949aSAndrew Thompson #include <sys/priv.h>
67ed6d949aSAndrew Thompson 
6802ac6454SAndrew Thompson #include <dev/usb/usb.h>
69ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
7002ac6454SAndrew Thompson 
7102ac6454SAndrew Thompson #define	USB_DEBUG_VAR ehcidebug
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7502ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
7602ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
7802ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
7902ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
8002ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
8102ac6454SAndrew Thompson 
8202ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
8302ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
8402ac6454SAndrew Thompson #include <dev/usb/controller/ehci.h>
851def609aSAndrew Thompson #include <dev/usb/controller/ehcireg.h>
8602ac6454SAndrew Thompson 
87578d0effSAndrew Thompson #define	EHCI_BUS2SC(bus) \
88578d0effSAndrew Thompson    ((ehci_softc_t *)(((uint8_t *)(bus)) - \
89578d0effSAndrew Thompson     ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))
9002ac6454SAndrew Thompson 
91b850ecc1SAndrew Thompson #ifdef USB_DEBUG
9202ac6454SAndrew Thompson static int ehcidebug = 0;
9302ac6454SAndrew Thompson static int ehcinohighspeed = 0;
94cf223a88SAndrew Thompson static int ehciiaadbug = 0;
95cf223a88SAndrew Thompson static int ehcilostintrbug = 0;
9602ac6454SAndrew Thompson 
979360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
989360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
9902ac6454SAndrew Thompson     &ehcidebug, 0, "Debug level");
1009360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RW,
10102ac6454SAndrew Thompson     &ehcinohighspeed, 0, "Disable High Speed USB");
102cf223a88SAndrew Thompson SYSCTL_INT(_hw_usb_ehci, OID_AUTO, iaadbug, CTLFLAG_RW,
103cf223a88SAndrew Thompson     &ehciiaadbug, 0, "Enable doorbell bug workaround");
104cf223a88SAndrew Thompson SYSCTL_INT(_hw_usb_ehci, OID_AUTO, lostintrbug, CTLFLAG_RW,
105cf223a88SAndrew Thompson     &ehcilostintrbug, 0, "Enable lost interrupt bug workaround");
10602ac6454SAndrew Thompson 
107c13fd8d4SAndrew Thompson TUNABLE_INT("hw.usb.ehci.debug", &ehcidebug);
108c13fd8d4SAndrew Thompson TUNABLE_INT("hw.usb.ehci.no_hs", &ehcinohighspeed);
109cf223a88SAndrew Thompson TUNABLE_INT("hw.usb.ehci.iaadbug", &ehciiaadbug);
110cf223a88SAndrew Thompson TUNABLE_INT("hw.usb.ehci.lostintrbug", &ehcilostintrbug);
111c13fd8d4SAndrew Thompson 
11202ac6454SAndrew Thompson static void ehci_dump_regs(ehci_softc_t *sc);
11302ac6454SAndrew Thompson static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson #endif
11602ac6454SAndrew Thompson 
11702ac6454SAndrew Thompson #define	EHCI_INTR_ENDPT 1
11802ac6454SAndrew Thompson 
119760bc48eSAndrew Thompson extern struct usb_bus_methods ehci_bus_methods;
120760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_bulk_methods;
121760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_ctrl_methods;
122760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_intr_methods;
123760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_isoc_fs_methods;
124760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_isoc_hs_methods;
12502ac6454SAndrew Thompson 
12630b22abeSAndrew Thompson static void ehci_do_poll(struct usb_bus *);
12730b22abeSAndrew Thompson static void ehci_device_done(struct usb_xfer *, usb_error_t);
12830b22abeSAndrew Thompson static uint8_t ehci_check_transfer(struct usb_xfer *);
12930b22abeSAndrew Thompson static void ehci_timeout(void *);
13030b22abeSAndrew Thompson static void ehci_poll_timeout(void *);
13130b22abeSAndrew Thompson 
13239307315SAndrew Thompson static void ehci_root_intr(ehci_softc_t *sc);
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson struct ehci_std_temp {
13502ac6454SAndrew Thompson 	ehci_softc_t *sc;
136760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
13702ac6454SAndrew Thompson 	ehci_qtd_t *td;
13802ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
13902ac6454SAndrew Thompson 	uint32_t average;
14002ac6454SAndrew Thompson 	uint32_t qtd_status;
14102ac6454SAndrew Thompson 	uint32_t len;
14202ac6454SAndrew Thompson 	uint16_t max_frame_size;
14302ac6454SAndrew Thompson 	uint8_t	shortpkt;
14402ac6454SAndrew Thompson 	uint8_t	auto_data_toggle;
14502ac6454SAndrew Thompson 	uint8_t	setup_alt_next;
1460f7d4548SAndrew Thompson 	uint8_t	last_frame;
14702ac6454SAndrew Thompson };
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson void
150e0a69b51SAndrew Thompson ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
15102ac6454SAndrew Thompson {
15202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
15302ac6454SAndrew Thompson 	uint32_t i;
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
15602ac6454SAndrew Thompson 	    sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
15702ac6454SAndrew Thompson 
15853e0bf6eSHans Petter Selasky 	cb(bus, &sc->sc_hw.terminate_pc, &sc->sc_hw.terminate_pg,
15953e0bf6eSHans Petter Selasky 	    sizeof(struct ehci_qh_sub), EHCI_QH_ALIGN);
16053e0bf6eSHans Petter Selasky 
16102ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
16202ac6454SAndrew Thompson 	    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
16502ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.intr_start_pc + i,
16602ac6454SAndrew Thompson 		    sc->sc_hw.intr_start_pg + i,
16702ac6454SAndrew Thompson 		    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
16802ac6454SAndrew Thompson 	}
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
17102ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
17202ac6454SAndrew Thompson 		    sc->sc_hw.isoc_hs_start_pg + i,
17302ac6454SAndrew Thompson 		    sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
17402ac6454SAndrew Thompson 	}
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
17702ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
17802ac6454SAndrew Thompson 		    sc->sc_hw.isoc_fs_start_pg + i,
17902ac6454SAndrew Thompson 		    sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
18002ac6454SAndrew Thompson 	}
18102ac6454SAndrew Thompson }
18202ac6454SAndrew Thompson 
183e0a69b51SAndrew Thompson usb_error_t
184e55e1ebcSAndrew Thompson ehci_reset(ehci_softc_t *sc)
18502ac6454SAndrew Thompson {
18602ac6454SAndrew Thompson 	uint32_t hcr;
187e55e1ebcSAndrew Thompson 	int i;
188e55e1ebcSAndrew Thompson 
189e55e1ebcSAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
190e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
191a593f6b8SAndrew Thompson 		usb_pause_mtx(NULL, hz / 1000);
192e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
193e55e1ebcSAndrew Thompson 		if (!hcr) {
194e55e1ebcSAndrew Thompson 			if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) {
195e55e1ebcSAndrew Thompson 				/*
196e55e1ebcSAndrew Thompson 				 * Force USBMODE as requested.  Controllers
197e55e1ebcSAndrew Thompson 				 * may have multiple operating modes.
198e55e1ebcSAndrew Thompson 				 */
199e55e1ebcSAndrew Thompson 				uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE);
200e55e1ebcSAndrew Thompson 				if (sc->sc_flags & EHCI_SCFLG_SETMODE) {
201e55e1ebcSAndrew Thompson 					usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST;
202e55e1ebcSAndrew Thompson 					device_printf(sc->sc_bus.bdev,
203e55e1ebcSAndrew Thompson 					    "set host controller mode\n");
204e55e1ebcSAndrew Thompson 				}
205e55e1ebcSAndrew Thompson 				if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) {
206e55e1ebcSAndrew Thompson 					usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE;
207e55e1ebcSAndrew Thompson 					device_printf(sc->sc_bus.bdev,
208e55e1ebcSAndrew Thompson 					    "set big-endian mode\n");
209e55e1ebcSAndrew Thompson 				}
210e55e1ebcSAndrew Thompson 				EOWRITE4(sc,  EHCI_USBMODE, usbmode);
211e55e1ebcSAndrew Thompson 			}
212e55e1ebcSAndrew Thompson 			return (0);
213e55e1ebcSAndrew Thompson 		}
214e55e1ebcSAndrew Thompson 	}
215e55e1ebcSAndrew Thompson 	device_printf(sc->sc_bus.bdev, "reset timeout\n");
216e55e1ebcSAndrew Thompson 	return (USB_ERR_IOERROR);
217e55e1ebcSAndrew Thompson }
218e55e1ebcSAndrew Thompson 
219e0a69b51SAndrew Thompson static usb_error_t
220e55e1ebcSAndrew Thompson ehci_hcreset(ehci_softc_t *sc)
221e55e1ebcSAndrew Thompson {
222e55e1ebcSAndrew Thompson 	uint32_t hcr;
223e55e1ebcSAndrew Thompson 	int i;
22402ac6454SAndrew Thompson 
22502ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
226e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
227a593f6b8SAndrew Thompson 		usb_pause_mtx(NULL, hz / 1000);
228e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
229e55e1ebcSAndrew Thompson 		if (hcr)
23002ac6454SAndrew Thompson 			break;
23102ac6454SAndrew Thompson 	}
232e55e1ebcSAndrew Thompson 	if (!hcr)
23302ac6454SAndrew Thompson 		/*
23402ac6454SAndrew Thompson                  * Fall through and try reset anyway even though
23502ac6454SAndrew Thompson                  * Table 2-9 in the EHCI spec says this will result
23602ac6454SAndrew Thompson                  * in undefined behavior.
23702ac6454SAndrew Thompson                  */
238e55e1ebcSAndrew Thompson 		device_printf(sc->sc_bus.bdev, "stop timeout\n");
23902ac6454SAndrew Thompson 
240e55e1ebcSAndrew Thompson 	return ehci_reset(sc);
24102ac6454SAndrew Thompson }
24202ac6454SAndrew Thompson 
243e0a69b51SAndrew Thompson usb_error_t
24402ac6454SAndrew Thompson ehci_init(ehci_softc_t *sc)
24502ac6454SAndrew Thompson {
246760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
24702ac6454SAndrew Thompson 	uint32_t version;
24802ac6454SAndrew Thompson 	uint32_t sparams;
24902ac6454SAndrew Thompson 	uint32_t cparams;
25002ac6454SAndrew Thompson 	uint32_t hcr;
25102ac6454SAndrew Thompson 	uint16_t i;
25202ac6454SAndrew Thompson 	uint16_t x;
25302ac6454SAndrew Thompson 	uint16_t y;
25402ac6454SAndrew Thompson 	uint16_t bit;
255e0a69b51SAndrew Thompson 	usb_error_t err = 0;
25602ac6454SAndrew Thompson 
25702ac6454SAndrew Thompson 	DPRINTF("start\n");
25802ac6454SAndrew Thompson 
259a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
26030b22abeSAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_mtx, 0);
26102ac6454SAndrew Thompson 
26246873d15SHans Petter Selasky 	sc->sc_offs = EHCI_CAPLENGTH(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
26346873d15SHans Petter Selasky 
264b850ecc1SAndrew Thompson #ifdef USB_DEBUG
265cf223a88SAndrew Thompson 	if (ehciiaadbug)
266cf223a88SAndrew Thompson 		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
267cf223a88SAndrew Thompson 	if (ehcilostintrbug)
268cf223a88SAndrew Thompson 		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
26902ac6454SAndrew Thompson 	if (ehcidebug > 2) {
27002ac6454SAndrew Thompson 		ehci_dump_regs(sc);
27102ac6454SAndrew Thompson 	}
27202ac6454SAndrew Thompson #endif
27302ac6454SAndrew Thompson 
274495ed64cSNathan Whitehorn 	version = EHCI_HCIVERSION(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
27502ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
27602ac6454SAndrew Thompson 	    version >> 8, version & 0xff);
27702ac6454SAndrew Thompson 
27802ac6454SAndrew Thompson 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
27902ac6454SAndrew Thompson 	DPRINTF("sparams=0x%x\n", sparams);
28002ac6454SAndrew Thompson 
28102ac6454SAndrew Thompson 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
28202ac6454SAndrew Thompson 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
28302ac6454SAndrew Thompson 	DPRINTF("cparams=0x%x\n", cparams);
28402ac6454SAndrew Thompson 
28502ac6454SAndrew Thompson 	if (EHCI_HCC_64BIT(cparams)) {
28602ac6454SAndrew Thompson 		DPRINTF("HCC uses 64-bit structures\n");
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson 		/* MUST clear segment register if 64 bit capable */
28902ac6454SAndrew Thompson 		EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
29002ac6454SAndrew Thompson 	}
29102ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_2_0;
29202ac6454SAndrew Thompson 
29302ac6454SAndrew Thompson 	/* Reset the controller */
29402ac6454SAndrew Thompson 	DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev));
29502ac6454SAndrew Thompson 
296e55e1ebcSAndrew Thompson 	err = ehci_hcreset(sc);
29702ac6454SAndrew Thompson 	if (err) {
29802ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "reset timeout\n");
29902ac6454SAndrew Thompson 		return (err);
30002ac6454SAndrew Thompson 	}
30102ac6454SAndrew Thompson 	/*
30202ac6454SAndrew Thompson 	 * use current frame-list-size selection 0: 1024*4 bytes 1:  512*4
30302ac6454SAndrew Thompson 	 * bytes 2:  256*4 bytes 3:      unknown
30402ac6454SAndrew Thompson 	 */
30502ac6454SAndrew Thompson 	if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
30602ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
30702ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
30802ac6454SAndrew Thompson 	}
30902ac6454SAndrew Thompson 	/* set up the bus struct */
31002ac6454SAndrew Thompson 	sc->sc_bus.methods = &ehci_bus_methods;
31102ac6454SAndrew Thompson 
31202ac6454SAndrew Thompson 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
31302ac6454SAndrew Thompson 
31453e0bf6eSHans Petter Selasky 	if (1) {
31553e0bf6eSHans Petter Selasky 		struct ehci_qh_sub *qh;
31653e0bf6eSHans Petter Selasky 
31753e0bf6eSHans Petter Selasky 		usbd_get_page(&sc->sc_hw.terminate_pc, 0, &buf_res);
31853e0bf6eSHans Petter Selasky 
31953e0bf6eSHans Petter Selasky 		qh = buf_res.buffer;
32053e0bf6eSHans Petter Selasky 
32153e0bf6eSHans Petter Selasky 		sc->sc_terminate_self = htohc32(sc, buf_res.physaddr);
32253e0bf6eSHans Petter Selasky 
32353e0bf6eSHans Petter Selasky 		/* init terminate TD */
32453e0bf6eSHans Petter Selasky 		qh->qtd_next =
32553e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_LINK_TERMINATE);
32653e0bf6eSHans Petter Selasky 		qh->qtd_altnext =
32753e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_LINK_TERMINATE);
32853e0bf6eSHans Petter Selasky 		qh->qtd_status =
32953e0bf6eSHans Petter Selasky 		    htohc32(sc, EHCI_QTD_HALTED);
33053e0bf6eSHans Petter Selasky 	}
33153e0bf6eSHans Petter Selasky 
33202ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
33302ac6454SAndrew Thompson 		ehci_qh_t *qh;
33402ac6454SAndrew Thompson 
335a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson 		qh = buf_res.buffer;
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson 		/* initialize page cache pointer */
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 		qh->page_cache = sc->sc_hw.intr_start_pc + i;
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 		/* store a pointer to queue head */
34402ac6454SAndrew Thompson 
34502ac6454SAndrew Thompson 		sc->sc_intr_p_last[i] = qh;
34602ac6454SAndrew Thompson 
34702ac6454SAndrew Thompson 		qh->qh_self =
348e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
349e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
35002ac6454SAndrew Thompson 
35102ac6454SAndrew Thompson 		qh->qh_endp =
352e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
35302ac6454SAndrew Thompson 		qh->qh_endphub =
354e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_MULT(1));
35502ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 		qh->qh_qtd.qtd_next =
358e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
35902ac6454SAndrew Thompson 		qh->qh_qtd.qtd_altnext =
360e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
36102ac6454SAndrew Thompson 		qh->qh_qtd.qtd_status =
362e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QTD_HALTED);
36302ac6454SAndrew Thompson 	}
36402ac6454SAndrew Thompson 
36502ac6454SAndrew Thompson 	/*
36602ac6454SAndrew Thompson 	 * the QHs are arranged to give poll intervals that are
36702ac6454SAndrew Thompson 	 * powers of 2 times 1ms
36802ac6454SAndrew Thompson 	 */
36902ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
37002ac6454SAndrew Thompson 	while (bit) {
37102ac6454SAndrew Thompson 		x = bit;
37202ac6454SAndrew Thompson 		while (x & bit) {
37302ac6454SAndrew Thompson 			ehci_qh_t *qh_x;
37402ac6454SAndrew Thompson 			ehci_qh_t *qh_y;
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 			y = (x ^ bit) | (bit / 2);
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson 			qh_x = sc->sc_intr_p_last[x];
37902ac6454SAndrew Thompson 			qh_y = sc->sc_intr_p_last[y];
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 			/*
38202ac6454SAndrew Thompson 			 * the next QH has half the poll interval
38302ac6454SAndrew Thompson 			 */
38402ac6454SAndrew Thompson 			qh_x->qh_link = qh_y->qh_self;
38502ac6454SAndrew Thompson 
38602ac6454SAndrew Thompson 			x++;
38702ac6454SAndrew Thompson 		}
38802ac6454SAndrew Thompson 		bit >>= 1;
38902ac6454SAndrew Thompson 	}
39002ac6454SAndrew Thompson 
39102ac6454SAndrew Thompson 	if (1) {
39202ac6454SAndrew Thompson 		ehci_qh_t *qh;
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 		qh = sc->sc_intr_p_last[0];
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 		/* the last (1ms) QH terminates */
397e55e1ebcSAndrew Thompson 		qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
39802ac6454SAndrew Thompson 	}
39902ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
40002ac6454SAndrew Thompson 		ehci_sitd_t *sitd;
40102ac6454SAndrew Thompson 		ehci_itd_t *itd;
40202ac6454SAndrew Thompson 
403a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
40402ac6454SAndrew Thompson 
40502ac6454SAndrew Thompson 		sitd = buf_res.buffer;
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson 		/* initialize page cache pointer */
40802ac6454SAndrew Thompson 
40902ac6454SAndrew Thompson 		sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
41002ac6454SAndrew Thompson 
41102ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
41202ac6454SAndrew Thompson 
41302ac6454SAndrew Thompson 		sc->sc_isoc_fs_p_last[i] = sitd;
41402ac6454SAndrew Thompson 
41502ac6454SAndrew Thompson 		/* initialize full speed isochronous */
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson 		sitd->sitd_self =
418e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
419e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_SITD);
42002ac6454SAndrew Thompson 
42102ac6454SAndrew Thompson 		sitd->sitd_back =
422e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson 		sitd->sitd_next =
42502ac6454SAndrew Thompson 		    sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
42602ac6454SAndrew Thompson 
42702ac6454SAndrew Thompson 
428a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson 		itd = buf_res.buffer;
43102ac6454SAndrew Thompson 
43202ac6454SAndrew Thompson 		/* initialize page cache pointer */
43302ac6454SAndrew Thompson 
43402ac6454SAndrew Thompson 		itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
43502ac6454SAndrew Thompson 
43602ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson 		sc->sc_isoc_hs_p_last[i] = itd;
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 		/* initialize high speed isochronous */
44102ac6454SAndrew Thompson 
44202ac6454SAndrew Thompson 		itd->itd_self =
443e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
444e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_ITD);
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson 		itd->itd_next =
44702ac6454SAndrew Thompson 		    sitd->sitd_self;
44802ac6454SAndrew Thompson 	}
44902ac6454SAndrew Thompson 
450a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
45102ac6454SAndrew Thompson 
45202ac6454SAndrew Thompson 	if (1) {
45302ac6454SAndrew Thompson 		uint32_t *pframes;
45402ac6454SAndrew Thompson 
45502ac6454SAndrew Thompson 		pframes = buf_res.buffer;
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 		/*
45802ac6454SAndrew Thompson 		 * execution order:
45902ac6454SAndrew Thompson 		 * pframes -> high speed isochronous ->
46002ac6454SAndrew Thompson 		 *    full speed isochronous -> interrupt QH's
46102ac6454SAndrew Thompson 		 */
46202ac6454SAndrew Thompson 		for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
46302ac6454SAndrew Thompson 			pframes[i] = sc->sc_isoc_hs_p_last
46402ac6454SAndrew Thompson 			    [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
46502ac6454SAndrew Thompson 		}
46602ac6454SAndrew Thompson 	}
46702ac6454SAndrew Thompson 	/* setup sync list pointer */
46802ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
46902ac6454SAndrew Thompson 
470a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 	if (1) {
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson 		ehci_qh_t *qh;
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson 		qh = buf_res.buffer;
47702ac6454SAndrew Thompson 
47802ac6454SAndrew Thompson 		/* initialize page cache pointer */
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 		qh->page_cache = &sc->sc_hw.async_start_pc;
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson 		/* store a pointer to the queue head */
48302ac6454SAndrew Thompson 
48402ac6454SAndrew Thompson 		sc->sc_async_p_last = qh;
48502ac6454SAndrew Thompson 
48602ac6454SAndrew Thompson 		/* init dummy QH that starts the async list */
48702ac6454SAndrew Thompson 
48802ac6454SAndrew Thompson 		qh->qh_self =
489e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
490e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
49102ac6454SAndrew Thompson 
49202ac6454SAndrew Thompson 		/* fill the QH */
49302ac6454SAndrew Thompson 		qh->qh_endp =
494e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
495e55e1ebcSAndrew Thompson 		qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
49602ac6454SAndrew Thompson 		qh->qh_link = qh->qh_self;
49702ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
49802ac6454SAndrew Thompson 
49902ac6454SAndrew Thompson 		/* fill the overlay qTD */
500e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
501e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
502e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
50302ac6454SAndrew Thompson 	}
50402ac6454SAndrew Thompson 	/* flush all cache into memory */
50502ac6454SAndrew Thompson 
506a593f6b8SAndrew Thompson 	usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
50702ac6454SAndrew Thompson 
508b850ecc1SAndrew Thompson #ifdef USB_DEBUG
50902ac6454SAndrew Thompson 	if (ehcidebug) {
51002ac6454SAndrew Thompson 		ehci_dump_sqh(sc, sc->sc_async_p_last);
51102ac6454SAndrew Thompson 	}
51202ac6454SAndrew Thompson #endif
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 	/* setup async list pointer */
51502ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 
51802ac6454SAndrew Thompson 	/* enable interrupts */
51902ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
52002ac6454SAndrew Thompson 
52102ac6454SAndrew Thompson 	/* turn on controller */
52202ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD,
52302ac6454SAndrew Thompson 	    EHCI_CMD_ITC_1 |		/* 1 microframes interrupt delay */
52402ac6454SAndrew Thompson 	    (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
52502ac6454SAndrew Thompson 	    EHCI_CMD_ASE |
52602ac6454SAndrew Thompson 	    EHCI_CMD_PSE |
52702ac6454SAndrew Thompson 	    EHCI_CMD_RS);
52802ac6454SAndrew Thompson 
52902ac6454SAndrew Thompson 	/* Take over port ownership */
53002ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
533a593f6b8SAndrew Thompson 		usb_pause_mtx(NULL, hz / 1000);
53402ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
53502ac6454SAndrew Thompson 		if (!hcr) {
53602ac6454SAndrew Thompson 			break;
53702ac6454SAndrew Thompson 		}
53802ac6454SAndrew Thompson 	}
53902ac6454SAndrew Thompson 	if (hcr) {
54002ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "run timeout\n");
54102ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
54202ac6454SAndrew Thompson 	}
54302ac6454SAndrew Thompson 
54402ac6454SAndrew Thompson 	if (!err) {
54502ac6454SAndrew Thompson 		/* catch any lost interrupts */
54602ac6454SAndrew Thompson 		ehci_do_poll(&sc->sc_bus);
54702ac6454SAndrew Thompson 	}
54802ac6454SAndrew Thompson 	return (err);
54902ac6454SAndrew Thompson }
55002ac6454SAndrew Thompson 
55102ac6454SAndrew Thompson /*
55202ac6454SAndrew Thompson  * shut down the controller when the system is going down
55302ac6454SAndrew Thompson  */
55402ac6454SAndrew Thompson void
55502ac6454SAndrew Thompson ehci_detach(ehci_softc_t *sc)
55602ac6454SAndrew Thompson {
55702ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
55802ac6454SAndrew Thompson 
559a593f6b8SAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_pcd);
56030b22abeSAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_poll);
56102ac6454SAndrew Thompson 
562bda9babeSAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, 0);
563e55e1ebcSAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
56402ac6454SAndrew Thompson 
565e55e1ebcSAndrew Thompson 	if (ehci_hcreset(sc)) {
56602ac6454SAndrew Thompson 		DPRINTF("reset failed!\n");
56702ac6454SAndrew Thompson 	}
56802ac6454SAndrew Thompson 
56902ac6454SAndrew Thompson 	/* XXX let stray task complete */
570a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL, hz / 20);
57102ac6454SAndrew Thompson 
572a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_pcd);
57330b22abeSAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_poll);
57402ac6454SAndrew Thompson }
57502ac6454SAndrew Thompson 
57602ac6454SAndrew Thompson void
57702ac6454SAndrew Thompson ehci_suspend(ehci_softc_t *sc)
57802ac6454SAndrew Thompson {
57902ac6454SAndrew Thompson 	uint32_t cmd;
58002ac6454SAndrew Thompson 	uint32_t hcr;
58102ac6454SAndrew Thompson 	uint8_t i;
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
58402ac6454SAndrew Thompson 
58502ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
58602ac6454SAndrew Thompson 		cmd = EOREAD4(sc, EHCI_PORTSC(i));
58702ac6454SAndrew Thompson 		if (((cmd & EHCI_PS_PO) == 0) &&
58802ac6454SAndrew Thompson 		    ((cmd & EHCI_PS_PE) == EHCI_PS_PE)) {
58902ac6454SAndrew Thompson 			EOWRITE4(sc, EHCI_PORTSC(i),
59002ac6454SAndrew Thompson 			    cmd | EHCI_PS_SUSP);
59102ac6454SAndrew Thompson 		}
59202ac6454SAndrew Thompson 	}
59302ac6454SAndrew Thompson 
59402ac6454SAndrew Thompson 	sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
59502ac6454SAndrew Thompson 
59602ac6454SAndrew Thompson 	cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
59702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, cmd);
59802ac6454SAndrew Thompson 
59902ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
60002ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) &
60102ac6454SAndrew Thompson 		    (EHCI_STS_ASS | EHCI_STS_PSS);
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson 		if (hcr == 0) {
60402ac6454SAndrew Thompson 			break;
60502ac6454SAndrew Thompson 		}
606a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
60702ac6454SAndrew Thompson 	}
60802ac6454SAndrew Thompson 
60902ac6454SAndrew Thompson 	if (hcr != 0) {
61002ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "reset timeout\n");
61102ac6454SAndrew Thompson 	}
61202ac6454SAndrew Thompson 	cmd &= ~EHCI_CMD_RS;
61302ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, cmd);
61402ac6454SAndrew Thompson 
61502ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
61602ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
61702ac6454SAndrew Thompson 		if (hcr == EHCI_STS_HCH) {
61802ac6454SAndrew Thompson 			break;
61902ac6454SAndrew Thompson 		}
620a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
62102ac6454SAndrew Thompson 	}
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 	if (hcr != EHCI_STS_HCH) {
62402ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev,
62502ac6454SAndrew Thompson 		    "config timeout\n");
62602ac6454SAndrew Thompson 	}
62702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
62802ac6454SAndrew Thompson }
62902ac6454SAndrew Thompson 
63002ac6454SAndrew Thompson void
63102ac6454SAndrew Thompson ehci_resume(ehci_softc_t *sc)
63202ac6454SAndrew Thompson {
633760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
63402ac6454SAndrew Thompson 	uint32_t cmd;
63502ac6454SAndrew Thompson 	uint32_t hcr;
63602ac6454SAndrew Thompson 	uint8_t i;
63702ac6454SAndrew Thompson 
63802ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
63902ac6454SAndrew Thompson 
64002ac6454SAndrew Thompson 	/* restore things in case the bios doesn't */
64102ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
64202ac6454SAndrew Thompson 
643a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
64402ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
64502ac6454SAndrew Thompson 
646a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
64702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
65002ac6454SAndrew Thompson 
65102ac6454SAndrew Thompson 	hcr = 0;
65202ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
65302ac6454SAndrew Thompson 		cmd = EOREAD4(sc, EHCI_PORTSC(i));
65402ac6454SAndrew Thompson 		if (((cmd & EHCI_PS_PO) == 0) &&
65502ac6454SAndrew Thompson 		    ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
65602ac6454SAndrew Thompson 			EOWRITE4(sc, EHCI_PORTSC(i),
65702ac6454SAndrew Thompson 			    cmd | EHCI_PS_FPR);
65802ac6454SAndrew Thompson 			hcr = 1;
65902ac6454SAndrew Thompson 		}
66002ac6454SAndrew Thompson 	}
66102ac6454SAndrew Thompson 
66202ac6454SAndrew Thompson 	if (hcr) {
663a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx,
66402ac6454SAndrew Thompson 		    USB_MS_TO_TICKS(USB_RESUME_WAIT));
66502ac6454SAndrew Thompson 
66602ac6454SAndrew Thompson 		for (i = 1; i <= sc->sc_noport; i++) {
66702ac6454SAndrew Thompson 			cmd = EOREAD4(sc, EHCI_PORTSC(i));
66802ac6454SAndrew Thompson 			if (((cmd & EHCI_PS_PO) == 0) &&
66902ac6454SAndrew Thompson 			    ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
67002ac6454SAndrew Thompson 				EOWRITE4(sc, EHCI_PORTSC(i),
67102ac6454SAndrew Thompson 				    cmd & ~EHCI_PS_FPR);
67202ac6454SAndrew Thompson 			}
67302ac6454SAndrew Thompson 		}
67402ac6454SAndrew Thompson 	}
67502ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
67602ac6454SAndrew Thompson 
67702ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
67802ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
67902ac6454SAndrew Thompson 		if (hcr != EHCI_STS_HCH) {
68002ac6454SAndrew Thompson 			break;
68102ac6454SAndrew Thompson 		}
682a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
68302ac6454SAndrew Thompson 	}
68402ac6454SAndrew Thompson 	if (hcr == EHCI_STS_HCH) {
68502ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "config timeout\n");
68602ac6454SAndrew Thompson 	}
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
68902ac6454SAndrew Thompson 
690a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL,
69102ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(USB_RESUME_WAIT));
69202ac6454SAndrew Thompson 
69302ac6454SAndrew Thompson 	/* catch any lost interrupts */
69402ac6454SAndrew Thompson 	ehci_do_poll(&sc->sc_bus);
69502ac6454SAndrew Thompson }
69602ac6454SAndrew Thompson 
69702ac6454SAndrew Thompson void
69802ac6454SAndrew Thompson ehci_shutdown(ehci_softc_t *sc)
69902ac6454SAndrew Thompson {
70002ac6454SAndrew Thompson 	DPRINTF("stopping the HC\n");
70102ac6454SAndrew Thompson 
702e55e1ebcSAndrew Thompson 	if (ehci_hcreset(sc)) {
70302ac6454SAndrew Thompson 		DPRINTF("reset failed!\n");
70402ac6454SAndrew Thompson 	}
70502ac6454SAndrew Thompson }
70602ac6454SAndrew Thompson 
707b850ecc1SAndrew Thompson #ifdef USB_DEBUG
70802ac6454SAndrew Thompson static void
70902ac6454SAndrew Thompson ehci_dump_regs(ehci_softc_t *sc)
71002ac6454SAndrew Thompson {
71102ac6454SAndrew Thompson 	uint32_t i;
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBCMD);
71402ac6454SAndrew Thompson 	printf("cmd=0x%08x\n", i);
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_1)
71702ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_1\n");
71802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_2)
71902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_2\n");
72002ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_4)
72102ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_4\n");
72202ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_8)
72302ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_8\n");
72402ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_16)
72502ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_16\n");
72602ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_32)
72702ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_32\n");
72802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_64)
72902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_64\n");
73002ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPME)
73102ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPME\n");
73202ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPMC)
73302ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPMC\n");
73402ac6454SAndrew Thompson 	if (i & EHCI_CMD_LHCR)
73502ac6454SAndrew Thompson 		printf(" EHCI_CMD_LHCR\n");
73602ac6454SAndrew Thompson 	if (i & EHCI_CMD_IAAD)
73702ac6454SAndrew Thompson 		printf(" EHCI_CMD_IAAD\n");
73802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASE)
73902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASE\n");
74002ac6454SAndrew Thompson 	if (i & EHCI_CMD_PSE)
74102ac6454SAndrew Thompson 		printf(" EHCI_CMD_PSE\n");
74202ac6454SAndrew Thompson 	if (i & EHCI_CMD_FLS_M)
74302ac6454SAndrew Thompson 		printf(" EHCI_CMD_FLS_M\n");
74402ac6454SAndrew Thompson 	if (i & EHCI_CMD_HCRESET)
74502ac6454SAndrew Thompson 		printf(" EHCI_CMD_HCRESET\n");
74602ac6454SAndrew Thompson 	if (i & EHCI_CMD_RS)
74702ac6454SAndrew Thompson 		printf(" EHCI_CMD_RS\n");
74802ac6454SAndrew Thompson 
74902ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBSTS);
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson 	printf("sts=0x%08x\n", i);
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 	if (i & EHCI_STS_ASS)
75402ac6454SAndrew Thompson 		printf(" EHCI_STS_ASS\n");
75502ac6454SAndrew Thompson 	if (i & EHCI_STS_PSS)
75602ac6454SAndrew Thompson 		printf(" EHCI_STS_PSS\n");
75702ac6454SAndrew Thompson 	if (i & EHCI_STS_REC)
75802ac6454SAndrew Thompson 		printf(" EHCI_STS_REC\n");
75902ac6454SAndrew Thompson 	if (i & EHCI_STS_HCH)
76002ac6454SAndrew Thompson 		printf(" EHCI_STS_HCH\n");
76102ac6454SAndrew Thompson 	if (i & EHCI_STS_IAA)
76202ac6454SAndrew Thompson 		printf(" EHCI_STS_IAA\n");
76302ac6454SAndrew Thompson 	if (i & EHCI_STS_HSE)
76402ac6454SAndrew Thompson 		printf(" EHCI_STS_HSE\n");
76502ac6454SAndrew Thompson 	if (i & EHCI_STS_FLR)
76602ac6454SAndrew Thompson 		printf(" EHCI_STS_FLR\n");
76702ac6454SAndrew Thompson 	if (i & EHCI_STS_PCD)
76802ac6454SAndrew Thompson 		printf(" EHCI_STS_PCD\n");
76902ac6454SAndrew Thompson 	if (i & EHCI_STS_ERRINT)
77002ac6454SAndrew Thompson 		printf(" EHCI_STS_ERRINT\n");
77102ac6454SAndrew Thompson 	if (i & EHCI_STS_INT)
77202ac6454SAndrew Thompson 		printf(" EHCI_STS_INT\n");
77302ac6454SAndrew Thompson 
77402ac6454SAndrew Thompson 	printf("ien=0x%08x\n",
77502ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_USBINTR));
77602ac6454SAndrew Thompson 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
77702ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_FRINDEX),
77802ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
77902ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
78002ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
78102ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
78202ac6454SAndrew Thompson 		printf("port %d status=0x%08x\n", i,
78302ac6454SAndrew Thompson 		    EOREAD4(sc, EHCI_PORTSC(i)));
78402ac6454SAndrew Thompson 	}
78502ac6454SAndrew Thompson }
78602ac6454SAndrew Thompson 
78702ac6454SAndrew Thompson static void
78802ac6454SAndrew Thompson ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
78902ac6454SAndrew Thompson {
790e55e1ebcSAndrew Thompson 	link = hc32toh(sc, link);
79102ac6454SAndrew Thompson 	printf("0x%08x", link);
79202ac6454SAndrew Thompson 	if (link & EHCI_LINK_TERMINATE)
79302ac6454SAndrew Thompson 		printf("<T>");
79402ac6454SAndrew Thompson 	else {
79502ac6454SAndrew Thompson 		printf("<");
79602ac6454SAndrew Thompson 		if (type) {
79702ac6454SAndrew Thompson 			switch (EHCI_LINK_TYPE(link)) {
79802ac6454SAndrew Thompson 			case EHCI_LINK_ITD:
79902ac6454SAndrew Thompson 				printf("ITD");
80002ac6454SAndrew Thompson 				break;
80102ac6454SAndrew Thompson 			case EHCI_LINK_QH:
80202ac6454SAndrew Thompson 				printf("QH");
80302ac6454SAndrew Thompson 				break;
80402ac6454SAndrew Thompson 			case EHCI_LINK_SITD:
80502ac6454SAndrew Thompson 				printf("SITD");
80602ac6454SAndrew Thompson 				break;
80702ac6454SAndrew Thompson 			case EHCI_LINK_FSTN:
80802ac6454SAndrew Thompson 				printf("FSTN");
80902ac6454SAndrew Thompson 				break;
81002ac6454SAndrew Thompson 			}
81102ac6454SAndrew Thompson 		}
81202ac6454SAndrew Thompson 		printf(">");
81302ac6454SAndrew Thompson 	}
81402ac6454SAndrew Thompson }
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson static void
81702ac6454SAndrew Thompson ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
81802ac6454SAndrew Thompson {
81902ac6454SAndrew Thompson 	uint32_t s;
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson 	printf("  next=");
82202ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_next, 0);
82302ac6454SAndrew Thompson 	printf(" altnext=");
82402ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_altnext, 0);
82502ac6454SAndrew Thompson 	printf("\n");
826e55e1ebcSAndrew Thompson 	s = hc32toh(sc, qtd->qtd_status);
82702ac6454SAndrew Thompson 	printf("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
82802ac6454SAndrew Thompson 	    s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
82902ac6454SAndrew Thompson 	    EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
83002ac6454SAndrew Thompson 	printf("    cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
83102ac6454SAndrew Thompson 	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
83202ac6454SAndrew Thompson 	    (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
83302ac6454SAndrew Thompson 	    (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
83402ac6454SAndrew Thompson 	    (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
83502ac6454SAndrew Thompson 	    (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
83602ac6454SAndrew Thompson 	    (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
83702ac6454SAndrew Thompson 	    (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
83802ac6454SAndrew Thompson 	    (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
83902ac6454SAndrew Thompson 	    (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
84002ac6454SAndrew Thompson 
84102ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
84202ac6454SAndrew Thompson 		printf("  buffer[%d]=0x%08x\n", s,
843e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer[s]));
84402ac6454SAndrew Thompson 	}
84502ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
84602ac6454SAndrew Thompson 		printf("  buffer_hi[%d]=0x%08x\n", s,
847e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer_hi[s]));
84802ac6454SAndrew Thompson 	}
84902ac6454SAndrew Thompson }
85002ac6454SAndrew Thompson 
85102ac6454SAndrew Thompson static uint8_t
85202ac6454SAndrew Thompson ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
85302ac6454SAndrew Thompson {
85402ac6454SAndrew Thompson 	uint8_t temp;
85502ac6454SAndrew Thompson 
856a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sqtd->page_cache);
857e55e1ebcSAndrew Thompson 	printf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
85802ac6454SAndrew Thompson 	ehci_dump_qtd(sc, sqtd);
859e55e1ebcSAndrew Thompson 	temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
86002ac6454SAndrew Thompson 	return (temp);
86102ac6454SAndrew Thompson }
86202ac6454SAndrew Thompson 
86302ac6454SAndrew Thompson static void
86402ac6454SAndrew Thompson ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
86502ac6454SAndrew Thompson {
86602ac6454SAndrew Thompson 	uint16_t i;
86702ac6454SAndrew Thompson 	uint8_t stop;
86802ac6454SAndrew Thompson 
86902ac6454SAndrew Thompson 	stop = 0;
87002ac6454SAndrew Thompson 	for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
87102ac6454SAndrew Thompson 		stop = ehci_dump_sqtd(sc, sqtd);
87202ac6454SAndrew Thompson 	}
87302ac6454SAndrew Thompson 	if (sqtd) {
87402ac6454SAndrew Thompson 		printf("dump aborted, too many TDs\n");
87502ac6454SAndrew Thompson 	}
87602ac6454SAndrew Thompson }
87702ac6454SAndrew Thompson 
87802ac6454SAndrew Thompson static void
87902ac6454SAndrew Thompson ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
88002ac6454SAndrew Thompson {
88102ac6454SAndrew Thompson 	uint32_t endp;
88202ac6454SAndrew Thompson 	uint32_t endphub;
88302ac6454SAndrew Thompson 
884a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
885e55e1ebcSAndrew Thompson 	printf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
88602ac6454SAndrew Thompson 	printf("  link=");
88702ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_link, 1);
88802ac6454SAndrew Thompson 	printf("\n");
889e55e1ebcSAndrew Thompson 	endp = hc32toh(sc, qh->qh_endp);
89002ac6454SAndrew Thompson 	printf("  endp=0x%08x\n", endp);
89102ac6454SAndrew Thompson 	printf("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
89202ac6454SAndrew Thompson 	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
89302ac6454SAndrew Thompson 	    EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
89402ac6454SAndrew Thompson 	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
89502ac6454SAndrew Thompson 	printf("    mpl=0x%x ctl=%d nrl=%d\n",
89602ac6454SAndrew Thompson 	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
89702ac6454SAndrew Thompson 	    EHCI_QH_GET_NRL(endp));
898e55e1ebcSAndrew Thompson 	endphub = hc32toh(sc, qh->qh_endphub);
89902ac6454SAndrew Thompson 	printf("  endphub=0x%08x\n", endphub);
90002ac6454SAndrew Thompson 	printf("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
90102ac6454SAndrew Thompson 	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
90202ac6454SAndrew Thompson 	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
90302ac6454SAndrew Thompson 	    EHCI_QH_GET_MULT(endphub));
90402ac6454SAndrew Thompson 	printf("  curqtd=");
90502ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_curqtd, 0);
90602ac6454SAndrew Thompson 	printf("\n");
90702ac6454SAndrew Thompson 	printf("Overlay qTD:\n");
90802ac6454SAndrew Thompson 	ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
90902ac6454SAndrew Thompson }
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson static void
91202ac6454SAndrew Thompson ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
91302ac6454SAndrew Thompson {
914a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sitd->page_cache);
915e55e1ebcSAndrew Thompson 	printf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
916e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
91702ac6454SAndrew Thompson 	printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
918e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_portaddr),
919e55e1ebcSAndrew Thompson 	    (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
92002ac6454SAndrew Thompson 	    ? "in" : "out",
921e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
922e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
923e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
924e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
925e55e1ebcSAndrew Thompson 	printf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
926e55e1ebcSAndrew Thompson 	printf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
927e55e1ebcSAndrew Thompson 	    (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
928e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
92902ac6454SAndrew Thompson 	printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
930e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_back),
931e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[0]),
932e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[1]),
933e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[0]),
934e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[1]));
93502ac6454SAndrew Thompson }
93602ac6454SAndrew Thompson 
93702ac6454SAndrew Thompson static void
93802ac6454SAndrew Thompson ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
93902ac6454SAndrew Thompson {
940a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(itd->page_cache);
941e55e1ebcSAndrew Thompson 	printf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
942e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
943e55e1ebcSAndrew Thompson 	printf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
944e55e1ebcSAndrew Thompson 	    (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
945e55e1ebcSAndrew Thompson 	printf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
946e55e1ebcSAndrew Thompson 	    (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
947e55e1ebcSAndrew Thompson 	printf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
948e55e1ebcSAndrew Thompson 	    (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
949e55e1ebcSAndrew Thompson 	printf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
950e55e1ebcSAndrew Thompson 	    (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
951e55e1ebcSAndrew Thompson 	printf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
952e55e1ebcSAndrew Thompson 	    (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
953e55e1ebcSAndrew Thompson 	printf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
954e55e1ebcSAndrew Thompson 	    (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
955e55e1ebcSAndrew Thompson 	printf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
956e55e1ebcSAndrew Thompson 	    (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
957e55e1ebcSAndrew Thompson 	printf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
958e55e1ebcSAndrew Thompson 	    (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
959e55e1ebcSAndrew Thompson 	printf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
96002ac6454SAndrew Thompson 	printf("  addr=0x%02x; endpt=0x%01x\n",
961e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
962e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
963e55e1ebcSAndrew Thompson 	printf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
96402ac6454SAndrew Thompson 	printf(" dir=%s; mpl=0x%02x\n",
965e55e1ebcSAndrew Thompson 	    (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
966e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
96702ac6454SAndrew Thompson 	printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
968e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[2]),
969e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[3]),
970e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[4]),
971e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[5]),
972e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[6]));
97302ac6454SAndrew Thompson 	printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
97402ac6454SAndrew Thompson 	    "       0x%08x,0x%08x,0x%08x\n",
975e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[0]),
976e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[1]),
977e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[2]),
978e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[3]),
979e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[4]),
980e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[5]),
981e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[6]));
98202ac6454SAndrew Thompson }
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson static void
98502ac6454SAndrew Thompson ehci_dump_isoc(ehci_softc_t *sc)
98602ac6454SAndrew Thompson {
98702ac6454SAndrew Thompson 	ehci_itd_t *itd;
98802ac6454SAndrew Thompson 	ehci_sitd_t *sitd;
98902ac6454SAndrew Thompson 	uint16_t max = 1000;
99002ac6454SAndrew Thompson 	uint16_t pos;
99102ac6454SAndrew Thompson 
99202ac6454SAndrew Thompson 	pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
99302ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
99402ac6454SAndrew Thompson 
99502ac6454SAndrew Thompson 	printf("%s: isochronous dump from frame 0x%03x:\n",
99602ac6454SAndrew Thompson 	    __FUNCTION__, pos);
99702ac6454SAndrew Thompson 
99802ac6454SAndrew Thompson 	itd = sc->sc_isoc_hs_p_last[pos];
99902ac6454SAndrew Thompson 	sitd = sc->sc_isoc_fs_p_last[pos];
100002ac6454SAndrew Thompson 
100102ac6454SAndrew Thompson 	while (itd && max && max--) {
100202ac6454SAndrew Thompson 		ehci_dump_itd(sc, itd);
100302ac6454SAndrew Thompson 		itd = itd->prev;
100402ac6454SAndrew Thompson 	}
100502ac6454SAndrew Thompson 
100602ac6454SAndrew Thompson 	while (sitd && max && max--) {
100702ac6454SAndrew Thompson 		ehci_dump_sitd(sc, sitd);
100802ac6454SAndrew Thompson 		sitd = sitd->prev;
100902ac6454SAndrew Thompson 	}
101002ac6454SAndrew Thompson }
101102ac6454SAndrew Thompson 
101202ac6454SAndrew Thompson #endif
101302ac6454SAndrew Thompson 
101402ac6454SAndrew Thompson static void
1015760bc48eSAndrew Thompson ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
101602ac6454SAndrew Thompson {
101702ac6454SAndrew Thompson 	/* check for early completion */
101802ac6454SAndrew Thompson 	if (ehci_check_transfer(xfer)) {
101902ac6454SAndrew Thompson 		return;
102002ac6454SAndrew Thompson 	}
102102ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
1022a593f6b8SAndrew Thompson 	usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
102302ac6454SAndrew Thompson 
102402ac6454SAndrew Thompson 	/* start timeout, if any */
102502ac6454SAndrew Thompson 	if (xfer->timeout != 0) {
1026a593f6b8SAndrew Thompson 		usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
102702ac6454SAndrew Thompson 	}
102802ac6454SAndrew Thompson }
102902ac6454SAndrew Thompson 
103002ac6454SAndrew Thompson #define	EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
103102ac6454SAndrew Thompson static ehci_sitd_t *
103202ac6454SAndrew Thompson _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
103302ac6454SAndrew Thompson {
103402ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
103502ac6454SAndrew Thompson 
103602ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
103702ac6454SAndrew Thompson 
103802ac6454SAndrew Thompson 	std->next = last->next;
103902ac6454SAndrew Thompson 	std->sitd_next = last->sitd_next;
104002ac6454SAndrew Thompson 
104102ac6454SAndrew Thompson 	std->prev = last;
104202ac6454SAndrew Thompson 
1043a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
104402ac6454SAndrew Thompson 
104502ac6454SAndrew Thompson 	/*
104602ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
104702ac6454SAndrew Thompson 	 */
104802ac6454SAndrew Thompson 	last->next = std;
104902ac6454SAndrew Thompson 	last->sitd_next = std->sitd_self;
105002ac6454SAndrew Thompson 
1051a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
105202ac6454SAndrew Thompson 
105302ac6454SAndrew Thompson 	return (std);
105402ac6454SAndrew Thompson }
105502ac6454SAndrew Thompson 
105602ac6454SAndrew Thompson #define	EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
105702ac6454SAndrew Thompson static ehci_itd_t *
105802ac6454SAndrew Thompson _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
105902ac6454SAndrew Thompson {
106002ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
106102ac6454SAndrew Thompson 
106202ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
106302ac6454SAndrew Thompson 
106402ac6454SAndrew Thompson 	std->next = last->next;
106502ac6454SAndrew Thompson 	std->itd_next = last->itd_next;
106602ac6454SAndrew Thompson 
106702ac6454SAndrew Thompson 	std->prev = last;
106802ac6454SAndrew Thompson 
1069a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
107002ac6454SAndrew Thompson 
107102ac6454SAndrew Thompson 	/*
107202ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
107302ac6454SAndrew Thompson 	 */
107402ac6454SAndrew Thompson 	last->next = std;
107502ac6454SAndrew Thompson 	last->itd_next = std->itd_self;
107602ac6454SAndrew Thompson 
1077a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
107802ac6454SAndrew Thompson 
107902ac6454SAndrew Thompson 	return (std);
108002ac6454SAndrew Thompson }
108102ac6454SAndrew Thompson 
108202ac6454SAndrew Thompson #define	EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
108302ac6454SAndrew Thompson static ehci_qh_t *
108402ac6454SAndrew Thompson _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
108502ac6454SAndrew Thompson {
108602ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", sqh, last);
108702ac6454SAndrew Thompson 
108802ac6454SAndrew Thompson 	if (sqh->prev != NULL) {
108902ac6454SAndrew Thompson 		/* should not happen */
109002ac6454SAndrew Thompson 		DPRINTFN(0, "QH already linked!\n");
109102ac6454SAndrew Thompson 		return (last);
109202ac6454SAndrew Thompson 	}
109302ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
109402ac6454SAndrew Thompson 
109502ac6454SAndrew Thompson 	sqh->next = last->next;
109602ac6454SAndrew Thompson 	sqh->qh_link = last->qh_link;
109702ac6454SAndrew Thompson 
109802ac6454SAndrew Thompson 	sqh->prev = last;
109902ac6454SAndrew Thompson 
1100a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(sqh->page_cache);
110102ac6454SAndrew Thompson 
110202ac6454SAndrew Thompson 	/*
110302ac6454SAndrew Thompson 	 * the last->next->prev is never followed: sqh->next->prev = sqh;
110402ac6454SAndrew Thompson 	 */
110502ac6454SAndrew Thompson 
110602ac6454SAndrew Thompson 	last->next = sqh;
110702ac6454SAndrew Thompson 	last->qh_link = sqh->qh_self;
110802ac6454SAndrew Thompson 
1109a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
111002ac6454SAndrew Thompson 
111102ac6454SAndrew Thompson 	return (sqh);
111202ac6454SAndrew Thompson }
111302ac6454SAndrew Thompson 
111402ac6454SAndrew Thompson #define	EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
111502ac6454SAndrew Thompson static ehci_sitd_t *
111602ac6454SAndrew Thompson _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
111702ac6454SAndrew Thompson {
111802ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
111902ac6454SAndrew Thompson 
112002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
112102ac6454SAndrew Thompson 
112202ac6454SAndrew Thompson 	std->prev->next = std->next;
112302ac6454SAndrew Thompson 	std->prev->sitd_next = std->sitd_next;
112402ac6454SAndrew Thompson 
1125a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
112602ac6454SAndrew Thompson 
112702ac6454SAndrew Thompson 	if (std->next) {
112802ac6454SAndrew Thompson 		std->next->prev = std->prev;
1129a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
113002ac6454SAndrew Thompson 	}
113102ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
113202ac6454SAndrew Thompson }
113302ac6454SAndrew Thompson 
113402ac6454SAndrew Thompson #define	EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
113502ac6454SAndrew Thompson static ehci_itd_t *
113602ac6454SAndrew Thompson _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
113702ac6454SAndrew Thompson {
113802ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
113902ac6454SAndrew Thompson 
114002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
114102ac6454SAndrew Thompson 
114202ac6454SAndrew Thompson 	std->prev->next = std->next;
114302ac6454SAndrew Thompson 	std->prev->itd_next = std->itd_next;
114402ac6454SAndrew Thompson 
1145a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
114602ac6454SAndrew Thompson 
114702ac6454SAndrew Thompson 	if (std->next) {
114802ac6454SAndrew Thompson 		std->next->prev = std->prev;
1149a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
115002ac6454SAndrew Thompson 	}
115102ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
115202ac6454SAndrew Thompson }
115302ac6454SAndrew Thompson 
115402ac6454SAndrew Thompson #define	EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
115502ac6454SAndrew Thompson static ehci_qh_t *
115602ac6454SAndrew Thompson _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
115702ac6454SAndrew Thompson {
115802ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", sqh, last);
115902ac6454SAndrew Thompson 
116002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
116102ac6454SAndrew Thompson 
116202ac6454SAndrew Thompson 	/* only remove if not removed from a queue */
116302ac6454SAndrew Thompson 	if (sqh->prev) {
116402ac6454SAndrew Thompson 
116502ac6454SAndrew Thompson 		sqh->prev->next = sqh->next;
116602ac6454SAndrew Thompson 		sqh->prev->qh_link = sqh->qh_link;
116702ac6454SAndrew Thompson 
1168a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->prev->page_cache);
116902ac6454SAndrew Thompson 
117002ac6454SAndrew Thompson 		if (sqh->next) {
117102ac6454SAndrew Thompson 			sqh->next->prev = sqh->prev;
1172a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(sqh->next->page_cache);
117302ac6454SAndrew Thompson 		}
117402ac6454SAndrew Thompson 		last = ((last == sqh) ? sqh->prev : last);
117502ac6454SAndrew Thompson 
117602ac6454SAndrew Thompson 		sqh->prev = 0;
117702ac6454SAndrew Thompson 
1178a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->page_cache);
117902ac6454SAndrew Thompson 	}
118002ac6454SAndrew Thompson 	return (last);
118102ac6454SAndrew Thompson }
118202ac6454SAndrew Thompson 
1183dbe63d3aSHans Petter Selasky static void
1184dbe63d3aSHans Petter Selasky ehci_data_toggle_update(struct usb_xfer *xfer, uint16_t actlen, uint16_t xlen)
1185dbe63d3aSHans Petter Selasky {
1186ca794e78SHans Petter Selasky 	uint16_t rem;
1187dbe63d3aSHans Petter Selasky 	uint8_t dt;
1188dbe63d3aSHans Petter Selasky 
1189dbe63d3aSHans Petter Selasky 	/* count number of full packets */
1190dbe63d3aSHans Petter Selasky 	dt = (actlen / xfer->max_packet_size) & 1;
1191dbe63d3aSHans Petter Selasky 
1192996c2735SHans Petter Selasky 	/* compute remainder */
1193ca794e78SHans Petter Selasky 	rem = actlen % xfer->max_packet_size;
1194dbe63d3aSHans Petter Selasky 
1195ca794e78SHans Petter Selasky 	if (rem > 0)
1196dbe63d3aSHans Petter Selasky 		dt ^= 1;	/* short packet at the end */
1197ca794e78SHans Petter Selasky 	else if (actlen != xlen)
1198dbe63d3aSHans Petter Selasky 		dt ^= 1;	/* zero length packet at the end */
1199*ea719edeSHans Petter Selasky 	else if (xlen == 0)
1200*ea719edeSHans Petter Selasky 		dt ^= 1;	/* zero length transfer */
1201dbe63d3aSHans Petter Selasky 
1202dbe63d3aSHans Petter Selasky 	xfer->endpoint->toggle_next ^= dt;
1203dbe63d3aSHans Petter Selasky }
1204dbe63d3aSHans Petter Selasky 
1205e0a69b51SAndrew Thompson static usb_error_t
1206760bc48eSAndrew Thompson ehci_non_isoc_done_sub(struct usb_xfer *xfer)
120702ac6454SAndrew Thompson {
120802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
120902ac6454SAndrew Thompson 	ehci_qtd_t *td;
121002ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
121102ac6454SAndrew Thompson 	uint32_t status;
121202ac6454SAndrew Thompson 	uint16_t len;
121302ac6454SAndrew Thompson 
121402ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
121502ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
121602ac6454SAndrew Thompson 
121702ac6454SAndrew Thompson 	if (xfer->aframes != xfer->nframes) {
1218ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
121902ac6454SAndrew Thompson 	}
122002ac6454SAndrew Thompson 	while (1) {
122102ac6454SAndrew Thompson 
1222a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1223e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->qtd_status);
122402ac6454SAndrew Thompson 
122502ac6454SAndrew Thompson 		len = EHCI_QTD_GET_BYTES(status);
122602ac6454SAndrew Thompson 
122702ac6454SAndrew Thompson 		/*
122802ac6454SAndrew Thompson 	         * Verify the status length and
122902ac6454SAndrew Thompson 		 * add the length to "frlengths[]":
123002ac6454SAndrew Thompson 	         */
123102ac6454SAndrew Thompson 		if (len > td->len) {
123202ac6454SAndrew Thompson 			/* should not happen */
123302ac6454SAndrew Thompson 			DPRINTF("Invalid status length, "
123402ac6454SAndrew Thompson 			    "0x%04x/0x%04x bytes\n", len, td->len);
123502ac6454SAndrew Thompson 			status |= EHCI_QTD_HALTED;
123602ac6454SAndrew Thompson 		} else if (xfer->aframes != xfer->nframes) {
123702ac6454SAndrew Thompson 			xfer->frlengths[xfer->aframes] += td->len - len;
1238dbe63d3aSHans Petter Selasky 			/* manually update data toggle */
1239dbe63d3aSHans Petter Selasky 			ehci_data_toggle_update(xfer, td->len - len, td->len);
124002ac6454SAndrew Thompson 		}
1241dbe63d3aSHans Petter Selasky 
124202ac6454SAndrew Thompson 		/* Check for last transfer */
124302ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
124402ac6454SAndrew Thompson 			td = NULL;
124502ac6454SAndrew Thompson 			break;
124602ac6454SAndrew Thompson 		}
124702ac6454SAndrew Thompson 		/* Check for transfer error */
124802ac6454SAndrew Thompson 		if (status & EHCI_QTD_HALTED) {
124902ac6454SAndrew Thompson 			/* the transfer is finished */
125002ac6454SAndrew Thompson 			td = NULL;
125102ac6454SAndrew Thompson 			break;
125202ac6454SAndrew Thompson 		}
125302ac6454SAndrew Thompson 		/* Check for short transfer */
125402ac6454SAndrew Thompson 		if (len > 0) {
125502ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
125602ac6454SAndrew Thompson 				/* follow alt next */
125702ac6454SAndrew Thompson 				td = td->alt_next;
125802ac6454SAndrew Thompson 			} else {
125902ac6454SAndrew Thompson 				/* the transfer is finished */
126002ac6454SAndrew Thompson 				td = NULL;
126102ac6454SAndrew Thompson 			}
126202ac6454SAndrew Thompson 			break;
126302ac6454SAndrew Thompson 		}
126402ac6454SAndrew Thompson 		td = td->obj_next;
126502ac6454SAndrew Thompson 
126602ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
126702ac6454SAndrew Thompson 			/* this USB frame is complete */
126802ac6454SAndrew Thompson 			break;
126902ac6454SAndrew Thompson 		}
127002ac6454SAndrew Thompson 	}
127102ac6454SAndrew Thompson 
127202ac6454SAndrew Thompson 	/* update transfer cache */
127302ac6454SAndrew Thompson 
127402ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
127502ac6454SAndrew Thompson 
1276b850ecc1SAndrew Thompson #ifdef USB_DEBUG
127702ac6454SAndrew Thompson 	if (status & EHCI_QTD_STATERRS) {
127802ac6454SAndrew Thompson 		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
127902ac6454SAndrew Thompson 		    "status=%s%s%s%s%s%s%s%s\n",
1280ae60fdfbSAndrew Thompson 		    xfer->address, xfer->endpointno, xfer->aframes,
128102ac6454SAndrew Thompson 		    (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
128202ac6454SAndrew Thompson 		    (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
128302ac6454SAndrew Thompson 		    (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
128402ac6454SAndrew Thompson 		    (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
128502ac6454SAndrew Thompson 		    (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
128602ac6454SAndrew Thompson 		    (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
128702ac6454SAndrew Thompson 		    (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
128802ac6454SAndrew Thompson 		    (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
128902ac6454SAndrew Thompson 	}
129002ac6454SAndrew Thompson #endif
129102ac6454SAndrew Thompson 
129202ac6454SAndrew Thompson 	return ((status & EHCI_QTD_HALTED) ?
129302ac6454SAndrew Thompson 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
129402ac6454SAndrew Thompson }
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson static void
1297760bc48eSAndrew Thompson ehci_non_isoc_done(struct usb_xfer *xfer)
129802ac6454SAndrew Thompson {
1299c8a14b91SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1300c8a14b91SAndrew Thompson 	ehci_qh_t *qh;
1301c8a14b91SAndrew Thompson 	uint32_t status;
1302e0a69b51SAndrew Thompson 	usb_error_t err = 0;
130302ac6454SAndrew Thompson 
1304ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1305ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
130602ac6454SAndrew Thompson 
1307b850ecc1SAndrew Thompson #ifdef USB_DEBUG
130802ac6454SAndrew Thompson 	if (ehcidebug > 10) {
130902ac6454SAndrew Thompson 		ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
131002ac6454SAndrew Thompson 
131102ac6454SAndrew Thompson 		ehci_dump_sqtds(sc, xfer->td_transfer_first);
131202ac6454SAndrew Thompson 	}
131302ac6454SAndrew Thompson #endif
131402ac6454SAndrew Thompson 
1315c8a14b91SAndrew Thompson 	/* extract data toggle directly from the QH's overlay area */
1316c8a14b91SAndrew Thompson 
1317c8a14b91SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1318c8a14b91SAndrew Thompson 
1319c8a14b91SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
1320c8a14b91SAndrew Thompson 
1321c8a14b91SAndrew Thompson 	status = hc32toh(sc, qh->qh_qtd.qtd_status);
1322c8a14b91SAndrew Thompson 
132302ac6454SAndrew Thompson 	/* reset scanner */
132402ac6454SAndrew Thompson 
132502ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
132602ac6454SAndrew Thompson 
132702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
132802ac6454SAndrew Thompson 
132902ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
133002ac6454SAndrew Thompson 
133102ac6454SAndrew Thompson 			err = ehci_non_isoc_done_sub(xfer);
133202ac6454SAndrew Thompson 		}
133302ac6454SAndrew Thompson 		xfer->aframes = 1;
133402ac6454SAndrew Thompson 
133502ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
133602ac6454SAndrew Thompson 			goto done;
133702ac6454SAndrew Thompson 		}
133802ac6454SAndrew Thompson 	}
133902ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
134002ac6454SAndrew Thompson 
134102ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
134202ac6454SAndrew Thompson 		xfer->aframes++;
134302ac6454SAndrew Thompson 
134402ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
134502ac6454SAndrew Thompson 			goto done;
134602ac6454SAndrew Thompson 		}
134702ac6454SAndrew Thompson 	}
134802ac6454SAndrew Thompson 
134902ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
135002ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
135102ac6454SAndrew Thompson 
135202ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
135302ac6454SAndrew Thompson 	}
135402ac6454SAndrew Thompson done:
135502ac6454SAndrew Thompson 	ehci_device_done(xfer, err);
135602ac6454SAndrew Thompson }
135702ac6454SAndrew Thompson 
135802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
135902ac6454SAndrew Thompson  *	ehci_check_transfer
136002ac6454SAndrew Thompson  *
136102ac6454SAndrew Thompson  * Return values:
136202ac6454SAndrew Thompson  *    0: USB transfer is not finished
136302ac6454SAndrew Thompson  * Else: USB transfer is finished
136402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
136502ac6454SAndrew Thompson static uint8_t
1366760bc48eSAndrew Thompson ehci_check_transfer(struct usb_xfer *xfer)
136702ac6454SAndrew Thompson {
1368ae60fdfbSAndrew Thompson 	struct usb_pipe_methods *methods = xfer->endpoint->methods;
136902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
137002ac6454SAndrew Thompson 
137102ac6454SAndrew Thompson 	uint32_t status;
137202ac6454SAndrew Thompson 
137302ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
137402ac6454SAndrew Thompson 
137502ac6454SAndrew Thompson 	if (methods == &ehci_device_isoc_fs_methods) {
137602ac6454SAndrew Thompson 		ehci_sitd_t *td;
137702ac6454SAndrew Thompson 
137802ac6454SAndrew Thompson 		/* isochronous full speed transfer */
137902ac6454SAndrew Thompson 
138002ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1381a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1382e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
138302ac6454SAndrew Thompson 
138402ac6454SAndrew Thompson 		/* also check if first is complete */
138502ac6454SAndrew Thompson 
138602ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1387a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1388e55e1ebcSAndrew Thompson 		status |= hc32toh(sc, td->sitd_status);
138902ac6454SAndrew Thompson 
139002ac6454SAndrew Thompson 		if (!(status & EHCI_SITD_ACTIVE)) {
139102ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
139202ac6454SAndrew Thompson 			goto transferred;
139302ac6454SAndrew Thompson 		}
139402ac6454SAndrew Thompson 	} else if (methods == &ehci_device_isoc_hs_methods) {
139502ac6454SAndrew Thompson 		ehci_itd_t *td;
139602ac6454SAndrew Thompson 
139702ac6454SAndrew Thompson 		/* isochronous high speed transfer */
139802ac6454SAndrew Thompson 
139989119752SAndrew Thompson 		/* check last transfer */
140002ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1401a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1402d1f7c4baSAndrew Thompson 		status = td->itd_status[0];
1403d1f7c4baSAndrew Thompson 		status |= td->itd_status[1];
1404d1f7c4baSAndrew Thompson 		status |= td->itd_status[2];
1405d1f7c4baSAndrew Thompson 		status |= td->itd_status[3];
1406d1f7c4baSAndrew Thompson 		status |= td->itd_status[4];
1407d1f7c4baSAndrew Thompson 		status |= td->itd_status[5];
1408d1f7c4baSAndrew Thompson 		status |= td->itd_status[6];
1409d1f7c4baSAndrew Thompson 		status |= td->itd_status[7];
141002ac6454SAndrew Thompson 
141102ac6454SAndrew Thompson 		/* also check first transfer */
141202ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1413a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
141489119752SAndrew Thompson 		status |= td->itd_status[0];
1415d1f7c4baSAndrew Thompson 		status |= td->itd_status[1];
1416d1f7c4baSAndrew Thompson 		status |= td->itd_status[2];
1417d1f7c4baSAndrew Thompson 		status |= td->itd_status[3];
1418d1f7c4baSAndrew Thompson 		status |= td->itd_status[4];
1419d1f7c4baSAndrew Thompson 		status |= td->itd_status[5];
1420d1f7c4baSAndrew Thompson 		status |= td->itd_status[6];
1421d1f7c4baSAndrew Thompson 		status |= td->itd_status[7];
142202ac6454SAndrew Thompson 
142302ac6454SAndrew Thompson 		/* if no transactions are active we continue */
1424e55e1ebcSAndrew Thompson 		if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
142502ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
142602ac6454SAndrew Thompson 			goto transferred;
142702ac6454SAndrew Thompson 		}
142802ac6454SAndrew Thompson 	} else {
142902ac6454SAndrew Thompson 		ehci_qtd_t *td;
1430c8a14b91SAndrew Thompson 		ehci_qh_t *qh;
143102ac6454SAndrew Thompson 
143202ac6454SAndrew Thompson 		/* non-isochronous transfer */
143302ac6454SAndrew Thompson 
143402ac6454SAndrew Thompson 		/*
143502ac6454SAndrew Thompson 		 * check whether there is an error somewhere in the middle,
143602ac6454SAndrew Thompson 		 * or whether there was a short packet (SPD and not ACTIVE)
143702ac6454SAndrew Thompson 		 */
143802ac6454SAndrew Thompson 		td = xfer->td_transfer_cache;
143902ac6454SAndrew Thompson 
1440c8a14b91SAndrew Thompson 		qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1441c8a14b91SAndrew Thompson 
1442c8a14b91SAndrew Thompson 		usb_pc_cpu_invalidate(qh->page_cache);
1443c8a14b91SAndrew Thompson 
1444c8a14b91SAndrew Thompson 		status = hc32toh(sc, qh->qh_qtd.qtd_status);
1445c8a14b91SAndrew Thompson 		if (status & EHCI_QTD_ACTIVE) {
1446c8a14b91SAndrew Thompson 			/* transfer is pending */
1447c8a14b91SAndrew Thompson 			goto done;
1448c8a14b91SAndrew Thompson 		}
1449c8a14b91SAndrew Thompson 
145002ac6454SAndrew Thompson 		while (1) {
1451a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(td->page_cache);
1452e55e1ebcSAndrew Thompson 			status = hc32toh(sc, td->qtd_status);
145302ac6454SAndrew Thompson 
145402ac6454SAndrew Thompson 			/*
1455c8a14b91SAndrew Thompson 			 * Check if there is an active TD which
1456c8a14b91SAndrew Thompson 			 * indicates that the transfer isn't done.
145702ac6454SAndrew Thompson 			 */
145802ac6454SAndrew Thompson 			if (status & EHCI_QTD_ACTIVE) {
145902ac6454SAndrew Thompson 				/* update cache */
146002ac6454SAndrew Thompson 				xfer->td_transfer_cache = td;
146102ac6454SAndrew Thompson 				goto done;
146202ac6454SAndrew Thompson 			}
146302ac6454SAndrew Thompson 			/*
146402ac6454SAndrew Thompson 			 * last transfer descriptor makes the transfer done
146502ac6454SAndrew Thompson 			 */
146602ac6454SAndrew Thompson 			if (((void *)td) == xfer->td_transfer_last) {
146702ac6454SAndrew Thompson 				break;
146802ac6454SAndrew Thompson 			}
146902ac6454SAndrew Thompson 			/*
147002ac6454SAndrew Thompson 			 * any kind of error makes the transfer done
147102ac6454SAndrew Thompson 			 */
147202ac6454SAndrew Thompson 			if (status & EHCI_QTD_HALTED) {
147302ac6454SAndrew Thompson 				break;
147402ac6454SAndrew Thompson 			}
147502ac6454SAndrew Thompson 			/*
147602ac6454SAndrew Thompson 			 * if there is no alternate next transfer, a short
147702ac6454SAndrew Thompson 			 * packet also makes the transfer done
147802ac6454SAndrew Thompson 			 */
147902ac6454SAndrew Thompson 			if (EHCI_QTD_GET_BYTES(status)) {
148002ac6454SAndrew Thompson 				if (xfer->flags_int.short_frames_ok) {
148102ac6454SAndrew Thompson 					/* follow alt next */
148202ac6454SAndrew Thompson 					if (td->alt_next) {
148302ac6454SAndrew Thompson 						td = td->alt_next;
148402ac6454SAndrew Thompson 						continue;
148502ac6454SAndrew Thompson 					}
148602ac6454SAndrew Thompson 				}
148702ac6454SAndrew Thompson 				/* transfer is done */
148802ac6454SAndrew Thompson 				break;
148902ac6454SAndrew Thompson 			}
149002ac6454SAndrew Thompson 			td = td->obj_next;
149102ac6454SAndrew Thompson 		}
149202ac6454SAndrew Thompson 		ehci_non_isoc_done(xfer);
149302ac6454SAndrew Thompson 		goto transferred;
149402ac6454SAndrew Thompson 	}
149502ac6454SAndrew Thompson 
149602ac6454SAndrew Thompson done:
149702ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
149802ac6454SAndrew Thompson 	return (0);
149902ac6454SAndrew Thompson 
150002ac6454SAndrew Thompson transferred:
150102ac6454SAndrew Thompson 	return (1);
150202ac6454SAndrew Thompson }
150302ac6454SAndrew Thompson 
150402ac6454SAndrew Thompson static void
150502ac6454SAndrew Thompson ehci_pcd_enable(ehci_softc_t *sc)
150602ac6454SAndrew Thompson {
150702ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
150802ac6454SAndrew Thompson 
150902ac6454SAndrew Thompson 	sc->sc_eintrs |= EHCI_STS_PCD;
151002ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
151102ac6454SAndrew Thompson 
151202ac6454SAndrew Thompson 	/* acknowledge any PCD interrupt */
151302ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
151402ac6454SAndrew Thompson 
151539307315SAndrew Thompson 	ehci_root_intr(sc);
151602ac6454SAndrew Thompson }
151702ac6454SAndrew Thompson 
151802ac6454SAndrew Thompson static void
151902ac6454SAndrew Thompson ehci_interrupt_poll(ehci_softc_t *sc)
152002ac6454SAndrew Thompson {
1521760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
152202ac6454SAndrew Thompson 
152302ac6454SAndrew Thompson repeat:
152402ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
152502ac6454SAndrew Thompson 		/*
152602ac6454SAndrew Thompson 		 * check if transfer is transferred
152702ac6454SAndrew Thompson 		 */
152802ac6454SAndrew Thompson 		if (ehci_check_transfer(xfer)) {
152902ac6454SAndrew Thompson 			/* queue has been modified */
153002ac6454SAndrew Thompson 			goto repeat;
153102ac6454SAndrew Thompson 		}
153202ac6454SAndrew Thompson 	}
153302ac6454SAndrew Thompson }
153402ac6454SAndrew Thompson 
153530b22abeSAndrew Thompson /*
153630b22abeSAndrew Thompson  * Some EHCI chips from VIA / ATI seem to trigger interrupts before
153730b22abeSAndrew Thompson  * writing back the qTD status, or miss signalling occasionally under
153830b22abeSAndrew Thompson  * heavy load.  If the host machine is too fast, we can miss
153930b22abeSAndrew Thompson  * transaction completion - when we scan the active list the
154030b22abeSAndrew Thompson  * transaction still seems to be active. This generally exhibits
154130b22abeSAndrew Thompson  * itself as a umass stall that never recovers.
154230b22abeSAndrew Thompson  *
154330b22abeSAndrew Thompson  * We work around this behaviour by setting up this callback after any
154430b22abeSAndrew Thompson  * softintr that completes with transactions still pending, giving us
154530b22abeSAndrew Thompson  * another chance to check for completion after the writeback has
154630b22abeSAndrew Thompson  * taken place.
154730b22abeSAndrew Thompson  */
154830b22abeSAndrew Thompson static void
154930b22abeSAndrew Thompson ehci_poll_timeout(void *arg)
155030b22abeSAndrew Thompson {
155130b22abeSAndrew Thompson 	ehci_softc_t *sc = arg;
155230b22abeSAndrew Thompson 
155330b22abeSAndrew Thompson 	DPRINTFN(3, "\n");
155430b22abeSAndrew Thompson 	ehci_interrupt_poll(sc);
155530b22abeSAndrew Thompson }
155630b22abeSAndrew Thompson 
155702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
155802ac6454SAndrew Thompson  *	ehci_interrupt - EHCI interrupt handler
155902ac6454SAndrew Thompson  *
156002ac6454SAndrew Thompson  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
156102ac6454SAndrew Thompson  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
156202ac6454SAndrew Thompson  * is present !
156302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
156402ac6454SAndrew Thompson void
156502ac6454SAndrew Thompson ehci_interrupt(ehci_softc_t *sc)
156602ac6454SAndrew Thompson {
156702ac6454SAndrew Thompson 	uint32_t status;
156802ac6454SAndrew Thompson 
156902ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
157002ac6454SAndrew Thompson 
157102ac6454SAndrew Thompson 	DPRINTFN(16, "real interrupt\n");
157202ac6454SAndrew Thompson 
1573b850ecc1SAndrew Thompson #ifdef USB_DEBUG
157402ac6454SAndrew Thompson 	if (ehcidebug > 15) {
157502ac6454SAndrew Thompson 		ehci_dump_regs(sc);
157602ac6454SAndrew Thompson 	}
157702ac6454SAndrew Thompson #endif
157802ac6454SAndrew Thompson 
157902ac6454SAndrew Thompson 	status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
158002ac6454SAndrew Thompson 	if (status == 0) {
158102ac6454SAndrew Thompson 		/* the interrupt was not for us */
158202ac6454SAndrew Thompson 		goto done;
158302ac6454SAndrew Thompson 	}
158402ac6454SAndrew Thompson 	if (!(status & sc->sc_eintrs)) {
158502ac6454SAndrew Thompson 		goto done;
158602ac6454SAndrew Thompson 	}
158702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, status);	/* acknowledge */
158802ac6454SAndrew Thompson 
158902ac6454SAndrew Thompson 	status &= sc->sc_eintrs;
159002ac6454SAndrew Thompson 
159102ac6454SAndrew Thompson 	if (status & EHCI_STS_HSE) {
159202ac6454SAndrew Thompson 		printf("%s: unrecoverable error, "
159302ac6454SAndrew Thompson 		    "controller halted\n", __FUNCTION__);
1594b850ecc1SAndrew Thompson #ifdef USB_DEBUG
159502ac6454SAndrew Thompson 		ehci_dump_regs(sc);
159602ac6454SAndrew Thompson 		ehci_dump_isoc(sc);
159702ac6454SAndrew Thompson #endif
159802ac6454SAndrew Thompson 	}
159902ac6454SAndrew Thompson 	if (status & EHCI_STS_PCD) {
160002ac6454SAndrew Thompson 		/*
160102ac6454SAndrew Thompson 		 * Disable PCD interrupt for now, because it will be
160202ac6454SAndrew Thompson 		 * on until the port has been reset.
160302ac6454SAndrew Thompson 		 */
160402ac6454SAndrew Thompson 		sc->sc_eintrs &= ~EHCI_STS_PCD;
160502ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
160602ac6454SAndrew Thompson 
160739307315SAndrew Thompson 		ehci_root_intr(sc);
160802ac6454SAndrew Thompson 
160902ac6454SAndrew Thompson 		/* do not allow RHSC interrupts > 1 per second */
1610a593f6b8SAndrew Thompson 		usb_callout_reset(&sc->sc_tmo_pcd, hz,
161102ac6454SAndrew Thompson 		    (void *)&ehci_pcd_enable, sc);
161202ac6454SAndrew Thompson 	}
161302ac6454SAndrew Thompson 	status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
161402ac6454SAndrew Thompson 
161502ac6454SAndrew Thompson 	if (status != 0) {
161602ac6454SAndrew Thompson 		/* block unprocessed interrupts */
161702ac6454SAndrew Thompson 		sc->sc_eintrs &= ~status;
161802ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
161902ac6454SAndrew Thompson 		printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
162002ac6454SAndrew Thompson 	}
162102ac6454SAndrew Thompson 	/* poll all the USB transfers */
162202ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
162302ac6454SAndrew Thompson 
162430b22abeSAndrew Thompson 	if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) {
162530b22abeSAndrew Thompson 		usb_callout_reset(&sc->sc_tmo_poll, hz / 128,
162630b22abeSAndrew Thompson 		    (void *)&ehci_poll_timeout, sc);
162730b22abeSAndrew Thompson 	}
162830b22abeSAndrew Thompson 
162902ac6454SAndrew Thompson done:
163002ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
163102ac6454SAndrew Thompson }
163202ac6454SAndrew Thompson 
163302ac6454SAndrew Thompson /*
163402ac6454SAndrew Thompson  * called when a request does not complete
163502ac6454SAndrew Thompson  */
163602ac6454SAndrew Thompson static void
163702ac6454SAndrew Thompson ehci_timeout(void *arg)
163802ac6454SAndrew Thompson {
1639760bc48eSAndrew Thompson 	struct usb_xfer *xfer = arg;
164002ac6454SAndrew Thompson 
164102ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
164202ac6454SAndrew Thompson 
164302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
164402ac6454SAndrew Thompson 
164502ac6454SAndrew Thompson 	/* transfer is transferred */
164602ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_TIMEOUT);
164702ac6454SAndrew Thompson }
164802ac6454SAndrew Thompson 
164902ac6454SAndrew Thompson static void
1650760bc48eSAndrew Thompson ehci_do_poll(struct usb_bus *bus)
165102ac6454SAndrew Thompson {
165202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
165302ac6454SAndrew Thompson 
165402ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
165502ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
165602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
165702ac6454SAndrew Thompson }
165802ac6454SAndrew Thompson 
165902ac6454SAndrew Thompson static void
166002ac6454SAndrew Thompson ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
166102ac6454SAndrew Thompson {
1662760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
166302ac6454SAndrew Thompson 	ehci_qtd_t *td;
166402ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
166502ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
166602ac6454SAndrew Thompson 	uint32_t buf_offset;
166702ac6454SAndrew Thompson 	uint32_t average;
166802ac6454SAndrew Thompson 	uint32_t len_old;
16690f7d4548SAndrew Thompson 	uint32_t terminate;
167053e0bf6eSHans Petter Selasky 	uint32_t qtd_altnext;
167102ac6454SAndrew Thompson 	uint8_t shortpkt_old;
167202ac6454SAndrew Thompson 	uint8_t precompute;
167302ac6454SAndrew Thompson 
167453e0bf6eSHans Petter Selasky 	terminate = temp->sc->sc_terminate_self;
167553e0bf6eSHans Petter Selasky 	qtd_altnext = temp->sc->sc_terminate_self;
167602ac6454SAndrew Thompson 	td_alt_next = NULL;
167702ac6454SAndrew Thompson 	buf_offset = 0;
167802ac6454SAndrew Thompson 	shortpkt_old = temp->shortpkt;
167902ac6454SAndrew Thompson 	len_old = temp->len;
168002ac6454SAndrew Thompson 	precompute = 1;
168102ac6454SAndrew Thompson 
168202ac6454SAndrew Thompson restart:
168302ac6454SAndrew Thompson 
168402ac6454SAndrew Thompson 	td = temp->td;
168502ac6454SAndrew Thompson 	td_next = temp->td_next;
168602ac6454SAndrew Thompson 
168702ac6454SAndrew Thompson 	while (1) {
168802ac6454SAndrew Thompson 
168902ac6454SAndrew Thompson 		if (temp->len == 0) {
169002ac6454SAndrew Thompson 
169102ac6454SAndrew Thompson 			if (temp->shortpkt) {
169202ac6454SAndrew Thompson 				break;
169302ac6454SAndrew Thompson 			}
169402ac6454SAndrew Thompson 			/* send a Zero Length Packet, ZLP, last */
169502ac6454SAndrew Thompson 
169602ac6454SAndrew Thompson 			temp->shortpkt = 1;
169702ac6454SAndrew Thompson 			average = 0;
169802ac6454SAndrew Thompson 
169902ac6454SAndrew Thompson 		} else {
170002ac6454SAndrew Thompson 
170102ac6454SAndrew Thompson 			average = temp->average;
170202ac6454SAndrew Thompson 
170302ac6454SAndrew Thompson 			if (temp->len < average) {
170402ac6454SAndrew Thompson 				if (temp->len % temp->max_frame_size) {
170502ac6454SAndrew Thompson 					temp->shortpkt = 1;
170602ac6454SAndrew Thompson 				}
170702ac6454SAndrew Thompson 				average = temp->len;
170802ac6454SAndrew Thompson 			}
170902ac6454SAndrew Thompson 		}
171002ac6454SAndrew Thompson 
171102ac6454SAndrew Thompson 		if (td_next == NULL) {
171202ac6454SAndrew Thompson 			panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
171302ac6454SAndrew Thompson 		}
171402ac6454SAndrew Thompson 		/* get next TD */
171502ac6454SAndrew Thompson 
171602ac6454SAndrew Thompson 		td = td_next;
171702ac6454SAndrew Thompson 		td_next = td->obj_next;
171802ac6454SAndrew Thompson 
171902ac6454SAndrew Thompson 		/* check if we are pre-computing */
172002ac6454SAndrew Thompson 
172102ac6454SAndrew Thompson 		if (precompute) {
172202ac6454SAndrew Thompson 
172302ac6454SAndrew Thompson 			/* update remaining length */
172402ac6454SAndrew Thompson 
172502ac6454SAndrew Thompson 			temp->len -= average;
172602ac6454SAndrew Thompson 
172702ac6454SAndrew Thompson 			continue;
172802ac6454SAndrew Thompson 		}
172902ac6454SAndrew Thompson 		/* fill out current TD */
173002ac6454SAndrew Thompson 
173102ac6454SAndrew Thompson 		td->qtd_status =
173202ac6454SAndrew Thompson 		    temp->qtd_status |
1733c8a14b91SAndrew Thompson 		    htohc32(temp->sc, EHCI_QTD_IOC |
1734c8a14b91SAndrew Thompson 			EHCI_QTD_SET_BYTES(average));
173502ac6454SAndrew Thompson 
173602ac6454SAndrew Thompson 		if (average == 0) {
173702ac6454SAndrew Thompson 
173802ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
173902ac6454SAndrew Thompson 
174002ac6454SAndrew Thompson 				/* update data toggle, ZLP case */
174102ac6454SAndrew Thompson 
174202ac6454SAndrew Thompson 				temp->qtd_status ^=
1743e55e1ebcSAndrew Thompson 				    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
174402ac6454SAndrew Thompson 			}
174502ac6454SAndrew Thompson 			td->len = 0;
174602ac6454SAndrew Thompson 
174702ac6454SAndrew Thompson 			td->qtd_buffer[0] = 0;
174802ac6454SAndrew Thompson 			td->qtd_buffer_hi[0] = 0;
174902ac6454SAndrew Thompson 
175002ac6454SAndrew Thompson 			td->qtd_buffer[1] = 0;
175102ac6454SAndrew Thompson 			td->qtd_buffer_hi[1] = 0;
175202ac6454SAndrew Thompson 
175302ac6454SAndrew Thompson 		} else {
175402ac6454SAndrew Thompson 
175502ac6454SAndrew Thompson 			uint8_t x;
175602ac6454SAndrew Thompson 
175702ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
175802ac6454SAndrew Thompson 
175902ac6454SAndrew Thompson 				/* update data toggle */
176002ac6454SAndrew Thompson 
176102ac6454SAndrew Thompson 				if (((average + temp->max_frame_size - 1) /
176202ac6454SAndrew Thompson 				    temp->max_frame_size) & 1) {
176302ac6454SAndrew Thompson 					temp->qtd_status ^=
1764e55e1ebcSAndrew Thompson 					    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
176502ac6454SAndrew Thompson 				}
176602ac6454SAndrew Thompson 			}
176702ac6454SAndrew Thompson 			td->len = average;
176802ac6454SAndrew Thompson 
176902ac6454SAndrew Thompson 			/* update remaining length */
177002ac6454SAndrew Thompson 
177102ac6454SAndrew Thompson 			temp->len -= average;
177202ac6454SAndrew Thompson 
177302ac6454SAndrew Thompson 			/* fill out buffer pointers */
177402ac6454SAndrew Thompson 
1775a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset, &buf_res);
177602ac6454SAndrew Thompson 			td->qtd_buffer[0] =
1777e55e1ebcSAndrew Thompson 			    htohc32(temp->sc, buf_res.physaddr);
177802ac6454SAndrew Thompson 			td->qtd_buffer_hi[0] = 0;
177902ac6454SAndrew Thompson 
178002ac6454SAndrew Thompson 			x = 1;
178102ac6454SAndrew Thompson 
178202ac6454SAndrew Thompson 			while (average > EHCI_PAGE_SIZE) {
178302ac6454SAndrew Thompson 				average -= EHCI_PAGE_SIZE;
178402ac6454SAndrew Thompson 				buf_offset += EHCI_PAGE_SIZE;
1785a593f6b8SAndrew Thompson 				usbd_get_page(temp->pc, buf_offset, &buf_res);
178602ac6454SAndrew Thompson 				td->qtd_buffer[x] =
1787e55e1ebcSAndrew Thompson 				    htohc32(temp->sc,
178802ac6454SAndrew Thompson 				    buf_res.physaddr & (~0xFFF));
178902ac6454SAndrew Thompson 				td->qtd_buffer_hi[x] = 0;
179002ac6454SAndrew Thompson 				x++;
179102ac6454SAndrew Thompson 			}
179202ac6454SAndrew Thompson 
179302ac6454SAndrew Thompson 			/*
179402ac6454SAndrew Thompson 			 * NOTE: The "average" variable is never zero after
179502ac6454SAndrew Thompson 			 * exiting the loop above !
179602ac6454SAndrew Thompson 			 *
179702ac6454SAndrew Thompson 			 * NOTE: We have to subtract one from the offset to
179802ac6454SAndrew Thompson 			 * ensure that we are computing the physical address
179902ac6454SAndrew Thompson 			 * of a valid page !
180002ac6454SAndrew Thompson 			 */
180102ac6454SAndrew Thompson 			buf_offset += average;
1802a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
180302ac6454SAndrew Thompson 			td->qtd_buffer[x] =
1804e55e1ebcSAndrew Thompson 			    htohc32(temp->sc,
180502ac6454SAndrew Thompson 			    buf_res.physaddr & (~0xFFF));
180602ac6454SAndrew Thompson 			td->qtd_buffer_hi[x] = 0;
180702ac6454SAndrew Thompson 		}
180802ac6454SAndrew Thompson 
180902ac6454SAndrew Thompson 		if (td_next) {
181002ac6454SAndrew Thompson 			/* link the current TD with the next one */
181102ac6454SAndrew Thompson 			td->qtd_next = td_next->qtd_self;
181202ac6454SAndrew Thompson 		}
181353e0bf6eSHans Petter Selasky 		td->qtd_altnext = qtd_altnext;
181402ac6454SAndrew Thompson 		td->alt_next = td_alt_next;
181502ac6454SAndrew Thompson 
1816a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
181702ac6454SAndrew Thompson 	}
181802ac6454SAndrew Thompson 
181902ac6454SAndrew Thompson 	if (precompute) {
182002ac6454SAndrew Thompson 		precompute = 0;
182102ac6454SAndrew Thompson 
182202ac6454SAndrew Thompson 		/* setup alt next pointer, if any */
18230f7d4548SAndrew Thompson 		if (temp->last_frame) {
18240f7d4548SAndrew Thompson 			td_alt_next = NULL;
182553e0bf6eSHans Petter Selasky 			qtd_altnext = terminate;
182602ac6454SAndrew Thompson 		} else {
182702ac6454SAndrew Thompson 			/* we use this field internally */
182802ac6454SAndrew Thompson 			td_alt_next = td_next;
182953e0bf6eSHans Petter Selasky 			if (temp->setup_alt_next) {
183053e0bf6eSHans Petter Selasky 				qtd_altnext = td_next->qtd_self;
183153e0bf6eSHans Petter Selasky 			} else {
183253e0bf6eSHans Petter Selasky 				qtd_altnext = terminate;
183353e0bf6eSHans Petter Selasky 			}
183402ac6454SAndrew Thompson 		}
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson 		/* restore */
183702ac6454SAndrew Thompson 		temp->shortpkt = shortpkt_old;
183802ac6454SAndrew Thompson 		temp->len = len_old;
183902ac6454SAndrew Thompson 		goto restart;
184002ac6454SAndrew Thompson 	}
184102ac6454SAndrew Thompson 	temp->td = td;
184202ac6454SAndrew Thompson 	temp->td_next = td_next;
184302ac6454SAndrew Thompson }
184402ac6454SAndrew Thompson 
184502ac6454SAndrew Thompson static void
1846760bc48eSAndrew Thompson ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
184702ac6454SAndrew Thompson {
184802ac6454SAndrew Thompson 	struct ehci_std_temp temp;
1849760bc48eSAndrew Thompson 	struct usb_pipe_methods *methods;
185002ac6454SAndrew Thompson 	ehci_qh_t *qh;
185102ac6454SAndrew Thompson 	ehci_qtd_t *td;
185202ac6454SAndrew Thompson 	uint32_t qh_endp;
185302ac6454SAndrew Thompson 	uint32_t qh_endphub;
185402ac6454SAndrew Thompson 	uint32_t x;
185502ac6454SAndrew Thompson 
185602ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1857ae60fdfbSAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
1858a593f6b8SAndrew Thompson 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
185902ac6454SAndrew Thompson 
1860578d0effSAndrew Thompson 	temp.average = xfer->max_hc_frame_size;
186102ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
186202ac6454SAndrew Thompson 	temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
186302ac6454SAndrew Thompson 
186402ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
186502ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
186602ac6454SAndrew Thompson 
186702ac6454SAndrew Thompson 	/* get next DMA set */
186802ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
186902ac6454SAndrew Thompson 
187002ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
187102ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
187202ac6454SAndrew Thompson 
187302ac6454SAndrew Thompson 	temp.td = NULL;
187402ac6454SAndrew Thompson 	temp.td_next = td;
187502ac6454SAndrew Thompson 	temp.qtd_status = 0;
18760f7d4548SAndrew Thompson 	temp.last_frame = 0;
187702ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
187802ac6454SAndrew Thompson 
187902ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
1880ae60fdfbSAndrew Thompson 		if (xfer->endpoint->toggle_next) {
188102ac6454SAndrew Thompson 			/* DATA1 is next */
188202ac6454SAndrew Thompson 			temp.qtd_status |=
1883e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
188402ac6454SAndrew Thompson 		}
188502ac6454SAndrew Thompson 		temp.auto_data_toggle = 0;
188602ac6454SAndrew Thompson 	} else {
188702ac6454SAndrew Thompson 		temp.auto_data_toggle = 1;
188802ac6454SAndrew Thompson 	}
188902ac6454SAndrew Thompson 
189053e0bf6eSHans Petter Selasky 	if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
189153e0bf6eSHans Petter Selasky 	    (xfer->xroot->udev->address != 0)) {
189202ac6454SAndrew Thompson 		/* max 3 retries */
189302ac6454SAndrew Thompson 		temp.qtd_status |=
1894e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
189502ac6454SAndrew Thompson 	}
189602ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
189702ac6454SAndrew Thompson 
189802ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
189902ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
190002ac6454SAndrew Thompson 
1901dbe63d3aSHans Petter Selasky 			xfer->endpoint->toggle_next = 0;
1902dbe63d3aSHans Petter Selasky 
190302ac6454SAndrew Thompson 			temp.qtd_status &=
1904e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1905e55e1ebcSAndrew Thompson 			temp.qtd_status |= htohc32(temp.sc,
19065f128668SAndrew Thompson 			    EHCI_QTD_ACTIVE |
190702ac6454SAndrew Thompson 			    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
190802ac6454SAndrew Thompson 			    EHCI_QTD_SET_TOGGLE(0));
190902ac6454SAndrew Thompson 
191002ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
191102ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + 0;
191202ac6454SAndrew Thompson 			temp.shortpkt = temp.len ? 1 : 0;
19130f7d4548SAndrew Thompson 			/* check for last frame */
19140f7d4548SAndrew Thompson 			if (xfer->nframes == 1) {
19150f7d4548SAndrew Thompson 				/* no STATUS stage yet, SETUP is last */
19160f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
19170f7d4548SAndrew Thompson 					temp.last_frame = 1;
19180f7d4548SAndrew Thompson 					temp.setup_alt_next = 0;
19190f7d4548SAndrew Thompson 				}
19200f7d4548SAndrew Thompson 			}
192102ac6454SAndrew Thompson 			ehci_setup_standard_chain_sub(&temp);
192202ac6454SAndrew Thompson 		}
192302ac6454SAndrew Thompson 		x = 1;
192402ac6454SAndrew Thompson 	} else {
192502ac6454SAndrew Thompson 		x = 0;
192602ac6454SAndrew Thompson 	}
192702ac6454SAndrew Thompson 
192802ac6454SAndrew Thompson 	while (x != xfer->nframes) {
192902ac6454SAndrew Thompson 
193002ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
193102ac6454SAndrew Thompson 
193202ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
193302ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
193402ac6454SAndrew Thompson 
193502ac6454SAndrew Thompson 		x++;
193602ac6454SAndrew Thompson 
193702ac6454SAndrew Thompson 		if (x == xfer->nframes) {
19380f7d4548SAndrew Thompson 			if (xfer->flags_int.control_xfr) {
19390f7d4548SAndrew Thompson 				/* no STATUS stage yet, DATA is last */
19400f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
19410f7d4548SAndrew Thompson 					temp.last_frame = 1;
194202ac6454SAndrew Thompson 					temp.setup_alt_next = 0;
194302ac6454SAndrew Thompson 				}
19440f7d4548SAndrew Thompson 			} else {
19450f7d4548SAndrew Thompson 				temp.last_frame = 1;
19460f7d4548SAndrew Thompson 				temp.setup_alt_next = 0;
19470f7d4548SAndrew Thompson 			}
19480f7d4548SAndrew Thompson 		}
194902ac6454SAndrew Thompson 		/* keep previous data toggle and error count */
195002ac6454SAndrew Thompson 
195102ac6454SAndrew Thompson 		temp.qtd_status &=
1952e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
195302ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
195402ac6454SAndrew Thompson 
195502ac6454SAndrew Thompson 		if (temp.len == 0) {
195602ac6454SAndrew Thompson 
195702ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
195802ac6454SAndrew Thompson 
195902ac6454SAndrew Thompson 			temp.shortpkt = 0;
196002ac6454SAndrew Thompson 
196102ac6454SAndrew Thompson 		} else {
196202ac6454SAndrew Thompson 
196302ac6454SAndrew Thompson 			/* regular data transfer */
196402ac6454SAndrew Thompson 
196502ac6454SAndrew Thompson 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
196602ac6454SAndrew Thompson 		}
196702ac6454SAndrew Thompson 
196802ac6454SAndrew Thompson 		/* set endpoint direction */
196902ac6454SAndrew Thompson 
197002ac6454SAndrew Thompson 		temp.qtd_status |=
1971ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1972e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
197302ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1974e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
197502ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
197602ac6454SAndrew Thompson 
197702ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
197802ac6454SAndrew Thompson 	}
197902ac6454SAndrew Thompson 
198002ac6454SAndrew Thompson 	/* check if we should append a status stage */
198102ac6454SAndrew Thompson 
198202ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
198302ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
198402ac6454SAndrew Thompson 
198502ac6454SAndrew Thompson 		/*
198602ac6454SAndrew Thompson 		 * Send a DATA1 message and invert the current endpoint
198702ac6454SAndrew Thompson 		 * direction.
198802ac6454SAndrew Thompson 		 */
198902ac6454SAndrew Thompson 
1990e55e1ebcSAndrew Thompson 		temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
199102ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
199202ac6454SAndrew Thompson 		temp.qtd_status |=
1993ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1994e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
199502ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
199602ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1)) :
1997e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
199802ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
199902ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
200002ac6454SAndrew Thompson 
200102ac6454SAndrew Thompson 		temp.len = 0;
200202ac6454SAndrew Thompson 		temp.pc = NULL;
200302ac6454SAndrew Thompson 		temp.shortpkt = 0;
20040f7d4548SAndrew Thompson 		temp.last_frame = 1;
20050f7d4548SAndrew Thompson 		temp.setup_alt_next = 0;
200602ac6454SAndrew Thompson 
200702ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
200802ac6454SAndrew Thompson 	}
200902ac6454SAndrew Thompson 	td = temp.td;
201002ac6454SAndrew Thompson 
201102ac6454SAndrew Thompson 	/* the last TD terminates the transfer: */
2012e55e1ebcSAndrew Thompson 	td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
2013e55e1ebcSAndrew Thompson 	td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
201402ac6454SAndrew Thompson 
2015a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(td->page_cache);
201602ac6454SAndrew Thompson 
201702ac6454SAndrew Thompson 	/* must have at least one frame! */
201802ac6454SAndrew Thompson 
201902ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
202002ac6454SAndrew Thompson 
2021b850ecc1SAndrew Thompson #ifdef USB_DEBUG
202202ac6454SAndrew Thompson 	if (ehcidebug > 8) {
202302ac6454SAndrew Thompson 		DPRINTF("nexttog=%d; data before transfer:\n",
2024ae60fdfbSAndrew Thompson 		    xfer->endpoint->toggle_next);
202502ac6454SAndrew Thompson 		ehci_dump_sqtds(temp.sc,
202602ac6454SAndrew Thompson 		    xfer->td_transfer_first);
202702ac6454SAndrew Thompson 	}
202802ac6454SAndrew Thompson #endif
202902ac6454SAndrew Thompson 
2030ae60fdfbSAndrew Thompson 	methods = xfer->endpoint->methods;
203102ac6454SAndrew Thompson 
203202ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
203302ac6454SAndrew Thompson 
203402ac6454SAndrew Thompson 	/* the "qh_link" field is filled when the QH is added */
203502ac6454SAndrew Thompson 
203602ac6454SAndrew Thompson 	qh_endp =
203702ac6454SAndrew Thompson 	    (EHCI_QH_SET_ADDR(xfer->address) |
2038ae60fdfbSAndrew Thompson 	    EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
203902ac6454SAndrew Thompson 	    EHCI_QH_SET_MPL(xfer->max_packet_size));
204002ac6454SAndrew Thompson 
2041a593f6b8SAndrew Thompson 	if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
20422094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
204302ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods)
204402ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(8);
204502ac6454SAndrew Thompson 	} else {
204602ac6454SAndrew Thompson 
2047a593f6b8SAndrew Thompson 		if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
20482094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
204902ac6454SAndrew Thompson 		} else {
20502094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
205102ac6454SAndrew Thompson 		}
205202ac6454SAndrew Thompson 
205302ac6454SAndrew Thompson 		if (methods == &ehci_device_ctrl_methods) {
205402ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_CTL;
205502ac6454SAndrew Thompson 		}
205602ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods) {
205702ac6454SAndrew Thompson 			/* Only try one time per microframe! */
205802ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(1);
205902ac6454SAndrew Thompson 		}
206002ac6454SAndrew Thompson 	}
206102ac6454SAndrew Thompson 
20622094ecdaSAndrew Thompson 	if (temp.auto_data_toggle == 0) {
20632094ecdaSAndrew Thompson 		/* software computes the data toggle */
20642094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_DTC;
20652094ecdaSAndrew Thompson 	}
20662094ecdaSAndrew Thompson 
2067e55e1ebcSAndrew Thompson 	qh->qh_endp = htohc32(temp.sc, qh_endp);
206802ac6454SAndrew Thompson 
206902ac6454SAndrew Thompson 	qh_endphub =
207002ac6454SAndrew Thompson 	    (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
2071f12c6c29SAndrew Thompson 	    EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) |
2072f12c6c29SAndrew Thompson 	    EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) |
207302ac6454SAndrew Thompson 	    EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
207402ac6454SAndrew Thompson 	    EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
207502ac6454SAndrew Thompson 
2076e55e1ebcSAndrew Thompson 	qh->qh_endphub = htohc32(temp.sc, qh_endphub);
20772094ecdaSAndrew Thompson 	qh->qh_curqtd = 0;
207802ac6454SAndrew Thompson 
207902ac6454SAndrew Thompson 	/* fill the overlay qTD */
208002ac6454SAndrew Thompson 
20812094ecdaSAndrew Thompson 	if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
208202ac6454SAndrew Thompson 		/* DATA1 is next */
20832094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
20842094ecdaSAndrew Thompson 	} else {
20852094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = 0;
208602ac6454SAndrew Thompson 	}
20872094ecdaSAndrew Thompson 
208802ac6454SAndrew Thompson 	td = xfer->td_transfer_first;
208902ac6454SAndrew Thompson 
209002ac6454SAndrew Thompson 	qh->qh_qtd.qtd_next = td->qtd_self;
209102ac6454SAndrew Thompson 	qh->qh_qtd.qtd_altnext =
2092e55e1ebcSAndrew Thompson 	    htohc32(temp.sc, EHCI_LINK_TERMINATE);
209302ac6454SAndrew Thompson 
2094a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(qh->page_cache);
209502ac6454SAndrew Thompson 
2096ec8f3127SAndrew Thompson 	if (xfer->xroot->udev->flags.self_suspended == 0) {
209702ac6454SAndrew Thompson 		EHCI_APPEND_QH(qh, *qh_last);
209802ac6454SAndrew Thompson 	}
209902ac6454SAndrew Thompson }
210002ac6454SAndrew Thompson 
210102ac6454SAndrew Thompson static void
210239307315SAndrew Thompson ehci_root_intr(ehci_softc_t *sc)
210302ac6454SAndrew Thompson {
210402ac6454SAndrew Thompson 	uint16_t i;
210502ac6454SAndrew Thompson 	uint16_t m;
210602ac6454SAndrew Thompson 
210702ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
210802ac6454SAndrew Thompson 
210902ac6454SAndrew Thompson 	/* clear any old interrupt data */
211039307315SAndrew Thompson 	memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
211102ac6454SAndrew Thompson 
211202ac6454SAndrew Thompson 	/* set bits */
211302ac6454SAndrew Thompson 	m = (sc->sc_noport + 1);
211402ac6454SAndrew Thompson 	if (m > (8 * sizeof(sc->sc_hub_idata))) {
211502ac6454SAndrew Thompson 		m = (8 * sizeof(sc->sc_hub_idata));
211602ac6454SAndrew Thompson 	}
211702ac6454SAndrew Thompson 	for (i = 1; i < m; i++) {
211802ac6454SAndrew Thompson 		/* pick out CHANGE bits from the status register */
211902ac6454SAndrew Thompson 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
212002ac6454SAndrew Thompson 			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
212102ac6454SAndrew Thompson 			DPRINTF("port %d changed\n", i);
212202ac6454SAndrew Thompson 		}
212302ac6454SAndrew Thompson 	}
212439307315SAndrew Thompson 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
212539307315SAndrew Thompson 	    sizeof(sc->sc_hub_idata));
212602ac6454SAndrew Thompson }
212702ac6454SAndrew Thompson 
212802ac6454SAndrew Thompson static void
2129760bc48eSAndrew Thompson ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
213002ac6454SAndrew Thompson {
213102ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
213202ac6454SAndrew Thompson 	uint32_t status;
213302ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
213402ac6454SAndrew Thompson 	uint16_t len = 0;
213502ac6454SAndrew Thompson 	ehci_sitd_t *td = xfer->td_transfer_first;
213602ac6454SAndrew Thompson 	ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
213702ac6454SAndrew Thompson 
2138ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2139ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
214002ac6454SAndrew Thompson 
214102ac6454SAndrew Thompson 	while (nframes--) {
214202ac6454SAndrew Thompson 		if (td == NULL) {
214302ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
214402ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
214502ac6454SAndrew Thompson 		}
214602ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
214702ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
214802ac6454SAndrew Thompson 		}
2149b850ecc1SAndrew Thompson #ifdef USB_DEBUG
215002ac6454SAndrew Thompson 		if (ehcidebug > 15) {
215102ac6454SAndrew Thompson 			DPRINTF("isoc FS-TD\n");
215202ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
215302ac6454SAndrew Thompson 		}
215402ac6454SAndrew Thompson #endif
2155a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2156e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
215702ac6454SAndrew Thompson 
215802ac6454SAndrew Thompson 		len = EHCI_SITD_GET_LEN(status);
215902ac6454SAndrew Thompson 
2160c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2161c773c254SAndrew Thompson 
216202ac6454SAndrew Thompson 		if (*plen >= len) {
216302ac6454SAndrew Thompson 			len = *plen - len;
216402ac6454SAndrew Thompson 		} else {
216502ac6454SAndrew Thompson 			len = 0;
216602ac6454SAndrew Thompson 		}
216702ac6454SAndrew Thompson 
216802ac6454SAndrew Thompson 		*plen = len;
216902ac6454SAndrew Thompson 
217002ac6454SAndrew Thompson 		/* remove FS-TD from schedule */
217102ac6454SAndrew Thompson 		EHCI_REMOVE_FS_TD(td, *pp_last);
217202ac6454SAndrew Thompson 
217302ac6454SAndrew Thompson 		pp_last++;
217402ac6454SAndrew Thompson 		plen++;
217502ac6454SAndrew Thompson 		td = td->obj_next;
217602ac6454SAndrew Thompson 	}
217702ac6454SAndrew Thompson 
217802ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
217902ac6454SAndrew Thompson }
218002ac6454SAndrew Thompson 
218102ac6454SAndrew Thompson static void
2182760bc48eSAndrew Thompson ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
218302ac6454SAndrew Thompson {
218402ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
218502ac6454SAndrew Thompson 	uint32_t status;
218602ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
218702ac6454SAndrew Thompson 	uint16_t len = 0;
218802ac6454SAndrew Thompson 	uint8_t td_no = 0;
218902ac6454SAndrew Thompson 	ehci_itd_t *td = xfer->td_transfer_first;
219002ac6454SAndrew Thompson 	ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
219102ac6454SAndrew Thompson 
2192ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2193ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
219402ac6454SAndrew Thompson 
2195883bb300SAndrew Thompson 	while (nframes) {
219602ac6454SAndrew Thompson 		if (td == NULL) {
219702ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
219802ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
219902ac6454SAndrew Thompson 		}
220002ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
220102ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
220202ac6454SAndrew Thompson 		}
2203b850ecc1SAndrew Thompson #ifdef USB_DEBUG
220402ac6454SAndrew Thompson 		if (ehcidebug > 15) {
220502ac6454SAndrew Thompson 			DPRINTF("isoc HS-TD\n");
220602ac6454SAndrew Thompson 			ehci_dump_itd(sc, td);
220702ac6454SAndrew Thompson 		}
220802ac6454SAndrew Thompson #endif
220902ac6454SAndrew Thompson 
2210a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2211e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->itd_status[td_no]);
221202ac6454SAndrew Thompson 
221302ac6454SAndrew Thompson 		len = EHCI_ITD_GET_LEN(status);
221402ac6454SAndrew Thompson 
2215c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2216c773c254SAndrew Thompson 
2217f12c6c29SAndrew Thompson 		if (xfer->endpoint->usb_smask & (1 << td_no)) {
2218883bb300SAndrew Thompson 
221902ac6454SAndrew Thompson 			if (*plen >= len) {
222002ac6454SAndrew Thompson 				/*
2221883bb300SAndrew Thompson 				 * The length is valid. NOTE: The
2222883bb300SAndrew Thompson 				 * complete length is written back
2223883bb300SAndrew Thompson 				 * into the status field, and not the
2224883bb300SAndrew Thompson 				 * remainder like with other transfer
2225883bb300SAndrew Thompson 				 * descriptor types.
222602ac6454SAndrew Thompson 				 */
222702ac6454SAndrew Thompson 			} else {
222802ac6454SAndrew Thompson 				/* Invalid length - truncate */
222902ac6454SAndrew Thompson 				len = 0;
223002ac6454SAndrew Thompson 			}
223102ac6454SAndrew Thompson 
223202ac6454SAndrew Thompson 			*plen = len;
223302ac6454SAndrew Thompson 			plen++;
2234883bb300SAndrew Thompson 			nframes--;
2235883bb300SAndrew Thompson 		}
2236883bb300SAndrew Thompson 
223702ac6454SAndrew Thompson 		td_no++;
223802ac6454SAndrew Thompson 
223902ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
224002ac6454SAndrew Thompson 			/* remove HS-TD from schedule */
224102ac6454SAndrew Thompson 			EHCI_REMOVE_HS_TD(td, *pp_last);
224202ac6454SAndrew Thompson 			pp_last++;
224302ac6454SAndrew Thompson 
224402ac6454SAndrew Thompson 			td_no = 0;
224502ac6454SAndrew Thompson 			td = td->obj_next;
224602ac6454SAndrew Thompson 		}
224702ac6454SAndrew Thompson 	}
224802ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
224902ac6454SAndrew Thompson }
225002ac6454SAndrew Thompson 
225102ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row,
225202ac6454SAndrew Thompson  * from close and from interrupt
225302ac6454SAndrew Thompson  */
225402ac6454SAndrew Thompson static void
2255e0a69b51SAndrew Thompson ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
225602ac6454SAndrew Thompson {
2257ae60fdfbSAndrew Thompson 	struct usb_pipe_methods *methods = xfer->endpoint->methods;
225802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
225902ac6454SAndrew Thompson 
226002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
226102ac6454SAndrew Thompson 
2262ae60fdfbSAndrew Thompson 	DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2263ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint, error);
226402ac6454SAndrew Thompson 
226502ac6454SAndrew Thompson 	if ((methods == &ehci_device_bulk_methods) ||
226602ac6454SAndrew Thompson 	    (methods == &ehci_device_ctrl_methods)) {
2267b850ecc1SAndrew Thompson #ifdef USB_DEBUG
226802ac6454SAndrew Thompson 		if (ehcidebug > 8) {
226902ac6454SAndrew Thompson 			DPRINTF("nexttog=%d; data after transfer:\n",
2270ae60fdfbSAndrew Thompson 			    xfer->endpoint->toggle_next);
227102ac6454SAndrew Thompson 			ehci_dump_sqtds(sc,
227202ac6454SAndrew Thompson 			    xfer->td_transfer_first);
227302ac6454SAndrew Thompson 		}
227402ac6454SAndrew Thompson #endif
227502ac6454SAndrew Thompson 
227602ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
227702ac6454SAndrew Thompson 		    sc->sc_async_p_last);
227802ac6454SAndrew Thompson 	}
227902ac6454SAndrew Thompson 	if (methods == &ehci_device_intr_methods) {
228002ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
228102ac6454SAndrew Thompson 		    sc->sc_intr_p_last[xfer->qh_pos]);
228202ac6454SAndrew Thompson 	}
228302ac6454SAndrew Thompson 	/*
228402ac6454SAndrew Thompson 	 * Only finish isochronous transfers once which will update
228502ac6454SAndrew Thompson 	 * "xfer->frlengths".
228602ac6454SAndrew Thompson 	 */
228702ac6454SAndrew Thompson 	if (xfer->td_transfer_first &&
228802ac6454SAndrew Thompson 	    xfer->td_transfer_last) {
228902ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_fs_methods) {
229002ac6454SAndrew Thompson 			ehci_isoc_fs_done(sc, xfer);
229102ac6454SAndrew Thompson 		}
229202ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_hs_methods) {
229302ac6454SAndrew Thompson 			ehci_isoc_hs_done(sc, xfer);
229402ac6454SAndrew Thompson 		}
229502ac6454SAndrew Thompson 		xfer->td_transfer_first = NULL;
229602ac6454SAndrew Thompson 		xfer->td_transfer_last = NULL;
229702ac6454SAndrew Thompson 	}
229802ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
2299a593f6b8SAndrew Thompson 	usbd_transfer_done(xfer, error);
230002ac6454SAndrew Thompson }
230102ac6454SAndrew Thompson 
230202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
230302ac6454SAndrew Thompson  * ehci bulk support
230402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
230502ac6454SAndrew Thompson static void
2306760bc48eSAndrew Thompson ehci_device_bulk_open(struct usb_xfer *xfer)
230702ac6454SAndrew Thompson {
230802ac6454SAndrew Thompson 	return;
230902ac6454SAndrew Thompson }
231002ac6454SAndrew Thompson 
231102ac6454SAndrew Thompson static void
2312760bc48eSAndrew Thompson ehci_device_bulk_close(struct usb_xfer *xfer)
231302ac6454SAndrew Thompson {
231402ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
231502ac6454SAndrew Thompson }
231602ac6454SAndrew Thompson 
231702ac6454SAndrew Thompson static void
2318760bc48eSAndrew Thompson ehci_device_bulk_enter(struct usb_xfer *xfer)
231902ac6454SAndrew Thompson {
232002ac6454SAndrew Thompson 	return;
232102ac6454SAndrew Thompson }
232202ac6454SAndrew Thompson 
232302ac6454SAndrew Thompson static void
2324760bc48eSAndrew Thompson ehci_device_bulk_start(struct usb_xfer *xfer)
232502ac6454SAndrew Thompson {
232602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2327d1864afbSAndrew Thompson 	uint32_t temp;
232802ac6454SAndrew Thompson 
232902ac6454SAndrew Thompson 	/* setup TD's and QH */
233002ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
233102ac6454SAndrew Thompson 
233202ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
233302ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
2334d1864afbSAndrew Thompson 
2335cf223a88SAndrew Thompson 	/*
2336cf223a88SAndrew Thompson 	 * XXX Certain nVidia chipsets choke when using the IAAD
2337cf223a88SAndrew Thompson 	 * feature too frequently.
2338cf223a88SAndrew Thompson 	 */
2339cf223a88SAndrew Thompson 	if (sc->sc_flags & EHCI_SCFLG_IAADBUG)
2340cf223a88SAndrew Thompson 		return;
2341cf223a88SAndrew Thompson 
2342d1864afbSAndrew Thompson 	/* XXX Performance quirk: Some Host Controllers have a too low
2343d1864afbSAndrew Thompson 	 * interrupt rate. Issue an IAAD to stimulate the Host
2344d1864afbSAndrew Thompson 	 * Controller after queueing the BULK transfer.
2345d1864afbSAndrew Thompson 	 */
2346d1864afbSAndrew Thompson 	temp = EOREAD4(sc, EHCI_USBCMD);
2347d1864afbSAndrew Thompson 	if (!(temp & EHCI_CMD_IAAD))
2348d1864afbSAndrew Thompson 		EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
234902ac6454SAndrew Thompson }
235002ac6454SAndrew Thompson 
2351760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_bulk_methods =
235202ac6454SAndrew Thompson {
235302ac6454SAndrew Thompson 	.open = ehci_device_bulk_open,
235402ac6454SAndrew Thompson 	.close = ehci_device_bulk_close,
235502ac6454SAndrew Thompson 	.enter = ehci_device_bulk_enter,
235602ac6454SAndrew Thompson 	.start = ehci_device_bulk_start,
235702ac6454SAndrew Thompson };
235802ac6454SAndrew Thompson 
235902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
236002ac6454SAndrew Thompson  * ehci control support
236102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
236202ac6454SAndrew Thompson static void
2363760bc48eSAndrew Thompson ehci_device_ctrl_open(struct usb_xfer *xfer)
236402ac6454SAndrew Thompson {
236502ac6454SAndrew Thompson 	return;
236602ac6454SAndrew Thompson }
236702ac6454SAndrew Thompson 
236802ac6454SAndrew Thompson static void
2369760bc48eSAndrew Thompson ehci_device_ctrl_close(struct usb_xfer *xfer)
237002ac6454SAndrew Thompson {
237102ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
237202ac6454SAndrew Thompson }
237302ac6454SAndrew Thompson 
237402ac6454SAndrew Thompson static void
2375760bc48eSAndrew Thompson ehci_device_ctrl_enter(struct usb_xfer *xfer)
237602ac6454SAndrew Thompson {
237702ac6454SAndrew Thompson 	return;
237802ac6454SAndrew Thompson }
237902ac6454SAndrew Thompson 
238002ac6454SAndrew Thompson static void
2381760bc48eSAndrew Thompson ehci_device_ctrl_start(struct usb_xfer *xfer)
238202ac6454SAndrew Thompson {
238302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
238402ac6454SAndrew Thompson 
238502ac6454SAndrew Thompson 	/* setup TD's and QH */
238602ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
238702ac6454SAndrew Thompson 
238802ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
238902ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
239002ac6454SAndrew Thompson }
239102ac6454SAndrew Thompson 
2392760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_ctrl_methods =
239302ac6454SAndrew Thompson {
239402ac6454SAndrew Thompson 	.open = ehci_device_ctrl_open,
239502ac6454SAndrew Thompson 	.close = ehci_device_ctrl_close,
239602ac6454SAndrew Thompson 	.enter = ehci_device_ctrl_enter,
239702ac6454SAndrew Thompson 	.start = ehci_device_ctrl_start,
239802ac6454SAndrew Thompson };
239902ac6454SAndrew Thompson 
240002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
240102ac6454SAndrew Thompson  * ehci interrupt support
240202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
240302ac6454SAndrew Thompson static void
2404760bc48eSAndrew Thompson ehci_device_intr_open(struct usb_xfer *xfer)
240502ac6454SAndrew Thompson {
240602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
240702ac6454SAndrew Thompson 	uint16_t best;
240802ac6454SAndrew Thompson 	uint16_t bit;
240902ac6454SAndrew Thompson 	uint16_t x;
241002ac6454SAndrew Thompson 
2411f12c6c29SAndrew Thompson 	usb_hs_bandwidth_alloc(xfer);
241202ac6454SAndrew Thompson 
241302ac6454SAndrew Thompson 	/*
241402ac6454SAndrew Thompson 	 * Find the best QH position corresponding to the given interval:
241502ac6454SAndrew Thompson 	 */
241602ac6454SAndrew Thompson 
241702ac6454SAndrew Thompson 	best = 0;
241802ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
241902ac6454SAndrew Thompson 	while (bit) {
242002ac6454SAndrew Thompson 		if (xfer->interval >= bit) {
242102ac6454SAndrew Thompson 			x = bit;
242202ac6454SAndrew Thompson 			best = bit;
242302ac6454SAndrew Thompson 			while (x & bit) {
242402ac6454SAndrew Thompson 				if (sc->sc_intr_stat[x] <
242502ac6454SAndrew Thompson 				    sc->sc_intr_stat[best]) {
242602ac6454SAndrew Thompson 					best = x;
242702ac6454SAndrew Thompson 				}
242802ac6454SAndrew Thompson 				x++;
242902ac6454SAndrew Thompson 			}
243002ac6454SAndrew Thompson 			break;
243102ac6454SAndrew Thompson 		}
243202ac6454SAndrew Thompson 		bit >>= 1;
243302ac6454SAndrew Thompson 	}
243402ac6454SAndrew Thompson 
243502ac6454SAndrew Thompson 	sc->sc_intr_stat[best]++;
243602ac6454SAndrew Thompson 	xfer->qh_pos = best;
243702ac6454SAndrew Thompson 
243802ac6454SAndrew Thompson 	DPRINTFN(3, "best=%d interval=%d\n",
243902ac6454SAndrew Thompson 	    best, xfer->interval);
244002ac6454SAndrew Thompson }
244102ac6454SAndrew Thompson 
244202ac6454SAndrew Thompson static void
2443760bc48eSAndrew Thompson ehci_device_intr_close(struct usb_xfer *xfer)
244402ac6454SAndrew Thompson {
244502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
244602ac6454SAndrew Thompson 
244702ac6454SAndrew Thompson 	sc->sc_intr_stat[xfer->qh_pos]--;
244802ac6454SAndrew Thompson 
244902ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
2450f12c6c29SAndrew Thompson 
2451f12c6c29SAndrew Thompson 	/* bandwidth must be freed after device done */
2452f12c6c29SAndrew Thompson 	usb_hs_bandwidth_free(xfer);
245302ac6454SAndrew Thompson }
245402ac6454SAndrew Thompson 
245502ac6454SAndrew Thompson static void
2456760bc48eSAndrew Thompson ehci_device_intr_enter(struct usb_xfer *xfer)
245702ac6454SAndrew Thompson {
245802ac6454SAndrew Thompson 	return;
245902ac6454SAndrew Thompson }
246002ac6454SAndrew Thompson 
246102ac6454SAndrew Thompson static void
2462760bc48eSAndrew Thompson ehci_device_intr_start(struct usb_xfer *xfer)
246302ac6454SAndrew Thompson {
246402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
246502ac6454SAndrew Thompson 
246602ac6454SAndrew Thompson 	/* setup TD's and QH */
246702ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
246802ac6454SAndrew Thompson 
246902ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
247002ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
247102ac6454SAndrew Thompson }
247202ac6454SAndrew Thompson 
2473760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_intr_methods =
247402ac6454SAndrew Thompson {
247502ac6454SAndrew Thompson 	.open = ehci_device_intr_open,
247602ac6454SAndrew Thompson 	.close = ehci_device_intr_close,
247702ac6454SAndrew Thompson 	.enter = ehci_device_intr_enter,
247802ac6454SAndrew Thompson 	.start = ehci_device_intr_start,
247902ac6454SAndrew Thompson };
248002ac6454SAndrew Thompson 
248102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
248202ac6454SAndrew Thompson  * ehci full speed isochronous support
248302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
248402ac6454SAndrew Thompson static void
2485760bc48eSAndrew Thompson ehci_device_isoc_fs_open(struct usb_xfer *xfer)
248602ac6454SAndrew Thompson {
248702ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
248802ac6454SAndrew Thompson 	ehci_sitd_t *td;
248902ac6454SAndrew Thompson 	uint32_t sitd_portaddr;
249002ac6454SAndrew Thompson 	uint8_t ds;
249102ac6454SAndrew Thompson 
249202ac6454SAndrew Thompson 	sitd_portaddr =
249302ac6454SAndrew Thompson 	    EHCI_SITD_SET_ADDR(xfer->address) |
2494ae60fdfbSAndrew Thompson 	    EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
249502ac6454SAndrew Thompson 	    EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
249602ac6454SAndrew Thompson 	    EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
249702ac6454SAndrew Thompson 
2498ae60fdfbSAndrew Thompson 	if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
249902ac6454SAndrew Thompson 		sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
250002ac6454SAndrew Thompson 	}
2501e55e1ebcSAndrew Thompson 	sitd_portaddr = htohc32(sc, sitd_portaddr);
250202ac6454SAndrew Thompson 
250302ac6454SAndrew Thompson 	/* initialize all TD's */
250402ac6454SAndrew Thompson 
250502ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
250602ac6454SAndrew Thompson 
250702ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
250802ac6454SAndrew Thompson 
250902ac6454SAndrew Thompson 			td->sitd_portaddr = sitd_portaddr;
251002ac6454SAndrew Thompson 
251102ac6454SAndrew Thompson 			/*
251202ac6454SAndrew Thompson 			 * TODO: make some kind of automatic
251302ac6454SAndrew Thompson 			 * SMASK/CMASK selection based on micro-frame
251402ac6454SAndrew Thompson 			 * usage
251502ac6454SAndrew Thompson 			 *
251602ac6454SAndrew Thompson 			 * micro-frame usage (8 microframes per 1ms)
251702ac6454SAndrew Thompson 			 */
2518e55e1ebcSAndrew Thompson 			td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
251902ac6454SAndrew Thompson 
2520a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
252102ac6454SAndrew Thompson 		}
252202ac6454SAndrew Thompson 	}
252302ac6454SAndrew Thompson }
252402ac6454SAndrew Thompson 
252502ac6454SAndrew Thompson static void
2526760bc48eSAndrew Thompson ehci_device_isoc_fs_close(struct usb_xfer *xfer)
252702ac6454SAndrew Thompson {
252802ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
252902ac6454SAndrew Thompson }
253002ac6454SAndrew Thompson 
253102ac6454SAndrew Thompson static void
2532760bc48eSAndrew Thompson ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
253302ac6454SAndrew Thompson {
2534760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
253502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2536760bc48eSAndrew Thompson 	struct usb_fs_isoc_schedule *fss_start;
2537760bc48eSAndrew Thompson 	struct usb_fs_isoc_schedule *fss_end;
2538760bc48eSAndrew Thompson 	struct usb_fs_isoc_schedule *fss;
253902ac6454SAndrew Thompson 	ehci_sitd_t *td;
254002ac6454SAndrew Thompson 	ehci_sitd_t *td_last = NULL;
254102ac6454SAndrew Thompson 	ehci_sitd_t **pp_last;
254202ac6454SAndrew Thompson 	uint32_t *plen;
254302ac6454SAndrew Thompson 	uint32_t buf_offset;
254402ac6454SAndrew Thompson 	uint32_t nframes;
254502ac6454SAndrew Thompson 	uint32_t temp;
254602ac6454SAndrew Thompson 	uint32_t sitd_mask;
254702ac6454SAndrew Thompson 	uint16_t tlen;
254802ac6454SAndrew Thompson 	uint8_t sa;
254902ac6454SAndrew Thompson 	uint8_t sb;
255002ac6454SAndrew Thompson 	uint8_t error;
255102ac6454SAndrew Thompson 
2552b850ecc1SAndrew Thompson #ifdef USB_DEBUG
255302ac6454SAndrew Thompson 	uint8_t once = 1;
255402ac6454SAndrew Thompson 
255502ac6454SAndrew Thompson #endif
255602ac6454SAndrew Thompson 
255702ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2558ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
255902ac6454SAndrew Thompson 
256002ac6454SAndrew Thompson 	/* get the current frame index */
256102ac6454SAndrew Thompson 
256202ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
256302ac6454SAndrew Thompson 
256402ac6454SAndrew Thompson 	/*
256502ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
256602ac6454SAndrew Thompson 	 * will be inserted
256702ac6454SAndrew Thompson 	 */
2568ae60fdfbSAndrew Thompson 	buf_offset = (nframes - xfer->endpoint->isoc_next) &
256902ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
257002ac6454SAndrew Thompson 
2571ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
257202ac6454SAndrew Thompson 	    (buf_offset < xfer->nframes)) {
257302ac6454SAndrew Thompson 		/*
257402ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
257502ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
257602ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
257702ac6454SAndrew Thompson 		 * overlap.
257802ac6454SAndrew Thompson 		 */
2579ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) &
258002ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2581ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
2582ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
258302ac6454SAndrew Thompson 	}
258402ac6454SAndrew Thompson 	/*
258502ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
258602ac6454SAndrew Thompson 	 * current frame position:
258702ac6454SAndrew Thompson 	 */
2588ae60fdfbSAndrew Thompson 	buf_offset = (xfer->endpoint->isoc_next - nframes) &
258902ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
259002ac6454SAndrew Thompson 
259102ac6454SAndrew Thompson 	/*
259202ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
259302ac6454SAndrew Thompson 	 */
259402ac6454SAndrew Thompson 	xfer->isoc_time_complete =
2595a593f6b8SAndrew Thompson 	    usbd_fs_isoc_schedule_isoc_time_expand
259602ac6454SAndrew Thompson 	    (xfer->xroot->udev, &fss_start, &fss_end, nframes) + buf_offset +
259702ac6454SAndrew Thompson 	    xfer->nframes;
259802ac6454SAndrew Thompson 
259902ac6454SAndrew Thompson 	/* get the real number of frames */
260002ac6454SAndrew Thompson 
260102ac6454SAndrew Thompson 	nframes = xfer->nframes;
260202ac6454SAndrew Thompson 
260302ac6454SAndrew Thompson 	buf_offset = 0;
260402ac6454SAndrew Thompson 
260502ac6454SAndrew Thompson 	plen = xfer->frlengths;
260602ac6454SAndrew Thompson 
260702ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
260802ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
260902ac6454SAndrew Thompson 
261002ac6454SAndrew Thompson 	/* get next DMA set */
261102ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
261202ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
261302ac6454SAndrew Thompson 
2614ae60fdfbSAndrew Thompson 	pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];
261502ac6454SAndrew Thompson 
261602ac6454SAndrew Thompson 	/* store starting position */
261702ac6454SAndrew Thompson 
2618ae60fdfbSAndrew Thompson 	xfer->qh_pos = xfer->endpoint->isoc_next;
261902ac6454SAndrew Thompson 
262002ac6454SAndrew Thompson 	fss = fss_start + (xfer->qh_pos % USB_ISOC_TIME_MAX);
262102ac6454SAndrew Thompson 
262202ac6454SAndrew Thompson 	while (nframes--) {
262302ac6454SAndrew Thompson 		if (td == NULL) {
262402ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
262502ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
262602ac6454SAndrew Thompson 		}
262702ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
262802ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
262902ac6454SAndrew Thompson 		}
263002ac6454SAndrew Thompson 		if (fss >= fss_end) {
263102ac6454SAndrew Thompson 			fss = fss_start;
263202ac6454SAndrew Thompson 		}
263302ac6454SAndrew Thompson 		/* reuse sitd_portaddr and sitd_back from last transfer */
263402ac6454SAndrew Thompson 
263502ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
2636b850ecc1SAndrew Thompson #ifdef USB_DEBUG
263702ac6454SAndrew Thompson 			if (once) {
263802ac6454SAndrew Thompson 				once = 0;
263902ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d "
264002ac6454SAndrew Thompson 				    "bytes (frame truncated)\n",
264102ac6454SAndrew Thompson 				    __FUNCTION__, *plen,
264202ac6454SAndrew Thompson 				    xfer->max_frame_size);
264302ac6454SAndrew Thompson 			}
264402ac6454SAndrew Thompson #endif
264502ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
264602ac6454SAndrew Thompson 		}
264702ac6454SAndrew Thompson 		/*
264802ac6454SAndrew Thompson 		 * We currently don't care if the ISOCHRONOUS schedule is
264902ac6454SAndrew Thompson 		 * full!
265002ac6454SAndrew Thompson 		 */
2651a593f6b8SAndrew Thompson 		error = usbd_fs_isoc_schedule_alloc(fss, &sa, *plen);
265202ac6454SAndrew Thompson 		if (error) {
265302ac6454SAndrew Thompson 			/*
265402ac6454SAndrew Thompson 			 * The FULL speed schedule is FULL! Set length
265502ac6454SAndrew Thompson 			 * to zero.
265602ac6454SAndrew Thompson 			 */
265702ac6454SAndrew Thompson 			*plen = 0;
265802ac6454SAndrew Thompson 		}
265902ac6454SAndrew Thompson 		if (*plen) {
266002ac6454SAndrew Thompson 			/*
2661a593f6b8SAndrew Thompson 			 * only call "usbd_get_page()" when we have a
266202ac6454SAndrew Thompson 			 * non-zero length
266302ac6454SAndrew Thompson 			 */
2664a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2665e55e1ebcSAndrew Thompson 			td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
266602ac6454SAndrew Thompson 			buf_offset += *plen;
266702ac6454SAndrew Thompson 			/*
266802ac6454SAndrew Thompson 			 * NOTE: We need to subtract one from the offset so
266902ac6454SAndrew Thompson 			 * that we are on a valid page!
267002ac6454SAndrew Thompson 			 */
2671a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset - 1,
267202ac6454SAndrew Thompson 			    &buf_res);
267302ac6454SAndrew Thompson 			temp = buf_res.physaddr & ~0xFFF;
267402ac6454SAndrew Thompson 		} else {
267502ac6454SAndrew Thompson 			td->sitd_bp[0] = 0;
267602ac6454SAndrew Thompson 			temp = 0;
267702ac6454SAndrew Thompson 		}
267802ac6454SAndrew Thompson 
2679ae60fdfbSAndrew Thompson 		if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
268002ac6454SAndrew Thompson 			tlen = *plen;
268102ac6454SAndrew Thompson 			if (tlen <= 188) {
268202ac6454SAndrew Thompson 				temp |= 1;	/* T-count = 1, TP = ALL */
268302ac6454SAndrew Thompson 				tlen = 1;
268402ac6454SAndrew Thompson 			} else {
268502ac6454SAndrew Thompson 				tlen += 187;
268602ac6454SAndrew Thompson 				tlen /= 188;
268702ac6454SAndrew Thompson 				temp |= tlen;	/* T-count = [1..6] */
268802ac6454SAndrew Thompson 				temp |= 8;	/* TP = Begin */
268902ac6454SAndrew Thompson 			}
269002ac6454SAndrew Thompson 
269102ac6454SAndrew Thompson 			tlen += sa;
269202ac6454SAndrew Thompson 
269302ac6454SAndrew Thompson 			if (tlen >= 8) {
269402ac6454SAndrew Thompson 				sb = 0;
269502ac6454SAndrew Thompson 			} else {
269602ac6454SAndrew Thompson 				sb = (1 << tlen);
269702ac6454SAndrew Thompson 			}
269802ac6454SAndrew Thompson 
269902ac6454SAndrew Thompson 			sa = (1 << sa);
270002ac6454SAndrew Thompson 			sa = (sb - sa) & 0x3F;
270102ac6454SAndrew Thompson 			sb = 0;
270202ac6454SAndrew Thompson 		} else {
270302ac6454SAndrew Thompson 			sb = (-(4 << sa)) & 0xFE;
270402ac6454SAndrew Thompson 			sa = (1 << sa) & 0x3F;
270502ac6454SAndrew Thompson 		}
270602ac6454SAndrew Thompson 
270702ac6454SAndrew Thompson 		sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
270802ac6454SAndrew Thompson 		    EHCI_SITD_SET_CMASK(sb));
270902ac6454SAndrew Thompson 
2710e55e1ebcSAndrew Thompson 		td->sitd_bp[1] = htohc32(sc, temp);
271102ac6454SAndrew Thompson 
2712e55e1ebcSAndrew Thompson 		td->sitd_mask = htohc32(sc, sitd_mask);
271302ac6454SAndrew Thompson 
271402ac6454SAndrew Thompson 		if (nframes == 0) {
2715e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
27165f128668SAndrew Thompson 			    EHCI_SITD_IOC |
271702ac6454SAndrew Thompson 			    EHCI_SITD_ACTIVE |
271802ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
271902ac6454SAndrew Thompson 		} else {
2720e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
27215f128668SAndrew Thompson 			    EHCI_SITD_ACTIVE |
272202ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
272302ac6454SAndrew Thompson 		}
2724a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
272502ac6454SAndrew Thompson 
2726b850ecc1SAndrew Thompson #ifdef USB_DEBUG
272702ac6454SAndrew Thompson 		if (ehcidebug > 15) {
272802ac6454SAndrew Thompson 			DPRINTF("FS-TD %d\n", nframes);
272902ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
273002ac6454SAndrew Thompson 		}
273102ac6454SAndrew Thompson #endif
273202ac6454SAndrew Thompson 		/* insert TD into schedule */
273302ac6454SAndrew Thompson 		EHCI_APPEND_FS_TD(td, *pp_last);
273402ac6454SAndrew Thompson 		pp_last++;
273502ac6454SAndrew Thompson 
273602ac6454SAndrew Thompson 		plen++;
273702ac6454SAndrew Thompson 		fss++;
273802ac6454SAndrew Thompson 		td_last = td;
273902ac6454SAndrew Thompson 		td = td->obj_next;
274002ac6454SAndrew Thompson 	}
274102ac6454SAndrew Thompson 
274202ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
274302ac6454SAndrew Thompson 
274402ac6454SAndrew Thompson 	/* update isoc_next */
2745ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
274602ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
274702ac6454SAndrew Thompson }
274802ac6454SAndrew Thompson 
274902ac6454SAndrew Thompson static void
2750760bc48eSAndrew Thompson ehci_device_isoc_fs_start(struct usb_xfer *xfer)
275102ac6454SAndrew Thompson {
275202ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
275302ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
275402ac6454SAndrew Thompson }
275502ac6454SAndrew Thompson 
2756760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_isoc_fs_methods =
275702ac6454SAndrew Thompson {
275802ac6454SAndrew Thompson 	.open = ehci_device_isoc_fs_open,
275902ac6454SAndrew Thompson 	.close = ehci_device_isoc_fs_close,
276002ac6454SAndrew Thompson 	.enter = ehci_device_isoc_fs_enter,
276102ac6454SAndrew Thompson 	.start = ehci_device_isoc_fs_start,
276202ac6454SAndrew Thompson };
276302ac6454SAndrew Thompson 
276402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
276502ac6454SAndrew Thompson  * ehci high speed isochronous support
276602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
276702ac6454SAndrew Thompson static void
2768760bc48eSAndrew Thompson ehci_device_isoc_hs_open(struct usb_xfer *xfer)
276902ac6454SAndrew Thompson {
277002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
277102ac6454SAndrew Thompson 	ehci_itd_t *td;
277202ac6454SAndrew Thompson 	uint32_t temp;
277302ac6454SAndrew Thompson 	uint8_t ds;
2774883bb300SAndrew Thompson 
2775f12c6c29SAndrew Thompson 	usb_hs_bandwidth_alloc(xfer);
277602ac6454SAndrew Thompson 
277702ac6454SAndrew Thompson 	/* initialize all TD's */
277802ac6454SAndrew Thompson 
277902ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
278002ac6454SAndrew Thompson 
278102ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
278202ac6454SAndrew Thompson 
278302ac6454SAndrew Thompson 			/* set TD inactive */
278402ac6454SAndrew Thompson 			td->itd_status[0] = 0;
278502ac6454SAndrew Thompson 			td->itd_status[1] = 0;
278602ac6454SAndrew Thompson 			td->itd_status[2] = 0;
278702ac6454SAndrew Thompson 			td->itd_status[3] = 0;
278802ac6454SAndrew Thompson 			td->itd_status[4] = 0;
278902ac6454SAndrew Thompson 			td->itd_status[5] = 0;
279002ac6454SAndrew Thompson 			td->itd_status[6] = 0;
279102ac6454SAndrew Thompson 			td->itd_status[7] = 0;
279202ac6454SAndrew Thompson 
279302ac6454SAndrew Thompson 			/* set endpoint and address */
2794e55e1ebcSAndrew Thompson 			td->itd_bp[0] = htohc32(sc,
27955f128668SAndrew Thompson 			    EHCI_ITD_SET_ADDR(xfer->address) |
2796ae60fdfbSAndrew Thompson 			    EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
279702ac6454SAndrew Thompson 
279802ac6454SAndrew Thompson 			temp =
279902ac6454SAndrew Thompson 			    EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
280002ac6454SAndrew Thompson 
280102ac6454SAndrew Thompson 			/* set direction */
2802ae60fdfbSAndrew Thompson 			if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
280302ac6454SAndrew Thompson 				temp |= EHCI_ITD_SET_DIR_IN;
280402ac6454SAndrew Thompson 			}
280502ac6454SAndrew Thompson 			/* set maximum packet size */
2806e55e1ebcSAndrew Thompson 			td->itd_bp[1] = htohc32(sc, temp);
280702ac6454SAndrew Thompson 
280802ac6454SAndrew Thompson 			/* set transfer multiplier */
2809e55e1ebcSAndrew Thompson 			td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
281002ac6454SAndrew Thompson 
2811a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
281202ac6454SAndrew Thompson 		}
281302ac6454SAndrew Thompson 	}
281402ac6454SAndrew Thompson }
281502ac6454SAndrew Thompson 
281602ac6454SAndrew Thompson static void
2817760bc48eSAndrew Thompson ehci_device_isoc_hs_close(struct usb_xfer *xfer)
281802ac6454SAndrew Thompson {
281902ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
2820f12c6c29SAndrew Thompson 
2821f12c6c29SAndrew Thompson 	/* bandwidth must be freed after device done */
2822f12c6c29SAndrew Thompson 	usb_hs_bandwidth_free(xfer);
282302ac6454SAndrew Thompson }
282402ac6454SAndrew Thompson 
282502ac6454SAndrew Thompson static void
2826760bc48eSAndrew Thompson ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
282702ac6454SAndrew Thompson {
2828760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
282902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
283002ac6454SAndrew Thompson 	ehci_itd_t *td;
283102ac6454SAndrew Thompson 	ehci_itd_t *td_last = NULL;
283202ac6454SAndrew Thompson 	ehci_itd_t **pp_last;
283302ac6454SAndrew Thompson 	bus_size_t page_addr;
283402ac6454SAndrew Thompson 	uint32_t *plen;
283502ac6454SAndrew Thompson 	uint32_t status;
283602ac6454SAndrew Thompson 	uint32_t buf_offset;
283702ac6454SAndrew Thompson 	uint32_t nframes;
283802ac6454SAndrew Thompson 	uint32_t itd_offset[8 + 1];
283902ac6454SAndrew Thompson 	uint8_t x;
284002ac6454SAndrew Thompson 	uint8_t td_no;
284102ac6454SAndrew Thompson 	uint8_t page_no;
2842d1f7c4baSAndrew Thompson 	uint8_t shift = usbd_xfer_get_fps_shift(xfer);
284302ac6454SAndrew Thompson 
2844b850ecc1SAndrew Thompson #ifdef USB_DEBUG
284502ac6454SAndrew Thompson 	uint8_t once = 1;
284602ac6454SAndrew Thompson 
284702ac6454SAndrew Thompson #endif
284802ac6454SAndrew Thompson 
2849d1f7c4baSAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d shift=%d\n",
2850d1f7c4baSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes, (int)shift);
285102ac6454SAndrew Thompson 
285202ac6454SAndrew Thompson 	/* get the current frame index */
285302ac6454SAndrew Thompson 
285402ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
285502ac6454SAndrew Thompson 
285602ac6454SAndrew Thompson 	/*
285702ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
285802ac6454SAndrew Thompson 	 * will be inserted
285902ac6454SAndrew Thompson 	 */
2860ae60fdfbSAndrew Thompson 	buf_offset = (nframes - xfer->endpoint->isoc_next) &
286102ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
286202ac6454SAndrew Thompson 
2863ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
2864d1f7c4baSAndrew Thompson 	    (buf_offset < (((xfer->nframes << shift) + 7) / 8))) {
286502ac6454SAndrew Thompson 		/*
286602ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
286702ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
286802ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
286902ac6454SAndrew Thompson 		 * overlap.
287002ac6454SAndrew Thompson 		 */
2871ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) &
287202ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2873ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
2874ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
287502ac6454SAndrew Thompson 	}
287602ac6454SAndrew Thompson 	/*
287702ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
287802ac6454SAndrew Thompson 	 * current frame position:
287902ac6454SAndrew Thompson 	 */
2880ae60fdfbSAndrew Thompson 	buf_offset = (xfer->endpoint->isoc_next - nframes) &
288102ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
288202ac6454SAndrew Thompson 
288302ac6454SAndrew Thompson 	/*
288402ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
288502ac6454SAndrew Thompson 	 */
288602ac6454SAndrew Thompson 	xfer->isoc_time_complete =
2887a593f6b8SAndrew Thompson 	    usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
2888d1f7c4baSAndrew Thompson 	    (((xfer->nframes << shift) + 7) / 8);
288902ac6454SAndrew Thompson 
289002ac6454SAndrew Thompson 	/* get the real number of frames */
289102ac6454SAndrew Thompson 
289202ac6454SAndrew Thompson 	nframes = xfer->nframes;
289302ac6454SAndrew Thompson 
289402ac6454SAndrew Thompson 	buf_offset = 0;
289502ac6454SAndrew Thompson 	td_no = 0;
289602ac6454SAndrew Thompson 
289702ac6454SAndrew Thompson 	plen = xfer->frlengths;
289802ac6454SAndrew Thompson 
289902ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
290002ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
290102ac6454SAndrew Thompson 
290202ac6454SAndrew Thompson 	/* get next DMA set */
290302ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
290402ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
290502ac6454SAndrew Thompson 
2906ae60fdfbSAndrew Thompson 	pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];
290702ac6454SAndrew Thompson 
290802ac6454SAndrew Thompson 	/* store starting position */
290902ac6454SAndrew Thompson 
2910ae60fdfbSAndrew Thompson 	xfer->qh_pos = xfer->endpoint->isoc_next;
291102ac6454SAndrew Thompson 
2912883bb300SAndrew Thompson 	while (nframes) {
291302ac6454SAndrew Thompson 		if (td == NULL) {
291402ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
291502ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
291602ac6454SAndrew Thompson 		}
291702ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
291802ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
291902ac6454SAndrew Thompson 		}
292002ac6454SAndrew Thompson 		/* range check */
292102ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
2922b850ecc1SAndrew Thompson #ifdef USB_DEBUG
292302ac6454SAndrew Thompson 			if (once) {
292402ac6454SAndrew Thompson 				once = 0;
292502ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d bytes "
292602ac6454SAndrew Thompson 				    "(frame truncated)\n",
292702ac6454SAndrew Thompson 				    __FUNCTION__, *plen, xfer->max_frame_size);
292802ac6454SAndrew Thompson 			}
292902ac6454SAndrew Thompson #endif
293002ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
293102ac6454SAndrew Thompson 		}
2932883bb300SAndrew Thompson 
2933f12c6c29SAndrew Thompson 		if (xfer->endpoint->usb_smask & (1 << td_no)) {
293402ac6454SAndrew Thompson 			status = (EHCI_ITD_SET_LEN(*plen) |
293502ac6454SAndrew Thompson 			    EHCI_ITD_ACTIVE |
293602ac6454SAndrew Thompson 			    EHCI_ITD_SET_PG(0));
2937e55e1ebcSAndrew Thompson 			td->itd_status[td_no] = htohc32(sc, status);
293802ac6454SAndrew Thompson 			itd_offset[td_no] = buf_offset;
293902ac6454SAndrew Thompson 			buf_offset += *plen;
294002ac6454SAndrew Thompson 			plen++;
2941883bb300SAndrew Thompson 			nframes --;
2942883bb300SAndrew Thompson 		} else {
2943883bb300SAndrew Thompson 			td->itd_status[td_no] = 0;	/* not active */
2944883bb300SAndrew Thompson 			itd_offset[td_no] = buf_offset;
2945883bb300SAndrew Thompson 		}
2946883bb300SAndrew Thompson 
294702ac6454SAndrew Thompson 		td_no++;
294802ac6454SAndrew Thompson 
294902ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
295002ac6454SAndrew Thompson 
295102ac6454SAndrew Thompson 			/* the rest of the transfers are not active, if any */
295202ac6454SAndrew Thompson 			for (x = td_no; x != 8; x++) {
295302ac6454SAndrew Thompson 				td->itd_status[x] = 0;	/* not active */
295402ac6454SAndrew Thompson 			}
295502ac6454SAndrew Thompson 
295602ac6454SAndrew Thompson 			/* check if there is any data to be transferred */
295702ac6454SAndrew Thompson 			if (itd_offset[0] != buf_offset) {
295802ac6454SAndrew Thompson 				page_no = 0;
295902ac6454SAndrew Thompson 				itd_offset[td_no] = buf_offset;
296002ac6454SAndrew Thompson 
296102ac6454SAndrew Thompson 				/* get first page offset */
2962a593f6b8SAndrew Thompson 				usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
296302ac6454SAndrew Thompson 				/* get page address */
296402ac6454SAndrew Thompson 				page_addr = buf_res.physaddr & ~0xFFF;
296502ac6454SAndrew Thompson 				/* update page address */
2966e55e1ebcSAndrew Thompson 				td->itd_bp[0] &= htohc32(sc, 0xFFF);
2967e55e1ebcSAndrew Thompson 				td->itd_bp[0] |= htohc32(sc, page_addr);
296802ac6454SAndrew Thompson 
296902ac6454SAndrew Thompson 				for (x = 0; x != td_no; x++) {
297002ac6454SAndrew Thompson 					/* set page number and page offset */
297102ac6454SAndrew Thompson 					status = (EHCI_ITD_SET_PG(page_no) |
297202ac6454SAndrew Thompson 					    (buf_res.physaddr & 0xFFF));
2973e55e1ebcSAndrew Thompson 					td->itd_status[x] |= htohc32(sc, status);
297402ac6454SAndrew Thompson 
297502ac6454SAndrew Thompson 					/* get next page offset */
297602ac6454SAndrew Thompson 					if (itd_offset[x + 1] == buf_offset) {
297702ac6454SAndrew Thompson 						/*
297802ac6454SAndrew Thompson 						 * We subtract one so that
297902ac6454SAndrew Thompson 						 * we don't go off the last
298002ac6454SAndrew Thompson 						 * page!
298102ac6454SAndrew Thompson 						 */
2982a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
298302ac6454SAndrew Thompson 					} else {
2984a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
298502ac6454SAndrew Thompson 					}
298602ac6454SAndrew Thompson 
298702ac6454SAndrew Thompson 					/* check if we need a new page */
298802ac6454SAndrew Thompson 					if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
298902ac6454SAndrew Thompson 						/* new page needed */
299002ac6454SAndrew Thompson 						page_addr = buf_res.physaddr & ~0xFFF;
299102ac6454SAndrew Thompson 						if (page_no == 6) {
299202ac6454SAndrew Thompson 							panic("%s: too many pages\n", __FUNCTION__);
299302ac6454SAndrew Thompson 						}
299402ac6454SAndrew Thompson 						page_no++;
299502ac6454SAndrew Thompson 						/* update page address */
2996e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2997e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] |= htohc32(sc, page_addr);
299802ac6454SAndrew Thompson 					}
299902ac6454SAndrew Thompson 				}
300002ac6454SAndrew Thompson 			}
300102ac6454SAndrew Thompson 			/* set IOC bit if we are complete */
300202ac6454SAndrew Thompson 			if (nframes == 0) {
3003883bb300SAndrew Thompson 				td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC);
300402ac6454SAndrew Thompson 			}
3005a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
3006b850ecc1SAndrew Thompson #ifdef USB_DEBUG
300702ac6454SAndrew Thompson 			if (ehcidebug > 15) {
300802ac6454SAndrew Thompson 				DPRINTF("HS-TD %d\n", nframes);
300902ac6454SAndrew Thompson 				ehci_dump_itd(sc, td);
301002ac6454SAndrew Thompson 			}
301102ac6454SAndrew Thompson #endif
301202ac6454SAndrew Thompson 			/* insert TD into schedule */
301302ac6454SAndrew Thompson 			EHCI_APPEND_HS_TD(td, *pp_last);
301402ac6454SAndrew Thompson 			pp_last++;
301502ac6454SAndrew Thompson 
301602ac6454SAndrew Thompson 			td_no = 0;
301702ac6454SAndrew Thompson 			td_last = td;
301802ac6454SAndrew Thompson 			td = td->obj_next;
301902ac6454SAndrew Thompson 		}
302002ac6454SAndrew Thompson 	}
302102ac6454SAndrew Thompson 
302202ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
302302ac6454SAndrew Thompson 
302402ac6454SAndrew Thompson 	/* update isoc_next */
3025ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
302602ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
302702ac6454SAndrew Thompson }
302802ac6454SAndrew Thompson 
302902ac6454SAndrew Thompson static void
3030760bc48eSAndrew Thompson ehci_device_isoc_hs_start(struct usb_xfer *xfer)
303102ac6454SAndrew Thompson {
303202ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
303302ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
303402ac6454SAndrew Thompson }
303502ac6454SAndrew Thompson 
3036760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_isoc_hs_methods =
303702ac6454SAndrew Thompson {
303802ac6454SAndrew Thompson 	.open = ehci_device_isoc_hs_open,
303902ac6454SAndrew Thompson 	.close = ehci_device_isoc_hs_close,
304002ac6454SAndrew Thompson 	.enter = ehci_device_isoc_hs_enter,
304102ac6454SAndrew Thompson 	.start = ehci_device_isoc_hs_start,
304202ac6454SAndrew Thompson };
304302ac6454SAndrew Thompson 
304402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
304502ac6454SAndrew Thompson  * ehci root control support
304602ac6454SAndrew Thompson  *------------------------------------------------------------------------*
304739307315SAndrew Thompson  * Simulate a hardware hub by handling all the necessary requests.
304802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
304902ac6454SAndrew Thompson 
305002ac6454SAndrew Thompson static const
3051760bc48eSAndrew Thompson struct usb_device_descriptor ehci_devd =
305202ac6454SAndrew Thompson {
3053760bc48eSAndrew Thompson 	sizeof(struct usb_device_descriptor),
305402ac6454SAndrew Thompson 	UDESC_DEVICE,			/* type */
305502ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
305602ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
305702ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
305802ac6454SAndrew Thompson 	UDPROTO_HSHUBSTT,		/* protocol */
305902ac6454SAndrew Thompson 	64,				/* max packet */
306002ac6454SAndrew Thompson 	{0}, {0}, {0x00, 0x01},		/* device id */
306102ac6454SAndrew Thompson 	1, 2, 0,			/* string indicies */
306202ac6454SAndrew Thompson 	1				/* # of configurations */
306302ac6454SAndrew Thompson };
306402ac6454SAndrew Thompson 
306502ac6454SAndrew Thompson static const
3066760bc48eSAndrew Thompson struct usb_device_qualifier ehci_odevd =
306702ac6454SAndrew Thompson {
3068760bc48eSAndrew Thompson 	sizeof(struct usb_device_qualifier),
306902ac6454SAndrew Thompson 	UDESC_DEVICE_QUALIFIER,		/* type */
307002ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
307102ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
307202ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
307302ac6454SAndrew Thompson 	UDPROTO_FSHUB,			/* protocol */
307402ac6454SAndrew Thompson 	0,				/* max packet */
307502ac6454SAndrew Thompson 	0,				/* # of configurations */
307602ac6454SAndrew Thompson 	0
307702ac6454SAndrew Thompson };
307802ac6454SAndrew Thompson 
307902ac6454SAndrew Thompson static const struct ehci_config_desc ehci_confd = {
308002ac6454SAndrew Thompson 	.confd = {
3081760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_config_descriptor),
308202ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
308302ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(ehci_confd),
308402ac6454SAndrew Thompson 		.bNumInterface = 1,
308502ac6454SAndrew Thompson 		.bConfigurationValue = 1,
308602ac6454SAndrew Thompson 		.iConfiguration = 0,
308702ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
308802ac6454SAndrew Thompson 		.bMaxPower = 0		/* max power */
308902ac6454SAndrew Thompson 	},
309002ac6454SAndrew Thompson 	.ifcd = {
3091760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_interface_descriptor),
309202ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
309302ac6454SAndrew Thompson 		.bNumEndpoints = 1,
309402ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
309502ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
30961678e135SHans Petter Selasky 		.bInterfaceProtocol = 0,
309702ac6454SAndrew Thompson 	},
309802ac6454SAndrew Thompson 	.endpd = {
3099760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_endpoint_descriptor),
310002ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
310102ac6454SAndrew Thompson 		.bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
310202ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
310302ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
310402ac6454SAndrew Thompson 		.bInterval = 255,
310502ac6454SAndrew Thompson 	},
310602ac6454SAndrew Thompson };
310702ac6454SAndrew Thompson 
310802ac6454SAndrew Thompson static const
3109760bc48eSAndrew Thompson struct usb_hub_descriptor ehci_hubd =
311002ac6454SAndrew Thompson {
311102ac6454SAndrew Thompson 	0,				/* dynamic length */
311202ac6454SAndrew Thompson 	UDESC_HUB,
311302ac6454SAndrew Thompson 	0,
311402ac6454SAndrew Thompson 	{0, 0},
311502ac6454SAndrew Thompson 	0,
311602ac6454SAndrew Thompson 	0,
311702ac6454SAndrew Thompson 	{0},
311802ac6454SAndrew Thompson };
311902ac6454SAndrew Thompson 
312002ac6454SAndrew Thompson static void
312102ac6454SAndrew Thompson ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
312202ac6454SAndrew Thompson {
312302ac6454SAndrew Thompson 	uint32_t port;
312402ac6454SAndrew Thompson 	uint32_t v;
312502ac6454SAndrew Thompson 
312602ac6454SAndrew Thompson 	DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
312702ac6454SAndrew Thompson 
312802ac6454SAndrew Thompson 	port = EHCI_PORTSC(index);
312902ac6454SAndrew Thompson 	v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
313002ac6454SAndrew Thompson 	EOWRITE4(sc, port, v | EHCI_PS_PO);
313102ac6454SAndrew Thompson }
313202ac6454SAndrew Thompson 
3133e0a69b51SAndrew Thompson static usb_error_t
3134760bc48eSAndrew Thompson ehci_roothub_exec(struct usb_device *udev,
3135760bc48eSAndrew Thompson     struct usb_device_request *req, const void **pptr, uint16_t *plength)
313602ac6454SAndrew Thompson {
3137459d369eSAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3138459d369eSAndrew Thompson 	const char *str_ptr;
3139459d369eSAndrew Thompson 	const void *ptr;
314002ac6454SAndrew Thompson 	uint32_t port;
314102ac6454SAndrew Thompson 	uint32_t v;
3142459d369eSAndrew Thompson 	uint16_t len;
314302ac6454SAndrew Thompson 	uint16_t i;
314402ac6454SAndrew Thompson 	uint16_t value;
314502ac6454SAndrew Thompson 	uint16_t index;
3146e0a69b51SAndrew Thompson 	usb_error_t err;
314702ac6454SAndrew Thompson 
314802ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
314902ac6454SAndrew Thompson 
315002ac6454SAndrew Thompson 	/* buffer reset */
3151459d369eSAndrew Thompson 	ptr = (const void *)&sc->sc_hub_desc;
3152459d369eSAndrew Thompson 	len = 0;
3153459d369eSAndrew Thompson 	err = 0;
315402ac6454SAndrew Thompson 
3155459d369eSAndrew Thompson 	value = UGETW(req->wValue);
3156459d369eSAndrew Thompson 	index = UGETW(req->wIndex);
315702ac6454SAndrew Thompson 
315802ac6454SAndrew Thompson 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
315902ac6454SAndrew Thompson 	    "wValue=0x%04x wIndex=0x%04x\n",
3160459d369eSAndrew Thompson 	    req->bmRequestType, req->bRequest,
3161459d369eSAndrew Thompson 	    UGETW(req->wLength), value, index);
316202ac6454SAndrew Thompson 
316302ac6454SAndrew Thompson #define	C(x,y) ((x) | ((y) << 8))
3164459d369eSAndrew Thompson 	switch (C(req->bRequest, req->bmRequestType)) {
316502ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
316602ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
316702ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
316802ac6454SAndrew Thompson 		/*
316902ac6454SAndrew Thompson 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
317002ac6454SAndrew Thompson 		 * for the integrated root hub.
317102ac6454SAndrew Thompson 		 */
317202ac6454SAndrew Thompson 		break;
317302ac6454SAndrew Thompson 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
3174459d369eSAndrew Thompson 		len = 1;
317502ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
317602ac6454SAndrew Thompson 		break;
317702ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
317802ac6454SAndrew Thompson 		switch (value >> 8) {
317902ac6454SAndrew Thompson 		case UDESC_DEVICE:
318002ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3181459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
318202ac6454SAndrew Thompson 				goto done;
318302ac6454SAndrew Thompson 			}
3184459d369eSAndrew Thompson 			len = sizeof(ehci_devd);
3185459d369eSAndrew Thompson 			ptr = (const void *)&ehci_devd;
318602ac6454SAndrew Thompson 			break;
318702ac6454SAndrew Thompson 			/*
318802ac6454SAndrew Thompson 			 * We can't really operate at another speed,
318902ac6454SAndrew Thompson 			 * but the specification says we need this
319002ac6454SAndrew Thompson 			 * descriptor:
319102ac6454SAndrew Thompson 			 */
319202ac6454SAndrew Thompson 		case UDESC_DEVICE_QUALIFIER:
319302ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3194459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
319502ac6454SAndrew Thompson 				goto done;
319602ac6454SAndrew Thompson 			}
3197459d369eSAndrew Thompson 			len = sizeof(ehci_odevd);
3198459d369eSAndrew Thompson 			ptr = (const void *)&ehci_odevd;
319902ac6454SAndrew Thompson 			break;
320002ac6454SAndrew Thompson 
320102ac6454SAndrew Thompson 		case UDESC_CONFIG:
320202ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3203459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
320402ac6454SAndrew Thompson 				goto done;
320502ac6454SAndrew Thompson 			}
3206459d369eSAndrew Thompson 			len = sizeof(ehci_confd);
3207459d369eSAndrew Thompson 			ptr = (const void *)&ehci_confd;
320802ac6454SAndrew Thompson 			break;
320902ac6454SAndrew Thompson 
321002ac6454SAndrew Thompson 		case UDESC_STRING:
321102ac6454SAndrew Thompson 			switch (value & 0xff) {
321202ac6454SAndrew Thompson 			case 0:	/* Language table */
3213459d369eSAndrew Thompson 				str_ptr = "\001";
321402ac6454SAndrew Thompson 				break;
321502ac6454SAndrew Thompson 
321602ac6454SAndrew Thompson 			case 1:	/* Vendor */
3217459d369eSAndrew Thompson 				str_ptr = sc->sc_vendor;
321802ac6454SAndrew Thompson 				break;
321902ac6454SAndrew Thompson 
322002ac6454SAndrew Thompson 			case 2:	/* Product */
3221459d369eSAndrew Thompson 				str_ptr = "EHCI root HUB";
322202ac6454SAndrew Thompson 				break;
322302ac6454SAndrew Thompson 
322402ac6454SAndrew Thompson 			default:
3225459d369eSAndrew Thompson 				str_ptr = "";
322602ac6454SAndrew Thompson 				break;
322702ac6454SAndrew Thompson 			}
322802ac6454SAndrew Thompson 
3229a593f6b8SAndrew Thompson 			len = usb_make_str_desc(
3230459d369eSAndrew Thompson 			    sc->sc_hub_desc.temp,
323102ac6454SAndrew Thompson 			    sizeof(sc->sc_hub_desc.temp),
3232459d369eSAndrew Thompson 			    str_ptr);
323302ac6454SAndrew Thompson 			break;
323402ac6454SAndrew Thompson 		default:
3235459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
323602ac6454SAndrew Thompson 			goto done;
323702ac6454SAndrew Thompson 		}
323802ac6454SAndrew Thompson 		break;
323902ac6454SAndrew Thompson 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3240459d369eSAndrew Thompson 		len = 1;
324102ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = 0;
324202ac6454SAndrew Thompson 		break;
324302ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_DEVICE):
3244459d369eSAndrew Thompson 		len = 2;
324502ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
324602ac6454SAndrew Thompson 		break;
324702ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
324802ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3249459d369eSAndrew Thompson 		len = 2;
325002ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
325102ac6454SAndrew Thompson 		break;
325202ac6454SAndrew Thompson 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3253ab42e8b2SAndrew Thompson 		if (value >= EHCI_MAX_DEVICES) {
3254459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
325502ac6454SAndrew Thompson 			goto done;
325602ac6454SAndrew Thompson 		}
325702ac6454SAndrew Thompson 		sc->sc_addr = value;
325802ac6454SAndrew Thompson 		break;
325902ac6454SAndrew Thompson 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
326002ac6454SAndrew Thompson 		if ((value != 0) && (value != 1)) {
3261459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
326202ac6454SAndrew Thompson 			goto done;
326302ac6454SAndrew Thompson 		}
326402ac6454SAndrew Thompson 		sc->sc_conf = value;
326502ac6454SAndrew Thompson 		break;
326602ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
326702ac6454SAndrew Thompson 		break;
326802ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
326902ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
327002ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3271459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
327202ac6454SAndrew Thompson 		goto done;
327302ac6454SAndrew Thompson 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
327402ac6454SAndrew Thompson 		break;
327502ac6454SAndrew Thompson 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
327602ac6454SAndrew Thompson 		break;
327702ac6454SAndrew Thompson 		/* Hub requests */
327802ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
327902ac6454SAndrew Thompson 		break;
328002ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
328102ac6454SAndrew Thompson 		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
328202ac6454SAndrew Thompson 
328302ac6454SAndrew Thompson 		if ((index < 1) ||
328402ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3285459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
328602ac6454SAndrew Thompson 			goto done;
328702ac6454SAndrew Thompson 		}
328802ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
328902ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
329002ac6454SAndrew Thompson 		switch (value) {
329102ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
329202ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PE);
329302ac6454SAndrew Thompson 			break;
329402ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
329502ac6454SAndrew Thompson 			if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
329602ac6454SAndrew Thompson 
329702ac6454SAndrew Thompson 				/*
329802ac6454SAndrew Thompson 				 * waking up a High Speed device is rather
329902ac6454SAndrew Thompson 				 * complicated if
330002ac6454SAndrew Thompson 				 */
330102ac6454SAndrew Thompson 				EOWRITE4(sc, port, v | EHCI_PS_FPR);
330202ac6454SAndrew Thompson 			}
330302ac6454SAndrew Thompson 			/* wait 20ms for resume sequence to complete */
3304a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
330502ac6454SAndrew Thompson 
330602ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
330702ac6454SAndrew Thompson 			    EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
330802ac6454SAndrew Thompson 
33094a35cda7SAndrew Thompson 			/* 4ms settle time */
3310a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
331102ac6454SAndrew Thompson 			break;
331202ac6454SAndrew Thompson 		case UHF_PORT_POWER:
331302ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PP);
331402ac6454SAndrew Thompson 			break;
331502ac6454SAndrew Thompson 		case UHF_PORT_TEST:
331602ac6454SAndrew Thompson 			DPRINTFN(3, "clear port test "
331702ac6454SAndrew Thompson 			    "%d\n", index);
331802ac6454SAndrew Thompson 			break;
331902ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
332002ac6454SAndrew Thompson 			DPRINTFN(3, "clear port ind "
332102ac6454SAndrew Thompson 			    "%d\n", index);
332202ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
332302ac6454SAndrew Thompson 			break;
332402ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
332502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
332602ac6454SAndrew Thompson 			break;
332702ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
332802ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
332902ac6454SAndrew Thompson 			break;
333002ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
333102ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
333202ac6454SAndrew Thompson 			break;
333302ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
333402ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
333502ac6454SAndrew Thompson 			break;
333602ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
333702ac6454SAndrew Thompson 			sc->sc_isreset = 0;
333802ac6454SAndrew Thompson 			break;
333902ac6454SAndrew Thompson 		default:
3340459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
334102ac6454SAndrew Thompson 			goto done;
334202ac6454SAndrew Thompson 		}
334302ac6454SAndrew Thompson 		break;
334402ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
334502ac6454SAndrew Thompson 		if ((value & 0xff) != 0) {
3346459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
334702ac6454SAndrew Thompson 			goto done;
334802ac6454SAndrew Thompson 		}
3349b494261cSHans Petter Selasky 		v = EREAD4(sc, EHCI_HCSPARAMS);
335002ac6454SAndrew Thompson 
335102ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd = ehci_hubd;
335202ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
335353e0bf6eSHans Petter Selasky 
335453e0bf6eSHans Petter Selasky 		if (EHCI_HCS_PPC(v))
335553e0bf6eSHans Petter Selasky 			i = UHD_PWR_INDIVIDUAL;
335653e0bf6eSHans Petter Selasky 		else
335753e0bf6eSHans Petter Selasky 			i = UHD_PWR_NO_SWITCH;
335853e0bf6eSHans Petter Selasky 
335953e0bf6eSHans Petter Selasky 		if (EHCI_HCS_P_INDICATOR(v))
336053e0bf6eSHans Petter Selasky 			i |= UHD_PORT_IND;
336153e0bf6eSHans Petter Selasky 
336253e0bf6eSHans Petter Selasky 		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
336302ac6454SAndrew Thompson 		/* XXX can't find out? */
336402ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
336553e0bf6eSHans Petter Selasky 		/* XXX don't know if ports are removable or not */
336602ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bDescLength =
336702ac6454SAndrew Thompson 		    8 + ((sc->sc_noport + 7) / 8);
3368459d369eSAndrew Thompson 		len = sc->sc_hub_desc.hubd.bDescLength;
336902ac6454SAndrew Thompson 		break;
337002ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3371459d369eSAndrew Thompson 		len = 16;
337202ac6454SAndrew Thompson 		bzero(sc->sc_hub_desc.temp, 16);
337302ac6454SAndrew Thompson 		break;
337402ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
337502ac6454SAndrew Thompson 		DPRINTFN(9, "get port status i=%d\n",
337602ac6454SAndrew Thompson 		    index);
337702ac6454SAndrew Thompson 		if ((index < 1) ||
337802ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3379459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
338002ac6454SAndrew Thompson 			goto done;
338102ac6454SAndrew Thompson 		}
338202ac6454SAndrew Thompson 		v = EOREAD4(sc, EHCI_PORTSC(index));
338302ac6454SAndrew Thompson 		DPRINTFN(9, "port status=0x%04x\n", v);
3384e55e1ebcSAndrew Thompson 		if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) {
338502ac6454SAndrew Thompson 			if ((v & 0xc000000) == 0x8000000)
338602ac6454SAndrew Thompson 				i = UPS_HIGH_SPEED;
338702ac6454SAndrew Thompson 			else if ((v & 0xc000000) == 0x4000000)
338802ac6454SAndrew Thompson 				i = UPS_LOW_SPEED;
338902ac6454SAndrew Thompson 			else
339002ac6454SAndrew Thompson 				i = 0;
339102ac6454SAndrew Thompson 		} else {
339202ac6454SAndrew Thompson 			i = UPS_HIGH_SPEED;
339302ac6454SAndrew Thompson 		}
339402ac6454SAndrew Thompson 		if (v & EHCI_PS_CS)
339502ac6454SAndrew Thompson 			i |= UPS_CURRENT_CONNECT_STATUS;
339602ac6454SAndrew Thompson 		if (v & EHCI_PS_PE)
339702ac6454SAndrew Thompson 			i |= UPS_PORT_ENABLED;
339802ac6454SAndrew Thompson 		if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
339902ac6454SAndrew Thompson 			i |= UPS_SUSPEND;
340002ac6454SAndrew Thompson 		if (v & EHCI_PS_OCA)
340102ac6454SAndrew Thompson 			i |= UPS_OVERCURRENT_INDICATOR;
340202ac6454SAndrew Thompson 		if (v & EHCI_PS_PR)
340302ac6454SAndrew Thompson 			i |= UPS_RESET;
340402ac6454SAndrew Thompson 		if (v & EHCI_PS_PP)
340502ac6454SAndrew Thompson 			i |= UPS_PORT_POWER;
340602ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortStatus, i);
340702ac6454SAndrew Thompson 		i = 0;
340802ac6454SAndrew Thompson 		if (v & EHCI_PS_CSC)
340902ac6454SAndrew Thompson 			i |= UPS_C_CONNECT_STATUS;
341002ac6454SAndrew Thompson 		if (v & EHCI_PS_PEC)
341102ac6454SAndrew Thompson 			i |= UPS_C_PORT_ENABLED;
341202ac6454SAndrew Thompson 		if (v & EHCI_PS_OCC)
341302ac6454SAndrew Thompson 			i |= UPS_C_OVERCURRENT_INDICATOR;
341402ac6454SAndrew Thompson 		if (v & EHCI_PS_FPR)
341502ac6454SAndrew Thompson 			i |= UPS_C_SUSPEND;
341602ac6454SAndrew Thompson 		if (sc->sc_isreset)
341702ac6454SAndrew Thompson 			i |= UPS_C_PORT_RESET;
341802ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortChange, i);
3419459d369eSAndrew Thompson 		len = sizeof(sc->sc_hub_desc.ps);
342002ac6454SAndrew Thompson 		break;
342102ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3422459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
342302ac6454SAndrew Thompson 		goto done;
342402ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
342502ac6454SAndrew Thompson 		break;
342602ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
342702ac6454SAndrew Thompson 		if ((index < 1) ||
342802ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3429459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
343002ac6454SAndrew Thompson 			goto done;
343102ac6454SAndrew Thompson 		}
343202ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
343302ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
343402ac6454SAndrew Thompson 		switch (value) {
343502ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
343602ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PE);
343702ac6454SAndrew Thompson 			break;
343802ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
343902ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
344002ac6454SAndrew Thompson 			break;
344102ac6454SAndrew Thompson 		case UHF_PORT_RESET:
344202ac6454SAndrew Thompson 			DPRINTFN(6, "reset port %d\n", index);
3443b850ecc1SAndrew Thompson #ifdef USB_DEBUG
344402ac6454SAndrew Thompson 			if (ehcinohighspeed) {
344502ac6454SAndrew Thompson 				/*
344602ac6454SAndrew Thompson 				 * Connect USB device to companion
344702ac6454SAndrew Thompson 				 * controller.
344802ac6454SAndrew Thompson 				 */
344902ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
345002ac6454SAndrew Thompson 				break;
345102ac6454SAndrew Thompson 			}
345202ac6454SAndrew Thompson #endif
3453e55e1ebcSAndrew Thompson 			if (EHCI_PS_IS_LOWSPEED(v) &&
3454e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
345502ac6454SAndrew Thompson 				/* Low speed device, give up ownership. */
345602ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
345702ac6454SAndrew Thompson 				break;
345802ac6454SAndrew Thompson 			}
345902ac6454SAndrew Thompson 			/* Start reset sequence. */
346002ac6454SAndrew Thompson 			v &= ~(EHCI_PS_PE | EHCI_PS_PR);
346102ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PR);
346202ac6454SAndrew Thompson 
346302ac6454SAndrew Thompson 			/* Wait for reset to complete. */
3464a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
346502ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
346602ac6454SAndrew Thompson 
346702ac6454SAndrew Thompson 			/* Terminate reset sequence. */
346802ac6454SAndrew Thompson 			if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
346902ac6454SAndrew Thompson 				EOWRITE4(sc, port, v);
347002ac6454SAndrew Thompson 
347102ac6454SAndrew Thompson 			/* Wait for HC to complete reset. */
3472a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
347302ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
347402ac6454SAndrew Thompson 
347502ac6454SAndrew Thompson 			v = EOREAD4(sc, port);
347602ac6454SAndrew Thompson 			DPRINTF("ehci after reset, status=0x%08x\n", v);
347702ac6454SAndrew Thompson 			if (v & EHCI_PS_PR) {
347802ac6454SAndrew Thompson 				device_printf(sc->sc_bus.bdev,
347902ac6454SAndrew Thompson 				    "port reset timeout\n");
3480459d369eSAndrew Thompson 				err = USB_ERR_TIMEOUT;
348102ac6454SAndrew Thompson 				goto done;
348202ac6454SAndrew Thompson 			}
3483e55e1ebcSAndrew Thompson 			if (!(v & EHCI_PS_PE) &&
3484e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3485e55e1ebcSAndrew Thompson 				/* Not a high speed device, give up ownership.*/
348602ac6454SAndrew Thompson 				ehci_disown(sc, index, 0);
348702ac6454SAndrew Thompson 				break;
348802ac6454SAndrew Thompson 			}
348902ac6454SAndrew Thompson 			sc->sc_isreset = 1;
349002ac6454SAndrew Thompson 			DPRINTF("ehci port %d reset, status = 0x%08x\n",
349102ac6454SAndrew Thompson 			    index, v);
349202ac6454SAndrew Thompson 			break;
349302ac6454SAndrew Thompson 
349402ac6454SAndrew Thompson 		case UHF_PORT_POWER:
349502ac6454SAndrew Thompson 			DPRINTFN(3, "set port power %d\n", index);
349602ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PP);
349702ac6454SAndrew Thompson 			break;
349802ac6454SAndrew Thompson 
349902ac6454SAndrew Thompson 		case UHF_PORT_TEST:
350002ac6454SAndrew Thompson 			DPRINTFN(3, "set port test %d\n", index);
350102ac6454SAndrew Thompson 			break;
350202ac6454SAndrew Thompson 
350302ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
350402ac6454SAndrew Thompson 			DPRINTFN(3, "set port ind %d\n", index);
350502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
350602ac6454SAndrew Thompson 			break;
350702ac6454SAndrew Thompson 
350802ac6454SAndrew Thompson 		default:
3509459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
351002ac6454SAndrew Thompson 			goto done;
351102ac6454SAndrew Thompson 		}
351202ac6454SAndrew Thompson 		break;
351302ac6454SAndrew Thompson 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
351402ac6454SAndrew Thompson 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
351502ac6454SAndrew Thompson 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
351602ac6454SAndrew Thompson 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
351702ac6454SAndrew Thompson 		break;
351802ac6454SAndrew Thompson 	default:
3519459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
352002ac6454SAndrew Thompson 		goto done;
352102ac6454SAndrew Thompson 	}
352202ac6454SAndrew Thompson done:
3523459d369eSAndrew Thompson 	*plength = len;
3524459d369eSAndrew Thompson 	*pptr = ptr;
3525459d369eSAndrew Thompson 	return (err);
352602ac6454SAndrew Thompson }
352702ac6454SAndrew Thompson 
352802ac6454SAndrew Thompson static void
3529760bc48eSAndrew Thompson ehci_xfer_setup(struct usb_setup_params *parm)
353002ac6454SAndrew Thompson {
3531760bc48eSAndrew Thompson 	struct usb_page_search page_info;
3532760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
353302ac6454SAndrew Thompson 	ehci_softc_t *sc;
3534760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
353502ac6454SAndrew Thompson 	void *last_obj;
353602ac6454SAndrew Thompson 	uint32_t nqtd;
353702ac6454SAndrew Thompson 	uint32_t nqh;
353802ac6454SAndrew Thompson 	uint32_t nsitd;
353902ac6454SAndrew Thompson 	uint32_t nitd;
354002ac6454SAndrew Thompson 	uint32_t n;
354102ac6454SAndrew Thompson 
354202ac6454SAndrew Thompson 	sc = EHCI_BUS2SC(parm->udev->bus);
354302ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
354402ac6454SAndrew Thompson 
354502ac6454SAndrew Thompson 	nqtd = 0;
354602ac6454SAndrew Thompson 	nqh = 0;
354702ac6454SAndrew Thompson 	nsitd = 0;
354802ac6454SAndrew Thompson 	nitd = 0;
354902ac6454SAndrew Thompson 
355002ac6454SAndrew Thompson 	/*
355102ac6454SAndrew Thompson 	 * compute maximum number of some structures
355202ac6454SAndrew Thompson 	 */
355302ac6454SAndrew Thompson 	if (parm->methods == &ehci_device_ctrl_methods) {
355402ac6454SAndrew Thompson 
355502ac6454SAndrew Thompson 		/*
355602ac6454SAndrew Thompson 		 * The proof for the "nqtd" formula is illustrated like
355702ac6454SAndrew Thompson 		 * this:
355802ac6454SAndrew Thompson 		 *
355902ac6454SAndrew Thompson 		 * +------------------------------------+
356002ac6454SAndrew Thompson 		 * |                                    |
356102ac6454SAndrew Thompson 		 * |         |remainder ->              |
356202ac6454SAndrew Thompson 		 * |   +-----+---+                      |
356302ac6454SAndrew Thompson 		 * |   | xxx | x | frm 0                |
356402ac6454SAndrew Thompson 		 * |   +-----+---++                     |
356502ac6454SAndrew Thompson 		 * |   | xxx | xx | frm 1               |
356602ac6454SAndrew Thompson 		 * |   +-----+----+                     |
356702ac6454SAndrew Thompson 		 * |            ...                     |
356802ac6454SAndrew Thompson 		 * +------------------------------------+
356902ac6454SAndrew Thompson 		 *
357002ac6454SAndrew Thompson 		 * "xxx" means a completely full USB transfer descriptor
357102ac6454SAndrew Thompson 		 *
357202ac6454SAndrew Thompson 		 * "x" and "xx" means a short USB packet
357302ac6454SAndrew Thompson 		 *
357402ac6454SAndrew Thompson 		 * For the remainder of an USB transfer modulo
357502ac6454SAndrew Thompson 		 * "max_data_length" we need two USB transfer descriptors.
357602ac6454SAndrew Thompson 		 * One to transfer the remaining data and one to finalise
357702ac6454SAndrew Thompson 		 * with a zero length packet in case the "force_short_xfer"
357802ac6454SAndrew Thompson 		 * flag is set. We only need two USB transfer descriptors in
357902ac6454SAndrew Thompson 		 * the case where the transfer length of the first one is a
358002ac6454SAndrew Thompson 		 * factor of "max_frame_size". The rest of the needed USB
358102ac6454SAndrew Thompson 		 * transfer descriptors is given by the buffer size divided
358202ac6454SAndrew Thompson 		 * by the maximum data payload.
358302ac6454SAndrew Thompson 		 */
358402ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
358502ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
358602ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
358702ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
358802ac6454SAndrew Thompson 
3589a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
359002ac6454SAndrew Thompson 
359102ac6454SAndrew Thompson 		nqh = 1;
359202ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes) + 1	/* STATUS */
3593578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
359402ac6454SAndrew Thompson 
359502ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_bulk_methods) {
359602ac6454SAndrew Thompson 
359702ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
359802ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
359902ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
360002ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
360102ac6454SAndrew Thompson 
3602a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
360302ac6454SAndrew Thompson 
360402ac6454SAndrew Thompson 		nqh = 1;
360502ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3606578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
360702ac6454SAndrew Thompson 
360802ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_intr_methods) {
360902ac6454SAndrew Thompson 
361002ac6454SAndrew Thompson 		if (parm->speed == USB_SPEED_HIGH) {
361102ac6454SAndrew Thompson 			parm->hc_max_packet_size = 0x400;
361202ac6454SAndrew Thompson 			parm->hc_max_packet_count = 3;
361302ac6454SAndrew Thompson 		} else if (parm->speed == USB_SPEED_FULL) {
361402ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
361502ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
361602ac6454SAndrew Thompson 		} else {
361702ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
361802ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
361902ac6454SAndrew Thompson 		}
362002ac6454SAndrew Thompson 
362102ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
362202ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
362302ac6454SAndrew Thompson 
3624a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
362502ac6454SAndrew Thompson 
362602ac6454SAndrew Thompson 		nqh = 1;
362702ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3628578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
362902ac6454SAndrew Thompson 
363002ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_fs_methods) {
363102ac6454SAndrew Thompson 
363202ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x3FF;
363302ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
363402ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x3FF;
363502ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
363602ac6454SAndrew Thompson 
3637a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
363802ac6454SAndrew Thompson 
363902ac6454SAndrew Thompson 		nsitd = xfer->nframes;
364002ac6454SAndrew Thompson 
364102ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_hs_methods) {
364202ac6454SAndrew Thompson 
364302ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
364402ac6454SAndrew Thompson 		parm->hc_max_packet_count = 3;
364502ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0xC00;
364602ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
364702ac6454SAndrew Thompson 
3648a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
364902ac6454SAndrew Thompson 
3650883bb300SAndrew Thompson 		nitd = ((xfer->nframes + 7) / 8) <<
3651883bb300SAndrew Thompson 		    usbd_xfer_get_fps_shift(xfer);
365202ac6454SAndrew Thompson 
365302ac6454SAndrew Thompson 	} else {
365402ac6454SAndrew Thompson 
365502ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
365602ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
365702ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x400;
365802ac6454SAndrew Thompson 
3659a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
366002ac6454SAndrew Thompson 	}
366102ac6454SAndrew Thompson 
366202ac6454SAndrew Thompson alloc_dma_set:
366302ac6454SAndrew Thompson 
366402ac6454SAndrew Thompson 	if (parm->err) {
366502ac6454SAndrew Thompson 		return;
366602ac6454SAndrew Thompson 	}
366702ac6454SAndrew Thompson 	/*
366802ac6454SAndrew Thompson 	 * Allocate queue heads and transfer descriptors
366902ac6454SAndrew Thompson 	 */
367002ac6454SAndrew Thompson 	last_obj = NULL;
367102ac6454SAndrew Thompson 
3672a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
367302ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_itd_t),
367402ac6454SAndrew Thompson 	    EHCI_ITD_ALIGN, nitd)) {
367502ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
367602ac6454SAndrew Thompson 		return;
367702ac6454SAndrew Thompson 	}
367802ac6454SAndrew Thompson 	if (parm->buf) {
367902ac6454SAndrew Thompson 		for (n = 0; n != nitd; n++) {
368002ac6454SAndrew Thompson 			ehci_itd_t *td;
368102ac6454SAndrew Thompson 
3682a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
368302ac6454SAndrew Thompson 
368402ac6454SAndrew Thompson 			td = page_info.buffer;
368502ac6454SAndrew Thompson 
368602ac6454SAndrew Thompson 			/* init TD */
3687e55e1ebcSAndrew Thompson 			td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
368802ac6454SAndrew Thompson 			td->obj_next = last_obj;
368902ac6454SAndrew Thompson 			td->page_cache = pc + n;
369002ac6454SAndrew Thompson 
369102ac6454SAndrew Thompson 			last_obj = td;
369202ac6454SAndrew Thompson 
3693a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
369402ac6454SAndrew Thompson 		}
369502ac6454SAndrew Thompson 	}
3696a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
369702ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_sitd_t),
369802ac6454SAndrew Thompson 	    EHCI_SITD_ALIGN, nsitd)) {
369902ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
370002ac6454SAndrew Thompson 		return;
370102ac6454SAndrew Thompson 	}
370202ac6454SAndrew Thompson 	if (parm->buf) {
370302ac6454SAndrew Thompson 		for (n = 0; n != nsitd; n++) {
370402ac6454SAndrew Thompson 			ehci_sitd_t *td;
370502ac6454SAndrew Thompson 
3706a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
370702ac6454SAndrew Thompson 
370802ac6454SAndrew Thompson 			td = page_info.buffer;
370902ac6454SAndrew Thompson 
371002ac6454SAndrew Thompson 			/* init TD */
3711e55e1ebcSAndrew Thompson 			td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
371202ac6454SAndrew Thompson 			td->obj_next = last_obj;
371302ac6454SAndrew Thompson 			td->page_cache = pc + n;
371402ac6454SAndrew Thompson 
371502ac6454SAndrew Thompson 			last_obj = td;
371602ac6454SAndrew Thompson 
3717a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
371802ac6454SAndrew Thompson 		}
371902ac6454SAndrew Thompson 	}
3720a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
372102ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qtd_t),
372202ac6454SAndrew Thompson 	    EHCI_QTD_ALIGN, nqtd)) {
372302ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
372402ac6454SAndrew Thompson 		return;
372502ac6454SAndrew Thompson 	}
372602ac6454SAndrew Thompson 	if (parm->buf) {
372702ac6454SAndrew Thompson 		for (n = 0; n != nqtd; n++) {
372802ac6454SAndrew Thompson 			ehci_qtd_t *qtd;
372902ac6454SAndrew Thompson 
3730a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
373102ac6454SAndrew Thompson 
373202ac6454SAndrew Thompson 			qtd = page_info.buffer;
373302ac6454SAndrew Thompson 
373402ac6454SAndrew Thompson 			/* init TD */
3735e55e1ebcSAndrew Thompson 			qtd->qtd_self = htohc32(sc, page_info.physaddr);
373602ac6454SAndrew Thompson 			qtd->obj_next = last_obj;
373702ac6454SAndrew Thompson 			qtd->page_cache = pc + n;
373802ac6454SAndrew Thompson 
373902ac6454SAndrew Thompson 			last_obj = qtd;
374002ac6454SAndrew Thompson 
3741a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
374202ac6454SAndrew Thompson 		}
374302ac6454SAndrew Thompson 	}
374402ac6454SAndrew Thompson 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
374502ac6454SAndrew Thompson 
374602ac6454SAndrew Thompson 	last_obj = NULL;
374702ac6454SAndrew Thompson 
3748a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
374902ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qh_t),
375002ac6454SAndrew Thompson 	    EHCI_QH_ALIGN, nqh)) {
375102ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
375202ac6454SAndrew Thompson 		return;
375302ac6454SAndrew Thompson 	}
375402ac6454SAndrew Thompson 	if (parm->buf) {
375502ac6454SAndrew Thompson 		for (n = 0; n != nqh; n++) {
375602ac6454SAndrew Thompson 			ehci_qh_t *qh;
375702ac6454SAndrew Thompson 
3758a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
375902ac6454SAndrew Thompson 
376002ac6454SAndrew Thompson 			qh = page_info.buffer;
376102ac6454SAndrew Thompson 
376202ac6454SAndrew Thompson 			/* init QH */
3763e55e1ebcSAndrew Thompson 			qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
376402ac6454SAndrew Thompson 			qh->obj_next = last_obj;
376502ac6454SAndrew Thompson 			qh->page_cache = pc + n;
376602ac6454SAndrew Thompson 
376702ac6454SAndrew Thompson 			last_obj = qh;
376802ac6454SAndrew Thompson 
3769a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
377002ac6454SAndrew Thompson 		}
377102ac6454SAndrew Thompson 	}
377202ac6454SAndrew Thompson 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
377302ac6454SAndrew Thompson 
377402ac6454SAndrew Thompson 	if (!xfer->flags_int.curr_dma_set) {
377502ac6454SAndrew Thompson 		xfer->flags_int.curr_dma_set = 1;
377602ac6454SAndrew Thompson 		goto alloc_dma_set;
377702ac6454SAndrew Thompson 	}
377802ac6454SAndrew Thompson }
377902ac6454SAndrew Thompson 
378002ac6454SAndrew Thompson static void
3781760bc48eSAndrew Thompson ehci_xfer_unsetup(struct usb_xfer *xfer)
378202ac6454SAndrew Thompson {
378302ac6454SAndrew Thompson 	return;
378402ac6454SAndrew Thompson }
378502ac6454SAndrew Thompson 
378602ac6454SAndrew Thompson static void
3787ae60fdfbSAndrew Thompson ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3788ae60fdfbSAndrew Thompson     struct usb_endpoint *ep)
378902ac6454SAndrew Thompson {
379002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
379102ac6454SAndrew Thompson 
3792ae60fdfbSAndrew Thompson 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3793ae60fdfbSAndrew Thompson 	    ep, udev->address,
3794f29a0724SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb_mode,
379502ac6454SAndrew Thompson 	    sc->sc_addr);
379602ac6454SAndrew Thompson 
3797f29a0724SAndrew Thompson 	if (udev->flags.usb_mode != USB_MODE_HOST) {
379802ac6454SAndrew Thompson 		/* not supported */
379902ac6454SAndrew Thompson 		return;
380002ac6454SAndrew Thompson 	}
380139307315SAndrew Thompson 	if (udev->device_index != sc->sc_addr) {
380239307315SAndrew Thompson 
380302ac6454SAndrew Thompson 		if ((udev->speed != USB_SPEED_HIGH) &&
380402ac6454SAndrew Thompson 		    ((udev->hs_hub_addr == 0) ||
380502ac6454SAndrew Thompson 		    (udev->hs_port_no == 0) ||
3806495f25ceSAndrew Thompson 		    (udev->parent_hs_hub == NULL) ||
3807495f25ceSAndrew Thompson 		    (udev->parent_hs_hub->hub == NULL))) {
380802ac6454SAndrew Thompson 			/* We need a transaction translator */
380902ac6454SAndrew Thompson 			goto done;
381002ac6454SAndrew Thompson 		}
381102ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
381202ac6454SAndrew Thompson 		case UE_CONTROL:
3813ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_ctrl_methods;
381402ac6454SAndrew Thompson 			break;
381502ac6454SAndrew Thompson 		case UE_INTERRUPT:
3816ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_intr_methods;
381702ac6454SAndrew Thompson 			break;
381802ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
381902ac6454SAndrew Thompson 			if (udev->speed == USB_SPEED_HIGH) {
3820ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_hs_methods;
382102ac6454SAndrew Thompson 			} else if (udev->speed == USB_SPEED_FULL) {
3822ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_fs_methods;
382302ac6454SAndrew Thompson 			}
382402ac6454SAndrew Thompson 			break;
382502ac6454SAndrew Thompson 		case UE_BULK:
3826ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_bulk_methods;
382702ac6454SAndrew Thompson 			break;
382802ac6454SAndrew Thompson 		default:
382902ac6454SAndrew Thompson 			/* do nothing */
383002ac6454SAndrew Thompson 			break;
383102ac6454SAndrew Thompson 		}
383202ac6454SAndrew Thompson 	}
383302ac6454SAndrew Thompson done:
383402ac6454SAndrew Thompson 	return;
383502ac6454SAndrew Thompson }
383602ac6454SAndrew Thompson 
383702ac6454SAndrew Thompson static void
3838527aa7b2SAndrew Thompson ehci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
383902ac6454SAndrew Thompson {
384002ac6454SAndrew Thompson 	/*
384102ac6454SAndrew Thompson 	 * Wait until the hardware has finished any possible use of
384202ac6454SAndrew Thompson 	 * the transfer descriptor(s) and QH
384302ac6454SAndrew Thompson 	 */
384402ac6454SAndrew Thompson 	*pus = (188);			/* microseconds */
384502ac6454SAndrew Thompson }
384602ac6454SAndrew Thompson 
384702ac6454SAndrew Thompson static void
3848760bc48eSAndrew Thompson ehci_device_resume(struct usb_device *udev)
384902ac6454SAndrew Thompson {
385002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3851760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3852760bc48eSAndrew Thompson 	struct usb_pipe_methods *methods;
385302ac6454SAndrew Thompson 
385402ac6454SAndrew Thompson 	DPRINTF("\n");
385502ac6454SAndrew Thompson 
385602ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
385702ac6454SAndrew Thompson 
385802ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
385902ac6454SAndrew Thompson 
386002ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
386102ac6454SAndrew Thompson 
3862ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
386302ac6454SAndrew Thompson 
386402ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
386502ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
386602ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
386702ac6454SAndrew Thompson 				    sc->sc_async_p_last);
386802ac6454SAndrew Thompson 			}
386902ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
387002ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
387102ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
387202ac6454SAndrew Thompson 			}
387302ac6454SAndrew Thompson 		}
387402ac6454SAndrew Thompson 	}
387502ac6454SAndrew Thompson 
387602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
387702ac6454SAndrew Thompson 
387802ac6454SAndrew Thompson 	return;
387902ac6454SAndrew Thompson }
388002ac6454SAndrew Thompson 
388102ac6454SAndrew Thompson static void
3882760bc48eSAndrew Thompson ehci_device_suspend(struct usb_device *udev)
388302ac6454SAndrew Thompson {
388402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3885760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3886760bc48eSAndrew Thompson 	struct usb_pipe_methods *methods;
388702ac6454SAndrew Thompson 
388802ac6454SAndrew Thompson 	DPRINTF("\n");
388902ac6454SAndrew Thompson 
389002ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
389102ac6454SAndrew Thompson 
389202ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
389302ac6454SAndrew Thompson 
389402ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
389502ac6454SAndrew Thompson 
3896ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
389702ac6454SAndrew Thompson 
389802ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
389902ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
390002ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
390102ac6454SAndrew Thompson 				    sc->sc_async_p_last);
390202ac6454SAndrew Thompson 			}
390302ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
390402ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
390502ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
390602ac6454SAndrew Thompson 			}
390702ac6454SAndrew Thompson 		}
390802ac6454SAndrew Thompson 	}
390902ac6454SAndrew Thompson 
391002ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
391102ac6454SAndrew Thompson 
391202ac6454SAndrew Thompson 	return;
391302ac6454SAndrew Thompson }
391402ac6454SAndrew Thompson 
391502ac6454SAndrew Thompson static void
3916760bc48eSAndrew Thompson ehci_set_hw_power(struct usb_bus *bus)
391702ac6454SAndrew Thompson {
391802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
391902ac6454SAndrew Thompson 	uint32_t temp;
392002ac6454SAndrew Thompson 	uint32_t flags;
392102ac6454SAndrew Thompson 
392202ac6454SAndrew Thompson 	DPRINTF("\n");
392302ac6454SAndrew Thompson 
392402ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
392502ac6454SAndrew Thompson 
392602ac6454SAndrew Thompson 	flags = bus->hw_power_state;
392702ac6454SAndrew Thompson 
392802ac6454SAndrew Thompson 	temp = EOREAD4(sc, EHCI_USBCMD);
392902ac6454SAndrew Thompson 
393002ac6454SAndrew Thompson 	temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
393102ac6454SAndrew Thompson 
393202ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_CONTROL |
393302ac6454SAndrew Thompson 	    USB_HW_POWER_BULK)) {
393402ac6454SAndrew Thompson 		DPRINTF("Async is active\n");
393502ac6454SAndrew Thompson 		temp |= EHCI_CMD_ASE;
393602ac6454SAndrew Thompson 	}
393702ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_INTERRUPT |
393802ac6454SAndrew Thompson 	    USB_HW_POWER_ISOC)) {
393902ac6454SAndrew Thompson 		DPRINTF("Periodic is active\n");
394002ac6454SAndrew Thompson 		temp |= EHCI_CMD_PSE;
394102ac6454SAndrew Thompson 	}
394202ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, temp);
394302ac6454SAndrew Thompson 
394402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
394502ac6454SAndrew Thompson 
394602ac6454SAndrew Thompson 	return;
394702ac6454SAndrew Thompson }
394802ac6454SAndrew Thompson 
3949760bc48eSAndrew Thompson struct usb_bus_methods ehci_bus_methods =
395002ac6454SAndrew Thompson {
3951ae60fdfbSAndrew Thompson 	.endpoint_init = ehci_ep_init,
395202ac6454SAndrew Thompson 	.xfer_setup = ehci_xfer_setup,
395302ac6454SAndrew Thompson 	.xfer_unsetup = ehci_xfer_unsetup,
395402ac6454SAndrew Thompson 	.get_dma_delay = ehci_get_dma_delay,
395502ac6454SAndrew Thompson 	.device_resume = ehci_device_resume,
395602ac6454SAndrew Thompson 	.device_suspend = ehci_device_suspend,
395702ac6454SAndrew Thompson 	.set_hw_power = ehci_set_hw_power,
395839307315SAndrew Thompson 	.roothub_exec = ehci_roothub_exec,
3959dddb25f9SAlfred Perlstein 	.xfer_poll = ehci_do_poll,
396002ac6454SAndrew Thompson };
3961