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