xref: /freebsd/sys/dev/usb/controller/ehci.c (revision 4a35cda73150578052b41b9dfe89c08f186b274d)
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 
4902ac6454SAndrew Thompson #include <dev/usb/usb.h>
5002ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
5102ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
5202ac6454SAndrew Thompson #include <dev/usb/usb_defs.h>
5302ac6454SAndrew Thompson 
5402ac6454SAndrew Thompson #define	USB_DEBUG_VAR ehcidebug
5502ac6454SAndrew Thompson 
5602ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5802ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6002ac6454SAndrew Thompson #include <dev/usb/usb_sw_transfer.h>
6102ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
6202ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
6302ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
6402ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
6802ac6454SAndrew Thompson #include <dev/usb/controller/ehci.h>
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson #define	EHCI_BUS2SC(bus) ((ehci_softc_t *)(((uint8_t *)(bus)) - \
7102ac6454SAndrew Thompson    USB_P2U(&(((ehci_softc_t *)0)->sc_bus))))
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #if USB_DEBUG
7402ac6454SAndrew Thompson static int ehcidebug = 0;
7502ac6454SAndrew Thompson static int ehcinohighspeed = 0;
7602ac6454SAndrew Thompson 
7702ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
7802ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_ehci, OID_AUTO, debug, CTLFLAG_RW,
7902ac6454SAndrew Thompson     &ehcidebug, 0, "Debug level");
8002ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_ehci, OID_AUTO, no_hs, CTLFLAG_RW,
8102ac6454SAndrew Thompson     &ehcinohighspeed, 0, "Disable High Speed USB");
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson static void ehci_dump_regs(ehci_softc_t *sc);
8402ac6454SAndrew Thompson static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);
8502ac6454SAndrew Thompson 
8602ac6454SAndrew Thompson #endif
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson #define	EHCI_INTR_ENDPT 1
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson extern struct usb2_bus_methods ehci_bus_methods;
9102ac6454SAndrew Thompson extern struct usb2_pipe_methods ehci_device_bulk_methods;
9202ac6454SAndrew Thompson extern struct usb2_pipe_methods ehci_device_ctrl_methods;
9302ac6454SAndrew Thompson extern struct usb2_pipe_methods ehci_device_intr_methods;
9402ac6454SAndrew Thompson extern struct usb2_pipe_methods ehci_device_isoc_fs_methods;
9502ac6454SAndrew Thompson extern struct usb2_pipe_methods ehci_device_isoc_hs_methods;
9602ac6454SAndrew Thompson extern struct usb2_pipe_methods ehci_root_ctrl_methods;
9702ac6454SAndrew Thompson extern struct usb2_pipe_methods ehci_root_intr_methods;
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson static void ehci_do_poll(struct usb2_bus *bus);
10002ac6454SAndrew Thompson static void ehci_root_ctrl_poll(ehci_softc_t *sc);
10102ac6454SAndrew Thompson static void ehci_device_done(struct usb2_xfer *xfer, usb2_error_t error);
10202ac6454SAndrew Thompson static uint8_t ehci_check_transfer(struct usb2_xfer *xfer);
10302ac6454SAndrew Thompson static void ehci_timeout(void *arg);
10402ac6454SAndrew Thompson 
10502ac6454SAndrew Thompson static usb2_sw_transfer_func_t ehci_root_intr_done;
10602ac6454SAndrew Thompson static usb2_sw_transfer_func_t ehci_root_ctrl_done;
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson struct ehci_std_temp {
10902ac6454SAndrew Thompson 	ehci_softc_t *sc;
11002ac6454SAndrew Thompson 	struct usb2_page_cache *pc;
11102ac6454SAndrew Thompson 	ehci_qtd_t *td;
11202ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
11302ac6454SAndrew Thompson 	uint32_t average;
11402ac6454SAndrew Thompson 	uint32_t qtd_status;
11502ac6454SAndrew Thompson 	uint32_t len;
11602ac6454SAndrew Thompson 	uint16_t max_frame_size;
11702ac6454SAndrew Thompson 	uint8_t	shortpkt;
11802ac6454SAndrew Thompson 	uint8_t	auto_data_toggle;
11902ac6454SAndrew Thompson 	uint8_t	setup_alt_next;
12002ac6454SAndrew Thompson 	uint8_t	short_frames_ok;
12102ac6454SAndrew Thompson };
12202ac6454SAndrew Thompson 
12302ac6454SAndrew Thompson /*
12402ac6454SAndrew Thompson  * Byte-order conversion functions.
12502ac6454SAndrew Thompson  */
12602ac6454SAndrew Thompson static uint32_t
12702ac6454SAndrew Thompson htoehci32(ehci_softc_t *sc, const uint32_t v)
12802ac6454SAndrew Thompson {
12902ac6454SAndrew Thompson 	return ((sc->sc_flags & EHCI_SCFLG_BIGEDESC) ?
13002ac6454SAndrew Thompson 	    htobe32(v) : htole32(v));
13102ac6454SAndrew Thompson }
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson static uint32_t
13402ac6454SAndrew Thompson ehci32toh(ehci_softc_t *sc, const uint32_t v)
13502ac6454SAndrew Thompson {
13602ac6454SAndrew Thompson 	return ((sc->sc_flags & EHCI_SCFLG_BIGEDESC) ?
13702ac6454SAndrew Thompson 	    be32toh(v) : le32toh(v));
13802ac6454SAndrew Thompson }
13902ac6454SAndrew Thompson 
14002ac6454SAndrew Thompson void
14102ac6454SAndrew Thompson ehci_iterate_hw_softc(struct usb2_bus *bus, usb2_bus_mem_sub_cb_t *cb)
14202ac6454SAndrew Thompson {
14302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
14402ac6454SAndrew Thompson 	uint32_t i;
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
14702ac6454SAndrew Thompson 	    sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
15002ac6454SAndrew Thompson 	    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
15302ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.intr_start_pc + i,
15402ac6454SAndrew Thompson 		    sc->sc_hw.intr_start_pg + i,
15502ac6454SAndrew Thompson 		    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
15602ac6454SAndrew Thompson 	}
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
15902ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
16002ac6454SAndrew Thompson 		    sc->sc_hw.isoc_hs_start_pg + i,
16102ac6454SAndrew Thompson 		    sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
16202ac6454SAndrew Thompson 	}
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
16502ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
16602ac6454SAndrew Thompson 		    sc->sc_hw.isoc_fs_start_pg + i,
16702ac6454SAndrew Thompson 		    sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
16802ac6454SAndrew Thompson 	}
16902ac6454SAndrew Thompson }
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson static usb2_error_t
17202ac6454SAndrew Thompson ehci_hc_reset(ehci_softc_t *sc)
17302ac6454SAndrew Thompson {
17402ac6454SAndrew Thompson 	uint32_t hcr;
17502ac6454SAndrew Thompson 	uint32_t n;
17602ac6454SAndrew Thompson 
17702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
17802ac6454SAndrew Thompson 
17902ac6454SAndrew Thompson 	for (n = 0; n != 100; n++) {
18002ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
18102ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS);
18202ac6454SAndrew Thompson 		if (hcr & EHCI_STS_HCH) {
18302ac6454SAndrew Thompson 			hcr = 0;
18402ac6454SAndrew Thompson 			break;
18502ac6454SAndrew Thompson 		}
18602ac6454SAndrew Thompson 	}
18702ac6454SAndrew Thompson 
18802ac6454SAndrew Thompson 	/*
18902ac6454SAndrew Thompson 	 * Fall through and try reset anyway even though
19002ac6454SAndrew Thompson 	 * Table 2-9 in the EHCI spec says this will result
19102ac6454SAndrew Thompson 	 * in undefined behavior.
19202ac6454SAndrew Thompson 	 */
19302ac6454SAndrew Thompson 
19402ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
19502ac6454SAndrew Thompson 	for (n = 0; n != 100; n++) {
19602ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
19702ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBCMD);
19802ac6454SAndrew Thompson 		if (!(hcr & EHCI_CMD_HCRESET)) {
19902ac6454SAndrew Thompson 			if (sc->sc_flags & EHCI_SCFLG_SETMODE)
20002ac6454SAndrew Thompson 				EOWRITE4(sc, 0x68, 0x3);
20102ac6454SAndrew Thompson 			hcr = 0;
20202ac6454SAndrew Thompson 			break;
20302ac6454SAndrew Thompson 		}
20402ac6454SAndrew Thompson 	}
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson 	if (hcr) {
20702ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
20802ac6454SAndrew Thompson 	}
20902ac6454SAndrew Thompson 	return (0);
21002ac6454SAndrew Thompson }
21102ac6454SAndrew Thompson 
21202ac6454SAndrew Thompson usb2_error_t
21302ac6454SAndrew Thompson ehci_init(ehci_softc_t *sc)
21402ac6454SAndrew Thompson {
21502ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
21602ac6454SAndrew Thompson 	uint32_t version;
21702ac6454SAndrew Thompson 	uint32_t sparams;
21802ac6454SAndrew Thompson 	uint32_t cparams;
21902ac6454SAndrew Thompson 	uint32_t hcr;
22002ac6454SAndrew Thompson 	uint16_t i;
22102ac6454SAndrew Thompson 	uint16_t x;
22202ac6454SAndrew Thompson 	uint16_t y;
22302ac6454SAndrew Thompson 	uint16_t bit;
22402ac6454SAndrew Thompson 	usb2_error_t err = 0;
22502ac6454SAndrew Thompson 
22602ac6454SAndrew Thompson 	DPRINTF("start\n");
22702ac6454SAndrew Thompson 
22802ac6454SAndrew Thompson 	usb2_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
22902ac6454SAndrew Thompson 
23002ac6454SAndrew Thompson #if USB_DEBUG
23102ac6454SAndrew Thompson 	if (ehcidebug > 2) {
23202ac6454SAndrew Thompson 		ehci_dump_regs(sc);
23302ac6454SAndrew Thompson 	}
23402ac6454SAndrew Thompson #endif
23502ac6454SAndrew Thompson 
23602ac6454SAndrew Thompson 	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
23702ac6454SAndrew Thompson 
23802ac6454SAndrew Thompson 	version = EREAD2(sc, EHCI_HCIVERSION);
23902ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
24002ac6454SAndrew Thompson 	    version >> 8, version & 0xff);
24102ac6454SAndrew Thompson 
24202ac6454SAndrew Thompson 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
24302ac6454SAndrew Thompson 	DPRINTF("sparams=0x%x\n", sparams);
24402ac6454SAndrew Thompson 
24502ac6454SAndrew Thompson 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
24602ac6454SAndrew Thompson 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
24702ac6454SAndrew Thompson 	DPRINTF("cparams=0x%x\n", cparams);
24802ac6454SAndrew Thompson 
24902ac6454SAndrew Thompson 	if (EHCI_HCC_64BIT(cparams)) {
25002ac6454SAndrew Thompson 		DPRINTF("HCC uses 64-bit structures\n");
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson 		/* MUST clear segment register if 64 bit capable */
25302ac6454SAndrew Thompson 		EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
25402ac6454SAndrew Thompson 	}
25502ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_2_0;
25602ac6454SAndrew Thompson 
25702ac6454SAndrew Thompson 	/* Reset the controller */
25802ac6454SAndrew Thompson 	DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev));
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
26102ac6454SAndrew Thompson 	err = ehci_hc_reset(sc);
26202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
26302ac6454SAndrew Thompson 	if (err) {
26402ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "reset timeout\n");
26502ac6454SAndrew Thompson 		return (err);
26602ac6454SAndrew Thompson 	}
26702ac6454SAndrew Thompson 	/*
26802ac6454SAndrew Thompson 	 * use current frame-list-size selection 0: 1024*4 bytes 1:  512*4
26902ac6454SAndrew Thompson 	 * bytes 2:  256*4 bytes 3:      unknown
27002ac6454SAndrew Thompson 	 */
27102ac6454SAndrew Thompson 	if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
27202ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
27302ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
27402ac6454SAndrew Thompson 	}
27502ac6454SAndrew Thompson 	/* set up the bus struct */
27602ac6454SAndrew Thompson 	sc->sc_bus.methods = &ehci_bus_methods;
27702ac6454SAndrew Thompson 
27802ac6454SAndrew Thompson 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
27902ac6454SAndrew Thompson 
28002ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
28102ac6454SAndrew Thompson 		ehci_qh_t *qh;
28202ac6454SAndrew Thompson 
28302ac6454SAndrew Thompson 		usb2_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);
28402ac6454SAndrew Thompson 
28502ac6454SAndrew Thompson 		qh = buf_res.buffer;
28602ac6454SAndrew Thompson 
28702ac6454SAndrew Thompson 		/* initialize page cache pointer */
28802ac6454SAndrew Thompson 
28902ac6454SAndrew Thompson 		qh->page_cache = sc->sc_hw.intr_start_pc + i;
29002ac6454SAndrew Thompson 
29102ac6454SAndrew Thompson 		/* store a pointer to queue head */
29202ac6454SAndrew Thompson 
29302ac6454SAndrew Thompson 		sc->sc_intr_p_last[i] = qh;
29402ac6454SAndrew Thompson 
29502ac6454SAndrew Thompson 		qh->qh_self =
29602ac6454SAndrew Thompson 		    htoehci32(sc, buf_res.physaddr) |
29702ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_LINK_QH);
29802ac6454SAndrew Thompson 
29902ac6454SAndrew Thompson 		qh->qh_endp =
30002ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
30102ac6454SAndrew Thompson 		qh->qh_endphub =
30202ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_QH_SET_MULT(1));
30302ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
30402ac6454SAndrew Thompson 
30502ac6454SAndrew Thompson 		qh->qh_qtd.qtd_next =
30602ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_LINK_TERMINATE);
30702ac6454SAndrew Thompson 		qh->qh_qtd.qtd_altnext =
30802ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_LINK_TERMINATE);
30902ac6454SAndrew Thompson 		qh->qh_qtd.qtd_status =
31002ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_QTD_HALTED);
31102ac6454SAndrew Thompson 	}
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson 	/*
31402ac6454SAndrew Thompson 	 * the QHs are arranged to give poll intervals that are
31502ac6454SAndrew Thompson 	 * powers of 2 times 1ms
31602ac6454SAndrew Thompson 	 */
31702ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
31802ac6454SAndrew Thompson 	while (bit) {
31902ac6454SAndrew Thompson 		x = bit;
32002ac6454SAndrew Thompson 		while (x & bit) {
32102ac6454SAndrew Thompson 			ehci_qh_t *qh_x;
32202ac6454SAndrew Thompson 			ehci_qh_t *qh_y;
32302ac6454SAndrew Thompson 
32402ac6454SAndrew Thompson 			y = (x ^ bit) | (bit / 2);
32502ac6454SAndrew Thompson 
32602ac6454SAndrew Thompson 			qh_x = sc->sc_intr_p_last[x];
32702ac6454SAndrew Thompson 			qh_y = sc->sc_intr_p_last[y];
32802ac6454SAndrew Thompson 
32902ac6454SAndrew Thompson 			/*
33002ac6454SAndrew Thompson 			 * the next QH has half the poll interval
33102ac6454SAndrew Thompson 			 */
33202ac6454SAndrew Thompson 			qh_x->qh_link = qh_y->qh_self;
33302ac6454SAndrew Thompson 
33402ac6454SAndrew Thompson 			x++;
33502ac6454SAndrew Thompson 		}
33602ac6454SAndrew Thompson 		bit >>= 1;
33702ac6454SAndrew Thompson 	}
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson 	if (1) {
34002ac6454SAndrew Thompson 		ehci_qh_t *qh;
34102ac6454SAndrew Thompson 
34202ac6454SAndrew Thompson 		qh = sc->sc_intr_p_last[0];
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 		/* the last (1ms) QH terminates */
34502ac6454SAndrew Thompson 		qh->qh_link = htoehci32(sc, EHCI_LINK_TERMINATE);
34602ac6454SAndrew Thompson 	}
34702ac6454SAndrew Thompson 	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
34802ac6454SAndrew Thompson 		ehci_sitd_t *sitd;
34902ac6454SAndrew Thompson 		ehci_itd_t *itd;
35002ac6454SAndrew Thompson 
35102ac6454SAndrew Thompson 		usb2_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
35202ac6454SAndrew Thompson 
35302ac6454SAndrew Thompson 		sitd = buf_res.buffer;
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson 		/* initialize page cache pointer */
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 		sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
36002ac6454SAndrew Thompson 
36102ac6454SAndrew Thompson 		sc->sc_isoc_fs_p_last[i] = sitd;
36202ac6454SAndrew Thompson 
36302ac6454SAndrew Thompson 		/* initialize full speed isochronous */
36402ac6454SAndrew Thompson 
36502ac6454SAndrew Thompson 		sitd->sitd_self =
36602ac6454SAndrew Thompson 		    htoehci32(sc, buf_res.physaddr) |
36702ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_LINK_SITD);
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson 		sitd->sitd_back =
37002ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_LINK_TERMINATE);
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson 		sitd->sitd_next =
37302ac6454SAndrew Thompson 		    sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 		usb2_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson 		itd = buf_res.buffer;
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson 		/* initialize page cache pointer */
38102ac6454SAndrew Thompson 
38202ac6454SAndrew Thompson 		itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;
38302ac6454SAndrew Thompson 
38402ac6454SAndrew Thompson 		/* store a pointer to the transfer descriptor */
38502ac6454SAndrew Thompson 
38602ac6454SAndrew Thompson 		sc->sc_isoc_hs_p_last[i] = itd;
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson 		/* initialize high speed isochronous */
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson 		itd->itd_self =
39102ac6454SAndrew Thompson 		    htoehci32(sc, buf_res.physaddr) |
39202ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_LINK_ITD);
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 		itd->itd_next =
39502ac6454SAndrew Thompson 		    sitd->sitd_self;
39602ac6454SAndrew Thompson 	}
39702ac6454SAndrew Thompson 
39802ac6454SAndrew Thompson 	usb2_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
39902ac6454SAndrew Thompson 
40002ac6454SAndrew Thompson 	if (1) {
40102ac6454SAndrew Thompson 		uint32_t *pframes;
40202ac6454SAndrew Thompson 
40302ac6454SAndrew Thompson 		pframes = buf_res.buffer;
40402ac6454SAndrew Thompson 
40502ac6454SAndrew Thompson 		/*
40602ac6454SAndrew Thompson 		 * execution order:
40702ac6454SAndrew Thompson 		 * pframes -> high speed isochronous ->
40802ac6454SAndrew Thompson 		 *    full speed isochronous -> interrupt QH's
40902ac6454SAndrew Thompson 		 */
41002ac6454SAndrew Thompson 		for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
41102ac6454SAndrew Thompson 			pframes[i] = sc->sc_isoc_hs_p_last
41202ac6454SAndrew Thompson 			    [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
41302ac6454SAndrew Thompson 		}
41402ac6454SAndrew Thompson 	}
41502ac6454SAndrew Thompson 	/* setup sync list pointer */
41602ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
41702ac6454SAndrew Thompson 
41802ac6454SAndrew Thompson 	usb2_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson 	if (1) {
42102ac6454SAndrew Thompson 
42202ac6454SAndrew Thompson 		ehci_qh_t *qh;
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson 		qh = buf_res.buffer;
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson 		/* initialize page cache pointer */
42702ac6454SAndrew Thompson 
42802ac6454SAndrew Thompson 		qh->page_cache = &sc->sc_hw.async_start_pc;
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson 		/* store a pointer to the queue head */
43102ac6454SAndrew Thompson 
43202ac6454SAndrew Thompson 		sc->sc_async_p_last = qh;
43302ac6454SAndrew Thompson 
43402ac6454SAndrew Thompson 		/* init dummy QH that starts the async list */
43502ac6454SAndrew Thompson 
43602ac6454SAndrew Thompson 		qh->qh_self =
43702ac6454SAndrew Thompson 		    htoehci32(sc, buf_res.physaddr) |
43802ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_LINK_QH);
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 		/* fill the QH */
44102ac6454SAndrew Thompson 		qh->qh_endp =
44202ac6454SAndrew Thompson 		    htoehci32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
44302ac6454SAndrew Thompson 		qh->qh_endphub = htoehci32(sc, EHCI_QH_SET_MULT(1));
44402ac6454SAndrew Thompson 		qh->qh_link = qh->qh_self;
44502ac6454SAndrew Thompson 		qh->qh_curqtd = 0;
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson 		/* fill the overlay qTD */
44802ac6454SAndrew Thompson 		qh->qh_qtd.qtd_next = htoehci32(sc, EHCI_LINK_TERMINATE);
44902ac6454SAndrew Thompson 		qh->qh_qtd.qtd_altnext = htoehci32(sc, EHCI_LINK_TERMINATE);
45002ac6454SAndrew Thompson 		qh->qh_qtd.qtd_status = htoehci32(sc, EHCI_QTD_HALTED);
45102ac6454SAndrew Thompson 	}
45202ac6454SAndrew Thompson 	/* flush all cache into memory */
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 	usb2_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson #if USB_DEBUG
45702ac6454SAndrew Thompson 	if (ehcidebug) {
45802ac6454SAndrew Thompson 		ehci_dump_sqh(sc, sc->sc_async_p_last);
45902ac6454SAndrew Thompson 	}
46002ac6454SAndrew Thompson #endif
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson 	/* setup async list pointer */
46302ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
46402ac6454SAndrew Thompson 
46502ac6454SAndrew Thompson 
46602ac6454SAndrew Thompson 	/* enable interrupts */
46702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
46802ac6454SAndrew Thompson 
46902ac6454SAndrew Thompson 	/* turn on controller */
47002ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD,
47102ac6454SAndrew Thompson 	    EHCI_CMD_ITC_1 |		/* 1 microframes interrupt delay */
47202ac6454SAndrew Thompson 	    (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
47302ac6454SAndrew Thompson 	    EHCI_CMD_ASE |
47402ac6454SAndrew Thompson 	    EHCI_CMD_PSE |
47502ac6454SAndrew Thompson 	    EHCI_CMD_RS);
47602ac6454SAndrew Thompson 
47702ac6454SAndrew Thompson 	/* Take over port ownership */
47802ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
48102ac6454SAndrew Thompson 		usb2_pause_mtx(NULL, hz / 1000);
48202ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
48302ac6454SAndrew Thompson 		if (!hcr) {
48402ac6454SAndrew Thompson 			break;
48502ac6454SAndrew Thompson 		}
48602ac6454SAndrew Thompson 	}
48702ac6454SAndrew Thompson 	if (hcr) {
48802ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "run timeout\n");
48902ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
49002ac6454SAndrew Thompson 	}
49102ac6454SAndrew Thompson 
49202ac6454SAndrew Thompson 	if (!err) {
49302ac6454SAndrew Thompson 		/* catch any lost interrupts */
49402ac6454SAndrew Thompson 		ehci_do_poll(&sc->sc_bus);
49502ac6454SAndrew Thompson 	}
49602ac6454SAndrew Thompson 	return (err);
49702ac6454SAndrew Thompson }
49802ac6454SAndrew Thompson 
49902ac6454SAndrew Thompson /*
50002ac6454SAndrew Thompson  * shut down the controller when the system is going down
50102ac6454SAndrew Thompson  */
50202ac6454SAndrew Thompson void
50302ac6454SAndrew Thompson ehci_detach(ehci_softc_t *sc)
50402ac6454SAndrew Thompson {
50502ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
50602ac6454SAndrew Thompson 
50702ac6454SAndrew Thompson 	usb2_callout_stop(&sc->sc_tmo_pcd);
50802ac6454SAndrew Thompson 
50902ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson 	if (ehci_hc_reset(sc)) {
51202ac6454SAndrew Thompson 		DPRINTF("reset failed!\n");
51302ac6454SAndrew Thompson 	}
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 	/* XXX let stray task complete */
51802ac6454SAndrew Thompson 	usb2_pause_mtx(NULL, hz / 20);
51902ac6454SAndrew Thompson 
52002ac6454SAndrew Thompson 	usb2_callout_drain(&sc->sc_tmo_pcd);
52102ac6454SAndrew Thompson }
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson void
52402ac6454SAndrew Thompson ehci_suspend(ehci_softc_t *sc)
52502ac6454SAndrew Thompson {
52602ac6454SAndrew Thompson 	uint32_t cmd;
52702ac6454SAndrew Thompson 	uint32_t hcr;
52802ac6454SAndrew Thompson 	uint8_t i;
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
53302ac6454SAndrew Thompson 		cmd = EOREAD4(sc, EHCI_PORTSC(i));
53402ac6454SAndrew Thompson 		if (((cmd & EHCI_PS_PO) == 0) &&
53502ac6454SAndrew Thompson 		    ((cmd & EHCI_PS_PE) == EHCI_PS_PE)) {
53602ac6454SAndrew Thompson 			EOWRITE4(sc, EHCI_PORTSC(i),
53702ac6454SAndrew Thompson 			    cmd | EHCI_PS_SUSP);
53802ac6454SAndrew Thompson 		}
53902ac6454SAndrew Thompson 	}
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 	sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson 	cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
54402ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, cmd);
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
54702ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) &
54802ac6454SAndrew Thompson 		    (EHCI_STS_ASS | EHCI_STS_PSS);
54902ac6454SAndrew Thompson 
55002ac6454SAndrew Thompson 		if (hcr == 0) {
55102ac6454SAndrew Thompson 			break;
55202ac6454SAndrew Thompson 		}
55302ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
55402ac6454SAndrew Thompson 	}
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson 	if (hcr != 0) {
55702ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "reset timeout\n");
55802ac6454SAndrew Thompson 	}
55902ac6454SAndrew Thompson 	cmd &= ~EHCI_CMD_RS;
56002ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, cmd);
56102ac6454SAndrew Thompson 
56202ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
56302ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
56402ac6454SAndrew Thompson 		if (hcr == EHCI_STS_HCH) {
56502ac6454SAndrew Thompson 			break;
56602ac6454SAndrew Thompson 		}
56702ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
56802ac6454SAndrew Thompson 	}
56902ac6454SAndrew Thompson 
57002ac6454SAndrew Thompson 	if (hcr != EHCI_STS_HCH) {
57102ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev,
57202ac6454SAndrew Thompson 		    "config timeout\n");
57302ac6454SAndrew Thompson 	}
57402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
57502ac6454SAndrew Thompson }
57602ac6454SAndrew Thompson 
57702ac6454SAndrew Thompson void
57802ac6454SAndrew Thompson ehci_resume(ehci_softc_t *sc)
57902ac6454SAndrew Thompson {
58002ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
58102ac6454SAndrew Thompson 	uint32_t cmd;
58202ac6454SAndrew Thompson 	uint32_t hcr;
58302ac6454SAndrew Thompson 	uint8_t i;
58402ac6454SAndrew Thompson 
58502ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
58602ac6454SAndrew Thompson 
58702ac6454SAndrew Thompson 	/* restore things in case the bios doesn't */
58802ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson 	usb2_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
59102ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);
59202ac6454SAndrew Thompson 
59302ac6454SAndrew Thompson 	usb2_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
59402ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);
59502ac6454SAndrew Thompson 
59602ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson 	hcr = 0;
59902ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
60002ac6454SAndrew Thompson 		cmd = EOREAD4(sc, EHCI_PORTSC(i));
60102ac6454SAndrew Thompson 		if (((cmd & EHCI_PS_PO) == 0) &&
60202ac6454SAndrew Thompson 		    ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
60302ac6454SAndrew Thompson 			EOWRITE4(sc, EHCI_PORTSC(i),
60402ac6454SAndrew Thompson 			    cmd | EHCI_PS_FPR);
60502ac6454SAndrew Thompson 			hcr = 1;
60602ac6454SAndrew Thompson 		}
60702ac6454SAndrew Thompson 	}
60802ac6454SAndrew Thompson 
60902ac6454SAndrew Thompson 	if (hcr) {
61002ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx,
61102ac6454SAndrew Thompson 		    USB_MS_TO_TICKS(USB_RESUME_WAIT));
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson 		for (i = 1; i <= sc->sc_noport; i++) {
61402ac6454SAndrew Thompson 			cmd = EOREAD4(sc, EHCI_PORTSC(i));
61502ac6454SAndrew Thompson 			if (((cmd & EHCI_PS_PO) == 0) &&
61602ac6454SAndrew Thompson 			    ((cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)) {
61702ac6454SAndrew Thompson 				EOWRITE4(sc, EHCI_PORTSC(i),
61802ac6454SAndrew Thompson 				    cmd & ~EHCI_PS_FPR);
61902ac6454SAndrew Thompson 			}
62002ac6454SAndrew Thompson 		}
62102ac6454SAndrew Thompson 	}
62202ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
62302ac6454SAndrew Thompson 
62402ac6454SAndrew Thompson 	for (i = 0; i < 100; i++) {
62502ac6454SAndrew Thompson 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
62602ac6454SAndrew Thompson 		if (hcr != EHCI_STS_HCH) {
62702ac6454SAndrew Thompson 			break;
62802ac6454SAndrew Thompson 		}
62902ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
63002ac6454SAndrew Thompson 	}
63102ac6454SAndrew Thompson 	if (hcr == EHCI_STS_HCH) {
63202ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "config timeout\n");
63302ac6454SAndrew Thompson 	}
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	usb2_pause_mtx(NULL,
63802ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(USB_RESUME_WAIT));
63902ac6454SAndrew Thompson 
64002ac6454SAndrew Thompson 	/* catch any lost interrupts */
64102ac6454SAndrew Thompson 	ehci_do_poll(&sc->sc_bus);
64202ac6454SAndrew Thompson }
64302ac6454SAndrew Thompson 
64402ac6454SAndrew Thompson void
64502ac6454SAndrew Thompson ehci_shutdown(ehci_softc_t *sc)
64602ac6454SAndrew Thompson {
64702ac6454SAndrew Thompson 	DPRINTF("stopping the HC\n");
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
65002ac6454SAndrew Thompson 
65102ac6454SAndrew Thompson 	if (ehci_hc_reset(sc)) {
65202ac6454SAndrew Thompson 		DPRINTF("reset failed!\n");
65302ac6454SAndrew Thompson 	}
65402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
65502ac6454SAndrew Thompson }
65602ac6454SAndrew Thompson 
65702ac6454SAndrew Thompson #if USB_DEBUG
65802ac6454SAndrew Thompson static void
65902ac6454SAndrew Thompson ehci_dump_regs(ehci_softc_t *sc)
66002ac6454SAndrew Thompson {
66102ac6454SAndrew Thompson 	uint32_t i;
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBCMD);
66402ac6454SAndrew Thompson 	printf("cmd=0x%08x\n", i);
66502ac6454SAndrew Thompson 
66602ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_1)
66702ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_1\n");
66802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_2)
66902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_2\n");
67002ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_4)
67102ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_4\n");
67202ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_8)
67302ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_8\n");
67402ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_16)
67502ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_16\n");
67602ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_32)
67702ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_32\n");
67802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ITC_64)
67902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ITC_64\n");
68002ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPME)
68102ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPME\n");
68202ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASPMC)
68302ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASPMC\n");
68402ac6454SAndrew Thompson 	if (i & EHCI_CMD_LHCR)
68502ac6454SAndrew Thompson 		printf(" EHCI_CMD_LHCR\n");
68602ac6454SAndrew Thompson 	if (i & EHCI_CMD_IAAD)
68702ac6454SAndrew Thompson 		printf(" EHCI_CMD_IAAD\n");
68802ac6454SAndrew Thompson 	if (i & EHCI_CMD_ASE)
68902ac6454SAndrew Thompson 		printf(" EHCI_CMD_ASE\n");
69002ac6454SAndrew Thompson 	if (i & EHCI_CMD_PSE)
69102ac6454SAndrew Thompson 		printf(" EHCI_CMD_PSE\n");
69202ac6454SAndrew Thompson 	if (i & EHCI_CMD_FLS_M)
69302ac6454SAndrew Thompson 		printf(" EHCI_CMD_FLS_M\n");
69402ac6454SAndrew Thompson 	if (i & EHCI_CMD_HCRESET)
69502ac6454SAndrew Thompson 		printf(" EHCI_CMD_HCRESET\n");
69602ac6454SAndrew Thompson 	if (i & EHCI_CMD_RS)
69702ac6454SAndrew Thompson 		printf(" EHCI_CMD_RS\n");
69802ac6454SAndrew Thompson 
69902ac6454SAndrew Thompson 	i = EOREAD4(sc, EHCI_USBSTS);
70002ac6454SAndrew Thompson 
70102ac6454SAndrew Thompson 	printf("sts=0x%08x\n", i);
70202ac6454SAndrew Thompson 
70302ac6454SAndrew Thompson 	if (i & EHCI_STS_ASS)
70402ac6454SAndrew Thompson 		printf(" EHCI_STS_ASS\n");
70502ac6454SAndrew Thompson 	if (i & EHCI_STS_PSS)
70602ac6454SAndrew Thompson 		printf(" EHCI_STS_PSS\n");
70702ac6454SAndrew Thompson 	if (i & EHCI_STS_REC)
70802ac6454SAndrew Thompson 		printf(" EHCI_STS_REC\n");
70902ac6454SAndrew Thompson 	if (i & EHCI_STS_HCH)
71002ac6454SAndrew Thompson 		printf(" EHCI_STS_HCH\n");
71102ac6454SAndrew Thompson 	if (i & EHCI_STS_IAA)
71202ac6454SAndrew Thompson 		printf(" EHCI_STS_IAA\n");
71302ac6454SAndrew Thompson 	if (i & EHCI_STS_HSE)
71402ac6454SAndrew Thompson 		printf(" EHCI_STS_HSE\n");
71502ac6454SAndrew Thompson 	if (i & EHCI_STS_FLR)
71602ac6454SAndrew Thompson 		printf(" EHCI_STS_FLR\n");
71702ac6454SAndrew Thompson 	if (i & EHCI_STS_PCD)
71802ac6454SAndrew Thompson 		printf(" EHCI_STS_PCD\n");
71902ac6454SAndrew Thompson 	if (i & EHCI_STS_ERRINT)
72002ac6454SAndrew Thompson 		printf(" EHCI_STS_ERRINT\n");
72102ac6454SAndrew Thompson 	if (i & EHCI_STS_INT)
72202ac6454SAndrew Thompson 		printf(" EHCI_STS_INT\n");
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson 	printf("ien=0x%08x\n",
72502ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_USBINTR));
72602ac6454SAndrew Thompson 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
72702ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_FRINDEX),
72802ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
72902ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
73002ac6454SAndrew Thompson 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
73102ac6454SAndrew Thompson 	for (i = 1; i <= sc->sc_noport; i++) {
73202ac6454SAndrew Thompson 		printf("port %d status=0x%08x\n", i,
73302ac6454SAndrew Thompson 		    EOREAD4(sc, EHCI_PORTSC(i)));
73402ac6454SAndrew Thompson 	}
73502ac6454SAndrew Thompson }
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson static void
73802ac6454SAndrew Thompson ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
73902ac6454SAndrew Thompson {
74002ac6454SAndrew Thompson 	link = ehci32toh(sc, link);
74102ac6454SAndrew Thompson 	printf("0x%08x", link);
74202ac6454SAndrew Thompson 	if (link & EHCI_LINK_TERMINATE)
74302ac6454SAndrew Thompson 		printf("<T>");
74402ac6454SAndrew Thompson 	else {
74502ac6454SAndrew Thompson 		printf("<");
74602ac6454SAndrew Thompson 		if (type) {
74702ac6454SAndrew Thompson 			switch (EHCI_LINK_TYPE(link)) {
74802ac6454SAndrew Thompson 			case EHCI_LINK_ITD:
74902ac6454SAndrew Thompson 				printf("ITD");
75002ac6454SAndrew Thompson 				break;
75102ac6454SAndrew Thompson 			case EHCI_LINK_QH:
75202ac6454SAndrew Thompson 				printf("QH");
75302ac6454SAndrew Thompson 				break;
75402ac6454SAndrew Thompson 			case EHCI_LINK_SITD:
75502ac6454SAndrew Thompson 				printf("SITD");
75602ac6454SAndrew Thompson 				break;
75702ac6454SAndrew Thompson 			case EHCI_LINK_FSTN:
75802ac6454SAndrew Thompson 				printf("FSTN");
75902ac6454SAndrew Thompson 				break;
76002ac6454SAndrew Thompson 			}
76102ac6454SAndrew Thompson 		}
76202ac6454SAndrew Thompson 		printf(">");
76302ac6454SAndrew Thompson 	}
76402ac6454SAndrew Thompson }
76502ac6454SAndrew Thompson 
76602ac6454SAndrew Thompson static void
76702ac6454SAndrew Thompson ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
76802ac6454SAndrew Thompson {
76902ac6454SAndrew Thompson 	uint32_t s;
77002ac6454SAndrew Thompson 
77102ac6454SAndrew Thompson 	printf("  next=");
77202ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_next, 0);
77302ac6454SAndrew Thompson 	printf(" altnext=");
77402ac6454SAndrew Thompson 	ehci_dump_link(sc, qtd->qtd_altnext, 0);
77502ac6454SAndrew Thompson 	printf("\n");
77602ac6454SAndrew Thompson 	s = ehci32toh(sc, qtd->qtd_status);
77702ac6454SAndrew Thompson 	printf("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
77802ac6454SAndrew Thompson 	    s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
77902ac6454SAndrew Thompson 	    EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
78002ac6454SAndrew Thompson 	printf("    cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
78102ac6454SAndrew Thompson 	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
78202ac6454SAndrew Thompson 	    (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
78302ac6454SAndrew Thompson 	    (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
78402ac6454SAndrew Thompson 	    (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
78502ac6454SAndrew Thompson 	    (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
78602ac6454SAndrew Thompson 	    (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
78702ac6454SAndrew Thompson 	    (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
78802ac6454SAndrew Thompson 	    (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
78902ac6454SAndrew Thompson 	    (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
79202ac6454SAndrew Thompson 		printf("  buffer[%d]=0x%08x\n", s,
79302ac6454SAndrew Thompson 		    ehci32toh(sc, qtd->qtd_buffer[s]));
79402ac6454SAndrew Thompson 	}
79502ac6454SAndrew Thompson 	for (s = 0; s < 5; s++) {
79602ac6454SAndrew Thompson 		printf("  buffer_hi[%d]=0x%08x\n", s,
79702ac6454SAndrew Thompson 		    ehci32toh(sc, qtd->qtd_buffer_hi[s]));
79802ac6454SAndrew Thompson 	}
79902ac6454SAndrew Thompson }
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson static uint8_t
80202ac6454SAndrew Thompson ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
80302ac6454SAndrew Thompson {
80402ac6454SAndrew Thompson 	uint8_t temp;
80502ac6454SAndrew Thompson 
80602ac6454SAndrew Thompson 	usb2_pc_cpu_invalidate(sqtd->page_cache);
80702ac6454SAndrew Thompson 	printf("QTD(%p) at 0x%08x:\n", sqtd, ehci32toh(sc, sqtd->qtd_self));
80802ac6454SAndrew Thompson 	ehci_dump_qtd(sc, sqtd);
80902ac6454SAndrew Thompson 	temp = (sqtd->qtd_next & htoehci32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
81002ac6454SAndrew Thompson 	return (temp);
81102ac6454SAndrew Thompson }
81202ac6454SAndrew Thompson 
81302ac6454SAndrew Thompson static void
81402ac6454SAndrew Thompson ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
81502ac6454SAndrew Thompson {
81602ac6454SAndrew Thompson 	uint16_t i;
81702ac6454SAndrew Thompson 	uint8_t stop;
81802ac6454SAndrew Thompson 
81902ac6454SAndrew Thompson 	stop = 0;
82002ac6454SAndrew Thompson 	for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
82102ac6454SAndrew Thompson 		stop = ehci_dump_sqtd(sc, sqtd);
82202ac6454SAndrew Thompson 	}
82302ac6454SAndrew Thompson 	if (sqtd) {
82402ac6454SAndrew Thompson 		printf("dump aborted, too many TDs\n");
82502ac6454SAndrew Thompson 	}
82602ac6454SAndrew Thompson }
82702ac6454SAndrew Thompson 
82802ac6454SAndrew Thompson static void
82902ac6454SAndrew Thompson ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
83002ac6454SAndrew Thompson {
83102ac6454SAndrew Thompson 	uint32_t endp;
83202ac6454SAndrew Thompson 	uint32_t endphub;
83302ac6454SAndrew Thompson 
83402ac6454SAndrew Thompson 	usb2_pc_cpu_invalidate(qh->page_cache);
83502ac6454SAndrew Thompson 	printf("QH(%p) at 0x%08x:\n", qh, ehci32toh(sc, qh->qh_self) & ~0x1F);
83602ac6454SAndrew Thompson 	printf("  link=");
83702ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_link, 1);
83802ac6454SAndrew Thompson 	printf("\n");
83902ac6454SAndrew Thompson 	endp = ehci32toh(sc, qh->qh_endp);
84002ac6454SAndrew Thompson 	printf("  endp=0x%08x\n", endp);
84102ac6454SAndrew Thompson 	printf("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
84202ac6454SAndrew Thompson 	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
84302ac6454SAndrew Thompson 	    EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
84402ac6454SAndrew Thompson 	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
84502ac6454SAndrew Thompson 	printf("    mpl=0x%x ctl=%d nrl=%d\n",
84602ac6454SAndrew Thompson 	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
84702ac6454SAndrew Thompson 	    EHCI_QH_GET_NRL(endp));
84802ac6454SAndrew Thompson 	endphub = ehci32toh(sc, qh->qh_endphub);
84902ac6454SAndrew Thompson 	printf("  endphub=0x%08x\n", endphub);
85002ac6454SAndrew Thompson 	printf("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
85102ac6454SAndrew Thompson 	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
85202ac6454SAndrew Thompson 	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
85302ac6454SAndrew Thompson 	    EHCI_QH_GET_MULT(endphub));
85402ac6454SAndrew Thompson 	printf("  curqtd=");
85502ac6454SAndrew Thompson 	ehci_dump_link(sc, qh->qh_curqtd, 0);
85602ac6454SAndrew Thompson 	printf("\n");
85702ac6454SAndrew Thompson 	printf("Overlay qTD:\n");
85802ac6454SAndrew Thompson 	ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
85902ac6454SAndrew Thompson }
86002ac6454SAndrew Thompson 
86102ac6454SAndrew Thompson static void
86202ac6454SAndrew Thompson ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
86302ac6454SAndrew Thompson {
86402ac6454SAndrew Thompson 	usb2_pc_cpu_invalidate(sitd->page_cache);
86502ac6454SAndrew Thompson 	printf("SITD(%p) at 0x%08x\n", sitd, ehci32toh(sc, sitd->sitd_self) & ~0x1F);
86602ac6454SAndrew Thompson 	printf(" next=0x%08x\n", ehci32toh(sc, sitd->sitd_next));
86702ac6454SAndrew Thompson 	printf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
86802ac6454SAndrew Thompson 	    ehci32toh(sc, sitd->sitd_portaddr),
86902ac6454SAndrew Thompson 	    (sitd->sitd_portaddr & htoehci32(sc, EHCI_SITD_SET_DIR_IN))
87002ac6454SAndrew Thompson 	    ? "in" : "out",
87102ac6454SAndrew Thompson 	    EHCI_SITD_GET_ADDR(ehci32toh(sc, sitd->sitd_portaddr)),
87202ac6454SAndrew Thompson 	    EHCI_SITD_GET_ENDPT(ehci32toh(sc, sitd->sitd_portaddr)),
87302ac6454SAndrew Thompson 	    EHCI_SITD_GET_PORT(ehci32toh(sc, sitd->sitd_portaddr)),
87402ac6454SAndrew Thompson 	    EHCI_SITD_GET_HUBA(ehci32toh(sc, sitd->sitd_portaddr)));
87502ac6454SAndrew Thompson 	printf(" mask=0x%08x\n", ehci32toh(sc, sitd->sitd_mask));
87602ac6454SAndrew Thompson 	printf(" status=0x%08x <%s> len=0x%x\n", ehci32toh(sc, sitd->sitd_status),
87702ac6454SAndrew Thompson 	    (sitd->sitd_status & htoehci32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
87802ac6454SAndrew Thompson 	    EHCI_SITD_GET_LEN(ehci32toh(sc, sitd->sitd_status)));
87902ac6454SAndrew Thompson 	printf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
88002ac6454SAndrew Thompson 	    ehci32toh(sc, sitd->sitd_back),
88102ac6454SAndrew Thompson 	    ehci32toh(sc, sitd->sitd_bp[0]),
88202ac6454SAndrew Thompson 	    ehci32toh(sc, sitd->sitd_bp[1]),
88302ac6454SAndrew Thompson 	    ehci32toh(sc, sitd->sitd_bp_hi[0]),
88402ac6454SAndrew Thompson 	    ehci32toh(sc, sitd->sitd_bp_hi[1]));
88502ac6454SAndrew Thompson }
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson static void
88802ac6454SAndrew Thompson ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
88902ac6454SAndrew Thompson {
89002ac6454SAndrew Thompson 	usb2_pc_cpu_invalidate(itd->page_cache);
89102ac6454SAndrew Thompson 	printf("ITD(%p) at 0x%08x\n", itd, ehci32toh(sc, itd->itd_self) & ~0x1F);
89202ac6454SAndrew Thompson 	printf(" next=0x%08x\n", ehci32toh(sc, itd->itd_next));
89302ac6454SAndrew Thompson 	printf(" status[0]=0x%08x; <%s>\n", ehci32toh(sc, itd->itd_status[0]),
89402ac6454SAndrew Thompson 	    (itd->itd_status[0] & htoehci32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
89502ac6454SAndrew Thompson 	printf(" status[1]=0x%08x; <%s>\n", ehci32toh(sc, itd->itd_status[1]),
89602ac6454SAndrew Thompson 	    (itd->itd_status[1] & htoehci32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
89702ac6454SAndrew Thompson 	printf(" status[2]=0x%08x; <%s>\n", ehci32toh(sc, itd->itd_status[2]),
89802ac6454SAndrew Thompson 	    (itd->itd_status[2] & htoehci32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
89902ac6454SAndrew Thompson 	printf(" status[3]=0x%08x; <%s>\n", ehci32toh(sc, itd->itd_status[3]),
90002ac6454SAndrew Thompson 	    (itd->itd_status[3] & htoehci32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
90102ac6454SAndrew Thompson 	printf(" status[4]=0x%08x; <%s>\n", ehci32toh(sc, itd->itd_status[4]),
90202ac6454SAndrew Thompson 	    (itd->itd_status[4] & htoehci32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
90302ac6454SAndrew Thompson 	printf(" status[5]=0x%08x; <%s>\n", ehci32toh(sc, itd->itd_status[5]),
90402ac6454SAndrew Thompson 	    (itd->itd_status[5] & htoehci32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
90502ac6454SAndrew Thompson 	printf(" status[6]=0x%08x; <%s>\n", ehci32toh(sc, itd->itd_status[6]),
90602ac6454SAndrew Thompson 	    (itd->itd_status[6] & htoehci32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
90702ac6454SAndrew Thompson 	printf(" status[7]=0x%08x; <%s>\n", ehci32toh(sc, itd->itd_status[7]),
90802ac6454SAndrew Thompson 	    (itd->itd_status[7] & htoehci32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
90902ac6454SAndrew Thompson 	printf(" bp[0]=0x%08x\n", ehci32toh(sc, itd->itd_bp[0]));
91002ac6454SAndrew Thompson 	printf("  addr=0x%02x; endpt=0x%01x\n",
91102ac6454SAndrew Thompson 	    EHCI_ITD_GET_ADDR(ehci32toh(sc, itd->itd_bp[0])),
91202ac6454SAndrew Thompson 	    EHCI_ITD_GET_ENDPT(ehci32toh(sc, itd->itd_bp[0])));
91302ac6454SAndrew Thompson 	printf(" bp[1]=0x%08x\n", ehci32toh(sc, itd->itd_bp[1]));
91402ac6454SAndrew Thompson 	printf(" dir=%s; mpl=0x%02x\n",
91502ac6454SAndrew Thompson 	    (ehci32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
91602ac6454SAndrew Thompson 	    EHCI_ITD_GET_MPL(ehci32toh(sc, itd->itd_bp[1])));
91702ac6454SAndrew Thompson 	printf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
91802ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp[2]),
91902ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp[3]),
92002ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp[4]),
92102ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp[5]),
92202ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp[6]));
92302ac6454SAndrew Thompson 	printf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
92402ac6454SAndrew Thompson 	    "       0x%08x,0x%08x,0x%08x\n",
92502ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp_hi[0]),
92602ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp_hi[1]),
92702ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp_hi[2]),
92802ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp_hi[3]),
92902ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp_hi[4]),
93002ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp_hi[5]),
93102ac6454SAndrew Thompson 	    ehci32toh(sc, itd->itd_bp_hi[6]));
93202ac6454SAndrew Thompson }
93302ac6454SAndrew Thompson 
93402ac6454SAndrew Thompson static void
93502ac6454SAndrew Thompson ehci_dump_isoc(ehci_softc_t *sc)
93602ac6454SAndrew Thompson {
93702ac6454SAndrew Thompson 	ehci_itd_t *itd;
93802ac6454SAndrew Thompson 	ehci_sitd_t *sitd;
93902ac6454SAndrew Thompson 	uint16_t max = 1000;
94002ac6454SAndrew Thompson 	uint16_t pos;
94102ac6454SAndrew Thompson 
94202ac6454SAndrew Thompson 	pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
94302ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson 	printf("%s: isochronous dump from frame 0x%03x:\n",
94602ac6454SAndrew Thompson 	    __FUNCTION__, pos);
94702ac6454SAndrew Thompson 
94802ac6454SAndrew Thompson 	itd = sc->sc_isoc_hs_p_last[pos];
94902ac6454SAndrew Thompson 	sitd = sc->sc_isoc_fs_p_last[pos];
95002ac6454SAndrew Thompson 
95102ac6454SAndrew Thompson 	while (itd && max && max--) {
95202ac6454SAndrew Thompson 		ehci_dump_itd(sc, itd);
95302ac6454SAndrew Thompson 		itd = itd->prev;
95402ac6454SAndrew Thompson 	}
95502ac6454SAndrew Thompson 
95602ac6454SAndrew Thompson 	while (sitd && max && max--) {
95702ac6454SAndrew Thompson 		ehci_dump_sitd(sc, sitd);
95802ac6454SAndrew Thompson 		sitd = sitd->prev;
95902ac6454SAndrew Thompson 	}
96002ac6454SAndrew Thompson }
96102ac6454SAndrew Thompson 
96202ac6454SAndrew Thompson #endif
96302ac6454SAndrew Thompson 
96402ac6454SAndrew Thompson static void
96502ac6454SAndrew Thompson ehci_transfer_intr_enqueue(struct usb2_xfer *xfer)
96602ac6454SAndrew Thompson {
96702ac6454SAndrew Thompson 	/* check for early completion */
96802ac6454SAndrew Thompson 	if (ehci_check_transfer(xfer)) {
96902ac6454SAndrew Thompson 		return;
97002ac6454SAndrew Thompson 	}
97102ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
97202ac6454SAndrew Thompson 	usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
97302ac6454SAndrew Thompson 
97402ac6454SAndrew Thompson 	/* start timeout, if any */
97502ac6454SAndrew Thompson 	if (xfer->timeout != 0) {
97602ac6454SAndrew Thompson 		usb2_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
97702ac6454SAndrew Thompson 	}
97802ac6454SAndrew Thompson }
97902ac6454SAndrew Thompson 
98002ac6454SAndrew Thompson #define	EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last)
98102ac6454SAndrew Thompson static ehci_sitd_t *
98202ac6454SAndrew Thompson _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
98302ac6454SAndrew Thompson {
98402ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
98702ac6454SAndrew Thompson 
98802ac6454SAndrew Thompson 	std->next = last->next;
98902ac6454SAndrew Thompson 	std->sitd_next = last->sitd_next;
99002ac6454SAndrew Thompson 
99102ac6454SAndrew Thompson 	std->prev = last;
99202ac6454SAndrew Thompson 
99302ac6454SAndrew Thompson 	usb2_pc_cpu_flush(std->page_cache);
99402ac6454SAndrew Thompson 
99502ac6454SAndrew Thompson 	/*
99602ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
99702ac6454SAndrew Thompson 	 */
99802ac6454SAndrew Thompson 	last->next = std;
99902ac6454SAndrew Thompson 	last->sitd_next = std->sitd_self;
100002ac6454SAndrew Thompson 
100102ac6454SAndrew Thompson 	usb2_pc_cpu_flush(last->page_cache);
100202ac6454SAndrew Thompson 
100302ac6454SAndrew Thompson 	return (std);
100402ac6454SAndrew Thompson }
100502ac6454SAndrew Thompson 
100602ac6454SAndrew Thompson #define	EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last)
100702ac6454SAndrew Thompson static ehci_itd_t *
100802ac6454SAndrew Thompson _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last)
100902ac6454SAndrew Thompson {
101002ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
101102ac6454SAndrew Thompson 
101202ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
101302ac6454SAndrew Thompson 
101402ac6454SAndrew Thompson 	std->next = last->next;
101502ac6454SAndrew Thompson 	std->itd_next = last->itd_next;
101602ac6454SAndrew Thompson 
101702ac6454SAndrew Thompson 	std->prev = last;
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson 	usb2_pc_cpu_flush(std->page_cache);
102002ac6454SAndrew Thompson 
102102ac6454SAndrew Thompson 	/*
102202ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
102302ac6454SAndrew Thompson 	 */
102402ac6454SAndrew Thompson 	last->next = std;
102502ac6454SAndrew Thompson 	last->itd_next = std->itd_self;
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson 	usb2_pc_cpu_flush(last->page_cache);
102802ac6454SAndrew Thompson 
102902ac6454SAndrew Thompson 	return (std);
103002ac6454SAndrew Thompson }
103102ac6454SAndrew Thompson 
103202ac6454SAndrew Thompson #define	EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last)
103302ac6454SAndrew Thompson static ehci_qh_t *
103402ac6454SAndrew Thompson _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
103502ac6454SAndrew Thompson {
103602ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", sqh, last);
103702ac6454SAndrew Thompson 
103802ac6454SAndrew Thompson 	if (sqh->prev != NULL) {
103902ac6454SAndrew Thompson 		/* should not happen */
104002ac6454SAndrew Thompson 		DPRINTFN(0, "QH already linked!\n");
104102ac6454SAndrew Thompson 		return (last);
104202ac6454SAndrew Thompson 	}
104302ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
104402ac6454SAndrew Thompson 
104502ac6454SAndrew Thompson 	sqh->next = last->next;
104602ac6454SAndrew Thompson 	sqh->qh_link = last->qh_link;
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson 	sqh->prev = last;
104902ac6454SAndrew Thompson 
105002ac6454SAndrew Thompson 	usb2_pc_cpu_flush(sqh->page_cache);
105102ac6454SAndrew Thompson 
105202ac6454SAndrew Thompson 	/*
105302ac6454SAndrew Thompson 	 * the last->next->prev is never followed: sqh->next->prev = sqh;
105402ac6454SAndrew Thompson 	 */
105502ac6454SAndrew Thompson 
105602ac6454SAndrew Thompson 	last->next = sqh;
105702ac6454SAndrew Thompson 	last->qh_link = sqh->qh_self;
105802ac6454SAndrew Thompson 
105902ac6454SAndrew Thompson 	usb2_pc_cpu_flush(last->page_cache);
106002ac6454SAndrew Thompson 
106102ac6454SAndrew Thompson 	return (sqh);
106202ac6454SAndrew Thompson }
106302ac6454SAndrew Thompson 
106402ac6454SAndrew Thompson #define	EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last)
106502ac6454SAndrew Thompson static ehci_sitd_t *
106602ac6454SAndrew Thompson _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last)
106702ac6454SAndrew Thompson {
106802ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
106902ac6454SAndrew Thompson 
107002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
107102ac6454SAndrew Thompson 
107202ac6454SAndrew Thompson 	std->prev->next = std->next;
107302ac6454SAndrew Thompson 	std->prev->sitd_next = std->sitd_next;
107402ac6454SAndrew Thompson 
107502ac6454SAndrew Thompson 	usb2_pc_cpu_flush(std->prev->page_cache);
107602ac6454SAndrew Thompson 
107702ac6454SAndrew Thompson 	if (std->next) {
107802ac6454SAndrew Thompson 		std->next->prev = std->prev;
107902ac6454SAndrew Thompson 		usb2_pc_cpu_flush(std->next->page_cache);
108002ac6454SAndrew Thompson 	}
108102ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
108202ac6454SAndrew Thompson }
108302ac6454SAndrew Thompson 
108402ac6454SAndrew Thompson #define	EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last)
108502ac6454SAndrew Thompson static ehci_itd_t *
108602ac6454SAndrew Thompson _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last)
108702ac6454SAndrew Thompson {
108802ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
108902ac6454SAndrew Thompson 
109002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
109102ac6454SAndrew Thompson 
109202ac6454SAndrew Thompson 	std->prev->next = std->next;
109302ac6454SAndrew Thompson 	std->prev->itd_next = std->itd_next;
109402ac6454SAndrew Thompson 
109502ac6454SAndrew Thompson 	usb2_pc_cpu_flush(std->prev->page_cache);
109602ac6454SAndrew Thompson 
109702ac6454SAndrew Thompson 	if (std->next) {
109802ac6454SAndrew Thompson 		std->next->prev = std->prev;
109902ac6454SAndrew Thompson 		usb2_pc_cpu_flush(std->next->page_cache);
110002ac6454SAndrew Thompson 	}
110102ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
110202ac6454SAndrew Thompson }
110302ac6454SAndrew Thompson 
110402ac6454SAndrew Thompson #define	EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last)
110502ac6454SAndrew Thompson static ehci_qh_t *
110602ac6454SAndrew Thompson _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
110702ac6454SAndrew Thompson {
110802ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", sqh, last);
110902ac6454SAndrew Thompson 
111002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
111102ac6454SAndrew Thompson 
111202ac6454SAndrew Thompson 	/* only remove if not removed from a queue */
111302ac6454SAndrew Thompson 	if (sqh->prev) {
111402ac6454SAndrew Thompson 
111502ac6454SAndrew Thompson 		sqh->prev->next = sqh->next;
111602ac6454SAndrew Thompson 		sqh->prev->qh_link = sqh->qh_link;
111702ac6454SAndrew Thompson 
111802ac6454SAndrew Thompson 		usb2_pc_cpu_flush(sqh->prev->page_cache);
111902ac6454SAndrew Thompson 
112002ac6454SAndrew Thompson 		if (sqh->next) {
112102ac6454SAndrew Thompson 			sqh->next->prev = sqh->prev;
112202ac6454SAndrew Thompson 			usb2_pc_cpu_flush(sqh->next->page_cache);
112302ac6454SAndrew Thompson 		}
112402ac6454SAndrew Thompson 		last = ((last == sqh) ? sqh->prev : last);
112502ac6454SAndrew Thompson 
112602ac6454SAndrew Thompson 		sqh->prev = 0;
112702ac6454SAndrew Thompson 
112802ac6454SAndrew Thompson 		usb2_pc_cpu_flush(sqh->page_cache);
112902ac6454SAndrew Thompson 	}
113002ac6454SAndrew Thompson 	return (last);
113102ac6454SAndrew Thompson }
113202ac6454SAndrew Thompson 
113302ac6454SAndrew Thompson static usb2_error_t
113402ac6454SAndrew Thompson ehci_non_isoc_done_sub(struct usb2_xfer *xfer)
113502ac6454SAndrew Thompson {
113602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
113702ac6454SAndrew Thompson 	ehci_qtd_t *td;
113802ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
113902ac6454SAndrew Thompson 	uint32_t status;
114002ac6454SAndrew Thompson 	uint16_t len;
114102ac6454SAndrew Thompson 
114202ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
114302ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
114402ac6454SAndrew Thompson 
114502ac6454SAndrew Thompson 	if (xfer->aframes != xfer->nframes) {
114602ac6454SAndrew Thompson 		xfer->frlengths[xfer->aframes] = 0;
114702ac6454SAndrew Thompson 	}
114802ac6454SAndrew Thompson 	while (1) {
114902ac6454SAndrew Thompson 
115002ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
115102ac6454SAndrew Thompson 		status = ehci32toh(sc, td->qtd_status);
115202ac6454SAndrew Thompson 
115302ac6454SAndrew Thompson 		len = EHCI_QTD_GET_BYTES(status);
115402ac6454SAndrew Thompson 
115502ac6454SAndrew Thompson 		/*
115602ac6454SAndrew Thompson 	         * Verify the status length and
115702ac6454SAndrew Thompson 		 * add the length to "frlengths[]":
115802ac6454SAndrew Thompson 	         */
115902ac6454SAndrew Thompson 		if (len > td->len) {
116002ac6454SAndrew Thompson 			/* should not happen */
116102ac6454SAndrew Thompson 			DPRINTF("Invalid status length, "
116202ac6454SAndrew Thompson 			    "0x%04x/0x%04x bytes\n", len, td->len);
116302ac6454SAndrew Thompson 			status |= EHCI_QTD_HALTED;
116402ac6454SAndrew Thompson 		} else if (xfer->aframes != xfer->nframes) {
116502ac6454SAndrew Thompson 			xfer->frlengths[xfer->aframes] += td->len - len;
116602ac6454SAndrew Thompson 		}
116702ac6454SAndrew Thompson 		/* Check for last transfer */
116802ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
116902ac6454SAndrew Thompson 			if (len == 0) {
117002ac6454SAndrew Thompson 				/*
117102ac6454SAndrew Thompson 			         * Halt is ok if descriptor is last,
117202ac6454SAndrew Thompson 			         * and complete:
117302ac6454SAndrew Thompson 			         */
117402ac6454SAndrew Thompson 				status &= ~EHCI_QTD_HALTED;
117502ac6454SAndrew Thompson 			}
117602ac6454SAndrew Thompson 			td = NULL;
117702ac6454SAndrew Thompson 			break;
117802ac6454SAndrew Thompson 		}
117902ac6454SAndrew Thompson 		/* Check for transfer error */
118002ac6454SAndrew Thompson 		if (status & EHCI_QTD_HALTED) {
118102ac6454SAndrew Thompson 			/* the transfer is finished */
118202ac6454SAndrew Thompson 			td = NULL;
118302ac6454SAndrew Thompson 			break;
118402ac6454SAndrew Thompson 		}
118502ac6454SAndrew Thompson 		/* Check for short transfer */
118602ac6454SAndrew Thompson 		if (len > 0) {
118702ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
118802ac6454SAndrew Thompson 				/* follow alt next */
118902ac6454SAndrew Thompson 				td = td->alt_next;
119002ac6454SAndrew Thompson 			} else {
119102ac6454SAndrew Thompson 				/* the transfer is finished */
119202ac6454SAndrew Thompson 				td = NULL;
119302ac6454SAndrew Thompson 			}
119402ac6454SAndrew Thompson 			break;
119502ac6454SAndrew Thompson 		}
119602ac6454SAndrew Thompson 		td = td->obj_next;
119702ac6454SAndrew Thompson 
119802ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
119902ac6454SAndrew Thompson 			/* this USB frame is complete */
120002ac6454SAndrew Thompson 			break;
120102ac6454SAndrew Thompson 		}
120202ac6454SAndrew Thompson 	}
120302ac6454SAndrew Thompson 
120402ac6454SAndrew Thompson 	/* update transfer cache */
120502ac6454SAndrew Thompson 
120602ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
120702ac6454SAndrew Thompson 
120802ac6454SAndrew Thompson 	/* update data toggle */
120902ac6454SAndrew Thompson 
121002ac6454SAndrew Thompson 	xfer->pipe->toggle_next =
121102ac6454SAndrew Thompson 	    (status & EHCI_QTD_TOGGLE_MASK) ? 1 : 0;
121202ac6454SAndrew Thompson 
121302ac6454SAndrew Thompson #if USB_DEBUG
121402ac6454SAndrew Thompson 	if (status & EHCI_QTD_STATERRS) {
121502ac6454SAndrew Thompson 		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
121602ac6454SAndrew Thompson 		    "status=%s%s%s%s%s%s%s%s\n",
121702ac6454SAndrew Thompson 		    xfer->address, xfer->endpoint, xfer->aframes,
121802ac6454SAndrew Thompson 		    (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
121902ac6454SAndrew Thompson 		    (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
122002ac6454SAndrew Thompson 		    (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
122102ac6454SAndrew Thompson 		    (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
122202ac6454SAndrew Thompson 		    (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
122302ac6454SAndrew Thompson 		    (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
122402ac6454SAndrew Thompson 		    (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
122502ac6454SAndrew Thompson 		    (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
122602ac6454SAndrew Thompson 	}
122702ac6454SAndrew Thompson #endif
122802ac6454SAndrew Thompson 
122902ac6454SAndrew Thompson 	return ((status & EHCI_QTD_HALTED) ?
123002ac6454SAndrew Thompson 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
123102ac6454SAndrew Thompson }
123202ac6454SAndrew Thompson 
123302ac6454SAndrew Thompson static void
123402ac6454SAndrew Thompson ehci_non_isoc_done(struct usb2_xfer *xfer)
123502ac6454SAndrew Thompson {
123602ac6454SAndrew Thompson 	usb2_error_t err = 0;
123702ac6454SAndrew Thompson 
123802ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
123902ac6454SAndrew Thompson 	    xfer, xfer->pipe);
124002ac6454SAndrew Thompson 
124102ac6454SAndrew Thompson #if USB_DEBUG
124202ac6454SAndrew Thompson 	if (ehcidebug > 10) {
124302ac6454SAndrew Thompson 		ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
124402ac6454SAndrew Thompson 
124502ac6454SAndrew Thompson 		ehci_dump_sqtds(sc, xfer->td_transfer_first);
124602ac6454SAndrew Thompson 	}
124702ac6454SAndrew Thompson #endif
124802ac6454SAndrew Thompson 
124902ac6454SAndrew Thompson 	/* reset scanner */
125002ac6454SAndrew Thompson 
125102ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
125202ac6454SAndrew Thompson 
125302ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
125402ac6454SAndrew Thompson 
125502ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
125602ac6454SAndrew Thompson 
125702ac6454SAndrew Thompson 			err = ehci_non_isoc_done_sub(xfer);
125802ac6454SAndrew Thompson 		}
125902ac6454SAndrew Thompson 		xfer->aframes = 1;
126002ac6454SAndrew Thompson 
126102ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
126202ac6454SAndrew Thompson 			goto done;
126302ac6454SAndrew Thompson 		}
126402ac6454SAndrew Thompson 	}
126502ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
126602ac6454SAndrew Thompson 
126702ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
126802ac6454SAndrew Thompson 		xfer->aframes++;
126902ac6454SAndrew Thompson 
127002ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
127102ac6454SAndrew Thompson 			goto done;
127202ac6454SAndrew Thompson 		}
127302ac6454SAndrew Thompson 	}
127402ac6454SAndrew Thompson 
127502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
127602ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
127702ac6454SAndrew Thompson 
127802ac6454SAndrew Thompson 		err = ehci_non_isoc_done_sub(xfer);
127902ac6454SAndrew Thompson 	}
128002ac6454SAndrew Thompson done:
128102ac6454SAndrew Thompson 	ehci_device_done(xfer, err);
128202ac6454SAndrew Thompson }
128302ac6454SAndrew Thompson 
128402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
128502ac6454SAndrew Thompson  *	ehci_check_transfer
128602ac6454SAndrew Thompson  *
128702ac6454SAndrew Thompson  * Return values:
128802ac6454SAndrew Thompson  *    0: USB transfer is not finished
128902ac6454SAndrew Thompson  * Else: USB transfer is finished
129002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
129102ac6454SAndrew Thompson static uint8_t
129202ac6454SAndrew Thompson ehci_check_transfer(struct usb2_xfer *xfer)
129302ac6454SAndrew Thompson {
129402ac6454SAndrew Thompson 	struct usb2_pipe_methods *methods = xfer->pipe->methods;
129502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
129602ac6454SAndrew Thompson 
129702ac6454SAndrew Thompson 	uint32_t status;
129802ac6454SAndrew Thompson 
129902ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
130002ac6454SAndrew Thompson 
130102ac6454SAndrew Thompson 	if (methods == &ehci_device_isoc_fs_methods) {
130202ac6454SAndrew Thompson 		ehci_sitd_t *td;
130302ac6454SAndrew Thompson 
130402ac6454SAndrew Thompson 		/* isochronous full speed transfer */
130502ac6454SAndrew Thompson 
130602ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
130702ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
130802ac6454SAndrew Thompson 		status = ehci32toh(sc, td->sitd_status);
130902ac6454SAndrew Thompson 
131002ac6454SAndrew Thompson 		/* also check if first is complete */
131102ac6454SAndrew Thompson 
131202ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
131302ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
131402ac6454SAndrew Thompson 		status |= ehci32toh(sc, td->sitd_status);
131502ac6454SAndrew Thompson 
131602ac6454SAndrew Thompson 		if (!(status & EHCI_SITD_ACTIVE)) {
131702ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
131802ac6454SAndrew Thompson 			goto transferred;
131902ac6454SAndrew Thompson 		}
132002ac6454SAndrew Thompson 	} else if (methods == &ehci_device_isoc_hs_methods) {
132102ac6454SAndrew Thompson 		ehci_itd_t *td;
132202ac6454SAndrew Thompson 
132302ac6454SAndrew Thompson 		/* isochronous high speed transfer */
132402ac6454SAndrew Thompson 
132502ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
132602ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
132702ac6454SAndrew Thompson 		status =
132802ac6454SAndrew Thompson 		    td->itd_status[0] | td->itd_status[1] |
132902ac6454SAndrew Thompson 		    td->itd_status[2] | td->itd_status[3] |
133002ac6454SAndrew Thompson 		    td->itd_status[4] | td->itd_status[5] |
133102ac6454SAndrew Thompson 		    td->itd_status[6] | td->itd_status[7];
133202ac6454SAndrew Thompson 
133302ac6454SAndrew Thompson 		/* also check first transfer */
133402ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
133502ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
133602ac6454SAndrew Thompson 		status |=
133702ac6454SAndrew Thompson 		    td->itd_status[0] | td->itd_status[1] |
133802ac6454SAndrew Thompson 		    td->itd_status[2] | td->itd_status[3] |
133902ac6454SAndrew Thompson 		    td->itd_status[4] | td->itd_status[5] |
134002ac6454SAndrew Thompson 		    td->itd_status[6] | td->itd_status[7];
134102ac6454SAndrew Thompson 
134202ac6454SAndrew Thompson 		/* if no transactions are active we continue */
134302ac6454SAndrew Thompson 		if (!(status & htoehci32(sc, EHCI_ITD_ACTIVE))) {
134402ac6454SAndrew Thompson 			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
134502ac6454SAndrew Thompson 			goto transferred;
134602ac6454SAndrew Thompson 		}
134702ac6454SAndrew Thompson 	} else {
134802ac6454SAndrew Thompson 		ehci_qtd_t *td;
134902ac6454SAndrew Thompson 
135002ac6454SAndrew Thompson 		/* non-isochronous transfer */
135102ac6454SAndrew Thompson 
135202ac6454SAndrew Thompson 		/*
135302ac6454SAndrew Thompson 		 * check whether there is an error somewhere in the middle,
135402ac6454SAndrew Thompson 		 * or whether there was a short packet (SPD and not ACTIVE)
135502ac6454SAndrew Thompson 		 */
135602ac6454SAndrew Thompson 		td = xfer->td_transfer_cache;
135702ac6454SAndrew Thompson 
135802ac6454SAndrew Thompson 		while (1) {
135902ac6454SAndrew Thompson 			usb2_pc_cpu_invalidate(td->page_cache);
136002ac6454SAndrew Thompson 			status = ehci32toh(sc, td->qtd_status);
136102ac6454SAndrew Thompson 
136202ac6454SAndrew Thompson 			/*
136302ac6454SAndrew Thompson 			 * if there is an active TD the transfer isn't done
136402ac6454SAndrew Thompson 			 */
136502ac6454SAndrew Thompson 			if (status & EHCI_QTD_ACTIVE) {
136602ac6454SAndrew Thompson 				/* update cache */
136702ac6454SAndrew Thompson 				xfer->td_transfer_cache = td;
136802ac6454SAndrew Thompson 				goto done;
136902ac6454SAndrew Thompson 			}
137002ac6454SAndrew Thompson 			/*
137102ac6454SAndrew Thompson 			 * last transfer descriptor makes the transfer done
137202ac6454SAndrew Thompson 			 */
137302ac6454SAndrew Thompson 			if (((void *)td) == xfer->td_transfer_last) {
137402ac6454SAndrew Thompson 				break;
137502ac6454SAndrew Thompson 			}
137602ac6454SAndrew Thompson 			/*
137702ac6454SAndrew Thompson 			 * any kind of error makes the transfer done
137802ac6454SAndrew Thompson 			 */
137902ac6454SAndrew Thompson 			if (status & EHCI_QTD_HALTED) {
138002ac6454SAndrew Thompson 				break;
138102ac6454SAndrew Thompson 			}
138202ac6454SAndrew Thompson 			/*
138302ac6454SAndrew Thompson 			 * if there is no alternate next transfer, a short
138402ac6454SAndrew Thompson 			 * packet also makes the transfer done
138502ac6454SAndrew Thompson 			 */
138602ac6454SAndrew Thompson 			if (EHCI_QTD_GET_BYTES(status)) {
138702ac6454SAndrew Thompson 				if (xfer->flags_int.short_frames_ok) {
138802ac6454SAndrew Thompson 					/* follow alt next */
138902ac6454SAndrew Thompson 					if (td->alt_next) {
139002ac6454SAndrew Thompson 						td = td->alt_next;
139102ac6454SAndrew Thompson 						continue;
139202ac6454SAndrew Thompson 					}
139302ac6454SAndrew Thompson 				}
139402ac6454SAndrew Thompson 				/* transfer is done */
139502ac6454SAndrew Thompson 				break;
139602ac6454SAndrew Thompson 			}
139702ac6454SAndrew Thompson 			td = td->obj_next;
139802ac6454SAndrew Thompson 		}
139902ac6454SAndrew Thompson 		ehci_non_isoc_done(xfer);
140002ac6454SAndrew Thompson 		goto transferred;
140102ac6454SAndrew Thompson 	}
140202ac6454SAndrew Thompson 
140302ac6454SAndrew Thompson done:
140402ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
140502ac6454SAndrew Thompson 	return (0);
140602ac6454SAndrew Thompson 
140702ac6454SAndrew Thompson transferred:
140802ac6454SAndrew Thompson 	return (1);
140902ac6454SAndrew Thompson }
141002ac6454SAndrew Thompson 
141102ac6454SAndrew Thompson static void
141202ac6454SAndrew Thompson ehci_pcd_enable(ehci_softc_t *sc)
141302ac6454SAndrew Thompson {
141402ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
141502ac6454SAndrew Thompson 
141602ac6454SAndrew Thompson 	sc->sc_eintrs |= EHCI_STS_PCD;
141702ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
141802ac6454SAndrew Thompson 
141902ac6454SAndrew Thompson 	/* acknowledge any PCD interrupt */
142002ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);
142102ac6454SAndrew Thompson 
142202ac6454SAndrew Thompson 	usb2_sw_transfer(&sc->sc_root_intr,
142302ac6454SAndrew Thompson 	    &ehci_root_intr_done);
142402ac6454SAndrew Thompson }
142502ac6454SAndrew Thompson 
142602ac6454SAndrew Thompson static void
142702ac6454SAndrew Thompson ehci_interrupt_poll(ehci_softc_t *sc)
142802ac6454SAndrew Thompson {
142902ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
143002ac6454SAndrew Thompson 
143102ac6454SAndrew Thompson repeat:
143202ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
143302ac6454SAndrew Thompson 		/*
143402ac6454SAndrew Thompson 		 * check if transfer is transferred
143502ac6454SAndrew Thompson 		 */
143602ac6454SAndrew Thompson 		if (ehci_check_transfer(xfer)) {
143702ac6454SAndrew Thompson 			/* queue has been modified */
143802ac6454SAndrew Thompson 			goto repeat;
143902ac6454SAndrew Thompson 		}
144002ac6454SAndrew Thompson 	}
144102ac6454SAndrew Thompson }
144202ac6454SAndrew Thompson 
144302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
144402ac6454SAndrew Thompson  *	ehci_interrupt - EHCI interrupt handler
144502ac6454SAndrew Thompson  *
144602ac6454SAndrew Thompson  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
144702ac6454SAndrew Thompson  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
144802ac6454SAndrew Thompson  * is present !
144902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
145002ac6454SAndrew Thompson void
145102ac6454SAndrew Thompson ehci_interrupt(ehci_softc_t *sc)
145202ac6454SAndrew Thompson {
145302ac6454SAndrew Thompson 	uint32_t status;
145402ac6454SAndrew Thompson 
145502ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
145602ac6454SAndrew Thompson 
145702ac6454SAndrew Thompson 	DPRINTFN(16, "real interrupt\n");
145802ac6454SAndrew Thompson 
145902ac6454SAndrew Thompson #if USB_DEBUG
146002ac6454SAndrew Thompson 	if (ehcidebug > 15) {
146102ac6454SAndrew Thompson 		ehci_dump_regs(sc);
146202ac6454SAndrew Thompson 	}
146302ac6454SAndrew Thompson #endif
146402ac6454SAndrew Thompson 
146502ac6454SAndrew Thompson 	status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
146602ac6454SAndrew Thompson 	if (status == 0) {
146702ac6454SAndrew Thompson 		/* the interrupt was not for us */
146802ac6454SAndrew Thompson 		goto done;
146902ac6454SAndrew Thompson 	}
147002ac6454SAndrew Thompson 	if (!(status & sc->sc_eintrs)) {
147102ac6454SAndrew Thompson 		goto done;
147202ac6454SAndrew Thompson 	}
147302ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBSTS, status);	/* acknowledge */
147402ac6454SAndrew Thompson 
147502ac6454SAndrew Thompson 	status &= sc->sc_eintrs;
147602ac6454SAndrew Thompson 
147702ac6454SAndrew Thompson 	if (status & EHCI_STS_HSE) {
147802ac6454SAndrew Thompson 		printf("%s: unrecoverable error, "
147902ac6454SAndrew Thompson 		    "controller halted\n", __FUNCTION__);
148002ac6454SAndrew Thompson #if USB_DEBUG
148102ac6454SAndrew Thompson 		ehci_dump_regs(sc);
148202ac6454SAndrew Thompson 		ehci_dump_isoc(sc);
148302ac6454SAndrew Thompson #endif
148402ac6454SAndrew Thompson 	}
148502ac6454SAndrew Thompson 	if (status & EHCI_STS_PCD) {
148602ac6454SAndrew Thompson 		/*
148702ac6454SAndrew Thompson 		 * Disable PCD interrupt for now, because it will be
148802ac6454SAndrew Thompson 		 * on until the port has been reset.
148902ac6454SAndrew Thompson 		 */
149002ac6454SAndrew Thompson 		sc->sc_eintrs &= ~EHCI_STS_PCD;
149102ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
149202ac6454SAndrew Thompson 
149302ac6454SAndrew Thompson 		usb2_sw_transfer(&sc->sc_root_intr,
149402ac6454SAndrew Thompson 		    &ehci_root_intr_done);
149502ac6454SAndrew Thompson 
149602ac6454SAndrew Thompson 		/* do not allow RHSC interrupts > 1 per second */
149702ac6454SAndrew Thompson 		usb2_callout_reset(&sc->sc_tmo_pcd, hz,
149802ac6454SAndrew Thompson 		    (void *)&ehci_pcd_enable, sc);
149902ac6454SAndrew Thompson 	}
150002ac6454SAndrew Thompson 	status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);
150102ac6454SAndrew Thompson 
150202ac6454SAndrew Thompson 	if (status != 0) {
150302ac6454SAndrew Thompson 		/* block unprocessed interrupts */
150402ac6454SAndrew Thompson 		sc->sc_eintrs &= ~status;
150502ac6454SAndrew Thompson 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
150602ac6454SAndrew Thompson 		printf("%s: blocking interrupts 0x%x\n", __FUNCTION__, status);
150702ac6454SAndrew Thompson 	}
150802ac6454SAndrew Thompson 	/* poll all the USB transfers */
150902ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
151002ac6454SAndrew Thompson 
151102ac6454SAndrew Thompson done:
151202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
151302ac6454SAndrew Thompson }
151402ac6454SAndrew Thompson 
151502ac6454SAndrew Thompson /*
151602ac6454SAndrew Thompson  * called when a request does not complete
151702ac6454SAndrew Thompson  */
151802ac6454SAndrew Thompson static void
151902ac6454SAndrew Thompson ehci_timeout(void *arg)
152002ac6454SAndrew Thompson {
152102ac6454SAndrew Thompson 	struct usb2_xfer *xfer = arg;
152202ac6454SAndrew Thompson 
152302ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
152402ac6454SAndrew Thompson 
152502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
152602ac6454SAndrew Thompson 
152702ac6454SAndrew Thompson 	/* transfer is transferred */
152802ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_TIMEOUT);
152902ac6454SAndrew Thompson }
153002ac6454SAndrew Thompson 
153102ac6454SAndrew Thompson static void
153202ac6454SAndrew Thompson ehci_do_poll(struct usb2_bus *bus)
153302ac6454SAndrew Thompson {
153402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
153502ac6454SAndrew Thompson 
153602ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
153702ac6454SAndrew Thompson 	ehci_interrupt_poll(sc);
153802ac6454SAndrew Thompson 	ehci_root_ctrl_poll(sc);
153902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
154002ac6454SAndrew Thompson }
154102ac6454SAndrew Thompson 
154202ac6454SAndrew Thompson static void
154302ac6454SAndrew Thompson ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
154402ac6454SAndrew Thompson {
154502ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
154602ac6454SAndrew Thompson 	ehci_qtd_t *td;
154702ac6454SAndrew Thompson 	ehci_qtd_t *td_next;
154802ac6454SAndrew Thompson 	ehci_qtd_t *td_alt_next;
154902ac6454SAndrew Thompson 	uint32_t qtd_altnext;
155002ac6454SAndrew Thompson 	uint32_t buf_offset;
155102ac6454SAndrew Thompson 	uint32_t average;
155202ac6454SAndrew Thompson 	uint32_t len_old;
155302ac6454SAndrew Thompson 	uint8_t shortpkt_old;
155402ac6454SAndrew Thompson 	uint8_t precompute;
155502ac6454SAndrew Thompson 
155602ac6454SAndrew Thompson 	qtd_altnext = htoehci32(temp->sc, EHCI_LINK_TERMINATE);
155702ac6454SAndrew Thompson 	td_alt_next = NULL;
155802ac6454SAndrew Thompson 	buf_offset = 0;
155902ac6454SAndrew Thompson 	shortpkt_old = temp->shortpkt;
156002ac6454SAndrew Thompson 	len_old = temp->len;
156102ac6454SAndrew Thompson 	precompute = 1;
156202ac6454SAndrew Thompson 
156302ac6454SAndrew Thompson restart:
156402ac6454SAndrew Thompson 
156502ac6454SAndrew Thompson 	td = temp->td;
156602ac6454SAndrew Thompson 	td_next = temp->td_next;
156702ac6454SAndrew Thompson 
156802ac6454SAndrew Thompson 	while (1) {
156902ac6454SAndrew Thompson 
157002ac6454SAndrew Thompson 		if (temp->len == 0) {
157102ac6454SAndrew Thompson 
157202ac6454SAndrew Thompson 			if (temp->shortpkt) {
157302ac6454SAndrew Thompson 				break;
157402ac6454SAndrew Thompson 			}
157502ac6454SAndrew Thompson 			/* send a Zero Length Packet, ZLP, last */
157602ac6454SAndrew Thompson 
157702ac6454SAndrew Thompson 			temp->shortpkt = 1;
157802ac6454SAndrew Thompson 			average = 0;
157902ac6454SAndrew Thompson 
158002ac6454SAndrew Thompson 		} else {
158102ac6454SAndrew Thompson 
158202ac6454SAndrew Thompson 			average = temp->average;
158302ac6454SAndrew Thompson 
158402ac6454SAndrew Thompson 			if (temp->len < average) {
158502ac6454SAndrew Thompson 				if (temp->len % temp->max_frame_size) {
158602ac6454SAndrew Thompson 					temp->shortpkt = 1;
158702ac6454SAndrew Thompson 				}
158802ac6454SAndrew Thompson 				average = temp->len;
158902ac6454SAndrew Thompson 			}
159002ac6454SAndrew Thompson 		}
159102ac6454SAndrew Thompson 
159202ac6454SAndrew Thompson 		if (td_next == NULL) {
159302ac6454SAndrew Thompson 			panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
159402ac6454SAndrew Thompson 		}
159502ac6454SAndrew Thompson 		/* get next TD */
159602ac6454SAndrew Thompson 
159702ac6454SAndrew Thompson 		td = td_next;
159802ac6454SAndrew Thompson 		td_next = td->obj_next;
159902ac6454SAndrew Thompson 
160002ac6454SAndrew Thompson 		/* check if we are pre-computing */
160102ac6454SAndrew Thompson 
160202ac6454SAndrew Thompson 		if (precompute) {
160302ac6454SAndrew Thompson 
160402ac6454SAndrew Thompson 			/* update remaining length */
160502ac6454SAndrew Thompson 
160602ac6454SAndrew Thompson 			temp->len -= average;
160702ac6454SAndrew Thompson 
160802ac6454SAndrew Thompson 			continue;
160902ac6454SAndrew Thompson 		}
161002ac6454SAndrew Thompson 		/* fill out current TD */
161102ac6454SAndrew Thompson 
161202ac6454SAndrew Thompson 		td->qtd_status =
161302ac6454SAndrew Thompson 		    temp->qtd_status |
161402ac6454SAndrew Thompson 		    htoehci32(temp->sc, EHCI_QTD_SET_BYTES(average));
161502ac6454SAndrew Thompson 
161602ac6454SAndrew Thompson 		if (average == 0) {
161702ac6454SAndrew Thompson 
161802ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
161902ac6454SAndrew Thompson 
162002ac6454SAndrew Thompson 				/* update data toggle, ZLP case */
162102ac6454SAndrew Thompson 
162202ac6454SAndrew Thompson 				temp->qtd_status ^=
162302ac6454SAndrew Thompson 				    htoehci32(temp->sc, EHCI_QTD_TOGGLE_MASK);
162402ac6454SAndrew Thompson 			}
162502ac6454SAndrew Thompson 			td->len = 0;
162602ac6454SAndrew Thompson 
162702ac6454SAndrew Thompson 			td->qtd_buffer[0] = 0;
162802ac6454SAndrew Thompson 			td->qtd_buffer_hi[0] = 0;
162902ac6454SAndrew Thompson 
163002ac6454SAndrew Thompson 			td->qtd_buffer[1] = 0;
163102ac6454SAndrew Thompson 			td->qtd_buffer_hi[1] = 0;
163202ac6454SAndrew Thompson 
163302ac6454SAndrew Thompson 		} else {
163402ac6454SAndrew Thompson 
163502ac6454SAndrew Thompson 			uint8_t x;
163602ac6454SAndrew Thompson 
163702ac6454SAndrew Thompson 			if (temp->auto_data_toggle == 0) {
163802ac6454SAndrew Thompson 
163902ac6454SAndrew Thompson 				/* update data toggle */
164002ac6454SAndrew Thompson 
164102ac6454SAndrew Thompson 				if (((average + temp->max_frame_size - 1) /
164202ac6454SAndrew Thompson 				    temp->max_frame_size) & 1) {
164302ac6454SAndrew Thompson 					temp->qtd_status ^=
164402ac6454SAndrew Thompson 					    htoehci32(temp->sc, EHCI_QTD_TOGGLE_MASK);
164502ac6454SAndrew Thompson 				}
164602ac6454SAndrew Thompson 			}
164702ac6454SAndrew Thompson 			td->len = average;
164802ac6454SAndrew Thompson 
164902ac6454SAndrew Thompson 			/* update remaining length */
165002ac6454SAndrew Thompson 
165102ac6454SAndrew Thompson 			temp->len -= average;
165202ac6454SAndrew Thompson 
165302ac6454SAndrew Thompson 			/* fill out buffer pointers */
165402ac6454SAndrew Thompson 
165502ac6454SAndrew Thompson 			usb2_get_page(temp->pc, buf_offset, &buf_res);
165602ac6454SAndrew Thompson 			td->qtd_buffer[0] =
165702ac6454SAndrew Thompson 			    htoehci32(temp->sc, buf_res.physaddr);
165802ac6454SAndrew Thompson 			td->qtd_buffer_hi[0] = 0;
165902ac6454SAndrew Thompson 
166002ac6454SAndrew Thompson 			x = 1;
166102ac6454SAndrew Thompson 
166202ac6454SAndrew Thompson 			while (average > EHCI_PAGE_SIZE) {
166302ac6454SAndrew Thompson 				average -= EHCI_PAGE_SIZE;
166402ac6454SAndrew Thompson 				buf_offset += EHCI_PAGE_SIZE;
166502ac6454SAndrew Thompson 				usb2_get_page(temp->pc, buf_offset, &buf_res);
166602ac6454SAndrew Thompson 				td->qtd_buffer[x] =
166702ac6454SAndrew Thompson 				    htoehci32(temp->sc,
166802ac6454SAndrew Thompson 				    buf_res.physaddr & (~0xFFF));
166902ac6454SAndrew Thompson 				td->qtd_buffer_hi[x] = 0;
167002ac6454SAndrew Thompson 				x++;
167102ac6454SAndrew Thompson 			}
167202ac6454SAndrew Thompson 
167302ac6454SAndrew Thompson 			/*
167402ac6454SAndrew Thompson 			 * NOTE: The "average" variable is never zero after
167502ac6454SAndrew Thompson 			 * exiting the loop above !
167602ac6454SAndrew Thompson 			 *
167702ac6454SAndrew Thompson 			 * NOTE: We have to subtract one from the offset to
167802ac6454SAndrew Thompson 			 * ensure that we are computing the physical address
167902ac6454SAndrew Thompson 			 * of a valid page !
168002ac6454SAndrew Thompson 			 */
168102ac6454SAndrew Thompson 			buf_offset += average;
168202ac6454SAndrew Thompson 			usb2_get_page(temp->pc, buf_offset - 1, &buf_res);
168302ac6454SAndrew Thompson 			td->qtd_buffer[x] =
168402ac6454SAndrew Thompson 			    htoehci32(temp->sc,
168502ac6454SAndrew Thompson 			    buf_res.physaddr & (~0xFFF));
168602ac6454SAndrew Thompson 			td->qtd_buffer_hi[x] = 0;
168702ac6454SAndrew Thompson 		}
168802ac6454SAndrew Thompson 
168902ac6454SAndrew Thompson 		if (td_next) {
169002ac6454SAndrew Thompson 			/* link the current TD with the next one */
169102ac6454SAndrew Thompson 			td->qtd_next = td_next->qtd_self;
169202ac6454SAndrew Thompson 		}
169302ac6454SAndrew Thompson 		td->qtd_altnext = qtd_altnext;
169402ac6454SAndrew Thompson 		td->alt_next = td_alt_next;
169502ac6454SAndrew Thompson 
169602ac6454SAndrew Thompson 		usb2_pc_cpu_flush(td->page_cache);
169702ac6454SAndrew Thompson 	}
169802ac6454SAndrew Thompson 
169902ac6454SAndrew Thompson 	if (precompute) {
170002ac6454SAndrew Thompson 		precompute = 0;
170102ac6454SAndrew Thompson 
170202ac6454SAndrew Thompson 		/* setup alt next pointer, if any */
170302ac6454SAndrew Thompson 		if (temp->short_frames_ok) {
170402ac6454SAndrew Thompson 			if (temp->setup_alt_next) {
170502ac6454SAndrew Thompson 				td_alt_next = td_next;
170602ac6454SAndrew Thompson 				qtd_altnext = td_next->qtd_self;
170702ac6454SAndrew Thompson 			}
170802ac6454SAndrew Thompson 		} else {
170902ac6454SAndrew Thompson 			/* we use this field internally */
171002ac6454SAndrew Thompson 			td_alt_next = td_next;
171102ac6454SAndrew Thompson 		}
171202ac6454SAndrew Thompson 
171302ac6454SAndrew Thompson 		/* restore */
171402ac6454SAndrew Thompson 		temp->shortpkt = shortpkt_old;
171502ac6454SAndrew Thompson 		temp->len = len_old;
171602ac6454SAndrew Thompson 		goto restart;
171702ac6454SAndrew Thompson 	}
171802ac6454SAndrew Thompson 	temp->td = td;
171902ac6454SAndrew Thompson 	temp->td_next = td_next;
172002ac6454SAndrew Thompson }
172102ac6454SAndrew Thompson 
172202ac6454SAndrew Thompson static void
172302ac6454SAndrew Thompson ehci_setup_standard_chain(struct usb2_xfer *xfer, ehci_qh_t **qh_last)
172402ac6454SAndrew Thompson {
172502ac6454SAndrew Thompson 	struct ehci_std_temp temp;
172602ac6454SAndrew Thompson 	struct usb2_pipe_methods *methods;
172702ac6454SAndrew Thompson 	ehci_qh_t *qh;
172802ac6454SAndrew Thompson 	ehci_qtd_t *td;
172902ac6454SAndrew Thompson 	uint32_t qh_endp;
173002ac6454SAndrew Thompson 	uint32_t qh_endphub;
173102ac6454SAndrew Thompson 	uint32_t x;
173202ac6454SAndrew Thompson 
173302ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
173402ac6454SAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpoint),
173502ac6454SAndrew Thompson 	    xfer->sumlen, usb2_get_speed(xfer->xroot->udev));
173602ac6454SAndrew Thompson 
173702ac6454SAndrew Thompson 	temp.average = xfer->max_usb2_frame_size;
173802ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
173902ac6454SAndrew Thompson 	temp.sc = EHCI_BUS2SC(xfer->xroot->bus);
174002ac6454SAndrew Thompson 
174102ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
174202ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
174302ac6454SAndrew Thompson 
174402ac6454SAndrew Thompson 	/* get next DMA set */
174502ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
174602ac6454SAndrew Thompson 
174702ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
174802ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
174902ac6454SAndrew Thompson 
175002ac6454SAndrew Thompson 	temp.td = NULL;
175102ac6454SAndrew Thompson 	temp.td_next = td;
175202ac6454SAndrew Thompson 	temp.qtd_status = 0;
175302ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
175402ac6454SAndrew Thompson 	temp.short_frames_ok = xfer->flags_int.short_frames_ok;
175502ac6454SAndrew Thompson 
175602ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
175702ac6454SAndrew Thompson 		if (xfer->pipe->toggle_next) {
175802ac6454SAndrew Thompson 			/* DATA1 is next */
175902ac6454SAndrew Thompson 			temp.qtd_status |=
176002ac6454SAndrew Thompson 			    htoehci32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
176102ac6454SAndrew Thompson 		}
176202ac6454SAndrew Thompson 		temp.auto_data_toggle = 0;
176302ac6454SAndrew Thompson 	} else {
176402ac6454SAndrew Thompson 		temp.auto_data_toggle = 1;
176502ac6454SAndrew Thompson 	}
176602ac6454SAndrew Thompson 
176702ac6454SAndrew Thompson 	if (usb2_get_speed(xfer->xroot->udev) != USB_SPEED_HIGH) {
176802ac6454SAndrew Thompson 		/* max 3 retries */
176902ac6454SAndrew Thompson 		temp.qtd_status |=
177002ac6454SAndrew Thompson 		    htoehci32(temp.sc, EHCI_QTD_SET_CERR(3));
177102ac6454SAndrew Thompson 	}
177202ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
177302ac6454SAndrew Thompson 
177402ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
177502ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
177602ac6454SAndrew Thompson 
177702ac6454SAndrew Thompson 			temp.qtd_status &=
177802ac6454SAndrew Thompson 			    htoehci32(temp.sc, EHCI_QTD_SET_CERR(3));
177902ac6454SAndrew Thompson 			temp.qtd_status |= htole32
178002ac6454SAndrew Thompson 			    (EHCI_QTD_ACTIVE |
178102ac6454SAndrew Thompson 			    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
178202ac6454SAndrew Thompson 			    EHCI_QTD_SET_TOGGLE(0));
178302ac6454SAndrew Thompson 
178402ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
178502ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + 0;
178602ac6454SAndrew Thompson 			temp.shortpkt = temp.len ? 1 : 0;
178702ac6454SAndrew Thompson 
178802ac6454SAndrew Thompson 			ehci_setup_standard_chain_sub(&temp);
178902ac6454SAndrew Thompson 		}
179002ac6454SAndrew Thompson 		x = 1;
179102ac6454SAndrew Thompson 	} else {
179202ac6454SAndrew Thompson 		x = 0;
179302ac6454SAndrew Thompson 	}
179402ac6454SAndrew Thompson 
179502ac6454SAndrew Thompson 	while (x != xfer->nframes) {
179602ac6454SAndrew Thompson 
179702ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
179802ac6454SAndrew Thompson 
179902ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
180002ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
180102ac6454SAndrew Thompson 
180202ac6454SAndrew Thompson 		x++;
180302ac6454SAndrew Thompson 
180402ac6454SAndrew Thompson 		if (x == xfer->nframes) {
180502ac6454SAndrew Thompson 			temp.setup_alt_next = 0;
180602ac6454SAndrew Thompson 		}
180702ac6454SAndrew Thompson 		/* keep previous data toggle and error count */
180802ac6454SAndrew Thompson 
180902ac6454SAndrew Thompson 		temp.qtd_status &=
181002ac6454SAndrew Thompson 		    htoehci32(temp.sc, EHCI_QTD_SET_CERR(3) |
181102ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
181202ac6454SAndrew Thompson 
181302ac6454SAndrew Thompson 		if (temp.len == 0) {
181402ac6454SAndrew Thompson 
181502ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
181602ac6454SAndrew Thompson 
181702ac6454SAndrew Thompson 			temp.shortpkt = 0;
181802ac6454SAndrew Thompson 
181902ac6454SAndrew Thompson 		} else {
182002ac6454SAndrew Thompson 
182102ac6454SAndrew Thompson 			/* regular data transfer */
182202ac6454SAndrew Thompson 
182302ac6454SAndrew Thompson 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
182402ac6454SAndrew Thompson 		}
182502ac6454SAndrew Thompson 
182602ac6454SAndrew Thompson 		/* set endpoint direction */
182702ac6454SAndrew Thompson 
182802ac6454SAndrew Thompson 		temp.qtd_status |=
182902ac6454SAndrew Thompson 		    (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) ?
183002ac6454SAndrew Thompson 		    htoehci32(temp.sc, EHCI_QTD_ACTIVE |
183102ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
183202ac6454SAndrew Thompson 		    htoehci32(temp.sc, EHCI_QTD_ACTIVE |
183302ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));
183402ac6454SAndrew Thompson 
183502ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
183602ac6454SAndrew Thompson 	}
183702ac6454SAndrew Thompson 
183802ac6454SAndrew Thompson 	/* check if we should append a status stage */
183902ac6454SAndrew Thompson 
184002ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
184102ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
184202ac6454SAndrew Thompson 
184302ac6454SAndrew Thompson 		/*
184402ac6454SAndrew Thompson 		 * Send a DATA1 message and invert the current endpoint
184502ac6454SAndrew Thompson 		 * direction.
184602ac6454SAndrew Thompson 		 */
184702ac6454SAndrew Thompson 
184802ac6454SAndrew Thompson 		temp.qtd_status &= htoehci32(temp.sc, EHCI_QTD_SET_CERR(3) |
184902ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
185002ac6454SAndrew Thompson 		temp.qtd_status |=
185102ac6454SAndrew Thompson 		    (UE_GET_DIR(xfer->endpoint) == UE_DIR_OUT) ?
185202ac6454SAndrew Thompson 		    htoehci32(temp.sc, EHCI_QTD_ACTIVE |
185302ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
185402ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1)) :
185502ac6454SAndrew Thompson 		    htoehci32(temp.sc, EHCI_QTD_ACTIVE |
185602ac6454SAndrew Thompson 		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
185702ac6454SAndrew Thompson 		    EHCI_QTD_SET_TOGGLE(1));
185802ac6454SAndrew Thompson 
185902ac6454SAndrew Thompson 		temp.len = 0;
186002ac6454SAndrew Thompson 		temp.pc = NULL;
186102ac6454SAndrew Thompson 		temp.shortpkt = 0;
186202ac6454SAndrew Thompson 
186302ac6454SAndrew Thompson 		ehci_setup_standard_chain_sub(&temp);
186402ac6454SAndrew Thompson 	}
186502ac6454SAndrew Thompson 	td = temp.td;
186602ac6454SAndrew Thompson 
186702ac6454SAndrew Thompson 	/* the last TD terminates the transfer: */
186802ac6454SAndrew Thompson 	td->qtd_next = htoehci32(temp.sc, EHCI_LINK_TERMINATE);
186902ac6454SAndrew Thompson 	td->qtd_altnext = htoehci32(temp.sc, EHCI_LINK_TERMINATE);
187002ac6454SAndrew Thompson 	td->qtd_status |= htoehci32(temp.sc, EHCI_QTD_IOC);
187102ac6454SAndrew Thompson 
187202ac6454SAndrew Thompson 	usb2_pc_cpu_flush(td->page_cache);
187302ac6454SAndrew Thompson 
187402ac6454SAndrew Thompson 	/* must have at least one frame! */
187502ac6454SAndrew Thompson 
187602ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
187702ac6454SAndrew Thompson 
187802ac6454SAndrew Thompson #if USB_DEBUG
187902ac6454SAndrew Thompson 	if (ehcidebug > 8) {
188002ac6454SAndrew Thompson 		DPRINTF("nexttog=%d; data before transfer:\n",
188102ac6454SAndrew Thompson 		    xfer->pipe->toggle_next);
188202ac6454SAndrew Thompson 		ehci_dump_sqtds(temp.sc,
188302ac6454SAndrew Thompson 		    xfer->td_transfer_first);
188402ac6454SAndrew Thompson 	}
188502ac6454SAndrew Thompson #endif
188602ac6454SAndrew Thompson 
188702ac6454SAndrew Thompson 	methods = xfer->pipe->methods;
188802ac6454SAndrew Thompson 
188902ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
189002ac6454SAndrew Thompson 
189102ac6454SAndrew Thompson 	/* the "qh_link" field is filled when the QH is added */
189202ac6454SAndrew Thompson 
189302ac6454SAndrew Thompson 	qh_endp =
189402ac6454SAndrew Thompson 	    (EHCI_QH_SET_ADDR(xfer->address) |
189502ac6454SAndrew Thompson 	    EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpoint)) |
189602ac6454SAndrew Thompson 	    EHCI_QH_SET_MPL(xfer->max_packet_size));
189702ac6454SAndrew Thompson 
189802ac6454SAndrew Thompson 	if (usb2_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
189902ac6454SAndrew Thompson 		qh_endp |= (EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) |
190002ac6454SAndrew Thompson 		    EHCI_QH_DTC);
190102ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods)
190202ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(8);
190302ac6454SAndrew Thompson 	} else {
190402ac6454SAndrew Thompson 
190502ac6454SAndrew Thompson 		if (usb2_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
190602ac6454SAndrew Thompson 			qh_endp |= (EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL) |
190702ac6454SAndrew Thompson 			    EHCI_QH_DTC);
190802ac6454SAndrew Thompson 		} else {
190902ac6454SAndrew Thompson 			qh_endp |= (EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW) |
191002ac6454SAndrew Thompson 			    EHCI_QH_DTC);
191102ac6454SAndrew Thompson 		}
191202ac6454SAndrew Thompson 
191302ac6454SAndrew Thompson 		if (methods == &ehci_device_ctrl_methods) {
191402ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_CTL;
191502ac6454SAndrew Thompson 		}
191602ac6454SAndrew Thompson 		if (methods != &ehci_device_intr_methods) {
191702ac6454SAndrew Thompson 			/* Only try one time per microframe! */
191802ac6454SAndrew Thompson 			qh_endp |= EHCI_QH_SET_NRL(1);
191902ac6454SAndrew Thompson 		}
192002ac6454SAndrew Thompson 	}
192102ac6454SAndrew Thompson 
192202ac6454SAndrew Thompson 	qh->qh_endp = htoehci32(temp.sc, qh_endp);
192302ac6454SAndrew Thompson 
192402ac6454SAndrew Thompson 	qh_endphub =
192502ac6454SAndrew Thompson 	    (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
192602ac6454SAndrew Thompson 	    EHCI_QH_SET_CMASK(xfer->usb2_cmask) |
192702ac6454SAndrew Thompson 	    EHCI_QH_SET_SMASK(xfer->usb2_smask) |
192802ac6454SAndrew Thompson 	    EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
192902ac6454SAndrew Thompson 	    EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));
193002ac6454SAndrew Thompson 
193102ac6454SAndrew Thompson 	qh->qh_endphub = htoehci32(temp.sc, qh_endphub);
193202ac6454SAndrew Thompson 	qh->qh_curqtd = htoehci32(temp.sc, 0);
193302ac6454SAndrew Thompson 
193402ac6454SAndrew Thompson 	/* fill the overlay qTD */
193502ac6454SAndrew Thompson 	qh->qh_qtd.qtd_status = htoehci32(temp.sc, 0);
193602ac6454SAndrew Thompson 
193702ac6454SAndrew Thompson 	if (temp.auto_data_toggle) {
193802ac6454SAndrew Thompson 
193902ac6454SAndrew Thompson 		/* let the hardware compute the data toggle */
194002ac6454SAndrew Thompson 
194102ac6454SAndrew Thompson 		qh->qh_endp &= htoehci32(temp.sc, ~EHCI_QH_DTC);
194202ac6454SAndrew Thompson 
194302ac6454SAndrew Thompson 		if (xfer->pipe->toggle_next) {
194402ac6454SAndrew Thompson 			/* DATA1 is next */
194502ac6454SAndrew Thompson 			qh->qh_qtd.qtd_status |=
194602ac6454SAndrew Thompson 			    htoehci32(temp.sc, EHCI_QTD_SET_TOGGLE(1));
194702ac6454SAndrew Thompson 		}
194802ac6454SAndrew Thompson 	}
194902ac6454SAndrew Thompson 	td = xfer->td_transfer_first;
195002ac6454SAndrew Thompson 
195102ac6454SAndrew Thompson 	qh->qh_qtd.qtd_next = td->qtd_self;
195202ac6454SAndrew Thompson 	qh->qh_qtd.qtd_altnext =
195302ac6454SAndrew Thompson 	    htoehci32(temp.sc, EHCI_LINK_TERMINATE);
195402ac6454SAndrew Thompson 
195502ac6454SAndrew Thompson 	usb2_pc_cpu_flush(qh->page_cache);
195602ac6454SAndrew Thompson 
195702ac6454SAndrew Thompson 	if (xfer->xroot->udev->pwr_save.suspended == 0) {
195802ac6454SAndrew Thompson 		EHCI_APPEND_QH(qh, *qh_last);
195902ac6454SAndrew Thompson 	}
196002ac6454SAndrew Thompson }
196102ac6454SAndrew Thompson 
196202ac6454SAndrew Thompson static void
196302ac6454SAndrew Thompson ehci_root_intr_done(struct usb2_xfer *xfer,
196402ac6454SAndrew Thompson     struct usb2_sw_transfer *std)
196502ac6454SAndrew Thompson {
196602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
196702ac6454SAndrew Thompson 	uint16_t i;
196802ac6454SAndrew Thompson 	uint16_t m;
196902ac6454SAndrew Thompson 
197002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
197102ac6454SAndrew Thompson 
197202ac6454SAndrew Thompson 	if (std->state != USB_SW_TR_PRE_DATA) {
197302ac6454SAndrew Thompson 		if (std->state == USB_SW_TR_PRE_CALLBACK) {
197402ac6454SAndrew Thompson 			/* transfer transferred */
197502ac6454SAndrew Thompson 			ehci_device_done(xfer, std->err);
197602ac6454SAndrew Thompson 		}
197702ac6454SAndrew Thompson 		goto done;
197802ac6454SAndrew Thompson 	}
197902ac6454SAndrew Thompson 	/* setup buffer */
198002ac6454SAndrew Thompson 	std->ptr = sc->sc_hub_idata;
198102ac6454SAndrew Thompson 	std->len = sizeof(sc->sc_hub_idata);
198202ac6454SAndrew Thompson 
198302ac6454SAndrew Thompson 	/* clear any old interrupt data */
198402ac6454SAndrew Thompson 	bzero(sc->sc_hub_idata, sizeof(sc->sc_hub_idata));
198502ac6454SAndrew Thompson 
198602ac6454SAndrew Thompson 	/* set bits */
198702ac6454SAndrew Thompson 	m = (sc->sc_noport + 1);
198802ac6454SAndrew Thompson 	if (m > (8 * sizeof(sc->sc_hub_idata))) {
198902ac6454SAndrew Thompson 		m = (8 * sizeof(sc->sc_hub_idata));
199002ac6454SAndrew Thompson 	}
199102ac6454SAndrew Thompson 	for (i = 1; i < m; i++) {
199202ac6454SAndrew Thompson 		/* pick out CHANGE bits from the status register */
199302ac6454SAndrew Thompson 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
199402ac6454SAndrew Thompson 			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
199502ac6454SAndrew Thompson 			DPRINTF("port %d changed\n", i);
199602ac6454SAndrew Thompson 		}
199702ac6454SAndrew Thompson 	}
199802ac6454SAndrew Thompson done:
199902ac6454SAndrew Thompson 	return;
200002ac6454SAndrew Thompson }
200102ac6454SAndrew Thompson 
200202ac6454SAndrew Thompson static void
200302ac6454SAndrew Thompson ehci_isoc_fs_done(ehci_softc_t *sc, struct usb2_xfer *xfer)
200402ac6454SAndrew Thompson {
200502ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
200602ac6454SAndrew Thompson 	uint32_t status;
200702ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
200802ac6454SAndrew Thompson 	uint16_t len = 0;
200902ac6454SAndrew Thompson 	ehci_sitd_t *td = xfer->td_transfer_first;
201002ac6454SAndrew Thompson 	ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];
201102ac6454SAndrew Thompson 
201202ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
201302ac6454SAndrew Thompson 	    xfer, xfer->pipe);
201402ac6454SAndrew Thompson 
201502ac6454SAndrew Thompson 	while (nframes--) {
201602ac6454SAndrew Thompson 		if (td == NULL) {
201702ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
201802ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
201902ac6454SAndrew Thompson 		}
202002ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
202102ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
202202ac6454SAndrew Thompson 		}
202302ac6454SAndrew Thompson #if USB_DEBUG
202402ac6454SAndrew Thompson 		if (ehcidebug > 15) {
202502ac6454SAndrew Thompson 			DPRINTF("isoc FS-TD\n");
202602ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
202702ac6454SAndrew Thompson 		}
202802ac6454SAndrew Thompson #endif
202902ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
203002ac6454SAndrew Thompson 		status = ehci32toh(sc, td->sitd_status);
203102ac6454SAndrew Thompson 
203202ac6454SAndrew Thompson 		len = EHCI_SITD_GET_LEN(status);
203302ac6454SAndrew Thompson 
203402ac6454SAndrew Thompson 		if (*plen >= len) {
203502ac6454SAndrew Thompson 			len = *plen - len;
203602ac6454SAndrew Thompson 		} else {
203702ac6454SAndrew Thompson 			len = 0;
203802ac6454SAndrew Thompson 		}
203902ac6454SAndrew Thompson 
204002ac6454SAndrew Thompson 		*plen = len;
204102ac6454SAndrew Thompson 
204202ac6454SAndrew Thompson 		/* remove FS-TD from schedule */
204302ac6454SAndrew Thompson 		EHCI_REMOVE_FS_TD(td, *pp_last);
204402ac6454SAndrew Thompson 
204502ac6454SAndrew Thompson 		pp_last++;
204602ac6454SAndrew Thompson 		plen++;
204702ac6454SAndrew Thompson 		td = td->obj_next;
204802ac6454SAndrew Thompson 	}
204902ac6454SAndrew Thompson 
205002ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
205102ac6454SAndrew Thompson }
205202ac6454SAndrew Thompson 
205302ac6454SAndrew Thompson static void
205402ac6454SAndrew Thompson ehci_isoc_hs_done(ehci_softc_t *sc, struct usb2_xfer *xfer)
205502ac6454SAndrew Thompson {
205602ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
205702ac6454SAndrew Thompson 	uint32_t status;
205802ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
205902ac6454SAndrew Thompson 	uint16_t len = 0;
206002ac6454SAndrew Thompson 	uint8_t td_no = 0;
206102ac6454SAndrew Thompson 	ehci_itd_t *td = xfer->td_transfer_first;
206202ac6454SAndrew Thompson 	ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];
206302ac6454SAndrew Thompson 
206402ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
206502ac6454SAndrew Thompson 	    xfer, xfer->pipe);
206602ac6454SAndrew Thompson 
206702ac6454SAndrew Thompson 	while (nframes--) {
206802ac6454SAndrew Thompson 		if (td == NULL) {
206902ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
207002ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
207102ac6454SAndrew Thompson 		}
207202ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
207302ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
207402ac6454SAndrew Thompson 		}
207502ac6454SAndrew Thompson #if USB_DEBUG
207602ac6454SAndrew Thompson 		if (ehcidebug > 15) {
207702ac6454SAndrew Thompson 			DPRINTF("isoc HS-TD\n");
207802ac6454SAndrew Thompson 			ehci_dump_itd(sc, td);
207902ac6454SAndrew Thompson 		}
208002ac6454SAndrew Thompson #endif
208102ac6454SAndrew Thompson 
208202ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
208302ac6454SAndrew Thompson 		status = ehci32toh(sc, td->itd_status[td_no]);
208402ac6454SAndrew Thompson 
208502ac6454SAndrew Thompson 		len = EHCI_ITD_GET_LEN(status);
208602ac6454SAndrew Thompson 
208702ac6454SAndrew Thompson 		if (*plen >= len) {
208802ac6454SAndrew Thompson 			/*
208902ac6454SAndrew Thompson 			 * The length is valid. NOTE: The complete
209002ac6454SAndrew Thompson 			 * length is written back into the status
209102ac6454SAndrew Thompson 			 * field, and not the remainder like with
209202ac6454SAndrew Thompson 			 * other transfer descriptor types.
209302ac6454SAndrew Thompson 			 */
209402ac6454SAndrew Thompson 		} else {
209502ac6454SAndrew Thompson 			/* Invalid length - truncate */
209602ac6454SAndrew Thompson 			len = 0;
209702ac6454SAndrew Thompson 		}
209802ac6454SAndrew Thompson 
209902ac6454SAndrew Thompson 		*plen = len;
210002ac6454SAndrew Thompson 
210102ac6454SAndrew Thompson 		plen++;
210202ac6454SAndrew Thompson 		td_no++;
210302ac6454SAndrew Thompson 
210402ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
210502ac6454SAndrew Thompson 			/* remove HS-TD from schedule */
210602ac6454SAndrew Thompson 			EHCI_REMOVE_HS_TD(td, *pp_last);
210702ac6454SAndrew Thompson 			pp_last++;
210802ac6454SAndrew Thompson 
210902ac6454SAndrew Thompson 			td_no = 0;
211002ac6454SAndrew Thompson 			td = td->obj_next;
211102ac6454SAndrew Thompson 		}
211202ac6454SAndrew Thompson 	}
211302ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
211402ac6454SAndrew Thompson }
211502ac6454SAndrew Thompson 
211602ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row,
211702ac6454SAndrew Thompson  * from close and from interrupt
211802ac6454SAndrew Thompson  */
211902ac6454SAndrew Thompson static void
212002ac6454SAndrew Thompson ehci_device_done(struct usb2_xfer *xfer, usb2_error_t error)
212102ac6454SAndrew Thompson {
212202ac6454SAndrew Thompson 	struct usb2_pipe_methods *methods = xfer->pipe->methods;
212302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
212402ac6454SAndrew Thompson 
212502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
212602ac6454SAndrew Thompson 
212702ac6454SAndrew Thompson 	DPRINTFN(2, "xfer=%p, pipe=%p, error=%d\n",
212802ac6454SAndrew Thompson 	    xfer, xfer->pipe, error);
212902ac6454SAndrew Thompson 
213002ac6454SAndrew Thompson 	if ((methods == &ehci_device_bulk_methods) ||
213102ac6454SAndrew Thompson 	    (methods == &ehci_device_ctrl_methods)) {
213202ac6454SAndrew Thompson #if USB_DEBUG
213302ac6454SAndrew Thompson 		if (ehcidebug > 8) {
213402ac6454SAndrew Thompson 			DPRINTF("nexttog=%d; data after transfer:\n",
213502ac6454SAndrew Thompson 			    xfer->pipe->toggle_next);
213602ac6454SAndrew Thompson 			ehci_dump_sqtds(sc,
213702ac6454SAndrew Thompson 			    xfer->td_transfer_first);
213802ac6454SAndrew Thompson 		}
213902ac6454SAndrew Thompson #endif
214002ac6454SAndrew Thompson 
214102ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
214202ac6454SAndrew Thompson 		    sc->sc_async_p_last);
214302ac6454SAndrew Thompson 	}
214402ac6454SAndrew Thompson 	if (methods == &ehci_device_intr_methods) {
214502ac6454SAndrew Thompson 		EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
214602ac6454SAndrew Thompson 		    sc->sc_intr_p_last[xfer->qh_pos]);
214702ac6454SAndrew Thompson 	}
214802ac6454SAndrew Thompson 	/*
214902ac6454SAndrew Thompson 	 * Only finish isochronous transfers once which will update
215002ac6454SAndrew Thompson 	 * "xfer->frlengths".
215102ac6454SAndrew Thompson 	 */
215202ac6454SAndrew Thompson 	if (xfer->td_transfer_first &&
215302ac6454SAndrew Thompson 	    xfer->td_transfer_last) {
215402ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_fs_methods) {
215502ac6454SAndrew Thompson 			ehci_isoc_fs_done(sc, xfer);
215602ac6454SAndrew Thompson 		}
215702ac6454SAndrew Thompson 		if (methods == &ehci_device_isoc_hs_methods) {
215802ac6454SAndrew Thompson 			ehci_isoc_hs_done(sc, xfer);
215902ac6454SAndrew Thompson 		}
216002ac6454SAndrew Thompson 		xfer->td_transfer_first = NULL;
216102ac6454SAndrew Thompson 		xfer->td_transfer_last = NULL;
216202ac6454SAndrew Thompson 	}
216302ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
216402ac6454SAndrew Thompson 	usb2_transfer_done(xfer, error);
216502ac6454SAndrew Thompson }
216602ac6454SAndrew Thompson 
216702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
216802ac6454SAndrew Thompson  * ehci bulk support
216902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
217002ac6454SAndrew Thompson static void
217102ac6454SAndrew Thompson ehci_device_bulk_open(struct usb2_xfer *xfer)
217202ac6454SAndrew Thompson {
217302ac6454SAndrew Thompson 	return;
217402ac6454SAndrew Thompson }
217502ac6454SAndrew Thompson 
217602ac6454SAndrew Thompson static void
217702ac6454SAndrew Thompson ehci_device_bulk_close(struct usb2_xfer *xfer)
217802ac6454SAndrew Thompson {
217902ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
218002ac6454SAndrew Thompson }
218102ac6454SAndrew Thompson 
218202ac6454SAndrew Thompson static void
218302ac6454SAndrew Thompson ehci_device_bulk_enter(struct usb2_xfer *xfer)
218402ac6454SAndrew Thompson {
218502ac6454SAndrew Thompson 	return;
218602ac6454SAndrew Thompson }
218702ac6454SAndrew Thompson 
218802ac6454SAndrew Thompson static void
218902ac6454SAndrew Thompson ehci_device_bulk_start(struct usb2_xfer *xfer)
219002ac6454SAndrew Thompson {
219102ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
219202ac6454SAndrew Thompson 
219302ac6454SAndrew Thompson 	/* setup TD's and QH */
219402ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
219502ac6454SAndrew Thompson 
219602ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
219702ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
219802ac6454SAndrew Thompson }
219902ac6454SAndrew Thompson 
220002ac6454SAndrew Thompson struct usb2_pipe_methods ehci_device_bulk_methods =
220102ac6454SAndrew Thompson {
220202ac6454SAndrew Thompson 	.open = ehci_device_bulk_open,
220302ac6454SAndrew Thompson 	.close = ehci_device_bulk_close,
220402ac6454SAndrew Thompson 	.enter = ehci_device_bulk_enter,
220502ac6454SAndrew Thompson 	.start = ehci_device_bulk_start,
220602ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
220702ac6454SAndrew Thompson 	.start_is_cancelable = 1,
220802ac6454SAndrew Thompson };
220902ac6454SAndrew Thompson 
221002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
221102ac6454SAndrew Thompson  * ehci control support
221202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
221302ac6454SAndrew Thompson static void
221402ac6454SAndrew Thompson ehci_device_ctrl_open(struct usb2_xfer *xfer)
221502ac6454SAndrew Thompson {
221602ac6454SAndrew Thompson 	return;
221702ac6454SAndrew Thompson }
221802ac6454SAndrew Thompson 
221902ac6454SAndrew Thompson static void
222002ac6454SAndrew Thompson ehci_device_ctrl_close(struct usb2_xfer *xfer)
222102ac6454SAndrew Thompson {
222202ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
222302ac6454SAndrew Thompson }
222402ac6454SAndrew Thompson 
222502ac6454SAndrew Thompson static void
222602ac6454SAndrew Thompson ehci_device_ctrl_enter(struct usb2_xfer *xfer)
222702ac6454SAndrew Thompson {
222802ac6454SAndrew Thompson 	return;
222902ac6454SAndrew Thompson }
223002ac6454SAndrew Thompson 
223102ac6454SAndrew Thompson static void
223202ac6454SAndrew Thompson ehci_device_ctrl_start(struct usb2_xfer *xfer)
223302ac6454SAndrew Thompson {
223402ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
223502ac6454SAndrew Thompson 
223602ac6454SAndrew Thompson 	/* setup TD's and QH */
223702ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);
223802ac6454SAndrew Thompson 
223902ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
224002ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
224102ac6454SAndrew Thompson }
224202ac6454SAndrew Thompson 
224302ac6454SAndrew Thompson struct usb2_pipe_methods ehci_device_ctrl_methods =
224402ac6454SAndrew Thompson {
224502ac6454SAndrew Thompson 	.open = ehci_device_ctrl_open,
224602ac6454SAndrew Thompson 	.close = ehci_device_ctrl_close,
224702ac6454SAndrew Thompson 	.enter = ehci_device_ctrl_enter,
224802ac6454SAndrew Thompson 	.start = ehci_device_ctrl_start,
224902ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
225002ac6454SAndrew Thompson 	.start_is_cancelable = 1,
225102ac6454SAndrew Thompson };
225202ac6454SAndrew Thompson 
225302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
225402ac6454SAndrew Thompson  * ehci interrupt support
225502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
225602ac6454SAndrew Thompson static void
225702ac6454SAndrew Thompson ehci_device_intr_open(struct usb2_xfer *xfer)
225802ac6454SAndrew Thompson {
225902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
226002ac6454SAndrew Thompson 	uint16_t best;
226102ac6454SAndrew Thompson 	uint16_t bit;
226202ac6454SAndrew Thompson 	uint16_t x;
226302ac6454SAndrew Thompson 	uint8_t slot;
226402ac6454SAndrew Thompson 
226502ac6454SAndrew Thompson 	/* Allocate a microframe slot first: */
226602ac6454SAndrew Thompson 
226702ac6454SAndrew Thompson 	slot = usb2_intr_schedule_adjust
226802ac6454SAndrew Thompson 	    (xfer->xroot->udev, xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX);
226902ac6454SAndrew Thompson 
227002ac6454SAndrew Thompson 	if (usb2_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
227102ac6454SAndrew Thompson 		xfer->usb2_uframe = slot;
227202ac6454SAndrew Thompson 		xfer->usb2_smask = (1 << slot) & 0xFF;
227302ac6454SAndrew Thompson 		xfer->usb2_cmask = 0;
227402ac6454SAndrew Thompson 	} else {
227502ac6454SAndrew Thompson 		xfer->usb2_uframe = slot;
227602ac6454SAndrew Thompson 		xfer->usb2_smask = (1 << slot) & 0x3F;
227702ac6454SAndrew Thompson 		xfer->usb2_cmask = (-(4 << slot)) & 0xFE;
227802ac6454SAndrew Thompson 	}
227902ac6454SAndrew Thompson 
228002ac6454SAndrew Thompson 	/*
228102ac6454SAndrew Thompson 	 * Find the best QH position corresponding to the given interval:
228202ac6454SAndrew Thompson 	 */
228302ac6454SAndrew Thompson 
228402ac6454SAndrew Thompson 	best = 0;
228502ac6454SAndrew Thompson 	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
228602ac6454SAndrew Thompson 	while (bit) {
228702ac6454SAndrew Thompson 		if (xfer->interval >= bit) {
228802ac6454SAndrew Thompson 			x = bit;
228902ac6454SAndrew Thompson 			best = bit;
229002ac6454SAndrew Thompson 			while (x & bit) {
229102ac6454SAndrew Thompson 				if (sc->sc_intr_stat[x] <
229202ac6454SAndrew Thompson 				    sc->sc_intr_stat[best]) {
229302ac6454SAndrew Thompson 					best = x;
229402ac6454SAndrew Thompson 				}
229502ac6454SAndrew Thompson 				x++;
229602ac6454SAndrew Thompson 			}
229702ac6454SAndrew Thompson 			break;
229802ac6454SAndrew Thompson 		}
229902ac6454SAndrew Thompson 		bit >>= 1;
230002ac6454SAndrew Thompson 	}
230102ac6454SAndrew Thompson 
230202ac6454SAndrew Thompson 	sc->sc_intr_stat[best]++;
230302ac6454SAndrew Thompson 	xfer->qh_pos = best;
230402ac6454SAndrew Thompson 
230502ac6454SAndrew Thompson 	DPRINTFN(3, "best=%d interval=%d\n",
230602ac6454SAndrew Thompson 	    best, xfer->interval);
230702ac6454SAndrew Thompson }
230802ac6454SAndrew Thompson 
230902ac6454SAndrew Thompson static void
231002ac6454SAndrew Thompson ehci_device_intr_close(struct usb2_xfer *xfer)
231102ac6454SAndrew Thompson {
231202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
231302ac6454SAndrew Thompson 	uint8_t slot;
231402ac6454SAndrew Thompson 
231502ac6454SAndrew Thompson 	slot = usb2_intr_schedule_adjust
231602ac6454SAndrew Thompson 	    (xfer->xroot->udev, -(xfer->max_frame_size), xfer->usb2_uframe);
231702ac6454SAndrew Thompson 
231802ac6454SAndrew Thompson 	sc->sc_intr_stat[xfer->qh_pos]--;
231902ac6454SAndrew Thompson 
232002ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
232102ac6454SAndrew Thompson }
232202ac6454SAndrew Thompson 
232302ac6454SAndrew Thompson static void
232402ac6454SAndrew Thompson ehci_device_intr_enter(struct usb2_xfer *xfer)
232502ac6454SAndrew Thompson {
232602ac6454SAndrew Thompson 	return;
232702ac6454SAndrew Thompson }
232802ac6454SAndrew Thompson 
232902ac6454SAndrew Thompson static void
233002ac6454SAndrew Thompson ehci_device_intr_start(struct usb2_xfer *xfer)
233102ac6454SAndrew Thompson {
233202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
233302ac6454SAndrew Thompson 
233402ac6454SAndrew Thompson 	/* setup TD's and QH */
233502ac6454SAndrew Thompson 	ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
233602ac6454SAndrew Thompson 
233702ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
233802ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
233902ac6454SAndrew Thompson }
234002ac6454SAndrew Thompson 
234102ac6454SAndrew Thompson struct usb2_pipe_methods ehci_device_intr_methods =
234202ac6454SAndrew Thompson {
234302ac6454SAndrew Thompson 	.open = ehci_device_intr_open,
234402ac6454SAndrew Thompson 	.close = ehci_device_intr_close,
234502ac6454SAndrew Thompson 	.enter = ehci_device_intr_enter,
234602ac6454SAndrew Thompson 	.start = ehci_device_intr_start,
234702ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
234802ac6454SAndrew Thompson 	.start_is_cancelable = 1,
234902ac6454SAndrew Thompson };
235002ac6454SAndrew Thompson 
235102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
235202ac6454SAndrew Thompson  * ehci full speed isochronous support
235302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
235402ac6454SAndrew Thompson static void
235502ac6454SAndrew Thompson ehci_device_isoc_fs_open(struct usb2_xfer *xfer)
235602ac6454SAndrew Thompson {
235702ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
235802ac6454SAndrew Thompson 	ehci_sitd_t *td;
235902ac6454SAndrew Thompson 	uint32_t sitd_portaddr;
236002ac6454SAndrew Thompson 	uint8_t ds;
236102ac6454SAndrew Thompson 
236202ac6454SAndrew Thompson 	sitd_portaddr =
236302ac6454SAndrew Thompson 	    EHCI_SITD_SET_ADDR(xfer->address) |
236402ac6454SAndrew Thompson 	    EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpoint)) |
236502ac6454SAndrew Thompson 	    EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
236602ac6454SAndrew Thompson 	    EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);
236702ac6454SAndrew Thompson 
236802ac6454SAndrew Thompson 	if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) {
236902ac6454SAndrew Thompson 		sitd_portaddr |= EHCI_SITD_SET_DIR_IN;
237002ac6454SAndrew Thompson 	}
237102ac6454SAndrew Thompson 	sitd_portaddr = htoehci32(sc, sitd_portaddr);
237202ac6454SAndrew Thompson 
237302ac6454SAndrew Thompson 	/* initialize all TD's */
237402ac6454SAndrew Thompson 
237502ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
237602ac6454SAndrew Thompson 
237702ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
237802ac6454SAndrew Thompson 
237902ac6454SAndrew Thompson 			td->sitd_portaddr = sitd_portaddr;
238002ac6454SAndrew Thompson 
238102ac6454SAndrew Thompson 			/*
238202ac6454SAndrew Thompson 			 * TODO: make some kind of automatic
238302ac6454SAndrew Thompson 			 * SMASK/CMASK selection based on micro-frame
238402ac6454SAndrew Thompson 			 * usage
238502ac6454SAndrew Thompson 			 *
238602ac6454SAndrew Thompson 			 * micro-frame usage (8 microframes per 1ms)
238702ac6454SAndrew Thompson 			 */
238802ac6454SAndrew Thompson 			td->sitd_back = htoehci32(sc, EHCI_LINK_TERMINATE);
238902ac6454SAndrew Thompson 
239002ac6454SAndrew Thompson 			usb2_pc_cpu_flush(td->page_cache);
239102ac6454SAndrew Thompson 		}
239202ac6454SAndrew Thompson 	}
239302ac6454SAndrew Thompson }
239402ac6454SAndrew Thompson 
239502ac6454SAndrew Thompson static void
239602ac6454SAndrew Thompson ehci_device_isoc_fs_close(struct usb2_xfer *xfer)
239702ac6454SAndrew Thompson {
239802ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
239902ac6454SAndrew Thompson }
240002ac6454SAndrew Thompson 
240102ac6454SAndrew Thompson static void
240202ac6454SAndrew Thompson ehci_device_isoc_fs_enter(struct usb2_xfer *xfer)
240302ac6454SAndrew Thompson {
240402ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
240502ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
240602ac6454SAndrew Thompson 	struct usb2_fs_isoc_schedule *fss_start;
240702ac6454SAndrew Thompson 	struct usb2_fs_isoc_schedule *fss_end;
240802ac6454SAndrew Thompson 	struct usb2_fs_isoc_schedule *fss;
240902ac6454SAndrew Thompson 	ehci_sitd_t *td;
241002ac6454SAndrew Thompson 	ehci_sitd_t *td_last = NULL;
241102ac6454SAndrew Thompson 	ehci_sitd_t **pp_last;
241202ac6454SAndrew Thompson 	uint32_t *plen;
241302ac6454SAndrew Thompson 	uint32_t buf_offset;
241402ac6454SAndrew Thompson 	uint32_t nframes;
241502ac6454SAndrew Thompson 	uint32_t temp;
241602ac6454SAndrew Thompson 	uint32_t sitd_mask;
241702ac6454SAndrew Thompson 	uint16_t tlen;
241802ac6454SAndrew Thompson 	uint8_t sa;
241902ac6454SAndrew Thompson 	uint8_t sb;
242002ac6454SAndrew Thompson 	uint8_t error;
242102ac6454SAndrew Thompson 
242202ac6454SAndrew Thompson #if USB_DEBUG
242302ac6454SAndrew Thompson 	uint8_t once = 1;
242402ac6454SAndrew Thompson 
242502ac6454SAndrew Thompson #endif
242602ac6454SAndrew Thompson 
242702ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
242802ac6454SAndrew Thompson 	    xfer, xfer->pipe->isoc_next, xfer->nframes);
242902ac6454SAndrew Thompson 
243002ac6454SAndrew Thompson 	/* get the current frame index */
243102ac6454SAndrew Thompson 
243202ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
243302ac6454SAndrew Thompson 
243402ac6454SAndrew Thompson 	/*
243502ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
243602ac6454SAndrew Thompson 	 * will be inserted
243702ac6454SAndrew Thompson 	 */
243802ac6454SAndrew Thompson 	buf_offset = (nframes - xfer->pipe->isoc_next) &
243902ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
244002ac6454SAndrew Thompson 
244102ac6454SAndrew Thompson 	if ((xfer->pipe->is_synced == 0) ||
244202ac6454SAndrew Thompson 	    (buf_offset < xfer->nframes)) {
244302ac6454SAndrew Thompson 		/*
244402ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
244502ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
244602ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
244702ac6454SAndrew Thompson 		 * overlap.
244802ac6454SAndrew Thompson 		 */
244902ac6454SAndrew Thompson 		xfer->pipe->isoc_next = (nframes + 3) &
245002ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
245102ac6454SAndrew Thompson 		xfer->pipe->is_synced = 1;
245202ac6454SAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
245302ac6454SAndrew Thompson 	}
245402ac6454SAndrew Thompson 	/*
245502ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
245602ac6454SAndrew Thompson 	 * current frame position:
245702ac6454SAndrew Thompson 	 */
245802ac6454SAndrew Thompson 	buf_offset = (xfer->pipe->isoc_next - nframes) &
245902ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
246002ac6454SAndrew Thompson 
246102ac6454SAndrew Thompson 	/*
246202ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
246302ac6454SAndrew Thompson 	 */
246402ac6454SAndrew Thompson 	xfer->isoc_time_complete =
246502ac6454SAndrew Thompson 	    usb2_fs_isoc_schedule_isoc_time_expand
246602ac6454SAndrew Thompson 	    (xfer->xroot->udev, &fss_start, &fss_end, nframes) + buf_offset +
246702ac6454SAndrew Thompson 	    xfer->nframes;
246802ac6454SAndrew Thompson 
246902ac6454SAndrew Thompson 	/* get the real number of frames */
247002ac6454SAndrew Thompson 
247102ac6454SAndrew Thompson 	nframes = xfer->nframes;
247202ac6454SAndrew Thompson 
247302ac6454SAndrew Thompson 	buf_offset = 0;
247402ac6454SAndrew Thompson 
247502ac6454SAndrew Thompson 	plen = xfer->frlengths;
247602ac6454SAndrew Thompson 
247702ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
247802ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
247902ac6454SAndrew Thompson 
248002ac6454SAndrew Thompson 	/* get next DMA set */
248102ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
248202ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
248302ac6454SAndrew Thompson 
248402ac6454SAndrew Thompson 	pp_last = &sc->sc_isoc_fs_p_last[xfer->pipe->isoc_next];
248502ac6454SAndrew Thompson 
248602ac6454SAndrew Thompson 	/* store starting position */
248702ac6454SAndrew Thompson 
248802ac6454SAndrew Thompson 	xfer->qh_pos = xfer->pipe->isoc_next;
248902ac6454SAndrew Thompson 
249002ac6454SAndrew Thompson 	fss = fss_start + (xfer->qh_pos % USB_ISOC_TIME_MAX);
249102ac6454SAndrew Thompson 
249202ac6454SAndrew Thompson 	while (nframes--) {
249302ac6454SAndrew Thompson 		if (td == NULL) {
249402ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
249502ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
249602ac6454SAndrew Thompson 		}
249702ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
249802ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_fs_p_last[0];
249902ac6454SAndrew Thompson 		}
250002ac6454SAndrew Thompson 		if (fss >= fss_end) {
250102ac6454SAndrew Thompson 			fss = fss_start;
250202ac6454SAndrew Thompson 		}
250302ac6454SAndrew Thompson 		/* reuse sitd_portaddr and sitd_back from last transfer */
250402ac6454SAndrew Thompson 
250502ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
250602ac6454SAndrew Thompson #if USB_DEBUG
250702ac6454SAndrew Thompson 			if (once) {
250802ac6454SAndrew Thompson 				once = 0;
250902ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d "
251002ac6454SAndrew Thompson 				    "bytes (frame truncated)\n",
251102ac6454SAndrew Thompson 				    __FUNCTION__, *plen,
251202ac6454SAndrew Thompson 				    xfer->max_frame_size);
251302ac6454SAndrew Thompson 			}
251402ac6454SAndrew Thompson #endif
251502ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
251602ac6454SAndrew Thompson 		}
251702ac6454SAndrew Thompson 		/*
251802ac6454SAndrew Thompson 		 * We currently don't care if the ISOCHRONOUS schedule is
251902ac6454SAndrew Thompson 		 * full!
252002ac6454SAndrew Thompson 		 */
252102ac6454SAndrew Thompson 		error = usb2_fs_isoc_schedule_alloc(fss, &sa, *plen);
252202ac6454SAndrew Thompson 		if (error) {
252302ac6454SAndrew Thompson 			/*
252402ac6454SAndrew Thompson 			 * The FULL speed schedule is FULL! Set length
252502ac6454SAndrew Thompson 			 * to zero.
252602ac6454SAndrew Thompson 			 */
252702ac6454SAndrew Thompson 			*plen = 0;
252802ac6454SAndrew Thompson 		}
252902ac6454SAndrew Thompson 		if (*plen) {
253002ac6454SAndrew Thompson 			/*
253102ac6454SAndrew Thompson 			 * only call "usb2_get_page()" when we have a
253202ac6454SAndrew Thompson 			 * non-zero length
253302ac6454SAndrew Thompson 			 */
253402ac6454SAndrew Thompson 			usb2_get_page(xfer->frbuffers, buf_offset, &buf_res);
253502ac6454SAndrew Thompson 			td->sitd_bp[0] = htoehci32(sc, buf_res.physaddr);
253602ac6454SAndrew Thompson 			buf_offset += *plen;
253702ac6454SAndrew Thompson 			/*
253802ac6454SAndrew Thompson 			 * NOTE: We need to subtract one from the offset so
253902ac6454SAndrew Thompson 			 * that we are on a valid page!
254002ac6454SAndrew Thompson 			 */
254102ac6454SAndrew Thompson 			usb2_get_page(xfer->frbuffers, buf_offset - 1,
254202ac6454SAndrew Thompson 			    &buf_res);
254302ac6454SAndrew Thompson 			temp = buf_res.physaddr & ~0xFFF;
254402ac6454SAndrew Thompson 		} else {
254502ac6454SAndrew Thompson 			td->sitd_bp[0] = 0;
254602ac6454SAndrew Thompson 			temp = 0;
254702ac6454SAndrew Thompson 		}
254802ac6454SAndrew Thompson 
254902ac6454SAndrew Thompson 		if (UE_GET_DIR(xfer->endpoint) == UE_DIR_OUT) {
255002ac6454SAndrew Thompson 			tlen = *plen;
255102ac6454SAndrew Thompson 			if (tlen <= 188) {
255202ac6454SAndrew Thompson 				temp |= 1;	/* T-count = 1, TP = ALL */
255302ac6454SAndrew Thompson 				tlen = 1;
255402ac6454SAndrew Thompson 			} else {
255502ac6454SAndrew Thompson 				tlen += 187;
255602ac6454SAndrew Thompson 				tlen /= 188;
255702ac6454SAndrew Thompson 				temp |= tlen;	/* T-count = [1..6] */
255802ac6454SAndrew Thompson 				temp |= 8;	/* TP = Begin */
255902ac6454SAndrew Thompson 			}
256002ac6454SAndrew Thompson 
256102ac6454SAndrew Thompson 			tlen += sa;
256202ac6454SAndrew Thompson 
256302ac6454SAndrew Thompson 			if (tlen >= 8) {
256402ac6454SAndrew Thompson 				sb = 0;
256502ac6454SAndrew Thompson 			} else {
256602ac6454SAndrew Thompson 				sb = (1 << tlen);
256702ac6454SAndrew Thompson 			}
256802ac6454SAndrew Thompson 
256902ac6454SAndrew Thompson 			sa = (1 << sa);
257002ac6454SAndrew Thompson 			sa = (sb - sa) & 0x3F;
257102ac6454SAndrew Thompson 			sb = 0;
257202ac6454SAndrew Thompson 		} else {
257302ac6454SAndrew Thompson 			sb = (-(4 << sa)) & 0xFE;
257402ac6454SAndrew Thompson 			sa = (1 << sa) & 0x3F;
257502ac6454SAndrew Thompson 		}
257602ac6454SAndrew Thompson 
257702ac6454SAndrew Thompson 		sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
257802ac6454SAndrew Thompson 		    EHCI_SITD_SET_CMASK(sb));
257902ac6454SAndrew Thompson 
258002ac6454SAndrew Thompson 		td->sitd_bp[1] = htoehci32(sc, temp);
258102ac6454SAndrew Thompson 
258202ac6454SAndrew Thompson 		td->sitd_mask = htoehci32(sc, sitd_mask);
258302ac6454SAndrew Thompson 
258402ac6454SAndrew Thompson 		if (nframes == 0) {
258502ac6454SAndrew Thompson 			td->sitd_status = htole32
258602ac6454SAndrew Thompson 			    (EHCI_SITD_IOC |
258702ac6454SAndrew Thompson 			    EHCI_SITD_ACTIVE |
258802ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
258902ac6454SAndrew Thompson 		} else {
259002ac6454SAndrew Thompson 			td->sitd_status = htole32
259102ac6454SAndrew Thompson 			    (EHCI_SITD_ACTIVE |
259202ac6454SAndrew Thompson 			    EHCI_SITD_SET_LEN(*plen));
259302ac6454SAndrew Thompson 		}
259402ac6454SAndrew Thompson 		usb2_pc_cpu_flush(td->page_cache);
259502ac6454SAndrew Thompson 
259602ac6454SAndrew Thompson #if USB_DEBUG
259702ac6454SAndrew Thompson 		if (ehcidebug > 15) {
259802ac6454SAndrew Thompson 			DPRINTF("FS-TD %d\n", nframes);
259902ac6454SAndrew Thompson 			ehci_dump_sitd(sc, td);
260002ac6454SAndrew Thompson 		}
260102ac6454SAndrew Thompson #endif
260202ac6454SAndrew Thompson 		/* insert TD into schedule */
260302ac6454SAndrew Thompson 		EHCI_APPEND_FS_TD(td, *pp_last);
260402ac6454SAndrew Thompson 		pp_last++;
260502ac6454SAndrew Thompson 
260602ac6454SAndrew Thompson 		plen++;
260702ac6454SAndrew Thompson 		fss++;
260802ac6454SAndrew Thompson 		td_last = td;
260902ac6454SAndrew Thompson 		td = td->obj_next;
261002ac6454SAndrew Thompson 	}
261102ac6454SAndrew Thompson 
261202ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
261302ac6454SAndrew Thompson 
261402ac6454SAndrew Thompson 	/* update isoc_next */
261502ac6454SAndrew Thompson 	xfer->pipe->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
261602ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
261702ac6454SAndrew Thompson }
261802ac6454SAndrew Thompson 
261902ac6454SAndrew Thompson static void
262002ac6454SAndrew Thompson ehci_device_isoc_fs_start(struct usb2_xfer *xfer)
262102ac6454SAndrew Thompson {
262202ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
262302ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
262402ac6454SAndrew Thompson }
262502ac6454SAndrew Thompson 
262602ac6454SAndrew Thompson struct usb2_pipe_methods ehci_device_isoc_fs_methods =
262702ac6454SAndrew Thompson {
262802ac6454SAndrew Thompson 	.open = ehci_device_isoc_fs_open,
262902ac6454SAndrew Thompson 	.close = ehci_device_isoc_fs_close,
263002ac6454SAndrew Thompson 	.enter = ehci_device_isoc_fs_enter,
263102ac6454SAndrew Thompson 	.start = ehci_device_isoc_fs_start,
263202ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
263302ac6454SAndrew Thompson 	.start_is_cancelable = 1,
263402ac6454SAndrew Thompson };
263502ac6454SAndrew Thompson 
263602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
263702ac6454SAndrew Thompson  * ehci high speed isochronous support
263802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
263902ac6454SAndrew Thompson static void
264002ac6454SAndrew Thompson ehci_device_isoc_hs_open(struct usb2_xfer *xfer)
264102ac6454SAndrew Thompson {
264202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
264302ac6454SAndrew Thompson 	ehci_itd_t *td;
264402ac6454SAndrew Thompson 	uint32_t temp;
264502ac6454SAndrew Thompson 	uint8_t ds;
264602ac6454SAndrew Thompson 
264702ac6454SAndrew Thompson 	/* initialize all TD's */
264802ac6454SAndrew Thompson 
264902ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
265002ac6454SAndrew Thompson 
265102ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
265202ac6454SAndrew Thompson 
265302ac6454SAndrew Thompson 			/* set TD inactive */
265402ac6454SAndrew Thompson 			td->itd_status[0] = 0;
265502ac6454SAndrew Thompson 			td->itd_status[1] = 0;
265602ac6454SAndrew Thompson 			td->itd_status[2] = 0;
265702ac6454SAndrew Thompson 			td->itd_status[3] = 0;
265802ac6454SAndrew Thompson 			td->itd_status[4] = 0;
265902ac6454SAndrew Thompson 			td->itd_status[5] = 0;
266002ac6454SAndrew Thompson 			td->itd_status[6] = 0;
266102ac6454SAndrew Thompson 			td->itd_status[7] = 0;
266202ac6454SAndrew Thompson 
266302ac6454SAndrew Thompson 			/* set endpoint and address */
266402ac6454SAndrew Thompson 			td->itd_bp[0] = htole32
266502ac6454SAndrew Thompson 			    (EHCI_ITD_SET_ADDR(xfer->address) |
266602ac6454SAndrew Thompson 			    EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpoint)));
266702ac6454SAndrew Thompson 
266802ac6454SAndrew Thompson 			temp =
266902ac6454SAndrew Thompson 			    EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);
267002ac6454SAndrew Thompson 
267102ac6454SAndrew Thompson 			/* set direction */
267202ac6454SAndrew Thompson 			if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) {
267302ac6454SAndrew Thompson 				temp |= EHCI_ITD_SET_DIR_IN;
267402ac6454SAndrew Thompson 			}
267502ac6454SAndrew Thompson 			/* set maximum packet size */
267602ac6454SAndrew Thompson 			td->itd_bp[1] = htoehci32(sc, temp);
267702ac6454SAndrew Thompson 
267802ac6454SAndrew Thompson 			/* set transfer multiplier */
267902ac6454SAndrew Thompson 			td->itd_bp[2] = htoehci32(sc, xfer->max_packet_count & 3);
268002ac6454SAndrew Thompson 
268102ac6454SAndrew Thompson 			usb2_pc_cpu_flush(td->page_cache);
268202ac6454SAndrew Thompson 		}
268302ac6454SAndrew Thompson 	}
268402ac6454SAndrew Thompson }
268502ac6454SAndrew Thompson 
268602ac6454SAndrew Thompson static void
268702ac6454SAndrew Thompson ehci_device_isoc_hs_close(struct usb2_xfer *xfer)
268802ac6454SAndrew Thompson {
268902ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
269002ac6454SAndrew Thompson }
269102ac6454SAndrew Thompson 
269202ac6454SAndrew Thompson static void
269302ac6454SAndrew Thompson ehci_device_isoc_hs_enter(struct usb2_xfer *xfer)
269402ac6454SAndrew Thompson {
269502ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
269602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
269702ac6454SAndrew Thompson 	ehci_itd_t *td;
269802ac6454SAndrew Thompson 	ehci_itd_t *td_last = NULL;
269902ac6454SAndrew Thompson 	ehci_itd_t **pp_last;
270002ac6454SAndrew Thompson 	bus_size_t page_addr;
270102ac6454SAndrew Thompson 	uint32_t *plen;
270202ac6454SAndrew Thompson 	uint32_t status;
270302ac6454SAndrew Thompson 	uint32_t buf_offset;
270402ac6454SAndrew Thompson 	uint32_t nframes;
270502ac6454SAndrew Thompson 	uint32_t itd_offset[8 + 1];
270602ac6454SAndrew Thompson 	uint8_t x;
270702ac6454SAndrew Thompson 	uint8_t td_no;
270802ac6454SAndrew Thompson 	uint8_t page_no;
270902ac6454SAndrew Thompson 
271002ac6454SAndrew Thompson #if USB_DEBUG
271102ac6454SAndrew Thompson 	uint8_t once = 1;
271202ac6454SAndrew Thompson 
271302ac6454SAndrew Thompson #endif
271402ac6454SAndrew Thompson 
271502ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
271602ac6454SAndrew Thompson 	    xfer, xfer->pipe->isoc_next, xfer->nframes);
271702ac6454SAndrew Thompson 
271802ac6454SAndrew Thompson 	/* get the current frame index */
271902ac6454SAndrew Thompson 
272002ac6454SAndrew Thompson 	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;
272102ac6454SAndrew Thompson 
272202ac6454SAndrew Thompson 	/*
272302ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
272402ac6454SAndrew Thompson 	 * will be inserted
272502ac6454SAndrew Thompson 	 */
272602ac6454SAndrew Thompson 	buf_offset = (nframes - xfer->pipe->isoc_next) &
272702ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
272802ac6454SAndrew Thompson 
272902ac6454SAndrew Thompson 	if ((xfer->pipe->is_synced == 0) ||
273002ac6454SAndrew Thompson 	    (buf_offset < ((xfer->nframes + 7) / 8))) {
273102ac6454SAndrew Thompson 		/*
273202ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
273302ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
273402ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
273502ac6454SAndrew Thompson 		 * overlap.
273602ac6454SAndrew Thompson 		 */
273702ac6454SAndrew Thompson 		xfer->pipe->isoc_next = (nframes + 3) &
273802ac6454SAndrew Thompson 		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
273902ac6454SAndrew Thompson 		xfer->pipe->is_synced = 1;
274002ac6454SAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
274102ac6454SAndrew Thompson 	}
274202ac6454SAndrew Thompson 	/*
274302ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
274402ac6454SAndrew Thompson 	 * current frame position:
274502ac6454SAndrew Thompson 	 */
274602ac6454SAndrew Thompson 	buf_offset = (xfer->pipe->isoc_next - nframes) &
274702ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
274802ac6454SAndrew Thompson 
274902ac6454SAndrew Thompson 	/*
275002ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
275102ac6454SAndrew Thompson 	 */
275202ac6454SAndrew Thompson 	xfer->isoc_time_complete =
275302ac6454SAndrew Thompson 	    usb2_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
275402ac6454SAndrew Thompson 	    ((xfer->nframes + 7) / 8);
275502ac6454SAndrew Thompson 
275602ac6454SAndrew Thompson 	/* get the real number of frames */
275702ac6454SAndrew Thompson 
275802ac6454SAndrew Thompson 	nframes = xfer->nframes;
275902ac6454SAndrew Thompson 
276002ac6454SAndrew Thompson 	buf_offset = 0;
276102ac6454SAndrew Thompson 	td_no = 0;
276202ac6454SAndrew Thompson 
276302ac6454SAndrew Thompson 	plen = xfer->frlengths;
276402ac6454SAndrew Thompson 
276502ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
276602ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
276702ac6454SAndrew Thompson 
276802ac6454SAndrew Thompson 	/* get next DMA set */
276902ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
277002ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
277102ac6454SAndrew Thompson 
277202ac6454SAndrew Thompson 	pp_last = &sc->sc_isoc_hs_p_last[xfer->pipe->isoc_next];
277302ac6454SAndrew Thompson 
277402ac6454SAndrew Thompson 	/* store starting position */
277502ac6454SAndrew Thompson 
277602ac6454SAndrew Thompson 	xfer->qh_pos = xfer->pipe->isoc_next;
277702ac6454SAndrew Thompson 
277802ac6454SAndrew Thompson 	while (nframes--) {
277902ac6454SAndrew Thompson 		if (td == NULL) {
278002ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
278102ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
278202ac6454SAndrew Thompson 		}
278302ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
278402ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_hs_p_last[0];
278502ac6454SAndrew Thompson 		}
278602ac6454SAndrew Thompson 		/* range check */
278702ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
278802ac6454SAndrew Thompson #if USB_DEBUG
278902ac6454SAndrew Thompson 			if (once) {
279002ac6454SAndrew Thompson 				once = 0;
279102ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d bytes "
279202ac6454SAndrew Thompson 				    "(frame truncated)\n",
279302ac6454SAndrew Thompson 				    __FUNCTION__, *plen, xfer->max_frame_size);
279402ac6454SAndrew Thompson 			}
279502ac6454SAndrew Thompson #endif
279602ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
279702ac6454SAndrew Thompson 		}
279802ac6454SAndrew Thompson 		status = (EHCI_ITD_SET_LEN(*plen) |
279902ac6454SAndrew Thompson 		    EHCI_ITD_ACTIVE |
280002ac6454SAndrew Thompson 		    EHCI_ITD_SET_PG(0));
280102ac6454SAndrew Thompson 		td->itd_status[td_no] = htoehci32(sc, status);
280202ac6454SAndrew Thompson 		itd_offset[td_no] = buf_offset;
280302ac6454SAndrew Thompson 		buf_offset += *plen;
280402ac6454SAndrew Thompson 		plen++;
280502ac6454SAndrew Thompson 		td_no++;
280602ac6454SAndrew Thompson 
280702ac6454SAndrew Thompson 		if ((td_no == 8) || (nframes == 0)) {
280802ac6454SAndrew Thompson 
280902ac6454SAndrew Thompson 			/* the rest of the transfers are not active, if any */
281002ac6454SAndrew Thompson 			for (x = td_no; x != 8; x++) {
281102ac6454SAndrew Thompson 				td->itd_status[x] = 0;	/* not active */
281202ac6454SAndrew Thompson 			}
281302ac6454SAndrew Thompson 
281402ac6454SAndrew Thompson 			/* check if there is any data to be transferred */
281502ac6454SAndrew Thompson 			if (itd_offset[0] != buf_offset) {
281602ac6454SAndrew Thompson 				page_no = 0;
281702ac6454SAndrew Thompson 				itd_offset[td_no] = buf_offset;
281802ac6454SAndrew Thompson 
281902ac6454SAndrew Thompson 				/* get first page offset */
282002ac6454SAndrew Thompson 				usb2_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
282102ac6454SAndrew Thompson 				/* get page address */
282202ac6454SAndrew Thompson 				page_addr = buf_res.physaddr & ~0xFFF;
282302ac6454SAndrew Thompson 				/* update page address */
282402ac6454SAndrew Thompson 				td->itd_bp[0] &= htoehci32(sc, 0xFFF);
282502ac6454SAndrew Thompson 				td->itd_bp[0] |= htoehci32(sc, page_addr);
282602ac6454SAndrew Thompson 
282702ac6454SAndrew Thompson 				for (x = 0; x != td_no; x++) {
282802ac6454SAndrew Thompson 					/* set page number and page offset */
282902ac6454SAndrew Thompson 					status = (EHCI_ITD_SET_PG(page_no) |
283002ac6454SAndrew Thompson 					    (buf_res.physaddr & 0xFFF));
283102ac6454SAndrew Thompson 					td->itd_status[x] |= htoehci32(sc, status);
283202ac6454SAndrew Thompson 
283302ac6454SAndrew Thompson 					/* get next page offset */
283402ac6454SAndrew Thompson 					if (itd_offset[x + 1] == buf_offset) {
283502ac6454SAndrew Thompson 						/*
283602ac6454SAndrew Thompson 						 * We subtract one so that
283702ac6454SAndrew Thompson 						 * we don't go off the last
283802ac6454SAndrew Thompson 						 * page!
283902ac6454SAndrew Thompson 						 */
284002ac6454SAndrew Thompson 						usb2_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
284102ac6454SAndrew Thompson 					} else {
284202ac6454SAndrew Thompson 						usb2_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
284302ac6454SAndrew Thompson 					}
284402ac6454SAndrew Thompson 
284502ac6454SAndrew Thompson 					/* check if we need a new page */
284602ac6454SAndrew Thompson 					if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
284702ac6454SAndrew Thompson 						/* new page needed */
284802ac6454SAndrew Thompson 						page_addr = buf_res.physaddr & ~0xFFF;
284902ac6454SAndrew Thompson 						if (page_no == 6) {
285002ac6454SAndrew Thompson 							panic("%s: too many pages\n", __FUNCTION__);
285102ac6454SAndrew Thompson 						}
285202ac6454SAndrew Thompson 						page_no++;
285302ac6454SAndrew Thompson 						/* update page address */
285402ac6454SAndrew Thompson 						td->itd_bp[page_no] &= htoehci32(sc, 0xFFF);
285502ac6454SAndrew Thompson 						td->itd_bp[page_no] |= htoehci32(sc, page_addr);
285602ac6454SAndrew Thompson 					}
285702ac6454SAndrew Thompson 				}
285802ac6454SAndrew Thompson 			}
285902ac6454SAndrew Thompson 			/* set IOC bit if we are complete */
286002ac6454SAndrew Thompson 			if (nframes == 0) {
286102ac6454SAndrew Thompson 				td->itd_status[7] |= htoehci32(sc, EHCI_ITD_IOC);
286202ac6454SAndrew Thompson 			}
286302ac6454SAndrew Thompson 			usb2_pc_cpu_flush(td->page_cache);
286402ac6454SAndrew Thompson #if USB_DEBUG
286502ac6454SAndrew Thompson 			if (ehcidebug > 15) {
286602ac6454SAndrew Thompson 				DPRINTF("HS-TD %d\n", nframes);
286702ac6454SAndrew Thompson 				ehci_dump_itd(sc, td);
286802ac6454SAndrew Thompson 			}
286902ac6454SAndrew Thompson #endif
287002ac6454SAndrew Thompson 			/* insert TD into schedule */
287102ac6454SAndrew Thompson 			EHCI_APPEND_HS_TD(td, *pp_last);
287202ac6454SAndrew Thompson 			pp_last++;
287302ac6454SAndrew Thompson 
287402ac6454SAndrew Thompson 			td_no = 0;
287502ac6454SAndrew Thompson 			td_last = td;
287602ac6454SAndrew Thompson 			td = td->obj_next;
287702ac6454SAndrew Thompson 		}
287802ac6454SAndrew Thompson 	}
287902ac6454SAndrew Thompson 
288002ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
288102ac6454SAndrew Thompson 
288202ac6454SAndrew Thompson 	/* update isoc_next */
288302ac6454SAndrew Thompson 	xfer->pipe->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
288402ac6454SAndrew Thompson 	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
288502ac6454SAndrew Thompson }
288602ac6454SAndrew Thompson 
288702ac6454SAndrew Thompson static void
288802ac6454SAndrew Thompson ehci_device_isoc_hs_start(struct usb2_xfer *xfer)
288902ac6454SAndrew Thompson {
289002ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
289102ac6454SAndrew Thompson 	ehci_transfer_intr_enqueue(xfer);
289202ac6454SAndrew Thompson }
289302ac6454SAndrew Thompson 
289402ac6454SAndrew Thompson struct usb2_pipe_methods ehci_device_isoc_hs_methods =
289502ac6454SAndrew Thompson {
289602ac6454SAndrew Thompson 	.open = ehci_device_isoc_hs_open,
289702ac6454SAndrew Thompson 	.close = ehci_device_isoc_hs_close,
289802ac6454SAndrew Thompson 	.enter = ehci_device_isoc_hs_enter,
289902ac6454SAndrew Thompson 	.start = ehci_device_isoc_hs_start,
290002ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
290102ac6454SAndrew Thompson 	.start_is_cancelable = 1,
290202ac6454SAndrew Thompson };
290302ac6454SAndrew Thompson 
290402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
290502ac6454SAndrew Thompson  * ehci root control support
290602ac6454SAndrew Thompson  *------------------------------------------------------------------------*
290702ac6454SAndrew Thompson  * simulate a hardware hub by handling
290802ac6454SAndrew Thompson  * all the necessary requests
290902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
291002ac6454SAndrew Thompson 
291102ac6454SAndrew Thompson static void
291202ac6454SAndrew Thompson ehci_root_ctrl_open(struct usb2_xfer *xfer)
291302ac6454SAndrew Thompson {
291402ac6454SAndrew Thompson 	return;
291502ac6454SAndrew Thompson }
291602ac6454SAndrew Thompson 
291702ac6454SAndrew Thompson static void
291802ac6454SAndrew Thompson ehci_root_ctrl_close(struct usb2_xfer *xfer)
291902ac6454SAndrew Thompson {
292002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
292102ac6454SAndrew Thompson 
292202ac6454SAndrew Thompson 	if (sc->sc_root_ctrl.xfer == xfer) {
292302ac6454SAndrew Thompson 		sc->sc_root_ctrl.xfer = NULL;
292402ac6454SAndrew Thompson 	}
292502ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
292602ac6454SAndrew Thompson }
292702ac6454SAndrew Thompson 
292802ac6454SAndrew Thompson /* data structures and routines
292902ac6454SAndrew Thompson  * to emulate the root hub:
293002ac6454SAndrew Thompson  */
293102ac6454SAndrew Thompson 
293202ac6454SAndrew Thompson static const
293302ac6454SAndrew Thompson struct usb2_device_descriptor ehci_devd =
293402ac6454SAndrew Thompson {
293502ac6454SAndrew Thompson 	sizeof(struct usb2_device_descriptor),
293602ac6454SAndrew Thompson 	UDESC_DEVICE,			/* type */
293702ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
293802ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
293902ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
294002ac6454SAndrew Thompson 	UDPROTO_HSHUBSTT,		/* protocol */
294102ac6454SAndrew Thompson 	64,				/* max packet */
294202ac6454SAndrew Thompson 	{0}, {0}, {0x00, 0x01},		/* device id */
294302ac6454SAndrew Thompson 	1, 2, 0,			/* string indicies */
294402ac6454SAndrew Thompson 	1				/* # of configurations */
294502ac6454SAndrew Thompson };
294602ac6454SAndrew Thompson 
294702ac6454SAndrew Thompson static const
294802ac6454SAndrew Thompson struct usb2_device_qualifier ehci_odevd =
294902ac6454SAndrew Thompson {
295002ac6454SAndrew Thompson 	sizeof(struct usb2_device_qualifier),
295102ac6454SAndrew Thompson 	UDESC_DEVICE_QUALIFIER,		/* type */
295202ac6454SAndrew Thompson 	{0x00, 0x02},			/* USB version */
295302ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
295402ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
295502ac6454SAndrew Thompson 	UDPROTO_FSHUB,			/* protocol */
295602ac6454SAndrew Thompson 	0,				/* max packet */
295702ac6454SAndrew Thompson 	0,				/* # of configurations */
295802ac6454SAndrew Thompson 	0
295902ac6454SAndrew Thompson };
296002ac6454SAndrew Thompson 
296102ac6454SAndrew Thompson static const struct ehci_config_desc ehci_confd = {
296202ac6454SAndrew Thompson 	.confd = {
296302ac6454SAndrew Thompson 		.bLength = sizeof(struct usb2_config_descriptor),
296402ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
296502ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(ehci_confd),
296602ac6454SAndrew Thompson 		.bNumInterface = 1,
296702ac6454SAndrew Thompson 		.bConfigurationValue = 1,
296802ac6454SAndrew Thompson 		.iConfiguration = 0,
296902ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
297002ac6454SAndrew Thompson 		.bMaxPower = 0		/* max power */
297102ac6454SAndrew Thompson 	},
297202ac6454SAndrew Thompson 
297302ac6454SAndrew Thompson 	.ifcd = {
297402ac6454SAndrew Thompson 		.bLength = sizeof(struct usb2_interface_descriptor),
297502ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
297602ac6454SAndrew Thompson 		.bNumEndpoints = 1,
297702ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
297802ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
297902ac6454SAndrew Thompson 		.bInterfaceProtocol = UIPROTO_HSHUBSTT,
298002ac6454SAndrew Thompson 		0
298102ac6454SAndrew Thompson 	},
298202ac6454SAndrew Thompson 
298302ac6454SAndrew Thompson 	.endpd = {
298402ac6454SAndrew Thompson 		.bLength = sizeof(struct usb2_endpoint_descriptor),
298502ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
298602ac6454SAndrew Thompson 		.bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
298702ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
298802ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
298902ac6454SAndrew Thompson 		.bInterval = 255,
299002ac6454SAndrew Thompson 	},
299102ac6454SAndrew Thompson };
299202ac6454SAndrew Thompson 
299302ac6454SAndrew Thompson static const
299402ac6454SAndrew Thompson struct usb2_hub_descriptor ehci_hubd =
299502ac6454SAndrew Thompson {
299602ac6454SAndrew Thompson 	0,				/* dynamic length */
299702ac6454SAndrew Thompson 	UDESC_HUB,
299802ac6454SAndrew Thompson 	0,
299902ac6454SAndrew Thompson 	{0, 0},
300002ac6454SAndrew Thompson 	0,
300102ac6454SAndrew Thompson 	0,
300202ac6454SAndrew Thompson 	{0},
300302ac6454SAndrew Thompson };
300402ac6454SAndrew Thompson 
300502ac6454SAndrew Thompson static void
300602ac6454SAndrew Thompson ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed)
300702ac6454SAndrew Thompson {
300802ac6454SAndrew Thompson 	uint32_t port;
300902ac6454SAndrew Thompson 	uint32_t v;
301002ac6454SAndrew Thompson 
301102ac6454SAndrew Thompson 	DPRINTF("index=%d lowspeed=%d\n", index, lowspeed);
301202ac6454SAndrew Thompson 
301302ac6454SAndrew Thompson 	port = EHCI_PORTSC(index);
301402ac6454SAndrew Thompson 	v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
301502ac6454SAndrew Thompson 	EOWRITE4(sc, port, v | EHCI_PS_PO);
301602ac6454SAndrew Thompson }
301702ac6454SAndrew Thompson 
301802ac6454SAndrew Thompson static void
301902ac6454SAndrew Thompson ehci_root_ctrl_enter(struct usb2_xfer *xfer)
302002ac6454SAndrew Thompson {
302102ac6454SAndrew Thompson 	return;
302202ac6454SAndrew Thompson }
302302ac6454SAndrew Thompson 
302402ac6454SAndrew Thompson static void
302502ac6454SAndrew Thompson ehci_root_ctrl_start(struct usb2_xfer *xfer)
302602ac6454SAndrew Thompson {
302702ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
302802ac6454SAndrew Thompson 
302902ac6454SAndrew Thompson 	DPRINTF("\n");
303002ac6454SAndrew Thompson 
303102ac6454SAndrew Thompson 	sc->sc_root_ctrl.xfer = xfer;
303202ac6454SAndrew Thompson 
303302ac6454SAndrew Thompson 	usb2_bus_roothub_exec(xfer->xroot->bus);
303402ac6454SAndrew Thompson }
303502ac6454SAndrew Thompson 
303602ac6454SAndrew Thompson static void
303702ac6454SAndrew Thompson ehci_root_ctrl_task(struct usb2_bus *bus)
303802ac6454SAndrew Thompson {
303902ac6454SAndrew Thompson 	ehci_root_ctrl_poll(EHCI_BUS2SC(bus));
304002ac6454SAndrew Thompson }
304102ac6454SAndrew Thompson 
304202ac6454SAndrew Thompson static void
304302ac6454SAndrew Thompson ehci_root_ctrl_done(struct usb2_xfer *xfer,
304402ac6454SAndrew Thompson     struct usb2_sw_transfer *std)
304502ac6454SAndrew Thompson {
304602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
304702ac6454SAndrew Thompson 	char *ptr;
304802ac6454SAndrew Thompson 	uint32_t port;
304902ac6454SAndrew Thompson 	uint32_t v;
305002ac6454SAndrew Thompson 	uint16_t i;
305102ac6454SAndrew Thompson 	uint16_t value;
305202ac6454SAndrew Thompson 	uint16_t index;
305302ac6454SAndrew Thompson 	uint8_t l;
305402ac6454SAndrew Thompson 
305502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
305602ac6454SAndrew Thompson 
305702ac6454SAndrew Thompson 	if (std->state != USB_SW_TR_SETUP) {
305802ac6454SAndrew Thompson 		if (std->state == USB_SW_TR_PRE_CALLBACK) {
305902ac6454SAndrew Thompson 			/* transfer transferred */
306002ac6454SAndrew Thompson 			ehci_device_done(xfer, std->err);
306102ac6454SAndrew Thompson 		}
306202ac6454SAndrew Thompson 		goto done;
306302ac6454SAndrew Thompson 	}
306402ac6454SAndrew Thompson 	/* buffer reset */
306502ac6454SAndrew Thompson 	std->ptr = sc->sc_hub_desc.temp;
306602ac6454SAndrew Thompson 	std->len = 0;
306702ac6454SAndrew Thompson 
306802ac6454SAndrew Thompson 	value = UGETW(std->req.wValue);
306902ac6454SAndrew Thompson 	index = UGETW(std->req.wIndex);
307002ac6454SAndrew Thompson 
307102ac6454SAndrew Thompson 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
307202ac6454SAndrew Thompson 	    "wValue=0x%04x wIndex=0x%04x\n",
307302ac6454SAndrew Thompson 	    std->req.bmRequestType, std->req.bRequest,
307402ac6454SAndrew Thompson 	    UGETW(std->req.wLength), value, index);
307502ac6454SAndrew Thompson 
307602ac6454SAndrew Thompson #define	C(x,y) ((x) | ((y) << 8))
307702ac6454SAndrew Thompson 	switch (C(std->req.bRequest, std->req.bmRequestType)) {
307802ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
307902ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
308002ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
308102ac6454SAndrew Thompson 		/*
308202ac6454SAndrew Thompson 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
308302ac6454SAndrew Thompson 		 * for the integrated root hub.
308402ac6454SAndrew Thompson 		 */
308502ac6454SAndrew Thompson 		break;
308602ac6454SAndrew Thompson 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
308702ac6454SAndrew Thompson 		std->len = 1;
308802ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
308902ac6454SAndrew Thompson 		break;
309002ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
309102ac6454SAndrew Thompson 		switch (value >> 8) {
309202ac6454SAndrew Thompson 		case UDESC_DEVICE:
309302ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
309402ac6454SAndrew Thompson 				std->err = USB_ERR_IOERROR;
309502ac6454SAndrew Thompson 				goto done;
309602ac6454SAndrew Thompson 			}
309702ac6454SAndrew Thompson 			std->len = sizeof(ehci_devd);
309802ac6454SAndrew Thompson 			sc->sc_hub_desc.devd = ehci_devd;
309902ac6454SAndrew Thompson 			break;
310002ac6454SAndrew Thompson 			/*
310102ac6454SAndrew Thompson 			 * We can't really operate at another speed,
310202ac6454SAndrew Thompson 			 * but the specification says we need this
310302ac6454SAndrew Thompson 			 * descriptor:
310402ac6454SAndrew Thompson 			 */
310502ac6454SAndrew Thompson 		case UDESC_DEVICE_QUALIFIER:
310602ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
310702ac6454SAndrew Thompson 				std->err = USB_ERR_IOERROR;
310802ac6454SAndrew Thompson 				goto done;
310902ac6454SAndrew Thompson 			}
311002ac6454SAndrew Thompson 			std->len = sizeof(ehci_odevd);
311102ac6454SAndrew Thompson 			sc->sc_hub_desc.odevd = ehci_odevd;
311202ac6454SAndrew Thompson 			break;
311302ac6454SAndrew Thompson 
311402ac6454SAndrew Thompson 		case UDESC_CONFIG:
311502ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
311602ac6454SAndrew Thompson 				std->err = USB_ERR_IOERROR;
311702ac6454SAndrew Thompson 				goto done;
311802ac6454SAndrew Thompson 			}
311902ac6454SAndrew Thompson 			std->len = sizeof(ehci_confd);
312002ac6454SAndrew Thompson 			std->ptr = USB_ADD_BYTES(&ehci_confd, 0);
312102ac6454SAndrew Thompson 			break;
312202ac6454SAndrew Thompson 
312302ac6454SAndrew Thompson 		case UDESC_STRING:
312402ac6454SAndrew Thompson 			switch (value & 0xff) {
312502ac6454SAndrew Thompson 			case 0:	/* Language table */
312602ac6454SAndrew Thompson 				ptr = "\001";
312702ac6454SAndrew Thompson 				break;
312802ac6454SAndrew Thompson 
312902ac6454SAndrew Thompson 			case 1:	/* Vendor */
313002ac6454SAndrew Thompson 				ptr = sc->sc_vendor;
313102ac6454SAndrew Thompson 				break;
313202ac6454SAndrew Thompson 
313302ac6454SAndrew Thompson 			case 2:	/* Product */
313402ac6454SAndrew Thompson 				ptr = "EHCI root HUB";
313502ac6454SAndrew Thompson 				break;
313602ac6454SAndrew Thompson 
313702ac6454SAndrew Thompson 			default:
313802ac6454SAndrew Thompson 				ptr = "";
313902ac6454SAndrew Thompson 				break;
314002ac6454SAndrew Thompson 			}
314102ac6454SAndrew Thompson 
314202ac6454SAndrew Thompson 			std->len = usb2_make_str_desc
314302ac6454SAndrew Thompson 			    (sc->sc_hub_desc.temp,
314402ac6454SAndrew Thompson 			    sizeof(sc->sc_hub_desc.temp),
314502ac6454SAndrew Thompson 			    ptr);
314602ac6454SAndrew Thompson 			break;
314702ac6454SAndrew Thompson 		default:
314802ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
314902ac6454SAndrew Thompson 			goto done;
315002ac6454SAndrew Thompson 		}
315102ac6454SAndrew Thompson 		break;
315202ac6454SAndrew Thompson 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
315302ac6454SAndrew Thompson 		std->len = 1;
315402ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = 0;
315502ac6454SAndrew Thompson 		break;
315602ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_DEVICE):
315702ac6454SAndrew Thompson 		std->len = 2;
315802ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
315902ac6454SAndrew Thompson 		break;
316002ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
316102ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
316202ac6454SAndrew Thompson 		std->len = 2;
316302ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
316402ac6454SAndrew Thompson 		break;
316502ac6454SAndrew Thompson 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
316602ac6454SAndrew Thompson 		if (value >= USB_MAX_DEVICES) {
316702ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
316802ac6454SAndrew Thompson 			goto done;
316902ac6454SAndrew Thompson 		}
317002ac6454SAndrew Thompson 		sc->sc_addr = value;
317102ac6454SAndrew Thompson 		break;
317202ac6454SAndrew Thompson 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
317302ac6454SAndrew Thompson 		if ((value != 0) && (value != 1)) {
317402ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
317502ac6454SAndrew Thompson 			goto done;
317602ac6454SAndrew Thompson 		}
317702ac6454SAndrew Thompson 		sc->sc_conf = value;
317802ac6454SAndrew Thompson 		break;
317902ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
318002ac6454SAndrew Thompson 		break;
318102ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
318202ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
318302ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
318402ac6454SAndrew Thompson 		std->err = USB_ERR_IOERROR;
318502ac6454SAndrew Thompson 		goto done;
318602ac6454SAndrew Thompson 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
318702ac6454SAndrew Thompson 		break;
318802ac6454SAndrew Thompson 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
318902ac6454SAndrew Thompson 		break;
319002ac6454SAndrew Thompson 		/* Hub requests */
319102ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
319202ac6454SAndrew Thompson 		break;
319302ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
319402ac6454SAndrew Thompson 		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
319502ac6454SAndrew Thompson 
319602ac6454SAndrew Thompson 		if ((index < 1) ||
319702ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
319802ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
319902ac6454SAndrew Thompson 			goto done;
320002ac6454SAndrew Thompson 		}
320102ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
320202ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
320302ac6454SAndrew Thompson 		switch (value) {
320402ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
320502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PE);
320602ac6454SAndrew Thompson 			break;
320702ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
320802ac6454SAndrew Thompson 			if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {
320902ac6454SAndrew Thompson 
321002ac6454SAndrew Thompson 				/*
321102ac6454SAndrew Thompson 				 * waking up a High Speed device is rather
321202ac6454SAndrew Thompson 				 * complicated if
321302ac6454SAndrew Thompson 				 */
321402ac6454SAndrew Thompson 				EOWRITE4(sc, port, v | EHCI_PS_FPR);
321502ac6454SAndrew Thompson 			}
321602ac6454SAndrew Thompson 			/* wait 20ms for resume sequence to complete */
321702ac6454SAndrew Thompson 			usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
321802ac6454SAndrew Thompson 
321902ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
322002ac6454SAndrew Thompson 			    EHCI_PS_FPR | (3 << 10) /* High Speed */ ));
322102ac6454SAndrew Thompson 
32224a35cda7SAndrew Thompson 			/* 4ms settle time */
322302ac6454SAndrew Thompson 			usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
322402ac6454SAndrew Thompson 			break;
322502ac6454SAndrew Thompson 		case UHF_PORT_POWER:
322602ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PP);
322702ac6454SAndrew Thompson 			break;
322802ac6454SAndrew Thompson 		case UHF_PORT_TEST:
322902ac6454SAndrew Thompson 			DPRINTFN(3, "clear port test "
323002ac6454SAndrew Thompson 			    "%d\n", index);
323102ac6454SAndrew Thompson 			break;
323202ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
323302ac6454SAndrew Thompson 			DPRINTFN(3, "clear port ind "
323402ac6454SAndrew Thompson 			    "%d\n", index);
323502ac6454SAndrew Thompson 			EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
323602ac6454SAndrew Thompson 			break;
323702ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
323802ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
323902ac6454SAndrew Thompson 			break;
324002ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
324102ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
324202ac6454SAndrew Thompson 			break;
324302ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
324402ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
324502ac6454SAndrew Thompson 			break;
324602ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
324702ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
324802ac6454SAndrew Thompson 			break;
324902ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
325002ac6454SAndrew Thompson 			sc->sc_isreset = 0;
325102ac6454SAndrew Thompson 			break;
325202ac6454SAndrew Thompson 		default:
325302ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
325402ac6454SAndrew Thompson 			goto done;
325502ac6454SAndrew Thompson 		}
325602ac6454SAndrew Thompson 		break;
325702ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
325802ac6454SAndrew Thompson 		if ((value & 0xff) != 0) {
325902ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
326002ac6454SAndrew Thompson 			goto done;
326102ac6454SAndrew Thompson 		}
326202ac6454SAndrew Thompson 		v = EOREAD4(sc, EHCI_HCSPARAMS);
326302ac6454SAndrew Thompson 
326402ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd = ehci_hubd;
326502ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
326602ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics,
326702ac6454SAndrew Thompson 		    (EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH) |
326802ac6454SAndrew Thompson 		    (EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS)) ?
326902ac6454SAndrew Thompson 		    UHD_PORT_IND : 0));
327002ac6454SAndrew Thompson 		/* XXX can't find out? */
327102ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
327202ac6454SAndrew Thompson 		for (l = 0; l < sc->sc_noport; l++) {
327302ac6454SAndrew Thompson 			/* XXX can't find out? */
327402ac6454SAndrew Thompson 			sc->sc_hub_desc.hubd.DeviceRemovable[l / 8] &= ~(1 << (l % 8));
327502ac6454SAndrew Thompson 		}
327602ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bDescLength =
327702ac6454SAndrew Thompson 		    8 + ((sc->sc_noport + 7) / 8);
327802ac6454SAndrew Thompson 		std->len = sc->sc_hub_desc.hubd.bDescLength;
327902ac6454SAndrew Thompson 		break;
328002ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
328102ac6454SAndrew Thompson 		std->len = 16;
328202ac6454SAndrew Thompson 		bzero(sc->sc_hub_desc.temp, 16);
328302ac6454SAndrew Thompson 		break;
328402ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
328502ac6454SAndrew Thompson 		DPRINTFN(9, "get port status i=%d\n",
328602ac6454SAndrew Thompson 		    index);
328702ac6454SAndrew Thompson 		if ((index < 1) ||
328802ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
328902ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
329002ac6454SAndrew Thompson 			goto done;
329102ac6454SAndrew Thompson 		}
329202ac6454SAndrew Thompson 		v = EOREAD4(sc, EHCI_PORTSC(index));
329302ac6454SAndrew Thompson 		DPRINTFN(9, "port status=0x%04x\n", v);
329402ac6454SAndrew Thompson 		if (sc->sc_flags & EHCI_SCFLG_FORCESPEED) {
329502ac6454SAndrew Thompson 			if ((v & 0xc000000) == 0x8000000)
329602ac6454SAndrew Thompson 				i = UPS_HIGH_SPEED;
329702ac6454SAndrew Thompson 			else if ((v & 0xc000000) == 0x4000000)
329802ac6454SAndrew Thompson 				i = UPS_LOW_SPEED;
329902ac6454SAndrew Thompson 			else
330002ac6454SAndrew Thompson 				i = 0;
330102ac6454SAndrew Thompson 		} else {
330202ac6454SAndrew Thompson 			i = UPS_HIGH_SPEED;
330302ac6454SAndrew Thompson 		}
330402ac6454SAndrew Thompson 		if (v & EHCI_PS_CS)
330502ac6454SAndrew Thompson 			i |= UPS_CURRENT_CONNECT_STATUS;
330602ac6454SAndrew Thompson 		if (v & EHCI_PS_PE)
330702ac6454SAndrew Thompson 			i |= UPS_PORT_ENABLED;
330802ac6454SAndrew Thompson 		if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
330902ac6454SAndrew Thompson 			i |= UPS_SUSPEND;
331002ac6454SAndrew Thompson 		if (v & EHCI_PS_OCA)
331102ac6454SAndrew Thompson 			i |= UPS_OVERCURRENT_INDICATOR;
331202ac6454SAndrew Thompson 		if (v & EHCI_PS_PR)
331302ac6454SAndrew Thompson 			i |= UPS_RESET;
331402ac6454SAndrew Thompson 		if (v & EHCI_PS_PP)
331502ac6454SAndrew Thompson 			i |= UPS_PORT_POWER;
331602ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortStatus, i);
331702ac6454SAndrew Thompson 		i = 0;
331802ac6454SAndrew Thompson 		if (v & EHCI_PS_CSC)
331902ac6454SAndrew Thompson 			i |= UPS_C_CONNECT_STATUS;
332002ac6454SAndrew Thompson 		if (v & EHCI_PS_PEC)
332102ac6454SAndrew Thompson 			i |= UPS_C_PORT_ENABLED;
332202ac6454SAndrew Thompson 		if (v & EHCI_PS_OCC)
332302ac6454SAndrew Thompson 			i |= UPS_C_OVERCURRENT_INDICATOR;
332402ac6454SAndrew Thompson 		if (v & EHCI_PS_FPR)
332502ac6454SAndrew Thompson 			i |= UPS_C_SUSPEND;
332602ac6454SAndrew Thompson 		if (sc->sc_isreset)
332702ac6454SAndrew Thompson 			i |= UPS_C_PORT_RESET;
332802ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortChange, i);
332902ac6454SAndrew Thompson 		std->len = sizeof(sc->sc_hub_desc.ps);
333002ac6454SAndrew Thompson 		break;
333102ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
333202ac6454SAndrew Thompson 		std->err = USB_ERR_IOERROR;
333302ac6454SAndrew Thompson 		goto done;
333402ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
333502ac6454SAndrew Thompson 		break;
333602ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
333702ac6454SAndrew Thompson 		if ((index < 1) ||
333802ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
333902ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
334002ac6454SAndrew Thompson 			goto done;
334102ac6454SAndrew Thompson 		}
334202ac6454SAndrew Thompson 		port = EHCI_PORTSC(index);
334302ac6454SAndrew Thompson 		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
334402ac6454SAndrew Thompson 		switch (value) {
334502ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
334602ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PE);
334702ac6454SAndrew Thompson 			break;
334802ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
334902ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
335002ac6454SAndrew Thompson 			break;
335102ac6454SAndrew Thompson 		case UHF_PORT_RESET:
335202ac6454SAndrew Thompson 			DPRINTFN(6, "reset port %d\n", index);
335302ac6454SAndrew Thompson #if USB_DEBUG
335402ac6454SAndrew Thompson 			if (ehcinohighspeed) {
335502ac6454SAndrew Thompson 				/*
335602ac6454SAndrew Thompson 				 * Connect USB device to companion
335702ac6454SAndrew Thompson 				 * controller.
335802ac6454SAndrew Thompson 				 */
335902ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
336002ac6454SAndrew Thompson 				break;
336102ac6454SAndrew Thompson 			}
336202ac6454SAndrew Thompson #endif
336302ac6454SAndrew Thompson 			if (EHCI_PS_IS_LOWSPEED(v)) {
336402ac6454SAndrew Thompson 				/* Low speed device, give up ownership. */
336502ac6454SAndrew Thompson 				ehci_disown(sc, index, 1);
336602ac6454SAndrew Thompson 				break;
336702ac6454SAndrew Thompson 			}
336802ac6454SAndrew Thompson 			/* Start reset sequence. */
336902ac6454SAndrew Thompson 			v &= ~(EHCI_PS_PE | EHCI_PS_PR);
337002ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PR);
337102ac6454SAndrew Thompson 
337202ac6454SAndrew Thompson 			/* Wait for reset to complete. */
337302ac6454SAndrew Thompson 			usb2_pause_mtx(&sc->sc_bus.bus_mtx,
337402ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
337502ac6454SAndrew Thompson 
337602ac6454SAndrew Thompson 			/* Terminate reset sequence. */
337702ac6454SAndrew Thompson 			if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
337802ac6454SAndrew Thompson 				EOWRITE4(sc, port, v);
337902ac6454SAndrew Thompson 
338002ac6454SAndrew Thompson 			/* Wait for HC to complete reset. */
338102ac6454SAndrew Thompson 			usb2_pause_mtx(&sc->sc_bus.bus_mtx,
338202ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));
338302ac6454SAndrew Thompson 
338402ac6454SAndrew Thompson 			v = EOREAD4(sc, port);
338502ac6454SAndrew Thompson 			DPRINTF("ehci after reset, status=0x%08x\n", v);
338602ac6454SAndrew Thompson 			if (v & EHCI_PS_PR) {
338702ac6454SAndrew Thompson 				device_printf(sc->sc_bus.bdev,
338802ac6454SAndrew Thompson 				    "port reset timeout\n");
338902ac6454SAndrew Thompson 				std->err = USB_ERR_TIMEOUT;
339002ac6454SAndrew Thompson 				goto done;
339102ac6454SAndrew Thompson 			}
339202ac6454SAndrew Thompson 			if (!(v & EHCI_PS_PE)) {
339302ac6454SAndrew Thompson 				/*
339402ac6454SAndrew Thompson 				 * Not a high speed device, give up
339502ac6454SAndrew Thompson 				 * ownership.
339602ac6454SAndrew Thompson 				 */
339702ac6454SAndrew Thompson 				ehci_disown(sc, index, 0);
339802ac6454SAndrew Thompson 				break;
339902ac6454SAndrew Thompson 			}
340002ac6454SAndrew Thompson 			sc->sc_isreset = 1;
340102ac6454SAndrew Thompson 			DPRINTF("ehci port %d reset, status = 0x%08x\n",
340202ac6454SAndrew Thompson 			    index, v);
340302ac6454SAndrew Thompson 			break;
340402ac6454SAndrew Thompson 
340502ac6454SAndrew Thompson 		case UHF_PORT_POWER:
340602ac6454SAndrew Thompson 			DPRINTFN(3, "set port power %d\n", index);
340702ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PP);
340802ac6454SAndrew Thompson 			break;
340902ac6454SAndrew Thompson 
341002ac6454SAndrew Thompson 		case UHF_PORT_TEST:
341102ac6454SAndrew Thompson 			DPRINTFN(3, "set port test %d\n", index);
341202ac6454SAndrew Thompson 			break;
341302ac6454SAndrew Thompson 
341402ac6454SAndrew Thompson 		case UHF_PORT_INDICATOR:
341502ac6454SAndrew Thompson 			DPRINTFN(3, "set port ind %d\n", index);
341602ac6454SAndrew Thompson 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
341702ac6454SAndrew Thompson 			break;
341802ac6454SAndrew Thompson 
341902ac6454SAndrew Thompson 		default:
342002ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
342102ac6454SAndrew Thompson 			goto done;
342202ac6454SAndrew Thompson 		}
342302ac6454SAndrew Thompson 		break;
342402ac6454SAndrew Thompson 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
342502ac6454SAndrew Thompson 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
342602ac6454SAndrew Thompson 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
342702ac6454SAndrew Thompson 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
342802ac6454SAndrew Thompson 		break;
342902ac6454SAndrew Thompson 	default:
343002ac6454SAndrew Thompson 		std->err = USB_ERR_IOERROR;
343102ac6454SAndrew Thompson 		goto done;
343202ac6454SAndrew Thompson 	}
343302ac6454SAndrew Thompson done:
343402ac6454SAndrew Thompson 	return;
343502ac6454SAndrew Thompson }
343602ac6454SAndrew Thompson 
343702ac6454SAndrew Thompson static void
343802ac6454SAndrew Thompson ehci_root_ctrl_poll(ehci_softc_t *sc)
343902ac6454SAndrew Thompson {
344002ac6454SAndrew Thompson 	usb2_sw_transfer(&sc->sc_root_ctrl,
344102ac6454SAndrew Thompson 	    &ehci_root_ctrl_done);
344202ac6454SAndrew Thompson }
344302ac6454SAndrew Thompson 
344402ac6454SAndrew Thompson struct usb2_pipe_methods ehci_root_ctrl_methods =
344502ac6454SAndrew Thompson {
344602ac6454SAndrew Thompson 	.open = ehci_root_ctrl_open,
344702ac6454SAndrew Thompson 	.close = ehci_root_ctrl_close,
344802ac6454SAndrew Thompson 	.enter = ehci_root_ctrl_enter,
344902ac6454SAndrew Thompson 	.start = ehci_root_ctrl_start,
345002ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
345102ac6454SAndrew Thompson 	.start_is_cancelable = 0,
345202ac6454SAndrew Thompson };
345302ac6454SAndrew Thompson 
345402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
345502ac6454SAndrew Thompson  * ehci root interrupt support
345602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
345702ac6454SAndrew Thompson static void
345802ac6454SAndrew Thompson ehci_root_intr_open(struct usb2_xfer *xfer)
345902ac6454SAndrew Thompson {
346002ac6454SAndrew Thompson 	return;
346102ac6454SAndrew Thompson }
346202ac6454SAndrew Thompson 
346302ac6454SAndrew Thompson static void
346402ac6454SAndrew Thompson ehci_root_intr_close(struct usb2_xfer *xfer)
346502ac6454SAndrew Thompson {
346602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
346702ac6454SAndrew Thompson 
346802ac6454SAndrew Thompson 	if (sc->sc_root_intr.xfer == xfer) {
346902ac6454SAndrew Thompson 		sc->sc_root_intr.xfer = NULL;
347002ac6454SAndrew Thompson 	}
347102ac6454SAndrew Thompson 	ehci_device_done(xfer, USB_ERR_CANCELLED);
347202ac6454SAndrew Thompson }
347302ac6454SAndrew Thompson 
347402ac6454SAndrew Thompson static void
347502ac6454SAndrew Thompson ehci_root_intr_enter(struct usb2_xfer *xfer)
347602ac6454SAndrew Thompson {
347702ac6454SAndrew Thompson 	return;
347802ac6454SAndrew Thompson }
347902ac6454SAndrew Thompson 
348002ac6454SAndrew Thompson static void
348102ac6454SAndrew Thompson ehci_root_intr_start(struct usb2_xfer *xfer)
348202ac6454SAndrew Thompson {
348302ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
348402ac6454SAndrew Thompson 
348502ac6454SAndrew Thompson 	sc->sc_root_intr.xfer = xfer;
348602ac6454SAndrew Thompson }
348702ac6454SAndrew Thompson 
348802ac6454SAndrew Thompson struct usb2_pipe_methods ehci_root_intr_methods =
348902ac6454SAndrew Thompson {
349002ac6454SAndrew Thompson 	.open = ehci_root_intr_open,
349102ac6454SAndrew Thompson 	.close = ehci_root_intr_close,
349202ac6454SAndrew Thompson 	.enter = ehci_root_intr_enter,
349302ac6454SAndrew Thompson 	.start = ehci_root_intr_start,
349402ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
349502ac6454SAndrew Thompson 	.start_is_cancelable = 1,
349602ac6454SAndrew Thompson };
349702ac6454SAndrew Thompson 
349802ac6454SAndrew Thompson static void
349902ac6454SAndrew Thompson ehci_xfer_setup(struct usb2_setup_params *parm)
350002ac6454SAndrew Thompson {
350102ac6454SAndrew Thompson 	struct usb2_page_search page_info;
350202ac6454SAndrew Thompson 	struct usb2_page_cache *pc;
350302ac6454SAndrew Thompson 	ehci_softc_t *sc;
350402ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
350502ac6454SAndrew Thompson 	void *last_obj;
350602ac6454SAndrew Thompson 	uint32_t nqtd;
350702ac6454SAndrew Thompson 	uint32_t nqh;
350802ac6454SAndrew Thompson 	uint32_t nsitd;
350902ac6454SAndrew Thompson 	uint32_t nitd;
351002ac6454SAndrew Thompson 	uint32_t n;
351102ac6454SAndrew Thompson 
351202ac6454SAndrew Thompson 	sc = EHCI_BUS2SC(parm->udev->bus);
351302ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
351402ac6454SAndrew Thompson 
351502ac6454SAndrew Thompson 	nqtd = 0;
351602ac6454SAndrew Thompson 	nqh = 0;
351702ac6454SAndrew Thompson 	nsitd = 0;
351802ac6454SAndrew Thompson 	nitd = 0;
351902ac6454SAndrew Thompson 
352002ac6454SAndrew Thompson 	/*
352102ac6454SAndrew Thompson 	 * compute maximum number of some structures
352202ac6454SAndrew Thompson 	 */
352302ac6454SAndrew Thompson 	if (parm->methods == &ehci_device_ctrl_methods) {
352402ac6454SAndrew Thompson 
352502ac6454SAndrew Thompson 		/*
352602ac6454SAndrew Thompson 		 * The proof for the "nqtd" formula is illustrated like
352702ac6454SAndrew Thompson 		 * this:
352802ac6454SAndrew Thompson 		 *
352902ac6454SAndrew Thompson 		 * +------------------------------------+
353002ac6454SAndrew Thompson 		 * |                                    |
353102ac6454SAndrew Thompson 		 * |         |remainder ->              |
353202ac6454SAndrew Thompson 		 * |   +-----+---+                      |
353302ac6454SAndrew Thompson 		 * |   | xxx | x | frm 0                |
353402ac6454SAndrew Thompson 		 * |   +-----+---++                     |
353502ac6454SAndrew Thompson 		 * |   | xxx | xx | frm 1               |
353602ac6454SAndrew Thompson 		 * |   +-----+----+                     |
353702ac6454SAndrew Thompson 		 * |            ...                     |
353802ac6454SAndrew Thompson 		 * +------------------------------------+
353902ac6454SAndrew Thompson 		 *
354002ac6454SAndrew Thompson 		 * "xxx" means a completely full USB transfer descriptor
354102ac6454SAndrew Thompson 		 *
354202ac6454SAndrew Thompson 		 * "x" and "xx" means a short USB packet
354302ac6454SAndrew Thompson 		 *
354402ac6454SAndrew Thompson 		 * For the remainder of an USB transfer modulo
354502ac6454SAndrew Thompson 		 * "max_data_length" we need two USB transfer descriptors.
354602ac6454SAndrew Thompson 		 * One to transfer the remaining data and one to finalise
354702ac6454SAndrew Thompson 		 * with a zero length packet in case the "force_short_xfer"
354802ac6454SAndrew Thompson 		 * flag is set. We only need two USB transfer descriptors in
354902ac6454SAndrew Thompson 		 * the case where the transfer length of the first one is a
355002ac6454SAndrew Thompson 		 * factor of "max_frame_size". The rest of the needed USB
355102ac6454SAndrew Thompson 		 * transfer descriptors is given by the buffer size divided
355202ac6454SAndrew Thompson 		 * by the maximum data payload.
355302ac6454SAndrew Thompson 		 */
355402ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
355502ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
355602ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
355702ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
355802ac6454SAndrew Thompson 
355902ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
356002ac6454SAndrew Thompson 
356102ac6454SAndrew Thompson 		nqh = 1;
356202ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes) + 1	/* STATUS */
356302ac6454SAndrew Thompson 		    + (xfer->max_data_length / xfer->max_usb2_frame_size));
356402ac6454SAndrew Thompson 
356502ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_bulk_methods) {
356602ac6454SAndrew Thompson 
356702ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
356802ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
356902ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
357002ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
357102ac6454SAndrew Thompson 
357202ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
357302ac6454SAndrew Thompson 
357402ac6454SAndrew Thompson 		nqh = 1;
357502ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
357602ac6454SAndrew Thompson 		    + (xfer->max_data_length / xfer->max_usb2_frame_size));
357702ac6454SAndrew Thompson 
357802ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_intr_methods) {
357902ac6454SAndrew Thompson 
358002ac6454SAndrew Thompson 		if (parm->speed == USB_SPEED_HIGH) {
358102ac6454SAndrew Thompson 			parm->hc_max_packet_size = 0x400;
358202ac6454SAndrew Thompson 			parm->hc_max_packet_count = 3;
358302ac6454SAndrew Thompson 		} else if (parm->speed == USB_SPEED_FULL) {
358402ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
358502ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
358602ac6454SAndrew Thompson 		} else {
358702ac6454SAndrew Thompson 			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
358802ac6454SAndrew Thompson 			parm->hc_max_packet_count = 1;
358902ac6454SAndrew Thompson 		}
359002ac6454SAndrew Thompson 
359102ac6454SAndrew Thompson 		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
359202ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
359302ac6454SAndrew Thompson 
359402ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
359502ac6454SAndrew Thompson 
359602ac6454SAndrew Thompson 		nqh = 1;
359702ac6454SAndrew Thompson 		nqtd = ((2 * xfer->nframes)
359802ac6454SAndrew Thompson 		    + (xfer->max_data_length / xfer->max_usb2_frame_size));
359902ac6454SAndrew Thompson 
360002ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_fs_methods) {
360102ac6454SAndrew Thompson 
360202ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x3FF;
360302ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
360402ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x3FF;
360502ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
360602ac6454SAndrew Thompson 
360702ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
360802ac6454SAndrew Thompson 
360902ac6454SAndrew Thompson 		nsitd = xfer->nframes;
361002ac6454SAndrew Thompson 
361102ac6454SAndrew Thompson 	} else if (parm->methods == &ehci_device_isoc_hs_methods) {
361202ac6454SAndrew Thompson 
361302ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
361402ac6454SAndrew Thompson 		parm->hc_max_packet_count = 3;
361502ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0xC00;
361602ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
361702ac6454SAndrew Thompson 
361802ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
361902ac6454SAndrew Thompson 
362002ac6454SAndrew Thompson 		nitd = (xfer->nframes + 7) / 8;
362102ac6454SAndrew Thompson 
362202ac6454SAndrew Thompson 	} else {
362302ac6454SAndrew Thompson 
362402ac6454SAndrew Thompson 		parm->hc_max_packet_size = 0x400;
362502ac6454SAndrew Thompson 		parm->hc_max_packet_count = 1;
362602ac6454SAndrew Thompson 		parm->hc_max_frame_size = 0x400;
362702ac6454SAndrew Thompson 
362802ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
362902ac6454SAndrew Thompson 	}
363002ac6454SAndrew Thompson 
363102ac6454SAndrew Thompson alloc_dma_set:
363202ac6454SAndrew Thompson 
363302ac6454SAndrew Thompson 	if (parm->err) {
363402ac6454SAndrew Thompson 		return;
363502ac6454SAndrew Thompson 	}
363602ac6454SAndrew Thompson 	/*
363702ac6454SAndrew Thompson 	 * Allocate queue heads and transfer descriptors
363802ac6454SAndrew Thompson 	 */
363902ac6454SAndrew Thompson 	last_obj = NULL;
364002ac6454SAndrew Thompson 
364102ac6454SAndrew Thompson 	if (usb2_transfer_setup_sub_malloc(
364202ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_itd_t),
364302ac6454SAndrew Thompson 	    EHCI_ITD_ALIGN, nitd)) {
364402ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
364502ac6454SAndrew Thompson 		return;
364602ac6454SAndrew Thompson 	}
364702ac6454SAndrew Thompson 	if (parm->buf) {
364802ac6454SAndrew Thompson 		for (n = 0; n != nitd; n++) {
364902ac6454SAndrew Thompson 			ehci_itd_t *td;
365002ac6454SAndrew Thompson 
365102ac6454SAndrew Thompson 			usb2_get_page(pc + n, 0, &page_info);
365202ac6454SAndrew Thompson 
365302ac6454SAndrew Thompson 			td = page_info.buffer;
365402ac6454SAndrew Thompson 
365502ac6454SAndrew Thompson 			/* init TD */
365602ac6454SAndrew Thompson 			td->itd_self = htoehci32(sc, page_info.physaddr | EHCI_LINK_ITD);
365702ac6454SAndrew Thompson 			td->obj_next = last_obj;
365802ac6454SAndrew Thompson 			td->page_cache = pc + n;
365902ac6454SAndrew Thompson 
366002ac6454SAndrew Thompson 			last_obj = td;
366102ac6454SAndrew Thompson 
366202ac6454SAndrew Thompson 			usb2_pc_cpu_flush(pc + n);
366302ac6454SAndrew Thompson 		}
366402ac6454SAndrew Thompson 	}
366502ac6454SAndrew Thompson 	if (usb2_transfer_setup_sub_malloc(
366602ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_sitd_t),
366702ac6454SAndrew Thompson 	    EHCI_SITD_ALIGN, nsitd)) {
366802ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
366902ac6454SAndrew Thompson 		return;
367002ac6454SAndrew Thompson 	}
367102ac6454SAndrew Thompson 	if (parm->buf) {
367202ac6454SAndrew Thompson 		for (n = 0; n != nsitd; n++) {
367302ac6454SAndrew Thompson 			ehci_sitd_t *td;
367402ac6454SAndrew Thompson 
367502ac6454SAndrew Thompson 			usb2_get_page(pc + n, 0, &page_info);
367602ac6454SAndrew Thompson 
367702ac6454SAndrew Thompson 			td = page_info.buffer;
367802ac6454SAndrew Thompson 
367902ac6454SAndrew Thompson 			/* init TD */
368002ac6454SAndrew Thompson 			td->sitd_self = htoehci32(sc, page_info.physaddr | EHCI_LINK_SITD);
368102ac6454SAndrew Thompson 			td->obj_next = last_obj;
368202ac6454SAndrew Thompson 			td->page_cache = pc + n;
368302ac6454SAndrew Thompson 
368402ac6454SAndrew Thompson 			last_obj = td;
368502ac6454SAndrew Thompson 
368602ac6454SAndrew Thompson 			usb2_pc_cpu_flush(pc + n);
368702ac6454SAndrew Thompson 		}
368802ac6454SAndrew Thompson 	}
368902ac6454SAndrew Thompson 	if (usb2_transfer_setup_sub_malloc(
369002ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qtd_t),
369102ac6454SAndrew Thompson 	    EHCI_QTD_ALIGN, nqtd)) {
369202ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
369302ac6454SAndrew Thompson 		return;
369402ac6454SAndrew Thompson 	}
369502ac6454SAndrew Thompson 	if (parm->buf) {
369602ac6454SAndrew Thompson 		for (n = 0; n != nqtd; n++) {
369702ac6454SAndrew Thompson 			ehci_qtd_t *qtd;
369802ac6454SAndrew Thompson 
369902ac6454SAndrew Thompson 			usb2_get_page(pc + n, 0, &page_info);
370002ac6454SAndrew Thompson 
370102ac6454SAndrew Thompson 			qtd = page_info.buffer;
370202ac6454SAndrew Thompson 
370302ac6454SAndrew Thompson 			/* init TD */
370402ac6454SAndrew Thompson 			qtd->qtd_self = htoehci32(sc, page_info.physaddr);
370502ac6454SAndrew Thompson 			qtd->obj_next = last_obj;
370602ac6454SAndrew Thompson 			qtd->page_cache = pc + n;
370702ac6454SAndrew Thompson 
370802ac6454SAndrew Thompson 			last_obj = qtd;
370902ac6454SAndrew Thompson 
371002ac6454SAndrew Thompson 			usb2_pc_cpu_flush(pc + n);
371102ac6454SAndrew Thompson 		}
371202ac6454SAndrew Thompson 	}
371302ac6454SAndrew Thompson 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
371402ac6454SAndrew Thompson 
371502ac6454SAndrew Thompson 	last_obj = NULL;
371602ac6454SAndrew Thompson 
371702ac6454SAndrew Thompson 	if (usb2_transfer_setup_sub_malloc(
371802ac6454SAndrew Thompson 	    parm, &pc, sizeof(ehci_qh_t),
371902ac6454SAndrew Thompson 	    EHCI_QH_ALIGN, nqh)) {
372002ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
372102ac6454SAndrew Thompson 		return;
372202ac6454SAndrew Thompson 	}
372302ac6454SAndrew Thompson 	if (parm->buf) {
372402ac6454SAndrew Thompson 		for (n = 0; n != nqh; n++) {
372502ac6454SAndrew Thompson 			ehci_qh_t *qh;
372602ac6454SAndrew Thompson 
372702ac6454SAndrew Thompson 			usb2_get_page(pc + n, 0, &page_info);
372802ac6454SAndrew Thompson 
372902ac6454SAndrew Thompson 			qh = page_info.buffer;
373002ac6454SAndrew Thompson 
373102ac6454SAndrew Thompson 			/* init QH */
373202ac6454SAndrew Thompson 			qh->qh_self = htoehci32(sc, page_info.physaddr | EHCI_LINK_QH);
373302ac6454SAndrew Thompson 			qh->obj_next = last_obj;
373402ac6454SAndrew Thompson 			qh->page_cache = pc + n;
373502ac6454SAndrew Thompson 
373602ac6454SAndrew Thompson 			last_obj = qh;
373702ac6454SAndrew Thompson 
373802ac6454SAndrew Thompson 			usb2_pc_cpu_flush(pc + n);
373902ac6454SAndrew Thompson 		}
374002ac6454SAndrew Thompson 	}
374102ac6454SAndrew Thompson 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
374202ac6454SAndrew Thompson 
374302ac6454SAndrew Thompson 	if (!xfer->flags_int.curr_dma_set) {
374402ac6454SAndrew Thompson 		xfer->flags_int.curr_dma_set = 1;
374502ac6454SAndrew Thompson 		goto alloc_dma_set;
374602ac6454SAndrew Thompson 	}
374702ac6454SAndrew Thompson }
374802ac6454SAndrew Thompson 
374902ac6454SAndrew Thompson static void
375002ac6454SAndrew Thompson ehci_xfer_unsetup(struct usb2_xfer *xfer)
375102ac6454SAndrew Thompson {
375202ac6454SAndrew Thompson 	return;
375302ac6454SAndrew Thompson }
375402ac6454SAndrew Thompson 
375502ac6454SAndrew Thompson static void
375602ac6454SAndrew Thompson ehci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc,
375702ac6454SAndrew Thompson     struct usb2_pipe *pipe)
375802ac6454SAndrew Thompson {
375902ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
376002ac6454SAndrew Thompson 
376102ac6454SAndrew Thompson 	DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
376202ac6454SAndrew Thompson 	    pipe, udev->address,
376302ac6454SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb2_mode,
376402ac6454SAndrew Thompson 	    sc->sc_addr);
376502ac6454SAndrew Thompson 
376602ac6454SAndrew Thompson 	if (udev->flags.usb2_mode != USB_MODE_HOST) {
376702ac6454SAndrew Thompson 		/* not supported */
376802ac6454SAndrew Thompson 		return;
376902ac6454SAndrew Thompson 	}
377002ac6454SAndrew Thompson 	if (udev->device_index == sc->sc_addr) {
377102ac6454SAndrew Thompson 		switch (edesc->bEndpointAddress) {
377202ac6454SAndrew Thompson 		case USB_CONTROL_ENDPOINT:
377302ac6454SAndrew Thompson 			pipe->methods = &ehci_root_ctrl_methods;
377402ac6454SAndrew Thompson 			break;
377502ac6454SAndrew Thompson 		case UE_DIR_IN | EHCI_INTR_ENDPT:
377602ac6454SAndrew Thompson 			pipe->methods = &ehci_root_intr_methods;
377702ac6454SAndrew Thompson 			break;
377802ac6454SAndrew Thompson 		default:
377902ac6454SAndrew Thompson 			/* do nothing */
378002ac6454SAndrew Thompson 			break;
378102ac6454SAndrew Thompson 		}
378202ac6454SAndrew Thompson 	} else {
378302ac6454SAndrew Thompson 		if ((udev->speed != USB_SPEED_HIGH) &&
378402ac6454SAndrew Thompson 		    ((udev->hs_hub_addr == 0) ||
378502ac6454SAndrew Thompson 		    (udev->hs_port_no == 0) ||
378602ac6454SAndrew Thompson 		    (udev->bus->devices[udev->hs_hub_addr] == NULL) ||
378702ac6454SAndrew Thompson 		    (udev->bus->devices[udev->hs_hub_addr]->hub == NULL))) {
378802ac6454SAndrew Thompson 			/* We need a transaction translator */
378902ac6454SAndrew Thompson 			goto done;
379002ac6454SAndrew Thompson 		}
379102ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
379202ac6454SAndrew Thompson 		case UE_CONTROL:
379302ac6454SAndrew Thompson 			pipe->methods = &ehci_device_ctrl_methods;
379402ac6454SAndrew Thompson 			break;
379502ac6454SAndrew Thompson 		case UE_INTERRUPT:
379602ac6454SAndrew Thompson 			pipe->methods = &ehci_device_intr_methods;
379702ac6454SAndrew Thompson 			break;
379802ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
379902ac6454SAndrew Thompson 			if (udev->speed == USB_SPEED_HIGH) {
380002ac6454SAndrew Thompson 				pipe->methods = &ehci_device_isoc_hs_methods;
380102ac6454SAndrew Thompson 			} else if (udev->speed == USB_SPEED_FULL) {
380202ac6454SAndrew Thompson 				pipe->methods = &ehci_device_isoc_fs_methods;
380302ac6454SAndrew Thompson 			}
380402ac6454SAndrew Thompson 			break;
380502ac6454SAndrew Thompson 		case UE_BULK:
380602ac6454SAndrew Thompson 			if (udev->speed != USB_SPEED_LOW) {
380702ac6454SAndrew Thompson 				pipe->methods = &ehci_device_bulk_methods;
380802ac6454SAndrew Thompson 			}
380902ac6454SAndrew Thompson 			break;
381002ac6454SAndrew Thompson 		default:
381102ac6454SAndrew Thompson 			/* do nothing */
381202ac6454SAndrew Thompson 			break;
381302ac6454SAndrew Thompson 		}
381402ac6454SAndrew Thompson 	}
381502ac6454SAndrew Thompson done:
381602ac6454SAndrew Thompson 	return;
381702ac6454SAndrew Thompson }
381802ac6454SAndrew Thompson 
381902ac6454SAndrew Thompson static void
382002ac6454SAndrew Thompson ehci_get_dma_delay(struct usb2_bus *bus, uint32_t *pus)
382102ac6454SAndrew Thompson {
382202ac6454SAndrew Thompson 	/*
382302ac6454SAndrew Thompson 	 * Wait until the hardware has finished any possible use of
382402ac6454SAndrew Thompson 	 * the transfer descriptor(s) and QH
382502ac6454SAndrew Thompson 	 */
382602ac6454SAndrew Thompson 	*pus = (188);			/* microseconds */
382702ac6454SAndrew Thompson }
382802ac6454SAndrew Thompson 
382902ac6454SAndrew Thompson static void
383002ac6454SAndrew Thompson ehci_device_resume(struct usb2_device *udev)
383102ac6454SAndrew Thompson {
383202ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
383302ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
383402ac6454SAndrew Thompson 	struct usb2_pipe_methods *methods;
383502ac6454SAndrew Thompson 
383602ac6454SAndrew Thompson 	DPRINTF("\n");
383702ac6454SAndrew Thompson 
383802ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
383902ac6454SAndrew Thompson 
384002ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
384102ac6454SAndrew Thompson 
384202ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
384302ac6454SAndrew Thompson 
384402ac6454SAndrew Thompson 			methods = xfer->pipe->methods;
384502ac6454SAndrew Thompson 
384602ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
384702ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
384802ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
384902ac6454SAndrew Thompson 				    sc->sc_async_p_last);
385002ac6454SAndrew Thompson 			}
385102ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
385202ac6454SAndrew Thompson 				EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
385302ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
385402ac6454SAndrew Thompson 			}
385502ac6454SAndrew Thompson 		}
385602ac6454SAndrew Thompson 	}
385702ac6454SAndrew Thompson 
385802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
385902ac6454SAndrew Thompson 
386002ac6454SAndrew Thompson 	return;
386102ac6454SAndrew Thompson }
386202ac6454SAndrew Thompson 
386302ac6454SAndrew Thompson static void
386402ac6454SAndrew Thompson ehci_device_suspend(struct usb2_device *udev)
386502ac6454SAndrew Thompson {
386602ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
386702ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
386802ac6454SAndrew Thompson 	struct usb2_pipe_methods *methods;
386902ac6454SAndrew Thompson 
387002ac6454SAndrew Thompson 	DPRINTF("\n");
387102ac6454SAndrew Thompson 
387202ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
387302ac6454SAndrew Thompson 
387402ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
387502ac6454SAndrew Thompson 
387602ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
387702ac6454SAndrew Thompson 
387802ac6454SAndrew Thompson 			methods = xfer->pipe->methods;
387902ac6454SAndrew Thompson 
388002ac6454SAndrew Thompson 			if ((methods == &ehci_device_bulk_methods) ||
388102ac6454SAndrew Thompson 			    (methods == &ehci_device_ctrl_methods)) {
388202ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
388302ac6454SAndrew Thompson 				    sc->sc_async_p_last);
388402ac6454SAndrew Thompson 			}
388502ac6454SAndrew Thompson 			if (methods == &ehci_device_intr_methods) {
388602ac6454SAndrew Thompson 				EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set],
388702ac6454SAndrew Thompson 				    sc->sc_intr_p_last[xfer->qh_pos]);
388802ac6454SAndrew Thompson 			}
388902ac6454SAndrew Thompson 		}
389002ac6454SAndrew Thompson 	}
389102ac6454SAndrew Thompson 
389202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
389302ac6454SAndrew Thompson 
389402ac6454SAndrew Thompson 	return;
389502ac6454SAndrew Thompson }
389602ac6454SAndrew Thompson 
389702ac6454SAndrew Thompson static void
389802ac6454SAndrew Thompson ehci_set_hw_power(struct usb2_bus *bus)
389902ac6454SAndrew Thompson {
390002ac6454SAndrew Thompson 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
390102ac6454SAndrew Thompson 	uint32_t temp;
390202ac6454SAndrew Thompson 	uint32_t flags;
390302ac6454SAndrew Thompson 
390402ac6454SAndrew Thompson 	DPRINTF("\n");
390502ac6454SAndrew Thompson 
390602ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
390702ac6454SAndrew Thompson 
390802ac6454SAndrew Thompson 	flags = bus->hw_power_state;
390902ac6454SAndrew Thompson 
391002ac6454SAndrew Thompson 	temp = EOREAD4(sc, EHCI_USBCMD);
391102ac6454SAndrew Thompson 
391202ac6454SAndrew Thompson 	temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
391302ac6454SAndrew Thompson 
391402ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_CONTROL |
391502ac6454SAndrew Thompson 	    USB_HW_POWER_BULK)) {
391602ac6454SAndrew Thompson 		DPRINTF("Async is active\n");
391702ac6454SAndrew Thompson 		temp |= EHCI_CMD_ASE;
391802ac6454SAndrew Thompson 	}
391902ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_INTERRUPT |
392002ac6454SAndrew Thompson 	    USB_HW_POWER_ISOC)) {
392102ac6454SAndrew Thompson 		DPRINTF("Periodic is active\n");
392202ac6454SAndrew Thompson 		temp |= EHCI_CMD_PSE;
392302ac6454SAndrew Thompson 	}
392402ac6454SAndrew Thompson 	EOWRITE4(sc, EHCI_USBCMD, temp);
392502ac6454SAndrew Thompson 
392602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
392702ac6454SAndrew Thompson 
392802ac6454SAndrew Thompson 	return;
392902ac6454SAndrew Thompson }
393002ac6454SAndrew Thompson 
393102ac6454SAndrew Thompson struct usb2_bus_methods ehci_bus_methods =
393202ac6454SAndrew Thompson {
393302ac6454SAndrew Thompson 	.pipe_init = ehci_pipe_init,
393402ac6454SAndrew Thompson 	.xfer_setup = ehci_xfer_setup,
393502ac6454SAndrew Thompson 	.xfer_unsetup = ehci_xfer_unsetup,
393602ac6454SAndrew Thompson 	.get_dma_delay = ehci_get_dma_delay,
393702ac6454SAndrew Thompson 	.device_resume = ehci_device_resume,
393802ac6454SAndrew Thompson 	.device_suspend = ehci_device_suspend,
393902ac6454SAndrew Thompson 	.set_hw_power = ehci_set_hw_power,
394002ac6454SAndrew Thompson 	.roothub_exec = ehci_root_ctrl_task,
394102ac6454SAndrew Thompson };
3942