xref: /freebsd/sys/dev/usb/controller/ohci.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
502ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
602ac6454SAndrew Thompson  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
902ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1002ac6454SAndrew Thompson  * are met:
1102ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1302ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1502ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1602ac6454SAndrew Thompson  *
1702ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1802ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1902ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2002ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2102ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2202ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2302ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2402ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2502ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2602ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2702ac6454SAndrew Thompson  * SUCH DAMAGE.
2802ac6454SAndrew Thompson  */
2902ac6454SAndrew Thompson 
3002ac6454SAndrew Thompson /*
3102ac6454SAndrew Thompson  * USB Open Host Controller driver.
3202ac6454SAndrew Thompson  *
3302ac6454SAndrew Thompson  * OHCI spec: http://www.compaq.com/productinfo/development/openhci.html
3402ac6454SAndrew Thompson  * USB spec:  http://www.usb.org/developers/docs/usbspec.zip
3502ac6454SAndrew Thompson  */
3602ac6454SAndrew Thompson 
37d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
38d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
39d2b99310SHans Petter Selasky #else
40ed6d949aSAndrew Thompson #include <sys/stdint.h>
41ed6d949aSAndrew Thompson #include <sys/stddef.h>
42ed6d949aSAndrew Thompson #include <sys/param.h>
43ed6d949aSAndrew Thompson #include <sys/queue.h>
44ed6d949aSAndrew Thompson #include <sys/types.h>
45ed6d949aSAndrew Thompson #include <sys/systm.h>
46ed6d949aSAndrew Thompson #include <sys/kernel.h>
47ed6d949aSAndrew Thompson #include <sys/bus.h>
48ed6d949aSAndrew Thompson #include <sys/module.h>
49ed6d949aSAndrew Thompson #include <sys/lock.h>
50ed6d949aSAndrew Thompson #include <sys/mutex.h>
51ed6d949aSAndrew Thompson #include <sys/condvar.h>
52ed6d949aSAndrew Thompson #include <sys/sysctl.h>
53ed6d949aSAndrew Thompson #include <sys/sx.h>
54ed6d949aSAndrew Thompson #include <sys/unistd.h>
55ed6d949aSAndrew Thompson #include <sys/callout.h>
56ed6d949aSAndrew Thompson #include <sys/malloc.h>
57ed6d949aSAndrew Thompson #include <sys/priv.h>
58ed6d949aSAndrew Thompson 
5902ac6454SAndrew Thompson #include <dev/usb/usb.h>
60ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
6102ac6454SAndrew Thompson 
6202ac6454SAndrew Thompson #define	USB_DEBUG_VAR ohcidebug
6302ac6454SAndrew Thompson 
6402ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
6502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6802ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
6902ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
7002ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
7102ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
75d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
76d2b99310SHans Petter Selasky 
7702ac6454SAndrew Thompson #include <dev/usb/controller/ohci.h>
781def609aSAndrew Thompson #include <dev/usb/controller/ohcireg.h>
7902ac6454SAndrew Thompson 
80578d0effSAndrew Thompson #define	OHCI_BUS2SC(bus) \
8152ab576dSJohn Baldwin 	__containerof(bus, ohci_softc_t, sc_bus)
8202ac6454SAndrew Thompson 
83ed6d949aSAndrew Thompson #ifdef USB_DEBUG
8402ac6454SAndrew Thompson static int ohcidebug = 0;
8502ac6454SAndrew Thompson 
86f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ohci, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
87f8d2b1f3SPawel Biernacki     "USB ohci");
88af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_ohci, OID_AUTO, debug, CTLFLAG_RWTUN,
8902ac6454SAndrew Thompson     &ohcidebug, 0, "ohci debug level");
90c13fd8d4SAndrew Thompson 
9102ac6454SAndrew Thompson static void ohci_dumpregs(ohci_softc_t *);
9202ac6454SAndrew Thompson static void ohci_dump_tds(ohci_td_t *);
9302ac6454SAndrew Thompson static uint8_t ohci_dump_td(ohci_td_t *);
9402ac6454SAndrew Thompson static void ohci_dump_ed(ohci_ed_t *);
9502ac6454SAndrew Thompson static uint8_t ohci_dump_itd(ohci_itd_t *);
9602ac6454SAndrew Thompson static void ohci_dump_itds(ohci_itd_t *);
9702ac6454SAndrew Thompson 
9802ac6454SAndrew Thompson #endif
9902ac6454SAndrew Thompson 
10002ac6454SAndrew Thompson #define	OBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
10102ac6454SAndrew Thompson 			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
10202ac6454SAndrew Thompson #define	OWRITE1(sc, r, x) \
10302ac6454SAndrew Thompson  do { OBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); } while (0)
10402ac6454SAndrew Thompson #define	OWRITE2(sc, r, x) \
10502ac6454SAndrew Thompson  do { OBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); } while (0)
10602ac6454SAndrew Thompson #define	OWRITE4(sc, r, x) \
10702ac6454SAndrew Thompson  do { OBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); } while (0)
10802ac6454SAndrew Thompson #define	OREAD1(sc, r) (OBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
10902ac6454SAndrew Thompson #define	OREAD2(sc, r) (OBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
11002ac6454SAndrew Thompson #define	OREAD4(sc, r) (OBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
11102ac6454SAndrew Thompson 
11202ac6454SAndrew Thompson #define	OHCI_INTR_ENDPT 1
11302ac6454SAndrew Thompson 
114e892b3feSHans Petter Selasky static const struct usb_bus_methods ohci_bus_methods;
115e892b3feSHans Petter Selasky static const struct usb_pipe_methods ohci_device_bulk_methods;
116e892b3feSHans Petter Selasky static const struct usb_pipe_methods ohci_device_ctrl_methods;
117e892b3feSHans Petter Selasky static const struct usb_pipe_methods ohci_device_intr_methods;
118e892b3feSHans Petter Selasky static const struct usb_pipe_methods ohci_device_isoc_methods;
11902ac6454SAndrew Thompson 
120760bc48eSAndrew Thompson static void ohci_do_poll(struct usb_bus *bus);
121e0a69b51SAndrew Thompson static void ohci_device_done(struct usb_xfer *xfer, usb_error_t error);
12202ac6454SAndrew Thompson static void ohci_timeout(void *arg);
123760bc48eSAndrew Thompson static uint8_t ohci_check_transfer(struct usb_xfer *xfer);
12439307315SAndrew Thompson static void ohci_root_intr(ohci_softc_t *sc);
12502ac6454SAndrew Thompson 
12602ac6454SAndrew Thompson struct ohci_std_temp {
127760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
12802ac6454SAndrew Thompson 	ohci_td_t *td;
12902ac6454SAndrew Thompson 	ohci_td_t *td_next;
13002ac6454SAndrew Thompson 	uint32_t average;
13102ac6454SAndrew Thompson 	uint32_t td_flags;
13202ac6454SAndrew Thompson 	uint32_t len;
13302ac6454SAndrew Thompson 	uint16_t max_frame_size;
13402ac6454SAndrew Thompson 	uint8_t	shortpkt;
13502ac6454SAndrew Thompson 	uint8_t	setup_alt_next;
1360f7d4548SAndrew Thompson 	uint8_t last_frame;
13702ac6454SAndrew Thompson };
13802ac6454SAndrew Thompson 
13902ac6454SAndrew Thompson static struct ohci_hcca *
ohci_get_hcca(ohci_softc_t * sc)14002ac6454SAndrew Thompson ohci_get_hcca(ohci_softc_t *sc)
14102ac6454SAndrew Thompson {
142a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(&sc->sc_hw.hcca_pc);
14302ac6454SAndrew Thompson 	return (sc->sc_hcca_p);
14402ac6454SAndrew Thompson }
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson void
ohci_iterate_hw_softc(struct usb_bus * bus,usb_bus_mem_sub_cb_t * cb)147e0a69b51SAndrew Thompson ohci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
14802ac6454SAndrew Thompson {
14902ac6454SAndrew Thompson 	struct ohci_softc *sc = OHCI_BUS2SC(bus);
15002ac6454SAndrew Thompson 	uint32_t i;
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.hcca_pc, &sc->sc_hw.hcca_pg,
15302ac6454SAndrew Thompson 	    sizeof(ohci_hcca_t), OHCI_HCCA_ALIGN);
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.ctrl_start_pc, &sc->sc_hw.ctrl_start_pg,
15602ac6454SAndrew Thompson 	    sizeof(ohci_ed_t), OHCI_ED_ALIGN);
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
15902ac6454SAndrew Thompson 	    sizeof(ohci_ed_t), OHCI_ED_ALIGN);
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson 	cb(bus, &sc->sc_hw.isoc_start_pc, &sc->sc_hw.isoc_start_pg,
16202ac6454SAndrew Thompson 	    sizeof(ohci_ed_t), OHCI_ED_ALIGN);
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	for (i = 0; i != OHCI_NO_EDS; i++) {
16502ac6454SAndrew Thompson 		cb(bus, sc->sc_hw.intr_start_pc + i, sc->sc_hw.intr_start_pg + i,
16602ac6454SAndrew Thompson 		    sizeof(ohci_ed_t), OHCI_ED_ALIGN);
16702ac6454SAndrew Thompson 	}
16802ac6454SAndrew Thompson }
16902ac6454SAndrew Thompson 
170e0a69b51SAndrew Thompson static usb_error_t
ohci_controller_init(ohci_softc_t * sc,int do_suspend)1712e141748SHans Petter Selasky ohci_controller_init(ohci_softc_t *sc, int do_suspend)
17202ac6454SAndrew Thompson {
173760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
17402ac6454SAndrew Thompson 	uint32_t i;
17502ac6454SAndrew Thompson 	uint32_t ctl;
17602ac6454SAndrew Thompson 	uint32_t ival;
17702ac6454SAndrew Thompson 	uint32_t hcr;
17802ac6454SAndrew Thompson 	uint32_t fm;
17902ac6454SAndrew Thompson 	uint32_t per;
18002ac6454SAndrew Thompson 	uint32_t desca;
18102ac6454SAndrew Thompson 
182c2e8b89cSAndrew Thompson 	/* Determine in what context we are running. */
183c2e8b89cSAndrew Thompson 	ctl = OREAD4(sc, OHCI_CONTROL);
184c2e8b89cSAndrew Thompson 	if (ctl & OHCI_IR) {
185c2e8b89cSAndrew Thompson 		/* SMM active, request change */
186c2e8b89cSAndrew Thompson 		DPRINTF("SMM active, request owner change\n");
187c2e8b89cSAndrew Thompson 		OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_OCR);
188c2e8b89cSAndrew Thompson 		for (i = 0; (i < 100) && (ctl & OHCI_IR); i++) {
189c2e8b89cSAndrew Thompson 			usb_pause_mtx(NULL, hz / 1000);
190c2e8b89cSAndrew Thompson 			ctl = OREAD4(sc, OHCI_CONTROL);
191c2e8b89cSAndrew Thompson 		}
192c2e8b89cSAndrew Thompson 		if (ctl & OHCI_IR) {
193c2e8b89cSAndrew Thompson 			device_printf(sc->sc_bus.bdev,
194c2e8b89cSAndrew Thompson 			    "SMM does not respond, resetting\n");
195c2e8b89cSAndrew Thompson 			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
196c2e8b89cSAndrew Thompson 			goto reset;
197c2e8b89cSAndrew Thompson 		}
198c2e8b89cSAndrew Thompson 	} else {
199c2e8b89cSAndrew Thompson 		DPRINTF("cold started\n");
200c2e8b89cSAndrew Thompson reset:
201c2e8b89cSAndrew Thompson 		/* controller was cold started */
202c2e8b89cSAndrew Thompson 		usb_pause_mtx(NULL,
203c2e8b89cSAndrew Thompson 		    USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
204c2e8b89cSAndrew Thompson 	}
205c2e8b89cSAndrew Thompson 
20602ac6454SAndrew Thompson 	/*
20702ac6454SAndrew Thompson 	 * This reset should not be necessary according to the OHCI spec, but
20802ac6454SAndrew Thompson 	 * without it some controllers do not start.
20902ac6454SAndrew Thompson 	 */
21002ac6454SAndrew Thompson 	DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev));
21102ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
21202ac6454SAndrew Thompson 
213a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL,
21402ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
21502ac6454SAndrew Thompson 
21602ac6454SAndrew Thompson 	/* we now own the host controller and the bus has been reset */
21702ac6454SAndrew Thompson 	ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
21802ac6454SAndrew Thompson 
21902ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR);	/* Reset HC */
22002ac6454SAndrew Thompson 	/* nominal time for a reset is 10 us */
22102ac6454SAndrew Thompson 	for (i = 0; i < 10; i++) {
22202ac6454SAndrew Thompson 		DELAY(10);
22302ac6454SAndrew Thompson 		hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
22402ac6454SAndrew Thompson 		if (!hcr) {
22502ac6454SAndrew Thompson 			break;
22602ac6454SAndrew Thompson 		}
22702ac6454SAndrew Thompson 	}
22802ac6454SAndrew Thompson 	if (hcr) {
22902ac6454SAndrew Thompson 		device_printf(sc->sc_bus.bdev, "reset timeout\n");
23002ac6454SAndrew Thompson 		return (USB_ERR_IOERROR);
23102ac6454SAndrew Thompson 	}
232ed6d949aSAndrew Thompson #ifdef USB_DEBUG
23302ac6454SAndrew Thompson 	if (ohcidebug > 15) {
23402ac6454SAndrew Thompson 		ohci_dumpregs(sc);
23502ac6454SAndrew Thompson 	}
23602ac6454SAndrew Thompson #endif
23702ac6454SAndrew Thompson 
2382e141748SHans Petter Selasky 	if (do_suspend) {
2392e141748SHans Petter Selasky 		OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_SUSPEND);
2402e141748SHans Petter Selasky 		return (USB_ERR_NORMAL_COMPLETION);
2412e141748SHans Petter Selasky 	}
2422e141748SHans Petter Selasky 
24302ac6454SAndrew Thompson 	/* The controller is now in SUSPEND state, we have 2ms to finish. */
24402ac6454SAndrew Thompson 
24502ac6454SAndrew Thompson 	/* set up HC registers */
246a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.hcca_pc, 0, &buf_res);
24702ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_HCCA, buf_res.physaddr);
24802ac6454SAndrew Thompson 
249a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.ctrl_start_pc, 0, &buf_res);
25002ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_CONTROL_HEAD_ED, buf_res.physaddr);
25102ac6454SAndrew Thompson 
252a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.bulk_start_pc, 0, &buf_res);
25302ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_BULK_HEAD_ED, buf_res.physaddr);
25402ac6454SAndrew Thompson 
25502ac6454SAndrew Thompson 	/* disable all interrupts and then switch on all desired interrupts */
25602ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
25702ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
25802ac6454SAndrew Thompson 	/* switch on desired functional features */
25902ac6454SAndrew Thompson 	ctl = OREAD4(sc, OHCI_CONTROL);
26002ac6454SAndrew Thompson 	ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
26102ac6454SAndrew Thompson 	ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
26202ac6454SAndrew Thompson 	    OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
26302ac6454SAndrew Thompson 	/* And finally start it! */
26402ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_CONTROL, ctl);
26502ac6454SAndrew Thompson 
26602ac6454SAndrew Thompson 	/*
26702ac6454SAndrew Thompson 	 * The controller is now OPERATIONAL.  Set a some final
26802ac6454SAndrew Thompson 	 * registers that should be set earlier, but that the
26902ac6454SAndrew Thompson 	 * controller ignores when in the SUSPEND state.
27002ac6454SAndrew Thompson 	 */
27102ac6454SAndrew Thompson 	fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
27202ac6454SAndrew Thompson 	fm |= OHCI_FSMPS(ival) | ival;
27302ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_FM_INTERVAL, fm);
27402ac6454SAndrew Thompson 	per = OHCI_PERIODIC(ival);	/* 90% periodic */
27502ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_PERIODIC_START, per);
27602ac6454SAndrew Thompson 
27702ac6454SAndrew Thompson 	/* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
27802ac6454SAndrew Thompson 	desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
27902ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
28002ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC);	/* Enable port power */
281a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL,
28202ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(OHCI_ENABLE_POWER_DELAY));
28302ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
28402ac6454SAndrew Thompson 
28502ac6454SAndrew Thompson 	/*
28602ac6454SAndrew Thompson 	 * The AMD756 requires a delay before re-reading the register,
28702ac6454SAndrew Thompson 	 * otherwise it will occasionally report 0 ports.
28802ac6454SAndrew Thompson 	 */
28902ac6454SAndrew Thompson 	sc->sc_noport = 0;
29002ac6454SAndrew Thompson 	for (i = 0; (i < 10) && (sc->sc_noport == 0); i++) {
291a593f6b8SAndrew Thompson 		usb_pause_mtx(NULL,
29202ac6454SAndrew Thompson 		    USB_MS_TO_TICKS(OHCI_READ_DESC_DELAY));
29302ac6454SAndrew Thompson 		sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
29402ac6454SAndrew Thompson 	}
29502ac6454SAndrew Thompson 
296ed6d949aSAndrew Thompson #ifdef USB_DEBUG
29702ac6454SAndrew Thompson 	if (ohcidebug > 5) {
29802ac6454SAndrew Thompson 		ohci_dumpregs(sc);
29902ac6454SAndrew Thompson 	}
30002ac6454SAndrew Thompson #endif
30102ac6454SAndrew Thompson 	return (USB_ERR_NORMAL_COMPLETION);
30202ac6454SAndrew Thompson }
30302ac6454SAndrew Thompson 
30402ac6454SAndrew Thompson static struct ohci_ed *
ohci_init_ed(struct usb_page_cache * pc)305760bc48eSAndrew Thompson ohci_init_ed(struct usb_page_cache *pc)
30602ac6454SAndrew Thompson {
307760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
30802ac6454SAndrew Thompson 	struct ohci_ed *ed;
30902ac6454SAndrew Thompson 
310a593f6b8SAndrew Thompson 	usbd_get_page(pc, 0, &buf_res);
31102ac6454SAndrew Thompson 
31202ac6454SAndrew Thompson 	ed = buf_res.buffer;
31302ac6454SAndrew Thompson 
31402ac6454SAndrew Thompson 	ed->ed_self = htole32(buf_res.physaddr);
31502ac6454SAndrew Thompson 	ed->ed_flags = htole32(OHCI_ED_SKIP);
31602ac6454SAndrew Thompson 	ed->page_cache = pc;
31702ac6454SAndrew Thompson 
31802ac6454SAndrew Thompson 	return (ed);
31902ac6454SAndrew Thompson }
32002ac6454SAndrew Thompson 
321e0a69b51SAndrew Thompson usb_error_t
ohci_init(ohci_softc_t * sc)32202ac6454SAndrew Thompson ohci_init(ohci_softc_t *sc)
32302ac6454SAndrew Thompson {
324760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
32502ac6454SAndrew Thompson 	uint16_t i;
32602ac6454SAndrew Thompson 	uint16_t bit;
32702ac6454SAndrew Thompson 	uint16_t x;
32802ac6454SAndrew Thompson 	uint16_t y;
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 	DPRINTF("start\n");
33102ac6454SAndrew Thompson 
33202ac6454SAndrew Thompson 	sc->sc_eintrs = OHCI_NORMAL_INTRS;
33302ac6454SAndrew Thompson 
33402ac6454SAndrew Thompson 	/*
33502ac6454SAndrew Thompson 	 * Setup all ED's
33602ac6454SAndrew Thompson 	 */
33702ac6454SAndrew Thompson 
33802ac6454SAndrew Thompson 	sc->sc_ctrl_p_last =
33902ac6454SAndrew Thompson 	    ohci_init_ed(&sc->sc_hw.ctrl_start_pc);
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	sc->sc_bulk_p_last =
34202ac6454SAndrew Thompson 	    ohci_init_ed(&sc->sc_hw.bulk_start_pc);
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 	sc->sc_isoc_p_last =
34502ac6454SAndrew Thompson 	    ohci_init_ed(&sc->sc_hw.isoc_start_pc);
34602ac6454SAndrew Thompson 
34702ac6454SAndrew Thompson 	for (i = 0; i != OHCI_NO_EDS; i++) {
34802ac6454SAndrew Thompson 		sc->sc_intr_p_last[i] =
34902ac6454SAndrew Thompson 		    ohci_init_ed(sc->sc_hw.intr_start_pc + i);
35002ac6454SAndrew Thompson 	}
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson 	/*
35302ac6454SAndrew Thompson 	 * the QHs are arranged to give poll intervals that are
35402ac6454SAndrew Thompson 	 * powers of 2 times 1ms
35502ac6454SAndrew Thompson 	 */
35602ac6454SAndrew Thompson 	bit = OHCI_NO_EDS / 2;
35702ac6454SAndrew Thompson 	while (bit) {
35802ac6454SAndrew Thompson 		x = bit;
35902ac6454SAndrew Thompson 		while (x & bit) {
36002ac6454SAndrew Thompson 			ohci_ed_t *ed_x;
36102ac6454SAndrew Thompson 			ohci_ed_t *ed_y;
36202ac6454SAndrew Thompson 
36302ac6454SAndrew Thompson 			y = (x ^ bit) | (bit / 2);
36402ac6454SAndrew Thompson 
36502ac6454SAndrew Thompson 			/*
36602ac6454SAndrew Thompson 			 * the next QH has half the poll interval
36702ac6454SAndrew Thompson 			 */
36802ac6454SAndrew Thompson 			ed_x = sc->sc_intr_p_last[x];
36902ac6454SAndrew Thompson 			ed_y = sc->sc_intr_p_last[y];
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson 			ed_x->next = NULL;
37202ac6454SAndrew Thompson 			ed_x->ed_next = ed_y->ed_self;
37302ac6454SAndrew Thompson 
37402ac6454SAndrew Thompson 			x++;
37502ac6454SAndrew Thompson 		}
37602ac6454SAndrew Thompson 		bit >>= 1;
37702ac6454SAndrew Thompson 	}
37802ac6454SAndrew Thompson 
37902ac6454SAndrew Thompson 	if (1) {
38002ac6454SAndrew Thompson 		ohci_ed_t *ed_int;
38102ac6454SAndrew Thompson 		ohci_ed_t *ed_isc;
38202ac6454SAndrew Thompson 
38302ac6454SAndrew Thompson 		ed_int = sc->sc_intr_p_last[0];
38402ac6454SAndrew Thompson 		ed_isc = sc->sc_isoc_p_last;
38502ac6454SAndrew Thompson 
38602ac6454SAndrew Thompson 		/* the last (1ms) QH */
38702ac6454SAndrew Thompson 		ed_int->next = ed_isc;
38802ac6454SAndrew Thompson 		ed_int->ed_next = ed_isc->ed_self;
38902ac6454SAndrew Thompson 	}
390a593f6b8SAndrew Thompson 	usbd_get_page(&sc->sc_hw.hcca_pc, 0, &buf_res);
39102ac6454SAndrew Thompson 
39202ac6454SAndrew Thompson 	sc->sc_hcca_p = buf_res.buffer;
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 	/*
39502ac6454SAndrew Thompson 	 * Fill HCCA interrupt table.  The bit reversal is to get
39602ac6454SAndrew Thompson 	 * the tree set up properly to spread the interrupts.
39702ac6454SAndrew Thompson 	 */
39802ac6454SAndrew Thompson 	for (i = 0; i != OHCI_NO_INTRS; i++) {
39902ac6454SAndrew Thompson 		sc->sc_hcca_p->hcca_interrupt_table[i] =
40002ac6454SAndrew Thompson 		    sc->sc_intr_p_last[i | (OHCI_NO_EDS / 2)]->ed_self;
40102ac6454SAndrew Thompson 	}
40202ac6454SAndrew Thompson 	/* flush all cache into memory */
40302ac6454SAndrew Thompson 
404a593f6b8SAndrew Thompson 	usb_bus_mem_flush_all(&sc->sc_bus, &ohci_iterate_hw_softc);
40502ac6454SAndrew Thompson 
40602ac6454SAndrew Thompson 	/* set up the bus struct */
40702ac6454SAndrew Thompson 	sc->sc_bus.methods = &ohci_bus_methods;
40802ac6454SAndrew Thompson 
409a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_tmo_rhsc, &sc->sc_bus.bus_mtx, 0);
41002ac6454SAndrew Thompson 
411ed6d949aSAndrew Thompson #ifdef USB_DEBUG
41202ac6454SAndrew Thompson 	if (ohcidebug > 15) {
41302ac6454SAndrew Thompson 		for (i = 0; i != OHCI_NO_EDS; i++) {
41402ac6454SAndrew Thompson 			printf("ed#%d ", i);
41502ac6454SAndrew Thompson 			ohci_dump_ed(sc->sc_intr_p_last[i]);
41602ac6454SAndrew Thompson 		}
41702ac6454SAndrew Thompson 		printf("iso ");
41802ac6454SAndrew Thompson 		ohci_dump_ed(sc->sc_isoc_p_last);
41902ac6454SAndrew Thompson 	}
42002ac6454SAndrew Thompson #endif
42102ac6454SAndrew Thompson 
42202ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_1_0;
42302ac6454SAndrew Thompson 
4242e141748SHans Petter Selasky 	if (ohci_controller_init(sc, 0) != 0)
42502ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
4262e141748SHans Petter Selasky 
42702ac6454SAndrew Thompson 	/* catch any lost interrupts */
42802ac6454SAndrew Thompson 	ohci_do_poll(&sc->sc_bus);
42902ac6454SAndrew Thompson 	return (USB_ERR_NORMAL_COMPLETION);
43002ac6454SAndrew Thompson }
43102ac6454SAndrew Thompson 
43202ac6454SAndrew Thompson /*
43302ac6454SAndrew Thompson  * shut down the controller when the system is going down
43402ac6454SAndrew Thompson  */
43502ac6454SAndrew Thompson void
ohci_detach(struct ohci_softc * sc)43602ac6454SAndrew Thompson ohci_detach(struct ohci_softc *sc)
43702ac6454SAndrew Thompson {
43802ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
43902ac6454SAndrew Thompson 
440a593f6b8SAndrew Thompson 	usb_callout_stop(&sc->sc_tmo_rhsc);
44102ac6454SAndrew Thompson 
44202ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
44302ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
44402ac6454SAndrew Thompson 
44502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson 	/* XXX let stray task complete */
448a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL, hz / 20);
44902ac6454SAndrew Thompson 
450a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_tmo_rhsc);
45102ac6454SAndrew Thompson }
45202ac6454SAndrew Thompson 
4532e141748SHans Petter Selasky static void
ohci_suspend(ohci_softc_t * sc)45402ac6454SAndrew Thompson ohci_suspend(ohci_softc_t *sc)
45502ac6454SAndrew Thompson {
4562e141748SHans Petter Selasky 	DPRINTF("\n");
45702ac6454SAndrew Thompson 
458ed6d949aSAndrew Thompson #ifdef USB_DEBUG
4592e141748SHans Petter Selasky 	if (ohcidebug > 2)
46002ac6454SAndrew Thompson 		ohci_dumpregs(sc);
46102ac6454SAndrew Thompson #endif
46202ac6454SAndrew Thompson 
4632e141748SHans Petter Selasky 	/* reset HC and leave it suspended */
4642e141748SHans Petter Selasky 	ohci_controller_init(sc, 1);
46502ac6454SAndrew Thompson }
46602ac6454SAndrew Thompson 
4672e141748SHans Petter Selasky static void
ohci_resume(ohci_softc_t * sc)46802ac6454SAndrew Thompson ohci_resume(ohci_softc_t *sc)
46902ac6454SAndrew Thompson {
4702e141748SHans Petter Selasky 	DPRINTF("\n");
47102ac6454SAndrew Thompson 
472ed6d949aSAndrew Thompson #ifdef USB_DEBUG
4732e141748SHans Petter Selasky 	if (ohcidebug > 2)
47402ac6454SAndrew Thompson 		ohci_dumpregs(sc);
47502ac6454SAndrew Thompson #endif
4762e141748SHans Petter Selasky 
47702ac6454SAndrew Thompson 	/* some broken BIOSes never initialize the Controller chip */
4782e141748SHans Petter Selasky 	ohci_controller_init(sc, 0);
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 	/* catch any lost interrupts */
48102ac6454SAndrew Thompson 	ohci_do_poll(&sc->sc_bus);
48202ac6454SAndrew Thompson }
48302ac6454SAndrew Thompson 
484ed6d949aSAndrew Thompson #ifdef USB_DEBUG
48502ac6454SAndrew Thompson static void
ohci_dumpregs(ohci_softc_t * sc)48602ac6454SAndrew Thompson ohci_dumpregs(ohci_softc_t *sc)
48702ac6454SAndrew Thompson {
48802ac6454SAndrew Thompson 	struct ohci_hcca *hcca;
48902ac6454SAndrew Thompson 
49002ac6454SAndrew Thompson 	DPRINTF("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
49102ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_REVISION),
49202ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_CONTROL),
49302ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_COMMAND_STATUS));
49402ac6454SAndrew Thompson 	DPRINTF("               intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
49502ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_INTERRUPT_STATUS),
49602ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_INTERRUPT_ENABLE),
49702ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_INTERRUPT_DISABLE));
49802ac6454SAndrew Thompson 	DPRINTF("               hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
49902ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_HCCA),
50002ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
50102ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_CONTROL_HEAD_ED));
50202ac6454SAndrew Thompson 	DPRINTF("               ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
50302ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
50402ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_BULK_HEAD_ED),
50502ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_BULK_CURRENT_ED));
50602ac6454SAndrew Thompson 	DPRINTF("               done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
50702ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_DONE_HEAD),
50802ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_FM_INTERVAL),
50902ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_FM_REMAINING));
51002ac6454SAndrew Thompson 	DPRINTF("               fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
51102ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_FM_NUMBER),
51202ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_PERIODIC_START),
51302ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_LS_THRESHOLD));
51402ac6454SAndrew Thompson 	DPRINTF("               desca=0x%08x descb=0x%08x stat=0x%08x\n",
51502ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
51602ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
51702ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_RH_STATUS));
51802ac6454SAndrew Thompson 	DPRINTF("               port1=0x%08x port2=0x%08x\n",
51902ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
52002ac6454SAndrew Thompson 	    OREAD4(sc, OHCI_RH_PORT_STATUS(2)));
52102ac6454SAndrew Thompson 
52202ac6454SAndrew Thompson 	hcca = ohci_get_hcca(sc);
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson 	DPRINTF("         HCCA: frame_number=0x%04x done_head=0x%08x\n",
52502ac6454SAndrew Thompson 	    le32toh(hcca->hcca_frame_number),
52602ac6454SAndrew Thompson 	    le32toh(hcca->hcca_done_head));
52702ac6454SAndrew Thompson }
52802ac6454SAndrew Thompson static void
ohci_dump_tds(ohci_td_t * std)52902ac6454SAndrew Thompson ohci_dump_tds(ohci_td_t *std)
53002ac6454SAndrew Thompson {
53102ac6454SAndrew Thompson 	for (; std; std = std->obj_next) {
53202ac6454SAndrew Thompson 		if (ohci_dump_td(std)) {
53302ac6454SAndrew Thompson 			break;
53402ac6454SAndrew Thompson 		}
53502ac6454SAndrew Thompson 	}
53602ac6454SAndrew Thompson }
53702ac6454SAndrew Thompson 
53802ac6454SAndrew Thompson static uint8_t
ohci_dump_td(ohci_td_t * std)53902ac6454SAndrew Thompson ohci_dump_td(ohci_td_t *std)
54002ac6454SAndrew Thompson {
54102ac6454SAndrew Thompson 	uint32_t td_flags;
54202ac6454SAndrew Thompson 	uint8_t temp;
54302ac6454SAndrew Thompson 
544a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(std->page_cache);
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson 	td_flags = le32toh(std->td_flags);
54702ac6454SAndrew Thompson 	temp = (std->td_next == 0);
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson 	printf("TD(%p) at 0x%08x: %s%s%s%s%s delay=%d ec=%d "
55002ac6454SAndrew Thompson 	    "cc=%d\ncbp=0x%08x next=0x%08x be=0x%08x\n",
55102ac6454SAndrew Thompson 	    std, le32toh(std->td_self),
55202ac6454SAndrew Thompson 	    (td_flags & OHCI_TD_R) ? "-R" : "",
55302ac6454SAndrew Thompson 	    (td_flags & OHCI_TD_OUT) ? "-OUT" : "",
55402ac6454SAndrew Thompson 	    (td_flags & OHCI_TD_IN) ? "-IN" : "",
55502ac6454SAndrew Thompson 	    ((td_flags & OHCI_TD_TOGGLE_MASK) == OHCI_TD_TOGGLE_1) ? "-TOG1" : "",
55602ac6454SAndrew Thompson 	    ((td_flags & OHCI_TD_TOGGLE_MASK) == OHCI_TD_TOGGLE_0) ? "-TOG0" : "",
55702ac6454SAndrew Thompson 	    OHCI_TD_GET_DI(td_flags),
55802ac6454SAndrew Thompson 	    OHCI_TD_GET_EC(td_flags),
55902ac6454SAndrew Thompson 	    OHCI_TD_GET_CC(td_flags),
56002ac6454SAndrew Thompson 	    le32toh(std->td_cbp),
56102ac6454SAndrew Thompson 	    le32toh(std->td_next),
56202ac6454SAndrew Thompson 	    le32toh(std->td_be));
56302ac6454SAndrew Thompson 
56402ac6454SAndrew Thompson 	return (temp);
56502ac6454SAndrew Thompson }
56602ac6454SAndrew Thompson 
56702ac6454SAndrew Thompson static uint8_t
ohci_dump_itd(ohci_itd_t * sitd)56802ac6454SAndrew Thompson ohci_dump_itd(ohci_itd_t *sitd)
56902ac6454SAndrew Thompson {
57002ac6454SAndrew Thompson 	uint32_t itd_flags;
57102ac6454SAndrew Thompson 	uint16_t i;
57202ac6454SAndrew Thompson 	uint8_t temp;
57302ac6454SAndrew Thompson 
574a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sitd->page_cache);
57502ac6454SAndrew Thompson 
57602ac6454SAndrew Thompson 	itd_flags = le32toh(sitd->itd_flags);
57702ac6454SAndrew Thompson 	temp = (sitd->itd_next == 0);
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson 	printf("ITD(%p) at 0x%08x: sf=%d di=%d fc=%d cc=%d\n"
58002ac6454SAndrew Thompson 	    "bp0=0x%08x next=0x%08x be=0x%08x\n",
58102ac6454SAndrew Thompson 	    sitd, le32toh(sitd->itd_self),
58202ac6454SAndrew Thompson 	    OHCI_ITD_GET_SF(itd_flags),
58302ac6454SAndrew Thompson 	    OHCI_ITD_GET_DI(itd_flags),
58402ac6454SAndrew Thompson 	    OHCI_ITD_GET_FC(itd_flags),
58502ac6454SAndrew Thompson 	    OHCI_ITD_GET_CC(itd_flags),
58602ac6454SAndrew Thompson 	    le32toh(sitd->itd_bp0),
58702ac6454SAndrew Thompson 	    le32toh(sitd->itd_next),
58802ac6454SAndrew Thompson 	    le32toh(sitd->itd_be));
58902ac6454SAndrew Thompson 	for (i = 0; i < OHCI_ITD_NOFFSET; i++) {
59002ac6454SAndrew Thompson 		printf("offs[%d]=0x%04x ", i,
59102ac6454SAndrew Thompson 		    (uint32_t)le16toh(sitd->itd_offset[i]));
59202ac6454SAndrew Thompson 	}
59302ac6454SAndrew Thompson 	printf("\n");
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson 	return (temp);
59602ac6454SAndrew Thompson }
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson static void
ohci_dump_itds(ohci_itd_t * sitd)59902ac6454SAndrew Thompson ohci_dump_itds(ohci_itd_t *sitd)
60002ac6454SAndrew Thompson {
60102ac6454SAndrew Thompson 	for (; sitd; sitd = sitd->obj_next) {
60202ac6454SAndrew Thompson 		if (ohci_dump_itd(sitd)) {
60302ac6454SAndrew Thompson 			break;
60402ac6454SAndrew Thompson 		}
60502ac6454SAndrew Thompson 	}
60602ac6454SAndrew Thompson }
60702ac6454SAndrew Thompson 
60802ac6454SAndrew Thompson static void
ohci_dump_ed(ohci_ed_t * sed)60902ac6454SAndrew Thompson ohci_dump_ed(ohci_ed_t *sed)
61002ac6454SAndrew Thompson {
61102ac6454SAndrew Thompson 	uint32_t ed_flags;
61202ac6454SAndrew Thompson 	uint32_t ed_headp;
61302ac6454SAndrew Thompson 
614a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(sed->page_cache);
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson 	ed_flags = le32toh(sed->ed_flags);
61702ac6454SAndrew Thompson 	ed_headp = le32toh(sed->ed_headp);
61802ac6454SAndrew Thompson 
61902ac6454SAndrew Thompson 	printf("ED(%p) at 0x%08x: addr=%d endpt=%d maxp=%d flags=%s%s%s%s%s\n"
62002ac6454SAndrew Thompson 	    "tailp=0x%08x headflags=%s%s headp=0x%08x nexted=0x%08x\n",
62102ac6454SAndrew Thompson 	    sed, le32toh(sed->ed_self),
62202ac6454SAndrew Thompson 	    OHCI_ED_GET_FA(ed_flags),
62302ac6454SAndrew Thompson 	    OHCI_ED_GET_EN(ed_flags),
62402ac6454SAndrew Thompson 	    OHCI_ED_GET_MAXP(ed_flags),
62502ac6454SAndrew Thompson 	    (ed_flags & OHCI_ED_DIR_OUT) ? "-OUT" : "",
62602ac6454SAndrew Thompson 	    (ed_flags & OHCI_ED_DIR_IN) ? "-IN" : "",
62702ac6454SAndrew Thompson 	    (ed_flags & OHCI_ED_SPEED) ? "-LOWSPEED" : "",
62802ac6454SAndrew Thompson 	    (ed_flags & OHCI_ED_SKIP) ? "-SKIP" : "",
62902ac6454SAndrew Thompson 	    (ed_flags & OHCI_ED_FORMAT_ISO) ? "-ISO" : "",
63002ac6454SAndrew Thompson 	    le32toh(sed->ed_tailp),
63102ac6454SAndrew Thompson 	    (ed_headp & OHCI_HALTED) ? "-HALTED" : "",
63202ac6454SAndrew Thompson 	    (ed_headp & OHCI_TOGGLECARRY) ? "-CARRY" : "",
63302ac6454SAndrew Thompson 	    le32toh(sed->ed_headp),
63402ac6454SAndrew Thompson 	    le32toh(sed->ed_next));
63502ac6454SAndrew Thompson }
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson #endif
63802ac6454SAndrew Thompson 
63902ac6454SAndrew Thompson static void
ohci_transfer_intr_enqueue(struct usb_xfer * xfer)640760bc48eSAndrew Thompson ohci_transfer_intr_enqueue(struct usb_xfer *xfer)
64102ac6454SAndrew Thompson {
64202ac6454SAndrew Thompson 	/* check for early completion */
64302ac6454SAndrew Thompson 	if (ohci_check_transfer(xfer)) {
64402ac6454SAndrew Thompson 		return;
64502ac6454SAndrew Thompson 	}
64602ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
647a593f6b8SAndrew Thompson 	usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	/* start timeout, if any */
65002ac6454SAndrew Thompson 	if (xfer->timeout != 0) {
651a593f6b8SAndrew Thompson 		usbd_transfer_timeout_ms(xfer, &ohci_timeout, xfer->timeout);
65202ac6454SAndrew Thompson 	}
65302ac6454SAndrew Thompson }
65402ac6454SAndrew Thompson 
65502ac6454SAndrew Thompson #define	OHCI_APPEND_QH(sed,last) (last) = _ohci_append_qh(sed,last)
65602ac6454SAndrew Thompson static ohci_ed_t *
_ohci_append_qh(ohci_ed_t * sed,ohci_ed_t * last)65702ac6454SAndrew Thompson _ohci_append_qh(ohci_ed_t *sed, ohci_ed_t *last)
65802ac6454SAndrew Thompson {
65902ac6454SAndrew Thompson 	DPRINTFN(11, "%p to %p\n", sed, last);
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson 	if (sed->prev != NULL) {
66202ac6454SAndrew Thompson 		/* should not happen */
66302ac6454SAndrew Thompson 		DPRINTFN(0, "ED already linked!\n");
66402ac6454SAndrew Thompson 		return (last);
66502ac6454SAndrew Thompson 	}
66602ac6454SAndrew Thompson 	/* (sc->sc_bus.bus_mtx) must be locked */
66702ac6454SAndrew Thompson 
66802ac6454SAndrew Thompson 	sed->next = last->next;
66902ac6454SAndrew Thompson 	sed->ed_next = last->ed_next;
67002ac6454SAndrew Thompson 	sed->ed_tailp = 0;
67102ac6454SAndrew Thompson 
67202ac6454SAndrew Thompson 	sed->prev = last;
67302ac6454SAndrew Thompson 
674a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(sed->page_cache);
67502ac6454SAndrew Thompson 
67602ac6454SAndrew Thompson 	/*
67702ac6454SAndrew Thompson 	 * the last->next->prev is never followed: sed->next->prev = sed;
67802ac6454SAndrew Thompson 	 */
67902ac6454SAndrew Thompson 
68002ac6454SAndrew Thompson 	last->next = sed;
68102ac6454SAndrew Thompson 	last->ed_next = sed->ed_self;
68202ac6454SAndrew Thompson 
683a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(last->page_cache);
68402ac6454SAndrew Thompson 
68502ac6454SAndrew Thompson 	return (sed);
68602ac6454SAndrew Thompson }
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson #define	OHCI_REMOVE_QH(sed,last) (last) = _ohci_remove_qh(sed,last)
68902ac6454SAndrew Thompson static ohci_ed_t *
_ohci_remove_qh(ohci_ed_t * sed,ohci_ed_t * last)69002ac6454SAndrew Thompson _ohci_remove_qh(ohci_ed_t *sed, ohci_ed_t *last)
69102ac6454SAndrew Thompson {
69202ac6454SAndrew Thompson 	DPRINTFN(11, "%p from %p\n", sed, last);
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 	/* (sc->sc_bus.bus_mtx) must be locked */
69502ac6454SAndrew Thompson 
69602ac6454SAndrew Thompson 	/* only remove if not removed from a queue */
69702ac6454SAndrew Thompson 	if (sed->prev) {
69802ac6454SAndrew Thompson 		sed->prev->next = sed->next;
69902ac6454SAndrew Thompson 		sed->prev->ed_next = sed->ed_next;
70002ac6454SAndrew Thompson 
701a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sed->prev->page_cache);
70202ac6454SAndrew Thompson 
70302ac6454SAndrew Thompson 		if (sed->next) {
70402ac6454SAndrew Thompson 			sed->next->prev = sed->prev;
705a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(sed->next->page_cache);
70602ac6454SAndrew Thompson 		}
70702ac6454SAndrew Thompson 		last = ((last == sed) ? sed->prev : last);
70802ac6454SAndrew Thompson 
70902ac6454SAndrew Thompson 		sed->prev = 0;
71002ac6454SAndrew Thompson 
711a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(sed->page_cache);
71202ac6454SAndrew Thompson 	}
71302ac6454SAndrew Thompson 	return (last);
71402ac6454SAndrew Thompson }
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson static void
ohci_isoc_done(struct usb_xfer * xfer)717760bc48eSAndrew Thompson ohci_isoc_done(struct usb_xfer *xfer)
71802ac6454SAndrew Thompson {
71902ac6454SAndrew Thompson 	uint8_t nframes;
72002ac6454SAndrew Thompson 	uint32_t *plen = xfer->frlengths;
72102ac6454SAndrew Thompson 	volatile uint16_t *olen;
72202ac6454SAndrew Thompson 	uint16_t len = 0;
72302ac6454SAndrew Thompson 	ohci_itd_t *td = xfer->td_transfer_first;
72402ac6454SAndrew Thompson 
72502ac6454SAndrew Thompson 	while (1) {
72602ac6454SAndrew Thompson 		if (td == NULL) {
72702ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
72802ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
72902ac6454SAndrew Thompson 		}
730ed6d949aSAndrew Thompson #ifdef USB_DEBUG
73102ac6454SAndrew Thompson 		if (ohcidebug > 5) {
73202ac6454SAndrew Thompson 			DPRINTF("isoc TD\n");
73302ac6454SAndrew Thompson 			ohci_dump_itd(td);
73402ac6454SAndrew Thompson 		}
73502ac6454SAndrew Thompson #endif
736a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
73702ac6454SAndrew Thompson 
73802ac6454SAndrew Thompson 		nframes = td->frames;
73902ac6454SAndrew Thompson 		olen = &td->itd_offset[0];
74002ac6454SAndrew Thompson 
74102ac6454SAndrew Thompson 		if (nframes > 8) {
74202ac6454SAndrew Thompson 			nframes = 8;
74302ac6454SAndrew Thompson 		}
74402ac6454SAndrew Thompson 		while (nframes--) {
74502ac6454SAndrew Thompson 			len = le16toh(*olen);
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson 			if ((len >> 12) == OHCI_CC_NOT_ACCESSED) {
74802ac6454SAndrew Thompson 				len = 0;
74902ac6454SAndrew Thompson 			} else {
75002ac6454SAndrew Thompson 				len &= ((1 << 12) - 1);
75102ac6454SAndrew Thompson 			}
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 			if (len > *plen) {
75402ac6454SAndrew Thompson 				len = 0;/* invalid length */
75502ac6454SAndrew Thompson 			}
75602ac6454SAndrew Thompson 			*plen = len;
75702ac6454SAndrew Thompson 			plen++;
75802ac6454SAndrew Thompson 			olen++;
75902ac6454SAndrew Thompson 		}
76002ac6454SAndrew Thompson 
76102ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
76202ac6454SAndrew Thompson 			break;
76302ac6454SAndrew Thompson 		}
76402ac6454SAndrew Thompson 		td = td->obj_next;
76502ac6454SAndrew Thompson 	}
76602ac6454SAndrew Thompson 
76702ac6454SAndrew Thompson 	xfer->aframes = xfer->nframes;
76802ac6454SAndrew Thompson 	ohci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
76902ac6454SAndrew Thompson }
77002ac6454SAndrew Thompson 
771ed6d949aSAndrew Thompson #ifdef USB_DEBUG
77202ac6454SAndrew Thompson static const char *const
77302ac6454SAndrew Thompson 	ohci_cc_strs[] =
77402ac6454SAndrew Thompson {
77502ac6454SAndrew Thompson 	"NO_ERROR",
77602ac6454SAndrew Thompson 	"CRC",
77702ac6454SAndrew Thompson 	"BIT_STUFFING",
77802ac6454SAndrew Thompson 	"DATA_TOGGLE_MISMATCH",
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson 	"STALL",
78102ac6454SAndrew Thompson 	"DEVICE_NOT_RESPONDING",
78202ac6454SAndrew Thompson 	"PID_CHECK_FAILURE",
78302ac6454SAndrew Thompson 	"UNEXPECTED_PID",
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson 	"DATA_OVERRUN",
78602ac6454SAndrew Thompson 	"DATA_UNDERRUN",
78702ac6454SAndrew Thompson 	"BUFFER_OVERRUN",
78802ac6454SAndrew Thompson 	"BUFFER_UNDERRUN",
78902ac6454SAndrew Thompson 
79002ac6454SAndrew Thompson 	"reserved",
79102ac6454SAndrew Thompson 	"reserved",
79202ac6454SAndrew Thompson 	"NOT_ACCESSED",
79302ac6454SAndrew Thompson 	"NOT_ACCESSED"
79402ac6454SAndrew Thompson };
79502ac6454SAndrew Thompson 
79602ac6454SAndrew Thompson #endif
79702ac6454SAndrew Thompson 
798e0a69b51SAndrew Thompson static usb_error_t
ohci_non_isoc_done_sub(struct usb_xfer * xfer)799760bc48eSAndrew Thompson ohci_non_isoc_done_sub(struct usb_xfer *xfer)
80002ac6454SAndrew Thompson {
80102ac6454SAndrew Thompson 	ohci_td_t *td;
80202ac6454SAndrew Thompson 	ohci_td_t *td_alt_next;
80302ac6454SAndrew Thompson 	uint32_t temp;
80402ac6454SAndrew Thompson 	uint32_t phy_start;
80502ac6454SAndrew Thompson 	uint32_t phy_end;
80602ac6454SAndrew Thompson 	uint32_t td_flags;
80702ac6454SAndrew Thompson 	uint16_t cc;
80802ac6454SAndrew Thompson 
80902ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
81002ac6454SAndrew Thompson 	td_alt_next = td->alt_next;
81102ac6454SAndrew Thompson 	td_flags = 0;
81202ac6454SAndrew Thompson 
81302ac6454SAndrew Thompson 	if (xfer->aframes != xfer->nframes) {
814ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
81502ac6454SAndrew Thompson 	}
81602ac6454SAndrew Thompson 	while (1) {
817a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
81802ac6454SAndrew Thompson 		phy_start = le32toh(td->td_cbp);
81902ac6454SAndrew Thompson 		td_flags = le32toh(td->td_flags);
82002ac6454SAndrew Thompson 		cc = OHCI_TD_GET_CC(td_flags);
82102ac6454SAndrew Thompson 
82202ac6454SAndrew Thompson 		if (phy_start) {
82302ac6454SAndrew Thompson 			/*
82402ac6454SAndrew Thompson 			 * short transfer - compute the number of remaining
82502ac6454SAndrew Thompson 			 * bytes in the hardware buffer:
82602ac6454SAndrew Thompson 			 */
82702ac6454SAndrew Thompson 			phy_end = le32toh(td->td_be);
82802ac6454SAndrew Thompson 			temp = (OHCI_PAGE(phy_start ^ phy_end) ?
82902ac6454SAndrew Thompson 			    (OHCI_PAGE_SIZE + 1) : 0x0001);
83002ac6454SAndrew Thompson 			temp += OHCI_PAGE_OFFSET(phy_end);
83102ac6454SAndrew Thompson 			temp -= OHCI_PAGE_OFFSET(phy_start);
83202ac6454SAndrew Thompson 
83302ac6454SAndrew Thompson 			if (temp > td->len) {
83402ac6454SAndrew Thompson 				/* guard against corruption */
83502ac6454SAndrew Thompson 				cc = OHCI_CC_STALL;
83602ac6454SAndrew Thompson 			} else if (xfer->aframes != xfer->nframes) {
83702ac6454SAndrew Thompson 				/*
83802ac6454SAndrew Thompson 				 * Sum up total transfer length
83902ac6454SAndrew Thompson 				 * in "frlengths[]":
84002ac6454SAndrew Thompson 				 */
84102ac6454SAndrew Thompson 				xfer->frlengths[xfer->aframes] += td->len - temp;
84202ac6454SAndrew Thompson 			}
84302ac6454SAndrew Thompson 		} else {
84402ac6454SAndrew Thompson 			if (xfer->aframes != xfer->nframes) {
84502ac6454SAndrew Thompson 				/* transfer was complete */
84602ac6454SAndrew Thompson 				xfer->frlengths[xfer->aframes] += td->len;
84702ac6454SAndrew Thompson 			}
84802ac6454SAndrew Thompson 		}
84902ac6454SAndrew Thompson 		/* Check for last transfer */
85002ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
85102ac6454SAndrew Thompson 			td = NULL;
85202ac6454SAndrew Thompson 			break;
85302ac6454SAndrew Thompson 		}
85402ac6454SAndrew Thompson 		/* Check transfer status */
85502ac6454SAndrew Thompson 		if (cc) {
85602ac6454SAndrew Thompson 			/* the transfer is finished */
85702ac6454SAndrew Thompson 			td = NULL;
85802ac6454SAndrew Thompson 			break;
85902ac6454SAndrew Thompson 		}
86002ac6454SAndrew Thompson 		/* Check for short transfer */
86102ac6454SAndrew Thompson 		if (phy_start) {
86202ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
86302ac6454SAndrew Thompson 				/* follow alt next */
86402ac6454SAndrew Thompson 				td = td->alt_next;
86502ac6454SAndrew Thompson 			} else {
86602ac6454SAndrew Thompson 				/* the transfer is finished */
86702ac6454SAndrew Thompson 				td = NULL;
86802ac6454SAndrew Thompson 			}
86902ac6454SAndrew Thompson 			break;
87002ac6454SAndrew Thompson 		}
87102ac6454SAndrew Thompson 		td = td->obj_next;
87202ac6454SAndrew Thompson 
87302ac6454SAndrew Thompson 		if (td->alt_next != td_alt_next) {
87402ac6454SAndrew Thompson 			/* this USB frame is complete */
87502ac6454SAndrew Thompson 			break;
87602ac6454SAndrew Thompson 		}
87702ac6454SAndrew Thompson 	}
87802ac6454SAndrew Thompson 
87902ac6454SAndrew Thompson 	/* update transfer cache */
88002ac6454SAndrew Thompson 
88102ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 	DPRINTFN(16, "error cc=%d (%s)\n",
88402ac6454SAndrew Thompson 	    cc, ohci_cc_strs[cc]);
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson 	return ((cc == 0) ? USB_ERR_NORMAL_COMPLETION :
88702ac6454SAndrew Thompson 	    (cc == OHCI_CC_STALL) ? USB_ERR_STALLED : USB_ERR_IOERROR);
88802ac6454SAndrew Thompson }
88902ac6454SAndrew Thompson 
89002ac6454SAndrew Thompson static void
ohci_non_isoc_done(struct usb_xfer * xfer)891760bc48eSAndrew Thompson ohci_non_isoc_done(struct usb_xfer *xfer)
89202ac6454SAndrew Thompson {
893e0a69b51SAndrew Thompson 	usb_error_t err = 0;
89402ac6454SAndrew Thompson 
895ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
896ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
89702ac6454SAndrew Thompson 
898ed6d949aSAndrew Thompson #ifdef USB_DEBUG
89902ac6454SAndrew Thompson 	if (ohcidebug > 10) {
90002ac6454SAndrew Thompson 		ohci_dump_tds(xfer->td_transfer_first);
90102ac6454SAndrew Thompson 	}
90202ac6454SAndrew Thompson #endif
90302ac6454SAndrew Thompson 
90402ac6454SAndrew Thompson 	/* reset scanner */
90502ac6454SAndrew Thompson 
90602ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
90702ac6454SAndrew Thompson 
90802ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
90902ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
91002ac6454SAndrew Thompson 			err = ohci_non_isoc_done_sub(xfer);
91102ac6454SAndrew Thompson 		}
91202ac6454SAndrew Thompson 		xfer->aframes = 1;
91302ac6454SAndrew Thompson 
91402ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
91502ac6454SAndrew Thompson 			goto done;
91602ac6454SAndrew Thompson 		}
91702ac6454SAndrew Thompson 	}
91802ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
91902ac6454SAndrew Thompson 		err = ohci_non_isoc_done_sub(xfer);
92002ac6454SAndrew Thompson 		xfer->aframes++;
92102ac6454SAndrew Thompson 
92202ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
92302ac6454SAndrew Thompson 			goto done;
92402ac6454SAndrew Thompson 		}
92502ac6454SAndrew Thompson 	}
92602ac6454SAndrew Thompson 
92702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
92802ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
92902ac6454SAndrew Thompson 		err = ohci_non_isoc_done_sub(xfer);
93002ac6454SAndrew Thompson 	}
93102ac6454SAndrew Thompson done:
93202ac6454SAndrew Thompson 	ohci_device_done(xfer, err);
93302ac6454SAndrew Thompson }
93402ac6454SAndrew Thompson 
93502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
93602ac6454SAndrew Thompson  *	ohci_check_transfer_sub
93702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
93802ac6454SAndrew Thompson static void
ohci_check_transfer_sub(struct usb_xfer * xfer)939760bc48eSAndrew Thompson ohci_check_transfer_sub(struct usb_xfer *xfer)
94002ac6454SAndrew Thompson {
94102ac6454SAndrew Thompson 	ohci_td_t *td;
94202ac6454SAndrew Thompson 	ohci_ed_t *ed;
94302ac6454SAndrew Thompson 	uint32_t phy_start;
94402ac6454SAndrew Thompson 	uint32_t td_flags;
94502ac6454SAndrew Thompson 	uint32_t td_next;
94602ac6454SAndrew Thompson 	uint16_t cc;
94702ac6454SAndrew Thompson 
94802ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
94902ac6454SAndrew Thompson 
95002ac6454SAndrew Thompson 	while (1) {
951a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(td->page_cache);
95202ac6454SAndrew Thompson 		phy_start = le32toh(td->td_cbp);
95302ac6454SAndrew Thompson 		td_flags = le32toh(td->td_flags);
95402ac6454SAndrew Thompson 		td_next = le32toh(td->td_next);
95502ac6454SAndrew Thompson 
95602ac6454SAndrew Thompson 		/* Check for last transfer */
95702ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
95802ac6454SAndrew Thompson 			/* the transfer is finished */
95902ac6454SAndrew Thompson 			td = NULL;
96002ac6454SAndrew Thompson 			break;
96102ac6454SAndrew Thompson 		}
96202ac6454SAndrew Thompson 		/* Check transfer status */
96302ac6454SAndrew Thompson 		cc = OHCI_TD_GET_CC(td_flags);
96402ac6454SAndrew Thompson 		if (cc) {
96502ac6454SAndrew Thompson 			/* the transfer is finished */
96602ac6454SAndrew Thompson 			td = NULL;
96702ac6454SAndrew Thompson 			break;
96802ac6454SAndrew Thompson 		}
96902ac6454SAndrew Thompson 		/*
97002ac6454SAndrew Thompson 	         * Check if we reached the last packet
97102ac6454SAndrew Thompson 	         * or if there is a short packet:
97202ac6454SAndrew Thompson 	         */
97302ac6454SAndrew Thompson 
97402ac6454SAndrew Thompson 		if (((td_next & (~0xF)) == OHCI_TD_NEXT_END) || phy_start) {
97502ac6454SAndrew Thompson 			/* follow alt next */
97602ac6454SAndrew Thompson 			td = td->alt_next;
97702ac6454SAndrew Thompson 			break;
97802ac6454SAndrew Thompson 		}
97902ac6454SAndrew Thompson 		td = td->obj_next;
98002ac6454SAndrew Thompson 	}
98102ac6454SAndrew Thompson 
98202ac6454SAndrew Thompson 	/* update transfer cache */
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson 	if (td) {
98702ac6454SAndrew Thompson 		ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
98802ac6454SAndrew Thompson 
98902ac6454SAndrew Thompson 		ed->ed_headp = td->td_self;
990a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(ed->page_cache);
99102ac6454SAndrew Thompson 
99202ac6454SAndrew Thompson 		DPRINTFN(13, "xfer=%p following alt next\n", xfer);
9930f7d4548SAndrew Thompson 
9940f7d4548SAndrew Thompson 		/*
9950f7d4548SAndrew Thompson 		 * Make sure that the OHCI re-scans the schedule by
9960f7d4548SAndrew Thompson 		 * writing the BLF and CLF bits:
9970f7d4548SAndrew Thompson 		 */
9980f7d4548SAndrew Thompson 
999ec8f3127SAndrew Thompson 		if (xfer->xroot->udev->flags.self_suspended) {
10000f7d4548SAndrew Thompson 			/* nothing to do */
1001ae60fdfbSAndrew Thompson 		} else if (xfer->endpoint->methods == &ohci_device_bulk_methods) {
10020f7d4548SAndrew Thompson 			ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
10030f7d4548SAndrew Thompson 
10040f7d4548SAndrew Thompson 			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
1005ae60fdfbSAndrew Thompson 		} else if (xfer->endpoint->methods == &ohci_device_ctrl_methods) {
10060f7d4548SAndrew Thompson 			ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
10070f7d4548SAndrew Thompson 
10080f7d4548SAndrew Thompson 			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
10090f7d4548SAndrew Thompson 		}
101002ac6454SAndrew Thompson 	}
101102ac6454SAndrew Thompson }
101202ac6454SAndrew Thompson 
101302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
101402ac6454SAndrew Thompson  *	ohci_check_transfer
101502ac6454SAndrew Thompson  *
101602ac6454SAndrew Thompson  * Return values:
101702ac6454SAndrew Thompson  *    0: USB transfer is not finished
101802ac6454SAndrew Thompson  * Else: USB transfer is finished
101902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
102002ac6454SAndrew Thompson static uint8_t
ohci_check_transfer(struct usb_xfer * xfer)1021760bc48eSAndrew Thompson ohci_check_transfer(struct usb_xfer *xfer)
102202ac6454SAndrew Thompson {
102302ac6454SAndrew Thompson 	ohci_ed_t *ed;
102402ac6454SAndrew Thompson 	uint32_t ed_headp;
102502ac6454SAndrew Thompson 	uint32_t ed_tailp;
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
102802ac6454SAndrew Thompson 
102902ac6454SAndrew Thompson 	ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
103002ac6454SAndrew Thompson 
1031a593f6b8SAndrew Thompson 	usb_pc_cpu_invalidate(ed->page_cache);
103202ac6454SAndrew Thompson 	ed_headp = le32toh(ed->ed_headp);
103302ac6454SAndrew Thompson 	ed_tailp = le32toh(ed->ed_tailp);
103402ac6454SAndrew Thompson 
10350f7d4548SAndrew Thompson 	if ((ed_headp & OHCI_HALTED) ||
103602ac6454SAndrew Thompson 	    (((ed_headp ^ ed_tailp) & (~0xF)) == 0)) {
1037ae60fdfbSAndrew Thompson 		if (xfer->endpoint->methods == &ohci_device_isoc_methods) {
103802ac6454SAndrew Thompson 			/* isochronous transfer */
103902ac6454SAndrew Thompson 			ohci_isoc_done(xfer);
104002ac6454SAndrew Thompson 		} else {
104102ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
104202ac6454SAndrew Thompson 				ohci_check_transfer_sub(xfer);
104302ac6454SAndrew Thompson 				if (xfer->td_transfer_cache) {
104402ac6454SAndrew Thompson 					/* not finished yet */
104502ac6454SAndrew Thompson 					return (0);
104602ac6454SAndrew Thompson 				}
104702ac6454SAndrew Thompson 			}
104802ac6454SAndrew Thompson 			/* store data-toggle */
104902ac6454SAndrew Thompson 			if (ed_headp & OHCI_TOGGLECARRY) {
1050ae60fdfbSAndrew Thompson 				xfer->endpoint->toggle_next = 1;
105102ac6454SAndrew Thompson 			} else {
1052ae60fdfbSAndrew Thompson 				xfer->endpoint->toggle_next = 0;
105302ac6454SAndrew Thompson 			}
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson 			/* non-isochronous transfer */
105602ac6454SAndrew Thompson 			ohci_non_isoc_done(xfer);
105702ac6454SAndrew Thompson 		}
105802ac6454SAndrew Thompson 		return (1);
105902ac6454SAndrew Thompson 	}
106002ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
106102ac6454SAndrew Thompson 	return (0);
106202ac6454SAndrew Thompson }
106302ac6454SAndrew Thompson 
106402ac6454SAndrew Thompson static void
ohci_rhsc_enable(ohci_softc_t * sc)106502ac6454SAndrew Thompson ohci_rhsc_enable(ohci_softc_t *sc)
106602ac6454SAndrew Thompson {
106702ac6454SAndrew Thompson 	DPRINTFN(5, "\n");
106802ac6454SAndrew Thompson 
106902ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
107002ac6454SAndrew Thompson 
107102ac6454SAndrew Thompson 	sc->sc_eintrs |= OHCI_RHSC;
107202ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
107302ac6454SAndrew Thompson 
107402ac6454SAndrew Thompson 	/* acknowledge any RHSC interrupt */
107502ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_INTERRUPT_STATUS, OHCI_RHSC);
107602ac6454SAndrew Thompson 
107739307315SAndrew Thompson 	ohci_root_intr(sc);
107802ac6454SAndrew Thompson }
107902ac6454SAndrew Thompson 
108002ac6454SAndrew Thompson static void
ohci_interrupt_poll(ohci_softc_t * sc)108102ac6454SAndrew Thompson ohci_interrupt_poll(ohci_softc_t *sc)
108202ac6454SAndrew Thompson {
1083760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
108402ac6454SAndrew Thompson 
108502ac6454SAndrew Thompson repeat:
108602ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
108702ac6454SAndrew Thompson 		/*
108802ac6454SAndrew Thompson 		 * check if transfer is transferred
108902ac6454SAndrew Thompson 		 */
109002ac6454SAndrew Thompson 		if (ohci_check_transfer(xfer)) {
109102ac6454SAndrew Thompson 			/* queue has been modified */
109202ac6454SAndrew Thompson 			goto repeat;
109302ac6454SAndrew Thompson 		}
109402ac6454SAndrew Thompson 	}
109502ac6454SAndrew Thompson }
109602ac6454SAndrew Thompson 
109702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
109802ac6454SAndrew Thompson  *	ohci_interrupt - OHCI interrupt handler
109902ac6454SAndrew Thompson  *
110002ac6454SAndrew Thompson  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
110102ac6454SAndrew Thompson  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
110202ac6454SAndrew Thompson  * is present !
110302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
110402ac6454SAndrew Thompson void
ohci_interrupt(ohci_softc_t * sc)110502ac6454SAndrew Thompson ohci_interrupt(ohci_softc_t *sc)
110602ac6454SAndrew Thompson {
110702ac6454SAndrew Thompson 	struct ohci_hcca *hcca;
110802ac6454SAndrew Thompson 	uint32_t status;
110902ac6454SAndrew Thompson 	uint32_t done;
111002ac6454SAndrew Thompson 
111102ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
111202ac6454SAndrew Thompson 
111302ac6454SAndrew Thompson 	hcca = ohci_get_hcca(sc);
111402ac6454SAndrew Thompson 
111502ac6454SAndrew Thompson 	DPRINTFN(16, "real interrupt\n");
111602ac6454SAndrew Thompson 
1117ed6d949aSAndrew Thompson #ifdef USB_DEBUG
111802ac6454SAndrew Thompson 	if (ohcidebug > 15) {
111902ac6454SAndrew Thompson 		ohci_dumpregs(sc);
112002ac6454SAndrew Thompson 	}
112102ac6454SAndrew Thompson #endif
112202ac6454SAndrew Thompson 
112302ac6454SAndrew Thompson 	done = le32toh(hcca->hcca_done_head);
112402ac6454SAndrew Thompson 
112502ac6454SAndrew Thompson 	/*
112602ac6454SAndrew Thompson 	 * The LSb of done is used to inform the HC Driver that an interrupt
112702ac6454SAndrew Thompson 	 * condition exists for both the Done list and for another event
112802ac6454SAndrew Thompson 	 * recorded in HcInterruptStatus. On an interrupt from the HC, the
112902ac6454SAndrew Thompson 	 * HC Driver checks the HccaDoneHead Value. If this value is 0, then
113002ac6454SAndrew Thompson 	 * the interrupt was caused by other than the HccaDoneHead update
113102ac6454SAndrew Thompson 	 * and the HcInterruptStatus register needs to be accessed to
113202ac6454SAndrew Thompson 	 * determine that exact interrupt cause. If HccaDoneHead is nonzero,
113302ac6454SAndrew Thompson 	 * then a Done list update interrupt is indicated and if the LSb of
113402ac6454SAndrew Thompson 	 * done is nonzero, then an additional interrupt event is indicated
113502ac6454SAndrew Thompson 	 * and HcInterruptStatus should be checked to determine its cause.
113602ac6454SAndrew Thompson 	 */
113702ac6454SAndrew Thompson 	if (done != 0) {
113802ac6454SAndrew Thompson 		status = 0;
113902ac6454SAndrew Thompson 
114002ac6454SAndrew Thompson 		if (done & ~OHCI_DONE_INTRS) {
114102ac6454SAndrew Thompson 			status |= OHCI_WDH;
114202ac6454SAndrew Thompson 		}
114302ac6454SAndrew Thompson 		if (done & OHCI_DONE_INTRS) {
114402ac6454SAndrew Thompson 			status |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
114502ac6454SAndrew Thompson 		}
114602ac6454SAndrew Thompson 		hcca->hcca_done_head = 0;
114702ac6454SAndrew Thompson 
1148a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(&sc->sc_hw.hcca_pc);
114902ac6454SAndrew Thompson 	} else {
115002ac6454SAndrew Thompson 		status = OREAD4(sc, OHCI_INTERRUPT_STATUS) & ~OHCI_WDH;
115102ac6454SAndrew Thompson 	}
115202ac6454SAndrew Thompson 
115302ac6454SAndrew Thompson 	status &= ~OHCI_MIE;
115402ac6454SAndrew Thompson 	if (status == 0) {
115502ac6454SAndrew Thompson 		/*
115602ac6454SAndrew Thompson 		 * nothing to be done (PCI shared
115702ac6454SAndrew Thompson 		 * interrupt)
115802ac6454SAndrew Thompson 		 */
115902ac6454SAndrew Thompson 		goto done;
116002ac6454SAndrew Thompson 	}
116102ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_INTERRUPT_STATUS, status);	/* Acknowledge */
116202ac6454SAndrew Thompson 
116302ac6454SAndrew Thompson 	status &= sc->sc_eintrs;
116402ac6454SAndrew Thompson 	if (status == 0) {
116502ac6454SAndrew Thompson 		goto done;
116602ac6454SAndrew Thompson 	}
116702ac6454SAndrew Thompson 	if (status & (OHCI_SO | OHCI_RD | OHCI_UE | OHCI_RHSC)) {
116802ac6454SAndrew Thompson #if 0
116902ac6454SAndrew Thompson 		if (status & OHCI_SO) {
117002ac6454SAndrew Thompson 			/* XXX do what */
117102ac6454SAndrew Thompson 		}
117202ac6454SAndrew Thompson #endif
117302ac6454SAndrew Thompson 		if (status & OHCI_RD) {
117402ac6454SAndrew Thompson 			printf("%s: resume detect\n", __FUNCTION__);
117502ac6454SAndrew Thompson 			/* XXX process resume detect */
117602ac6454SAndrew Thompson 		}
117702ac6454SAndrew Thompson 		if (status & OHCI_UE) {
117802ac6454SAndrew Thompson 			printf("%s: unrecoverable error, "
117902ac6454SAndrew Thompson 			    "controller halted\n", __FUNCTION__);
118002ac6454SAndrew Thompson 			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
118102ac6454SAndrew Thompson 			/* XXX what else */
118202ac6454SAndrew Thompson 		}
118302ac6454SAndrew Thompson 		if (status & OHCI_RHSC) {
118402ac6454SAndrew Thompson 			/*
118502ac6454SAndrew Thompson 			 * Disable RHSC interrupt for now, because it will be
118602ac6454SAndrew Thompson 			 * on until the port has been reset.
118702ac6454SAndrew Thompson 			 */
118802ac6454SAndrew Thompson 			sc->sc_eintrs &= ~OHCI_RHSC;
118902ac6454SAndrew Thompson 			OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
119002ac6454SAndrew Thompson 
119139307315SAndrew Thompson 			ohci_root_intr(sc);
119202ac6454SAndrew Thompson 
119302ac6454SAndrew Thompson 			/* do not allow RHSC interrupts > 1 per second */
1194a593f6b8SAndrew Thompson 			usb_callout_reset(&sc->sc_tmo_rhsc, hz,
119502ac6454SAndrew Thompson 			    (void *)&ohci_rhsc_enable, sc);
119602ac6454SAndrew Thompson 		}
119702ac6454SAndrew Thompson 	}
119802ac6454SAndrew Thompson 	status &= ~(OHCI_RHSC | OHCI_WDH | OHCI_SO);
119902ac6454SAndrew Thompson 	if (status != 0) {
120002ac6454SAndrew Thompson 		/* Block unprocessed interrupts. XXX */
120102ac6454SAndrew Thompson 		OWRITE4(sc, OHCI_INTERRUPT_DISABLE, status);
120202ac6454SAndrew Thompson 		sc->sc_eintrs &= ~status;
120302ac6454SAndrew Thompson 		printf("%s: blocking intrs 0x%x\n",
120402ac6454SAndrew Thompson 		    __FUNCTION__, status);
120502ac6454SAndrew Thompson 	}
120602ac6454SAndrew Thompson 	/* poll all the USB transfers */
120702ac6454SAndrew Thompson 	ohci_interrupt_poll(sc);
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson done:
121002ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
121102ac6454SAndrew Thompson }
121202ac6454SAndrew Thompson 
121302ac6454SAndrew Thompson /*
121402ac6454SAndrew Thompson  * called when a request does not complete
121502ac6454SAndrew Thompson  */
121602ac6454SAndrew Thompson static void
ohci_timeout(void * arg)121702ac6454SAndrew Thompson ohci_timeout(void *arg)
121802ac6454SAndrew Thompson {
1219760bc48eSAndrew Thompson 	struct usb_xfer *xfer = arg;
122002ac6454SAndrew Thompson 
122102ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
122202ac6454SAndrew Thompson 
122302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
122402ac6454SAndrew Thompson 
122502ac6454SAndrew Thompson 	/* transfer is transferred */
122602ac6454SAndrew Thompson 	ohci_device_done(xfer, USB_ERR_TIMEOUT);
122702ac6454SAndrew Thompson }
122802ac6454SAndrew Thompson 
122902ac6454SAndrew Thompson static void
ohci_do_poll(struct usb_bus * bus)1230760bc48eSAndrew Thompson ohci_do_poll(struct usb_bus *bus)
123102ac6454SAndrew Thompson {
123202ac6454SAndrew Thompson 	struct ohci_softc *sc = OHCI_BUS2SC(bus);
123302ac6454SAndrew Thompson 
123402ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
123502ac6454SAndrew Thompson 	ohci_interrupt_poll(sc);
123602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
123702ac6454SAndrew Thompson }
123802ac6454SAndrew Thompson 
123902ac6454SAndrew Thompson static void
ohci_setup_standard_chain_sub(struct ohci_std_temp * temp)124002ac6454SAndrew Thompson ohci_setup_standard_chain_sub(struct ohci_std_temp *temp)
124102ac6454SAndrew Thompson {
1242760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
124302ac6454SAndrew Thompson 	ohci_td_t *td;
124402ac6454SAndrew Thompson 	ohci_td_t *td_next;
124502ac6454SAndrew Thompson 	ohci_td_t *td_alt_next;
124602ac6454SAndrew Thompson 	uint32_t buf_offset;
124702ac6454SAndrew Thompson 	uint32_t average;
124802ac6454SAndrew Thompson 	uint32_t len_old;
124902ac6454SAndrew Thompson 	uint8_t shortpkt_old;
125002ac6454SAndrew Thompson 	uint8_t precompute;
125102ac6454SAndrew Thompson 
125202ac6454SAndrew Thompson 	td_alt_next = NULL;
125302ac6454SAndrew Thompson 	buf_offset = 0;
125402ac6454SAndrew Thompson 	shortpkt_old = temp->shortpkt;
125502ac6454SAndrew Thompson 	len_old = temp->len;
125602ac6454SAndrew Thompson 	precompute = 1;
125702ac6454SAndrew Thompson 
125802ac6454SAndrew Thompson 	/* software is used to detect short incoming transfers */
125902ac6454SAndrew Thompson 
126002ac6454SAndrew Thompson 	if ((temp->td_flags & htole32(OHCI_TD_DP_MASK)) == htole32(OHCI_TD_IN)) {
126102ac6454SAndrew Thompson 		temp->td_flags |= htole32(OHCI_TD_R);
126202ac6454SAndrew Thompson 	} else {
126302ac6454SAndrew Thompson 		temp->td_flags &= ~htole32(OHCI_TD_R);
126402ac6454SAndrew Thompson 	}
126502ac6454SAndrew Thompson 
126602ac6454SAndrew Thompson restart:
126702ac6454SAndrew Thompson 
126802ac6454SAndrew Thompson 	td = temp->td;
126902ac6454SAndrew Thompson 	td_next = temp->td_next;
127002ac6454SAndrew Thompson 
127102ac6454SAndrew Thompson 	while (1) {
127202ac6454SAndrew Thompson 		if (temp->len == 0) {
127302ac6454SAndrew Thompson 			if (temp->shortpkt) {
127402ac6454SAndrew Thompson 				break;
127502ac6454SAndrew Thompson 			}
127602ac6454SAndrew Thompson 			/* send a Zero Length Packet, ZLP, last */
127702ac6454SAndrew Thompson 
127802ac6454SAndrew Thompson 			temp->shortpkt = 1;
127902ac6454SAndrew Thompson 			average = 0;
128002ac6454SAndrew Thompson 
128102ac6454SAndrew Thompson 		} else {
128202ac6454SAndrew Thompson 			average = temp->average;
128302ac6454SAndrew Thompson 
128402ac6454SAndrew Thompson 			if (temp->len < average) {
128502ac6454SAndrew Thompson 				if (temp->len % temp->max_frame_size) {
128602ac6454SAndrew Thompson 					temp->shortpkt = 1;
128702ac6454SAndrew Thompson 				}
128802ac6454SAndrew Thompson 				average = temp->len;
128902ac6454SAndrew Thompson 			}
129002ac6454SAndrew Thompson 		}
129102ac6454SAndrew Thompson 
129202ac6454SAndrew Thompson 		if (td_next == NULL) {
129302ac6454SAndrew Thompson 			panic("%s: out of OHCI transfer descriptors!", __FUNCTION__);
129402ac6454SAndrew Thompson 		}
129502ac6454SAndrew Thompson 		/* get next TD */
129602ac6454SAndrew Thompson 
129702ac6454SAndrew Thompson 		td = td_next;
129802ac6454SAndrew Thompson 		td_next = td->obj_next;
129902ac6454SAndrew Thompson 
130002ac6454SAndrew Thompson 		/* check if we are pre-computing */
130102ac6454SAndrew Thompson 
130202ac6454SAndrew Thompson 		if (precompute) {
130302ac6454SAndrew Thompson 			/* update remaining length */
130402ac6454SAndrew Thompson 
130502ac6454SAndrew Thompson 			temp->len -= average;
130602ac6454SAndrew Thompson 
130702ac6454SAndrew Thompson 			continue;
130802ac6454SAndrew Thompson 		}
130902ac6454SAndrew Thompson 		/* fill out current TD */
131002ac6454SAndrew Thompson 		td->td_flags = temp->td_flags;
131102ac6454SAndrew Thompson 
131202ac6454SAndrew Thompson 		/* the next TD uses TOGGLE_CARRY */
131302ac6454SAndrew Thompson 		temp->td_flags &= ~htole32(OHCI_TD_TOGGLE_MASK);
131402ac6454SAndrew Thompson 
131502ac6454SAndrew Thompson 		if (average == 0) {
1316b3e9a2aaSAndrew Thompson 			/*
1317b3e9a2aaSAndrew Thompson 			 * The buffer start and end phys addresses should be
1318b3e9a2aaSAndrew Thompson 			 * 0x0 for a zero length packet.
1319b3e9a2aaSAndrew Thompson 			 */
132002ac6454SAndrew Thompson 			td->td_cbp = 0;
1321b3e9a2aaSAndrew Thompson 			td->td_be = 0;
132202ac6454SAndrew Thompson 			td->len = 0;
132302ac6454SAndrew Thompson 
132402ac6454SAndrew Thompson 		} else {
1325a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset, &buf_res);
132602ac6454SAndrew Thompson 			td->td_cbp = htole32(buf_res.physaddr);
132702ac6454SAndrew Thompson 			buf_offset += (average - 1);
132802ac6454SAndrew Thompson 
1329a593f6b8SAndrew Thompson 			usbd_get_page(temp->pc, buf_offset, &buf_res);
133002ac6454SAndrew Thompson 			td->td_be = htole32(buf_res.physaddr);
133102ac6454SAndrew Thompson 			buf_offset++;
133202ac6454SAndrew Thompson 
133302ac6454SAndrew Thompson 			td->len = average;
133402ac6454SAndrew Thompson 
133502ac6454SAndrew Thompson 			/* update remaining length */
133602ac6454SAndrew Thompson 
133702ac6454SAndrew Thompson 			temp->len -= average;
133802ac6454SAndrew Thompson 		}
133902ac6454SAndrew Thompson 
134002ac6454SAndrew Thompson 		if ((td_next == td_alt_next) && temp->setup_alt_next) {
134102ac6454SAndrew Thompson 			/* we need to receive these frames one by one ! */
134202ac6454SAndrew Thompson 			td->td_flags &= htole32(~OHCI_TD_INTR_MASK);
134302ac6454SAndrew Thompson 			td->td_flags |= htole32(OHCI_TD_SET_DI(1));
134402ac6454SAndrew Thompson 			td->td_next = htole32(OHCI_TD_NEXT_END);
134502ac6454SAndrew Thompson 		} else {
134602ac6454SAndrew Thompson 			if (td_next) {
134702ac6454SAndrew Thompson 				/* link the current TD with the next one */
134802ac6454SAndrew Thompson 				td->td_next = td_next->td_self;
134902ac6454SAndrew Thompson 			}
135002ac6454SAndrew Thompson 		}
135102ac6454SAndrew Thompson 
135202ac6454SAndrew Thompson 		td->alt_next = td_alt_next;
135302ac6454SAndrew Thompson 
1354a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(td->page_cache);
135502ac6454SAndrew Thompson 	}
135602ac6454SAndrew Thompson 
135702ac6454SAndrew Thompson 	if (precompute) {
135802ac6454SAndrew Thompson 		precompute = 0;
135902ac6454SAndrew Thompson 
136002ac6454SAndrew Thompson 		/* setup alt next pointer, if any */
13610f7d4548SAndrew Thompson 		if (temp->last_frame) {
13620f7d4548SAndrew Thompson 			/* no alternate next */
13630f7d4548SAndrew Thompson 			td_alt_next = NULL;
136402ac6454SAndrew Thompson 		} else {
136502ac6454SAndrew Thompson 			/* we use this field internally */
136602ac6454SAndrew Thompson 			td_alt_next = td_next;
136702ac6454SAndrew Thompson 		}
136802ac6454SAndrew Thompson 
136902ac6454SAndrew Thompson 		/* restore */
137002ac6454SAndrew Thompson 		temp->shortpkt = shortpkt_old;
137102ac6454SAndrew Thompson 		temp->len = len_old;
137202ac6454SAndrew Thompson 		goto restart;
137302ac6454SAndrew Thompson 	}
137402ac6454SAndrew Thompson 	temp->td = td;
137502ac6454SAndrew Thompson 	temp->td_next = td_next;
137602ac6454SAndrew Thompson }
137702ac6454SAndrew Thompson 
137802ac6454SAndrew Thompson static void
ohci_setup_standard_chain(struct usb_xfer * xfer,ohci_ed_t ** ed_last)1379760bc48eSAndrew Thompson ohci_setup_standard_chain(struct usb_xfer *xfer, ohci_ed_t **ed_last)
138002ac6454SAndrew Thompson {
138102ac6454SAndrew Thompson 	struct ohci_std_temp temp;
1382e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
138302ac6454SAndrew Thompson 	ohci_ed_t *ed;
138402ac6454SAndrew Thompson 	ohci_td_t *td;
138502ac6454SAndrew Thompson 	uint32_t ed_flags;
138602ac6454SAndrew Thompson 	uint32_t x;
138702ac6454SAndrew Thompson 
138802ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1389ae60fdfbSAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
1390a593f6b8SAndrew Thompson 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
139102ac6454SAndrew Thompson 
1392578d0effSAndrew Thompson 	temp.average = xfer->max_hc_frame_size;
139302ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
139402ac6454SAndrew Thompson 
139502ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
139602ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
139702ac6454SAndrew Thompson 
139802ac6454SAndrew Thompson 	/* get next DMA set */
139902ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
140002ac6454SAndrew Thompson 
140102ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
140202ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
140302ac6454SAndrew Thompson 
140402ac6454SAndrew Thompson 	temp.td = NULL;
140502ac6454SAndrew Thompson 	temp.td_next = td;
14060f7d4548SAndrew Thompson 	temp.last_frame = 0;
140702ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
140802ac6454SAndrew Thompson 
1409ae60fdfbSAndrew Thompson 	methods = xfer->endpoint->methods;
141002ac6454SAndrew Thompson 
141102ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
141202ac6454SAndrew Thompson 
141302ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
141402ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
141502ac6454SAndrew Thompson 			temp.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
141602ac6454SAndrew Thompson 			    OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
141702ac6454SAndrew Thompson 
141802ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
141902ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + 0;
142002ac6454SAndrew Thompson 			temp.shortpkt = temp.len ? 1 : 0;
14210f7d4548SAndrew Thompson 			/* check for last frame */
14220f7d4548SAndrew Thompson 			if (xfer->nframes == 1) {
14230f7d4548SAndrew Thompson 				/* no STATUS stage yet, SETUP is last */
14240f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
14250f7d4548SAndrew Thompson 					temp.last_frame = 1;
14260f7d4548SAndrew Thompson 					temp.setup_alt_next = 0;
14270f7d4548SAndrew Thompson 				}
14280f7d4548SAndrew Thompson 			}
142902ac6454SAndrew Thompson 			ohci_setup_standard_chain_sub(&temp);
143002ac6454SAndrew Thompson 
143102ac6454SAndrew Thompson 			/*
143202ac6454SAndrew Thompson 			 * XXX assume that the setup message is
143302ac6454SAndrew Thompson 			 * contained within one USB packet:
143402ac6454SAndrew Thompson 			 */
1435ae60fdfbSAndrew Thompson 			xfer->endpoint->toggle_next = 1;
143602ac6454SAndrew Thompson 		}
143702ac6454SAndrew Thompson 		x = 1;
143802ac6454SAndrew Thompson 	} else {
143902ac6454SAndrew Thompson 		x = 0;
144002ac6454SAndrew Thompson 	}
144102ac6454SAndrew Thompson 	temp.td_flags = htole32(OHCI_TD_NOCC | OHCI_TD_NOINTR);
144202ac6454SAndrew Thompson 
144302ac6454SAndrew Thompson 	/* set data toggle */
144402ac6454SAndrew Thompson 
1445ae60fdfbSAndrew Thompson 	if (xfer->endpoint->toggle_next) {
144602ac6454SAndrew Thompson 		temp.td_flags |= htole32(OHCI_TD_TOGGLE_1);
144702ac6454SAndrew Thompson 	} else {
144802ac6454SAndrew Thompson 		temp.td_flags |= htole32(OHCI_TD_TOGGLE_0);
144902ac6454SAndrew Thompson 	}
145002ac6454SAndrew Thompson 
145102ac6454SAndrew Thompson 	/* set endpoint direction */
145202ac6454SAndrew Thompson 
1453ae60fdfbSAndrew Thompson 	if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
145402ac6454SAndrew Thompson 		temp.td_flags |= htole32(OHCI_TD_IN);
145502ac6454SAndrew Thompson 	} else {
145602ac6454SAndrew Thompson 		temp.td_flags |= htole32(OHCI_TD_OUT);
145702ac6454SAndrew Thompson 	}
145802ac6454SAndrew Thompson 
145902ac6454SAndrew Thompson 	while (x != xfer->nframes) {
146002ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
146102ac6454SAndrew Thompson 
146202ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
146302ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
146402ac6454SAndrew Thompson 
146502ac6454SAndrew Thompson 		x++;
146602ac6454SAndrew Thompson 
146702ac6454SAndrew Thompson 		if (x == xfer->nframes) {
14680f7d4548SAndrew Thompson 			if (xfer->flags_int.control_xfr) {
14690f7d4548SAndrew Thompson 				/* no STATUS stage yet, DATA is last */
14700f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
14710f7d4548SAndrew Thompson 					temp.last_frame = 1;
147202ac6454SAndrew Thompson 					temp.setup_alt_next = 0;
147302ac6454SAndrew Thompson 				}
14740f7d4548SAndrew Thompson 			} else {
14750f7d4548SAndrew Thompson 				temp.last_frame = 1;
14760f7d4548SAndrew Thompson 				temp.setup_alt_next = 0;
14770f7d4548SAndrew Thompson 			}
14780f7d4548SAndrew Thompson 		}
147902ac6454SAndrew Thompson 		if (temp.len == 0) {
148002ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
148102ac6454SAndrew Thompson 
148202ac6454SAndrew Thompson 			temp.shortpkt = 0;
148302ac6454SAndrew Thompson 
148402ac6454SAndrew Thompson 		} else {
148502ac6454SAndrew Thompson 			/* regular data transfer */
148602ac6454SAndrew Thompson 
148702ac6454SAndrew Thompson 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
148802ac6454SAndrew Thompson 		}
148902ac6454SAndrew Thompson 
149002ac6454SAndrew Thompson 		ohci_setup_standard_chain_sub(&temp);
149102ac6454SAndrew Thompson 	}
149202ac6454SAndrew Thompson 
149302ac6454SAndrew Thompson 	/* check if we should append a status stage */
149402ac6454SAndrew Thompson 
149502ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
149602ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
149702ac6454SAndrew Thompson 		/*
149802ac6454SAndrew Thompson 		 * Send a DATA1 message and invert the current endpoint
149902ac6454SAndrew Thompson 		 * direction.
150002ac6454SAndrew Thompson 		 */
150102ac6454SAndrew Thompson 
150202ac6454SAndrew Thompson 		/* set endpoint direction and data toggle */
150302ac6454SAndrew Thompson 
1504ae60fdfbSAndrew Thompson 		if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
150502ac6454SAndrew Thompson 			temp.td_flags = htole32(OHCI_TD_OUT |
150602ac6454SAndrew Thompson 			    OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
150702ac6454SAndrew Thompson 		} else {
150802ac6454SAndrew Thompson 			temp.td_flags = htole32(OHCI_TD_IN |
150902ac6454SAndrew Thompson 			    OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
151002ac6454SAndrew Thompson 		}
151102ac6454SAndrew Thompson 
151202ac6454SAndrew Thompson 		temp.len = 0;
151302ac6454SAndrew Thompson 		temp.pc = NULL;
151402ac6454SAndrew Thompson 		temp.shortpkt = 0;
15150f7d4548SAndrew Thompson 		temp.last_frame = 1;
15160f7d4548SAndrew Thompson 		temp.setup_alt_next = 0;
151702ac6454SAndrew Thompson 
151802ac6454SAndrew Thompson 		ohci_setup_standard_chain_sub(&temp);
151902ac6454SAndrew Thompson 	}
152002ac6454SAndrew Thompson 	td = temp.td;
152102ac6454SAndrew Thompson 
15220f7d4548SAndrew Thompson 	/* Ensure that last TD is terminating: */
152302ac6454SAndrew Thompson 	td->td_next = htole32(OHCI_TD_NEXT_END);
152402ac6454SAndrew Thompson 	td->td_flags &= ~htole32(OHCI_TD_INTR_MASK);
152502ac6454SAndrew Thompson 	td->td_flags |= htole32(OHCI_TD_SET_DI(1));
152602ac6454SAndrew Thompson 
1527a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(td->page_cache);
152802ac6454SAndrew Thompson 
152902ac6454SAndrew Thompson 	/* must have at least one frame! */
153002ac6454SAndrew Thompson 
153102ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
153202ac6454SAndrew Thompson 
1533ed6d949aSAndrew Thompson #ifdef USB_DEBUG
153402ac6454SAndrew Thompson 	if (ohcidebug > 8) {
153502ac6454SAndrew Thompson 		DPRINTF("nexttog=%d; data before transfer:\n",
1536ae60fdfbSAndrew Thompson 		    xfer->endpoint->toggle_next);
153702ac6454SAndrew Thompson 		ohci_dump_tds(xfer->td_transfer_first);
153802ac6454SAndrew Thompson 	}
153902ac6454SAndrew Thompson #endif
154002ac6454SAndrew Thompson 
154102ac6454SAndrew Thompson 	ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
154202ac6454SAndrew Thompson 
154302ac6454SAndrew Thompson 	ed_flags = (OHCI_ED_SET_FA(xfer->address) |
1544ae60fdfbSAndrew Thompson 	    OHCI_ED_SET_EN(UE_GET_ADDR(xfer->endpointno)) |
154502ac6454SAndrew Thompson 	    OHCI_ED_SET_MAXP(xfer->max_frame_size));
154602ac6454SAndrew Thompson 
154702ac6454SAndrew Thompson 	ed_flags |= (OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD);
154802ac6454SAndrew Thompson 
154902ac6454SAndrew Thompson 	if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
155002ac6454SAndrew Thompson 		ed_flags |= OHCI_ED_SPEED;
155102ac6454SAndrew Thompson 	}
155202ac6454SAndrew Thompson 	ed->ed_flags = htole32(ed_flags);
155302ac6454SAndrew Thompson 
155402ac6454SAndrew Thompson 	td = xfer->td_transfer_first;
155502ac6454SAndrew Thompson 
155602ac6454SAndrew Thompson 	ed->ed_headp = td->td_self;
155702ac6454SAndrew Thompson 
1558ec8f3127SAndrew Thompson 	if (xfer->xroot->udev->flags.self_suspended == 0) {
155902ac6454SAndrew Thompson 		/* the append function will flush the endpoint descriptor */
156002ac6454SAndrew Thompson 		OHCI_APPEND_QH(ed, *ed_last);
156102ac6454SAndrew Thompson 
156202ac6454SAndrew Thompson 		if (methods == &ohci_device_bulk_methods) {
156302ac6454SAndrew Thompson 			ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
156402ac6454SAndrew Thompson 
156502ac6454SAndrew Thompson 			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
156602ac6454SAndrew Thompson 		}
156702ac6454SAndrew Thompson 		if (methods == &ohci_device_ctrl_methods) {
156802ac6454SAndrew Thompson 			ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
156902ac6454SAndrew Thompson 
157002ac6454SAndrew Thompson 			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
157102ac6454SAndrew Thompson 		}
157202ac6454SAndrew Thompson 	} else {
1573a593f6b8SAndrew Thompson 		usb_pc_cpu_flush(ed->page_cache);
157402ac6454SAndrew Thompson 	}
157502ac6454SAndrew Thompson }
157602ac6454SAndrew Thompson 
157702ac6454SAndrew Thompson static void
ohci_root_intr(ohci_softc_t * sc)157839307315SAndrew Thompson ohci_root_intr(ohci_softc_t *sc)
157902ac6454SAndrew Thompson {
1580ffd8101eSJohn Baldwin 	uint32_t hstatus __usbdebug_used;
158102ac6454SAndrew Thompson 	uint16_t i;
158202ac6454SAndrew Thompson 	uint16_t m;
158302ac6454SAndrew Thompson 
158402ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
158502ac6454SAndrew Thompson 
158602ac6454SAndrew Thompson 	/* clear any old interrupt data */
158739307315SAndrew Thompson 	memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
158802ac6454SAndrew Thompson 
158902ac6454SAndrew Thompson 	hstatus = OREAD4(sc, OHCI_RH_STATUS);
159039307315SAndrew Thompson 	DPRINTF("sc=%p hstatus=0x%08x\n",
159139307315SAndrew Thompson 	    sc, hstatus);
159202ac6454SAndrew Thompson 
159302ac6454SAndrew Thompson 	/* set bits */
159402ac6454SAndrew Thompson 	m = (sc->sc_noport + 1);
159502ac6454SAndrew Thompson 	if (m > (8 * sizeof(sc->sc_hub_idata))) {
159602ac6454SAndrew Thompson 		m = (8 * sizeof(sc->sc_hub_idata));
159702ac6454SAndrew Thompson 	}
159802ac6454SAndrew Thompson 	for (i = 1; i < m; i++) {
159902ac6454SAndrew Thompson 		/* pick out CHANGE bits from the status register */
160002ac6454SAndrew Thompson 		if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16) {
160102ac6454SAndrew Thompson 			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
160202ac6454SAndrew Thompson 			DPRINTF("port %d changed\n", i);
160302ac6454SAndrew Thompson 		}
160402ac6454SAndrew Thompson 	}
160539307315SAndrew Thompson 
160639307315SAndrew Thompson 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
160739307315SAndrew Thompson 	    sizeof(sc->sc_hub_idata));
160802ac6454SAndrew Thompson }
160902ac6454SAndrew Thompson 
161002ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row,
161102ac6454SAndrew Thompson  * from close and from interrupt
161202ac6454SAndrew Thompson  */
161302ac6454SAndrew Thompson static void
ohci_device_done(struct usb_xfer * xfer,usb_error_t error)1614e0a69b51SAndrew Thompson ohci_device_done(struct usb_xfer *xfer, usb_error_t error)
161502ac6454SAndrew Thompson {
1616e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods = xfer->endpoint->methods;
161702ac6454SAndrew Thompson 	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
161802ac6454SAndrew Thompson 	ohci_ed_t *ed;
161902ac6454SAndrew Thompson 
162002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
162102ac6454SAndrew Thompson 
1622ae60fdfbSAndrew Thompson 	DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
1623ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint, error);
162402ac6454SAndrew Thompson 
162502ac6454SAndrew Thompson 	ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
162602ac6454SAndrew Thompson 	if (ed) {
1627a593f6b8SAndrew Thompson 		usb_pc_cpu_invalidate(ed->page_cache);
162802ac6454SAndrew Thompson 	}
162902ac6454SAndrew Thompson 	if (methods == &ohci_device_bulk_methods) {
163002ac6454SAndrew Thompson 		OHCI_REMOVE_QH(ed, sc->sc_bulk_p_last);
163102ac6454SAndrew Thompson 	}
163202ac6454SAndrew Thompson 	if (methods == &ohci_device_ctrl_methods) {
163302ac6454SAndrew Thompson 		OHCI_REMOVE_QH(ed, sc->sc_ctrl_p_last);
163402ac6454SAndrew Thompson 	}
163502ac6454SAndrew Thompson 	if (methods == &ohci_device_intr_methods) {
163602ac6454SAndrew Thompson 		OHCI_REMOVE_QH(ed, sc->sc_intr_p_last[xfer->qh_pos]);
163702ac6454SAndrew Thompson 	}
163802ac6454SAndrew Thompson 	if (methods == &ohci_device_isoc_methods) {
163902ac6454SAndrew Thompson 		OHCI_REMOVE_QH(ed, sc->sc_isoc_p_last);
164002ac6454SAndrew Thompson 	}
164102ac6454SAndrew Thompson 	xfer->td_transfer_first = NULL;
164202ac6454SAndrew Thompson 	xfer->td_transfer_last = NULL;
164302ac6454SAndrew Thompson 
164402ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
1645a593f6b8SAndrew Thompson 	usbd_transfer_done(xfer, error);
164602ac6454SAndrew Thompson }
164702ac6454SAndrew Thompson 
164802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
164902ac6454SAndrew Thompson  * ohci bulk support
165002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
165102ac6454SAndrew Thompson static void
ohci_device_bulk_open(struct usb_xfer * xfer)1652760bc48eSAndrew Thompson ohci_device_bulk_open(struct usb_xfer *xfer)
165302ac6454SAndrew Thompson {
165402ac6454SAndrew Thompson 	return;
165502ac6454SAndrew Thompson }
165602ac6454SAndrew Thompson 
165702ac6454SAndrew Thompson static void
ohci_device_bulk_close(struct usb_xfer * xfer)1658760bc48eSAndrew Thompson ohci_device_bulk_close(struct usb_xfer *xfer)
165902ac6454SAndrew Thompson {
166002ac6454SAndrew Thompson 	ohci_device_done(xfer, USB_ERR_CANCELLED);
166102ac6454SAndrew Thompson }
166202ac6454SAndrew Thompson 
166302ac6454SAndrew Thompson static void
ohci_device_bulk_enter(struct usb_xfer * xfer)1664760bc48eSAndrew Thompson ohci_device_bulk_enter(struct usb_xfer *xfer)
166502ac6454SAndrew Thompson {
166602ac6454SAndrew Thompson 	return;
166702ac6454SAndrew Thompson }
166802ac6454SAndrew Thompson 
166902ac6454SAndrew Thompson static void
ohci_device_bulk_start(struct usb_xfer * xfer)1670760bc48eSAndrew Thompson ohci_device_bulk_start(struct usb_xfer *xfer)
167102ac6454SAndrew Thompson {
167202ac6454SAndrew Thompson 	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
167302ac6454SAndrew Thompson 
167402ac6454SAndrew Thompson 	/* setup TD's and QH */
167502ac6454SAndrew Thompson 	ohci_setup_standard_chain(xfer, &sc->sc_bulk_p_last);
167602ac6454SAndrew Thompson 
167702ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
167802ac6454SAndrew Thompson 	ohci_transfer_intr_enqueue(xfer);
167902ac6454SAndrew Thompson }
168002ac6454SAndrew Thompson 
1681e892b3feSHans Petter Selasky static const struct usb_pipe_methods ohci_device_bulk_methods =
168202ac6454SAndrew Thompson {
168302ac6454SAndrew Thompson 	.open = ohci_device_bulk_open,
168402ac6454SAndrew Thompson 	.close = ohci_device_bulk_close,
168502ac6454SAndrew Thompson 	.enter = ohci_device_bulk_enter,
168602ac6454SAndrew Thompson 	.start = ohci_device_bulk_start,
168702ac6454SAndrew Thompson };
168802ac6454SAndrew Thompson 
168902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
169002ac6454SAndrew Thompson  * ohci control support
169102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
169202ac6454SAndrew Thompson static void
ohci_device_ctrl_open(struct usb_xfer * xfer)1693760bc48eSAndrew Thompson ohci_device_ctrl_open(struct usb_xfer *xfer)
169402ac6454SAndrew Thompson {
169502ac6454SAndrew Thompson 	return;
169602ac6454SAndrew Thompson }
169702ac6454SAndrew Thompson 
169802ac6454SAndrew Thompson static void
ohci_device_ctrl_close(struct usb_xfer * xfer)1699760bc48eSAndrew Thompson ohci_device_ctrl_close(struct usb_xfer *xfer)
170002ac6454SAndrew Thompson {
170102ac6454SAndrew Thompson 	ohci_device_done(xfer, USB_ERR_CANCELLED);
170202ac6454SAndrew Thompson }
170302ac6454SAndrew Thompson 
170402ac6454SAndrew Thompson static void
ohci_device_ctrl_enter(struct usb_xfer * xfer)1705760bc48eSAndrew Thompson ohci_device_ctrl_enter(struct usb_xfer *xfer)
170602ac6454SAndrew Thompson {
170702ac6454SAndrew Thompson 	return;
170802ac6454SAndrew Thompson }
170902ac6454SAndrew Thompson 
171002ac6454SAndrew Thompson static void
ohci_device_ctrl_start(struct usb_xfer * xfer)1711760bc48eSAndrew Thompson ohci_device_ctrl_start(struct usb_xfer *xfer)
171202ac6454SAndrew Thompson {
171302ac6454SAndrew Thompson 	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
171402ac6454SAndrew Thompson 
171502ac6454SAndrew Thompson 	/* setup TD's and QH */
171602ac6454SAndrew Thompson 	ohci_setup_standard_chain(xfer, &sc->sc_ctrl_p_last);
171702ac6454SAndrew Thompson 
171802ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
171902ac6454SAndrew Thompson 	ohci_transfer_intr_enqueue(xfer);
172002ac6454SAndrew Thompson }
172102ac6454SAndrew Thompson 
1722e892b3feSHans Petter Selasky static const struct usb_pipe_methods ohci_device_ctrl_methods =
172302ac6454SAndrew Thompson {
172402ac6454SAndrew Thompson 	.open = ohci_device_ctrl_open,
172502ac6454SAndrew Thompson 	.close = ohci_device_ctrl_close,
172602ac6454SAndrew Thompson 	.enter = ohci_device_ctrl_enter,
172702ac6454SAndrew Thompson 	.start = ohci_device_ctrl_start,
172802ac6454SAndrew Thompson };
172902ac6454SAndrew Thompson 
173002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
173102ac6454SAndrew Thompson  * ohci interrupt support
173202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
173302ac6454SAndrew Thompson static void
ohci_device_intr_open(struct usb_xfer * xfer)1734760bc48eSAndrew Thompson ohci_device_intr_open(struct usb_xfer *xfer)
173502ac6454SAndrew Thompson {
173602ac6454SAndrew Thompson 	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
173702ac6454SAndrew Thompson 	uint16_t best;
173802ac6454SAndrew Thompson 	uint16_t bit;
173902ac6454SAndrew Thompson 	uint16_t x;
174002ac6454SAndrew Thompson 
174102ac6454SAndrew Thompson 	best = 0;
174202ac6454SAndrew Thompson 	bit = OHCI_NO_EDS / 2;
174302ac6454SAndrew Thompson 	while (bit) {
174402ac6454SAndrew Thompson 		if (xfer->interval >= bit) {
174502ac6454SAndrew Thompson 			x = bit;
174602ac6454SAndrew Thompson 			best = bit;
174702ac6454SAndrew Thompson 			while (x & bit) {
174802ac6454SAndrew Thompson 				if (sc->sc_intr_stat[x] <
174902ac6454SAndrew Thompson 				    sc->sc_intr_stat[best]) {
175002ac6454SAndrew Thompson 					best = x;
175102ac6454SAndrew Thompson 				}
175202ac6454SAndrew Thompson 				x++;
175302ac6454SAndrew Thompson 			}
175402ac6454SAndrew Thompson 			break;
175502ac6454SAndrew Thompson 		}
175602ac6454SAndrew Thompson 		bit >>= 1;
175702ac6454SAndrew Thompson 	}
175802ac6454SAndrew Thompson 
175902ac6454SAndrew Thompson 	sc->sc_intr_stat[best]++;
176002ac6454SAndrew Thompson 	xfer->qh_pos = best;
176102ac6454SAndrew Thompson 
176202ac6454SAndrew Thompson 	DPRINTFN(3, "best=%d interval=%d\n",
176302ac6454SAndrew Thompson 	    best, xfer->interval);
176402ac6454SAndrew Thompson }
176502ac6454SAndrew Thompson 
176602ac6454SAndrew Thompson static void
ohci_device_intr_close(struct usb_xfer * xfer)1767760bc48eSAndrew Thompson ohci_device_intr_close(struct usb_xfer *xfer)
176802ac6454SAndrew Thompson {
176902ac6454SAndrew Thompson 	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
177002ac6454SAndrew Thompson 
177102ac6454SAndrew Thompson 	sc->sc_intr_stat[xfer->qh_pos]--;
177202ac6454SAndrew Thompson 
177302ac6454SAndrew Thompson 	ohci_device_done(xfer, USB_ERR_CANCELLED);
177402ac6454SAndrew Thompson }
177502ac6454SAndrew Thompson 
177602ac6454SAndrew Thompson static void
ohci_device_intr_enter(struct usb_xfer * xfer)1777760bc48eSAndrew Thompson ohci_device_intr_enter(struct usb_xfer *xfer)
177802ac6454SAndrew Thompson {
177902ac6454SAndrew Thompson 	return;
178002ac6454SAndrew Thompson }
178102ac6454SAndrew Thompson 
178202ac6454SAndrew Thompson static void
ohci_device_intr_start(struct usb_xfer * xfer)1783760bc48eSAndrew Thompson ohci_device_intr_start(struct usb_xfer *xfer)
178402ac6454SAndrew Thompson {
178502ac6454SAndrew Thompson 	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
178602ac6454SAndrew Thompson 
178702ac6454SAndrew Thompson 	/* setup TD's and QH */
178802ac6454SAndrew Thompson 	ohci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
178902ac6454SAndrew Thompson 
179002ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
179102ac6454SAndrew Thompson 	ohci_transfer_intr_enqueue(xfer);
179202ac6454SAndrew Thompson }
179302ac6454SAndrew Thompson 
1794e892b3feSHans Petter Selasky static const struct usb_pipe_methods ohci_device_intr_methods =
179502ac6454SAndrew Thompson {
179602ac6454SAndrew Thompson 	.open = ohci_device_intr_open,
179702ac6454SAndrew Thompson 	.close = ohci_device_intr_close,
179802ac6454SAndrew Thompson 	.enter = ohci_device_intr_enter,
179902ac6454SAndrew Thompson 	.start = ohci_device_intr_start,
180002ac6454SAndrew Thompson };
180102ac6454SAndrew Thompson 
180202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
180302ac6454SAndrew Thompson  * ohci isochronous support
180402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
180502ac6454SAndrew Thompson static void
ohci_device_isoc_open(struct usb_xfer * xfer)1806760bc48eSAndrew Thompson ohci_device_isoc_open(struct usb_xfer *xfer)
180702ac6454SAndrew Thompson {
180802ac6454SAndrew Thompson 	return;
180902ac6454SAndrew Thompson }
181002ac6454SAndrew Thompson 
181102ac6454SAndrew Thompson static void
ohci_device_isoc_close(struct usb_xfer * xfer)1812760bc48eSAndrew Thompson ohci_device_isoc_close(struct usb_xfer *xfer)
181302ac6454SAndrew Thompson {
181402ac6454SAndrew Thompson 	/**/
181502ac6454SAndrew Thompson 	ohci_device_done(xfer, USB_ERR_CANCELLED);
181602ac6454SAndrew Thompson }
181702ac6454SAndrew Thompson 
181802ac6454SAndrew Thompson static void
ohci_device_isoc_enter(struct usb_xfer * xfer)1819760bc48eSAndrew Thompson ohci_device_isoc_enter(struct usb_xfer *xfer)
182002ac6454SAndrew Thompson {
1821760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
182202ac6454SAndrew Thompson 	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
182302ac6454SAndrew Thompson 	struct ohci_hcca *hcca;
182402ac6454SAndrew Thompson 	uint32_t buf_offset;
182502ac6454SAndrew Thompson 	uint32_t nframes;
18268fc2a3c4SHans Petter Selasky 	uint32_t startframe;
182702ac6454SAndrew Thompson 	uint32_t ed_flags;
182802ac6454SAndrew Thompson 	uint32_t *plen;
182902ac6454SAndrew Thompson 	uint16_t itd_offset[OHCI_ITD_NOFFSET];
183002ac6454SAndrew Thompson 	uint16_t length;
183102ac6454SAndrew Thompson 	uint8_t ncur;
183202ac6454SAndrew Thompson 	ohci_itd_t *td;
183302ac6454SAndrew Thompson 	ohci_itd_t *td_last = NULL;
183402ac6454SAndrew Thompson 	ohci_ed_t *ed;
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson 	hcca = ohci_get_hcca(sc);
183702ac6454SAndrew Thompson 
183802ac6454SAndrew Thompson 	nframes = le32toh(hcca->hcca_frame_number);
183902ac6454SAndrew Thompson 
184002ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p isoc_next=%u nframes=%u hcca_fn=%u\n",
1841ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes, nframes);
184202ac6454SAndrew Thompson 
18438fc2a3c4SHans Petter Selasky 	if (usbd_xfer_get_isochronous_start_frame(
18448fc2a3c4SHans Petter Selasky 	    xfer, nframes, 0, 1, 0xFFFF, &startframe))
18458fc2a3c4SHans Petter Selasky 		DPRINTFN(3, "start next=%d\n", startframe);
184602ac6454SAndrew Thompson 
184702ac6454SAndrew Thompson 	/* get the real number of frames */
184802ac6454SAndrew Thompson 
184902ac6454SAndrew Thompson 	nframes = xfer->nframes;
185002ac6454SAndrew Thompson 
185102ac6454SAndrew Thompson 	buf_offset = 0;
185202ac6454SAndrew Thompson 
185302ac6454SAndrew Thompson 	plen = xfer->frlengths;
185402ac6454SAndrew Thompson 
185502ac6454SAndrew Thompson 	/* toggle the DMA set we are using */
185602ac6454SAndrew Thompson 	xfer->flags_int.curr_dma_set ^= 1;
185702ac6454SAndrew Thompson 
185802ac6454SAndrew Thompson 	/* get next DMA set */
185902ac6454SAndrew Thompson 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
186002ac6454SAndrew Thompson 
186102ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
186202ac6454SAndrew Thompson 
186302ac6454SAndrew Thompson 	ncur = 0;
186402ac6454SAndrew Thompson 	length = 0;
186502ac6454SAndrew Thompson 
186602ac6454SAndrew Thompson 	while (nframes--) {
186702ac6454SAndrew Thompson 		if (td == NULL) {
186802ac6454SAndrew Thompson 			panic("%s:%d: out of TD's\n",
186902ac6454SAndrew Thompson 			    __FUNCTION__, __LINE__);
187002ac6454SAndrew Thompson 		}
187102ac6454SAndrew Thompson 		itd_offset[ncur] = length;
187202ac6454SAndrew Thompson 		buf_offset += *plen;
187302ac6454SAndrew Thompson 		length += *plen;
187402ac6454SAndrew Thompson 		plen++;
187502ac6454SAndrew Thompson 		ncur++;
187602ac6454SAndrew Thompson 
187702ac6454SAndrew Thompson 		if (			/* check if the ITD is full */
187802ac6454SAndrew Thompson 		    (ncur == OHCI_ITD_NOFFSET) ||
187902ac6454SAndrew Thompson 		/* check if we have put more than 4K into the ITD */
188002ac6454SAndrew Thompson 		    (length & 0xF000) ||
188102ac6454SAndrew Thompson 		/* check if it is the last frame */
188202ac6454SAndrew Thompson 		    (nframes == 0)) {
188302ac6454SAndrew Thompson 			/* fill current ITD */
188402ac6454SAndrew Thompson 			td->itd_flags = htole32(
188502ac6454SAndrew Thompson 			    OHCI_ITD_NOCC |
18868fc2a3c4SHans Petter Selasky 			    OHCI_ITD_SET_SF(startframe) |
188702ac6454SAndrew Thompson 			    OHCI_ITD_NOINTR |
188802ac6454SAndrew Thompson 			    OHCI_ITD_SET_FC(ncur));
188902ac6454SAndrew Thompson 
189002ac6454SAndrew Thompson 			td->frames = ncur;
18918fc2a3c4SHans Petter Selasky 			startframe += ncur;
189202ac6454SAndrew Thompson 
189302ac6454SAndrew Thompson 			if (length == 0) {
189402ac6454SAndrew Thompson 				/* all zero */
189502ac6454SAndrew Thompson 				td->itd_bp0 = 0;
189602ac6454SAndrew Thompson 				td->itd_be = ~0;
189702ac6454SAndrew Thompson 
189802ac6454SAndrew Thompson 				while (ncur--) {
189902ac6454SAndrew Thompson 					td->itd_offset[ncur] =
190002ac6454SAndrew Thompson 					    htole16(OHCI_ITD_MK_OFFS(0));
190102ac6454SAndrew Thompson 				}
190202ac6454SAndrew Thompson 			} else {
1903a593f6b8SAndrew Thompson 				usbd_get_page(xfer->frbuffers, buf_offset - length, &buf_res);
190402ac6454SAndrew Thompson 				length = OHCI_PAGE_MASK(buf_res.physaddr);
190502ac6454SAndrew Thompson 				buf_res.physaddr =
190602ac6454SAndrew Thompson 				    OHCI_PAGE(buf_res.physaddr);
190702ac6454SAndrew Thompson 				td->itd_bp0 = htole32(buf_res.physaddr);
1908a593f6b8SAndrew Thompson 				usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
190902ac6454SAndrew Thompson 				td->itd_be = htole32(buf_res.physaddr);
191002ac6454SAndrew Thompson 
191102ac6454SAndrew Thompson 				while (ncur--) {
191202ac6454SAndrew Thompson 					itd_offset[ncur] += length;
191302ac6454SAndrew Thompson 					itd_offset[ncur] =
191402ac6454SAndrew Thompson 					    OHCI_ITD_MK_OFFS(itd_offset[ncur]);
191502ac6454SAndrew Thompson 					td->itd_offset[ncur] =
191602ac6454SAndrew Thompson 					    htole16(itd_offset[ncur]);
191702ac6454SAndrew Thompson 				}
191802ac6454SAndrew Thompson 			}
191902ac6454SAndrew Thompson 			ncur = 0;
192002ac6454SAndrew Thompson 			length = 0;
192102ac6454SAndrew Thompson 			td_last = td;
192202ac6454SAndrew Thompson 			td = td->obj_next;
192302ac6454SAndrew Thompson 
192402ac6454SAndrew Thompson 			if (td) {
192502ac6454SAndrew Thompson 				/* link the last TD with the next one */
192602ac6454SAndrew Thompson 				td_last->itd_next = td->itd_self;
192702ac6454SAndrew Thompson 			}
1928a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(td_last->page_cache);
192902ac6454SAndrew Thompson 		}
193002ac6454SAndrew Thompson 	}
193102ac6454SAndrew Thompson 
193202ac6454SAndrew Thompson 	/* update the last TD */
193302ac6454SAndrew Thompson 	td_last->itd_flags &= ~htole32(OHCI_ITD_NOINTR);
193402ac6454SAndrew Thompson 	td_last->itd_flags |= htole32(OHCI_ITD_SET_DI(0));
193502ac6454SAndrew Thompson 	td_last->itd_next = 0;
193602ac6454SAndrew Thompson 
1937a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(td_last->page_cache);
193802ac6454SAndrew Thompson 
193902ac6454SAndrew Thompson 	xfer->td_transfer_last = td_last;
194002ac6454SAndrew Thompson 
1941ed6d949aSAndrew Thompson #ifdef USB_DEBUG
194202ac6454SAndrew Thompson 	if (ohcidebug > 8) {
194302ac6454SAndrew Thompson 		DPRINTF("data before transfer:\n");
194402ac6454SAndrew Thompson 		ohci_dump_itds(xfer->td_transfer_first);
194502ac6454SAndrew Thompson 	}
194602ac6454SAndrew Thompson #endif
194702ac6454SAndrew Thompson 	ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
194802ac6454SAndrew Thompson 
1949ae60fdfbSAndrew Thompson 	if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
195002ac6454SAndrew Thompson 		ed_flags = (OHCI_ED_DIR_IN | OHCI_ED_FORMAT_ISO);
195102ac6454SAndrew Thompson 	else
195202ac6454SAndrew Thompson 		ed_flags = (OHCI_ED_DIR_OUT | OHCI_ED_FORMAT_ISO);
195302ac6454SAndrew Thompson 
195402ac6454SAndrew Thompson 	ed_flags |= (OHCI_ED_SET_FA(xfer->address) |
1955ae60fdfbSAndrew Thompson 	    OHCI_ED_SET_EN(UE_GET_ADDR(xfer->endpointno)) |
195602ac6454SAndrew Thompson 	    OHCI_ED_SET_MAXP(xfer->max_frame_size));
195702ac6454SAndrew Thompson 
195802ac6454SAndrew Thompson 	if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
195902ac6454SAndrew Thompson 		ed_flags |= OHCI_ED_SPEED;
196002ac6454SAndrew Thompson 	}
196102ac6454SAndrew Thompson 	ed->ed_flags = htole32(ed_flags);
196202ac6454SAndrew Thompson 
196302ac6454SAndrew Thompson 	td = xfer->td_transfer_first;
196402ac6454SAndrew Thompson 
196502ac6454SAndrew Thompson 	ed->ed_headp = td->itd_self;
196602ac6454SAndrew Thompson 
196702ac6454SAndrew Thompson 	/* isochronous transfers are not affected by suspend / resume */
196802ac6454SAndrew Thompson 	/* the append function will flush the endpoint descriptor */
196902ac6454SAndrew Thompson 
197002ac6454SAndrew Thompson 	OHCI_APPEND_QH(ed, sc->sc_isoc_p_last);
197102ac6454SAndrew Thompson }
197202ac6454SAndrew Thompson 
197302ac6454SAndrew Thompson static void
ohci_device_isoc_start(struct usb_xfer * xfer)1974760bc48eSAndrew Thompson ohci_device_isoc_start(struct usb_xfer *xfer)
197502ac6454SAndrew Thompson {
197602ac6454SAndrew Thompson 	/* put transfer on interrupt queue */
197702ac6454SAndrew Thompson 	ohci_transfer_intr_enqueue(xfer);
197802ac6454SAndrew Thompson }
197902ac6454SAndrew Thompson 
1980e892b3feSHans Petter Selasky static const struct usb_pipe_methods ohci_device_isoc_methods =
198102ac6454SAndrew Thompson {
198202ac6454SAndrew Thompson 	.open = ohci_device_isoc_open,
198302ac6454SAndrew Thompson 	.close = ohci_device_isoc_close,
198402ac6454SAndrew Thompson 	.enter = ohci_device_isoc_enter,
198502ac6454SAndrew Thompson 	.start = ohci_device_isoc_start,
198602ac6454SAndrew Thompson };
198702ac6454SAndrew Thompson 
198802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
198902ac6454SAndrew Thompson  * ohci root control support
199002ac6454SAndrew Thompson  *------------------------------------------------------------------------*
199139307315SAndrew Thompson  * Simulate a hardware hub by handling all the necessary requests.
199202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
199302ac6454SAndrew Thompson 
199402ac6454SAndrew Thompson static const
1995760bc48eSAndrew Thompson struct usb_device_descriptor ohci_devd =
199602ac6454SAndrew Thompson {
1997760bc48eSAndrew Thompson 	sizeof(struct usb_device_descriptor),
199802ac6454SAndrew Thompson 	UDESC_DEVICE,			/* type */
199902ac6454SAndrew Thompson 	{0x00, 0x01},			/* USB version */
200002ac6454SAndrew Thompson 	UDCLASS_HUB,			/* class */
200102ac6454SAndrew Thompson 	UDSUBCLASS_HUB,			/* subclass */
200202ac6454SAndrew Thompson 	UDPROTO_FSHUB,			/* protocol */
200302ac6454SAndrew Thompson 	64,				/* max packet */
200402ac6454SAndrew Thompson 	{0}, {0}, {0x00, 0x01},		/* device id */
200520733245SPedro F. Giffuni 	1, 2, 0,			/* string indexes */
200602ac6454SAndrew Thompson 	1				/* # of configurations */
200702ac6454SAndrew Thompson };
200802ac6454SAndrew Thompson 
200902ac6454SAndrew Thompson static const
201002ac6454SAndrew Thompson struct ohci_config_desc ohci_confd =
201102ac6454SAndrew Thompson {
201202ac6454SAndrew Thompson 	.confd = {
2013760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_config_descriptor),
201402ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
201502ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(ohci_confd),
201602ac6454SAndrew Thompson 		.bNumInterface = 1,
201702ac6454SAndrew Thompson 		.bConfigurationValue = 1,
201802ac6454SAndrew Thompson 		.iConfiguration = 0,
201902ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
202002ac6454SAndrew Thompson 		.bMaxPower = 0,		/* max power */
202102ac6454SAndrew Thompson 	},
202202ac6454SAndrew Thompson 	.ifcd = {
2023760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_interface_descriptor),
202402ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
202502ac6454SAndrew Thompson 		.bNumEndpoints = 1,
202602ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
202702ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
20283b6f59eeSHans Petter Selasky 		.bInterfaceProtocol = 0,
202902ac6454SAndrew Thompson 	},
203002ac6454SAndrew Thompson 	.endpd = {
2031760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_endpoint_descriptor),
203202ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
203302ac6454SAndrew Thompson 		.bEndpointAddress = UE_DIR_IN | OHCI_INTR_ENDPT,
203402ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
203502ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 32,/* max packet (255 ports) */
203602ac6454SAndrew Thompson 		.bInterval = 255,
203702ac6454SAndrew Thompson 	},
203802ac6454SAndrew Thompson };
203902ac6454SAndrew Thompson 
204002ac6454SAndrew Thompson static const
2041760bc48eSAndrew Thompson struct usb_hub_descriptor ohci_hubd =
204202ac6454SAndrew Thompson {
20436d917491SHans Petter Selasky 	.bDescLength = 0,	/* dynamic length */
20446d917491SHans Petter Selasky 	.bDescriptorType = UDESC_HUB,
204502ac6454SAndrew Thompson };
204602ac6454SAndrew Thompson 
2047e0a69b51SAndrew Thompson static usb_error_t
ohci_roothub_exec(struct usb_device * udev,struct usb_device_request * req,const void ** pptr,uint16_t * plength)2048760bc48eSAndrew Thompson ohci_roothub_exec(struct usb_device *udev,
2049760bc48eSAndrew Thompson     struct usb_device_request *req, const void **pptr, uint16_t *plength)
205002ac6454SAndrew Thompson {
2051459d369eSAndrew Thompson 	ohci_softc_t *sc = OHCI_BUS2SC(udev->bus);
2052459d369eSAndrew Thompson 	const void *ptr;
2053459d369eSAndrew Thompson 	const char *str_ptr;
205402ac6454SAndrew Thompson 	uint32_t port;
205502ac6454SAndrew Thompson 	uint32_t v;
2056459d369eSAndrew Thompson 	uint16_t len;
205702ac6454SAndrew Thompson 	uint16_t value;
205802ac6454SAndrew Thompson 	uint16_t index;
205902ac6454SAndrew Thompson 	uint8_t l;
2060e0a69b51SAndrew Thompson 	usb_error_t err;
206102ac6454SAndrew Thompson 
206202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
206302ac6454SAndrew Thompson 
206402ac6454SAndrew Thompson 	/* buffer reset */
2065459d369eSAndrew Thompson 	ptr = (const void *)&sc->sc_hub_desc.temp;
2066459d369eSAndrew Thompson 	len = 0;
2067459d369eSAndrew Thompson 	err = 0;
206802ac6454SAndrew Thompson 
2069459d369eSAndrew Thompson 	value = UGETW(req->wValue);
2070459d369eSAndrew Thompson 	index = UGETW(req->wIndex);
207102ac6454SAndrew Thompson 
207202ac6454SAndrew Thompson 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
207302ac6454SAndrew Thompson 	    "wValue=0x%04x wIndex=0x%04x\n",
2074459d369eSAndrew Thompson 	    req->bmRequestType, req->bRequest,
2075459d369eSAndrew Thompson 	    UGETW(req->wLength), value, index);
207602ac6454SAndrew Thompson 
207702ac6454SAndrew Thompson #define	C(x,y) ((x) | ((y) << 8))
2078459d369eSAndrew Thompson 	switch (C(req->bRequest, req->bmRequestType)) {
207902ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
208002ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
208102ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
208202ac6454SAndrew Thompson 		/*
208302ac6454SAndrew Thompson 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
208402ac6454SAndrew Thompson 		 * for the integrated root hub.
208502ac6454SAndrew Thompson 		 */
208602ac6454SAndrew Thompson 		break;
208702ac6454SAndrew Thompson 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
2088459d369eSAndrew Thompson 		len = 1;
208902ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
209002ac6454SAndrew Thompson 		break;
209102ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
209202ac6454SAndrew Thompson 		switch (value >> 8) {
209302ac6454SAndrew Thompson 		case UDESC_DEVICE:
209402ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
2095459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
209602ac6454SAndrew Thompson 				goto done;
209702ac6454SAndrew Thompson 			}
2098459d369eSAndrew Thompson 			len = sizeof(ohci_devd);
2099459d369eSAndrew Thompson 			ptr = (const void *)&ohci_devd;
210002ac6454SAndrew Thompson 			break;
210102ac6454SAndrew Thompson 
210202ac6454SAndrew Thompson 		case UDESC_CONFIG:
210302ac6454SAndrew Thompson 			if ((value & 0xff) != 0) {
2104459d369eSAndrew Thompson 				err = USB_ERR_IOERROR;
210502ac6454SAndrew Thompson 				goto done;
210602ac6454SAndrew Thompson 			}
2107459d369eSAndrew Thompson 			len = sizeof(ohci_confd);
2108459d369eSAndrew Thompson 			ptr = (const void *)&ohci_confd;
210902ac6454SAndrew Thompson 			break;
211002ac6454SAndrew Thompson 
211102ac6454SAndrew Thompson 		case UDESC_STRING:
211202ac6454SAndrew Thompson 			switch (value & 0xff) {
211302ac6454SAndrew Thompson 			case 0:	/* Language table */
2114459d369eSAndrew Thompson 				str_ptr = "\001";
211502ac6454SAndrew Thompson 				break;
211602ac6454SAndrew Thompson 
211702ac6454SAndrew Thompson 			case 1:	/* Vendor */
2118459d369eSAndrew Thompson 				str_ptr = sc->sc_vendor;
211902ac6454SAndrew Thompson 				break;
212002ac6454SAndrew Thompson 
212102ac6454SAndrew Thompson 			case 2:	/* Product */
2122459d369eSAndrew Thompson 				str_ptr = "OHCI root HUB";
212302ac6454SAndrew Thompson 				break;
212402ac6454SAndrew Thompson 
212502ac6454SAndrew Thompson 			default:
2126459d369eSAndrew Thompson 				str_ptr = "";
212702ac6454SAndrew Thompson 				break;
212802ac6454SAndrew Thompson 			}
212902ac6454SAndrew Thompson 
2130a593f6b8SAndrew Thompson 			len = usb_make_str_desc(
2131459d369eSAndrew Thompson 			    sc->sc_hub_desc.temp,
213202ac6454SAndrew Thompson 			    sizeof(sc->sc_hub_desc.temp),
2133459d369eSAndrew Thompson 			    str_ptr);
213402ac6454SAndrew Thompson 			break;
213502ac6454SAndrew Thompson 
213602ac6454SAndrew Thompson 		default:
2137459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
213802ac6454SAndrew Thompson 			goto done;
213902ac6454SAndrew Thompson 		}
214002ac6454SAndrew Thompson 		break;
214102ac6454SAndrew Thompson 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2142459d369eSAndrew Thompson 		len = 1;
214302ac6454SAndrew Thompson 		sc->sc_hub_desc.temp[0] = 0;
214402ac6454SAndrew Thompson 		break;
214502ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_DEVICE):
2146459d369eSAndrew Thompson 		len = 2;
214702ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
214802ac6454SAndrew Thompson 		break;
214902ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
215002ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2151459d369eSAndrew Thompson 		len = 2;
215202ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
215302ac6454SAndrew Thompson 		break;
215402ac6454SAndrew Thompson 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2155ab42e8b2SAndrew Thompson 		if (value >= OHCI_MAX_DEVICES) {
2156459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
215702ac6454SAndrew Thompson 			goto done;
215802ac6454SAndrew Thompson 		}
215902ac6454SAndrew Thompson 		sc->sc_addr = value;
216002ac6454SAndrew Thompson 		break;
216102ac6454SAndrew Thompson 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
216202ac6454SAndrew Thompson 		if ((value != 0) && (value != 1)) {
2163459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
216402ac6454SAndrew Thompson 			goto done;
216502ac6454SAndrew Thompson 		}
216602ac6454SAndrew Thompson 		sc->sc_conf = value;
216702ac6454SAndrew Thompson 		break;
216802ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
216902ac6454SAndrew Thompson 		break;
217002ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
217102ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
217202ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2173459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
217402ac6454SAndrew Thompson 		goto done;
217502ac6454SAndrew Thompson 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
217602ac6454SAndrew Thompson 		break;
217702ac6454SAndrew Thompson 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
217802ac6454SAndrew Thompson 		break;
217902ac6454SAndrew Thompson 		/* Hub requests */
218002ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
218102ac6454SAndrew Thompson 		break;
218202ac6454SAndrew Thompson 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
218302ac6454SAndrew Thompson 		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE "
218402ac6454SAndrew Thompson 		    "port=%d feature=%d\n",
218502ac6454SAndrew Thompson 		    index, value);
218602ac6454SAndrew Thompson 		if ((index < 1) ||
218702ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
2188459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
218902ac6454SAndrew Thompson 			goto done;
219002ac6454SAndrew Thompson 		}
219102ac6454SAndrew Thompson 		port = OHCI_RH_PORT_STATUS(index);
219202ac6454SAndrew Thompson 		switch (value) {
219302ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
219402ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
219502ac6454SAndrew Thompson 			break;
219602ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
219702ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
219802ac6454SAndrew Thompson 			break;
219902ac6454SAndrew Thompson 		case UHF_PORT_POWER:
220002ac6454SAndrew Thompson 			/* Yes, writing to the LOW_SPEED bit clears power. */
220102ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_LOW_SPEED);
220202ac6454SAndrew Thompson 			break;
220302ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
220402ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
220502ac6454SAndrew Thompson 			break;
220602ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
220702ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
220802ac6454SAndrew Thompson 			break;
220902ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
221002ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_C_SUSPEND << 16);
221102ac6454SAndrew Thompson 			break;
221202ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
221302ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
221402ac6454SAndrew Thompson 			break;
221502ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
221602ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
221702ac6454SAndrew Thompson 			break;
221802ac6454SAndrew Thompson 		default:
2219459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
222002ac6454SAndrew Thompson 			goto done;
222102ac6454SAndrew Thompson 		}
222202ac6454SAndrew Thompson 		switch (value) {
222302ac6454SAndrew Thompson 		case UHF_C_PORT_CONNECTION:
222402ac6454SAndrew Thompson 		case UHF_C_PORT_ENABLE:
222502ac6454SAndrew Thompson 		case UHF_C_PORT_SUSPEND:
222602ac6454SAndrew Thompson 		case UHF_C_PORT_OVER_CURRENT:
222702ac6454SAndrew Thompson 		case UHF_C_PORT_RESET:
222802ac6454SAndrew Thompson 			/* enable RHSC interrupt if condition is cleared. */
222902ac6454SAndrew Thompson 			if ((OREAD4(sc, port) >> 16) == 0)
223002ac6454SAndrew Thompson 				ohci_rhsc_enable(sc);
223102ac6454SAndrew Thompson 			break;
223202ac6454SAndrew Thompson 		default:
223302ac6454SAndrew Thompson 			break;
223402ac6454SAndrew Thompson 		}
223502ac6454SAndrew Thompson 		break;
223602ac6454SAndrew Thompson 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
223702ac6454SAndrew Thompson 		if ((value & 0xff) != 0) {
2238459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
223902ac6454SAndrew Thompson 			goto done;
224002ac6454SAndrew Thompson 		}
224102ac6454SAndrew Thompson 		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
224202ac6454SAndrew Thompson 
224302ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd = ohci_hubd;
224402ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
224502ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics,
224602ac6454SAndrew Thompson 		    (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
224702ac6454SAndrew Thompson 		    v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
224802ac6454SAndrew Thompson 		/* XXX overcurrent */
224902ac6454SAndrew Thompson 		    );
225002ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
225102ac6454SAndrew Thompson 		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
225202ac6454SAndrew Thompson 
225302ac6454SAndrew Thompson 		for (l = 0; l < sc->sc_noport; l++) {
225402ac6454SAndrew Thompson 			if (v & 1) {
225502ac6454SAndrew Thompson 				sc->sc_hub_desc.hubd.DeviceRemovable[l / 8] |= (1 << (l % 8));
225602ac6454SAndrew Thompson 			}
225702ac6454SAndrew Thompson 			v >>= 1;
225802ac6454SAndrew Thompson 		}
225902ac6454SAndrew Thompson 		sc->sc_hub_desc.hubd.bDescLength =
226002ac6454SAndrew Thompson 		    8 + ((sc->sc_noport + 7) / 8);
2261459d369eSAndrew Thompson 		len = sc->sc_hub_desc.hubd.bDescLength;
226202ac6454SAndrew Thompson 		break;
226302ac6454SAndrew Thompson 
226402ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2265459d369eSAndrew Thompson 		len = 16;
2266271ae033SHans Petter Selasky 		memset(sc->sc_hub_desc.temp, 0, 16);
226702ac6454SAndrew Thompson 		break;
226802ac6454SAndrew Thompson 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
226902ac6454SAndrew Thompson 		DPRINTFN(9, "get port status i=%d\n",
227002ac6454SAndrew Thompson 		    index);
227102ac6454SAndrew Thompson 		if ((index < 1) ||
227202ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
2273459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
227402ac6454SAndrew Thompson 			goto done;
227502ac6454SAndrew Thompson 		}
227602ac6454SAndrew Thompson 		v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
227702ac6454SAndrew Thompson 		DPRINTFN(9, "port status=0x%04x\n", v);
22780324d54aSHans Petter Selasky 		v &= ~UPS_PORT_MODE_DEVICE;	/* force host mode */
227902ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortStatus, v);
228002ac6454SAndrew Thompson 		USETW(sc->sc_hub_desc.ps.wPortChange, v >> 16);
2281459d369eSAndrew Thompson 		len = sizeof(sc->sc_hub_desc.ps);
228202ac6454SAndrew Thompson 		break;
228302ac6454SAndrew Thompson 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2284459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
228502ac6454SAndrew Thompson 		goto done;
228602ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
228702ac6454SAndrew Thompson 		break;
228802ac6454SAndrew Thompson 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
228902ac6454SAndrew Thompson 		if ((index < 1) ||
229002ac6454SAndrew Thompson 		    (index > sc->sc_noport)) {
2291459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
229202ac6454SAndrew Thompson 			goto done;
229302ac6454SAndrew Thompson 		}
229402ac6454SAndrew Thompson 		port = OHCI_RH_PORT_STATUS(index);
229502ac6454SAndrew Thompson 		switch (value) {
229602ac6454SAndrew Thompson 		case UHF_PORT_ENABLE:
229702ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_PORT_ENABLED);
229802ac6454SAndrew Thompson 			break;
229902ac6454SAndrew Thompson 		case UHF_PORT_SUSPEND:
230002ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_SUSPEND);
230102ac6454SAndrew Thompson 			break;
230202ac6454SAndrew Thompson 		case UHF_PORT_RESET:
230302ac6454SAndrew Thompson 			DPRINTFN(6, "reset port %d\n", index);
230402ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_RESET);
230502ac6454SAndrew Thompson 			for (v = 0;; v++) {
230602ac6454SAndrew Thompson 				if (v < 12) {
2307a593f6b8SAndrew Thompson 					usb_pause_mtx(&sc->sc_bus.bus_mtx,
230837506412SHans Petter Selasky 					    USB_MS_TO_TICKS(usb_port_root_reset_delay));
230902ac6454SAndrew Thompson 
231002ac6454SAndrew Thompson 					if ((OREAD4(sc, port) & UPS_RESET) == 0) {
231102ac6454SAndrew Thompson 						break;
231202ac6454SAndrew Thompson 					}
231302ac6454SAndrew Thompson 				} else {
2314459d369eSAndrew Thompson 					err = USB_ERR_TIMEOUT;
231502ac6454SAndrew Thompson 					goto done;
231602ac6454SAndrew Thompson 				}
231702ac6454SAndrew Thompson 			}
231802ac6454SAndrew Thompson 			DPRINTFN(9, "ohci port %d reset, status = 0x%04x\n",
231902ac6454SAndrew Thompson 			    index, OREAD4(sc, port));
232002ac6454SAndrew Thompson 			break;
232102ac6454SAndrew Thompson 		case UHF_PORT_POWER:
232202ac6454SAndrew Thompson 			DPRINTFN(3, "set port power %d\n", index);
232302ac6454SAndrew Thompson 			OWRITE4(sc, port, UPS_PORT_POWER);
232402ac6454SAndrew Thompson 			break;
232502ac6454SAndrew Thompson 		default:
2326459d369eSAndrew Thompson 			err = USB_ERR_IOERROR;
232702ac6454SAndrew Thompson 			goto done;
232802ac6454SAndrew Thompson 		}
232902ac6454SAndrew Thompson 		break;
233002ac6454SAndrew Thompson 	default:
2331459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
233202ac6454SAndrew Thompson 		goto done;
233302ac6454SAndrew Thompson 	}
233402ac6454SAndrew Thompson done:
2335459d369eSAndrew Thompson 	*plength = len;
2336459d369eSAndrew Thompson 	*pptr = ptr;
2337459d369eSAndrew Thompson 	return (err);
233802ac6454SAndrew Thompson }
233902ac6454SAndrew Thompson 
234002ac6454SAndrew Thompson static void
ohci_xfer_setup(struct usb_setup_params * parm)2341760bc48eSAndrew Thompson ohci_xfer_setup(struct usb_setup_params *parm)
234202ac6454SAndrew Thompson {
2343760bc48eSAndrew Thompson 	struct usb_page_search page_info;
2344760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
2345760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
234602ac6454SAndrew Thompson 	void *last_obj;
234702ac6454SAndrew Thompson 	uint32_t ntd;
234802ac6454SAndrew Thompson 	uint32_t nitd;
234902ac6454SAndrew Thompson 	uint32_t nqh;
235002ac6454SAndrew Thompson 	uint32_t n;
235102ac6454SAndrew Thompson 
235202ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
235302ac6454SAndrew Thompson 
235402ac6454SAndrew Thompson 	parm->hc_max_packet_size = 0x500;
235502ac6454SAndrew Thompson 	parm->hc_max_packet_count = 1;
235602ac6454SAndrew Thompson 	parm->hc_max_frame_size = OHCI_PAGE_SIZE;
235702ac6454SAndrew Thompson 
235802ac6454SAndrew Thompson 	/*
235902ac6454SAndrew Thompson 	 * calculate ntd and nqh
236002ac6454SAndrew Thompson 	 */
236102ac6454SAndrew Thompson 	if (parm->methods == &ohci_device_ctrl_methods) {
236202ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
236302ac6454SAndrew Thompson 
2364a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
236502ac6454SAndrew Thompson 
236602ac6454SAndrew Thompson 		nitd = 0;
236702ac6454SAndrew Thompson 		ntd = ((2 * xfer->nframes) + 1	/* STATUS */
2368578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
236902ac6454SAndrew Thompson 		nqh = 1;
237002ac6454SAndrew Thompson 
237102ac6454SAndrew Thompson 	} else if (parm->methods == &ohci_device_bulk_methods) {
237202ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
237302ac6454SAndrew Thompson 
2374a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
237502ac6454SAndrew Thompson 
237602ac6454SAndrew Thompson 		nitd = 0;
237702ac6454SAndrew Thompson 		ntd = ((2 * xfer->nframes)
2378578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
237902ac6454SAndrew Thompson 		nqh = 1;
238002ac6454SAndrew Thompson 
238102ac6454SAndrew Thompson 	} else if (parm->methods == &ohci_device_intr_methods) {
238202ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
238302ac6454SAndrew Thompson 
2384a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
238502ac6454SAndrew Thompson 
238602ac6454SAndrew Thompson 		nitd = 0;
238702ac6454SAndrew Thompson 		ntd = ((2 * xfer->nframes)
2388578d0effSAndrew Thompson 		    + (xfer->max_data_length / xfer->max_hc_frame_size));
238902ac6454SAndrew Thompson 		nqh = 1;
239002ac6454SAndrew Thompson 
239102ac6454SAndrew Thompson 	} else if (parm->methods == &ohci_device_isoc_methods) {
239202ac6454SAndrew Thompson 		xfer->flags_int.bdma_enable = 1;
239302ac6454SAndrew Thompson 
2394a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
239502ac6454SAndrew Thompson 
239602ac6454SAndrew Thompson 		nitd = ((xfer->max_data_length / OHCI_PAGE_SIZE) +
2397057b4402SPedro F. Giffuni 		    howmany(xfer->nframes, OHCI_ITD_NOFFSET) +
239802ac6454SAndrew Thompson 		    1 /* EXTRA */ );
239902ac6454SAndrew Thompson 		ntd = 0;
240002ac6454SAndrew Thompson 		nqh = 1;
240102ac6454SAndrew Thompson 
240202ac6454SAndrew Thompson 	} else {
2403a593f6b8SAndrew Thompson 		usbd_transfer_setup_sub(parm);
240402ac6454SAndrew Thompson 
240502ac6454SAndrew Thompson 		nitd = 0;
240602ac6454SAndrew Thompson 		ntd = 0;
240702ac6454SAndrew Thompson 		nqh = 0;
240802ac6454SAndrew Thompson 	}
240902ac6454SAndrew Thompson 
241002ac6454SAndrew Thompson alloc_dma_set:
241102ac6454SAndrew Thompson 
241202ac6454SAndrew Thompson 	if (parm->err) {
241302ac6454SAndrew Thompson 		return;
241402ac6454SAndrew Thompson 	}
241502ac6454SAndrew Thompson 	last_obj = NULL;
241602ac6454SAndrew Thompson 
2417a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
241802ac6454SAndrew Thompson 	    parm, &pc, sizeof(ohci_td_t),
241902ac6454SAndrew Thompson 	    OHCI_TD_ALIGN, ntd)) {
242002ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
242102ac6454SAndrew Thompson 		return;
242202ac6454SAndrew Thompson 	}
242302ac6454SAndrew Thompson 	if (parm->buf) {
242402ac6454SAndrew Thompson 		for (n = 0; n != ntd; n++) {
242502ac6454SAndrew Thompson 			ohci_td_t *td;
242602ac6454SAndrew Thompson 
2427a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
242802ac6454SAndrew Thompson 
242902ac6454SAndrew Thompson 			td = page_info.buffer;
243002ac6454SAndrew Thompson 
243102ac6454SAndrew Thompson 			/* init TD */
243202ac6454SAndrew Thompson 			td->td_self = htole32(page_info.physaddr);
243302ac6454SAndrew Thompson 			td->obj_next = last_obj;
243402ac6454SAndrew Thompson 			td->page_cache = pc + n;
243502ac6454SAndrew Thompson 
243602ac6454SAndrew Thompson 			last_obj = td;
243702ac6454SAndrew Thompson 
2438a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
243902ac6454SAndrew Thompson 		}
244002ac6454SAndrew Thompson 	}
2441a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
244202ac6454SAndrew Thompson 	    parm, &pc, sizeof(ohci_itd_t),
244302ac6454SAndrew Thompson 	    OHCI_ITD_ALIGN, nitd)) {
244402ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
244502ac6454SAndrew Thompson 		return;
244602ac6454SAndrew Thompson 	}
244702ac6454SAndrew Thompson 	if (parm->buf) {
244802ac6454SAndrew Thompson 		for (n = 0; n != nitd; n++) {
244902ac6454SAndrew Thompson 			ohci_itd_t *itd;
245002ac6454SAndrew Thompson 
2451a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
245202ac6454SAndrew Thompson 
245302ac6454SAndrew Thompson 			itd = page_info.buffer;
245402ac6454SAndrew Thompson 
245502ac6454SAndrew Thompson 			/* init TD */
245602ac6454SAndrew Thompson 			itd->itd_self = htole32(page_info.physaddr);
245702ac6454SAndrew Thompson 			itd->obj_next = last_obj;
245802ac6454SAndrew Thompson 			itd->page_cache = pc + n;
245902ac6454SAndrew Thompson 
246002ac6454SAndrew Thompson 			last_obj = itd;
246102ac6454SAndrew Thompson 
2462a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
246302ac6454SAndrew Thompson 		}
246402ac6454SAndrew Thompson 	}
246502ac6454SAndrew Thompson 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
246602ac6454SAndrew Thompson 
246702ac6454SAndrew Thompson 	last_obj = NULL;
246802ac6454SAndrew Thompson 
2469a593f6b8SAndrew Thompson 	if (usbd_transfer_setup_sub_malloc(
247002ac6454SAndrew Thompson 	    parm, &pc, sizeof(ohci_ed_t),
247102ac6454SAndrew Thompson 	    OHCI_ED_ALIGN, nqh)) {
247202ac6454SAndrew Thompson 		parm->err = USB_ERR_NOMEM;
247302ac6454SAndrew Thompson 		return;
247402ac6454SAndrew Thompson 	}
247502ac6454SAndrew Thompson 	if (parm->buf) {
247602ac6454SAndrew Thompson 		for (n = 0; n != nqh; n++) {
247702ac6454SAndrew Thompson 			ohci_ed_t *ed;
247802ac6454SAndrew Thompson 
2479a593f6b8SAndrew Thompson 			usbd_get_page(pc + n, 0, &page_info);
248002ac6454SAndrew Thompson 
248102ac6454SAndrew Thompson 			ed = page_info.buffer;
248202ac6454SAndrew Thompson 
248302ac6454SAndrew Thompson 			/* init QH */
248402ac6454SAndrew Thompson 			ed->ed_self = htole32(page_info.physaddr);
248502ac6454SAndrew Thompson 			ed->obj_next = last_obj;
248602ac6454SAndrew Thompson 			ed->page_cache = pc + n;
248702ac6454SAndrew Thompson 
248802ac6454SAndrew Thompson 			last_obj = ed;
248902ac6454SAndrew Thompson 
2490a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc + n);
249102ac6454SAndrew Thompson 		}
249202ac6454SAndrew Thompson 	}
249302ac6454SAndrew Thompson 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
249402ac6454SAndrew Thompson 
249502ac6454SAndrew Thompson 	if (!xfer->flags_int.curr_dma_set) {
249602ac6454SAndrew Thompson 		xfer->flags_int.curr_dma_set = 1;
249702ac6454SAndrew Thompson 		goto alloc_dma_set;
249802ac6454SAndrew Thompson 	}
249902ac6454SAndrew Thompson }
250002ac6454SAndrew Thompson 
250102ac6454SAndrew Thompson static void
ohci_ep_init(struct usb_device * udev,struct usb_endpoint_descriptor * edesc,struct usb_endpoint * ep)2502ae60fdfbSAndrew Thompson ohci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2503ae60fdfbSAndrew Thompson     struct usb_endpoint *ep)
250402ac6454SAndrew Thompson {
250502ac6454SAndrew Thompson 	ohci_softc_t *sc = OHCI_BUS2SC(udev->bus);
250602ac6454SAndrew Thompson 
2507ae60fdfbSAndrew Thompson 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
2508ae60fdfbSAndrew Thompson 	    ep, udev->address,
2509f29a0724SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb_mode,
251002ac6454SAndrew Thompson 	    sc->sc_addr);
251102ac6454SAndrew Thompson 
251239307315SAndrew Thompson 	if (udev->device_index != sc->sc_addr) {
251302ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
251402ac6454SAndrew Thompson 		case UE_CONTROL:
2515ae60fdfbSAndrew Thompson 			ep->methods = &ohci_device_ctrl_methods;
251602ac6454SAndrew Thompson 			break;
251702ac6454SAndrew Thompson 		case UE_INTERRUPT:
2518ae60fdfbSAndrew Thompson 			ep->methods = &ohci_device_intr_methods;
251902ac6454SAndrew Thompson 			break;
252002ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
252102ac6454SAndrew Thompson 			if (udev->speed == USB_SPEED_FULL) {
2522ae60fdfbSAndrew Thompson 				ep->methods = &ohci_device_isoc_methods;
252302ac6454SAndrew Thompson 			}
252402ac6454SAndrew Thompson 			break;
252502ac6454SAndrew Thompson 		case UE_BULK:
2526ae60fdfbSAndrew Thompson 			ep->methods = &ohci_device_bulk_methods;
252702ac6454SAndrew Thompson 			break;
252802ac6454SAndrew Thompson 		default:
252902ac6454SAndrew Thompson 			/* do nothing */
253002ac6454SAndrew Thompson 			break;
253102ac6454SAndrew Thompson 		}
253202ac6454SAndrew Thompson 	}
253302ac6454SAndrew Thompson }
253402ac6454SAndrew Thompson 
253502ac6454SAndrew Thompson static void
ohci_xfer_unsetup(struct usb_xfer * xfer)2536760bc48eSAndrew Thompson ohci_xfer_unsetup(struct usb_xfer *xfer)
253702ac6454SAndrew Thompson {
253802ac6454SAndrew Thompson 	return;
253902ac6454SAndrew Thompson }
254002ac6454SAndrew Thompson 
254102ac6454SAndrew Thompson static void
ohci_get_dma_delay(struct usb_device * udev,uint32_t * pus)2542527aa7b2SAndrew Thompson ohci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
254302ac6454SAndrew Thompson {
254402ac6454SAndrew Thompson 	/*
254502ac6454SAndrew Thompson 	 * Wait until hardware has finished any possible use of the
254602ac6454SAndrew Thompson 	 * transfer descriptor(s) and QH
254702ac6454SAndrew Thompson 	 */
254802ac6454SAndrew Thompson 	*pus = (1125);			/* microseconds */
254902ac6454SAndrew Thompson }
255002ac6454SAndrew Thompson 
255102ac6454SAndrew Thompson static void
ohci_device_resume(struct usb_device * udev)2552760bc48eSAndrew Thompson ohci_device_resume(struct usb_device *udev)
255302ac6454SAndrew Thompson {
255402ac6454SAndrew Thompson 	struct ohci_softc *sc = OHCI_BUS2SC(udev->bus);
2555760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
2556e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
255702ac6454SAndrew Thompson 	ohci_ed_t *ed;
255802ac6454SAndrew Thompson 
255902ac6454SAndrew Thompson 	DPRINTF("\n");
256002ac6454SAndrew Thompson 
256102ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
256202ac6454SAndrew Thompson 
256302ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
256402ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
2565ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
256602ac6454SAndrew Thompson 			ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
256702ac6454SAndrew Thompson 
256802ac6454SAndrew Thompson 			if (methods == &ohci_device_bulk_methods) {
256902ac6454SAndrew Thompson 				OHCI_APPEND_QH(ed, sc->sc_bulk_p_last);
257002ac6454SAndrew Thompson 				OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
257102ac6454SAndrew Thompson 			}
257202ac6454SAndrew Thompson 			if (methods == &ohci_device_ctrl_methods) {
257302ac6454SAndrew Thompson 				OHCI_APPEND_QH(ed, sc->sc_ctrl_p_last);
257402ac6454SAndrew Thompson 				OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
257502ac6454SAndrew Thompson 			}
257602ac6454SAndrew Thompson 			if (methods == &ohci_device_intr_methods) {
257702ac6454SAndrew Thompson 				OHCI_APPEND_QH(ed, sc->sc_intr_p_last[xfer->qh_pos]);
257802ac6454SAndrew Thompson 			}
257902ac6454SAndrew Thompson 		}
258002ac6454SAndrew Thompson 	}
258102ac6454SAndrew Thompson 
258202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
258302ac6454SAndrew Thompson 
258402ac6454SAndrew Thompson 	return;
258502ac6454SAndrew Thompson }
258602ac6454SAndrew Thompson 
258702ac6454SAndrew Thompson static void
ohci_device_suspend(struct usb_device * udev)2588760bc48eSAndrew Thompson ohci_device_suspend(struct usb_device *udev)
258902ac6454SAndrew Thompson {
259002ac6454SAndrew Thompson 	struct ohci_softc *sc = OHCI_BUS2SC(udev->bus);
2591760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
2592e892b3feSHans Petter Selasky 	const struct usb_pipe_methods *methods;
259302ac6454SAndrew Thompson 	ohci_ed_t *ed;
259402ac6454SAndrew Thompson 
259502ac6454SAndrew Thompson 	DPRINTF("\n");
259602ac6454SAndrew Thompson 
259702ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
259802ac6454SAndrew Thompson 
259902ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
260002ac6454SAndrew Thompson 		if (xfer->xroot->udev == udev) {
2601ae60fdfbSAndrew Thompson 			methods = xfer->endpoint->methods;
260202ac6454SAndrew Thompson 			ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
260302ac6454SAndrew Thompson 
260402ac6454SAndrew Thompson 			if (methods == &ohci_device_bulk_methods) {
260502ac6454SAndrew Thompson 				OHCI_REMOVE_QH(ed, sc->sc_bulk_p_last);
260602ac6454SAndrew Thompson 			}
260702ac6454SAndrew Thompson 			if (methods == &ohci_device_ctrl_methods) {
260802ac6454SAndrew Thompson 				OHCI_REMOVE_QH(ed, sc->sc_ctrl_p_last);
260902ac6454SAndrew Thompson 			}
261002ac6454SAndrew Thompson 			if (methods == &ohci_device_intr_methods) {
261102ac6454SAndrew Thompson 				OHCI_REMOVE_QH(ed, sc->sc_intr_p_last[xfer->qh_pos]);
261202ac6454SAndrew Thompson 			}
261302ac6454SAndrew Thompson 		}
261402ac6454SAndrew Thompson 	}
261502ac6454SAndrew Thompson 
261602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
261702ac6454SAndrew Thompson 
261802ac6454SAndrew Thompson 	return;
261902ac6454SAndrew Thompson }
262002ac6454SAndrew Thompson 
262102ac6454SAndrew Thompson static void
ohci_set_hw_power_sleep(struct usb_bus * bus,uint32_t state)26222e141748SHans Petter Selasky ohci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
26232e141748SHans Petter Selasky {
26242e141748SHans Petter Selasky 	struct ohci_softc *sc = OHCI_BUS2SC(bus);
26252e141748SHans Petter Selasky 
26262e141748SHans Petter Selasky 	switch (state) {
26272e141748SHans Petter Selasky 	case USB_HW_POWER_SUSPEND:
26282e141748SHans Petter Selasky 	case USB_HW_POWER_SHUTDOWN:
26292e141748SHans Petter Selasky 		ohci_suspend(sc);
26302e141748SHans Petter Selasky 		break;
26312e141748SHans Petter Selasky 	case USB_HW_POWER_RESUME:
26322e141748SHans Petter Selasky 		ohci_resume(sc);
26332e141748SHans Petter Selasky 		break;
26342e141748SHans Petter Selasky 	default:
26352e141748SHans Petter Selasky 		break;
26362e141748SHans Petter Selasky 	}
26372e141748SHans Petter Selasky }
26382e141748SHans Petter Selasky 
26392e141748SHans Petter Selasky static void
ohci_set_hw_power(struct usb_bus * bus)2640760bc48eSAndrew Thompson ohci_set_hw_power(struct usb_bus *bus)
264102ac6454SAndrew Thompson {
264202ac6454SAndrew Thompson 	struct ohci_softc *sc = OHCI_BUS2SC(bus);
264302ac6454SAndrew Thompson 	uint32_t temp;
264402ac6454SAndrew Thompson 	uint32_t flags;
264502ac6454SAndrew Thompson 
264602ac6454SAndrew Thompson 	DPRINTF("\n");
264702ac6454SAndrew Thompson 
264802ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
264902ac6454SAndrew Thompson 
265002ac6454SAndrew Thompson 	flags = bus->hw_power_state;
265102ac6454SAndrew Thompson 
265202ac6454SAndrew Thompson 	temp = OREAD4(sc, OHCI_CONTROL);
265302ac6454SAndrew Thompson 	temp &= ~(OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE);
265402ac6454SAndrew Thompson 
265502ac6454SAndrew Thompson 	if (flags & USB_HW_POWER_CONTROL)
265602ac6454SAndrew Thompson 		temp |= OHCI_CLE;
265702ac6454SAndrew Thompson 
265802ac6454SAndrew Thompson 	if (flags & USB_HW_POWER_BULK)
265902ac6454SAndrew Thompson 		temp |= OHCI_BLE;
266002ac6454SAndrew Thompson 
266102ac6454SAndrew Thompson 	if (flags & USB_HW_POWER_INTERRUPT)
266202ac6454SAndrew Thompson 		temp |= OHCI_PLE;
266302ac6454SAndrew Thompson 
266402ac6454SAndrew Thompson 	if (flags & USB_HW_POWER_ISOC)
266502ac6454SAndrew Thompson 		temp |= OHCI_IE | OHCI_PLE;
266602ac6454SAndrew Thompson 
266702ac6454SAndrew Thompson 	OWRITE4(sc, OHCI_CONTROL, temp);
266802ac6454SAndrew Thompson 
266902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
267002ac6454SAndrew Thompson 
267102ac6454SAndrew Thompson 	return;
267202ac6454SAndrew Thompson }
267302ac6454SAndrew Thompson 
2674e892b3feSHans Petter Selasky static const struct usb_bus_methods ohci_bus_methods =
267502ac6454SAndrew Thompson {
2676ae60fdfbSAndrew Thompson 	.endpoint_init = ohci_ep_init,
267702ac6454SAndrew Thompson 	.xfer_setup = ohci_xfer_setup,
267802ac6454SAndrew Thompson 	.xfer_unsetup = ohci_xfer_unsetup,
267902ac6454SAndrew Thompson 	.get_dma_delay = ohci_get_dma_delay,
268002ac6454SAndrew Thompson 	.device_resume = ohci_device_resume,
268102ac6454SAndrew Thompson 	.device_suspend = ohci_device_suspend,
268202ac6454SAndrew Thompson 	.set_hw_power = ohci_set_hw_power,
26832e141748SHans Petter Selasky 	.set_hw_power_sleep = ohci_set_hw_power_sleep,
268439307315SAndrew Thompson 	.roothub_exec = ohci_roothub_exec,
2685dddb25f9SAlfred Perlstein 	.xfer_poll = ohci_do_poll,
268602ac6454SAndrew Thompson };
2687