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