xref: /freebsd/sys/dev/usb/controller/ehci.c (revision c8a14b91249be80157c80a086bfd47cb601e4c7e)
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/linker_set.h>
58ed6d949aSAndrew Thompson #include <sys/module.h>
59ed6d949aSAndrew Thompson #include <sys/lock.h>
60ed6d949aSAndrew Thompson #include <sys/mutex.h>
61ed6d949aSAndrew Thompson #include <sys/condvar.h>
62ed6d949aSAndrew Thompson #include <sys/sysctl.h>
63ed6d949aSAndrew Thompson #include <sys/sx.h>
64ed6d949aSAndrew Thompson #include <sys/unistd.h>
65ed6d949aSAndrew Thompson #include <sys/callout.h>
66ed6d949aSAndrew Thompson #include <sys/malloc.h>
67ed6d949aSAndrew Thompson #include <sys/priv.h>
68ed6d949aSAndrew Thompson 
6902ac6454SAndrew Thompson #include <dev/usb/usb.h>
70ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson #define	USB_DEBUG_VAR ehcidebug
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
7502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7802ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
7902ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
8002ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
8102ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
8402ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
8502ac6454SAndrew Thompson #include <dev/usb/controller/ehci.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 
9102ac6454SAndrew Thompson #if USB_DEBUG
9202ac6454SAndrew Thompson static int ehcidebug = 0;
9302ac6454SAndrew Thompson static int ehcinohighspeed = 0;
9402ac6454SAndrew Thompson 
959360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
969360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
9702ac6454SAndrew Thompson     &ehcidebug, 0, "Debug level");
989360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RW,
9902ac6454SAndrew Thompson     &ehcinohighspeed, 0, "Disable High Speed USB");
10002ac6454SAndrew Thompson 
10102ac6454SAndrew Thompson static void ehci_dump_regs(ehci_softc_t *sc);
10202ac6454SAndrew Thompson static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson #endif
10502ac6454SAndrew Thompson 
10602ac6454SAndrew Thompson #define	EHCI_INTR_ENDPT 1
10702ac6454SAndrew Thompson 
108760bc48eSAndrew Thompson extern struct usb_bus_methods ehci_bus_methods;
109760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_bulk_methods;
110760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_ctrl_methods;
111760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_intr_methods;
112760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_isoc_fs_methods;
113760bc48eSAndrew Thompson extern struct usb_pipe_methods ehci_device_isoc_hs_methods;
11402ac6454SAndrew Thompson 
115760bc48eSAndrew Thompson static void ehci_do_poll(struct usb_bus *bus);
116e0a69b51SAndrew Thompson static void ehci_device_done(struct usb_xfer *xfer, usb_error_t error);
117760bc48eSAndrew Thompson static uint8_t ehci_check_transfer(struct usb_xfer *xfer);
11802ac6454SAndrew Thompson static void ehci_timeout(void *arg);
11939307315SAndrew Thompson static void ehci_root_intr(ehci_softc_t *sc);
12002ac6454SAndrew Thompson 
12102ac6454SAndrew Thompson struct ehci_std_temp {
12202ac6454SAndrew Thompson 	ehci_softc_t *sc;
123760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
12402ac6454SAndrew Thompson 	ehci_qtd_t *td;
12502ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
12602ac6454SAndrew Thompson 	uint32_t average;
12702ac6454SAndrew Thompson 	uint32_t qtd_status;
12802ac6454SAndrew Thompson 	uint32_t len;
12902ac6454SAndrew Thompson 	uint16_t max_frame_size;
13002ac6454SAndrew Thompson 	uint8_t	shortpkt;
13102ac6454SAndrew Thompson 	uint8_t	auto_data_toggle;
13202ac6454SAndrew Thompson 	uint8_t	setup_alt_next;
1330f7d4548SAndrew Thompson 	uint8_t	last_frame;
134c8a14b91SAndrew Thompson 	uint8_t can_use_next;
13502ac6454SAndrew Thompson };
13602ac6454SAndrew Thompson 
13702ac6454SAndrew Thompson void
138e0a69b51SAndrew Thompson ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
13902ac6454SAndrew Thompson {
14002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
14102ac6454SAndrew Thompson 	uint32_t i;
14202ac6454SAndrew Thompson 
14302ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
14402ac6454SAndrew Thompson 	    sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
14702ac6454SAndrew Thompson 	    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
15002ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.intr_start_pc + i,
15102ac6454SAndrew Thompson 		    sc->sc_hw.intr_start_pg + i,
15202ac6454SAndrew Thompson 		    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
15302ac6454SAndrew Thompson 	}
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
15602ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
15702ac6454SAndrew Thompson 		    sc->sc_hw.isoc_hs_start_pg + i,
15802ac6454SAndrew Thompson 		    sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
15902ac6454SAndrew Thompson 	}
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
16202ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
16302ac6454SAndrew Thompson 		    sc->sc_hw.isoc_fs_start_pg + i,
16402ac6454SAndrew Thompson 		    sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
16502ac6454SAndrew Thompson 	}
16602ac6454SAndrew Thompson }
16702ac6454SAndrew Thompson 
168e0a69b51SAndrew Thompson usb_error_t
169e55e1ebcSAndrew Thompson ehci_reset(ehci_softc_t *sc)
17002ac6454SAndrew Thompson {
17102ac6454SAndrew Thompson 	uint32_t hcr;
172e55e1ebcSAndrew Thompson 	int i;
173e55e1ebcSAndrew Thompson 
174e55e1ebcSAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
175e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
176a593f6b8SAndrew Thompson 		usb_pause_mtx(NULL, hz / 1000);
177e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
178e55e1ebcSAndrew Thompson 		if (!hcr) {
179e55e1ebcSAndrew Thompson 			if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) {
180e55e1ebcSAndrew Thompson 				/*
181e55e1ebcSAndrew Thompson 				 * Force USBMODE as requested.  Controllers
182e55e1ebcSAndrew Thompson 				 * may have multiple operating modes.
183e55e1ebcSAndrew Thompson 				 */
184e55e1ebcSAndrew Thompson 				uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE);
185e55e1ebcSAndrew Thompson 				if (sc->sc_flags & EHCI_SCFLG_SETMODE) {
186e55e1ebcSAndrew Thompson 					usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST;
187e55e1ebcSAndrew Thompson 					device_printf(sc->sc_bus.bdev,
188e55e1ebcSAndrew Thompson 					    "set host controller mode\n");
189e55e1ebcSAndrew Thompson 				}
190e55e1ebcSAndrew Thompson 				if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) {
191e55e1ebcSAndrew Thompson 					usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE;
192e55e1ebcSAndrew Thompson 					device_printf(sc->sc_bus.bdev,
193e55e1ebcSAndrew Thompson 					    "set big-endian mode\n");
194e55e1ebcSAndrew Thompson 				}
195e55e1ebcSAndrew Thompson 				EOWRITE4(sc,  EHCI_USBMODE, usbmode);
196e55e1ebcSAndrew Thompson 			}
197e55e1ebcSAndrew Thompson 			return (0);
198e55e1ebcSAndrew Thompson 		}
199e55e1ebcSAndrew Thompson 	}
200e55e1ebcSAndrew Thompson 	device_printf(sc->sc_bus.bdev, "reset timeout\n");
201e55e1ebcSAndrew Thompson 	return (USB_ERR_IOERROR);
202e55e1ebcSAndrew Thompson }
203e55e1ebcSAndrew Thompson 
204e0a69b51SAndrew Thompson static usb_error_t
205e55e1ebcSAndrew Thompson ehci_hcreset(ehci_softc_t *sc)
206e55e1ebcSAndrew Thompson {
207e55e1ebcSAndrew Thompson 	uint32_t hcr;
208e55e1ebcSAndrew Thompson 	int i;
20902ac6454SAndrew Thompson 
21002ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
211e55e1ebcSAndrew Thompson 	for (i = 0; i < 100; i++) {
212a593f6b8SAndrew Thompson 		usb_pause_mtx(NULL, hz / 1000);
213e55e1ebcSAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
214e55e1ebcSAndrew Thompson 		if (hcr)
21502ac6454SAndrew Thompson 			break;
21602ac6454SAndrew Thompson 	}
217e55e1ebcSAndrew Thompson 	if (!hcr)
21802ac6454SAndrew Thompson 		/*
21902ac6454SAndrew Thompson                  * Fall through and try reset anyway even though
22002ac6454SAndrew Thompson                  * Table 2-9 in the EHCI spec says this will result
22102ac6454SAndrew Thompson                  * in undefined behavior.
22202ac6454SAndrew Thompson                  */
223e55e1ebcSAndrew Thompson 		device_printf(sc->sc_bus.bdev, "stop timeout\n");
22402ac6454SAndrew Thompson 
225e55e1ebcSAndrew Thompson 	return ehci_reset(sc);
22602ac6454SAndrew Thompson }
22702ac6454SAndrew Thompson 
228e0a69b51SAndrew Thompson usb_error_t
22902ac6454SAndrew Thompson ehci_init(ehci_softc_t *sc)
23002ac6454SAndrew Thompson {
231760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
23202ac6454SAndrew Thompson 	uint32_t version;
23302ac6454SAndrew Thompson 	uint32_t sparams;
23402ac6454SAndrew Thompson 	uint32_t cparams;
23502ac6454SAndrew Thompson 	uint32_t hcr;
23602ac6454SAndrew Thompson 	uint16_t i;
23702ac6454SAndrew Thompson 	uint16_t x;
23802ac6454SAndrew Thompson 	uint16_t y;
23902ac6454SAndrew Thompson 	uint16_t bit;
240e0a69b51SAndrew Thompson 	usb_error_t err = 0;
24102ac6454SAndrew Thompson 
24202ac6454SAndrew Thompson 	DPRINTF("start\n");
24302ac6454SAndrew Thompson 
244a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
24502ac6454SAndrew Thompson 
24602ac6454SAndrew Thompson #if USB_DEBUG
24702ac6454SAndrew Thompson 	if (ehcidebug > 2) {
24802ac6454SAndrew Thompson 		ehci_dump_regs(sc);
24902ac6454SAndrew Thompson 	}
25002ac6454SAndrew Thompson #endif
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson 	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson 	version = EREAD2(sc, EHCI_HCIVERSION);
25502ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
25602ac6454SAndrew Thompson 	    version >> 8, version & 0xff);
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
25902ac6454SAndrew Thompson 	DPRINTF("sparams=0x%x\n", sparams);
26002ac6454SAndrew Thompson 
26102ac6454SAndrew Thompson 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
26202ac6454SAndrew Thompson 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
26302ac6454SAndrew Thompson 	DPRINTF("cparams=0x%x\n", cparams);
26402ac6454SAndrew Thompson 
26502ac6454SAndrew Thompson 	if (EHCI_HCC_64BIT(cparams)) {
26602ac6454SAndrew Thompson 		DPRINTF("HCC uses 64-bit structures\n");
26702ac6454SAndrew Thompson 
26802ac6454SAndrew Thompson 		/* MUST clear segment register if 64 bit capable */
26902ac6454SAndrew Thompson 		EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
27002ac6454SAndrew Thompson 	}
27102ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_2_0;
27202ac6454SAndrew Thompson 
27302ac6454SAndrew Thompson 	/* Reset the controller */
27402ac6454SAndrew Thompson 	DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev));
27502ac6454SAndrew Thompson 
276e55e1ebcSAndrew Thompson 	err = ehci_hcreset(sc);
27702ac6454SAndrew Thompson 	if (err) {
27802ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "reset timeout\n");
27902ac6454SAndrew Thompson 		return (err);
28002ac6454SAndrew Thompson 	}
28102ac6454SAndrew Thompson 	/*
28202ac6454SAndrew Thompson 	 * use current frame-list-size selection 0: 1024*4 bytes 1:  512*4
28302ac6454SAndrew Thompson 	 * bytes 2:  256*4 bytes 3:      unknown
28402ac6454SAndrew Thompson 	 */
28502ac6454SAndrew Thompson 	if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
28602ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
28702ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
28802ac6454SAndrew Thompson 	}
28902ac6454SAndrew Thompson 	/* set up the bus struct */
29002ac6454SAndrew Thompson 	sc->sc_bus.methods = &ehci_bus_methods;
29102ac6454SAndrew Thompson 
29202ac6454SAndrew Thompson 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
29302ac6454SAndrew Thompson 
29402ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
29502ac6454SAndrew Thompson 		ehci_qh_t *qh;
29602ac6454SAndrew Thompson 
297a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
29802ac6454SAndrew Thompson 
29902ac6454SAndrew Thompson 		qh = buf_res.buffer;
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson 		/* initialize page cache pointer */
30202ac6454SAndrew Thompson 
30302ac6454SAndrew Thompson 		qh->page_cache = sc->sc_hw.intr_start_pc + i;
30402ac6454SAndrew Thompson 
30502ac6454SAndrew Thompson 		/* store a pointer to queue head */
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson 		sc->sc_intr_p_last[i] = qh;
30802ac6454SAndrew Thompson 
30902ac6454SAndrew Thompson 		qh->qh_self =
310e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
311e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson 		qh->qh_endp =
314e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
31502ac6454SAndrew Thompson 		qh->qh_endphub =
316e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_MULT(1));
31702ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
31802ac6454SAndrew Thompson 
31902ac6454SAndrew Thompson 		qh->qh_qtd.qtd_next =
320e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
32102ac6454SAndrew Thompson 		qh->qh_qtd.qtd_altnext =
322e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
32302ac6454SAndrew Thompson 		qh->qh_qtd.qtd_status =
324e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QTD_HALTED);
32502ac6454SAndrew Thompson 	}
32602ac6454SAndrew Thompson 
32702ac6454SAndrew Thompson 	/*
32802ac6454SAndrew Thompson 	 * the QHs are arranged to give poll intervals that are
32902ac6454SAndrew Thompson 	 * powers of 2 times 1ms
33002ac6454SAndrew Thompson 	 */
33102ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
33202ac6454SAndrew Thompson 	while (bit) {
33302ac6454SAndrew Thompson 		x = bit;
33402ac6454SAndrew Thompson 		while (x & bit) {
33502ac6454SAndrew Thompson 			ehci_qh_t *qh_x;
33602ac6454SAndrew Thompson 			ehci_qh_t *qh_y;
33702ac6454SAndrew Thompson 
33802ac6454SAndrew Thompson 			y = (x ^ bit) | (bit / 2);
33902ac6454SAndrew Thompson 
34002ac6454SAndrew Thompson 			qh_x = sc->sc_intr_p_last[x];
34102ac6454SAndrew Thompson 			qh_y = sc->sc_intr_p_last[y];
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 			/*
34402ac6454SAndrew Thompson 			 * the next QH has half the poll interval
34502ac6454SAndrew Thompson 			 */
34602ac6454SAndrew Thompson 			qh_x->qh_link = qh_y->qh_self;
34702ac6454SAndrew Thompson 
34802ac6454SAndrew Thompson 			x++;
34902ac6454SAndrew Thompson 		}
35002ac6454SAndrew Thompson 		bit >>= 1;
35102ac6454SAndrew Thompson 	}
35202ac6454SAndrew Thompson 
35302ac6454SAndrew Thompson 	if (1) {
35402ac6454SAndrew Thompson 		ehci_qh_t *qh;
35502ac6454SAndrew Thompson 
35602ac6454SAndrew Thompson 		qh = sc->sc_intr_p_last[0];
35702ac6454SAndrew Thompson 
35802ac6454SAndrew Thompson 		/* the last (1ms) QH terminates */
359e55e1ebcSAndrew Thompson 		qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);
36002ac6454SAndrew Thompson 	}
36102ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
36202ac6454SAndrew Thompson 		ehci_sitd_t *sitd;
36302ac6454SAndrew Thompson 		ehci_itd_t *itd;
36402ac6454SAndrew Thompson 
365a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson 		sitd = buf_res.buffer;
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson 		/* initialize page cache pointer */
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson 		sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
37202ac6454SAndrew Thompson 
37302ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 		sc->sc_isoc_fs_p_last[i] = sitd;
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 		/* initialize full speed isochronous */
37802ac6454SAndrew Thompson 
37902ac6454SAndrew Thompson 		sitd->sitd_self =
380e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
381e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_SITD);
38202ac6454SAndrew Thompson 
38302ac6454SAndrew Thompson 		sitd->sitd_back =
384e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_TERMINATE);
38502ac6454SAndrew Thompson 
38602ac6454SAndrew Thompson 		sitd->sitd_next =
38702ac6454SAndrew Thompson 		    sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 
390a593f6b8SAndrew Thompson 		usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
39102ac6454SAndrew Thompson 
39202ac6454SAndrew Thompson 		itd = buf_res.buffer;
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 		/* initialize page cache pointer */
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 		itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
39702ac6454SAndrew Thompson 
39802ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
39902ac6454SAndrew Thompson 
40002ac6454SAndrew Thompson 		sc->sc_isoc_hs_p_last[i] = itd;
40102ac6454SAndrew Thompson 
40202ac6454SAndrew Thompson 		/* initialize high speed isochronous */
40302ac6454SAndrew Thompson 
40402ac6454SAndrew Thompson 		itd->itd_self =
405e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
406e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_ITD);
40702ac6454SAndrew Thompson 
40802ac6454SAndrew Thompson 		itd->itd_next =
40902ac6454SAndrew Thompson 		    sitd->sitd_self;
41002ac6454SAndrew Thompson 	}
41102ac6454SAndrew Thompson 
412a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
41302ac6454SAndrew Thompson 
41402ac6454SAndrew Thompson 	if (1) {
41502ac6454SAndrew Thompson 		uint32_t *pframes;
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson 		pframes = buf_res.buffer;
41802ac6454SAndrew Thompson 
41902ac6454SAndrew Thompson 		/*
42002ac6454SAndrew Thompson 		 * execution order:
42102ac6454SAndrew Thompson 		 * pframes -> high speed isochronous ->
42202ac6454SAndrew Thompson 		 *    full speed isochronous -> interrupt QH's
42302ac6454SAndrew Thompson 		 */
42402ac6454SAndrew Thompson 		for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
42502ac6454SAndrew Thompson 			pframes[i] = sc->sc_isoc_hs_p_last
42602ac6454SAndrew Thompson 			    [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
42702ac6454SAndrew Thompson 		}
42802ac6454SAndrew Thompson 	}
42902ac6454SAndrew Thompson 	/* setup sync list pointer */
43002ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
43102ac6454SAndrew Thompson 
432a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
43302ac6454SAndrew Thompson 
43402ac6454SAndrew Thompson 	if (1) {
43502ac6454SAndrew Thompson 
43602ac6454SAndrew Thompson 		ehci_qh_t *qh;
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson 		qh = buf_res.buffer;
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 		/* initialize page cache pointer */
44102ac6454SAndrew Thompson 
44202ac6454SAndrew Thompson 		qh->page_cache = &sc->sc_hw.async_start_pc;
44302ac6454SAndrew Thompson 
44402ac6454SAndrew Thompson 		/* store a pointer to the queue head */
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson 		sc->sc_async_p_last = qh;
44702ac6454SAndrew Thompson 
44802ac6454SAndrew Thompson 		/* init dummy QH that starts the async list */
44902ac6454SAndrew Thompson 
45002ac6454SAndrew Thompson 		qh->qh_self =
451e55e1ebcSAndrew Thompson 		    htohc32(sc, buf_res.physaddr) |
452e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_LINK_QH);
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 		/* fill the QH */
45502ac6454SAndrew Thompson 		qh->qh_endp =
456e55e1ebcSAndrew Thompson 		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
457e55e1ebcSAndrew Thompson 		qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
45802ac6454SAndrew Thompson 		qh->qh_link = qh->qh_self;
45902ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
46002ac6454SAndrew Thompson 
46102ac6454SAndrew Thompson 		/* fill the overlay qTD */
462e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
463e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
464e55e1ebcSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);
46502ac6454SAndrew Thompson 	}
46602ac6454SAndrew Thompson 	/* flush all cache into memory */
46702ac6454SAndrew Thompson 
468a593f6b8SAndrew Thompson 	usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
46902ac6454SAndrew Thompson 
47002ac6454SAndrew Thompson #if USB_DEBUG
47102ac6454SAndrew Thompson 	if (ehcidebug) {
47202ac6454SAndrew Thompson 		ehci_dump_sqh(sc, sc->sc_async_p_last);
47302ac6454SAndrew Thompson 	}
47402ac6454SAndrew Thompson #endif
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson 	/* setup async list pointer */
47702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
47802ac6454SAndrew Thompson 
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 	/* enable interrupts */
48102ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
48202ac6454SAndrew Thompson 
48302ac6454SAndrew Thompson 	/* turn on controller */
48402ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD,
48502ac6454SAndrew Thompson 	    EHCI_CMD_ITC_1 |		/* 1 microframes interrupt delay */
48602ac6454SAndrew Thompson 	    (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
48702ac6454SAndrew Thompson 	    EHCI_CMD_ASE |
48802ac6454SAndrew Thompson 	    EHCI_CMD_PSE |
48902ac6454SAndrew Thompson 	    EHCI_CMD_RS);
49002ac6454SAndrew Thompson 
49102ac6454SAndrew Thompson 	/* Take over port ownership */
49202ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
49302ac6454SAndrew Thompson 
49402ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
495a593f6b8SAndrew Thompson 		usb_pause_mtx(NULL, hz / 1000);
49602ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
49702ac6454SAndrew Thompson 		if (!hcr) {
49802ac6454SAndrew Thompson 			break;
49902ac6454SAndrew Thompson 		}
50002ac6454SAndrew Thompson 	}
50102ac6454SAndrew Thompson 	if (hcr) {
50202ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "run timeout\n");
50302ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
50402ac6454SAndrew Thompson 	}
50502ac6454SAndrew Thompson 
50602ac6454SAndrew Thompson 	if (!err) {
50702ac6454SAndrew Thompson 		/* catch any lost interrupts */
50802ac6454SAndrew Thompson 		ehci_do_poll(&sc->sc_bus);
50902ac6454SAndrew Thompson 	}
51002ac6454SAndrew Thompson 	return (err);
51102ac6454SAndrew Thompson }
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson /*
51402ac6454SAndrew Thompson  * shut down the controller when the system is going down
51502ac6454SAndrew Thompson  */
51602ac6454SAndrew Thompson void
51702ac6454SAndrew Thompson ehci_detach(ehci_softc_t *sc)
51802ac6454SAndrew Thompson {
51902ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
52002ac6454SAndrew Thompson 
521a593f6b8SAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_pcd);
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
524e55e1ebcSAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
52502ac6454SAndrew Thompson 
526e55e1ebcSAndrew Thompson 	if (ehci_hcreset(sc)) {
52702ac6454SAndrew Thompson 		DPRINTF("reset failed!\n");
52802ac6454SAndrew Thompson 	}
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson 	/* XXX let stray task complete */
531a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL, hz / 20);
53202ac6454SAndrew Thompson 
533a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_pcd);
53402ac6454SAndrew Thompson }
53502ac6454SAndrew Thompson 
53602ac6454SAndrew Thompson void
53702ac6454SAndrew Thompson ehci_suspend(ehci_softc_t *sc)
53802ac6454SAndrew Thompson {
53902ac6454SAndrew Thompson 	uint32_t cmd;
54002ac6454SAndrew Thompson 	uint32_t hcr;
54102ac6454SAndrew Thompson 	uint8_t i;
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
54402ac6454SAndrew Thompson 
54502ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
54602ac6454SAndrew Thompson 		cmd = EOREAD4(sc, EHCI_PORTSC(i));
54702ac6454SAndrew Thompson 		if (((cmd & EHCI_PS_PO) == 0) &&
54802ac6454SAndrew Thompson 		    ((cmd & EHCI_PS_PE) == EHCI_PS_PE)) {
54902ac6454SAndrew Thompson 			EOWRITE4(sc, EHCI_PORTSC(i),
55002ac6454SAndrew Thompson 			    cmd | EHCI_PS_SUSP);
55102ac6454SAndrew Thompson 		}
55202ac6454SAndrew Thompson 	}
55302ac6454SAndrew Thompson 
55402ac6454SAndrew Thompson 	sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson 	cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
55702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, cmd);
55802ac6454SAndrew Thompson 
55902ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
56002ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) &
56102ac6454SAndrew Thompson 		    (EHCI_STS_ASS | EHCI_STS_PSS);
56202ac6454SAndrew Thompson 
56302ac6454SAndrew Thompson 		if (hcr == 0) {
56402ac6454SAndrew Thompson 			break;
56502ac6454SAndrew Thompson 		}
566a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
56702ac6454SAndrew Thompson 	}
56802ac6454SAndrew Thompson 
56902ac6454SAndrew Thompson 	if (hcr != 0) {
57002ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "reset timeout\n");
57102ac6454SAndrew Thompson 	}
57202ac6454SAndrew Thompson 	cmd &= ~EHCI_CMD_RS;
57302ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, cmd);
57402ac6454SAndrew Thompson 
57502ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
57602ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
57702ac6454SAndrew Thompson 		if (hcr == EHCI_STS_HCH) {
57802ac6454SAndrew Thompson 			break;
57902ac6454SAndrew Thompson 		}
580a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
58102ac6454SAndrew Thompson 	}
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson 	if (hcr != EHCI_STS_HCH) {
58402ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev,
58502ac6454SAndrew Thompson 		    "config timeout\n");
58602ac6454SAndrew Thompson 	}
58702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
58802ac6454SAndrew Thompson }
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson void
59102ac6454SAndrew Thompson ehci_resume(ehci_softc_t *sc)
59202ac6454SAndrew Thompson {
593760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
59402ac6454SAndrew Thompson 	uint32_t cmd;
59502ac6454SAndrew Thompson 	uint32_t hcr;
59602ac6454SAndrew Thompson 	uint8_t i;
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
59902ac6454SAndrew Thompson 
60002ac6454SAndrew Thompson 	/* restore things in case the bios doesn't */
60102ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
60202ac6454SAndrew Thompson 
603a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
60402ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
60502ac6454SAndrew Thompson 
606a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
60702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
60802ac6454SAndrew Thompson 
60902ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson 	hcr = 0;
61202ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
61302ac6454SAndrew Thompson 		cmd = EOREAD4(sc, EHCI_PORTSC(i));
61402ac6454SAndrew Thompson 		if (((cmd & EHCI_PS_PO) == 0) &&
61502ac6454SAndrew Thompson 		    ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
61602ac6454SAndrew Thompson 			EOWRITE4(sc, EHCI_PORTSC(i),
61702ac6454SAndrew Thompson 			    cmd | EHCI_PS_FPR);
61802ac6454SAndrew Thompson 			hcr = 1;
61902ac6454SAndrew Thompson 		}
62002ac6454SAndrew Thompson 	}
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 	if (hcr) {
623a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx,
62402ac6454SAndrew Thompson 		    USB_MS_TO_TICKS(USB_RESUME_WAIT));
62502ac6454SAndrew Thompson 
62602ac6454SAndrew Thompson 		for (i = 1; i <= sc->sc_noport; i++) {
62702ac6454SAndrew Thompson 			cmd = EOREAD4(sc, EHCI_PORTSC(i));
62802ac6454SAndrew Thompson 			if (((cmd & EHCI_PS_PO) == 0) &&
62902ac6454SAndrew Thompson 			    ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
63002ac6454SAndrew Thompson 				EOWRITE4(sc, EHCI_PORTSC(i),
63102ac6454SAndrew Thompson 				    cmd & ~EHCI_PS_FPR);
63202ac6454SAndrew Thompson 			}
63302ac6454SAndrew Thompson 		}
63402ac6454SAndrew Thompson 	}
63502ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
63802ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
63902ac6454SAndrew Thompson 		if (hcr != EHCI_STS_HCH) {
64002ac6454SAndrew Thompson 			break;
64102ac6454SAndrew Thompson 		}
642a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
64302ac6454SAndrew Thompson 	}
64402ac6454SAndrew Thompson 	if (hcr == EHCI_STS_HCH) {
64502ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "config timeout\n");
64602ac6454SAndrew Thompson 	}
64702ac6454SAndrew Thompson 
64802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
64902ac6454SAndrew Thompson 
650a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL,
65102ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(USB_RESUME_WAIT));
65202ac6454SAndrew Thompson 
65302ac6454SAndrew Thompson 	/* catch any lost interrupts */
65402ac6454SAndrew Thompson 	ehci_do_poll(&sc->sc_bus);
65502ac6454SAndrew Thompson }
65602ac6454SAndrew Thompson 
65702ac6454SAndrew Thompson void
65802ac6454SAndrew Thompson ehci_shutdown(ehci_softc_t *sc)
65902ac6454SAndrew Thompson {
66002ac6454SAndrew Thompson 	DPRINTF("stopping the HC\n");
66102ac6454SAndrew Thompson 
662e55e1ebcSAndrew Thompson 	if (ehci_hcreset(sc)) {
66302ac6454SAndrew Thompson 		DPRINTF("reset failed!\n");
66402ac6454SAndrew Thompson 	}
66502ac6454SAndrew Thompson }
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson #if USB_DEBUG
66802ac6454SAndrew Thompson static void
66902ac6454SAndrew Thompson ehci_dump_regs(ehci_softc_t *sc)
67002ac6454SAndrew Thompson {
67102ac6454SAndrew Thompson 	uint32_t i;
67202ac6454SAndrew Thompson 
67302ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBCMD);
67402ac6454SAndrew Thompson 	printf("cmd=0x%08x\n", i);
67502ac6454SAndrew Thompson 
67602ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_1)
67702ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_1\n");
67802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_2)
67902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_2\n");
68002ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_4)
68102ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_4\n");
68202ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_8)
68302ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_8\n");
68402ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_16)
68502ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_16\n");
68602ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_32)
68702ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_32\n");
68802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_64)
68902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_64\n");
69002ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPME)
69102ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPME\n");
69202ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPMC)
69302ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPMC\n");
69402ac6454SAndrew Thompson 	if (i & EHCI_CMD_LHCR)
69502ac6454SAndrew Thompson 		printf(" EHCI_CMD_LHCR\n");
69602ac6454SAndrew Thompson 	if (i & EHCI_CMD_IAAD)
69702ac6454SAndrew Thompson 		printf(" EHCI_CMD_IAAD\n");
69802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASE)
69902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASE\n");
70002ac6454SAndrew Thompson 	if (i & EHCI_CMD_PSE)
70102ac6454SAndrew Thompson 		printf(" EHCI_CMD_PSE\n");
70202ac6454SAndrew Thompson 	if (i & EHCI_CMD_FLS_M)
70302ac6454SAndrew Thompson 		printf(" EHCI_CMD_FLS_M\n");
70402ac6454SAndrew Thompson 	if (i & EHCI_CMD_HCRESET)
70502ac6454SAndrew Thompson 		printf(" EHCI_CMD_HCRESET\n");
70602ac6454SAndrew Thompson 	if (i & EHCI_CMD_RS)
70702ac6454SAndrew Thompson 		printf(" EHCI_CMD_RS\n");
70802ac6454SAndrew Thompson 
70902ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBSTS);
71002ac6454SAndrew Thompson 
71102ac6454SAndrew Thompson 	printf("sts=0x%08x\n", i);
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 	if (i & EHCI_STS_ASS)
71402ac6454SAndrew Thompson 		printf(" EHCI_STS_ASS\n");
71502ac6454SAndrew Thompson 	if (i & EHCI_STS_PSS)
71602ac6454SAndrew Thompson 		printf(" EHCI_STS_PSS\n");
71702ac6454SAndrew Thompson 	if (i & EHCI_STS_REC)
71802ac6454SAndrew Thompson 		printf(" EHCI_STS_REC\n");
71902ac6454SAndrew Thompson 	if (i & EHCI_STS_HCH)
72002ac6454SAndrew Thompson 		printf(" EHCI_STS_HCH\n");
72102ac6454SAndrew Thompson 	if (i & EHCI_STS_IAA)
72202ac6454SAndrew Thompson 		printf(" EHCI_STS_IAA\n");
72302ac6454SAndrew Thompson 	if (i & EHCI_STS_HSE)
72402ac6454SAndrew Thompson 		printf(" EHCI_STS_HSE\n");
72502ac6454SAndrew Thompson 	if (i & EHCI_STS_FLR)
72602ac6454SAndrew Thompson 		printf(" EHCI_STS_FLR\n");
72702ac6454SAndrew Thompson 	if (i & EHCI_STS_PCD)
72802ac6454SAndrew Thompson 		printf(" EHCI_STS_PCD\n");
72902ac6454SAndrew Thompson 	if (i & EHCI_STS_ERRINT)
73002ac6454SAndrew Thompson 		printf(" EHCI_STS_ERRINT\n");
73102ac6454SAndrew Thompson 	if (i & EHCI_STS_INT)
73202ac6454SAndrew Thompson 		printf(" EHCI_STS_INT\n");
73302ac6454SAndrew Thompson 
73402ac6454SAndrew Thompson 	printf("ien=0x%08x\n",
73502ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_USBINTR));
73602ac6454SAndrew Thompson 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
73702ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_FRINDEX),
73802ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
73902ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
74002ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
74102ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
74202ac6454SAndrew Thompson 		printf("port %d status=0x%08x\n", i,
74302ac6454SAndrew Thompson 		    EOREAD4(sc, EHCI_PORTSC(i)));
74402ac6454SAndrew Thompson 	}
74502ac6454SAndrew Thompson }
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson static void
74802ac6454SAndrew Thompson ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
74902ac6454SAndrew Thompson {
750e55e1ebcSAndrew Thompson 	link = hc32toh(sc, link);
75102ac6454SAndrew Thompson 	printf("0x%08x", link);
75202ac6454SAndrew Thompson 	if (link & EHCI_LINK_TERMINATE)
75302ac6454SAndrew Thompson 		printf("<T>");
75402ac6454SAndrew Thompson 	else {
75502ac6454SAndrew Thompson 		printf("<");
75602ac6454SAndrew Thompson 		if (type) {
75702ac6454SAndrew Thompson 			switch (EHCI_LINK_TYPE(link)) {
75802ac6454SAndrew Thompson 			case EHCI_LINK_ITD:
75902ac6454SAndrew Thompson 				printf("ITD");
76002ac6454SAndrew Thompson 				break;
76102ac6454SAndrew Thompson 			case EHCI_LINK_QH:
76202ac6454SAndrew Thompson 				printf("QH");
76302ac6454SAndrew Thompson 				break;
76402ac6454SAndrew Thompson 			case EHCI_LINK_SITD:
76502ac6454SAndrew Thompson 				printf("SITD");
76602ac6454SAndrew Thompson 				break;
76702ac6454SAndrew Thompson 			case EHCI_LINK_FSTN:
76802ac6454SAndrew Thompson 				printf("FSTN");
76902ac6454SAndrew Thompson 				break;
77002ac6454SAndrew Thompson 			}
77102ac6454SAndrew Thompson 		}
77202ac6454SAndrew Thompson 		printf(">");
77302ac6454SAndrew Thompson 	}
77402ac6454SAndrew Thompson }
77502ac6454SAndrew Thompson 
77602ac6454SAndrew Thompson static void
77702ac6454SAndrew Thompson ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
77802ac6454SAndrew Thompson {
77902ac6454SAndrew Thompson 	uint32_t s;
78002ac6454SAndrew Thompson 
78102ac6454SAndrew Thompson 	printf("  next=");
78202ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_next, 0);
78302ac6454SAndrew Thompson 	printf(" altnext=");
78402ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_altnext, 0);
78502ac6454SAndrew Thompson 	printf("\n");
786e55e1ebcSAndrew Thompson 	s = hc32toh(sc, qtd->qtd_status);
78702ac6454SAndrew Thompson 	printf("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
78802ac6454SAndrew Thompson 	    s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
78902ac6454SAndrew Thompson 	    EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
79002ac6454SAndrew Thompson 	printf("    cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
79102ac6454SAndrew Thompson 	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
79202ac6454SAndrew Thompson 	    (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
79302ac6454SAndrew Thompson 	    (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
79402ac6454SAndrew Thompson 	    (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
79502ac6454SAndrew Thompson 	    (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
79602ac6454SAndrew Thompson 	    (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
79702ac6454SAndrew Thompson 	    (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
79802ac6454SAndrew Thompson 	    (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
79902ac6454SAndrew Thompson 	    (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
80202ac6454SAndrew Thompson 		printf("  buffer[%d]=0x%08x\n", s,
803e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer[s]));
80402ac6454SAndrew Thompson 	}
80502ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
80602ac6454SAndrew Thompson 		printf("  buffer_hi[%d]=0x%08x\n", s,
807e55e1ebcSAndrew Thompson 		    hc32toh(sc, qtd->qtd_buffer_hi[s]));
80802ac6454SAndrew Thompson 	}
80902ac6454SAndrew Thompson }
81002ac6454SAndrew Thompson 
81102ac6454SAndrew Thompson static uint8_t
81202ac6454SAndrew Thompson ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
81302ac6454SAndrew Thompson {
81402ac6454SAndrew Thompson 	uint8_t temp;
81502ac6454SAndrew Thompson 
816a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sqtd->page_cache);
817e55e1ebcSAndrew Thompson 	printf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
81802ac6454SAndrew Thompson 	ehci_dump_qtd(sc, sqtd);
819e55e1ebcSAndrew Thompson 	temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
82002ac6454SAndrew Thompson 	return (temp);
82102ac6454SAndrew Thompson }
82202ac6454SAndrew Thompson 
82302ac6454SAndrew Thompson static void
82402ac6454SAndrew Thompson ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
82502ac6454SAndrew Thompson {
82602ac6454SAndrew Thompson 	uint16_t i;
82702ac6454SAndrew Thompson 	uint8_t stop;
82802ac6454SAndrew Thompson 
82902ac6454SAndrew Thompson 	stop = 0;
83002ac6454SAndrew Thompson 	for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
83102ac6454SAndrew Thompson 		stop = ehci_dump_sqtd(sc, sqtd);
83202ac6454SAndrew Thompson 	}
83302ac6454SAndrew Thompson 	if (sqtd) {
83402ac6454SAndrew Thompson 		printf("dump aborted, too many TDs\n");
83502ac6454SAndrew Thompson 	}
83602ac6454SAndrew Thompson }
83702ac6454SAndrew Thompson 
83802ac6454SAndrew Thompson static void
83902ac6454SAndrew Thompson ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
84002ac6454SAndrew Thompson {
84102ac6454SAndrew Thompson 	uint32_t endp;
84202ac6454SAndrew Thompson 	uint32_t endphub;
84302ac6454SAndrew Thompson 
844a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
845e55e1ebcSAndrew Thompson 	printf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
84602ac6454SAndrew Thompson 	printf("  link=");
84702ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_link, 1);
84802ac6454SAndrew Thompson 	printf("\n");
849e55e1ebcSAndrew Thompson 	endp = hc32toh(sc, qh->qh_endp);
85002ac6454SAndrew Thompson 	printf("  endp=0x%08x\n", endp);
85102ac6454SAndrew Thompson 	printf("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
85202ac6454SAndrew Thompson 	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
85302ac6454SAndrew Thompson 	    EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
85402ac6454SAndrew Thompson 	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
85502ac6454SAndrew Thompson 	printf("    mpl=0x%x ctl=%d nrl=%d\n",
85602ac6454SAndrew Thompson 	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
85702ac6454SAndrew Thompson 	    EHCI_QH_GET_NRL(endp));
858e55e1ebcSAndrew Thompson 	endphub = hc32toh(sc, qh->qh_endphub);
85902ac6454SAndrew Thompson 	printf("  endphub=0x%08x\n", endphub);
86002ac6454SAndrew Thompson 	printf("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
86102ac6454SAndrew Thompson 	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
86202ac6454SAndrew Thompson 	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
86302ac6454SAndrew Thompson 	    EHCI_QH_GET_MULT(endphub));
86402ac6454SAndrew Thompson 	printf("  curqtd=");
86502ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_curqtd, 0);
86602ac6454SAndrew Thompson 	printf("\n");
86702ac6454SAndrew Thompson 	printf("Overlay qTD:\n");
86802ac6454SAndrew Thompson 	ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
86902ac6454SAndrew Thompson }
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson static void
87202ac6454SAndrew Thompson ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
87302ac6454SAndrew Thompson {
874a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sitd->page_cache);
875e55e1ebcSAndrew Thompson 	printf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
876e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
87702ac6454SAndrew Thompson 	printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
878e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_portaddr),
879e55e1ebcSAndrew Thompson 	    (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
88002ac6454SAndrew Thompson 	    ? "in" : "out",
881e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
882e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
883e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
884e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
885e55e1ebcSAndrew Thompson 	printf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
886e55e1ebcSAndrew Thompson 	printf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
887e55e1ebcSAndrew Thompson 	    (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
888e55e1ebcSAndrew Thompson 	    EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
88902ac6454SAndrew Thompson 	printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
890e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_back),
891e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[0]),
892e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp[1]),
893e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[0]),
894e55e1ebcSAndrew Thompson 	    hc32toh(sc, sitd->sitd_bp_hi[1]));
89502ac6454SAndrew Thompson }
89602ac6454SAndrew Thompson 
89702ac6454SAndrew Thompson static void
89802ac6454SAndrew Thompson ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
89902ac6454SAndrew Thompson {
900a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(itd->page_cache);
901e55e1ebcSAndrew Thompson 	printf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
902e55e1ebcSAndrew Thompson 	printf(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
903e55e1ebcSAndrew Thompson 	printf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
904e55e1ebcSAndrew Thompson 	    (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
905e55e1ebcSAndrew Thompson 	printf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
906e55e1ebcSAndrew Thompson 	    (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
907e55e1ebcSAndrew Thompson 	printf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
908e55e1ebcSAndrew Thompson 	    (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
909e55e1ebcSAndrew Thompson 	printf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
910e55e1ebcSAndrew Thompson 	    (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
911e55e1ebcSAndrew Thompson 	printf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
912e55e1ebcSAndrew Thompson 	    (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
913e55e1ebcSAndrew Thompson 	printf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
914e55e1ebcSAndrew Thompson 	    (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
915e55e1ebcSAndrew Thompson 	printf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
916e55e1ebcSAndrew Thompson 	    (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
917e55e1ebcSAndrew Thompson 	printf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
918e55e1ebcSAndrew Thompson 	    (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
919e55e1ebcSAndrew Thompson 	printf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
92002ac6454SAndrew Thompson 	printf("  addr=0x%02x; endpt=0x%01x\n",
921e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
922e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
923e55e1ebcSAndrew Thompson 	printf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
92402ac6454SAndrew Thompson 	printf(" dir=%s; mpl=0x%02x\n",
925e55e1ebcSAndrew Thompson 	    (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
926e55e1ebcSAndrew Thompson 	    EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
92702ac6454SAndrew Thompson 	printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
928e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[2]),
929e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[3]),
930e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[4]),
931e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[5]),
932e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp[6]));
93302ac6454SAndrew Thompson 	printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
93402ac6454SAndrew Thompson 	    "       0x%08x,0x%08x,0x%08x\n",
935e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[0]),
936e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[1]),
937e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[2]),
938e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[3]),
939e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[4]),
940e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[5]),
941e55e1ebcSAndrew Thompson 	    hc32toh(sc, itd->itd_bp_hi[6]));
94202ac6454SAndrew Thompson }
94302ac6454SAndrew Thompson 
94402ac6454SAndrew Thompson static void
94502ac6454SAndrew Thompson ehci_dump_isoc(ehci_softc_t *sc)
94602ac6454SAndrew Thompson {
94702ac6454SAndrew Thompson 	ehci_itd_t *itd;
94802ac6454SAndrew Thompson 	ehci_sitd_t *sitd;
94902ac6454SAndrew Thompson 	uint16_t max = 1000;
95002ac6454SAndrew Thompson 	uint16_t pos;
95102ac6454SAndrew Thompson 
95202ac6454SAndrew Thompson 	pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
95302ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
95402ac6454SAndrew Thompson 
95502ac6454SAndrew Thompson 	printf("%s: isochronous dump from frame 0x%03x:\n",
95602ac6454SAndrew Thompson 	    __FUNCTION__, pos);
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson 	itd = sc->sc_isoc_hs_p_last[pos];
95902ac6454SAndrew Thompson 	sitd = sc->sc_isoc_fs_p_last[pos];
96002ac6454SAndrew Thompson 
96102ac6454SAndrew Thompson 	while (itd && max && max--) {
96202ac6454SAndrew Thompson 		ehci_dump_itd(sc, itd);
96302ac6454SAndrew Thompson 		itd = itd->prev;
96402ac6454SAndrew Thompson 	}
96502ac6454SAndrew Thompson 
96602ac6454SAndrew Thompson 	while (sitd && max && max--) {
96702ac6454SAndrew Thompson 		ehci_dump_sitd(sc, sitd);
96802ac6454SAndrew Thompson 		sitd = sitd->prev;
96902ac6454SAndrew Thompson 	}
97002ac6454SAndrew Thompson }
97102ac6454SAndrew Thompson 
97202ac6454SAndrew Thompson #endif
97302ac6454SAndrew Thompson 
97402ac6454SAndrew Thompson static void
975760bc48eSAndrew Thompson ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
97602ac6454SAndrew Thompson {
97702ac6454SAndrew Thompson 	/* check for early completion */
97802ac6454SAndrew Thompson 	if (ehci_check_transfer(xfer)) {
97902ac6454SAndrew Thompson 		return;
98002ac6454SAndrew Thompson 	}
98102ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
982a593f6b8SAndrew Thompson 	usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 	/* start timeout, if any */
98502ac6454SAndrew Thompson 	if (xfer->timeout != 0) {
986a593f6b8SAndrew Thompson 		usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
98702ac6454SAndrew Thompson 	}
98802ac6454SAndrew Thompson }
98902ac6454SAndrew Thompson 
99002ac6454SAndrew Thompson #define	EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
99102ac6454SAndrew Thompson static ehci_sitd_t *
99202ac6454SAndrew Thompson _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
99302ac6454SAndrew Thompson {
99402ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
99502ac6454SAndrew Thompson 
99602ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
99702ac6454SAndrew Thompson 
99802ac6454SAndrew Thompson 	std->next = last->next;
99902ac6454SAndrew Thompson 	std->sitd_next = last->sitd_next;
100002ac6454SAndrew Thompson 
100102ac6454SAndrew Thompson 	std->prev = last;
100202ac6454SAndrew Thompson 
1003a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
100402ac6454SAndrew Thompson 
100502ac6454SAndrew Thompson 	/*
100602ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
100702ac6454SAndrew Thompson 	 */
100802ac6454SAndrew Thompson 	last->next = std;
100902ac6454SAndrew Thompson 	last->sitd_next = std->sitd_self;
101002ac6454SAndrew Thompson 
1011a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
101202ac6454SAndrew Thompson 
101302ac6454SAndrew Thompson 	return (std);
101402ac6454SAndrew Thompson }
101502ac6454SAndrew Thompson 
101602ac6454SAndrew Thompson #define	EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
101702ac6454SAndrew Thompson static ehci_itd_t *
101802ac6454SAndrew Thompson _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
101902ac6454SAndrew Thompson {
102002ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
102102ac6454SAndrew Thompson 
102202ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
102302ac6454SAndrew Thompson 
102402ac6454SAndrew Thompson 	std->next = last->next;
102502ac6454SAndrew Thompson 	std->itd_next = last->itd_next;
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson 	std->prev = last;
102802ac6454SAndrew Thompson 
1029a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
103002ac6454SAndrew Thompson 
103102ac6454SAndrew Thompson 	/*
103202ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
103302ac6454SAndrew Thompson 	 */
103402ac6454SAndrew Thompson 	last->next = std;
103502ac6454SAndrew Thompson 	last->itd_next = std->itd_self;
103602ac6454SAndrew Thompson 
1037a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
103802ac6454SAndrew Thompson 
103902ac6454SAndrew Thompson 	return (std);
104002ac6454SAndrew Thompson }
104102ac6454SAndrew Thompson 
104202ac6454SAndrew Thompson #define	EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
104302ac6454SAndrew Thompson static ehci_qh_t *
104402ac6454SAndrew Thompson _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
104502ac6454SAndrew Thompson {
104602ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", sqh, last);
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson 	if (sqh->prev != NULL) {
104902ac6454SAndrew Thompson 		/* should not happen */
105002ac6454SAndrew Thompson 		DPRINTFN(0, "QH already linked!\n");
105102ac6454SAndrew Thompson 		return (last);
105202ac6454SAndrew Thompson 	}
105302ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson 	sqh->next = last->next;
105602ac6454SAndrew Thompson 	sqh->qh_link = last->qh_link;
105702ac6454SAndrew Thompson 
105802ac6454SAndrew Thompson 	sqh->prev = last;
105902ac6454SAndrew Thompson 
1060a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(sqh->page_cache);
106102ac6454SAndrew Thompson 
106202ac6454SAndrew Thompson 	/*
106302ac6454SAndrew Thompson 	 * the last->next->prev is never followed: sqh->next->prev = sqh;
106402ac6454SAndrew Thompson 	 */
106502ac6454SAndrew Thompson 
106602ac6454SAndrew Thompson 	last->next = sqh;
106702ac6454SAndrew Thompson 	last->qh_link = sqh->qh_self;
106802ac6454SAndrew Thompson 
1069a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
107002ac6454SAndrew Thompson 
107102ac6454SAndrew Thompson 	return (sqh);
107202ac6454SAndrew Thompson }
107302ac6454SAndrew Thompson 
107402ac6454SAndrew Thompson #define	EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
107502ac6454SAndrew Thompson static ehci_sitd_t *
107602ac6454SAndrew Thompson _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
107702ac6454SAndrew Thompson {
107802ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
107902ac6454SAndrew Thompson 
108002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
108102ac6454SAndrew Thompson 
108202ac6454SAndrew Thompson 	std->prev->next = std->next;
108302ac6454SAndrew Thompson 	std->prev->sitd_next = std->sitd_next;
108402ac6454SAndrew Thompson 
1085a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
108602ac6454SAndrew Thompson 
108702ac6454SAndrew Thompson 	if (std->next) {
108802ac6454SAndrew Thompson 		std->next->prev = std->prev;
1089a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
109002ac6454SAndrew Thompson 	}
109102ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
109202ac6454SAndrew Thompson }
109302ac6454SAndrew Thompson 
109402ac6454SAndrew Thompson #define	EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
109502ac6454SAndrew Thompson static ehci_itd_t *
109602ac6454SAndrew Thompson _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
109702ac6454SAndrew Thompson {
109802ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
109902ac6454SAndrew Thompson 
110002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
110102ac6454SAndrew Thompson 
110202ac6454SAndrew Thompson 	std->prev->next = std->next;
110302ac6454SAndrew Thompson 	std->prev->itd_next = std->itd_next;
110402ac6454SAndrew Thompson 
1105a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
110602ac6454SAndrew Thompson 
110702ac6454SAndrew Thompson 	if (std->next) {
110802ac6454SAndrew Thompson 		std->next->prev = std->prev;
1109a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
111002ac6454SAndrew Thompson 	}
111102ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
111202ac6454SAndrew Thompson }
111302ac6454SAndrew Thompson 
111402ac6454SAndrew Thompson #define	EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
111502ac6454SAndrew Thompson static ehci_qh_t *
111602ac6454SAndrew Thompson _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
111702ac6454SAndrew Thompson {
111802ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", sqh, last);
111902ac6454SAndrew Thompson 
112002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
112102ac6454SAndrew Thompson 
112202ac6454SAndrew Thompson 	/* only remove if not removed from a queue */
112302ac6454SAndrew Thompson 	if (sqh->prev) {
112402ac6454SAndrew Thompson 
112502ac6454SAndrew Thompson 		sqh->prev->next = sqh->next;
112602ac6454SAndrew Thompson 		sqh->prev->qh_link = sqh->qh_link;
112702ac6454SAndrew Thompson 
1128a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->prev->page_cache);
112902ac6454SAndrew Thompson 
113002ac6454SAndrew Thompson 		if (sqh->next) {
113102ac6454SAndrew Thompson 			sqh->next->prev = sqh->prev;
1132a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(sqh->next->page_cache);
113302ac6454SAndrew Thompson 		}
113402ac6454SAndrew Thompson 		last = ((last == sqh) ? sqh->prev : last);
113502ac6454SAndrew Thompson 
113602ac6454SAndrew Thompson 		sqh->prev = 0;
113702ac6454SAndrew Thompson 
1138a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->page_cache);
113902ac6454SAndrew Thompson 	}
114002ac6454SAndrew Thompson 	return (last);
114102ac6454SAndrew Thompson }
114202ac6454SAndrew Thompson 
1143e0a69b51SAndrew Thompson static usb_error_t
1144760bc48eSAndrew Thompson ehci_non_isoc_done_sub(struct usb_xfer *xfer)
114502ac6454SAndrew Thompson {
114602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
114702ac6454SAndrew Thompson 	ehci_qtd_t *td;
114802ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
114902ac6454SAndrew Thompson 	uint32_t status;
115002ac6454SAndrew Thompson 	uint16_t len;
115102ac6454SAndrew Thompson 
115202ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
115302ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
115402ac6454SAndrew Thompson 
115502ac6454SAndrew Thompson 	if (xfer->aframes != xfer->nframes) {
1156ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
115702ac6454SAndrew Thompson 	}
115802ac6454SAndrew Thompson 	while (1) {
115902ac6454SAndrew Thompson 
1160a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1161e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->qtd_status);
116202ac6454SAndrew Thompson 
116302ac6454SAndrew Thompson 		len = EHCI_QTD_GET_BYTES(status);
116402ac6454SAndrew Thompson 
116502ac6454SAndrew Thompson 		/*
116602ac6454SAndrew Thompson 	         * Verify the status length and
116702ac6454SAndrew Thompson 		 * add the length to "frlengths[]":
116802ac6454SAndrew Thompson 	         */
116902ac6454SAndrew Thompson 		if (len > td->len) {
117002ac6454SAndrew Thompson 			/* should not happen */
117102ac6454SAndrew Thompson 			DPRINTF("Invalid status length, "
117202ac6454SAndrew Thompson 			    "0x%04x/0x%04x bytes\n", len, td->len);
117302ac6454SAndrew Thompson 			status |= EHCI_QTD_HALTED;
117402ac6454SAndrew Thompson 		} else if (xfer->aframes != xfer->nframes) {
117502ac6454SAndrew Thompson 			xfer->frlengths[xfer->aframes] += td->len - len;
117602ac6454SAndrew Thompson 		}
117702ac6454SAndrew Thompson 		/* Check for last transfer */
117802ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
117902ac6454SAndrew Thompson 			td = NULL;
118002ac6454SAndrew Thompson 			break;
118102ac6454SAndrew Thompson 		}
118202ac6454SAndrew Thompson 		/* Check for transfer error */
118302ac6454SAndrew Thompson 		if (status & EHCI_QTD_HALTED) {
118402ac6454SAndrew Thompson 			/* the transfer is finished */
118502ac6454SAndrew Thompson 			td = NULL;
118602ac6454SAndrew Thompson 			break;
118702ac6454SAndrew Thompson 		}
118802ac6454SAndrew Thompson 		/* Check for short transfer */
118902ac6454SAndrew Thompson 		if (len > 0) {
119002ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
119102ac6454SAndrew Thompson 				/* follow alt next */
119202ac6454SAndrew Thompson 				td = td->alt_next;
119302ac6454SAndrew Thompson 			} else {
119402ac6454SAndrew Thompson 				/* the transfer is finished */
119502ac6454SAndrew Thompson 				td = NULL;
119602ac6454SAndrew Thompson 			}
119702ac6454SAndrew Thompson 			break;
119802ac6454SAndrew Thompson 		}
119902ac6454SAndrew Thompson 		td = td->obj_next;
120002ac6454SAndrew Thompson 
120102ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
120202ac6454SAndrew Thompson 			/* this USB frame is complete */
120302ac6454SAndrew Thompson 			break;
120402ac6454SAndrew Thompson 		}
120502ac6454SAndrew Thompson 	}
120602ac6454SAndrew Thompson 
120702ac6454SAndrew Thompson 	/* update transfer cache */
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
121002ac6454SAndrew Thompson 
121102ac6454SAndrew Thompson #if USB_DEBUG
121202ac6454SAndrew Thompson 	if (status & EHCI_QTD_STATERRS) {
121302ac6454SAndrew Thompson 		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
121402ac6454SAndrew Thompson 		    "status=%s%s%s%s%s%s%s%s\n",
1215ae60fdfbSAndrew Thompson 		    xfer->address, xfer->endpointno, xfer->aframes,
121602ac6454SAndrew Thompson 		    (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
121702ac6454SAndrew Thompson 		    (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
121802ac6454SAndrew Thompson 		    (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
121902ac6454SAndrew Thompson 		    (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
122002ac6454SAndrew Thompson 		    (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
122102ac6454SAndrew Thompson 		    (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
122202ac6454SAndrew Thompson 		    (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
122302ac6454SAndrew Thompson 		    (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
122402ac6454SAndrew Thompson 	}
122502ac6454SAndrew Thompson #endif
122602ac6454SAndrew Thompson 
122702ac6454SAndrew Thompson 	return ((status & EHCI_QTD_HALTED) ?
122802ac6454SAndrew Thompson 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
122902ac6454SAndrew Thompson }
123002ac6454SAndrew Thompson 
123102ac6454SAndrew Thompson static void
1232760bc48eSAndrew Thompson ehci_non_isoc_done(struct usb_xfer *xfer)
123302ac6454SAndrew Thompson {
1234c8a14b91SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
1235c8a14b91SAndrew Thompson 	ehci_qh_t *qh;
1236c8a14b91SAndrew Thompson 	uint32_t status;
1237e0a69b51SAndrew Thompson 	usb_error_t err = 0;
123802ac6454SAndrew Thompson 
1239ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1240ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
124102ac6454SAndrew Thompson 
124202ac6454SAndrew Thompson #if USB_DEBUG
124302ac6454SAndrew Thompson 	if (ehcidebug > 10) {
124402ac6454SAndrew Thompson 		ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
124502ac6454SAndrew Thompson 
124602ac6454SAndrew Thompson 		ehci_dump_sqtds(sc, xfer->td_transfer_first);
124702ac6454SAndrew Thompson 	}
124802ac6454SAndrew Thompson #endif
124902ac6454SAndrew Thompson 
1250c8a14b91SAndrew Thompson 	/* extract data toggle directly from the QH's overlay area */
1251c8a14b91SAndrew Thompson 
1252c8a14b91SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1253c8a14b91SAndrew Thompson 
1254c8a14b91SAndrew Thompson 	usb_pc_cpu_invalidate(qh->page_cache);
1255c8a14b91SAndrew Thompson 
1256c8a14b91SAndrew Thompson 	status = hc32toh(sc, qh->qh_qtd.qtd_status);
1257c8a14b91SAndrew Thompson 
1258c8a14b91SAndrew Thompson 	xfer->endpoint->toggle_next =
1259c8a14b91SAndrew Thompson 	    (status & EHCI_QTD_TOGGLE_MASK) ? 1 : 0;
1260c8a14b91SAndrew Thompson 
126102ac6454SAndrew Thompson 	/* reset scanner */
126202ac6454SAndrew Thompson 
126302ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
126402ac6454SAndrew Thompson 
126502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
126602ac6454SAndrew Thompson 
126702ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
126802ac6454SAndrew Thompson 
126902ac6454SAndrew Thompson 			err = ehci_non_isoc_done_sub(xfer);
127002ac6454SAndrew Thompson 		}
127102ac6454SAndrew Thompson 		xfer->aframes = 1;
127202ac6454SAndrew Thompson 
127302ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
127402ac6454SAndrew Thompson 			goto done;
127502ac6454SAndrew Thompson 		}
127602ac6454SAndrew Thompson 	}
127702ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
127802ac6454SAndrew Thompson 
127902ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
128002ac6454SAndrew Thompson 		xfer->aframes++;
128102ac6454SAndrew Thompson 
128202ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
128302ac6454SAndrew Thompson 			goto done;
128402ac6454SAndrew Thompson 		}
128502ac6454SAndrew Thompson 	}
128602ac6454SAndrew Thompson 
128702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
128802ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
128902ac6454SAndrew Thompson 
129002ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
129102ac6454SAndrew Thompson 	}
129202ac6454SAndrew Thompson done:
129302ac6454SAndrew Thompson 	ehci_device_done(xfer, err);
129402ac6454SAndrew Thompson }
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
129702ac6454SAndrew Thompson  *	ehci_check_transfer
129802ac6454SAndrew Thompson  *
129902ac6454SAndrew Thompson  * Return values:
130002ac6454SAndrew Thompson  *    0: USB transfer is not finished
130102ac6454SAndrew Thompson  * Else: USB transfer is finished
130202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
130302ac6454SAndrew Thompson static uint8_t
1304760bc48eSAndrew Thompson ehci_check_transfer(struct usb_xfer *xfer)
130502ac6454SAndrew Thompson {
1306ae60fdfbSAndrew Thompson 	struct usb_pipe_methods *methods = xfer->endpoint->methods;
130702ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
130802ac6454SAndrew Thompson 
130902ac6454SAndrew Thompson 	uint32_t status;
131002ac6454SAndrew Thompson 
131102ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
131202ac6454SAndrew Thompson 
131302ac6454SAndrew Thompson 	if (methods == &ehci_device_isoc_fs_methods) {
131402ac6454SAndrew Thompson 		ehci_sitd_t *td;
131502ac6454SAndrew Thompson 
131602ac6454SAndrew Thompson 		/* isochronous full speed transfer */
131702ac6454SAndrew Thompson 
131802ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1319a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1320e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
132102ac6454SAndrew Thompson 
132202ac6454SAndrew Thompson 		/* also check if first is complete */
132302ac6454SAndrew Thompson 
132402ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1325a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
1326e55e1ebcSAndrew Thompson 		status |= hc32toh(sc, td->sitd_status);
132702ac6454SAndrew Thompson 
132802ac6454SAndrew Thompson 		if (!(status & EHCI_SITD_ACTIVE)) {
132902ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
133002ac6454SAndrew Thompson 			goto transferred;
133102ac6454SAndrew Thompson 		}
133202ac6454SAndrew Thompson 	} else if (methods == &ehci_device_isoc_hs_methods) {
133302ac6454SAndrew Thompson 		ehci_itd_t *td;
133402ac6454SAndrew Thompson 
133502ac6454SAndrew Thompson 		/* isochronous high speed transfer */
133602ac6454SAndrew Thompson 
133702ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
1338a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
133902ac6454SAndrew Thompson 		status =
134002ac6454SAndrew Thompson 		    td->itd_status[0] | td->itd_status[1] |
134102ac6454SAndrew Thompson 		    td->itd_status[2] | td->itd_status[3] |
134202ac6454SAndrew Thompson 		    td->itd_status[4] | td->itd_status[5] |
134302ac6454SAndrew Thompson 		    td->itd_status[6] | td->itd_status[7];
134402ac6454SAndrew Thompson 
134502ac6454SAndrew Thompson 		/* also check first transfer */
134602ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
1347a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
134802ac6454SAndrew Thompson 		status |=
134902ac6454SAndrew Thompson 		    td->itd_status[0] | td->itd_status[1] |
135002ac6454SAndrew Thompson 		    td->itd_status[2] | td->itd_status[3] |
135102ac6454SAndrew Thompson 		    td->itd_status[4] | td->itd_status[5] |
135202ac6454SAndrew Thompson 		    td->itd_status[6] | td->itd_status[7];
135302ac6454SAndrew Thompson 
135402ac6454SAndrew Thompson 		/* if no transactions are active we continue */
1355e55e1ebcSAndrew Thompson 		if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
135602ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
135702ac6454SAndrew Thompson 			goto transferred;
135802ac6454SAndrew Thompson 		}
135902ac6454SAndrew Thompson 	} else {
136002ac6454SAndrew Thompson 		ehci_qtd_t *td;
1361c8a14b91SAndrew Thompson 		ehci_qh_t *qh;
136202ac6454SAndrew Thompson 
136302ac6454SAndrew Thompson 		/* non-isochronous transfer */
136402ac6454SAndrew Thompson 
136502ac6454SAndrew Thompson 		/*
136602ac6454SAndrew Thompson 		 * check whether there is an error somewhere in the middle,
136702ac6454SAndrew Thompson 		 * or whether there was a short packet (SPD and not ACTIVE)
136802ac6454SAndrew Thompson 		 */
136902ac6454SAndrew Thompson 		td = xfer->td_transfer_cache;
137002ac6454SAndrew Thompson 
1371c8a14b91SAndrew Thompson 		qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1372c8a14b91SAndrew Thompson 
1373c8a14b91SAndrew Thompson 		usb_pc_cpu_invalidate(qh->page_cache);
1374c8a14b91SAndrew Thompson 
1375c8a14b91SAndrew Thompson 		status = hc32toh(sc, qh->qh_qtd.qtd_status);
1376c8a14b91SAndrew Thompson 		if (status & EHCI_QTD_ACTIVE) {
1377c8a14b91SAndrew Thompson 			/* transfer is pending */
1378c8a14b91SAndrew Thompson 			goto done;
1379c8a14b91SAndrew Thompson 		}
1380c8a14b91SAndrew Thompson 
138102ac6454SAndrew Thompson 		while (1) {
1382a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(td->page_cache);
1383e55e1ebcSAndrew Thompson 			status = hc32toh(sc, td->qtd_status);
138402ac6454SAndrew Thompson 
138502ac6454SAndrew Thompson 			/*
1386c8a14b91SAndrew Thompson 			 * Check if there is an active TD which
1387c8a14b91SAndrew Thompson 			 * indicates that the transfer isn't done.
138802ac6454SAndrew Thompson 			 */
138902ac6454SAndrew Thompson 			if (status & EHCI_QTD_ACTIVE) {
139002ac6454SAndrew Thompson 				/* update cache */
1391c8a14b91SAndrew Thompson 				if (xfer->td_transfer_cache != td) {
139202ac6454SAndrew Thompson 					xfer->td_transfer_cache = td;
1393c8a14b91SAndrew Thompson 					if (qh->qh_qtd.qtd_next &
1394c8a14b91SAndrew Thompson 					    htohc32(sc, EHCI_LINK_TERMINATE)) {
1395c8a14b91SAndrew Thompson 						/* XXX - manually advance to next frame */
1396c8a14b91SAndrew Thompson 						qh->qh_qtd.qtd_next = td->qtd_self;
1397c8a14b91SAndrew Thompson 						usb_pc_cpu_flush(td->page_cache);
1398c8a14b91SAndrew Thompson 					}
1399c8a14b91SAndrew Thompson 				}
140002ac6454SAndrew Thompson 				goto done;
140102ac6454SAndrew Thompson 			}
140202ac6454SAndrew Thompson 			/*
140302ac6454SAndrew Thompson 			 * last transfer descriptor makes the transfer done
140402ac6454SAndrew Thompson 			 */
140502ac6454SAndrew Thompson 			if (((void *)td) == xfer->td_transfer_last) {
140602ac6454SAndrew Thompson 				break;
140702ac6454SAndrew Thompson 			}
140802ac6454SAndrew Thompson 			/*
140902ac6454SAndrew Thompson 			 * any kind of error makes the transfer done
141002ac6454SAndrew Thompson 			 */
141102ac6454SAndrew Thompson 			if (status & EHCI_QTD_HALTED) {
141202ac6454SAndrew Thompson 				break;
141302ac6454SAndrew Thompson 			}
141402ac6454SAndrew Thompson 			/*
141502ac6454SAndrew Thompson 			 * if there is no alternate next transfer, a short
141602ac6454SAndrew Thompson 			 * packet also makes the transfer done
141702ac6454SAndrew Thompson 			 */
141802ac6454SAndrew Thompson 			if (EHCI_QTD_GET_BYTES(status)) {
141902ac6454SAndrew Thompson 				if (xfer->flags_int.short_frames_ok) {
142002ac6454SAndrew Thompson 					/* follow alt next */
142102ac6454SAndrew Thompson 					if (td->alt_next) {
142202ac6454SAndrew Thompson 						td = td->alt_next;
142302ac6454SAndrew Thompson 						continue;
142402ac6454SAndrew Thompson 					}
142502ac6454SAndrew Thompson 				}
142602ac6454SAndrew Thompson 				/* transfer is done */
142702ac6454SAndrew Thompson 				break;
142802ac6454SAndrew Thompson 			}
142902ac6454SAndrew Thompson 			td = td->obj_next;
143002ac6454SAndrew Thompson 		}
143102ac6454SAndrew Thompson 		ehci_non_isoc_done(xfer);
143202ac6454SAndrew Thompson 		goto transferred;
143302ac6454SAndrew Thompson 	}
143402ac6454SAndrew Thompson 
143502ac6454SAndrew Thompson done:
143602ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
143702ac6454SAndrew Thompson 	return (0);
143802ac6454SAndrew Thompson 
143902ac6454SAndrew Thompson transferred:
144002ac6454SAndrew Thompson 	return (1);
144102ac6454SAndrew Thompson }
144202ac6454SAndrew Thompson 
144302ac6454SAndrew Thompson static void
144402ac6454SAndrew Thompson ehci_pcd_enable(ehci_softc_t *sc)
144502ac6454SAndrew Thompson {
144602ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
144702ac6454SAndrew Thompson 
144802ac6454SAndrew Thompson 	sc->sc_eintrs |= EHCI_STS_PCD;
144902ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
145002ac6454SAndrew Thompson 
145102ac6454SAndrew Thompson 	/* acknowledge any PCD interrupt */
145202ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
145302ac6454SAndrew Thompson 
145439307315SAndrew Thompson 	ehci_root_intr(sc);
145502ac6454SAndrew Thompson }
145602ac6454SAndrew Thompson 
145702ac6454SAndrew Thompson static void
145802ac6454SAndrew Thompson ehci_interrupt_poll(ehci_softc_t *sc)
145902ac6454SAndrew Thompson {
1460760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
146102ac6454SAndrew Thompson 
146202ac6454SAndrew Thompson repeat:
146302ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
146402ac6454SAndrew Thompson 		/*
146502ac6454SAndrew Thompson 		 * check if transfer is transferred
146602ac6454SAndrew Thompson 		 */
146702ac6454SAndrew Thompson 		if (ehci_check_transfer(xfer)) {
146802ac6454SAndrew Thompson 			/* queue has been modified */
146902ac6454SAndrew Thompson 			goto repeat;
147002ac6454SAndrew Thompson 		}
147102ac6454SAndrew Thompson 	}
147202ac6454SAndrew Thompson }
147302ac6454SAndrew Thompson 
147402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
147502ac6454SAndrew Thompson  *	ehci_interrupt - EHCI interrupt handler
147602ac6454SAndrew Thompson  *
147702ac6454SAndrew Thompson  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
147802ac6454SAndrew Thompson  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
147902ac6454SAndrew Thompson  * is present !
148002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
148102ac6454SAndrew Thompson void
148202ac6454SAndrew Thompson ehci_interrupt(ehci_softc_t *sc)
148302ac6454SAndrew Thompson {
148402ac6454SAndrew Thompson 	uint32_t status;
148502ac6454SAndrew Thompson 
148602ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
148702ac6454SAndrew Thompson 
148802ac6454SAndrew Thompson 	DPRINTFN(16, "real interrupt\n");
148902ac6454SAndrew Thompson 
149002ac6454SAndrew Thompson #if USB_DEBUG
149102ac6454SAndrew Thompson 	if (ehcidebug > 15) {
149202ac6454SAndrew Thompson 		ehci_dump_regs(sc);
149302ac6454SAndrew Thompson 	}
149402ac6454SAndrew Thompson #endif
149502ac6454SAndrew Thompson 
149602ac6454SAndrew Thompson 	status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
149702ac6454SAndrew Thompson 	if (status == 0) {
149802ac6454SAndrew Thompson 		/* the interrupt was not for us */
149902ac6454SAndrew Thompson 		goto done;
150002ac6454SAndrew Thompson 	}
150102ac6454SAndrew Thompson 	if (!(status & sc->sc_eintrs)) {
150202ac6454SAndrew Thompson 		goto done;
150302ac6454SAndrew Thompson 	}
150402ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, status);	/* acknowledge */
150502ac6454SAndrew Thompson 
150602ac6454SAndrew Thompson 	status &= sc->sc_eintrs;
150702ac6454SAndrew Thompson 
150802ac6454SAndrew Thompson 	if (status & EHCI_STS_HSE) {
150902ac6454SAndrew Thompson 		printf("%s: unrecoverable error, "
151002ac6454SAndrew Thompson 		    "controller halted\n", __FUNCTION__);
151102ac6454SAndrew Thompson #if USB_DEBUG
151202ac6454SAndrew Thompson 		ehci_dump_regs(sc);
151302ac6454SAndrew Thompson 		ehci_dump_isoc(sc);
151402ac6454SAndrew Thompson #endif
151502ac6454SAndrew Thompson 	}
151602ac6454SAndrew Thompson 	if (status & EHCI_STS_PCD) {
151702ac6454SAndrew Thompson 		/*
151802ac6454SAndrew Thompson 		 * Disable PCD interrupt for now, because it will be
151902ac6454SAndrew Thompson 		 * on until the port has been reset.
152002ac6454SAndrew Thompson 		 */
152102ac6454SAndrew Thompson 		sc->sc_eintrs &= ~EHCI_STS_PCD;
152202ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
152302ac6454SAndrew Thompson 
152439307315SAndrew Thompson 		ehci_root_intr(sc);
152502ac6454SAndrew Thompson 
152602ac6454SAndrew Thompson 		/* do not allow RHSC interrupts > 1 per second */
1527a593f6b8SAndrew Thompson 		usb_callout_reset(&sc->sc_tmo_pcd, hz,
152802ac6454SAndrew Thompson 		    (void *)&ehci_pcd_enable, sc);
152902ac6454SAndrew Thompson 	}
153002ac6454SAndrew Thompson 	status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
153102ac6454SAndrew Thompson 
153202ac6454SAndrew Thompson 	if (status != 0) {
153302ac6454SAndrew Thompson 		/* block unprocessed interrupts */
153402ac6454SAndrew Thompson 		sc->sc_eintrs &= ~status;
153502ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
153602ac6454SAndrew Thompson 		printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
153702ac6454SAndrew Thompson 	}
153802ac6454SAndrew Thompson 	/* poll all the USB transfers */
153902ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
154002ac6454SAndrew Thompson 
154102ac6454SAndrew Thompson done:
154202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
154302ac6454SAndrew Thompson }
154402ac6454SAndrew Thompson 
154502ac6454SAndrew Thompson /*
154602ac6454SAndrew Thompson  * called when a request does not complete
154702ac6454SAndrew Thompson  */
154802ac6454SAndrew Thompson static void
154902ac6454SAndrew Thompson ehci_timeout(void *arg)
155002ac6454SAndrew Thompson {
1551760bc48eSAndrew Thompson 	struct usb_xfer *xfer = arg;
155202ac6454SAndrew Thompson 
155302ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
155402ac6454SAndrew Thompson 
155502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
155602ac6454SAndrew Thompson 
155702ac6454SAndrew Thompson 	/* transfer is transferred */
155802ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_TIMEOUT);
155902ac6454SAndrew Thompson }
156002ac6454SAndrew Thompson 
156102ac6454SAndrew Thompson static void
1562760bc48eSAndrew Thompson ehci_do_poll(struct usb_bus *bus)
156302ac6454SAndrew Thompson {
156402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
156502ac6454SAndrew Thompson 
156602ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
156702ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
156802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
156902ac6454SAndrew Thompson }
157002ac6454SAndrew Thompson 
157102ac6454SAndrew Thompson static void
157202ac6454SAndrew Thompson ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
157302ac6454SAndrew Thompson {
1574760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
157502ac6454SAndrew Thompson 	ehci_qtd_t *td;
157602ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
157702ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
157802ac6454SAndrew Thompson 	uint32_t buf_offset;
157902ac6454SAndrew Thompson 	uint32_t average;
158002ac6454SAndrew Thompson 	uint32_t len_old;
15810f7d4548SAndrew Thompson 	uint32_t terminate;
158202ac6454SAndrew Thompson 	uint8_t shortpkt_old;
158302ac6454SAndrew Thompson 	uint8_t precompute;
158402ac6454SAndrew Thompson 
15850f7d4548SAndrew Thompson 	terminate = htohc32(temp->sc, EHCI_LINK_TERMINATE);
158602ac6454SAndrew Thompson 	td_alt_next = NULL;
158702ac6454SAndrew Thompson 	buf_offset = 0;
158802ac6454SAndrew Thompson 	shortpkt_old = temp->shortpkt;
158902ac6454SAndrew Thompson 	len_old = temp->len;
159002ac6454SAndrew Thompson 	precompute = 1;
159102ac6454SAndrew Thompson 
159202ac6454SAndrew Thompson restart:
159302ac6454SAndrew Thompson 
159402ac6454SAndrew Thompson 	td = temp->td;
159502ac6454SAndrew Thompson 	td_next = temp->td_next;
159602ac6454SAndrew Thompson 
159702ac6454SAndrew Thompson 	while (1) {
159802ac6454SAndrew Thompson 
159902ac6454SAndrew Thompson 		if (temp->len == 0) {
160002ac6454SAndrew Thompson 
160102ac6454SAndrew Thompson 			if (temp->shortpkt) {
160202ac6454SAndrew Thompson 				break;
160302ac6454SAndrew Thompson 			}
160402ac6454SAndrew Thompson 			/* send a Zero Length Packet, ZLP, last */
160502ac6454SAndrew Thompson 
160602ac6454SAndrew Thompson 			temp->shortpkt = 1;
160702ac6454SAndrew Thompson 			average = 0;
160802ac6454SAndrew Thompson 
160902ac6454SAndrew Thompson 		} else {
161002ac6454SAndrew Thompson 
161102ac6454SAndrew Thompson 			average = temp->average;
161202ac6454SAndrew Thompson 
161302ac6454SAndrew Thompson 			if (temp->len < average) {
161402ac6454SAndrew Thompson 				if (temp->len % temp->max_frame_size) {
161502ac6454SAndrew Thompson 					temp->shortpkt = 1;
161602ac6454SAndrew Thompson 				}
161702ac6454SAndrew Thompson 				average = temp->len;
161802ac6454SAndrew Thompson 			}
161902ac6454SAndrew Thompson 		}
162002ac6454SAndrew Thompson 
162102ac6454SAndrew Thompson 		if (td_next == NULL) {
162202ac6454SAndrew Thompson 			panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
162302ac6454SAndrew Thompson 		}
162402ac6454SAndrew Thompson 		/* get next TD */
162502ac6454SAndrew Thompson 
162602ac6454SAndrew Thompson 		td = td_next;
162702ac6454SAndrew Thompson 		td_next = td->obj_next;
162802ac6454SAndrew Thompson 
162902ac6454SAndrew Thompson 		/* check if we are pre-computing */
163002ac6454SAndrew Thompson 
163102ac6454SAndrew Thompson 		if (precompute) {
163202ac6454SAndrew Thompson 
163302ac6454SAndrew Thompson 			/* update remaining length */
163402ac6454SAndrew Thompson 
163502ac6454SAndrew Thompson 			temp->len -= average;
163602ac6454SAndrew Thompson 
163702ac6454SAndrew Thompson 			continue;
163802ac6454SAndrew Thompson 		}
163902ac6454SAndrew Thompson 		/* fill out current TD */
164002ac6454SAndrew Thompson 
164102ac6454SAndrew Thompson 		td->qtd_status =
164202ac6454SAndrew Thompson 		    temp->qtd_status |
1643c8a14b91SAndrew Thompson 		    htohc32(temp->sc, EHCI_QTD_IOC |
1644c8a14b91SAndrew Thompson 			EHCI_QTD_SET_BYTES(average));
164502ac6454SAndrew Thompson 
164602ac6454SAndrew Thompson 		if (average == 0) {
164702ac6454SAndrew Thompson 
164802ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
164902ac6454SAndrew Thompson 
165002ac6454SAndrew Thompson 				/* update data toggle, ZLP case */
165102ac6454SAndrew Thompson 
165202ac6454SAndrew Thompson 				temp->qtd_status ^=
1653e55e1ebcSAndrew Thompson 				    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
165402ac6454SAndrew Thompson 			}
165502ac6454SAndrew Thompson 			td->len = 0;
165602ac6454SAndrew Thompson 
165702ac6454SAndrew Thompson 			td->qtd_buffer[0] = 0;
165802ac6454SAndrew Thompson 			td->qtd_buffer_hi[0] = 0;
165902ac6454SAndrew Thompson 
166002ac6454SAndrew Thompson 			td->qtd_buffer[1] = 0;
166102ac6454SAndrew Thompson 			td->qtd_buffer_hi[1] = 0;
166202ac6454SAndrew Thompson 
166302ac6454SAndrew Thompson 		} else {
166402ac6454SAndrew Thompson 
166502ac6454SAndrew Thompson 			uint8_t x;
166602ac6454SAndrew Thompson 
166702ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
166802ac6454SAndrew Thompson 
166902ac6454SAndrew Thompson 				/* update data toggle */
167002ac6454SAndrew Thompson 
167102ac6454SAndrew Thompson 				if (((average + temp->max_frame_size - 1) /
167202ac6454SAndrew Thompson 				    temp->max_frame_size) & 1) {
167302ac6454SAndrew Thompson 					temp->qtd_status ^=
1674e55e1ebcSAndrew Thompson 					    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
167502ac6454SAndrew Thompson 				}
167602ac6454SAndrew Thompson 			}
167702ac6454SAndrew Thompson 			td->len = average;
167802ac6454SAndrew Thompson 
167902ac6454SAndrew Thompson 			/* update remaining length */
168002ac6454SAndrew Thompson 
168102ac6454SAndrew Thompson 			temp->len -= average;
168202ac6454SAndrew Thompson 
168302ac6454SAndrew Thompson 			/* fill out buffer pointers */
168402ac6454SAndrew Thompson 
1685a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset, &buf_res);
168602ac6454SAndrew Thompson 			td->qtd_buffer[0] =
1687e55e1ebcSAndrew Thompson 			    htohc32(temp->sc, buf_res.physaddr);
168802ac6454SAndrew Thompson 			td->qtd_buffer_hi[0] = 0;
168902ac6454SAndrew Thompson 
169002ac6454SAndrew Thompson 			x = 1;
169102ac6454SAndrew Thompson 
169202ac6454SAndrew Thompson 			while (average > EHCI_PAGE_SIZE) {
169302ac6454SAndrew Thompson 				average -= EHCI_PAGE_SIZE;
169402ac6454SAndrew Thompson 				buf_offset += EHCI_PAGE_SIZE;
1695a593f6b8SAndrew Thompson 				usbd_get_page(temp->pc, buf_offset, &buf_res);
169602ac6454SAndrew Thompson 				td->qtd_buffer[x] =
1697e55e1ebcSAndrew Thompson 				    htohc32(temp->sc,
169802ac6454SAndrew Thompson 				    buf_res.physaddr & (~0xFFF));
169902ac6454SAndrew Thompson 				td->qtd_buffer_hi[x] = 0;
170002ac6454SAndrew Thompson 				x++;
170102ac6454SAndrew Thompson 			}
170202ac6454SAndrew Thompson 
170302ac6454SAndrew Thompson 			/*
170402ac6454SAndrew Thompson 			 * NOTE: The "average" variable is never zero after
170502ac6454SAndrew Thompson 			 * exiting the loop above !
170602ac6454SAndrew Thompson 			 *
170702ac6454SAndrew Thompson 			 * NOTE: We have to subtract one from the offset to
170802ac6454SAndrew Thompson 			 * ensure that we are computing the physical address
170902ac6454SAndrew Thompson 			 * of a valid page !
171002ac6454SAndrew Thompson 			 */
171102ac6454SAndrew Thompson 			buf_offset += average;
1712a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
171302ac6454SAndrew Thompson 			td->qtd_buffer[x] =
1714e55e1ebcSAndrew Thompson 			    htohc32(temp->sc,
171502ac6454SAndrew Thompson 			    buf_res.physaddr & (~0xFFF));
171602ac6454SAndrew Thompson 			td->qtd_buffer_hi[x] = 0;
171702ac6454SAndrew Thompson 		}
171802ac6454SAndrew Thompson 
1719c8a14b91SAndrew Thompson 		if (temp->can_use_next) {
172002ac6454SAndrew Thompson 			if (td_next) {
172102ac6454SAndrew Thompson 				/* link the current TD with the next one */
172202ac6454SAndrew Thompson 				td->qtd_next = td_next->qtd_self;
172302ac6454SAndrew Thompson 			}
1724c8a14b91SAndrew Thompson 		} else {
1725c8a14b91SAndrew Thompson 			/*
1726c8a14b91SAndrew Thompson 			 * BUG WARNING: The EHCI HW can use the
1727c8a14b91SAndrew Thompson 			 * qtd_next field instead of qtd_altnext when
1728c8a14b91SAndrew Thompson 			 * a short packet is received! We work this
1729c8a14b91SAndrew Thompson 			 * around in software by not queueing more
1730c8a14b91SAndrew Thompson 			 * than one job/TD at a time!
1731c8a14b91SAndrew Thompson 			 */
1732c8a14b91SAndrew Thompson 			td->qtd_next = terminate;
1733c8a14b91SAndrew Thompson 		}
1734c8a14b91SAndrew Thompson 
1735c8a14b91SAndrew Thompson 		td->qtd_altnext = terminate;
173602ac6454SAndrew Thompson 		td->alt_next = td_alt_next;
173702ac6454SAndrew Thompson 
1738a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
173902ac6454SAndrew Thompson 	}
174002ac6454SAndrew Thompson 
174102ac6454SAndrew Thompson 	if (precompute) {
174202ac6454SAndrew Thompson 		precompute = 0;
174302ac6454SAndrew Thompson 
174402ac6454SAndrew Thompson 		/* setup alt next pointer, if any */
17450f7d4548SAndrew Thompson 		if (temp->last_frame) {
17460f7d4548SAndrew Thompson 			td_alt_next = NULL;
174702ac6454SAndrew Thompson 		} else {
174802ac6454SAndrew Thompson 			/* we use this field internally */
174902ac6454SAndrew Thompson 			td_alt_next = td_next;
175002ac6454SAndrew Thompson 		}
175102ac6454SAndrew Thompson 
175202ac6454SAndrew Thompson 		/* restore */
175302ac6454SAndrew Thompson 		temp->shortpkt = shortpkt_old;
175402ac6454SAndrew Thompson 		temp->len = len_old;
175502ac6454SAndrew Thompson 		goto restart;
175602ac6454SAndrew Thompson 	}
175702ac6454SAndrew Thompson 	temp->td = td;
175802ac6454SAndrew Thompson 	temp->td_next = td_next;
175902ac6454SAndrew Thompson }
176002ac6454SAndrew Thompson 
176102ac6454SAndrew Thompson static void
1762760bc48eSAndrew Thompson ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
176302ac6454SAndrew Thompson {
176402ac6454SAndrew Thompson 	struct ehci_std_temp temp;
1765760bc48eSAndrew Thompson 	struct usb_pipe_methods *methods;
176602ac6454SAndrew Thompson 	ehci_qh_t *qh;
176702ac6454SAndrew Thompson 	ehci_qtd_t *td;
176802ac6454SAndrew Thompson 	uint32_t qh_endp;
176902ac6454SAndrew Thompson 	uint32_t qh_endphub;
177002ac6454SAndrew Thompson 	uint32_t x;
177102ac6454SAndrew Thompson 
177202ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1773ae60fdfbSAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
1774a593f6b8SAndrew Thompson 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
177502ac6454SAndrew Thompson 
1776578d0effSAndrew Thompson 	temp.average = xfer->max_hc_frame_size;
177702ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
177802ac6454SAndrew Thompson 	temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
177902ac6454SAndrew Thompson 
178002ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
178102ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
178202ac6454SAndrew Thompson 
178302ac6454SAndrew Thompson 	/* get next DMA set */
178402ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
178502ac6454SAndrew Thompson 
178602ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
178702ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
178802ac6454SAndrew Thompson 
178902ac6454SAndrew Thompson 	temp.td = NULL;
179002ac6454SAndrew Thompson 	temp.td_next = td;
179102ac6454SAndrew Thompson 	temp.qtd_status = 0;
17920f7d4548SAndrew Thompson 	temp.last_frame = 0;
179302ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1794c8a14b91SAndrew Thompson 	temp.can_use_next = (xfer->flags_int.control_xfr ||
1795c8a14b91SAndrew Thompson 	    (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT));
179602ac6454SAndrew Thompson 
179702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
1798ae60fdfbSAndrew Thompson 		if (xfer->endpoint->toggle_next) {
179902ac6454SAndrew Thompson 			/* DATA1 is next */
180002ac6454SAndrew Thompson 			temp.qtd_status |=
1801e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
180202ac6454SAndrew Thompson 		}
180302ac6454SAndrew Thompson 		temp.auto_data_toggle = 0;
180402ac6454SAndrew Thompson 	} else {
180502ac6454SAndrew Thompson 		temp.auto_data_toggle = 1;
180602ac6454SAndrew Thompson 	}
180702ac6454SAndrew Thompson 
1808a593f6b8SAndrew Thompson 	if (usbd_get_speed(xfer->xroot->udev) != USB_SPEED_HIGH) {
180902ac6454SAndrew Thompson 		/* max 3 retries */
181002ac6454SAndrew Thompson 		temp.qtd_status |=
1811e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
181202ac6454SAndrew Thompson 	}
181302ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
181402ac6454SAndrew Thompson 
181502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
181602ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
181702ac6454SAndrew Thompson 
181802ac6454SAndrew Thompson 			temp.qtd_status &=
1819e55e1ebcSAndrew Thompson 			    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
1820e55e1ebcSAndrew Thompson 			temp.qtd_status |= htohc32(temp.sc,
18215f128668SAndrew Thompson 			    EHCI_QTD_ACTIVE |
182202ac6454SAndrew Thompson 			    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
182302ac6454SAndrew Thompson 			    EHCI_QTD_SET_TOGGLE(0));
182402ac6454SAndrew Thompson 
182502ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
182602ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + 0;
182702ac6454SAndrew Thompson 			temp.shortpkt = temp.len ? 1 : 0;
18280f7d4548SAndrew Thompson 			/* check for last frame */
18290f7d4548SAndrew Thompson 			if (xfer->nframes == 1) {
18300f7d4548SAndrew Thompson 				/* no STATUS stage yet, SETUP is last */
18310f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
18320f7d4548SAndrew Thompson 					temp.last_frame = 1;
18330f7d4548SAndrew Thompson 					temp.setup_alt_next = 0;
18340f7d4548SAndrew Thompson 				}
18350f7d4548SAndrew Thompson 			}
183602ac6454SAndrew Thompson 			ehci_setup_standard_chain_sub(&temp);
183702ac6454SAndrew Thompson 		}
183802ac6454SAndrew Thompson 		x = 1;
183902ac6454SAndrew Thompson 	} else {
184002ac6454SAndrew Thompson 		x = 0;
184102ac6454SAndrew Thompson 	}
184202ac6454SAndrew Thompson 
184302ac6454SAndrew Thompson 	while (x != xfer->nframes) {
184402ac6454SAndrew Thompson 
184502ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
184602ac6454SAndrew Thompson 
184702ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
184802ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
184902ac6454SAndrew Thompson 
185002ac6454SAndrew Thompson 		x++;
185102ac6454SAndrew Thompson 
185202ac6454SAndrew Thompson 		if (x == xfer->nframes) {
18530f7d4548SAndrew Thompson 			if (xfer->flags_int.control_xfr) {
18540f7d4548SAndrew Thompson 				/* no STATUS stage yet, DATA is last */
18550f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
18560f7d4548SAndrew Thompson 					temp.last_frame = 1;
185702ac6454SAndrew Thompson 					temp.setup_alt_next = 0;
185802ac6454SAndrew Thompson 				}
18590f7d4548SAndrew Thompson 			} else {
18600f7d4548SAndrew Thompson 				temp.last_frame = 1;
18610f7d4548SAndrew Thompson 				temp.setup_alt_next = 0;
18620f7d4548SAndrew Thompson 			}
18630f7d4548SAndrew Thompson 		}
186402ac6454SAndrew Thompson 		/* keep previous data toggle and error count */
186502ac6454SAndrew Thompson 
186602ac6454SAndrew Thompson 		temp.qtd_status &=
1867e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
186802ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
186902ac6454SAndrew Thompson 
187002ac6454SAndrew Thompson 		if (temp.len == 0) {
187102ac6454SAndrew Thompson 
187202ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
187302ac6454SAndrew Thompson 
187402ac6454SAndrew Thompson 			temp.shortpkt = 0;
187502ac6454SAndrew Thompson 
187602ac6454SAndrew Thompson 		} else {
187702ac6454SAndrew Thompson 
187802ac6454SAndrew Thompson 			/* regular data transfer */
187902ac6454SAndrew Thompson 
188002ac6454SAndrew Thompson 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
188102ac6454SAndrew Thompson 		}
188202ac6454SAndrew Thompson 
188302ac6454SAndrew Thompson 		/* set endpoint direction */
188402ac6454SAndrew Thompson 
188502ac6454SAndrew Thompson 		temp.qtd_status |=
1886ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
1887e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
188802ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
1889e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
189002ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
189102ac6454SAndrew Thompson 
189202ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
189302ac6454SAndrew Thompson 	}
189402ac6454SAndrew Thompson 
189502ac6454SAndrew Thompson 	/* check if we should append a status stage */
189602ac6454SAndrew Thompson 
189702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
189802ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
189902ac6454SAndrew Thompson 
190002ac6454SAndrew Thompson 		/*
190102ac6454SAndrew Thompson 		 * Send a DATA1 message and invert the current endpoint
190202ac6454SAndrew Thompson 		 * direction.
190302ac6454SAndrew Thompson 		 */
190402ac6454SAndrew Thompson 
1905e55e1ebcSAndrew Thompson 		temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
190602ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
190702ac6454SAndrew Thompson 		temp.qtd_status |=
1908ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
1909e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
191002ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
191102ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1)) :
1912e55e1ebcSAndrew Thompson 		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
191302ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
191402ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
191502ac6454SAndrew Thompson 
191602ac6454SAndrew Thompson 		temp.len = 0;
191702ac6454SAndrew Thompson 		temp.pc = NULL;
191802ac6454SAndrew Thompson 		temp.shortpkt = 0;
19190f7d4548SAndrew Thompson 		temp.last_frame = 1;
19200f7d4548SAndrew Thompson 		temp.setup_alt_next = 0;
192102ac6454SAndrew Thompson 
192202ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
192302ac6454SAndrew Thompson 	}
192402ac6454SAndrew Thompson 	td = temp.td;
192502ac6454SAndrew Thompson 
192602ac6454SAndrew Thompson 	/* the last TD terminates the transfer: */
1927e55e1ebcSAndrew Thompson 	td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
1928e55e1ebcSAndrew Thompson 	td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);
192902ac6454SAndrew Thompson 
1930a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(td->page_cache);
193102ac6454SAndrew Thompson 
193202ac6454SAndrew Thompson 	/* must have at least one frame! */
193302ac6454SAndrew Thompson 
193402ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
193502ac6454SAndrew Thompson 
193602ac6454SAndrew Thompson #if USB_DEBUG
193702ac6454SAndrew Thompson 	if (ehcidebug > 8) {
193802ac6454SAndrew Thompson 		DPRINTF("nexttog=%d; data before transfer:\n",
1939ae60fdfbSAndrew Thompson 		    xfer->endpoint->toggle_next);
194002ac6454SAndrew Thompson 		ehci_dump_sqtds(temp.sc,
194102ac6454SAndrew Thompson 		    xfer->td_transfer_first);
194202ac6454SAndrew Thompson 	}
194302ac6454SAndrew Thompson #endif
194402ac6454SAndrew Thompson 
1945ae60fdfbSAndrew Thompson 	methods = xfer->endpoint->methods;
194602ac6454SAndrew Thompson 
194702ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
194802ac6454SAndrew Thompson 
194902ac6454SAndrew Thompson 	/* the "qh_link" field is filled when the QH is added */
195002ac6454SAndrew Thompson 
195102ac6454SAndrew Thompson 	qh_endp =
195202ac6454SAndrew Thompson 	    (EHCI_QH_SET_ADDR(xfer->address) |
1953ae60fdfbSAndrew Thompson 	    EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
195402ac6454SAndrew Thompson 	    EHCI_QH_SET_MPL(xfer->max_packet_size));
195502ac6454SAndrew Thompson 
1956a593f6b8SAndrew Thompson 	if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
19572094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
195802ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods)
195902ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(8);
196002ac6454SAndrew Thompson 	} else {
196102ac6454SAndrew Thompson 
1962a593f6b8SAndrew Thompson 		if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
19632094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
196402ac6454SAndrew Thompson 		} else {
19652094ecdaSAndrew Thompson 			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
196602ac6454SAndrew Thompson 		}
196702ac6454SAndrew Thompson 
196802ac6454SAndrew Thompson 		if (methods == &ehci_device_ctrl_methods) {
196902ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_CTL;
197002ac6454SAndrew Thompson 		}
197102ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods) {
197202ac6454SAndrew Thompson 			/* Only try one time per microframe! */
197302ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(1);
197402ac6454SAndrew Thompson 		}
197502ac6454SAndrew Thompson 	}
197602ac6454SAndrew Thompson 
19772094ecdaSAndrew Thompson 	if (temp.auto_data_toggle == 0) {
19782094ecdaSAndrew Thompson 		/* software computes the data toggle */
19792094ecdaSAndrew Thompson 		qh_endp |= EHCI_QH_DTC;
19802094ecdaSAndrew Thompson 	}
19812094ecdaSAndrew Thompson 
1982e55e1ebcSAndrew Thompson 	qh->qh_endp = htohc32(temp.sc, qh_endp);
198302ac6454SAndrew Thompson 
198402ac6454SAndrew Thompson 	qh_endphub =
198502ac6454SAndrew Thompson 	    (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
1986a593f6b8SAndrew Thompson 	    EHCI_QH_SET_CMASK(xfer->usb_cmask) |
1987a593f6b8SAndrew Thompson 	    EHCI_QH_SET_SMASK(xfer->usb_smask) |
198802ac6454SAndrew Thompson 	    EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
198902ac6454SAndrew Thompson 	    EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
199002ac6454SAndrew Thompson 
1991e55e1ebcSAndrew Thompson 	qh->qh_endphub = htohc32(temp.sc, qh_endphub);
19922094ecdaSAndrew Thompson 	qh->qh_curqtd = 0;
199302ac6454SAndrew Thompson 
199402ac6454SAndrew Thompson 	/* fill the overlay qTD */
199502ac6454SAndrew Thompson 
19962094ecdaSAndrew Thompson 	if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
199702ac6454SAndrew Thompson 		/* DATA1 is next */
19982094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
19992094ecdaSAndrew Thompson 	} else {
20002094ecdaSAndrew Thompson 		qh->qh_qtd.qtd_status = 0;
200102ac6454SAndrew Thompson 	}
20022094ecdaSAndrew Thompson 
200302ac6454SAndrew Thompson 	td = xfer->td_transfer_first;
200402ac6454SAndrew Thompson 
200502ac6454SAndrew Thompson 	qh->qh_qtd.qtd_next = td->qtd_self;
200602ac6454SAndrew Thompson 	qh->qh_qtd.qtd_altnext =
2007e55e1ebcSAndrew Thompson 	    htohc32(temp.sc, EHCI_LINK_TERMINATE);
200802ac6454SAndrew Thompson 
2009a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(qh->page_cache);
201002ac6454SAndrew Thompson 
2011ec8f3127SAndrew Thompson 	if (xfer->xroot->udev->flags.self_suspended == 0) {
201202ac6454SAndrew Thompson 		EHCI_APPEND_QH(qh, *qh_last);
201302ac6454SAndrew Thompson 	}
201402ac6454SAndrew Thompson }
201502ac6454SAndrew Thompson 
201602ac6454SAndrew Thompson static void
201739307315SAndrew Thompson ehci_root_intr(ehci_softc_t *sc)
201802ac6454SAndrew Thompson {
201902ac6454SAndrew Thompson 	uint16_t i;
202002ac6454SAndrew Thompson 	uint16_t m;
202102ac6454SAndrew Thompson 
202202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
202302ac6454SAndrew Thompson 
202402ac6454SAndrew Thompson 	/* clear any old interrupt data */
202539307315SAndrew Thompson 	memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
202602ac6454SAndrew Thompson 
202702ac6454SAndrew Thompson 	/* set bits */
202802ac6454SAndrew Thompson 	m = (sc->sc_noport + 1);
202902ac6454SAndrew Thompson 	if (m > (8 * sizeof(sc->sc_hub_idata))) {
203002ac6454SAndrew Thompson 		m = (8 * sizeof(sc->sc_hub_idata));
203102ac6454SAndrew Thompson 	}
203202ac6454SAndrew Thompson 	for (i = 1; i < m; i++) {
203302ac6454SAndrew Thompson 		/* pick out CHANGE bits from the status register */
203402ac6454SAndrew Thompson 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
203502ac6454SAndrew Thompson 			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
203602ac6454SAndrew Thompson 			DPRINTF("port %d changed\n", i);
203702ac6454SAndrew Thompson 		}
203802ac6454SAndrew Thompson 	}
203939307315SAndrew Thompson 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
204039307315SAndrew Thompson 	    sizeof(sc->sc_hub_idata));
204102ac6454SAndrew Thompson }
204202ac6454SAndrew Thompson 
204302ac6454SAndrew Thompson static void
2044760bc48eSAndrew Thompson ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
204502ac6454SAndrew Thompson {
204602ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
204702ac6454SAndrew Thompson 	uint32_t status;
204802ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
204902ac6454SAndrew Thompson 	uint16_t len = 0;
205002ac6454SAndrew Thompson 	ehci_sitd_t *td = xfer->td_transfer_first;
205102ac6454SAndrew Thompson 	ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
205202ac6454SAndrew Thompson 
2053ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2054ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
205502ac6454SAndrew Thompson 
205602ac6454SAndrew Thompson 	while (nframes--) {
205702ac6454SAndrew Thompson 		if (td == NULL) {
205802ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
205902ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
206002ac6454SAndrew Thompson 		}
206102ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
206202ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
206302ac6454SAndrew Thompson 		}
206402ac6454SAndrew Thompson #if USB_DEBUG
206502ac6454SAndrew Thompson 		if (ehcidebug > 15) {
206602ac6454SAndrew Thompson 			DPRINTF("isoc FS-TD\n");
206702ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
206802ac6454SAndrew Thompson 		}
206902ac6454SAndrew Thompson #endif
2070a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2071e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->sitd_status);
207202ac6454SAndrew Thompson 
207302ac6454SAndrew Thompson 		len = EHCI_SITD_GET_LEN(status);
207402ac6454SAndrew Thompson 
2075c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);
2076c773c254SAndrew Thompson 
207702ac6454SAndrew Thompson 		if (*plen >= len) {
207802ac6454SAndrew Thompson 			len = *plen - len;
207902ac6454SAndrew Thompson 		} else {
208002ac6454SAndrew Thompson 			len = 0;
208102ac6454SAndrew Thompson 		}
208202ac6454SAndrew Thompson 
208302ac6454SAndrew Thompson 		*plen = len;
208402ac6454SAndrew Thompson 
208502ac6454SAndrew Thompson 		/* remove FS-TD from schedule */
208602ac6454SAndrew Thompson 		EHCI_REMOVE_FS_TD(td, *pp_last);
208702ac6454SAndrew Thompson 
208802ac6454SAndrew Thompson 		pp_last++;
208902ac6454SAndrew Thompson 		plen++;
209002ac6454SAndrew Thompson 		td = td->obj_next;
209102ac6454SAndrew Thompson 	}
209202ac6454SAndrew Thompson 
209302ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
209402ac6454SAndrew Thompson }
209502ac6454SAndrew Thompson 
209602ac6454SAndrew Thompson static void
2097760bc48eSAndrew Thompson ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
209802ac6454SAndrew Thompson {
209902ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
210002ac6454SAndrew Thompson 	uint32_t status;
210102ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
210202ac6454SAndrew Thompson 	uint16_t len = 0;
210302ac6454SAndrew Thompson 	uint8_t td_no = 0;
210402ac6454SAndrew Thompson 	ehci_itd_t *td = xfer->td_transfer_first;
210502ac6454SAndrew Thompson 	ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
210602ac6454SAndrew Thompson 
2107ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2108ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
210902ac6454SAndrew Thompson 
211002ac6454SAndrew Thompson 	while (nframes--) {
211102ac6454SAndrew Thompson 		if (td == NULL) {
211202ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
211302ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
211402ac6454SAndrew Thompson 		}
211502ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
211602ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
211702ac6454SAndrew Thompson 		}
211802ac6454SAndrew Thompson #if USB_DEBUG
211902ac6454SAndrew Thompson 		if (ehcidebug > 15) {
212002ac6454SAndrew Thompson 			DPRINTF("isoc HS-TD\n");
212102ac6454SAndrew Thompson 			ehci_dump_itd(sc, td);
212202ac6454SAndrew Thompson 		}
212302ac6454SAndrew Thompson #endif
212402ac6454SAndrew Thompson 
2125a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
2126e55e1ebcSAndrew Thompson 		status = hc32toh(sc, td->itd_status[td_no]);
212702ac6454SAndrew Thompson 
212802ac6454SAndrew Thompson 		len = EHCI_ITD_GET_LEN(status);
212902ac6454SAndrew Thompson 
2130c773c254SAndrew Thompson 		DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);
2131c773c254SAndrew Thompson 
213202ac6454SAndrew Thompson 		if (*plen >= len) {
213302ac6454SAndrew Thompson 			/*
213402ac6454SAndrew Thompson 			 * The length is valid. NOTE: The complete
213502ac6454SAndrew Thompson 			 * length is written back into the status
213602ac6454SAndrew Thompson 			 * field, and not the remainder like with
213702ac6454SAndrew Thompson 			 * other transfer descriptor types.
213802ac6454SAndrew Thompson 			 */
213902ac6454SAndrew Thompson 		} else {
214002ac6454SAndrew Thompson 			/* Invalid length - truncate */
214102ac6454SAndrew Thompson 			len = 0;
214202ac6454SAndrew Thompson 		}
214302ac6454SAndrew Thompson 
214402ac6454SAndrew Thompson 		*plen = len;
214502ac6454SAndrew Thompson 
214602ac6454SAndrew Thompson 		plen++;
214702ac6454SAndrew Thompson 		td_no++;
214802ac6454SAndrew Thompson 
214902ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
215002ac6454SAndrew Thompson 			/* remove HS-TD from schedule */
215102ac6454SAndrew Thompson 			EHCI_REMOVE_HS_TD(td, *pp_last);
215202ac6454SAndrew Thompson 			pp_last++;
215302ac6454SAndrew Thompson 
215402ac6454SAndrew Thompson 			td_no = 0;
215502ac6454SAndrew Thompson 			td = td->obj_next;
215602ac6454SAndrew Thompson 		}
215702ac6454SAndrew Thompson 	}
215802ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
215902ac6454SAndrew Thompson }
216002ac6454SAndrew Thompson 
216102ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row,
216202ac6454SAndrew Thompson  * from close and from interrupt
216302ac6454SAndrew Thompson  */
216402ac6454SAndrew Thompson static void
2165e0a69b51SAndrew Thompson ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
216602ac6454SAndrew Thompson {
2167ae60fdfbSAndrew Thompson 	struct usb_pipe_methods *methods = xfer->endpoint->methods;
216802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
216902ac6454SAndrew Thompson 
217002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
217102ac6454SAndrew Thompson 
2172ae60fdfbSAndrew Thompson 	DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2173ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint, error);
217402ac6454SAndrew Thompson 
217502ac6454SAndrew Thompson 	if ((methods == &ehci_device_bulk_methods) ||
217602ac6454SAndrew Thompson 	    (methods == &ehci_device_ctrl_methods)) {
217702ac6454SAndrew Thompson #if USB_DEBUG
217802ac6454SAndrew Thompson 		if (ehcidebug > 8) {
217902ac6454SAndrew Thompson 			DPRINTF("nexttog=%d; data after transfer:\n",
2180ae60fdfbSAndrew Thompson 			    xfer->endpoint->toggle_next);
218102ac6454SAndrew Thompson 			ehci_dump_sqtds(sc,
218202ac6454SAndrew Thompson 			    xfer->td_transfer_first);
218302ac6454SAndrew Thompson 		}
218402ac6454SAndrew Thompson #endif
218502ac6454SAndrew Thompson 
218602ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
218702ac6454SAndrew Thompson 		    sc->sc_async_p_last);
218802ac6454SAndrew Thompson 	}
218902ac6454SAndrew Thompson 	if (methods == &ehci_device_intr_methods) {
219002ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
219102ac6454SAndrew Thompson 		    sc->sc_intr_p_last[xfer->qh_pos]);
219202ac6454SAndrew Thompson 	}
219302ac6454SAndrew Thompson 	/*
219402ac6454SAndrew Thompson 	 * Only finish isochronous transfers once which will update
219502ac6454SAndrew Thompson 	 * "xfer->frlengths".
219602ac6454SAndrew Thompson 	 */
219702ac6454SAndrew Thompson 	if (xfer->td_transfer_first &&
219802ac6454SAndrew Thompson 	    xfer->td_transfer_last) {
219902ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_fs_methods) {
220002ac6454SAndrew Thompson 			ehci_isoc_fs_done(sc, xfer);
220102ac6454SAndrew Thompson 		}
220202ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_hs_methods) {
220302ac6454SAndrew Thompson 			ehci_isoc_hs_done(sc, xfer);
220402ac6454SAndrew Thompson 		}
220502ac6454SAndrew Thompson 		xfer->td_transfer_first = NULL;
220602ac6454SAndrew Thompson 		xfer->td_transfer_last = NULL;
220702ac6454SAndrew Thompson 	}
220802ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
2209a593f6b8SAndrew Thompson 	usbd_transfer_done(xfer, error);
221002ac6454SAndrew Thompson }
221102ac6454SAndrew Thompson 
221202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
221302ac6454SAndrew Thompson  * ehci bulk support
221402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
221502ac6454SAndrew Thompson static void
2216760bc48eSAndrew Thompson ehci_device_bulk_open(struct usb_xfer *xfer)
221702ac6454SAndrew Thompson {
221802ac6454SAndrew Thompson 	return;
221902ac6454SAndrew Thompson }
222002ac6454SAndrew Thompson 
222102ac6454SAndrew Thompson static void
2222760bc48eSAndrew Thompson ehci_device_bulk_close(struct usb_xfer *xfer)
222302ac6454SAndrew Thompson {
222402ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
222502ac6454SAndrew Thompson }
222602ac6454SAndrew Thompson 
222702ac6454SAndrew Thompson static void
2228760bc48eSAndrew Thompson ehci_device_bulk_enter(struct usb_xfer *xfer)
222902ac6454SAndrew Thompson {
223002ac6454SAndrew Thompson 	return;
223102ac6454SAndrew Thompson }
223202ac6454SAndrew Thompson 
223302ac6454SAndrew Thompson static void
2234760bc48eSAndrew Thompson ehci_device_bulk_start(struct usb_xfer *xfer)
223502ac6454SAndrew Thompson {
223602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2237d1864afbSAndrew Thompson 	uint32_t temp;
223802ac6454SAndrew Thompson 
223902ac6454SAndrew Thompson 	/* setup TD's and QH */
224002ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
224102ac6454SAndrew Thompson 
224202ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
224302ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
2244d1864afbSAndrew Thompson 
2245d1864afbSAndrew Thompson 	/* XXX Performance quirk: Some Host Controllers have a too low
2246d1864afbSAndrew Thompson 	 * interrupt rate. Issue an IAAD to stimulate the Host
2247d1864afbSAndrew Thompson 	 * Controller after queueing the BULK transfer.
2248d1864afbSAndrew Thompson 	 */
2249d1864afbSAndrew Thompson 	temp = EOREAD4(sc, EHCI_USBCMD);
2250d1864afbSAndrew Thompson 	if (!(temp & EHCI_CMD_IAAD))
2251d1864afbSAndrew Thompson 		EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
225202ac6454SAndrew Thompson }
225302ac6454SAndrew Thompson 
2254760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_bulk_methods =
225502ac6454SAndrew Thompson {
225602ac6454SAndrew Thompson 	.open = ehci_device_bulk_open,
225702ac6454SAndrew Thompson 	.close = ehci_device_bulk_close,
225802ac6454SAndrew Thompson 	.enter = ehci_device_bulk_enter,
225902ac6454SAndrew Thompson 	.start = ehci_device_bulk_start,
226002ac6454SAndrew Thompson };
226102ac6454SAndrew Thompson 
226202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
226302ac6454SAndrew Thompson  * ehci control support
226402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
226502ac6454SAndrew Thompson static void
2266760bc48eSAndrew Thompson ehci_device_ctrl_open(struct usb_xfer *xfer)
226702ac6454SAndrew Thompson {
226802ac6454SAndrew Thompson 	return;
226902ac6454SAndrew Thompson }
227002ac6454SAndrew Thompson 
227102ac6454SAndrew Thompson static void
2272760bc48eSAndrew Thompson ehci_device_ctrl_close(struct usb_xfer *xfer)
227302ac6454SAndrew Thompson {
227402ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
227502ac6454SAndrew Thompson }
227602ac6454SAndrew Thompson 
227702ac6454SAndrew Thompson static void
2278760bc48eSAndrew Thompson ehci_device_ctrl_enter(struct usb_xfer *xfer)
227902ac6454SAndrew Thompson {
228002ac6454SAndrew Thompson 	return;
228102ac6454SAndrew Thompson }
228202ac6454SAndrew Thompson 
228302ac6454SAndrew Thompson static void
2284760bc48eSAndrew Thompson ehci_device_ctrl_start(struct usb_xfer *xfer)
228502ac6454SAndrew Thompson {
228602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
228702ac6454SAndrew Thompson 
228802ac6454SAndrew Thompson 	/* setup TD's and QH */
228902ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
229002ac6454SAndrew Thompson 
229102ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
229202ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
229302ac6454SAndrew Thompson }
229402ac6454SAndrew Thompson 
2295760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_ctrl_methods =
229602ac6454SAndrew Thompson {
229702ac6454SAndrew Thompson 	.open = ehci_device_ctrl_open,
229802ac6454SAndrew Thompson 	.close = ehci_device_ctrl_close,
229902ac6454SAndrew Thompson 	.enter = ehci_device_ctrl_enter,
230002ac6454SAndrew Thompson 	.start = ehci_device_ctrl_start,
230102ac6454SAndrew Thompson };
230202ac6454SAndrew Thompson 
230302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
230402ac6454SAndrew Thompson  * ehci interrupt support
230502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
230602ac6454SAndrew Thompson static void
2307760bc48eSAndrew Thompson ehci_device_intr_open(struct usb_xfer *xfer)
230802ac6454SAndrew Thompson {
230902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
231002ac6454SAndrew Thompson 	uint16_t best;
231102ac6454SAndrew Thompson 	uint16_t bit;
231202ac6454SAndrew Thompson 	uint16_t x;
231302ac6454SAndrew Thompson 	uint8_t slot;
231402ac6454SAndrew Thompson 
231502ac6454SAndrew Thompson 	/* Allocate a microframe slot first: */
231602ac6454SAndrew Thompson 
2317a593f6b8SAndrew Thompson 	slot = usb_intr_schedule_adjust
231802ac6454SAndrew Thompson 	    (xfer->xroot->udev, xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX);
231902ac6454SAndrew Thompson 
2320a593f6b8SAndrew Thompson 	if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
2321a593f6b8SAndrew Thompson 		xfer->usb_uframe = slot;
2322a593f6b8SAndrew Thompson 		xfer->usb_smask = (1 << slot) & 0xFF;
2323a593f6b8SAndrew Thompson 		xfer->usb_cmask = 0;
232402ac6454SAndrew Thompson 	} else {
2325a593f6b8SAndrew Thompson 		xfer->usb_uframe = slot;
2326a593f6b8SAndrew Thompson 		xfer->usb_smask = (1 << slot) & 0x3F;
2327a593f6b8SAndrew Thompson 		xfer->usb_cmask = (-(4 << slot)) & 0xFE;
232802ac6454SAndrew Thompson 	}
232902ac6454SAndrew Thompson 
233002ac6454SAndrew Thompson 	/*
233102ac6454SAndrew Thompson 	 * Find the best QH position corresponding to the given interval:
233202ac6454SAndrew Thompson 	 */
233302ac6454SAndrew Thompson 
233402ac6454SAndrew Thompson 	best = 0;
233502ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
233602ac6454SAndrew Thompson 	while (bit) {
233702ac6454SAndrew Thompson 		if (xfer->interval >= bit) {
233802ac6454SAndrew Thompson 			x = bit;
233902ac6454SAndrew Thompson 			best = bit;
234002ac6454SAndrew Thompson 			while (x & bit) {
234102ac6454SAndrew Thompson 				if (sc->sc_intr_stat[x] <
234202ac6454SAndrew Thompson 				    sc->sc_intr_stat[best]) {
234302ac6454SAndrew Thompson 					best = x;
234402ac6454SAndrew Thompson 				}
234502ac6454SAndrew Thompson 				x++;
234602ac6454SAndrew Thompson 			}
234702ac6454SAndrew Thompson 			break;
234802ac6454SAndrew Thompson 		}
234902ac6454SAndrew Thompson 		bit >>= 1;
235002ac6454SAndrew Thompson 	}
235102ac6454SAndrew Thompson 
235202ac6454SAndrew Thompson 	sc->sc_intr_stat[best]++;
235302ac6454SAndrew Thompson 	xfer->qh_pos = best;
235402ac6454SAndrew Thompson 
235502ac6454SAndrew Thompson 	DPRINTFN(3, "best=%d interval=%d\n",
235602ac6454SAndrew Thompson 	    best, xfer->interval);
235702ac6454SAndrew Thompson }
235802ac6454SAndrew Thompson 
235902ac6454SAndrew Thompson static void
2360760bc48eSAndrew Thompson ehci_device_intr_close(struct usb_xfer *xfer)
236102ac6454SAndrew Thompson {
236202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
236302ac6454SAndrew Thompson 	uint8_t slot;
236402ac6454SAndrew Thompson 
2365a593f6b8SAndrew Thompson 	slot = usb_intr_schedule_adjust
2366a593f6b8SAndrew Thompson 	    (xfer->xroot->udev, -(xfer->max_frame_size), xfer->usb_uframe);
236702ac6454SAndrew Thompson 
236802ac6454SAndrew Thompson 	sc->sc_intr_stat[xfer->qh_pos]--;
236902ac6454SAndrew Thompson 
237002ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
237102ac6454SAndrew Thompson }
237202ac6454SAndrew Thompson 
237302ac6454SAndrew Thompson static void
2374760bc48eSAndrew Thompson ehci_device_intr_enter(struct usb_xfer *xfer)
237502ac6454SAndrew Thompson {
237602ac6454SAndrew Thompson 	return;
237702ac6454SAndrew Thompson }
237802ac6454SAndrew Thompson 
237902ac6454SAndrew Thompson static void
2380760bc48eSAndrew Thompson ehci_device_intr_start(struct usb_xfer *xfer)
238102ac6454SAndrew Thompson {
238202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
238302ac6454SAndrew Thompson 
238402ac6454SAndrew Thompson 	/* setup TD's and QH */
238502ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
238602ac6454SAndrew Thompson 
238702ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
238802ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
238902ac6454SAndrew Thompson }
239002ac6454SAndrew Thompson 
2391760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_intr_methods =
239202ac6454SAndrew Thompson {
239302ac6454SAndrew Thompson 	.open = ehci_device_intr_open,
239402ac6454SAndrew Thompson 	.close = ehci_device_intr_close,
239502ac6454SAndrew Thompson 	.enter = ehci_device_intr_enter,
239602ac6454SAndrew Thompson 	.start = ehci_device_intr_start,
239702ac6454SAndrew Thompson };
239802ac6454SAndrew Thompson 
239902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
240002ac6454SAndrew Thompson  * ehci full speed isochronous support
240102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
240202ac6454SAndrew Thompson static void
2403760bc48eSAndrew Thompson ehci_device_isoc_fs_open(struct usb_xfer *xfer)
240402ac6454SAndrew Thompson {
240502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
240602ac6454SAndrew Thompson 	ehci_sitd_t *td;
240702ac6454SAndrew Thompson 	uint32_t sitd_portaddr;
240802ac6454SAndrew Thompson 	uint8_t ds;
240902ac6454SAndrew Thompson 
241002ac6454SAndrew Thompson 	sitd_portaddr =
241102ac6454SAndrew Thompson 	    EHCI_SITD_SET_ADDR(xfer->address) |
2412ae60fdfbSAndrew Thompson 	    EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
241302ac6454SAndrew Thompson 	    EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
241402ac6454SAndrew Thompson 	    EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
241502ac6454SAndrew Thompson 
2416ae60fdfbSAndrew Thompson 	if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
241702ac6454SAndrew Thompson 		sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
241802ac6454SAndrew Thompson 	}
2419e55e1ebcSAndrew Thompson 	sitd_portaddr = htohc32(sc, sitd_portaddr);
242002ac6454SAndrew Thompson 
242102ac6454SAndrew Thompson 	/* initialize all TD's */
242202ac6454SAndrew Thompson 
242302ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
242402ac6454SAndrew Thompson 
242502ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
242602ac6454SAndrew Thompson 
242702ac6454SAndrew Thompson 			td->sitd_portaddr = sitd_portaddr;
242802ac6454SAndrew Thompson 
242902ac6454SAndrew Thompson 			/*
243002ac6454SAndrew Thompson 			 * TODO: make some kind of automatic
243102ac6454SAndrew Thompson 			 * SMASK/CMASK selection based on micro-frame
243202ac6454SAndrew Thompson 			 * usage
243302ac6454SAndrew Thompson 			 *
243402ac6454SAndrew Thompson 			 * micro-frame usage (8 microframes per 1ms)
243502ac6454SAndrew Thompson 			 */
2436e55e1ebcSAndrew Thompson 			td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);
243702ac6454SAndrew Thompson 
2438a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
243902ac6454SAndrew Thompson 		}
244002ac6454SAndrew Thompson 	}
244102ac6454SAndrew Thompson }
244202ac6454SAndrew Thompson 
244302ac6454SAndrew Thompson static void
2444760bc48eSAndrew Thompson ehci_device_isoc_fs_close(struct usb_xfer *xfer)
244502ac6454SAndrew Thompson {
244602ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
244702ac6454SAndrew Thompson }
244802ac6454SAndrew Thompson 
244902ac6454SAndrew Thompson static void
2450760bc48eSAndrew Thompson ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
245102ac6454SAndrew Thompson {
2452760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
245302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
2454760bc48eSAndrew Thompson 	struct usb_fs_isoc_schedule *fss_start;
2455760bc48eSAndrew Thompson 	struct usb_fs_isoc_schedule *fss_end;
2456760bc48eSAndrew Thompson 	struct usb_fs_isoc_schedule *fss;
245702ac6454SAndrew Thompson 	ehci_sitd_t *td;
245802ac6454SAndrew Thompson 	ehci_sitd_t *td_last = NULL;
245902ac6454SAndrew Thompson 	ehci_sitd_t **pp_last;
246002ac6454SAndrew Thompson 	uint32_t *plen;
246102ac6454SAndrew Thompson 	uint32_t buf_offset;
246202ac6454SAndrew Thompson 	uint32_t nframes;
246302ac6454SAndrew Thompson 	uint32_t temp;
246402ac6454SAndrew Thompson 	uint32_t sitd_mask;
246502ac6454SAndrew Thompson 	uint16_t tlen;
246602ac6454SAndrew Thompson 	uint8_t sa;
246702ac6454SAndrew Thompson 	uint8_t sb;
246802ac6454SAndrew Thompson 	uint8_t error;
246902ac6454SAndrew Thompson 
247002ac6454SAndrew Thompson #if USB_DEBUG
247102ac6454SAndrew Thompson 	uint8_t once = 1;
247202ac6454SAndrew Thompson 
247302ac6454SAndrew Thompson #endif
247402ac6454SAndrew Thompson 
247502ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2476ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
247702ac6454SAndrew Thompson 
247802ac6454SAndrew Thompson 	/* get the current frame index */
247902ac6454SAndrew Thompson 
248002ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
248102ac6454SAndrew Thompson 
248202ac6454SAndrew Thompson 	/*
248302ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
248402ac6454SAndrew Thompson 	 * will be inserted
248502ac6454SAndrew Thompson 	 */
2486ae60fdfbSAndrew Thompson 	buf_offset = (nframes - xfer->endpoint->isoc_next) &
248702ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
248802ac6454SAndrew Thompson 
2489ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
249002ac6454SAndrew Thompson 	    (buf_offset < xfer->nframes)) {
249102ac6454SAndrew Thompson 		/*
249202ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
249302ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
249402ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
249502ac6454SAndrew Thompson 		 * overlap.
249602ac6454SAndrew Thompson 		 */
2497ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) &
249802ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2499ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
2500ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
250102ac6454SAndrew Thompson 	}
250202ac6454SAndrew Thompson 	/*
250302ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
250402ac6454SAndrew Thompson 	 * current frame position:
250502ac6454SAndrew Thompson 	 */
2506ae60fdfbSAndrew Thompson 	buf_offset = (xfer->endpoint->isoc_next - nframes) &
250702ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
250802ac6454SAndrew Thompson 
250902ac6454SAndrew Thompson 	/*
251002ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
251102ac6454SAndrew Thompson 	 */
251202ac6454SAndrew Thompson 	xfer->isoc_time_complete =
2513a593f6b8SAndrew Thompson 	    usbd_fs_isoc_schedule_isoc_time_expand
251402ac6454SAndrew Thompson 	    (xfer->xroot->udev, &fss_start, &fss_end, nframes) + buf_offset +
251502ac6454SAndrew Thompson 	    xfer->nframes;
251602ac6454SAndrew Thompson 
251702ac6454SAndrew Thompson 	/* get the real number of frames */
251802ac6454SAndrew Thompson 
251902ac6454SAndrew Thompson 	nframes = xfer->nframes;
252002ac6454SAndrew Thompson 
252102ac6454SAndrew Thompson 	buf_offset = 0;
252202ac6454SAndrew Thompson 
252302ac6454SAndrew Thompson 	plen = xfer->frlengths;
252402ac6454SAndrew Thompson 
252502ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
252602ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
252702ac6454SAndrew Thompson 
252802ac6454SAndrew Thompson 	/* get next DMA set */
252902ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
253002ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
253102ac6454SAndrew Thompson 
2532ae60fdfbSAndrew Thompson 	pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];
253302ac6454SAndrew Thompson 
253402ac6454SAndrew Thompson 	/* store starting position */
253502ac6454SAndrew Thompson 
2536ae60fdfbSAndrew Thompson 	xfer->qh_pos = xfer->endpoint->isoc_next;
253702ac6454SAndrew Thompson 
253802ac6454SAndrew Thompson 	fss = fss_start + (xfer->qh_pos % USB_ISOC_TIME_MAX);
253902ac6454SAndrew Thompson 
254002ac6454SAndrew Thompson 	while (nframes--) {
254102ac6454SAndrew Thompson 		if (td == NULL) {
254202ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
254302ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
254402ac6454SAndrew Thompson 		}
254502ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
254602ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
254702ac6454SAndrew Thompson 		}
254802ac6454SAndrew Thompson 		if (fss >= fss_end) {
254902ac6454SAndrew Thompson 			fss = fss_start;
255002ac6454SAndrew Thompson 		}
255102ac6454SAndrew Thompson 		/* reuse sitd_portaddr and sitd_back from last transfer */
255202ac6454SAndrew Thompson 
255302ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
255402ac6454SAndrew Thompson #if USB_DEBUG
255502ac6454SAndrew Thompson 			if (once) {
255602ac6454SAndrew Thompson 				once = 0;
255702ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d "
255802ac6454SAndrew Thompson 				    "bytes (frame truncated)\n",
255902ac6454SAndrew Thompson 				    __FUNCTION__, *plen,
256002ac6454SAndrew Thompson 				    xfer->max_frame_size);
256102ac6454SAndrew Thompson 			}
256202ac6454SAndrew Thompson #endif
256302ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
256402ac6454SAndrew Thompson 		}
256502ac6454SAndrew Thompson 		/*
256602ac6454SAndrew Thompson 		 * We currently don't care if the ISOCHRONOUS schedule is
256702ac6454SAndrew Thompson 		 * full!
256802ac6454SAndrew Thompson 		 */
2569a593f6b8SAndrew Thompson 		error = usbd_fs_isoc_schedule_alloc(fss, &sa, *plen);
257002ac6454SAndrew Thompson 		if (error) {
257102ac6454SAndrew Thompson 			/*
257202ac6454SAndrew Thompson 			 * The FULL speed schedule is FULL! Set length
257302ac6454SAndrew Thompson 			 * to zero.
257402ac6454SAndrew Thompson 			 */
257502ac6454SAndrew Thompson 			*plen = 0;
257602ac6454SAndrew Thompson 		}
257702ac6454SAndrew Thompson 		if (*plen) {
257802ac6454SAndrew Thompson 			/*
2579a593f6b8SAndrew Thompson 			 * only call "usbd_get_page()" when we have a
258002ac6454SAndrew Thompson 			 * non-zero length
258102ac6454SAndrew Thompson 			 */
2582a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
2583e55e1ebcSAndrew Thompson 			td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
258402ac6454SAndrew Thompson 			buf_offset += *plen;
258502ac6454SAndrew Thompson 			/*
258602ac6454SAndrew Thompson 			 * NOTE: We need to subtract one from the offset so
258702ac6454SAndrew Thompson 			 * that we are on a valid page!
258802ac6454SAndrew Thompson 			 */
2589a593f6b8SAndrew Thompson 			usbd_get_page(xfer->frbuffers, buf_offset - 1,
259002ac6454SAndrew Thompson 			    &buf_res);
259102ac6454SAndrew Thompson 			temp = buf_res.physaddr & ~0xFFF;
259202ac6454SAndrew Thompson 		} else {
259302ac6454SAndrew Thompson 			td->sitd_bp[0] = 0;
259402ac6454SAndrew Thompson 			temp = 0;
259502ac6454SAndrew Thompson 		}
259602ac6454SAndrew Thompson 
2597ae60fdfbSAndrew Thompson 		if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
259802ac6454SAndrew Thompson 			tlen = *plen;
259902ac6454SAndrew Thompson 			if (tlen <= 188) {
260002ac6454SAndrew Thompson 				temp |= 1;	/* T-count = 1, TP = ALL */
260102ac6454SAndrew Thompson 				tlen = 1;
260202ac6454SAndrew Thompson 			} else {
260302ac6454SAndrew Thompson 				tlen += 187;
260402ac6454SAndrew Thompson 				tlen /= 188;
260502ac6454SAndrew Thompson 				temp |= tlen;	/* T-count = [1..6] */
260602ac6454SAndrew Thompson 				temp |= 8;	/* TP = Begin */
260702ac6454SAndrew Thompson 			}
260802ac6454SAndrew Thompson 
260902ac6454SAndrew Thompson 			tlen += sa;
261002ac6454SAndrew Thompson 
261102ac6454SAndrew Thompson 			if (tlen >= 8) {
261202ac6454SAndrew Thompson 				sb = 0;
261302ac6454SAndrew Thompson 			} else {
261402ac6454SAndrew Thompson 				sb = (1 << tlen);
261502ac6454SAndrew Thompson 			}
261602ac6454SAndrew Thompson 
261702ac6454SAndrew Thompson 			sa = (1 << sa);
261802ac6454SAndrew Thompson 			sa = (sb - sa) & 0x3F;
261902ac6454SAndrew Thompson 			sb = 0;
262002ac6454SAndrew Thompson 		} else {
262102ac6454SAndrew Thompson 			sb = (-(4 << sa)) & 0xFE;
262202ac6454SAndrew Thompson 			sa = (1 << sa) & 0x3F;
262302ac6454SAndrew Thompson 		}
262402ac6454SAndrew Thompson 
262502ac6454SAndrew Thompson 		sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
262602ac6454SAndrew Thompson 		    EHCI_SITD_SET_CMASK(sb));
262702ac6454SAndrew Thompson 
2628e55e1ebcSAndrew Thompson 		td->sitd_bp[1] = htohc32(sc, temp);
262902ac6454SAndrew Thompson 
2630e55e1ebcSAndrew Thompson 		td->sitd_mask = htohc32(sc, sitd_mask);
263102ac6454SAndrew Thompson 
263202ac6454SAndrew Thompson 		if (nframes == 0) {
2633e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
26345f128668SAndrew Thompson 			    EHCI_SITD_IOC |
263502ac6454SAndrew Thompson 			    EHCI_SITD_ACTIVE |
263602ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
263702ac6454SAndrew Thompson 		} else {
2638e55e1ebcSAndrew Thompson 			td->sitd_status = htohc32(sc,
26395f128668SAndrew Thompson 			    EHCI_SITD_ACTIVE |
264002ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
264102ac6454SAndrew Thompson 		}
2642a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
264302ac6454SAndrew Thompson 
264402ac6454SAndrew Thompson #if USB_DEBUG
264502ac6454SAndrew Thompson 		if (ehcidebug > 15) {
264602ac6454SAndrew Thompson 			DPRINTF("FS-TD %d\n", nframes);
264702ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
264802ac6454SAndrew Thompson 		}
264902ac6454SAndrew Thompson #endif
265002ac6454SAndrew Thompson 		/* insert TD into schedule */
265102ac6454SAndrew Thompson 		EHCI_APPEND_FS_TD(td, *pp_last);
265202ac6454SAndrew Thompson 		pp_last++;
265302ac6454SAndrew Thompson 
265402ac6454SAndrew Thompson 		plen++;
265502ac6454SAndrew Thompson 		fss++;
265602ac6454SAndrew Thompson 		td_last = td;
265702ac6454SAndrew Thompson 		td = td->obj_next;
265802ac6454SAndrew Thompson 	}
265902ac6454SAndrew Thompson 
266002ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
266102ac6454SAndrew Thompson 
266202ac6454SAndrew Thompson 	/* update isoc_next */
2663ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
266402ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
266502ac6454SAndrew Thompson }
266602ac6454SAndrew Thompson 
266702ac6454SAndrew Thompson static void
2668760bc48eSAndrew Thompson ehci_device_isoc_fs_start(struct usb_xfer *xfer)
266902ac6454SAndrew Thompson {
267002ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
267102ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
267202ac6454SAndrew Thompson }
267302ac6454SAndrew Thompson 
2674760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_isoc_fs_methods =
267502ac6454SAndrew Thompson {
267602ac6454SAndrew Thompson 	.open = ehci_device_isoc_fs_open,
267702ac6454SAndrew Thompson 	.close = ehci_device_isoc_fs_close,
267802ac6454SAndrew Thompson 	.enter = ehci_device_isoc_fs_enter,
267902ac6454SAndrew Thompson 	.start = ehci_device_isoc_fs_start,
268002ac6454SAndrew Thompson };
268102ac6454SAndrew Thompson 
268202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
268302ac6454SAndrew Thompson  * ehci high speed isochronous support
268402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
268502ac6454SAndrew Thompson static void
2686760bc48eSAndrew Thompson ehci_device_isoc_hs_open(struct usb_xfer *xfer)
268702ac6454SAndrew Thompson {
268802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
268902ac6454SAndrew Thompson 	ehci_itd_t *td;
269002ac6454SAndrew Thompson 	uint32_t temp;
269102ac6454SAndrew Thompson 	uint8_t ds;
269202ac6454SAndrew Thompson 
269302ac6454SAndrew Thompson 	/* initialize all TD's */
269402ac6454SAndrew Thompson 
269502ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
269602ac6454SAndrew Thompson 
269702ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
269802ac6454SAndrew Thompson 
269902ac6454SAndrew Thompson 			/* set TD inactive */
270002ac6454SAndrew Thompson 			td->itd_status[0] = 0;
270102ac6454SAndrew Thompson 			td->itd_status[1] = 0;
270202ac6454SAndrew Thompson 			td->itd_status[2] = 0;
270302ac6454SAndrew Thompson 			td->itd_status[3] = 0;
270402ac6454SAndrew Thompson 			td->itd_status[4] = 0;
270502ac6454SAndrew Thompson 			td->itd_status[5] = 0;
270602ac6454SAndrew Thompson 			td->itd_status[6] = 0;
270702ac6454SAndrew Thompson 			td->itd_status[7] = 0;
270802ac6454SAndrew Thompson 
270902ac6454SAndrew Thompson 			/* set endpoint and address */
2710e55e1ebcSAndrew Thompson 			td->itd_bp[0] = htohc32(sc,
27115f128668SAndrew Thompson 			    EHCI_ITD_SET_ADDR(xfer->address) |
2712ae60fdfbSAndrew Thompson 			    EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));
271302ac6454SAndrew Thompson 
271402ac6454SAndrew Thompson 			temp =
271502ac6454SAndrew Thompson 			    EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
271602ac6454SAndrew Thompson 
271702ac6454SAndrew Thompson 			/* set direction */
2718ae60fdfbSAndrew Thompson 			if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
271902ac6454SAndrew Thompson 				temp |= EHCI_ITD_SET_DIR_IN;
272002ac6454SAndrew Thompson 			}
272102ac6454SAndrew Thompson 			/* set maximum packet size */
2722e55e1ebcSAndrew Thompson 			td->itd_bp[1] = htohc32(sc, temp);
272302ac6454SAndrew Thompson 
272402ac6454SAndrew Thompson 			/* set transfer multiplier */
2725e55e1ebcSAndrew Thompson 			td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);
272602ac6454SAndrew Thompson 
2727a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
272802ac6454SAndrew Thompson 		}
272902ac6454SAndrew Thompson 	}
273002ac6454SAndrew Thompson }
273102ac6454SAndrew Thompson 
273202ac6454SAndrew Thompson static void
2733760bc48eSAndrew Thompson ehci_device_isoc_hs_close(struct usb_xfer *xfer)
273402ac6454SAndrew Thompson {
273502ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
273602ac6454SAndrew Thompson }
273702ac6454SAndrew Thompson 
273802ac6454SAndrew Thompson static void
2739760bc48eSAndrew Thompson ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
274002ac6454SAndrew Thompson {
2741760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
274202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
274302ac6454SAndrew Thompson 	ehci_itd_t *td;
274402ac6454SAndrew Thompson 	ehci_itd_t *td_last = NULL;
274502ac6454SAndrew Thompson 	ehci_itd_t **pp_last;
274602ac6454SAndrew Thompson 	bus_size_t page_addr;
274702ac6454SAndrew Thompson 	uint32_t *plen;
274802ac6454SAndrew Thompson 	uint32_t status;
274902ac6454SAndrew Thompson 	uint32_t buf_offset;
275002ac6454SAndrew Thompson 	uint32_t nframes;
275102ac6454SAndrew Thompson 	uint32_t itd_offset[8 + 1];
275202ac6454SAndrew Thompson 	uint8_t x;
275302ac6454SAndrew Thompson 	uint8_t td_no;
275402ac6454SAndrew Thompson 	uint8_t page_no;
275502ac6454SAndrew Thompson 
275602ac6454SAndrew Thompson #if USB_DEBUG
275702ac6454SAndrew Thompson 	uint8_t once = 1;
275802ac6454SAndrew Thompson 
275902ac6454SAndrew Thompson #endif
276002ac6454SAndrew Thompson 
276102ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2762ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
276302ac6454SAndrew Thompson 
276402ac6454SAndrew Thompson 	/* get the current frame index */
276502ac6454SAndrew Thompson 
276602ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
276702ac6454SAndrew Thompson 
276802ac6454SAndrew Thompson 	/*
276902ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
277002ac6454SAndrew Thompson 	 * will be inserted
277102ac6454SAndrew Thompson 	 */
2772ae60fdfbSAndrew Thompson 	buf_offset = (nframes - xfer->endpoint->isoc_next) &
277302ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
277402ac6454SAndrew Thompson 
2775ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
277602ac6454SAndrew Thompson 	    (buf_offset < ((xfer->nframes + 7) / 8))) {
277702ac6454SAndrew Thompson 		/*
277802ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
277902ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
278002ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
278102ac6454SAndrew Thompson 		 * overlap.
278202ac6454SAndrew Thompson 		 */
2783ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) &
278402ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
2785ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
2786ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
278702ac6454SAndrew Thompson 	}
278802ac6454SAndrew Thompson 	/*
278902ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
279002ac6454SAndrew Thompson 	 * current frame position:
279102ac6454SAndrew Thompson 	 */
2792ae60fdfbSAndrew Thompson 	buf_offset = (xfer->endpoint->isoc_next - nframes) &
279302ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
279402ac6454SAndrew Thompson 
279502ac6454SAndrew Thompson 	/*
279602ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
279702ac6454SAndrew Thompson 	 */
279802ac6454SAndrew Thompson 	xfer->isoc_time_complete =
2799a593f6b8SAndrew Thompson 	    usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
280002ac6454SAndrew Thompson 	    ((xfer->nframes + 7) / 8);
280102ac6454SAndrew Thompson 
280202ac6454SAndrew Thompson 	/* get the real number of frames */
280302ac6454SAndrew Thompson 
280402ac6454SAndrew Thompson 	nframes = xfer->nframes;
280502ac6454SAndrew Thompson 
280602ac6454SAndrew Thompson 	buf_offset = 0;
280702ac6454SAndrew Thompson 	td_no = 0;
280802ac6454SAndrew Thompson 
280902ac6454SAndrew Thompson 	plen = xfer->frlengths;
281002ac6454SAndrew Thompson 
281102ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
281202ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
281302ac6454SAndrew Thompson 
281402ac6454SAndrew Thompson 	/* get next DMA set */
281502ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
281602ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
281702ac6454SAndrew Thompson 
2818ae60fdfbSAndrew Thompson 	pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];
281902ac6454SAndrew Thompson 
282002ac6454SAndrew Thompson 	/* store starting position */
282102ac6454SAndrew Thompson 
2822ae60fdfbSAndrew Thompson 	xfer->qh_pos = xfer->endpoint->isoc_next;
282302ac6454SAndrew Thompson 
282402ac6454SAndrew Thompson 	while (nframes--) {
282502ac6454SAndrew Thompson 		if (td == NULL) {
282602ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
282702ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
282802ac6454SAndrew Thompson 		}
282902ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
283002ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
283102ac6454SAndrew Thompson 		}
283202ac6454SAndrew Thompson 		/* range check */
283302ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
283402ac6454SAndrew Thompson #if USB_DEBUG
283502ac6454SAndrew Thompson 			if (once) {
283602ac6454SAndrew Thompson 				once = 0;
283702ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d bytes "
283802ac6454SAndrew Thompson 				    "(frame truncated)\n",
283902ac6454SAndrew Thompson 				    __FUNCTION__, *plen, xfer->max_frame_size);
284002ac6454SAndrew Thompson 			}
284102ac6454SAndrew Thompson #endif
284202ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
284302ac6454SAndrew Thompson 		}
284402ac6454SAndrew Thompson 		status = (EHCI_ITD_SET_LEN(*plen) |
284502ac6454SAndrew Thompson 		    EHCI_ITD_ACTIVE |
284602ac6454SAndrew Thompson 		    EHCI_ITD_SET_PG(0));
2847e55e1ebcSAndrew Thompson 		td->itd_status[td_no] = htohc32(sc, status);
284802ac6454SAndrew Thompson 		itd_offset[td_no] = buf_offset;
284902ac6454SAndrew Thompson 		buf_offset += *plen;
285002ac6454SAndrew Thompson 		plen++;
285102ac6454SAndrew Thompson 		td_no++;
285202ac6454SAndrew Thompson 
285302ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
285402ac6454SAndrew Thompson 
285502ac6454SAndrew Thompson 			/* the rest of the transfers are not active, if any */
285602ac6454SAndrew Thompson 			for (x = td_no; x != 8; x++) {
285702ac6454SAndrew Thompson 				td->itd_status[x] = 0;	/* not active */
285802ac6454SAndrew Thompson 			}
285902ac6454SAndrew Thompson 
286002ac6454SAndrew Thompson 			/* check if there is any data to be transferred */
286102ac6454SAndrew Thompson 			if (itd_offset[0] != buf_offset) {
286202ac6454SAndrew Thompson 				page_no = 0;
286302ac6454SAndrew Thompson 				itd_offset[td_no] = buf_offset;
286402ac6454SAndrew Thompson 
286502ac6454SAndrew Thompson 				/* get first page offset */
2866a593f6b8SAndrew Thompson 				usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
286702ac6454SAndrew Thompson 				/* get page address */
286802ac6454SAndrew Thompson 				page_addr = buf_res.physaddr & ~0xFFF;
286902ac6454SAndrew Thompson 				/* update page address */
2870e55e1ebcSAndrew Thompson 				td->itd_bp[0] &= htohc32(sc, 0xFFF);
2871e55e1ebcSAndrew Thompson 				td->itd_bp[0] |= htohc32(sc, page_addr);
287202ac6454SAndrew Thompson 
287302ac6454SAndrew Thompson 				for (x = 0; x != td_no; x++) {
287402ac6454SAndrew Thompson 					/* set page number and page offset */
287502ac6454SAndrew Thompson 					status = (EHCI_ITD_SET_PG(page_no) |
287602ac6454SAndrew Thompson 					    (buf_res.physaddr & 0xFFF));
2877e55e1ebcSAndrew Thompson 					td->itd_status[x] |= htohc32(sc, status);
287802ac6454SAndrew Thompson 
287902ac6454SAndrew Thompson 					/* get next page offset */
288002ac6454SAndrew Thompson 					if (itd_offset[x + 1] == buf_offset) {
288102ac6454SAndrew Thompson 						/*
288202ac6454SAndrew Thompson 						 * We subtract one so that
288302ac6454SAndrew Thompson 						 * we don't go off the last
288402ac6454SAndrew Thompson 						 * page!
288502ac6454SAndrew Thompson 						 */
2886a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
288702ac6454SAndrew Thompson 					} else {
2888a593f6b8SAndrew Thompson 						usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
288902ac6454SAndrew Thompson 					}
289002ac6454SAndrew Thompson 
289102ac6454SAndrew Thompson 					/* check if we need a new page */
289202ac6454SAndrew Thompson 					if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
289302ac6454SAndrew Thompson 						/* new page needed */
289402ac6454SAndrew Thompson 						page_addr = buf_res.physaddr & ~0xFFF;
289502ac6454SAndrew Thompson 						if (page_no == 6) {
289602ac6454SAndrew Thompson 							panic("%s: too many pages\n", __FUNCTION__);
289702ac6454SAndrew Thompson 						}
289802ac6454SAndrew Thompson 						page_no++;
289902ac6454SAndrew Thompson 						/* update page address */
2900e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
2901e55e1ebcSAndrew Thompson 						td->itd_bp[page_no] |= htohc32(sc, page_addr);
290202ac6454SAndrew Thompson 					}
290302ac6454SAndrew Thompson 				}
290402ac6454SAndrew Thompson 			}
290502ac6454SAndrew Thompson 			/* set IOC bit if we are complete */
290602ac6454SAndrew Thompson 			if (nframes == 0) {
2907e55e1ebcSAndrew Thompson 				td->itd_status[7] |= htohc32(sc, EHCI_ITD_IOC);
290802ac6454SAndrew Thompson 			}
2909a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
291002ac6454SAndrew Thompson #if USB_DEBUG
291102ac6454SAndrew Thompson 			if (ehcidebug > 15) {
291202ac6454SAndrew Thompson 				DPRINTF("HS-TD %d\n", nframes);
291302ac6454SAndrew Thompson 				ehci_dump_itd(sc, td);
291402ac6454SAndrew Thompson 			}
291502ac6454SAndrew Thompson #endif
291602ac6454SAndrew Thompson 			/* insert TD into schedule */
291702ac6454SAndrew Thompson 			EHCI_APPEND_HS_TD(td, *pp_last);
291802ac6454SAndrew Thompson 			pp_last++;
291902ac6454SAndrew Thompson 
292002ac6454SAndrew Thompson 			td_no = 0;
292102ac6454SAndrew Thompson 			td_last = td;
292202ac6454SAndrew Thompson 			td = td->obj_next;
292302ac6454SAndrew Thompson 		}
292402ac6454SAndrew Thompson 	}
292502ac6454SAndrew Thompson 
292602ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
292702ac6454SAndrew Thompson 
292802ac6454SAndrew Thompson 	/* update isoc_next */
2929ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
293002ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
293102ac6454SAndrew Thompson }
293202ac6454SAndrew Thompson 
293302ac6454SAndrew Thompson static void
2934760bc48eSAndrew Thompson ehci_device_isoc_hs_start(struct usb_xfer *xfer)
293502ac6454SAndrew Thompson {
293602ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
293702ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
293802ac6454SAndrew Thompson }
293902ac6454SAndrew Thompson 
2940760bc48eSAndrew Thompson struct usb_pipe_methods ehci_device_isoc_hs_methods =
294102ac6454SAndrew Thompson {
294202ac6454SAndrew Thompson 	.open = ehci_device_isoc_hs_open,
294302ac6454SAndrew Thompson 	.close = ehci_device_isoc_hs_close,
294402ac6454SAndrew Thompson 	.enter = ehci_device_isoc_hs_enter,
294502ac6454SAndrew Thompson 	.start = ehci_device_isoc_hs_start,
294602ac6454SAndrew Thompson };
294702ac6454SAndrew Thompson 
294802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
294902ac6454SAndrew Thompson  * ehci root control support
295002ac6454SAndrew Thompson  *------------------------------------------------------------------------*
295139307315SAndrew Thompson  * Simulate a hardware hub by handling all the necessary requests.
295202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
295302ac6454SAndrew Thompson 
295402ac6454SAndrew Thompson static const
2955760bc48eSAndrew Thompson struct usb_device_descriptor ehci_devd =
295602ac6454SAndrew Thompson {
2957760bc48eSAndrew Thompson 	sizeof(struct usb_device_descriptor),
295802ac6454SAndrew Thompson 	UDESC_DEVICE,			/* type */
295902ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
296002ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
296102ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
296202ac6454SAndrew Thompson 	UDPROTO_HSHUBSTT,		/* protocol */
296302ac6454SAndrew Thompson 	64,				/* max packet */
296402ac6454SAndrew Thompson 	{0}, {0}, {0x00, 0x01},		/* device id */
296502ac6454SAndrew Thompson 	1, 2, 0,			/* string indicies */
296602ac6454SAndrew Thompson 	1				/* # of configurations */
296702ac6454SAndrew Thompson };
296802ac6454SAndrew Thompson 
296902ac6454SAndrew Thompson static const
2970760bc48eSAndrew Thompson struct usb_device_qualifier ehci_odevd =
297102ac6454SAndrew Thompson {
2972760bc48eSAndrew Thompson 	sizeof(struct usb_device_qualifier),
297302ac6454SAndrew Thompson 	UDESC_DEVICE_QUALIFIER,		/* type */
297402ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
297502ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
297602ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
297702ac6454SAndrew Thompson 	UDPROTO_FSHUB,			/* protocol */
297802ac6454SAndrew Thompson 	0,				/* max packet */
297902ac6454SAndrew Thompson 	0,				/* # of configurations */
298002ac6454SAndrew Thompson 	0
298102ac6454SAndrew Thompson };
298202ac6454SAndrew Thompson 
298302ac6454SAndrew Thompson static const struct ehci_config_desc ehci_confd = {
298402ac6454SAndrew Thompson 	.confd = {
2985760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_config_descriptor),
298602ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
298702ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(ehci_confd),
298802ac6454SAndrew Thompson 		.bNumInterface = 1,
298902ac6454SAndrew Thompson 		.bConfigurationValue = 1,
299002ac6454SAndrew Thompson 		.iConfiguration = 0,
299102ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
299202ac6454SAndrew Thompson 		.bMaxPower = 0		/* max power */
299302ac6454SAndrew Thompson 	},
299402ac6454SAndrew Thompson 	.ifcd = {
2995760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_interface_descriptor),
299602ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
299702ac6454SAndrew Thompson 		.bNumEndpoints = 1,
299802ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
299902ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
300002ac6454SAndrew Thompson 		.bInterfaceProtocol = UIPROTO_HSHUBSTT,
300102ac6454SAndrew Thompson 		0
300202ac6454SAndrew Thompson 	},
300302ac6454SAndrew Thompson 	.endpd = {
3004760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_endpoint_descriptor),
300502ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
300602ac6454SAndrew Thompson 		.bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
300702ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
300802ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
300902ac6454SAndrew Thompson 		.bInterval = 255,
301002ac6454SAndrew Thompson 	},
301102ac6454SAndrew Thompson };
301202ac6454SAndrew Thompson 
301302ac6454SAndrew Thompson static const
3014760bc48eSAndrew Thompson struct usb_hub_descriptor ehci_hubd =
301502ac6454SAndrew Thompson {
301602ac6454SAndrew Thompson 	0,				/* dynamic length */
301702ac6454SAndrew Thompson 	UDESC_HUB,
301802ac6454SAndrew Thompson 	0,
301902ac6454SAndrew Thompson 	{0, 0},
302002ac6454SAndrew Thompson 	0,
302102ac6454SAndrew Thompson 	0,
302202ac6454SAndrew Thompson 	{0},
302302ac6454SAndrew Thompson };
302402ac6454SAndrew Thompson 
302502ac6454SAndrew Thompson static void
302602ac6454SAndrew Thompson ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
302702ac6454SAndrew Thompson {
302802ac6454SAndrew Thompson 	uint32_t port;
302902ac6454SAndrew Thompson 	uint32_t v;
303002ac6454SAndrew Thompson 
303102ac6454SAndrew Thompson 	DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
303202ac6454SAndrew Thompson 
303302ac6454SAndrew Thompson 	port = EHCI_PORTSC(index);
303402ac6454SAndrew Thompson 	v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
303502ac6454SAndrew Thompson 	EOWRITE4(sc, port, v | EHCI_PS_PO);
303602ac6454SAndrew Thompson }
303702ac6454SAndrew Thompson 
3038e0a69b51SAndrew Thompson static usb_error_t
3039760bc48eSAndrew Thompson ehci_roothub_exec(struct usb_device *udev,
3040760bc48eSAndrew Thompson     struct usb_device_request *req, const void **pptr, uint16_t *plength)
304102ac6454SAndrew Thompson {
3042459d369eSAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3043459d369eSAndrew Thompson 	const char *str_ptr;
3044459d369eSAndrew Thompson 	const void *ptr;
304502ac6454SAndrew Thompson 	uint32_t port;
304602ac6454SAndrew Thompson 	uint32_t v;
3047459d369eSAndrew Thompson 	uint16_t len;
304802ac6454SAndrew Thompson 	uint16_t i;
304902ac6454SAndrew Thompson 	uint16_t value;
305002ac6454SAndrew Thompson 	uint16_t index;
305102ac6454SAndrew Thompson 	uint8_t l;
3052e0a69b51SAndrew Thompson 	usb_error_t err;
305302ac6454SAndrew Thompson 
305402ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
305502ac6454SAndrew Thompson 
305602ac6454SAndrew Thompson 	/* buffer reset */
3057459d369eSAndrew Thompson 	ptr = (const void *)&sc->sc_hub_desc;
3058459d369eSAndrew Thompson 	len = 0;
3059459d369eSAndrew Thompson 	err = 0;
306002ac6454SAndrew Thompson 
3061459d369eSAndrew Thompson 	value = UGETW(req->wValue);
3062459d369eSAndrew Thompson 	index = UGETW(req->wIndex);
306302ac6454SAndrew Thompson 
306402ac6454SAndrew Thompson 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
306502ac6454SAndrew Thompson 	    "wValue=0x%04x wIndex=0x%04x\n",
3066459d369eSAndrew Thompson 	    req->bmRequestType, req->bRequest,
3067459d369eSAndrew Thompson 	    UGETW(req->wLength), value, index);
306802ac6454SAndrew Thompson 
306902ac6454SAndrew Thompson #define	C(x,y) ((x) | ((y) << 8))
3070459d369eSAndrew Thompson 	switch (C(req->bRequest, req->bmRequestType)) {
307102ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
307202ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
307302ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
307402ac6454SAndrew Thompson 		/*
307502ac6454SAndrew Thompson 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
307602ac6454SAndrew Thompson 		 * for the integrated root hub.
307702ac6454SAndrew Thompson 		 */
307802ac6454SAndrew Thompson 		break;
307902ac6454SAndrew Thompson 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
3080459d369eSAndrew Thompson 		len = 1;
308102ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
308202ac6454SAndrew Thompson 		break;
308302ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
308402ac6454SAndrew Thompson 		switch (value >> 8) {
308502ac6454SAndrew Thompson 		case UDESC_DEVICE:
308602ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3087459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
308802ac6454SAndrew Thompson 				goto done;
308902ac6454SAndrew Thompson 			}
3090459d369eSAndrew Thompson 			len = sizeof(ehci_devd);
3091459d369eSAndrew Thompson 			ptr = (const void *)&ehci_devd;
309202ac6454SAndrew Thompson 			break;
309302ac6454SAndrew Thompson 			/*
309402ac6454SAndrew Thompson 			 * We can't really operate at another speed,
309502ac6454SAndrew Thompson 			 * but the specification says we need this
309602ac6454SAndrew Thompson 			 * descriptor:
309702ac6454SAndrew Thompson 			 */
309802ac6454SAndrew Thompson 		case UDESC_DEVICE_QUALIFIER:
309902ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3100459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
310102ac6454SAndrew Thompson 				goto done;
310202ac6454SAndrew Thompson 			}
3103459d369eSAndrew Thompson 			len = sizeof(ehci_odevd);
3104459d369eSAndrew Thompson 			ptr = (const void *)&ehci_odevd;
310502ac6454SAndrew Thompson 			break;
310602ac6454SAndrew Thompson 
310702ac6454SAndrew Thompson 		case UDESC_CONFIG:
310802ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
3109459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
311002ac6454SAndrew Thompson 				goto done;
311102ac6454SAndrew Thompson 			}
3112459d369eSAndrew Thompson 			len = sizeof(ehci_confd);
3113459d369eSAndrew Thompson 			ptr = (const void *)&ehci_confd;
311402ac6454SAndrew Thompson 			break;
311502ac6454SAndrew Thompson 
311602ac6454SAndrew Thompson 		case UDESC_STRING:
311702ac6454SAndrew Thompson 			switch (value & 0xff) {
311802ac6454SAndrew Thompson 			case 0:	/* Language table */
3119459d369eSAndrew Thompson 				str_ptr = "\001";
312002ac6454SAndrew Thompson 				break;
312102ac6454SAndrew Thompson 
312202ac6454SAndrew Thompson 			case 1:	/* Vendor */
3123459d369eSAndrew Thompson 				str_ptr = sc->sc_vendor;
312402ac6454SAndrew Thompson 				break;
312502ac6454SAndrew Thompson 
312602ac6454SAndrew Thompson 			case 2:	/* Product */
3127459d369eSAndrew Thompson 				str_ptr = "EHCI root HUB";
312802ac6454SAndrew Thompson 				break;
312902ac6454SAndrew Thompson 
313002ac6454SAndrew Thompson 			default:
3131459d369eSAndrew Thompson 				str_ptr = "";
313202ac6454SAndrew Thompson 				break;
313302ac6454SAndrew Thompson 			}
313402ac6454SAndrew Thompson 
3135a593f6b8SAndrew Thompson 			len = usb_make_str_desc(
3136459d369eSAndrew Thompson 			    sc->sc_hub_desc.temp,
313702ac6454SAndrew Thompson 			    sizeof(sc->sc_hub_desc.temp),
3138459d369eSAndrew Thompson 			    str_ptr);
313902ac6454SAndrew Thompson 			break;
314002ac6454SAndrew Thompson 		default:
3141459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
314202ac6454SAndrew Thompson 			goto done;
314302ac6454SAndrew Thompson 		}
314402ac6454SAndrew Thompson 		break;
314502ac6454SAndrew Thompson 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3146459d369eSAndrew Thompson 		len = 1;
314702ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = 0;
314802ac6454SAndrew Thompson 		break;
314902ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_DEVICE):
3150459d369eSAndrew Thompson 		len = 2;
315102ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
315202ac6454SAndrew Thompson 		break;
315302ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
315402ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3155459d369eSAndrew Thompson 		len = 2;
315602ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
315702ac6454SAndrew Thompson 		break;
315802ac6454SAndrew Thompson 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3159ab42e8b2SAndrew Thompson 		if (value >= EHCI_MAX_DEVICES) {
3160459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
316102ac6454SAndrew Thompson 			goto done;
316202ac6454SAndrew Thompson 		}
316302ac6454SAndrew Thompson 		sc->sc_addr = value;
316402ac6454SAndrew Thompson 		break;
316502ac6454SAndrew Thompson 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
316602ac6454SAndrew Thompson 		if ((value != 0) && (value != 1)) {
3167459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
316802ac6454SAndrew Thompson 			goto done;
316902ac6454SAndrew Thompson 		}
317002ac6454SAndrew Thompson 		sc->sc_conf = value;
317102ac6454SAndrew Thompson 		break;
317202ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
317302ac6454SAndrew Thompson 		break;
317402ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
317502ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
317602ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3177459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
317802ac6454SAndrew Thompson 		goto done;
317902ac6454SAndrew Thompson 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
318002ac6454SAndrew Thompson 		break;
318102ac6454SAndrew Thompson 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
318202ac6454SAndrew Thompson 		break;
318302ac6454SAndrew Thompson 		/* Hub requests */
318402ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
318502ac6454SAndrew Thompson 		break;
318602ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
318702ac6454SAndrew Thompson 		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
318802ac6454SAndrew Thompson 
318902ac6454SAndrew Thompson 		if ((index < 1) ||
319002ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3191459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
319202ac6454SAndrew Thompson 			goto done;
319302ac6454SAndrew Thompson 		}
319402ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
319502ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
319602ac6454SAndrew Thompson 		switch (value) {
319702ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
319802ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PE);
319902ac6454SAndrew Thompson 			break;
320002ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
320102ac6454SAndrew Thompson 			if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
320202ac6454SAndrew Thompson 
320302ac6454SAndrew Thompson 				/*
320402ac6454SAndrew Thompson 				 * waking up a High Speed device is rather
320502ac6454SAndrew Thompson 				 * complicated if
320602ac6454SAndrew Thompson 				 */
320702ac6454SAndrew Thompson 				EOWRITE4(sc, port, v | EHCI_PS_FPR);
320802ac6454SAndrew Thompson 			}
320902ac6454SAndrew Thompson 			/* wait 20ms for resume sequence to complete */
3210a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
321102ac6454SAndrew Thompson 
321202ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
321302ac6454SAndrew Thompson 			    EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
321402ac6454SAndrew Thompson 
32154a35cda7SAndrew Thompson 			/* 4ms settle time */
3216a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
321702ac6454SAndrew Thompson 			break;
321802ac6454SAndrew Thompson 		case UHF_PORT_POWER:
321902ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PP);
322002ac6454SAndrew Thompson 			break;
322102ac6454SAndrew Thompson 		case UHF_PORT_TEST:
322202ac6454SAndrew Thompson 			DPRINTFN(3, "clear port test "
322302ac6454SAndrew Thompson 			    "%d\n", index);
322402ac6454SAndrew Thompson 			break;
322502ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
322602ac6454SAndrew Thompson 			DPRINTFN(3, "clear port ind "
322702ac6454SAndrew Thompson 			    "%d\n", index);
322802ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
322902ac6454SAndrew Thompson 			break;
323002ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
323102ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
323202ac6454SAndrew Thompson 			break;
323302ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
323402ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
323502ac6454SAndrew Thompson 			break;
323602ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
323702ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
323802ac6454SAndrew Thompson 			break;
323902ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
324002ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
324102ac6454SAndrew Thompson 			break;
324202ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
324302ac6454SAndrew Thompson 			sc->sc_isreset = 0;
324402ac6454SAndrew Thompson 			break;
324502ac6454SAndrew Thompson 		default:
3246459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
324702ac6454SAndrew Thompson 			goto done;
324802ac6454SAndrew Thompson 		}
324902ac6454SAndrew Thompson 		break;
325002ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
325102ac6454SAndrew Thompson 		if ((value & 0xff) != 0) {
3252459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
325302ac6454SAndrew Thompson 			goto done;
325402ac6454SAndrew Thompson 		}
325502ac6454SAndrew Thompson 		v = EOREAD4(sc, EHCI_HCSPARAMS);
325602ac6454SAndrew Thompson 
325702ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd = ehci_hubd;
325802ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
325902ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics,
326002ac6454SAndrew Thompson 		    (EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH) |
326102ac6454SAndrew Thompson 		    (EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS)) ?
326202ac6454SAndrew Thompson 		    UHD_PORT_IND : 0));
326302ac6454SAndrew Thompson 		/* XXX can't find out? */
326402ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
326502ac6454SAndrew Thompson 		for (l = 0; l < sc->sc_noport; l++) {
326602ac6454SAndrew Thompson 			/* XXX can't find out? */
326702ac6454SAndrew Thompson 			sc->sc_hub_desc.hubd.DeviceRemovable[l / 8] &= ~(1 << (l % 8));
326802ac6454SAndrew Thompson 		}
326902ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bDescLength =
327002ac6454SAndrew Thompson 		    8 + ((sc->sc_noport + 7) / 8);
3271459d369eSAndrew Thompson 		len = sc->sc_hub_desc.hubd.bDescLength;
327202ac6454SAndrew Thompson 		break;
327302ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3274459d369eSAndrew Thompson 		len = 16;
327502ac6454SAndrew Thompson 		bzero(sc->sc_hub_desc.temp, 16);
327602ac6454SAndrew Thompson 		break;
327702ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
327802ac6454SAndrew Thompson 		DPRINTFN(9, "get port status i=%d\n",
327902ac6454SAndrew Thompson 		    index);
328002ac6454SAndrew Thompson 		if ((index < 1) ||
328102ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3282459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
328302ac6454SAndrew Thompson 			goto done;
328402ac6454SAndrew Thompson 		}
328502ac6454SAndrew Thompson 		v = EOREAD4(sc, EHCI_PORTSC(index));
328602ac6454SAndrew Thompson 		DPRINTFN(9, "port status=0x%04x\n", v);
3287e55e1ebcSAndrew Thompson 		if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) {
328802ac6454SAndrew Thompson 			if ((v & 0xc000000) == 0x8000000)
328902ac6454SAndrew Thompson 				i = UPS_HIGH_SPEED;
329002ac6454SAndrew Thompson 			else if ((v & 0xc000000) == 0x4000000)
329102ac6454SAndrew Thompson 				i = UPS_LOW_SPEED;
329202ac6454SAndrew Thompson 			else
329302ac6454SAndrew Thompson 				i = 0;
329402ac6454SAndrew Thompson 		} else {
329502ac6454SAndrew Thompson 			i = UPS_HIGH_SPEED;
329602ac6454SAndrew Thompson 		}
329702ac6454SAndrew Thompson 		if (v & EHCI_PS_CS)
329802ac6454SAndrew Thompson 			i |= UPS_CURRENT_CONNECT_STATUS;
329902ac6454SAndrew Thompson 		if (v & EHCI_PS_PE)
330002ac6454SAndrew Thompson 			i |= UPS_PORT_ENABLED;
330102ac6454SAndrew Thompson 		if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
330202ac6454SAndrew Thompson 			i |= UPS_SUSPEND;
330302ac6454SAndrew Thompson 		if (v & EHCI_PS_OCA)
330402ac6454SAndrew Thompson 			i |= UPS_OVERCURRENT_INDICATOR;
330502ac6454SAndrew Thompson 		if (v & EHCI_PS_PR)
330602ac6454SAndrew Thompson 			i |= UPS_RESET;
330702ac6454SAndrew Thompson 		if (v & EHCI_PS_PP)
330802ac6454SAndrew Thompson 			i |= UPS_PORT_POWER;
330902ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortStatus, i);
331002ac6454SAndrew Thompson 		i = 0;
331102ac6454SAndrew Thompson 		if (v & EHCI_PS_CSC)
331202ac6454SAndrew Thompson 			i |= UPS_C_CONNECT_STATUS;
331302ac6454SAndrew Thompson 		if (v & EHCI_PS_PEC)
331402ac6454SAndrew Thompson 			i |= UPS_C_PORT_ENABLED;
331502ac6454SAndrew Thompson 		if (v & EHCI_PS_OCC)
331602ac6454SAndrew Thompson 			i |= UPS_C_OVERCURRENT_INDICATOR;
331702ac6454SAndrew Thompson 		if (v & EHCI_PS_FPR)
331802ac6454SAndrew Thompson 			i |= UPS_C_SUSPEND;
331902ac6454SAndrew Thompson 		if (sc->sc_isreset)
332002ac6454SAndrew Thompson 			i |= UPS_C_PORT_RESET;
332102ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortChange, i);
3322459d369eSAndrew Thompson 		len = sizeof(sc->sc_hub_desc.ps);
332302ac6454SAndrew Thompson 		break;
332402ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3325459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
332602ac6454SAndrew Thompson 		goto done;
332702ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
332802ac6454SAndrew Thompson 		break;
332902ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
333002ac6454SAndrew Thompson 		if ((index < 1) ||
333102ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
3332459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
333302ac6454SAndrew Thompson 			goto done;
333402ac6454SAndrew Thompson 		}
333502ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
333602ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
333702ac6454SAndrew Thompson 		switch (value) {
333802ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
333902ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PE);
334002ac6454SAndrew Thompson 			break;
334102ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
334202ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
334302ac6454SAndrew Thompson 			break;
334402ac6454SAndrew Thompson 		case UHF_PORT_RESET:
334502ac6454SAndrew Thompson 			DPRINTFN(6, "reset port %d\n", index);
334602ac6454SAndrew Thompson #if USB_DEBUG
334702ac6454SAndrew Thompson 			if (ehcinohighspeed) {
334802ac6454SAndrew Thompson 				/*
334902ac6454SAndrew Thompson 				 * Connect USB device to companion
335002ac6454SAndrew Thompson 				 * controller.
335102ac6454SAndrew Thompson 				 */
335202ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
335302ac6454SAndrew Thompson 				break;
335402ac6454SAndrew Thompson 			}
335502ac6454SAndrew Thompson #endif
3356e55e1ebcSAndrew Thompson 			if (EHCI_PS_IS_LOWSPEED(v) &&
3357e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
335802ac6454SAndrew Thompson 				/* Low speed device, give up ownership. */
335902ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
336002ac6454SAndrew Thompson 				break;
336102ac6454SAndrew Thompson 			}
336202ac6454SAndrew Thompson 			/* Start reset sequence. */
336302ac6454SAndrew Thompson 			v &= ~(EHCI_PS_PE | EHCI_PS_PR);
336402ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PR);
336502ac6454SAndrew Thompson 
336602ac6454SAndrew Thompson 			/* Wait for reset to complete. */
3367a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
336802ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
336902ac6454SAndrew Thompson 
337002ac6454SAndrew Thompson 			/* Terminate reset sequence. */
337102ac6454SAndrew Thompson 			if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
337202ac6454SAndrew Thompson 				EOWRITE4(sc, port, v);
337302ac6454SAndrew Thompson 
337402ac6454SAndrew Thompson 			/* Wait for HC to complete reset. */
3375a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx,
337602ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
337702ac6454SAndrew Thompson 
337802ac6454SAndrew Thompson 			v = EOREAD4(sc, port);
337902ac6454SAndrew Thompson 			DPRINTF("ehci after reset, status=0x%08x\n", v);
338002ac6454SAndrew Thompson 			if (v & EHCI_PS_PR) {
338102ac6454SAndrew Thompson 				device_printf(sc->sc_bus.bdev,
338202ac6454SAndrew Thompson 				    "port reset timeout\n");
3383459d369eSAndrew Thompson 				err = USB_ERR_TIMEOUT;
338402ac6454SAndrew Thompson 				goto done;
338502ac6454SAndrew Thompson 			}
3386e55e1ebcSAndrew Thompson 			if (!(v & EHCI_PS_PE) &&
3387e55e1ebcSAndrew Thompson 			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
3388e55e1ebcSAndrew Thompson 				/* Not a high speed device, give up ownership.*/
338902ac6454SAndrew Thompson 				ehci_disown(sc, index, 0);
339002ac6454SAndrew Thompson 				break;
339102ac6454SAndrew Thompson 			}
339202ac6454SAndrew Thompson 			sc->sc_isreset = 1;
339302ac6454SAndrew Thompson 			DPRINTF("ehci port %d reset, status = 0x%08x\n",
339402ac6454SAndrew Thompson 			    index, v);
339502ac6454SAndrew Thompson 			break;
339602ac6454SAndrew Thompson 
339702ac6454SAndrew Thompson 		case UHF_PORT_POWER:
339802ac6454SAndrew Thompson 			DPRINTFN(3, "set port power %d\n", index);
339902ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PP);
340002ac6454SAndrew Thompson 			break;
340102ac6454SAndrew Thompson 
340202ac6454SAndrew Thompson 		case UHF_PORT_TEST:
340302ac6454SAndrew Thompson 			DPRINTFN(3, "set port test %d\n", index);
340402ac6454SAndrew Thompson 			break;
340502ac6454SAndrew Thompson 
340602ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
340702ac6454SAndrew Thompson 			DPRINTFN(3, "set port ind %d\n", index);
340802ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
340902ac6454SAndrew Thompson 			break;
341002ac6454SAndrew Thompson 
341102ac6454SAndrew Thompson 		default:
3412459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
341302ac6454SAndrew Thompson 			goto done;
341402ac6454SAndrew Thompson 		}
341502ac6454SAndrew Thompson 		break;
341602ac6454SAndrew Thompson 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
341702ac6454SAndrew Thompson 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
341802ac6454SAndrew Thompson 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
341902ac6454SAndrew Thompson 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
342002ac6454SAndrew Thompson 		break;
342102ac6454SAndrew Thompson 	default:
3422459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
342302ac6454SAndrew Thompson 		goto done;
342402ac6454SAndrew Thompson 	}
342502ac6454SAndrew Thompson done:
3426459d369eSAndrew Thompson 	*plength = len;
3427459d369eSAndrew Thompson 	*pptr = ptr;
3428459d369eSAndrew Thompson 	return (err);
342902ac6454SAndrew Thompson }
343002ac6454SAndrew Thompson 
343102ac6454SAndrew Thompson static void
3432760bc48eSAndrew Thompson ehci_xfer_setup(struct usb_setup_params *parm)
343302ac6454SAndrew Thompson {
3434760bc48eSAndrew Thompson 	struct usb_page_search page_info;
3435760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
343602ac6454SAndrew Thompson 	ehci_softc_t *sc;
3437760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
343802ac6454SAndrew Thompson 	void *last_obj;
343902ac6454SAndrew Thompson 	uint32_t nqtd;
344002ac6454SAndrew Thompson 	uint32_t nqh;
344102ac6454SAndrew Thompson 	uint32_t nsitd;
344202ac6454SAndrew Thompson 	uint32_t nitd;
344302ac6454SAndrew Thompson 	uint32_t n;
344402ac6454SAndrew Thompson 
344502ac6454SAndrew Thompson 	sc = EHCI_BUS2SC(parm->udev->bus);
344602ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
344702ac6454SAndrew Thompson 
344802ac6454SAndrew Thompson 	nqtd = 0;
344902ac6454SAndrew Thompson 	nqh = 0;
345002ac6454SAndrew Thompson 	nsitd = 0;
345102ac6454SAndrew Thompson 	nitd = 0;
345202ac6454SAndrew Thompson 
345302ac6454SAndrew Thompson 	/*
345402ac6454SAndrew Thompson 	 * compute maximum number of some structures
345502ac6454SAndrew Thompson 	 */
345602ac6454SAndrew Thompson 	if (parm->methods == &ehci_device_ctrl_methods) {
345702ac6454SAndrew Thompson 
345802ac6454SAndrew Thompson 		/*
345902ac6454SAndrew Thompson 		 * The proof for the "nqtd" formula is illustrated like
346002ac6454SAndrew Thompson 		 * this:
346102ac6454SAndrew Thompson 		 *
346202ac6454SAndrew Thompson 		 * +------------------------------------+
346302ac6454SAndrew Thompson 		 * |                                    |
346402ac6454SAndrew Thompson 		 * |         |remainder ->              |
346502ac6454SAndrew Thompson 		 * |   +-----+---+                      |
346602ac6454SAndrew Thompson 		 * |   | xxx | x | frm 0                |
346702ac6454SAndrew Thompson 		 * |   +-----+---++                     |
346802ac6454SAndrew Thompson 		 * |   | xxx | xx | frm 1               |
346902ac6454SAndrew Thompson 		 * |   +-----+----+                     |
347002ac6454SAndrew Thompson 		 * |            ...                     |
347102ac6454SAndrew Thompson 		 * +------------------------------------+
347202ac6454SAndrew Thompson 		 *
347302ac6454SAndrew Thompson 		 * "xxx" means a completely full USB transfer descriptor
347402ac6454SAndrew Thompson 		 *
347502ac6454SAndrew Thompson 		 * "x" and "xx" means a short USB packet
347602ac6454SAndrew Thompson 		 *
347702ac6454SAndrew Thompson 		 * For the remainder of an USB transfer modulo
347802ac6454SAndrew Thompson 		 * "max_data_length" we need two USB transfer descriptors.
347902ac6454SAndrew Thompson 		 * One to transfer the remaining data and one to finalise
348002ac6454SAndrew Thompson 		 * with a zero length packet in case the "force_short_xfer"
348102ac6454SAndrew Thompson 		 * flag is set. We only need two USB transfer descriptors in
348202ac6454SAndrew Thompson 		 * the case where the transfer length of the first one is a
348302ac6454SAndrew Thompson 		 * factor of "max_frame_size". The rest of the needed USB
348402ac6454SAndrew Thompson 		 * transfer descriptors is given by the buffer size divided
348502ac6454SAndrew Thompson 		 * by the maximum data payload.
348602ac6454SAndrew Thompson 		 */
348702ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
348802ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
348902ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
349002ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
349102ac6454SAndrew Thompson 
3492a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
349302ac6454SAndrew Thompson 
349402ac6454SAndrew Thompson 		nqh = 1;
349502ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes) + 1	/* STATUS */
3496578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
349702ac6454SAndrew Thompson 
349802ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_bulk_methods) {
349902ac6454SAndrew Thompson 
350002ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
350102ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
350202ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
350302ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
350402ac6454SAndrew Thompson 
3505a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
350602ac6454SAndrew Thompson 
350702ac6454SAndrew Thompson 		nqh = 1;
350802ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3509578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
351002ac6454SAndrew Thompson 
351102ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_intr_methods) {
351202ac6454SAndrew Thompson 
351302ac6454SAndrew Thompson 		if (parm->speed == USB_SPEED_HIGH) {
351402ac6454SAndrew Thompson 			parm->hc_max_packet_size = 0x400;
351502ac6454SAndrew Thompson 			parm->hc_max_packet_count = 3;
351602ac6454SAndrew Thompson 		} else if (parm->speed == USB_SPEED_FULL) {
351702ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
351802ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
351902ac6454SAndrew Thompson 		} else {
352002ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
352102ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
352202ac6454SAndrew Thompson 		}
352302ac6454SAndrew Thompson 
352402ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
352502ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
352602ac6454SAndrew Thompson 
3527a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
352802ac6454SAndrew Thompson 
352902ac6454SAndrew Thompson 		nqh = 1;
353002ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
3531578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
353202ac6454SAndrew Thompson 
353302ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_fs_methods) {
353402ac6454SAndrew Thompson 
353502ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x3FF;
353602ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
353702ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x3FF;
353802ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
353902ac6454SAndrew Thompson 
3540a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
354102ac6454SAndrew Thompson 
354202ac6454SAndrew Thompson 		nsitd = xfer->nframes;
354302ac6454SAndrew Thompson 
354402ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_hs_methods) {
354502ac6454SAndrew Thompson 
354602ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
354702ac6454SAndrew Thompson 		parm->hc_max_packet_count = 3;
354802ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0xC00;
354902ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
355002ac6454SAndrew Thompson 
3551a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
355202ac6454SAndrew Thompson 
355302ac6454SAndrew Thompson 		nitd = (xfer->nframes + 7) / 8;
355402ac6454SAndrew Thompson 
355502ac6454SAndrew Thompson 	} else {
355602ac6454SAndrew Thompson 
355702ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
355802ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
355902ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x400;
356002ac6454SAndrew Thompson 
3561a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
356202ac6454SAndrew Thompson 	}
356302ac6454SAndrew Thompson 
356402ac6454SAndrew Thompson alloc_dma_set:
356502ac6454SAndrew Thompson 
356602ac6454SAndrew Thompson 	if (parm->err) {
356702ac6454SAndrew Thompson 		return;
356802ac6454SAndrew Thompson 	}
356902ac6454SAndrew Thompson 	/*
357002ac6454SAndrew Thompson 	 * Allocate queue heads and transfer descriptors
357102ac6454SAndrew Thompson 	 */
357202ac6454SAndrew Thompson 	last_obj = NULL;
357302ac6454SAndrew Thompson 
3574a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
357502ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_itd_t),
357602ac6454SAndrew Thompson 	    EHCI_ITD_ALIGN, nitd)) {
357702ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
357802ac6454SAndrew Thompson 		return;
357902ac6454SAndrew Thompson 	}
358002ac6454SAndrew Thompson 	if (parm->buf) {
358102ac6454SAndrew Thompson 		for (n = 0; n != nitd; n++) {
358202ac6454SAndrew Thompson 			ehci_itd_t *td;
358302ac6454SAndrew Thompson 
3584a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
358502ac6454SAndrew Thompson 
358602ac6454SAndrew Thompson 			td = page_info.buffer;
358702ac6454SAndrew Thompson 
358802ac6454SAndrew Thompson 			/* init TD */
3589e55e1ebcSAndrew Thompson 			td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
359002ac6454SAndrew Thompson 			td->obj_next = last_obj;
359102ac6454SAndrew Thompson 			td->page_cache = pc + n;
359202ac6454SAndrew Thompson 
359302ac6454SAndrew Thompson 			last_obj = td;
359402ac6454SAndrew Thompson 
3595a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
359602ac6454SAndrew Thompson 		}
359702ac6454SAndrew Thompson 	}
3598a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
359902ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_sitd_t),
360002ac6454SAndrew Thompson 	    EHCI_SITD_ALIGN, nsitd)) {
360102ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
360202ac6454SAndrew Thompson 		return;
360302ac6454SAndrew Thompson 	}
360402ac6454SAndrew Thompson 	if (parm->buf) {
360502ac6454SAndrew Thompson 		for (n = 0; n != nsitd; n++) {
360602ac6454SAndrew Thompson 			ehci_sitd_t *td;
360702ac6454SAndrew Thompson 
3608a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
360902ac6454SAndrew Thompson 
361002ac6454SAndrew Thompson 			td = page_info.buffer;
361102ac6454SAndrew Thompson 
361202ac6454SAndrew Thompson 			/* init TD */
3613e55e1ebcSAndrew Thompson 			td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
361402ac6454SAndrew Thompson 			td->obj_next = last_obj;
361502ac6454SAndrew Thompson 			td->page_cache = pc + n;
361602ac6454SAndrew Thompson 
361702ac6454SAndrew Thompson 			last_obj = td;
361802ac6454SAndrew Thompson 
3619a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
362002ac6454SAndrew Thompson 		}
362102ac6454SAndrew Thompson 	}
3622a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
362302ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qtd_t),
362402ac6454SAndrew Thompson 	    EHCI_QTD_ALIGN, nqtd)) {
362502ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
362602ac6454SAndrew Thompson 		return;
362702ac6454SAndrew Thompson 	}
362802ac6454SAndrew Thompson 	if (parm->buf) {
362902ac6454SAndrew Thompson 		for (n = 0; n != nqtd; n++) {
363002ac6454SAndrew Thompson 			ehci_qtd_t *qtd;
363102ac6454SAndrew Thompson 
3632a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
363302ac6454SAndrew Thompson 
363402ac6454SAndrew Thompson 			qtd = page_info.buffer;
363502ac6454SAndrew Thompson 
363602ac6454SAndrew Thompson 			/* init TD */
3637e55e1ebcSAndrew Thompson 			qtd->qtd_self = htohc32(sc, page_info.physaddr);
363802ac6454SAndrew Thompson 			qtd->obj_next = last_obj;
363902ac6454SAndrew Thompson 			qtd->page_cache = pc + n;
364002ac6454SAndrew Thompson 
364102ac6454SAndrew Thompson 			last_obj = qtd;
364202ac6454SAndrew Thompson 
3643a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
364402ac6454SAndrew Thompson 		}
364502ac6454SAndrew Thompson 	}
364602ac6454SAndrew Thompson 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
364702ac6454SAndrew Thompson 
364802ac6454SAndrew Thompson 	last_obj = NULL;
364902ac6454SAndrew Thompson 
3650a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
365102ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qh_t),
365202ac6454SAndrew Thompson 	    EHCI_QH_ALIGN, nqh)) {
365302ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
365402ac6454SAndrew Thompson 		return;
365502ac6454SAndrew Thompson 	}
365602ac6454SAndrew Thompson 	if (parm->buf) {
365702ac6454SAndrew Thompson 		for (n = 0; n != nqh; n++) {
365802ac6454SAndrew Thompson 			ehci_qh_t *qh;
365902ac6454SAndrew Thompson 
3660a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
366102ac6454SAndrew Thompson 
366202ac6454SAndrew Thompson 			qh = page_info.buffer;
366302ac6454SAndrew Thompson 
366402ac6454SAndrew Thompson 			/* init QH */
3665e55e1ebcSAndrew Thompson 			qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
366602ac6454SAndrew Thompson 			qh->obj_next = last_obj;
366702ac6454SAndrew Thompson 			qh->page_cache = pc + n;
366802ac6454SAndrew Thompson 
366902ac6454SAndrew Thompson 			last_obj = qh;
367002ac6454SAndrew Thompson 
3671a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
367202ac6454SAndrew Thompson 		}
367302ac6454SAndrew Thompson 	}
367402ac6454SAndrew Thompson 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
367502ac6454SAndrew Thompson 
367602ac6454SAndrew Thompson 	if (!xfer->flags_int.curr_dma_set) {
367702ac6454SAndrew Thompson 		xfer->flags_int.curr_dma_set = 1;
367802ac6454SAndrew Thompson 		goto alloc_dma_set;
367902ac6454SAndrew Thompson 	}
368002ac6454SAndrew Thompson }
368102ac6454SAndrew Thompson 
368202ac6454SAndrew Thompson static void
3683760bc48eSAndrew Thompson ehci_xfer_unsetup(struct usb_xfer *xfer)
368402ac6454SAndrew Thompson {
368502ac6454SAndrew Thompson 	return;
368602ac6454SAndrew Thompson }
368702ac6454SAndrew Thompson 
368802ac6454SAndrew Thompson static void
3689ae60fdfbSAndrew Thompson ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3690ae60fdfbSAndrew Thompson     struct usb_endpoint *ep)
369102ac6454SAndrew Thompson {
369202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
369302ac6454SAndrew Thompson 
3694ae60fdfbSAndrew Thompson 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3695ae60fdfbSAndrew Thompson 	    ep, udev->address,
3696f29a0724SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb_mode,
369702ac6454SAndrew Thompson 	    sc->sc_addr);
369802ac6454SAndrew Thompson 
3699f29a0724SAndrew Thompson 	if (udev->flags.usb_mode != USB_MODE_HOST) {
370002ac6454SAndrew Thompson 		/* not supported */
370102ac6454SAndrew Thompson 		return;
370202ac6454SAndrew Thompson 	}
370339307315SAndrew Thompson 	if (udev->device_index != sc->sc_addr) {
370439307315SAndrew Thompson 
370502ac6454SAndrew Thompson 		if ((udev->speed != USB_SPEED_HIGH) &&
370602ac6454SAndrew Thompson 		    ((udev->hs_hub_addr == 0) ||
370702ac6454SAndrew Thompson 		    (udev->hs_port_no == 0) ||
3708495f25ceSAndrew Thompson 		    (udev->parent_hs_hub == NULL) ||
3709495f25ceSAndrew Thompson 		    (udev->parent_hs_hub->hub == NULL))) {
371002ac6454SAndrew Thompson 			/* We need a transaction translator */
371102ac6454SAndrew Thompson 			goto done;
371202ac6454SAndrew Thompson 		}
371302ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
371402ac6454SAndrew Thompson 		case UE_CONTROL:
3715ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_ctrl_methods;
371602ac6454SAndrew Thompson 			break;
371702ac6454SAndrew Thompson 		case UE_INTERRUPT:
3718ae60fdfbSAndrew Thompson 			ep->methods = &ehci_device_intr_methods;
371902ac6454SAndrew Thompson 			break;
372002ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
372102ac6454SAndrew Thompson 			if (udev->speed == USB_SPEED_HIGH) {
3722ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_hs_methods;
372302ac6454SAndrew Thompson 			} else if (udev->speed == USB_SPEED_FULL) {
3724ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_isoc_fs_methods;
372502ac6454SAndrew Thompson 			}
372602ac6454SAndrew Thompson 			break;
372702ac6454SAndrew Thompson 		case UE_BULK:
372802ac6454SAndrew Thompson 			if (udev->speed != USB_SPEED_LOW) {
3729ae60fdfbSAndrew Thompson 				ep->methods = &ehci_device_bulk_methods;
373002ac6454SAndrew Thompson 			}
373102ac6454SAndrew Thompson 			break;
373202ac6454SAndrew Thompson 		default:
373302ac6454SAndrew Thompson 			/* do nothing */
373402ac6454SAndrew Thompson 			break;
373502ac6454SAndrew Thompson 		}
373602ac6454SAndrew Thompson 	}
373702ac6454SAndrew Thompson done:
373802ac6454SAndrew Thompson 	return;
373902ac6454SAndrew Thompson }
374002ac6454SAndrew Thompson 
374102ac6454SAndrew Thompson static void
3742760bc48eSAndrew Thompson ehci_get_dma_delay(struct usb_bus *bus, uint32_t *pus)
374302ac6454SAndrew Thompson {
374402ac6454SAndrew Thompson 	/*
374502ac6454SAndrew Thompson 	 * Wait until the hardware has finished any possible use of
374602ac6454SAndrew Thompson 	 * the transfer descriptor(s) and QH
374702ac6454SAndrew Thompson 	 */
374802ac6454SAndrew Thompson 	*pus = (188);			/* microseconds */
374902ac6454SAndrew Thompson }
375002ac6454SAndrew Thompson 
375102ac6454SAndrew Thompson static void
3752760bc48eSAndrew Thompson ehci_device_resume(struct usb_device *udev)
375302ac6454SAndrew Thompson {
375402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3755760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3756760bc48eSAndrew Thompson 	struct usb_pipe_methods *methods;
375702ac6454SAndrew Thompson 
375802ac6454SAndrew Thompson 	DPRINTF("\n");
375902ac6454SAndrew Thompson 
376002ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
376102ac6454SAndrew Thompson 
376202ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
376302ac6454SAndrew Thompson 
376402ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
376502ac6454SAndrew Thompson 
3766ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
376702ac6454SAndrew Thompson 
376802ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
376902ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
377002ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
377102ac6454SAndrew Thompson 				    sc->sc_async_p_last);
377202ac6454SAndrew Thompson 			}
377302ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
377402ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
377502ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
377602ac6454SAndrew Thompson 			}
377702ac6454SAndrew Thompson 		}
377802ac6454SAndrew Thompson 	}
377902ac6454SAndrew Thompson 
378002ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
378102ac6454SAndrew Thompson 
378202ac6454SAndrew Thompson 	return;
378302ac6454SAndrew Thompson }
378402ac6454SAndrew Thompson 
378502ac6454SAndrew Thompson static void
3786760bc48eSAndrew Thompson ehci_device_suspend(struct usb_device *udev)
378702ac6454SAndrew Thompson {
378802ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
3789760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3790760bc48eSAndrew Thompson 	struct usb_pipe_methods *methods;
379102ac6454SAndrew Thompson 
379202ac6454SAndrew Thompson 	DPRINTF("\n");
379302ac6454SAndrew Thompson 
379402ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
379502ac6454SAndrew Thompson 
379602ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
379702ac6454SAndrew Thompson 
379802ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
379902ac6454SAndrew Thompson 
3800ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
380102ac6454SAndrew Thompson 
380202ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
380302ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
380402ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
380502ac6454SAndrew Thompson 				    sc->sc_async_p_last);
380602ac6454SAndrew Thompson 			}
380702ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
380802ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
380902ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
381002ac6454SAndrew Thompson 			}
381102ac6454SAndrew Thompson 		}
381202ac6454SAndrew Thompson 	}
381302ac6454SAndrew Thompson 
381402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
381502ac6454SAndrew Thompson 
381602ac6454SAndrew Thompson 	return;
381702ac6454SAndrew Thompson }
381802ac6454SAndrew Thompson 
381902ac6454SAndrew Thompson static void
3820760bc48eSAndrew Thompson ehci_set_hw_power(struct usb_bus *bus)
382102ac6454SAndrew Thompson {
382202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
382302ac6454SAndrew Thompson 	uint32_t temp;
382402ac6454SAndrew Thompson 	uint32_t flags;
382502ac6454SAndrew Thompson 
382602ac6454SAndrew Thompson 	DPRINTF("\n");
382702ac6454SAndrew Thompson 
382802ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
382902ac6454SAndrew Thompson 
383002ac6454SAndrew Thompson 	flags = bus->hw_power_state;
383102ac6454SAndrew Thompson 
383202ac6454SAndrew Thompson 	temp = EOREAD4(sc, EHCI_USBCMD);
383302ac6454SAndrew Thompson 
383402ac6454SAndrew Thompson 	temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
383502ac6454SAndrew Thompson 
383602ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_CONTROL |
383702ac6454SAndrew Thompson 	    USB_HW_POWER_BULK)) {
383802ac6454SAndrew Thompson 		DPRINTF("Async is active\n");
383902ac6454SAndrew Thompson 		temp |= EHCI_CMD_ASE;
384002ac6454SAndrew Thompson 	}
384102ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_INTERRUPT |
384202ac6454SAndrew Thompson 	    USB_HW_POWER_ISOC)) {
384302ac6454SAndrew Thompson 		DPRINTF("Periodic is active\n");
384402ac6454SAndrew Thompson 		temp |= EHCI_CMD_PSE;
384502ac6454SAndrew Thompson 	}
384602ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, temp);
384702ac6454SAndrew Thompson 
384802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
384902ac6454SAndrew Thompson 
385002ac6454SAndrew Thompson 	return;
385102ac6454SAndrew Thompson }
385202ac6454SAndrew Thompson 
3853760bc48eSAndrew Thompson struct usb_bus_methods ehci_bus_methods =
385402ac6454SAndrew Thompson {
3855ae60fdfbSAndrew Thompson 	.endpoint_init = ehci_ep_init,
385602ac6454SAndrew Thompson 	.xfer_setup = ehci_xfer_setup,
385702ac6454SAndrew Thompson 	.xfer_unsetup = ehci_xfer_unsetup,
385802ac6454SAndrew Thompson 	.get_dma_delay = ehci_get_dma_delay,
385902ac6454SAndrew Thompson 	.device_resume = ehci_device_resume,
386002ac6454SAndrew Thompson 	.device_suspend = ehci_device_suspend,
386102ac6454SAndrew Thompson 	.set_hw_power = ehci_set_hw_power,
386239307315SAndrew Thompson 	.roothub_exec = ehci_roothub_exec,
3863dddb25f9SAlfred Perlstein 	.xfer_poll = ehci_do_poll,
386402ac6454SAndrew Thompson };
3865