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