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