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