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