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