xref: /freebsd/sys/dev/usb/controller/uhci.c (revision 02ac6454880b59bbc5f3f74dffaffa90b30adc8b)
102ac6454SAndrew Thompson /*-
202ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
302ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
402ac6454SAndrew Thompson  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
502ac6454SAndrew Thompson  *
602ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
702ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
802ac6454SAndrew Thompson  * are met:
902ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1102ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1302ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1402ac6454SAndrew Thompson  *
1502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1602ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1702ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1802ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1902ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2002ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2102ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2202ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2302ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2402ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2502ac6454SAndrew Thompson  * SUCH DAMAGE.
2602ac6454SAndrew Thompson  */
2702ac6454SAndrew Thompson 
2802ac6454SAndrew Thompson #include <sys/cdefs.h>
2902ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
3002ac6454SAndrew Thompson 
3102ac6454SAndrew Thompson /*
3202ac6454SAndrew Thompson  * USB Universal Host Controller driver.
3302ac6454SAndrew Thompson  * Handles e.g. PIIX3 and PIIX4.
3402ac6454SAndrew Thompson  *
3502ac6454SAndrew Thompson  * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
3602ac6454SAndrew Thompson  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
3702ac6454SAndrew Thompson  * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
3802ac6454SAndrew Thompson  *             ftp://download.intel.com/design/intarch/datashts/29056201.pdf
3902ac6454SAndrew Thompson  */
4002ac6454SAndrew Thompson 
4102ac6454SAndrew Thompson #include <dev/usb/usb.h>
4202ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
4302ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
4402ac6454SAndrew Thompson #include <dev/usb/usb_defs.h>
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson #define	USB_DEBUG_VAR uhcidebug
4702ac6454SAndrew Thompson 
4802ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
4902ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5002ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
5102ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5202ac6454SAndrew Thompson #include <dev/usb/usb_sw_transfer.h>
5302ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
5402ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
5502ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
5602ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
5702ac6454SAndrew Thompson 
5802ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
6002ac6454SAndrew Thompson #include <dev/usb/controller/uhci.h>
6102ac6454SAndrew Thompson 
6202ac6454SAndrew Thompson #define	alt_next next
6302ac6454SAndrew Thompson #define	UHCI_BUS2SC(bus) ((uhci_softc_t *)(((uint8_t *)(bus)) - \
6402ac6454SAndrew Thompson    USB_P2U(&(((uhci_softc_t *)0)->sc_bus))))
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson #if USB_DEBUG
6702ac6454SAndrew Thompson static int uhcidebug = 0;
6802ac6454SAndrew Thompson static int uhcinoloop = 0;
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
7102ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_uhci, OID_AUTO, debug, CTLFLAG_RW,
7202ac6454SAndrew Thompson     &uhcidebug, 0, "uhci debug level");
7302ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_uhci, OID_AUTO, loop, CTLFLAG_RW,
7402ac6454SAndrew Thompson     &uhcinoloop, 0, "uhci noloop");
7502ac6454SAndrew Thompson static void uhci_dumpregs(uhci_softc_t *sc);
7602ac6454SAndrew Thompson static void uhci_dump_tds(uhci_td_t *td);
7702ac6454SAndrew Thompson 
7802ac6454SAndrew Thompson #endif
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson #define	UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
8102ac6454SAndrew Thompson 			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
8202ac6454SAndrew Thompson #define	UWRITE1(sc, r, x) \
8302ac6454SAndrew Thompson  do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
8402ac6454SAndrew Thompson  } while (/*CONSTCOND*/0)
8502ac6454SAndrew Thompson #define	UWRITE2(sc, r, x) \
8602ac6454SAndrew Thompson  do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
8702ac6454SAndrew Thompson  } while (/*CONSTCOND*/0)
8802ac6454SAndrew Thompson #define	UWRITE4(sc, r, x) \
8902ac6454SAndrew Thompson  do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
9002ac6454SAndrew Thompson  } while (/*CONSTCOND*/0)
9102ac6454SAndrew Thompson #define	UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
9202ac6454SAndrew Thompson #define	UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
9302ac6454SAndrew Thompson #define	UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
9402ac6454SAndrew Thompson 
9502ac6454SAndrew Thompson #define	UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
9602ac6454SAndrew Thompson #define	UHCISTS(sc) UREAD2(sc, UHCI_STS)
9702ac6454SAndrew Thompson 
9802ac6454SAndrew Thompson #define	UHCI_RESET_TIMEOUT 100		/* ms, reset timeout */
9902ac6454SAndrew Thompson 
10002ac6454SAndrew Thompson #define	UHCI_INTR_ENDPT 1
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson struct uhci_mem_layout {
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
10502ac6454SAndrew Thompson 	struct usb2_page_search fix_res;
10602ac6454SAndrew Thompson 
10702ac6454SAndrew Thompson 	struct usb2_page_cache *buf_pc;
10802ac6454SAndrew Thompson 	struct usb2_page_cache *fix_pc;
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson 	uint32_t buf_offset;
11102ac6454SAndrew Thompson 
11202ac6454SAndrew Thompson 	uint16_t max_frame_size;
11302ac6454SAndrew Thompson };
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson struct uhci_std_temp {
11602ac6454SAndrew Thompson 
11702ac6454SAndrew Thompson 	struct uhci_mem_layout ml;
11802ac6454SAndrew Thompson 	uhci_td_t *td;
11902ac6454SAndrew Thompson 	uhci_td_t *td_next;
12002ac6454SAndrew Thompson 	uint32_t average;
12102ac6454SAndrew Thompson 	uint32_t td_status;
12202ac6454SAndrew Thompson 	uint32_t td_token;
12302ac6454SAndrew Thompson 	uint32_t len;
12402ac6454SAndrew Thompson 	uint16_t max_frame_size;
12502ac6454SAndrew Thompson 	uint8_t	shortpkt;
12602ac6454SAndrew Thompson 	uint8_t	setup_alt_next;
12702ac6454SAndrew Thompson 	uint8_t	short_frames_ok;
12802ac6454SAndrew Thompson };
12902ac6454SAndrew Thompson 
13002ac6454SAndrew Thompson extern struct usb2_bus_methods uhci_bus_methods;
13102ac6454SAndrew Thompson extern struct usb2_pipe_methods uhci_device_bulk_methods;
13202ac6454SAndrew Thompson extern struct usb2_pipe_methods uhci_device_ctrl_methods;
13302ac6454SAndrew Thompson extern struct usb2_pipe_methods uhci_device_intr_methods;
13402ac6454SAndrew Thompson extern struct usb2_pipe_methods uhci_device_isoc_methods;
13502ac6454SAndrew Thompson extern struct usb2_pipe_methods uhci_root_ctrl_methods;
13602ac6454SAndrew Thompson extern struct usb2_pipe_methods uhci_root_intr_methods;
13702ac6454SAndrew Thompson 
13802ac6454SAndrew Thompson static void	uhci_root_ctrl_poll(struct uhci_softc *);
13902ac6454SAndrew Thompson static void	uhci_do_poll(struct usb2_bus *);
14002ac6454SAndrew Thompson static void	uhci_device_done(struct usb2_xfer *, usb2_error_t);
14102ac6454SAndrew Thompson static void	uhci_transfer_intr_enqueue(struct usb2_xfer *);
14202ac6454SAndrew Thompson static void	uhci_root_intr_check(void *);
14302ac6454SAndrew Thompson static void	uhci_timeout(void *);
14402ac6454SAndrew Thompson static uint8_t	uhci_check_transfer(struct usb2_xfer *);
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson void
14702ac6454SAndrew Thompson uhci_iterate_hw_softc(struct usb2_bus *bus, usb2_bus_mem_sub_cb_t *cb)
14802ac6454SAndrew Thompson {
14902ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
15002ac6454SAndrew Thompson 	uint32_t i;
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
15302ac6454SAndrew Thompson 	    sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN);
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg,
15602ac6454SAndrew Thompson 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg,
15902ac6454SAndrew Thompson 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
16202ac6454SAndrew Thompson 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg,
16502ac6454SAndrew Thompson 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
16602ac6454SAndrew Thompson 
16702ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg,
16802ac6454SAndrew Thompson 	    sizeof(uhci_td_t), UHCI_TD_ALIGN);
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson 	for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) {
17102ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_start_pc + i,
17202ac6454SAndrew Thompson 		    sc->sc_hw.isoc_start_pg + i,
17302ac6454SAndrew Thompson 		    sizeof(uhci_td_t), UHCI_TD_ALIGN);
17402ac6454SAndrew Thompson 	}
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson 	for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) {
17702ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.intr_start_pc + i,
17802ac6454SAndrew Thompson 		    sc->sc_hw.intr_start_pg + i,
17902ac6454SAndrew Thompson 		    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
18002ac6454SAndrew Thompson 	}
18102ac6454SAndrew Thompson }
18202ac6454SAndrew Thompson 
18302ac6454SAndrew Thompson static void
18402ac6454SAndrew Thompson uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb2_xfer *xfer)
18502ac6454SAndrew Thompson {
18602ac6454SAndrew Thompson 	ml->buf_pc = xfer->frbuffers + 0;
18702ac6454SAndrew Thompson 	ml->fix_pc = xfer->buf_fixup;
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson 	ml->buf_offset = 0;
19002ac6454SAndrew Thompson 
19102ac6454SAndrew Thompson 	ml->max_frame_size = xfer->max_frame_size;
19202ac6454SAndrew Thompson }
19302ac6454SAndrew Thompson 
19402ac6454SAndrew Thompson static void
19502ac6454SAndrew Thompson uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td)
19602ac6454SAndrew Thompson {
19702ac6454SAndrew Thompson 	usb2_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res);
19802ac6454SAndrew Thompson 
19902ac6454SAndrew Thompson 	if (ml->buf_res.length < td->len) {
20002ac6454SAndrew Thompson 
20102ac6454SAndrew Thompson 		/* need to do a fixup */
20202ac6454SAndrew Thompson 
20302ac6454SAndrew Thompson 		usb2_get_page(ml->fix_pc, 0, &ml->fix_res);
20402ac6454SAndrew Thompson 
20502ac6454SAndrew Thompson 		td->td_buffer = htole32(ml->fix_res.physaddr);
20602ac6454SAndrew Thompson 
20702ac6454SAndrew Thompson 		/*
20802ac6454SAndrew Thompson 	         * The UHCI driver cannot handle
20902ac6454SAndrew Thompson 	         * page crossings, so a fixup is
21002ac6454SAndrew Thompson 	         * needed:
21102ac6454SAndrew Thompson 	         *
21202ac6454SAndrew Thompson 	         *  +----+----+ - - -
21302ac6454SAndrew Thompson 	         *  | YYY|Y   |
21402ac6454SAndrew Thompson 	         *  +----+----+ - - -
21502ac6454SAndrew Thompson 	         *     \    \
21602ac6454SAndrew Thompson 	         *      \    \
21702ac6454SAndrew Thompson 	         *       +----+
21802ac6454SAndrew Thompson 	         *       |YYYY|  (fixup)
21902ac6454SAndrew Thompson 	         *       +----+
22002ac6454SAndrew Thompson 	         */
22102ac6454SAndrew Thompson 
22202ac6454SAndrew Thompson 		if ((td->td_token & htole32(UHCI_TD_PID)) ==
22302ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_IN)) {
22402ac6454SAndrew Thompson 			td->fix_pc = ml->fix_pc;
22502ac6454SAndrew Thompson 			usb2_pc_cpu_invalidate(ml->fix_pc);
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson 		} else {
22802ac6454SAndrew Thompson 			td->fix_pc = NULL;
22902ac6454SAndrew Thompson 
23002ac6454SAndrew Thompson 			/* copy data to fixup location */
23102ac6454SAndrew Thompson 
23202ac6454SAndrew Thompson 			usb2_copy_out(ml->buf_pc, ml->buf_offset,
23302ac6454SAndrew Thompson 			    ml->fix_res.buffer, td->len);
23402ac6454SAndrew Thompson 
23502ac6454SAndrew Thompson 			usb2_pc_cpu_flush(ml->fix_pc);
23602ac6454SAndrew Thompson 		}
23702ac6454SAndrew Thompson 
23802ac6454SAndrew Thompson 		/* prepare next fixup */
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 		ml->fix_pc++;
24102ac6454SAndrew Thompson 
24202ac6454SAndrew Thompson 	} else {
24302ac6454SAndrew Thompson 
24402ac6454SAndrew Thompson 		td->td_buffer = htole32(ml->buf_res.physaddr);
24502ac6454SAndrew Thompson 		td->fix_pc = NULL;
24602ac6454SAndrew Thompson 	}
24702ac6454SAndrew Thompson 
24802ac6454SAndrew Thompson 	/* prepare next data location */
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson 	ml->buf_offset += td->len;
25102ac6454SAndrew Thompson }
25202ac6454SAndrew Thompson 
25302ac6454SAndrew Thompson void
25402ac6454SAndrew Thompson uhci_reset(uhci_softc_t *sc)
25502ac6454SAndrew Thompson {
25602ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
25702ac6454SAndrew Thompson 	uint16_t n;
25802ac6454SAndrew Thompson 
25902ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
26002ac6454SAndrew Thompson 
26102ac6454SAndrew Thompson 	DPRINTF("resetting the HC\n");
26202ac6454SAndrew Thompson 
26302ac6454SAndrew Thompson 	/* disable interrupts */
26402ac6454SAndrew Thompson 
26502ac6454SAndrew Thompson 	UWRITE2(sc, UHCI_INTR, 0);
26602ac6454SAndrew Thompson 
26702ac6454SAndrew Thompson 	/* global reset */
26802ac6454SAndrew Thompson 
26902ac6454SAndrew Thompson 	UHCICMD(sc, UHCI_CMD_GRESET);
27002ac6454SAndrew Thompson 
27102ac6454SAndrew Thompson 	/* wait */
27202ac6454SAndrew Thompson 
27302ac6454SAndrew Thompson 	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
27402ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
27502ac6454SAndrew Thompson 
27602ac6454SAndrew Thompson 	/* terminate all transfers */
27702ac6454SAndrew Thompson 
27802ac6454SAndrew Thompson 	UHCICMD(sc, UHCI_CMD_HCRESET);
27902ac6454SAndrew Thompson 
28002ac6454SAndrew Thompson 	/* the reset bit goes low when the controller is done */
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson 	n = UHCI_RESET_TIMEOUT;
28302ac6454SAndrew Thompson 	while (n--) {
28402ac6454SAndrew Thompson 		/* wait one millisecond */
28502ac6454SAndrew Thompson 
28602ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson 		if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
28902ac6454SAndrew Thompson 			goto done_1;
29002ac6454SAndrew Thompson 		}
29102ac6454SAndrew Thompson 	}
29202ac6454SAndrew Thompson 
29302ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev,
29402ac6454SAndrew Thompson 	    "controller did not reset\n");
29502ac6454SAndrew Thompson 
29602ac6454SAndrew Thompson done_1:
29702ac6454SAndrew Thompson 
29802ac6454SAndrew Thompson 	n = 10;
29902ac6454SAndrew Thompson 	while (n--) {
30002ac6454SAndrew Thompson 		/* wait one millisecond */
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
30302ac6454SAndrew Thompson 
30402ac6454SAndrew Thompson 		/* check if HC is stopped */
30502ac6454SAndrew Thompson 		if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
30602ac6454SAndrew Thompson 			goto done_2;
30702ac6454SAndrew Thompson 		}
30802ac6454SAndrew Thompson 	}
30902ac6454SAndrew Thompson 
31002ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev,
31102ac6454SAndrew Thompson 	    "controller did not stop\n");
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson done_2:
31402ac6454SAndrew Thompson 
31502ac6454SAndrew Thompson 	/* reload the configuration */
31602ac6454SAndrew Thompson 	usb2_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
31702ac6454SAndrew Thompson 	UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
31802ac6454SAndrew Thompson 	UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
31902ac6454SAndrew Thompson 	UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
32002ac6454SAndrew Thompson }
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson static void
32302ac6454SAndrew Thompson uhci_start(uhci_softc_t *sc)
32402ac6454SAndrew Thompson {
32502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
32602ac6454SAndrew Thompson 
32702ac6454SAndrew Thompson 	DPRINTFN(2, "enabling\n");
32802ac6454SAndrew Thompson 
32902ac6454SAndrew Thompson 	/* enable interrupts */
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson 	UWRITE2(sc, UHCI_INTR,
33202ac6454SAndrew Thompson 	    (UHCI_INTR_TOCRCIE |
33302ac6454SAndrew Thompson 	    UHCI_INTR_RIE |
33402ac6454SAndrew Thompson 	    UHCI_INTR_IOCE |
33502ac6454SAndrew Thompson 	    UHCI_INTR_SPIE));
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson 	/*
33802ac6454SAndrew Thompson 	 * assume 64 byte packets at frame end and start HC controller
33902ac6454SAndrew Thompson 	 */
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 	uint8_t n = 10;
34402ac6454SAndrew Thompson 
34502ac6454SAndrew Thompson 	while (n--) {
34602ac6454SAndrew Thompson 		/* wait one millisecond */
34702ac6454SAndrew Thompson 
34802ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
34902ac6454SAndrew Thompson 
35002ac6454SAndrew Thompson 		/* check that controller has started */
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson 		if (!(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH)) {
35302ac6454SAndrew Thompson 			goto done;
35402ac6454SAndrew Thompson 		}
35502ac6454SAndrew Thompson 	}
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev,
35802ac6454SAndrew Thompson 	    "cannot start HC controller\n");
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson done:
36102ac6454SAndrew Thompson 	return;
36202ac6454SAndrew Thompson }
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson static struct uhci_qh *
36502ac6454SAndrew Thompson uhci_init_qh(struct usb2_page_cache *pc)
36602ac6454SAndrew Thompson {
36702ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
36802ac6454SAndrew Thompson 	struct uhci_qh *qh;
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 	usb2_get_page(pc, 0, &buf_res);
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson 	qh = buf_res.buffer;
37302ac6454SAndrew Thompson 
37402ac6454SAndrew Thompson 	qh->qh_self =
37502ac6454SAndrew Thompson 	    htole32(buf_res.physaddr) |
37602ac6454SAndrew Thompson 	    htole32(UHCI_PTR_QH);
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson 	qh->page_cache = pc;
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson 	return (qh);
38102ac6454SAndrew Thompson }
38202ac6454SAndrew Thompson 
38302ac6454SAndrew Thompson static struct uhci_td *
38402ac6454SAndrew Thompson uhci_init_td(struct usb2_page_cache *pc)
38502ac6454SAndrew Thompson {
38602ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
38702ac6454SAndrew Thompson 	struct uhci_td *td;
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 	usb2_get_page(pc, 0, &buf_res);
39002ac6454SAndrew Thompson 
39102ac6454SAndrew Thompson 	td = buf_res.buffer;
39202ac6454SAndrew Thompson 
39302ac6454SAndrew Thompson 	td->td_self =
39402ac6454SAndrew Thompson 	    htole32(buf_res.physaddr) |
39502ac6454SAndrew Thompson 	    htole32(UHCI_PTR_TD);
39602ac6454SAndrew Thompson 
39702ac6454SAndrew Thompson 	td->page_cache = pc;
39802ac6454SAndrew Thompson 
39902ac6454SAndrew Thompson 	return (td);
40002ac6454SAndrew Thompson }
40102ac6454SAndrew Thompson 
40202ac6454SAndrew Thompson usb2_error_t
40302ac6454SAndrew Thompson uhci_init(uhci_softc_t *sc)
40402ac6454SAndrew Thompson {
40502ac6454SAndrew Thompson 	uint16_t bit;
40602ac6454SAndrew Thompson 	uint16_t x;
40702ac6454SAndrew Thompson 	uint16_t y;
40802ac6454SAndrew Thompson 
40902ac6454SAndrew Thompson 	DPRINTF("start\n");
41002ac6454SAndrew Thompson 
41102ac6454SAndrew Thompson #if USB_DEBUG
41202ac6454SAndrew Thompson 	if (uhcidebug > 2) {
41302ac6454SAndrew Thompson 		uhci_dumpregs(sc);
41402ac6454SAndrew Thompson 	}
41502ac6454SAndrew Thompson #endif
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson 	sc->sc_saved_sof = 0x40;	/* default value */
41802ac6454SAndrew Thompson 	sc->sc_saved_frnum = 0;		/* default frame number */
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson 	/*
42102ac6454SAndrew Thompson 	 * Setup QH's
42202ac6454SAndrew Thompson 	 */
42302ac6454SAndrew Thompson 	sc->sc_ls_ctl_p_last =
42402ac6454SAndrew Thompson 	    uhci_init_qh(&sc->sc_hw.ls_ctl_start_pc);
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson 	sc->sc_fs_ctl_p_last =
42702ac6454SAndrew Thompson 	    uhci_init_qh(&sc->sc_hw.fs_ctl_start_pc);
42802ac6454SAndrew Thompson 
42902ac6454SAndrew Thompson 	sc->sc_bulk_p_last =
43002ac6454SAndrew Thompson 	    uhci_init_qh(&sc->sc_hw.bulk_start_pc);
43102ac6454SAndrew Thompson #if 0
43202ac6454SAndrew Thompson 	sc->sc_reclaim_qh_p =
43302ac6454SAndrew Thompson 	    sc->sc_fs_ctl_p_last;
43402ac6454SAndrew Thompson #else
43502ac6454SAndrew Thompson 	/* setup reclaim looping point */
43602ac6454SAndrew Thompson 	sc->sc_reclaim_qh_p =
43702ac6454SAndrew Thompson 	    sc->sc_bulk_p_last;
43802ac6454SAndrew Thompson #endif
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 	sc->sc_last_qh_p =
44102ac6454SAndrew Thompson 	    uhci_init_qh(&sc->sc_hw.last_qh_pc);
44202ac6454SAndrew Thompson 
44302ac6454SAndrew Thompson 	sc->sc_last_td_p =
44402ac6454SAndrew Thompson 	    uhci_init_td(&sc->sc_hw.last_td_pc);
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson 	for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
44702ac6454SAndrew Thompson 		sc->sc_isoc_p_last[x] =
44802ac6454SAndrew Thompson 		    uhci_init_td(sc->sc_hw.isoc_start_pc + x);
44902ac6454SAndrew Thompson 	}
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 	for (x = 0; x != UHCI_IFRAMELIST_COUNT; x++) {
45202ac6454SAndrew Thompson 		sc->sc_intr_p_last[x] =
45302ac6454SAndrew Thompson 		    uhci_init_qh(sc->sc_hw.intr_start_pc + x);
45402ac6454SAndrew Thompson 	}
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson 	/*
45702ac6454SAndrew Thompson 	 * the QHs are arranged to give poll intervals that are
45802ac6454SAndrew Thompson 	 * powers of 2 times 1ms
45902ac6454SAndrew Thompson 	 */
46002ac6454SAndrew Thompson 	bit = UHCI_IFRAMELIST_COUNT / 2;
46102ac6454SAndrew Thompson 	while (bit) {
46202ac6454SAndrew Thompson 		x = bit;
46302ac6454SAndrew Thompson 		while (x & bit) {
46402ac6454SAndrew Thompson 			uhci_qh_t *qh_x;
46502ac6454SAndrew Thompson 			uhci_qh_t *qh_y;
46602ac6454SAndrew Thompson 
46702ac6454SAndrew Thompson 			y = (x ^ bit) | (bit / 2);
46802ac6454SAndrew Thompson 
46902ac6454SAndrew Thompson 			/*
47002ac6454SAndrew Thompson 			 * the next QH has half the poll interval
47102ac6454SAndrew Thompson 			 */
47202ac6454SAndrew Thompson 			qh_x = sc->sc_intr_p_last[x];
47302ac6454SAndrew Thompson 			qh_y = sc->sc_intr_p_last[y];
47402ac6454SAndrew Thompson 
47502ac6454SAndrew Thompson 			qh_x->h_next = NULL;
47602ac6454SAndrew Thompson 			qh_x->qh_h_next = qh_y->qh_self;
47702ac6454SAndrew Thompson 			qh_x->e_next = NULL;
47802ac6454SAndrew Thompson 			qh_x->qh_e_next = htole32(UHCI_PTR_T);
47902ac6454SAndrew Thompson 			x++;
48002ac6454SAndrew Thompson 		}
48102ac6454SAndrew Thompson 		bit >>= 1;
48202ac6454SAndrew Thompson 	}
48302ac6454SAndrew Thompson 
48402ac6454SAndrew Thompson 	if (1) {
48502ac6454SAndrew Thompson 		uhci_qh_t *qh_ls;
48602ac6454SAndrew Thompson 		uhci_qh_t *qh_intr;
48702ac6454SAndrew Thompson 
48802ac6454SAndrew Thompson 		qh_ls = sc->sc_ls_ctl_p_last;
48902ac6454SAndrew Thompson 		qh_intr = sc->sc_intr_p_last[0];
49002ac6454SAndrew Thompson 
49102ac6454SAndrew Thompson 		/* start QH for interrupt traffic */
49202ac6454SAndrew Thompson 		qh_intr->h_next = qh_ls;
49302ac6454SAndrew Thompson 		qh_intr->qh_h_next = qh_ls->qh_self;
49402ac6454SAndrew Thompson 		qh_intr->e_next = 0;
49502ac6454SAndrew Thompson 		qh_intr->qh_e_next = htole32(UHCI_PTR_T);
49602ac6454SAndrew Thompson 	}
49702ac6454SAndrew Thompson 	for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
49802ac6454SAndrew Thompson 
49902ac6454SAndrew Thompson 		uhci_td_t *td_x;
50002ac6454SAndrew Thompson 		uhci_qh_t *qh_intr;
50102ac6454SAndrew Thompson 
50202ac6454SAndrew Thompson 		td_x = sc->sc_isoc_p_last[x];
50302ac6454SAndrew Thompson 		qh_intr = sc->sc_intr_p_last[x | (UHCI_IFRAMELIST_COUNT / 2)];
50402ac6454SAndrew Thompson 
50502ac6454SAndrew Thompson 		/* start TD for isochronous traffic */
50602ac6454SAndrew Thompson 		td_x->next = NULL;
50702ac6454SAndrew Thompson 		td_x->td_next = qh_intr->qh_self;
50802ac6454SAndrew Thompson 		td_x->td_status = htole32(UHCI_TD_IOS);
50902ac6454SAndrew Thompson 		td_x->td_token = htole32(0);
51002ac6454SAndrew Thompson 		td_x->td_buffer = htole32(0);
51102ac6454SAndrew Thompson 	}
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson 	if (1) {
51402ac6454SAndrew Thompson 		uhci_qh_t *qh_ls;
51502ac6454SAndrew Thompson 		uhci_qh_t *qh_fs;
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 		qh_ls = sc->sc_ls_ctl_p_last;
51802ac6454SAndrew Thompson 		qh_fs = sc->sc_fs_ctl_p_last;
51902ac6454SAndrew Thompson 
52002ac6454SAndrew Thompson 		/* start QH where low speed control traffic will be queued */
52102ac6454SAndrew Thompson 		qh_ls->h_next = qh_fs;
52202ac6454SAndrew Thompson 		qh_ls->qh_h_next = qh_fs->qh_self;
52302ac6454SAndrew Thompson 		qh_ls->e_next = 0;
52402ac6454SAndrew Thompson 		qh_ls->qh_e_next = htole32(UHCI_PTR_T);
52502ac6454SAndrew Thompson 	}
52602ac6454SAndrew Thompson 	if (1) {
52702ac6454SAndrew Thompson 		uhci_qh_t *qh_ctl;
52802ac6454SAndrew Thompson 		uhci_qh_t *qh_blk;
52902ac6454SAndrew Thompson 		uhci_qh_t *qh_lst;
53002ac6454SAndrew Thompson 		uhci_td_t *td_lst;
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 		qh_ctl = sc->sc_fs_ctl_p_last;
53302ac6454SAndrew Thompson 		qh_blk = sc->sc_bulk_p_last;
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson 		/* start QH where full speed control traffic will be queued */
53602ac6454SAndrew Thompson 		qh_ctl->h_next = qh_blk;
53702ac6454SAndrew Thompson 		qh_ctl->qh_h_next = qh_blk->qh_self;
53802ac6454SAndrew Thompson 		qh_ctl->e_next = 0;
53902ac6454SAndrew Thompson 		qh_ctl->qh_e_next = htole32(UHCI_PTR_T);
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 		qh_lst = sc->sc_last_qh_p;
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson 		/* start QH where bulk traffic will be queued */
54402ac6454SAndrew Thompson 		qh_blk->h_next = qh_lst;
54502ac6454SAndrew Thompson 		qh_blk->qh_h_next = qh_lst->qh_self;
54602ac6454SAndrew Thompson 		qh_blk->e_next = 0;
54702ac6454SAndrew Thompson 		qh_blk->qh_e_next = htole32(UHCI_PTR_T);
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson 		td_lst = sc->sc_last_td_p;
55002ac6454SAndrew Thompson 
55102ac6454SAndrew Thompson 		/* end QH which is used for looping the QHs */
55202ac6454SAndrew Thompson 		qh_lst->h_next = 0;
55302ac6454SAndrew Thompson 		qh_lst->qh_h_next = htole32(UHCI_PTR_T);	/* end of QH chain */
55402ac6454SAndrew Thompson 		qh_lst->e_next = td_lst;
55502ac6454SAndrew Thompson 		qh_lst->qh_e_next = td_lst->td_self;
55602ac6454SAndrew Thompson 
55702ac6454SAndrew Thompson 		/*
55802ac6454SAndrew Thompson 		 * end TD which hangs from the last QH, to avoid a bug in the PIIX
55902ac6454SAndrew Thompson 		 * that makes it run berserk otherwise
56002ac6454SAndrew Thompson 		 */
56102ac6454SAndrew Thompson 		td_lst->next = 0;
56202ac6454SAndrew Thompson 		td_lst->td_next = htole32(UHCI_PTR_T);
56302ac6454SAndrew Thompson 		td_lst->td_status = htole32(0);	/* inactive */
56402ac6454SAndrew Thompson 		td_lst->td_token = htole32(0);
56502ac6454SAndrew Thompson 		td_lst->td_buffer = htole32(0);
56602ac6454SAndrew Thompson 	}
56702ac6454SAndrew Thompson 	if (1) {
56802ac6454SAndrew Thompson 		struct usb2_page_search buf_res;
56902ac6454SAndrew Thompson 		uint32_t *pframes;
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson 		usb2_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson 		pframes = buf_res.buffer;
57402ac6454SAndrew Thompson 
57502ac6454SAndrew Thompson 
57602ac6454SAndrew Thompson 		/*
57702ac6454SAndrew Thompson 		 * Setup UHCI framelist
57802ac6454SAndrew Thompson 		 *
57902ac6454SAndrew Thompson 		 * Execution order:
58002ac6454SAndrew Thompson 		 *
58102ac6454SAndrew Thompson 		 * pframes -> full speed isochronous -> interrupt QH's -> low
58202ac6454SAndrew Thompson 		 * speed control -> full speed control -> bulk transfers
58302ac6454SAndrew Thompson 		 *
58402ac6454SAndrew Thompson 		 */
58502ac6454SAndrew Thompson 
58602ac6454SAndrew Thompson 		for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) {
58702ac6454SAndrew Thompson 			pframes[x] =
58802ac6454SAndrew Thompson 			    sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self;
58902ac6454SAndrew Thompson 		}
59002ac6454SAndrew Thompson 	}
59102ac6454SAndrew Thompson 	/* flush all cache into memory */
59202ac6454SAndrew Thompson 
59302ac6454SAndrew Thompson 	usb2_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc);
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson 	/* set up the bus struct */
59602ac6454SAndrew Thompson 	sc->sc_bus.methods = &uhci_bus_methods;
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
59902ac6454SAndrew Thompson 	/* reset the controller */
60002ac6454SAndrew Thompson 	uhci_reset(sc);
60102ac6454SAndrew Thompson 
60202ac6454SAndrew Thompson 	/* start the controller */
60302ac6454SAndrew Thompson 	uhci_start(sc);
60402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
60502ac6454SAndrew Thompson 
60602ac6454SAndrew Thompson 	/* catch lost interrupts */
60702ac6454SAndrew Thompson 	uhci_do_poll(&sc->sc_bus);
60802ac6454SAndrew Thompson 
60902ac6454SAndrew Thompson 	return (0);
61002ac6454SAndrew Thompson }
61102ac6454SAndrew Thompson 
61202ac6454SAndrew Thompson /* NOTE: suspend/resume is called from
61302ac6454SAndrew Thompson  * interrupt context and cannot sleep!
61402ac6454SAndrew Thompson  */
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson void
61702ac6454SAndrew Thompson uhci_suspend(uhci_softc_t *sc)
61802ac6454SAndrew Thompson {
61902ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson #if USB_DEBUG
62202ac6454SAndrew Thompson 	if (uhcidebug > 2) {
62302ac6454SAndrew Thompson 		uhci_dumpregs(sc);
62402ac6454SAndrew Thompson 	}
62502ac6454SAndrew Thompson #endif
62602ac6454SAndrew Thompson 	/* save some state if BIOS doesn't */
62702ac6454SAndrew Thompson 
62802ac6454SAndrew Thompson 	sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
62902ac6454SAndrew Thompson 	sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
63002ac6454SAndrew Thompson 
63102ac6454SAndrew Thompson 	/* stop the controller */
63202ac6454SAndrew Thompson 
63302ac6454SAndrew Thompson 	uhci_reset(sc);
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson 	/* enter global suspend */
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	UHCICMD(sc, UHCI_CMD_EGSM);
63802ac6454SAndrew Thompson 
63902ac6454SAndrew Thompson 	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
64002ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(USB_RESUME_WAIT));
64102ac6454SAndrew Thompson 
64202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
64302ac6454SAndrew Thompson }
64402ac6454SAndrew Thompson 
64502ac6454SAndrew Thompson void
64602ac6454SAndrew Thompson uhci_resume(uhci_softc_t *sc)
64702ac6454SAndrew Thompson {
64802ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
64902ac6454SAndrew Thompson 
65002ac6454SAndrew Thompson 	/* reset the controller */
65102ac6454SAndrew Thompson 
65202ac6454SAndrew Thompson 	uhci_reset(sc);
65302ac6454SAndrew Thompson 
65402ac6454SAndrew Thompson 	/* force global resume */
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 	UHCICMD(sc, UHCI_CMD_FGR);
65702ac6454SAndrew Thompson 
65802ac6454SAndrew Thompson 	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
65902ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(USB_RESUME_DELAY));
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson 	/* and start traffic again */
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson 	uhci_start(sc);
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson #if USB_DEBUG
66602ac6454SAndrew Thompson 	if (uhcidebug > 2) {
66702ac6454SAndrew Thompson 		uhci_dumpregs(sc);
66802ac6454SAndrew Thompson 	}
66902ac6454SAndrew Thompson #endif
67002ac6454SAndrew Thompson 
67102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
67202ac6454SAndrew Thompson 
67302ac6454SAndrew Thompson 	/* catch lost interrupts */
67402ac6454SAndrew Thompson 	uhci_do_poll(&sc->sc_bus);
67502ac6454SAndrew Thompson }
67602ac6454SAndrew Thompson 
67702ac6454SAndrew Thompson #if USB_DEBUG
67802ac6454SAndrew Thompson static void
67902ac6454SAndrew Thompson uhci_dumpregs(uhci_softc_t *sc)
68002ac6454SAndrew Thompson {
68102ac6454SAndrew Thompson 	DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
68202ac6454SAndrew Thompson 	    "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
68302ac6454SAndrew Thompson 	    device_get_nameunit(sc->sc_bus.bdev),
68402ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_CMD),
68502ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_STS),
68602ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_INTR),
68702ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_FRNUM),
68802ac6454SAndrew Thompson 	    UREAD4(sc, UHCI_FLBASEADDR),
68902ac6454SAndrew Thompson 	    UREAD1(sc, UHCI_SOF),
69002ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_PORTSC1),
69102ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_PORTSC2));
69202ac6454SAndrew Thompson }
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson static uint8_t
69502ac6454SAndrew Thompson uhci_dump_td(uhci_td_t *p)
69602ac6454SAndrew Thompson {
69702ac6454SAndrew Thompson 	uint32_t td_next;
69802ac6454SAndrew Thompson 	uint32_t td_status;
69902ac6454SAndrew Thompson 	uint32_t td_token;
70002ac6454SAndrew Thompson 	uint8_t temp;
70102ac6454SAndrew Thompson 
70202ac6454SAndrew Thompson 	usb2_pc_cpu_invalidate(p->page_cache);
70302ac6454SAndrew Thompson 
70402ac6454SAndrew Thompson 	td_next = le32toh(p->td_next);
70502ac6454SAndrew Thompson 	td_status = le32toh(p->td_status);
70602ac6454SAndrew Thompson 	td_token = le32toh(p->td_token);
70702ac6454SAndrew Thompson 
70802ac6454SAndrew Thompson 	/*
70902ac6454SAndrew Thompson 	 * Check whether the link pointer in this TD marks the link pointer
71002ac6454SAndrew Thompson 	 * as end of queue:
71102ac6454SAndrew Thompson 	 */
71202ac6454SAndrew Thompson 	temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
71302ac6454SAndrew Thompson 
71402ac6454SAndrew Thompson 	printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
71502ac6454SAndrew Thompson 	    "token=0x%08x buffer=0x%08x\n",
71602ac6454SAndrew Thompson 	    p,
71702ac6454SAndrew Thompson 	    le32toh(p->td_self),
71802ac6454SAndrew Thompson 	    td_next,
71902ac6454SAndrew Thompson 	    td_status,
72002ac6454SAndrew Thompson 	    td_token,
72102ac6454SAndrew Thompson 	    le32toh(p->td_buffer));
72202ac6454SAndrew Thompson 
72302ac6454SAndrew Thompson 	printf("TD(%p) td_next=%s%s%s td_status=%s%s%s%s%s%s%s%s%s%s%s, errcnt=%d, actlen=%d pid=%02x,"
72402ac6454SAndrew Thompson 	    "addr=%d,endpt=%d,D=%d,maxlen=%d\n",
72502ac6454SAndrew Thompson 	    p,
72602ac6454SAndrew Thompson 	    (td_next & 1) ? "-T" : "",
72702ac6454SAndrew Thompson 	    (td_next & 2) ? "-Q" : "",
72802ac6454SAndrew Thompson 	    (td_next & 4) ? "-VF" : "",
72902ac6454SAndrew Thompson 	    (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
73002ac6454SAndrew Thompson 	    (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
73102ac6454SAndrew Thompson 	    (td_status & UHCI_TD_NAK) ? "-NAK" : "",
73202ac6454SAndrew Thompson 	    (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
73302ac6454SAndrew Thompson 	    (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
73402ac6454SAndrew Thompson 	    (td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
73502ac6454SAndrew Thompson 	    (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
73602ac6454SAndrew Thompson 	    (td_status & UHCI_TD_IOC) ? "-IOC" : "",
73702ac6454SAndrew Thompson 	    (td_status & UHCI_TD_IOS) ? "-IOS" : "",
73802ac6454SAndrew Thompson 	    (td_status & UHCI_TD_LS) ? "-LS" : "",
73902ac6454SAndrew Thompson 	    (td_status & UHCI_TD_SPD) ? "-SPD" : "",
74002ac6454SAndrew Thompson 	    UHCI_TD_GET_ERRCNT(td_status),
74102ac6454SAndrew Thompson 	    UHCI_TD_GET_ACTLEN(td_status),
74202ac6454SAndrew Thompson 	    UHCI_TD_GET_PID(td_token),
74302ac6454SAndrew Thompson 	    UHCI_TD_GET_DEVADDR(td_token),
74402ac6454SAndrew Thompson 	    UHCI_TD_GET_ENDPT(td_token),
74502ac6454SAndrew Thompson 	    UHCI_TD_GET_DT(td_token),
74602ac6454SAndrew Thompson 	    UHCI_TD_GET_MAXLEN(td_token));
74702ac6454SAndrew Thompson 
74802ac6454SAndrew Thompson 	return (temp);
74902ac6454SAndrew Thompson }
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson static uint8_t
75202ac6454SAndrew Thompson uhci_dump_qh(uhci_qh_t *sqh)
75302ac6454SAndrew Thompson {
75402ac6454SAndrew Thompson 	uint8_t temp;
75502ac6454SAndrew Thompson 	uint32_t qh_h_next;
75602ac6454SAndrew Thompson 	uint32_t qh_e_next;
75702ac6454SAndrew Thompson 
75802ac6454SAndrew Thompson 	usb2_pc_cpu_invalidate(sqh->page_cache);
75902ac6454SAndrew Thompson 
76002ac6454SAndrew Thompson 	qh_h_next = le32toh(sqh->qh_h_next);
76102ac6454SAndrew Thompson 	qh_e_next = le32toh(sqh->qh_e_next);
76202ac6454SAndrew Thompson 
76302ac6454SAndrew Thompson 	DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
76402ac6454SAndrew Thompson 	    le32toh(sqh->qh_self), qh_h_next, qh_e_next);
76502ac6454SAndrew Thompson 
76602ac6454SAndrew Thompson 	temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
76702ac6454SAndrew Thompson 	    (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
76802ac6454SAndrew Thompson 
76902ac6454SAndrew Thompson 	return (temp);
77002ac6454SAndrew Thompson }
77102ac6454SAndrew Thompson 
77202ac6454SAndrew Thompson static void
77302ac6454SAndrew Thompson uhci_dump_all(uhci_softc_t *sc)
77402ac6454SAndrew Thompson {
77502ac6454SAndrew Thompson 	uhci_dumpregs(sc);
77602ac6454SAndrew Thompson 	uhci_dump_qh(sc->sc_ls_ctl_p_last);
77702ac6454SAndrew Thompson 	uhci_dump_qh(sc->sc_fs_ctl_p_last);
77802ac6454SAndrew Thompson 	uhci_dump_qh(sc->sc_bulk_p_last);
77902ac6454SAndrew Thompson 	uhci_dump_qh(sc->sc_last_qh_p);
78002ac6454SAndrew Thompson }
78102ac6454SAndrew Thompson 
78202ac6454SAndrew Thompson static void
78302ac6454SAndrew Thompson uhci_dump_qhs(uhci_qh_t *sqh)
78402ac6454SAndrew Thompson {
78502ac6454SAndrew Thompson 	uint8_t temp;
78602ac6454SAndrew Thompson 
78702ac6454SAndrew Thompson 	temp = uhci_dump_qh(sqh);
78802ac6454SAndrew Thompson 
78902ac6454SAndrew Thompson 	/*
79002ac6454SAndrew Thompson 	 * uhci_dump_qhs displays all the QHs and TDs from the given QH
79102ac6454SAndrew Thompson 	 * onwards Traverses sideways first, then down.
79202ac6454SAndrew Thompson 	 *
79302ac6454SAndrew Thompson 	 * QH1 QH2 No QH TD2.1 TD2.2 TD1.1 etc.
79402ac6454SAndrew Thompson 	 *
79502ac6454SAndrew Thompson 	 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
79602ac6454SAndrew Thompson 	 */
79702ac6454SAndrew Thompson 
79802ac6454SAndrew Thompson 	if (temp & 1)
79902ac6454SAndrew Thompson 		uhci_dump_qhs(sqh->h_next);
80002ac6454SAndrew Thompson 	else
80102ac6454SAndrew Thompson 		DPRINTF("No QH\n");
80202ac6454SAndrew Thompson 
80302ac6454SAndrew Thompson 	if (temp & 2)
80402ac6454SAndrew Thompson 		uhci_dump_tds(sqh->e_next);
80502ac6454SAndrew Thompson 	else
80602ac6454SAndrew Thompson 		DPRINTF("No TD\n");
80702ac6454SAndrew Thompson }
80802ac6454SAndrew Thompson 
80902ac6454SAndrew Thompson static void
81002ac6454SAndrew Thompson uhci_dump_tds(uhci_td_t *td)
81102ac6454SAndrew Thompson {
81202ac6454SAndrew Thompson 	for (;
81302ac6454SAndrew Thompson 	    td != NULL;
81402ac6454SAndrew Thompson 	    td = td->obj_next) {
81502ac6454SAndrew Thompson 		if (uhci_dump_td(td)) {
81602ac6454SAndrew Thompson 			break;
81702ac6454SAndrew Thompson 		}
81802ac6454SAndrew Thompson 	}
81902ac6454SAndrew Thompson }
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson #endif
82202ac6454SAndrew Thompson 
82302ac6454SAndrew Thompson /*
82402ac6454SAndrew Thompson  * Let the last QH loop back to the full speed control transfer QH.
82502ac6454SAndrew Thompson  * This is what intel calls "bandwidth reclamation" and improves
82602ac6454SAndrew Thompson  * USB performance a lot for some devices.
82702ac6454SAndrew Thompson  * If we are already looping, just count it.
82802ac6454SAndrew Thompson  */
82902ac6454SAndrew Thompson static void
83002ac6454SAndrew Thompson uhci_add_loop(uhci_softc_t *sc)
83102ac6454SAndrew Thompson {
83202ac6454SAndrew Thompson 	struct uhci_qh *qh_lst;
83302ac6454SAndrew Thompson 	struct uhci_qh *qh_rec;
83402ac6454SAndrew Thompson 
83502ac6454SAndrew Thompson #if USB_DEBUG
83602ac6454SAndrew Thompson 	if (uhcinoloop) {
83702ac6454SAndrew Thompson 		return;
83802ac6454SAndrew Thompson 	}
83902ac6454SAndrew Thompson #endif
84002ac6454SAndrew Thompson 	if (++(sc->sc_loops) == 1) {
84102ac6454SAndrew Thompson 		DPRINTFN(6, "add\n");
84202ac6454SAndrew Thompson 
84302ac6454SAndrew Thompson 		qh_lst = sc->sc_last_qh_p;
84402ac6454SAndrew Thompson 		qh_rec = sc->sc_reclaim_qh_p;
84502ac6454SAndrew Thompson 
84602ac6454SAndrew Thompson 		/* NOTE: we don't loop back the soft pointer */
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 		qh_lst->qh_h_next = qh_rec->qh_self;
84902ac6454SAndrew Thompson 		usb2_pc_cpu_flush(qh_lst->page_cache);
85002ac6454SAndrew Thompson 	}
85102ac6454SAndrew Thompson }
85202ac6454SAndrew Thompson 
85302ac6454SAndrew Thompson static void
85402ac6454SAndrew Thompson uhci_rem_loop(uhci_softc_t *sc)
85502ac6454SAndrew Thompson {
85602ac6454SAndrew Thompson 	struct uhci_qh *qh_lst;
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson #if USB_DEBUG
85902ac6454SAndrew Thompson 	if (uhcinoloop) {
86002ac6454SAndrew Thompson 		return;
86102ac6454SAndrew Thompson 	}
86202ac6454SAndrew Thompson #endif
86302ac6454SAndrew Thompson 	if (--(sc->sc_loops) == 0) {
86402ac6454SAndrew Thompson 		DPRINTFN(6, "remove\n");
86502ac6454SAndrew Thompson 
86602ac6454SAndrew Thompson 		qh_lst = sc->sc_last_qh_p;
86702ac6454SAndrew Thompson 		qh_lst->qh_h_next = htole32(UHCI_PTR_T);
86802ac6454SAndrew Thompson 		usb2_pc_cpu_flush(qh_lst->page_cache);
86902ac6454SAndrew Thompson 	}
87002ac6454SAndrew Thompson }
87102ac6454SAndrew Thompson 
87202ac6454SAndrew Thompson static void
87302ac6454SAndrew Thompson uhci_transfer_intr_enqueue(struct usb2_xfer *xfer)
87402ac6454SAndrew Thompson {
87502ac6454SAndrew Thompson 	/* check for early completion */
87602ac6454SAndrew Thompson 	if (uhci_check_transfer(xfer)) {
87702ac6454SAndrew Thompson 		return;
87802ac6454SAndrew Thompson 	}
87902ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
88002ac6454SAndrew Thompson 	usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson 	/* start timeout, if any */
88302ac6454SAndrew Thompson 	if (xfer->timeout != 0) {
88402ac6454SAndrew Thompson 		usb2_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
88502ac6454SAndrew Thompson 	}
88602ac6454SAndrew Thompson }
88702ac6454SAndrew Thompson 
88802ac6454SAndrew Thompson #define	UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
88902ac6454SAndrew Thompson static uhci_td_t *
89002ac6454SAndrew Thompson _uhci_append_td(uhci_td_t *std, uhci_td_t *last)
89102ac6454SAndrew Thompson {
89202ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
89302ac6454SAndrew Thompson 
89402ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
89502ac6454SAndrew Thompson 
89602ac6454SAndrew Thompson 	std->next = last->next;
89702ac6454SAndrew Thompson 	std->td_next = last->td_next;
89802ac6454SAndrew Thompson 
89902ac6454SAndrew Thompson 	std->prev = last;
90002ac6454SAndrew Thompson 
90102ac6454SAndrew Thompson 	usb2_pc_cpu_flush(std->page_cache);
90202ac6454SAndrew Thompson 
90302ac6454SAndrew Thompson 	/*
90402ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
90502ac6454SAndrew Thompson 	 */
90602ac6454SAndrew Thompson 	last->next = std;
90702ac6454SAndrew Thompson 	last->td_next = std->td_self;
90802ac6454SAndrew Thompson 
90902ac6454SAndrew Thompson 	usb2_pc_cpu_flush(last->page_cache);
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson 	return (std);
91202ac6454SAndrew Thompson }
91302ac6454SAndrew Thompson 
91402ac6454SAndrew Thompson #define	UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
91502ac6454SAndrew Thompson static uhci_qh_t *
91602ac6454SAndrew Thompson _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
91702ac6454SAndrew Thompson {
91802ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", sqh, last);
91902ac6454SAndrew Thompson 
92002ac6454SAndrew Thompson 	if (sqh->h_prev != NULL) {
92102ac6454SAndrew Thompson 		/* should not happen */
92202ac6454SAndrew Thompson 		DPRINTFN(0, "QH already linked!\n");
92302ac6454SAndrew Thompson 		return (last);
92402ac6454SAndrew Thompson 	}
92502ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
92602ac6454SAndrew Thompson 
92702ac6454SAndrew Thompson 	sqh->h_next = last->h_next;
92802ac6454SAndrew Thompson 	sqh->qh_h_next = last->qh_h_next;
92902ac6454SAndrew Thompson 
93002ac6454SAndrew Thompson 	sqh->h_prev = last;
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 	usb2_pc_cpu_flush(sqh->page_cache);
93302ac6454SAndrew Thompson 
93402ac6454SAndrew Thompson 	/*
93502ac6454SAndrew Thompson 	 * The "last->h_next->h_prev" is never followed:
93602ac6454SAndrew Thompson 	 *
93702ac6454SAndrew Thompson 	 * "sqh->h_next->h_prev" = sqh;
93802ac6454SAndrew Thompson 	 */
93902ac6454SAndrew Thompson 
94002ac6454SAndrew Thompson 	last->h_next = sqh;
94102ac6454SAndrew Thompson 	last->qh_h_next = sqh->qh_self;
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 	usb2_pc_cpu_flush(last->page_cache);
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson 	return (sqh);
94602ac6454SAndrew Thompson }
94702ac6454SAndrew Thompson 
94802ac6454SAndrew Thompson /**/
94902ac6454SAndrew Thompson 
95002ac6454SAndrew Thompson #define	UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
95102ac6454SAndrew Thompson static uhci_td_t *
95202ac6454SAndrew Thompson _uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
95302ac6454SAndrew Thompson {
95402ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
95502ac6454SAndrew Thompson 
95602ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson 	std->prev->next = std->next;
95902ac6454SAndrew Thompson 	std->prev->td_next = std->td_next;
96002ac6454SAndrew Thompson 
96102ac6454SAndrew Thompson 	usb2_pc_cpu_flush(std->prev->page_cache);
96202ac6454SAndrew Thompson 
96302ac6454SAndrew Thompson 	if (std->next) {
96402ac6454SAndrew Thompson 		std->next->prev = std->prev;
96502ac6454SAndrew Thompson 		usb2_pc_cpu_flush(std->next->page_cache);
96602ac6454SAndrew Thompson 	}
96702ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
96802ac6454SAndrew Thompson }
96902ac6454SAndrew Thompson 
97002ac6454SAndrew Thompson #define	UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
97102ac6454SAndrew Thompson static uhci_qh_t *
97202ac6454SAndrew Thompson _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
97302ac6454SAndrew Thompson {
97402ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", sqh, last);
97502ac6454SAndrew Thompson 
97602ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson 	/* only remove if not removed from a queue */
97902ac6454SAndrew Thompson 	if (sqh->h_prev) {
98002ac6454SAndrew Thompson 
98102ac6454SAndrew Thompson 		sqh->h_prev->h_next = sqh->h_next;
98202ac6454SAndrew Thompson 		sqh->h_prev->qh_h_next = sqh->qh_h_next;
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 		usb2_pc_cpu_flush(sqh->h_prev->page_cache);
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson 		if (sqh->h_next) {
98702ac6454SAndrew Thompson 			sqh->h_next->h_prev = sqh->h_prev;
98802ac6454SAndrew Thompson 			usb2_pc_cpu_flush(sqh->h_next->page_cache);
98902ac6454SAndrew Thompson 		}
99002ac6454SAndrew Thompson 		last = ((last == sqh) ? sqh->h_prev : last);
99102ac6454SAndrew Thompson 
99202ac6454SAndrew Thompson 		sqh->h_prev = 0;
99302ac6454SAndrew Thompson 
99402ac6454SAndrew Thompson 		usb2_pc_cpu_flush(sqh->page_cache);
99502ac6454SAndrew Thompson 	}
99602ac6454SAndrew Thompson 	return (last);
99702ac6454SAndrew Thompson }
99802ac6454SAndrew Thompson 
99902ac6454SAndrew Thompson static void
100002ac6454SAndrew Thompson uhci_isoc_done(uhci_softc_t *sc, struct usb2_xfer *xfer)
100102ac6454SAndrew Thompson {
100202ac6454SAndrew Thompson 	struct usb2_page_search res;
100302ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
100402ac6454SAndrew Thompson 	uint32_t status;
100502ac6454SAndrew Thompson 	uint32_t offset = 0;
100602ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
100702ac6454SAndrew Thompson 	uint16_t len = 0;
100802ac6454SAndrew Thompson 	uhci_td_t *td = xfer->td_transfer_first;
100902ac6454SAndrew Thompson 	uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
101002ac6454SAndrew Thompson 
101102ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
101202ac6454SAndrew Thompson 	    xfer, xfer->pipe);
101302ac6454SAndrew Thompson 
101402ac6454SAndrew Thompson 	/* sync any DMA memory before doing fixups */
101502ac6454SAndrew Thompson 
101602ac6454SAndrew Thompson 	usb2_bdma_post_sync(xfer);
101702ac6454SAndrew Thompson 
101802ac6454SAndrew Thompson 	while (nframes--) {
101902ac6454SAndrew Thompson 		if (td == NULL) {
102002ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
102102ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
102202ac6454SAndrew Thompson 		}
102302ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
102402ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_p_last[0];
102502ac6454SAndrew Thompson 		}
102602ac6454SAndrew Thompson #if USB_DEBUG
102702ac6454SAndrew Thompson 		if (uhcidebug > 5) {
102802ac6454SAndrew Thompson 			DPRINTF("isoc TD\n");
102902ac6454SAndrew Thompson 			uhci_dump_td(td);
103002ac6454SAndrew Thompson 		}
103102ac6454SAndrew Thompson #endif
103202ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
103302ac6454SAndrew Thompson 		status = le32toh(td->td_status);
103402ac6454SAndrew Thompson 
103502ac6454SAndrew Thompson 		len = UHCI_TD_GET_ACTLEN(status);
103602ac6454SAndrew Thompson 
103702ac6454SAndrew Thompson 		if (len > *plen) {
103802ac6454SAndrew Thompson 			len = *plen;
103902ac6454SAndrew Thompson 		}
104002ac6454SAndrew Thompson 		if (td->fix_pc) {
104102ac6454SAndrew Thompson 
104202ac6454SAndrew Thompson 			usb2_get_page(td->fix_pc, 0, &res);
104302ac6454SAndrew Thompson 
104402ac6454SAndrew Thompson 			/* copy data from fixup location to real location */
104502ac6454SAndrew Thompson 
104602ac6454SAndrew Thompson 			usb2_pc_cpu_invalidate(td->fix_pc);
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, offset,
104902ac6454SAndrew Thompson 			    res.buffer, len);
105002ac6454SAndrew Thompson 		}
105102ac6454SAndrew Thompson 		offset += *plen;
105202ac6454SAndrew Thompson 
105302ac6454SAndrew Thompson 		*plen = len;
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson 		/* remove TD from schedule */
105602ac6454SAndrew Thompson 		UHCI_REMOVE_TD(td, *pp_last);
105702ac6454SAndrew Thompson 
105802ac6454SAndrew Thompson 		pp_last++;
105902ac6454SAndrew Thompson 		plen++;
106002ac6454SAndrew Thompson 		td = td->obj_next;
106102ac6454SAndrew Thompson 	}
106202ac6454SAndrew Thompson 
106302ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
106402ac6454SAndrew Thompson }
106502ac6454SAndrew Thompson 
106602ac6454SAndrew Thompson static usb2_error_t
106702ac6454SAndrew Thompson uhci_non_isoc_done_sub(struct usb2_xfer *xfer)
106802ac6454SAndrew Thompson {
106902ac6454SAndrew Thompson 	struct usb2_page_search res;
107002ac6454SAndrew Thompson 	uhci_td_t *td;
107102ac6454SAndrew Thompson 	uhci_td_t *td_alt_next;
107202ac6454SAndrew Thompson 	uint32_t status;
107302ac6454SAndrew Thompson 	uint32_t token;
107402ac6454SAndrew Thompson 	uint16_t len;
107502ac6454SAndrew Thompson 
107602ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
107702ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
107802ac6454SAndrew Thompson 
107902ac6454SAndrew Thompson 	if (xfer->aframes != xfer->nframes) {
108002ac6454SAndrew Thompson 		xfer->frlengths[xfer->aframes] = 0;
108102ac6454SAndrew Thompson 	}
108202ac6454SAndrew Thompson 	while (1) {
108302ac6454SAndrew Thompson 
108402ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
108502ac6454SAndrew Thompson 		status = le32toh(td->td_status);
108602ac6454SAndrew Thompson 		token = le32toh(td->td_token);
108702ac6454SAndrew Thompson 
108802ac6454SAndrew Thompson 		/*
108902ac6454SAndrew Thompson 	         * Verify the status and add
109002ac6454SAndrew Thompson 	         * up the actual length:
109102ac6454SAndrew Thompson 	         */
109202ac6454SAndrew Thompson 
109302ac6454SAndrew Thompson 		len = UHCI_TD_GET_ACTLEN(status);
109402ac6454SAndrew Thompson 		if (len > td->len) {
109502ac6454SAndrew Thompson 			/* should not happen */
109602ac6454SAndrew Thompson 			DPRINTF("Invalid status length, "
109702ac6454SAndrew Thompson 			    "0x%04x/0x%04x bytes\n", len, td->len);
109802ac6454SAndrew Thompson 			status |= UHCI_TD_STALLED;
109902ac6454SAndrew Thompson 
110002ac6454SAndrew Thompson 		} else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
110102ac6454SAndrew Thompson 
110202ac6454SAndrew Thompson 			if (td->fix_pc) {
110302ac6454SAndrew Thompson 
110402ac6454SAndrew Thompson 				usb2_get_page(td->fix_pc, 0, &res);
110502ac6454SAndrew Thompson 
110602ac6454SAndrew Thompson 				/*
110702ac6454SAndrew Thompson 				 * copy data from fixup location to real
110802ac6454SAndrew Thompson 				 * location
110902ac6454SAndrew Thompson 				 */
111002ac6454SAndrew Thompson 
111102ac6454SAndrew Thompson 				usb2_pc_cpu_invalidate(td->fix_pc);
111202ac6454SAndrew Thompson 
111302ac6454SAndrew Thompson 				usb2_copy_in(xfer->frbuffers + xfer->aframes,
111402ac6454SAndrew Thompson 				    xfer->frlengths[xfer->aframes], res.buffer, len);
111502ac6454SAndrew Thompson 			}
111602ac6454SAndrew Thompson 			/* update actual length */
111702ac6454SAndrew Thompson 
111802ac6454SAndrew Thompson 			xfer->frlengths[xfer->aframes] += len;
111902ac6454SAndrew Thompson 		}
112002ac6454SAndrew Thompson 		/* Check for last transfer */
112102ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
112202ac6454SAndrew Thompson 			td = NULL;
112302ac6454SAndrew Thompson 			break;
112402ac6454SAndrew Thompson 		}
112502ac6454SAndrew Thompson 		if (status & UHCI_TD_STALLED) {
112602ac6454SAndrew Thompson 			/* the transfer is finished */
112702ac6454SAndrew Thompson 			td = NULL;
112802ac6454SAndrew Thompson 			break;
112902ac6454SAndrew Thompson 		}
113002ac6454SAndrew Thompson 		/* Check for short transfer */
113102ac6454SAndrew Thompson 		if (len != td->len) {
113202ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
113302ac6454SAndrew Thompson 				/* follow alt next */
113402ac6454SAndrew Thompson 				td = td->alt_next;
113502ac6454SAndrew Thompson 			} else {
113602ac6454SAndrew Thompson 				/* the transfer is finished */
113702ac6454SAndrew Thompson 				td = NULL;
113802ac6454SAndrew Thompson 			}
113902ac6454SAndrew Thompson 			break;
114002ac6454SAndrew Thompson 		}
114102ac6454SAndrew Thompson 		td = td->obj_next;
114202ac6454SAndrew Thompson 
114302ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
114402ac6454SAndrew Thompson 			/* this USB frame is complete */
114502ac6454SAndrew Thompson 			break;
114602ac6454SAndrew Thompson 		}
114702ac6454SAndrew Thompson 	}
114802ac6454SAndrew Thompson 
114902ac6454SAndrew Thompson 	/* update transfer cache */
115002ac6454SAndrew Thompson 
115102ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
115202ac6454SAndrew Thompson 
115302ac6454SAndrew Thompson 	/* update data toggle */
115402ac6454SAndrew Thompson 
115502ac6454SAndrew Thompson 	xfer->pipe->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
115602ac6454SAndrew Thompson 
115702ac6454SAndrew Thompson #if USB_DEBUG
115802ac6454SAndrew Thompson 	if (status & UHCI_TD_ERROR) {
115902ac6454SAndrew Thompson 		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
116002ac6454SAndrew Thompson 		    "status=%s%s%s%s%s%s%s%s%s%s%s\n",
116102ac6454SAndrew Thompson 		    xfer->address, xfer->endpoint, xfer->aframes,
116202ac6454SAndrew Thompson 		    (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
116302ac6454SAndrew Thompson 		    (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
116402ac6454SAndrew Thompson 		    (status & UHCI_TD_NAK) ? "[NAK]" : "",
116502ac6454SAndrew Thompson 		    (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
116602ac6454SAndrew Thompson 		    (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
116702ac6454SAndrew Thompson 		    (status & UHCI_TD_STALLED) ? "[STALLED]" : "",
116802ac6454SAndrew Thompson 		    (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
116902ac6454SAndrew Thompson 		    (status & UHCI_TD_IOC) ? "[IOC]" : "",
117002ac6454SAndrew Thompson 		    (status & UHCI_TD_IOS) ? "[IOS]" : "",
117102ac6454SAndrew Thompson 		    (status & UHCI_TD_LS) ? "[LS]" : "",
117202ac6454SAndrew Thompson 		    (status & UHCI_TD_SPD) ? "[SPD]" : "");
117302ac6454SAndrew Thompson 	}
117402ac6454SAndrew Thompson #endif
117502ac6454SAndrew Thompson 	return (status & UHCI_TD_STALLED) ?
117602ac6454SAndrew Thompson 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION;
117702ac6454SAndrew Thompson }
117802ac6454SAndrew Thompson 
117902ac6454SAndrew Thompson static void
118002ac6454SAndrew Thompson uhci_non_isoc_done(struct usb2_xfer *xfer)
118102ac6454SAndrew Thompson {
118202ac6454SAndrew Thompson 	usb2_error_t err = 0;
118302ac6454SAndrew Thompson 
118402ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
118502ac6454SAndrew Thompson 	    xfer, xfer->pipe);
118602ac6454SAndrew Thompson 
118702ac6454SAndrew Thompson #if USB_DEBUG
118802ac6454SAndrew Thompson 	if (uhcidebug > 10) {
118902ac6454SAndrew Thompson 		uhci_dump_tds(xfer->td_transfer_first);
119002ac6454SAndrew Thompson 	}
119102ac6454SAndrew Thompson #endif
119202ac6454SAndrew Thompson 
119302ac6454SAndrew Thompson 	/* sync any DMA memory before doing fixups */
119402ac6454SAndrew Thompson 
119502ac6454SAndrew Thompson 	usb2_bdma_post_sync(xfer);
119602ac6454SAndrew Thompson 
119702ac6454SAndrew Thompson 	/* reset scanner */
119802ac6454SAndrew Thompson 
119902ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
120002ac6454SAndrew Thompson 
120102ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
120202ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
120302ac6454SAndrew Thompson 
120402ac6454SAndrew Thompson 			err = uhci_non_isoc_done_sub(xfer);
120502ac6454SAndrew Thompson 		}
120602ac6454SAndrew Thompson 		xfer->aframes = 1;
120702ac6454SAndrew Thompson 
120802ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
120902ac6454SAndrew Thompson 			goto done;
121002ac6454SAndrew Thompson 		}
121102ac6454SAndrew Thompson 	}
121202ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
121302ac6454SAndrew Thompson 
121402ac6454SAndrew Thompson 		err = uhci_non_isoc_done_sub(xfer);
121502ac6454SAndrew Thompson 		xfer->aframes++;
121602ac6454SAndrew Thompson 
121702ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
121802ac6454SAndrew Thompson 			goto done;
121902ac6454SAndrew Thompson 		}
122002ac6454SAndrew Thompson 	}
122102ac6454SAndrew Thompson 
122202ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
122302ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
122402ac6454SAndrew Thompson 
122502ac6454SAndrew Thompson 		err = uhci_non_isoc_done_sub(xfer);
122602ac6454SAndrew Thompson 	}
122702ac6454SAndrew Thompson done:
122802ac6454SAndrew Thompson 	uhci_device_done(xfer, err);
122902ac6454SAndrew Thompson }
123002ac6454SAndrew Thompson 
123102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
123202ac6454SAndrew Thompson  *	uhci_check_transfer_sub
123302ac6454SAndrew Thompson  *
123402ac6454SAndrew Thompson  * The main purpose of this function is to update the data-toggle
123502ac6454SAndrew Thompson  * in case it is wrong.
123602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
123702ac6454SAndrew Thompson static void
123802ac6454SAndrew Thompson uhci_check_transfer_sub(struct usb2_xfer *xfer)
123902ac6454SAndrew Thompson {
124002ac6454SAndrew Thompson 	uhci_qh_t *qh;
124102ac6454SAndrew Thompson 	uhci_td_t *td;
124202ac6454SAndrew Thompson 	uhci_td_t *td_alt_next;
124302ac6454SAndrew Thompson 
124402ac6454SAndrew Thompson 	uint32_t td_token;
124502ac6454SAndrew Thompson 	uint32_t td_self;
124602ac6454SAndrew Thompson 
124702ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
124802ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
124902ac6454SAndrew Thompson 
125002ac6454SAndrew Thompson 	td_token = td->obj_next->td_token;
125102ac6454SAndrew Thompson 	td = td->alt_next;
125202ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
125302ac6454SAndrew Thompson 	td_self = td->td_self;
125402ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
125502ac6454SAndrew Thompson 
125602ac6454SAndrew Thompson 	if ((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))) {
125702ac6454SAndrew Thompson 
125802ac6454SAndrew Thompson 		/*
125902ac6454SAndrew Thompson 	         * The data toggle is wrong and
126002ac6454SAndrew Thompson 	         * we need to switch it !
126102ac6454SAndrew Thompson 	         */
126202ac6454SAndrew Thompson 
126302ac6454SAndrew Thompson 		while (1) {
126402ac6454SAndrew Thompson 
126502ac6454SAndrew Thompson 			td->td_token ^= htole32(UHCI_TD_SET_DT(1));
126602ac6454SAndrew Thompson 			usb2_pc_cpu_flush(td->page_cache);
126702ac6454SAndrew Thompson 
126802ac6454SAndrew Thompson 			if (td == xfer->td_transfer_last) {
126902ac6454SAndrew Thompson 				/* last transfer */
127002ac6454SAndrew Thompson 				break;
127102ac6454SAndrew Thompson 			}
127202ac6454SAndrew Thompson 			td = td->obj_next;
127302ac6454SAndrew Thompson 
127402ac6454SAndrew Thompson 			if (td->alt_next != td_alt_next) {
127502ac6454SAndrew Thompson 				/* next frame */
127602ac6454SAndrew Thompson 				break;
127702ac6454SAndrew Thompson 			}
127802ac6454SAndrew Thompson 		}
127902ac6454SAndrew Thompson 	}
128002ac6454SAndrew Thompson 	/* update the QH */
128102ac6454SAndrew Thompson 	qh->qh_e_next = td_self;
128202ac6454SAndrew Thompson 	usb2_pc_cpu_flush(qh->page_cache);
128302ac6454SAndrew Thompson 
128402ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p following alt next\n", xfer);
128502ac6454SAndrew Thompson }
128602ac6454SAndrew Thompson 
128702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
128802ac6454SAndrew Thompson  *	uhci_check_transfer
128902ac6454SAndrew Thompson  *
129002ac6454SAndrew Thompson  * Return values:
129102ac6454SAndrew Thompson  *    0: USB transfer is not finished
129202ac6454SAndrew Thompson  * Else: USB transfer is finished
129302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
129402ac6454SAndrew Thompson static uint8_t
129502ac6454SAndrew Thompson uhci_check_transfer(struct usb2_xfer *xfer)
129602ac6454SAndrew Thompson {
129702ac6454SAndrew Thompson 	uint32_t status;
129802ac6454SAndrew Thompson 	uint32_t token;
129902ac6454SAndrew Thompson 	uhci_td_t *td;
130002ac6454SAndrew Thompson 
130102ac6454SAndrew Thompson 	DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
130202ac6454SAndrew Thompson 
130302ac6454SAndrew Thompson 	if (xfer->pipe->methods == &uhci_device_isoc_methods) {
130402ac6454SAndrew Thompson 		/* isochronous transfer */
130502ac6454SAndrew Thompson 
130602ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
130702ac6454SAndrew Thompson 
130802ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
130902ac6454SAndrew Thompson 		status = le32toh(td->td_status);
131002ac6454SAndrew Thompson 
131102ac6454SAndrew Thompson 		/* check also if the first is complete */
131202ac6454SAndrew Thompson 
131302ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
131402ac6454SAndrew Thompson 
131502ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(td->page_cache);
131602ac6454SAndrew Thompson 		status |= le32toh(td->td_status);
131702ac6454SAndrew Thompson 
131802ac6454SAndrew Thompson 		if (!(status & UHCI_TD_ACTIVE)) {
131902ac6454SAndrew Thompson 			uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
132002ac6454SAndrew Thompson 			goto transferred;
132102ac6454SAndrew Thompson 		}
132202ac6454SAndrew Thompson 	} else {
132302ac6454SAndrew Thompson 		/* non-isochronous transfer */
132402ac6454SAndrew Thompson 
132502ac6454SAndrew Thompson 		/*
132602ac6454SAndrew Thompson 		 * check whether there is an error somewhere
132702ac6454SAndrew Thompson 		 * in the middle, or whether there was a short
132802ac6454SAndrew Thompson 		 * packet (SPD and not ACTIVE)
132902ac6454SAndrew Thompson 		 */
133002ac6454SAndrew Thompson 		td = xfer->td_transfer_cache;
133102ac6454SAndrew Thompson 
133202ac6454SAndrew Thompson 		while (1) {
133302ac6454SAndrew Thompson 			usb2_pc_cpu_invalidate(td->page_cache);
133402ac6454SAndrew Thompson 			status = le32toh(td->td_status);
133502ac6454SAndrew Thompson 			token = le32toh(td->td_token);
133602ac6454SAndrew Thompson 
133702ac6454SAndrew Thompson 			/*
133802ac6454SAndrew Thompson 			 * if there is an active TD the transfer isn't done
133902ac6454SAndrew Thompson 			 */
134002ac6454SAndrew Thompson 			if (status & UHCI_TD_ACTIVE) {
134102ac6454SAndrew Thompson 				/* update cache */
134202ac6454SAndrew Thompson 				xfer->td_transfer_cache = td;
134302ac6454SAndrew Thompson 				goto done;
134402ac6454SAndrew Thompson 			}
134502ac6454SAndrew Thompson 			/*
134602ac6454SAndrew Thompson 			 * last transfer descriptor makes the transfer done
134702ac6454SAndrew Thompson 			 */
134802ac6454SAndrew Thompson 			if (((void *)td) == xfer->td_transfer_last) {
134902ac6454SAndrew Thompson 				break;
135002ac6454SAndrew Thompson 			}
135102ac6454SAndrew Thompson 			/*
135202ac6454SAndrew Thompson 			 * any kind of error makes the transfer done
135302ac6454SAndrew Thompson 			 */
135402ac6454SAndrew Thompson 			if (status & UHCI_TD_STALLED) {
135502ac6454SAndrew Thompson 				break;
135602ac6454SAndrew Thompson 			}
135702ac6454SAndrew Thompson 			/*
135802ac6454SAndrew Thompson 			 * check if we reached the last packet
135902ac6454SAndrew Thompson 			 * or if there is a short packet:
136002ac6454SAndrew Thompson 			 */
136102ac6454SAndrew Thompson 			if ((td->td_next == htole32(UHCI_PTR_T)) ||
136202ac6454SAndrew Thompson 			    (UHCI_TD_GET_ACTLEN(status) < td->len)) {
136302ac6454SAndrew Thompson 
136402ac6454SAndrew Thompson 				if (xfer->flags_int.short_frames_ok) {
136502ac6454SAndrew Thompson 					/* follow alt next */
136602ac6454SAndrew Thompson 					if (td->alt_next) {
136702ac6454SAndrew Thompson 						/* update cache */
136802ac6454SAndrew Thompson 						xfer->td_transfer_cache = td;
136902ac6454SAndrew Thompson 						uhci_check_transfer_sub(xfer);
137002ac6454SAndrew Thompson 						goto done;
137102ac6454SAndrew Thompson 					}
137202ac6454SAndrew Thompson 				}
137302ac6454SAndrew Thompson 				/* transfer is done */
137402ac6454SAndrew Thompson 				break;
137502ac6454SAndrew Thompson 			}
137602ac6454SAndrew Thompson 			td = td->obj_next;
137702ac6454SAndrew Thompson 		}
137802ac6454SAndrew Thompson 		uhci_non_isoc_done(xfer);
137902ac6454SAndrew Thompson 		goto transferred;
138002ac6454SAndrew Thompson 	}
138102ac6454SAndrew Thompson 
138202ac6454SAndrew Thompson done:
138302ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
138402ac6454SAndrew Thompson 	return (0);
138502ac6454SAndrew Thompson 
138602ac6454SAndrew Thompson transferred:
138702ac6454SAndrew Thompson 	return (1);
138802ac6454SAndrew Thompson }
138902ac6454SAndrew Thompson 
139002ac6454SAndrew Thompson static void
139102ac6454SAndrew Thompson uhci_interrupt_poll(uhci_softc_t *sc)
139202ac6454SAndrew Thompson {
139302ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
139402ac6454SAndrew Thompson 
139502ac6454SAndrew Thompson repeat:
139602ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
139702ac6454SAndrew Thompson 		/*
139802ac6454SAndrew Thompson 		 * check if transfer is transferred
139902ac6454SAndrew Thompson 		 */
140002ac6454SAndrew Thompson 		if (uhci_check_transfer(xfer)) {
140102ac6454SAndrew Thompson 			/* queue has been modified */
140202ac6454SAndrew Thompson 			goto repeat;
140302ac6454SAndrew Thompson 		}
140402ac6454SAndrew Thompson 	}
140502ac6454SAndrew Thompson }
140602ac6454SAndrew Thompson 
140702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
140802ac6454SAndrew Thompson  *	uhci_interrupt - UHCI interrupt handler
140902ac6454SAndrew Thompson  *
141002ac6454SAndrew Thompson  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
141102ac6454SAndrew Thompson  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
141202ac6454SAndrew Thompson  * is present !
141302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
141402ac6454SAndrew Thompson void
141502ac6454SAndrew Thompson uhci_interrupt(uhci_softc_t *sc)
141602ac6454SAndrew Thompson {
141702ac6454SAndrew Thompson 	uint32_t status;
141802ac6454SAndrew Thompson 
141902ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
142002ac6454SAndrew Thompson 
142102ac6454SAndrew Thompson 	DPRINTFN(16, "real interrupt\n");
142202ac6454SAndrew Thompson 
142302ac6454SAndrew Thompson #if USB_DEBUG
142402ac6454SAndrew Thompson 	if (uhcidebug > 15) {
142502ac6454SAndrew Thompson 		uhci_dumpregs(sc);
142602ac6454SAndrew Thompson 	}
142702ac6454SAndrew Thompson #endif
142802ac6454SAndrew Thompson 	status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
142902ac6454SAndrew Thompson 	if (status == 0) {
143002ac6454SAndrew Thompson 		/* the interrupt was not for us */
143102ac6454SAndrew Thompson 		goto done;
143202ac6454SAndrew Thompson 	}
143302ac6454SAndrew Thompson 	if (status & (UHCI_STS_RD | UHCI_STS_HSE |
143402ac6454SAndrew Thompson 	    UHCI_STS_HCPE | UHCI_STS_HCH)) {
143502ac6454SAndrew Thompson 
143602ac6454SAndrew Thompson 		if (status & UHCI_STS_RD) {
143702ac6454SAndrew Thompson #if USB_DEBUG
143802ac6454SAndrew Thompson 			printf("%s: resume detect\n",
143902ac6454SAndrew Thompson 			    __FUNCTION__);
144002ac6454SAndrew Thompson #endif
144102ac6454SAndrew Thompson 		}
144202ac6454SAndrew Thompson 		if (status & UHCI_STS_HSE) {
144302ac6454SAndrew Thompson 			printf("%s: host system error\n",
144402ac6454SAndrew Thompson 			    __FUNCTION__);
144502ac6454SAndrew Thompson 		}
144602ac6454SAndrew Thompson 		if (status & UHCI_STS_HCPE) {
144702ac6454SAndrew Thompson 			printf("%s: host controller process error\n",
144802ac6454SAndrew Thompson 			    __FUNCTION__);
144902ac6454SAndrew Thompson 		}
145002ac6454SAndrew Thompson 		if (status & UHCI_STS_HCH) {
145102ac6454SAndrew Thompson 			/* no acknowledge needed */
145202ac6454SAndrew Thompson 			DPRINTF("%s: host controller halted\n",
145302ac6454SAndrew Thompson 			    __FUNCTION__);
145402ac6454SAndrew Thompson #if USB_DEBUG
145502ac6454SAndrew Thompson 			if (uhcidebug > 0) {
145602ac6454SAndrew Thompson 				uhci_dump_all(sc);
145702ac6454SAndrew Thompson 			}
145802ac6454SAndrew Thompson #endif
145902ac6454SAndrew Thompson 		}
146002ac6454SAndrew Thompson 	}
146102ac6454SAndrew Thompson 	/* get acknowledge bits */
146202ac6454SAndrew Thompson 	status &= (UHCI_STS_USBINT |
146302ac6454SAndrew Thompson 	    UHCI_STS_USBEI |
146402ac6454SAndrew Thompson 	    UHCI_STS_RD |
146502ac6454SAndrew Thompson 	    UHCI_STS_HSE |
146602ac6454SAndrew Thompson 	    UHCI_STS_HCPE);
146702ac6454SAndrew Thompson 
146802ac6454SAndrew Thompson 	if (status == 0) {
146902ac6454SAndrew Thompson 		/* nothing to acknowledge */
147002ac6454SAndrew Thompson 		goto done;
147102ac6454SAndrew Thompson 	}
147202ac6454SAndrew Thompson 	/* acknowledge interrupts */
147302ac6454SAndrew Thompson 	UWRITE2(sc, UHCI_STS, status);
147402ac6454SAndrew Thompson 
147502ac6454SAndrew Thompson 	/* poll all the USB transfers */
147602ac6454SAndrew Thompson 	uhci_interrupt_poll(sc);
147702ac6454SAndrew Thompson 
147802ac6454SAndrew Thompson done:
147902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
148002ac6454SAndrew Thompson }
148102ac6454SAndrew Thompson 
148202ac6454SAndrew Thompson /*
148302ac6454SAndrew Thompson  * called when a request does not complete
148402ac6454SAndrew Thompson  */
148502ac6454SAndrew Thompson static void
148602ac6454SAndrew Thompson uhci_timeout(void *arg)
148702ac6454SAndrew Thompson {
148802ac6454SAndrew Thompson 	struct usb2_xfer *xfer = arg;
148902ac6454SAndrew Thompson 
149002ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
149102ac6454SAndrew Thompson 
149202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
149302ac6454SAndrew Thompson 
149402ac6454SAndrew Thompson 	/* transfer is transferred */
149502ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_TIMEOUT);
149602ac6454SAndrew Thompson }
149702ac6454SAndrew Thompson 
149802ac6454SAndrew Thompson static void
149902ac6454SAndrew Thompson uhci_do_poll(struct usb2_bus *bus)
150002ac6454SAndrew Thompson {
150102ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
150202ac6454SAndrew Thompson 
150302ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
150402ac6454SAndrew Thompson 	uhci_interrupt_poll(sc);
150502ac6454SAndrew Thompson 	uhci_root_ctrl_poll(sc);
150602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
150702ac6454SAndrew Thompson }
150802ac6454SAndrew Thompson 
150902ac6454SAndrew Thompson static void
151002ac6454SAndrew Thompson uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
151102ac6454SAndrew Thompson {
151202ac6454SAndrew Thompson 	uhci_td_t *td;
151302ac6454SAndrew Thompson 	uhci_td_t *td_next;
151402ac6454SAndrew Thompson 	uhci_td_t *td_alt_next;
151502ac6454SAndrew Thompson 	uint32_t average;
151602ac6454SAndrew Thompson 	uint32_t len_old;
151702ac6454SAndrew Thompson 	uint8_t shortpkt_old;
151802ac6454SAndrew Thompson 	uint8_t precompute;
151902ac6454SAndrew Thompson 
152002ac6454SAndrew Thompson 	td_alt_next = NULL;
152102ac6454SAndrew Thompson 	shortpkt_old = temp->shortpkt;
152202ac6454SAndrew Thompson 	len_old = temp->len;
152302ac6454SAndrew Thompson 	precompute = 1;
152402ac6454SAndrew Thompson 
152502ac6454SAndrew Thompson 	/* software is used to detect short incoming transfers */
152602ac6454SAndrew Thompson 
152702ac6454SAndrew Thompson 	if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
152802ac6454SAndrew Thompson 		temp->td_status |= htole32(UHCI_TD_SPD);
152902ac6454SAndrew Thompson 	} else {
153002ac6454SAndrew Thompson 		temp->td_status &= ~htole32(UHCI_TD_SPD);
153102ac6454SAndrew Thompson 	}
153202ac6454SAndrew Thompson 
153302ac6454SAndrew Thompson 	temp->ml.buf_offset = 0;
153402ac6454SAndrew Thompson 
153502ac6454SAndrew Thompson restart:
153602ac6454SAndrew Thompson 
153702ac6454SAndrew Thompson 	temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
153802ac6454SAndrew Thompson 	temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
153902ac6454SAndrew Thompson 
154002ac6454SAndrew Thompson 	td = temp->td;
154102ac6454SAndrew Thompson 	td_next = temp->td_next;
154202ac6454SAndrew Thompson 
154302ac6454SAndrew Thompson 	while (1) {
154402ac6454SAndrew Thompson 
154502ac6454SAndrew Thompson 		if (temp->len == 0) {
154602ac6454SAndrew Thompson 
154702ac6454SAndrew Thompson 			if (temp->shortpkt) {
154802ac6454SAndrew Thompson 				break;
154902ac6454SAndrew Thompson 			}
155002ac6454SAndrew Thompson 			/* send a Zero Length Packet, ZLP, last */
155102ac6454SAndrew Thompson 
155202ac6454SAndrew Thompson 			temp->shortpkt = 1;
155302ac6454SAndrew Thompson 			temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
155402ac6454SAndrew Thompson 			average = 0;
155502ac6454SAndrew Thompson 
155602ac6454SAndrew Thompson 		} else {
155702ac6454SAndrew Thompson 
155802ac6454SAndrew Thompson 			average = temp->average;
155902ac6454SAndrew Thompson 
156002ac6454SAndrew Thompson 			if (temp->len < average) {
156102ac6454SAndrew Thompson 				temp->shortpkt = 1;
156202ac6454SAndrew Thompson 				temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
156302ac6454SAndrew Thompson 				temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
156402ac6454SAndrew Thompson 				average = temp->len;
156502ac6454SAndrew Thompson 			}
156602ac6454SAndrew Thompson 		}
156702ac6454SAndrew Thompson 
156802ac6454SAndrew Thompson 		if (td_next == NULL) {
156902ac6454SAndrew Thompson 			panic("%s: out of UHCI transfer descriptors!", __FUNCTION__);
157002ac6454SAndrew Thompson 		}
157102ac6454SAndrew Thompson 		/* get next TD */
157202ac6454SAndrew Thompson 
157302ac6454SAndrew Thompson 		td = td_next;
157402ac6454SAndrew Thompson 		td_next = td->obj_next;
157502ac6454SAndrew Thompson 
157602ac6454SAndrew Thompson 		/* check if we are pre-computing */
157702ac6454SAndrew Thompson 
157802ac6454SAndrew Thompson 		if (precompute) {
157902ac6454SAndrew Thompson 
158002ac6454SAndrew Thompson 			/* update remaining length */
158102ac6454SAndrew Thompson 
158202ac6454SAndrew Thompson 			temp->len -= average;
158302ac6454SAndrew Thompson 
158402ac6454SAndrew Thompson 			continue;
158502ac6454SAndrew Thompson 		}
158602ac6454SAndrew Thompson 		/* fill out current TD */
158702ac6454SAndrew Thompson 
158802ac6454SAndrew Thompson 		td->td_status = temp->td_status;
158902ac6454SAndrew Thompson 		td->td_token = temp->td_token;
159002ac6454SAndrew Thompson 
159102ac6454SAndrew Thompson 		/* update data toggle */
159202ac6454SAndrew Thompson 
159302ac6454SAndrew Thompson 		temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
159402ac6454SAndrew Thompson 
159502ac6454SAndrew Thompson 		if (average == 0) {
159602ac6454SAndrew Thompson 
159702ac6454SAndrew Thompson 			td->len = 0;
159802ac6454SAndrew Thompson 			td->td_buffer = 0;
159902ac6454SAndrew Thompson 			td->fix_pc = NULL;
160002ac6454SAndrew Thompson 
160102ac6454SAndrew Thompson 		} else {
160202ac6454SAndrew Thompson 
160302ac6454SAndrew Thompson 			/* update remaining length */
160402ac6454SAndrew Thompson 
160502ac6454SAndrew Thompson 			temp->len -= average;
160602ac6454SAndrew Thompson 
160702ac6454SAndrew Thompson 			td->len = average;
160802ac6454SAndrew Thompson 
160902ac6454SAndrew Thompson 			/* fill out buffer pointer and do fixup, if any */
161002ac6454SAndrew Thompson 
161102ac6454SAndrew Thompson 			uhci_mem_layout_fixup(&temp->ml, td);
161202ac6454SAndrew Thompson 		}
161302ac6454SAndrew Thompson 
161402ac6454SAndrew Thompson 		td->alt_next = td_alt_next;
161502ac6454SAndrew Thompson 
161602ac6454SAndrew Thompson 		if ((td_next == td_alt_next) && temp->setup_alt_next) {
161702ac6454SAndrew Thompson 			/* we need to receive these frames one by one ! */
161802ac6454SAndrew Thompson 			td->td_status |= htole32(UHCI_TD_IOC);
161902ac6454SAndrew Thompson 			td->td_next = htole32(UHCI_PTR_T);
162002ac6454SAndrew Thompson 		} else {
162102ac6454SAndrew Thompson 			if (td_next) {
162202ac6454SAndrew Thompson 				/* link the current TD with the next one */
162302ac6454SAndrew Thompson 				td->td_next = td_next->td_self;
162402ac6454SAndrew Thompson 			}
162502ac6454SAndrew Thompson 		}
162602ac6454SAndrew Thompson 
162702ac6454SAndrew Thompson 		usb2_pc_cpu_flush(td->page_cache);
162802ac6454SAndrew Thompson 	}
162902ac6454SAndrew Thompson 
163002ac6454SAndrew Thompson 	if (precompute) {
163102ac6454SAndrew Thompson 		precompute = 0;
163202ac6454SAndrew Thompson 
163302ac6454SAndrew Thompson 		/* setup alt next pointer, if any */
163402ac6454SAndrew Thompson 		if (temp->short_frames_ok) {
163502ac6454SAndrew Thompson 			if (temp->setup_alt_next) {
163602ac6454SAndrew Thompson 				td_alt_next = td_next;
163702ac6454SAndrew Thompson 			}
163802ac6454SAndrew Thompson 		} else {
163902ac6454SAndrew Thompson 			/* we use this field internally */
164002ac6454SAndrew Thompson 			td_alt_next = td_next;
164102ac6454SAndrew Thompson 		}
164202ac6454SAndrew Thompson 
164302ac6454SAndrew Thompson 		/* restore */
164402ac6454SAndrew Thompson 		temp->shortpkt = shortpkt_old;
164502ac6454SAndrew Thompson 		temp->len = len_old;
164602ac6454SAndrew Thompson 		goto restart;
164702ac6454SAndrew Thompson 	}
164802ac6454SAndrew Thompson 	temp->td = td;
164902ac6454SAndrew Thompson 	temp->td_next = td_next;
165002ac6454SAndrew Thompson }
165102ac6454SAndrew Thompson 
165202ac6454SAndrew Thompson static uhci_td_t *
165302ac6454SAndrew Thompson uhci_setup_standard_chain(struct usb2_xfer *xfer)
165402ac6454SAndrew Thompson {
165502ac6454SAndrew Thompson 	struct uhci_std_temp temp;
165602ac6454SAndrew Thompson 	uhci_td_t *td;
165702ac6454SAndrew Thompson 	uint32_t x;
165802ac6454SAndrew Thompson 
165902ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
166002ac6454SAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpoint),
166102ac6454SAndrew Thompson 	    xfer->sumlen, usb2_get_speed(xfer->xroot->udev));
166202ac6454SAndrew Thompson 
166302ac6454SAndrew Thompson 	temp.average = xfer->max_frame_size;
166402ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
166502ac6454SAndrew Thompson 
166602ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
166702ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
166802ac6454SAndrew Thompson 
166902ac6454SAndrew Thompson 	/* get next DMA set */
167002ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
167102ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
167202ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
167302ac6454SAndrew Thompson 
167402ac6454SAndrew Thompson 	temp.td = NULL;
167502ac6454SAndrew Thompson 	temp.td_next = td;
167602ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
167702ac6454SAndrew Thompson 	temp.short_frames_ok = xfer->flags_int.short_frames_ok;
167802ac6454SAndrew Thompson 
167902ac6454SAndrew Thompson 	uhci_mem_layout_init(&temp.ml, xfer);
168002ac6454SAndrew Thompson 
168102ac6454SAndrew Thompson 	temp.td_status =
168202ac6454SAndrew Thompson 	    htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
168302ac6454SAndrew Thompson 	    UHCI_TD_ACTIVE));
168402ac6454SAndrew Thompson 
168502ac6454SAndrew Thompson 	if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
168602ac6454SAndrew Thompson 		temp.td_status |= htole32(UHCI_TD_LS);
168702ac6454SAndrew Thompson 	}
168802ac6454SAndrew Thompson 	temp.td_token =
168902ac6454SAndrew Thompson 	    htole32(UHCI_TD_SET_ENDPT(xfer->endpoint) |
169002ac6454SAndrew Thompson 	    UHCI_TD_SET_DEVADDR(xfer->address));
169102ac6454SAndrew Thompson 
169202ac6454SAndrew Thompson 	if (xfer->pipe->toggle_next) {
169302ac6454SAndrew Thompson 		/* DATA1 is next */
169402ac6454SAndrew Thompson 		temp.td_token |= htole32(UHCI_TD_SET_DT(1));
169502ac6454SAndrew Thompson 	}
169602ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
169702ac6454SAndrew Thompson 
169802ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
169902ac6454SAndrew Thompson 
170002ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
170102ac6454SAndrew Thompson 
170202ac6454SAndrew Thompson 			temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
170302ac6454SAndrew Thompson 			    UHCI_TD_SET_ENDPT(0xF));
170402ac6454SAndrew Thompson 			temp.td_token |= htole32(UHCI_TD_PID_SETUP |
170502ac6454SAndrew Thompson 			    UHCI_TD_SET_DT(0));
170602ac6454SAndrew Thompson 
170702ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
170802ac6454SAndrew Thompson 			temp.ml.buf_pc = xfer->frbuffers + 0;
170902ac6454SAndrew Thompson 			temp.shortpkt = temp.len ? 1 : 0;
171002ac6454SAndrew Thompson 
171102ac6454SAndrew Thompson 			uhci_setup_standard_chain_sub(&temp);
171202ac6454SAndrew Thompson 		}
171302ac6454SAndrew Thompson 		x = 1;
171402ac6454SAndrew Thompson 	} else {
171502ac6454SAndrew Thompson 		x = 0;
171602ac6454SAndrew Thompson 	}
171702ac6454SAndrew Thompson 
171802ac6454SAndrew Thompson 	while (x != xfer->nframes) {
171902ac6454SAndrew Thompson 
172002ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
172102ac6454SAndrew Thompson 
172202ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
172302ac6454SAndrew Thompson 		temp.ml.buf_pc = xfer->frbuffers + x;
172402ac6454SAndrew Thompson 
172502ac6454SAndrew Thompson 		x++;
172602ac6454SAndrew Thompson 
172702ac6454SAndrew Thompson 		if (x == xfer->nframes) {
172802ac6454SAndrew Thompson 			temp.setup_alt_next = 0;
172902ac6454SAndrew Thompson 		}
173002ac6454SAndrew Thompson 		/*
173102ac6454SAndrew Thompson 		 * Keep previous data toggle,
173202ac6454SAndrew Thompson 		 * device address and endpoint number:
173302ac6454SAndrew Thompson 		 */
173402ac6454SAndrew Thompson 
173502ac6454SAndrew Thompson 		temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
173602ac6454SAndrew Thompson 		    UHCI_TD_SET_ENDPT(0xF) |
173702ac6454SAndrew Thompson 		    UHCI_TD_SET_DT(1));
173802ac6454SAndrew Thompson 
173902ac6454SAndrew Thompson 		if (temp.len == 0) {
174002ac6454SAndrew Thompson 
174102ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
174202ac6454SAndrew Thompson 
174302ac6454SAndrew Thompson 			temp.shortpkt = 0;
174402ac6454SAndrew Thompson 
174502ac6454SAndrew Thompson 		} else {
174602ac6454SAndrew Thompson 
174702ac6454SAndrew Thompson 			/* regular data transfer */
174802ac6454SAndrew Thompson 
174902ac6454SAndrew Thompson 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
175002ac6454SAndrew Thompson 		}
175102ac6454SAndrew Thompson 
175202ac6454SAndrew Thompson 		/* set endpoint direction */
175302ac6454SAndrew Thompson 
175402ac6454SAndrew Thompson 		temp.td_token |=
175502ac6454SAndrew Thompson 		    (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) ?
175602ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_IN) :
175702ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_OUT);
175802ac6454SAndrew Thompson 
175902ac6454SAndrew Thompson 		uhci_setup_standard_chain_sub(&temp);
176002ac6454SAndrew Thompson 	}
176102ac6454SAndrew Thompson 
176202ac6454SAndrew Thompson 	/* check if we should append a status stage */
176302ac6454SAndrew Thompson 
176402ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
176502ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
176602ac6454SAndrew Thompson 
176702ac6454SAndrew Thompson 		/*
176802ac6454SAndrew Thompson 		 * send a DATA1 message and reverse the current endpoint
176902ac6454SAndrew Thompson 		 * direction
177002ac6454SAndrew Thompson 		 */
177102ac6454SAndrew Thompson 
177202ac6454SAndrew Thompson 		temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
177302ac6454SAndrew Thompson 		    UHCI_TD_SET_ENDPT(0xF) |
177402ac6454SAndrew Thompson 		    UHCI_TD_SET_DT(1));
177502ac6454SAndrew Thompson 		temp.td_token |=
177602ac6454SAndrew Thompson 		    (UE_GET_DIR(xfer->endpoint) == UE_DIR_OUT) ?
177702ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
177802ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
177902ac6454SAndrew Thompson 
178002ac6454SAndrew Thompson 		temp.len = 0;
178102ac6454SAndrew Thompson 		temp.ml.buf_pc = NULL;
178202ac6454SAndrew Thompson 		temp.shortpkt = 0;
178302ac6454SAndrew Thompson 
178402ac6454SAndrew Thompson 		uhci_setup_standard_chain_sub(&temp);
178502ac6454SAndrew Thompson 	}
178602ac6454SAndrew Thompson 	td = temp.td;
178702ac6454SAndrew Thompson 
178802ac6454SAndrew Thompson 	td->td_next = htole32(UHCI_PTR_T);
178902ac6454SAndrew Thompson 
179002ac6454SAndrew Thompson 	/* set interrupt bit */
179102ac6454SAndrew Thompson 
179202ac6454SAndrew Thompson 	td->td_status |= htole32(UHCI_TD_IOC);
179302ac6454SAndrew Thompson 
179402ac6454SAndrew Thompson 	usb2_pc_cpu_flush(td->page_cache);
179502ac6454SAndrew Thompson 
179602ac6454SAndrew Thompson 	/* must have at least one frame! */
179702ac6454SAndrew Thompson 
179802ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
179902ac6454SAndrew Thompson 
180002ac6454SAndrew Thompson #if USB_DEBUG
180102ac6454SAndrew Thompson 	if (uhcidebug > 8) {
180202ac6454SAndrew Thompson 		DPRINTF("nexttog=%d; data before transfer:\n",
180302ac6454SAndrew Thompson 		    xfer->pipe->toggle_next);
180402ac6454SAndrew Thompson 		uhci_dump_tds(xfer->td_transfer_first);
180502ac6454SAndrew Thompson 	}
180602ac6454SAndrew Thompson #endif
180702ac6454SAndrew Thompson 	return (xfer->td_transfer_first);
180802ac6454SAndrew Thompson }
180902ac6454SAndrew Thompson 
181002ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row,
181102ac6454SAndrew Thompson  * from close and from interrupt
181202ac6454SAndrew Thompson  */
181302ac6454SAndrew Thompson 
181402ac6454SAndrew Thompson static void
181502ac6454SAndrew Thompson uhci_device_done(struct usb2_xfer *xfer, usb2_error_t error)
181602ac6454SAndrew Thompson {
181702ac6454SAndrew Thompson 	struct usb2_pipe_methods *methods = xfer->pipe->methods;
181802ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
181902ac6454SAndrew Thompson 	uhci_qh_t *qh;
182002ac6454SAndrew Thompson 
182102ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
182202ac6454SAndrew Thompson 
182302ac6454SAndrew Thompson 	DPRINTFN(2, "xfer=%p, pipe=%p, error=%d\n",
182402ac6454SAndrew Thompson 	    xfer, xfer->pipe, error);
182502ac6454SAndrew Thompson 
182602ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
182702ac6454SAndrew Thompson 	if (qh) {
182802ac6454SAndrew Thompson 		usb2_pc_cpu_invalidate(qh->page_cache);
182902ac6454SAndrew Thompson 	}
183002ac6454SAndrew Thompson 	if (xfer->flags_int.bandwidth_reclaimed) {
183102ac6454SAndrew Thompson 		xfer->flags_int.bandwidth_reclaimed = 0;
183202ac6454SAndrew Thompson 		uhci_rem_loop(sc);
183302ac6454SAndrew Thompson 	}
183402ac6454SAndrew Thompson 	if (methods == &uhci_device_bulk_methods) {
183502ac6454SAndrew Thompson 		UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
183602ac6454SAndrew Thompson 	}
183702ac6454SAndrew Thompson 	if (methods == &uhci_device_ctrl_methods) {
183802ac6454SAndrew Thompson 		if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
183902ac6454SAndrew Thompson 			UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
184002ac6454SAndrew Thompson 		} else {
184102ac6454SAndrew Thompson 			UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
184202ac6454SAndrew Thompson 		}
184302ac6454SAndrew Thompson 	}
184402ac6454SAndrew Thompson 	if (methods == &uhci_device_intr_methods) {
184502ac6454SAndrew Thompson 		UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
184602ac6454SAndrew Thompson 	}
184702ac6454SAndrew Thompson 	/*
184802ac6454SAndrew Thompson 	 * Only finish isochronous transfers once
184902ac6454SAndrew Thompson 	 * which will update "xfer->frlengths".
185002ac6454SAndrew Thompson 	 */
185102ac6454SAndrew Thompson 	if (xfer->td_transfer_first &&
185202ac6454SAndrew Thompson 	    xfer->td_transfer_last) {
185302ac6454SAndrew Thompson 		if (methods == &uhci_device_isoc_methods) {
185402ac6454SAndrew Thompson 			uhci_isoc_done(sc, xfer);
185502ac6454SAndrew Thompson 		}
185602ac6454SAndrew Thompson 		xfer->td_transfer_first = NULL;
185702ac6454SAndrew Thompson 		xfer->td_transfer_last = NULL;
185802ac6454SAndrew Thompson 	}
185902ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
186002ac6454SAndrew Thompson 	usb2_transfer_done(xfer, error);
186102ac6454SAndrew Thompson }
186202ac6454SAndrew Thompson 
186302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
186402ac6454SAndrew Thompson  * uhci bulk support
186502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
186602ac6454SAndrew Thompson static void
186702ac6454SAndrew Thompson uhci_device_bulk_open(struct usb2_xfer *xfer)
186802ac6454SAndrew Thompson {
186902ac6454SAndrew Thompson 	return;
187002ac6454SAndrew Thompson }
187102ac6454SAndrew Thompson 
187202ac6454SAndrew Thompson static void
187302ac6454SAndrew Thompson uhci_device_bulk_close(struct usb2_xfer *xfer)
187402ac6454SAndrew Thompson {
187502ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
187602ac6454SAndrew Thompson }
187702ac6454SAndrew Thompson 
187802ac6454SAndrew Thompson static void
187902ac6454SAndrew Thompson uhci_device_bulk_enter(struct usb2_xfer *xfer)
188002ac6454SAndrew Thompson {
188102ac6454SAndrew Thompson 	return;
188202ac6454SAndrew Thompson }
188302ac6454SAndrew Thompson 
188402ac6454SAndrew Thompson static void
188502ac6454SAndrew Thompson uhci_device_bulk_start(struct usb2_xfer *xfer)
188602ac6454SAndrew Thompson {
188702ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
188802ac6454SAndrew Thompson 	uhci_td_t *td;
188902ac6454SAndrew Thompson 	uhci_qh_t *qh;
189002ac6454SAndrew Thompson 
189102ac6454SAndrew Thompson 	/* setup TD's */
189202ac6454SAndrew Thompson 	td = uhci_setup_standard_chain(xfer);
189302ac6454SAndrew Thompson 
189402ac6454SAndrew Thompson 	/* setup QH */
189502ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
189602ac6454SAndrew Thompson 
189702ac6454SAndrew Thompson 	qh->e_next = td;
189802ac6454SAndrew Thompson 	qh->qh_e_next = td->td_self;
189902ac6454SAndrew Thompson 
190002ac6454SAndrew Thompson 	if (xfer->xroot->udev->pwr_save.suspended == 0) {
190102ac6454SAndrew Thompson 		UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
190202ac6454SAndrew Thompson 		uhci_add_loop(sc);
190302ac6454SAndrew Thompson 		xfer->flags_int.bandwidth_reclaimed = 1;
190402ac6454SAndrew Thompson 	} else {
190502ac6454SAndrew Thompson 		usb2_pc_cpu_flush(qh->page_cache);
190602ac6454SAndrew Thompson 	}
190702ac6454SAndrew Thompson 
190802ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
190902ac6454SAndrew Thompson 	uhci_transfer_intr_enqueue(xfer);
191002ac6454SAndrew Thompson }
191102ac6454SAndrew Thompson 
191202ac6454SAndrew Thompson struct usb2_pipe_methods uhci_device_bulk_methods =
191302ac6454SAndrew Thompson {
191402ac6454SAndrew Thompson 	.open = uhci_device_bulk_open,
191502ac6454SAndrew Thompson 	.close = uhci_device_bulk_close,
191602ac6454SAndrew Thompson 	.enter = uhci_device_bulk_enter,
191702ac6454SAndrew Thompson 	.start = uhci_device_bulk_start,
191802ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
191902ac6454SAndrew Thompson 	.start_is_cancelable = 1,
192002ac6454SAndrew Thompson };
192102ac6454SAndrew Thompson 
192202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
192302ac6454SAndrew Thompson  * uhci control support
192402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
192502ac6454SAndrew Thompson static void
192602ac6454SAndrew Thompson uhci_device_ctrl_open(struct usb2_xfer *xfer)
192702ac6454SAndrew Thompson {
192802ac6454SAndrew Thompson 	return;
192902ac6454SAndrew Thompson }
193002ac6454SAndrew Thompson 
193102ac6454SAndrew Thompson static void
193202ac6454SAndrew Thompson uhci_device_ctrl_close(struct usb2_xfer *xfer)
193302ac6454SAndrew Thompson {
193402ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
193502ac6454SAndrew Thompson }
193602ac6454SAndrew Thompson 
193702ac6454SAndrew Thompson static void
193802ac6454SAndrew Thompson uhci_device_ctrl_enter(struct usb2_xfer *xfer)
193902ac6454SAndrew Thompson {
194002ac6454SAndrew Thompson 	return;
194102ac6454SAndrew Thompson }
194202ac6454SAndrew Thompson 
194302ac6454SAndrew Thompson static void
194402ac6454SAndrew Thompson uhci_device_ctrl_start(struct usb2_xfer *xfer)
194502ac6454SAndrew Thompson {
194602ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
194702ac6454SAndrew Thompson 	uhci_qh_t *qh;
194802ac6454SAndrew Thompson 	uhci_td_t *td;
194902ac6454SAndrew Thompson 
195002ac6454SAndrew Thompson 	/* setup TD's */
195102ac6454SAndrew Thompson 	td = uhci_setup_standard_chain(xfer);
195202ac6454SAndrew Thompson 
195302ac6454SAndrew Thompson 	/* setup QH */
195402ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
195502ac6454SAndrew Thompson 
195602ac6454SAndrew Thompson 	qh->e_next = td;
195702ac6454SAndrew Thompson 	qh->qh_e_next = td->td_self;
195802ac6454SAndrew Thompson 
195902ac6454SAndrew Thompson 	/*
196002ac6454SAndrew Thompson 	 * NOTE: some devices choke on bandwidth- reclamation for control
196102ac6454SAndrew Thompson 	 * transfers
196202ac6454SAndrew Thompson 	 */
196302ac6454SAndrew Thompson 	if (xfer->xroot->udev->pwr_save.suspended == 0) {
196402ac6454SAndrew Thompson 		if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
196502ac6454SAndrew Thompson 			UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
196602ac6454SAndrew Thompson 		} else {
196702ac6454SAndrew Thompson 			UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
196802ac6454SAndrew Thompson 		}
196902ac6454SAndrew Thompson 	} else {
197002ac6454SAndrew Thompson 		usb2_pc_cpu_flush(qh->page_cache);
197102ac6454SAndrew Thompson 	}
197202ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
197302ac6454SAndrew Thompson 	uhci_transfer_intr_enqueue(xfer);
197402ac6454SAndrew Thompson }
197502ac6454SAndrew Thompson 
197602ac6454SAndrew Thompson struct usb2_pipe_methods uhci_device_ctrl_methods =
197702ac6454SAndrew Thompson {
197802ac6454SAndrew Thompson 	.open = uhci_device_ctrl_open,
197902ac6454SAndrew Thompson 	.close = uhci_device_ctrl_close,
198002ac6454SAndrew Thompson 	.enter = uhci_device_ctrl_enter,
198102ac6454SAndrew Thompson 	.start = uhci_device_ctrl_start,
198202ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
198302ac6454SAndrew Thompson 	.start_is_cancelable = 1,
198402ac6454SAndrew Thompson };
198502ac6454SAndrew Thompson 
198602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
198702ac6454SAndrew Thompson  * uhci interrupt support
198802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
198902ac6454SAndrew Thompson static void
199002ac6454SAndrew Thompson uhci_device_intr_open(struct usb2_xfer *xfer)
199102ac6454SAndrew Thompson {
199202ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
199302ac6454SAndrew Thompson 	uint16_t best;
199402ac6454SAndrew Thompson 	uint16_t bit;
199502ac6454SAndrew Thompson 	uint16_t x;
199602ac6454SAndrew Thompson 
199702ac6454SAndrew Thompson 	best = 0;
199802ac6454SAndrew Thompson 	bit = UHCI_IFRAMELIST_COUNT / 2;
199902ac6454SAndrew Thompson 	while (bit) {
200002ac6454SAndrew Thompson 		if (xfer->interval >= bit) {
200102ac6454SAndrew Thompson 			x = bit;
200202ac6454SAndrew Thompson 			best = bit;
200302ac6454SAndrew Thompson 			while (x & bit) {
200402ac6454SAndrew Thompson 				if (sc->sc_intr_stat[x] <
200502ac6454SAndrew Thompson 				    sc->sc_intr_stat[best]) {
200602ac6454SAndrew Thompson 					best = x;
200702ac6454SAndrew Thompson 				}
200802ac6454SAndrew Thompson 				x++;
200902ac6454SAndrew Thompson 			}
201002ac6454SAndrew Thompson 			break;
201102ac6454SAndrew Thompson 		}
201202ac6454SAndrew Thompson 		bit >>= 1;
201302ac6454SAndrew Thompson 	}
201402ac6454SAndrew Thompson 
201502ac6454SAndrew Thompson 	sc->sc_intr_stat[best]++;
201602ac6454SAndrew Thompson 	xfer->qh_pos = best;
201702ac6454SAndrew Thompson 
201802ac6454SAndrew Thompson 	DPRINTFN(3, "best=%d interval=%d\n",
201902ac6454SAndrew Thompson 	    best, xfer->interval);
202002ac6454SAndrew Thompson }
202102ac6454SAndrew Thompson 
202202ac6454SAndrew Thompson static void
202302ac6454SAndrew Thompson uhci_device_intr_close(struct usb2_xfer *xfer)
202402ac6454SAndrew Thompson {
202502ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
202602ac6454SAndrew Thompson 
202702ac6454SAndrew Thompson 	sc->sc_intr_stat[xfer->qh_pos]--;
202802ac6454SAndrew Thompson 
202902ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
203002ac6454SAndrew Thompson }
203102ac6454SAndrew Thompson 
203202ac6454SAndrew Thompson static void
203302ac6454SAndrew Thompson uhci_device_intr_enter(struct usb2_xfer *xfer)
203402ac6454SAndrew Thompson {
203502ac6454SAndrew Thompson 	return;
203602ac6454SAndrew Thompson }
203702ac6454SAndrew Thompson 
203802ac6454SAndrew Thompson static void
203902ac6454SAndrew Thompson uhci_device_intr_start(struct usb2_xfer *xfer)
204002ac6454SAndrew Thompson {
204102ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
204202ac6454SAndrew Thompson 	uhci_qh_t *qh;
204302ac6454SAndrew Thompson 	uhci_td_t *td;
204402ac6454SAndrew Thompson 
204502ac6454SAndrew Thompson 	/* setup TD's */
204602ac6454SAndrew Thompson 	td = uhci_setup_standard_chain(xfer);
204702ac6454SAndrew Thompson 
204802ac6454SAndrew Thompson 	/* setup QH */
204902ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
205002ac6454SAndrew Thompson 
205102ac6454SAndrew Thompson 	qh->e_next = td;
205202ac6454SAndrew Thompson 	qh->qh_e_next = td->td_self;
205302ac6454SAndrew Thompson 
205402ac6454SAndrew Thompson 	if (xfer->xroot->udev->pwr_save.suspended == 0) {
205502ac6454SAndrew Thompson 
205602ac6454SAndrew Thompson 		/* enter QHs into the controller data structures */
205702ac6454SAndrew Thompson 		UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
205802ac6454SAndrew Thompson 
205902ac6454SAndrew Thompson 	} else {
206002ac6454SAndrew Thompson 		usb2_pc_cpu_flush(qh->page_cache);
206102ac6454SAndrew Thompson 	}
206202ac6454SAndrew Thompson 
206302ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
206402ac6454SAndrew Thompson 	uhci_transfer_intr_enqueue(xfer);
206502ac6454SAndrew Thompson }
206602ac6454SAndrew Thompson 
206702ac6454SAndrew Thompson struct usb2_pipe_methods uhci_device_intr_methods =
206802ac6454SAndrew Thompson {
206902ac6454SAndrew Thompson 	.open = uhci_device_intr_open,
207002ac6454SAndrew Thompson 	.close = uhci_device_intr_close,
207102ac6454SAndrew Thompson 	.enter = uhci_device_intr_enter,
207202ac6454SAndrew Thompson 	.start = uhci_device_intr_start,
207302ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
207402ac6454SAndrew Thompson 	.start_is_cancelable = 1,
207502ac6454SAndrew Thompson };
207602ac6454SAndrew Thompson 
207702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
207802ac6454SAndrew Thompson  * uhci isochronous support
207902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
208002ac6454SAndrew Thompson static void
208102ac6454SAndrew Thompson uhci_device_isoc_open(struct usb2_xfer *xfer)
208202ac6454SAndrew Thompson {
208302ac6454SAndrew Thompson 	uhci_td_t *td;
208402ac6454SAndrew Thompson 	uint32_t td_token;
208502ac6454SAndrew Thompson 	uint8_t ds;
208602ac6454SAndrew Thompson 
208702ac6454SAndrew Thompson 	td_token =
208802ac6454SAndrew Thompson 	    (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) ?
208902ac6454SAndrew Thompson 	    UHCI_TD_IN(0, xfer->endpoint, xfer->address, 0) :
209002ac6454SAndrew Thompson 	    UHCI_TD_OUT(0, xfer->endpoint, xfer->address, 0);
209102ac6454SAndrew Thompson 
209202ac6454SAndrew Thompson 	td_token = htole32(td_token);
209302ac6454SAndrew Thompson 
209402ac6454SAndrew Thompson 	/* initialize all TD's */
209502ac6454SAndrew Thompson 
209602ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
209702ac6454SAndrew Thompson 
209802ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
209902ac6454SAndrew Thompson 
210002ac6454SAndrew Thompson 			/* mark TD as inactive */
210102ac6454SAndrew Thompson 			td->td_status = htole32(UHCI_TD_IOS);
210202ac6454SAndrew Thompson 			td->td_token = td_token;
210302ac6454SAndrew Thompson 
210402ac6454SAndrew Thompson 			usb2_pc_cpu_flush(td->page_cache);
210502ac6454SAndrew Thompson 		}
210602ac6454SAndrew Thompson 	}
210702ac6454SAndrew Thompson }
210802ac6454SAndrew Thompson 
210902ac6454SAndrew Thompson static void
211002ac6454SAndrew Thompson uhci_device_isoc_close(struct usb2_xfer *xfer)
211102ac6454SAndrew Thompson {
211202ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
211302ac6454SAndrew Thompson }
211402ac6454SAndrew Thompson 
211502ac6454SAndrew Thompson static void
211602ac6454SAndrew Thompson uhci_device_isoc_enter(struct usb2_xfer *xfer)
211702ac6454SAndrew Thompson {
211802ac6454SAndrew Thompson 	struct uhci_mem_layout ml;
211902ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
212002ac6454SAndrew Thompson 	uint32_t nframes;
212102ac6454SAndrew Thompson 	uint32_t temp;
212202ac6454SAndrew Thompson 	uint32_t *plen;
212302ac6454SAndrew Thompson 
212402ac6454SAndrew Thompson #if USB_DEBUG
212502ac6454SAndrew Thompson 	uint8_t once = 1;
212602ac6454SAndrew Thompson 
212702ac6454SAndrew Thompson #endif
212802ac6454SAndrew Thompson 	uhci_td_t *td;
212902ac6454SAndrew Thompson 	uhci_td_t *td_last = NULL;
213002ac6454SAndrew Thompson 	uhci_td_t **pp_last;
213102ac6454SAndrew Thompson 
213202ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
213302ac6454SAndrew Thompson 	    xfer, xfer->pipe->isoc_next, xfer->nframes);
213402ac6454SAndrew Thompson 
213502ac6454SAndrew Thompson 	nframes = UREAD2(sc, UHCI_FRNUM);
213602ac6454SAndrew Thompson 
213702ac6454SAndrew Thompson 	temp = (nframes - xfer->pipe->isoc_next) &
213802ac6454SAndrew Thompson 	    (UHCI_VFRAMELIST_COUNT - 1);
213902ac6454SAndrew Thompson 
214002ac6454SAndrew Thompson 	if ((xfer->pipe->is_synced == 0) ||
214102ac6454SAndrew Thompson 	    (temp < xfer->nframes)) {
214202ac6454SAndrew Thompson 		/*
214302ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
214402ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
214502ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
214602ac6454SAndrew Thompson 		 * overlap.
214702ac6454SAndrew Thompson 		 */
214802ac6454SAndrew Thompson 		xfer->pipe->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
214902ac6454SAndrew Thompson 		xfer->pipe->is_synced = 1;
215002ac6454SAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
215102ac6454SAndrew Thompson 	}
215202ac6454SAndrew Thompson 	/*
215302ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
215402ac6454SAndrew Thompson 	 * current frame position:
215502ac6454SAndrew Thompson 	 */
215602ac6454SAndrew Thompson 	temp = (xfer->pipe->isoc_next - nframes) &
215702ac6454SAndrew Thompson 	    (UHCI_VFRAMELIST_COUNT - 1);
215802ac6454SAndrew Thompson 
215902ac6454SAndrew Thompson 	/*
216002ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
216102ac6454SAndrew Thompson 	 */
216202ac6454SAndrew Thompson 	xfer->isoc_time_complete =
216302ac6454SAndrew Thompson 	    usb2_isoc_time_expand(&sc->sc_bus, nframes) + temp +
216402ac6454SAndrew Thompson 	    xfer->nframes;
216502ac6454SAndrew Thompson 
216602ac6454SAndrew Thompson 	/* get the real number of frames */
216702ac6454SAndrew Thompson 
216802ac6454SAndrew Thompson 	nframes = xfer->nframes;
216902ac6454SAndrew Thompson 
217002ac6454SAndrew Thompson 	uhci_mem_layout_init(&ml, xfer);
217102ac6454SAndrew Thompson 
217202ac6454SAndrew Thompson 	plen = xfer->frlengths;
217302ac6454SAndrew Thompson 
217402ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
217502ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
217602ac6454SAndrew Thompson 
217702ac6454SAndrew Thompson 	/* get next DMA set */
217802ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
217902ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
218002ac6454SAndrew Thompson 
218102ac6454SAndrew Thompson 	pp_last = &sc->sc_isoc_p_last[xfer->pipe->isoc_next];
218202ac6454SAndrew Thompson 
218302ac6454SAndrew Thompson 	/* store starting position */
218402ac6454SAndrew Thompson 
218502ac6454SAndrew Thompson 	xfer->qh_pos = xfer->pipe->isoc_next;
218602ac6454SAndrew Thompson 
218702ac6454SAndrew Thompson 	while (nframes--) {
218802ac6454SAndrew Thompson 		if (td == NULL) {
218902ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
219002ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
219102ac6454SAndrew Thompson 		}
219202ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
219302ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_p_last[0];
219402ac6454SAndrew Thompson 		}
219502ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
219602ac6454SAndrew Thompson #if USB_DEBUG
219702ac6454SAndrew Thompson 			if (once) {
219802ac6454SAndrew Thompson 				once = 0;
219902ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d "
220002ac6454SAndrew Thompson 				    "bytes (frame truncated)\n",
220102ac6454SAndrew Thompson 				    __FUNCTION__, *plen,
220202ac6454SAndrew Thompson 				    xfer->max_frame_size);
220302ac6454SAndrew Thompson 			}
220402ac6454SAndrew Thompson #endif
220502ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
220602ac6454SAndrew Thompson 		}
220702ac6454SAndrew Thompson 		/* reuse td_token from last transfer */
220802ac6454SAndrew Thompson 
220902ac6454SAndrew Thompson 		td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
221002ac6454SAndrew Thompson 		td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
221102ac6454SAndrew Thompson 
221202ac6454SAndrew Thompson 		td->len = *plen;
221302ac6454SAndrew Thompson 
221402ac6454SAndrew Thompson 		if (td->len == 0) {
221502ac6454SAndrew Thompson 			/*
221602ac6454SAndrew Thompson 			 * Do not call "uhci_mem_layout_fixup()" when the
221702ac6454SAndrew Thompson 			 * length is zero!
221802ac6454SAndrew Thompson 			 */
221902ac6454SAndrew Thompson 			td->td_buffer = 0;
222002ac6454SAndrew Thompson 			td->fix_pc = NULL;
222102ac6454SAndrew Thompson 
222202ac6454SAndrew Thompson 		} else {
222302ac6454SAndrew Thompson 
222402ac6454SAndrew Thompson 			/* fill out buffer pointer and do fixup, if any */
222502ac6454SAndrew Thompson 
222602ac6454SAndrew Thompson 			uhci_mem_layout_fixup(&ml, td);
222702ac6454SAndrew Thompson 
222802ac6454SAndrew Thompson 		}
222902ac6454SAndrew Thompson 
223002ac6454SAndrew Thompson 		/* update status */
223102ac6454SAndrew Thompson 		if (nframes == 0) {
223202ac6454SAndrew Thompson 			td->td_status = htole32
223302ac6454SAndrew Thompson 			    (UHCI_TD_ZERO_ACTLEN
223402ac6454SAndrew Thompson 			    (UHCI_TD_SET_ERRCNT(0) |
223502ac6454SAndrew Thompson 			    UHCI_TD_ACTIVE |
223602ac6454SAndrew Thompson 			    UHCI_TD_IOS |
223702ac6454SAndrew Thompson 			    UHCI_TD_IOC));
223802ac6454SAndrew Thompson 		} else {
223902ac6454SAndrew Thompson 			td->td_status = htole32
224002ac6454SAndrew Thompson 			    (UHCI_TD_ZERO_ACTLEN
224102ac6454SAndrew Thompson 			    (UHCI_TD_SET_ERRCNT(0) |
224202ac6454SAndrew Thompson 			    UHCI_TD_ACTIVE |
224302ac6454SAndrew Thompson 			    UHCI_TD_IOS));
224402ac6454SAndrew Thompson 		}
224502ac6454SAndrew Thompson 
224602ac6454SAndrew Thompson 		usb2_pc_cpu_flush(td->page_cache);
224702ac6454SAndrew Thompson 
224802ac6454SAndrew Thompson #if USB_DEBUG
224902ac6454SAndrew Thompson 		if (uhcidebug > 5) {
225002ac6454SAndrew Thompson 			DPRINTF("TD %d\n", nframes);
225102ac6454SAndrew Thompson 			uhci_dump_td(td);
225202ac6454SAndrew Thompson 		}
225302ac6454SAndrew Thompson #endif
225402ac6454SAndrew Thompson 		/* insert TD into schedule */
225502ac6454SAndrew Thompson 		UHCI_APPEND_TD(td, *pp_last);
225602ac6454SAndrew Thompson 		pp_last++;
225702ac6454SAndrew Thompson 
225802ac6454SAndrew Thompson 		plen++;
225902ac6454SAndrew Thompson 		td_last = td;
226002ac6454SAndrew Thompson 		td = td->obj_next;
226102ac6454SAndrew Thompson 	}
226202ac6454SAndrew Thompson 
226302ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
226402ac6454SAndrew Thompson 
226502ac6454SAndrew Thompson 	/* update isoc_next */
226602ac6454SAndrew Thompson 	xfer->pipe->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
226702ac6454SAndrew Thompson 	    (UHCI_VFRAMELIST_COUNT - 1);
226802ac6454SAndrew Thompson }
226902ac6454SAndrew Thompson 
227002ac6454SAndrew Thompson static void
227102ac6454SAndrew Thompson uhci_device_isoc_start(struct usb2_xfer *xfer)
227202ac6454SAndrew Thompson {
227302ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
227402ac6454SAndrew Thompson 	uhci_transfer_intr_enqueue(xfer);
227502ac6454SAndrew Thompson }
227602ac6454SAndrew Thompson 
227702ac6454SAndrew Thompson struct usb2_pipe_methods uhci_device_isoc_methods =
227802ac6454SAndrew Thompson {
227902ac6454SAndrew Thompson 	.open = uhci_device_isoc_open,
228002ac6454SAndrew Thompson 	.close = uhci_device_isoc_close,
228102ac6454SAndrew Thompson 	.enter = uhci_device_isoc_enter,
228202ac6454SAndrew Thompson 	.start = uhci_device_isoc_start,
228302ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
228402ac6454SAndrew Thompson 	.start_is_cancelable = 1,
228502ac6454SAndrew Thompson };
228602ac6454SAndrew Thompson 
228702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
228802ac6454SAndrew Thompson  * uhci root control support
228902ac6454SAndrew Thompson  *------------------------------------------------------------------------*
229002ac6454SAndrew Thompson  * simulate a hardware hub by handling
229102ac6454SAndrew Thompson  * all the necessary requests
229202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
229302ac6454SAndrew Thompson 
229402ac6454SAndrew Thompson static void
229502ac6454SAndrew Thompson uhci_root_ctrl_open(struct usb2_xfer *xfer)
229602ac6454SAndrew Thompson {
229702ac6454SAndrew Thompson 	return;
229802ac6454SAndrew Thompson }
229902ac6454SAndrew Thompson 
230002ac6454SAndrew Thompson static void
230102ac6454SAndrew Thompson uhci_root_ctrl_close(struct usb2_xfer *xfer)
230202ac6454SAndrew Thompson {
230302ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
230402ac6454SAndrew Thompson 
230502ac6454SAndrew Thompson 	if (sc->sc_root_ctrl.xfer == xfer) {
230602ac6454SAndrew Thompson 		sc->sc_root_ctrl.xfer = NULL;
230702ac6454SAndrew Thompson 	}
230802ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
230902ac6454SAndrew Thompson }
231002ac6454SAndrew Thompson 
231102ac6454SAndrew Thompson /* data structures and routines
231202ac6454SAndrew Thompson  * to emulate the root hub:
231302ac6454SAndrew Thompson  */
231402ac6454SAndrew Thompson 
231502ac6454SAndrew Thompson static const
231602ac6454SAndrew Thompson struct usb2_device_descriptor uhci_devd =
231702ac6454SAndrew Thompson {
231802ac6454SAndrew Thompson 	sizeof(struct usb2_device_descriptor),
231902ac6454SAndrew Thompson 	UDESC_DEVICE,			/* type */
232002ac6454SAndrew Thompson 	{0x00, 0x01},			/* USB version */
232102ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
232202ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
232302ac6454SAndrew Thompson 	UDPROTO_FSHUB,			/* protocol */
232402ac6454SAndrew Thompson 	64,				/* max packet */
232502ac6454SAndrew Thompson 	{0}, {0}, {0x00, 0x01},		/* device id */
232602ac6454SAndrew Thompson 	1, 2, 0,			/* string indicies */
232702ac6454SAndrew Thompson 	1				/* # of configurations */
232802ac6454SAndrew Thompson };
232902ac6454SAndrew Thompson 
233002ac6454SAndrew Thompson static const struct uhci_config_desc uhci_confd = {
233102ac6454SAndrew Thompson 	.confd = {
233202ac6454SAndrew Thompson 		.bLength = sizeof(struct usb2_config_descriptor),
233302ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
233402ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(uhci_confd),
233502ac6454SAndrew Thompson 		.bNumInterface = 1,
233602ac6454SAndrew Thompson 		.bConfigurationValue = 1,
233702ac6454SAndrew Thompson 		.iConfiguration = 0,
233802ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
233902ac6454SAndrew Thompson 		.bMaxPower = 0		/* max power */
234002ac6454SAndrew Thompson 	},
234102ac6454SAndrew Thompson 
234202ac6454SAndrew Thompson 	.ifcd = {
234302ac6454SAndrew Thompson 		.bLength = sizeof(struct usb2_interface_descriptor),
234402ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
234502ac6454SAndrew Thompson 		.bNumEndpoints = 1,
234602ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
234702ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
234802ac6454SAndrew Thompson 		.bInterfaceProtocol = UIPROTO_FSHUB,
234902ac6454SAndrew Thompson 	},
235002ac6454SAndrew Thompson 
235102ac6454SAndrew Thompson 	.endpd = {
235202ac6454SAndrew Thompson 		.bLength = sizeof(struct usb2_endpoint_descriptor),
235302ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
235402ac6454SAndrew Thompson 		.bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
235502ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
235602ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
235702ac6454SAndrew Thompson 		.bInterval = 255,
235802ac6454SAndrew Thompson 	},
235902ac6454SAndrew Thompson };
236002ac6454SAndrew Thompson 
236102ac6454SAndrew Thompson static const
236202ac6454SAndrew Thompson struct usb2_hub_descriptor_min uhci_hubd_piix =
236302ac6454SAndrew Thompson {
236402ac6454SAndrew Thompson 	sizeof(uhci_hubd_piix),
236502ac6454SAndrew Thompson 	UDESC_HUB,
236602ac6454SAndrew Thompson 	2,
236702ac6454SAndrew Thompson 	{UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
236802ac6454SAndrew Thompson 	50,				/* power on to power good */
236902ac6454SAndrew Thompson 	0,
237002ac6454SAndrew Thompson 	{0x00},				/* both ports are removable */
237102ac6454SAndrew Thompson };
237202ac6454SAndrew Thompson 
237302ac6454SAndrew Thompson /*
237402ac6454SAndrew Thompson  * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
237502ac6454SAndrew Thompson  * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
237602ac6454SAndrew Thompson  * should not be used by the USB subsystem.  As we cannot issue a
237702ac6454SAndrew Thompson  * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
237802ac6454SAndrew Thompson  * will be enabled as part of the reset.
237902ac6454SAndrew Thompson  *
238002ac6454SAndrew Thompson  * On the VT83C572, the port cannot be successfully enabled until the
238102ac6454SAndrew Thompson  * outstanding "port enable change" and "connection status change"
238202ac6454SAndrew Thompson  * events have been reset.
238302ac6454SAndrew Thompson  */
238402ac6454SAndrew Thompson static usb2_error_t
238502ac6454SAndrew Thompson uhci_portreset(uhci_softc_t *sc, uint16_t index, uint8_t use_polling)
238602ac6454SAndrew Thompson {
238702ac6454SAndrew Thompson 	uint16_t port;
238802ac6454SAndrew Thompson 	uint16_t x;
238902ac6454SAndrew Thompson 	uint8_t lim;
239002ac6454SAndrew Thompson 
239102ac6454SAndrew Thompson 	if (index == 1)
239202ac6454SAndrew Thompson 		port = UHCI_PORTSC1;
239302ac6454SAndrew Thompson 	else if (index == 2)
239402ac6454SAndrew Thompson 		port = UHCI_PORTSC2;
239502ac6454SAndrew Thompson 	else
239602ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
239702ac6454SAndrew Thompson 
239802ac6454SAndrew Thompson 	/*
239902ac6454SAndrew Thompson 	 * Before we do anything, turn on SOF messages on the USB
240002ac6454SAndrew Thompson 	 * BUS. Some USB devices do not cope without them!
240102ac6454SAndrew Thompson 	 */
240202ac6454SAndrew Thompson   	if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS)) {
240302ac6454SAndrew Thompson 
240402ac6454SAndrew Thompson 		DPRINTF("Activating SOFs!\n");
240502ac6454SAndrew Thompson 
240602ac6454SAndrew Thompson 		UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
240702ac6454SAndrew Thompson 
240802ac6454SAndrew Thompson 		/* wait a little bit */
240902ac6454SAndrew Thompson 		if (use_polling) {
241002ac6454SAndrew Thompson 			DELAY(10000);
241102ac6454SAndrew Thompson 		} else {
241202ac6454SAndrew Thompson 			usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
241302ac6454SAndrew Thompson 		}
241402ac6454SAndrew Thompson 	}
241502ac6454SAndrew Thompson 
241602ac6454SAndrew Thompson 	x = URWMASK(UREAD2(sc, port));
241702ac6454SAndrew Thompson 	UWRITE2(sc, port, x | UHCI_PORTSC_PR);
241802ac6454SAndrew Thompson 
241902ac6454SAndrew Thompson 	if (use_polling) {
242002ac6454SAndrew Thompson 		/* polling */
242102ac6454SAndrew Thompson 		DELAY(USB_PORT_ROOT_RESET_DELAY * 1000);
242202ac6454SAndrew Thompson 	} else {
242302ac6454SAndrew Thompson 		usb2_pause_mtx(&sc->sc_bus.bus_mtx,
242402ac6454SAndrew Thompson 		    USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
242502ac6454SAndrew Thompson 	}
242602ac6454SAndrew Thompson 
242702ac6454SAndrew Thompson 	DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
242802ac6454SAndrew Thompson 	    index, UREAD2(sc, port));
242902ac6454SAndrew Thompson 
243002ac6454SAndrew Thompson 	x = URWMASK(UREAD2(sc, port));
243102ac6454SAndrew Thompson 	UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
243202ac6454SAndrew Thompson 
243302ac6454SAndrew Thompson 
243402ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_bus.bus_mtx);
243502ac6454SAndrew Thompson 
243602ac6454SAndrew Thompson 	/*
243702ac6454SAndrew Thompson 	 * This delay needs to be exactly 100us, else some USB devices
243802ac6454SAndrew Thompson 	 * fail to attach!
243902ac6454SAndrew Thompson 	 */
244002ac6454SAndrew Thompson 	DELAY(100);
244102ac6454SAndrew Thompson 
244202ac6454SAndrew Thompson 	mtx_lock(&sc->sc_bus.bus_mtx);
244302ac6454SAndrew Thompson 
244402ac6454SAndrew Thompson 	DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
244502ac6454SAndrew Thompson 	    index, UREAD2(sc, port));
244602ac6454SAndrew Thompson 
244702ac6454SAndrew Thompson 	x = URWMASK(UREAD2(sc, port));
244802ac6454SAndrew Thompson 	UWRITE2(sc, port, x | UHCI_PORTSC_PE);
244902ac6454SAndrew Thompson 
245002ac6454SAndrew Thompson 	for (lim = 0; lim < 12; lim++) {
245102ac6454SAndrew Thompson 
245202ac6454SAndrew Thompson 		if (use_polling) {
245302ac6454SAndrew Thompson 			/* polling */
245402ac6454SAndrew Thompson 			DELAY(USB_PORT_RESET_DELAY * 1000);
245502ac6454SAndrew Thompson 		} else {
245602ac6454SAndrew Thompson 			usb2_pause_mtx(&sc->sc_bus.bus_mtx,
245702ac6454SAndrew Thompson 			    USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
245802ac6454SAndrew Thompson 		}
245902ac6454SAndrew Thompson 
246002ac6454SAndrew Thompson 		x = UREAD2(sc, port);
246102ac6454SAndrew Thompson 
246202ac6454SAndrew Thompson 		DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
246302ac6454SAndrew Thompson 		    index, lim, x);
246402ac6454SAndrew Thompson 
246502ac6454SAndrew Thompson 		if (!(x & UHCI_PORTSC_CCS)) {
246602ac6454SAndrew Thompson 			/*
246702ac6454SAndrew Thompson 			 * No device is connected (or was disconnected
246802ac6454SAndrew Thompson 			 * during reset).  Consider the port reset.
246902ac6454SAndrew Thompson 			 * The delay must be long enough to ensure on
247002ac6454SAndrew Thompson 			 * the initial iteration that the device
247102ac6454SAndrew Thompson 			 * connection will have been registered.  50ms
247202ac6454SAndrew Thompson 			 * appears to be sufficient, but 20ms is not.
247302ac6454SAndrew Thompson 			 */
247402ac6454SAndrew Thompson 			DPRINTFN(4, "uhci port %d loop %u, device detached\n",
247502ac6454SAndrew Thompson 			    index, lim);
247602ac6454SAndrew Thompson 			goto done;
247702ac6454SAndrew Thompson 		}
247802ac6454SAndrew Thompson 		if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
247902ac6454SAndrew Thompson 			/*
248002ac6454SAndrew Thompson 			 * Port enabled changed and/or connection
248102ac6454SAndrew Thompson 			 * status changed were set.  Reset either or
248202ac6454SAndrew Thompson 			 * both raised flags (by writing a 1 to that
248302ac6454SAndrew Thompson 			 * bit), and wait again for state to settle.
248402ac6454SAndrew Thompson 			 */
248502ac6454SAndrew Thompson 			UWRITE2(sc, port, URWMASK(x) |
248602ac6454SAndrew Thompson 			    (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
248702ac6454SAndrew Thompson 			continue;
248802ac6454SAndrew Thompson 		}
248902ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_PE) {
249002ac6454SAndrew Thompson 			/* port is enabled */
249102ac6454SAndrew Thompson 			goto done;
249202ac6454SAndrew Thompson 		}
249302ac6454SAndrew Thompson 		UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
249402ac6454SAndrew Thompson 	}
249502ac6454SAndrew Thompson 
249602ac6454SAndrew Thompson 	DPRINTFN(2, "uhci port %d reset timed out\n", index);
249702ac6454SAndrew Thompson 	return (USB_ERR_TIMEOUT);
249802ac6454SAndrew Thompson 
249902ac6454SAndrew Thompson done:
250002ac6454SAndrew Thompson 	DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
250102ac6454SAndrew Thompson 	    index, UREAD2(sc, port));
250202ac6454SAndrew Thompson 
250302ac6454SAndrew Thompson 	sc->sc_isreset = 1;
250402ac6454SAndrew Thompson 	return (USB_ERR_NORMAL_COMPLETION);
250502ac6454SAndrew Thompson }
250602ac6454SAndrew Thompson 
250702ac6454SAndrew Thompson static void
250802ac6454SAndrew Thompson uhci_root_ctrl_enter(struct usb2_xfer *xfer)
250902ac6454SAndrew Thompson {
251002ac6454SAndrew Thompson 	return;
251102ac6454SAndrew Thompson }
251202ac6454SAndrew Thompson 
251302ac6454SAndrew Thompson static void
251402ac6454SAndrew Thompson uhci_root_ctrl_start(struct usb2_xfer *xfer)
251502ac6454SAndrew Thompson {
251602ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
251702ac6454SAndrew Thompson 
251802ac6454SAndrew Thompson 	DPRINTF("\n");
251902ac6454SAndrew Thompson 
252002ac6454SAndrew Thompson 	sc->sc_root_ctrl.xfer = xfer;
252102ac6454SAndrew Thompson 
252202ac6454SAndrew Thompson 	usb2_bus_roothub_exec(xfer->xroot->bus);
252302ac6454SAndrew Thompson }
252402ac6454SAndrew Thompson 
252502ac6454SAndrew Thompson static void
252602ac6454SAndrew Thompson uhci_root_ctrl_task(struct usb2_bus *bus)
252702ac6454SAndrew Thompson {
252802ac6454SAndrew Thompson 	uhci_root_ctrl_poll(UHCI_BUS2SC(bus));
252902ac6454SAndrew Thompson }
253002ac6454SAndrew Thompson 
253102ac6454SAndrew Thompson static void
253202ac6454SAndrew Thompson uhci_root_ctrl_done(struct usb2_xfer *xfer,
253302ac6454SAndrew Thompson     struct usb2_sw_transfer *std)
253402ac6454SAndrew Thompson {
253502ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
253602ac6454SAndrew Thompson 	char *ptr;
253702ac6454SAndrew Thompson 	uint16_t x;
253802ac6454SAndrew Thompson 	uint16_t port;
253902ac6454SAndrew Thompson 	uint16_t value;
254002ac6454SAndrew Thompson 	uint16_t index;
254102ac6454SAndrew Thompson 	uint16_t status;
254202ac6454SAndrew Thompson 	uint16_t change;
254302ac6454SAndrew Thompson 	uint8_t use_polling;
254402ac6454SAndrew Thompson 
254502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
254602ac6454SAndrew Thompson 
254702ac6454SAndrew Thompson 	if (std->state != USB_SW_TR_SETUP) {
254802ac6454SAndrew Thompson 		if (std->state == USB_SW_TR_PRE_CALLBACK) {
254902ac6454SAndrew Thompson 			/* transfer transferred */
255002ac6454SAndrew Thompson 			uhci_device_done(xfer, std->err);
255102ac6454SAndrew Thompson 		}
255202ac6454SAndrew Thompson 		goto done;
255302ac6454SAndrew Thompson 	}
255402ac6454SAndrew Thompson 	/* buffer reset */
255502ac6454SAndrew Thompson 	std->ptr = sc->sc_hub_desc.temp;
255602ac6454SAndrew Thompson 	std->len = 0;
255702ac6454SAndrew Thompson 
255802ac6454SAndrew Thompson 	value = UGETW(std->req.wValue);
255902ac6454SAndrew Thompson 	index = UGETW(std->req.wIndex);
256002ac6454SAndrew Thompson 
256102ac6454SAndrew Thompson 	use_polling = mtx_owned(xfer->xroot->xfer_mtx) ? 1 : 0;
256202ac6454SAndrew Thompson 
256302ac6454SAndrew Thompson 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
256402ac6454SAndrew Thompson 	    "wValue=0x%04x wIndex=0x%04x\n",
256502ac6454SAndrew Thompson 	    std->req.bmRequestType, std->req.bRequest,
256602ac6454SAndrew Thompson 	    UGETW(std->req.wLength), value, index);
256702ac6454SAndrew Thompson 
256802ac6454SAndrew Thompson #define	C(x,y) ((x) | ((y) << 8))
256902ac6454SAndrew Thompson 	switch (C(std->req.bRequest, std->req.bmRequestType)) {
257002ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
257102ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
257202ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
257302ac6454SAndrew Thompson 		/*
257402ac6454SAndrew Thompson 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
257502ac6454SAndrew Thompson 		 * for the integrated root hub.
257602ac6454SAndrew Thompson 		 */
257702ac6454SAndrew Thompson 		break;
257802ac6454SAndrew Thompson 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
257902ac6454SAndrew Thompson 		std->len = 1;
258002ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
258102ac6454SAndrew Thompson 		break;
258202ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
258302ac6454SAndrew Thompson 		switch (value >> 8) {
258402ac6454SAndrew Thompson 		case UDESC_DEVICE:
258502ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
258602ac6454SAndrew Thompson 				std->err = USB_ERR_IOERROR;
258702ac6454SAndrew Thompson 				goto done;
258802ac6454SAndrew Thompson 			}
258902ac6454SAndrew Thompson 			std->len = sizeof(uhci_devd);
259002ac6454SAndrew Thompson 			sc->sc_hub_desc.devd = uhci_devd;
259102ac6454SAndrew Thompson 			break;
259202ac6454SAndrew Thompson 
259302ac6454SAndrew Thompson 		case UDESC_CONFIG:
259402ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
259502ac6454SAndrew Thompson 				std->err = USB_ERR_IOERROR;
259602ac6454SAndrew Thompson 				goto done;
259702ac6454SAndrew Thompson 			}
259802ac6454SAndrew Thompson 			std->len = sizeof(uhci_confd);
259902ac6454SAndrew Thompson 			std->ptr = USB_ADD_BYTES(&uhci_confd, 0);
260002ac6454SAndrew Thompson 			break;
260102ac6454SAndrew Thompson 
260202ac6454SAndrew Thompson 		case UDESC_STRING:
260302ac6454SAndrew Thompson 			switch (value & 0xff) {
260402ac6454SAndrew Thompson 			case 0:	/* Language table */
260502ac6454SAndrew Thompson 				ptr = "\001";
260602ac6454SAndrew Thompson 				break;
260702ac6454SAndrew Thompson 
260802ac6454SAndrew Thompson 			case 1:	/* Vendor */
260902ac6454SAndrew Thompson 				ptr = sc->sc_vendor;
261002ac6454SAndrew Thompson 				break;
261102ac6454SAndrew Thompson 
261202ac6454SAndrew Thompson 			case 2:	/* Product */
261302ac6454SAndrew Thompson 				ptr = "UHCI root HUB";
261402ac6454SAndrew Thompson 				break;
261502ac6454SAndrew Thompson 
261602ac6454SAndrew Thompson 			default:
261702ac6454SAndrew Thompson 				ptr = "";
261802ac6454SAndrew Thompson 				break;
261902ac6454SAndrew Thompson 			}
262002ac6454SAndrew Thompson 
262102ac6454SAndrew Thompson 			std->len = usb2_make_str_desc
262202ac6454SAndrew Thompson 			    (sc->sc_hub_desc.temp,
262302ac6454SAndrew Thompson 			    sizeof(sc->sc_hub_desc.temp),
262402ac6454SAndrew Thompson 			    ptr);
262502ac6454SAndrew Thompson 			break;
262602ac6454SAndrew Thompson 
262702ac6454SAndrew Thompson 		default:
262802ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
262902ac6454SAndrew Thompson 			goto done;
263002ac6454SAndrew Thompson 		}
263102ac6454SAndrew Thompson 		break;
263202ac6454SAndrew Thompson 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
263302ac6454SAndrew Thompson 		std->len = 1;
263402ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = 0;
263502ac6454SAndrew Thompson 		break;
263602ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_DEVICE):
263702ac6454SAndrew Thompson 		std->len = 2;
263802ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
263902ac6454SAndrew Thompson 		break;
264002ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
264102ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
264202ac6454SAndrew Thompson 		std->len = 2;
264302ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
264402ac6454SAndrew Thompson 		break;
264502ac6454SAndrew Thompson 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
264602ac6454SAndrew Thompson 		if (value >= USB_MAX_DEVICES) {
264702ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
264802ac6454SAndrew Thompson 			goto done;
264902ac6454SAndrew Thompson 		}
265002ac6454SAndrew Thompson 		sc->sc_addr = value;
265102ac6454SAndrew Thompson 		break;
265202ac6454SAndrew Thompson 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
265302ac6454SAndrew Thompson 		if ((value != 0) && (value != 1)) {
265402ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
265502ac6454SAndrew Thompson 			goto done;
265602ac6454SAndrew Thompson 		}
265702ac6454SAndrew Thompson 		sc->sc_conf = value;
265802ac6454SAndrew Thompson 		break;
265902ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
266002ac6454SAndrew Thompson 		break;
266102ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
266202ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
266302ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
266402ac6454SAndrew Thompson 		std->err = USB_ERR_IOERROR;
266502ac6454SAndrew Thompson 		goto done;
266602ac6454SAndrew Thompson 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
266702ac6454SAndrew Thompson 		break;
266802ac6454SAndrew Thompson 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
266902ac6454SAndrew Thompson 		break;
267002ac6454SAndrew Thompson 		/* Hub requests */
267102ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
267202ac6454SAndrew Thompson 		break;
267302ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
267402ac6454SAndrew Thompson 		DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
267502ac6454SAndrew Thompson 		    "port=%d feature=%d\n",
267602ac6454SAndrew Thompson 		    index, value);
267702ac6454SAndrew Thompson 		if (index == 1)
267802ac6454SAndrew Thompson 			port = UHCI_PORTSC1;
267902ac6454SAndrew Thompson 		else if (index == 2)
268002ac6454SAndrew Thompson 			port = UHCI_PORTSC2;
268102ac6454SAndrew Thompson 		else {
268202ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
268302ac6454SAndrew Thompson 			goto done;
268402ac6454SAndrew Thompson 		}
268502ac6454SAndrew Thompson 		switch (value) {
268602ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
268702ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
268802ac6454SAndrew Thompson 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
268902ac6454SAndrew Thompson 			break;
269002ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
269102ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
269202ac6454SAndrew Thompson 			UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
269302ac6454SAndrew Thompson 			break;
269402ac6454SAndrew Thompson 		case UHF_PORT_RESET:
269502ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
269602ac6454SAndrew Thompson 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
269702ac6454SAndrew Thompson 			break;
269802ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
269902ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
270002ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
270102ac6454SAndrew Thompson 			break;
270202ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
270302ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
270402ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
270502ac6454SAndrew Thompson 			break;
270602ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
270702ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
270802ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
270902ac6454SAndrew Thompson 			break;
271002ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
271102ac6454SAndrew Thompson 			sc->sc_isreset = 0;
271202ac6454SAndrew Thompson 			std->err = USB_ERR_NORMAL_COMPLETION;
271302ac6454SAndrew Thompson 			goto done;
271402ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
271502ac6454SAndrew Thompson 			sc->sc_isresumed &= ~(1 << index);
271602ac6454SAndrew Thompson 			break;
271702ac6454SAndrew Thompson 		case UHF_PORT_CONNECTION:
271802ac6454SAndrew Thompson 		case UHF_PORT_OVER_CURRENT:
271902ac6454SAndrew Thompson 		case UHF_PORT_POWER:
272002ac6454SAndrew Thompson 		case UHF_PORT_LOW_SPEED:
272102ac6454SAndrew Thompson 		default:
272202ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
272302ac6454SAndrew Thompson 			goto done;
272402ac6454SAndrew Thompson 		}
272502ac6454SAndrew Thompson 		break;
272602ac6454SAndrew Thompson 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
272702ac6454SAndrew Thompson 		if (index == 1)
272802ac6454SAndrew Thompson 			port = UHCI_PORTSC1;
272902ac6454SAndrew Thompson 		else if (index == 2)
273002ac6454SAndrew Thompson 			port = UHCI_PORTSC2;
273102ac6454SAndrew Thompson 		else {
273202ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
273302ac6454SAndrew Thompson 			goto done;
273402ac6454SAndrew Thompson 		}
273502ac6454SAndrew Thompson 		std->len = 1;
273602ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] =
273702ac6454SAndrew Thompson 		    ((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
273802ac6454SAndrew Thompson 		    UHCI_PORTSC_LS_SHIFT);
273902ac6454SAndrew Thompson 		break;
274002ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
274102ac6454SAndrew Thompson 		if ((value & 0xff) != 0) {
274202ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
274302ac6454SAndrew Thompson 			goto done;
274402ac6454SAndrew Thompson 		}
274502ac6454SAndrew Thompson 		std->len = sizeof(uhci_hubd_piix);
274602ac6454SAndrew Thompson 		std->ptr = USB_ADD_BYTES(&uhci_hubd_piix, 0);
274702ac6454SAndrew Thompson 		break;
274802ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
274902ac6454SAndrew Thompson 		std->len = 16;
275002ac6454SAndrew Thompson 		bzero(sc->sc_hub_desc.temp, 16);
275102ac6454SAndrew Thompson 		break;
275202ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
275302ac6454SAndrew Thompson 		if (index == 1)
275402ac6454SAndrew Thompson 			port = UHCI_PORTSC1;
275502ac6454SAndrew Thompson 		else if (index == 2)
275602ac6454SAndrew Thompson 			port = UHCI_PORTSC2;
275702ac6454SAndrew Thompson 		else {
275802ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
275902ac6454SAndrew Thompson 			goto done;
276002ac6454SAndrew Thompson 		}
276102ac6454SAndrew Thompson 		x = UREAD2(sc, port);
276202ac6454SAndrew Thompson 		status = change = 0;
276302ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_CCS)
276402ac6454SAndrew Thompson 			status |= UPS_CURRENT_CONNECT_STATUS;
276502ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_CSC)
276602ac6454SAndrew Thompson 			change |= UPS_C_CONNECT_STATUS;
276702ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_PE)
276802ac6454SAndrew Thompson 			status |= UPS_PORT_ENABLED;
276902ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_POEDC)
277002ac6454SAndrew Thompson 			change |= UPS_C_PORT_ENABLED;
277102ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_OCI)
277202ac6454SAndrew Thompson 			status |= UPS_OVERCURRENT_INDICATOR;
277302ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_OCIC)
277402ac6454SAndrew Thompson 			change |= UPS_C_OVERCURRENT_INDICATOR;
277502ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_LSDA)
277602ac6454SAndrew Thompson 			status |= UPS_LOW_SPEED;
277702ac6454SAndrew Thompson 		if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
277802ac6454SAndrew Thompson 			/* need to do a write back */
277902ac6454SAndrew Thompson 			UWRITE2(sc, port, URWMASK(x));
278002ac6454SAndrew Thompson 
278102ac6454SAndrew Thompson 			/* wait 20ms for resume sequence to complete */
278202ac6454SAndrew Thompson 			if (use_polling) {
278302ac6454SAndrew Thompson 				/* polling */
278402ac6454SAndrew Thompson 				DELAY(20000);
278502ac6454SAndrew Thompson 			} else {
278602ac6454SAndrew Thompson 				usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
278702ac6454SAndrew Thompson 			}
278802ac6454SAndrew Thompson 
278902ac6454SAndrew Thompson 			/* clear suspend and resume detect */
279002ac6454SAndrew Thompson 			UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
279102ac6454SAndrew Thompson 			    UHCI_PORTSC_SUSP));
279202ac6454SAndrew Thompson 
279302ac6454SAndrew Thompson 			/* wait a little bit */
279402ac6454SAndrew Thompson 			usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500);
279502ac6454SAndrew Thompson 
279602ac6454SAndrew Thompson 			sc->sc_isresumed |= (1 << index);
279702ac6454SAndrew Thompson 
279802ac6454SAndrew Thompson 		} else if (x & UHCI_PORTSC_SUSP) {
279902ac6454SAndrew Thompson 			status |= UPS_SUSPEND;
280002ac6454SAndrew Thompson 		}
280102ac6454SAndrew Thompson 		status |= UPS_PORT_POWER;
280202ac6454SAndrew Thompson 		if (sc->sc_isresumed & (1 << index))
280302ac6454SAndrew Thompson 			change |= UPS_C_SUSPEND;
280402ac6454SAndrew Thompson 		if (sc->sc_isreset)
280502ac6454SAndrew Thompson 			change |= UPS_C_PORT_RESET;
280602ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortStatus, status);
280702ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortChange, change);
280802ac6454SAndrew Thompson 		std->len = sizeof(sc->sc_hub_desc.ps);
280902ac6454SAndrew Thompson 		break;
281002ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
281102ac6454SAndrew Thompson 		std->err = USB_ERR_IOERROR;
281202ac6454SAndrew Thompson 		goto done;
281302ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
281402ac6454SAndrew Thompson 		break;
281502ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
281602ac6454SAndrew Thompson 		if (index == 1)
281702ac6454SAndrew Thompson 			port = UHCI_PORTSC1;
281802ac6454SAndrew Thompson 		else if (index == 2)
281902ac6454SAndrew Thompson 			port = UHCI_PORTSC2;
282002ac6454SAndrew Thompson 		else {
282102ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
282202ac6454SAndrew Thompson 			goto done;
282302ac6454SAndrew Thompson 		}
282402ac6454SAndrew Thompson 		switch (value) {
282502ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
282602ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
282702ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_PE);
282802ac6454SAndrew Thompson 			break;
282902ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
283002ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
283102ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
283202ac6454SAndrew Thompson 			break;
283302ac6454SAndrew Thompson 		case UHF_PORT_RESET:
283402ac6454SAndrew Thompson 			std->err = uhci_portreset(sc, index, use_polling);
283502ac6454SAndrew Thompson 			goto done;
283602ac6454SAndrew Thompson 		case UHF_PORT_POWER:
283702ac6454SAndrew Thompson 			/* pretend we turned on power */
283802ac6454SAndrew Thompson 			std->err = USB_ERR_NORMAL_COMPLETION;
283902ac6454SAndrew Thompson 			goto done;
284002ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
284102ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
284202ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
284302ac6454SAndrew Thompson 		case UHF_PORT_CONNECTION:
284402ac6454SAndrew Thompson 		case UHF_PORT_OVER_CURRENT:
284502ac6454SAndrew Thompson 		case UHF_PORT_LOW_SPEED:
284602ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
284702ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
284802ac6454SAndrew Thompson 		default:
284902ac6454SAndrew Thompson 			std->err = USB_ERR_IOERROR;
285002ac6454SAndrew Thompson 			goto done;
285102ac6454SAndrew Thompson 		}
285202ac6454SAndrew Thompson 		break;
285302ac6454SAndrew Thompson 	default:
285402ac6454SAndrew Thompson 		std->err = USB_ERR_IOERROR;
285502ac6454SAndrew Thompson 		goto done;
285602ac6454SAndrew Thompson 	}
285702ac6454SAndrew Thompson done:
285802ac6454SAndrew Thompson 	return;
285902ac6454SAndrew Thompson }
286002ac6454SAndrew Thompson 
286102ac6454SAndrew Thompson static void
286202ac6454SAndrew Thompson uhci_root_ctrl_poll(struct uhci_softc *sc)
286302ac6454SAndrew Thompson {
286402ac6454SAndrew Thompson 	usb2_sw_transfer(&sc->sc_root_ctrl,
286502ac6454SAndrew Thompson 	    &uhci_root_ctrl_done);
286602ac6454SAndrew Thompson }
286702ac6454SAndrew Thompson 
286802ac6454SAndrew Thompson struct usb2_pipe_methods uhci_root_ctrl_methods =
286902ac6454SAndrew Thompson {
287002ac6454SAndrew Thompson 	.open = uhci_root_ctrl_open,
287102ac6454SAndrew Thompson 	.close = uhci_root_ctrl_close,
287202ac6454SAndrew Thompson 	.enter = uhci_root_ctrl_enter,
287302ac6454SAndrew Thompson 	.start = uhci_root_ctrl_start,
287402ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
287502ac6454SAndrew Thompson 	.start_is_cancelable = 0,
287602ac6454SAndrew Thompson };
287702ac6454SAndrew Thompson 
287802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
287902ac6454SAndrew Thompson  * uhci root interrupt support
288002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
288102ac6454SAndrew Thompson static void
288202ac6454SAndrew Thompson uhci_root_intr_open(struct usb2_xfer *xfer)
288302ac6454SAndrew Thompson {
288402ac6454SAndrew Thompson 	return;
288502ac6454SAndrew Thompson }
288602ac6454SAndrew Thompson 
288702ac6454SAndrew Thompson static void
288802ac6454SAndrew Thompson uhci_root_intr_close(struct usb2_xfer *xfer)
288902ac6454SAndrew Thompson {
289002ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
289102ac6454SAndrew Thompson 
289202ac6454SAndrew Thompson 	if (sc->sc_root_intr.xfer == xfer) {
289302ac6454SAndrew Thompson 		sc->sc_root_intr.xfer = NULL;
289402ac6454SAndrew Thompson 	}
289502ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
289602ac6454SAndrew Thompson }
289702ac6454SAndrew Thompson 
289802ac6454SAndrew Thompson static void
289902ac6454SAndrew Thompson uhci_root_intr_enter(struct usb2_xfer *xfer)
290002ac6454SAndrew Thompson {
290102ac6454SAndrew Thompson 	return;
290202ac6454SAndrew Thompson }
290302ac6454SAndrew Thompson 
290402ac6454SAndrew Thompson static void
290502ac6454SAndrew Thompson uhci_root_intr_start(struct usb2_xfer *xfer)
290602ac6454SAndrew Thompson {
290702ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
290802ac6454SAndrew Thompson 
290902ac6454SAndrew Thompson 	sc->sc_root_intr.xfer = xfer;
291002ac6454SAndrew Thompson 
291102ac6454SAndrew Thompson 	usb2_transfer_timeout_ms(xfer,
291202ac6454SAndrew Thompson 	    &uhci_root_intr_check, xfer->interval);
291302ac6454SAndrew Thompson }
291402ac6454SAndrew Thompson 
291502ac6454SAndrew Thompson static void
291602ac6454SAndrew Thompson uhci_root_intr_done(struct usb2_xfer *xfer,
291702ac6454SAndrew Thompson     struct usb2_sw_transfer *std)
291802ac6454SAndrew Thompson {
291902ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
292002ac6454SAndrew Thompson 
292102ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
292202ac6454SAndrew Thompson 
292302ac6454SAndrew Thompson 	if (std->state != USB_SW_TR_PRE_DATA) {
292402ac6454SAndrew Thompson 		if (std->state == USB_SW_TR_PRE_CALLBACK) {
292502ac6454SAndrew Thompson 			/* transfer is transferred */
292602ac6454SAndrew Thompson 			uhci_device_done(xfer, std->err);
292702ac6454SAndrew Thompson 		}
292802ac6454SAndrew Thompson 		goto done;
292902ac6454SAndrew Thompson 	}
293002ac6454SAndrew Thompson 	/* setup buffer */
293102ac6454SAndrew Thompson 	std->ptr = sc->sc_hub_idata;
293202ac6454SAndrew Thompson 	std->len = sizeof(sc->sc_hub_idata);
293302ac6454SAndrew Thompson done:
293402ac6454SAndrew Thompson 	return;
293502ac6454SAndrew Thompson }
293602ac6454SAndrew Thompson 
293702ac6454SAndrew Thompson /*
293802ac6454SAndrew Thompson  * this routine is executed periodically and simulates interrupts
293902ac6454SAndrew Thompson  * from the root controller interrupt pipe for port status change
294002ac6454SAndrew Thompson  */
294102ac6454SAndrew Thompson static void
294202ac6454SAndrew Thompson uhci_root_intr_check(void *arg)
294302ac6454SAndrew Thompson {
294402ac6454SAndrew Thompson 	struct usb2_xfer *xfer = arg;
294502ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
294602ac6454SAndrew Thompson 
294702ac6454SAndrew Thompson 	DPRINTFN(21, "\n");
294802ac6454SAndrew Thompson 
294902ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
295002ac6454SAndrew Thompson 
295102ac6454SAndrew Thompson 	sc->sc_hub_idata[0] = 0;
295202ac6454SAndrew Thompson 
295302ac6454SAndrew Thompson 	if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
295402ac6454SAndrew Thompson 	    UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
295502ac6454SAndrew Thompson 		sc->sc_hub_idata[0] |= 1 << 1;
295602ac6454SAndrew Thompson 	}
295702ac6454SAndrew Thompson 	if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
295802ac6454SAndrew Thompson 	    UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
295902ac6454SAndrew Thompson 		sc->sc_hub_idata[0] |= 1 << 2;
296002ac6454SAndrew Thompson 	}
296102ac6454SAndrew Thompson 	if (sc->sc_hub_idata[0] == 0) {
296202ac6454SAndrew Thompson 		/*
296302ac6454SAndrew Thompson 		 * no change or controller not running, try again in a while
296402ac6454SAndrew Thompson 		 */
296502ac6454SAndrew Thompson 		uhci_root_intr_start(xfer);
296602ac6454SAndrew Thompson 	} else {
296702ac6454SAndrew Thompson 		usb2_sw_transfer(&sc->sc_root_intr,
296802ac6454SAndrew Thompson 		    &uhci_root_intr_done);
296902ac6454SAndrew Thompson 	}
297002ac6454SAndrew Thompson }
297102ac6454SAndrew Thompson 
297202ac6454SAndrew Thompson struct usb2_pipe_methods uhci_root_intr_methods =
297302ac6454SAndrew Thompson {
297402ac6454SAndrew Thompson 	.open = uhci_root_intr_open,
297502ac6454SAndrew Thompson 	.close = uhci_root_intr_close,
297602ac6454SAndrew Thompson 	.enter = uhci_root_intr_enter,
297702ac6454SAndrew Thompson 	.start = uhci_root_intr_start,
297802ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
297902ac6454SAndrew Thompson 	.start_is_cancelable = 1,
298002ac6454SAndrew Thompson };
298102ac6454SAndrew Thompson 
298202ac6454SAndrew Thompson static void
298302ac6454SAndrew Thompson uhci_xfer_setup(struct usb2_setup_params *parm)
298402ac6454SAndrew Thompson {
298502ac6454SAndrew Thompson 	struct usb2_page_search page_info;
298602ac6454SAndrew Thompson 	struct usb2_page_cache *pc;
298702ac6454SAndrew Thompson 	uhci_softc_t *sc;
298802ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
298902ac6454SAndrew Thompson 	void *last_obj;
299002ac6454SAndrew Thompson 	uint32_t ntd;
299102ac6454SAndrew Thompson 	uint32_t nqh;
299202ac6454SAndrew Thompson 	uint32_t nfixup;
299302ac6454SAndrew Thompson 	uint32_t n;
299402ac6454SAndrew Thompson 	uint16_t align;
299502ac6454SAndrew Thompson 
299602ac6454SAndrew Thompson 	sc = UHCI_BUS2SC(parm->udev->bus);
299702ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
299802ac6454SAndrew Thompson 
299902ac6454SAndrew Thompson 	parm->hc_max_packet_size = 0x500;
300002ac6454SAndrew Thompson 	parm->hc_max_packet_count = 1;
300102ac6454SAndrew Thompson 	parm->hc_max_frame_size = 0x500;
300202ac6454SAndrew Thompson 
300302ac6454SAndrew Thompson 	/*
300402ac6454SAndrew Thompson 	 * compute ntd and nqh
300502ac6454SAndrew Thompson 	 */
300602ac6454SAndrew Thompson 	if (parm->methods == &uhci_device_ctrl_methods) {
300702ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
300802ac6454SAndrew Thompson 		xfer->flags_int.bdma_no_post_sync = 1;
300902ac6454SAndrew Thompson 
301002ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
301102ac6454SAndrew Thompson 
301202ac6454SAndrew Thompson 		/* see EHCI HC driver for proof of "ntd" formula */
301302ac6454SAndrew Thompson 
301402ac6454SAndrew Thompson 		nqh = 1;
301502ac6454SAndrew Thompson 		ntd = ((2 * xfer->nframes) + 1	/* STATUS */
301602ac6454SAndrew Thompson 		    + (xfer->max_data_length / xfer->max_frame_size));
301702ac6454SAndrew Thompson 
301802ac6454SAndrew Thompson 	} else if (parm->methods == &uhci_device_bulk_methods) {
301902ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
302002ac6454SAndrew Thompson 		xfer->flags_int.bdma_no_post_sync = 1;
302102ac6454SAndrew Thompson 
302202ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
302302ac6454SAndrew Thompson 
302402ac6454SAndrew Thompson 		nqh = 1;
302502ac6454SAndrew Thompson 		ntd = ((2 * xfer->nframes)
302602ac6454SAndrew Thompson 		    + (xfer->max_data_length / xfer->max_frame_size));
302702ac6454SAndrew Thompson 
302802ac6454SAndrew Thompson 	} else if (parm->methods == &uhci_device_intr_methods) {
302902ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
303002ac6454SAndrew Thompson 		xfer->flags_int.bdma_no_post_sync = 1;
303102ac6454SAndrew Thompson 
303202ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
303302ac6454SAndrew Thompson 
303402ac6454SAndrew Thompson 		nqh = 1;
303502ac6454SAndrew Thompson 		ntd = ((2 * xfer->nframes)
303602ac6454SAndrew Thompson 		    + (xfer->max_data_length / xfer->max_frame_size));
303702ac6454SAndrew Thompson 
303802ac6454SAndrew Thompson 	} else if (parm->methods == &uhci_device_isoc_methods) {
303902ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
304002ac6454SAndrew Thompson 		xfer->flags_int.bdma_no_post_sync = 1;
304102ac6454SAndrew Thompson 
304202ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
304302ac6454SAndrew Thompson 
304402ac6454SAndrew Thompson 		nqh = 0;
304502ac6454SAndrew Thompson 		ntd = xfer->nframes;
304602ac6454SAndrew Thompson 
304702ac6454SAndrew Thompson 	} else {
304802ac6454SAndrew Thompson 
304902ac6454SAndrew Thompson 		usb2_transfer_setup_sub(parm);
305002ac6454SAndrew Thompson 
305102ac6454SAndrew Thompson 		nqh = 0;
305202ac6454SAndrew Thompson 		ntd = 0;
305302ac6454SAndrew Thompson 	}
305402ac6454SAndrew Thompson 
305502ac6454SAndrew Thompson 	if (parm->err) {
305602ac6454SAndrew Thompson 		return;
305702ac6454SAndrew Thompson 	}
305802ac6454SAndrew Thompson 	/*
305902ac6454SAndrew Thompson 	 * NOTE: the UHCI controller requires that
306002ac6454SAndrew Thompson 	 * every packet must be contiguous on
306102ac6454SAndrew Thompson 	 * the same USB memory page !
306202ac6454SAndrew Thompson 	 */
306302ac6454SAndrew Thompson 	nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
306402ac6454SAndrew Thompson 
306502ac6454SAndrew Thompson 	/*
306602ac6454SAndrew Thompson 	 * Compute a suitable power of two alignment
306702ac6454SAndrew Thompson 	 * for our "max_frame_size" fixup buffer(s):
306802ac6454SAndrew Thompson 	 */
306902ac6454SAndrew Thompson 	align = xfer->max_frame_size;
307002ac6454SAndrew Thompson 	n = 0;
307102ac6454SAndrew Thompson 	while (align) {
307202ac6454SAndrew Thompson 		align >>= 1;
307302ac6454SAndrew Thompson 		n++;
307402ac6454SAndrew Thompson 	}
307502ac6454SAndrew Thompson 
307602ac6454SAndrew Thompson 	/* check for power of two */
307702ac6454SAndrew Thompson 	if (!(xfer->max_frame_size &
307802ac6454SAndrew Thompson 	    (xfer->max_frame_size - 1))) {
307902ac6454SAndrew Thompson 		n--;
308002ac6454SAndrew Thompson 	}
308102ac6454SAndrew Thompson 	/*
308202ac6454SAndrew Thompson 	 * We don't allow alignments of
308302ac6454SAndrew Thompson 	 * less than 8 bytes:
308402ac6454SAndrew Thompson 	 *
308502ac6454SAndrew Thompson 	 * NOTE: Allocating using an aligment
308602ac6454SAndrew Thompson 	 * of 1 byte has special meaning!
308702ac6454SAndrew Thompson 	 */
308802ac6454SAndrew Thompson 	if (n < 3) {
308902ac6454SAndrew Thompson 		n = 3;
309002ac6454SAndrew Thompson 	}
309102ac6454SAndrew Thompson 	align = (1 << n);
309202ac6454SAndrew Thompson 
309302ac6454SAndrew Thompson 	if (usb2_transfer_setup_sub_malloc(
309402ac6454SAndrew Thompson 	    parm, &pc, xfer->max_frame_size,
309502ac6454SAndrew Thompson 	    align, nfixup)) {
309602ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
309702ac6454SAndrew Thompson 		return;
309802ac6454SAndrew Thompson 	}
309902ac6454SAndrew Thompson 	xfer->buf_fixup = pc;
310002ac6454SAndrew Thompson 
310102ac6454SAndrew Thompson alloc_dma_set:
310202ac6454SAndrew Thompson 
310302ac6454SAndrew Thompson 	if (parm->err) {
310402ac6454SAndrew Thompson 		return;
310502ac6454SAndrew Thompson 	}
310602ac6454SAndrew Thompson 	last_obj = NULL;
310702ac6454SAndrew Thompson 
310802ac6454SAndrew Thompson 	if (usb2_transfer_setup_sub_malloc(
310902ac6454SAndrew Thompson 	    parm, &pc, sizeof(uhci_td_t),
311002ac6454SAndrew Thompson 	    UHCI_TD_ALIGN, ntd)) {
311102ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
311202ac6454SAndrew Thompson 		return;
311302ac6454SAndrew Thompson 	}
311402ac6454SAndrew Thompson 	if (parm->buf) {
311502ac6454SAndrew Thompson 		for (n = 0; n != ntd; n++) {
311602ac6454SAndrew Thompson 			uhci_td_t *td;
311702ac6454SAndrew Thompson 
311802ac6454SAndrew Thompson 			usb2_get_page(pc + n, 0, &page_info);
311902ac6454SAndrew Thompson 
312002ac6454SAndrew Thompson 			td = page_info.buffer;
312102ac6454SAndrew Thompson 
312202ac6454SAndrew Thompson 			/* init TD */
312302ac6454SAndrew Thompson 			if ((parm->methods == &uhci_device_bulk_methods) ||
312402ac6454SAndrew Thompson 			    (parm->methods == &uhci_device_ctrl_methods) ||
312502ac6454SAndrew Thompson 			    (parm->methods == &uhci_device_intr_methods)) {
312602ac6454SAndrew Thompson 				/* set depth first bit */
312702ac6454SAndrew Thompson 				td->td_self = htole32(page_info.physaddr |
312802ac6454SAndrew Thompson 				    UHCI_PTR_TD | UHCI_PTR_VF);
312902ac6454SAndrew Thompson 			} else {
313002ac6454SAndrew Thompson 				td->td_self = htole32(page_info.physaddr |
313102ac6454SAndrew Thompson 				    UHCI_PTR_TD);
313202ac6454SAndrew Thompson 			}
313302ac6454SAndrew Thompson 
313402ac6454SAndrew Thompson 			td->obj_next = last_obj;
313502ac6454SAndrew Thompson 			td->page_cache = pc + n;
313602ac6454SAndrew Thompson 
313702ac6454SAndrew Thompson 			last_obj = td;
313802ac6454SAndrew Thompson 
313902ac6454SAndrew Thompson 			usb2_pc_cpu_flush(pc + n);
314002ac6454SAndrew Thompson 		}
314102ac6454SAndrew Thompson 	}
314202ac6454SAndrew Thompson 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
314302ac6454SAndrew Thompson 
314402ac6454SAndrew Thompson 	last_obj = NULL;
314502ac6454SAndrew Thompson 
314602ac6454SAndrew Thompson 	if (usb2_transfer_setup_sub_malloc(
314702ac6454SAndrew Thompson 	    parm, &pc, sizeof(uhci_qh_t),
314802ac6454SAndrew Thompson 	    UHCI_QH_ALIGN, nqh)) {
314902ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
315002ac6454SAndrew Thompson 		return;
315102ac6454SAndrew Thompson 	}
315202ac6454SAndrew Thompson 	if (parm->buf) {
315302ac6454SAndrew Thompson 		for (n = 0; n != nqh; n++) {
315402ac6454SAndrew Thompson 			uhci_qh_t *qh;
315502ac6454SAndrew Thompson 
315602ac6454SAndrew Thompson 			usb2_get_page(pc + n, 0, &page_info);
315702ac6454SAndrew Thompson 
315802ac6454SAndrew Thompson 			qh = page_info.buffer;
315902ac6454SAndrew Thompson 
316002ac6454SAndrew Thompson 			/* init QH */
316102ac6454SAndrew Thompson 			qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
316202ac6454SAndrew Thompson 			qh->obj_next = last_obj;
316302ac6454SAndrew Thompson 			qh->page_cache = pc + n;
316402ac6454SAndrew Thompson 
316502ac6454SAndrew Thompson 			last_obj = qh;
316602ac6454SAndrew Thompson 
316702ac6454SAndrew Thompson 			usb2_pc_cpu_flush(pc + n);
316802ac6454SAndrew Thompson 		}
316902ac6454SAndrew Thompson 	}
317002ac6454SAndrew Thompson 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
317102ac6454SAndrew Thompson 
317202ac6454SAndrew Thompson 	if (!xfer->flags_int.curr_dma_set) {
317302ac6454SAndrew Thompson 		xfer->flags_int.curr_dma_set = 1;
317402ac6454SAndrew Thompson 		goto alloc_dma_set;
317502ac6454SAndrew Thompson 	}
317602ac6454SAndrew Thompson }
317702ac6454SAndrew Thompson 
317802ac6454SAndrew Thompson static void
317902ac6454SAndrew Thompson uhci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc,
318002ac6454SAndrew Thompson     struct usb2_pipe *pipe)
318102ac6454SAndrew Thompson {
318202ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
318302ac6454SAndrew Thompson 
318402ac6454SAndrew Thompson 	DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
318502ac6454SAndrew Thompson 	    pipe, udev->address,
318602ac6454SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb2_mode,
318702ac6454SAndrew Thompson 	    sc->sc_addr);
318802ac6454SAndrew Thompson 
318902ac6454SAndrew Thompson 	if (udev->flags.usb2_mode != USB_MODE_HOST) {
319002ac6454SAndrew Thompson 		/* not supported */
319102ac6454SAndrew Thompson 		return;
319202ac6454SAndrew Thompson 	}
319302ac6454SAndrew Thompson 	if (udev->device_index == sc->sc_addr) {
319402ac6454SAndrew Thompson 		switch (edesc->bEndpointAddress) {
319502ac6454SAndrew Thompson 		case USB_CONTROL_ENDPOINT:
319602ac6454SAndrew Thompson 			pipe->methods = &uhci_root_ctrl_methods;
319702ac6454SAndrew Thompson 			break;
319802ac6454SAndrew Thompson 		case UE_DIR_IN | UHCI_INTR_ENDPT:
319902ac6454SAndrew Thompson 			pipe->methods = &uhci_root_intr_methods;
320002ac6454SAndrew Thompson 			break;
320102ac6454SAndrew Thompson 		default:
320202ac6454SAndrew Thompson 			/* do nothing */
320302ac6454SAndrew Thompson 			break;
320402ac6454SAndrew Thompson 		}
320502ac6454SAndrew Thompson 	} else {
320602ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
320702ac6454SAndrew Thompson 		case UE_CONTROL:
320802ac6454SAndrew Thompson 			pipe->methods = &uhci_device_ctrl_methods;
320902ac6454SAndrew Thompson 			break;
321002ac6454SAndrew Thompson 		case UE_INTERRUPT:
321102ac6454SAndrew Thompson 			pipe->methods = &uhci_device_intr_methods;
321202ac6454SAndrew Thompson 			break;
321302ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
321402ac6454SAndrew Thompson 			if (udev->speed == USB_SPEED_FULL) {
321502ac6454SAndrew Thompson 				pipe->methods = &uhci_device_isoc_methods;
321602ac6454SAndrew Thompson 			}
321702ac6454SAndrew Thompson 			break;
321802ac6454SAndrew Thompson 		case UE_BULK:
321902ac6454SAndrew Thompson 			if (udev->speed != USB_SPEED_LOW) {
322002ac6454SAndrew Thompson 				pipe->methods = &uhci_device_bulk_methods;
322102ac6454SAndrew Thompson 			}
322202ac6454SAndrew Thompson 			break;
322302ac6454SAndrew Thompson 		default:
322402ac6454SAndrew Thompson 			/* do nothing */
322502ac6454SAndrew Thompson 			break;
322602ac6454SAndrew Thompson 		}
322702ac6454SAndrew Thompson 	}
322802ac6454SAndrew Thompson }
322902ac6454SAndrew Thompson 
323002ac6454SAndrew Thompson static void
323102ac6454SAndrew Thompson uhci_xfer_unsetup(struct usb2_xfer *xfer)
323202ac6454SAndrew Thompson {
323302ac6454SAndrew Thompson 	return;
323402ac6454SAndrew Thompson }
323502ac6454SAndrew Thompson 
323602ac6454SAndrew Thompson static void
323702ac6454SAndrew Thompson uhci_get_dma_delay(struct usb2_bus *bus, uint32_t *pus)
323802ac6454SAndrew Thompson {
323902ac6454SAndrew Thompson 	/*
324002ac6454SAndrew Thompson 	 * Wait until hardware has finished any possible use of the
324102ac6454SAndrew Thompson 	 * transfer descriptor(s) and QH
324202ac6454SAndrew Thompson 	 */
324302ac6454SAndrew Thompson 	*pus = (1125);			/* microseconds */
324402ac6454SAndrew Thompson }
324502ac6454SAndrew Thompson 
324602ac6454SAndrew Thompson static void
324702ac6454SAndrew Thompson uhci_device_resume(struct usb2_device *udev)
324802ac6454SAndrew Thompson {
324902ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
325002ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
325102ac6454SAndrew Thompson 	struct usb2_pipe_methods *methods;
325202ac6454SAndrew Thompson 	uhci_qh_t *qh;
325302ac6454SAndrew Thompson 
325402ac6454SAndrew Thompson 	DPRINTF("\n");
325502ac6454SAndrew Thompson 
325602ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
325702ac6454SAndrew Thompson 
325802ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
325902ac6454SAndrew Thompson 
326002ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
326102ac6454SAndrew Thompson 
326202ac6454SAndrew Thompson 			methods = xfer->pipe->methods;
326302ac6454SAndrew Thompson 			qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
326402ac6454SAndrew Thompson 
326502ac6454SAndrew Thompson 			if (methods == &uhci_device_bulk_methods) {
326602ac6454SAndrew Thompson 				UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
326702ac6454SAndrew Thompson 				uhci_add_loop(sc);
326802ac6454SAndrew Thompson 				xfer->flags_int.bandwidth_reclaimed = 1;
326902ac6454SAndrew Thompson 			}
327002ac6454SAndrew Thompson 			if (methods == &uhci_device_ctrl_methods) {
327102ac6454SAndrew Thompson 				if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
327202ac6454SAndrew Thompson 					UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
327302ac6454SAndrew Thompson 				} else {
327402ac6454SAndrew Thompson 					UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
327502ac6454SAndrew Thompson 				}
327602ac6454SAndrew Thompson 			}
327702ac6454SAndrew Thompson 			if (methods == &uhci_device_intr_methods) {
327802ac6454SAndrew Thompson 				UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
327902ac6454SAndrew Thompson 			}
328002ac6454SAndrew Thompson 		}
328102ac6454SAndrew Thompson 	}
328202ac6454SAndrew Thompson 
328302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
328402ac6454SAndrew Thompson 
328502ac6454SAndrew Thompson 	return;
328602ac6454SAndrew Thompson }
328702ac6454SAndrew Thompson 
328802ac6454SAndrew Thompson static void
328902ac6454SAndrew Thompson uhci_device_suspend(struct usb2_device *udev)
329002ac6454SAndrew Thompson {
329102ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
329202ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
329302ac6454SAndrew Thompson 	struct usb2_pipe_methods *methods;
329402ac6454SAndrew Thompson 	uhci_qh_t *qh;
329502ac6454SAndrew Thompson 
329602ac6454SAndrew Thompson 	DPRINTF("\n");
329702ac6454SAndrew Thompson 
329802ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
329902ac6454SAndrew Thompson 
330002ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
330102ac6454SAndrew Thompson 
330202ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
330302ac6454SAndrew Thompson 
330402ac6454SAndrew Thompson 			methods = xfer->pipe->methods;
330502ac6454SAndrew Thompson 			qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
330602ac6454SAndrew Thompson 
330702ac6454SAndrew Thompson 			if (xfer->flags_int.bandwidth_reclaimed) {
330802ac6454SAndrew Thompson 				xfer->flags_int.bandwidth_reclaimed = 0;
330902ac6454SAndrew Thompson 				uhci_rem_loop(sc);
331002ac6454SAndrew Thompson 			}
331102ac6454SAndrew Thompson 			if (methods == &uhci_device_bulk_methods) {
331202ac6454SAndrew Thompson 				UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
331302ac6454SAndrew Thompson 			}
331402ac6454SAndrew Thompson 			if (methods == &uhci_device_ctrl_methods) {
331502ac6454SAndrew Thompson 				if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
331602ac6454SAndrew Thompson 					UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
331702ac6454SAndrew Thompson 				} else {
331802ac6454SAndrew Thompson 					UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
331902ac6454SAndrew Thompson 				}
332002ac6454SAndrew Thompson 			}
332102ac6454SAndrew Thompson 			if (methods == &uhci_device_intr_methods) {
332202ac6454SAndrew Thompson 				UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
332302ac6454SAndrew Thompson 			}
332402ac6454SAndrew Thompson 		}
332502ac6454SAndrew Thompson 	}
332602ac6454SAndrew Thompson 
332702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
332802ac6454SAndrew Thompson 
332902ac6454SAndrew Thompson 	return;
333002ac6454SAndrew Thompson }
333102ac6454SAndrew Thompson 
333202ac6454SAndrew Thompson static void
333302ac6454SAndrew Thompson uhci_set_hw_power(struct usb2_bus *bus)
333402ac6454SAndrew Thompson {
333502ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
333602ac6454SAndrew Thompson 	uint32_t flags;
333702ac6454SAndrew Thompson 
333802ac6454SAndrew Thompson 	DPRINTF("\n");
333902ac6454SAndrew Thompson 
334002ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
334102ac6454SAndrew Thompson 
334202ac6454SAndrew Thompson 	flags = bus->hw_power_state;
334302ac6454SAndrew Thompson 
334402ac6454SAndrew Thompson 	/*
334502ac6454SAndrew Thompson 	 * WARNING: Some FULL speed USB devices require periodic SOF
334602ac6454SAndrew Thompson 	 * messages! If any USB devices are connected through the
334702ac6454SAndrew Thompson 	 * UHCI, power save will be disabled!
334802ac6454SAndrew Thompson 	 */
334902ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_CONTROL |
335002ac6454SAndrew Thompson 	    USB_HW_POWER_NON_ROOT_HUB |
335102ac6454SAndrew Thompson 	    USB_HW_POWER_BULK |
335202ac6454SAndrew Thompson 	    USB_HW_POWER_INTERRUPT |
335302ac6454SAndrew Thompson 	    USB_HW_POWER_ISOC)) {
335402ac6454SAndrew Thompson 		DPRINTF("Some USB transfer is "
335502ac6454SAndrew Thompson 		    "active on %u.\n",
335602ac6454SAndrew Thompson 		    device_get_unit(sc->sc_bus.bdev));
335702ac6454SAndrew Thompson 		UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
335802ac6454SAndrew Thompson 	} else {
335902ac6454SAndrew Thompson 		DPRINTF("Power save on %u.\n",
336002ac6454SAndrew Thompson 		    device_get_unit(sc->sc_bus.bdev));
336102ac6454SAndrew Thompson 		UHCICMD(sc, UHCI_CMD_MAXP);
336202ac6454SAndrew Thompson 	}
336302ac6454SAndrew Thompson 
336402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
336502ac6454SAndrew Thompson 
336602ac6454SAndrew Thompson 	return;
336702ac6454SAndrew Thompson }
336802ac6454SAndrew Thompson 
336902ac6454SAndrew Thompson 
337002ac6454SAndrew Thompson struct usb2_bus_methods uhci_bus_methods =
337102ac6454SAndrew Thompson {
337202ac6454SAndrew Thompson 	.pipe_init = uhci_pipe_init,
337302ac6454SAndrew Thompson 	.xfer_setup = uhci_xfer_setup,
337402ac6454SAndrew Thompson 	.xfer_unsetup = uhci_xfer_unsetup,
337502ac6454SAndrew Thompson 	.do_poll = uhci_do_poll,
337602ac6454SAndrew Thompson 	.get_dma_delay = uhci_get_dma_delay,
337702ac6454SAndrew Thompson 	.device_resume = uhci_device_resume,
337802ac6454SAndrew Thompson 	.device_suspend = uhci_device_suspend,
337902ac6454SAndrew Thompson 	.set_hw_power = uhci_set_hw_power,
338002ac6454SAndrew Thompson 	.roothub_exec = uhci_root_ctrl_task,
338102ac6454SAndrew Thompson };
3382