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