Home | History | Annotate | Line # | Download | only in usb
ohci.c revision 1.254.2.42
      1 /*	$NetBSD: ohci.c,v 1.254.2.42 2016/01/10 10:16:00 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2004, 2005, 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology, Jared D. McNeill (jmcneill (at) invisible.ca)
     10  * and Matthew R. Green (mrg (at) eterna.com.au).
     11  * This code is derived from software contributed to The NetBSD Foundation
     12  * by Charles M. Hannum.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * USB Open Host Controller driver.
     38  *
     39  * OHCI spec: http://www.compaq.com/productinfo/development/openhci.html
     40  * USB spec: http://www.usb.org/developers/docs/
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: ohci.c,v 1.254.2.42 2016/01/10 10:16:00 skrll Exp $");
     45 
     46 #include "opt_usb.h"
     47 
     48 #include <sys/param.h>
     49 
     50 #include <sys/cpu.h>
     51 #include <sys/device.h>
     52 #include <sys/kernel.h>
     53 #include <sys/kmem.h>
     54 #include <sys/proc.h>
     55 #include <sys/queue.h>
     56 #include <sys/select.h>
     57 #include <sys/sysctl.h>
     58 #include <sys/systm.h>
     59 
     60 #include <machine/endian.h>
     61 
     62 #include <dev/usb/usb.h>
     63 #include <dev/usb/usbdi.h>
     64 #include <dev/usb/usbdivar.h>
     65 #include <dev/usb/usb_mem.h>
     66 #include <dev/usb/usb_quirks.h>
     67 
     68 #include <dev/usb/ohcireg.h>
     69 #include <dev/usb/ohcivar.h>
     70 #include <dev/usb/usbroothub.h>
     71 #include <dev/usb/usbhist.h>
     72 
     73 #ifdef USB_DEBUG
     74 #ifndef OHCI_DEBUG
     75 #define ohcidebug 0
     76 #else
     77 static int ohcidebug = 0;
     78 
     79 SYSCTL_SETUP(sysctl_hw_ohci_setup, "sysctl hw.ohci setup")
     80 {
     81 	int err;
     82 	const struct sysctlnode *rnode;
     83 	const struct sysctlnode *cnode;
     84 
     85 	err = sysctl_createv(clog, 0, NULL, &rnode,
     86 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ohci",
     87 	    SYSCTL_DESCR("ohci global controls"),
     88 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
     89 
     90 	if (err)
     91 		goto fail;
     92 
     93 	/* control debugging printfs */
     94 	err = sysctl_createv(clog, 0, &rnode, &cnode,
     95 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
     96 	    "debug", SYSCTL_DESCR("Enable debugging output"),
     97 	    NULL, 0, &ohcidebug, sizeof(ohcidebug), CTL_CREATE, CTL_EOL);
     98 	if (err)
     99 		goto fail;
    100 
    101 	return;
    102 fail:
    103 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    104 }
    105 
    106 #endif /* OHCI_DEBUG */
    107 #endif /* USB_DEBUG */
    108 
    109 #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(ohcidebug,FMT,A,B,C,D)
    110 #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(ohcidebug,N,FMT,A,B,C,D)
    111 #define	OHCIHIST_FUNC()		USBHIST_FUNC()
    112 #define	OHCIHIST_CALLED(name)	USBHIST_CALLED(ohcidebug)
    113 
    114 #if BYTE_ORDER == BIG_ENDIAN
    115 #define	SWAP_ENDIAN	OHCI_LITTLE_ENDIAN
    116 #else
    117 #define	SWAP_ENDIAN	OHCI_BIG_ENDIAN
    118 #endif
    119 
    120 #define	O16TOH(val)	(sc->sc_endian == SWAP_ENDIAN ? bswap16(val) : val)
    121 #define	O32TOH(val)	(sc->sc_endian == SWAP_ENDIAN ? bswap32(val) : val)
    122 #define	HTOO16(val)	O16TOH(val)
    123 #define	HTOO32(val)	O32TOH(val)
    124 
    125 struct ohci_pipe;
    126 
    127 Static ohci_soft_ed_t  *ohci_alloc_sed(ohci_softc_t *);
    128 Static void		ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);
    129 
    130 Static ohci_soft_td_t  *ohci_alloc_std(ohci_softc_t *);
    131 Static void		ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
    132 Static void		ohci_free_std_locked(ohci_softc_t *, ohci_soft_td_t *);
    133 
    134 Static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
    135 Static void		ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
    136 Static void		ohci_free_sitd_locked(ohci_softc_t *,
    137 			    ohci_soft_itd_t *);
    138 
    139 Static usbd_status	ohci_alloc_std_chain(ohci_softc_t *, struct usbd_xfer *,
    140 			    int, int);
    141 Static void		ohci_free_stds(ohci_softc_t *, struct ohci_xfer *);
    142 
    143 Static void		ohci_reset_std_chain(ohci_softc_t *, struct usbd_xfer *,
    144 			    int, int, ohci_soft_td_t *, ohci_soft_td_t **);
    145 
    146 Static usbd_status	ohci_open(struct usbd_pipe *);
    147 Static void		ohci_poll(struct usbd_bus *);
    148 Static void		ohci_softintr(void *);
    149 Static void		ohci_waitintr(ohci_softc_t *, struct usbd_xfer *);
    150 Static void		ohci_rhsc(ohci_softc_t *, struct usbd_xfer *);
    151 Static void		ohci_rhsc_softint(void *);
    152 
    153 Static void		ohci_add_ed(ohci_softc_t *, ohci_soft_ed_t *,
    154 			    ohci_soft_ed_t *);
    155 
    156 Static void		ohci_rem_ed(ohci_softc_t *, ohci_soft_ed_t *,
    157 				    ohci_soft_ed_t *);
    158 Static void		ohci_hash_add_td(ohci_softc_t *, ohci_soft_td_t *);
    159 Static void		ohci_hash_rem_td(ohci_softc_t *, ohci_soft_td_t *);
    160 Static ohci_soft_td_t  *ohci_hash_find_td(ohci_softc_t *, ohci_physaddr_t);
    161 Static void		ohci_hash_add_itd(ohci_softc_t *, ohci_soft_itd_t *);
    162 Static void		ohci_hash_rem_itd(ohci_softc_t *, ohci_soft_itd_t *);
    163 Static ohci_soft_itd_t  *ohci_hash_find_itd(ohci_softc_t *, ohci_physaddr_t);
    164 
    165 Static usbd_status	ohci_setup_isoc(struct usbd_pipe *);
    166 Static void		ohci_device_isoc_enter(struct usbd_xfer *);
    167 
    168 Static struct usbd_xfer *
    169 			ohci_allocx(struct usbd_bus *, unsigned int);
    170 Static void		ohci_freex(struct usbd_bus *, struct usbd_xfer *);
    171 Static void		ohci_get_lock(struct usbd_bus *, kmutex_t **);
    172 Static int		ohci_roothub_ctrl(struct usbd_bus *,
    173 			    usb_device_request_t *, void *, int);
    174 
    175 Static usbd_status	ohci_root_intr_transfer(struct usbd_xfer *);
    176 Static usbd_status	ohci_root_intr_start(struct usbd_xfer *);
    177 Static void		ohci_root_intr_abort(struct usbd_xfer *);
    178 Static void		ohci_root_intr_close(struct usbd_pipe *);
    179 Static void		ohci_root_intr_done(struct usbd_xfer *);
    180 
    181 Static int		ohci_device_ctrl_init(struct usbd_xfer *);
    182 Static void		ohci_device_ctrl_fini(struct usbd_xfer *);
    183 Static usbd_status	ohci_device_ctrl_transfer(struct usbd_xfer *);
    184 Static usbd_status	ohci_device_ctrl_start(struct usbd_xfer *);
    185 Static void		ohci_device_ctrl_abort(struct usbd_xfer *);
    186 Static void		ohci_device_ctrl_close(struct usbd_pipe *);
    187 Static void		ohci_device_ctrl_done(struct usbd_xfer *);
    188 
    189 Static int		ohci_device_bulk_init(struct usbd_xfer *);
    190 Static void		ohci_device_bulk_fini(struct usbd_xfer *);
    191 Static usbd_status	ohci_device_bulk_transfer(struct usbd_xfer *);
    192 Static usbd_status	ohci_device_bulk_start(struct usbd_xfer *);
    193 Static void		ohci_device_bulk_abort(struct usbd_xfer *);
    194 Static void		ohci_device_bulk_close(struct usbd_pipe *);
    195 Static void		ohci_device_bulk_done(struct usbd_xfer *);
    196 
    197 Static int		ohci_device_intr_init(struct usbd_xfer *);
    198 Static void		ohci_device_intr_fini(struct usbd_xfer *);
    199 Static usbd_status	ohci_device_intr_transfer(struct usbd_xfer *);
    200 Static usbd_status	ohci_device_intr_start(struct usbd_xfer *);
    201 Static void		ohci_device_intr_abort(struct usbd_xfer *);
    202 Static void		ohci_device_intr_close(struct usbd_pipe *);
    203 Static void		ohci_device_intr_done(struct usbd_xfer *);
    204 
    205 Static int		ohci_device_isoc_init(struct usbd_xfer *);
    206 Static void		ohci_device_isoc_fini(struct usbd_xfer *);
    207 Static usbd_status	ohci_device_isoc_transfer(struct usbd_xfer *);
    208 Static usbd_status	ohci_device_isoc_start(struct usbd_xfer *);
    209 Static void		ohci_device_isoc_abort(struct usbd_xfer *);
    210 Static void		ohci_device_isoc_close(struct usbd_pipe *);
    211 Static void		ohci_device_isoc_done(struct usbd_xfer *);
    212 
    213 Static usbd_status	ohci_device_setintr(ohci_softc_t *,
    214 			    struct ohci_pipe *, int);
    215 
    216 Static void		ohci_timeout(void *);
    217 Static void		ohci_timeout_task(void *);
    218 Static void		ohci_rhsc_enable(void *);
    219 
    220 Static void		ohci_close_pipe(struct usbd_pipe *, ohci_soft_ed_t *);
    221 Static void		ohci_abort_xfer(struct usbd_xfer *, usbd_status);
    222 
    223 Static void		ohci_device_clear_toggle(struct usbd_pipe *);
    224 Static void		ohci_noop(struct usbd_pipe *);
    225 
    226 #ifdef OHCI_DEBUG
    227 Static void		ohci_dumpregs(ohci_softc_t *);
    228 Static void		ohci_dump_tds(ohci_softc_t *, ohci_soft_td_t *);
    229 Static void		ohci_dump_td(ohci_softc_t *, ohci_soft_td_t *);
    230 Static void		ohci_dump_ed(ohci_softc_t *, ohci_soft_ed_t *);
    231 Static void		ohci_dump_itd(ohci_softc_t *, ohci_soft_itd_t *);
    232 Static void		ohci_dump_itds(ohci_softc_t *, ohci_soft_itd_t *);
    233 #endif
    234 
    235 #define OBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
    236 			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
    237 #define OWRITE1(sc, r, x) \
    238  do { OBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
    239 #define OWRITE2(sc, r, x) \
    240  do { OBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
    241 #define OWRITE4(sc, r, x) \
    242  do { OBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
    243 
    244 static __inline uint32_t
    245 OREAD4(ohci_softc_t *sc, bus_size_t r)
    246 {
    247 
    248 	OBARR(sc);
    249 	return bus_space_read_4(sc->iot, sc->ioh, r);
    250 }
    251 
    252 /* Reverse the bits in a value 0 .. 31 */
    253 Static uint8_t revbits[OHCI_NO_INTRS] =
    254   { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
    255     0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
    256     0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
    257     0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
    258 
    259 struct ohci_pipe {
    260 	struct usbd_pipe pipe;
    261 	ohci_soft_ed_t *sed;
    262 	union {
    263 		ohci_soft_td_t *td;
    264 		ohci_soft_itd_t *itd;
    265 	} tail;
    266 	/* Info needed for different pipe kinds. */
    267 	union {
    268 		/* Control pipe */
    269 		struct {
    270 			usb_dma_t reqdma;
    271 		} ctrl;
    272 		/* Interrupt pipe */
    273 		struct {
    274 			int nslots;
    275 			int pos;
    276 		} intr;
    277 		/* Isochronous pipe */
    278 		struct isoc {
    279 			int next, inuse;
    280 		} isoc;
    281 	};
    282 };
    283 
    284 Static const struct usbd_bus_methods ohci_bus_methods = {
    285 	.ubm_open =	ohci_open,
    286 	.ubm_softint =	ohci_softintr,
    287 	.ubm_dopoll =	ohci_poll,
    288 	.ubm_allocx =	ohci_allocx,
    289 	.ubm_freex =	ohci_freex,
    290 	.ubm_getlock =	ohci_get_lock,
    291 	.ubm_rhctrl =	ohci_roothub_ctrl,
    292 };
    293 
    294 Static const struct usbd_pipe_methods ohci_root_intr_methods = {
    295 	.upm_transfer =	ohci_root_intr_transfer,
    296 	.upm_start =	ohci_root_intr_start,
    297 	.upm_abort =	ohci_root_intr_abort,
    298 	.upm_close =	ohci_root_intr_close,
    299 	.upm_cleartoggle =	ohci_noop,
    300 	.upm_done =	ohci_root_intr_done,
    301 };
    302 
    303 Static const struct usbd_pipe_methods ohci_device_ctrl_methods = {
    304 	.upm_init =	ohci_device_ctrl_init,
    305 	.upm_fini =	ohci_device_ctrl_fini,
    306 	.upm_transfer =	ohci_device_ctrl_transfer,
    307 	.upm_start =	ohci_device_ctrl_start,
    308 	.upm_abort =	ohci_device_ctrl_abort,
    309 	.upm_close =	ohci_device_ctrl_close,
    310 	.upm_cleartoggle =	ohci_noop,
    311 	.upm_done =	ohci_device_ctrl_done,
    312 };
    313 
    314 Static const struct usbd_pipe_methods ohci_device_intr_methods = {
    315 	.upm_init =	ohci_device_intr_init,
    316 	.upm_fini =	ohci_device_intr_fini,
    317 	.upm_transfer =	ohci_device_intr_transfer,
    318 	.upm_start =	ohci_device_intr_start,
    319 	.upm_abort =	ohci_device_intr_abort,
    320 	.upm_close =	ohci_device_intr_close,
    321 	.upm_cleartoggle =	ohci_device_clear_toggle,
    322 	.upm_done =	ohci_device_intr_done,
    323 };
    324 
    325 Static const struct usbd_pipe_methods ohci_device_bulk_methods = {
    326 	.upm_init =	ohci_device_bulk_init,
    327 	.upm_fini =	ohci_device_bulk_fini,
    328 	.upm_transfer =	ohci_device_bulk_transfer,
    329 	.upm_start =	ohci_device_bulk_start,
    330 	.upm_abort =	ohci_device_bulk_abort,
    331 	.upm_close =	ohci_device_bulk_close,
    332 	.upm_cleartoggle =	ohci_device_clear_toggle,
    333 	.upm_done =	ohci_device_bulk_done,
    334 };
    335 
    336 Static const struct usbd_pipe_methods ohci_device_isoc_methods = {
    337 	.upm_init =	ohci_device_isoc_init,
    338 	.upm_fini =	ohci_device_isoc_fini,
    339 	.upm_transfer =	ohci_device_isoc_transfer,
    340 	.upm_start =	ohci_device_isoc_start,
    341 	.upm_abort =	ohci_device_isoc_abort,
    342 	.upm_close =	ohci_device_isoc_close,
    343 	.upm_cleartoggle =	ohci_noop,
    344 	.upm_done =	ohci_device_isoc_done,
    345 };
    346 
    347 int
    348 ohci_activate(device_t self, enum devact act)
    349 {
    350 	struct ohci_softc *sc = device_private(self);
    351 
    352 	switch (act) {
    353 	case DVACT_DEACTIVATE:
    354 		sc->sc_dying = 1;
    355 		return 0;
    356 	default:
    357 		return EOPNOTSUPP;
    358 	}
    359 }
    360 
    361 void
    362 ohci_childdet(device_t self, device_t child)
    363 {
    364 	struct ohci_softc *sc = device_private(self);
    365 
    366 	KASSERT(sc->sc_child == child);
    367 	sc->sc_child = NULL;
    368 }
    369 
    370 int
    371 ohci_detach(struct ohci_softc *sc, int flags)
    372 {
    373 	int rv = 0;
    374 
    375 	if (sc->sc_child != NULL)
    376 		rv = config_detach(sc->sc_child, flags);
    377 
    378 	if (rv != 0)
    379 		return rv;
    380 
    381 	callout_halt(&sc->sc_tmo_rhsc, &sc->sc_lock);
    382 
    383 	usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
    384 	callout_destroy(&sc->sc_tmo_rhsc);
    385 
    386 	softint_disestablish(sc->sc_rhsc_si);
    387 
    388 	cv_destroy(&sc->sc_softwake_cv);
    389 
    390 	mutex_destroy(&sc->sc_lock);
    391 	mutex_destroy(&sc->sc_intr_lock);
    392 
    393 	if (sc->sc_hcca != NULL)
    394 		usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
    395 	pool_cache_destroy(sc->sc_xferpool);
    396 
    397 	return rv;
    398 }
    399 
    400 ohci_soft_ed_t *
    401 ohci_alloc_sed(ohci_softc_t *sc)
    402 {
    403 	ohci_soft_ed_t *sed;
    404 	usbd_status err;
    405 	int i, offs;
    406 	usb_dma_t dma;
    407 
    408 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
    409 
    410 	mutex_enter(&sc->sc_lock);
    411 	if (sc->sc_freeeds == NULL) {
    412 		DPRINTFN(2, "allocating chunk", 0, 0, 0, 0);
    413 		mutex_exit(&sc->sc_lock);
    414 
    415 		err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
    416 			  OHCI_ED_ALIGN, &dma);
    417 		if (err)
    418 			return 0;
    419 
    420 		mutex_enter(&sc->sc_lock);
    421 		for (i = 0; i < OHCI_SED_CHUNK; i++) {
    422 			offs = i * OHCI_SED_SIZE;
    423 			sed = KERNADDR(&dma, offs);
    424 			sed->physaddr = DMAADDR(&dma, offs);
    425 			sed->dma = dma;
    426 			sed->offs = offs;
    427 			sed->next = sc->sc_freeeds;
    428 			sc->sc_freeeds = sed;
    429 		}
    430 	}
    431 	sed = sc->sc_freeeds;
    432 	sc->sc_freeeds = sed->next;
    433 	mutex_exit(&sc->sc_lock);
    434 
    435 	memset(&sed->ed, 0, sizeof(ohci_ed_t));
    436 	sed->next = 0;
    437 	return sed;
    438 }
    439 
    440 static inline void
    441 ohci_free_sed_locked(ohci_softc_t *sc, ohci_soft_ed_t *sed)
    442 {
    443 
    444 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
    445 
    446 	sed->next = sc->sc_freeeds;
    447 	sc->sc_freeeds = sed;
    448 }
    449 
    450 void
    451 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
    452 {
    453 
    454 	mutex_enter(&sc->sc_lock);
    455 	ohci_free_sed_locked(sc, sed);
    456 	mutex_exit(&sc->sc_lock);
    457 }
    458 
    459 ohci_soft_td_t *
    460 ohci_alloc_std(ohci_softc_t *sc)
    461 {
    462 	ohci_soft_td_t *std;
    463 	usbd_status err;
    464 	int i, offs;
    465 	usb_dma_t dma;
    466 
    467 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
    468 
    469 	mutex_enter(&sc->sc_lock);
    470 	if (sc->sc_freetds == NULL) {
    471 		DPRINTFN(2, "allocating chunk", 0, 0, 0, 0);
    472 		mutex_exit(&sc->sc_lock);
    473 
    474 		err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
    475 			  OHCI_TD_ALIGN, &dma);
    476 		if (err)
    477 			return NULL;
    478 
    479 		mutex_enter(&sc->sc_lock);
    480 		for(i = 0; i < OHCI_STD_CHUNK; i++) {
    481 			offs = i * OHCI_STD_SIZE;
    482 			std = KERNADDR(&dma, offs);
    483 			std->physaddr = DMAADDR(&dma, offs);
    484 			std->dma = dma;
    485 			std->offs = offs;
    486 			std->nexttd = sc->sc_freetds;
    487 			sc->sc_freetds = std;
    488 		}
    489 	}
    490 
    491 	std = sc->sc_freetds;
    492 	sc->sc_freetds = std->nexttd;
    493 	mutex_exit(&sc->sc_lock);
    494 
    495 	memset(&std->td, 0, sizeof(ohci_td_t));
    496 	std->nexttd = NULL;
    497 	std->xfer = NULL;
    498 
    499 	return std;
    500 }
    501 
    502 void
    503 ohci_free_std_locked(ohci_softc_t *sc, ohci_soft_td_t *std)
    504 {
    505 
    506 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
    507 
    508 	std->nexttd = sc->sc_freetds;
    509 	sc->sc_freetds = std;
    510 }
    511 
    512 void
    513 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
    514 {
    515 
    516 	mutex_enter(&sc->sc_lock);
    517 	ohci_free_std_locked(sc, std);
    518 	mutex_exit(&sc->sc_lock);
    519 }
    520 
    521 Static usbd_status
    522 ohci_alloc_std_chain(ohci_softc_t *sc, struct usbd_xfer *xfer, int alen, int rd)
    523 {
    524 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
    525 	struct usbd_pipe *pipe = xfer->ux_pipe;
    526 	ohci_soft_td_t *next, *cur;
    527 	ohci_physaddr_t dataphys, dataphysend;
    528 	uint32_t tdflags;
    529 	int len = alen;
    530 	int curlen;
    531 	usb_dma_t *dma = &xfer->ux_dmabuf;
    532 	uint16_t flags = xfer->ux_flags;
    533 
    534 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
    535 
    536 	DPRINTFN(8, "addr=%d endpt=%d len=%d speed=%d",
    537 	    pipe->up_dev->ud_addr,
    538 	    UE_GET_ADDR(pipe->up_endpoint->ue_edesc->bEndpointAddress),
    539 	    alen, pipe->up_dev->ud_speed);
    540 
    541 	ASSERT_SLEEPABLE();
    542 
    543 	size_t nstd = (flags & USBD_FORCE_SHORT_XFER) ? 1 : 0;
    544 	nstd += ((len + OHCI_PAGE_SIZE - 1) / OHCI_PAGE_SIZE);
    545 	ox->ox_stds = kmem_zalloc(sizeof(ohci_soft_td_t *) * nstd,
    546 	    KM_SLEEP);
    547 	ox->ox_nstd = nstd;
    548 	int mps = UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize);
    549 
    550 	DPRINTFN(8, "xfer %p nstd %d mps %d", xfer, nstd, mps, 0);
    551 
    552 	len = alen;
    553 	cur = ohci_alloc_std(sc);
    554 	if (cur == NULL)
    555 		goto nomem;
    556 
    557 	dataphys = DMAADDR(dma, 0);
    558 	dataphysend = OHCI_PAGE(dataphys + len - 1);
    559 	tdflags = HTOO32(
    560 	    (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
    561 	    (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) |
    562 	    OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_NOINTR);
    563 
    564 	for (size_t j = 0;;) {
    565 		ox->ox_stds[j++] = cur;
    566 		next = ohci_alloc_std(sc);
    567 		if (next == NULL)
    568 			goto nomem;
    569 
    570 		/* The OHCI hardware can handle at most one page crossing. */
    571 		if (OHCI_PAGE(dataphys) == dataphysend ||
    572 		    OHCI_PAGE(dataphys) + OHCI_PAGE_SIZE == dataphysend) {
    573 			/* we can handle it in this TD */
    574 			curlen = len;
    575 		} else {
    576 			/* must use multiple TDs, fill as much as possible. */
    577 			curlen = 2 * OHCI_PAGE_SIZE -
    578 				 (dataphys & (OHCI_PAGE_SIZE-1));
    579 			/* the length must be a multiple of the max size */
    580 			curlen -= curlen % mps;
    581 			KASSERT(curlen != 0);
    582 		}
    583 		DPRINTFN(4, "dataphys=0x%08x dataphysend=0x%08x "
    584 		    "len=%d curlen=%d",  dataphys, dataphysend, len, curlen);
    585 		len -= curlen;
    586 
    587 		cur->td.td_flags = tdflags;
    588 		cur->td.td_cbp = HTOO32(dataphys);
    589 		cur->td.td_nexttd = HTOO32(next->physaddr);
    590 		cur->td.td_be = HTOO32(dataphys + curlen - 1);
    591 		cur->nexttd = next;
    592 		cur->len = curlen;
    593 		cur->flags = OHCI_ADD_LEN;
    594 		cur->xfer = xfer;
    595 
    596 		DPRINTFN(10, "cbp=0x%08x be=0x%08x", dataphys,
    597 		    dataphys + curlen - 1, 0, 0);
    598 		if (len == 0)
    599 			break;
    600 		DPRINTFN(10, "extend chain", 0, 0, 0, 0);
    601 		dataphys += curlen;
    602 		cur = next;
    603 	}
    604 	if (!rd && (flags & USBD_FORCE_SHORT_XFER) &&
    605 	    alen % mps == 0) {
    606 		/* Force a 0 length transfer at the end. */
    607 
    608 		cur = next;
    609 		next = ohci_alloc_std(sc);
    610 		if (next == NULL)
    611 			goto nomem;
    612 
    613 		cur->td.td_flags = tdflags;
    614 		cur->td.td_cbp = 0; /* indicate 0 length packet */
    615 		cur->td.td_nexttd = HTOO32(next->physaddr);
    616 		cur->td.td_be = ~0;
    617 		cur->nexttd = next;
    618 		cur->len = 0;
    619 		cur->flags = 0;
    620 		cur->xfer = xfer;
    621 
    622 		DPRINTFN(2, "add 0 xfer", 0, 0, 0, 0);
    623 	}
    624 
    625 	return USBD_NORMAL_COMPLETION;
    626 
    627  nomem:
    628 	ohci_free_stds(sc, ox);
    629 
    630 	return USBD_NOMEM;
    631 }
    632 
    633 Static void
    634 ohci_free_stds(ohci_softc_t *sc, struct ohci_xfer *ox)
    635 {
    636 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
    637 	DPRINTF("ox=%p", ox, 0, 0, 0);
    638 
    639 	mutex_enter(&sc->sc_lock);
    640 	for (size_t i = 0; i < ox->ox_nstd; i++) {
    641 		ohci_soft_td_t *std = ox->ox_stds[i];
    642 		if (std == NULL)
    643 			break;
    644 		ohci_free_std_locked(sc, std);
    645 	}
    646 	mutex_exit(&sc->sc_lock);
    647 }
    648 
    649 void
    650 ohci_reset_std_chain(ohci_softc_t *sc, struct usbd_xfer *xfer,
    651     int alen, int rd, ohci_soft_td_t *sp, ohci_soft_td_t **ep)
    652 {
    653 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
    654 	ohci_soft_td_t *next, *cur;
    655 	ohci_physaddr_t dataphys, dataphysend;
    656 	uint32_t tdflags;
    657 	int len, curlen;
    658 	usb_dma_t *dma = &xfer->ux_dmabuf;
    659 	uint16_t flags = xfer->ux_flags;
    660 
    661 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
    662 	DPRINTF("start len=%d", alen, 0, 0, 0);
    663 
    664 	KASSERT(mutex_owned(&sc->sc_lock));
    665 
    666 	DPRINTFN(8, "addr=%d endpt=%d len=%d speed=%d",
    667 	    xfer->ux_pipe->up_dev->ud_addr,
    668 	    UE_GET_ADDR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress),
    669 	    alen, xfer->ux_pipe->up_dev->ud_speed);
    670 
    671 	KASSERT(sp);
    672 
    673 	int mps = UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize);
    674 
    675 	len = alen;
    676 	cur = sp;
    677 
    678 	dataphys = DMAADDR(dma, 0);
    679 	dataphysend = OHCI_PAGE(dataphys + len - 1);
    680 	usb_syncmem(dma, 0, len,
    681 	    rd ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    682 	tdflags = HTOO32(
    683 	    (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
    684 	    (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) |
    685 	    OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_NOINTR);
    686 
    687 	for (size_t j = 1;;) {
    688 		if (j == ox->ox_nstd)
    689 			next = NULL;
    690 		else
    691 			next = ox->ox_stds[j++];
    692 		KASSERT(next != cur);
    693 
    694 		/* The OHCI hardware can handle at most one page crossing. */
    695 		if (OHCI_PAGE(dataphys) == dataphysend ||
    696 		    OHCI_PAGE(dataphys) + OHCI_PAGE_SIZE == dataphysend) {
    697 			/* we can handle it in this TD */
    698 			curlen = len;
    699 		} else {
    700 			/* must use multiple TDs, fill as much as possible. */
    701 			curlen = 2 * OHCI_PAGE_SIZE -
    702 				 (dataphys & (OHCI_PAGE_SIZE - 1));
    703 			/* the length must be a multiple of the max size */
    704 			curlen -= curlen % mps;
    705 			KASSERT(curlen != 0);
    706 		}
    707 		DPRINTFN(4, "dataphys=0x%08x dataphysend=0x%08x "
    708 		    "len=%d curlen=%d",  dataphys, dataphysend, len, curlen);
    709 		len -= curlen;
    710 
    711 		cur->td.td_flags = tdflags;
    712 		cur->td.td_cbp = HTOO32(dataphys);
    713 		cur->td.td_be = HTOO32(dataphys + curlen - 1);
    714 		cur->td.td_nexttd = (next != NULL) ? HTOO32(next->physaddr) : 0;
    715 		cur->nexttd = next;
    716 		cur->len = curlen;
    717 		cur->flags = OHCI_ADD_LEN;
    718 		cur->xfer = xfer;
    719 	 	ohci_hash_add_td(sc, cur);
    720 
    721 		usb_syncmem(&cur->dma, cur->offs, sizeof(cur->td),
    722 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    723 		DPRINTFN(10, "cbp=0x%08x be=0x%08x", dataphys,
    724 		    dataphys + curlen - 1, 0, 0);
    725 		if (len == 0)
    726 			break;
    727 		KASSERT(next != NULL);
    728 		DPRINTFN(10, "extend chain", 0, 0, 0, 0);
    729 		dataphys += curlen;
    730 		cur = next;
    731 	}
    732 	if (!rd &&
    733 	    (flags & USBD_FORCE_SHORT_XFER) &&
    734 	    alen % mps == 0) {
    735 		/* Force a 0 length transfer at the end. */
    736 
    737 		KASSERT(next != NULL);
    738 		cur = next;
    739 
    740 		cur->td.td_flags = tdflags;
    741 		cur->td.td_cbp = 0; /* indicate 0 length packet */
    742 		cur->td.td_nexttd = HTOO32(next->physaddr);
    743 		cur->td.td_be = ~0;
    744 		cur->nexttd = NULL;
    745 		cur->len = 0;
    746 		cur->flags = 0;
    747 		cur->xfer = xfer;
    748 	 	ohci_hash_add_td(sc, cur);
    749 
    750 		usb_syncmem(&cur->dma, cur->offs, sizeof(cur->td),
    751 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    752 		DPRINTFN(2, "add 0 xfer", 0, 0, 0, 0);
    753 	}
    754 	*ep = cur;
    755 }
    756 
    757 ohci_soft_itd_t *
    758 ohci_alloc_sitd(ohci_softc_t *sc)
    759 {
    760 	ohci_soft_itd_t *sitd;
    761 	usbd_status err;
    762 	int i, offs;
    763 	usb_dma_t dma;
    764 
    765 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
    766 
    767 	mutex_enter(&sc->sc_lock);
    768 	if (sc->sc_freeitds == NULL) {
    769 		DPRINTFN(2, "allocating chunk", 0, 0, 0, 0);
    770 		mutex_exit(&sc->sc_lock);
    771 
    772 		err = usb_allocmem(&sc->sc_bus, OHCI_SITD_SIZE * OHCI_SITD_CHUNK,
    773 			  OHCI_ITD_ALIGN, &dma);
    774 		if (err)
    775 			return NULL;
    776 		mutex_enter(&sc->sc_lock);
    777 		for(i = 0; i < OHCI_SITD_CHUNK; i++) {
    778 			offs = i * OHCI_SITD_SIZE;
    779 			sitd = KERNADDR(&dma, offs);
    780 			sitd->physaddr = DMAADDR(&dma, offs);
    781 			sitd->dma = dma;
    782 			sitd->offs = offs;
    783 			sitd->nextitd = sc->sc_freeitds;
    784 			sc->sc_freeitds = sitd;
    785 		}
    786 	}
    787 
    788 	sitd = sc->sc_freeitds;
    789 	sc->sc_freeitds = sitd->nextitd;
    790 	mutex_exit(&sc->sc_lock);
    791 
    792 	memset(&sitd->itd, 0, sizeof(ohci_itd_t));
    793 	sitd->nextitd = NULL;
    794 	sitd->xfer = NULL;
    795 
    796 #ifdef DIAGNOSTIC
    797 	sitd->isdone = false;
    798 #endif
    799 
    800 	return sitd;
    801 }
    802 
    803 Static void
    804 ohci_free_sitd_locked(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
    805 {
    806 
    807 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
    808 	DPRINTFN(10, "sitd=%p", sitd, 0, 0, 0);
    809 
    810 	KASSERT(sitd->isdone);
    811 #ifdef DIAGNOSTIC
    812 	/* Warn double free */
    813 	sitd->isdone = false;
    814 #endif
    815 
    816 	sitd->nextitd = sc->sc_freeitds;
    817 	sc->sc_freeitds = sitd;
    818 }
    819 
    820 void
    821 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
    822 {
    823 
    824 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
    825 
    826 	mutex_enter(&sc->sc_lock);
    827 	ohci_free_sitd_locked(sc, sitd);
    828 	mutex_exit(&sc->sc_lock);
    829 }
    830 
    831 int
    832 ohci_init(ohci_softc_t *sc)
    833 {
    834 	ohci_soft_ed_t *sed, *psed;
    835 	usbd_status err;
    836 	int i;
    837 	uint32_t s, ctl, rwc, ival, hcr, fm, per, rev, desca /*, descb */;
    838 
    839 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
    840 
    841 	aprint_normal_dev(sc->sc_dev, "");
    842 
    843 	sc->sc_hcca = NULL;
    844 	callout_init(&sc->sc_tmo_rhsc, CALLOUT_MPSAFE);
    845 
    846 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    847 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
    848 	cv_init(&sc->sc_softwake_cv, "ohciab");
    849 
    850 	sc->sc_rhsc_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
    851 	    ohci_rhsc_softint, sc);
    852 
    853 	for (i = 0; i < OHCI_HASH_SIZE; i++)
    854 		LIST_INIT(&sc->sc_hash_tds[i]);
    855 	for (i = 0; i < OHCI_HASH_SIZE; i++)
    856 		LIST_INIT(&sc->sc_hash_itds[i]);
    857 
    858 	sc->sc_xferpool = pool_cache_init(sizeof(struct ohci_xfer), 0, 0, 0,
    859 	    "ohcixfer", NULL, IPL_USB, NULL, NULL, NULL);
    860 
    861 	rev = OREAD4(sc, OHCI_REVISION);
    862 	aprint_normal("OHCI version %d.%d%s\n",
    863 	    OHCI_REV_HI(rev), OHCI_REV_LO(rev),
    864 	    OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
    865 
    866 	if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
    867 		aprint_error_dev(sc->sc_dev, "unsupported OHCI revision\n");
    868 		sc->sc_bus.ub_revision = USBREV_UNKNOWN;
    869 		return -1;
    870 	}
    871 	sc->sc_bus.ub_revision = USBREV_1_0;
    872 	sc->sc_bus.ub_usedma = true;
    873 
    874 	/* XXX determine alignment by R/W */
    875 	/* Allocate the HCCA area. */
    876 	err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
    877 			 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
    878 	if (err) {
    879 		sc->sc_hcca = NULL;
    880 		return err;
    881 	}
    882 	sc->sc_hcca = KERNADDR(&sc->sc_hccadma, 0);
    883 	memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
    884 
    885 	sc->sc_eintrs = OHCI_NORMAL_INTRS;
    886 
    887 	/* Allocate dummy ED that starts the control list. */
    888 	sc->sc_ctrl_head = ohci_alloc_sed(sc);
    889 	if (sc->sc_ctrl_head == NULL) {
    890 		err = ENOMEM;
    891 		goto bad1;
    892 	}
    893 	sc->sc_ctrl_head->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
    894 
    895 	/* Allocate dummy ED that starts the bulk list. */
    896 	sc->sc_bulk_head = ohci_alloc_sed(sc);
    897 	if (sc->sc_bulk_head == NULL) {
    898 		err = ENOMEM;
    899 		goto bad2;
    900 	}
    901 	sc->sc_bulk_head->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
    902 	usb_syncmem(&sc->sc_bulk_head->dma, sc->sc_bulk_head->offs,
    903 	    sizeof(sc->sc_bulk_head->ed),
    904 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    905 
    906 	/* Allocate dummy ED that starts the isochronous list. */
    907 	sc->sc_isoc_head = ohci_alloc_sed(sc);
    908 	if (sc->sc_isoc_head == NULL) {
    909 		err = ENOMEM;
    910 		goto bad3;
    911 	}
    912 	sc->sc_isoc_head->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
    913 	usb_syncmem(&sc->sc_isoc_head->dma, sc->sc_isoc_head->offs,
    914 	    sizeof(sc->sc_isoc_head->ed),
    915 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    916 
    917 	/* Allocate all the dummy EDs that make up the interrupt tree. */
    918 	for (i = 0; i < OHCI_NO_EDS; i++) {
    919 		sed = ohci_alloc_sed(sc);
    920 		if (sed == NULL) {
    921 			while (--i >= 0)
    922 				ohci_free_sed(sc, sc->sc_eds[i]);
    923 			err = ENOMEM;
    924 			goto bad4;
    925 		}
    926 		/* All ED fields are set to 0. */
    927 		sc->sc_eds[i] = sed;
    928 		sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
    929 		if (i != 0)
    930 			psed = sc->sc_eds[(i-1) / 2];
    931 		else
    932 			psed= sc->sc_isoc_head;
    933 		sed->next = psed;
    934 		sed->ed.ed_nexted = HTOO32(psed->physaddr);
    935 		usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
    936 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    937 	}
    938 	/*
    939 	 * Fill HCCA interrupt table.  The bit reversal is to get
    940 	 * the tree set up properly to spread the interrupts.
    941 	 */
    942 	for (i = 0; i < OHCI_NO_INTRS; i++)
    943 		sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
    944 		    HTOO32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
    945 	usb_syncmem(&sc->sc_hccadma, 0, OHCI_HCCA_SIZE,
    946 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    947 
    948 #ifdef OHCI_DEBUG
    949 	DPRINTFN(15, "--- dump start ---", 0, 0, 0 ,0);
    950 	if (ohcidebug >= 15) {
    951 		for (i = 0; i < OHCI_NO_EDS; i++) {
    952 			DPRINTFN(15, "ed#%d ", i, 0, 0, 0);
    953 			ohci_dump_ed(sc, sc->sc_eds[i]);
    954 		}
    955 		DPRINTFN(15, "iso", 0, 0, 0 ,0);
    956 		ohci_dump_ed(sc, sc->sc_isoc_head);
    957 	}
    958 	DPRINTFN(15, "--- dump end ---", 0, 0, 0 ,0);
    959 #endif
    960 
    961 	/* Preserve values programmed by SMM/BIOS but lost over reset. */
    962 	ctl = OREAD4(sc, OHCI_CONTROL);
    963 	rwc = ctl & OHCI_RWC;
    964 	fm = OREAD4(sc, OHCI_FM_INTERVAL);
    965 	desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
    966 	/* descb = OREAD4(sc, OHCI_RH_DESCRIPTOR_B); */
    967 
    968 	/* Determine in what context we are running. */
    969 	if (ctl & OHCI_IR) {
    970 		/* SMM active, request change */
    971 		DPRINTF("SMM active, request owner change", 0, 0, 0, 0);
    972 		if ((sc->sc_intre & (OHCI_OC | OHCI_MIE)) ==
    973 		    (OHCI_OC | OHCI_MIE))
    974 			OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_MIE);
    975 		s = OREAD4(sc, OHCI_COMMAND_STATUS);
    976 		OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
    977 		for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
    978 			usb_delay_ms(&sc->sc_bus, 1);
    979 			ctl = OREAD4(sc, OHCI_CONTROL);
    980 		}
    981 		OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_MIE);
    982 		if ((ctl & OHCI_IR) == 0) {
    983 			aprint_error_dev(sc->sc_dev,
    984 			    "SMM does not respond, resetting\n");
    985 			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET | rwc);
    986 			goto reset;
    987 		}
    988 #if 0
    989 /* Don't bother trying to reuse the BIOS init, we'll reset it anyway. */
    990 	} else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
    991 		/* BIOS started controller. */
    992 		DPRINTF("BIOS active", 0, 0, 0, 0);
    993 		if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
    994 			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL | rwc);
    995 			usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
    996 		}
    997 #endif
    998 	} else {
    999 		DPRINTF("cold started", 0 ,0 ,0 ,0);
   1000 	reset:
   1001 		/* Controller was cold started. */
   1002 		usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
   1003 	}
   1004 
   1005 	/*
   1006 	 * This reset should not be necessary according to the OHCI spec, but
   1007 	 * without it some controllers do not start.
   1008 	 */
   1009 	DPRINTF("sc %p: resetting", sc, 0, 0, 0);
   1010 	OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET | rwc);
   1011 	usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
   1012 
   1013 	/* We now own the host controller and the bus has been reset. */
   1014 
   1015 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
   1016 	/* Nominal time for a reset is 10 us. */
   1017 	for (i = 0; i < 10; i++) {
   1018 		delay(10);
   1019 		hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
   1020 		if (!hcr)
   1021 			break;
   1022 	}
   1023 	if (hcr) {
   1024 		aprint_error_dev(sc->sc_dev, "reset timeout\n");
   1025 		err = EIO;
   1026 		goto bad5;
   1027 	}
   1028 #ifdef OHCI_DEBUG
   1029 	if (ohcidebug >= 15)
   1030 		ohci_dumpregs(sc);
   1031 #endif
   1032 
   1033 	/* The controller is now in SUSPEND state, we have 2ms to finish. */
   1034 
   1035 	/* Set up HC registers. */
   1036 	OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
   1037 	OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
   1038 	OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
   1039 	/* disable all interrupts and then switch on all desired interrupts */
   1040 	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
   1041 	/* switch on desired functional features */
   1042 	ctl = OREAD4(sc, OHCI_CONTROL);
   1043 	ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
   1044 	ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
   1045 		OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL | rwc;
   1046 	/* And finally start it! */
   1047 	OWRITE4(sc, OHCI_CONTROL, ctl);
   1048 
   1049 	/*
   1050 	 * The controller is now OPERATIONAL.  Set a some final
   1051 	 * registers that should be set earlier, but that the
   1052 	 * controller ignores when in the SUSPEND state.
   1053 	 */
   1054 	ival = OHCI_GET_IVAL(fm);
   1055 	fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
   1056 	fm |= OHCI_FSMPS(ival) | ival;
   1057 	OWRITE4(sc, OHCI_FM_INTERVAL, fm);
   1058 	per = OHCI_PERIODIC(ival); /* 90% periodic */
   1059 	OWRITE4(sc, OHCI_PERIODIC_START, per);
   1060 
   1061 	if (sc->sc_flags & OHCIF_SUPERIO) {
   1062 		/* no overcurrent protection */
   1063 		desca |= OHCI_NOCP;
   1064 		/*
   1065 		 * Clear NoPowerSwitching and PowerOnToPowerGoodTime meaning
   1066 		 * that
   1067 		 *  - ports are always power switched
   1068 		 *  - don't wait for powered root hub port
   1069 		 */
   1070 		desca &= ~(__SHIFTIN(0xff, OHCI_POTPGT_MASK) | OHCI_NPS);
   1071 	}
   1072 
   1073 	/* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
   1074 	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
   1075 	OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
   1076 	usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY);
   1077 	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
   1078 
   1079 	/*
   1080 	 * The AMD756 requires a delay before re-reading the register,
   1081 	 * otherwise it will occasionally report 0 ports.
   1082 	 */
   1083 	sc->sc_noport = 0;
   1084 	for (i = 0; i < 10 && sc->sc_noport == 0; i++) {
   1085 		usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY);
   1086 		sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
   1087 	}
   1088 
   1089 #ifdef OHCI_DEBUG
   1090 	if (ohcidebug >= 5)
   1091 		ohci_dumpregs(sc);
   1092 #endif
   1093 
   1094 	/* Set up the bus struct. */
   1095 	sc->sc_bus.ub_methods = &ohci_bus_methods;
   1096 	sc->sc_bus.ub_pipesize = sizeof(struct ohci_pipe);
   1097 
   1098 	sc->sc_control = sc->sc_intre = 0;
   1099 
   1100 	/* Finally, turn on interrupts. */
   1101 	DPRINTF("enabling %#x", sc->sc_eintrs | OHCI_MIE, 0, 0, 0);
   1102 	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
   1103 
   1104 	return 0;
   1105 
   1106  bad5:
   1107 	for (i = 0; i < OHCI_NO_EDS; i++)
   1108 		ohci_free_sed(sc, sc->sc_eds[i]);
   1109  bad4:
   1110 	ohci_free_sed(sc, sc->sc_isoc_head);
   1111  bad3:
   1112 	ohci_free_sed(sc, sc->sc_bulk_head);
   1113  bad2:
   1114 	ohci_free_sed(sc, sc->sc_ctrl_head);
   1115  bad1:
   1116 	usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
   1117 	sc->sc_hcca = NULL;
   1118 	return err;
   1119 }
   1120 
   1121 struct usbd_xfer *
   1122 ohci_allocx(struct usbd_bus *bus, unsigned int nframes)
   1123 {
   1124 	ohci_softc_t *sc = OHCI_BUS2SC(bus);
   1125 	struct usbd_xfer *xfer;
   1126 
   1127 	xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
   1128 	if (xfer != NULL) {
   1129 		memset(xfer, 0, sizeof(struct ohci_xfer));
   1130 #ifdef DIAGNOSTIC
   1131 		xfer->ux_state = XFER_BUSY;
   1132 #endif
   1133 	}
   1134 	return xfer;
   1135 }
   1136 
   1137 void
   1138 ohci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
   1139 {
   1140 	ohci_softc_t *sc = OHCI_BUS2SC(bus);
   1141 
   1142 	KASSERTMSG(xfer->ux_state == XFER_BUSY,
   1143 	    "xfer=%p not busy, 0x%08x\n", xfer, xfer->ux_state);
   1144 #ifdef DIAGNOSTIC
   1145 	xfer->ux_state = XFER_FREE;
   1146 #endif
   1147 	pool_cache_put(sc->sc_xferpool, xfer);
   1148 }
   1149 
   1150 Static void
   1151 ohci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
   1152 {
   1153 	ohci_softc_t *sc = OHCI_BUS2SC(bus);
   1154 
   1155 	*lock = &sc->sc_lock;
   1156 }
   1157 
   1158 /*
   1159  * Shut down the controller when the system is going down.
   1160  */
   1161 bool
   1162 ohci_shutdown(device_t self, int flags)
   1163 {
   1164 	ohci_softc_t *sc = device_private(self);
   1165 
   1166 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1167 
   1168 	DPRINTF("stopping the HC", 0, 0, 0, 0);
   1169 	OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
   1170 	return true;
   1171 }
   1172 
   1173 bool
   1174 ohci_resume(device_t dv, const pmf_qual_t *qual)
   1175 {
   1176 	ohci_softc_t *sc = device_private(dv);
   1177 	uint32_t ctl;
   1178 
   1179 	mutex_spin_enter(&sc->sc_intr_lock);
   1180 	sc->sc_bus.ub_usepolling++;
   1181 	mutex_spin_exit(&sc->sc_intr_lock);
   1182 
   1183 	/* Some broken BIOSes do not recover these values */
   1184 	OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
   1185 	OWRITE4(sc, OHCI_CONTROL_HEAD_ED,
   1186 	    sc->sc_ctrl_head->physaddr);
   1187 	OWRITE4(sc, OHCI_BULK_HEAD_ED,
   1188 	    sc->sc_bulk_head->physaddr);
   1189 	if (sc->sc_intre)
   1190 		OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_intre &
   1191 		    (OHCI_ALL_INTRS | OHCI_MIE));
   1192 	if (sc->sc_control)
   1193 		ctl = sc->sc_control;
   1194 	else
   1195 		ctl = OREAD4(sc, OHCI_CONTROL);
   1196 	ctl |= OHCI_HCFS_RESUME;
   1197 	OWRITE4(sc, OHCI_CONTROL, ctl);
   1198 	usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
   1199 	ctl = (ctl & ~OHCI_HCFS_MASK) | OHCI_HCFS_OPERATIONAL;
   1200 	OWRITE4(sc, OHCI_CONTROL, ctl);
   1201 	usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
   1202 	sc->sc_control = sc->sc_intre = 0;
   1203 
   1204 	mutex_spin_enter(&sc->sc_intr_lock);
   1205 	sc->sc_bus.ub_usepolling--;
   1206 	mutex_spin_exit(&sc->sc_intr_lock);
   1207 
   1208 	return true;
   1209 }
   1210 
   1211 bool
   1212 ohci_suspend(device_t dv, const pmf_qual_t *qual)
   1213 {
   1214 	ohci_softc_t *sc = device_private(dv);
   1215 	uint32_t ctl;
   1216 
   1217 	mutex_spin_enter(&sc->sc_intr_lock);
   1218 	sc->sc_bus.ub_usepolling++;
   1219 	mutex_spin_exit(&sc->sc_intr_lock);
   1220 
   1221 	ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK;
   1222 	if (sc->sc_control == 0) {
   1223 		/*
   1224 		 * Preserve register values, in case that BIOS
   1225 		 * does not recover them.
   1226 		 */
   1227 		sc->sc_control = ctl;
   1228 		sc->sc_intre = OREAD4(sc,
   1229 		    OHCI_INTERRUPT_ENABLE);
   1230 	}
   1231 	ctl |= OHCI_HCFS_SUSPEND;
   1232 	OWRITE4(sc, OHCI_CONTROL, ctl);
   1233 	usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
   1234 
   1235 	mutex_spin_enter(&sc->sc_intr_lock);
   1236 	sc->sc_bus.ub_usepolling--;
   1237 	mutex_spin_exit(&sc->sc_intr_lock);
   1238 
   1239 	return true;
   1240 }
   1241 
   1242 #ifdef OHCI_DEBUG
   1243 void
   1244 ohci_dumpregs(ohci_softc_t *sc)
   1245 {
   1246 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1247 
   1248 	DPRINTF("rev=0x%08x control=0x%08x command=0x%08x",
   1249 		 OREAD4(sc, OHCI_REVISION),
   1250 		 OREAD4(sc, OHCI_CONTROL),
   1251 		 OREAD4(sc, OHCI_COMMAND_STATUS), 0);
   1252 	DPRINTF("               intrstat=0x%08x intre=0x%08x intrd=0x%08x",
   1253 		 OREAD4(sc, OHCI_INTERRUPT_STATUS),
   1254 		 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
   1255 		 OREAD4(sc, OHCI_INTERRUPT_DISABLE), 0);
   1256 	DPRINTF("               hcca=0x%08x percur=0x%08x ctrlhd=0x%08x",
   1257 		 OREAD4(sc, OHCI_HCCA),
   1258 		 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
   1259 		 OREAD4(sc, OHCI_CONTROL_HEAD_ED), 0);
   1260 	DPRINTF("               ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x",
   1261 		 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
   1262 		 OREAD4(sc, OHCI_BULK_HEAD_ED),
   1263 		 OREAD4(sc, OHCI_BULK_CURRENT_ED) ,0);
   1264 	DPRINTF("               done=0x%08x fmival=0x%08x fmrem=0x%08x",
   1265 		 OREAD4(sc, OHCI_DONE_HEAD),
   1266 		 OREAD4(sc, OHCI_FM_INTERVAL),
   1267 		 OREAD4(sc, OHCI_FM_REMAINING), 0);
   1268 	DPRINTF("               fmnum=0x%08x perst=0x%08x lsthrs=0x%08x",
   1269 		 OREAD4(sc, OHCI_FM_NUMBER),
   1270 		 OREAD4(sc, OHCI_PERIODIC_START),
   1271 		 OREAD4(sc, OHCI_LS_THRESHOLD), 0);
   1272 	DPRINTF("               desca=0x%08x descb=0x%08x stat=0x%08x",
   1273 		 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
   1274 		 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
   1275 		 OREAD4(sc, OHCI_RH_STATUS), 0);
   1276 	DPRINTF("               port1=0x%08x port2=0x%08x",
   1277 		 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
   1278 		 OREAD4(sc, OHCI_RH_PORT_STATUS(2)), 0, 0);
   1279 	DPRINTF("         HCCA: frame_number=0x%04x done_head=0x%08x",
   1280 		 O32TOH(sc->sc_hcca->hcca_frame_number),
   1281 		 O32TOH(sc->sc_hcca->hcca_done_head), 0, 0);
   1282 }
   1283 #endif
   1284 
   1285 Static int ohci_intr1(ohci_softc_t *);
   1286 
   1287 int
   1288 ohci_intr(void *p)
   1289 {
   1290 	ohci_softc_t *sc = p;
   1291 	int ret = 0;
   1292 
   1293 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1294 
   1295 	if (sc == NULL)
   1296 		return 0;
   1297 
   1298 	mutex_spin_enter(&sc->sc_intr_lock);
   1299 
   1300 	if (sc->sc_dying || !device_has_power(sc->sc_dev))
   1301 		goto done;
   1302 
   1303 	/* If we get an interrupt while polling, then just ignore it. */
   1304 	if (sc->sc_bus.ub_usepolling) {
   1305 		DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
   1306 		/* for level triggered intrs, should do something to ack */
   1307 		OWRITE4(sc, OHCI_INTERRUPT_STATUS,
   1308 			OREAD4(sc, OHCI_INTERRUPT_STATUS));
   1309 
   1310 		goto done;
   1311 	}
   1312 
   1313 	ret = ohci_intr1(sc);
   1314 
   1315 done:
   1316 	mutex_spin_exit(&sc->sc_intr_lock);
   1317 	return ret;
   1318 }
   1319 
   1320 Static int
   1321 ohci_intr1(ohci_softc_t *sc)
   1322 {
   1323 	uint32_t intrs, eintrs;
   1324 
   1325 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1326 
   1327 	/* In case the interrupt occurs before initialization has completed. */
   1328 	if (sc == NULL || sc->sc_hcca == NULL) {
   1329 #ifdef DIAGNOSTIC
   1330 		printf("ohci_intr: sc->sc_hcca == NULL\n");
   1331 #endif
   1332 		return 0;
   1333 	}
   1334 
   1335 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1336 
   1337 	intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
   1338 	if (!intrs)
   1339 		return 0;
   1340 
   1341 	/* Acknowledge */
   1342 	OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs & ~(OHCI_MIE|OHCI_WDH));
   1343 	eintrs = intrs & sc->sc_eintrs;
   1344 	DPRINTFN(7, "sc=%p", sc, 0, 0, 0);
   1345 	DPRINTFN(7, "intrs=%#x(%#x) eintrs=%#x(%#x)",
   1346 	    intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS), eintrs,
   1347 	    sc->sc_eintrs);
   1348 
   1349 	if (!eintrs) {
   1350 		return 0;
   1351 	}
   1352 
   1353 	if (eintrs & OHCI_SO) {
   1354 		sc->sc_overrun_cnt++;
   1355 		if (usbd_ratecheck(&sc->sc_overrun_ntc)) {
   1356 			printf("%s: %u scheduling overruns\n",
   1357 			    device_xname(sc->sc_dev), sc->sc_overrun_cnt);
   1358 			sc->sc_overrun_cnt = 0;
   1359 		}
   1360 		/* XXX do what */
   1361 		eintrs &= ~OHCI_SO;
   1362 	}
   1363 	if (eintrs & OHCI_WDH) {
   1364 		/*
   1365 		 * We block the interrupt below, and reenable it later from
   1366 		 * ohci_softintr().
   1367 		 */
   1368 		usb_schedsoftintr(&sc->sc_bus);
   1369 	}
   1370 	if (eintrs & OHCI_RD) {
   1371 		printf("%s: resume detect\n", device_xname(sc->sc_dev));
   1372 		/* XXX process resume detect */
   1373 	}
   1374 	if (eintrs & OHCI_UE) {
   1375 		printf("%s: unrecoverable error, controller halted\n",
   1376 		       device_xname(sc->sc_dev));
   1377 		OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
   1378 		/* XXX what else */
   1379 	}
   1380 	if (eintrs & OHCI_RHSC) {
   1381 		/*
   1382 		 * We block the interrupt below, and reenable it later from
   1383 		 * a timeout.
   1384 		 */
   1385 		softint_schedule(sc->sc_rhsc_si);
   1386 	}
   1387 
   1388 	if (eintrs != 0) {
   1389 		/* Block unprocessed interrupts. */
   1390 		OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs);
   1391 		sc->sc_eintrs &= ~eintrs;
   1392 		DPRINTF("sc %p blocking intrs 0x%x", sc, eintrs, 0, 0);
   1393 	}
   1394 
   1395 	return 1;
   1396 }
   1397 
   1398 void
   1399 ohci_rhsc_enable(void *v_sc)
   1400 {
   1401 	ohci_softc_t *sc = v_sc;
   1402 
   1403 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1404 	DPRINTF("sc %p", sc, 0, 0, 0);
   1405 	mutex_spin_enter(&sc->sc_intr_lock);
   1406 	sc->sc_eintrs |= OHCI_RHSC;
   1407 	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
   1408 	mutex_spin_exit(&sc->sc_intr_lock);
   1409 }
   1410 
   1411 #ifdef OHCI_DEBUG
   1412 const char *ohci_cc_strs[] = {
   1413 	"NO_ERROR",
   1414 	"CRC",
   1415 	"BIT_STUFFING",
   1416 	"DATA_TOGGLE_MISMATCH",
   1417 	"STALL",
   1418 	"DEVICE_NOT_RESPONDING",
   1419 	"PID_CHECK_FAILURE",
   1420 	"UNEXPECTED_PID",
   1421 	"DATA_OVERRUN",
   1422 	"DATA_UNDERRUN",
   1423 	"BUFFER_OVERRUN",
   1424 	"BUFFER_UNDERRUN",
   1425 	"reserved",
   1426 	"reserved",
   1427 	"NOT_ACCESSED",
   1428 	"NOT_ACCESSED",
   1429 };
   1430 #endif
   1431 
   1432 void
   1433 ohci_softintr(void *v)
   1434 {
   1435 	struct usbd_bus *bus = v;
   1436 	ohci_softc_t *sc = OHCI_BUS2SC(bus);
   1437 	ohci_soft_itd_t *sitd, *sidone, *sitdnext;
   1438 	ohci_soft_td_t  *std,  *sdone,  *stdnext;
   1439 	struct usbd_xfer *xfer;
   1440 	struct ohci_pipe *opipe;
   1441 	int len, cc;
   1442 	int i, j, actlen, iframes, uedir;
   1443 	ohci_physaddr_t done;
   1444 
   1445 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   1446 
   1447 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1448 
   1449 	usb_syncmem(&sc->sc_hccadma, offsetof(struct ohci_hcca, hcca_done_head),
   1450 	    sizeof(sc->sc_hcca->hcca_done_head),
   1451 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1452 	done = O32TOH(sc->sc_hcca->hcca_done_head) & ~OHCI_DONE_INTRS;
   1453 	sc->sc_hcca->hcca_done_head = 0;
   1454 	usb_syncmem(&sc->sc_hccadma, offsetof(struct ohci_hcca, hcca_done_head),
   1455 	    sizeof(sc->sc_hcca->hcca_done_head),
   1456 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1457 	OWRITE4(sc, OHCI_INTERRUPT_STATUS, OHCI_WDH);
   1458 	sc->sc_eintrs |= OHCI_WDH;
   1459 	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_WDH);
   1460 
   1461 	/* Reverse the done list. */
   1462 	for (sdone = NULL, sidone = NULL; done != 0; ) {
   1463 		std = ohci_hash_find_td(sc, done);
   1464 		if (std != NULL) {
   1465 			usb_syncmem(&std->dma, std->offs, sizeof(std->td),
   1466 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1467 			std->dnext = sdone;
   1468 			done = O32TOH(std->td.td_nexttd);
   1469 			sdone = std;
   1470 			DPRINTFN(10, "add TD %p", std, 0, 0, 0);
   1471 			continue;
   1472 		}
   1473 		sitd = ohci_hash_find_itd(sc, done);
   1474 		if (sitd != NULL) {
   1475 			usb_syncmem(&sitd->dma, sitd->offs, sizeof(sitd->itd),
   1476 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1477 			sitd->dnext = sidone;
   1478 			done = O32TOH(sitd->itd.itd_nextitd);
   1479 			sidone = sitd;
   1480 			DPRINTFN(5, "add ITD %p", sitd, 0, 0, 0);
   1481 			continue;
   1482 		}
   1483 		device_printf(sc->sc_dev, "WARNING: addr 0x%08lx not found\n",
   1484 		    (u_long)done);
   1485 		break;
   1486 	}
   1487 
   1488 	DPRINTFN(10, "sdone=%p sidone=%p", sdone, sidone, 0, 0);
   1489 	DPRINTFN(10, "--- TD dump start ---", 0, 0, 0, 0);
   1490 #ifdef OHCI_DEBUG
   1491 	if (ohcidebug >= 10) {
   1492 		for (std = sdone; std; std = std->dnext)
   1493 			ohci_dump_td(sc, std);
   1494 	}
   1495 #endif
   1496 	DPRINTFN(10, "--- TD dump end ---", 0, 0, 0, 0);
   1497 
   1498 	for (std = sdone; std; std = stdnext) {
   1499 		xfer = std->xfer;
   1500 		stdnext = std->dnext;
   1501 		DPRINTFN(10, "std=%p xfer=%p hcpriv=%p", std, xfer,
   1502 		    xfer ? xfer->ux_hcpriv : 0, 0);
   1503 		if (xfer == NULL) {
   1504 			/*
   1505 			 * xfer == NULL: There seems to be no xfer associated
   1506 			 * with this TD. It is tailp that happened to end up on
   1507 			 * the done queue.
   1508 			 * Shouldn't happen, but some chips are broken(?).
   1509 			 */
   1510 			continue;
   1511 		}
   1512 		if (xfer->ux_status == USBD_CANCELLED ||
   1513 		    xfer->ux_status == USBD_TIMEOUT) {
   1514 			DPRINTF("cancel/timeout %p", xfer, 0, 0, 0);
   1515 			/* Handled by abort routine. */
   1516 			continue;
   1517 		}
   1518 		callout_stop(&xfer->ux_callout);
   1519 
   1520 		len = std->len;
   1521 		if (std->td.td_cbp != 0)
   1522 			len -= O32TOH(std->td.td_be) -
   1523 			       O32TOH(std->td.td_cbp) + 1;
   1524 		DPRINTFN(10, "len=%d, flags=0x%x", len, std->flags, 0, 0);
   1525 		if (std->flags & OHCI_ADD_LEN)
   1526 			xfer->ux_actlen += len;
   1527 
   1528 		cc = OHCI_TD_GET_CC(O32TOH(std->td.td_flags));
   1529 		if (cc == OHCI_CC_NO_ERROR) {
   1530 			ohci_hash_rem_td(sc, std);
   1531 			if (std->flags & OHCI_CALL_DONE) {
   1532 				xfer->ux_status = USBD_NORMAL_COMPLETION;
   1533 				usb_transfer_complete(xfer);
   1534 			}
   1535 		} else {
   1536 			/*
   1537 			 * Endpoint is halted.  First unlink all the TDs
   1538 			 * belonging to the failed transfer, and then restart
   1539 			 * the endpoint.
   1540 			 */
   1541 			ohci_soft_td_t *p, *n;
   1542 			opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   1543 
   1544 			DPRINTFN(15, "error cc=%d",
   1545 			    OHCI_TD_GET_CC(O32TOH(std->td.td_flags)), 0, 0, 0);
   1546 
   1547 			/* remove xfer's TDs from the hash */
   1548 			for (p = std; p->xfer == xfer; p = n) {
   1549 				n = p->nexttd;
   1550 				ohci_hash_rem_td(sc, p);
   1551 			}
   1552 
   1553 			/* clear halt */
   1554 			opipe->sed->ed.ed_headp = HTOO32(p->physaddr);
   1555 			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
   1556 
   1557 			if (cc == OHCI_CC_STALL)
   1558 				xfer->ux_status = USBD_STALLED;
   1559 			else
   1560 				xfer->ux_status = USBD_IOERROR;
   1561 			usb_transfer_complete(xfer);
   1562 		}
   1563 	}
   1564 	DPRINTFN(10, "--- ITD dump start ---", 0, 0, 0, 0);
   1565 #ifdef OHCI_DEBUG
   1566 	if (ohcidebug >= 10) {
   1567 		for (sitd = sidone; sitd; sitd = sitd->dnext)
   1568 			ohci_dump_itd(sc, sitd);
   1569 	}
   1570 #endif
   1571 	DPRINTFN(10, "--- ITD dump end ---", 0, 0, 0, 0);
   1572 
   1573 	for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
   1574 		xfer = sitd->xfer;
   1575 		sitdnext = sitd->dnext;
   1576 		DPRINTFN(1, "sitd=%p xfer=%p hcpriv=%p", sitd, xfer,
   1577 		    xfer ? xfer->ux_hcpriv : 0, 0);
   1578 		if (xfer == NULL)
   1579 			continue;
   1580 		if (xfer->ux_status == USBD_CANCELLED ||
   1581 		    xfer->ux_status == USBD_TIMEOUT) {
   1582 			DPRINTF("cancel/timeout %p", xfer, 0, 0, 0);
   1583 			/* Handled by abort routine. */
   1584 			continue;
   1585 		}
   1586 		KASSERT(!sitd->isdone);
   1587 #ifdef DIAGNOSTIC
   1588 		sitd->isdone = true;
   1589 #endif
   1590 		if (sitd->flags & OHCI_CALL_DONE) {
   1591 			ohci_soft_itd_t *next;
   1592 
   1593 			opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   1594 			opipe->isoc.inuse -= xfer->ux_nframes;
   1595 			uedir = UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->
   1596 			    bEndpointAddress);
   1597 			xfer->ux_status = USBD_NORMAL_COMPLETION;
   1598 			actlen = 0;
   1599 			for (i = 0, sitd = xfer->ux_hcpriv;;
   1600 			    sitd = next) {
   1601 				next = sitd->nextitd;
   1602 				if (OHCI_ITD_GET_CC(O32TOH(sitd->
   1603 				    itd.itd_flags)) != OHCI_CC_NO_ERROR)
   1604 					xfer->ux_status = USBD_IOERROR;
   1605 				/* For input, update frlengths with actual */
   1606 				/* XXX anything necessary for output? */
   1607 				if (uedir == UE_DIR_IN &&
   1608 				    xfer->ux_status == USBD_NORMAL_COMPLETION) {
   1609 					iframes = OHCI_ITD_GET_FC(O32TOH(
   1610 					    sitd->itd.itd_flags));
   1611 					for (j = 0; j < iframes; i++, j++) {
   1612 						len = O16TOH(sitd->
   1613 						    itd.itd_offset[j]);
   1614 						if ((OHCI_ITD_PSW_GET_CC(len) &
   1615 						    OHCI_CC_NOT_ACCESSED_MASK)
   1616 						    == OHCI_CC_NOT_ACCESSED)
   1617 							len = 0;
   1618 						else
   1619 							len = OHCI_ITD_PSW_LENGTH(len);
   1620 						xfer->ux_frlengths[i] = len;
   1621 						actlen += len;
   1622 					}
   1623 				}
   1624 				if (sitd->flags & OHCI_CALL_DONE)
   1625 					break;
   1626 				ohci_hash_rem_itd(sc, sitd);
   1627 
   1628 			}
   1629 			ohci_hash_rem_itd(sc, sitd);
   1630 			if (uedir == UE_DIR_IN &&
   1631 			    xfer->ux_status == USBD_NORMAL_COMPLETION)
   1632 				xfer->ux_actlen = actlen;
   1633 			xfer->ux_hcpriv = NULL;
   1634 
   1635 			usb_transfer_complete(xfer);
   1636 		}
   1637 	}
   1638 
   1639 	if (sc->sc_softwake) {
   1640 		sc->sc_softwake = 0;
   1641 		cv_broadcast(&sc->sc_softwake_cv);
   1642 	}
   1643 
   1644 	DPRINTFN(10, "done", 0, 0, 0, 0);
   1645 }
   1646 
   1647 void
   1648 ohci_device_ctrl_done(struct usbd_xfer *xfer)
   1649 {
   1650 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   1651 	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);
   1652 	int len = UGETW(xfer->ux_request.wLength);
   1653 	int isread = (xfer->ux_request.bmRequestType & UT_READ);
   1654 
   1655 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1656 	DPRINTFN(10, "xfer=%p", xfer, 0, 0, 0);
   1657 
   1658 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   1659 	KASSERT(xfer->ux_rqflags & URQ_REQUEST);
   1660 
   1661 	if (len)
   1662 		usb_syncmem(&xfer->ux_dmabuf, 0, len,
   1663 		    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1664 	usb_syncmem(&opipe->ctrl.reqdma, 0,
   1665 	    sizeof(usb_device_request_t),  BUS_DMASYNC_POSTWRITE);
   1666 }
   1667 
   1668 void
   1669 ohci_device_intr_done(struct usbd_xfer *xfer)
   1670 {
   1671 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   1672 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   1673 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   1674 	ohci_soft_ed_t *sed = opipe->sed;
   1675 	int isread =
   1676 	    (UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN);
   1677 
   1678 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1679 	DPRINTFN(10, "xfer=%p, actlen=%d", xfer, xfer->ux_actlen, 0, 0);
   1680 
   1681 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   1682 
   1683 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   1684 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1685 	if (xfer->ux_pipe->up_repeat) {
   1686 	    	ohci_soft_td_t *data, *last, *tail;
   1687 		int len = xfer->ux_length;
   1688 
   1689 		/*
   1690 		 * Use the pipe "tail" TD as our first and loan our first TD
   1691 		 * to the next transfer.
   1692 		 */
   1693 		data = opipe->tail.td;
   1694 		opipe->tail.td = ox->ox_stds[0];
   1695 		ox->ox_stds[0] = data;
   1696 		ohci_reset_std_chain(sc, xfer, len, isread, data, &last);
   1697 
   1698 		tail = opipe->tail.td;	/* point at sentinel */
   1699 		memset(&tail->td, 0, sizeof(tail->td));
   1700 		tail->nexttd = NULL;
   1701 		tail->xfer = NULL;
   1702 		usb_syncmem(&tail->dma, tail->offs, sizeof(tail->td),
   1703 		    BUS_DMASYNC_PREWRITE);
   1704 
   1705 		/* We want interrupt at the end of the transfer. */
   1706 		last->td.td_flags &= HTOO32(~OHCI_TD_INTR_MASK);
   1707 		last->td.td_flags |= HTOO32(OHCI_TD_SET_DI(1));
   1708 
   1709 		last->td.td_nexttd = HTOO32(tail->physaddr);
   1710 		last->nexttd = tail;
   1711 		last->flags |= OHCI_CALL_DONE;
   1712 		usb_syncmem(&last->dma, last->offs, sizeof(last->td),
   1713 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1714 
   1715 		xfer->ux_hcpriv = data;
   1716 		xfer->ux_actlen = 0;
   1717 
   1718 		/* Insert ED in schedule */
   1719 		sed->ed.ed_tailp = HTOO32(tail->physaddr);
   1720 		usb_syncmem(&sed->dma,
   1721 		    sed->offs + offsetof(ohci_ed_t, ed_tailp),
   1722 		    sizeof(sed->ed.ed_tailp),
   1723 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1724 	}
   1725 }
   1726 
   1727 void
   1728 ohci_device_bulk_done(struct usbd_xfer *xfer)
   1729 {
   1730 	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);
   1731 
   1732 	int isread =
   1733 	    (UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN);
   1734 
   1735 	KASSERT(mutex_owned(&sc->sc_lock));
   1736 
   1737 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1738 	DPRINTFN(10, "xfer=%p, actlen=%d", xfer, xfer->ux_actlen, 0, 0);
   1739 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   1740 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1741 }
   1742 
   1743 Static void
   1744 ohci_rhsc_softint(void *arg)
   1745 {
   1746 	ohci_softc_t *sc = arg;
   1747 
   1748 	mutex_enter(&sc->sc_lock);
   1749 
   1750 	ohci_rhsc(sc, sc->sc_intrxfer);
   1751 
   1752 	/* Do not allow RHSC interrupts > 1 per second */
   1753 	callout_reset(&sc->sc_tmo_rhsc, hz, ohci_rhsc_enable, sc);
   1754 
   1755 	mutex_exit(&sc->sc_lock);
   1756 }
   1757 
   1758 void
   1759 ohci_rhsc(ohci_softc_t *sc, struct usbd_xfer *xfer)
   1760 {
   1761 	u_char *p;
   1762 	int i, m;
   1763 	int hstatus __unused;
   1764 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1765 
   1766 	KASSERT(mutex_owned(&sc->sc_lock));
   1767 
   1768 	hstatus = OREAD4(sc, OHCI_RH_STATUS);
   1769 	DPRINTF("sc=%p xfer=%p hstatus=0x%08x", sc, xfer, hstatus, 0);
   1770 
   1771 	if (xfer == NULL) {
   1772 		/* Just ignore the change. */
   1773 		return;
   1774 	}
   1775 
   1776 	p = xfer->ux_buf;
   1777 	m = min(sc->sc_noport, xfer->ux_length * 8 - 1);
   1778 	memset(p, 0, xfer->ux_length);
   1779 	for (i = 1; i <= m; i++) {
   1780 		/* Pick out CHANGE bits from the status reg. */
   1781 		if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
   1782 			p[i/8] |= 1 << (i%8);
   1783 	}
   1784 	DPRINTF("change=0x%02x", *p, 0, 0, 0);
   1785 	xfer->ux_actlen = xfer->ux_length;
   1786 	xfer->ux_status = USBD_NORMAL_COMPLETION;
   1787 
   1788 	usb_transfer_complete(xfer);
   1789 }
   1790 
   1791 void
   1792 ohci_root_intr_done(struct usbd_xfer *xfer)
   1793 {
   1794 }
   1795 
   1796 /*
   1797  * Wait here until controller claims to have an interrupt.
   1798  * Then call ohci_intr and return.  Use timeout to avoid waiting
   1799  * too long.
   1800  */
   1801 void
   1802 ohci_waitintr(ohci_softc_t *sc, struct usbd_xfer *xfer)
   1803 {
   1804 	int timo;
   1805 	uint32_t intrs;
   1806 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1807 
   1808 	mutex_enter(&sc->sc_lock);
   1809 
   1810 	xfer->ux_status = USBD_IN_PROGRESS;
   1811 	for (timo = xfer->ux_timeout; timo >= 0; timo--) {
   1812 		usb_delay_ms(&sc->sc_bus, 1);
   1813 		if (sc->sc_dying)
   1814 			break;
   1815 		intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
   1816 		DPRINTFN(15, "intrs 0x%04x", intrs, 0, 0, 0);
   1817 #ifdef OHCI_DEBUG
   1818 		if (ohcidebug > 15)
   1819 			ohci_dumpregs(sc);
   1820 #endif
   1821 		if (intrs) {
   1822 			mutex_spin_enter(&sc->sc_intr_lock);
   1823 			ohci_intr1(sc);
   1824 			mutex_spin_exit(&sc->sc_intr_lock);
   1825 			if (xfer->ux_status != USBD_IN_PROGRESS)
   1826 				goto done;
   1827 		}
   1828 	}
   1829 
   1830 	/* Timeout */
   1831 	DPRINTF("timeout", 0, 0, 0, 0);
   1832 	xfer->ux_status = USBD_TIMEOUT;
   1833 	usb_transfer_complete(xfer);
   1834 
   1835 done:
   1836 	mutex_exit(&sc->sc_lock);
   1837 }
   1838 
   1839 void
   1840 ohci_poll(struct usbd_bus *bus)
   1841 {
   1842 	ohci_softc_t *sc = OHCI_BUS2SC(bus);
   1843 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1844 
   1845 #ifdef OHCI_DEBUG
   1846 	static int last;
   1847 	int new;
   1848 	new = OREAD4(sc, OHCI_INTERRUPT_STATUS);
   1849 	if (new != last) {
   1850 		DPRINTFN(10, "intrs=0x%04x", new, 0, 0, 0);
   1851 		last = new;
   1852 	}
   1853 #endif
   1854 	sc->sc_eintrs |= OHCI_WDH;
   1855 	if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs) {
   1856 		mutex_spin_enter(&sc->sc_intr_lock);
   1857 		ohci_intr1(sc);
   1858 		mutex_spin_exit(&sc->sc_intr_lock);
   1859 	}
   1860 }
   1861 
   1862 /*
   1863  * Add an ED to the schedule.  Called with USB lock held.
   1864  */
   1865 Static void
   1866 ohci_add_ed(ohci_softc_t *sc, ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
   1867 {
   1868 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1869 	DPRINTFN(8, "sed=%p head=%p", sed, head, 0, 0);
   1870 
   1871 	KASSERT(mutex_owned(&sc->sc_lock));
   1872 
   1873 	usb_syncmem(&head->dma, head->offs + offsetof(ohci_ed_t, ed_nexted),
   1874 	    sizeof(head->ed.ed_nexted),
   1875 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1876 	sed->next = head->next;
   1877 	sed->ed.ed_nexted = head->ed.ed_nexted;
   1878 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_nexted),
   1879 	    sizeof(sed->ed.ed_nexted),
   1880 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1881 	head->next = sed;
   1882 	head->ed.ed_nexted = HTOO32(sed->physaddr);
   1883 	usb_syncmem(&head->dma, head->offs + offsetof(ohci_ed_t, ed_nexted),
   1884 	    sizeof(head->ed.ed_nexted),
   1885 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1886 }
   1887 
   1888 /*
   1889  * Remove an ED from the schedule.  Called with USB lock held.
   1890  */
   1891 Static void
   1892 ohci_rem_ed(ohci_softc_t *sc, ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
   1893 {
   1894 	ohci_soft_ed_t *p;
   1895 
   1896 	KASSERT(mutex_owned(&sc->sc_lock));
   1897 
   1898 	/* XXX */
   1899 	for (p = head; p != NULL && p->next != sed; p = p->next)
   1900 		;
   1901 	KASSERT(p != NULL);
   1902 
   1903 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_nexted),
   1904 	    sizeof(sed->ed.ed_nexted),
   1905 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1906 	p->next = sed->next;
   1907 	p->ed.ed_nexted = sed->ed.ed_nexted;
   1908 	usb_syncmem(&p->dma, p->offs + offsetof(ohci_ed_t, ed_nexted),
   1909 	    sizeof(p->ed.ed_nexted),
   1910 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1911 }
   1912 
   1913 /*
   1914  * When a transfer is completed the TD is added to the done queue by
   1915  * the host controller.  This queue is the processed by software.
   1916  * Unfortunately the queue contains the physical address of the TD
   1917  * and we have no simple way to translate this back to a kernel address.
   1918  * To make the translation possible (and fast) we use a hash table of
   1919  * TDs currently in the schedule.  The physical address is used as the
   1920  * hash value.
   1921  */
   1922 
   1923 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
   1924 /* Called with USB lock held. */
   1925 void
   1926 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
   1927 {
   1928 	int h = HASH(std->physaddr);
   1929 
   1930 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   1931 
   1932 	LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
   1933 }
   1934 
   1935 /* Called with USB lock held. */
   1936 void
   1937 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
   1938 {
   1939 
   1940 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   1941 
   1942 	LIST_REMOVE(std, hnext);
   1943 }
   1944 
   1945 ohci_soft_td_t *
   1946 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
   1947 {
   1948 	int h = HASH(a);
   1949 	ohci_soft_td_t *std;
   1950 
   1951 	for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
   1952 	     std != NULL;
   1953 	     std = LIST_NEXT(std, hnext))
   1954 		if (std->physaddr == a)
   1955 			return std;
   1956 	return NULL;
   1957 }
   1958 
   1959 /* Called with USB lock held. */
   1960 void
   1961 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
   1962 {
   1963 	int h = HASH(sitd->physaddr);
   1964 
   1965 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1966 
   1967 	KASSERT(mutex_owned(&sc->sc_lock));
   1968 
   1969 	DPRINTFN(10, "sitd=%p physaddr=0x%08lx", sitd, (u_long)sitd->physaddr,
   1970 	    0, 0);
   1971 
   1972 	LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
   1973 }
   1974 
   1975 /* Called with USB lock held. */
   1976 void
   1977 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
   1978 {
   1979 
   1980 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   1981 
   1982 	KASSERT(mutex_owned(&sc->sc_lock));
   1983 
   1984 	DPRINTFN(10, "sitd=%p physaddr=0x%08lx", sitd, (u_long)sitd->physaddr,
   1985 	    0, 0);
   1986 
   1987 	LIST_REMOVE(sitd, hnext);
   1988 }
   1989 
   1990 ohci_soft_itd_t *
   1991 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
   1992 {
   1993 	int h = HASH(a);
   1994 	ohci_soft_itd_t *sitd;
   1995 
   1996 	for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
   1997 	     sitd != NULL;
   1998 	     sitd = LIST_NEXT(sitd, hnext))
   1999 		if (sitd->physaddr == a)
   2000 			return sitd;
   2001 	return NULL;
   2002 }
   2003 
   2004 void
   2005 ohci_timeout(void *addr)
   2006 {
   2007 	struct usbd_xfer *xfer = addr;
   2008 	struct ohci_xfer *oxfer = OHCI_XFER2OXFER(xfer);
   2009 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2010 
   2011 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2012 	DPRINTF("oxfer=%p", oxfer, 0, 0, 0);
   2013 
   2014 	if (sc->sc_dying) {
   2015 		mutex_enter(&sc->sc_lock);
   2016 		ohci_abort_xfer(xfer, USBD_TIMEOUT);
   2017 		mutex_exit(&sc->sc_lock);
   2018 		return;
   2019 	}
   2020 
   2021 	/* Execute the abort in a process context. */
   2022 	usb_init_task(&oxfer->abort_task, ohci_timeout_task, addr,
   2023 	    USB_TASKQ_MPSAFE);
   2024 	usb_add_task(xfer->ux_pipe->up_dev, &oxfer->abort_task,
   2025 	    USB_TASKQ_HC);
   2026 }
   2027 
   2028 void
   2029 ohci_timeout_task(void *addr)
   2030 {
   2031 	struct usbd_xfer *xfer = addr;
   2032 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2033 
   2034 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2035 
   2036 	DPRINTF("xfer=%p", xfer, 0, 0, 0);
   2037 
   2038 	mutex_enter(&sc->sc_lock);
   2039 	ohci_abort_xfer(xfer, USBD_TIMEOUT);
   2040 	mutex_exit(&sc->sc_lock);
   2041 }
   2042 
   2043 #ifdef OHCI_DEBUG
   2044 void
   2045 ohci_dump_tds(ohci_softc_t *sc, ohci_soft_td_t *std)
   2046 {
   2047 	for (; std; std = std->nexttd) {
   2048 		ohci_dump_td(sc, std);
   2049 		KASSERTMSG(std->nexttd == NULL || std != std->nexttd,
   2050 		    "std %p next %p", std, std->nexttd);
   2051 	}
   2052 }
   2053 
   2054 void
   2055 ohci_dump_td(ohci_softc_t *sc, ohci_soft_td_t *std)
   2056 {
   2057 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2058 
   2059 	usb_syncmem(&std->dma, std->offs, sizeof(std->td),
   2060 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   2061 
   2062 	uint32_t flags = O32TOH(std->td.td_flags);
   2063 	DPRINTF("TD(%p) at 0x%08lx:", std, (u_long)std->physaddr, 0, 0);
   2064 	DPRINTF("    round=%d DP=%x DI=%x T=%x",
   2065 	    !!(flags & OHCI_TD_R),
   2066 	    __SHIFTOUT(flags, OHCI_TD_DP_MASK),
   2067 	    OHCI_TD_GET_DI(flags),
   2068 	    __SHIFTOUT(flags, OHCI_TD_TOGGLE_MASK));
   2069 	DPRINTF("    EC=%d CC=%d", OHCI_TD_GET_EC(flags), OHCI_TD_GET_CC(flags),
   2070 	    0, 0);
   2071 	DPRINTF("    cbp=0x%08lx nexttd=0x%08lx be=0x%08lx",
   2072 	       (u_long)O32TOH(std->td.td_cbp),
   2073 	       (u_long)O32TOH(std->td.td_nexttd),
   2074 	       (u_long)O32TOH(std->td.td_be), 0);
   2075 }
   2076 
   2077 void
   2078 ohci_dump_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
   2079 {
   2080 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2081 
   2082 	usb_syncmem(&sitd->dma, sitd->offs, sizeof(sitd->itd),
   2083 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   2084 
   2085 	uint32_t flags = O32TOH(sitd->itd.itd_flags);
   2086 	DPRINTF("ITD(%p) at 0x%08lx", sitd, (u_long)sitd->physaddr, 0, 0);
   2087 	DPRINTF("    sf=%d di=%d fc=%d cc=%d",
   2088 	    OHCI_ITD_GET_SF(flags), OHCI_ITD_GET_DI(flags),
   2089 	    OHCI_ITD_GET_FC(flags), OHCI_ITD_GET_CC(flags));
   2090 	DPRINTF("    bp0=0x%08x next=0x%08x be=0x%08x",
   2091 	    O32TOH(sitd->itd.itd_bp0),
   2092 	    O32TOH(sitd->itd.itd_nextitd),
   2093 	    O32TOH(sitd->itd.itd_be), 0);
   2094 	CTASSERT(OHCI_ITD_NOFFSET == 8);
   2095 	DPRINTF("    offs[0] = 0x%04x  offs[1] = 0x%04x  "
   2096 	    "offs[2] = 0x%04x  offs[3] = 0x%04x",
   2097 	    O16TOH(sitd->itd.itd_offset[0]),
   2098 	    O16TOH(sitd->itd.itd_offset[1]),
   2099 	    O16TOH(sitd->itd.itd_offset[2]),
   2100 	    O16TOH(sitd->itd.itd_offset[3]));
   2101 	DPRINTF("    offs[4] = 0x%04x  offs[5] = 0x%04x  "
   2102 	    "offs[6] = 0x%04x  offs[7] = 0x%04x",
   2103 	    O16TOH(sitd->itd.itd_offset[4]),
   2104 	    O16TOH(sitd->itd.itd_offset[5]),
   2105 	    O16TOH(sitd->itd.itd_offset[6]),
   2106 	    O16TOH(sitd->itd.itd_offset[7]));
   2107 }
   2108 
   2109 void
   2110 ohci_dump_itds(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
   2111 {
   2112 	for (; sitd; sitd = sitd->nextitd)
   2113 		ohci_dump_itd(sc, sitd);
   2114 }
   2115 
   2116 void
   2117 ohci_dump_ed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
   2118 {
   2119 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2120 
   2121 	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
   2122 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   2123 
   2124 	uint32_t flags = O32TOH(sed->ed.ed_flags);
   2125 	DPRINTF("ED(%p) at 0x%08lx:", sed, sed->physaddr, 0, 0);
   2126 	DPRINTF("    addr=%d endpt=%d maxp=%d",
   2127 	    OHCI_ED_GET_FA(flags),
   2128 	    OHCI_ED_GET_EN(flags),
   2129 	    OHCI_ED_GET_MAXP(flags),
   2130 	    0);
   2131 	DPRINTF("    dir=%d speed=%d skip=%d iso=%d",
   2132 	   __SHIFTOUT(flags, OHCI_ED_DIR_MASK),
   2133 	    !!(flags & OHCI_ED_SPEED),
   2134 	    !!(flags & OHCI_ED_SKIP),
   2135 	    !!(flags & OHCI_ED_FORMAT_ISO));
   2136 	DPRINTF("    tailp=0x%08lx", (u_long)O32TOH(sed->ed.ed_tailp),
   2137 	    0, 0, 0);
   2138 	DPRINTF("    headp=0x%08lx nexted=0x%08lx halted=%d carry=%d",
   2139 	    O32TOH(sed->ed.ed_headp), O32TOH(sed->ed.ed_nexted),
   2140 	    !!(O32TOH(sed->ed.ed_headp) & OHCI_HALTED),
   2141 	    !!(O32TOH(sed->ed.ed_headp) & OHCI_TOGGLECARRY));
   2142 }
   2143 #endif
   2144 
   2145 usbd_status
   2146 ohci_open(struct usbd_pipe *pipe)
   2147 {
   2148 	struct usbd_device *dev = pipe->up_dev;
   2149 	struct usbd_bus *bus = dev->ud_bus;
   2150 	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
   2151 	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
   2152 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
   2153 	uint8_t addr = dev->ud_addr;
   2154 	uint8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
   2155 	ohci_soft_ed_t *sed;
   2156 	ohci_soft_td_t *std;
   2157 	ohci_soft_itd_t *sitd;
   2158 	ohci_physaddr_t tdphys;
   2159 	uint32_t fmt;
   2160 	usbd_status err = USBD_NOMEM;
   2161 	int ival;
   2162 
   2163 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2164 	DPRINTFN(1, "pipe=%p, addr=%d, endpt=%d (%d)", pipe, addr,
   2165 	    ed->bEndpointAddress, bus->ub_rhaddr);
   2166 
   2167 	if (sc->sc_dying) {
   2168 		return USBD_IOERROR;
   2169 	}
   2170 
   2171 	std = NULL;
   2172 	sed = NULL;
   2173 
   2174 	if (addr == bus->ub_rhaddr) {
   2175 		switch (ed->bEndpointAddress) {
   2176 		case USB_CONTROL_ENDPOINT:
   2177 			pipe->up_methods = &roothub_ctrl_methods;
   2178 			break;
   2179 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
   2180 			pipe->up_methods = &ohci_root_intr_methods;
   2181 			break;
   2182 		default:
   2183 			err = USBD_INVAL;
   2184 			goto bad;
   2185 		}
   2186 	} else {
   2187 		sed = ohci_alloc_sed(sc);
   2188 		if (sed == NULL)
   2189 			goto bad;
   2190 		opipe->sed = sed;
   2191 		if (xfertype == UE_ISOCHRONOUS) {
   2192 			sitd = ohci_alloc_sitd(sc);
   2193 			if (sitd == NULL)
   2194 				goto bad;
   2195 
   2196 			opipe->tail.itd = sitd;
   2197 			tdphys = sitd->physaddr;
   2198 			fmt = OHCI_ED_FORMAT_ISO;
   2199 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
   2200 				fmt |= OHCI_ED_DIR_IN;
   2201 			else
   2202 				fmt |= OHCI_ED_DIR_OUT;
   2203 		} else {
   2204 			std = ohci_alloc_std(sc);
   2205 			if (std == NULL)
   2206 				goto bad;
   2207 
   2208 			opipe->tail.td = std;
   2209 			tdphys = std->physaddr;
   2210 			fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD;
   2211 		}
   2212 		sed->ed.ed_flags = HTOO32(
   2213 			OHCI_ED_SET_FA(addr) |
   2214 			OHCI_ED_SET_EN(UE_GET_ADDR(ed->bEndpointAddress)) |
   2215 			(dev->ud_speed == USB_SPEED_LOW ? OHCI_ED_SPEED : 0) |
   2216 			fmt |
   2217 			OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
   2218 		sed->ed.ed_headp = HTOO32(tdphys |
   2219 		    (pipe->up_endpoint->ue_toggle ? OHCI_TOGGLECARRY : 0));
   2220 		sed->ed.ed_tailp = HTOO32(tdphys);
   2221 		usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
   2222 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2223 
   2224 		switch (xfertype) {
   2225 		case UE_CONTROL:
   2226 			pipe->up_methods = &ohci_device_ctrl_methods;
   2227 			err = usb_allocmem(&sc->sc_bus,
   2228 				  sizeof(usb_device_request_t),
   2229 				  0, &opipe->ctrl.reqdma);
   2230 			if (err)
   2231 				goto bad;
   2232 			mutex_enter(&sc->sc_lock);
   2233 			ohci_add_ed(sc, sed, sc->sc_ctrl_head);
   2234 			mutex_exit(&sc->sc_lock);
   2235 			break;
   2236 		case UE_INTERRUPT:
   2237 			pipe->up_methods = &ohci_device_intr_methods;
   2238 			ival = pipe->up_interval;
   2239 			if (ival == USBD_DEFAULT_INTERVAL)
   2240 				ival = ed->bInterval;
   2241 			err = ohci_device_setintr(sc, opipe, ival);
   2242 			if (err)
   2243 				goto bad;
   2244 			break;
   2245 		case UE_ISOCHRONOUS:
   2246 			pipe->up_methods = &ohci_device_isoc_methods;
   2247 			return ohci_setup_isoc(pipe);
   2248 		case UE_BULK:
   2249 			pipe->up_methods = &ohci_device_bulk_methods;
   2250 			mutex_enter(&sc->sc_lock);
   2251 			ohci_add_ed(sc, sed, sc->sc_bulk_head);
   2252 			mutex_exit(&sc->sc_lock);
   2253 			break;
   2254 		}
   2255 	}
   2256 
   2257 	return USBD_NORMAL_COMPLETION;
   2258 
   2259  bad:
   2260 	if (std != NULL) {
   2261 		ohci_free_std(sc, std);
   2262 	}
   2263 	if (sed != NULL)
   2264 		ohci_free_sed(sc, sed);
   2265 	return err;
   2266 
   2267 }
   2268 
   2269 /*
   2270  * Close a reqular pipe.
   2271  * Assumes that there are no pending transactions.
   2272  */
   2273 void
   2274 ohci_close_pipe(struct usbd_pipe *pipe, ohci_soft_ed_t *head)
   2275 {
   2276 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
   2277 	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
   2278 	ohci_soft_ed_t *sed = opipe->sed;
   2279 
   2280 	KASSERT(mutex_owned(&sc->sc_lock));
   2281 
   2282 #ifdef DIAGNOSTIC
   2283 	sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
   2284 	if ((O32TOH(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
   2285 	    (O32TOH(sed->ed.ed_headp) & OHCI_HEADMASK)) {
   2286 		ohci_soft_td_t *std;
   2287 		std = ohci_hash_find_td(sc, O32TOH(sed->ed.ed_headp));
   2288 		printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
   2289 		       "tl=0x%x pipe=%p, std=%p\n", sed,
   2290 		       (int)O32TOH(sed->ed.ed_headp),
   2291 		       (int)O32TOH(sed->ed.ed_tailp),
   2292 		       pipe, std);
   2293 #ifdef OHCI_DEBUG
   2294 		usbd_dump_pipe(&opipe->pipe);
   2295 		ohci_dump_ed(sc, sed);
   2296 		if (std)
   2297 			ohci_dump_td(sc, std);
   2298 #endif
   2299 		usb_delay_ms(&sc->sc_bus, 2);
   2300 		if ((O32TOH(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
   2301 		    (O32TOH(sed->ed.ed_headp) & OHCI_HEADMASK))
   2302 			printf("ohci_close_pipe: pipe still not empty\n");
   2303 	}
   2304 #endif
   2305 	ohci_rem_ed(sc, sed, head);
   2306 	/* Make sure the host controller is not touching this ED */
   2307 	usb_delay_ms(&sc->sc_bus, 1);
   2308 	pipe->up_endpoint->ue_toggle =
   2309 	    (O32TOH(sed->ed.ed_headp) & OHCI_TOGGLECARRY) ? 1 : 0;
   2310 	ohci_free_sed_locked(sc, opipe->sed);
   2311 }
   2312 
   2313 /*
   2314  * Abort a device request.
   2315  * If this routine is called at splusb() it guarantees that the request
   2316  * will be removed from the hardware scheduling and that the callback
   2317  * for it will be called with USBD_CANCELLED status.
   2318  * It's impossible to guarantee that the requested transfer will not
   2319  * have happened since the hardware runs concurrently.
   2320  * If the transaction has already happened we rely on the ordinary
   2321  * interrupt processing to process it.
   2322  * XXX This is most probably wrong.
   2323  * XXXMRG this doesn't make sense anymore.
   2324  */
   2325 void
   2326 ohci_abort_xfer(struct usbd_xfer *xfer, usbd_status status)
   2327 {
   2328 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   2329 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2330 	ohci_soft_ed_t *sed = opipe->sed;
   2331 	ohci_soft_td_t *p, *n;
   2332 	ohci_physaddr_t headp;
   2333 	int hit;
   2334 	int wake;
   2335 
   2336 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2337 	DPRINTF("xfer=%p pipe=%p sed=%p", xfer, opipe,sed, 0);
   2338 
   2339 	KASSERT(mutex_owned(&sc->sc_lock));
   2340 	ASSERT_SLEEPABLE();
   2341 
   2342 	if (sc->sc_dying) {
   2343 		/* If we're dying, just do the software part. */
   2344 		xfer->ux_status = status;	/* make software ignore it */
   2345 		callout_halt(&xfer->ux_callout, &sc->sc_lock);
   2346 		usb_transfer_complete(xfer);
   2347 		return;
   2348 	}
   2349 
   2350 	/*
   2351 	 * If an abort is already in progress then just wait for it to
   2352 	 * complete and return.
   2353 	 */
   2354 	if (xfer->ux_hcflags & UXFER_ABORTING) {
   2355 		DPRINTFN(2, "already aborting", 0, 0, 0, 0);
   2356 #ifdef DIAGNOSTIC
   2357 		if (status == USBD_TIMEOUT)
   2358 			printf("%s: TIMEOUT while aborting\n", __func__);
   2359 #endif
   2360 		/* Override the status which might be USBD_TIMEOUT. */
   2361 		xfer->ux_status = status;
   2362 		DPRINTFN(2, "waiting for abort to finish", 0, 0, 0, 0);
   2363 		xfer->ux_hcflags |= UXFER_ABORTWAIT;
   2364 		while (xfer->ux_hcflags & UXFER_ABORTING)
   2365 			cv_wait(&xfer->ux_hccv, &sc->sc_lock);
   2366 		goto done;
   2367 	}
   2368 	xfer->ux_hcflags |= UXFER_ABORTING;
   2369 
   2370 	/*
   2371 	 * Step 1: Make interrupt routine and hardware ignore xfer.
   2372 	 */
   2373 	xfer->ux_status = status;	/* make software ignore it */
   2374 	callout_stop(&xfer->ux_callout);
   2375 	DPRINTFN(1, "stop ed=%p", sed, 0, 0, 0);
   2376 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
   2377 	    sizeof(sed->ed.ed_flags),
   2378 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   2379 	sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP); /* force hardware skip */
   2380 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
   2381 	    sizeof(sed->ed.ed_flags),
   2382 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2383 
   2384 	/*
   2385 	 * Step 2: Wait until we know hardware has finished any possible
   2386 	 * use of the xfer.  Also make sure the soft interrupt routine
   2387 	 * has run.
   2388 	 */
   2389 	/* Hardware finishes in 1ms */
   2390 	usb_delay_ms_locked(opipe->pipe.up_dev->ud_bus, 20, &sc->sc_lock);
   2391 	sc->sc_softwake = 1;
   2392 	usb_schedsoftintr(&sc->sc_bus);
   2393 	cv_wait(&sc->sc_softwake_cv, &sc->sc_lock);
   2394 
   2395 	/*
   2396 	 * Step 3: Remove any vestiges of the xfer from the hardware.
   2397 	 * The complication here is that the hardware may have executed
   2398 	 * beyond the xfer we're trying to abort.  So as we're scanning
   2399 	 * the TDs of this xfer we check if the hardware points to
   2400 	 * any of them.
   2401 	 */
   2402 	p = xfer->ux_hcpriv;
   2403 	KASSERT(p);
   2404 
   2405 #ifdef OHCI_DEBUG
   2406 	DPRINTF("--- dump start ---", 0, 0, 0, 0);
   2407 
   2408 	if (ohcidebug >= 2) {
   2409 		DPRINTF("sed:", 0, 0, 0, 0);
   2410 		ohci_dump_ed(sc, sed);
   2411 		ohci_dump_tds(sc, p);
   2412 	}
   2413 	DPRINTF("--- dump end ---", 0, 0, 0, 0);
   2414 #endif
   2415 	headp = O32TOH(sed->ed.ed_headp) & OHCI_HEADMASK;
   2416 	hit = 0;
   2417 	for (; p->xfer == xfer; p = n) {
   2418 		hit |= headp == p->physaddr;
   2419 		n = p->nexttd;
   2420 		ohci_hash_rem_td(sc, p);
   2421 	}
   2422 	/* Zap headp register if hardware pointed inside the xfer. */
   2423 	if (hit) {
   2424 		DPRINTFN(1, "set hd=0x%08x, tl=0x%08x",  (int)p->physaddr,
   2425 		    (int)O32TOH(sed->ed.ed_tailp), 0, 0);
   2426 		sed->ed.ed_headp = HTOO32(p->physaddr); /* unlink TDs */
   2427 		usb_syncmem(&sed->dma,
   2428 		    sed->offs + offsetof(ohci_ed_t, ed_headp),
   2429 		    sizeof(sed->ed.ed_headp),
   2430 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2431 	} else {
   2432 		DPRINTFN(1, "no hit", 0, 0, 0, 0);
   2433 	}
   2434 
   2435 	/*
   2436 	 * Step 4: Turn on hardware again.
   2437 	 */
   2438 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
   2439 	    sizeof(sed->ed.ed_flags),
   2440 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   2441 	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP); /* remove hardware skip */
   2442 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
   2443 	    sizeof(sed->ed.ed_flags),
   2444 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2445 
   2446 	/*
   2447 	 * Step 5: Execute callback.
   2448 	 */
   2449 	wake = xfer->ux_hcflags & UXFER_ABORTWAIT;
   2450 	xfer->ux_hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
   2451 	usb_transfer_complete(xfer);
   2452 	if (wake)
   2453 		cv_broadcast(&xfer->ux_hccv);
   2454 
   2455 done:
   2456 	KASSERT(mutex_owned(&sc->sc_lock));
   2457 }
   2458 
   2459 /*
   2460  * Data structures and routines to emulate the root hub.
   2461  */
   2462 Static int
   2463 ohci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
   2464     void *buf, int buflen)
   2465 {
   2466 	ohci_softc_t *sc = OHCI_BUS2SC(bus);
   2467 	usb_port_status_t ps;
   2468 	uint16_t len, value, index;
   2469 	int l, totlen = 0;
   2470 	int port, i;
   2471 	uint32_t v;
   2472 
   2473 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2474 
   2475 	if (sc->sc_dying)
   2476 		return -1;
   2477 
   2478 	DPRINTFN(4, "type=0x%02x request=%02x", req->bmRequestType,
   2479 	    req->bRequest, 0, 0);
   2480 
   2481 	len = UGETW(req->wLength);
   2482 	value = UGETW(req->wValue);
   2483 	index = UGETW(req->wIndex);
   2484 
   2485 #define C(x,y) ((x) | ((y) << 8))
   2486 	switch (C(req->bRequest, req->bmRequestType)) {
   2487 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   2488 		DPRINTFN(8, "wValue=0x%04x", value, 0, 0, 0);
   2489 		if (len == 0)
   2490 			break;
   2491 		switch (value) {
   2492 		case C(0, UDESC_DEVICE): {
   2493 			usb_device_descriptor_t devd;
   2494 
   2495 			totlen = min(buflen, sizeof(devd));
   2496 			memcpy(&devd, buf, totlen);
   2497 			USETW(devd.idVendor, sc->sc_id_vendor);
   2498 			memcpy(buf, &devd, totlen);
   2499 			break;
   2500 		}
   2501 		case C(1, UDESC_STRING):
   2502 #define sd ((usb_string_descriptor_t *)buf)
   2503 			/* Vendor */
   2504 			totlen = usb_makestrdesc(sd, len, sc->sc_vendor);
   2505 			break;
   2506 		case C(2, UDESC_STRING):
   2507 			/* Product */
   2508 			totlen = usb_makestrdesc(sd, len, "OHCI root hub");
   2509 			break;
   2510 #undef sd
   2511 		default:
   2512 			/* default from usbroothub */
   2513 			return buflen;
   2514 		}
   2515 		break;
   2516 
   2517 	/* Hub requests */
   2518 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   2519 		break;
   2520 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   2521 		DPRINTFN(8, "UR_CLEAR_PORT_FEATURE port=%d feature=%d",
   2522 		    index, value, 0, 0);
   2523 		if (index < 1 || index > sc->sc_noport) {
   2524 			return -1;
   2525 		}
   2526 		port = OHCI_RH_PORT_STATUS(index);
   2527 		switch(value) {
   2528 		case UHF_PORT_ENABLE:
   2529 			OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
   2530 			break;
   2531 		case UHF_PORT_SUSPEND:
   2532 			OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
   2533 			break;
   2534 		case UHF_PORT_POWER:
   2535 			/* Yes, writing to the LOW_SPEED bit clears power. */
   2536 			OWRITE4(sc, port, UPS_LOW_SPEED);
   2537 			break;
   2538 		case UHF_C_PORT_CONNECTION:
   2539 			OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
   2540 			break;
   2541 		case UHF_C_PORT_ENABLE:
   2542 			OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
   2543 			break;
   2544 		case UHF_C_PORT_SUSPEND:
   2545 			OWRITE4(sc, port, UPS_C_SUSPEND << 16);
   2546 			break;
   2547 		case UHF_C_PORT_OVER_CURRENT:
   2548 			OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
   2549 			break;
   2550 		case UHF_C_PORT_RESET:
   2551 			OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
   2552 			break;
   2553 		default:
   2554 			return -1;
   2555 		}
   2556 		switch(value) {
   2557 		case UHF_C_PORT_CONNECTION:
   2558 		case UHF_C_PORT_ENABLE:
   2559 		case UHF_C_PORT_SUSPEND:
   2560 		case UHF_C_PORT_OVER_CURRENT:
   2561 		case UHF_C_PORT_RESET:
   2562 			/* Enable RHSC interrupt if condition is cleared. */
   2563 			if ((OREAD4(sc, port) >> 16) == 0)
   2564 				ohci_rhsc_enable(sc);
   2565 			break;
   2566 		default:
   2567 			break;
   2568 		}
   2569 		break;
   2570 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   2571 		if (len == 0)
   2572 			break;
   2573 		if ((value & 0xff) != 0) {
   2574 			return -1;
   2575 		}
   2576 		usb_hub_descriptor_t hubd;
   2577 
   2578 		totlen = min(buflen, sizeof(hubd));
   2579 		memcpy(&hubd, buf, totlen);
   2580 
   2581 		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
   2582 		hubd.bNbrPorts = sc->sc_noport;
   2583 		USETW(hubd.wHubCharacteristics,
   2584 		      (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
   2585 		       v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
   2586 		      /* XXX overcurrent */
   2587 		      );
   2588 		hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
   2589 		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
   2590 		for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
   2591 			hubd.DeviceRemovable[i++] = (uint8_t)v;
   2592 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
   2593 		totlen = min(totlen, hubd.bDescLength);
   2594 		memcpy(buf, &hubd, totlen);
   2595 		break;
   2596 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   2597 		if (len != 4) {
   2598 			return -1;
   2599 		}
   2600 		memset(buf, 0, len); /* ? XXX */
   2601 		totlen = len;
   2602 		break;
   2603 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   2604 		DPRINTFN(8, "get port status i=%d", index, 0, 0, 0);
   2605 		if (index < 1 || index > sc->sc_noport) {
   2606 			return -1;
   2607 		}
   2608 		if (len != 4) {
   2609 			return -1;
   2610 			}
   2611 		v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
   2612 		DPRINTFN(8, "port status=0x%04x", v, 0, 0, 0);
   2613 		USETW(ps.wPortStatus, v);
   2614 		USETW(ps.wPortChange, v >> 16);
   2615 		totlen = min(len, sizeof(ps));
   2616 		memcpy(buf, &ps, totlen);
   2617 		break;
   2618 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   2619 		return -1;
   2620 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   2621 		break;
   2622 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   2623 		if (index < 1 || index > sc->sc_noport) {
   2624 			return -1;
   2625 		}
   2626 		port = OHCI_RH_PORT_STATUS(index);
   2627 		switch(value) {
   2628 		case UHF_PORT_ENABLE:
   2629 			OWRITE4(sc, port, UPS_PORT_ENABLED);
   2630 			break;
   2631 		case UHF_PORT_SUSPEND:
   2632 			OWRITE4(sc, port, UPS_SUSPEND);
   2633 			break;
   2634 		case UHF_PORT_RESET:
   2635 			DPRINTFN(5, "reset port %d", index, 0, 0, 0);
   2636 			OWRITE4(sc, port, UPS_RESET);
   2637 			for (i = 0; i < 5; i++) {
   2638 				usb_delay_ms(&sc->sc_bus,
   2639 					     USB_PORT_ROOT_RESET_DELAY);
   2640 				if (sc->sc_dying) {
   2641 					return -1;
   2642 				}
   2643 				if ((OREAD4(sc, port) & UPS_RESET) == 0)
   2644 					break;
   2645 			}
   2646 			DPRINTFN(8, "port %d reset, status = 0x%04x", index,
   2647 			    OREAD4(sc, port), 0, 0);
   2648 			break;
   2649 		case UHF_PORT_POWER:
   2650 			DPRINTFN(2, "set port power %d", index, 0, 0, 0);
   2651 			OWRITE4(sc, port, UPS_PORT_POWER);
   2652 			break;
   2653 		default:
   2654 			return -1;
   2655 		}
   2656 		break;
   2657 	default:
   2658 		/* default from usbroothub */
   2659 		return buflen;
   2660 	}
   2661 
   2662 	return totlen;
   2663 }
   2664 
   2665 Static usbd_status
   2666 ohci_root_intr_transfer(struct usbd_xfer *xfer)
   2667 {
   2668 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2669 	usbd_status err;
   2670 
   2671 	/* Insert last in queue. */
   2672 	mutex_enter(&sc->sc_lock);
   2673 	err = usb_insert_transfer(xfer);
   2674 	mutex_exit(&sc->sc_lock);
   2675 	if (err)
   2676 		return err;
   2677 
   2678 	/* Pipe isn't running, start first */
   2679 	return ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   2680 }
   2681 
   2682 Static usbd_status
   2683 ohci_root_intr_start(struct usbd_xfer *xfer)
   2684 {
   2685 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2686 
   2687 	if (sc->sc_dying)
   2688 		return USBD_IOERROR;
   2689 
   2690 	mutex_enter(&sc->sc_lock);
   2691 	KASSERT(sc->sc_intrxfer == NULL);
   2692 	sc->sc_intrxfer = xfer;
   2693 	mutex_exit(&sc->sc_lock);
   2694 
   2695 	return USBD_IN_PROGRESS;
   2696 }
   2697 
   2698 /* Abort a root interrupt request. */
   2699 Static void
   2700 ohci_root_intr_abort(struct usbd_xfer *xfer)
   2701 {
   2702 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2703 
   2704 	KASSERT(mutex_owned(&sc->sc_lock));
   2705 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
   2706 
   2707 	sc->sc_intrxfer = NULL;
   2708 
   2709 	xfer->ux_status = USBD_CANCELLED;
   2710 	usb_transfer_complete(xfer);
   2711 }
   2712 
   2713 /* Close the root pipe. */
   2714 Static void
   2715 ohci_root_intr_close(struct usbd_pipe *pipe)
   2716 {
   2717 	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
   2718 
   2719 	KASSERT(mutex_owned(&sc->sc_lock));
   2720 
   2721 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2722 
   2723 	sc->sc_intrxfer = NULL;
   2724 }
   2725 
   2726 /************************/
   2727 
   2728 int
   2729 ohci_device_ctrl_init(struct usbd_xfer *xfer)
   2730 {
   2731 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   2732 	usb_device_request_t *req = &xfer->ux_request;
   2733 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2734 	ohci_soft_td_t *stat, *setup;
   2735 	int isread = req->bmRequestType & UT_READ;
   2736 	int len = xfer->ux_bufsize;
   2737 	int err = ENOMEM;
   2738 
   2739 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2740 
   2741 	setup = ohci_alloc_std(sc);
   2742 	if (setup == NULL) {
   2743 		goto bad1;
   2744 	}
   2745 	stat = ohci_alloc_std(sc);
   2746 	if (stat == NULL) {
   2747 		goto bad2;
   2748 	}
   2749 
   2750 	ox->ox_setup = setup;
   2751 	ox->ox_stat = stat;
   2752 	ox->ox_nstd = 0;
   2753 
   2754 	/* Set up data transaction */
   2755 	if (len != 0) {
   2756 		err = ohci_alloc_std_chain(sc, xfer, len, isread);
   2757 		if (err) {
   2758 			goto bad3;
   2759 		}
   2760 	}
   2761 	return 0;
   2762 
   2763  bad3:
   2764 	ohci_free_std(sc, stat);
   2765  bad2:
   2766 	ohci_free_std(sc, setup);
   2767  bad1:
   2768 	return err;
   2769 }
   2770 
   2771 void
   2772 ohci_device_ctrl_fini(struct usbd_xfer *xfer)
   2773 {
   2774 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   2775 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2776 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   2777 
   2778 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2779 	DPRINTFN(8, "xfer %p nstd %d", xfer, ox->ox_nstd, 0, 0);
   2780 
   2781 	mutex_enter(&sc->sc_lock);
   2782 	if (ox->ox_setup != opipe->tail.td) {
   2783 		ohci_free_std_locked(sc, ox->ox_setup);
   2784 	}
   2785 	for (size_t i = 0; i < ox->ox_nstd; i++) {
   2786 		ohci_soft_td_t *std = ox->ox_stds[i];
   2787 		if (std == NULL)
   2788 			break;
   2789 		ohci_free_std_locked(sc, std);
   2790 	}
   2791 	ohci_free_std_locked(sc, ox->ox_stat);
   2792 	mutex_exit(&sc->sc_lock);
   2793 
   2794 	if (ox->ox_nstd) {
   2795 		const size_t sz = sizeof(ohci_soft_td_t *) * ox->ox_nstd;
   2796 		kmem_free(ox->ox_stds, sz);
   2797 	}
   2798 }
   2799 
   2800 Static usbd_status
   2801 ohci_device_ctrl_transfer(struct usbd_xfer *xfer)
   2802 {
   2803 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2804 	usbd_status err;
   2805 
   2806 	/* Insert last in queue. */
   2807 	mutex_enter(&sc->sc_lock);
   2808 	err = usb_insert_transfer(xfer);
   2809 	mutex_exit(&sc->sc_lock);
   2810 	if (err)
   2811 		return err;
   2812 
   2813 	/* Pipe isn't running, start first */
   2814 	return ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   2815 }
   2816 
   2817 Static usbd_status
   2818 ohci_device_ctrl_start(struct usbd_xfer *xfer)
   2819 {
   2820 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   2821 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   2822 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   2823 	usb_device_request_t *req = &xfer->ux_request;
   2824 	struct usbd_device *dev __diagused = opipe->pipe.up_dev;
   2825 	ohci_soft_td_t *setup, *stat, *next, *tail;
   2826 	ohci_soft_ed_t *sed;
   2827 	int isread;
   2828 	int len;
   2829 
   2830 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   2831 
   2832 	if (sc->sc_dying)
   2833 		return USBD_IOERROR;
   2834 
   2835 	KASSERT(xfer->ux_rqflags & URQ_REQUEST);
   2836 
   2837 	isread = req->bmRequestType & UT_READ;
   2838 	len = UGETW(req->wLength);
   2839 
   2840 	DPRINTF("xfer=%p len=%d, addr=%d, endpt=%d", xfer, len, dev->ud_addr,
   2841 	    opipe->pipe.up_endpoint->ue_edesc->bEndpointAddress);
   2842 	DPRINTF("type=0x%02x, request=0x%02x, wValue=0x%04x, wIndex=0x%04x",
   2843 	    req->bmRequestType, req->bRequest, UGETW(req->wValue),
   2844 	    UGETW(req->wIndex));
   2845 
   2846 	/* Need to take lock here for pipe->tail.td */
   2847 	mutex_enter(&sc->sc_lock);
   2848 
   2849 	/*
   2850 	 * Use the pipe "tail" TD as our first and loan our first TD to the
   2851 	 * next transfer
   2852 	 */
   2853 	setup = opipe->tail.td;
   2854 	opipe->tail.td = ox->ox_setup;
   2855 	ox->ox_setup = setup;
   2856 
   2857 	stat = ox->ox_stat;
   2858 	tail = opipe->tail.td;	/* point at sentinel */
   2859 
   2860 	sed = opipe->sed;
   2861 
   2862 	KASSERTMSG(OHCI_ED_GET_FA(O32TOH(sed->ed.ed_flags)) == dev->ud_addr,
   2863 	    "address ED %d pipe %d\n",
   2864 	    OHCI_ED_GET_FA(O32TOH(sed->ed.ed_flags)), dev->ud_addr);
   2865 	KASSERTMSG(OHCI_ED_GET_MAXP(O32TOH(sed->ed.ed_flags)) ==
   2866 	    UGETW(opipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize),
   2867 	    "MPL ED %d pipe %d\n",
   2868 	    OHCI_ED_GET_MAXP(O32TOH(sed->ed.ed_flags)),
   2869 	    UGETW(opipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize));
   2870 
   2871 	/* next will point to data if len != 0 */
   2872 	next = stat;
   2873 
   2874 	/* Set up data transaction */
   2875 	if (len != 0) {
   2876 		ohci_soft_td_t *std;
   2877 		ohci_soft_td_t *end;
   2878 
   2879 		next = ox->ox_stds[0];
   2880 		ohci_reset_std_chain(sc, xfer, len, isread, next, &end);
   2881 
   2882 		end->td.td_nexttd = HTOO32(stat->physaddr);
   2883 		end->nexttd = stat;
   2884 
   2885 		usb_syncmem(&end->dma,
   2886 		    end->offs + offsetof(ohci_td_t, td_nexttd),
   2887 		    sizeof(end->td.td_nexttd),
   2888 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2889 
   2890 		usb_syncmem(&xfer->ux_dmabuf, 0, len,
   2891 		    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   2892 		std = ox->ox_stds[0];
   2893 		/* Start toggle at 1 and then use the carried toggle. */
   2894 		std->td.td_flags &= HTOO32(~OHCI_TD_TOGGLE_MASK);
   2895 		std->td.td_flags |= HTOO32(OHCI_TD_TOGGLE_1);
   2896 		usb_syncmem(&std->dma,
   2897 		    std->offs + offsetof(ohci_td_t, td_flags),
   2898 		    sizeof(std->td.td_flags),
   2899 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2900 	}
   2901 
   2902 	DPRINTFN(8, "setup %p data %p stat %p tail %p", setup,
   2903 	    (len != 0 ? ox->ox_stds[0] : NULL), stat, tail);
   2904 	KASSERT(opipe->tail.td == tail);
   2905 
   2906 	memcpy(KERNADDR(&opipe->ctrl.reqdma, 0), req, sizeof(*req));
   2907 	usb_syncmem(&opipe->ctrl.reqdma, 0, sizeof(*req), BUS_DMASYNC_PREWRITE);
   2908 
   2909 	setup->td.td_flags = HTOO32(OHCI_TD_SETUP | OHCI_TD_NOCC |
   2910 				     OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
   2911 	setup->td.td_cbp = HTOO32(DMAADDR(&opipe->ctrl.reqdma, 0));
   2912 	setup->td.td_nexttd = HTOO32(next->physaddr);
   2913 	setup->td.td_be = HTOO32(O32TOH(setup->td.td_cbp) + sizeof(*req) - 1);
   2914 	setup->nexttd = next;
   2915 	setup->len = 0;
   2916 	setup->xfer = xfer;
   2917 	setup->flags = 0;
   2918 	ohci_hash_add_td(sc, setup);
   2919 
   2920 	xfer->ux_hcpriv = setup;
   2921 	usb_syncmem(&setup->dma, setup->offs, sizeof(setup->td),
   2922 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2923 
   2924 	stat->td.td_flags = HTOO32(
   2925 		(isread ? OHCI_TD_OUT : OHCI_TD_IN) |
   2926 		OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
   2927 	stat->td.td_cbp = 0;
   2928 	stat->td.td_nexttd = HTOO32(tail->physaddr);
   2929 	stat->td.td_be = 0;
   2930 	stat->nexttd = tail;
   2931 	stat->flags = OHCI_CALL_DONE;
   2932 	stat->len = 0;
   2933 	stat->xfer = xfer;
   2934 	ohci_hash_add_td(sc, stat);
   2935 
   2936 	usb_syncmem(&stat->dma, stat->offs, sizeof(stat->td),
   2937 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2938 
   2939 	memset(&tail->td, 0, sizeof(tail->td));
   2940 	tail->nexttd = NULL;
   2941 	tail->xfer = NULL;
   2942 
   2943 	usb_syncmem(&tail->dma, tail->offs, sizeof(tail->td),
   2944 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2945 
   2946 
   2947 #ifdef OHCI_DEBUG
   2948 	USBHIST_LOGN(ohcidebug, 5, "--- dump start ---", 0, 0, 0, 0);
   2949 	if (ohcidebug >= 5) {
   2950 		ohci_dump_ed(sc, sed);
   2951 		ohci_dump_tds(sc, setup);
   2952 	}
   2953 	USBHIST_LOGN(ohcidebug, 5, "--- dump end ---", 0, 0, 0, 0);
   2954 #endif
   2955 
   2956 	/* Insert ED in schedule */
   2957 	sed->ed.ed_tailp = HTOO32(tail->physaddr);
   2958 	usb_syncmem(&sed->dma,
   2959 	    sed->offs + offsetof(ohci_ed_t, ed_tailp),
   2960 	    sizeof(sed->ed.ed_tailp),
   2961 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2962 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
   2963 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
   2964 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   2965 			    ohci_timeout, xfer);
   2966 	}
   2967 
   2968 #ifdef OHCI_DEBUG
   2969 	DPRINTFN(20, "--- dump start ---", 0, 0, 0, 0);
   2970 	if (ohcidebug >= 20) {
   2971 		delay(10000);
   2972 		DPRINTFN(20, "status=%x", OREAD4(sc, OHCI_COMMAND_STATUS),
   2973 		    0, 0, 0);
   2974 		ohci_dumpregs(sc);
   2975 		DPRINTFN(20, "ctrl head:", 0, 0, 0, 0);
   2976 		ohci_dump_ed(sc, sc->sc_ctrl_head);
   2977 		DPRINTF("sed:", 0, 0, 0, 0);
   2978 		ohci_dump_ed(sc, sed);
   2979 		ohci_dump_tds(sc, setup);
   2980 	}
   2981 	DPRINTFN(20, "--- dump start ---", 0, 0, 0, 0);
   2982 #endif
   2983 
   2984 	DPRINTF("done", 0, 0, 0, 0);
   2985 
   2986 	mutex_exit(&sc->sc_lock);
   2987 
   2988 	if (sc->sc_bus.ub_usepolling)
   2989 		ohci_waitintr(sc, xfer);
   2990 
   2991 	return USBD_IN_PROGRESS;
   2992 }
   2993 
   2994 /* Abort a device control request. */
   2995 Static void
   2996 ohci_device_ctrl_abort(struct usbd_xfer *xfer)
   2997 {
   2998 	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);
   2999 
   3000 	KASSERT(mutex_owned(&sc->sc_lock));
   3001 
   3002 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3003 	DPRINTF("xfer=%p", xfer, 0, 0, 0);
   3004 	ohci_abort_xfer(xfer, USBD_CANCELLED);
   3005 }
   3006 
   3007 /* Close a device control pipe. */
   3008 Static void
   3009 ohci_device_ctrl_close(struct usbd_pipe *pipe)
   3010 {
   3011 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
   3012 	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
   3013 
   3014 	KASSERT(mutex_owned(&sc->sc_lock));
   3015 
   3016 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3017 	DPRINTF("pipe=%p", pipe, 0, 0, 0);
   3018 	ohci_close_pipe(pipe, sc->sc_ctrl_head);
   3019 	ohci_free_std_locked(sc, opipe->tail.td);
   3020 }
   3021 
   3022 /************************/
   3023 
   3024 Static void
   3025 ohci_device_clear_toggle(struct usbd_pipe *pipe)
   3026 {
   3027 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
   3028 	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
   3029 
   3030 	opipe->sed->ed.ed_headp &= HTOO32(~OHCI_TOGGLECARRY);
   3031 }
   3032 
   3033 Static void
   3034 ohci_noop(struct usbd_pipe *pipe)
   3035 {
   3036 }
   3037 
   3038 Static int
   3039 ohci_device_bulk_init(struct usbd_xfer *xfer)
   3040 {
   3041 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3042 	int len = xfer->ux_bufsize;
   3043 	int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;;
   3044 	int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3045 	int err;
   3046 
   3047 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3048 
   3049 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
   3050 
   3051 	DPRINTFN(4, "xfer=%p len=%d isread=%d flags=%d", xfer, len, isread,
   3052 	    xfer->ux_flags);
   3053 	DPRINTFN(4, "endpt=%d", endpt, 0, 0, 0);
   3054 
   3055 	/* Allocate a chain of new TDs (including a new tail). */
   3056 	err = ohci_alloc_std_chain(sc, xfer, len, isread);
   3057 	if (err)
   3058 		return err;
   3059 
   3060 	return 0;
   3061 }
   3062 
   3063 Static void
   3064 ohci_device_bulk_fini(struct usbd_xfer *xfer)
   3065 {
   3066 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3067 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   3068 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   3069 
   3070 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3071 	DPRINTFN(8, "xfer %p nstd %d", xfer, ox->ox_nstd, 0, 0);
   3072 
   3073 	mutex_enter(&sc->sc_lock);
   3074 	for (size_t i = 0; i < ox->ox_nstd; i++) {
   3075 		ohci_soft_td_t *std = ox->ox_stds[i];
   3076 		if (std == NULL)
   3077 			break;
   3078 		if (std != opipe->tail.td)
   3079 			ohci_free_std_locked(sc, std);
   3080 	}
   3081 	mutex_exit(&sc->sc_lock);
   3082 
   3083 	if (ox->ox_nstd) {
   3084 		const size_t sz = sizeof(ohci_soft_td_t *) * ox->ox_nstd;
   3085 		kmem_free(ox->ox_stds, sz);
   3086 	}
   3087 }
   3088 
   3089 Static usbd_status
   3090 ohci_device_bulk_transfer(struct usbd_xfer *xfer)
   3091 {
   3092 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3093 	usbd_status err;
   3094 
   3095 	/* Insert last in queue. */
   3096 	mutex_enter(&sc->sc_lock);
   3097 	err = usb_insert_transfer(xfer);
   3098 	mutex_exit(&sc->sc_lock);
   3099 	if (err)
   3100 		return err;
   3101 
   3102 	/* Pipe isn't running, start first */
   3103 	return ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3104 }
   3105 
   3106 Static usbd_status
   3107 ohci_device_bulk_start(struct usbd_xfer *xfer)
   3108 {
   3109 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   3110 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   3111 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3112 	ohci_soft_td_t *last;
   3113 	ohci_soft_td_t *data, *tail, *tdp;
   3114 	ohci_soft_ed_t *sed;
   3115 	int len, isread, endpt;
   3116 
   3117 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3118 
   3119 	if (sc->sc_dying)
   3120 		return USBD_IOERROR;
   3121 
   3122 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
   3123 
   3124 	len = xfer->ux_length;
   3125 	endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
   3126 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3127 	sed = opipe->sed;
   3128 
   3129 	DPRINTFN(4, "xfer=%p len=%d isread=%d flags=%d", xfer, len, isread,
   3130 	    xfer->ux_flags);
   3131 	DPRINTFN(4, "endpt=%d", endpt, 0, 0, 0);
   3132 
   3133 	mutex_enter(&sc->sc_lock);
   3134 
   3135 	/*
   3136 	 * Use the pipe "tail" TD as our first and loan our first TD to the
   3137 	 * next transfer
   3138 	 */
   3139 	data = opipe->tail.td;
   3140 	opipe->tail.td = ox->ox_stds[0];
   3141 	ox->ox_stds[0] = data;
   3142 	ohci_reset_std_chain(sc, xfer, len, isread, data, &last);
   3143 
   3144 	tail = opipe->tail.td;	/* point at sentinel */
   3145 	memset(&tail->td, 0, sizeof(tail->td));
   3146 	tail->nexttd = NULL;
   3147 	tail->xfer = NULL;
   3148 	usb_syncmem(&tail->dma, tail->offs, sizeof(tail->td),
   3149 	    BUS_DMASYNC_PREWRITE);
   3150 	xfer->ux_hcpriv = data;
   3151 
   3152 	DPRINTFN(8, "xfer %p data %p tail %p", xfer, ox->ox_stds[0], tail, 0);
   3153 	KASSERT(opipe->tail.td == tail);
   3154 
   3155 	/* We want interrupt at the end of the transfer. */
   3156 	last->td.td_flags &= HTOO32(~OHCI_TD_INTR_MASK);
   3157 	last->td.td_flags |= HTOO32(OHCI_TD_SET_DI(1));
   3158 
   3159 	last->td.td_nexttd = HTOO32(tail->physaddr);
   3160 	last->nexttd = tail;
   3161 	last->flags |= OHCI_CALL_DONE;
   3162 	usb_syncmem(&last->dma, last->offs, sizeof(last->td),
   3163 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3164 
   3165 	DPRINTFN(4, "ed_flags=0x%08x td_flags=0x%08x "
   3166 		    "td_cbp=0x%08x td_be=0x%08x",
   3167 		    (int)O32TOH(sed->ed.ed_flags),
   3168 		    (int)O32TOH(data->td.td_flags),
   3169 		    (int)O32TOH(data->td.td_cbp),
   3170 		    (int)O32TOH(data->td.td_be));
   3171 
   3172 #ifdef OHCI_DEBUG
   3173 	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
   3174 	if (ohcidebug >= 5) {
   3175 		ohci_dump_ed(sc, sed);
   3176 		ohci_dump_tds(sc, data);
   3177 	}
   3178 	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
   3179 #endif
   3180 
   3181 	/* Insert ED in schedule */
   3182 	for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
   3183 		KASSERT(tdp->xfer == xfer);
   3184 	}
   3185 	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
   3186 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3187 	sed->ed.ed_tailp = HTOO32(tail->physaddr);
   3188 	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP);
   3189 	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
   3190 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3191 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
   3192 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
   3193 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   3194 			    ohci_timeout, xfer);
   3195 	}
   3196 	mutex_exit(&sc->sc_lock);
   3197 
   3198 	return USBD_IN_PROGRESS;
   3199 }
   3200 
   3201 Static void
   3202 ohci_device_bulk_abort(struct usbd_xfer *xfer)
   3203 {
   3204 	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);
   3205 
   3206 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3207 
   3208 	KASSERT(mutex_owned(&sc->sc_lock));
   3209 
   3210 	DPRINTF("xfer=%p", xfer, 0, 0, 0);
   3211 	ohci_abort_xfer(xfer, USBD_CANCELLED);
   3212 }
   3213 
   3214 /*
   3215  * Close a device bulk pipe.
   3216  */
   3217 Static void
   3218 ohci_device_bulk_close(struct usbd_pipe *pipe)
   3219 {
   3220 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
   3221 	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
   3222 
   3223 	KASSERT(mutex_owned(&sc->sc_lock));
   3224 
   3225 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3226 
   3227 	DPRINTF("pipe=%p", pipe, 0, 0, 0);
   3228 	ohci_close_pipe(pipe, sc->sc_bulk_head);
   3229 	ohci_free_std_locked(sc, opipe->tail.td);
   3230 }
   3231 
   3232 /************************/
   3233 
   3234 Static int
   3235 ohci_device_intr_init(struct usbd_xfer *xfer)
   3236 {
   3237 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   3238 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3239 	int len = xfer->ux_bufsize;
   3240 	int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;;
   3241 	int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3242 	int err;
   3243 
   3244 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3245 
   3246 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
   3247 	KASSERT(len != 0);
   3248 
   3249 	DPRINTFN(4, "xfer=%p len=%d isread=%d flags=%d", xfer, len, isread,
   3250 	    xfer->ux_flags);
   3251 	DPRINTFN(4, "endpt=%d", endpt, 0, 0, 0);
   3252 
   3253 	ox->ox_nstd = 0;
   3254 
   3255 	err = ohci_alloc_std_chain(sc, xfer, len, isread);
   3256 	if (err) {
   3257 		return err;
   3258 	}
   3259 
   3260 	return 0;
   3261 }
   3262 
   3263 Static void
   3264 ohci_device_intr_fini(struct usbd_xfer *xfer)
   3265 {
   3266 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3267 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   3268 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   3269 
   3270 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3271 	DPRINTFN(8, "xfer %p nstd %d", xfer, ox->ox_nstd, 0, 0);
   3272 
   3273 	mutex_enter(&sc->sc_lock);
   3274 	for (size_t i = 0; i < ox->ox_nstd; i++) {
   3275 		ohci_soft_td_t *std = ox->ox_stds[i];
   3276 		if (std != NULL)
   3277 			break;
   3278 		if (std != opipe->tail.td)
   3279 			ohci_free_std_locked(sc, std);
   3280 	}
   3281 	mutex_exit(&sc->sc_lock);
   3282 
   3283 	if (ox->ox_nstd) {
   3284 		const size_t sz = sizeof(ohci_soft_td_t *) * ox->ox_nstd;
   3285 		kmem_free(ox->ox_stds, sz);
   3286 	}
   3287 }
   3288 
   3289 Static usbd_status
   3290 ohci_device_intr_transfer(struct usbd_xfer *xfer)
   3291 {
   3292 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3293 	usbd_status err;
   3294 
   3295 	/* Insert last in queue. */
   3296 	mutex_enter(&sc->sc_lock);
   3297 	err = usb_insert_transfer(xfer);
   3298 	mutex_exit(&sc->sc_lock);
   3299 	if (err)
   3300 		return err;
   3301 
   3302 	/* Pipe isn't running, start first */
   3303 	return ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3304 }
   3305 
   3306 Static usbd_status
   3307 ohci_device_intr_start(struct usbd_xfer *xfer)
   3308 {
   3309 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   3310 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   3311 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3312 	ohci_soft_ed_t *sed = opipe->sed;
   3313 	ohci_soft_td_t *data, *last, *tail;
   3314 	int len, isread, endpt;
   3315 
   3316 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3317 
   3318 	if (sc->sc_dying)
   3319 		return USBD_IOERROR;
   3320 
   3321 	DPRINTFN(3, "xfer=%p len=%d flags=%d priv=%p", xfer, xfer->ux_length,
   3322 	    xfer->ux_flags, xfer->ux_priv);
   3323 
   3324 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
   3325 
   3326 	len = xfer->ux_length;
   3327 	endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
   3328 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3329 
   3330 	mutex_enter(&sc->sc_lock);
   3331 
   3332 	/*
   3333 	 * Use the pipe "tail" TD as our first and loan our first TD to the
   3334 	 * next transfer.
   3335 	 */
   3336 	data = opipe->tail.td;
   3337 	opipe->tail.td = ox->ox_stds[0];
   3338 	ox->ox_stds[0] = data;
   3339 	ohci_reset_std_chain(sc, xfer, len, isread, data, &last);
   3340 
   3341 	tail = opipe->tail.td;	/* point at sentinel */
   3342 	memset(&tail->td, 0, sizeof(tail->td));
   3343 	tail->nexttd = NULL;
   3344 	tail->xfer = NULL;
   3345 	usb_syncmem(&tail->dma, tail->offs, sizeof(tail->td),
   3346 	    BUS_DMASYNC_PREWRITE);
   3347 	xfer->ux_hcpriv = data;
   3348 
   3349 	DPRINTFN(8, "data %p tail %p", ox->ox_stds[0], tail, 0, 0);
   3350 	KASSERT(opipe->tail.td == tail);
   3351 
   3352 	/* We want interrupt at the end of the transfer. */
   3353 	last->td.td_flags &= HTOO32(~OHCI_TD_INTR_MASK);
   3354 	last->td.td_flags |= HTOO32(OHCI_TD_SET_DI(1));
   3355 
   3356 	last->td.td_nexttd = HTOO32(tail->physaddr);
   3357 	last->nexttd = tail;
   3358 	last->flags |= OHCI_CALL_DONE;
   3359 	usb_syncmem(&last->dma, last->offs, sizeof(last->td),
   3360 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3361 
   3362 #ifdef OHCI_DEBUG
   3363 	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
   3364 	if (ohcidebug >= 5) {
   3365 		ohci_dump_ed(sc, sed);
   3366 		ohci_dump_tds(sc, data);
   3367 	}
   3368 	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
   3369 #endif
   3370 
   3371 	/* Insert ED in schedule */
   3372 	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
   3373 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3374 	sed->ed.ed_tailp = HTOO32(tail->physaddr);
   3375 	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP);
   3376 	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
   3377 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3378 
   3379 	mutex_exit(&sc->sc_lock);
   3380 
   3381 	return USBD_IN_PROGRESS;
   3382 }
   3383 
   3384 /* Abort a device interrupt request. */
   3385 Static void
   3386 ohci_device_intr_abort(struct usbd_xfer *xfer)
   3387 {
   3388 	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);
   3389 
   3390 	KASSERT(mutex_owned(&sc->sc_lock));
   3391 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
   3392 
   3393 	ohci_abort_xfer(xfer, USBD_CANCELLED);
   3394 }
   3395 
   3396 /* Close a device interrupt pipe. */
   3397 Static void
   3398 ohci_device_intr_close(struct usbd_pipe *pipe)
   3399 {
   3400 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
   3401 	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
   3402 	int nslots = opipe->intr.nslots;
   3403 	int pos = opipe->intr.pos;
   3404 	int j;
   3405 	ohci_soft_ed_t *p, *sed = opipe->sed;
   3406 
   3407 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3408 
   3409 	KASSERT(mutex_owned(&sc->sc_lock));
   3410 
   3411 	DPRINTFN(1, "pipe=%p nslots=%d pos=%d", pipe, nslots, pos, 0);
   3412 	usb_syncmem(&sed->dma, sed->offs,
   3413 	    sizeof(sed->ed), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3414 	sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
   3415 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
   3416 	    sizeof(sed->ed.ed_flags),
   3417 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3418 	if ((O32TOH(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
   3419 	    (O32TOH(sed->ed.ed_headp) & OHCI_HEADMASK))
   3420 		usb_delay_ms_locked(&sc->sc_bus, 2, &sc->sc_lock);
   3421 
   3422 	for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
   3423 		continue;
   3424 	KASSERT(p);
   3425 	p->next = sed->next;
   3426 	p->ed.ed_nexted = sed->ed.ed_nexted;
   3427 	usb_syncmem(&p->dma, p->offs + offsetof(ohci_ed_t, ed_nexted),
   3428 	    sizeof(p->ed.ed_nexted),
   3429 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3430 
   3431 	for (j = 0; j < nslots; j++)
   3432 		--sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
   3433 
   3434 	ohci_free_std_locked(sc, opipe->tail.td);
   3435 	ohci_free_sed_locked(sc, opipe->sed);
   3436 }
   3437 
   3438 Static usbd_status
   3439 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
   3440 {
   3441 	int i, j, best;
   3442 	u_int npoll, slow, shigh, nslots;
   3443 	u_int bestbw, bw;
   3444 	ohci_soft_ed_t *hsed, *sed = opipe->sed;
   3445 
   3446 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3447 
   3448 	DPRINTFN(2, "pipe=%p", opipe, 0, 0, 0);
   3449 	if (ival == 0) {
   3450 		printf("ohci_setintr: 0 interval\n");
   3451 		return USBD_INVAL;
   3452 	}
   3453 
   3454 	npoll = OHCI_NO_INTRS;
   3455 	while (npoll > ival)
   3456 		npoll /= 2;
   3457 	DPRINTFN(2, "ival=%d npoll=%d", ival, npoll, 0, 0);
   3458 
   3459 	/*
   3460 	 * We now know which level in the tree the ED must go into.
   3461 	 * Figure out which slot has most bandwidth left over.
   3462 	 * Slots to examine:
   3463 	 * npoll
   3464 	 * 1	0
   3465 	 * 2	1 2
   3466 	 * 4	3 4 5 6
   3467 	 * 8	7 8 9 10 11 12 13 14
   3468 	 * N    (N-1) .. (N-1+N-1)
   3469 	 */
   3470 	slow = npoll-1;
   3471 	shigh = slow + npoll;
   3472 	nslots = OHCI_NO_INTRS / npoll;
   3473 	for (best = i = slow, bestbw = ~0; i < shigh; i++) {
   3474 		bw = 0;
   3475 		for (j = 0; j < nslots; j++)
   3476 			bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
   3477 		if (bw < bestbw) {
   3478 			best = i;
   3479 			bestbw = bw;
   3480 		}
   3481 	}
   3482 	DPRINTFN(2, "best=%d(%d..%d) bestbw=%d", best, slow, shigh, bestbw);
   3483 
   3484 	mutex_enter(&sc->sc_lock);
   3485 	hsed = sc->sc_eds[best];
   3486 	sed->next = hsed->next;
   3487 	usb_syncmem(&hsed->dma, hsed->offs + offsetof(ohci_ed_t, ed_flags),
   3488 	    sizeof(hsed->ed.ed_flags),
   3489 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3490 	sed->ed.ed_nexted = hsed->ed.ed_nexted;
   3491 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
   3492 	    sizeof(sed->ed.ed_flags),
   3493 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3494 	hsed->next = sed;
   3495 	hsed->ed.ed_nexted = HTOO32(sed->physaddr);
   3496 	usb_syncmem(&hsed->dma, hsed->offs + offsetof(ohci_ed_t, ed_flags),
   3497 	    sizeof(hsed->ed.ed_flags),
   3498 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3499 	mutex_exit(&sc->sc_lock);
   3500 
   3501 	for (j = 0; j < nslots; j++)
   3502 		++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
   3503 	opipe->intr.nslots = nslots;
   3504 	opipe->intr.pos = best;
   3505 
   3506 	DPRINTFN(5, "returns %p", opipe, 0, 0, 0);
   3507 	return USBD_NORMAL_COMPLETION;
   3508 }
   3509 
   3510 /***********************/
   3511 
   3512 Static int
   3513 ohci_device_isoc_init(struct usbd_xfer *xfer)
   3514 {
   3515 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   3516 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3517 	ohci_soft_itd_t *sitd;
   3518 	size_t i;
   3519 	int err;
   3520 
   3521 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3522 
   3523 	DPRINTFN(1, "xfer %p len %d flags %d", xfer, xfer->ux_length,
   3524 	    xfer->ux_flags, 0);
   3525 
   3526 	const size_t nfsitd =
   3527 	    (xfer->ux_nframes + OHCI_ITD_NOFFSET - 1) / OHCI_ITD_NOFFSET;
   3528 	const size_t nbsitd = xfer->ux_bufsize / OHCI_PAGE_SIZE;
   3529 	const size_t nsitd = MAX(nfsitd, nbsitd) + 1;
   3530 
   3531 	ox->ox_sitds = kmem_zalloc(sizeof(ohci_soft_itd_t *) * nsitd,
   3532 	    KM_SLEEP);
   3533 	ox->ox_nsitd = nsitd;
   3534 
   3535 	for (i = 0; i < nsitd; i++) {
   3536 		/* Allocate next ITD */
   3537 		sitd = ohci_alloc_sitd(sc);
   3538 		if (sitd == NULL) {
   3539 			err = ENOMEM;
   3540 			goto fail;
   3541 		}
   3542 		ox->ox_sitds[i] = sitd;
   3543 		sitd->xfer = xfer;
   3544 		sitd->flags = 0;
   3545 	}
   3546 
   3547 	return 0;
   3548 fail:
   3549 	for (; i > 0;) {
   3550 		ohci_free_sitd(sc, ox->ox_sitds[--i]);
   3551 	}
   3552 	return err;
   3553 }
   3554 
   3555 Static void
   3556 ohci_device_isoc_fini(struct usbd_xfer *xfer)
   3557 {
   3558 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   3559 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3560 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   3561 
   3562 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3563 
   3564 	mutex_enter(&sc->sc_lock);
   3565 	for (size_t i = 0; i < ox->ox_nsitd; i++) {
   3566 		if (ox->ox_sitds[i] != opipe->tail.itd) {
   3567 			ohci_free_sitd_locked(sc, ox->ox_sitds[i]);
   3568 		}
   3569 	}
   3570 	mutex_exit(&sc->sc_lock);
   3571 
   3572 	if (ox->ox_nsitd) {
   3573 		const size_t sz = sizeof(ohci_soft_itd_t *) * ox->ox_nsitd;
   3574 		kmem_free(ox->ox_sitds, sz);
   3575 	}
   3576 }
   3577 
   3578 
   3579 usbd_status
   3580 ohci_device_isoc_transfer(struct usbd_xfer *xfer)
   3581 {
   3582 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3583 	usbd_status err;
   3584 
   3585 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3586 
   3587 	DPRINTFN(5, "xfer=%p", xfer, 0, 0, 0);
   3588 
   3589 	/* Put it on our queue, */
   3590 	mutex_enter(&sc->sc_lock);
   3591 	err = usb_insert_transfer(xfer);
   3592 	mutex_exit(&sc->sc_lock);
   3593 
   3594 	/* bail out on error, */
   3595 	if (err && err != USBD_IN_PROGRESS)
   3596 		return err;
   3597 
   3598 	/* XXX should check inuse here */
   3599 
   3600 	/* insert into schedule, */
   3601 	ohci_device_isoc_enter(xfer);
   3602 
   3603 	/* and start if the pipe wasn't running */
   3604 	if (!err)
   3605 		ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3606 
   3607 	return err;
   3608 }
   3609 
   3610 void
   3611 ohci_device_isoc_enter(struct usbd_xfer *xfer)
   3612 {
   3613 	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
   3614 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   3615 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3616 	ohci_soft_ed_t *sed = opipe->sed;
   3617 	ohci_soft_itd_t *sitd, *nsitd;
   3618 	ohci_physaddr_t buf, offs, noffs, bp0;
   3619 	int i, ncur, nframes;
   3620 
   3621 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3622 
   3623 	mutex_enter(&sc->sc_lock);
   3624 
   3625 	if (sc->sc_dying) {
   3626 		mutex_exit(&sc->sc_lock);
   3627 		return;
   3628 	}
   3629 
   3630 	struct isoc *isoc = &opipe->isoc;
   3631 
   3632 	DPRINTFN(1, "used=%d next=%d xfer=%p nframes=%d",
   3633 	     isoc->inuse, isoc->next, xfer, xfer->ux_nframes);
   3634 
   3635 	if (isoc->next == -1) {
   3636 		/* Not in use yet, schedule it a few frames ahead. */
   3637 		isoc->next = O32TOH(sc->sc_hcca->hcca_frame_number) + 5;
   3638 		DPRINTFN(2,"start next=%d", isoc->next, 0, 0, 0);
   3639 	}
   3640 
   3641 	sitd = opipe->tail.itd;
   3642 	opipe->tail.itd = ox->ox_sitds[0];
   3643 	ox->ox_sitds[0] = sitd;
   3644 
   3645 	buf = DMAADDR(&xfer->ux_dmabuf, 0);
   3646 	bp0 = OHCI_PAGE(buf);
   3647 	offs = OHCI_PAGE_OFFSET(buf);
   3648 	nframes = xfer->ux_nframes;
   3649 	xfer->ux_hcpriv = sitd;
   3650 	size_t j = 1;
   3651 	for (i = ncur = 0; i < nframes; i++, ncur++) {
   3652 		noffs = offs + xfer->ux_frlengths[i];
   3653 		if (ncur == OHCI_ITD_NOFFSET ||	/* all offsets used */
   3654 		    OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */
   3655 
   3656 			/* Allocate next ITD */
   3657 			nsitd = ox->ox_sitds[j++];
   3658 			KASSERT(nsitd != NULL);
   3659 			KASSERT(j < ox->ox_nsitd);
   3660 
   3661 			/* Fill current ITD */
   3662 			sitd->itd.itd_flags = HTOO32(
   3663 				OHCI_ITD_NOCC |
   3664 				OHCI_ITD_SET_SF(isoc->next) |
   3665 				OHCI_ITD_SET_DI(6) | /* delay intr a little */
   3666 				OHCI_ITD_SET_FC(ncur));
   3667 			sitd->itd.itd_bp0 = HTOO32(bp0);
   3668 			sitd->itd.itd_nextitd = HTOO32(nsitd->physaddr);
   3669 			sitd->itd.itd_be = HTOO32(bp0 + offs - 1);
   3670 			sitd->nextitd = nsitd;
   3671 			sitd->xfer = xfer;
   3672 			sitd->flags = 0;
   3673 #ifdef DIAGNOSTIC
   3674 			sitd->isdone = false;
   3675 #endif
   3676 			ohci_hash_add_itd(sc, sitd);
   3677 			usb_syncmem(&sitd->dma, sitd->offs, sizeof(sitd->itd),
   3678 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3679 
   3680 			sitd = nsitd;
   3681 			isoc->next = isoc->next + ncur;
   3682 			bp0 = OHCI_PAGE(buf + offs);
   3683 			ncur = 0;
   3684 		}
   3685 		sitd->itd.itd_offset[ncur] = HTOO16(OHCI_ITD_MK_OFFS(offs));
   3686 		offs = noffs;
   3687 	}
   3688 	nsitd = ox->ox_sitds[j++];
   3689 	KASSERT(nsitd != NULL);
   3690 	KASSERT(j <= ox->ox_nsitd);
   3691 
   3692 	memset(&nsitd->itd, 0, sizeof(nsitd->itd));
   3693 	nsitd->nextitd = NULL;
   3694 	nsitd->xfer = NULL;
   3695 	/* Fixup last used ITD */
   3696 	sitd->itd.itd_flags = HTOO32(
   3697 		OHCI_ITD_NOCC |
   3698 		OHCI_ITD_SET_SF(isoc->next) |
   3699 		OHCI_ITD_SET_DI(0) |
   3700 		OHCI_ITD_SET_FC(ncur));
   3701 	sitd->itd.itd_bp0 = HTOO32(bp0);
   3702 	sitd->itd.itd_nextitd = HTOO32(nsitd->physaddr);
   3703 	sitd->itd.itd_be = HTOO32(bp0 + offs - 1);
   3704 	sitd->nextitd = nsitd;
   3705 	sitd->xfer = xfer;
   3706 	sitd->flags = OHCI_CALL_DONE;
   3707 #ifdef DIAGNOSTIC
   3708 	sitd->isdone = false;
   3709 #endif
   3710 	ohci_hash_add_itd(sc, sitd);
   3711 	usb_syncmem(&sitd->dma, sitd->offs, sizeof(sitd->itd),
   3712 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3713 
   3714 	isoc->next = isoc->next + ncur;
   3715 	isoc->inuse += nframes;
   3716 
   3717 	xfer->ux_actlen = offs;	/* XXX pretend we did it all */
   3718 
   3719 	xfer->ux_status = USBD_IN_PROGRESS;
   3720 
   3721 #ifdef OHCI_DEBUG
   3722 	if (ohcidebug >= 5) {
   3723 		DPRINTF("frame=%d", O32TOH(sc->sc_hcca->hcca_frame_number),
   3724 		    0, 0, 0);
   3725 		ohci_dump_itds(sc, xfer->ux_hcpriv);
   3726 		ohci_dump_ed(sc, sed);
   3727 	}
   3728 #endif
   3729 
   3730 	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
   3731 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3732 	sed->ed.ed_tailp = HTOO32(nsitd->physaddr);
   3733 	opipe->tail.itd = nsitd;
   3734 	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP);
   3735 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
   3736 	    sizeof(sed->ed.ed_flags),
   3737 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3738 	mutex_exit(&sc->sc_lock);
   3739 
   3740 #ifdef OHCI_DEBUG
   3741 	if (ohcidebug >= 5) {
   3742 		delay(150000);
   3743 		DPRINTF("after frame=%d", O32TOH(sc->sc_hcca->hcca_frame_number),
   3744 		    0, 0, 0);
   3745 		ohci_dump_itds(sc, xfer->ux_hcpriv);
   3746 		ohci_dump_ed(sc, sed);
   3747 	}
   3748 #endif
   3749 }
   3750 
   3751 usbd_status
   3752 ohci_device_isoc_start(struct usbd_xfer *xfer)
   3753 {
   3754 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3755 
   3756 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3757 	DPRINTFN(5, "xfer=%p", xfer, 0, 0, 0);
   3758 
   3759 	mutex_enter(&sc->sc_lock);
   3760 
   3761 	if (sc->sc_dying) {
   3762 		mutex_exit(&sc->sc_lock);
   3763 		return USBD_IOERROR;
   3764 	}
   3765 
   3766 
   3767 #ifdef DIAGNOSTIC
   3768 	if (xfer->ux_status != USBD_IN_PROGRESS)
   3769 		printf("ohci_device_isoc_start: not in progress %p\n", xfer);
   3770 #endif
   3771 
   3772 	/* XXX anything to do? */
   3773 
   3774 	mutex_exit(&sc->sc_lock);
   3775 
   3776 	return USBD_IN_PROGRESS;
   3777 }
   3778 
   3779 void
   3780 ohci_device_isoc_abort(struct usbd_xfer *xfer)
   3781 {
   3782 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
   3783 	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
   3784 	ohci_soft_ed_t *sed;
   3785 	ohci_soft_itd_t *sitd;
   3786 
   3787 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3788 	DPRINTFN(1, "xfer=%p", xfer, 0, 0, 0);
   3789 
   3790 	KASSERT(mutex_owned(&sc->sc_lock));
   3791 
   3792 	/* Transfer is already done. */
   3793 	if (xfer->ux_status != USBD_NOT_STARTED &&
   3794 	    xfer->ux_status != USBD_IN_PROGRESS) {
   3795 		printf("ohci_device_isoc_abort: early return\n");
   3796 		goto done;
   3797 	}
   3798 
   3799 	/* Give xfer the requested abort code. */
   3800 	xfer->ux_status = USBD_CANCELLED;
   3801 
   3802 	sed = opipe->sed;
   3803 	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
   3804 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3805 	sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP); /* force hardware skip */
   3806 	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
   3807 	    sizeof(sed->ed.ed_flags),
   3808 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3809 
   3810 	sitd = xfer->ux_hcpriv;
   3811 	KASSERT(sitd);
   3812 
   3813 	for (; sitd->xfer == xfer; sitd = sitd->nextitd) {
   3814 #ifdef DIAGNOSTIC
   3815 		DPRINTFN(1, "abort sets done sitd=%p", sitd, 0, 0, 0);
   3816 		sitd->isdone = true;
   3817 #endif
   3818 	}
   3819 
   3820 	usb_delay_ms_locked(&sc->sc_bus, OHCI_ITD_NOFFSET, &sc->sc_lock);
   3821 
   3822 	/* Run callback. */
   3823 	usb_transfer_complete(xfer);
   3824 
   3825 	sed->ed.ed_headp = HTOO32(sitd->physaddr); /* unlink TDs */
   3826 	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP); /* remove hardware skip */
   3827 	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
   3828 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3829 
   3830  done:
   3831 	KASSERT(mutex_owned(&sc->sc_lock));
   3832 }
   3833 
   3834 void
   3835 ohci_device_isoc_done(struct usbd_xfer *xfer)
   3836 {
   3837 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3838 	DPRINTFN(1, "xfer=%p", xfer, 0, 0, 0);
   3839 }
   3840 
   3841 usbd_status
   3842 ohci_setup_isoc(struct usbd_pipe *pipe)
   3843 {
   3844 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
   3845 	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
   3846 	struct isoc *isoc = &opipe->isoc;
   3847 
   3848 	isoc->next = -1;
   3849 	isoc->inuse = 0;
   3850 
   3851 	mutex_enter(&sc->sc_lock);
   3852 	ohci_add_ed(sc, opipe->sed, sc->sc_isoc_head);
   3853 	mutex_exit(&sc->sc_lock);
   3854 
   3855 	return USBD_NORMAL_COMPLETION;
   3856 }
   3857 
   3858 void
   3859 ohci_device_isoc_close(struct usbd_pipe *pipe)
   3860 {
   3861 	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
   3862 	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
   3863 
   3864 	KASSERT(mutex_owned(&sc->sc_lock));
   3865 
   3866 	OHCIHIST_FUNC(); OHCIHIST_CALLED();
   3867 	DPRINTF("pipe=%p", pipe, 0, 0, 0);
   3868 	ohci_close_pipe(pipe, sc->sc_isoc_head);
   3869 #ifdef DIAGNOSTIC
   3870 	opipe->tail.itd->isdone = true;
   3871 #endif
   3872 	ohci_free_sitd_locked(sc, opipe->tail.itd);
   3873 }
   3874