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