xref: /freebsd/sys/dev/usb/controller/uhci.c (revision 6d917491f5e4d12a9c833584e66600486ec55a09)
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 
41ed6d949aSAndrew Thompson #include <sys/stdint.h>
42ed6d949aSAndrew Thompson #include <sys/stddef.h>
43ed6d949aSAndrew Thompson #include <sys/param.h>
44ed6d949aSAndrew Thompson #include <sys/queue.h>
45ed6d949aSAndrew Thompson #include <sys/types.h>
46ed6d949aSAndrew Thompson #include <sys/systm.h>
47ed6d949aSAndrew Thompson #include <sys/kernel.h>
48ed6d949aSAndrew Thompson #include <sys/bus.h>
49ed6d949aSAndrew Thompson #include <sys/module.h>
50ed6d949aSAndrew Thompson #include <sys/lock.h>
51ed6d949aSAndrew Thompson #include <sys/mutex.h>
52ed6d949aSAndrew Thompson #include <sys/condvar.h>
53ed6d949aSAndrew Thompson #include <sys/sysctl.h>
54ed6d949aSAndrew Thompson #include <sys/sx.h>
55ed6d949aSAndrew Thompson #include <sys/unistd.h>
56ed6d949aSAndrew Thompson #include <sys/callout.h>
57ed6d949aSAndrew Thompson #include <sys/malloc.h>
58ed6d949aSAndrew Thompson #include <sys/priv.h>
59ed6d949aSAndrew Thompson 
6002ac6454SAndrew Thompson #include <dev/usb/usb.h>
61ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
6202ac6454SAndrew Thompson 
6302ac6454SAndrew Thompson #define	USB_DEBUG_VAR uhcidebug
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
6602ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6802ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6902ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
7002ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
7102ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
7202ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
7502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
7602ac6454SAndrew Thompson #include <dev/usb/controller/uhci.h>
771def609aSAndrew Thompson #include <dev/usb/controller/uhcireg.h>
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson #define	alt_next next
80578d0effSAndrew Thompson #define	UHCI_BUS2SC(bus) \
81578d0effSAndrew Thompson    ((uhci_softc_t *)(((uint8_t *)(bus)) - \
82578d0effSAndrew Thompson     ((uint8_t *)&(((uhci_softc_t *)0)->sc_bus))))
8302ac6454SAndrew Thompson 
84b850ecc1SAndrew Thompson #ifdef USB_DEBUG
8502ac6454SAndrew Thompson static int uhcidebug = 0;
8602ac6454SAndrew Thompson static int uhcinoloop = 0;
8702ac6454SAndrew Thompson 
886472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
899360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
9002ac6454SAndrew Thompson     &uhcidebug, 0, "uhci debug level");
919360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
9202ac6454SAndrew Thompson     &uhcinoloop, 0, "uhci noloop");
93c13fd8d4SAndrew Thompson 
94c13fd8d4SAndrew Thompson TUNABLE_INT("hw.usb.uhci.debug", &uhcidebug);
95c13fd8d4SAndrew Thompson TUNABLE_INT("hw.usb.uhci.loop", &uhcinoloop);
96c13fd8d4SAndrew Thompson 
9702ac6454SAndrew Thompson static void uhci_dumpregs(uhci_softc_t *sc);
9802ac6454SAndrew Thompson static void uhci_dump_tds(uhci_td_t *td);
9902ac6454SAndrew Thompson 
10002ac6454SAndrew Thompson #endif
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson #define	UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
10302ac6454SAndrew Thompson 			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
10402ac6454SAndrew Thompson #define	UWRITE1(sc, r, x) \
10502ac6454SAndrew Thompson  do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
10602ac6454SAndrew Thompson  } while (/*CONSTCOND*/0)
10702ac6454SAndrew Thompson #define	UWRITE2(sc, r, x) \
10802ac6454SAndrew Thompson  do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
10902ac6454SAndrew Thompson  } while (/*CONSTCOND*/0)
11002ac6454SAndrew Thompson #define	UWRITE4(sc, r, x) \
11102ac6454SAndrew Thompson  do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
11202ac6454SAndrew Thompson  } while (/*CONSTCOND*/0)
11302ac6454SAndrew Thompson #define	UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
11402ac6454SAndrew Thompson #define	UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
11502ac6454SAndrew Thompson #define	UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
11602ac6454SAndrew Thompson 
11702ac6454SAndrew Thompson #define	UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
11802ac6454SAndrew Thompson #define	UHCISTS(sc) UREAD2(sc, UHCI_STS)
11902ac6454SAndrew Thompson 
12002ac6454SAndrew Thompson #define	UHCI_RESET_TIMEOUT 100		/* ms, reset timeout */
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson #define	UHCI_INTR_ENDPT 1
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson struct uhci_mem_layout {
12502ac6454SAndrew Thompson 
126760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
127760bc48eSAndrew Thompson 	struct usb_page_search fix_res;
12802ac6454SAndrew Thompson 
129760bc48eSAndrew Thompson 	struct usb_page_cache *buf_pc;
130760bc48eSAndrew Thompson 	struct usb_page_cache *fix_pc;
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson 	uint32_t buf_offset;
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson 	uint16_t max_frame_size;
13502ac6454SAndrew Thompson };
13602ac6454SAndrew Thompson 
13702ac6454SAndrew Thompson struct uhci_std_temp {
13802ac6454SAndrew Thompson 
13902ac6454SAndrew Thompson 	struct uhci_mem_layout ml;
14002ac6454SAndrew Thompson 	uhci_td_t *td;
14102ac6454SAndrew Thompson 	uhci_td_t *td_next;
14202ac6454SAndrew Thompson 	uint32_t average;
14302ac6454SAndrew Thompson 	uint32_t td_status;
14402ac6454SAndrew Thompson 	uint32_t td_token;
14502ac6454SAndrew Thompson 	uint32_t len;
14602ac6454SAndrew Thompson 	uint16_t max_frame_size;
14702ac6454SAndrew Thompson 	uint8_t	shortpkt;
14802ac6454SAndrew Thompson 	uint8_t	setup_alt_next;
1490f7d4548SAndrew Thompson 	uint8_t	last_frame;
15002ac6454SAndrew Thompson };
15102ac6454SAndrew Thompson 
152760bc48eSAndrew Thompson extern struct usb_bus_methods uhci_bus_methods;
153760bc48eSAndrew Thompson extern struct usb_pipe_methods uhci_device_bulk_methods;
154760bc48eSAndrew Thompson extern struct usb_pipe_methods uhci_device_ctrl_methods;
155760bc48eSAndrew Thompson extern struct usb_pipe_methods uhci_device_intr_methods;
156760bc48eSAndrew Thompson extern struct usb_pipe_methods uhci_device_isoc_methods;
15702ac6454SAndrew Thompson 
158b9c81a5dSAndrew Thompson static uint8_t	uhci_restart(uhci_softc_t *sc);
159760bc48eSAndrew Thompson static void	uhci_do_poll(struct usb_bus *);
160e0a69b51SAndrew Thompson static void	uhci_device_done(struct usb_xfer *, usb_error_t);
161760bc48eSAndrew Thompson static void	uhci_transfer_intr_enqueue(struct usb_xfer *);
16202ac6454SAndrew Thompson static void	uhci_timeout(void *);
163760bc48eSAndrew Thompson static uint8_t	uhci_check_transfer(struct usb_xfer *);
16439307315SAndrew Thompson static void	uhci_root_intr(uhci_softc_t *sc);
16502ac6454SAndrew Thompson 
16602ac6454SAndrew Thompson void
167e0a69b51SAndrew Thompson uhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
16802ac6454SAndrew Thompson {
16902ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
17002ac6454SAndrew Thompson 	uint32_t i;
17102ac6454SAndrew Thompson 
17202ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
17302ac6454SAndrew Thompson 	    sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN);
17402ac6454SAndrew Thompson 
17502ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg,
17602ac6454SAndrew Thompson 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
17702ac6454SAndrew Thompson 
17802ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg,
17902ac6454SAndrew Thompson 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
18002ac6454SAndrew Thompson 
18102ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
18202ac6454SAndrew Thompson 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
18302ac6454SAndrew Thompson 
18402ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg,
18502ac6454SAndrew Thompson 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
18602ac6454SAndrew Thompson 
18702ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg,
18802ac6454SAndrew Thompson 	    sizeof(uhci_td_t), UHCI_TD_ALIGN);
18902ac6454SAndrew Thompson 
19002ac6454SAndrew Thompson 	for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) {
19102ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.isoc_start_pc + i,
19202ac6454SAndrew Thompson 		    sc->sc_hw.isoc_start_pg + i,
19302ac6454SAndrew Thompson 		    sizeof(uhci_td_t), UHCI_TD_ALIGN);
19402ac6454SAndrew Thompson 	}
19502ac6454SAndrew Thompson 
19602ac6454SAndrew Thompson 	for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) {
19702ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.intr_start_pc + i,
19802ac6454SAndrew Thompson 		    sc->sc_hw.intr_start_pg + i,
19902ac6454SAndrew Thompson 		    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
20002ac6454SAndrew Thompson 	}
20102ac6454SAndrew Thompson }
20202ac6454SAndrew Thompson 
20302ac6454SAndrew Thompson static void
204760bc48eSAndrew Thompson uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb_xfer *xfer)
20502ac6454SAndrew Thompson {
20602ac6454SAndrew Thompson 	ml->buf_pc = xfer->frbuffers + 0;
20702ac6454SAndrew Thompson 	ml->fix_pc = xfer->buf_fixup;
20802ac6454SAndrew Thompson 
20902ac6454SAndrew Thompson 	ml->buf_offset = 0;
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson 	ml->max_frame_size = xfer->max_frame_size;
21202ac6454SAndrew Thompson }
21302ac6454SAndrew Thompson 
21402ac6454SAndrew Thompson static void
21502ac6454SAndrew Thompson uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td)
21602ac6454SAndrew Thompson {
217a593f6b8SAndrew Thompson 	usbd_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res);
21802ac6454SAndrew Thompson 
21902ac6454SAndrew Thompson 	if (ml->buf_res.length < td->len) {
22002ac6454SAndrew Thompson 
22102ac6454SAndrew Thompson 		/* need to do a fixup */
22202ac6454SAndrew Thompson 
223a593f6b8SAndrew Thompson 		usbd_get_page(ml->fix_pc, 0, &ml->fix_res);
22402ac6454SAndrew Thompson 
22502ac6454SAndrew Thompson 		td->td_buffer = htole32(ml->fix_res.physaddr);
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson 		/*
22802ac6454SAndrew Thompson 	         * The UHCI driver cannot handle
22902ac6454SAndrew Thompson 	         * page crossings, so a fixup is
23002ac6454SAndrew Thompson 	         * needed:
23102ac6454SAndrew Thompson 	         *
23202ac6454SAndrew Thompson 	         *  +----+----+ - - -
23302ac6454SAndrew Thompson 	         *  | YYY|Y   |
23402ac6454SAndrew Thompson 	         *  +----+----+ - - -
23502ac6454SAndrew Thompson 	         *     \    \
23602ac6454SAndrew Thompson 	         *      \    \
23702ac6454SAndrew Thompson 	         *       +----+
23802ac6454SAndrew Thompson 	         *       |YYYY|  (fixup)
23902ac6454SAndrew Thompson 	         *       +----+
24002ac6454SAndrew Thompson 	         */
24102ac6454SAndrew Thompson 
24202ac6454SAndrew Thompson 		if ((td->td_token & htole32(UHCI_TD_PID)) ==
24302ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_IN)) {
24402ac6454SAndrew Thompson 			td->fix_pc = ml->fix_pc;
245a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(ml->fix_pc);
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson 		} else {
24802ac6454SAndrew Thompson 			td->fix_pc = NULL;
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson 			/* copy data to fixup location */
25102ac6454SAndrew Thompson 
252a593f6b8SAndrew Thompson 			usbd_copy_out(ml->buf_pc, ml->buf_offset,
25302ac6454SAndrew Thompson 			    ml->fix_res.buffer, td->len);
25402ac6454SAndrew Thompson 
255a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(ml->fix_pc);
25602ac6454SAndrew Thompson 		}
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson 		/* prepare next fixup */
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson 		ml->fix_pc++;
26102ac6454SAndrew Thompson 
26202ac6454SAndrew Thompson 	} else {
26302ac6454SAndrew Thompson 
26402ac6454SAndrew Thompson 		td->td_buffer = htole32(ml->buf_res.physaddr);
26502ac6454SAndrew Thompson 		td->fix_pc = NULL;
26602ac6454SAndrew Thompson 	}
26702ac6454SAndrew Thompson 
26802ac6454SAndrew Thompson 	/* prepare next data location */
26902ac6454SAndrew Thompson 
27002ac6454SAndrew Thompson 	ml->buf_offset += td->len;
27102ac6454SAndrew Thompson }
27202ac6454SAndrew Thompson 
273b9c81a5dSAndrew Thompson /*
274b9c81a5dSAndrew Thompson  * Return values:
275b9c81a5dSAndrew Thompson  * 0: Success
276b9c81a5dSAndrew Thompson  * Else: Failure
277b9c81a5dSAndrew Thompson  */
278b9c81a5dSAndrew Thompson static uint8_t
279b9c81a5dSAndrew Thompson uhci_restart(uhci_softc_t *sc)
280b9c81a5dSAndrew Thompson {
281760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
282b9c81a5dSAndrew Thompson 
283b9c81a5dSAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
284b9c81a5dSAndrew Thompson 
285b9c81a5dSAndrew Thompson   	if (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS) {
286b9c81a5dSAndrew Thompson 		DPRINTFN(2, "Already started\n");
287b9c81a5dSAndrew Thompson 		return (0);
288b9c81a5dSAndrew Thompson 	}
289b9c81a5dSAndrew Thompson 
290b9c81a5dSAndrew Thompson 	DPRINTFN(2, "Restarting\n");
291b9c81a5dSAndrew Thompson 
292a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
293b9c81a5dSAndrew Thompson 
294b9c81a5dSAndrew Thompson 	/* Reload fresh base address */
295b9c81a5dSAndrew Thompson 	UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
296b9c81a5dSAndrew Thompson 
297b9c81a5dSAndrew Thompson 	/*
298b9c81a5dSAndrew Thompson 	 * Assume 64 byte packets at frame end and start HC controller:
299b9c81a5dSAndrew Thompson 	 */
300b9c81a5dSAndrew Thompson 	UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
301b9c81a5dSAndrew Thompson 
302b9c81a5dSAndrew Thompson 	/* wait 10 milliseconds */
303b9c81a5dSAndrew Thompson 
304a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
305b9c81a5dSAndrew Thompson 
306b9c81a5dSAndrew Thompson 	/* check that controller has started */
307b9c81a5dSAndrew Thompson 
308b9c81a5dSAndrew Thompson 	if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
309b9c81a5dSAndrew Thompson 		DPRINTFN(2, "Failed\n");
310b9c81a5dSAndrew Thompson 		return (1);
311b9c81a5dSAndrew Thompson 	}
312b9c81a5dSAndrew Thompson 	return (0);
313b9c81a5dSAndrew Thompson }
314b9c81a5dSAndrew Thompson 
31502ac6454SAndrew Thompson void
31602ac6454SAndrew Thompson uhci_reset(uhci_softc_t *sc)
31702ac6454SAndrew Thompson {
31802ac6454SAndrew Thompson 	uint16_t n;
31902ac6454SAndrew Thompson 
32002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson 	DPRINTF("resetting the HC\n");
32302ac6454SAndrew Thompson 
32402ac6454SAndrew Thompson 	/* disable interrupts */
32502ac6454SAndrew Thompson 
32602ac6454SAndrew Thompson 	UWRITE2(sc, UHCI_INTR, 0);
32702ac6454SAndrew Thompson 
32802ac6454SAndrew Thompson 	/* global reset */
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 	UHCICMD(sc, UHCI_CMD_GRESET);
33102ac6454SAndrew Thompson 
33202ac6454SAndrew Thompson 	/* wait */
33302ac6454SAndrew Thompson 
334a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_bus.bus_mtx,
33502ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson 	/* terminate all transfers */
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson 	UHCICMD(sc, UHCI_CMD_HCRESET);
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	/* the reset bit goes low when the controller is done */
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 	n = UHCI_RESET_TIMEOUT;
34402ac6454SAndrew Thompson 	while (n--) {
34502ac6454SAndrew Thompson 		/* wait one millisecond */
34602ac6454SAndrew Thompson 
347a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
34802ac6454SAndrew Thompson 
34902ac6454SAndrew Thompson 		if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
35002ac6454SAndrew Thompson 			goto done_1;
35102ac6454SAndrew Thompson 		}
35202ac6454SAndrew Thompson 	}
35302ac6454SAndrew Thompson 
35402ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev,
35502ac6454SAndrew Thompson 	    "controller did not reset\n");
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson done_1:
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson 	n = 10;
36002ac6454SAndrew Thompson 	while (n--) {
36102ac6454SAndrew Thompson 		/* wait one millisecond */
36202ac6454SAndrew Thompson 
363a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
36402ac6454SAndrew Thompson 
36502ac6454SAndrew Thompson 		/* check if HC is stopped */
36602ac6454SAndrew Thompson 		if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
36702ac6454SAndrew Thompson 			goto done_2;
36802ac6454SAndrew Thompson 		}
36902ac6454SAndrew Thompson 	}
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson 	device_printf(sc->sc_bus.bdev,
37202ac6454SAndrew Thompson 	    "controller did not stop\n");
37302ac6454SAndrew Thompson 
37402ac6454SAndrew Thompson done_2:
37502ac6454SAndrew Thompson 
3762e141748SHans Petter Selasky 	/* reset frame number */
3772e141748SHans Petter Selasky 	UWRITE2(sc, UHCI_FRNUM, 0);
3782e141748SHans Petter Selasky 	/* set default SOF value */
3792e141748SHans Petter Selasky 	UWRITE1(sc, UHCI_SOF, 0x40);
38039307315SAndrew Thompson 
38139307315SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
38239307315SAndrew Thompson 
38339307315SAndrew Thompson 	/* stop root interrupt */
384a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_root_intr);
38539307315SAndrew Thompson 
38639307315SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
38702ac6454SAndrew Thompson }
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson static void
39002ac6454SAndrew Thompson uhci_start(uhci_softc_t *sc)
39102ac6454SAndrew Thompson {
39202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 	DPRINTFN(2, "enabling\n");
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 	/* enable interrupts */
39702ac6454SAndrew Thompson 
39802ac6454SAndrew Thompson 	UWRITE2(sc, UHCI_INTR,
39902ac6454SAndrew Thompson 	    (UHCI_INTR_TOCRCIE |
40002ac6454SAndrew Thompson 	    UHCI_INTR_RIE |
40102ac6454SAndrew Thompson 	    UHCI_INTR_IOCE |
40202ac6454SAndrew Thompson 	    UHCI_INTR_SPIE));
40302ac6454SAndrew Thompson 
404b9c81a5dSAndrew Thompson 	if (uhci_restart(sc)) {
40502ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev,
40602ac6454SAndrew Thompson 		    "cannot start HC controller\n");
407b9c81a5dSAndrew Thompson 	}
40802ac6454SAndrew Thompson 
40939307315SAndrew Thompson 	/* start root interrupt */
41039307315SAndrew Thompson 	uhci_root_intr(sc);
41102ac6454SAndrew Thompson }
41202ac6454SAndrew Thompson 
41302ac6454SAndrew Thompson static struct uhci_qh *
414760bc48eSAndrew Thompson uhci_init_qh(struct usb_page_cache *pc)
41502ac6454SAndrew Thompson {
416760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
41702ac6454SAndrew Thompson 	struct uhci_qh *qh;
41802ac6454SAndrew Thompson 
419a593f6b8SAndrew Thompson 	usbd_get_page(pc, 0, &buf_res);
42002ac6454SAndrew Thompson 
42102ac6454SAndrew Thompson 	qh = buf_res.buffer;
42202ac6454SAndrew Thompson 
42302ac6454SAndrew Thompson 	qh->qh_self =
42402ac6454SAndrew Thompson 	    htole32(buf_res.physaddr) |
42502ac6454SAndrew Thompson 	    htole32(UHCI_PTR_QH);
42602ac6454SAndrew Thompson 
42702ac6454SAndrew Thompson 	qh->page_cache = pc;
42802ac6454SAndrew Thompson 
42902ac6454SAndrew Thompson 	return (qh);
43002ac6454SAndrew Thompson }
43102ac6454SAndrew Thompson 
43202ac6454SAndrew Thompson static struct uhci_td *
433760bc48eSAndrew Thompson uhci_init_td(struct usb_page_cache *pc)
43402ac6454SAndrew Thompson {
435760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
43602ac6454SAndrew Thompson 	struct uhci_td *td;
43702ac6454SAndrew Thompson 
438a593f6b8SAndrew Thompson 	usbd_get_page(pc, 0, &buf_res);
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 	td = buf_res.buffer;
44102ac6454SAndrew Thompson 
44202ac6454SAndrew Thompson 	td->td_self =
44302ac6454SAndrew Thompson 	    htole32(buf_res.physaddr) |
44402ac6454SAndrew Thompson 	    htole32(UHCI_PTR_TD);
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson 	td->page_cache = pc;
44702ac6454SAndrew Thompson 
44802ac6454SAndrew Thompson 	return (td);
44902ac6454SAndrew Thompson }
45002ac6454SAndrew Thompson 
451e0a69b51SAndrew Thompson usb_error_t
45202ac6454SAndrew Thompson uhci_init(uhci_softc_t *sc)
45302ac6454SAndrew Thompson {
45402ac6454SAndrew Thompson 	uint16_t bit;
45502ac6454SAndrew Thompson 	uint16_t x;
45602ac6454SAndrew Thompson 	uint16_t y;
45702ac6454SAndrew Thompson 
45802ac6454SAndrew Thompson 	DPRINTF("start\n");
45902ac6454SAndrew Thompson 
460a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_root_intr, &sc->sc_bus.bus_mtx, 0);
46139307315SAndrew Thompson 
462b850ecc1SAndrew Thompson #ifdef USB_DEBUG
46302ac6454SAndrew Thompson 	if (uhcidebug > 2) {
46402ac6454SAndrew Thompson 		uhci_dumpregs(sc);
46502ac6454SAndrew Thompson 	}
46602ac6454SAndrew Thompson #endif
46702ac6454SAndrew Thompson 	/*
46802ac6454SAndrew Thompson 	 * Setup QH's
46902ac6454SAndrew Thompson 	 */
47002ac6454SAndrew Thompson 	sc->sc_ls_ctl_p_last =
47102ac6454SAndrew Thompson 	    uhci_init_qh(&sc->sc_hw.ls_ctl_start_pc);
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson 	sc->sc_fs_ctl_p_last =
47402ac6454SAndrew Thompson 	    uhci_init_qh(&sc->sc_hw.fs_ctl_start_pc);
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson 	sc->sc_bulk_p_last =
47702ac6454SAndrew Thompson 	    uhci_init_qh(&sc->sc_hw.bulk_start_pc);
47802ac6454SAndrew Thompson #if 0
47902ac6454SAndrew Thompson 	sc->sc_reclaim_qh_p =
48002ac6454SAndrew Thompson 	    sc->sc_fs_ctl_p_last;
48102ac6454SAndrew Thompson #else
48202ac6454SAndrew Thompson 	/* setup reclaim looping point */
48302ac6454SAndrew Thompson 	sc->sc_reclaim_qh_p =
48402ac6454SAndrew Thompson 	    sc->sc_bulk_p_last;
48502ac6454SAndrew Thompson #endif
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson 	sc->sc_last_qh_p =
48802ac6454SAndrew Thompson 	    uhci_init_qh(&sc->sc_hw.last_qh_pc);
48902ac6454SAndrew Thompson 
49002ac6454SAndrew Thompson 	sc->sc_last_td_p =
49102ac6454SAndrew Thompson 	    uhci_init_td(&sc->sc_hw.last_td_pc);
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
49402ac6454SAndrew Thompson 		sc->sc_isoc_p_last[x] =
49502ac6454SAndrew Thompson 		    uhci_init_td(sc->sc_hw.isoc_start_pc + x);
49602ac6454SAndrew Thompson 	}
49702ac6454SAndrew Thompson 
49802ac6454SAndrew Thompson 	for (x = 0; x != UHCI_IFRAMELIST_COUNT; x++) {
49902ac6454SAndrew Thompson 		sc->sc_intr_p_last[x] =
50002ac6454SAndrew Thompson 		    uhci_init_qh(sc->sc_hw.intr_start_pc + x);
50102ac6454SAndrew Thompson 	}
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 	/*
50402ac6454SAndrew Thompson 	 * the QHs are arranged to give poll intervals that are
50502ac6454SAndrew Thompson 	 * powers of 2 times 1ms
50602ac6454SAndrew Thompson 	 */
50702ac6454SAndrew Thompson 	bit = UHCI_IFRAMELIST_COUNT / 2;
50802ac6454SAndrew Thompson 	while (bit) {
50902ac6454SAndrew Thompson 		x = bit;
51002ac6454SAndrew Thompson 		while (x & bit) {
51102ac6454SAndrew Thompson 			uhci_qh_t *qh_x;
51202ac6454SAndrew Thompson 			uhci_qh_t *qh_y;
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 			y = (x ^ bit) | (bit / 2);
51502ac6454SAndrew Thompson 
51602ac6454SAndrew Thompson 			/*
51702ac6454SAndrew Thompson 			 * the next QH has half the poll interval
51802ac6454SAndrew Thompson 			 */
51902ac6454SAndrew Thompson 			qh_x = sc->sc_intr_p_last[x];
52002ac6454SAndrew Thompson 			qh_y = sc->sc_intr_p_last[y];
52102ac6454SAndrew Thompson 
52202ac6454SAndrew Thompson 			qh_x->h_next = NULL;
52302ac6454SAndrew Thompson 			qh_x->qh_h_next = qh_y->qh_self;
52402ac6454SAndrew Thompson 			qh_x->e_next = NULL;
52502ac6454SAndrew Thompson 			qh_x->qh_e_next = htole32(UHCI_PTR_T);
52602ac6454SAndrew Thompson 			x++;
52702ac6454SAndrew Thompson 		}
52802ac6454SAndrew Thompson 		bit >>= 1;
52902ac6454SAndrew Thompson 	}
53002ac6454SAndrew Thompson 
53102ac6454SAndrew Thompson 	if (1) {
53202ac6454SAndrew Thompson 		uhci_qh_t *qh_ls;
53302ac6454SAndrew Thompson 		uhci_qh_t *qh_intr;
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson 		qh_ls = sc->sc_ls_ctl_p_last;
53602ac6454SAndrew Thompson 		qh_intr = sc->sc_intr_p_last[0];
53702ac6454SAndrew Thompson 
53802ac6454SAndrew Thompson 		/* start QH for interrupt traffic */
53902ac6454SAndrew Thompson 		qh_intr->h_next = qh_ls;
54002ac6454SAndrew Thompson 		qh_intr->qh_h_next = qh_ls->qh_self;
54102ac6454SAndrew Thompson 		qh_intr->e_next = 0;
54202ac6454SAndrew Thompson 		qh_intr->qh_e_next = htole32(UHCI_PTR_T);
54302ac6454SAndrew Thompson 	}
54402ac6454SAndrew Thompson 	for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson 		uhci_td_t *td_x;
54702ac6454SAndrew Thompson 		uhci_qh_t *qh_intr;
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson 		td_x = sc->sc_isoc_p_last[x];
55002ac6454SAndrew Thompson 		qh_intr = sc->sc_intr_p_last[x | (UHCI_IFRAMELIST_COUNT / 2)];
55102ac6454SAndrew Thompson 
55202ac6454SAndrew Thompson 		/* start TD for isochronous traffic */
55302ac6454SAndrew Thompson 		td_x->next = NULL;
55402ac6454SAndrew Thompson 		td_x->td_next = qh_intr->qh_self;
55502ac6454SAndrew Thompson 		td_x->td_status = htole32(UHCI_TD_IOS);
55602ac6454SAndrew Thompson 		td_x->td_token = htole32(0);
55702ac6454SAndrew Thompson 		td_x->td_buffer = htole32(0);
55802ac6454SAndrew Thompson 	}
55902ac6454SAndrew Thompson 
56002ac6454SAndrew Thompson 	if (1) {
56102ac6454SAndrew Thompson 		uhci_qh_t *qh_ls;
56202ac6454SAndrew Thompson 		uhci_qh_t *qh_fs;
56302ac6454SAndrew Thompson 
56402ac6454SAndrew Thompson 		qh_ls = sc->sc_ls_ctl_p_last;
56502ac6454SAndrew Thompson 		qh_fs = sc->sc_fs_ctl_p_last;
56602ac6454SAndrew Thompson 
56702ac6454SAndrew Thompson 		/* start QH where low speed control traffic will be queued */
56802ac6454SAndrew Thompson 		qh_ls->h_next = qh_fs;
56902ac6454SAndrew Thompson 		qh_ls->qh_h_next = qh_fs->qh_self;
57002ac6454SAndrew Thompson 		qh_ls->e_next = 0;
57102ac6454SAndrew Thompson 		qh_ls->qh_e_next = htole32(UHCI_PTR_T);
57202ac6454SAndrew Thompson 	}
57302ac6454SAndrew Thompson 	if (1) {
57402ac6454SAndrew Thompson 		uhci_qh_t *qh_ctl;
57502ac6454SAndrew Thompson 		uhci_qh_t *qh_blk;
57602ac6454SAndrew Thompson 		uhci_qh_t *qh_lst;
57702ac6454SAndrew Thompson 		uhci_td_t *td_lst;
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson 		qh_ctl = sc->sc_fs_ctl_p_last;
58002ac6454SAndrew Thompson 		qh_blk = sc->sc_bulk_p_last;
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 		/* start QH where full speed control traffic will be queued */
58302ac6454SAndrew Thompson 		qh_ctl->h_next = qh_blk;
58402ac6454SAndrew Thompson 		qh_ctl->qh_h_next = qh_blk->qh_self;
58502ac6454SAndrew Thompson 		qh_ctl->e_next = 0;
58602ac6454SAndrew Thompson 		qh_ctl->qh_e_next = htole32(UHCI_PTR_T);
58702ac6454SAndrew Thompson 
58802ac6454SAndrew Thompson 		qh_lst = sc->sc_last_qh_p;
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson 		/* start QH where bulk traffic will be queued */
59102ac6454SAndrew Thompson 		qh_blk->h_next = qh_lst;
59202ac6454SAndrew Thompson 		qh_blk->qh_h_next = qh_lst->qh_self;
59302ac6454SAndrew Thompson 		qh_blk->e_next = 0;
59402ac6454SAndrew Thompson 		qh_blk->qh_e_next = htole32(UHCI_PTR_T);
59502ac6454SAndrew Thompson 
59602ac6454SAndrew Thompson 		td_lst = sc->sc_last_td_p;
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson 		/* end QH which is used for looping the QHs */
59902ac6454SAndrew Thompson 		qh_lst->h_next = 0;
60002ac6454SAndrew Thompson 		qh_lst->qh_h_next = htole32(UHCI_PTR_T);	/* end of QH chain */
60102ac6454SAndrew Thompson 		qh_lst->e_next = td_lst;
60202ac6454SAndrew Thompson 		qh_lst->qh_e_next = td_lst->td_self;
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 		/*
60502ac6454SAndrew Thompson 		 * end TD which hangs from the last QH, to avoid a bug in the PIIX
60602ac6454SAndrew Thompson 		 * that makes it run berserk otherwise
60702ac6454SAndrew Thompson 		 */
60802ac6454SAndrew Thompson 		td_lst->next = 0;
60902ac6454SAndrew Thompson 		td_lst->td_next = htole32(UHCI_PTR_T);
61002ac6454SAndrew Thompson 		td_lst->td_status = htole32(0);	/* inactive */
61102ac6454SAndrew Thompson 		td_lst->td_token = htole32(0);
61202ac6454SAndrew Thompson 		td_lst->td_buffer = htole32(0);
61302ac6454SAndrew Thompson 	}
61402ac6454SAndrew Thompson 	if (1) {
615760bc48eSAndrew Thompson 		struct usb_page_search buf_res;
61602ac6454SAndrew Thompson 		uint32_t *pframes;
61702ac6454SAndrew Thompson 
618a593f6b8SAndrew Thompson 		usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
61902ac6454SAndrew Thompson 
62002ac6454SAndrew Thompson 		pframes = buf_res.buffer;
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 		/*
62402ac6454SAndrew Thompson 		 * Setup UHCI framelist
62502ac6454SAndrew Thompson 		 *
62602ac6454SAndrew Thompson 		 * Execution order:
62702ac6454SAndrew Thompson 		 *
62802ac6454SAndrew Thompson 		 * pframes -> full speed isochronous -> interrupt QH's -> low
62902ac6454SAndrew Thompson 		 * speed control -> full speed control -> bulk transfers
63002ac6454SAndrew Thompson 		 *
63102ac6454SAndrew Thompson 		 */
63202ac6454SAndrew Thompson 
63302ac6454SAndrew Thompson 		for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) {
63402ac6454SAndrew Thompson 			pframes[x] =
63502ac6454SAndrew Thompson 			    sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self;
63602ac6454SAndrew Thompson 		}
63702ac6454SAndrew Thompson 	}
63802ac6454SAndrew Thompson 	/* flush all cache into memory */
63902ac6454SAndrew Thompson 
640a593f6b8SAndrew Thompson 	usb_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc);
64102ac6454SAndrew Thompson 
64202ac6454SAndrew Thompson 	/* set up the bus struct */
64302ac6454SAndrew Thompson 	sc->sc_bus.methods = &uhci_bus_methods;
64402ac6454SAndrew Thompson 
64502ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
64602ac6454SAndrew Thompson 	/* reset the controller */
64702ac6454SAndrew Thompson 	uhci_reset(sc);
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	/* start the controller */
65002ac6454SAndrew Thompson 	uhci_start(sc);
65102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
65202ac6454SAndrew Thompson 
65302ac6454SAndrew Thompson 	/* catch lost interrupts */
65402ac6454SAndrew Thompson 	uhci_do_poll(&sc->sc_bus);
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 	return (0);
65702ac6454SAndrew Thompson }
65802ac6454SAndrew Thompson 
6592e141748SHans Petter Selasky static void
66002ac6454SAndrew Thompson uhci_suspend(uhci_softc_t *sc)
66102ac6454SAndrew Thompson {
662b850ecc1SAndrew Thompson #ifdef USB_DEBUG
66302ac6454SAndrew Thompson 	if (uhcidebug > 2) {
66402ac6454SAndrew Thompson 		uhci_dumpregs(sc);
66502ac6454SAndrew Thompson 	}
66602ac6454SAndrew Thompson #endif
66702ac6454SAndrew Thompson 
6682e141748SHans Petter Selasky 	USB_BUS_LOCK(&sc->sc_bus);
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 	/* stop the controller */
67102ac6454SAndrew Thompson 
67202ac6454SAndrew Thompson 	uhci_reset(sc);
67302ac6454SAndrew Thompson 
67402ac6454SAndrew Thompson 	/* enter global suspend */
67502ac6454SAndrew Thompson 
67602ac6454SAndrew Thompson 	UHCICMD(sc, UHCI_CMD_EGSM);
67702ac6454SAndrew Thompson 
67802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
67902ac6454SAndrew Thompson }
68002ac6454SAndrew Thompson 
6812e141748SHans Petter Selasky static void
68202ac6454SAndrew Thompson uhci_resume(uhci_softc_t *sc)
68302ac6454SAndrew Thompson {
68402ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
68502ac6454SAndrew Thompson 
68602ac6454SAndrew Thompson 	/* reset the controller */
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 	uhci_reset(sc);
68902ac6454SAndrew Thompson 
69002ac6454SAndrew Thompson 	/* force global resume */
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson 	UHCICMD(sc, UHCI_CMD_FGR);
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 	/* and start traffic again */
69502ac6454SAndrew Thompson 
69602ac6454SAndrew Thompson 	uhci_start(sc);
69702ac6454SAndrew Thompson 
69802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
69902ac6454SAndrew Thompson 
7002e141748SHans Petter Selasky #ifdef USB_DEBUG
7012e141748SHans Petter Selasky 	if (uhcidebug > 2)
7022e141748SHans Petter Selasky 		uhci_dumpregs(sc);
7032e141748SHans Petter Selasky #endif
7042e141748SHans Petter Selasky 
70502ac6454SAndrew Thompson 	/* catch lost interrupts */
70602ac6454SAndrew Thompson 	uhci_do_poll(&sc->sc_bus);
70702ac6454SAndrew Thompson }
70802ac6454SAndrew Thompson 
709b850ecc1SAndrew Thompson #ifdef USB_DEBUG
71002ac6454SAndrew Thompson static void
71102ac6454SAndrew Thompson uhci_dumpregs(uhci_softc_t *sc)
71202ac6454SAndrew Thompson {
71302ac6454SAndrew Thompson 	DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
71402ac6454SAndrew Thompson 	    "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
71502ac6454SAndrew Thompson 	    device_get_nameunit(sc->sc_bus.bdev),
71602ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_CMD),
71702ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_STS),
71802ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_INTR),
71902ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_FRNUM),
72002ac6454SAndrew Thompson 	    UREAD4(sc, UHCI_FLBASEADDR),
72102ac6454SAndrew Thompson 	    UREAD1(sc, UHCI_SOF),
72202ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_PORTSC1),
72302ac6454SAndrew Thompson 	    UREAD2(sc, UHCI_PORTSC2));
72402ac6454SAndrew Thompson }
72502ac6454SAndrew Thompson 
72602ac6454SAndrew Thompson static uint8_t
72702ac6454SAndrew Thompson uhci_dump_td(uhci_td_t *p)
72802ac6454SAndrew Thompson {
72902ac6454SAndrew Thompson 	uint32_t td_next;
73002ac6454SAndrew Thompson 	uint32_t td_status;
73102ac6454SAndrew Thompson 	uint32_t td_token;
73202ac6454SAndrew Thompson 	uint8_t temp;
73302ac6454SAndrew Thompson 
734a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(p->page_cache);
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson 	td_next = le32toh(p->td_next);
73702ac6454SAndrew Thompson 	td_status = le32toh(p->td_status);
73802ac6454SAndrew Thompson 	td_token = le32toh(p->td_token);
73902ac6454SAndrew Thompson 
74002ac6454SAndrew Thompson 	/*
74102ac6454SAndrew Thompson 	 * Check whether the link pointer in this TD marks the link pointer
74202ac6454SAndrew Thompson 	 * as end of queue:
74302ac6454SAndrew Thompson 	 */
74402ac6454SAndrew Thompson 	temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
74502ac6454SAndrew Thompson 
74602ac6454SAndrew Thompson 	printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
74702ac6454SAndrew Thompson 	    "token=0x%08x buffer=0x%08x\n",
74802ac6454SAndrew Thompson 	    p,
74902ac6454SAndrew Thompson 	    le32toh(p->td_self),
75002ac6454SAndrew Thompson 	    td_next,
75102ac6454SAndrew Thompson 	    td_status,
75202ac6454SAndrew Thompson 	    td_token,
75302ac6454SAndrew Thompson 	    le32toh(p->td_buffer));
75402ac6454SAndrew Thompson 
75502ac6454SAndrew 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,"
75602ac6454SAndrew Thompson 	    "addr=%d,endpt=%d,D=%d,maxlen=%d\n",
75702ac6454SAndrew Thompson 	    p,
75802ac6454SAndrew Thompson 	    (td_next & 1) ? "-T" : "",
75902ac6454SAndrew Thompson 	    (td_next & 2) ? "-Q" : "",
76002ac6454SAndrew Thompson 	    (td_next & 4) ? "-VF" : "",
76102ac6454SAndrew Thompson 	    (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
76202ac6454SAndrew Thompson 	    (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
76302ac6454SAndrew Thompson 	    (td_status & UHCI_TD_NAK) ? "-NAK" : "",
76402ac6454SAndrew Thompson 	    (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
76502ac6454SAndrew Thompson 	    (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
76602ac6454SAndrew Thompson 	    (td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
76702ac6454SAndrew Thompson 	    (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
76802ac6454SAndrew Thompson 	    (td_status & UHCI_TD_IOC) ? "-IOC" : "",
76902ac6454SAndrew Thompson 	    (td_status & UHCI_TD_IOS) ? "-IOS" : "",
77002ac6454SAndrew Thompson 	    (td_status & UHCI_TD_LS) ? "-LS" : "",
77102ac6454SAndrew Thompson 	    (td_status & UHCI_TD_SPD) ? "-SPD" : "",
77202ac6454SAndrew Thompson 	    UHCI_TD_GET_ERRCNT(td_status),
77302ac6454SAndrew Thompson 	    UHCI_TD_GET_ACTLEN(td_status),
77402ac6454SAndrew Thompson 	    UHCI_TD_GET_PID(td_token),
77502ac6454SAndrew Thompson 	    UHCI_TD_GET_DEVADDR(td_token),
77602ac6454SAndrew Thompson 	    UHCI_TD_GET_ENDPT(td_token),
77702ac6454SAndrew Thompson 	    UHCI_TD_GET_DT(td_token),
77802ac6454SAndrew Thompson 	    UHCI_TD_GET_MAXLEN(td_token));
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson 	return (temp);
78102ac6454SAndrew Thompson }
78202ac6454SAndrew Thompson 
78302ac6454SAndrew Thompson static uint8_t
78402ac6454SAndrew Thompson uhci_dump_qh(uhci_qh_t *sqh)
78502ac6454SAndrew Thompson {
78602ac6454SAndrew Thompson 	uint8_t temp;
78702ac6454SAndrew Thompson 	uint32_t qh_h_next;
78802ac6454SAndrew Thompson 	uint32_t qh_e_next;
78902ac6454SAndrew Thompson 
790a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sqh->page_cache);
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 	qh_h_next = le32toh(sqh->qh_h_next);
79302ac6454SAndrew Thompson 	qh_e_next = le32toh(sqh->qh_e_next);
79402ac6454SAndrew Thompson 
79502ac6454SAndrew Thompson 	DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
79602ac6454SAndrew Thompson 	    le32toh(sqh->qh_self), qh_h_next, qh_e_next);
79702ac6454SAndrew Thompson 
79802ac6454SAndrew Thompson 	temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
79902ac6454SAndrew Thompson 	    (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson 	return (temp);
80202ac6454SAndrew Thompson }
80302ac6454SAndrew Thompson 
80402ac6454SAndrew Thompson static void
80502ac6454SAndrew Thompson uhci_dump_all(uhci_softc_t *sc)
80602ac6454SAndrew Thompson {
80702ac6454SAndrew Thompson 	uhci_dumpregs(sc);
80802ac6454SAndrew Thompson 	uhci_dump_qh(sc->sc_ls_ctl_p_last);
80902ac6454SAndrew Thompson 	uhci_dump_qh(sc->sc_fs_ctl_p_last);
81002ac6454SAndrew Thompson 	uhci_dump_qh(sc->sc_bulk_p_last);
81102ac6454SAndrew Thompson 	uhci_dump_qh(sc->sc_last_qh_p);
81202ac6454SAndrew Thompson }
81302ac6454SAndrew Thompson 
81402ac6454SAndrew Thompson static void
81502ac6454SAndrew Thompson uhci_dump_tds(uhci_td_t *td)
81602ac6454SAndrew Thompson {
81702ac6454SAndrew Thompson 	for (;
81802ac6454SAndrew Thompson 	    td != NULL;
81902ac6454SAndrew Thompson 	    td = td->obj_next) {
82002ac6454SAndrew Thompson 		if (uhci_dump_td(td)) {
82102ac6454SAndrew Thompson 			break;
82202ac6454SAndrew Thompson 		}
82302ac6454SAndrew Thompson 	}
82402ac6454SAndrew Thompson }
82502ac6454SAndrew Thompson 
82602ac6454SAndrew Thompson #endif
82702ac6454SAndrew Thompson 
82802ac6454SAndrew Thompson /*
82902ac6454SAndrew Thompson  * Let the last QH loop back to the full speed control transfer QH.
83002ac6454SAndrew Thompson  * This is what intel calls "bandwidth reclamation" and improves
83102ac6454SAndrew Thompson  * USB performance a lot for some devices.
83202ac6454SAndrew Thompson  * If we are already looping, just count it.
83302ac6454SAndrew Thompson  */
83402ac6454SAndrew Thompson static void
83502ac6454SAndrew Thompson uhci_add_loop(uhci_softc_t *sc)
83602ac6454SAndrew Thompson {
83702ac6454SAndrew Thompson 	struct uhci_qh *qh_lst;
83802ac6454SAndrew Thompson 	struct uhci_qh *qh_rec;
83902ac6454SAndrew Thompson 
840b850ecc1SAndrew Thompson #ifdef USB_DEBUG
84102ac6454SAndrew Thompson 	if (uhcinoloop) {
84202ac6454SAndrew Thompson 		return;
84302ac6454SAndrew Thompson 	}
84402ac6454SAndrew Thompson #endif
84502ac6454SAndrew Thompson 	if (++(sc->sc_loops) == 1) {
84602ac6454SAndrew Thompson 		DPRINTFN(6, "add\n");
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 		qh_lst = sc->sc_last_qh_p;
84902ac6454SAndrew Thompson 		qh_rec = sc->sc_reclaim_qh_p;
85002ac6454SAndrew Thompson 
85102ac6454SAndrew Thompson 		/* NOTE: we don't loop back the soft pointer */
85202ac6454SAndrew Thompson 
85302ac6454SAndrew Thompson 		qh_lst->qh_h_next = qh_rec->qh_self;
854a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(qh_lst->page_cache);
85502ac6454SAndrew Thompson 	}
85602ac6454SAndrew Thompson }
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson static void
85902ac6454SAndrew Thompson uhci_rem_loop(uhci_softc_t *sc)
86002ac6454SAndrew Thompson {
86102ac6454SAndrew Thompson 	struct uhci_qh *qh_lst;
86202ac6454SAndrew Thompson 
863b850ecc1SAndrew Thompson #ifdef USB_DEBUG
86402ac6454SAndrew Thompson 	if (uhcinoloop) {
86502ac6454SAndrew Thompson 		return;
86602ac6454SAndrew Thompson 	}
86702ac6454SAndrew Thompson #endif
86802ac6454SAndrew Thompson 	if (--(sc->sc_loops) == 0) {
86902ac6454SAndrew Thompson 		DPRINTFN(6, "remove\n");
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson 		qh_lst = sc->sc_last_qh_p;
87202ac6454SAndrew Thompson 		qh_lst->qh_h_next = htole32(UHCI_PTR_T);
873a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(qh_lst->page_cache);
87402ac6454SAndrew Thompson 	}
87502ac6454SAndrew Thompson }
87602ac6454SAndrew Thompson 
87702ac6454SAndrew Thompson static void
878760bc48eSAndrew Thompson uhci_transfer_intr_enqueue(struct usb_xfer *xfer)
87902ac6454SAndrew Thompson {
88002ac6454SAndrew Thompson 	/* check for early completion */
88102ac6454SAndrew Thompson 	if (uhci_check_transfer(xfer)) {
88202ac6454SAndrew Thompson 		return;
88302ac6454SAndrew Thompson 	}
88402ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
885a593f6b8SAndrew Thompson 	usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson 	/* start timeout, if any */
88802ac6454SAndrew Thompson 	if (xfer->timeout != 0) {
889a593f6b8SAndrew Thompson 		usbd_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
89002ac6454SAndrew Thompson 	}
89102ac6454SAndrew Thompson }
89202ac6454SAndrew Thompson 
89302ac6454SAndrew Thompson #define	UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
89402ac6454SAndrew Thompson static uhci_td_t *
89502ac6454SAndrew Thompson _uhci_append_td(uhci_td_t *std, uhci_td_t *last)
89602ac6454SAndrew Thompson {
89702ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", std, last);
89802ac6454SAndrew Thompson 
89902ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
90002ac6454SAndrew Thompson 
90102ac6454SAndrew Thompson 	std->next = last->next;
90202ac6454SAndrew Thompson 	std->td_next = last->td_next;
90302ac6454SAndrew Thompson 
90402ac6454SAndrew Thompson 	std->prev = last;
90502ac6454SAndrew Thompson 
906a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->page_cache);
90702ac6454SAndrew Thompson 
90802ac6454SAndrew Thompson 	/*
90902ac6454SAndrew Thompson 	 * the last->next->prev is never followed: std->next->prev = std;
91002ac6454SAndrew Thompson 	 */
91102ac6454SAndrew Thompson 	last->next = std;
91202ac6454SAndrew Thompson 	last->td_next = std->td_self;
91302ac6454SAndrew Thompson 
914a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
91502ac6454SAndrew Thompson 
91602ac6454SAndrew Thompson 	return (std);
91702ac6454SAndrew Thompson }
91802ac6454SAndrew Thompson 
91902ac6454SAndrew Thompson #define	UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
92002ac6454SAndrew Thompson static uhci_qh_t *
92102ac6454SAndrew Thompson _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
92202ac6454SAndrew Thompson {
92302ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", sqh, last);
92402ac6454SAndrew Thompson 
92502ac6454SAndrew Thompson 	if (sqh->h_prev != NULL) {
92602ac6454SAndrew Thompson 		/* should not happen */
92702ac6454SAndrew Thompson 		DPRINTFN(0, "QH already linked!\n");
92802ac6454SAndrew Thompson 		return (last);
92902ac6454SAndrew Thompson 	}
93002ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 	sqh->h_next = last->h_next;
93302ac6454SAndrew Thompson 	sqh->qh_h_next = last->qh_h_next;
93402ac6454SAndrew Thompson 
93502ac6454SAndrew Thompson 	sqh->h_prev = last;
93602ac6454SAndrew Thompson 
937a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(sqh->page_cache);
93802ac6454SAndrew Thompson 
93902ac6454SAndrew Thompson 	/*
94002ac6454SAndrew Thompson 	 * The "last->h_next->h_prev" is never followed:
94102ac6454SAndrew Thompson 	 *
94202ac6454SAndrew Thompson 	 * "sqh->h_next->h_prev" = sqh;
94302ac6454SAndrew Thompson 	 */
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson 	last->h_next = sqh;
94602ac6454SAndrew Thompson 	last->qh_h_next = sqh->qh_self;
94702ac6454SAndrew Thompson 
948a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
94902ac6454SAndrew Thompson 
95002ac6454SAndrew Thompson 	return (sqh);
95102ac6454SAndrew Thompson }
95202ac6454SAndrew Thompson 
95302ac6454SAndrew Thompson /**/
95402ac6454SAndrew Thompson 
95502ac6454SAndrew Thompson #define	UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
95602ac6454SAndrew Thompson static uhci_td_t *
95702ac6454SAndrew Thompson _uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
95802ac6454SAndrew Thompson {
95902ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", std, last);
96002ac6454SAndrew Thompson 
96102ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
96202ac6454SAndrew Thompson 
96302ac6454SAndrew Thompson 	std->prev->next = std->next;
96402ac6454SAndrew Thompson 	std->prev->td_next = std->td_next;
96502ac6454SAndrew Thompson 
966a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(std->prev->page_cache);
96702ac6454SAndrew Thompson 
96802ac6454SAndrew Thompson 	if (std->next) {
96902ac6454SAndrew Thompson 		std->next->prev = std->prev;
970a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(std->next->page_cache);
97102ac6454SAndrew Thompson 	}
97202ac6454SAndrew Thompson 	return ((last == std) ? std->prev : last);
97302ac6454SAndrew Thompson }
97402ac6454SAndrew Thompson 
97502ac6454SAndrew Thompson #define	UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
97602ac6454SAndrew Thompson static uhci_qh_t *
97702ac6454SAndrew Thompson _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
97802ac6454SAndrew Thompson {
97902ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", sqh, last);
98002ac6454SAndrew Thompson 
98102ac6454SAndrew Thompson 	/* (sc->sc_bus.mtx) must be locked */
98202ac6454SAndrew Thompson 
98302ac6454SAndrew Thompson 	/* only remove if not removed from a queue */
98402ac6454SAndrew Thompson 	if (sqh->h_prev) {
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson 		sqh->h_prev->h_next = sqh->h_next;
98702ac6454SAndrew Thompson 		sqh->h_prev->qh_h_next = sqh->qh_h_next;
98802ac6454SAndrew Thompson 
989a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->h_prev->page_cache);
99002ac6454SAndrew Thompson 
99102ac6454SAndrew Thompson 		if (sqh->h_next) {
99202ac6454SAndrew Thompson 			sqh->h_next->h_prev = sqh->h_prev;
993a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(sqh->h_next->page_cache);
99402ac6454SAndrew Thompson 		}
99502ac6454SAndrew Thompson 		last = ((last == sqh) ? sqh->h_prev : last);
99602ac6454SAndrew Thompson 
99702ac6454SAndrew Thompson 		sqh->h_prev = 0;
99802ac6454SAndrew Thompson 
999a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sqh->page_cache);
100002ac6454SAndrew Thompson 	}
100102ac6454SAndrew Thompson 	return (last);
100202ac6454SAndrew Thompson }
100302ac6454SAndrew Thompson 
100402ac6454SAndrew Thompson static void
1005760bc48eSAndrew Thompson uhci_isoc_done(uhci_softc_t *sc, struct usb_xfer *xfer)
100602ac6454SAndrew Thompson {
1007760bc48eSAndrew Thompson 	struct usb_page_search res;
100802ac6454SAndrew Thompson 	uint32_t nframes = xfer->nframes;
100902ac6454SAndrew Thompson 	uint32_t status;
101002ac6454SAndrew Thompson 	uint32_t offset = 0;
101102ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
101202ac6454SAndrew Thompson 	uint16_t len = 0;
101302ac6454SAndrew Thompson 	uhci_td_t *td = xfer->td_transfer_first;
101402ac6454SAndrew Thompson 	uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
101502ac6454SAndrew Thompson 
1016ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1017ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson 	/* sync any DMA memory before doing fixups */
102002ac6454SAndrew Thompson 
1021a593f6b8SAndrew Thompson 	usb_bdma_post_sync(xfer);
102202ac6454SAndrew Thompson 
102302ac6454SAndrew Thompson 	while (nframes--) {
102402ac6454SAndrew Thompson 		if (td == NULL) {
102502ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
102602ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
102702ac6454SAndrew Thompson 		}
102802ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
102902ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_p_last[0];
103002ac6454SAndrew Thompson 		}
1031b850ecc1SAndrew Thompson #ifdef USB_DEBUG
103202ac6454SAndrew Thompson 		if (uhcidebug > 5) {
103302ac6454SAndrew Thompson 			DPRINTF("isoc TD\n");
103402ac6454SAndrew Thompson 			uhci_dump_td(td);
103502ac6454SAndrew Thompson 		}
103602ac6454SAndrew Thompson #endif
1037a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
103802ac6454SAndrew Thompson 		status = le32toh(td->td_status);
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 		len = UHCI_TD_GET_ACTLEN(status);
104102ac6454SAndrew Thompson 
104202ac6454SAndrew Thompson 		if (len > *plen) {
104302ac6454SAndrew Thompson 			len = *plen;
104402ac6454SAndrew Thompson 		}
104502ac6454SAndrew Thompson 		if (td->fix_pc) {
104602ac6454SAndrew Thompson 
1047a593f6b8SAndrew Thompson 			usbd_get_page(td->fix_pc, 0, &res);
104802ac6454SAndrew Thompson 
104902ac6454SAndrew Thompson 			/* copy data from fixup location to real location */
105002ac6454SAndrew Thompson 
1051a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(td->fix_pc);
105202ac6454SAndrew Thompson 
1053a593f6b8SAndrew Thompson 			usbd_copy_in(xfer->frbuffers, offset,
105402ac6454SAndrew Thompson 			    res.buffer, len);
105502ac6454SAndrew Thompson 		}
105602ac6454SAndrew Thompson 		offset += *plen;
105702ac6454SAndrew Thompson 
105802ac6454SAndrew Thompson 		*plen = len;
105902ac6454SAndrew Thompson 
106002ac6454SAndrew Thompson 		/* remove TD from schedule */
106102ac6454SAndrew Thompson 		UHCI_REMOVE_TD(td, *pp_last);
106202ac6454SAndrew Thompson 
106302ac6454SAndrew Thompson 		pp_last++;
106402ac6454SAndrew Thompson 		plen++;
106502ac6454SAndrew Thompson 		td = td->obj_next;
106602ac6454SAndrew Thompson 	}
106702ac6454SAndrew Thompson 
106802ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
106902ac6454SAndrew Thompson }
107002ac6454SAndrew Thompson 
1071e0a69b51SAndrew Thompson static usb_error_t
1072760bc48eSAndrew Thompson uhci_non_isoc_done_sub(struct usb_xfer *xfer)
107302ac6454SAndrew Thompson {
1074760bc48eSAndrew Thompson 	struct usb_page_search res;
107502ac6454SAndrew Thompson 	uhci_td_t *td;
107602ac6454SAndrew Thompson 	uhci_td_t *td_alt_next;
107702ac6454SAndrew Thompson 	uint32_t status;
107802ac6454SAndrew Thompson 	uint32_t token;
107902ac6454SAndrew Thompson 	uint16_t len;
108002ac6454SAndrew Thompson 
108102ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
108202ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
108302ac6454SAndrew Thompson 
108402ac6454SAndrew Thompson 	if (xfer->aframes != xfer->nframes) {
1085ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
108602ac6454SAndrew Thompson 	}
108702ac6454SAndrew Thompson 	while (1) {
108802ac6454SAndrew Thompson 
1089a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
109002ac6454SAndrew Thompson 		status = le32toh(td->td_status);
109102ac6454SAndrew Thompson 		token = le32toh(td->td_token);
109202ac6454SAndrew Thompson 
109302ac6454SAndrew Thompson 		/*
109402ac6454SAndrew Thompson 	         * Verify the status and add
109502ac6454SAndrew Thompson 	         * up the actual length:
109602ac6454SAndrew Thompson 	         */
109702ac6454SAndrew Thompson 
109802ac6454SAndrew Thompson 		len = UHCI_TD_GET_ACTLEN(status);
109902ac6454SAndrew Thompson 		if (len > td->len) {
110002ac6454SAndrew Thompson 			/* should not happen */
110102ac6454SAndrew Thompson 			DPRINTF("Invalid status length, "
110202ac6454SAndrew Thompson 			    "0x%04x/0x%04x bytes\n", len, td->len);
110302ac6454SAndrew Thompson 			status |= UHCI_TD_STALLED;
110402ac6454SAndrew Thompson 
110502ac6454SAndrew Thompson 		} else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
110602ac6454SAndrew Thompson 
110702ac6454SAndrew Thompson 			if (td->fix_pc) {
110802ac6454SAndrew Thompson 
1109a593f6b8SAndrew Thompson 				usbd_get_page(td->fix_pc, 0, &res);
111002ac6454SAndrew Thompson 
111102ac6454SAndrew Thompson 				/*
111202ac6454SAndrew Thompson 				 * copy data from fixup location to real
111302ac6454SAndrew Thompson 				 * location
111402ac6454SAndrew Thompson 				 */
111502ac6454SAndrew Thompson 
1116a593f6b8SAndrew Thompson 				usb_pc_cpu_invalidate(td->fix_pc);
111702ac6454SAndrew Thompson 
1118a593f6b8SAndrew Thompson 				usbd_copy_in(xfer->frbuffers + xfer->aframes,
111902ac6454SAndrew Thompson 				    xfer->frlengths[xfer->aframes], res.buffer, len);
112002ac6454SAndrew Thompson 			}
112102ac6454SAndrew Thompson 			/* update actual length */
112202ac6454SAndrew Thompson 
112302ac6454SAndrew Thompson 			xfer->frlengths[xfer->aframes] += len;
112402ac6454SAndrew Thompson 		}
112502ac6454SAndrew Thompson 		/* Check for last transfer */
112602ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
112702ac6454SAndrew Thompson 			td = NULL;
112802ac6454SAndrew Thompson 			break;
112902ac6454SAndrew Thompson 		}
113002ac6454SAndrew Thompson 		if (status & UHCI_TD_STALLED) {
113102ac6454SAndrew Thompson 			/* the transfer is finished */
113202ac6454SAndrew Thompson 			td = NULL;
113302ac6454SAndrew Thompson 			break;
113402ac6454SAndrew Thompson 		}
113502ac6454SAndrew Thompson 		/* Check for short transfer */
113602ac6454SAndrew Thompson 		if (len != td->len) {
113702ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
113802ac6454SAndrew Thompson 				/* follow alt next */
113902ac6454SAndrew Thompson 				td = td->alt_next;
114002ac6454SAndrew Thompson 			} else {
114102ac6454SAndrew Thompson 				/* the transfer is finished */
114202ac6454SAndrew Thompson 				td = NULL;
114302ac6454SAndrew Thompson 			}
114402ac6454SAndrew Thompson 			break;
114502ac6454SAndrew Thompson 		}
114602ac6454SAndrew Thompson 		td = td->obj_next;
114702ac6454SAndrew Thompson 
114802ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
114902ac6454SAndrew Thompson 			/* this USB frame is complete */
115002ac6454SAndrew Thompson 			break;
115102ac6454SAndrew Thompson 		}
115202ac6454SAndrew Thompson 	}
115302ac6454SAndrew Thompson 
115402ac6454SAndrew Thompson 	/* update transfer cache */
115502ac6454SAndrew Thompson 
115602ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
115702ac6454SAndrew Thompson 
115802ac6454SAndrew Thompson 	/* update data toggle */
115902ac6454SAndrew Thompson 
1160ae60fdfbSAndrew Thompson 	xfer->endpoint->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
116102ac6454SAndrew Thompson 
1162b850ecc1SAndrew Thompson #ifdef USB_DEBUG
116302ac6454SAndrew Thompson 	if (status & UHCI_TD_ERROR) {
116402ac6454SAndrew Thompson 		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
116502ac6454SAndrew Thompson 		    "status=%s%s%s%s%s%s%s%s%s%s%s\n",
1166ae60fdfbSAndrew Thompson 		    xfer->address, xfer->endpointno, xfer->aframes,
116702ac6454SAndrew Thompson 		    (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
116802ac6454SAndrew Thompson 		    (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
116902ac6454SAndrew Thompson 		    (status & UHCI_TD_NAK) ? "[NAK]" : "",
117002ac6454SAndrew Thompson 		    (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
117102ac6454SAndrew Thompson 		    (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
117202ac6454SAndrew Thompson 		    (status & UHCI_TD_STALLED) ? "[STALLED]" : "",
117302ac6454SAndrew Thompson 		    (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
117402ac6454SAndrew Thompson 		    (status & UHCI_TD_IOC) ? "[IOC]" : "",
117502ac6454SAndrew Thompson 		    (status & UHCI_TD_IOS) ? "[IOS]" : "",
117602ac6454SAndrew Thompson 		    (status & UHCI_TD_LS) ? "[LS]" : "",
117702ac6454SAndrew Thompson 		    (status & UHCI_TD_SPD) ? "[SPD]" : "");
117802ac6454SAndrew Thompson 	}
117902ac6454SAndrew Thompson #endif
118002ac6454SAndrew Thompson 	return (status & UHCI_TD_STALLED) ?
118102ac6454SAndrew Thompson 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION;
118202ac6454SAndrew Thompson }
118302ac6454SAndrew Thompson 
118402ac6454SAndrew Thompson static void
1185760bc48eSAndrew Thompson uhci_non_isoc_done(struct usb_xfer *xfer)
118602ac6454SAndrew Thompson {
1187e0a69b51SAndrew Thompson 	usb_error_t err = 0;
118802ac6454SAndrew Thompson 
1189ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1190ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
119102ac6454SAndrew Thompson 
1192b850ecc1SAndrew Thompson #ifdef USB_DEBUG
119302ac6454SAndrew Thompson 	if (uhcidebug > 10) {
119402ac6454SAndrew Thompson 		uhci_dump_tds(xfer->td_transfer_first);
119502ac6454SAndrew Thompson 	}
119602ac6454SAndrew Thompson #endif
119702ac6454SAndrew Thompson 
119802ac6454SAndrew Thompson 	/* sync any DMA memory before doing fixups */
119902ac6454SAndrew Thompson 
1200a593f6b8SAndrew Thompson 	usb_bdma_post_sync(xfer);
120102ac6454SAndrew Thompson 
120202ac6454SAndrew Thompson 	/* reset scanner */
120302ac6454SAndrew Thompson 
120402ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
120502ac6454SAndrew Thompson 
120602ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
120702ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 			err = uhci_non_isoc_done_sub(xfer);
121002ac6454SAndrew Thompson 		}
121102ac6454SAndrew Thompson 		xfer->aframes = 1;
121202ac6454SAndrew Thompson 
121302ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
121402ac6454SAndrew Thompson 			goto done;
121502ac6454SAndrew Thompson 		}
121602ac6454SAndrew Thompson 	}
121702ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
121802ac6454SAndrew Thompson 
121902ac6454SAndrew Thompson 		err = uhci_non_isoc_done_sub(xfer);
122002ac6454SAndrew Thompson 		xfer->aframes++;
122102ac6454SAndrew Thompson 
122202ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
122302ac6454SAndrew Thompson 			goto done;
122402ac6454SAndrew Thompson 		}
122502ac6454SAndrew Thompson 	}
122602ac6454SAndrew Thompson 
122702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
122802ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
122902ac6454SAndrew Thompson 
123002ac6454SAndrew Thompson 		err = uhci_non_isoc_done_sub(xfer);
123102ac6454SAndrew Thompson 	}
123202ac6454SAndrew Thompson done:
123302ac6454SAndrew Thompson 	uhci_device_done(xfer, err);
123402ac6454SAndrew Thompson }
123502ac6454SAndrew Thompson 
123602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
123702ac6454SAndrew Thompson  *	uhci_check_transfer_sub
123802ac6454SAndrew Thompson  *
123902ac6454SAndrew Thompson  * The main purpose of this function is to update the data-toggle
124002ac6454SAndrew Thompson  * in case it is wrong.
124102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
124202ac6454SAndrew Thompson static void
1243760bc48eSAndrew Thompson uhci_check_transfer_sub(struct usb_xfer *xfer)
124402ac6454SAndrew Thompson {
124502ac6454SAndrew Thompson 	uhci_qh_t *qh;
124602ac6454SAndrew Thompson 	uhci_td_t *td;
124702ac6454SAndrew Thompson 	uhci_td_t *td_alt_next;
124802ac6454SAndrew Thompson 
124902ac6454SAndrew Thompson 	uint32_t td_token;
125002ac6454SAndrew Thompson 	uint32_t td_self;
125102ac6454SAndrew Thompson 
125202ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
125302ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
125402ac6454SAndrew Thompson 
125502ac6454SAndrew Thompson 	td_token = td->obj_next->td_token;
125602ac6454SAndrew Thompson 	td = td->alt_next;
125702ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
125802ac6454SAndrew Thompson 	td_self = td->td_self;
125902ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
126002ac6454SAndrew Thompson 
12610f7d4548SAndrew Thompson 	if (xfer->flags_int.control_xfr)
12620f7d4548SAndrew Thompson 		goto skip;	/* don't touch the DT value! */
12630f7d4548SAndrew Thompson 
12640f7d4548SAndrew Thompson 	if (!((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))))
12650f7d4548SAndrew Thompson 		goto skip;	/* data toggle has correct value */
126602ac6454SAndrew Thompson 
126702ac6454SAndrew Thompson 	/*
12680f7d4548SAndrew Thompson 	 * The data toggle is wrong and we need to toggle it !
126902ac6454SAndrew Thompson 	 */
127002ac6454SAndrew Thompson 	while (1) {
127102ac6454SAndrew Thompson 
127202ac6454SAndrew Thompson 		td->td_token ^= htole32(UHCI_TD_SET_DT(1));
1273a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
127402ac6454SAndrew Thompson 
127502ac6454SAndrew Thompson 		if (td == xfer->td_transfer_last) {
127602ac6454SAndrew Thompson 			/* last transfer */
127702ac6454SAndrew Thompson 			break;
127802ac6454SAndrew Thompson 		}
127902ac6454SAndrew Thompson 		td = td->obj_next;
128002ac6454SAndrew Thompson 
128102ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
128202ac6454SAndrew Thompson 			/* next frame */
128302ac6454SAndrew Thompson 			break;
128402ac6454SAndrew Thompson 		}
128502ac6454SAndrew Thompson 	}
12860f7d4548SAndrew Thompson skip:
12870f7d4548SAndrew Thompson 
128802ac6454SAndrew Thompson 	/* update the QH */
128902ac6454SAndrew Thompson 	qh->qh_e_next = td_self;
1290a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(qh->page_cache);
129102ac6454SAndrew Thompson 
129202ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p following alt next\n", xfer);
129302ac6454SAndrew Thompson }
129402ac6454SAndrew Thompson 
129502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
129602ac6454SAndrew Thompson  *	uhci_check_transfer
129702ac6454SAndrew Thompson  *
129802ac6454SAndrew Thompson  * Return values:
129902ac6454SAndrew Thompson  *    0: USB transfer is not finished
130002ac6454SAndrew Thompson  * Else: USB transfer is finished
130102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
130202ac6454SAndrew Thompson static uint8_t
1303760bc48eSAndrew Thompson uhci_check_transfer(struct usb_xfer *xfer)
130402ac6454SAndrew Thompson {
130502ac6454SAndrew Thompson 	uint32_t status;
130602ac6454SAndrew Thompson 	uint32_t token;
130702ac6454SAndrew Thompson 	uhci_td_t *td;
130802ac6454SAndrew Thompson 
130902ac6454SAndrew Thompson 	DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
131002ac6454SAndrew Thompson 
1311ae60fdfbSAndrew Thompson 	if (xfer->endpoint->methods == &uhci_device_isoc_methods) {
131202ac6454SAndrew Thompson 		/* isochronous transfer */
131302ac6454SAndrew Thompson 
131402ac6454SAndrew Thompson 		td = xfer->td_transfer_last;
131502ac6454SAndrew Thompson 
1316a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
131702ac6454SAndrew Thompson 		status = le32toh(td->td_status);
131802ac6454SAndrew Thompson 
131902ac6454SAndrew Thompson 		/* check also if the first is complete */
132002ac6454SAndrew Thompson 
132102ac6454SAndrew Thompson 		td = xfer->td_transfer_first;
132202ac6454SAndrew Thompson 
1323a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
132402ac6454SAndrew Thompson 		status |= le32toh(td->td_status);
132502ac6454SAndrew Thompson 
132602ac6454SAndrew Thompson 		if (!(status & UHCI_TD_ACTIVE)) {
132702ac6454SAndrew Thompson 			uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
132802ac6454SAndrew Thompson 			goto transferred;
132902ac6454SAndrew Thompson 		}
133002ac6454SAndrew Thompson 	} else {
133102ac6454SAndrew Thompson 		/* non-isochronous transfer */
133202ac6454SAndrew Thompson 
133302ac6454SAndrew Thompson 		/*
133402ac6454SAndrew Thompson 		 * check whether there is an error somewhere
133502ac6454SAndrew Thompson 		 * in the middle, or whether there was a short
133602ac6454SAndrew Thompson 		 * packet (SPD and not ACTIVE)
133702ac6454SAndrew Thompson 		 */
133802ac6454SAndrew Thompson 		td = xfer->td_transfer_cache;
133902ac6454SAndrew Thompson 
134002ac6454SAndrew Thompson 		while (1) {
1341a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(td->page_cache);
134202ac6454SAndrew Thompson 			status = le32toh(td->td_status);
134302ac6454SAndrew Thompson 			token = le32toh(td->td_token);
134402ac6454SAndrew Thompson 
134502ac6454SAndrew Thompson 			/*
134602ac6454SAndrew Thompson 			 * if there is an active TD the transfer isn't done
134702ac6454SAndrew Thompson 			 */
134802ac6454SAndrew Thompson 			if (status & UHCI_TD_ACTIVE) {
134902ac6454SAndrew Thompson 				/* update cache */
135002ac6454SAndrew Thompson 				xfer->td_transfer_cache = td;
135102ac6454SAndrew Thompson 				goto done;
135202ac6454SAndrew Thompson 			}
135302ac6454SAndrew Thompson 			/*
135402ac6454SAndrew Thompson 			 * last transfer descriptor makes the transfer done
135502ac6454SAndrew Thompson 			 */
135602ac6454SAndrew Thompson 			if (((void *)td) == xfer->td_transfer_last) {
135702ac6454SAndrew Thompson 				break;
135802ac6454SAndrew Thompson 			}
135902ac6454SAndrew Thompson 			/*
136002ac6454SAndrew Thompson 			 * any kind of error makes the transfer done
136102ac6454SAndrew Thompson 			 */
136202ac6454SAndrew Thompson 			if (status & UHCI_TD_STALLED) {
136302ac6454SAndrew Thompson 				break;
136402ac6454SAndrew Thompson 			}
136502ac6454SAndrew Thompson 			/*
136602ac6454SAndrew Thompson 			 * check if we reached the last packet
136702ac6454SAndrew Thompson 			 * or if there is a short packet:
136802ac6454SAndrew Thompson 			 */
136902ac6454SAndrew Thompson 			if ((td->td_next == htole32(UHCI_PTR_T)) ||
137002ac6454SAndrew Thompson 			    (UHCI_TD_GET_ACTLEN(status) < td->len)) {
137102ac6454SAndrew Thompson 
137202ac6454SAndrew Thompson 				if (xfer->flags_int.short_frames_ok) {
137302ac6454SAndrew Thompson 					/* follow alt next */
137402ac6454SAndrew Thompson 					if (td->alt_next) {
137502ac6454SAndrew Thompson 						/* update cache */
137602ac6454SAndrew Thompson 						xfer->td_transfer_cache = td;
137702ac6454SAndrew Thompson 						uhci_check_transfer_sub(xfer);
137802ac6454SAndrew Thompson 						goto done;
137902ac6454SAndrew Thompson 					}
138002ac6454SAndrew Thompson 				}
138102ac6454SAndrew Thompson 				/* transfer is done */
138202ac6454SAndrew Thompson 				break;
138302ac6454SAndrew Thompson 			}
138402ac6454SAndrew Thompson 			td = td->obj_next;
138502ac6454SAndrew Thompson 		}
138602ac6454SAndrew Thompson 		uhci_non_isoc_done(xfer);
138702ac6454SAndrew Thompson 		goto transferred;
138802ac6454SAndrew Thompson 	}
138902ac6454SAndrew Thompson 
139002ac6454SAndrew Thompson done:
139102ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
139202ac6454SAndrew Thompson 	return (0);
139302ac6454SAndrew Thompson 
139402ac6454SAndrew Thompson transferred:
139502ac6454SAndrew Thompson 	return (1);
139602ac6454SAndrew Thompson }
139702ac6454SAndrew Thompson 
139802ac6454SAndrew Thompson static void
139902ac6454SAndrew Thompson uhci_interrupt_poll(uhci_softc_t *sc)
140002ac6454SAndrew Thompson {
1401760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
140202ac6454SAndrew Thompson 
140302ac6454SAndrew Thompson repeat:
140402ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
140502ac6454SAndrew Thompson 		/*
140602ac6454SAndrew Thompson 		 * check if transfer is transferred
140702ac6454SAndrew Thompson 		 */
140802ac6454SAndrew Thompson 		if (uhci_check_transfer(xfer)) {
140902ac6454SAndrew Thompson 			/* queue has been modified */
141002ac6454SAndrew Thompson 			goto repeat;
141102ac6454SAndrew Thompson 		}
141202ac6454SAndrew Thompson 	}
141302ac6454SAndrew Thompson }
141402ac6454SAndrew Thompson 
141502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
141602ac6454SAndrew Thompson  *	uhci_interrupt - UHCI interrupt handler
141702ac6454SAndrew Thompson  *
141802ac6454SAndrew Thompson  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
141902ac6454SAndrew Thompson  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
142002ac6454SAndrew Thompson  * is present !
142102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
142202ac6454SAndrew Thompson void
142302ac6454SAndrew Thompson uhci_interrupt(uhci_softc_t *sc)
142402ac6454SAndrew Thompson {
142502ac6454SAndrew Thompson 	uint32_t status;
142602ac6454SAndrew Thompson 
142702ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
142802ac6454SAndrew Thompson 
142902ac6454SAndrew Thompson 	DPRINTFN(16, "real interrupt\n");
143002ac6454SAndrew Thompson 
1431b850ecc1SAndrew Thompson #ifdef USB_DEBUG
143202ac6454SAndrew Thompson 	if (uhcidebug > 15) {
143302ac6454SAndrew Thompson 		uhci_dumpregs(sc);
143402ac6454SAndrew Thompson 	}
143502ac6454SAndrew Thompson #endif
143602ac6454SAndrew Thompson 	status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
143702ac6454SAndrew Thompson 	if (status == 0) {
143802ac6454SAndrew Thompson 		/* the interrupt was not for us */
143902ac6454SAndrew Thompson 		goto done;
144002ac6454SAndrew Thompson 	}
144102ac6454SAndrew Thompson 	if (status & (UHCI_STS_RD | UHCI_STS_HSE |
144202ac6454SAndrew Thompson 	    UHCI_STS_HCPE | UHCI_STS_HCH)) {
144302ac6454SAndrew Thompson 
144402ac6454SAndrew Thompson 		if (status & UHCI_STS_RD) {
1445b850ecc1SAndrew Thompson #ifdef USB_DEBUG
144602ac6454SAndrew Thompson 			printf("%s: resume detect\n",
144702ac6454SAndrew Thompson 			    __FUNCTION__);
144802ac6454SAndrew Thompson #endif
144902ac6454SAndrew Thompson 		}
145002ac6454SAndrew Thompson 		if (status & UHCI_STS_HSE) {
145102ac6454SAndrew Thompson 			printf("%s: host system error\n",
145202ac6454SAndrew Thompson 			    __FUNCTION__);
145302ac6454SAndrew Thompson 		}
145402ac6454SAndrew Thompson 		if (status & UHCI_STS_HCPE) {
145502ac6454SAndrew Thompson 			printf("%s: host controller process error\n",
145602ac6454SAndrew Thompson 			    __FUNCTION__);
145702ac6454SAndrew Thompson 		}
145802ac6454SAndrew Thompson 		if (status & UHCI_STS_HCH) {
145902ac6454SAndrew Thompson 			/* no acknowledge needed */
146002ac6454SAndrew Thompson 			DPRINTF("%s: host controller halted\n",
146102ac6454SAndrew Thompson 			    __FUNCTION__);
1462b850ecc1SAndrew Thompson #ifdef USB_DEBUG
146302ac6454SAndrew Thompson 			if (uhcidebug > 0) {
146402ac6454SAndrew Thompson 				uhci_dump_all(sc);
146502ac6454SAndrew Thompson 			}
146602ac6454SAndrew Thompson #endif
146702ac6454SAndrew Thompson 		}
146802ac6454SAndrew Thompson 	}
146902ac6454SAndrew Thompson 	/* get acknowledge bits */
147002ac6454SAndrew Thompson 	status &= (UHCI_STS_USBINT |
147102ac6454SAndrew Thompson 	    UHCI_STS_USBEI |
147202ac6454SAndrew Thompson 	    UHCI_STS_RD |
147302ac6454SAndrew Thompson 	    UHCI_STS_HSE |
147402ac6454SAndrew Thompson 	    UHCI_STS_HCPE);
147502ac6454SAndrew Thompson 
147602ac6454SAndrew Thompson 	if (status == 0) {
147702ac6454SAndrew Thompson 		/* nothing to acknowledge */
147802ac6454SAndrew Thompson 		goto done;
147902ac6454SAndrew Thompson 	}
148002ac6454SAndrew Thompson 	/* acknowledge interrupts */
148102ac6454SAndrew Thompson 	UWRITE2(sc, UHCI_STS, status);
148202ac6454SAndrew Thompson 
148302ac6454SAndrew Thompson 	/* poll all the USB transfers */
148402ac6454SAndrew Thompson 	uhci_interrupt_poll(sc);
148502ac6454SAndrew Thompson 
148602ac6454SAndrew Thompson done:
148702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
148802ac6454SAndrew Thompson }
148902ac6454SAndrew Thompson 
149002ac6454SAndrew Thompson /*
149102ac6454SAndrew Thompson  * called when a request does not complete
149202ac6454SAndrew Thompson  */
149302ac6454SAndrew Thompson static void
149402ac6454SAndrew Thompson uhci_timeout(void *arg)
149502ac6454SAndrew Thompson {
1496760bc48eSAndrew Thompson 	struct usb_xfer *xfer = arg;
149702ac6454SAndrew Thompson 
149802ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
149902ac6454SAndrew Thompson 
150002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
150102ac6454SAndrew Thompson 
150202ac6454SAndrew Thompson 	/* transfer is transferred */
150302ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_TIMEOUT);
150402ac6454SAndrew Thompson }
150502ac6454SAndrew Thompson 
150602ac6454SAndrew Thompson static void
1507760bc48eSAndrew Thompson uhci_do_poll(struct usb_bus *bus)
150802ac6454SAndrew Thompson {
150902ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
151002ac6454SAndrew Thompson 
151102ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
151202ac6454SAndrew Thompson 	uhci_interrupt_poll(sc);
151302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
151402ac6454SAndrew Thompson }
151502ac6454SAndrew Thompson 
151602ac6454SAndrew Thompson static void
151702ac6454SAndrew Thompson uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
151802ac6454SAndrew Thompson {
151902ac6454SAndrew Thompson 	uhci_td_t *td;
152002ac6454SAndrew Thompson 	uhci_td_t *td_next;
152102ac6454SAndrew Thompson 	uhci_td_t *td_alt_next;
152202ac6454SAndrew Thompson 	uint32_t average;
152302ac6454SAndrew Thompson 	uint32_t len_old;
152402ac6454SAndrew Thompson 	uint8_t shortpkt_old;
152502ac6454SAndrew Thompson 	uint8_t precompute;
152602ac6454SAndrew Thompson 
152702ac6454SAndrew Thompson 	td_alt_next = NULL;
152802ac6454SAndrew Thompson 	shortpkt_old = temp->shortpkt;
152902ac6454SAndrew Thompson 	len_old = temp->len;
153002ac6454SAndrew Thompson 	precompute = 1;
153102ac6454SAndrew Thompson 
153202ac6454SAndrew Thompson 	/* software is used to detect short incoming transfers */
153302ac6454SAndrew Thompson 
153402ac6454SAndrew Thompson 	if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
153502ac6454SAndrew Thompson 		temp->td_status |= htole32(UHCI_TD_SPD);
153602ac6454SAndrew Thompson 	} else {
153702ac6454SAndrew Thompson 		temp->td_status &= ~htole32(UHCI_TD_SPD);
153802ac6454SAndrew Thompson 	}
153902ac6454SAndrew Thompson 
154002ac6454SAndrew Thompson 	temp->ml.buf_offset = 0;
154102ac6454SAndrew Thompson 
154202ac6454SAndrew Thompson restart:
154302ac6454SAndrew Thompson 
154402ac6454SAndrew Thompson 	temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
154502ac6454SAndrew Thompson 	temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
154602ac6454SAndrew Thompson 
154702ac6454SAndrew Thompson 	td = temp->td;
154802ac6454SAndrew Thompson 	td_next = temp->td_next;
154902ac6454SAndrew Thompson 
155002ac6454SAndrew Thompson 	while (1) {
155102ac6454SAndrew Thompson 
155202ac6454SAndrew Thompson 		if (temp->len == 0) {
155302ac6454SAndrew Thompson 
155402ac6454SAndrew Thompson 			if (temp->shortpkt) {
155502ac6454SAndrew Thompson 				break;
155602ac6454SAndrew Thompson 			}
155702ac6454SAndrew Thompson 			/* send a Zero Length Packet, ZLP, last */
155802ac6454SAndrew Thompson 
155902ac6454SAndrew Thompson 			temp->shortpkt = 1;
156002ac6454SAndrew Thompson 			temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
156102ac6454SAndrew Thompson 			average = 0;
156202ac6454SAndrew Thompson 
156302ac6454SAndrew Thompson 		} else {
156402ac6454SAndrew Thompson 
156502ac6454SAndrew Thompson 			average = temp->average;
156602ac6454SAndrew Thompson 
156702ac6454SAndrew Thompson 			if (temp->len < average) {
156802ac6454SAndrew Thompson 				temp->shortpkt = 1;
156902ac6454SAndrew Thompson 				temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
157002ac6454SAndrew Thompson 				temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
157102ac6454SAndrew Thompson 				average = temp->len;
157202ac6454SAndrew Thompson 			}
157302ac6454SAndrew Thompson 		}
157402ac6454SAndrew Thompson 
157502ac6454SAndrew Thompson 		if (td_next == NULL) {
157602ac6454SAndrew Thompson 			panic("%s: out of UHCI transfer descriptors!", __FUNCTION__);
157702ac6454SAndrew Thompson 		}
157802ac6454SAndrew Thompson 		/* get next TD */
157902ac6454SAndrew Thompson 
158002ac6454SAndrew Thompson 		td = td_next;
158102ac6454SAndrew Thompson 		td_next = td->obj_next;
158202ac6454SAndrew Thompson 
158302ac6454SAndrew Thompson 		/* check if we are pre-computing */
158402ac6454SAndrew Thompson 
158502ac6454SAndrew Thompson 		if (precompute) {
158602ac6454SAndrew Thompson 
158702ac6454SAndrew Thompson 			/* update remaining length */
158802ac6454SAndrew Thompson 
158902ac6454SAndrew Thompson 			temp->len -= average;
159002ac6454SAndrew Thompson 
159102ac6454SAndrew Thompson 			continue;
159202ac6454SAndrew Thompson 		}
159302ac6454SAndrew Thompson 		/* fill out current TD */
159402ac6454SAndrew Thompson 
159502ac6454SAndrew Thompson 		td->td_status = temp->td_status;
159602ac6454SAndrew Thompson 		td->td_token = temp->td_token;
159702ac6454SAndrew Thompson 
159802ac6454SAndrew Thompson 		/* update data toggle */
159902ac6454SAndrew Thompson 
160002ac6454SAndrew Thompson 		temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
160102ac6454SAndrew Thompson 
160202ac6454SAndrew Thompson 		if (average == 0) {
160302ac6454SAndrew Thompson 
160402ac6454SAndrew Thompson 			td->len = 0;
160502ac6454SAndrew Thompson 			td->td_buffer = 0;
160602ac6454SAndrew Thompson 			td->fix_pc = NULL;
160702ac6454SAndrew Thompson 
160802ac6454SAndrew Thompson 		} else {
160902ac6454SAndrew Thompson 
161002ac6454SAndrew Thompson 			/* update remaining length */
161102ac6454SAndrew Thompson 
161202ac6454SAndrew Thompson 			temp->len -= average;
161302ac6454SAndrew Thompson 
161402ac6454SAndrew Thompson 			td->len = average;
161502ac6454SAndrew Thompson 
161602ac6454SAndrew Thompson 			/* fill out buffer pointer and do fixup, if any */
161702ac6454SAndrew Thompson 
161802ac6454SAndrew Thompson 			uhci_mem_layout_fixup(&temp->ml, td);
161902ac6454SAndrew Thompson 		}
162002ac6454SAndrew Thompson 
162102ac6454SAndrew Thompson 		td->alt_next = td_alt_next;
162202ac6454SAndrew Thompson 
162302ac6454SAndrew Thompson 		if ((td_next == td_alt_next) && temp->setup_alt_next) {
162402ac6454SAndrew Thompson 			/* we need to receive these frames one by one ! */
162502ac6454SAndrew Thompson 			td->td_status |= htole32(UHCI_TD_IOC);
162602ac6454SAndrew Thompson 			td->td_next = htole32(UHCI_PTR_T);
162702ac6454SAndrew Thompson 		} else {
162802ac6454SAndrew Thompson 			if (td_next) {
162902ac6454SAndrew Thompson 				/* link the current TD with the next one */
163002ac6454SAndrew Thompson 				td->td_next = td_next->td_self;
163102ac6454SAndrew Thompson 			}
163202ac6454SAndrew Thompson 		}
163302ac6454SAndrew Thompson 
1634a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
163502ac6454SAndrew Thompson 	}
163602ac6454SAndrew Thompson 
163702ac6454SAndrew Thompson 	if (precompute) {
163802ac6454SAndrew Thompson 		precompute = 0;
163902ac6454SAndrew Thompson 
164002ac6454SAndrew Thompson 		/* setup alt next pointer, if any */
16410f7d4548SAndrew Thompson 		if (temp->last_frame) {
16420f7d4548SAndrew Thompson 			td_alt_next = NULL;
164302ac6454SAndrew Thompson 		} else {
164402ac6454SAndrew Thompson 			/* we use this field internally */
164502ac6454SAndrew Thompson 			td_alt_next = td_next;
164602ac6454SAndrew Thompson 		}
164702ac6454SAndrew Thompson 
164802ac6454SAndrew Thompson 		/* restore */
164902ac6454SAndrew Thompson 		temp->shortpkt = shortpkt_old;
165002ac6454SAndrew Thompson 		temp->len = len_old;
165102ac6454SAndrew Thompson 		goto restart;
165202ac6454SAndrew Thompson 	}
165302ac6454SAndrew Thompson 	temp->td = td;
165402ac6454SAndrew Thompson 	temp->td_next = td_next;
165502ac6454SAndrew Thompson }
165602ac6454SAndrew Thompson 
165702ac6454SAndrew Thompson static uhci_td_t *
1658760bc48eSAndrew Thompson uhci_setup_standard_chain(struct usb_xfer *xfer)
165902ac6454SAndrew Thompson {
166002ac6454SAndrew Thompson 	struct uhci_std_temp temp;
166102ac6454SAndrew Thompson 	uhci_td_t *td;
166202ac6454SAndrew Thompson 	uint32_t x;
166302ac6454SAndrew Thompson 
166402ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1665ae60fdfbSAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
1666a593f6b8SAndrew Thompson 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
166702ac6454SAndrew Thompson 
166802ac6454SAndrew Thompson 	temp.average = xfer->max_frame_size;
166902ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
167002ac6454SAndrew Thompson 
167102ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
167202ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
167302ac6454SAndrew Thompson 
167402ac6454SAndrew Thompson 	/* get next DMA set */
167502ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
167602ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
167702ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
167802ac6454SAndrew Thompson 
167902ac6454SAndrew Thompson 	temp.td = NULL;
168002ac6454SAndrew Thompson 	temp.td_next = td;
16810f7d4548SAndrew Thompson 	temp.last_frame = 0;
168202ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
168302ac6454SAndrew Thompson 
168402ac6454SAndrew Thompson 	uhci_mem_layout_init(&temp.ml, xfer);
168502ac6454SAndrew Thompson 
168602ac6454SAndrew Thompson 	temp.td_status =
168702ac6454SAndrew Thompson 	    htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
168802ac6454SAndrew Thompson 	    UHCI_TD_ACTIVE));
168902ac6454SAndrew Thompson 
169002ac6454SAndrew Thompson 	if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
169102ac6454SAndrew Thompson 		temp.td_status |= htole32(UHCI_TD_LS);
169202ac6454SAndrew Thompson 	}
169302ac6454SAndrew Thompson 	temp.td_token =
1694ae60fdfbSAndrew Thompson 	    htole32(UHCI_TD_SET_ENDPT(xfer->endpointno) |
169502ac6454SAndrew Thompson 	    UHCI_TD_SET_DEVADDR(xfer->address));
169602ac6454SAndrew Thompson 
1697ae60fdfbSAndrew Thompson 	if (xfer->endpoint->toggle_next) {
169802ac6454SAndrew Thompson 		/* DATA1 is next */
169902ac6454SAndrew Thompson 		temp.td_token |= htole32(UHCI_TD_SET_DT(1));
170002ac6454SAndrew Thompson 	}
170102ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
170202ac6454SAndrew Thompson 
170302ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
170402ac6454SAndrew Thompson 
170502ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
170602ac6454SAndrew Thompson 
170702ac6454SAndrew Thompson 			temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
170802ac6454SAndrew Thompson 			    UHCI_TD_SET_ENDPT(0xF));
170902ac6454SAndrew Thompson 			temp.td_token |= htole32(UHCI_TD_PID_SETUP |
171002ac6454SAndrew Thompson 			    UHCI_TD_SET_DT(0));
171102ac6454SAndrew Thompson 
171202ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
171302ac6454SAndrew Thompson 			temp.ml.buf_pc = xfer->frbuffers + 0;
171402ac6454SAndrew Thompson 			temp.shortpkt = temp.len ? 1 : 0;
17150f7d4548SAndrew Thompson 			/* check for last frame */
17160f7d4548SAndrew Thompson 			if (xfer->nframes == 1) {
17170f7d4548SAndrew Thompson 				/* no STATUS stage yet, SETUP is last */
17180f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
17190f7d4548SAndrew Thompson 					temp.last_frame = 1;
17200f7d4548SAndrew Thompson 					temp.setup_alt_next = 0;
17210f7d4548SAndrew Thompson 				}
17220f7d4548SAndrew Thompson 			}
172302ac6454SAndrew Thompson 			uhci_setup_standard_chain_sub(&temp);
172402ac6454SAndrew Thompson 		}
172502ac6454SAndrew Thompson 		x = 1;
172602ac6454SAndrew Thompson 	} else {
172702ac6454SAndrew Thompson 		x = 0;
172802ac6454SAndrew Thompson 	}
172902ac6454SAndrew Thompson 
173002ac6454SAndrew Thompson 	while (x != xfer->nframes) {
173102ac6454SAndrew Thompson 
173202ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
173302ac6454SAndrew Thompson 
173402ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
173502ac6454SAndrew Thompson 		temp.ml.buf_pc = xfer->frbuffers + x;
173602ac6454SAndrew Thompson 
173702ac6454SAndrew Thompson 		x++;
173802ac6454SAndrew Thompson 
173902ac6454SAndrew Thompson 		if (x == xfer->nframes) {
17400f7d4548SAndrew Thompson 			if (xfer->flags_int.control_xfr) {
17410f7d4548SAndrew Thompson 				/* no STATUS stage yet, DATA is last */
17420f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
17430f7d4548SAndrew Thompson 					temp.last_frame = 1;
174402ac6454SAndrew Thompson 					temp.setup_alt_next = 0;
174502ac6454SAndrew Thompson 				}
17460f7d4548SAndrew Thompson 			} else {
17470f7d4548SAndrew Thompson 				temp.last_frame = 1;
17480f7d4548SAndrew Thompson 				temp.setup_alt_next = 0;
17490f7d4548SAndrew Thompson 			}
17500f7d4548SAndrew Thompson 		}
175102ac6454SAndrew Thompson 		/*
175202ac6454SAndrew Thompson 		 * Keep previous data toggle,
175302ac6454SAndrew Thompson 		 * device address and endpoint number:
175402ac6454SAndrew Thompson 		 */
175502ac6454SAndrew Thompson 
175602ac6454SAndrew Thompson 		temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
175702ac6454SAndrew Thompson 		    UHCI_TD_SET_ENDPT(0xF) |
175802ac6454SAndrew Thompson 		    UHCI_TD_SET_DT(1));
175902ac6454SAndrew Thompson 
176002ac6454SAndrew Thompson 		if (temp.len == 0) {
176102ac6454SAndrew Thompson 
176202ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
176302ac6454SAndrew Thompson 
176402ac6454SAndrew Thompson 			temp.shortpkt = 0;
176502ac6454SAndrew Thompson 
176602ac6454SAndrew Thompson 		} else {
176702ac6454SAndrew Thompson 
176802ac6454SAndrew Thompson 			/* regular data transfer */
176902ac6454SAndrew Thompson 
177002ac6454SAndrew Thompson 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
177102ac6454SAndrew Thompson 		}
177202ac6454SAndrew Thompson 
177302ac6454SAndrew Thompson 		/* set endpoint direction */
177402ac6454SAndrew Thompson 
177502ac6454SAndrew Thompson 		temp.td_token |=
1776ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
177702ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_IN) :
177802ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_OUT);
177902ac6454SAndrew Thompson 
178002ac6454SAndrew Thompson 		uhci_setup_standard_chain_sub(&temp);
178102ac6454SAndrew Thompson 	}
178202ac6454SAndrew Thompson 
178302ac6454SAndrew Thompson 	/* check if we should append a status stage */
178402ac6454SAndrew Thompson 
178502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
178602ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
178702ac6454SAndrew Thompson 
178802ac6454SAndrew Thompson 		/*
178902ac6454SAndrew Thompson 		 * send a DATA1 message and reverse the current endpoint
179002ac6454SAndrew Thompson 		 * direction
179102ac6454SAndrew Thompson 		 */
179202ac6454SAndrew Thompson 
179302ac6454SAndrew Thompson 		temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
179402ac6454SAndrew Thompson 		    UHCI_TD_SET_ENDPT(0xF) |
179502ac6454SAndrew Thompson 		    UHCI_TD_SET_DT(1));
179602ac6454SAndrew Thompson 		temp.td_token |=
1797ae60fdfbSAndrew Thompson 		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
179802ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
179902ac6454SAndrew Thompson 		    htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
180002ac6454SAndrew Thompson 
180102ac6454SAndrew Thompson 		temp.len = 0;
180202ac6454SAndrew Thompson 		temp.ml.buf_pc = NULL;
180302ac6454SAndrew Thompson 		temp.shortpkt = 0;
18040f7d4548SAndrew Thompson 		temp.last_frame = 1;
18050f7d4548SAndrew Thompson 		temp.setup_alt_next = 0;
180602ac6454SAndrew Thompson 
180702ac6454SAndrew Thompson 		uhci_setup_standard_chain_sub(&temp);
180802ac6454SAndrew Thompson 	}
180902ac6454SAndrew Thompson 	td = temp.td;
181002ac6454SAndrew Thompson 
18110f7d4548SAndrew Thompson 	/* Ensure that last TD is terminating: */
181202ac6454SAndrew Thompson 	td->td_next = htole32(UHCI_PTR_T);
181302ac6454SAndrew Thompson 
181402ac6454SAndrew Thompson 	/* set interrupt bit */
181502ac6454SAndrew Thompson 
181602ac6454SAndrew Thompson 	td->td_status |= htole32(UHCI_TD_IOC);
181702ac6454SAndrew Thompson 
1818a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(td->page_cache);
181902ac6454SAndrew Thompson 
182002ac6454SAndrew Thompson 	/* must have at least one frame! */
182102ac6454SAndrew Thompson 
182202ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
182302ac6454SAndrew Thompson 
1824b850ecc1SAndrew Thompson #ifdef USB_DEBUG
182502ac6454SAndrew Thompson 	if (uhcidebug > 8) {
182602ac6454SAndrew Thompson 		DPRINTF("nexttog=%d; data before transfer:\n",
1827ae60fdfbSAndrew Thompson 		    xfer->endpoint->toggle_next);
182802ac6454SAndrew Thompson 		uhci_dump_tds(xfer->td_transfer_first);
182902ac6454SAndrew Thompson 	}
183002ac6454SAndrew Thompson #endif
183102ac6454SAndrew Thompson 	return (xfer->td_transfer_first);
183202ac6454SAndrew Thompson }
183302ac6454SAndrew Thompson 
183402ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row,
183502ac6454SAndrew Thompson  * from close and from interrupt
183602ac6454SAndrew Thompson  */
183702ac6454SAndrew Thompson 
183802ac6454SAndrew Thompson static void
1839e0a69b51SAndrew Thompson uhci_device_done(struct usb_xfer *xfer, usb_error_t error)
184002ac6454SAndrew Thompson {
1841ae60fdfbSAndrew Thompson 	struct usb_pipe_methods *methods = xfer->endpoint->methods;
184202ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
184302ac6454SAndrew Thompson 	uhci_qh_t *qh;
184402ac6454SAndrew Thompson 
184502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
184602ac6454SAndrew Thompson 
1847ae60fdfbSAndrew Thompson 	DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1848ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint, error);
184902ac6454SAndrew Thompson 
185002ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
185102ac6454SAndrew Thompson 	if (qh) {
1852a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(qh->page_cache);
185302ac6454SAndrew Thompson 	}
185402ac6454SAndrew Thompson 	if (xfer->flags_int.bandwidth_reclaimed) {
185502ac6454SAndrew Thompson 		xfer->flags_int.bandwidth_reclaimed = 0;
185602ac6454SAndrew Thompson 		uhci_rem_loop(sc);
185702ac6454SAndrew Thompson 	}
185802ac6454SAndrew Thompson 	if (methods == &uhci_device_bulk_methods) {
185902ac6454SAndrew Thompson 		UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
186002ac6454SAndrew Thompson 	}
186102ac6454SAndrew Thompson 	if (methods == &uhci_device_ctrl_methods) {
186202ac6454SAndrew Thompson 		if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
186302ac6454SAndrew Thompson 			UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
186402ac6454SAndrew Thompson 		} else {
186502ac6454SAndrew Thompson 			UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
186602ac6454SAndrew Thompson 		}
186702ac6454SAndrew Thompson 	}
186802ac6454SAndrew Thompson 	if (methods == &uhci_device_intr_methods) {
186902ac6454SAndrew Thompson 		UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
187002ac6454SAndrew Thompson 	}
187102ac6454SAndrew Thompson 	/*
187202ac6454SAndrew Thompson 	 * Only finish isochronous transfers once
187302ac6454SAndrew Thompson 	 * which will update "xfer->frlengths".
187402ac6454SAndrew Thompson 	 */
187502ac6454SAndrew Thompson 	if (xfer->td_transfer_first &&
187602ac6454SAndrew Thompson 	    xfer->td_transfer_last) {
187702ac6454SAndrew Thompson 		if (methods == &uhci_device_isoc_methods) {
187802ac6454SAndrew Thompson 			uhci_isoc_done(sc, xfer);
187902ac6454SAndrew Thompson 		}
188002ac6454SAndrew Thompson 		xfer->td_transfer_first = NULL;
188102ac6454SAndrew Thompson 		xfer->td_transfer_last = NULL;
188202ac6454SAndrew Thompson 	}
188302ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
1884a593f6b8SAndrew Thompson 	usbd_transfer_done(xfer, error);
188502ac6454SAndrew Thompson }
188602ac6454SAndrew Thompson 
188702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
188802ac6454SAndrew Thompson  * uhci bulk support
188902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
189002ac6454SAndrew Thompson static void
1891760bc48eSAndrew Thompson uhci_device_bulk_open(struct usb_xfer *xfer)
189202ac6454SAndrew Thompson {
189302ac6454SAndrew Thompson 	return;
189402ac6454SAndrew Thompson }
189502ac6454SAndrew Thompson 
189602ac6454SAndrew Thompson static void
1897760bc48eSAndrew Thompson uhci_device_bulk_close(struct usb_xfer *xfer)
189802ac6454SAndrew Thompson {
189902ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
190002ac6454SAndrew Thompson }
190102ac6454SAndrew Thompson 
190202ac6454SAndrew Thompson static void
1903760bc48eSAndrew Thompson uhci_device_bulk_enter(struct usb_xfer *xfer)
190402ac6454SAndrew Thompson {
190502ac6454SAndrew Thompson 	return;
190602ac6454SAndrew Thompson }
190702ac6454SAndrew Thompson 
190802ac6454SAndrew Thompson static void
1909760bc48eSAndrew Thompson uhci_device_bulk_start(struct usb_xfer *xfer)
191002ac6454SAndrew Thompson {
191102ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
191202ac6454SAndrew Thompson 	uhci_td_t *td;
191302ac6454SAndrew Thompson 	uhci_qh_t *qh;
191402ac6454SAndrew Thompson 
191502ac6454SAndrew Thompson 	/* setup TD's */
191602ac6454SAndrew Thompson 	td = uhci_setup_standard_chain(xfer);
191702ac6454SAndrew Thompson 
191802ac6454SAndrew Thompson 	/* setup QH */
191902ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
192002ac6454SAndrew Thompson 
192102ac6454SAndrew Thompson 	qh->e_next = td;
192202ac6454SAndrew Thompson 	qh->qh_e_next = td->td_self;
192302ac6454SAndrew Thompson 
1924ec8f3127SAndrew Thompson 	if (xfer->xroot->udev->flags.self_suspended == 0) {
192502ac6454SAndrew Thompson 		UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
192602ac6454SAndrew Thompson 		uhci_add_loop(sc);
192702ac6454SAndrew Thompson 		xfer->flags_int.bandwidth_reclaimed = 1;
192802ac6454SAndrew Thompson 	} else {
1929a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(qh->page_cache);
193002ac6454SAndrew Thompson 	}
193102ac6454SAndrew Thompson 
193202ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
193302ac6454SAndrew Thompson 	uhci_transfer_intr_enqueue(xfer);
193402ac6454SAndrew Thompson }
193502ac6454SAndrew Thompson 
1936760bc48eSAndrew Thompson struct usb_pipe_methods uhci_device_bulk_methods =
193702ac6454SAndrew Thompson {
193802ac6454SAndrew Thompson 	.open = uhci_device_bulk_open,
193902ac6454SAndrew Thompson 	.close = uhci_device_bulk_close,
194002ac6454SAndrew Thompson 	.enter = uhci_device_bulk_enter,
194102ac6454SAndrew Thompson 	.start = uhci_device_bulk_start,
194202ac6454SAndrew Thompson };
194302ac6454SAndrew Thompson 
194402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
194502ac6454SAndrew Thompson  * uhci control support
194602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
194702ac6454SAndrew Thompson static void
1948760bc48eSAndrew Thompson uhci_device_ctrl_open(struct usb_xfer *xfer)
194902ac6454SAndrew Thompson {
195002ac6454SAndrew Thompson 	return;
195102ac6454SAndrew Thompson }
195202ac6454SAndrew Thompson 
195302ac6454SAndrew Thompson static void
1954760bc48eSAndrew Thompson uhci_device_ctrl_close(struct usb_xfer *xfer)
195502ac6454SAndrew Thompson {
195602ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
195702ac6454SAndrew Thompson }
195802ac6454SAndrew Thompson 
195902ac6454SAndrew Thompson static void
1960760bc48eSAndrew Thompson uhci_device_ctrl_enter(struct usb_xfer *xfer)
196102ac6454SAndrew Thompson {
196202ac6454SAndrew Thompson 	return;
196302ac6454SAndrew Thompson }
196402ac6454SAndrew Thompson 
196502ac6454SAndrew Thompson static void
1966760bc48eSAndrew Thompson uhci_device_ctrl_start(struct usb_xfer *xfer)
196702ac6454SAndrew Thompson {
196802ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
196902ac6454SAndrew Thompson 	uhci_qh_t *qh;
197002ac6454SAndrew Thompson 	uhci_td_t *td;
197102ac6454SAndrew Thompson 
197202ac6454SAndrew Thompson 	/* setup TD's */
197302ac6454SAndrew Thompson 	td = uhci_setup_standard_chain(xfer);
197402ac6454SAndrew Thompson 
197502ac6454SAndrew Thompson 	/* setup QH */
197602ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
197702ac6454SAndrew Thompson 
197802ac6454SAndrew Thompson 	qh->e_next = td;
197902ac6454SAndrew Thompson 	qh->qh_e_next = td->td_self;
198002ac6454SAndrew Thompson 
198102ac6454SAndrew Thompson 	/*
198202ac6454SAndrew Thompson 	 * NOTE: some devices choke on bandwidth- reclamation for control
198302ac6454SAndrew Thompson 	 * transfers
198402ac6454SAndrew Thompson 	 */
1985ec8f3127SAndrew Thompson 	if (xfer->xroot->udev->flags.self_suspended == 0) {
198602ac6454SAndrew Thompson 		if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
198702ac6454SAndrew Thompson 			UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
198802ac6454SAndrew Thompson 		} else {
198902ac6454SAndrew Thompson 			UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
199002ac6454SAndrew Thompson 		}
199102ac6454SAndrew Thompson 	} else {
1992a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(qh->page_cache);
199302ac6454SAndrew Thompson 	}
199402ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
199502ac6454SAndrew Thompson 	uhci_transfer_intr_enqueue(xfer);
199602ac6454SAndrew Thompson }
199702ac6454SAndrew Thompson 
1998760bc48eSAndrew Thompson struct usb_pipe_methods uhci_device_ctrl_methods =
199902ac6454SAndrew Thompson {
200002ac6454SAndrew Thompson 	.open = uhci_device_ctrl_open,
200102ac6454SAndrew Thompson 	.close = uhci_device_ctrl_close,
200202ac6454SAndrew Thompson 	.enter = uhci_device_ctrl_enter,
200302ac6454SAndrew Thompson 	.start = uhci_device_ctrl_start,
200402ac6454SAndrew Thompson };
200502ac6454SAndrew Thompson 
200602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
200702ac6454SAndrew Thompson  * uhci interrupt support
200802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
200902ac6454SAndrew Thompson static void
2010760bc48eSAndrew Thompson uhci_device_intr_open(struct usb_xfer *xfer)
201102ac6454SAndrew Thompson {
201202ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
201302ac6454SAndrew Thompson 	uint16_t best;
201402ac6454SAndrew Thompson 	uint16_t bit;
201502ac6454SAndrew Thompson 	uint16_t x;
201602ac6454SAndrew Thompson 
201702ac6454SAndrew Thompson 	best = 0;
201802ac6454SAndrew Thompson 	bit = UHCI_IFRAMELIST_COUNT / 2;
201902ac6454SAndrew Thompson 	while (bit) {
202002ac6454SAndrew Thompson 		if (xfer->interval >= bit) {
202102ac6454SAndrew Thompson 			x = bit;
202202ac6454SAndrew Thompson 			best = bit;
202302ac6454SAndrew Thompson 			while (x & bit) {
202402ac6454SAndrew Thompson 				if (sc->sc_intr_stat[x] <
202502ac6454SAndrew Thompson 				    sc->sc_intr_stat[best]) {
202602ac6454SAndrew Thompson 					best = x;
202702ac6454SAndrew Thompson 				}
202802ac6454SAndrew Thompson 				x++;
202902ac6454SAndrew Thompson 			}
203002ac6454SAndrew Thompson 			break;
203102ac6454SAndrew Thompson 		}
203202ac6454SAndrew Thompson 		bit >>= 1;
203302ac6454SAndrew Thompson 	}
203402ac6454SAndrew Thompson 
203502ac6454SAndrew Thompson 	sc->sc_intr_stat[best]++;
203602ac6454SAndrew Thompson 	xfer->qh_pos = best;
203702ac6454SAndrew Thompson 
203802ac6454SAndrew Thompson 	DPRINTFN(3, "best=%d interval=%d\n",
203902ac6454SAndrew Thompson 	    best, xfer->interval);
204002ac6454SAndrew Thompson }
204102ac6454SAndrew Thompson 
204202ac6454SAndrew Thompson static void
2043760bc48eSAndrew Thompson uhci_device_intr_close(struct usb_xfer *xfer)
204402ac6454SAndrew Thompson {
204502ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
204602ac6454SAndrew Thompson 
204702ac6454SAndrew Thompson 	sc->sc_intr_stat[xfer->qh_pos]--;
204802ac6454SAndrew Thompson 
204902ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
205002ac6454SAndrew Thompson }
205102ac6454SAndrew Thompson 
205202ac6454SAndrew Thompson static void
2053760bc48eSAndrew Thompson uhci_device_intr_enter(struct usb_xfer *xfer)
205402ac6454SAndrew Thompson {
205502ac6454SAndrew Thompson 	return;
205602ac6454SAndrew Thompson }
205702ac6454SAndrew Thompson 
205802ac6454SAndrew Thompson static void
2059760bc48eSAndrew Thompson uhci_device_intr_start(struct usb_xfer *xfer)
206002ac6454SAndrew Thompson {
206102ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
206202ac6454SAndrew Thompson 	uhci_qh_t *qh;
206302ac6454SAndrew Thompson 	uhci_td_t *td;
206402ac6454SAndrew Thompson 
206502ac6454SAndrew Thompson 	/* setup TD's */
206602ac6454SAndrew Thompson 	td = uhci_setup_standard_chain(xfer);
206702ac6454SAndrew Thompson 
206802ac6454SAndrew Thompson 	/* setup QH */
206902ac6454SAndrew Thompson 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
207002ac6454SAndrew Thompson 
207102ac6454SAndrew Thompson 	qh->e_next = td;
207202ac6454SAndrew Thompson 	qh->qh_e_next = td->td_self;
207302ac6454SAndrew Thompson 
2074ec8f3127SAndrew Thompson 	if (xfer->xroot->udev->flags.self_suspended == 0) {
207502ac6454SAndrew Thompson 		/* enter QHs into the controller data structures */
207602ac6454SAndrew Thompson 		UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
207702ac6454SAndrew Thompson 	} else {
2078a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(qh->page_cache);
207902ac6454SAndrew Thompson 	}
208002ac6454SAndrew Thompson 
208102ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
208202ac6454SAndrew Thompson 	uhci_transfer_intr_enqueue(xfer);
208302ac6454SAndrew Thompson }
208402ac6454SAndrew Thompson 
2085760bc48eSAndrew Thompson struct usb_pipe_methods uhci_device_intr_methods =
208602ac6454SAndrew Thompson {
208702ac6454SAndrew Thompson 	.open = uhci_device_intr_open,
208802ac6454SAndrew Thompson 	.close = uhci_device_intr_close,
208902ac6454SAndrew Thompson 	.enter = uhci_device_intr_enter,
209002ac6454SAndrew Thompson 	.start = uhci_device_intr_start,
209102ac6454SAndrew Thompson };
209202ac6454SAndrew Thompson 
209302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
209402ac6454SAndrew Thompson  * uhci isochronous support
209502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
209602ac6454SAndrew Thompson static void
2097760bc48eSAndrew Thompson uhci_device_isoc_open(struct usb_xfer *xfer)
209802ac6454SAndrew Thompson {
209902ac6454SAndrew Thompson 	uhci_td_t *td;
210002ac6454SAndrew Thompson 	uint32_t td_token;
210102ac6454SAndrew Thompson 	uint8_t ds;
210202ac6454SAndrew Thompson 
210302ac6454SAndrew Thompson 	td_token =
2104ae60fdfbSAndrew Thompson 	    (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
2105ae60fdfbSAndrew Thompson 	    UHCI_TD_IN(0, xfer->endpointno, xfer->address, 0) :
2106ae60fdfbSAndrew Thompson 	    UHCI_TD_OUT(0, xfer->endpointno, xfer->address, 0);
210702ac6454SAndrew Thompson 
210802ac6454SAndrew Thompson 	td_token = htole32(td_token);
210902ac6454SAndrew Thompson 
211002ac6454SAndrew Thompson 	/* initialize all TD's */
211102ac6454SAndrew Thompson 
211202ac6454SAndrew Thompson 	for (ds = 0; ds != 2; ds++) {
211302ac6454SAndrew Thompson 
211402ac6454SAndrew Thompson 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
211502ac6454SAndrew Thompson 
211602ac6454SAndrew Thompson 			/* mark TD as inactive */
211702ac6454SAndrew Thompson 			td->td_status = htole32(UHCI_TD_IOS);
211802ac6454SAndrew Thompson 			td->td_token = td_token;
211902ac6454SAndrew Thompson 
2120a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td->page_cache);
212102ac6454SAndrew Thompson 		}
212202ac6454SAndrew Thompson 	}
212302ac6454SAndrew Thompson }
212402ac6454SAndrew Thompson 
212502ac6454SAndrew Thompson static void
2126760bc48eSAndrew Thompson uhci_device_isoc_close(struct usb_xfer *xfer)
212702ac6454SAndrew Thompson {
212802ac6454SAndrew Thompson 	uhci_device_done(xfer, USB_ERR_CANCELLED);
212902ac6454SAndrew Thompson }
213002ac6454SAndrew Thompson 
213102ac6454SAndrew Thompson static void
2132760bc48eSAndrew Thompson uhci_device_isoc_enter(struct usb_xfer *xfer)
213302ac6454SAndrew Thompson {
213402ac6454SAndrew Thompson 	struct uhci_mem_layout ml;
213502ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
213602ac6454SAndrew Thompson 	uint32_t nframes;
213702ac6454SAndrew Thompson 	uint32_t temp;
213802ac6454SAndrew Thompson 	uint32_t *plen;
213902ac6454SAndrew Thompson 
2140b850ecc1SAndrew Thompson #ifdef USB_DEBUG
214102ac6454SAndrew Thompson 	uint8_t once = 1;
214202ac6454SAndrew Thompson 
214302ac6454SAndrew Thompson #endif
214402ac6454SAndrew Thompson 	uhci_td_t *td;
214502ac6454SAndrew Thompson 	uhci_td_t *td_last = NULL;
214602ac6454SAndrew Thompson 	uhci_td_t **pp_last;
214702ac6454SAndrew Thompson 
214802ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2149ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
215002ac6454SAndrew Thompson 
215102ac6454SAndrew Thompson 	nframes = UREAD2(sc, UHCI_FRNUM);
215202ac6454SAndrew Thompson 
2153ae60fdfbSAndrew Thompson 	temp = (nframes - xfer->endpoint->isoc_next) &
215402ac6454SAndrew Thompson 	    (UHCI_VFRAMELIST_COUNT - 1);
215502ac6454SAndrew Thompson 
2156ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
215702ac6454SAndrew Thompson 	    (temp < xfer->nframes)) {
215802ac6454SAndrew Thompson 		/*
215902ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is empty we
216002ac6454SAndrew Thompson 		 * schedule the transfer a few frames ahead of the current
216102ac6454SAndrew Thompson 		 * frame position. Else two isochronous transfers might
216202ac6454SAndrew Thompson 		 * overlap.
216302ac6454SAndrew Thompson 		 */
2164ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
2165ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
2166ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
216702ac6454SAndrew Thompson 	}
216802ac6454SAndrew Thompson 	/*
216902ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
217002ac6454SAndrew Thompson 	 * current frame position:
217102ac6454SAndrew Thompson 	 */
2172ae60fdfbSAndrew Thompson 	temp = (xfer->endpoint->isoc_next - nframes) &
217302ac6454SAndrew Thompson 	    (UHCI_VFRAMELIST_COUNT - 1);
217402ac6454SAndrew Thompson 
217502ac6454SAndrew Thompson 	/*
217602ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
217702ac6454SAndrew Thompson 	 */
217802ac6454SAndrew Thompson 	xfer->isoc_time_complete =
2179a593f6b8SAndrew Thompson 	    usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
218002ac6454SAndrew Thompson 	    xfer->nframes;
218102ac6454SAndrew Thompson 
218202ac6454SAndrew Thompson 	/* get the real number of frames */
218302ac6454SAndrew Thompson 
218402ac6454SAndrew Thompson 	nframes = xfer->nframes;
218502ac6454SAndrew Thompson 
218602ac6454SAndrew Thompson 	uhci_mem_layout_init(&ml, xfer);
218702ac6454SAndrew Thompson 
218802ac6454SAndrew Thompson 	plen = xfer->frlengths;
218902ac6454SAndrew Thompson 
219002ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
219102ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
219202ac6454SAndrew Thompson 
219302ac6454SAndrew Thompson 	/* get next DMA set */
219402ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
219502ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
219602ac6454SAndrew Thompson 
2197ae60fdfbSAndrew Thompson 	pp_last = &sc->sc_isoc_p_last[xfer->endpoint->isoc_next];
219802ac6454SAndrew Thompson 
219902ac6454SAndrew Thompson 	/* store starting position */
220002ac6454SAndrew Thompson 
2201ae60fdfbSAndrew Thompson 	xfer->qh_pos = xfer->endpoint->isoc_next;
220202ac6454SAndrew Thompson 
220302ac6454SAndrew Thompson 	while (nframes--) {
220402ac6454SAndrew Thompson 		if (td == NULL) {
220502ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
220602ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
220702ac6454SAndrew Thompson 		}
220802ac6454SAndrew Thompson 		if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
220902ac6454SAndrew Thompson 			pp_last = &sc->sc_isoc_p_last[0];
221002ac6454SAndrew Thompson 		}
221102ac6454SAndrew Thompson 		if (*plen > xfer->max_frame_size) {
2212b850ecc1SAndrew Thompson #ifdef USB_DEBUG
221302ac6454SAndrew Thompson 			if (once) {
221402ac6454SAndrew Thompson 				once = 0;
221502ac6454SAndrew Thompson 				printf("%s: frame length(%d) exceeds %d "
221602ac6454SAndrew Thompson 				    "bytes (frame truncated)\n",
221702ac6454SAndrew Thompson 				    __FUNCTION__, *plen,
221802ac6454SAndrew Thompson 				    xfer->max_frame_size);
221902ac6454SAndrew Thompson 			}
222002ac6454SAndrew Thompson #endif
222102ac6454SAndrew Thompson 			*plen = xfer->max_frame_size;
222202ac6454SAndrew Thompson 		}
222302ac6454SAndrew Thompson 		/* reuse td_token from last transfer */
222402ac6454SAndrew Thompson 
222502ac6454SAndrew Thompson 		td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
222602ac6454SAndrew Thompson 		td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
222702ac6454SAndrew Thompson 
222802ac6454SAndrew Thompson 		td->len = *plen;
222902ac6454SAndrew Thompson 
223002ac6454SAndrew Thompson 		if (td->len == 0) {
223102ac6454SAndrew Thompson 			/*
223202ac6454SAndrew Thompson 			 * Do not call "uhci_mem_layout_fixup()" when the
223302ac6454SAndrew Thompson 			 * length is zero!
223402ac6454SAndrew Thompson 			 */
223502ac6454SAndrew Thompson 			td->td_buffer = 0;
223602ac6454SAndrew Thompson 			td->fix_pc = NULL;
223702ac6454SAndrew Thompson 
223802ac6454SAndrew Thompson 		} else {
223902ac6454SAndrew Thompson 
224002ac6454SAndrew Thompson 			/* fill out buffer pointer and do fixup, if any */
224102ac6454SAndrew Thompson 
224202ac6454SAndrew Thompson 			uhci_mem_layout_fixup(&ml, td);
224302ac6454SAndrew Thompson 
224402ac6454SAndrew Thompson 		}
224502ac6454SAndrew Thompson 
224602ac6454SAndrew Thompson 		/* update status */
224702ac6454SAndrew Thompson 		if (nframes == 0) {
224802ac6454SAndrew Thompson 			td->td_status = htole32
224902ac6454SAndrew Thompson 			    (UHCI_TD_ZERO_ACTLEN
225002ac6454SAndrew Thompson 			    (UHCI_TD_SET_ERRCNT(0) |
225102ac6454SAndrew Thompson 			    UHCI_TD_ACTIVE |
225202ac6454SAndrew Thompson 			    UHCI_TD_IOS |
225302ac6454SAndrew Thompson 			    UHCI_TD_IOC));
225402ac6454SAndrew Thompson 		} else {
225502ac6454SAndrew Thompson 			td->td_status = htole32
225602ac6454SAndrew Thompson 			    (UHCI_TD_ZERO_ACTLEN
225702ac6454SAndrew Thompson 			    (UHCI_TD_SET_ERRCNT(0) |
225802ac6454SAndrew Thompson 			    UHCI_TD_ACTIVE |
225902ac6454SAndrew Thompson 			    UHCI_TD_IOS));
226002ac6454SAndrew Thompson 		}
226102ac6454SAndrew Thompson 
2262a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
226302ac6454SAndrew Thompson 
2264b850ecc1SAndrew Thompson #ifdef USB_DEBUG
226502ac6454SAndrew Thompson 		if (uhcidebug > 5) {
226602ac6454SAndrew Thompson 			DPRINTF("TD %d\n", nframes);
226702ac6454SAndrew Thompson 			uhci_dump_td(td);
226802ac6454SAndrew Thompson 		}
226902ac6454SAndrew Thompson #endif
227002ac6454SAndrew Thompson 		/* insert TD into schedule */
227102ac6454SAndrew Thompson 		UHCI_APPEND_TD(td, *pp_last);
227202ac6454SAndrew Thompson 		pp_last++;
227302ac6454SAndrew Thompson 
227402ac6454SAndrew Thompson 		plen++;
227502ac6454SAndrew Thompson 		td_last = td;
227602ac6454SAndrew Thompson 		td = td->obj_next;
227702ac6454SAndrew Thompson 	}
227802ac6454SAndrew Thompson 
227902ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
228002ac6454SAndrew Thompson 
228102ac6454SAndrew Thompson 	/* update isoc_next */
2282ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
228302ac6454SAndrew Thompson 	    (UHCI_VFRAMELIST_COUNT - 1);
228402ac6454SAndrew Thompson }
228502ac6454SAndrew Thompson 
228602ac6454SAndrew Thompson static void
2287760bc48eSAndrew Thompson uhci_device_isoc_start(struct usb_xfer *xfer)
228802ac6454SAndrew Thompson {
228902ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
229002ac6454SAndrew Thompson 	uhci_transfer_intr_enqueue(xfer);
229102ac6454SAndrew Thompson }
229202ac6454SAndrew Thompson 
2293760bc48eSAndrew Thompson struct usb_pipe_methods uhci_device_isoc_methods =
229402ac6454SAndrew Thompson {
229502ac6454SAndrew Thompson 	.open = uhci_device_isoc_open,
229602ac6454SAndrew Thompson 	.close = uhci_device_isoc_close,
229702ac6454SAndrew Thompson 	.enter = uhci_device_isoc_enter,
229802ac6454SAndrew Thompson 	.start = uhci_device_isoc_start,
229902ac6454SAndrew Thompson };
230002ac6454SAndrew Thompson 
230102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
230202ac6454SAndrew Thompson  * uhci root control support
230302ac6454SAndrew Thompson  *------------------------------------------------------------------------*
230439307315SAndrew Thompson  * Simulate a hardware hub by handling all the necessary requests.
230502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
230602ac6454SAndrew Thompson 
230702ac6454SAndrew Thompson static const
2308760bc48eSAndrew Thompson struct usb_device_descriptor uhci_devd =
230902ac6454SAndrew Thompson {
2310760bc48eSAndrew Thompson 	sizeof(struct usb_device_descriptor),
231102ac6454SAndrew Thompson 	UDESC_DEVICE,			/* type */
231202ac6454SAndrew Thompson 	{0x00, 0x01},			/* USB version */
231302ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
231402ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
231502ac6454SAndrew Thompson 	UDPROTO_FSHUB,			/* protocol */
231602ac6454SAndrew Thompson 	64,				/* max packet */
231702ac6454SAndrew Thompson 	{0}, {0}, {0x00, 0x01},		/* device id */
231802ac6454SAndrew Thompson 	1, 2, 0,			/* string indicies */
231902ac6454SAndrew Thompson 	1				/* # of configurations */
232002ac6454SAndrew Thompson };
232102ac6454SAndrew Thompson 
232202ac6454SAndrew Thompson static const struct uhci_config_desc uhci_confd = {
232302ac6454SAndrew Thompson 	.confd = {
2324760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_config_descriptor),
232502ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
232602ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(uhci_confd),
232702ac6454SAndrew Thompson 		.bNumInterface = 1,
232802ac6454SAndrew Thompson 		.bConfigurationValue = 1,
232902ac6454SAndrew Thompson 		.iConfiguration = 0,
233002ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
233102ac6454SAndrew Thompson 		.bMaxPower = 0		/* max power */
233202ac6454SAndrew Thompson 	},
233302ac6454SAndrew Thompson 	.ifcd = {
2334760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_interface_descriptor),
233502ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
233602ac6454SAndrew Thompson 		.bNumEndpoints = 1,
233702ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
233802ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
233902ac6454SAndrew Thompson 		.bInterfaceProtocol = UIPROTO_FSHUB,
234002ac6454SAndrew Thompson 	},
234102ac6454SAndrew Thompson 	.endpd = {
2342760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_endpoint_descriptor),
234302ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
234402ac6454SAndrew Thompson 		.bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
234502ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
234602ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
234702ac6454SAndrew Thompson 		.bInterval = 255,
234802ac6454SAndrew Thompson 	},
234902ac6454SAndrew Thompson };
235002ac6454SAndrew Thompson 
235102ac6454SAndrew Thompson static const
2352760bc48eSAndrew Thompson struct usb_hub_descriptor_min uhci_hubd_piix =
235302ac6454SAndrew Thompson {
2354*6d917491SHans Petter Selasky 	.bDescLength = sizeof(uhci_hubd_piix),
2355*6d917491SHans Petter Selasky 	.bDescriptorType = UDESC_HUB,
2356*6d917491SHans Petter Selasky 	.bNbrPorts = 2,
2357*6d917491SHans Petter Selasky 	.wHubCharacteristics = {UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
2358*6d917491SHans Petter Selasky 	.bPwrOn2PwrGood = 50,
235902ac6454SAndrew Thompson };
236002ac6454SAndrew Thompson 
236102ac6454SAndrew Thompson /*
236202ac6454SAndrew Thompson  * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
236302ac6454SAndrew Thompson  * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
236402ac6454SAndrew Thompson  * should not be used by the USB subsystem.  As we cannot issue a
236502ac6454SAndrew Thompson  * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
236602ac6454SAndrew Thompson  * will be enabled as part of the reset.
236702ac6454SAndrew Thompson  *
236802ac6454SAndrew Thompson  * On the VT83C572, the port cannot be successfully enabled until the
236902ac6454SAndrew Thompson  * outstanding "port enable change" and "connection status change"
237002ac6454SAndrew Thompson  * events have been reset.
237102ac6454SAndrew Thompson  */
2372e0a69b51SAndrew Thompson static usb_error_t
23734a35cda7SAndrew Thompson uhci_portreset(uhci_softc_t *sc, uint16_t index)
237402ac6454SAndrew Thompson {
237502ac6454SAndrew Thompson 	uint16_t port;
237602ac6454SAndrew Thompson 	uint16_t x;
237702ac6454SAndrew Thompson 	uint8_t lim;
237802ac6454SAndrew Thompson 
237902ac6454SAndrew Thompson 	if (index == 1)
238002ac6454SAndrew Thompson 		port = UHCI_PORTSC1;
238102ac6454SAndrew Thompson 	else if (index == 2)
238202ac6454SAndrew Thompson 		port = UHCI_PORTSC2;
238302ac6454SAndrew Thompson 	else
238402ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
238502ac6454SAndrew Thompson 
238602ac6454SAndrew Thompson 	/*
238702ac6454SAndrew Thompson 	 * Before we do anything, turn on SOF messages on the USB
238802ac6454SAndrew Thompson 	 * BUS. Some USB devices do not cope without them!
238902ac6454SAndrew Thompson 	 */
2390b9c81a5dSAndrew Thompson 	uhci_restart(sc);
239102ac6454SAndrew Thompson 
239202ac6454SAndrew Thompson 	x = URWMASK(UREAD2(sc, port));
239302ac6454SAndrew Thompson 	UWRITE2(sc, port, x | UHCI_PORTSC_PR);
239402ac6454SAndrew Thompson 
2395a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_bus.bus_mtx,
239602ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
239702ac6454SAndrew Thompson 
239802ac6454SAndrew Thompson 	DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
239902ac6454SAndrew Thompson 	    index, UREAD2(sc, port));
240002ac6454SAndrew Thompson 
240102ac6454SAndrew Thompson 	x = URWMASK(UREAD2(sc, port));
240202ac6454SAndrew Thompson 	UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
240302ac6454SAndrew Thompson 
240402ac6454SAndrew Thompson 
240502ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_bus.bus_mtx);
240602ac6454SAndrew Thompson 
240702ac6454SAndrew Thompson 	/*
240802ac6454SAndrew Thompson 	 * This delay needs to be exactly 100us, else some USB devices
240902ac6454SAndrew Thompson 	 * fail to attach!
241002ac6454SAndrew Thompson 	 */
241102ac6454SAndrew Thompson 	DELAY(100);
241202ac6454SAndrew Thompson 
241302ac6454SAndrew Thompson 	mtx_lock(&sc->sc_bus.bus_mtx);
241402ac6454SAndrew Thompson 
241502ac6454SAndrew Thompson 	DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
241602ac6454SAndrew Thompson 	    index, UREAD2(sc, port));
241702ac6454SAndrew Thompson 
241802ac6454SAndrew Thompson 	x = URWMASK(UREAD2(sc, port));
241902ac6454SAndrew Thompson 	UWRITE2(sc, port, x | UHCI_PORTSC_PE);
242002ac6454SAndrew Thompson 
242102ac6454SAndrew Thompson 	for (lim = 0; lim < 12; lim++) {
242202ac6454SAndrew Thompson 
2423a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx,
242402ac6454SAndrew Thompson 		    USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
242502ac6454SAndrew Thompson 
242602ac6454SAndrew Thompson 		x = UREAD2(sc, port);
242702ac6454SAndrew Thompson 
242802ac6454SAndrew Thompson 		DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
242902ac6454SAndrew Thompson 		    index, lim, x);
243002ac6454SAndrew Thompson 
243102ac6454SAndrew Thompson 		if (!(x & UHCI_PORTSC_CCS)) {
243202ac6454SAndrew Thompson 			/*
243302ac6454SAndrew Thompson 			 * No device is connected (or was disconnected
243402ac6454SAndrew Thompson 			 * during reset).  Consider the port reset.
243502ac6454SAndrew Thompson 			 * The delay must be long enough to ensure on
243602ac6454SAndrew Thompson 			 * the initial iteration that the device
243702ac6454SAndrew Thompson 			 * connection will have been registered.  50ms
243802ac6454SAndrew Thompson 			 * appears to be sufficient, but 20ms is not.
243902ac6454SAndrew Thompson 			 */
244002ac6454SAndrew Thompson 			DPRINTFN(4, "uhci port %d loop %u, device detached\n",
244102ac6454SAndrew Thompson 			    index, lim);
244202ac6454SAndrew Thompson 			goto done;
244302ac6454SAndrew Thompson 		}
244402ac6454SAndrew Thompson 		if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
244502ac6454SAndrew Thompson 			/*
244602ac6454SAndrew Thompson 			 * Port enabled changed and/or connection
244702ac6454SAndrew Thompson 			 * status changed were set.  Reset either or
244802ac6454SAndrew Thompson 			 * both raised flags (by writing a 1 to that
244902ac6454SAndrew Thompson 			 * bit), and wait again for state to settle.
245002ac6454SAndrew Thompson 			 */
245102ac6454SAndrew Thompson 			UWRITE2(sc, port, URWMASK(x) |
245202ac6454SAndrew Thompson 			    (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
245302ac6454SAndrew Thompson 			continue;
245402ac6454SAndrew Thompson 		}
245502ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_PE) {
245602ac6454SAndrew Thompson 			/* port is enabled */
245702ac6454SAndrew Thompson 			goto done;
245802ac6454SAndrew Thompson 		}
245902ac6454SAndrew Thompson 		UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
246002ac6454SAndrew Thompson 	}
246102ac6454SAndrew Thompson 
246202ac6454SAndrew Thompson 	DPRINTFN(2, "uhci port %d reset timed out\n", index);
246302ac6454SAndrew Thompson 	return (USB_ERR_TIMEOUT);
246402ac6454SAndrew Thompson 
246502ac6454SAndrew Thompson done:
246602ac6454SAndrew Thompson 	DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
246702ac6454SAndrew Thompson 	    index, UREAD2(sc, port));
246802ac6454SAndrew Thompson 
246902ac6454SAndrew Thompson 	sc->sc_isreset = 1;
247002ac6454SAndrew Thompson 	return (USB_ERR_NORMAL_COMPLETION);
247102ac6454SAndrew Thompson }
247202ac6454SAndrew Thompson 
2473e0a69b51SAndrew Thompson static usb_error_t
2474760bc48eSAndrew Thompson uhci_roothub_exec(struct usb_device *udev,
2475760bc48eSAndrew Thompson     struct usb_device_request *req, const void **pptr, uint16_t *plength)
247602ac6454SAndrew Thompson {
2477459d369eSAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
2478459d369eSAndrew Thompson 	const void *ptr;
2479459d369eSAndrew Thompson 	const char *str_ptr;
248002ac6454SAndrew Thompson 	uint16_t x;
248102ac6454SAndrew Thompson 	uint16_t port;
248202ac6454SAndrew Thompson 	uint16_t value;
248302ac6454SAndrew Thompson 	uint16_t index;
248402ac6454SAndrew Thompson 	uint16_t status;
248502ac6454SAndrew Thompson 	uint16_t change;
2486459d369eSAndrew Thompson 	uint16_t len;
2487e0a69b51SAndrew Thompson 	usb_error_t err;
248802ac6454SAndrew Thompson 
248902ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
249002ac6454SAndrew Thompson 
249102ac6454SAndrew Thompson 	/* buffer reset */
2492459d369eSAndrew Thompson 	ptr = (const void *)&sc->sc_hub_desc.temp;
2493459d369eSAndrew Thompson 	len = 0;
2494459d369eSAndrew Thompson 	err = 0;
249502ac6454SAndrew Thompson 
2496459d369eSAndrew Thompson 	value = UGETW(req->wValue);
2497459d369eSAndrew Thompson 	index = UGETW(req->wIndex);
249802ac6454SAndrew Thompson 
249902ac6454SAndrew Thompson 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
250002ac6454SAndrew Thompson 	    "wValue=0x%04x wIndex=0x%04x\n",
2501459d369eSAndrew Thompson 	    req->bmRequestType, req->bRequest,
2502459d369eSAndrew Thompson 	    UGETW(req->wLength), value, index);
250302ac6454SAndrew Thompson 
250402ac6454SAndrew Thompson #define	C(x,y) ((x) | ((y) << 8))
2505459d369eSAndrew Thompson 	switch (C(req->bRequest, req->bmRequestType)) {
250602ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
250702ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
250802ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
250902ac6454SAndrew Thompson 		/*
251002ac6454SAndrew Thompson 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
251102ac6454SAndrew Thompson 		 * for the integrated root hub.
251202ac6454SAndrew Thompson 		 */
251302ac6454SAndrew Thompson 		break;
251402ac6454SAndrew Thompson 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
2515459d369eSAndrew Thompson 		len = 1;
251602ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
251702ac6454SAndrew Thompson 		break;
251802ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
251902ac6454SAndrew Thompson 		switch (value >> 8) {
252002ac6454SAndrew Thompson 		case UDESC_DEVICE:
252102ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
2522459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
252302ac6454SAndrew Thompson 				goto done;
252402ac6454SAndrew Thompson 			}
2525459d369eSAndrew Thompson 			len = sizeof(uhci_devd);
2526459d369eSAndrew Thompson 			ptr = (const void *)&uhci_devd;
252702ac6454SAndrew Thompson 			break;
252802ac6454SAndrew Thompson 
252902ac6454SAndrew Thompson 		case UDESC_CONFIG:
253002ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
2531459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
253202ac6454SAndrew Thompson 				goto done;
253302ac6454SAndrew Thompson 			}
2534459d369eSAndrew Thompson 			len = sizeof(uhci_confd);
2535459d369eSAndrew Thompson 			ptr = (const void *)&uhci_confd;
253602ac6454SAndrew Thompson 			break;
253702ac6454SAndrew Thompson 
253802ac6454SAndrew Thompson 		case UDESC_STRING:
253902ac6454SAndrew Thompson 			switch (value & 0xff) {
254002ac6454SAndrew Thompson 			case 0:	/* Language table */
2541459d369eSAndrew Thompson 				str_ptr = "\001";
254202ac6454SAndrew Thompson 				break;
254302ac6454SAndrew Thompson 
254402ac6454SAndrew Thompson 			case 1:	/* Vendor */
2545459d369eSAndrew Thompson 				str_ptr = sc->sc_vendor;
254602ac6454SAndrew Thompson 				break;
254702ac6454SAndrew Thompson 
254802ac6454SAndrew Thompson 			case 2:	/* Product */
2549459d369eSAndrew Thompson 				str_ptr = "UHCI root HUB";
255002ac6454SAndrew Thompson 				break;
255102ac6454SAndrew Thompson 
255202ac6454SAndrew Thompson 			default:
2553459d369eSAndrew Thompson 				str_ptr = "";
255402ac6454SAndrew Thompson 				break;
255502ac6454SAndrew Thompson 			}
255602ac6454SAndrew Thompson 
2557a593f6b8SAndrew Thompson 			len = usb_make_str_desc
255802ac6454SAndrew Thompson 			    (sc->sc_hub_desc.temp,
255902ac6454SAndrew Thompson 			    sizeof(sc->sc_hub_desc.temp),
2560459d369eSAndrew Thompson 			    str_ptr);
256102ac6454SAndrew Thompson 			break;
256202ac6454SAndrew Thompson 
256302ac6454SAndrew Thompson 		default:
2564459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
256502ac6454SAndrew Thompson 			goto done;
256602ac6454SAndrew Thompson 		}
256702ac6454SAndrew Thompson 		break;
256802ac6454SAndrew Thompson 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2569459d369eSAndrew Thompson 		len = 1;
257002ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = 0;
257102ac6454SAndrew Thompson 		break;
257202ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_DEVICE):
2573459d369eSAndrew Thompson 		len = 2;
257402ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
257502ac6454SAndrew Thompson 		break;
257602ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
257702ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2578459d369eSAndrew Thompson 		len = 2;
257902ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
258002ac6454SAndrew Thompson 		break;
258102ac6454SAndrew Thompson 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2582ab42e8b2SAndrew Thompson 		if (value >= UHCI_MAX_DEVICES) {
2583459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
258402ac6454SAndrew Thompson 			goto done;
258502ac6454SAndrew Thompson 		}
258602ac6454SAndrew Thompson 		sc->sc_addr = value;
258702ac6454SAndrew Thompson 		break;
258802ac6454SAndrew Thompson 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
258902ac6454SAndrew Thompson 		if ((value != 0) && (value != 1)) {
2590459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
259102ac6454SAndrew Thompson 			goto done;
259202ac6454SAndrew Thompson 		}
259302ac6454SAndrew Thompson 		sc->sc_conf = value;
259402ac6454SAndrew Thompson 		break;
259502ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
259602ac6454SAndrew Thompson 		break;
259702ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
259802ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
259902ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2600459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
260102ac6454SAndrew Thompson 		goto done;
260202ac6454SAndrew Thompson 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
260302ac6454SAndrew Thompson 		break;
260402ac6454SAndrew Thompson 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
260502ac6454SAndrew Thompson 		break;
260602ac6454SAndrew Thompson 		/* Hub requests */
260702ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
260802ac6454SAndrew Thompson 		break;
260902ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
261002ac6454SAndrew Thompson 		DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
261102ac6454SAndrew Thompson 		    "port=%d feature=%d\n",
261202ac6454SAndrew Thompson 		    index, value);
261302ac6454SAndrew Thompson 		if (index == 1)
261402ac6454SAndrew Thompson 			port = UHCI_PORTSC1;
261502ac6454SAndrew Thompson 		else if (index == 2)
261602ac6454SAndrew Thompson 			port = UHCI_PORTSC2;
261702ac6454SAndrew Thompson 		else {
2618459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
261902ac6454SAndrew Thompson 			goto done;
262002ac6454SAndrew Thompson 		}
262102ac6454SAndrew Thompson 		switch (value) {
262202ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
262302ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
262402ac6454SAndrew Thompson 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
262502ac6454SAndrew Thompson 			break;
262602ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
262702ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
262802ac6454SAndrew Thompson 			UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
262902ac6454SAndrew Thompson 			break;
263002ac6454SAndrew Thompson 		case UHF_PORT_RESET:
263102ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
263202ac6454SAndrew Thompson 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
263302ac6454SAndrew Thompson 			break;
263402ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
263502ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
263602ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
263702ac6454SAndrew Thompson 			break;
263802ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
263902ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
264002ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
264102ac6454SAndrew Thompson 			break;
264202ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
264302ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
264402ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
264502ac6454SAndrew Thompson 			break;
264602ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
264702ac6454SAndrew Thompson 			sc->sc_isreset = 0;
2648459d369eSAndrew Thompson 			err = USB_ERR_NORMAL_COMPLETION;
264902ac6454SAndrew Thompson 			goto done;
265002ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
265102ac6454SAndrew Thompson 			sc->sc_isresumed &= ~(1 << index);
265202ac6454SAndrew Thompson 			break;
265302ac6454SAndrew Thompson 		case UHF_PORT_CONNECTION:
265402ac6454SAndrew Thompson 		case UHF_PORT_OVER_CURRENT:
265502ac6454SAndrew Thompson 		case UHF_PORT_POWER:
265602ac6454SAndrew Thompson 		case UHF_PORT_LOW_SPEED:
265702ac6454SAndrew Thompson 		default:
2658459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
265902ac6454SAndrew Thompson 			goto done;
266002ac6454SAndrew Thompson 		}
266102ac6454SAndrew Thompson 		break;
266202ac6454SAndrew Thompson 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
266302ac6454SAndrew Thompson 		if (index == 1)
266402ac6454SAndrew Thompson 			port = UHCI_PORTSC1;
266502ac6454SAndrew Thompson 		else if (index == 2)
266602ac6454SAndrew Thompson 			port = UHCI_PORTSC2;
266702ac6454SAndrew Thompson 		else {
2668459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
266902ac6454SAndrew Thompson 			goto done;
267002ac6454SAndrew Thompson 		}
2671459d369eSAndrew Thompson 		len = 1;
267202ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] =
267302ac6454SAndrew Thompson 		    ((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
267402ac6454SAndrew Thompson 		    UHCI_PORTSC_LS_SHIFT);
267502ac6454SAndrew Thompson 		break;
267602ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
267702ac6454SAndrew Thompson 		if ((value & 0xff) != 0) {
2678459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
267902ac6454SAndrew Thompson 			goto done;
268002ac6454SAndrew Thompson 		}
2681459d369eSAndrew Thompson 		len = sizeof(uhci_hubd_piix);
2682459d369eSAndrew Thompson 		ptr = (const void *)&uhci_hubd_piix;
268302ac6454SAndrew Thompson 		break;
268402ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2685459d369eSAndrew Thompson 		len = 16;
2686271ae033SHans Petter Selasky 		memset(sc->sc_hub_desc.temp, 0, 16);
268702ac6454SAndrew Thompson 		break;
268802ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
268902ac6454SAndrew Thompson 		if (index == 1)
269002ac6454SAndrew Thompson 			port = UHCI_PORTSC1;
269102ac6454SAndrew Thompson 		else if (index == 2)
269202ac6454SAndrew Thompson 			port = UHCI_PORTSC2;
269302ac6454SAndrew Thompson 		else {
2694459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
269502ac6454SAndrew Thompson 			goto done;
269602ac6454SAndrew Thompson 		}
269702ac6454SAndrew Thompson 		x = UREAD2(sc, port);
269802ac6454SAndrew Thompson 		status = change = 0;
269902ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_CCS)
270002ac6454SAndrew Thompson 			status |= UPS_CURRENT_CONNECT_STATUS;
270102ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_CSC)
270202ac6454SAndrew Thompson 			change |= UPS_C_CONNECT_STATUS;
270302ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_PE)
270402ac6454SAndrew Thompson 			status |= UPS_PORT_ENABLED;
270502ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_POEDC)
270602ac6454SAndrew Thompson 			change |= UPS_C_PORT_ENABLED;
270702ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_OCI)
270802ac6454SAndrew Thompson 			status |= UPS_OVERCURRENT_INDICATOR;
270902ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_OCIC)
271002ac6454SAndrew Thompson 			change |= UPS_C_OVERCURRENT_INDICATOR;
271102ac6454SAndrew Thompson 		if (x & UHCI_PORTSC_LSDA)
271202ac6454SAndrew Thompson 			status |= UPS_LOW_SPEED;
271302ac6454SAndrew Thompson 		if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
271402ac6454SAndrew Thompson 			/* need to do a write back */
271502ac6454SAndrew Thompson 			UWRITE2(sc, port, URWMASK(x));
271602ac6454SAndrew Thompson 
271702ac6454SAndrew Thompson 			/* wait 20ms for resume sequence to complete */
2718a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
271902ac6454SAndrew Thompson 
272002ac6454SAndrew Thompson 			/* clear suspend and resume detect */
272102ac6454SAndrew Thompson 			UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
272202ac6454SAndrew Thompson 			    UHCI_PORTSC_SUSP));
272302ac6454SAndrew Thompson 
272402ac6454SAndrew Thompson 			/* wait a little bit */
2725a593f6b8SAndrew Thompson 			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500);
272602ac6454SAndrew Thompson 
272702ac6454SAndrew Thompson 			sc->sc_isresumed |= (1 << index);
272802ac6454SAndrew Thompson 
272902ac6454SAndrew Thompson 		} else if (x & UHCI_PORTSC_SUSP) {
273002ac6454SAndrew Thompson 			status |= UPS_SUSPEND;
273102ac6454SAndrew Thompson 		}
273202ac6454SAndrew Thompson 		status |= UPS_PORT_POWER;
273302ac6454SAndrew Thompson 		if (sc->sc_isresumed & (1 << index))
273402ac6454SAndrew Thompson 			change |= UPS_C_SUSPEND;
273502ac6454SAndrew Thompson 		if (sc->sc_isreset)
273602ac6454SAndrew Thompson 			change |= UPS_C_PORT_RESET;
273702ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortStatus, status);
273802ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortChange, change);
2739459d369eSAndrew Thompson 		len = sizeof(sc->sc_hub_desc.ps);
274002ac6454SAndrew Thompson 		break;
274102ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2742459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
274302ac6454SAndrew Thompson 		goto done;
274402ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
274502ac6454SAndrew Thompson 		break;
274602ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
274702ac6454SAndrew Thompson 		if (index == 1)
274802ac6454SAndrew Thompson 			port = UHCI_PORTSC1;
274902ac6454SAndrew Thompson 		else if (index == 2)
275002ac6454SAndrew Thompson 			port = UHCI_PORTSC2;
275102ac6454SAndrew Thompson 		else {
2752459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
275302ac6454SAndrew Thompson 			goto done;
275402ac6454SAndrew Thompson 		}
275502ac6454SAndrew Thompson 		switch (value) {
275602ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
275702ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
275802ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_PE);
275902ac6454SAndrew Thompson 			break;
276002ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
276102ac6454SAndrew Thompson 			x = URWMASK(UREAD2(sc, port));
276202ac6454SAndrew Thompson 			UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
276302ac6454SAndrew Thompson 			break;
276402ac6454SAndrew Thompson 		case UHF_PORT_RESET:
2765459d369eSAndrew Thompson 			err = uhci_portreset(sc, index);
276602ac6454SAndrew Thompson 			goto done;
276702ac6454SAndrew Thompson 		case UHF_PORT_POWER:
276802ac6454SAndrew Thompson 			/* pretend we turned on power */
2769459d369eSAndrew Thompson 			err = USB_ERR_NORMAL_COMPLETION;
277002ac6454SAndrew Thompson 			goto done;
277102ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
277202ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
277302ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
277402ac6454SAndrew Thompson 		case UHF_PORT_CONNECTION:
277502ac6454SAndrew Thompson 		case UHF_PORT_OVER_CURRENT:
277602ac6454SAndrew Thompson 		case UHF_PORT_LOW_SPEED:
277702ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
277802ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
277902ac6454SAndrew Thompson 		default:
2780459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
278102ac6454SAndrew Thompson 			goto done;
278202ac6454SAndrew Thompson 		}
278302ac6454SAndrew Thompson 		break;
278402ac6454SAndrew Thompson 	default:
2785459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
278602ac6454SAndrew Thompson 		goto done;
278702ac6454SAndrew Thompson 	}
278802ac6454SAndrew Thompson done:
2789459d369eSAndrew Thompson 	*plength = len;
2790459d369eSAndrew Thompson 	*pptr = ptr;
2791459d369eSAndrew Thompson 	return (err);
279202ac6454SAndrew Thompson }
279302ac6454SAndrew Thompson 
279402ac6454SAndrew Thompson /*
279539307315SAndrew Thompson  * This routine is executed periodically and simulates interrupts from
279639307315SAndrew Thompson  * the root controller interrupt pipe for port status change:
279702ac6454SAndrew Thompson  */
279802ac6454SAndrew Thompson static void
279939307315SAndrew Thompson uhci_root_intr(uhci_softc_t *sc)
280002ac6454SAndrew Thompson {
280102ac6454SAndrew Thompson 	DPRINTFN(21, "\n");
280202ac6454SAndrew Thompson 
280302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
280402ac6454SAndrew Thompson 
280502ac6454SAndrew Thompson 	sc->sc_hub_idata[0] = 0;
280602ac6454SAndrew Thompson 
280702ac6454SAndrew Thompson 	if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
280802ac6454SAndrew Thompson 	    UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
280902ac6454SAndrew Thompson 		sc->sc_hub_idata[0] |= 1 << 1;
281002ac6454SAndrew Thompson 	}
281102ac6454SAndrew Thompson 	if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
281202ac6454SAndrew Thompson 	    UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
281302ac6454SAndrew Thompson 		sc->sc_hub_idata[0] |= 1 << 2;
281402ac6454SAndrew Thompson 	}
281502ac6454SAndrew Thompson 
281639307315SAndrew Thompson 	/* restart timer */
2817a593f6b8SAndrew Thompson 	usb_callout_reset(&sc->sc_root_intr, hz,
281839307315SAndrew Thompson 	    (void *)&uhci_root_intr, sc);
281939307315SAndrew Thompson 
282039307315SAndrew Thompson 	if (sc->sc_hub_idata[0] != 0) {
282139307315SAndrew Thompson 		uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
282239307315SAndrew Thompson 		    sizeof(sc->sc_hub_idata));
282339307315SAndrew Thompson 	}
282439307315SAndrew Thompson }
282502ac6454SAndrew Thompson 
282602ac6454SAndrew Thompson static void
2827760bc48eSAndrew Thompson uhci_xfer_setup(struct usb_setup_params *parm)
282802ac6454SAndrew Thompson {
2829760bc48eSAndrew Thompson 	struct usb_page_search page_info;
2830760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
283102ac6454SAndrew Thompson 	uhci_softc_t *sc;
2832760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
283302ac6454SAndrew Thompson 	void *last_obj;
283402ac6454SAndrew Thompson 	uint32_t ntd;
283502ac6454SAndrew Thompson 	uint32_t nqh;
283602ac6454SAndrew Thompson 	uint32_t nfixup;
283702ac6454SAndrew Thompson 	uint32_t n;
283802ac6454SAndrew Thompson 	uint16_t align;
283902ac6454SAndrew Thompson 
284002ac6454SAndrew Thompson 	sc = UHCI_BUS2SC(parm->udev->bus);
284102ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
284202ac6454SAndrew Thompson 
284302ac6454SAndrew Thompson 	parm->hc_max_packet_size = 0x500;
284402ac6454SAndrew Thompson 	parm->hc_max_packet_count = 1;
284502ac6454SAndrew Thompson 	parm->hc_max_frame_size = 0x500;
284602ac6454SAndrew Thompson 
284702ac6454SAndrew Thompson 	/*
284802ac6454SAndrew Thompson 	 * compute ntd and nqh
284902ac6454SAndrew Thompson 	 */
285002ac6454SAndrew Thompson 	if (parm->methods == &uhci_device_ctrl_methods) {
285102ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
285202ac6454SAndrew Thompson 		xfer->flags_int.bdma_no_post_sync = 1;
285302ac6454SAndrew Thompson 
2854a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
285502ac6454SAndrew Thompson 
285602ac6454SAndrew Thompson 		/* see EHCI HC driver for proof of "ntd" formula */
285702ac6454SAndrew Thompson 
285802ac6454SAndrew Thompson 		nqh = 1;
285902ac6454SAndrew Thompson 		ntd = ((2 * xfer->nframes) + 1	/* STATUS */
286002ac6454SAndrew Thompson 		    + (xfer->max_data_length / xfer->max_frame_size));
286102ac6454SAndrew Thompson 
286202ac6454SAndrew Thompson 	} else if (parm->methods == &uhci_device_bulk_methods) {
286302ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
286402ac6454SAndrew Thompson 		xfer->flags_int.bdma_no_post_sync = 1;
286502ac6454SAndrew Thompson 
2866a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
286702ac6454SAndrew Thompson 
286802ac6454SAndrew Thompson 		nqh = 1;
286902ac6454SAndrew Thompson 		ntd = ((2 * xfer->nframes)
287002ac6454SAndrew Thompson 		    + (xfer->max_data_length / xfer->max_frame_size));
287102ac6454SAndrew Thompson 
287202ac6454SAndrew Thompson 	} else if (parm->methods == &uhci_device_intr_methods) {
287302ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
287402ac6454SAndrew Thompson 		xfer->flags_int.bdma_no_post_sync = 1;
287502ac6454SAndrew Thompson 
2876a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
287702ac6454SAndrew Thompson 
287802ac6454SAndrew Thompson 		nqh = 1;
287902ac6454SAndrew Thompson 		ntd = ((2 * xfer->nframes)
288002ac6454SAndrew Thompson 		    + (xfer->max_data_length / xfer->max_frame_size));
288102ac6454SAndrew Thompson 
288202ac6454SAndrew Thompson 	} else if (parm->methods == &uhci_device_isoc_methods) {
288302ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
288402ac6454SAndrew Thompson 		xfer->flags_int.bdma_no_post_sync = 1;
288502ac6454SAndrew Thompson 
2886a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
288702ac6454SAndrew Thompson 
288802ac6454SAndrew Thompson 		nqh = 0;
288902ac6454SAndrew Thompson 		ntd = xfer->nframes;
289002ac6454SAndrew Thompson 
289102ac6454SAndrew Thompson 	} else {
289202ac6454SAndrew Thompson 
2893a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
289402ac6454SAndrew Thompson 
289502ac6454SAndrew Thompson 		nqh = 0;
289602ac6454SAndrew Thompson 		ntd = 0;
289702ac6454SAndrew Thompson 	}
289802ac6454SAndrew Thompson 
289902ac6454SAndrew Thompson 	if (parm->err) {
290002ac6454SAndrew Thompson 		return;
290102ac6454SAndrew Thompson 	}
290202ac6454SAndrew Thompson 	/*
290302ac6454SAndrew Thompson 	 * NOTE: the UHCI controller requires that
290402ac6454SAndrew Thompson 	 * every packet must be contiguous on
290502ac6454SAndrew Thompson 	 * the same USB memory page !
290602ac6454SAndrew Thompson 	 */
290702ac6454SAndrew Thompson 	nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
290802ac6454SAndrew Thompson 
290902ac6454SAndrew Thompson 	/*
291002ac6454SAndrew Thompson 	 * Compute a suitable power of two alignment
291102ac6454SAndrew Thompson 	 * for our "max_frame_size" fixup buffer(s):
291202ac6454SAndrew Thompson 	 */
291302ac6454SAndrew Thompson 	align = xfer->max_frame_size;
291402ac6454SAndrew Thompson 	n = 0;
291502ac6454SAndrew Thompson 	while (align) {
291602ac6454SAndrew Thompson 		align >>= 1;
291702ac6454SAndrew Thompson 		n++;
291802ac6454SAndrew Thompson 	}
291902ac6454SAndrew Thompson 
292002ac6454SAndrew Thompson 	/* check for power of two */
292102ac6454SAndrew Thompson 	if (!(xfer->max_frame_size &
292202ac6454SAndrew Thompson 	    (xfer->max_frame_size - 1))) {
292302ac6454SAndrew Thompson 		n--;
292402ac6454SAndrew Thompson 	}
292502ac6454SAndrew Thompson 	/*
292602ac6454SAndrew Thompson 	 * We don't allow alignments of
292702ac6454SAndrew Thompson 	 * less than 8 bytes:
292802ac6454SAndrew Thompson 	 *
292902ac6454SAndrew Thompson 	 * NOTE: Allocating using an aligment
293002ac6454SAndrew Thompson 	 * of 1 byte has special meaning!
293102ac6454SAndrew Thompson 	 */
293202ac6454SAndrew Thompson 	if (n < 3) {
293302ac6454SAndrew Thompson 		n = 3;
293402ac6454SAndrew Thompson 	}
293502ac6454SAndrew Thompson 	align = (1 << n);
293602ac6454SAndrew Thompson 
2937a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
293802ac6454SAndrew Thompson 	    parm, &pc, xfer->max_frame_size,
293902ac6454SAndrew Thompson 	    align, nfixup)) {
294002ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
294102ac6454SAndrew Thompson 		return;
294202ac6454SAndrew Thompson 	}
294302ac6454SAndrew Thompson 	xfer->buf_fixup = pc;
294402ac6454SAndrew Thompson 
294502ac6454SAndrew Thompson alloc_dma_set:
294602ac6454SAndrew Thompson 
294702ac6454SAndrew Thompson 	if (parm->err) {
294802ac6454SAndrew Thompson 		return;
294902ac6454SAndrew Thompson 	}
295002ac6454SAndrew Thompson 	last_obj = NULL;
295102ac6454SAndrew Thompson 
2952a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
295302ac6454SAndrew Thompson 	    parm, &pc, sizeof(uhci_td_t),
295402ac6454SAndrew Thompson 	    UHCI_TD_ALIGN, ntd)) {
295502ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
295602ac6454SAndrew Thompson 		return;
295702ac6454SAndrew Thompson 	}
295802ac6454SAndrew Thompson 	if (parm->buf) {
295902ac6454SAndrew Thompson 		for (n = 0; n != ntd; n++) {
296002ac6454SAndrew Thompson 			uhci_td_t *td;
296102ac6454SAndrew Thompson 
2962a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
296302ac6454SAndrew Thompson 
296402ac6454SAndrew Thompson 			td = page_info.buffer;
296502ac6454SAndrew Thompson 
296602ac6454SAndrew Thompson 			/* init TD */
296702ac6454SAndrew Thompson 			if ((parm->methods == &uhci_device_bulk_methods) ||
296802ac6454SAndrew Thompson 			    (parm->methods == &uhci_device_ctrl_methods) ||
296902ac6454SAndrew Thompson 			    (parm->methods == &uhci_device_intr_methods)) {
297002ac6454SAndrew Thompson 				/* set depth first bit */
297102ac6454SAndrew Thompson 				td->td_self = htole32(page_info.physaddr |
297202ac6454SAndrew Thompson 				    UHCI_PTR_TD | UHCI_PTR_VF);
297302ac6454SAndrew Thompson 			} else {
297402ac6454SAndrew Thompson 				td->td_self = htole32(page_info.physaddr |
297502ac6454SAndrew Thompson 				    UHCI_PTR_TD);
297602ac6454SAndrew Thompson 			}
297702ac6454SAndrew Thompson 
297802ac6454SAndrew Thompson 			td->obj_next = last_obj;
297902ac6454SAndrew Thompson 			td->page_cache = pc + n;
298002ac6454SAndrew Thompson 
298102ac6454SAndrew Thompson 			last_obj = td;
298202ac6454SAndrew Thompson 
2983a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
298402ac6454SAndrew Thompson 		}
298502ac6454SAndrew Thompson 	}
298602ac6454SAndrew Thompson 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
298702ac6454SAndrew Thompson 
298802ac6454SAndrew Thompson 	last_obj = NULL;
298902ac6454SAndrew Thompson 
2990a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
299102ac6454SAndrew Thompson 	    parm, &pc, sizeof(uhci_qh_t),
299202ac6454SAndrew Thompson 	    UHCI_QH_ALIGN, nqh)) {
299302ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
299402ac6454SAndrew Thompson 		return;
299502ac6454SAndrew Thompson 	}
299602ac6454SAndrew Thompson 	if (parm->buf) {
299702ac6454SAndrew Thompson 		for (n = 0; n != nqh; n++) {
299802ac6454SAndrew Thompson 			uhci_qh_t *qh;
299902ac6454SAndrew Thompson 
3000a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
300102ac6454SAndrew Thompson 
300202ac6454SAndrew Thompson 			qh = page_info.buffer;
300302ac6454SAndrew Thompson 
300402ac6454SAndrew Thompson 			/* init QH */
300502ac6454SAndrew Thompson 			qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
300602ac6454SAndrew Thompson 			qh->obj_next = last_obj;
300702ac6454SAndrew Thompson 			qh->page_cache = pc + n;
300802ac6454SAndrew Thompson 
300902ac6454SAndrew Thompson 			last_obj = qh;
301002ac6454SAndrew Thompson 
3011a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
301202ac6454SAndrew Thompson 		}
301302ac6454SAndrew Thompson 	}
301402ac6454SAndrew Thompson 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
301502ac6454SAndrew Thompson 
301602ac6454SAndrew Thompson 	if (!xfer->flags_int.curr_dma_set) {
301702ac6454SAndrew Thompson 		xfer->flags_int.curr_dma_set = 1;
301802ac6454SAndrew Thompson 		goto alloc_dma_set;
301902ac6454SAndrew Thompson 	}
302002ac6454SAndrew Thompson }
302102ac6454SAndrew Thompson 
302202ac6454SAndrew Thompson static void
3023ae60fdfbSAndrew Thompson uhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3024ae60fdfbSAndrew Thompson     struct usb_endpoint *ep)
302502ac6454SAndrew Thompson {
302602ac6454SAndrew Thompson 	uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
302702ac6454SAndrew Thompson 
3028ae60fdfbSAndrew Thompson 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3029ae60fdfbSAndrew Thompson 	    ep, udev->address,
3030f29a0724SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb_mode,
303102ac6454SAndrew Thompson 	    sc->sc_addr);
303202ac6454SAndrew Thompson 
3033f29a0724SAndrew Thompson 	if (udev->flags.usb_mode != USB_MODE_HOST) {
303402ac6454SAndrew Thompson 		/* not supported */
303502ac6454SAndrew Thompson 		return;
303602ac6454SAndrew Thompson 	}
303739307315SAndrew Thompson 	if (udev->device_index != sc->sc_addr) {
303802ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
303902ac6454SAndrew Thompson 		case UE_CONTROL:
3040ae60fdfbSAndrew Thompson 			ep->methods = &uhci_device_ctrl_methods;
304102ac6454SAndrew Thompson 			break;
304202ac6454SAndrew Thompson 		case UE_INTERRUPT:
3043ae60fdfbSAndrew Thompson 			ep->methods = &uhci_device_intr_methods;
304402ac6454SAndrew Thompson 			break;
304502ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
304602ac6454SAndrew Thompson 			if (udev->speed == USB_SPEED_FULL) {
3047ae60fdfbSAndrew Thompson 				ep->methods = &uhci_device_isoc_methods;
304802ac6454SAndrew Thompson 			}
304902ac6454SAndrew Thompson 			break;
305002ac6454SAndrew Thompson 		case UE_BULK:
3051ae60fdfbSAndrew Thompson 			ep->methods = &uhci_device_bulk_methods;
305202ac6454SAndrew Thompson 			break;
305302ac6454SAndrew Thompson 		default:
305402ac6454SAndrew Thompson 			/* do nothing */
305502ac6454SAndrew Thompson 			break;
305602ac6454SAndrew Thompson 		}
305702ac6454SAndrew Thompson 	}
305802ac6454SAndrew Thompson }
305902ac6454SAndrew Thompson 
306002ac6454SAndrew Thompson static void
3061760bc48eSAndrew Thompson uhci_xfer_unsetup(struct usb_xfer *xfer)
306202ac6454SAndrew Thompson {
306302ac6454SAndrew Thompson 	return;
306402ac6454SAndrew Thompson }
306502ac6454SAndrew Thompson 
306602ac6454SAndrew Thompson static void
3067527aa7b2SAndrew Thompson uhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
306802ac6454SAndrew Thompson {
306902ac6454SAndrew Thompson 	/*
307002ac6454SAndrew Thompson 	 * Wait until hardware has finished any possible use of the
307102ac6454SAndrew Thompson 	 * transfer descriptor(s) and QH
307202ac6454SAndrew Thompson 	 */
307302ac6454SAndrew Thompson 	*pus = (1125);			/* microseconds */
307402ac6454SAndrew Thompson }
307502ac6454SAndrew Thompson 
307602ac6454SAndrew Thompson static void
3077760bc48eSAndrew Thompson uhci_device_resume(struct usb_device *udev)
307802ac6454SAndrew Thompson {
307902ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3080760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3081760bc48eSAndrew Thompson 	struct usb_pipe_methods *methods;
308202ac6454SAndrew Thompson 	uhci_qh_t *qh;
308302ac6454SAndrew Thompson 
308402ac6454SAndrew Thompson 	DPRINTF("\n");
308502ac6454SAndrew Thompson 
308602ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
308702ac6454SAndrew Thompson 
308802ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
308902ac6454SAndrew Thompson 
309002ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
309102ac6454SAndrew Thompson 
3092ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
309302ac6454SAndrew Thompson 			qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
309402ac6454SAndrew Thompson 
309502ac6454SAndrew Thompson 			if (methods == &uhci_device_bulk_methods) {
309602ac6454SAndrew Thompson 				UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
309702ac6454SAndrew Thompson 				uhci_add_loop(sc);
309802ac6454SAndrew Thompson 				xfer->flags_int.bandwidth_reclaimed = 1;
309902ac6454SAndrew Thompson 			}
310002ac6454SAndrew Thompson 			if (methods == &uhci_device_ctrl_methods) {
310102ac6454SAndrew Thompson 				if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
310202ac6454SAndrew Thompson 					UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
310302ac6454SAndrew Thompson 				} else {
310402ac6454SAndrew Thompson 					UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
310502ac6454SAndrew Thompson 				}
310602ac6454SAndrew Thompson 			}
310702ac6454SAndrew Thompson 			if (methods == &uhci_device_intr_methods) {
310802ac6454SAndrew Thompson 				UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
310902ac6454SAndrew Thompson 			}
311002ac6454SAndrew Thompson 		}
311102ac6454SAndrew Thompson 	}
311202ac6454SAndrew Thompson 
311302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
311402ac6454SAndrew Thompson 
311502ac6454SAndrew Thompson 	return;
311602ac6454SAndrew Thompson }
311702ac6454SAndrew Thompson 
311802ac6454SAndrew Thompson static void
3119760bc48eSAndrew Thompson uhci_device_suspend(struct usb_device *udev)
312002ac6454SAndrew Thompson {
312102ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3122760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
3123760bc48eSAndrew Thompson 	struct usb_pipe_methods *methods;
312402ac6454SAndrew Thompson 	uhci_qh_t *qh;
312502ac6454SAndrew Thompson 
312602ac6454SAndrew Thompson 	DPRINTF("\n");
312702ac6454SAndrew Thompson 
312802ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
312902ac6454SAndrew Thompson 
313002ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
313102ac6454SAndrew Thompson 
313202ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
313302ac6454SAndrew Thompson 
3134ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
313502ac6454SAndrew Thompson 			qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
313602ac6454SAndrew Thompson 
313702ac6454SAndrew Thompson 			if (xfer->flags_int.bandwidth_reclaimed) {
313802ac6454SAndrew Thompson 				xfer->flags_int.bandwidth_reclaimed = 0;
313902ac6454SAndrew Thompson 				uhci_rem_loop(sc);
314002ac6454SAndrew Thompson 			}
314102ac6454SAndrew Thompson 			if (methods == &uhci_device_bulk_methods) {
314202ac6454SAndrew Thompson 				UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
314302ac6454SAndrew Thompson 			}
314402ac6454SAndrew Thompson 			if (methods == &uhci_device_ctrl_methods) {
314502ac6454SAndrew Thompson 				if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
314602ac6454SAndrew Thompson 					UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
314702ac6454SAndrew Thompson 				} else {
314802ac6454SAndrew Thompson 					UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
314902ac6454SAndrew Thompson 				}
315002ac6454SAndrew Thompson 			}
315102ac6454SAndrew Thompson 			if (methods == &uhci_device_intr_methods) {
315202ac6454SAndrew Thompson 				UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
315302ac6454SAndrew Thompson 			}
315402ac6454SAndrew Thompson 		}
315502ac6454SAndrew Thompson 	}
315602ac6454SAndrew Thompson 
315702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
315802ac6454SAndrew Thompson 
315902ac6454SAndrew Thompson 	return;
316002ac6454SAndrew Thompson }
316102ac6454SAndrew Thompson 
316202ac6454SAndrew Thompson static void
31632e141748SHans Petter Selasky uhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
31642e141748SHans Petter Selasky {
31652e141748SHans Petter Selasky 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
31662e141748SHans Petter Selasky 
31672e141748SHans Petter Selasky 	switch (state) {
31682e141748SHans Petter Selasky 	case USB_HW_POWER_SUSPEND:
31692e141748SHans Petter Selasky 	case USB_HW_POWER_SHUTDOWN:
31702e141748SHans Petter Selasky 		uhci_suspend(sc);
31712e141748SHans Petter Selasky 		break;
31722e141748SHans Petter Selasky 	case USB_HW_POWER_RESUME:
31732e141748SHans Petter Selasky 		uhci_resume(sc);
31742e141748SHans Petter Selasky 		break;
31752e141748SHans Petter Selasky 	default:
31762e141748SHans Petter Selasky 		break;
31772e141748SHans Petter Selasky 	}
31782e141748SHans Petter Selasky }
31792e141748SHans Petter Selasky 
31802e141748SHans Petter Selasky static void
3181760bc48eSAndrew Thompson uhci_set_hw_power(struct usb_bus *bus)
318202ac6454SAndrew Thompson {
318302ac6454SAndrew Thompson 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
318402ac6454SAndrew Thompson 	uint32_t flags;
318502ac6454SAndrew Thompson 
318602ac6454SAndrew Thompson 	DPRINTF("\n");
318702ac6454SAndrew Thompson 
318802ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
318902ac6454SAndrew Thompson 
319002ac6454SAndrew Thompson 	flags = bus->hw_power_state;
319102ac6454SAndrew Thompson 
319202ac6454SAndrew Thompson 	/*
319302ac6454SAndrew Thompson 	 * WARNING: Some FULL speed USB devices require periodic SOF
319402ac6454SAndrew Thompson 	 * messages! If any USB devices are connected through the
319502ac6454SAndrew Thompson 	 * UHCI, power save will be disabled!
319602ac6454SAndrew Thompson 	 */
319702ac6454SAndrew Thompson 	if (flags & (USB_HW_POWER_CONTROL |
319802ac6454SAndrew Thompson 	    USB_HW_POWER_NON_ROOT_HUB |
319902ac6454SAndrew Thompson 	    USB_HW_POWER_BULK |
320002ac6454SAndrew Thompson 	    USB_HW_POWER_INTERRUPT |
320102ac6454SAndrew Thompson 	    USB_HW_POWER_ISOC)) {
320202ac6454SAndrew Thompson 		DPRINTF("Some USB transfer is "
3203b9c81a5dSAndrew Thompson 		    "active on unit %u.\n",
320402ac6454SAndrew Thompson 		    device_get_unit(sc->sc_bus.bdev));
3205b9c81a5dSAndrew Thompson 		uhci_restart(sc);
320602ac6454SAndrew Thompson 	} else {
3207b9c81a5dSAndrew Thompson 		DPRINTF("Power save on unit %u.\n",
320802ac6454SAndrew Thompson 		    device_get_unit(sc->sc_bus.bdev));
320902ac6454SAndrew Thompson 		UHCICMD(sc, UHCI_CMD_MAXP);
321002ac6454SAndrew Thompson 	}
321102ac6454SAndrew Thompson 
321202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
321302ac6454SAndrew Thompson 
321402ac6454SAndrew Thompson 	return;
321502ac6454SAndrew Thompson }
321602ac6454SAndrew Thompson 
321702ac6454SAndrew Thompson 
3218760bc48eSAndrew Thompson struct usb_bus_methods uhci_bus_methods =
321902ac6454SAndrew Thompson {
3220ae60fdfbSAndrew Thompson 	.endpoint_init = uhci_ep_init,
322102ac6454SAndrew Thompson 	.xfer_setup = uhci_xfer_setup,
322202ac6454SAndrew Thompson 	.xfer_unsetup = uhci_xfer_unsetup,
322302ac6454SAndrew Thompson 	.get_dma_delay = uhci_get_dma_delay,
322402ac6454SAndrew Thompson 	.device_resume = uhci_device_resume,
322502ac6454SAndrew Thompson 	.device_suspend = uhci_device_suspend,
322602ac6454SAndrew Thompson 	.set_hw_power = uhci_set_hw_power,
32272e141748SHans Petter Selasky 	.set_hw_power_sleep = uhci_set_hw_power_sleep,
322839307315SAndrew Thompson 	.roothub_exec = uhci_roothub_exec,
3229dddb25f9SAlfred Perlstein 	.xfer_poll = uhci_do_poll,
323002ac6454SAndrew Thompson };
3231