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