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