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