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