Home | History | Annotate | Line # | Download | only in usb
ehci.c revision 1.198
      1 /*	$NetBSD: ehci.c,v 1.198 2013/01/09 23:02:59 skrll Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004-2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net), Charles M. Hannum,
      9  * Jeremy Morse (jeremy.morse (at) gmail.com), Jared D. McNeill
     10  * (jmcneill (at) invisible.ca) and Matthew R. Green (mrg (at) eterna.com.au).
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
     36  *
     37  * The EHCI 1.0 spec can be found at
     38  * http://www.intel.com/technology/usb/spec.htm
     39  * and the USB 2.0 spec at
     40  * http://www.usb.org/developers/docs/
     41  *
     42  */
     43 
     44 /*
     45  * TODO:
     46  * 1) hold off explorations by companion controllers until ehci has started.
     47  *
     48  * 2) The hub driver needs to handle and schedule the transaction translator,
     49  *    to assign place in frame where different devices get to go. See chapter
     50  *    on hubs in USB 2.0 for details.
     51  *
     52  * 3) Command failures are not recovered correctly.
     53  */
     54 
     55 #include <sys/cdefs.h>
     56 __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.198 2013/01/09 23:02:59 skrll Exp $");
     57 
     58 #include "ohci.h"
     59 #include "uhci.h"
     60 
     61 #include <sys/param.h>
     62 #include <sys/systm.h>
     63 #include <sys/kernel.h>
     64 #include <sys/kmem.h>
     65 #include <sys/device.h>
     66 #include <sys/select.h>
     67 #include <sys/proc.h>
     68 #include <sys/queue.h>
     69 #include <sys/mutex.h>
     70 #include <sys/bus.h>
     71 #include <sys/cpu.h>
     72 
     73 #include <machine/endian.h>
     74 
     75 #include <dev/usb/usb.h>
     76 #include <dev/usb/usbdi.h>
     77 #include <dev/usb/usbdivar.h>
     78 #include <dev/usb/usb_mem.h>
     79 #include <dev/usb/usb_quirks.h>
     80 
     81 #include <dev/usb/ehcireg.h>
     82 #include <dev/usb/ehcivar.h>
     83 #include <dev/usb/usbroothub_subr.h>
     84 
     85 #ifdef EHCI_DEBUG
     86 #include <sys/kprintf.h>
     87 static void
     88 ehciprintf(const char *fmt, ...)
     89 {
     90 	va_list ap;
     91 
     92 	va_start(ap, fmt);
     93 	kprintf(fmt, TOLOG|TOCONS, NULL, NULL, ap);
     94 	va_end(ap);
     95 }
     96 
     97 #define DPRINTF(x)	do { if (ehcidebug) ehciprintf x; } while(0)
     98 #define DPRINTFN(n,x)	do { if (ehcidebug>(n)) ehciprintf x; } while (0)
     99 int ehcidebug = 0;
    100 #else
    101 #define DPRINTF(x)
    102 #define DPRINTFN(n,x)
    103 #endif
    104 
    105 struct ehci_pipe {
    106 	struct usbd_pipe pipe;
    107 	int nexttoggle;
    108 
    109 	ehci_soft_qh_t *sqh;
    110 	union {
    111 		ehci_soft_qtd_t *qtd;
    112 		/* ehci_soft_itd_t *itd; */
    113 	} tail;
    114 	union {
    115 		/* Control pipe */
    116 		struct {
    117 			usb_dma_t reqdma;
    118 		} ctl;
    119 		/* Interrupt pipe */
    120 		struct {
    121 			u_int length;
    122 		} intr;
    123 		/* Bulk pipe */
    124 		struct {
    125 			u_int length;
    126 		} bulk;
    127 		/* Iso pipe */
    128 		struct {
    129 			u_int next_frame;
    130 			u_int cur_xfers;
    131 		} isoc;
    132 	} u;
    133 };
    134 
    135 Static usbd_status	ehci_open(usbd_pipe_handle);
    136 Static void		ehci_poll(struct usbd_bus *);
    137 Static void		ehci_softintr(void *);
    138 Static int		ehci_intr1(ehci_softc_t *);
    139 Static void		ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
    140 Static void		ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
    141 Static void		ehci_check_qh_intr(ehci_softc_t *, struct ehci_xfer *);
    142 Static void		ehci_check_itd_intr(ehci_softc_t *, struct ehci_xfer *);
    143 Static void		ehci_idone(struct ehci_xfer *);
    144 Static void		ehci_timeout(void *);
    145 Static void		ehci_timeout_task(void *);
    146 Static void		ehci_intrlist_timeout(void *);
    147 Static void		ehci_doorbell(void *);
    148 Static void		ehci_pcd(void *);
    149 
    150 Static usbd_status	ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
    151 Static void		ehci_freem(struct usbd_bus *, usb_dma_t *);
    152 
    153 Static usbd_xfer_handle	ehci_allocx(struct usbd_bus *);
    154 Static void		ehci_freex(struct usbd_bus *, usbd_xfer_handle);
    155 Static void		ehci_get_lock(struct usbd_bus *, kmutex_t **);
    156 
    157 Static usbd_status	ehci_root_ctrl_transfer(usbd_xfer_handle);
    158 Static usbd_status	ehci_root_ctrl_start(usbd_xfer_handle);
    159 Static void		ehci_root_ctrl_abort(usbd_xfer_handle);
    160 Static void		ehci_root_ctrl_close(usbd_pipe_handle);
    161 Static void		ehci_root_ctrl_done(usbd_xfer_handle);
    162 
    163 Static usbd_status	ehci_root_intr_transfer(usbd_xfer_handle);
    164 Static usbd_status	ehci_root_intr_start(usbd_xfer_handle);
    165 Static void		ehci_root_intr_abort(usbd_xfer_handle);
    166 Static void		ehci_root_intr_close(usbd_pipe_handle);
    167 Static void		ehci_root_intr_done(usbd_xfer_handle);
    168 
    169 Static usbd_status	ehci_device_ctrl_transfer(usbd_xfer_handle);
    170 Static usbd_status	ehci_device_ctrl_start(usbd_xfer_handle);
    171 Static void		ehci_device_ctrl_abort(usbd_xfer_handle);
    172 Static void		ehci_device_ctrl_close(usbd_pipe_handle);
    173 Static void		ehci_device_ctrl_done(usbd_xfer_handle);
    174 
    175 Static usbd_status	ehci_device_bulk_transfer(usbd_xfer_handle);
    176 Static usbd_status	ehci_device_bulk_start(usbd_xfer_handle);
    177 Static void		ehci_device_bulk_abort(usbd_xfer_handle);
    178 Static void		ehci_device_bulk_close(usbd_pipe_handle);
    179 Static void		ehci_device_bulk_done(usbd_xfer_handle);
    180 
    181 Static usbd_status	ehci_device_intr_transfer(usbd_xfer_handle);
    182 Static usbd_status	ehci_device_intr_start(usbd_xfer_handle);
    183 Static void		ehci_device_intr_abort(usbd_xfer_handle);
    184 Static void		ehci_device_intr_close(usbd_pipe_handle);
    185 Static void		ehci_device_intr_done(usbd_xfer_handle);
    186 
    187 Static usbd_status	ehci_device_isoc_transfer(usbd_xfer_handle);
    188 Static usbd_status	ehci_device_isoc_start(usbd_xfer_handle);
    189 Static void		ehci_device_isoc_abort(usbd_xfer_handle);
    190 Static void		ehci_device_isoc_close(usbd_pipe_handle);
    191 Static void		ehci_device_isoc_done(usbd_xfer_handle);
    192 
    193 Static void		ehci_device_clear_toggle(usbd_pipe_handle pipe);
    194 Static void		ehci_noop(usbd_pipe_handle pipe);
    195 
    196 Static void		ehci_disown(ehci_softc_t *, int, int);
    197 
    198 Static ehci_soft_qh_t  *ehci_alloc_sqh(ehci_softc_t *);
    199 Static void		ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
    200 
    201 Static ehci_soft_qtd_t  *ehci_alloc_sqtd(ehci_softc_t *);
    202 Static void		ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
    203 Static usbd_status	ehci_alloc_sqtd_chain(struct ehci_pipe *,
    204 			    ehci_softc_t *, int, int, usbd_xfer_handle,
    205 			    ehci_soft_qtd_t **, ehci_soft_qtd_t **);
    206 Static void		ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qtd_t *,
    207 					    ehci_soft_qtd_t *);
    208 
    209 Static ehci_soft_itd_t	*ehci_alloc_itd(ehci_softc_t *sc);
    210 Static void		ehci_free_itd(ehci_softc_t *sc, ehci_soft_itd_t *itd);
    211 Static void 		ehci_rem_free_itd_chain(ehci_softc_t *sc,
    212 						struct ehci_xfer *exfer);
    213 Static void 		ehci_abort_isoc_xfer(usbd_xfer_handle xfer,
    214 						usbd_status status);
    215 
    216 Static usbd_status	ehci_device_request(usbd_xfer_handle xfer);
    217 
    218 Static usbd_status	ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *,
    219 			    int ival);
    220 
    221 Static void		ehci_add_qh(ehci_softc_t *, ehci_soft_qh_t *,
    222 				    ehci_soft_qh_t *);
    223 Static void		ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
    224 				    ehci_soft_qh_t *);
    225 Static void		ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *);
    226 Static void		ehci_sync_hc(ehci_softc_t *);
    227 
    228 Static void		ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
    229 Static void		ehci_abort_xfer(usbd_xfer_handle, usbd_status);
    230 
    231 #ifdef EHCI_DEBUG
    232 Static void		ehci_dump_regs(ehci_softc_t *);
    233 void			ehci_dump(void);
    234 Static ehci_softc_t 	*theehci;
    235 Static void		ehci_dump_link(ehci_link_t, int);
    236 Static void		ehci_dump_sqtds(ehci_soft_qtd_t *);
    237 Static void		ehci_dump_sqtd(ehci_soft_qtd_t *);
    238 Static void		ehci_dump_qtd(ehci_qtd_t *);
    239 Static void		ehci_dump_sqh(ehci_soft_qh_t *);
    240 #if notyet
    241 Static void		ehci_dump_sitd(struct ehci_soft_itd *itd);
    242 Static void		ehci_dump_itd(struct ehci_soft_itd *);
    243 #endif
    244 #ifdef DIAGNOSTIC
    245 Static void		ehci_dump_exfer(struct ehci_xfer *);
    246 #endif
    247 #endif
    248 
    249 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
    250 
    251 #define EHCI_INTR_ENDPT 1
    252 
    253 #define ehci_add_intr_list(sc, ex) \
    254 	TAILQ_INSERT_TAIL(&(sc)->sc_intrhead, (ex), inext);
    255 #define ehci_del_intr_list(sc, ex) \
    256 	do { \
    257 		TAILQ_REMOVE(&sc->sc_intrhead, (ex), inext); \
    258 		(ex)->inext.tqe_prev = NULL; \
    259 	} while (0)
    260 #define ehci_active_intr_list(ex) ((ex)->inext.tqe_prev != NULL)
    261 
    262 Static const struct usbd_bus_methods ehci_bus_methods = {
    263 	.open_pipe =	ehci_open,
    264 	.soft_intr =	ehci_softintr,
    265 	.do_poll =	ehci_poll,
    266 	.allocm =	ehci_allocm,
    267 	.freem =	ehci_freem,
    268 	.allocx =	ehci_allocx,
    269 	.freex =	ehci_freex,
    270 	.get_lock =	ehci_get_lock,
    271 };
    272 
    273 Static const struct usbd_pipe_methods ehci_root_ctrl_methods = {
    274 	.transfer =	ehci_root_ctrl_transfer,
    275 	.start =	ehci_root_ctrl_start,
    276 	.abort =	ehci_root_ctrl_abort,
    277 	.close =	ehci_root_ctrl_close,
    278 	.cleartoggle =	ehci_noop,
    279 	.done =		ehci_root_ctrl_done,
    280 };
    281 
    282 Static const struct usbd_pipe_methods ehci_root_intr_methods = {
    283 	.transfer =	ehci_root_intr_transfer,
    284 	.start =	ehci_root_intr_start,
    285 	.abort =	ehci_root_intr_abort,
    286 	.close =	ehci_root_intr_close,
    287 	.cleartoggle =	ehci_noop,
    288 	.done =		ehci_root_intr_done,
    289 };
    290 
    291 Static const struct usbd_pipe_methods ehci_device_ctrl_methods = {
    292 	.transfer =	ehci_device_ctrl_transfer,
    293 	.start =	ehci_device_ctrl_start,
    294 	.abort =	ehci_device_ctrl_abort,
    295 	.close =	ehci_device_ctrl_close,
    296 	.cleartoggle =	ehci_noop,
    297 	.done =		ehci_device_ctrl_done,
    298 };
    299 
    300 Static const struct usbd_pipe_methods ehci_device_intr_methods = {
    301 	.transfer =	ehci_device_intr_transfer,
    302 	.start =	ehci_device_intr_start,
    303 	.abort =	ehci_device_intr_abort,
    304 	.close =	ehci_device_intr_close,
    305 	.cleartoggle =	ehci_device_clear_toggle,
    306 	.done =		ehci_device_intr_done,
    307 };
    308 
    309 Static const struct usbd_pipe_methods ehci_device_bulk_methods = {
    310 	.transfer =	ehci_device_bulk_transfer,
    311 	.start =	ehci_device_bulk_start,
    312 	.abort =	ehci_device_bulk_abort,
    313 	.close =	ehci_device_bulk_close,
    314 	.cleartoggle =	ehci_device_clear_toggle,
    315 	.done =		ehci_device_bulk_done,
    316 };
    317 
    318 Static const struct usbd_pipe_methods ehci_device_isoc_methods = {
    319 	.transfer =	ehci_device_isoc_transfer,
    320 	.start =	ehci_device_isoc_start,
    321 	.abort =	ehci_device_isoc_abort,
    322 	.close =	ehci_device_isoc_close,
    323 	.cleartoggle =	ehci_noop,
    324 	.done =		ehci_device_isoc_done,
    325 };
    326 
    327 static const uint8_t revbits[EHCI_MAX_POLLRATE] = {
    328 0x00,0x40,0x20,0x60,0x10,0x50,0x30,0x70,0x08,0x48,0x28,0x68,0x18,0x58,0x38,0x78,
    329 0x04,0x44,0x24,0x64,0x14,0x54,0x34,0x74,0x0c,0x4c,0x2c,0x6c,0x1c,0x5c,0x3c,0x7c,
    330 0x02,0x42,0x22,0x62,0x12,0x52,0x32,0x72,0x0a,0x4a,0x2a,0x6a,0x1a,0x5a,0x3a,0x7a,
    331 0x06,0x46,0x26,0x66,0x16,0x56,0x36,0x76,0x0e,0x4e,0x2e,0x6e,0x1e,0x5e,0x3e,0x7e,
    332 0x01,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x09,0x49,0x29,0x69,0x19,0x59,0x39,0x79,
    333 0x05,0x45,0x25,0x65,0x15,0x55,0x35,0x75,0x0d,0x4d,0x2d,0x6d,0x1d,0x5d,0x3d,0x7d,
    334 0x03,0x43,0x23,0x63,0x13,0x53,0x33,0x73,0x0b,0x4b,0x2b,0x6b,0x1b,0x5b,0x3b,0x7b,
    335 0x07,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0x0f,0x4f,0x2f,0x6f,0x1f,0x5f,0x3f,0x7f,
    336 };
    337 
    338 usbd_status
    339 ehci_init(ehci_softc_t *sc)
    340 {
    341 	u_int32_t vers, sparams, cparams, hcr;
    342 	u_int i;
    343 	usbd_status err;
    344 	ehci_soft_qh_t *sqh;
    345 	u_int ncomp;
    346 
    347 	DPRINTF(("ehci_init: start\n"));
    348 #ifdef EHCI_DEBUG
    349 	theehci = sc;
    350 #endif
    351 
    352 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    353 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
    354 	cv_init(&sc->sc_softwake_cv, "ehciab");
    355 	cv_init(&sc->sc_doorbell, "ehcidi");
    356 
    357 	sc->sc_doorbell_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
    358 	    ehci_doorbell, sc);
    359 	sc->sc_pcd_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
    360 	    ehci_pcd, sc);
    361 
    362 	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
    363 
    364 	vers = EREAD2(sc, EHCI_HCIVERSION);
    365 	aprint_verbose("%s: EHCI version %x.%x\n", device_xname(sc->sc_dev),
    366 	       vers >> 8, vers & 0xff);
    367 
    368 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
    369 	DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
    370 	sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
    371 	ncomp = EHCI_HCS_N_CC(sparams);
    372 	if (ncomp != sc->sc_ncomp) {
    373 		aprint_verbose("%s: wrong number of companions (%d != %d)\n",
    374 			       device_xname(sc->sc_dev), ncomp, sc->sc_ncomp);
    375 #if NOHCI == 0 || NUHCI == 0
    376 		aprint_error("%s: ohci or uhci probably not configured\n",
    377 			     device_xname(sc->sc_dev));
    378 #endif
    379 		if (ncomp < sc->sc_ncomp)
    380 			sc->sc_ncomp = ncomp;
    381 	}
    382 	if (sc->sc_ncomp > 0) {
    383 		KASSERT(!(sc->sc_flags & EHCIF_ETTF));
    384 		aprint_normal("%s: companion controller%s, %d port%s each:",
    385 		    device_xname(sc->sc_dev), sc->sc_ncomp!=1 ? "s" : "",
    386 		    EHCI_HCS_N_PCC(sparams),
    387 		    EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
    388 		for (i = 0; i < sc->sc_ncomp; i++)
    389 			aprint_normal(" %s", device_xname(sc->sc_comps[i]));
    390 		aprint_normal("\n");
    391 	}
    392 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
    393 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
    394 	DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
    395 	sc->sc_hasppc = EHCI_HCS_PPC(sparams);
    396 
    397 	if (EHCI_HCC_64BIT(cparams)) {
    398 		/* MUST clear segment register if 64 bit capable. */
    399 		EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
    400 	}
    401 
    402 	sc->sc_bus.usbrev = USBREV_2_0;
    403 
    404 	usb_setup_reserve(sc->sc_dev, &sc->sc_dma_reserve, sc->sc_bus.dmatag,
    405 	    USB_MEM_RESERVE);
    406 
    407 	/* Reset the controller */
    408 	DPRINTF(("%s: resetting\n", device_xname(sc->sc_dev)));
    409 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
    410 	usb_delay_ms(&sc->sc_bus, 1);
    411 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
    412 	for (i = 0; i < 100; i++) {
    413 		usb_delay_ms(&sc->sc_bus, 1);
    414 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
    415 		if (!hcr)
    416 			break;
    417 	}
    418 	if (hcr) {
    419 		aprint_error("%s: reset timeout\n", device_xname(sc->sc_dev));
    420 		return (USBD_IOERROR);
    421 	}
    422 	if (sc->sc_vendor_init)
    423 		sc->sc_vendor_init(sc);
    424 
    425 	/*
    426 	 * If we are doing embedded transaction translation function, force
    427 	 * the controller to host mode.
    428 	 */
    429 	if (sc->sc_flags & EHCIF_ETTF) {
    430 		uint32_t usbmode = EREAD4(sc, EHCI_USBMODE);
    431 		usbmode &= ~EHCI_USBMODE_CM;
    432 		usbmode |= EHCI_USBMODE_CM_HOST;
    433 		EWRITE4(sc, EHCI_USBMODE, usbmode);
    434 	}
    435 
    436 	/* XXX need proper intr scheduling */
    437 	sc->sc_rand = 96;
    438 
    439 	/* frame list size at default, read back what we got and use that */
    440 	switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
    441 	case 0: sc->sc_flsize = 1024; break;
    442 	case 1: sc->sc_flsize = 512; break;
    443 	case 2: sc->sc_flsize = 256; break;
    444 	case 3: return (USBD_IOERROR);
    445 	}
    446 	err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t),
    447 	    EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
    448 	if (err)
    449 		return (err);
    450 	DPRINTF(("%s: flsize=%d\n", device_xname(sc->sc_dev),sc->sc_flsize));
    451 	sc->sc_flist = KERNADDR(&sc->sc_fldma, 0);
    452 
    453 	for (i = 0; i < sc->sc_flsize; i++) {
    454 		sc->sc_flist[i] = EHCI_NULL;
    455 	}
    456 
    457 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
    458 
    459 	sc->sc_softitds = kmem_zalloc(sc->sc_flsize * sizeof(ehci_soft_itd_t *),
    460 				     KM_SLEEP);
    461 	if (sc->sc_softitds == NULL)
    462 		return ENOMEM;
    463 	LIST_INIT(&sc->sc_freeitds);
    464 	TAILQ_INIT(&sc->sc_intrhead);
    465 
    466 	/* Set up the bus struct. */
    467 	sc->sc_bus.methods = &ehci_bus_methods;
    468 	sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
    469 
    470 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
    471 
    472 	/*
    473 	 * Allocate the interrupt dummy QHs. These are arranged to give poll
    474 	 * intervals that are powers of 2 times 1ms.
    475 	 */
    476 	for (i = 0; i < EHCI_INTRQHS; i++) {
    477 		sqh = ehci_alloc_sqh(sc);
    478 		if (sqh == NULL) {
    479 			err = USBD_NOMEM;
    480 			goto bad1;
    481 		}
    482 		sc->sc_islots[i].sqh = sqh;
    483 	}
    484 	for (i = 0; i < EHCI_INTRQHS; i++) {
    485 		sqh = sc->sc_islots[i].sqh;
    486 		if (i == 0) {
    487 			/* The last (1ms) QH terminates. */
    488 			sqh->qh.qh_link = EHCI_NULL;
    489 			sqh->next = NULL;
    490 		} else {
    491 			/* Otherwise the next QH has half the poll interval */
    492 			sqh->next = sc->sc_islots[(i + 1) / 2 - 1].sqh;
    493 			sqh->qh.qh_link = htole32(sqh->next->physaddr |
    494 			    EHCI_LINK_QH);
    495 		}
    496 		sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
    497 		sqh->qh.qh_curqtd = EHCI_NULL;
    498 		sqh->next = NULL;
    499 		sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
    500 		sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
    501 		sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
    502 		sqh->sqtd = NULL;
    503 		usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
    504 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    505 	}
    506 	/* Point the frame list at the last level (128ms). */
    507 	for (i = 0; i < sc->sc_flsize; i++) {
    508 		int j;
    509 
    510 		j = (i & ~(EHCI_MAX_POLLRATE-1)) |
    511 		    revbits[i & (EHCI_MAX_POLLRATE-1)];
    512 		sc->sc_flist[j] = htole32(EHCI_LINK_QH |
    513 		    sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES - 1,
    514 		    i)].sqh->physaddr);
    515 	}
    516 	usb_syncmem(&sc->sc_fldma, 0, sc->sc_flsize * sizeof(ehci_link_t),
    517 	    BUS_DMASYNC_PREWRITE);
    518 
    519 	/* Allocate dummy QH that starts the async list. */
    520 	sqh = ehci_alloc_sqh(sc);
    521 	if (sqh == NULL) {
    522 		err = USBD_NOMEM;
    523 		goto bad1;
    524 	}
    525 	/* Fill the QH */
    526 	sqh->qh.qh_endp =
    527 	    htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
    528 	sqh->qh.qh_link =
    529 	    htole32(sqh->physaddr | EHCI_LINK_QH);
    530 	sqh->qh.qh_curqtd = EHCI_NULL;
    531 	sqh->next = NULL;
    532 	/* Fill the overlay qTD */
    533 	sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
    534 	sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
    535 	sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
    536 	sqh->sqtd = NULL;
    537 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
    538 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    539 #ifdef EHCI_DEBUG
    540 	if (ehcidebug) {
    541 		ehci_dump_sqh(sqh);
    542 	}
    543 #endif
    544 
    545 	/* Point to async list */
    546 	sc->sc_async_head = sqh;
    547 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
    548 
    549 	callout_init(&sc->sc_tmo_intrlist, CALLOUT_MPSAFE);
    550 
    551 	/* Turn on controller */
    552 	EOWRITE4(sc, EHCI_USBCMD,
    553 		 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */
    554 		 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
    555 		 EHCI_CMD_ASE |
    556 		 EHCI_CMD_PSE |
    557 		 EHCI_CMD_RS);
    558 
    559 	/* Take over port ownership */
    560 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
    561 
    562 	for (i = 0; i < 100; i++) {
    563 		usb_delay_ms(&sc->sc_bus, 1);
    564 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
    565 		if (!hcr)
    566 			break;
    567 	}
    568 	if (hcr) {
    569 		aprint_error("%s: run timeout\n", device_xname(sc->sc_dev));
    570 		return (USBD_IOERROR);
    571 	}
    572 
    573 	/* Enable interrupts */
    574 	DPRINTFN(1,("ehci_init: enabling\n"));
    575 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    576 
    577 	return (USBD_NORMAL_COMPLETION);
    578 
    579 #if 0
    580  bad2:
    581 	ehci_free_sqh(sc, sc->sc_async_head);
    582 #endif
    583  bad1:
    584 	usb_freemem(&sc->sc_bus, &sc->sc_fldma);
    585 	return (err);
    586 }
    587 
    588 int
    589 ehci_intr(void *v)
    590 {
    591 	ehci_softc_t *sc = v;
    592 	int ret = 0;
    593 
    594 	if (sc == NULL)
    595 		return 0;
    596 
    597 	mutex_spin_enter(&sc->sc_intr_lock);
    598 
    599 	if (sc->sc_dying || !device_has_power(sc->sc_dev))
    600 		goto done;
    601 
    602 	/* If we get an interrupt while polling, then just ignore it. */
    603 	if (sc->sc_bus.use_polling) {
    604 		u_int32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
    605 
    606 		if (intrs)
    607 			EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
    608 #ifdef DIAGNOSTIC
    609 		DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
    610 #endif
    611 		goto done;
    612 	}
    613 
    614 	ret = ehci_intr1(sc);
    615 
    616 done:
    617 	mutex_spin_exit(&sc->sc_intr_lock);
    618 	return ret;
    619 }
    620 
    621 Static int
    622 ehci_intr1(ehci_softc_t *sc)
    623 {
    624 	u_int32_t intrs, eintrs;
    625 
    626 	DPRINTFN(20,("ehci_intr1: enter\n"));
    627 
    628 	/* In case the interrupt occurs before initialization has completed. */
    629 	if (sc == NULL) {
    630 #ifdef DIAGNOSTIC
    631 		printf("ehci_intr1: sc == NULL\n");
    632 #endif
    633 		return (0);
    634 	}
    635 
    636 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    637 
    638 	intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
    639 	if (!intrs)
    640 		return (0);
    641 
    642 	eintrs = intrs & sc->sc_eintrs;
    643 	DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
    644 		     sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
    645 		     (u_int)eintrs));
    646 	if (!eintrs)
    647 		return (0);
    648 
    649 	EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
    650 	sc->sc_bus.no_intrs++;
    651 	if (eintrs & EHCI_STS_IAA) {
    652 		DPRINTF(("ehci_intr1: door bell\n"));
    653 		kpreempt_disable();
    654 		softint_schedule(sc->sc_doorbell_si);
    655 		kpreempt_enable();
    656 		eintrs &= ~EHCI_STS_IAA;
    657 	}
    658 	if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
    659 		DPRINTFN(5,("ehci_intr1: %s %s\n",
    660 			    eintrs & EHCI_STS_INT ? "INT" : "",
    661 			    eintrs & EHCI_STS_ERRINT ? "ERRINT" : ""));
    662 		usb_schedsoftintr(&sc->sc_bus);
    663 		eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
    664 	}
    665 	if (eintrs & EHCI_STS_HSE) {
    666 		printf("%s: unrecoverable error, controller halted\n",
    667 		       device_xname(sc->sc_dev));
    668 		/* XXX what else */
    669 	}
    670 	if (eintrs & EHCI_STS_PCD) {
    671 		kpreempt_disable();
    672 		softint_schedule(sc->sc_pcd_si);
    673 		kpreempt_enable();
    674 		eintrs &= ~EHCI_STS_PCD;
    675 	}
    676 
    677 	if (eintrs != 0) {
    678 		/* Block unprocessed interrupts. */
    679 		sc->sc_eintrs &= ~eintrs;
    680 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    681 		printf("%s: blocking intrs 0x%x\n",
    682 		       device_xname(sc->sc_dev), eintrs);
    683 	}
    684 
    685 	return (1);
    686 }
    687 
    688 Static void
    689 ehci_doorbell(void *addr)
    690 {
    691 	ehci_softc_t *sc = addr;
    692 
    693 	mutex_enter(&sc->sc_lock);
    694 	cv_broadcast(&sc->sc_doorbell);
    695 	mutex_exit(&sc->sc_lock);
    696 }
    697 
    698 Static void
    699 ehci_pcd(void *addr)
    700 {
    701 	ehci_softc_t *sc = addr;
    702 	usbd_xfer_handle xfer;
    703 	usbd_pipe_handle pipe;
    704 	u_char *p;
    705 	int i, m;
    706 
    707 	mutex_enter(&sc->sc_lock);
    708 	xfer = sc->sc_intrxfer;
    709 
    710 	if (xfer == NULL) {
    711 		/* Just ignore the change. */
    712 		goto done;
    713 	}
    714 
    715 	pipe = xfer->pipe;
    716 
    717 	p = KERNADDR(&xfer->dmabuf, 0);
    718 	m = min(sc->sc_noport, xfer->length * 8 - 1);
    719 	memset(p, 0, xfer->length);
    720 	for (i = 1; i <= m; i++) {
    721 		/* Pick out CHANGE bits from the status reg. */
    722 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
    723 			p[i/8] |= 1 << (i%8);
    724 	}
    725 	DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
    726 	xfer->actlen = xfer->length;
    727 	xfer->status = USBD_NORMAL_COMPLETION;
    728 
    729 	usb_transfer_complete(xfer);
    730 
    731 done:
    732 	mutex_exit(&sc->sc_lock);
    733 }
    734 
    735 Static void
    736 ehci_softintr(void *v)
    737 {
    738 	struct usbd_bus *bus = v;
    739 	ehci_softc_t *sc = bus->hci_private;
    740 	struct ehci_xfer *ex, *nextex;
    741 
    742 	KASSERT(sc->sc_bus.use_polling || mutex_owned(&sc->sc_lock));
    743 
    744 	DPRINTFN(10,("%s: ehci_softintr\n", device_xname(sc->sc_dev)));
    745 
    746 	/*
    747 	 * The only explanation I can think of for why EHCI is as brain dead
    748 	 * as UHCI interrupt-wise is that Intel was involved in both.
    749 	 * An interrupt just tells us that something is done, we have no
    750 	 * clue what, so we need to scan through all active transfers. :-(
    751 	 */
    752 	for (ex = TAILQ_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
    753 		nextex = TAILQ_NEXT(ex, inext);
    754 		ehci_check_intr(sc, ex);
    755 	}
    756 
    757 	/* Schedule a callout to catch any dropped transactions. */
    758 	if ((sc->sc_flags & EHCIF_DROPPED_INTR_WORKAROUND) &&
    759 	    !TAILQ_EMPTY(&sc->sc_intrhead))
    760 		callout_reset(&sc->sc_tmo_intrlist,
    761 		    hz, ehci_intrlist_timeout, sc);
    762 
    763 	if (sc->sc_softwake) {
    764 		sc->sc_softwake = 0;
    765 		cv_broadcast(&sc->sc_softwake_cv);
    766 	}
    767 }
    768 
    769 /* Check for an interrupt. */
    770 Static void
    771 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
    772 {
    773 	int attr;
    774 
    775 	DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex));
    776 
    777 	KASSERT(mutex_owned(&sc->sc_lock));
    778 
    779 	attr = ex->xfer.pipe->endpoint->edesc->bmAttributes;
    780 	if (UE_GET_XFERTYPE(attr) == UE_ISOCHRONOUS)
    781 		ehci_check_itd_intr(sc, ex);
    782 	else
    783 		ehci_check_qh_intr(sc, ex);
    784 
    785 	return;
    786 }
    787 
    788 Static void
    789 ehci_check_qh_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
    790 {
    791 	ehci_soft_qtd_t *sqtd, *lsqtd;
    792 	__uint32_t status;
    793 
    794 	KASSERT(mutex_owned(&sc->sc_lock));
    795 
    796 	if (ex->sqtdstart == NULL) {
    797 		printf("ehci_check_qh_intr: not valid sqtd\n");
    798 		return;
    799 	}
    800 
    801 	lsqtd = ex->sqtdend;
    802 #ifdef DIAGNOSTIC
    803 	if (lsqtd == NULL) {
    804 		printf("ehci_check_qh_intr: lsqtd==0\n");
    805 		return;
    806 	}
    807 #endif
    808 	/*
    809 	 * If the last TD is still active we need to check whether there
    810 	 * is a an error somewhere in the middle, or whether there was a
    811 	 * short packet (SPD and not ACTIVE).
    812 	 */
    813 	usb_syncmem(&lsqtd->dma,
    814 	    lsqtd->offs + offsetof(ehci_qtd_t, qtd_status),
    815 	    sizeof(lsqtd->qtd.qtd_status),
    816 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    817 	if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) {
    818 		DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex));
    819 		for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) {
    820 			usb_syncmem(&sqtd->dma,
    821 			    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
    822 			    sizeof(sqtd->qtd.qtd_status),
    823 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    824 			status = le32toh(sqtd->qtd.qtd_status);
    825 			usb_syncmem(&sqtd->dma,
    826 			    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
    827 			    sizeof(sqtd->qtd.qtd_status), BUS_DMASYNC_PREREAD);
    828 			/* If there's an active QTD the xfer isn't done. */
    829 			if (status & EHCI_QTD_ACTIVE)
    830 				break;
    831 			/* Any kind of error makes the xfer done. */
    832 			if (status & EHCI_QTD_HALTED)
    833 				goto done;
    834 			/* We want short packets, and it is short: it's done */
    835 			if (EHCI_QTD_GET_BYTES(status) != 0)
    836 				goto done;
    837 		}
    838 		DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
    839 			      ex, ex->sqtdstart));
    840 		usb_syncmem(&lsqtd->dma,
    841 		    lsqtd->offs + offsetof(ehci_qtd_t, qtd_status),
    842 		    sizeof(lsqtd->qtd.qtd_status), BUS_DMASYNC_PREREAD);
    843 		return;
    844 	}
    845  done:
    846 	DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex));
    847 	callout_stop(&ex->xfer.timeout_handle);
    848 	ehci_idone(ex);
    849 }
    850 
    851 Static void
    852 ehci_check_itd_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
    853 {
    854 	ehci_soft_itd_t *itd;
    855 	int i;
    856 
    857 	KASSERT(mutex_owned(&sc->sc_lock));
    858 
    859 	if (&ex->xfer != SIMPLEQ_FIRST(&ex->xfer.pipe->queue))
    860 		return;
    861 
    862 	if (ex->itdstart == NULL) {
    863 		printf("ehci_check_itd_intr: not valid itd\n");
    864 		return;
    865 	}
    866 
    867 	itd = ex->itdend;
    868 #ifdef DIAGNOSTIC
    869 	if (itd == NULL) {
    870 		printf("ehci_check_itd_intr: itdend == 0\n");
    871 		return;
    872 	}
    873 #endif
    874 
    875 	/*
    876 	 * check no active transfers in last itd, meaning we're finished
    877 	 */
    878 
    879 	usb_syncmem(&itd->dma, itd->offs + offsetof(ehci_itd_t, itd_ctl),
    880 		    sizeof(itd->itd.itd_ctl), BUS_DMASYNC_POSTWRITE |
    881 		    BUS_DMASYNC_POSTREAD);
    882 
    883 	for (i = 0; i < EHCI_ITD_NUFRAMES; i++) {
    884 		if (le32toh(itd->itd.itd_ctl[i]) & EHCI_ITD_ACTIVE)
    885 			break;
    886 	}
    887 
    888 	if (i == EHCI_ITD_NUFRAMES) {
    889 		goto done; /* All 8 descriptors inactive, it's done */
    890 	}
    891 
    892 	DPRINTFN(12, ("ehci_check_itd_intr: ex %p itd %p still active\n", ex,
    893 			ex->itdstart));
    894 	return;
    895 done:
    896 	DPRINTFN(12, ("ehci_check_itd_intr: ex=%p done\n", ex));
    897 	callout_stop(&ex->xfer.timeout_handle);
    898 	ehci_idone(ex);
    899 }
    900 
    901 Static void
    902 ehci_idone(struct ehci_xfer *ex)
    903 {
    904 	usbd_xfer_handle xfer = &ex->xfer;
    905 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
    906 	struct ehci_softc *sc = xfer->pipe->device->bus->hci_private;
    907 	ehci_soft_qtd_t *sqtd, *lsqtd;
    908 	u_int32_t status = 0, nstatus = 0;
    909 	int actlen;
    910 
    911 	KASSERT(mutex_owned(&sc->sc_lock));
    912 
    913 	DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex));
    914 
    915 #ifdef DIAGNOSTIC
    916 	{
    917 		if (ex->isdone) {
    918 #ifdef EHCI_DEBUG
    919 			printf("ehci_idone: ex is done!\n   ");
    920 			ehci_dump_exfer(ex);
    921 #else
    922 			printf("ehci_idone: ex=%p is done!\n", ex);
    923 #endif
    924 			return;
    925 		}
    926 		ex->isdone = 1;
    927 	}
    928 #endif
    929 	if (xfer->status == USBD_CANCELLED ||
    930 	    xfer->status == USBD_TIMEOUT) {
    931 		DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer));
    932 		return;
    933 	}
    934 
    935 #ifdef EHCI_DEBUG
    936 	DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe));
    937 	if (ehcidebug > 10)
    938 		ehci_dump_sqtds(ex->sqtdstart);
    939 #endif
    940 
    941 	/* The transfer is done, compute actual length and status. */
    942 
    943 	if (UE_GET_XFERTYPE(xfer->pipe->endpoint->edesc->bmAttributes)
    944 				== UE_ISOCHRONOUS) {
    945 		/* Isoc transfer */
    946 		struct ehci_soft_itd *itd;
    947 		int i, nframes, len, uframes;
    948 
    949 		nframes = 0;
    950 		actlen = 0;
    951 
    952 		i = xfer->pipe->endpoint->edesc->bInterval;
    953 		uframes = min(1 << (i - 1), USB_UFRAMES_PER_FRAME);
    954 
    955 		for (itd = ex->itdstart; itd != NULL; itd = itd->xfer_next) {
    956 			usb_syncmem(&itd->dma,itd->offs + offsetof(ehci_itd_t,itd_ctl),
    957 			    sizeof(itd->itd.itd_ctl), BUS_DMASYNC_POSTWRITE |
    958 			    BUS_DMASYNC_POSTREAD);
    959 
    960 			for (i = 0; i < EHCI_ITD_NUFRAMES; i += uframes) {
    961 				/* XXX - driver didn't fill in the frame full
    962 				 *   of uframes. This leads to scheduling
    963 				 *   inefficiencies, but working around
    964 				 *   this doubles complexity of tracking
    965 				 *   an xfer.
    966 				 */
    967 				if (nframes >= xfer->nframes)
    968 					break;
    969 
    970 				status = le32toh(itd->itd.itd_ctl[i]);
    971 				len = EHCI_ITD_GET_LEN(status);
    972 				if (EHCI_ITD_GET_STATUS(status) != 0)
    973 					len = 0; /*No valid data on error*/
    974 
    975 				xfer->frlengths[nframes++] = len;
    976 				actlen += len;
    977 			}
    978 
    979 			if (nframes >= xfer->nframes)
    980 				break;
    981 	    	}
    982 
    983 		xfer->actlen = actlen;
    984 		xfer->status = USBD_NORMAL_COMPLETION;
    985 		goto end;
    986 	}
    987 
    988 	/* Continue processing xfers using queue heads */
    989 
    990 	lsqtd = ex->sqtdend;
    991 	actlen = 0;
    992 	for (sqtd = ex->sqtdstart; sqtd != lsqtd->nextqtd; sqtd = sqtd->nextqtd) {
    993 		usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
    994 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    995 		nstatus = le32toh(sqtd->qtd.qtd_status);
    996 		if (nstatus & EHCI_QTD_ACTIVE)
    997 			break;
    998 
    999 		status = nstatus;
   1000 		if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
   1001 			actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
   1002 	}
   1003 
   1004 
   1005 	/*
   1006 	 * If there are left over TDs we need to update the toggle.
   1007 	 * The default pipe doesn't need it since control transfers
   1008 	 * start the toggle at 0 every time.
   1009 	 * For a short transfer we need to update the toggle for the missing
   1010 	 * packets within the qTD.
   1011 	 */
   1012 	if ((sqtd != lsqtd->nextqtd || EHCI_QTD_GET_BYTES(status)) &&
   1013 	    xfer->pipe->device->default_pipe != xfer->pipe) {
   1014 		DPRINTFN(2, ("ehci_idone: need toggle update "
   1015 			     "status=%08x nstatus=%08x\n", status, nstatus));
   1016 #if 0
   1017 		ehci_dump_sqh(epipe->sqh);
   1018 		ehci_dump_sqtds(ex->sqtdstart);
   1019 #endif
   1020 		epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus);
   1021 	}
   1022 
   1023 	DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, status=0x%x\n",
   1024 			   xfer->length, actlen, status));
   1025 	xfer->actlen = actlen;
   1026 	if (status & EHCI_QTD_HALTED) {
   1027 #ifdef EHCI_DEBUG
   1028 		char sbuf[128];
   1029 
   1030 		snprintb(sbuf, sizeof(sbuf),
   1031 		    "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR\3MISSED\1PINGSTATE",
   1032 		    (u_int32_t)status);
   1033 
   1034 		DPRINTFN(2, ("ehci_idone: error, addr=%d, endpt=0x%02x, "
   1035 			  "status 0x%s\n",
   1036 			  xfer->pipe->device->address,
   1037 			  xfer->pipe->endpoint->edesc->bEndpointAddress,
   1038 			  sbuf));
   1039 		if (ehcidebug > 2) {
   1040 			ehci_dump_sqh(epipe->sqh);
   1041 			ehci_dump_sqtds(ex->sqtdstart);
   1042 		}
   1043 #endif
   1044 		/* low&full speed has an extra error flag */
   1045 		if (EHCI_QH_GET_EPS(epipe->sqh->qh.qh_endp) !=
   1046 		    EHCI_QH_SPEED_HIGH)
   1047 			status &= EHCI_QTD_STATERRS | EHCI_QTD_PINGSTATE;
   1048 		else
   1049 			status &= EHCI_QTD_STATERRS;
   1050 		if (status == 0) /* no other errors means a stall */ {
   1051 			xfer->status = USBD_STALLED;
   1052 		} else {
   1053 			xfer->status = USBD_IOERROR; /* more info XXX */
   1054 		}
   1055 		/* XXX need to reset TT on missed microframe */
   1056 		if (status & EHCI_QTD_MISSEDMICRO) {
   1057 			printf("%s: missed microframe, TT reset not "
   1058 			    "implemented, hub might be inoperational\n",
   1059 			    device_xname(sc->sc_dev));
   1060 		}
   1061 	} else {
   1062 		xfer->status = USBD_NORMAL_COMPLETION;
   1063 	}
   1064 
   1065     end:
   1066 	/* XXX transfer_complete memcpys out transfer data (for in endpoints)
   1067 	 * during this call, before methods->done is called: dma sync required
   1068 	 * beforehand? */
   1069 	usb_transfer_complete(xfer);
   1070 	DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex));
   1071 }
   1072 
   1073 /*
   1074  * Wait here until controller claims to have an interrupt.
   1075  * Then call ehci_intr and return.  Use timeout to avoid waiting
   1076  * too long.
   1077  */
   1078 Static void
   1079 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
   1080 {
   1081 	int timo;
   1082 	u_int32_t intrs;
   1083 
   1084 	xfer->status = USBD_IN_PROGRESS;
   1085 	for (timo = xfer->timeout; timo >= 0; timo--) {
   1086 		usb_delay_ms(&sc->sc_bus, 1);
   1087 		if (sc->sc_dying)
   1088 			break;
   1089 		intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
   1090 			sc->sc_eintrs;
   1091 		DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs));
   1092 #ifdef EHCI_DEBUG
   1093 		if (ehcidebug > 15)
   1094 			ehci_dump_regs(sc);
   1095 #endif
   1096 		if (intrs) {
   1097 			mutex_spin_enter(&sc->sc_intr_lock);
   1098 			ehci_intr1(sc);
   1099 			mutex_spin_exit(&sc->sc_intr_lock);
   1100 			if (xfer->status != USBD_IN_PROGRESS)
   1101 				return;
   1102 		}
   1103 	}
   1104 
   1105 	/* Timeout */
   1106 	DPRINTF(("ehci_waitintr: timeout\n"));
   1107 	xfer->status = USBD_TIMEOUT;
   1108 	mutex_enter(&sc->sc_lock);
   1109 	usb_transfer_complete(xfer);
   1110 	mutex_exit(&sc->sc_lock);
   1111 	/* XXX should free TD */
   1112 }
   1113 
   1114 Static void
   1115 ehci_poll(struct usbd_bus *bus)
   1116 {
   1117 	ehci_softc_t *sc = bus->hci_private;
   1118 #ifdef EHCI_DEBUG
   1119 	static int last;
   1120 	int new;
   1121 	new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
   1122 	if (new != last) {
   1123 		DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
   1124 		last = new;
   1125 	}
   1126 #endif
   1127 
   1128 	if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs) {
   1129 		mutex_spin_enter(&sc->sc_intr_lock);
   1130 		ehci_intr1(sc);
   1131 		mutex_spin_exit(&sc->sc_intr_lock);
   1132 	}
   1133 }
   1134 
   1135 void
   1136 ehci_childdet(device_t self, device_t child)
   1137 {
   1138 	struct ehci_softc *sc = device_private(self);
   1139 
   1140 	KASSERT(sc->sc_child == child);
   1141 	sc->sc_child = NULL;
   1142 }
   1143 
   1144 int
   1145 ehci_detach(struct ehci_softc *sc, int flags)
   1146 {
   1147 	usbd_xfer_handle xfer;
   1148 	int rv = 0;
   1149 
   1150 	if (sc->sc_child != NULL)
   1151 		rv = config_detach(sc->sc_child, flags);
   1152 
   1153 	if (rv != 0)
   1154 		return (rv);
   1155 
   1156 	callout_halt(&sc->sc_tmo_intrlist, NULL);
   1157 	callout_destroy(&sc->sc_tmo_intrlist);
   1158 
   1159 	/* XXX free other data structures XXX */
   1160 	if (sc->sc_softitds)
   1161 		kmem_free(sc->sc_softitds,
   1162 		    sc->sc_flsize * sizeof(ehci_soft_itd_t *));
   1163 	cv_destroy(&sc->sc_doorbell);
   1164 	cv_destroy(&sc->sc_softwake_cv);
   1165 
   1166 #if 0
   1167 	/* XXX destroyed in ehci_pci.c as it controls ehci_intr access */
   1168 
   1169 	softint_disestablish(sc->sc_doorbell_si);
   1170 	softint_disestablish(sc->sc_pcd_si);
   1171 
   1172 	mutex_destroy(&sc->sc_lock);
   1173 	mutex_destroy(&sc->sc_intr_lock);
   1174 #endif
   1175 
   1176 	while ((xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers)) != NULL) {
   1177 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
   1178 		kmem_free(xfer, sizeof(struct ehci_xfer));
   1179 	}
   1180 
   1181 	EOWRITE4(sc, EHCI_CONFIGFLAG, 0);
   1182 
   1183 	return (rv);
   1184 }
   1185 
   1186 
   1187 int
   1188 ehci_activate(device_t self, enum devact act)
   1189 {
   1190 	struct ehci_softc *sc = device_private(self);
   1191 
   1192 	switch (act) {
   1193 	case DVACT_DEACTIVATE:
   1194 		sc->sc_dying = 1;
   1195 		return 0;
   1196 	default:
   1197 		return EOPNOTSUPP;
   1198 	}
   1199 }
   1200 
   1201 /*
   1202  * Handle suspend/resume.
   1203  *
   1204  * We need to switch to polling mode here, because this routine is
   1205  * called from an interrupt context.  This is all right since we
   1206  * are almost suspended anyway.
   1207  *
   1208  * Note that this power handler isn't to be registered directly; the
   1209  * bus glue needs to call out to it.
   1210  */
   1211 bool
   1212 ehci_suspend(device_t dv, const pmf_qual_t *qual)
   1213 {
   1214 	ehci_softc_t *sc = device_private(dv);
   1215 	int i;
   1216 	uint32_t cmd, hcr;
   1217 
   1218 	mutex_spin_enter(&sc->sc_intr_lock);
   1219 	sc->sc_bus.use_polling++;
   1220 	mutex_spin_exit(&sc->sc_intr_lock);
   1221 
   1222 	for (i = 1; i <= sc->sc_noport; i++) {
   1223 		cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
   1224 		if ((cmd & EHCI_PS_PO) == 0 && (cmd & EHCI_PS_PE) == EHCI_PS_PE)
   1225 			EOWRITE4(sc, EHCI_PORTSC(i), cmd | EHCI_PS_SUSP);
   1226 	}
   1227 
   1228 	sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
   1229 
   1230 	cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
   1231 	EOWRITE4(sc, EHCI_USBCMD, cmd);
   1232 
   1233 	for (i = 0; i < 100; i++) {
   1234 		hcr = EOREAD4(sc, EHCI_USBSTS) & (EHCI_STS_ASS | EHCI_STS_PSS);
   1235 		if (hcr == 0)
   1236 			break;
   1237 
   1238 		usb_delay_ms(&sc->sc_bus, 1);
   1239 	}
   1240 	if (hcr != 0)
   1241 		printf("%s: reset timeout\n", device_xname(dv));
   1242 
   1243 	cmd &= ~EHCI_CMD_RS;
   1244 	EOWRITE4(sc, EHCI_USBCMD, cmd);
   1245 
   1246 	for (i = 0; i < 100; i++) {
   1247 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
   1248 		if (hcr == EHCI_STS_HCH)
   1249 			break;
   1250 
   1251 		usb_delay_ms(&sc->sc_bus, 1);
   1252 	}
   1253 	if (hcr != EHCI_STS_HCH)
   1254 		printf("%s: config timeout\n", device_xname(dv));
   1255 
   1256 	mutex_spin_enter(&sc->sc_intr_lock);
   1257 	sc->sc_bus.use_polling--;
   1258 	mutex_spin_exit(&sc->sc_intr_lock);
   1259 
   1260 	return true;
   1261 }
   1262 
   1263 bool
   1264 ehci_resume(device_t dv, const pmf_qual_t *qual)
   1265 {
   1266 	ehci_softc_t *sc = device_private(dv);
   1267 	int i;
   1268 	uint32_t cmd, hcr;
   1269 
   1270 	/* restore things in case the bios sucks */
   1271 	EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
   1272 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
   1273 	EOWRITE4(sc, EHCI_ASYNCLISTADDR,
   1274 	    sc->sc_async_head->physaddr | EHCI_LINK_QH);
   1275 
   1276 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs & ~EHCI_INTR_PCIE);
   1277 
   1278 	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
   1279 
   1280 	hcr = 0;
   1281 	for (i = 1; i <= sc->sc_noport; i++) {
   1282 		cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
   1283 		if ((cmd & EHCI_PS_PO) == 0 &&
   1284 		    (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP) {
   1285 			EOWRITE4(sc, EHCI_PORTSC(i), cmd | EHCI_PS_FPR);
   1286 			hcr = 1;
   1287 		}
   1288 	}
   1289 
   1290 	if (hcr) {
   1291 		usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
   1292 
   1293 		for (i = 1; i <= sc->sc_noport; i++) {
   1294 			cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
   1295 			if ((cmd & EHCI_PS_PO) == 0 &&
   1296 			    (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)
   1297 				EOWRITE4(sc, EHCI_PORTSC(i),
   1298 				    cmd & ~EHCI_PS_FPR);
   1299 		}
   1300 	}
   1301 
   1302 	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
   1303 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
   1304 
   1305 	for (i = 0; i < 100; i++) {
   1306 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
   1307 		if (hcr != EHCI_STS_HCH)
   1308 			break;
   1309 
   1310 		usb_delay_ms(&sc->sc_bus, 1);
   1311 	}
   1312 	if (hcr == EHCI_STS_HCH)
   1313 		printf("%s: config timeout\n", device_xname(dv));
   1314 
   1315 	return true;
   1316 }
   1317 
   1318 /*
   1319  * Shut down the controller when the system is going down.
   1320  */
   1321 bool
   1322 ehci_shutdown(device_t self, int flags)
   1323 {
   1324 	ehci_softc_t *sc = device_private(self);
   1325 
   1326 	DPRINTF(("ehci_shutdown: stopping the HC\n"));
   1327 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
   1328 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
   1329 	return true;
   1330 }
   1331 
   1332 Static usbd_status
   1333 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
   1334 {
   1335 	struct ehci_softc *sc = bus->hci_private;
   1336 	usbd_status err;
   1337 
   1338 	err = usb_allocmem_flags(&sc->sc_bus, size, 0, dma, USBMALLOC_MULTISEG);
   1339 #ifdef EHCI_DEBUG
   1340 	if (err)
   1341 		printf("ehci_allocm: usb_allocmem_flags()= %s (%d)\n",
   1342 			usbd_errstr(err), err);
   1343 #endif
   1344 	if (err == USBD_NOMEM)
   1345 		err = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size);
   1346 #ifdef EHCI_DEBUG
   1347 	if (err)
   1348 		printf("ehci_allocm: usb_reserve_allocm()= %s (%d)\n",
   1349 			usbd_errstr(err), err);
   1350 #endif
   1351 	return (err);
   1352 }
   1353 
   1354 Static void
   1355 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
   1356 {
   1357 	struct ehci_softc *sc = bus->hci_private;
   1358 
   1359 	if (dma->block->flags & USB_DMA_RESERVE) {
   1360 		usb_reserve_freem(&sc->sc_dma_reserve,
   1361 		    dma);
   1362 		return;
   1363 	}
   1364 	usb_freemem(&sc->sc_bus, dma);
   1365 }
   1366 
   1367 Static usbd_xfer_handle
   1368 ehci_allocx(struct usbd_bus *bus)
   1369 {
   1370 	struct ehci_softc *sc = bus->hci_private;
   1371 	usbd_xfer_handle xfer;
   1372 
   1373 	xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
   1374 	if (xfer != NULL) {
   1375 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
   1376 #ifdef DIAGNOSTIC
   1377 		if (xfer->busy_free != XFER_FREE) {
   1378 			printf("ehci_allocx: xfer=%p not free, 0x%08x\n", xfer,
   1379 			       xfer->busy_free);
   1380 		}
   1381 #endif
   1382 	} else {
   1383 		xfer = kmem_alloc(sizeof(struct ehci_xfer), KM_SLEEP);
   1384 	}
   1385 	if (xfer != NULL) {
   1386 		memset(xfer, 0, sizeof(struct ehci_xfer));
   1387 #ifdef DIAGNOSTIC
   1388 		EXFER(xfer)->isdone = 1;
   1389 		xfer->busy_free = XFER_BUSY;
   1390 #endif
   1391 	}
   1392 	return (xfer);
   1393 }
   1394 
   1395 Static void
   1396 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
   1397 {
   1398 	struct ehci_softc *sc = bus->hci_private;
   1399 
   1400 #ifdef DIAGNOSTIC
   1401 	if (xfer->busy_free != XFER_BUSY) {
   1402 		printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
   1403 		       xfer->busy_free);
   1404 	}
   1405 	xfer->busy_free = XFER_FREE;
   1406 	if (!EXFER(xfer)->isdone) {
   1407 		printf("ehci_freex: !isdone\n");
   1408 	}
   1409 #endif
   1410 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
   1411 }
   1412 
   1413 Static void
   1414 ehci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
   1415 {
   1416 	struct ehci_softc *sc = bus->hci_private;
   1417 
   1418 	*lock = &sc->sc_lock;
   1419 }
   1420 
   1421 Static void
   1422 ehci_device_clear_toggle(usbd_pipe_handle pipe)
   1423 {
   1424 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   1425 
   1426 	DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
   1427 		 epipe, epipe->sqh->qh.qh_qtd.qtd_status));
   1428 #ifdef EHCI_DEBUG
   1429 	if (ehcidebug)
   1430 		usbd_dump_pipe(pipe);
   1431 #endif
   1432 	epipe->nexttoggle = 0;
   1433 }
   1434 
   1435 Static void
   1436 ehci_noop(usbd_pipe_handle pipe)
   1437 {
   1438 }
   1439 
   1440 #ifdef EHCI_DEBUG
   1441 Static void
   1442 ehci_dump_regs(ehci_softc_t *sc)
   1443 {
   1444 	int i;
   1445 	printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
   1446 	       EOREAD4(sc, EHCI_USBCMD),
   1447 	       EOREAD4(sc, EHCI_USBSTS),
   1448 	       EOREAD4(sc, EHCI_USBINTR));
   1449 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
   1450 	       EOREAD4(sc, EHCI_FRINDEX),
   1451 	       EOREAD4(sc, EHCI_CTRLDSSEGMENT),
   1452 	       EOREAD4(sc, EHCI_PERIODICLISTBASE),
   1453 	       EOREAD4(sc, EHCI_ASYNCLISTADDR));
   1454 	for (i = 1; i <= sc->sc_noport; i++)
   1455 		printf("port %d status=0x%08x\n", i,
   1456 		       EOREAD4(sc, EHCI_PORTSC(i)));
   1457 }
   1458 
   1459 /*
   1460  * Unused function - this is meant to be called from a kernel
   1461  * debugger.
   1462  */
   1463 void
   1464 ehci_dump(void)
   1465 {
   1466 	ehci_dump_regs(theehci);
   1467 }
   1468 
   1469 Static void
   1470 ehci_dump_link(ehci_link_t link, int type)
   1471 {
   1472 	link = le32toh(link);
   1473 	printf("0x%08x", link);
   1474 	if (link & EHCI_LINK_TERMINATE)
   1475 		printf("<T>");
   1476 	else {
   1477 		printf("<");
   1478 		if (type) {
   1479 			switch (EHCI_LINK_TYPE(link)) {
   1480 			case EHCI_LINK_ITD: printf("ITD"); break;
   1481 			case EHCI_LINK_QH: printf("QH"); break;
   1482 			case EHCI_LINK_SITD: printf("SITD"); break;
   1483 			case EHCI_LINK_FSTN: printf("FSTN"); break;
   1484 			}
   1485 		}
   1486 		printf(">");
   1487 	}
   1488 }
   1489 
   1490 Static void
   1491 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
   1492 {
   1493 	int i;
   1494 	u_int32_t stop;
   1495 
   1496 	stop = 0;
   1497 	for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
   1498 		ehci_dump_sqtd(sqtd);
   1499 		usb_syncmem(&sqtd->dma,
   1500 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_next),
   1501 		    sizeof(sqtd->qtd),
   1502 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1503 		stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE);
   1504 		usb_syncmem(&sqtd->dma,
   1505 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_next),
   1506 		    sizeof(sqtd->qtd), BUS_DMASYNC_PREREAD);
   1507 	}
   1508 	if (sqtd)
   1509 		printf("dump aborted, too many TDs\n");
   1510 }
   1511 
   1512 Static void
   1513 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
   1514 {
   1515 	usb_syncmem(&sqtd->dma, sqtd->offs,
   1516 	    sizeof(sqtd->qtd), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1517 	printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr);
   1518 	ehci_dump_qtd(&sqtd->qtd);
   1519 	usb_syncmem(&sqtd->dma, sqtd->offs,
   1520 	    sizeof(sqtd->qtd), BUS_DMASYNC_PREREAD);
   1521 }
   1522 
   1523 Static void
   1524 ehci_dump_qtd(ehci_qtd_t *qtd)
   1525 {
   1526 	u_int32_t s;
   1527 	char sbuf[128];
   1528 
   1529 	printf("  next="); ehci_dump_link(qtd->qtd_next, 0);
   1530 	printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0);
   1531 	printf("\n");
   1532 	s = le32toh(qtd->qtd_status);
   1533 	snprintb(sbuf, sizeof(sbuf),
   1534 	    "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
   1535 	    "\3MISSED\2SPLIT\1PING", EHCI_QTD_GET_STATUS(s));
   1536 	printf("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
   1537 	       s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
   1538 	       EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
   1539 	printf("    cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s),
   1540 	       EHCI_QTD_GET_PID(s), sbuf);
   1541 	for (s = 0; s < 5; s++)
   1542 		printf("  buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s]));
   1543 }
   1544 
   1545 Static void
   1546 ehci_dump_sqh(ehci_soft_qh_t *sqh)
   1547 {
   1548 	ehci_qh_t *qh = &sqh->qh;
   1549 	u_int32_t endp, endphub;
   1550 
   1551 	usb_syncmem(&sqh->dma, sqh->offs,
   1552 	    sizeof(sqh->qh), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1553 	printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr);
   1554 	printf("  link="); ehci_dump_link(qh->qh_link, 1); printf("\n");
   1555 	endp = le32toh(qh->qh_endp);
   1556 	printf("  endp=0x%08x\n", endp);
   1557 	printf("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
   1558 	       EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
   1559 	       EHCI_QH_GET_ENDPT(endp),  EHCI_QH_GET_EPS(endp),
   1560 	       EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
   1561 	printf("    mpl=0x%x ctl=%d nrl=%d\n",
   1562 	       EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
   1563 	       EHCI_QH_GET_NRL(endp));
   1564 	endphub = le32toh(qh->qh_endphub);
   1565 	printf("  endphub=0x%08x\n", endphub);
   1566 	printf("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
   1567 	       EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
   1568 	       EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
   1569 	       EHCI_QH_GET_MULT(endphub));
   1570 	printf("  curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n");
   1571 	printf("Overlay qTD:\n");
   1572 	ehci_dump_qtd(&qh->qh_qtd);
   1573 	usb_syncmem(&sqh->dma, sqh->offs,
   1574 	    sizeof(sqh->qh), BUS_DMASYNC_PREREAD);
   1575 }
   1576 
   1577 #if notyet
   1578 Static void
   1579 ehci_dump_itd(struct ehci_soft_itd *itd)
   1580 {
   1581 	ehci_isoc_trans_t t;
   1582 	ehci_isoc_bufr_ptr_t b, b2, b3;
   1583 	int i;
   1584 
   1585 	printf("ITD: next phys=%X\n", itd->itd.itd_next);
   1586 
   1587 	for (i = 0; i < EHCI_ITD_NUFRAMES; i++) {
   1588 		t = le32toh(itd->itd.itd_ctl[i]);
   1589 		printf("ITDctl %d: stat=%X len=%X ioc=%X pg=%X offs=%X\n", i,
   1590 		    EHCI_ITD_GET_STATUS(t), EHCI_ITD_GET_LEN(t),
   1591 		    EHCI_ITD_GET_IOC(t), EHCI_ITD_GET_PG(t),
   1592 		    EHCI_ITD_GET_OFFS(t));
   1593 	}
   1594 	printf("ITDbufr: ");
   1595 	for (i = 0; i < EHCI_ITD_NBUFFERS; i++)
   1596 		printf("%X,", EHCI_ITD_GET_BPTR(le32toh(itd->itd.itd_bufr[i])));
   1597 
   1598 	b = le32toh(itd->itd.itd_bufr[0]);
   1599 	b2 = le32toh(itd->itd.itd_bufr[1]);
   1600 	b3 = le32toh(itd->itd.itd_bufr[2]);
   1601 	printf("\nep=%X daddr=%X dir=%d maxpkt=%X multi=%X\n",
   1602 	    EHCI_ITD_GET_EP(b), EHCI_ITD_GET_DADDR(b), EHCI_ITD_GET_DIR(b2),
   1603 	    EHCI_ITD_GET_MAXPKT(b2), EHCI_ITD_GET_MULTI(b3));
   1604 }
   1605 
   1606 Static void
   1607 ehci_dump_sitd(struct ehci_soft_itd *itd)
   1608 {
   1609 	printf("SITD %p next=%p prev=%p xfernext=%p physaddr=%X slot=%d\n",
   1610 			itd, itd->u.frame_list.next, itd->u.frame_list.prev,
   1611 			itd->xfer_next, itd->physaddr, itd->slot);
   1612 }
   1613 #endif
   1614 
   1615 #ifdef DIAGNOSTIC
   1616 Static void
   1617 ehci_dump_exfer(struct ehci_xfer *ex)
   1618 {
   1619 	printf("ehci_dump_exfer: ex=%p sqtdstart=%p end=%p itdstart=%p end=%p isdone=%d\n", ex, ex->sqtdstart, ex->sqtdend, ex->itdstart, ex->itdend, ex->isdone);
   1620 }
   1621 #endif
   1622 #endif
   1623 
   1624 Static usbd_status
   1625 ehci_open(usbd_pipe_handle pipe)
   1626 {
   1627 	usbd_device_handle dev = pipe->device;
   1628 	ehci_softc_t *sc = dev->bus->hci_private;
   1629 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
   1630 	u_int8_t addr = dev->address;
   1631 	u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
   1632 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   1633 	ehci_soft_qh_t *sqh;
   1634 	usbd_status err;
   1635 	int ival, speed, naks;
   1636 	int hshubaddr, hshubport;
   1637 
   1638 	DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
   1639 		     pipe, addr, ed->bEndpointAddress, sc->sc_addr));
   1640 
   1641 	if (dev->myhsport) {
   1642 		/*
   1643 		 * When directly attached FS/LS device while doing embedded
   1644 		 * transaction translations and we are the hub, set the hub
   1645 		 * address to 0 (us).
   1646 		 */
   1647 		if (!(sc->sc_flags & EHCIF_ETTF)
   1648 		    || (dev->myhsport->parent->address != sc->sc_addr)) {
   1649 			hshubaddr = dev->myhsport->parent->address;
   1650 		} else {
   1651 			hshubaddr = 0;
   1652 		}
   1653 		hshubport = dev->myhsport->portno;
   1654 	} else {
   1655 		hshubaddr = 0;
   1656 		hshubport = 0;
   1657 	}
   1658 
   1659 	if (sc->sc_dying)
   1660 		return (USBD_IOERROR);
   1661 
   1662 	/* toggle state needed for bulk endpoints */
   1663 	epipe->nexttoggle = pipe->endpoint->datatoggle;
   1664 
   1665 	if (addr == sc->sc_addr) {
   1666 		switch (ed->bEndpointAddress) {
   1667 		case USB_CONTROL_ENDPOINT:
   1668 			pipe->methods = &ehci_root_ctrl_methods;
   1669 			break;
   1670 		case UE_DIR_IN | EHCI_INTR_ENDPT:
   1671 			pipe->methods = &ehci_root_intr_methods;
   1672 			break;
   1673 		default:
   1674 			DPRINTF(("ehci_open: bad bEndpointAddress 0x%02x\n",
   1675 			    ed->bEndpointAddress));
   1676 			return (USBD_INVAL);
   1677 		}
   1678 		return (USBD_NORMAL_COMPLETION);
   1679 	}
   1680 
   1681 	/* XXX All this stuff is only valid for async. */
   1682 	switch (dev->speed) {
   1683 	case USB_SPEED_LOW:  speed = EHCI_QH_SPEED_LOW;  break;
   1684 	case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
   1685 	case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
   1686 	default: panic("ehci_open: bad device speed %d", dev->speed);
   1687 	}
   1688 	if (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_ISOCHRONOUS) {
   1689 		aprint_error_dev(sc->sc_dev, "error opening low/full speed "
   1690 		    "isoc endpoint.\n");
   1691 		aprint_normal_dev(sc->sc_dev, "a low/full speed device is "
   1692 		    "attached to a USB2 hub, and transaction translations are "
   1693 		    "not yet supported.\n");
   1694 		aprint_normal_dev(sc->sc_dev, "reattach the device to the "
   1695 		    "root hub instead.\n");
   1696 		DPRINTFN(1,("ehci_open: hshubaddr=%d hshubport=%d\n",
   1697 			    hshubaddr, hshubport));
   1698 		return USBD_INVAL;
   1699 	}
   1700 
   1701 	/*
   1702 	 * For interrupt transfer, nak throttling must be disabled, but for
   1703 	 * the other transfer type, nak throttling should be enabled from the
   1704 	 * viewpoint that avoids the memory thrashing.
   1705 	 */
   1706 	naks = (xfertype == UE_INTERRUPT) ? 0
   1707 	    : ((speed == EHCI_QH_SPEED_HIGH) ? 4 : 0);
   1708 
   1709 	/* Allocate sqh for everything, save isoc xfers */
   1710 	if (xfertype != UE_ISOCHRONOUS) {
   1711 		sqh = ehci_alloc_sqh(sc);
   1712 		if (sqh == NULL)
   1713 			return (USBD_NOMEM);
   1714 		/* qh_link filled when the QH is added */
   1715 		sqh->qh.qh_endp = htole32(
   1716 		    EHCI_QH_SET_ADDR(addr) |
   1717 		    EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
   1718 		    EHCI_QH_SET_EPS(speed) |
   1719 		    EHCI_QH_DTC |
   1720 		    EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
   1721 		    (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
   1722 		     EHCI_QH_CTL : 0) |
   1723 		    EHCI_QH_SET_NRL(naks)
   1724 		    );
   1725 		sqh->qh.qh_endphub = htole32(
   1726 		    EHCI_QH_SET_MULT(1) |
   1727 		    EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x02 : 0)
   1728 		    );
   1729 		if (speed != EHCI_QH_SPEED_HIGH)
   1730 			sqh->qh.qh_endphub |= htole32(
   1731 			    EHCI_QH_SET_PORT(hshubport) |
   1732 			    EHCI_QH_SET_HUBA(hshubaddr) |
   1733 			    EHCI_QH_SET_CMASK(0x08) /* XXX */
   1734 			);
   1735 		sqh->qh.qh_curqtd = EHCI_NULL;
   1736 		/* Fill the overlay qTD */
   1737 		sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
   1738 		sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
   1739 		sqh->qh.qh_qtd.qtd_status = htole32(0);
   1740 
   1741 		usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
   1742 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1743 		epipe->sqh = sqh;
   1744 	} else {
   1745 		sqh = NULL;
   1746 	} /*xfertype == UE_ISOC*/
   1747 
   1748 	switch (xfertype) {
   1749 	case UE_CONTROL:
   1750 		err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
   1751 				   0, &epipe->u.ctl.reqdma);
   1752 #ifdef EHCI_DEBUG
   1753 		if (err)
   1754 			printf("ehci_open: usb_allocmem()=%d\n", err);
   1755 #endif
   1756 		if (err)
   1757 			goto bad;
   1758 		pipe->methods = &ehci_device_ctrl_methods;
   1759 		mutex_enter(&sc->sc_lock);
   1760 		ehci_add_qh(sc, sqh, sc->sc_async_head);
   1761 		mutex_exit(&sc->sc_lock);
   1762 		break;
   1763 	case UE_BULK:
   1764 		pipe->methods = &ehci_device_bulk_methods;
   1765 		mutex_enter(&sc->sc_lock);
   1766 		ehci_add_qh(sc, sqh, sc->sc_async_head);
   1767 		mutex_exit(&sc->sc_lock);
   1768 		break;
   1769 	case UE_INTERRUPT:
   1770 		pipe->methods = &ehci_device_intr_methods;
   1771 		ival = pipe->interval;
   1772 		if (ival == USBD_DEFAULT_INTERVAL) {
   1773 			if (speed == EHCI_QH_SPEED_HIGH) {
   1774 				if (ed->bInterval > 16) {
   1775 					/*
   1776 					 * illegal with high-speed, but there
   1777 					 * were documentation bugs in the spec,
   1778 					 * so be generous
   1779 					 */
   1780 					ival = 256;
   1781 				} else
   1782 					ival = (1 << (ed->bInterval - 1)) / 8;
   1783 			} else
   1784 				ival = ed->bInterval;
   1785 		}
   1786 		err = ehci_device_setintr(sc, sqh, ival);
   1787 		if (err)
   1788 			goto bad;
   1789 		break;
   1790 	case UE_ISOCHRONOUS:
   1791 		pipe->methods = &ehci_device_isoc_methods;
   1792 		if (ed->bInterval == 0 || ed->bInterval > 16) {
   1793 			printf("ehci: opening pipe with invalid bInterval\n");
   1794 			err = USBD_INVAL;
   1795 			goto bad;
   1796 		}
   1797 		if (UGETW(ed->wMaxPacketSize) == 0) {
   1798 			printf("ehci: zero length endpoint open request\n");
   1799 			err = USBD_INVAL;
   1800 			goto bad;
   1801 		}
   1802 		epipe->u.isoc.next_frame = 0;
   1803 		epipe->u.isoc.cur_xfers = 0;
   1804 		break;
   1805 	default:
   1806 		DPRINTF(("ehci: bad xfer type %d\n", xfertype));
   1807 		err = USBD_INVAL;
   1808 		goto bad;
   1809 	}
   1810 	return (USBD_NORMAL_COMPLETION);
   1811 
   1812  bad:
   1813 	if (sqh != NULL)
   1814 		ehci_free_sqh(sc, sqh);
   1815 	return (err);
   1816 }
   1817 
   1818 /*
   1819  * Add an ED to the schedule.  Called with USB lock held.
   1820  */
   1821 Static void
   1822 ehci_add_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
   1823 {
   1824 
   1825 	KASSERT(mutex_owned(&sc->sc_lock));
   1826 
   1827 	usb_syncmem(&head->dma, head->offs + offsetof(ehci_qh_t, qh_link),
   1828 	    sizeof(head->qh.qh_link), BUS_DMASYNC_POSTWRITE);
   1829 	sqh->next = head->next;
   1830 	sqh->qh.qh_link = head->qh.qh_link;
   1831 	usb_syncmem(&sqh->dma, sqh->offs + offsetof(ehci_qh_t, qh_link),
   1832 	    sizeof(sqh->qh.qh_link), BUS_DMASYNC_PREWRITE);
   1833 	head->next = sqh;
   1834 	head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
   1835 	usb_syncmem(&head->dma, head->offs + offsetof(ehci_qh_t, qh_link),
   1836 	    sizeof(head->qh.qh_link), BUS_DMASYNC_PREWRITE);
   1837 
   1838 #ifdef EHCI_DEBUG
   1839 	if (ehcidebug > 5) {
   1840 		printf("ehci_add_qh:\n");
   1841 		ehci_dump_sqh(sqh);
   1842 	}
   1843 #endif
   1844 }
   1845 
   1846 /*
   1847  * Remove an ED from the schedule.  Called with USB lock held.
   1848  */
   1849 Static void
   1850 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
   1851 {
   1852 	ehci_soft_qh_t *p;
   1853 
   1854 	KASSERT(mutex_owned(&sc->sc_lock));
   1855 
   1856 	/* XXX */
   1857 	for (p = head; p != NULL && p->next != sqh; p = p->next)
   1858 		;
   1859 	if (p == NULL)
   1860 		panic("ehci_rem_qh: ED not found");
   1861 	usb_syncmem(&sqh->dma, sqh->offs + offsetof(ehci_qh_t, qh_link),
   1862 	    sizeof(sqh->qh.qh_link), BUS_DMASYNC_POSTWRITE);
   1863 	p->next = sqh->next;
   1864 	p->qh.qh_link = sqh->qh.qh_link;
   1865 	usb_syncmem(&p->dma, p->offs + offsetof(ehci_qh_t, qh_link),
   1866 	    sizeof(p->qh.qh_link), BUS_DMASYNC_PREWRITE);
   1867 
   1868 	ehci_sync_hc(sc);
   1869 }
   1870 
   1871 Static void
   1872 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
   1873 {
   1874 	int i;
   1875 	u_int32_t status;
   1876 
   1877 	/* Save toggle bit and ping status. */
   1878 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
   1879 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1880 	status = sqh->qh.qh_qtd.qtd_status &
   1881 	    htole32(EHCI_QTD_TOGGLE_MASK |
   1882 		    EHCI_QTD_SET_STATUS(EHCI_QTD_PINGSTATE));
   1883 	/* Set HALTED to make hw leave it alone. */
   1884 	sqh->qh.qh_qtd.qtd_status =
   1885 	    htole32(EHCI_QTD_SET_STATUS(EHCI_QTD_HALTED));
   1886 	usb_syncmem(&sqh->dma,
   1887 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   1888 	    sizeof(sqh->qh.qh_qtd.qtd_status),
   1889 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1890 	sqh->qh.qh_curqtd = 0;
   1891 	sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
   1892 	sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
   1893 	for (i = 0; i < EHCI_QTD_NBUFFERS; i++)
   1894 		sqh->qh.qh_qtd.qtd_buffer[i] = 0;
   1895 	sqh->sqtd = sqtd;
   1896 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
   1897 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1898 	/* Set !HALTED && !ACTIVE to start execution, preserve some fields */
   1899 	sqh->qh.qh_qtd.qtd_status = status;
   1900 	usb_syncmem(&sqh->dma,
   1901 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   1902 	    sizeof(sqh->qh.qh_qtd.qtd_status),
   1903 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1904 }
   1905 
   1906 /*
   1907  * Ensure that the HC has released all references to the QH.  We do this
   1908  * by asking for a Async Advance Doorbell interrupt and then we wait for
   1909  * the interrupt.
   1910  * To make this easier we first obtain exclusive use of the doorbell.
   1911  */
   1912 Static void
   1913 ehci_sync_hc(ehci_softc_t *sc)
   1914 {
   1915 	int error;
   1916 
   1917 	KASSERT(mutex_owned(&sc->sc_lock));
   1918 
   1919 	if (sc->sc_dying) {
   1920 		DPRINTFN(2,("ehci_sync_hc: dying\n"));
   1921 		return;
   1922 	}
   1923 	DPRINTFN(2,("ehci_sync_hc: enter\n"));
   1924 	/* ask for doorbell */
   1925 	EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
   1926 	DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
   1927 		    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
   1928 	error = cv_timedwait(&sc->sc_doorbell, &sc->sc_lock, hz); /* bell wait */
   1929 	DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
   1930 		    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
   1931 #ifdef DIAGNOSTIC
   1932 	if (error)
   1933 		printf("ehci_sync_hc: cv_timedwait() = %d\n", error);
   1934 #endif
   1935 	DPRINTFN(2,("ehci_sync_hc: exit\n"));
   1936 }
   1937 
   1938 Static void
   1939 ehci_rem_free_itd_chain(ehci_softc_t *sc, struct ehci_xfer *exfer)
   1940 {
   1941 	struct ehci_soft_itd *itd, *prev;
   1942 
   1943 	prev = NULL;
   1944 
   1945 	if (exfer->itdstart == NULL || exfer->itdend == NULL)
   1946 		panic("ehci isoc xfer being freed, but with no itd chain\n");
   1947 
   1948 	for (itd = exfer->itdstart; itd != NULL; itd = itd->xfer_next) {
   1949 		prev = itd->u.frame_list.prev;
   1950 		/* Unlink itd from hardware chain, or frame array */
   1951 		if (prev == NULL) { /* We're at the table head */
   1952 			sc->sc_softitds[itd->slot] = itd->u.frame_list.next;
   1953 			sc->sc_flist[itd->slot] = itd->itd.itd_next;
   1954 			usb_syncmem(&sc->sc_fldma,
   1955 			    sizeof(ehci_link_t) * itd->slot,
   1956                 	    sizeof(ehci_link_t),
   1957 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1958 
   1959 			if (itd->u.frame_list.next != NULL)
   1960 				itd->u.frame_list.next->u.frame_list.prev = NULL;
   1961 		} else {
   1962 			/* XXX this part is untested... */
   1963 			prev->itd.itd_next = itd->itd.itd_next;
   1964 			usb_syncmem(&itd->dma,
   1965 			    itd->offs + offsetof(ehci_itd_t, itd_next),
   1966                 	    sizeof(itd->itd.itd_next), BUS_DMASYNC_PREWRITE);
   1967 
   1968 			prev->u.frame_list.next = itd->u.frame_list.next;
   1969 			if (itd->u.frame_list.next != NULL)
   1970 				itd->u.frame_list.next->u.frame_list.prev = prev;
   1971 		}
   1972 	}
   1973 
   1974 	prev = NULL;
   1975 	for (itd = exfer->itdstart; itd != NULL; itd = itd->xfer_next) {
   1976 		if (prev != NULL)
   1977 			ehci_free_itd(sc, prev);
   1978 		prev = itd;
   1979 	}
   1980 	if (prev)
   1981 		ehci_free_itd(sc, prev);
   1982 	exfer->itdstart = NULL;
   1983 	exfer->itdend = NULL;
   1984 }
   1985 
   1986 /***********/
   1987 
   1988 /*
   1989  * Data structures and routines to emulate the root hub.
   1990  */
   1991 Static usb_device_descriptor_t ehci_devd = {
   1992 	USB_DEVICE_DESCRIPTOR_SIZE,
   1993 	UDESC_DEVICE,		/* type */
   1994 	{0x00, 0x02},		/* USB version */
   1995 	UDCLASS_HUB,		/* class */
   1996 	UDSUBCLASS_HUB,		/* subclass */
   1997 	UDPROTO_HSHUBSTT,	/* protocol */
   1998 	64,			/* max packet */
   1999 	{0},{0},{0x00,0x01},	/* device id */
   2000 	1,2,0,			/* string indicies */
   2001 	1			/* # of configurations */
   2002 };
   2003 
   2004 Static const usb_device_qualifier_t ehci_odevd = {
   2005 	USB_DEVICE_DESCRIPTOR_SIZE,
   2006 	UDESC_DEVICE_QUALIFIER,	/* type */
   2007 	{0x00, 0x02},		/* USB version */
   2008 	UDCLASS_HUB,		/* class */
   2009 	UDSUBCLASS_HUB,		/* subclass */
   2010 	UDPROTO_FSHUB,		/* protocol */
   2011 	64,			/* max packet */
   2012 	1,			/* # of configurations */
   2013 	0
   2014 };
   2015 
   2016 Static const usb_config_descriptor_t ehci_confd = {
   2017 	USB_CONFIG_DESCRIPTOR_SIZE,
   2018 	UDESC_CONFIG,
   2019 	{USB_CONFIG_DESCRIPTOR_SIZE +
   2020 	 USB_INTERFACE_DESCRIPTOR_SIZE +
   2021 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
   2022 	1,
   2023 	1,
   2024 	0,
   2025 	UC_ATTR_MBO | UC_SELF_POWERED,
   2026 	0			/* max power */
   2027 };
   2028 
   2029 Static const usb_interface_descriptor_t ehci_ifcd = {
   2030 	USB_INTERFACE_DESCRIPTOR_SIZE,
   2031 	UDESC_INTERFACE,
   2032 	0,
   2033 	0,
   2034 	1,
   2035 	UICLASS_HUB,
   2036 	UISUBCLASS_HUB,
   2037 	UIPROTO_HSHUBSTT,
   2038 	0
   2039 };
   2040 
   2041 Static const usb_endpoint_descriptor_t ehci_endpd = {
   2042 	USB_ENDPOINT_DESCRIPTOR_SIZE,
   2043 	UDESC_ENDPOINT,
   2044 	UE_DIR_IN | EHCI_INTR_ENDPT,
   2045 	UE_INTERRUPT,
   2046 	{8, 0},			/* max packet */
   2047 	12
   2048 };
   2049 
   2050 Static const usb_hub_descriptor_t ehci_hubd = {
   2051 	USB_HUB_DESCRIPTOR_SIZE,
   2052 	UDESC_HUB,
   2053 	0,
   2054 	{0,0},
   2055 	0,
   2056 	0,
   2057 	{""},
   2058 	{""},
   2059 };
   2060 
   2061 /*
   2062  * Simulate a hardware hub by handling all the necessary requests.
   2063  */
   2064 Static usbd_status
   2065 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
   2066 {
   2067 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   2068 	usbd_status err;
   2069 
   2070 	/* Insert last in queue. */
   2071 	mutex_enter(&sc->sc_lock);
   2072 	err = usb_insert_transfer(xfer);
   2073 	mutex_exit(&sc->sc_lock);
   2074 	if (err)
   2075 		return (err);
   2076 
   2077 	/* Pipe isn't running, start first */
   2078 	return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2079 }
   2080 
   2081 Static usbd_status
   2082 ehci_root_ctrl_start(usbd_xfer_handle xfer)
   2083 {
   2084 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   2085 	usb_device_request_t *req;
   2086 	void *buf = NULL;
   2087 	int port, i;
   2088 	int len, value, index, l, totlen = 0;
   2089 	usb_port_status_t ps;
   2090 	usb_hub_descriptor_t hubd;
   2091 	usbd_status err;
   2092 	u_int32_t v;
   2093 
   2094 	if (sc->sc_dying)
   2095 		return (USBD_IOERROR);
   2096 
   2097 #ifdef DIAGNOSTIC
   2098 	if (!(xfer->rqflags & URQ_REQUEST))
   2099 		/* XXX panic */
   2100 		return (USBD_INVAL);
   2101 #endif
   2102 	req = &xfer->request;
   2103 
   2104 	DPRINTFN(4,("ehci_root_ctrl_start: type=0x%02x request=%02x\n",
   2105 		    req->bmRequestType, req->bRequest));
   2106 
   2107 	len = UGETW(req->wLength);
   2108 	value = UGETW(req->wValue);
   2109 	index = UGETW(req->wIndex);
   2110 
   2111 	if (len != 0)
   2112 		buf = KERNADDR(&xfer->dmabuf, 0);
   2113 
   2114 #define C(x,y) ((x) | ((y) << 8))
   2115 	switch(C(req->bRequest, req->bmRequestType)) {
   2116 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
   2117 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
   2118 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
   2119 		/*
   2120 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
   2121 		 * for the integrated root hub.
   2122 		 */
   2123 		break;
   2124 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
   2125 		if (len > 0) {
   2126 			*(u_int8_t *)buf = sc->sc_conf;
   2127 			totlen = 1;
   2128 		}
   2129 		break;
   2130 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   2131 		DPRINTFN(8,("ehci_root_ctrl_start: wValue=0x%04x\n", value));
   2132 		if (len == 0)
   2133 			break;
   2134 		switch(value >> 8) {
   2135 		case UDESC_DEVICE:
   2136 			if ((value & 0xff) != 0) {
   2137 				err = USBD_IOERROR;
   2138 				goto ret;
   2139 			}
   2140 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   2141 			USETW(ehci_devd.idVendor, sc->sc_id_vendor);
   2142 			memcpy(buf, &ehci_devd, l);
   2143 			break;
   2144 		/*
   2145 		 * We can't really operate at another speed, but the spec says
   2146 		 * we need this descriptor.
   2147 		 */
   2148 		case UDESC_DEVICE_QUALIFIER:
   2149 			if ((value & 0xff) != 0) {
   2150 				err = USBD_IOERROR;
   2151 				goto ret;
   2152 			}
   2153 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   2154 			memcpy(buf, &ehci_odevd, l);
   2155 			break;
   2156 		/*
   2157 		 * We can't really operate at another speed, but the spec says
   2158 		 * we need this descriptor.
   2159 		 */
   2160 		case UDESC_OTHER_SPEED_CONFIGURATION:
   2161 		case UDESC_CONFIG:
   2162 			if ((value & 0xff) != 0) {
   2163 				err = USBD_IOERROR;
   2164 				goto ret;
   2165 			}
   2166 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
   2167 			memcpy(buf, &ehci_confd, l);
   2168 			((usb_config_descriptor_t *)buf)->bDescriptorType =
   2169 				value >> 8;
   2170 			buf = (char *)buf + l;
   2171 			len -= l;
   2172 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
   2173 			totlen += l;
   2174 			memcpy(buf, &ehci_ifcd, l);
   2175 			buf = (char *)buf + l;
   2176 			len -= l;
   2177 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
   2178 			totlen += l;
   2179 			memcpy(buf, &ehci_endpd, l);
   2180 			break;
   2181 		case UDESC_STRING:
   2182 #define sd ((usb_string_descriptor_t *)buf)
   2183 			switch (value & 0xff) {
   2184 			case 0: /* Language table */
   2185 				totlen = usb_makelangtbl(sd, len);
   2186 				break;
   2187 			case 1: /* Vendor */
   2188 				totlen = usb_makestrdesc(sd, len,
   2189 							 sc->sc_vendor);
   2190 				break;
   2191 			case 2: /* Product */
   2192 				totlen = usb_makestrdesc(sd, len,
   2193 							 "EHCI root hub");
   2194 				break;
   2195 			}
   2196 #undef sd
   2197 			break;
   2198 		default:
   2199 			err = USBD_IOERROR;
   2200 			goto ret;
   2201 		}
   2202 		break;
   2203 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   2204 		if (len > 0) {
   2205 			*(u_int8_t *)buf = 0;
   2206 			totlen = 1;
   2207 		}
   2208 		break;
   2209 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   2210 		if (len > 1) {
   2211 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   2212 			totlen = 2;
   2213 		}
   2214 		break;
   2215 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   2216 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   2217 		if (len > 1) {
   2218 			USETW(((usb_status_t *)buf)->wStatus, 0);
   2219 			totlen = 2;
   2220 		}
   2221 		break;
   2222 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   2223 		if (value >= USB_MAX_DEVICES) {
   2224 			err = USBD_IOERROR;
   2225 			goto ret;
   2226 		}
   2227 		sc->sc_addr = value;
   2228 		break;
   2229 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   2230 		if (value != 0 && value != 1) {
   2231 			err = USBD_IOERROR;
   2232 			goto ret;
   2233 		}
   2234 		sc->sc_conf = value;
   2235 		break;
   2236 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   2237 		break;
   2238 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   2239 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   2240 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   2241 		err = USBD_IOERROR;
   2242 		goto ret;
   2243 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   2244 		break;
   2245 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   2246 		break;
   2247 	/* Hub requests */
   2248 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   2249 		break;
   2250 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   2251 		DPRINTFN(4, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
   2252 			     "port=%d feature=%d\n",
   2253 			     index, value));
   2254 		if (index < 1 || index > sc->sc_noport) {
   2255 			err = USBD_IOERROR;
   2256 			goto ret;
   2257 		}
   2258 		port = EHCI_PORTSC(index);
   2259 		v = EOREAD4(sc, port);
   2260 		DPRINTFN(4, ("ehci_root_ctrl_start: portsc=0x%08x\n", v));
   2261 		v &= ~EHCI_PS_CLEAR;
   2262 		switch(value) {
   2263 		case UHF_PORT_ENABLE:
   2264 			EOWRITE4(sc, port, v &~ EHCI_PS_PE);
   2265 			break;
   2266 		case UHF_PORT_SUSPEND:
   2267 			if (!(v & EHCI_PS_SUSP)) /* not suspended */
   2268 				break;
   2269 			v &= ~EHCI_PS_SUSP;
   2270 			EOWRITE4(sc, port, v | EHCI_PS_FPR);
   2271 			/* see USB2 spec ch. 7.1.7.7 */
   2272 			usb_delay_ms(&sc->sc_bus, 20);
   2273 			EOWRITE4(sc, port, v);
   2274 			usb_delay_ms(&sc->sc_bus, 2);
   2275 #ifdef DEBUG
   2276 			v = EOREAD4(sc, port);
   2277 			if (v & (EHCI_PS_FPR | EHCI_PS_SUSP))
   2278 				printf("ehci: resume failed: %x\n", v);
   2279 #endif
   2280 			break;
   2281 		case UHF_PORT_POWER:
   2282 			if (sc->sc_hasppc)
   2283 				EOWRITE4(sc, port, v &~ EHCI_PS_PP);
   2284 			break;
   2285 		case UHF_PORT_TEST:
   2286 			DPRINTFN(2,("ehci_root_ctrl_start: clear port test "
   2287 				    "%d\n", index));
   2288 			break;
   2289 		case UHF_PORT_INDICATOR:
   2290 			DPRINTFN(2,("ehci_root_ctrl_start: clear port ind "
   2291 				    "%d\n", index));
   2292 			EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
   2293 			break;
   2294 		case UHF_C_PORT_CONNECTION:
   2295 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
   2296 			break;
   2297 		case UHF_C_PORT_ENABLE:
   2298 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
   2299 			break;
   2300 		case UHF_C_PORT_SUSPEND:
   2301 			/* how? */
   2302 			break;
   2303 		case UHF_C_PORT_OVER_CURRENT:
   2304 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
   2305 			break;
   2306 		case UHF_C_PORT_RESET:
   2307 			sc->sc_isreset[index] = 0;
   2308 			break;
   2309 		default:
   2310 			err = USBD_IOERROR;
   2311 			goto ret;
   2312 		}
   2313 #if 0
   2314 		switch(value) {
   2315 		case UHF_C_PORT_CONNECTION:
   2316 		case UHF_C_PORT_ENABLE:
   2317 		case UHF_C_PORT_SUSPEND:
   2318 		case UHF_C_PORT_OVER_CURRENT:
   2319 		case UHF_C_PORT_RESET:
   2320 		default:
   2321 			break;
   2322 		}
   2323 #endif
   2324 		break;
   2325 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   2326 		if (len == 0)
   2327 			break;
   2328 		if ((value & 0xff) != 0) {
   2329 			err = USBD_IOERROR;
   2330 			goto ret;
   2331 		}
   2332 		hubd = ehci_hubd;
   2333 		hubd.bNbrPorts = sc->sc_noport;
   2334 		v = EOREAD4(sc, EHCI_HCSPARAMS);
   2335 		USETW(hubd.wHubCharacteristics,
   2336 		    EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
   2337 		    EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
   2338 			? UHD_PORT_IND : 0);
   2339 		hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
   2340 		for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
   2341 			hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
   2342 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
   2343 		l = min(len, hubd.bDescLength);
   2344 		totlen = l;
   2345 		memcpy(buf, &hubd, l);
   2346 		break;
   2347 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   2348 		if (len != 4) {
   2349 			err = USBD_IOERROR;
   2350 			goto ret;
   2351 		}
   2352 		memset(buf, 0, len); /* ? XXX */
   2353 		totlen = len;
   2354 		break;
   2355 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   2356 		DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n",
   2357 			    index));
   2358 		if (index < 1 || index > sc->sc_noport) {
   2359 			err = USBD_IOERROR;
   2360 			goto ret;
   2361 		}
   2362 		if (len != 4) {
   2363 			err = USBD_IOERROR;
   2364 			goto ret;
   2365 		}
   2366 		v = EOREAD4(sc, EHCI_PORTSC(index));
   2367 		DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n", v));
   2368 
   2369 		i = UPS_HIGH_SPEED;
   2370 		if (sc->sc_flags & EHCIF_ETTF) {
   2371 			/*
   2372 			 * If we are doing embedded transaction translation,
   2373 			 * then directly attached LS/FS devices are reset by
   2374 			 * the EHCI controller itself.  PSPD is encoded
   2375 			 * the same way as in USBSTATUS.
   2376 			 */
   2377 			i = __SHIFTOUT(v, EHCI_PS_PSPD) * UPS_LOW_SPEED;
   2378 		}
   2379 		if (v & EHCI_PS_CS)	i |= UPS_CURRENT_CONNECT_STATUS;
   2380 		if (v & EHCI_PS_PE)	i |= UPS_PORT_ENABLED;
   2381 		if (v & EHCI_PS_SUSP)	i |= UPS_SUSPEND;
   2382 		if (v & EHCI_PS_OCA)	i |= UPS_OVERCURRENT_INDICATOR;
   2383 		if (v & EHCI_PS_PR)	i |= UPS_RESET;
   2384 		if (v & EHCI_PS_PP)	i |= UPS_PORT_POWER;
   2385 		if (sc->sc_vendor_port_status)
   2386 			i = sc->sc_vendor_port_status(sc, v, i);
   2387 		USETW(ps.wPortStatus, i);
   2388 		i = 0;
   2389 		if (v & EHCI_PS_CSC)	i |= UPS_C_CONNECT_STATUS;
   2390 		if (v & EHCI_PS_PEC)	i |= UPS_C_PORT_ENABLED;
   2391 		if (v & EHCI_PS_OCC)	i |= UPS_C_OVERCURRENT_INDICATOR;
   2392 		if (sc->sc_isreset[index]) i |= UPS_C_PORT_RESET;
   2393 		USETW(ps.wPortChange, i);
   2394 		l = min(len, sizeof ps);
   2395 		memcpy(buf, &ps, l);
   2396 		totlen = l;
   2397 		break;
   2398 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   2399 		err = USBD_IOERROR;
   2400 		goto ret;
   2401 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   2402 		break;
   2403 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   2404 		if (index < 1 || index > sc->sc_noport) {
   2405 			err = USBD_IOERROR;
   2406 			goto ret;
   2407 		}
   2408 		port = EHCI_PORTSC(index);
   2409 		v = EOREAD4(sc, port);
   2410 		DPRINTFN(4, ("ehci_root_ctrl_start: portsc=0x%08x\n", v));
   2411 		v &= ~EHCI_PS_CLEAR;
   2412 		switch(value) {
   2413 		case UHF_PORT_ENABLE:
   2414 			EOWRITE4(sc, port, v | EHCI_PS_PE);
   2415 			break;
   2416 		case UHF_PORT_SUSPEND:
   2417 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
   2418 			break;
   2419 		case UHF_PORT_RESET:
   2420 			DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n",
   2421 				    index));
   2422 			if (EHCI_PS_IS_LOWSPEED(v)
   2423 			    && sc->sc_ncomp > 0
   2424 			    && !(sc->sc_flags & EHCIF_ETTF)) {
   2425 				/*
   2426 				 * Low speed device on non-ETTF controller or
   2427 				 * unaccompanied controller, give up ownership.
   2428 				 */
   2429 				ehci_disown(sc, index, 1);
   2430 				break;
   2431 			}
   2432 			/* Start reset sequence. */
   2433 			v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
   2434 			EOWRITE4(sc, port, v | EHCI_PS_PR);
   2435 			/* Wait for reset to complete. */
   2436 			usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
   2437 			if (sc->sc_dying) {
   2438 				err = USBD_IOERROR;
   2439 				goto ret;
   2440 			}
   2441 			/*
   2442 			 * An embedded transaction translater will automatically
   2443 			 * terminate the reset sequence so there's no need to
   2444 			 * it.
   2445 			 */
   2446 			v = EOREAD4(sc, port);
   2447 			if (v & EHCI_PS_PR) {
   2448 				/* Terminate reset sequence. */
   2449 				EOWRITE4(sc, port, v & ~EHCI_PS_PR);
   2450 				/* Wait for HC to complete reset. */
   2451 				usb_delay_ms(&sc->sc_bus,
   2452 				    EHCI_PORT_RESET_COMPLETE);
   2453 				if (sc->sc_dying) {
   2454 					err = USBD_IOERROR;
   2455 					goto ret;
   2456 				}
   2457 			}
   2458 
   2459 			v = EOREAD4(sc, port);
   2460 			DPRINTF(("ehci after reset, status=0x%08x\n", v));
   2461 			if (v & EHCI_PS_PR) {
   2462 				printf("%s: port reset timeout\n",
   2463 				       device_xname(sc->sc_dev));
   2464 				return (USBD_TIMEOUT);
   2465 			}
   2466 			if (!(v & EHCI_PS_PE)) {
   2467 				/* Not a high speed device, give up ownership.*/
   2468 				ehci_disown(sc, index, 0);
   2469 				break;
   2470 			}
   2471 			sc->sc_isreset[index] = 1;
   2472 			DPRINTF(("ehci port %d reset, status = 0x%08x\n",
   2473 				 index, v));
   2474 			break;
   2475 		case UHF_PORT_POWER:
   2476 			DPRINTFN(2,("ehci_root_ctrl_start: set port power "
   2477 				    "%d (has PPC = %d)\n", index,
   2478 				    sc->sc_hasppc));
   2479 			if (sc->sc_hasppc)
   2480 				EOWRITE4(sc, port, v | EHCI_PS_PP);
   2481 			break;
   2482 		case UHF_PORT_TEST:
   2483 			DPRINTFN(2,("ehci_root_ctrl_start: set port test "
   2484 				    "%d\n", index));
   2485 			break;
   2486 		case UHF_PORT_INDICATOR:
   2487 			DPRINTFN(2,("ehci_root_ctrl_start: set port ind "
   2488 				    "%d\n", index));
   2489 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
   2490 			break;
   2491 		default:
   2492 			err = USBD_IOERROR;
   2493 			goto ret;
   2494 		}
   2495 		break;
   2496 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
   2497 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
   2498 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
   2499 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
   2500 		break;
   2501 	default:
   2502 		err = USBD_IOERROR;
   2503 		goto ret;
   2504 	}
   2505 	xfer->actlen = totlen;
   2506 	err = USBD_NORMAL_COMPLETION;
   2507  ret:
   2508 	mutex_enter(&sc->sc_lock);
   2509 	xfer->status = err;
   2510 	usb_transfer_complete(xfer);
   2511 	mutex_exit(&sc->sc_lock);
   2512 	return (USBD_IN_PROGRESS);
   2513 }
   2514 
   2515 Static void
   2516 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
   2517 {
   2518 	int port;
   2519 	u_int32_t v;
   2520 
   2521 	DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
   2522 #ifdef DIAGNOSTIC
   2523 	if (sc->sc_npcomp != 0) {
   2524 		int i = (index-1) / sc->sc_npcomp;
   2525 		if (i >= sc->sc_ncomp)
   2526 			printf("%s: strange port\n",
   2527 			       device_xname(sc->sc_dev));
   2528 		else
   2529 			printf("%s: handing over %s speed device on "
   2530 			       "port %d to %s\n",
   2531 			       device_xname(sc->sc_dev),
   2532 			       lowspeed ? "low" : "full",
   2533 			       index, device_xname(sc->sc_comps[i]));
   2534 	} else {
   2535 		printf("%s: npcomp == 0\n", device_xname(sc->sc_dev));
   2536 	}
   2537 #endif
   2538 	port = EHCI_PORTSC(index);
   2539 	v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
   2540 	EOWRITE4(sc, port, v | EHCI_PS_PO);
   2541 }
   2542 
   2543 /* Abort a root control request. */
   2544 Static void
   2545 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
   2546 {
   2547 	/* Nothing to do, all transfers are synchronous. */
   2548 }
   2549 
   2550 /* Close the root pipe. */
   2551 Static void
   2552 ehci_root_ctrl_close(usbd_pipe_handle pipe)
   2553 {
   2554 	DPRINTF(("ehci_root_ctrl_close\n"));
   2555 	/* Nothing to do. */
   2556 }
   2557 
   2558 Static void
   2559 ehci_root_intr_done(usbd_xfer_handle xfer)
   2560 {
   2561 	xfer->hcpriv = NULL;
   2562 }
   2563 
   2564 Static usbd_status
   2565 ehci_root_intr_transfer(usbd_xfer_handle xfer)
   2566 {
   2567 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   2568 	usbd_status err;
   2569 
   2570 	/* Insert last in queue. */
   2571 	mutex_enter(&sc->sc_lock);
   2572 	err = usb_insert_transfer(xfer);
   2573 	mutex_exit(&sc->sc_lock);
   2574 	if (err)
   2575 		return (err);
   2576 
   2577 	/* Pipe isn't running, start first */
   2578 	return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2579 }
   2580 
   2581 Static usbd_status
   2582 ehci_root_intr_start(usbd_xfer_handle xfer)
   2583 {
   2584 	usbd_pipe_handle pipe = xfer->pipe;
   2585 	ehci_softc_t *sc = pipe->device->bus->hci_private;
   2586 
   2587 	if (sc->sc_dying)
   2588 		return (USBD_IOERROR);
   2589 
   2590 	mutex_enter(&sc->sc_lock);
   2591 	sc->sc_intrxfer = xfer;
   2592 	mutex_exit(&sc->sc_lock);
   2593 
   2594 	return (USBD_IN_PROGRESS);
   2595 }
   2596 
   2597 /* Abort a root interrupt request. */
   2598 Static void
   2599 ehci_root_intr_abort(usbd_xfer_handle xfer)
   2600 {
   2601 #ifdef DIAGNOSTIC
   2602 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   2603 #endif
   2604 
   2605 	KASSERT(mutex_owned(&sc->sc_lock));
   2606 	if (xfer->pipe->intrxfer == xfer) {
   2607 		DPRINTF(("ehci_root_intr_abort: remove\n"));
   2608 		xfer->pipe->intrxfer = NULL;
   2609 	}
   2610 	xfer->status = USBD_CANCELLED;
   2611 	usb_transfer_complete(xfer);
   2612 }
   2613 
   2614 /* Close the root pipe. */
   2615 Static void
   2616 ehci_root_intr_close(usbd_pipe_handle pipe)
   2617 {
   2618 	ehci_softc_t *sc = pipe->device->bus->hci_private;
   2619 
   2620 	KASSERT(mutex_owned(&sc->sc_lock));
   2621 
   2622 	DPRINTF(("ehci_root_intr_close\n"));
   2623 
   2624 	sc->sc_intrxfer = NULL;
   2625 }
   2626 
   2627 Static void
   2628 ehci_root_ctrl_done(usbd_xfer_handle xfer)
   2629 {
   2630 	xfer->hcpriv = NULL;
   2631 }
   2632 
   2633 /************************/
   2634 
   2635 Static ehci_soft_qh_t *
   2636 ehci_alloc_sqh(ehci_softc_t *sc)
   2637 {
   2638 	ehci_soft_qh_t *sqh;
   2639 	usbd_status err;
   2640 	int i, offs;
   2641 	usb_dma_t dma;
   2642 
   2643 	if (sc->sc_freeqhs == NULL) {
   2644 		DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
   2645 		err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
   2646 			  EHCI_PAGE_SIZE, &dma);
   2647 #ifdef EHCI_DEBUG
   2648 		if (err)
   2649 			printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
   2650 #endif
   2651 		if (err)
   2652 			return (NULL);
   2653 		for(i = 0; i < EHCI_SQH_CHUNK; i++) {
   2654 			offs = i * EHCI_SQH_SIZE;
   2655 			sqh = KERNADDR(&dma, offs);
   2656 			sqh->physaddr = DMAADDR(&dma, offs);
   2657 			sqh->dma = dma;
   2658 			sqh->offs = offs;
   2659 			sqh->next = sc->sc_freeqhs;
   2660 			sc->sc_freeqhs = sqh;
   2661 		}
   2662 	}
   2663 	sqh = sc->sc_freeqhs;
   2664 	sc->sc_freeqhs = sqh->next;
   2665 	memset(&sqh->qh, 0, sizeof(ehci_qh_t));
   2666 	sqh->next = NULL;
   2667 	return (sqh);
   2668 }
   2669 
   2670 Static void
   2671 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
   2672 {
   2673 	sqh->next = sc->sc_freeqhs;
   2674 	sc->sc_freeqhs = sqh;
   2675 }
   2676 
   2677 Static ehci_soft_qtd_t *
   2678 ehci_alloc_sqtd(ehci_softc_t *sc)
   2679 {
   2680 	ehci_soft_qtd_t *sqtd = NULL;
   2681 	usbd_status err;
   2682 	int i, offs;
   2683 	usb_dma_t dma;
   2684 
   2685 	if (sc->sc_freeqtds == NULL) {
   2686 		DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n"));
   2687 
   2688 		err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
   2689 			  EHCI_PAGE_SIZE, &dma);
   2690 #ifdef EHCI_DEBUG
   2691 		if (err)
   2692 			printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
   2693 #endif
   2694 		if (err)
   2695 			goto done;
   2696 
   2697 		for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
   2698 			offs = i * EHCI_SQTD_SIZE;
   2699 			sqtd = KERNADDR(&dma, offs);
   2700 			sqtd->physaddr = DMAADDR(&dma, offs);
   2701 			sqtd->dma = dma;
   2702 			sqtd->offs = offs;
   2703 
   2704 			sqtd->nextqtd = sc->sc_freeqtds;
   2705 			sc->sc_freeqtds = sqtd;
   2706 		}
   2707 	}
   2708 
   2709 	sqtd = sc->sc_freeqtds;
   2710 	sc->sc_freeqtds = sqtd->nextqtd;
   2711 	memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t));
   2712 	sqtd->nextqtd = NULL;
   2713 	sqtd->xfer = NULL;
   2714 
   2715 done:
   2716 	return (sqtd);
   2717 }
   2718 
   2719 Static void
   2720 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
   2721 {
   2722 
   2723 	KASSERT(mutex_owned(&sc->sc_lock));
   2724 
   2725 	sqtd->nextqtd = sc->sc_freeqtds;
   2726 	sc->sc_freeqtds = sqtd;
   2727 }
   2728 
   2729 Static usbd_status
   2730 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
   2731 		     int alen, int rd, usbd_xfer_handle xfer,
   2732 		     ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
   2733 {
   2734 	ehci_soft_qtd_t *next, *cur;
   2735 	ehci_physaddr_t nextphys;
   2736 	u_int32_t qtdstatus;
   2737 	int len, curlen, mps;
   2738 	int i, tog;
   2739 	int pages, pageoffs;
   2740 	bus_size_t curoffs;
   2741 	vaddr_t va, va_offs;
   2742 	usb_dma_t *dma = &xfer->dmabuf;
   2743 	u_int16_t flags = xfer->flags;
   2744 	paddr_t a;
   2745 
   2746 	DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen));
   2747 
   2748 	len = alen;
   2749 	qtdstatus = EHCI_QTD_ACTIVE |
   2750 	    EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
   2751 	    EHCI_QTD_SET_CERR(3)
   2752 	    /* IOC set below */
   2753 	    /* BYTES set below */
   2754 	    ;
   2755 	mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize);
   2756 	tog = epipe->nexttoggle;
   2757 	qtdstatus |= EHCI_QTD_SET_TOGGLE(tog);
   2758 
   2759 	cur = ehci_alloc_sqtd(sc);
   2760 	*sp = cur;
   2761 	if (cur == NULL)
   2762 		goto nomem;
   2763 
   2764 	usb_syncmem(dma, 0, alen,
   2765 	    rd ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   2766 	curoffs = 0;
   2767 	for (;;) {
   2768 		/* The EHCI hardware can handle at most 5 pages. */
   2769 		va_offs = (vaddr_t)KERNADDR(dma, curoffs);
   2770 		va_offs = EHCI_PAGE_OFFSET(va_offs);
   2771 		if (len-curoffs < EHCI_QTD_NBUFFERS*EHCI_PAGE_SIZE - va_offs) {
   2772 			/* we can handle it in this QTD */
   2773 			curlen = len - curoffs;
   2774 		} else {
   2775 			/* must use multiple TDs, fill as much as possible. */
   2776 			curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE - va_offs;
   2777 
   2778 			/* the length must be a multiple of the max size */
   2779 			curlen -= curlen % mps;
   2780 			DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, "
   2781 				    "curlen=%d\n", curlen));
   2782 #ifdef DIAGNOSTIC
   2783 			if (curlen == 0)
   2784 				panic("ehci_alloc_sqtd_chain: curlen == 0");
   2785 #endif
   2786 		}
   2787 		DPRINTFN(4,("ehci_alloc_sqtd_chain: len=%d curlen=%d "
   2788 			    "curoffs=%d\n", len, curlen, curoffs));
   2789 
   2790 		/*
   2791 		 * Allocate another transfer if there's more data left,
   2792 		 * or if force last short transfer flag is set and we're
   2793 		 * allocating a multiple of the max packet size.
   2794 		 */
   2795 
   2796 		if (curoffs + curlen != len ||
   2797 		    ((curlen % mps) == 0 && !rd && curlen != 0 &&
   2798 		     (flags & USBD_FORCE_SHORT_XFER))) {
   2799 			next = ehci_alloc_sqtd(sc);
   2800 			if (next == NULL)
   2801 				goto nomem;
   2802 			nextphys = htole32(next->physaddr);
   2803 		} else {
   2804 			next = NULL;
   2805 			nextphys = EHCI_NULL;
   2806 		}
   2807 
   2808 		/* Find number of pages we'll be using, insert dma addresses */
   2809 		pages = EHCI_PAGE(curlen + EHCI_PAGE_SIZE -1) >> 12;
   2810 		KASSERT(pages <= EHCI_QTD_NBUFFERS);
   2811 		pageoffs = EHCI_PAGE(curoffs);
   2812 		for (i = 0; i < pages; i++) {
   2813 			a = DMAADDR(dma, pageoffs + i * EHCI_PAGE_SIZE);
   2814 			cur->qtd.qtd_buffer[i] = htole32(a & 0xFFFFF000);
   2815 			/* Cast up to avoid compiler warnings */
   2816 			cur->qtd.qtd_buffer_hi[i] = htole32((uint64_t)a >> 32);
   2817 		}
   2818 
   2819 		/* First buffer pointer requires a page offset to start at */
   2820 		va = (vaddr_t)KERNADDR(dma, curoffs);
   2821 		cur->qtd.qtd_buffer[0] |= htole32(EHCI_PAGE_OFFSET(va));
   2822 
   2823 		cur->nextqtd = next;
   2824 		cur->qtd.qtd_next = cur->qtd.qtd_altnext = nextphys;
   2825 		cur->qtd.qtd_status =
   2826 		    htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
   2827 		cur->xfer = xfer;
   2828 		cur->len = curlen;
   2829 
   2830 		DPRINTFN(10,("ehci_alloc_sqtd_chain: cbp=0x%08x end=0x%08x\n",
   2831 			    curoffs, curoffs + curlen));
   2832 
   2833 		/* adjust the toggle based on the number of packets in this
   2834 		   qtd */
   2835 		if (((curlen + mps - 1) / mps) & 1) {
   2836 			tog ^= 1;
   2837 			qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
   2838 		}
   2839 		if (next == NULL)
   2840 			break;
   2841 		usb_syncmem(&cur->dma, cur->offs, sizeof(cur->qtd),
   2842 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2843 		DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
   2844 		if (len)
   2845 			curoffs += curlen;
   2846 		cur = next;
   2847 	}
   2848 	cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
   2849 	usb_syncmem(&cur->dma, cur->offs, sizeof(cur->qtd),
   2850 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2851 	*ep = cur;
   2852 	epipe->nexttoggle = tog;
   2853 
   2854 	DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
   2855 		     *sp, *ep));
   2856 
   2857 	return (USBD_NORMAL_COMPLETION);
   2858 
   2859  nomem:
   2860 	/* XXX free chain */
   2861 	DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
   2862 	return (USBD_NOMEM);
   2863 }
   2864 
   2865 Static void
   2866 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd,
   2867 		    ehci_soft_qtd_t *sqtdend)
   2868 {
   2869 	ehci_soft_qtd_t *p;
   2870 	int i;
   2871 
   2872 	DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
   2873 		     sqtd, sqtdend));
   2874 
   2875 	for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
   2876 		p = sqtd->nextqtd;
   2877 		ehci_free_sqtd(sc, sqtd);
   2878 	}
   2879 }
   2880 
   2881 Static ehci_soft_itd_t *
   2882 ehci_alloc_itd(ehci_softc_t *sc)
   2883 {
   2884 	struct ehci_soft_itd *itd, *freeitd;
   2885 	usbd_status err;
   2886 	int i, offs, frindex, previndex;
   2887 	usb_dma_t dma;
   2888 
   2889 	mutex_enter(&sc->sc_lock);
   2890 
   2891 	/* Find an itd that wasn't freed this frame or last frame. This can
   2892 	 * discard itds that were freed before frindex wrapped around
   2893 	 * XXX - can this lead to thrashing? Could fix by enabling wrap-around
   2894 	 *       interrupt and fiddling with list when that happens */
   2895 	frindex = (EOREAD4(sc, EHCI_FRINDEX) + 1) >> 3;
   2896 	previndex = (frindex != 0) ? frindex - 1 : sc->sc_flsize;
   2897 
   2898 	freeitd = NULL;
   2899 	LIST_FOREACH(itd, &sc->sc_freeitds, u.free_list) {
   2900 		if (itd == NULL)
   2901 			break;
   2902 		if (itd->slot != frindex && itd->slot != previndex) {
   2903 			freeitd = itd;
   2904 			break;
   2905 		}
   2906 	}
   2907 
   2908 	if (freeitd == NULL) {
   2909 		DPRINTFN(2, ("ehci_alloc_itd allocating chunk\n"));
   2910 		err = usb_allocmem(&sc->sc_bus, EHCI_ITD_SIZE * EHCI_ITD_CHUNK,
   2911 				EHCI_PAGE_SIZE, &dma);
   2912 
   2913 		if (err) {
   2914 			DPRINTF(("ehci_alloc_itd, alloc returned %d\n", err));
   2915 			mutex_exit(&sc->sc_lock);
   2916 			return NULL;
   2917 		}
   2918 
   2919 		for (i = 0; i < EHCI_ITD_CHUNK; i++) {
   2920 			offs = i * EHCI_ITD_SIZE;
   2921 			itd = KERNADDR(&dma, offs);
   2922 			itd->physaddr = DMAADDR(&dma, offs);
   2923 	 		itd->dma = dma;
   2924 			itd->offs = offs;
   2925 			LIST_INSERT_HEAD(&sc->sc_freeitds, itd, u.free_list);
   2926 		}
   2927 		freeitd = LIST_FIRST(&sc->sc_freeitds);
   2928 	}
   2929 
   2930 	itd = freeitd;
   2931 	LIST_REMOVE(itd, u.free_list);
   2932 	memset(&itd->itd, 0, sizeof(ehci_itd_t));
   2933 	usb_syncmem(&itd->dma, itd->offs + offsetof(ehci_itd_t, itd_next),
   2934                     sizeof(itd->itd.itd_next), BUS_DMASYNC_PREWRITE |
   2935                     BUS_DMASYNC_PREREAD);
   2936 
   2937 	itd->u.frame_list.next = NULL;
   2938 	itd->u.frame_list.prev = NULL;
   2939 	itd->xfer_next = NULL;
   2940 	itd->slot = 0;
   2941 
   2942 	mutex_exit(&sc->sc_lock);
   2943 
   2944 	return itd;
   2945 }
   2946 
   2947 Static void
   2948 ehci_free_itd(ehci_softc_t *sc, ehci_soft_itd_t *itd)
   2949 {
   2950 
   2951 	KASSERT(mutex_owned(&sc->sc_lock));
   2952 
   2953 	LIST_INSERT_HEAD(&sc->sc_freeitds, itd, u.free_list);
   2954 }
   2955 
   2956 /****************/
   2957 
   2958 /*
   2959  * Close a reqular pipe.
   2960  * Assumes that there are no pending transactions.
   2961  */
   2962 Static void
   2963 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
   2964 {
   2965 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   2966 	ehci_softc_t *sc = pipe->device->bus->hci_private;
   2967 	ehci_soft_qh_t *sqh = epipe->sqh;
   2968 
   2969 	KASSERT(mutex_owned(&sc->sc_lock));
   2970 
   2971 	ehci_rem_qh(sc, sqh, head);
   2972 	ehci_free_sqh(sc, epipe->sqh);
   2973 }
   2974 
   2975 /*
   2976  * Abort a device request.
   2977  * If this routine is called at splusb() it guarantees that the request
   2978  * will be removed from the hardware scheduling and that the callback
   2979  * for it will be called with USBD_CANCELLED status.
   2980  * It's impossible to guarantee that the requested transfer will not
   2981  * have happened since the hardware runs concurrently.
   2982  * If the transaction has already happened we rely on the ordinary
   2983  * interrupt processing to process it.
   2984  * XXX This is most probably wrong.
   2985  * XXXMRG this doesn't make sense anymore.
   2986  */
   2987 Static void
   2988 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
   2989 {
   2990 #define exfer EXFER(xfer)
   2991 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   2992 	ehci_softc_t *sc = epipe->pipe.device->bus->hci_private;
   2993 	ehci_soft_qh_t *sqh = epipe->sqh;
   2994 	ehci_soft_qtd_t *sqtd;
   2995 	ehci_physaddr_t cur;
   2996 	u_int32_t qhstatus;
   2997 	int hit;
   2998 	int wake;
   2999 
   3000 	DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe));
   3001 
   3002 	KASSERT(mutex_owned(&sc->sc_lock));
   3003 
   3004 	if (sc->sc_dying) {
   3005 		/* If we're dying, just do the software part. */
   3006 		xfer->status = status;	/* make software ignore it */
   3007 		callout_stop(&xfer->timeout_handle);
   3008 		usb_transfer_complete(xfer);
   3009 		return;
   3010 	}
   3011 
   3012 	if (cpu_intr_p() || cpu_softintr_p())
   3013 		panic("ehci_abort_xfer: not in process context");
   3014 
   3015 	/*
   3016 	 * If an abort is already in progress then just wait for it to
   3017 	 * complete and return.
   3018 	 */
   3019 	if (xfer->hcflags & UXFER_ABORTING) {
   3020 		DPRINTFN(2, ("ehci_abort_xfer: already aborting\n"));
   3021 #ifdef DIAGNOSTIC
   3022 		if (status == USBD_TIMEOUT)
   3023 			printf("ehci_abort_xfer: TIMEOUT while aborting\n");
   3024 #endif
   3025 		/* Override the status which might be USBD_TIMEOUT. */
   3026 		xfer->status = status;
   3027 		DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n"));
   3028 		xfer->hcflags |= UXFER_ABORTWAIT;
   3029 		while (xfer->hcflags & UXFER_ABORTING)
   3030 			cv_wait(&xfer->hccv, &sc->sc_lock);
   3031 		return;
   3032 	}
   3033 	xfer->hcflags |= UXFER_ABORTING;
   3034 
   3035 	/*
   3036 	 * Step 1: Make interrupt routine and hardware ignore xfer.
   3037 	 */
   3038 	xfer->status = status;	/* make software ignore it */
   3039 	callout_stop(&xfer->timeout_handle);
   3040 
   3041 	usb_syncmem(&sqh->dma,
   3042 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   3043 	    sizeof(sqh->qh.qh_qtd.qtd_status),
   3044 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3045 	qhstatus = sqh->qh.qh_qtd.qtd_status;
   3046 	sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED);
   3047 	usb_syncmem(&sqh->dma,
   3048 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   3049 	    sizeof(sqh->qh.qh_qtd.qtd_status),
   3050 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3051 	for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
   3052 		usb_syncmem(&sqtd->dma,
   3053 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
   3054 		    sizeof(sqtd->qtd.qtd_status),
   3055 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3056 		sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
   3057 		usb_syncmem(&sqtd->dma,
   3058 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
   3059 		    sizeof(sqtd->qtd.qtd_status),
   3060 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3061 		if (sqtd == exfer->sqtdend)
   3062 			break;
   3063 	}
   3064 
   3065 	/*
   3066 	 * Step 2: Wait until we know hardware has finished any possible
   3067 	 * use of the xfer.  Also make sure the soft interrupt routine
   3068 	 * has run.
   3069 	 */
   3070 	ehci_sync_hc(sc);
   3071 	sc->sc_softwake = 1;
   3072 	usb_schedsoftintr(&sc->sc_bus);
   3073 	cv_wait(&sc->sc_softwake_cv, &sc->sc_lock);
   3074 
   3075 	/*
   3076 	 * Step 3: Remove any vestiges of the xfer from the hardware.
   3077 	 * The complication here is that the hardware may have executed
   3078 	 * beyond the xfer we're trying to abort.  So as we're scanning
   3079 	 * the TDs of this xfer we check if the hardware points to
   3080 	 * any of them.
   3081 	 */
   3082 
   3083 	usb_syncmem(&sqh->dma,
   3084 	    sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
   3085 	    sizeof(sqh->qh.qh_curqtd),
   3086 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3087 	cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
   3088 	hit = 0;
   3089 	for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
   3090 		hit |= cur == sqtd->physaddr;
   3091 		if (sqtd == exfer->sqtdend)
   3092 			break;
   3093 	}
   3094 	sqtd = sqtd->nextqtd;
   3095 	/* Zap curqtd register if hardware pointed inside the xfer. */
   3096 	if (hit && sqtd != NULL) {
   3097 		DPRINTFN(1,("ehci_abort_xfer: cur=0x%08x\n", sqtd->physaddr));
   3098 		sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */
   3099 		usb_syncmem(&sqh->dma,
   3100 		    sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
   3101 		    sizeof(sqh->qh.qh_curqtd),
   3102 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3103 		sqh->qh.qh_qtd.qtd_status = qhstatus;
   3104 		usb_syncmem(&sqh->dma,
   3105 		    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   3106 		    sizeof(sqh->qh.qh_qtd.qtd_status),
   3107 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3108 	} else {
   3109 		DPRINTFN(1,("ehci_abort_xfer: no hit\n"));
   3110 	}
   3111 
   3112 	/*
   3113 	 * Step 4: Execute callback.
   3114 	 */
   3115 #ifdef DIAGNOSTIC
   3116 	exfer->isdone = 1;
   3117 #endif
   3118 	wake = xfer->hcflags & UXFER_ABORTWAIT;
   3119 	xfer->hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
   3120 	usb_transfer_complete(xfer);
   3121 	if (wake) {
   3122 		cv_broadcast(&xfer->hccv);
   3123 	}
   3124 
   3125 	KASSERT(mutex_owned(&sc->sc_lock));
   3126 #undef exfer
   3127 }
   3128 
   3129 Static void
   3130 ehci_abort_isoc_xfer(usbd_xfer_handle xfer, usbd_status status)
   3131 {
   3132 	ehci_isoc_trans_t trans_status;
   3133 	struct ehci_pipe *epipe;
   3134 	struct ehci_xfer *exfer;
   3135 	ehci_softc_t *sc;
   3136 	struct ehci_soft_itd *itd;
   3137 	int i, wake;
   3138 
   3139 	epipe = (struct ehci_pipe *) xfer->pipe;
   3140 	exfer = EXFER(xfer);
   3141 	sc = epipe->pipe.device->bus->hci_private;
   3142 
   3143 	DPRINTF(("ehci_abort_isoc_xfer: xfer %p pipe %p\n", xfer, epipe));
   3144 
   3145 	KASSERT(mutex_owned(&sc->sc_lock));
   3146 
   3147 	if (sc->sc_dying) {
   3148 		xfer->status = status;
   3149 		callout_stop(&xfer->timeout_handle);
   3150 		usb_transfer_complete(xfer);
   3151 		return;
   3152 	}
   3153 
   3154 	if (xfer->hcflags & UXFER_ABORTING) {
   3155 		DPRINTFN(2, ("ehci_abort_isoc_xfer: already aborting\n"));
   3156 
   3157 #ifdef DIAGNOSTIC
   3158 		if (status == USBD_TIMEOUT)
   3159 			printf("ehci_abort_isoc_xfer: TIMEOUT while aborting\n");
   3160 #endif
   3161 
   3162 		xfer->status = status;
   3163 		DPRINTFN(2, ("ehci_abort_isoc_xfer: waiting for abort to finish\n"));
   3164 		xfer->hcflags |= UXFER_ABORTWAIT;
   3165 		while (xfer->hcflags & UXFER_ABORTING)
   3166 			cv_wait(&xfer->hccv, &sc->sc_lock);
   3167 		goto done;
   3168 	}
   3169 	xfer->hcflags |= UXFER_ABORTING;
   3170 
   3171 	xfer->status = status;
   3172 	callout_stop(&xfer->timeout_handle);
   3173 
   3174 	for (itd = exfer->itdstart; itd != NULL; itd = itd->xfer_next) {
   3175 		usb_syncmem(&itd->dma,
   3176 		    itd->offs + offsetof(ehci_itd_t, itd_ctl),
   3177 		    sizeof(itd->itd.itd_ctl),
   3178 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3179 
   3180 		for (i = 0; i < 8; i++) {
   3181 			trans_status = le32toh(itd->itd.itd_ctl[i]);
   3182 			trans_status &= ~EHCI_ITD_ACTIVE;
   3183 			itd->itd.itd_ctl[i] = htole32(trans_status);
   3184 		}
   3185 
   3186 		usb_syncmem(&itd->dma,
   3187 		    itd->offs + offsetof(ehci_itd_t, itd_ctl),
   3188 		    sizeof(itd->itd.itd_ctl),
   3189 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3190 	}
   3191 
   3192         sc->sc_softwake = 1;
   3193         usb_schedsoftintr(&sc->sc_bus);
   3194 	cv_wait(&sc->sc_softwake_cv, &sc->sc_lock);
   3195 
   3196 #ifdef DIAGNOSTIC
   3197 	exfer->isdone = 1;
   3198 #endif
   3199 	wake = xfer->hcflags & UXFER_ABORTWAIT;
   3200 	xfer->hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
   3201 	usb_transfer_complete(xfer);
   3202 	if (wake) {
   3203 		cv_broadcast(&xfer->hccv);
   3204 	}
   3205 
   3206 done:
   3207 	KASSERT(mutex_owned(&sc->sc_lock));
   3208 	return;
   3209 }
   3210 
   3211 Static void
   3212 ehci_timeout(void *addr)
   3213 {
   3214 	struct ehci_xfer *exfer = addr;
   3215 	struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe;
   3216 	ehci_softc_t *sc = epipe->pipe.device->bus->hci_private;
   3217 
   3218 	DPRINTF(("ehci_timeout: exfer=%p\n", exfer));
   3219 #ifdef EHCI_DEBUG
   3220 	if (ehcidebug > 1)
   3221 		usbd_dump_pipe(exfer->xfer.pipe);
   3222 #endif
   3223 
   3224 	if (sc->sc_dying) {
   3225 		mutex_enter(&sc->sc_lock);
   3226 		ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT);
   3227 		mutex_exit(&sc->sc_lock);
   3228 		return;
   3229 	}
   3230 
   3231 	/* Execute the abort in a process context. */
   3232 	usb_init_task(&exfer->abort_task, ehci_timeout_task, addr);
   3233 	usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task,
   3234 	    USB_TASKQ_HC);
   3235 }
   3236 
   3237 Static void
   3238 ehci_timeout_task(void *addr)
   3239 {
   3240 	usbd_xfer_handle xfer = addr;
   3241 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   3242 
   3243 	DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer));
   3244 
   3245 	mutex_enter(&sc->sc_lock);
   3246 	ehci_abort_xfer(xfer, USBD_TIMEOUT);
   3247 	mutex_exit(&sc->sc_lock);
   3248 }
   3249 
   3250 /************************/
   3251 
   3252 Static usbd_status
   3253 ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
   3254 {
   3255 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   3256 	usbd_status err;
   3257 
   3258 	/* Insert last in queue. */
   3259 	mutex_enter(&sc->sc_lock);
   3260 	err = usb_insert_transfer(xfer);
   3261 	mutex_exit(&sc->sc_lock);
   3262 	if (err)
   3263 		return (err);
   3264 
   3265 	/* Pipe isn't running, start first */
   3266 	return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   3267 }
   3268 
   3269 Static usbd_status
   3270 ehci_device_ctrl_start(usbd_xfer_handle xfer)
   3271 {
   3272 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   3273 	usbd_status err;
   3274 
   3275 	if (sc->sc_dying)
   3276 		return (USBD_IOERROR);
   3277 
   3278 #ifdef DIAGNOSTIC
   3279 	if (!(xfer->rqflags & URQ_REQUEST)) {
   3280 		/* XXX panic */
   3281 		printf("ehci_device_ctrl_transfer: not a request\n");
   3282 		return (USBD_INVAL);
   3283 	}
   3284 #endif
   3285 
   3286 	err = ehci_device_request(xfer);
   3287 	if (err) {
   3288 		return (err);
   3289 	}
   3290 
   3291 	if (sc->sc_bus.use_polling)
   3292 		ehci_waitintr(sc, xfer);
   3293 
   3294 	return (USBD_IN_PROGRESS);
   3295 }
   3296 
   3297 Static void
   3298 ehci_device_ctrl_done(usbd_xfer_handle xfer)
   3299 {
   3300 	struct ehci_xfer *ex = EXFER(xfer);
   3301 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   3302 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   3303 	usb_device_request_t *req = &xfer->request;
   3304 	int len = UGETW(req->wLength);
   3305 	int rd = req->bmRequestType & UT_READ;
   3306 
   3307 	DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer));
   3308 
   3309 	KASSERT(mutex_owned(&sc->sc_lock));
   3310 
   3311 #ifdef DIAGNOSTIC
   3312 	if (!(xfer->rqflags & URQ_REQUEST)) {
   3313 		panic("ehci_ctrl_done: not a request");
   3314 	}
   3315 #endif
   3316 
   3317 	if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
   3318 		ehci_del_intr_list(sc, ex);	/* remove from active list */
   3319 		ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
   3320 		usb_syncmem(&epipe->u.ctl.reqdma, 0, sizeof *req,
   3321 		    BUS_DMASYNC_POSTWRITE);
   3322 		if (len)
   3323 			usb_syncmem(&xfer->dmabuf, 0, len,
   3324 			    rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   3325 	}
   3326 
   3327 	DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen));
   3328 }
   3329 
   3330 /* Abort a device control request. */
   3331 Static void
   3332 ehci_device_ctrl_abort(usbd_xfer_handle xfer)
   3333 {
   3334 	DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer));
   3335 	ehci_abort_xfer(xfer, USBD_CANCELLED);
   3336 }
   3337 
   3338 /* Close a device control pipe. */
   3339 Static void
   3340 ehci_device_ctrl_close(usbd_pipe_handle pipe)
   3341 {
   3342 	ehci_softc_t *sc = pipe->device->bus->hci_private;
   3343 	/*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
   3344 
   3345 	KASSERT(mutex_owned(&sc->sc_lock));
   3346 
   3347 	DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe));
   3348 
   3349 	ehci_close_pipe(pipe, sc->sc_async_head);
   3350 }
   3351 
   3352 Static usbd_status
   3353 ehci_device_request(usbd_xfer_handle xfer)
   3354 {
   3355 #define exfer EXFER(xfer)
   3356 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   3357 	usb_device_request_t *req = &xfer->request;
   3358 	usbd_device_handle dev = epipe->pipe.device;
   3359 	ehci_softc_t *sc = dev->bus->hci_private;
   3360 	int addr = dev->address;
   3361 	ehci_soft_qtd_t *setup, *stat, *next;
   3362 	ehci_soft_qh_t *sqh;
   3363 	int isread;
   3364 	int len;
   3365 	usbd_status err;
   3366 
   3367 	isread = req->bmRequestType & UT_READ;
   3368 	len = UGETW(req->wLength);
   3369 
   3370 	DPRINTFN(3,("ehci_device_request: type=0x%02x, request=0x%02x, "
   3371 		    "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
   3372 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
   3373 		    UGETW(req->wIndex), len, addr,
   3374 		    epipe->pipe.endpoint->edesc->bEndpointAddress));
   3375 
   3376 	setup = ehci_alloc_sqtd(sc);
   3377 	if (setup == NULL) {
   3378 		err = USBD_NOMEM;
   3379 		goto bad1;
   3380 	}
   3381 	stat = ehci_alloc_sqtd(sc);
   3382 	if (stat == NULL) {
   3383 		err = USBD_NOMEM;
   3384 		goto bad2;
   3385 	}
   3386 
   3387 	mutex_enter(&sc->sc_lock);
   3388 
   3389 	sqh = epipe->sqh;
   3390 
   3391 	/*
   3392 	 * Update device address and length since they may have changed
   3393 	 * during the setup of the control pipe in usbd_new_device().
   3394 	 */
   3395 	/* XXX This only needs to be done once, but it's too early in open. */
   3396 	/* XXXX Should not touch ED here! */
   3397 	sqh->qh.qh_endp =
   3398 	    (sqh->qh.qh_endp & htole32(~(EHCI_QH_ADDRMASK | EHCI_QH_MPLMASK))) |
   3399 	    htole32(
   3400 	     EHCI_QH_SET_ADDR(addr) |
   3401 	     EHCI_QH_SET_MPL(UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize))
   3402 	    );
   3403 
   3404 	/* Set up data transaction */
   3405 	if (len != 0) {
   3406 		ehci_soft_qtd_t *end;
   3407 
   3408 		/* Start toggle at 1. */
   3409 		epipe->nexttoggle = 1;
   3410 		err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
   3411 			  &next, &end);
   3412 		if (err)
   3413 			goto bad3;
   3414 		end->qtd.qtd_status &= htole32(~EHCI_QTD_IOC);
   3415 		end->nextqtd = stat;
   3416 		end->qtd.qtd_next =
   3417 		end->qtd.qtd_altnext = htole32(stat->physaddr);
   3418 		usb_syncmem(&end->dma, end->offs, sizeof(end->qtd),
   3419 		   BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3420 	} else {
   3421 		next = stat;
   3422 	}
   3423 
   3424 	memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
   3425 	usb_syncmem(&epipe->u.ctl.reqdma, 0, sizeof *req, BUS_DMASYNC_PREWRITE);
   3426 
   3427 	/* Clear toggle */
   3428 	setup->qtd.qtd_status = htole32(
   3429 	    EHCI_QTD_ACTIVE |
   3430 	    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
   3431 	    EHCI_QTD_SET_CERR(3) |
   3432 	    EHCI_QTD_SET_TOGGLE(0) |
   3433 	    EHCI_QTD_SET_BYTES(sizeof *req)
   3434 	    );
   3435 	setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
   3436 	setup->qtd.qtd_buffer_hi[0] = 0;
   3437 	setup->nextqtd = next;
   3438 	setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
   3439 	setup->xfer = xfer;
   3440 	setup->len = sizeof *req;
   3441 	usb_syncmem(&setup->dma, setup->offs, sizeof(setup->qtd),
   3442 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3443 
   3444 	stat->qtd.qtd_status = htole32(
   3445 	    EHCI_QTD_ACTIVE |
   3446 	    EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
   3447 	    EHCI_QTD_SET_CERR(3) |
   3448 	    EHCI_QTD_SET_TOGGLE(1) |
   3449 	    EHCI_QTD_IOC
   3450 	    );
   3451 	stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
   3452 	stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */
   3453 	stat->nextqtd = NULL;
   3454 	stat->qtd.qtd_next = stat->qtd.qtd_altnext = EHCI_NULL;
   3455 	stat->xfer = xfer;
   3456 	stat->len = 0;
   3457 	usb_syncmem(&stat->dma, stat->offs, sizeof(stat->qtd),
   3458 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3459 
   3460 #ifdef EHCI_DEBUG
   3461 	if (ehcidebug > 5) {
   3462 		DPRINTF(("ehci_device_request:\n"));
   3463 		ehci_dump_sqh(sqh);
   3464 		ehci_dump_sqtds(setup);
   3465 	}
   3466 #endif
   3467 
   3468 	exfer->sqtdstart = setup;
   3469 	exfer->sqtdend = stat;
   3470 #ifdef DIAGNOSTIC
   3471 	if (!exfer->isdone) {
   3472 		printf("ehci_device_request: not done, exfer=%p\n", exfer);
   3473 	}
   3474 	exfer->isdone = 0;
   3475 #endif
   3476 
   3477 	/* Insert qTD in QH list. */
   3478 	ehci_set_qh_qtd(sqh, setup); /* also does usb_syncmem(sqh) */
   3479 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   3480 		callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
   3481 		    ehci_timeout, xfer);
   3482 	}
   3483 	ehci_add_intr_list(sc, exfer);
   3484 	xfer->status = USBD_IN_PROGRESS;
   3485 	mutex_exit(&sc->sc_lock);
   3486 
   3487 #ifdef EHCI_DEBUG
   3488 	if (ehcidebug > 10) {
   3489 		DPRINTF(("ehci_device_request: status=%x\n",
   3490 			 EOREAD4(sc, EHCI_USBSTS)));
   3491 		delay(10000);
   3492 		ehci_dump_regs(sc);
   3493 		ehci_dump_sqh(sc->sc_async_head);
   3494 		ehci_dump_sqh(sqh);
   3495 		ehci_dump_sqtds(setup);
   3496 	}
   3497 #endif
   3498 
   3499 	return (USBD_NORMAL_COMPLETION);
   3500 
   3501  bad3:
   3502 	mutex_exit(&sc->sc_lock);
   3503 	ehci_free_sqtd(sc, stat);
   3504  bad2:
   3505 	ehci_free_sqtd(sc, setup);
   3506  bad1:
   3507 	DPRINTFN(-1,("ehci_device_request: no memory\n"));
   3508 	mutex_enter(&sc->sc_lock);
   3509 	xfer->status = err;
   3510 	usb_transfer_complete(xfer);
   3511 	mutex_exit(&sc->sc_lock);
   3512 	return (err);
   3513 #undef exfer
   3514 }
   3515 
   3516 /*
   3517  * Some EHCI chips from VIA seem to trigger interrupts before writing back the
   3518  * qTD status, or miss signalling occasionally under heavy load.  If the host
   3519  * machine is too fast, we we can miss transaction completion - when we scan
   3520  * the active list the transaction still seems to be active.  This generally
   3521  * exhibits itself as a umass stall that never recovers.
   3522  *
   3523  * We work around this behaviour by setting up this callback after any softintr
   3524  * that completes with transactions still pending, giving us another chance to
   3525  * check for completion after the writeback has taken place.
   3526  */
   3527 Static void
   3528 ehci_intrlist_timeout(void *arg)
   3529 {
   3530 	ehci_softc_t *sc = arg;
   3531 
   3532 	DPRINTF(("ehci_intrlist_timeout\n"));
   3533 	usb_schedsoftintr(&sc->sc_bus);
   3534 }
   3535 
   3536 /************************/
   3537 
   3538 Static usbd_status
   3539 ehci_device_bulk_transfer(usbd_xfer_handle xfer)
   3540 {
   3541 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   3542 	usbd_status err;
   3543 
   3544 	/* Insert last in queue. */
   3545 	mutex_enter(&sc->sc_lock);
   3546 	err = usb_insert_transfer(xfer);
   3547 	mutex_exit(&sc->sc_lock);
   3548 	if (err)
   3549 		return (err);
   3550 
   3551 	/* Pipe isn't running, start first */
   3552 	return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   3553 }
   3554 
   3555 Static usbd_status
   3556 ehci_device_bulk_start(usbd_xfer_handle xfer)
   3557 {
   3558 #define exfer EXFER(xfer)
   3559 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   3560 	usbd_device_handle dev = epipe->pipe.device;
   3561 	ehci_softc_t *sc = dev->bus->hci_private;
   3562 	ehci_soft_qtd_t *data, *dataend;
   3563 	ehci_soft_qh_t *sqh;
   3564 	usbd_status err;
   3565 	int len, isread, endpt;
   3566 
   3567 	DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%d flags=%d\n",
   3568 		     xfer, xfer->length, xfer->flags));
   3569 
   3570 	if (sc->sc_dying)
   3571 		return (USBD_IOERROR);
   3572 
   3573 #ifdef DIAGNOSTIC
   3574 	if (xfer->rqflags & URQ_REQUEST)
   3575 		panic("ehci_device_bulk_start: a request");
   3576 #endif
   3577 
   3578 	mutex_enter(&sc->sc_lock);
   3579 
   3580 	len = xfer->length;
   3581 	endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
   3582 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3583 	sqh = epipe->sqh;
   3584 
   3585 	epipe->u.bulk.length = len;
   3586 
   3587 	err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
   3588 				   &dataend);
   3589 	if (err) {
   3590 		DPRINTFN(-1,("ehci_device_bulk_transfer: no memory\n"));
   3591 		xfer->status = err;
   3592 		usb_transfer_complete(xfer);
   3593 		mutex_exit(&sc->sc_lock);
   3594 		return (err);
   3595 	}
   3596 
   3597 #ifdef EHCI_DEBUG
   3598 	if (ehcidebug > 5) {
   3599 		DPRINTF(("ehci_device_bulk_start: data(1)\n"));
   3600 		ehci_dump_sqh(sqh);
   3601 		ehci_dump_sqtds(data);
   3602 	}
   3603 #endif
   3604 
   3605 	/* Set up interrupt info. */
   3606 	exfer->sqtdstart = data;
   3607 	exfer->sqtdend = dataend;
   3608 #ifdef DIAGNOSTIC
   3609 	if (!exfer->isdone) {
   3610 		printf("ehci_device_bulk_start: not done, ex=%p\n", exfer);
   3611 	}
   3612 	exfer->isdone = 0;
   3613 #endif
   3614 
   3615 	ehci_set_qh_qtd(sqh, data); /* also does usb_syncmem(sqh) */
   3616 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   3617 		callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
   3618 		    ehci_timeout, xfer);
   3619 	}
   3620 	ehci_add_intr_list(sc, exfer);
   3621 	xfer->status = USBD_IN_PROGRESS;
   3622 	mutex_exit(&sc->sc_lock);
   3623 
   3624 #ifdef EHCI_DEBUG
   3625 	if (ehcidebug > 10) {
   3626 		DPRINTF(("ehci_device_bulk_start: data(2)\n"));
   3627 		delay(10000);
   3628 		DPRINTF(("ehci_device_bulk_start: data(3)\n"));
   3629 		ehci_dump_regs(sc);
   3630 #if 0
   3631 		printf("async_head:\n");
   3632 		ehci_dump_sqh(sc->sc_async_head);
   3633 #endif
   3634 		printf("sqh:\n");
   3635 		ehci_dump_sqh(sqh);
   3636 		ehci_dump_sqtds(data);
   3637 	}
   3638 #endif
   3639 
   3640 	if (sc->sc_bus.use_polling)
   3641 		ehci_waitintr(sc, xfer);
   3642 
   3643 	return (USBD_IN_PROGRESS);
   3644 #undef exfer
   3645 }
   3646 
   3647 Static void
   3648 ehci_device_bulk_abort(usbd_xfer_handle xfer)
   3649 {
   3650 	DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer));
   3651 	ehci_abort_xfer(xfer, USBD_CANCELLED);
   3652 }
   3653 
   3654 /*
   3655  * Close a device bulk pipe.
   3656  */
   3657 Static void
   3658 ehci_device_bulk_close(usbd_pipe_handle pipe)
   3659 {
   3660 	ehci_softc_t *sc = pipe->device->bus->hci_private;
   3661 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   3662 
   3663 	KASSERT(mutex_owned(&sc->sc_lock));
   3664 
   3665 	DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe));
   3666 	pipe->endpoint->datatoggle = epipe->nexttoggle;
   3667 	ehci_close_pipe(pipe, sc->sc_async_head);
   3668 }
   3669 
   3670 Static void
   3671 ehci_device_bulk_done(usbd_xfer_handle xfer)
   3672 {
   3673 	struct ehci_xfer *ex = EXFER(xfer);
   3674 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   3675 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   3676 	int endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
   3677 	int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
   3678 
   3679 	DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
   3680 		     xfer, xfer->actlen));
   3681 
   3682 	KASSERT(mutex_owned(&sc->sc_lock));
   3683 
   3684 	if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
   3685 		ehci_del_intr_list(sc, ex);	/* remove from active list */
   3686 		ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
   3687 		usb_syncmem(&xfer->dmabuf, 0, xfer->length,
   3688 		    rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   3689 	}
   3690 
   3691 	DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen));
   3692 }
   3693 
   3694 /************************/
   3695 
   3696 Static usbd_status
   3697 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival)
   3698 {
   3699 	struct ehci_soft_islot *isp;
   3700 	int islot, lev;
   3701 
   3702 	/* Find a poll rate that is large enough. */
   3703 	for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--)
   3704 		if (EHCI_ILEV_IVAL(lev) <= ival)
   3705 			break;
   3706 
   3707 	/* Pick an interrupt slot at the right level. */
   3708 	/* XXX could do better than picking at random */
   3709 	sc->sc_rand = (sc->sc_rand + 191) % sc->sc_flsize;
   3710 	islot = EHCI_IQHIDX(lev, sc->sc_rand);
   3711 
   3712 	sqh->islot = islot;
   3713 	isp = &sc->sc_islots[islot];
   3714 	mutex_enter(&sc->sc_lock);
   3715 	ehci_add_qh(sc, sqh, isp->sqh);
   3716 	mutex_exit(&sc->sc_lock);
   3717 
   3718 	return (USBD_NORMAL_COMPLETION);
   3719 }
   3720 
   3721 Static usbd_status
   3722 ehci_device_intr_transfer(usbd_xfer_handle xfer)
   3723 {
   3724 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   3725 	usbd_status err;
   3726 
   3727 	/* Insert last in queue. */
   3728 	mutex_enter(&sc->sc_lock);
   3729 	err = usb_insert_transfer(xfer);
   3730 	mutex_exit(&sc->sc_lock);
   3731 	if (err)
   3732 		return (err);
   3733 
   3734 	/*
   3735 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   3736 	 * so start it first.
   3737 	 */
   3738 	return (ehci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   3739 }
   3740 
   3741 Static usbd_status
   3742 ehci_device_intr_start(usbd_xfer_handle xfer)
   3743 {
   3744 #define exfer EXFER(xfer)
   3745 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   3746 	usbd_device_handle dev = xfer->pipe->device;
   3747 	ehci_softc_t *sc = dev->bus->hci_private;
   3748 	ehci_soft_qtd_t *data, *dataend;
   3749 	ehci_soft_qh_t *sqh;
   3750 	usbd_status err;
   3751 	int len, isread, endpt;
   3752 
   3753 	DPRINTFN(2, ("ehci_device_intr_start: xfer=%p len=%d flags=%d\n",
   3754 	    xfer, xfer->length, xfer->flags));
   3755 
   3756 	if (sc->sc_dying)
   3757 		return (USBD_IOERROR);
   3758 
   3759 #ifdef DIAGNOSTIC
   3760 	if (xfer->rqflags & URQ_REQUEST)
   3761 		panic("ehci_device_intr_start: a request");
   3762 #endif
   3763 
   3764 	mutex_enter(&sc->sc_lock);
   3765 
   3766 	len = xfer->length;
   3767 	endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
   3768 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3769 	sqh = epipe->sqh;
   3770 
   3771 	epipe->u.intr.length = len;
   3772 
   3773 	err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
   3774 	    &dataend);
   3775 	if (err) {
   3776 		DPRINTFN(-1, ("ehci_device_intr_start: no memory\n"));
   3777 		xfer->status = err;
   3778 		usb_transfer_complete(xfer);
   3779 		mutex_exit(&sc->sc_lock);
   3780 		return (err);
   3781 	}
   3782 
   3783 #ifdef EHCI_DEBUG
   3784 	if (ehcidebug > 5) {
   3785 		DPRINTF(("ehci_device_intr_start: data(1)\n"));
   3786 		ehci_dump_sqh(sqh);
   3787 		ehci_dump_sqtds(data);
   3788 	}
   3789 #endif
   3790 
   3791 	/* Set up interrupt info. */
   3792 	exfer->sqtdstart = data;
   3793 	exfer->sqtdend = dataend;
   3794 #ifdef DIAGNOSTIC
   3795 	if (!exfer->isdone) {
   3796 		printf("ehci_device_intr_start: not done, ex=%p\n", exfer);
   3797 	}
   3798 	exfer->isdone = 0;
   3799 #endif
   3800 
   3801 	ehci_set_qh_qtd(sqh, data); /* also does usb_syncmem(sqh) */
   3802 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   3803 		callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
   3804 		    ehci_timeout, xfer);
   3805 	}
   3806 	ehci_add_intr_list(sc, exfer);
   3807 	xfer->status = USBD_IN_PROGRESS;
   3808 	mutex_exit(&sc->sc_lock);
   3809 
   3810 #ifdef EHCI_DEBUG
   3811 	if (ehcidebug > 10) {
   3812 		DPRINTF(("ehci_device_intr_start: data(2)\n"));
   3813 		delay(10000);
   3814 		DPRINTF(("ehci_device_intr_start: data(3)\n"));
   3815 		ehci_dump_regs(sc);
   3816 		printf("sqh:\n");
   3817 		ehci_dump_sqh(sqh);
   3818 		ehci_dump_sqtds(data);
   3819 	}
   3820 #endif
   3821 
   3822 	if (sc->sc_bus.use_polling)
   3823 		ehci_waitintr(sc, xfer);
   3824 
   3825 	return (USBD_IN_PROGRESS);
   3826 #undef exfer
   3827 }
   3828 
   3829 Static void
   3830 ehci_device_intr_abort(usbd_xfer_handle xfer)
   3831 {
   3832 	DPRINTFN(1, ("ehci_device_intr_abort: xfer=%p\n", xfer));
   3833 	if (xfer->pipe->intrxfer == xfer) {
   3834 		DPRINTFN(1, ("echi_device_intr_abort: remove\n"));
   3835 		xfer->pipe->intrxfer = NULL;
   3836 	}
   3837 	/*
   3838 	 * XXX - abort_xfer uses ehci_sync_hc, which syncs via the advance
   3839 	 *       async doorbell. That's dependent on the async list, wheras
   3840 	 *       intr xfers are periodic, should not use this?
   3841 	 */
   3842 	ehci_abort_xfer(xfer, USBD_CANCELLED);
   3843 }
   3844 
   3845 Static void
   3846 ehci_device_intr_close(usbd_pipe_handle pipe)
   3847 {
   3848 	ehci_softc_t *sc = pipe->device->bus->hci_private;
   3849 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   3850 	struct ehci_soft_islot *isp;
   3851 
   3852 	KASSERT(mutex_owned(&sc->sc_lock));
   3853 
   3854 	isp = &sc->sc_islots[epipe->sqh->islot];
   3855 	ehci_close_pipe(pipe, isp->sqh);
   3856 }
   3857 
   3858 Static void
   3859 ehci_device_intr_done(usbd_xfer_handle xfer)
   3860 {
   3861 #define exfer EXFER(xfer)
   3862 	struct ehci_xfer *ex = EXFER(xfer);
   3863 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   3864 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   3865 	ehci_soft_qtd_t *data, *dataend;
   3866 	ehci_soft_qh_t *sqh;
   3867 	usbd_status err;
   3868 	int len, isread, endpt;
   3869 
   3870 	DPRINTFN(10, ("ehci_device_intr_done: xfer=%p, actlen=%d\n",
   3871 	    xfer, xfer->actlen));
   3872 
   3873 	KASSERT(mutex_owned(&sc->sc_lock));
   3874 
   3875 	if (xfer->pipe->repeat) {
   3876 		ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
   3877 
   3878 		len = epipe->u.intr.length;
   3879 		xfer->length = len;
   3880 		endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
   3881 		isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3882 		usb_syncmem(&xfer->dmabuf, 0, len,
   3883 		    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   3884 		sqh = epipe->sqh;
   3885 
   3886 		err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
   3887 		    &data, &dataend);
   3888 		if (err) {
   3889 			DPRINTFN(-1, ("ehci_device_intr_done: no memory\n"));
   3890 			xfer->status = err;
   3891 			return;
   3892 		}
   3893 
   3894 		/* Set up interrupt info. */
   3895 		exfer->sqtdstart = data;
   3896 		exfer->sqtdend = dataend;
   3897 #ifdef DIAGNOSTIC
   3898 		if (!exfer->isdone) {
   3899 			printf("ehci_device_intr_done: not done, ex=%p\n",
   3900 			    exfer);
   3901 		}
   3902 		exfer->isdone = 0;
   3903 #endif
   3904 
   3905 		ehci_set_qh_qtd(sqh, data); /* also does usb_syncmem(sqh) */
   3906 		if (xfer->timeout && !sc->sc_bus.use_polling) {
   3907 			callout_reset(&xfer->timeout_handle,
   3908 			    mstohz(xfer->timeout), ehci_timeout, xfer);
   3909 		}
   3910 
   3911 		xfer->status = USBD_IN_PROGRESS;
   3912 	} else if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
   3913 		ehci_del_intr_list(sc, ex); /* remove from active list */
   3914 		ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
   3915 		endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
   3916 		isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3917 		usb_syncmem(&xfer->dmabuf, 0, xfer->length,
   3918 		    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   3919 	}
   3920 #undef exfer
   3921 }
   3922 
   3923 /************************/
   3924 
   3925 Static usbd_status
   3926 ehci_device_isoc_transfer(usbd_xfer_handle xfer)
   3927 {
   3928 	ehci_softc_t *sc = xfer->pipe->device->bus->hci_private;
   3929 	usbd_status err;
   3930 
   3931 	mutex_enter(&sc->sc_lock);
   3932 	err = usb_insert_transfer(xfer);
   3933 	mutex_exit(&sc->sc_lock);
   3934 	if (err && err != USBD_IN_PROGRESS)
   3935 		return err;
   3936 
   3937 	return ehci_device_isoc_start(xfer);
   3938 }
   3939 
   3940 Static usbd_status
   3941 ehci_device_isoc_start(usbd_xfer_handle xfer)
   3942 {
   3943 	struct ehci_pipe *epipe;
   3944 	usbd_device_handle dev;
   3945 	ehci_softc_t *sc;
   3946 	struct ehci_xfer *exfer;
   3947 	ehci_soft_itd_t *itd, *prev, *start, *stop;
   3948 	usb_dma_t *dma_buf;
   3949 	int i, j, k, frames, uframes, ufrperframe;
   3950 	int trans_count, offs, total_length;
   3951 	int frindex;
   3952 
   3953 	start = NULL;
   3954 	prev = NULL;
   3955 	itd = NULL;
   3956 	trans_count = 0;
   3957 	total_length = 0;
   3958 	exfer = (struct ehci_xfer *) xfer;
   3959 	sc = xfer->pipe->device->bus->hci_private;
   3960 	dev = xfer->pipe->device;
   3961 	epipe = (struct ehci_pipe *)xfer->pipe;
   3962 
   3963 	/*
   3964 	 * To allow continuous transfers, above we start all transfers
   3965 	 * immediately. However, we're still going to get usbd_start_next call
   3966 	 * this when another xfer completes. So, check if this is already
   3967 	 * in progress or not
   3968 	 */
   3969 
   3970 	if (exfer->itdstart != NULL)
   3971 		return USBD_IN_PROGRESS;
   3972 
   3973 	DPRINTFN(2, ("ehci_device_isoc_start: xfer %p len %d flags %d\n",
   3974 			xfer, xfer->length, xfer->flags));
   3975 
   3976 	if (sc->sc_dying)
   3977 		return USBD_IOERROR;
   3978 
   3979 	/*
   3980 	 * To avoid complication, don't allow a request right now that'll span
   3981 	 * the entire frame table. To within 4 frames, to allow some leeway
   3982 	 * on either side of where the hc currently is.
   3983 	 */
   3984 	if ((1 << (epipe->pipe.endpoint->edesc->bInterval)) *
   3985 			xfer->nframes >= (sc->sc_flsize - 4) * 8) {
   3986 		printf("ehci: isoc descriptor requested that spans the entire frametable, too many frames\n");
   3987 		return USBD_INVAL;
   3988 	}
   3989 
   3990 #ifdef DIAGNOSTIC
   3991 	if (xfer->rqflags & URQ_REQUEST)
   3992 		panic("ehci_device_isoc_start: request\n");
   3993 
   3994 	if (!exfer->isdone)
   3995 		printf("ehci_device_isoc_start: not done, ex = %p\n", exfer);
   3996 	exfer->isdone = 0;
   3997 #endif
   3998 
   3999 	/*
   4000 	 * Step 1: Allocate and initialize itds, how many do we need?
   4001 	 * One per transfer if interval >= 8 microframes, fewer if we use
   4002 	 * multiple microframes per frame.
   4003 	 */
   4004 
   4005 	i = epipe->pipe.endpoint->edesc->bInterval;
   4006 	if (i > 16 || i == 0) {
   4007 		/* Spec page 271 says intervals > 16 are invalid */
   4008 		DPRINTF(("ehci_device_isoc_start: bInvertal %d invalid\n", i));
   4009 		return USBD_INVAL;
   4010 	}
   4011 
   4012 	ufrperframe = max(1, USB_UFRAMES_PER_FRAME / (1 << (i - 1)));
   4013 	frames = (xfer->nframes + (ufrperframe - 1)) / ufrperframe;
   4014 	uframes = USB_UFRAMES_PER_FRAME / ufrperframe;
   4015 
   4016 	if (frames == 0) {
   4017 		DPRINTF(("ehci_device_isoc_start: frames == 0\n"));
   4018 		return USBD_INVAL;
   4019 	}
   4020 
   4021 	dma_buf = &xfer->dmabuf;
   4022 	offs = 0;
   4023 
   4024 	for (i = 0; i < frames; i++) {
   4025 		int froffs = offs;
   4026 		itd = ehci_alloc_itd(sc);
   4027 
   4028 		if (prev != NULL) {
   4029 			prev->itd.itd_next =
   4030 			    htole32(itd->physaddr | EHCI_LINK_ITD);
   4031 			usb_syncmem(&itd->dma,
   4032 			    itd->offs + offsetof(ehci_itd_t, itd_next),
   4033                 	    sizeof(itd->itd.itd_next), BUS_DMASYNC_POSTWRITE);
   4034 
   4035 			prev->xfer_next = itd;
   4036 	    	} else {
   4037 			start = itd;
   4038 		}
   4039 
   4040 		/*
   4041 		 * Step 1.5, initialize uframes
   4042 		 */
   4043 		for (j = 0; j < EHCI_ITD_NUFRAMES; j += uframes) {
   4044 			/* Calculate which page in the list this starts in */
   4045 			int addr = DMAADDR(dma_buf, froffs);
   4046 			addr = EHCI_PAGE_OFFSET(addr);
   4047 			addr += (offs - froffs);
   4048 			addr = EHCI_PAGE(addr);
   4049 			addr /= EHCI_PAGE_SIZE;
   4050 
   4051 			/* This gets the initial offset into the first page,
   4052 			 * looks how far further along the current uframe
   4053 			 * offset is. Works out how many pages that is.
   4054 			 */
   4055 
   4056 			itd->itd.itd_ctl[j] = htole32 ( EHCI_ITD_ACTIVE |
   4057 			    EHCI_ITD_SET_LEN(xfer->frlengths[trans_count]) |
   4058 			    EHCI_ITD_SET_PG(addr) |
   4059 			    EHCI_ITD_SET_OFFS(EHCI_PAGE_OFFSET(DMAADDR(dma_buf,offs))));
   4060 
   4061 			total_length += xfer->frlengths[trans_count];
   4062 			offs += xfer->frlengths[trans_count];
   4063 			trans_count++;
   4064 
   4065 			if (trans_count >= xfer->nframes) { /*Set IOC*/
   4066 				itd->itd.itd_ctl[j] |= htole32(EHCI_ITD_IOC);
   4067 				break;
   4068 			}
   4069 		}
   4070 
   4071 		/* Step 1.75, set buffer pointers. To simplify matters, all
   4072 		 * pointers are filled out for the next 7 hardware pages in
   4073 		 * the dma block, so no need to worry what pages to cover
   4074 		 * and what to not.
   4075 		 */
   4076 
   4077 		for (j = 0; j < EHCI_ITD_NBUFFERS; j++) {
   4078 			/*
   4079 			 * Don't try to lookup a page that's past the end
   4080 			 * of buffer
   4081 			 */
   4082 			int page_offs = EHCI_PAGE(froffs + (EHCI_PAGE_SIZE * j));
   4083 			if (page_offs >= dma_buf->block->size)
   4084 				break;
   4085 
   4086 			unsigned long long page = DMAADDR(dma_buf, page_offs);
   4087 			page = EHCI_PAGE(page);
   4088 			itd->itd.itd_bufr[j] =
   4089 			    htole32(EHCI_ITD_SET_BPTR(page));
   4090 			itd->itd.itd_bufr_hi[j] =
   4091 			    htole32(page >> 32);
   4092 		}
   4093 
   4094 		/*
   4095 		 * Other special values
   4096 		 */
   4097 
   4098 		k = epipe->pipe.endpoint->edesc->bEndpointAddress;
   4099 		itd->itd.itd_bufr[0] |= htole32(EHCI_ITD_SET_EP(UE_GET_ADDR(k)) |
   4100 		    EHCI_ITD_SET_DADDR(epipe->pipe.device->address));
   4101 
   4102 		k = (UE_GET_DIR(epipe->pipe.endpoint->edesc->bEndpointAddress))
   4103 		    ? 1 : 0;
   4104 		j = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize);
   4105 		itd->itd.itd_bufr[1] |= htole32(EHCI_ITD_SET_DIR(k) |
   4106 		    EHCI_ITD_SET_MAXPKT(UE_GET_SIZE(j)));
   4107 
   4108 		/* FIXME: handle invalid trans */
   4109 		itd->itd.itd_bufr[2] |=
   4110 		    htole32(EHCI_ITD_SET_MULTI(UE_GET_TRANS(j)+1));
   4111 
   4112 		usb_syncmem(&itd->dma,
   4113 		    itd->offs + offsetof(ehci_itd_t, itd_next),
   4114                     sizeof(ehci_itd_t),
   4115 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   4116 
   4117 		prev = itd;
   4118 	} /* End of frame */
   4119 
   4120 	stop = itd;
   4121 	stop->xfer_next = NULL;
   4122 	exfer->isoc_len = total_length;
   4123 
   4124 	usb_syncmem(&exfer->xfer.dmabuf, 0, total_length,
   4125 		BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   4126 
   4127 	/*
   4128 	 * Part 2: Transfer descriptors have now been set up, now they must
   4129 	 * be scheduled into the period frame list. Erk. Not wanting to
   4130 	 * complicate matters, transfer is denied if the transfer spans
   4131 	 * more than the period frame list.
   4132 	 */
   4133 
   4134 	mutex_enter(&sc->sc_lock);
   4135 
   4136 	/* Start inserting frames */
   4137 	if (epipe->u.isoc.cur_xfers > 0) {
   4138 		frindex = epipe->u.isoc.next_frame;
   4139 	} else {
   4140 		frindex = EOREAD4(sc, EHCI_FRINDEX);
   4141 		frindex = frindex >> 3; /* Erase microframe index */
   4142 		frindex += 2;
   4143 	}
   4144 
   4145 	if (frindex >= sc->sc_flsize)
   4146 		frindex &= (sc->sc_flsize - 1);
   4147 
   4148 	/* What's the frame interval? */
   4149 	i = (1 << (epipe->pipe.endpoint->edesc->bInterval - 1));
   4150 	if (i / USB_UFRAMES_PER_FRAME == 0)
   4151 		i = 1;
   4152 	else
   4153 		i /= USB_UFRAMES_PER_FRAME;
   4154 
   4155 	itd = start;
   4156 	for (j = 0; j < frames; j++) {
   4157 		if (itd == NULL)
   4158 			panic("ehci: unexpectedly ran out of isoc itds, isoc_start\n");
   4159 
   4160 		itd->itd.itd_next = sc->sc_flist[frindex];
   4161 		if (itd->itd.itd_next == 0)
   4162 			/* FIXME: frindex table gets initialized to NULL
   4163 			 * or EHCI_NULL? */
   4164 			itd->itd.itd_next = EHCI_NULL;
   4165 
   4166 		usb_syncmem(&itd->dma,
   4167 		    itd->offs + offsetof(ehci_itd_t, itd_next),
   4168                     sizeof(itd->itd.itd_next),
   4169 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   4170 
   4171 		sc->sc_flist[frindex] = htole32(EHCI_LINK_ITD | itd->physaddr);
   4172 
   4173 		usb_syncmem(&sc->sc_fldma,
   4174 		    sizeof(ehci_link_t) * frindex,
   4175                     sizeof(ehci_link_t),
   4176 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   4177 
   4178 		itd->u.frame_list.next = sc->sc_softitds[frindex];
   4179 		sc->sc_softitds[frindex] = itd;
   4180 		if (itd->u.frame_list.next != NULL)
   4181 			itd->u.frame_list.next->u.frame_list.prev = itd;
   4182 		itd->slot = frindex;
   4183 		itd->u.frame_list.prev = NULL;
   4184 
   4185 		frindex += i;
   4186 		if (frindex >= sc->sc_flsize)
   4187 			frindex -= sc->sc_flsize;
   4188 
   4189 		itd = itd->xfer_next;
   4190 	}
   4191 
   4192 	epipe->u.isoc.cur_xfers++;
   4193 	epipe->u.isoc.next_frame = frindex;
   4194 
   4195 	exfer->itdstart = start;
   4196 	exfer->itdend = stop;
   4197 	exfer->sqtdstart = NULL;
   4198 	exfer->sqtdstart = NULL;
   4199 
   4200 	ehci_add_intr_list(sc, exfer);
   4201 	xfer->status = USBD_IN_PROGRESS;
   4202 	xfer->done = 0;
   4203 	mutex_exit(&sc->sc_lock);
   4204 
   4205 	if (sc->sc_bus.use_polling) {
   4206 		printf("Starting ehci isoc xfer with polling. Bad idea?\n");
   4207 		ehci_waitintr(sc, xfer);
   4208 	}
   4209 
   4210 	return USBD_IN_PROGRESS;
   4211 }
   4212 
   4213 Static void
   4214 ehci_device_isoc_abort(usbd_xfer_handle xfer)
   4215 {
   4216 	DPRINTFN(1, ("ehci_device_isoc_abort: xfer = %p\n", xfer));
   4217 	ehci_abort_isoc_xfer(xfer, USBD_CANCELLED);
   4218 }
   4219 
   4220 Static void
   4221 ehci_device_isoc_close(usbd_pipe_handle pipe)
   4222 {
   4223 	DPRINTFN(1, ("ehci_device_isoc_close: nothing in the pipe to free?\n"));
   4224 }
   4225 
   4226 Static void
   4227 ehci_device_isoc_done(usbd_xfer_handle xfer)
   4228 {
   4229 	struct ehci_xfer *exfer;
   4230 	ehci_softc_t *sc;
   4231 	struct ehci_pipe *epipe;
   4232 
   4233 	exfer = EXFER(xfer);
   4234 	sc = xfer->pipe->device->bus->hci_private;
   4235 	epipe = (struct ehci_pipe *) xfer->pipe;
   4236 
   4237 	KASSERT(mutex_owned(&sc->sc_lock));
   4238 
   4239 	epipe->u.isoc.cur_xfers--;
   4240 	if (xfer->status != USBD_NOMEM && ehci_active_intr_list(exfer)) {
   4241 		ehci_del_intr_list(sc, exfer);
   4242 		ehci_rem_free_itd_chain(sc, exfer);
   4243 	}
   4244 
   4245 	usb_syncmem(&xfer->dmabuf, 0, xfer->length, BUS_DMASYNC_POSTWRITE |
   4246                     BUS_DMASYNC_POSTREAD);
   4247 
   4248 }
   4249