Home | History | Annotate | Line # | Download | only in usb
ehci.c revision 1.91.2.7
      1 /*	$NetBSD: ehci.c,v 1.91.2.7 2005/05/07 11:42:23 tron Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004 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) and by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
     41  *
     42  * The EHCI 1.0 spec can be found at
     43  * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
     44  * and the USB 2.0 spec at
     45  * http://www.usb.org/developers/docs/usb_20.zip
     46  *
     47  */
     48 
     49 /*
     50  * TODO:
     51  * 1) hold off explorations by companion controllers until ehci has started.
     52  *
     53  * 2) The EHCI driver lacks support for interrupt isochronous transfers, so
     54  *    devices using them don't work.
     55  *    Interrupt transfers are not difficult, it's just not done.
     56  *
     57  * 3) The meaty part to implement is the support for USB 2.0 hubs.
     58  *    They are quite complicated since the need to be able to do
     59  *    "transaction translation", i.e., converting to/from USB 2 and USB 1.
     60  *    So the hub driver needs to handle and schedule these things, to
     61  *    assign place in frame where different devices get to go. See chapter
     62  *    on hubs in USB 2.0 for details.
     63  *
     64  * 4) command failures are not recovered correctly
     65 */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.91.2.7 2005/05/07 11:42:23 tron Exp $");
     69 
     70 #include "ohci.h"
     71 #include "uhci.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/kernel.h>
     76 #include <sys/malloc.h>
     77 #include <sys/device.h>
     78 #include <sys/select.h>
     79 #include <sys/proc.h>
     80 #include <sys/queue.h>
     81 
     82 #include <machine/bus.h>
     83 #include <machine/endian.h>
     84 
     85 #include <dev/usb/usb.h>
     86 #include <dev/usb/usbdi.h>
     87 #include <dev/usb/usbdivar.h>
     88 #include <dev/usb/usb_mem.h>
     89 #include <dev/usb/usb_quirks.h>
     90 
     91 #include <dev/usb/ehcireg.h>
     92 #include <dev/usb/ehcivar.h>
     93 
     94 #ifdef EHCI_DEBUG
     95 #define DPRINTF(x)	do { if (ehcidebug) printf x; } while(0)
     96 #define DPRINTFN(n,x)	do { if (ehcidebug>(n)) printf x; } while (0)
     97 int ehcidebug = 0;
     98 #ifndef __NetBSD__
     99 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
    100 #endif
    101 #else
    102 #define DPRINTF(x)
    103 #define DPRINTFN(n,x)
    104 #endif
    105 
    106 struct ehci_pipe {
    107 	struct usbd_pipe pipe;
    108 	int nexttoggle;
    109 
    110 	ehci_soft_qh_t *sqh;
    111 	union {
    112 		ehci_soft_qtd_t *qtd;
    113 		/* ehci_soft_itd_t *itd; */
    114 	} tail;
    115 	union {
    116 		/* Control pipe */
    117 		struct {
    118 			usb_dma_t reqdma;
    119 			u_int length;
    120 			/*ehci_soft_qtd_t *setup, *data, *stat;*/
    121 		} ctl;
    122 		/* Interrupt pipe */
    123 		struct {
    124 			u_int length;
    125 		} intr;
    126 		/* Bulk pipe */
    127 		struct {
    128 			u_int length;
    129 		} bulk;
    130 		/* Iso pipe */
    131 		/* XXX */
    132 	} u;
    133 };
    134 
    135 Static void		ehci_shutdown(void *);
    136 Static void		ehci_power(int, void *);
    137 
    138 Static usbd_status	ehci_open(usbd_pipe_handle);
    139 Static void		ehci_poll(struct usbd_bus *);
    140 Static void		ehci_softintr(void *);
    141 Static int		ehci_intr1(ehci_softc_t *);
    142 Static void		ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
    143 Static void		ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
    144 Static void		ehci_idone(struct ehci_xfer *);
    145 Static void		ehci_timeout(void *);
    146 Static void		ehci_timeout_task(void *);
    147 
    148 Static usbd_status	ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
    149 Static void		ehci_freem(struct usbd_bus *, usb_dma_t *);
    150 
    151 Static usbd_xfer_handle	ehci_allocx(struct usbd_bus *);
    152 Static void		ehci_freex(struct usbd_bus *, usbd_xfer_handle);
    153 
    154 Static usbd_status	ehci_root_ctrl_transfer(usbd_xfer_handle);
    155 Static usbd_status	ehci_root_ctrl_start(usbd_xfer_handle);
    156 Static void		ehci_root_ctrl_abort(usbd_xfer_handle);
    157 Static void		ehci_root_ctrl_close(usbd_pipe_handle);
    158 Static void		ehci_root_ctrl_done(usbd_xfer_handle);
    159 
    160 Static usbd_status	ehci_root_intr_transfer(usbd_xfer_handle);
    161 Static usbd_status	ehci_root_intr_start(usbd_xfer_handle);
    162 Static void		ehci_root_intr_abort(usbd_xfer_handle);
    163 Static void		ehci_root_intr_close(usbd_pipe_handle);
    164 Static void		ehci_root_intr_done(usbd_xfer_handle);
    165 
    166 Static usbd_status	ehci_device_ctrl_transfer(usbd_xfer_handle);
    167 Static usbd_status	ehci_device_ctrl_start(usbd_xfer_handle);
    168 Static void		ehci_device_ctrl_abort(usbd_xfer_handle);
    169 Static void		ehci_device_ctrl_close(usbd_pipe_handle);
    170 Static void		ehci_device_ctrl_done(usbd_xfer_handle);
    171 
    172 Static usbd_status	ehci_device_bulk_transfer(usbd_xfer_handle);
    173 Static usbd_status	ehci_device_bulk_start(usbd_xfer_handle);
    174 Static void		ehci_device_bulk_abort(usbd_xfer_handle);
    175 Static void		ehci_device_bulk_close(usbd_pipe_handle);
    176 Static void		ehci_device_bulk_done(usbd_xfer_handle);
    177 
    178 Static usbd_status	ehci_device_intr_transfer(usbd_xfer_handle);
    179 Static usbd_status	ehci_device_intr_start(usbd_xfer_handle);
    180 Static void		ehci_device_intr_abort(usbd_xfer_handle);
    181 Static void		ehci_device_intr_close(usbd_pipe_handle);
    182 Static void		ehci_device_intr_done(usbd_xfer_handle);
    183 
    184 Static usbd_status	ehci_device_isoc_transfer(usbd_xfer_handle);
    185 Static usbd_status	ehci_device_isoc_start(usbd_xfer_handle);
    186 Static void		ehci_device_isoc_abort(usbd_xfer_handle);
    187 Static void		ehci_device_isoc_close(usbd_pipe_handle);
    188 Static void		ehci_device_isoc_done(usbd_xfer_handle);
    189 
    190 Static void		ehci_device_clear_toggle(usbd_pipe_handle pipe);
    191 Static void		ehci_noop(usbd_pipe_handle pipe);
    192 
    193 Static int		ehci_str(usb_string_descriptor_t *, int, char *);
    194 Static void		ehci_pcd(ehci_softc_t *, usbd_xfer_handle);
    195 Static void		ehci_pcd_able(ehci_softc_t *, int);
    196 Static void		ehci_pcd_enable(void *);
    197 Static void		ehci_disown(ehci_softc_t *, int, int);
    198 
    199 Static ehci_soft_qh_t  *ehci_alloc_sqh(ehci_softc_t *);
    200 Static void		ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
    201 
    202 Static ehci_soft_qtd_t  *ehci_alloc_sqtd(ehci_softc_t *);
    203 Static void		ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
    204 Static usbd_status	ehci_alloc_sqtd_chain(struct ehci_pipe *,
    205 			    ehci_softc_t *, int, int, usbd_xfer_handle,
    206 			    ehci_soft_qtd_t **, ehci_soft_qtd_t **);
    207 Static void		ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qtd_t *,
    208 					    ehci_soft_qtd_t *);
    209 
    210 Static usbd_status	ehci_device_request(usbd_xfer_handle xfer);
    211 
    212 Static usbd_status	ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *,
    213 			    int ival);
    214 
    215 Static void		ehci_add_qh(ehci_soft_qh_t *, ehci_soft_qh_t *);
    216 Static void		ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
    217 				    ehci_soft_qh_t *);
    218 Static void		ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *);
    219 Static void		ehci_sync_hc(ehci_softc_t *);
    220 
    221 Static void		ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
    222 Static void		ehci_abort_xfer(usbd_xfer_handle, usbd_status);
    223 
    224 #ifdef EHCI_DEBUG
    225 Static void		ehci_dump_regs(ehci_softc_t *);
    226 Static void		ehci_dump(void);
    227 Static ehci_softc_t 	*theehci;
    228 Static void		ehci_dump_link(ehci_link_t, int);
    229 Static void		ehci_dump_sqtds(ehci_soft_qtd_t *);
    230 Static void		ehci_dump_sqtd(ehci_soft_qtd_t *);
    231 Static void		ehci_dump_qtd(ehci_qtd_t *);
    232 Static void		ehci_dump_sqh(ehci_soft_qh_t *);
    233 #ifdef DIAGNOSTIC
    234 Static void		ehci_dump_exfer(struct ehci_xfer *);
    235 #endif
    236 #endif
    237 
    238 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
    239 
    240 #define EHCI_INTR_ENDPT 1
    241 
    242 #define ehci_add_intr_list(sc, ex) \
    243 	LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ex), inext);
    244 #define ehci_del_intr_list(ex) \
    245 	do { \
    246 		LIST_REMOVE((ex), inext); \
    247 		(ex)->inext.le_prev = NULL; \
    248 	} while (0)
    249 #define ehci_active_intr_list(ex) ((ex)->inext.le_prev != NULL)
    250 
    251 Static struct usbd_bus_methods ehci_bus_methods = {
    252 	ehci_open,
    253 	ehci_softintr,
    254 	ehci_poll,
    255 	ehci_allocm,
    256 	ehci_freem,
    257 	ehci_allocx,
    258 	ehci_freex,
    259 };
    260 
    261 Static struct usbd_pipe_methods ehci_root_ctrl_methods = {
    262 	ehci_root_ctrl_transfer,
    263 	ehci_root_ctrl_start,
    264 	ehci_root_ctrl_abort,
    265 	ehci_root_ctrl_close,
    266 	ehci_noop,
    267 	ehci_root_ctrl_done,
    268 };
    269 
    270 Static struct usbd_pipe_methods ehci_root_intr_methods = {
    271 	ehci_root_intr_transfer,
    272 	ehci_root_intr_start,
    273 	ehci_root_intr_abort,
    274 	ehci_root_intr_close,
    275 	ehci_noop,
    276 	ehci_root_intr_done,
    277 };
    278 
    279 Static struct usbd_pipe_methods ehci_device_ctrl_methods = {
    280 	ehci_device_ctrl_transfer,
    281 	ehci_device_ctrl_start,
    282 	ehci_device_ctrl_abort,
    283 	ehci_device_ctrl_close,
    284 	ehci_noop,
    285 	ehci_device_ctrl_done,
    286 };
    287 
    288 Static struct usbd_pipe_methods ehci_device_intr_methods = {
    289 	ehci_device_intr_transfer,
    290 	ehci_device_intr_start,
    291 	ehci_device_intr_abort,
    292 	ehci_device_intr_close,
    293 	ehci_device_clear_toggle,
    294 	ehci_device_intr_done,
    295 };
    296 
    297 Static struct usbd_pipe_methods ehci_device_bulk_methods = {
    298 	ehci_device_bulk_transfer,
    299 	ehci_device_bulk_start,
    300 	ehci_device_bulk_abort,
    301 	ehci_device_bulk_close,
    302 	ehci_device_clear_toggle,
    303 	ehci_device_bulk_done,
    304 };
    305 
    306 Static struct usbd_pipe_methods ehci_device_isoc_methods = {
    307 	ehci_device_isoc_transfer,
    308 	ehci_device_isoc_start,
    309 	ehci_device_isoc_abort,
    310 	ehci_device_isoc_close,
    311 	ehci_noop,
    312 	ehci_device_isoc_done,
    313 };
    314 
    315 static uint8_t revbits[EHCI_MAX_POLLRATE] = {
    316 0x00,0x40,0x20,0x60,0x10,0x50,0x30,0x70,0x08,0x48,0x28,0x68,0x18,0x58,0x38,0x78,
    317 0x04,0x44,0x24,0x64,0x14,0x54,0x34,0x74,0x0c,0x4c,0x2c,0x6c,0x1c,0x5c,0x3c,0x7c,
    318 0x02,0x42,0x22,0x62,0x12,0x52,0x32,0x72,0x0a,0x4a,0x2a,0x6a,0x1a,0x5a,0x3a,0x7a,
    319 0x06,0x46,0x26,0x66,0x16,0x56,0x36,0x76,0x0e,0x4e,0x2e,0x6e,0x1e,0x5e,0x3e,0x7e,
    320 0x01,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x09,0x49,0x29,0x69,0x19,0x59,0x39,0x79,
    321 0x05,0x45,0x25,0x65,0x15,0x55,0x35,0x75,0x0d,0x4d,0x2d,0x6d,0x1d,0x5d,0x3d,0x7d,
    322 0x03,0x43,0x23,0x63,0x13,0x53,0x33,0x73,0x0b,0x4b,0x2b,0x6b,0x1b,0x5b,0x3b,0x7b,
    323 0x07,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0x0f,0x4f,0x2f,0x6f,0x1f,0x5f,0x3f,0x7f,
    324 };
    325 
    326 usbd_status
    327 ehci_init(ehci_softc_t *sc)
    328 {
    329 	u_int32_t version, sparams, cparams, hcr;
    330 	u_int i;
    331 	usbd_status err;
    332 	ehci_soft_qh_t *sqh;
    333 	u_int ncomp;
    334 
    335 	DPRINTF(("ehci_init: start\n"));
    336 #ifdef EHCI_DEBUG
    337 	theehci = sc;
    338 #endif
    339 
    340 	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
    341 
    342 	version = EREAD2(sc, EHCI_HCIVERSION);
    343 	aprint_normal("%s: EHCI version %x.%x\n", USBDEVNAME(sc->sc_bus.bdev),
    344 	       version >> 8, version & 0xff);
    345 
    346 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
    347 	DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
    348 	sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
    349 	ncomp = EHCI_HCS_N_CC(sparams);
    350 	if (ncomp != sc->sc_ncomp) {
    351 		aprint_error("%s: wrong number of companions (%d != %d)\n",
    352 		       USBDEVNAME(sc->sc_bus.bdev),
    353 		       ncomp, sc->sc_ncomp);
    354 #if NOHCI == 0 || NUHCI == 0
    355 		aprint_error("%s: ohci or uhci probably not configured\n",
    356 			     USBDEVNAME(sc->sc_bus.bdev));
    357 #endif
    358 		if (ncomp < sc->sc_ncomp)
    359 			sc->sc_ncomp = ncomp;
    360 	}
    361 	if (sc->sc_ncomp > 0) {
    362 		aprint_normal("%s: companion controller%s, %d port%s each:",
    363 		    USBDEVNAME(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "",
    364 		    EHCI_HCS_N_PCC(sparams),
    365 		    EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
    366 		for (i = 0; i < sc->sc_ncomp; i++)
    367 			aprint_normal(" %s", USBDEVNAME(sc->sc_comps[i]->bdev));
    368 		aprint_normal("\n");
    369 	}
    370 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
    371 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
    372 	DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
    373 
    374 	if (EHCI_HCC_64BIT(cparams)) {
    375 		/* MUST clear segment register if 64 bit capable. */
    376 		EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
    377 	}
    378 
    379 	sc->sc_bus.usbrev = USBREV_2_0;
    380 
    381 	usb_setup_reserve(sc, &sc->sc_dma_reserve, sc->sc_bus.dmatag,
    382 	    USB_MEM_RESERVE);
    383 
    384 	/* Reset the controller */
    385 	DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
    386 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
    387 	usb_delay_ms(&sc->sc_bus, 1);
    388 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
    389 	for (i = 0; i < 100; i++) {
    390 		usb_delay_ms(&sc->sc_bus, 1);
    391 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
    392 		if (!hcr)
    393 			break;
    394 	}
    395 	if (hcr) {
    396 		aprint_error("%s: reset timeout\n",
    397 		    USBDEVNAME(sc->sc_bus.bdev));
    398 		return (USBD_IOERROR);
    399 	}
    400 
    401 	/* XXX need proper intr scheduling */
    402 	sc->sc_rand = 96;
    403 
    404 	/* frame list size at default, read back what we got and use that */
    405 	switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
    406 	case 0: sc->sc_flsize = 1024; break;
    407 	case 1: sc->sc_flsize = 512; break;
    408 	case 2: sc->sc_flsize = 256; break;
    409 	case 3: return (USBD_IOERROR);
    410 	}
    411 	err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t),
    412 	    EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
    413 	if (err)
    414 		return (err);
    415 	DPRINTF(("%s: flsize=%d\n", USBDEVNAME(sc->sc_bus.bdev),sc->sc_flsize));
    416 	sc->sc_flist = KERNADDR(&sc->sc_fldma, 0);
    417 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
    418 
    419 	/* Set up the bus struct. */
    420 	sc->sc_bus.methods = &ehci_bus_methods;
    421 	sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
    422 
    423 	sc->sc_powerhook = powerhook_establish(ehci_power, sc);
    424 	sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
    425 
    426 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
    427 
    428 	/*
    429 	 * Allocate the interrupt dummy QHs. These are arranged to give poll
    430 	 * intervals that are powers of 2 times 1ms.
    431 	 */
    432 	for (i = 0; i < EHCI_INTRQHS; i++) {
    433 		sqh = ehci_alloc_sqh(sc);
    434 		if (sqh == NULL) {
    435 			err = USBD_NOMEM;
    436 			goto bad1;
    437 		}
    438 		sc->sc_islots[i].sqh = sqh;
    439 	}
    440 	for (i = 0; i < EHCI_INTRQHS; i++) {
    441 		sqh = sc->sc_islots[i].sqh;
    442 		if (i == 0) {
    443 			/* The last (1ms) QH terminates. */
    444 			sqh->qh.qh_link = EHCI_NULL;
    445 			sqh->next = NULL;
    446 		} else {
    447 			/* Otherwise the next QH has half the poll interval */
    448 			sqh->next = sc->sc_islots[(i + 1) / 2 - 1].sqh;
    449 			sqh->qh.qh_link = htole32(sqh->next->physaddr |
    450 			    EHCI_LINK_QH);
    451 		}
    452 		sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
    453 		sqh->qh.qh_curqtd = EHCI_NULL;
    454 		sqh->next = NULL;
    455 		sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
    456 		sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
    457 		sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
    458 		sqh->sqtd = NULL;
    459 	}
    460 	/* Point the frame list at the last level (128ms). */
    461 	for (i = 0; i < sc->sc_flsize; i++) {
    462 		int j;
    463 
    464 		j = (i & ~(EHCI_MAX_POLLRATE-1)) |
    465 		    revbits[i & (EHCI_MAX_POLLRATE-1)];
    466 		sc->sc_flist[j] = htole32(EHCI_LINK_QH |
    467 		    sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES - 1,
    468 		    i)].sqh->physaddr);
    469 	}
    470 
    471 	/* Allocate dummy QH that starts the async list. */
    472 	sqh = ehci_alloc_sqh(sc);
    473 	if (sqh == NULL) {
    474 		err = USBD_NOMEM;
    475 		goto bad1;
    476 	}
    477 	/* Fill the QH */
    478 	sqh->qh.qh_endp =
    479 	    htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
    480 	sqh->qh.qh_link =
    481 	    htole32(sqh->physaddr | EHCI_LINK_QH);
    482 	sqh->qh.qh_curqtd = EHCI_NULL;
    483 	sqh->next = NULL;
    484 	/* Fill the overlay qTD */
    485 	sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
    486 	sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
    487 	sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
    488 	sqh->sqtd = NULL;
    489 #ifdef EHCI_DEBUG
    490 	if (ehcidebug) {
    491 		ehci_dump_sqh(sqh);
    492 	}
    493 #endif
    494 
    495 	/* Point to async list */
    496 	sc->sc_async_head = sqh;
    497 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
    498 
    499 	usb_callout_init(sc->sc_tmo_pcd);
    500 
    501 	lockinit(&sc->sc_doorbell_lock, PZERO, "ehcidb", 0, 0);
    502 
    503 	/* Enable interrupts */
    504 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    505 
    506 	/* Turn on controller */
    507 	EOWRITE4(sc, EHCI_USBCMD,
    508 		 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */
    509 		 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
    510 		 EHCI_CMD_ASE |
    511 		 EHCI_CMD_PSE |
    512 		 EHCI_CMD_RS);
    513 
    514 	/* Take over port ownership */
    515 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
    516 
    517 	for (i = 0; i < 100; i++) {
    518 		usb_delay_ms(&sc->sc_bus, 1);
    519 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
    520 		if (!hcr)
    521 			break;
    522 	}
    523 	if (hcr) {
    524 		aprint_error("%s: run timeout\n", USBDEVNAME(sc->sc_bus.bdev));
    525 		return (USBD_IOERROR);
    526 	}
    527 
    528 	return (USBD_NORMAL_COMPLETION);
    529 
    530 #if 0
    531  bad2:
    532 	ehci_free_sqh(sc, sc->sc_async_head);
    533 #endif
    534  bad1:
    535 	usb_freemem(&sc->sc_bus, &sc->sc_fldma);
    536 	return (err);
    537 }
    538 
    539 int
    540 ehci_intr(void *v)
    541 {
    542 	ehci_softc_t *sc = v;
    543 
    544 	if (sc == NULL || sc->sc_dying)
    545 		return (0);
    546 
    547 	/* If we get an interrupt while polling, then just ignore it. */
    548 	if (sc->sc_bus.use_polling) {
    549 		u_int32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
    550 
    551 		if (intrs)
    552 			EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
    553 #ifdef DIAGNOSTIC
    554 		DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
    555 #endif
    556 		return (0);
    557 	}
    558 
    559 	return (ehci_intr1(sc));
    560 }
    561 
    562 Static int
    563 ehci_intr1(ehci_softc_t *sc)
    564 {
    565 	u_int32_t intrs, eintrs;
    566 
    567 	DPRINTFN(20,("ehci_intr1: enter\n"));
    568 
    569 	/* In case the interrupt occurs before initialization has completed. */
    570 	if (sc == NULL) {
    571 #ifdef DIAGNOSTIC
    572 		printf("ehci_intr1: sc == NULL\n");
    573 #endif
    574 		return (0);
    575 	}
    576 
    577 	intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
    578 	if (!intrs)
    579 		return (0);
    580 
    581 	eintrs = intrs & sc->sc_eintrs;
    582 	DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
    583 		     sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
    584 		     (u_int)eintrs));
    585 	if (!eintrs)
    586 		return (0);
    587 
    588 	EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
    589 	sc->sc_bus.intr_context++;
    590 	sc->sc_bus.no_intrs++;
    591 	if (eintrs & EHCI_STS_IAA) {
    592 		DPRINTF(("ehci_intr1: door bell\n"));
    593 		wakeup(&sc->sc_async_head);
    594 		eintrs &= ~EHCI_STS_IAA;
    595 	}
    596 	if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
    597 		DPRINTFN(5,("ehci_intr1: %s %s\n",
    598 			    eintrs & EHCI_STS_INT ? "INT" : "",
    599 			    eintrs & EHCI_STS_ERRINT ? "ERRINT" : ""));
    600 		usb_schedsoftintr(&sc->sc_bus);
    601 		eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
    602 	}
    603 	if (eintrs & EHCI_STS_HSE) {
    604 		printf("%s: unrecoverable error, controller halted\n",
    605 		       USBDEVNAME(sc->sc_bus.bdev));
    606 		/* XXX what else */
    607 	}
    608 	if (eintrs & EHCI_STS_PCD) {
    609 		ehci_pcd(sc, sc->sc_intrxfer);
    610 		/*
    611 		 * Disable PCD interrupt for now, because it will be
    612 		 * on until the port has been reset.
    613 		 */
    614 		ehci_pcd_able(sc, 0);
    615 		/* Do not allow RHSC interrupts > 1 per second */
    616                 usb_callout(sc->sc_tmo_pcd, hz, ehci_pcd_enable, sc);
    617 		eintrs &= ~EHCI_STS_PCD;
    618 	}
    619 
    620 	sc->sc_bus.intr_context--;
    621 
    622 	if (eintrs != 0) {
    623 		/* Block unprocessed interrupts. */
    624 		sc->sc_eintrs &= ~eintrs;
    625 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    626 		printf("%s: blocking intrs 0x%x\n",
    627 		       USBDEVNAME(sc->sc_bus.bdev), eintrs);
    628 	}
    629 
    630 	return (1);
    631 }
    632 
    633 void
    634 ehci_pcd_able(ehci_softc_t *sc, int on)
    635 {
    636 	DPRINTFN(4, ("ehci_pcd_able: on=%d\n", on));
    637 	if (on)
    638 		sc->sc_eintrs |= EHCI_STS_PCD;
    639 	else
    640 		sc->sc_eintrs &= ~EHCI_STS_PCD;
    641 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    642 }
    643 
    644 void
    645 ehci_pcd_enable(void *v_sc)
    646 {
    647 	ehci_softc_t *sc = v_sc;
    648 
    649 	ehci_pcd_able(sc, 1);
    650 }
    651 
    652 void
    653 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer)
    654 {
    655 	usbd_pipe_handle pipe;
    656 	u_char *p;
    657 	int i, m;
    658 
    659 	if (xfer == NULL) {
    660 		/* Just ignore the change. */
    661 		return;
    662 	}
    663 
    664 	pipe = xfer->pipe;
    665 
    666 	p = KERNADDR(&xfer->dmabuf, 0);
    667 	m = min(sc->sc_noport, xfer->length * 8 - 1);
    668 	memset(p, 0, xfer->length);
    669 	for (i = 1; i <= m; i++) {
    670 		/* Pick out CHANGE bits from the status reg. */
    671 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
    672 			p[i/8] |= 1 << (i%8);
    673 	}
    674 	DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
    675 	xfer->actlen = xfer->length;
    676 	xfer->status = USBD_NORMAL_COMPLETION;
    677 
    678 	usb_transfer_complete(xfer);
    679 }
    680 
    681 void
    682 ehci_softintr(void *v)
    683 {
    684 	ehci_softc_t *sc = v;
    685 	struct ehci_xfer *ex, *nextex;
    686 
    687 	DPRINTFN(10,("%s: ehci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
    688 		     sc->sc_bus.intr_context));
    689 
    690 	sc->sc_bus.intr_context++;
    691 
    692 	/*
    693 	 * The only explanation I can think of for why EHCI is as brain dead
    694 	 * as UHCI interrupt-wise is that Intel was involved in both.
    695 	 * An interrupt just tells us that something is done, we have no
    696 	 * clue what, so we need to scan through all active transfers. :-(
    697 	 */
    698 	for (ex = LIST_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
    699 		nextex = LIST_NEXT(ex, inext);
    700 		ehci_check_intr(sc, ex);
    701 	}
    702 
    703 #ifdef USB_USE_SOFTINTR
    704 	if (sc->sc_softwake) {
    705 		sc->sc_softwake = 0;
    706 		wakeup(&sc->sc_softwake);
    707 	}
    708 #endif /* USB_USE_SOFTINTR */
    709 
    710 	sc->sc_bus.intr_context--;
    711 }
    712 
    713 /* Check for an interrupt. */
    714 void
    715 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
    716 {
    717 	ehci_soft_qtd_t *sqtd, *lsqtd;
    718 	u_int32_t status;
    719 
    720 	DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex));
    721 
    722 	if (ex->sqtdstart == NULL) {
    723 		printf("ehci_check_intr: sqtdstart=NULL\n");
    724 		return;
    725 	}
    726 	lsqtd = ex->sqtdend;
    727 #ifdef DIAGNOSTIC
    728 	if (lsqtd == NULL) {
    729 		printf("ehci_check_intr: lsqtd==0\n");
    730 		return;
    731 	}
    732 #endif
    733 	/*
    734 	 * If the last TD is still active we need to check whether there
    735 	 * is a an error somewhere in the middle, or whether there was a
    736 	 * short packet (SPD and not ACTIVE).
    737 	 */
    738 	if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) {
    739 		DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex));
    740 		for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) {
    741 			status = le32toh(sqtd->qtd.qtd_status);
    742 			/* If there's an active QTD the xfer isn't done. */
    743 			if (status & EHCI_QTD_ACTIVE)
    744 				break;
    745 			/* Any kind of error makes the xfer done. */
    746 			if (status & EHCI_QTD_HALTED)
    747 				goto done;
    748 			/* We want short packets, and it is short: it's done */
    749 			if (EHCI_QTD_GET_BYTES(status) != 0)
    750 				goto done;
    751 		}
    752 		DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
    753 			      ex, ex->sqtdstart));
    754 		return;
    755 	}
    756  done:
    757 	DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex));
    758 	usb_uncallout(ex->xfer.timeout_handle, ehci_timeout, ex);
    759 	ehci_idone(ex);
    760 }
    761 
    762 void
    763 ehci_idone(struct ehci_xfer *ex)
    764 {
    765 	usbd_xfer_handle xfer = &ex->xfer;
    766 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
    767 	ehci_soft_qtd_t *sqtd, *lsqtd;
    768 	u_int32_t status = 0, nstatus = 0;
    769 	int actlen;
    770 	uint pkts_left;
    771 
    772 	DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex));
    773 #ifdef DIAGNOSTIC
    774 	{
    775 		int s = splhigh();
    776 		if (ex->isdone) {
    777 			splx(s);
    778 #ifdef EHCI_DEBUG
    779 			printf("ehci_idone: ex is done!\n   ");
    780 			ehci_dump_exfer(ex);
    781 #else
    782 			printf("ehci_idone: ex=%p is done!\n", ex);
    783 #endif
    784 			return;
    785 		}
    786 		ex->isdone = 1;
    787 		splx(s);
    788 	}
    789 #endif
    790 
    791 	if (xfer->status == USBD_CANCELLED ||
    792 	    xfer->status == USBD_TIMEOUT) {
    793 		DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer));
    794 		return;
    795 	}
    796 
    797 #ifdef EHCI_DEBUG
    798 	DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe));
    799 	if (ehcidebug > 10)
    800 		ehci_dump_sqtds(ex->sqtdstart);
    801 #endif
    802 
    803 	/* The transfer is done, compute actual length and status. */
    804 	lsqtd = ex->sqtdend;
    805 	actlen = 0;
    806 	for (sqtd = ex->sqtdstart; sqtd != lsqtd->nextqtd; sqtd=sqtd->nextqtd) {
    807 		nstatus = le32toh(sqtd->qtd.qtd_status);
    808 		if (nstatus & EHCI_QTD_ACTIVE)
    809 			break;
    810 
    811 		status = nstatus;
    812 		if (EHCI_QTD_GET_PID(status) !=	EHCI_QTD_PID_SETUP)
    813 			actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
    814 	}
    815 
    816 	/*
    817 	 * If there are left over TDs we need to update the toggle.
    818 	 * The default pipe doesn't need it since control transfers
    819 	 * start the toggle at 0 every time.
    820 	 */
    821 	if (sqtd != lsqtd->nextqtd &&
    822 	    xfer->pipe->device->default_pipe != xfer->pipe) {
    823 		printf("ehci_idone: need toggle update status=%08x nstatus=%08x\n", status, nstatus);
    824 #if 0
    825 		ehci_dump_sqh(epipe->sqh);
    826 		ehci_dump_sqtds(ex->sqtdstart);
    827 #endif
    828 		epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus);
    829 	}
    830 
    831 	/*
    832 	 * For a short transfer we need to update the toggle for the missing
    833 	 * packets within the qTD.
    834 	 */
    835 	pkts_left = EHCI_QTD_GET_BYTES(status) /
    836 	    UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize);
    837 	epipe->nexttoggle ^= pkts_left % 2;
    838 
    839 	DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, status=0x%x\n",
    840 			   xfer->length, actlen, status));
    841 	xfer->actlen = actlen;
    842 	if (status & EHCI_QTD_HALTED) {
    843 #ifdef EHCI_DEBUG
    844 		char sbuf[128];
    845 
    846 		bitmask_snprintf((u_int32_t)status,
    847 				 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR"
    848 				 "\3MISSED\1PINGSTATE", sbuf, sizeof(sbuf));
    849 
    850 		DPRINTFN(2, ("ehci_idone: error, addr=%d, endpt=0x%02x, "
    851 			  "status 0x%s\n",
    852 			  xfer->pipe->device->address,
    853 			  xfer->pipe->endpoint->edesc->bEndpointAddress,
    854 			  sbuf));
    855 		if (ehcidebug > 2) {
    856 			ehci_dump_sqh(epipe->sqh);
    857 			ehci_dump_sqtds(ex->sqtdstart);
    858 		}
    859 #endif
    860 		/* low&full speed has an extra error flag */
    861 		if (EHCI_QH_GET_EPS(epipe->sqh->qh.qh_endp) !=
    862 		    EHCI_QH_SPEED_HIGH)
    863 			status &= EHCI_QTD_STATERRS | EHCI_QTD_PINGSTATE;
    864 		else
    865 			status &= EHCI_QTD_STATERRS;
    866 		if (status == 0) /* no other errors means a stall */
    867 			xfer->status = USBD_STALLED;
    868 		else
    869 			xfer->status = USBD_IOERROR; /* more info XXX */
    870 		/* XXX need to reset TT on missed microframe */
    871 		if (status & EHCI_QTD_MISSEDMICRO) {
    872 			ehci_softc_t *sc = (ehci_softc_t *)
    873 			    xfer->pipe->device->bus;
    874 
    875 			printf("%s: missed microframe, TT reset not "
    876 			    "implemented, hub might be inoperational\n",
    877 			    USBDEVNAME(sc->sc_bus.bdev));
    878 		}
    879 	} else {
    880 		xfer->status = USBD_NORMAL_COMPLETION;
    881 	}
    882 
    883 	usb_transfer_complete(xfer);
    884 	DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex));
    885 }
    886 
    887 /*
    888  * Wait here until controller claims to have an interrupt.
    889  * Then call ehci_intr and return.  Use timeout to avoid waiting
    890  * too long.
    891  */
    892 void
    893 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
    894 {
    895 	int timo = xfer->timeout;
    896 	int usecs;
    897 	u_int32_t intrs;
    898 
    899 	xfer->status = USBD_IN_PROGRESS;
    900 	for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
    901 		usb_delay_ms(&sc->sc_bus, 1);
    902 		if (sc->sc_dying)
    903 			break;
    904 		intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
    905 			sc->sc_eintrs;
    906 		DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs));
    907 #ifdef EHCI_DEBUG
    908 		if (ehcidebug > 15)
    909 			ehci_dump_regs(sc);
    910 #endif
    911 		if (intrs) {
    912 			ehci_intr1(sc);
    913 			if (xfer->status != USBD_IN_PROGRESS)
    914 				return;
    915 		}
    916 	}
    917 
    918 	/* Timeout */
    919 	DPRINTF(("ehci_waitintr: timeout\n"));
    920 	xfer->status = USBD_TIMEOUT;
    921 	usb_transfer_complete(xfer);
    922 	/* XXX should free TD */
    923 }
    924 
    925 void
    926 ehci_poll(struct usbd_bus *bus)
    927 {
    928 	ehci_softc_t *sc = (ehci_softc_t *)bus;
    929 #ifdef EHCI_DEBUG
    930 	static int last;
    931 	int new;
    932 	new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
    933 	if (new != last) {
    934 		DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
    935 		last = new;
    936 	}
    937 #endif
    938 
    939 	if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs)
    940 		ehci_intr1(sc);
    941 }
    942 
    943 int
    944 ehci_detach(struct ehci_softc *sc, int flags)
    945 {
    946 	int rv = 0;
    947 
    948 	if (sc->sc_child != NULL)
    949 		rv = config_detach(sc->sc_child, flags);
    950 
    951 	if (rv != 0)
    952 		return (rv);
    953 
    954 	usb_uncallout(sc->sc_tmo_pcd, ehci_pcd_enable, sc);
    955 
    956 	if (sc->sc_powerhook != NULL)
    957 		powerhook_disestablish(sc->sc_powerhook);
    958 	if (sc->sc_shutdownhook != NULL)
    959 		shutdownhook_disestablish(sc->sc_shutdownhook);
    960 
    961 	usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
    962 
    963 	/* XXX free other data structures XXX */
    964 
    965 	return (rv);
    966 }
    967 
    968 
    969 int
    970 ehci_activate(device_ptr_t self, enum devact act)
    971 {
    972 	struct ehci_softc *sc = (struct ehci_softc *)self;
    973 	int rv = 0;
    974 
    975 	switch (act) {
    976 	case DVACT_ACTIVATE:
    977 		return (EOPNOTSUPP);
    978 
    979 	case DVACT_DEACTIVATE:
    980 		if (sc->sc_child != NULL)
    981 			rv = config_deactivate(sc->sc_child);
    982 		sc->sc_dying = 1;
    983 		break;
    984 	}
    985 	return (rv);
    986 }
    987 
    988 /*
    989  * Handle suspend/resume.
    990  *
    991  * We need to switch to polling mode here, because this routine is
    992  * called from an interrupt context.  This is all right since we
    993  * are almost suspended anyway.
    994  */
    995 void
    996 ehci_power(int why, void *v)
    997 {
    998 	ehci_softc_t *sc = v;
    999 	u_int32_t cmd, hcr;
   1000 	int s, i;
   1001 
   1002 #ifdef EHCI_DEBUG
   1003 	DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
   1004 	if (ehcidebug > 0)
   1005 		ehci_dump_regs(sc);
   1006 #endif
   1007 
   1008 	s = splhardusb();
   1009 	switch (why) {
   1010 	case PWR_SUSPEND:
   1011 	case PWR_STANDBY:
   1012 		sc->sc_bus.use_polling++;
   1013 
   1014 		sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
   1015 
   1016 		cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
   1017 		EOWRITE4(sc, EHCI_USBCMD, cmd);
   1018 
   1019 		for (i = 0; i < 100; i++) {
   1020 			hcr = EOREAD4(sc, EHCI_USBSTS) &
   1021 			    (EHCI_STS_ASS | EHCI_STS_PSS);
   1022 			if (hcr == 0)
   1023 				break;
   1024 
   1025 			usb_delay_ms(&sc->sc_bus, 1);
   1026 		}
   1027 		if (hcr != 0) {
   1028 			printf("%s: reset timeout\n",
   1029 			    USBDEVNAME(sc->sc_bus.bdev));
   1030 		}
   1031 
   1032 		cmd &= ~EHCI_CMD_RS;
   1033 		EOWRITE4(sc, EHCI_USBCMD, cmd);
   1034 
   1035 		for (i = 0; i < 100; i++) {
   1036 			hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
   1037 			if (hcr == EHCI_STS_HCH)
   1038 				break;
   1039 
   1040 			usb_delay_ms(&sc->sc_bus, 1);
   1041 		}
   1042 		if (hcr != EHCI_STS_HCH) {
   1043 			printf("%s: config timeout\n",
   1044 			    USBDEVNAME(sc->sc_bus.bdev));
   1045 		}
   1046 
   1047 		sc->sc_bus.use_polling--;
   1048 		break;
   1049 
   1050 	case PWR_RESUME:
   1051 		sc->sc_bus.use_polling++;
   1052 
   1053 		/* restore things in case the bios sucks */
   1054 		EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
   1055 		EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
   1056 		EOWRITE4(sc, EHCI_ASYNCLISTADDR,
   1057 		    sc->sc_async_head->physaddr | EHCI_LINK_QH);
   1058 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
   1059 
   1060 		EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
   1061 
   1062 		for (i = 0; i < 100; i++) {
   1063 			hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
   1064 			if (hcr != EHCI_STS_HCH)
   1065 				break;
   1066 
   1067 			usb_delay_ms(&sc->sc_bus, 1);
   1068 		}
   1069 		if (hcr == EHCI_STS_HCH) {
   1070 			printf("%s: config timeout\n",
   1071 			    USBDEVNAME(sc->sc_bus.bdev));
   1072 		}
   1073 
   1074 		usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
   1075 
   1076 		sc->sc_bus.use_polling--;
   1077 		break;
   1078 	case PWR_SOFTSUSPEND:
   1079 	case PWR_SOFTSTANDBY:
   1080 	case PWR_SOFTRESUME:
   1081 		break;
   1082 	}
   1083 	splx(s);
   1084 
   1085 #ifdef EHCI_DEBUG
   1086 	DPRINTF(("ehci_power: sc=%p\n", sc));
   1087 	if (ehcidebug > 0)
   1088 		ehci_dump_regs(sc);
   1089 #endif
   1090 }
   1091 
   1092 /*
   1093  * Shut down the controller when the system is going down.
   1094  */
   1095 void
   1096 ehci_shutdown(void *v)
   1097 {
   1098 	ehci_softc_t *sc = v;
   1099 
   1100 	DPRINTF(("ehci_shutdown: stopping the HC\n"));
   1101 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
   1102 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
   1103 }
   1104 
   1105 usbd_status
   1106 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
   1107 {
   1108 	struct ehci_softc *sc = (struct ehci_softc *)bus;
   1109 	usbd_status err;
   1110 
   1111 	err = usb_allocmem(&sc->sc_bus, size, 0, dma);
   1112 	if (err == USBD_NOMEM)
   1113 		err = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size);
   1114 #ifdef EHCI_DEBUG
   1115 	if (err)
   1116 		printf("ehci_allocm: usb_allocmem()=%d\n", err);
   1117 #endif
   1118 	return (err);
   1119 }
   1120 
   1121 void
   1122 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
   1123 {
   1124 	struct ehci_softc *sc = (struct ehci_softc *)bus;
   1125 
   1126 	if (dma->block->flags & USB_DMA_RESERVE) {
   1127 		usb_reserve_freem(&((struct ehci_softc *)bus)->sc_dma_reserve,
   1128 		    dma);
   1129 		return;
   1130 	}
   1131 	usb_freemem(&sc->sc_bus, dma);
   1132 }
   1133 
   1134 usbd_xfer_handle
   1135 ehci_allocx(struct usbd_bus *bus)
   1136 {
   1137 	struct ehci_softc *sc = (struct ehci_softc *)bus;
   1138 	usbd_xfer_handle xfer;
   1139 
   1140 	xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
   1141 	if (xfer != NULL) {
   1142 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
   1143 #ifdef DIAGNOSTIC
   1144 		if (xfer->busy_free != XFER_FREE) {
   1145 			printf("ehci_allocx: xfer=%p not free, 0x%08x\n", xfer,
   1146 			       xfer->busy_free);
   1147 		}
   1148 #endif
   1149 	} else {
   1150 		xfer = malloc(sizeof(struct ehci_xfer), M_USB, M_NOWAIT);
   1151 	}
   1152 	if (xfer != NULL) {
   1153 		memset(xfer, 0, sizeof(struct ehci_xfer));
   1154 #ifdef DIAGNOSTIC
   1155 		EXFER(xfer)->isdone = 1;
   1156 		xfer->busy_free = XFER_BUSY;
   1157 #endif
   1158 	}
   1159 	return (xfer);
   1160 }
   1161 
   1162 void
   1163 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
   1164 {
   1165 	struct ehci_softc *sc = (struct ehci_softc *)bus;
   1166 
   1167 #ifdef DIAGNOSTIC
   1168 	if (xfer->busy_free != XFER_BUSY) {
   1169 		printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
   1170 		       xfer->busy_free);
   1171 		return;
   1172 	}
   1173 	xfer->busy_free = XFER_FREE;
   1174 	if (!EXFER(xfer)->isdone) {
   1175 		printf("ehci_freex: !isdone\n");
   1176 		return;
   1177 	}
   1178 #endif
   1179 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
   1180 }
   1181 
   1182 Static void
   1183 ehci_device_clear_toggle(usbd_pipe_handle pipe)
   1184 {
   1185 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   1186 
   1187 	DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
   1188 		 epipe, epipe->sqh->qh.qh_qtd.qtd_status));
   1189 #ifdef USB_DEBUG
   1190 	if (ehcidebug)
   1191 		usbd_dump_pipe(pipe);
   1192 #endif
   1193 	epipe->nexttoggle = 0;
   1194 }
   1195 
   1196 Static void
   1197 ehci_noop(usbd_pipe_handle pipe)
   1198 {
   1199 }
   1200 
   1201 #ifdef EHCI_DEBUG
   1202 void
   1203 ehci_dump_regs(ehci_softc_t *sc)
   1204 {
   1205 	int i;
   1206 	printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
   1207 	       EOREAD4(sc, EHCI_USBCMD),
   1208 	       EOREAD4(sc, EHCI_USBSTS),
   1209 	       EOREAD4(sc, EHCI_USBINTR));
   1210 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
   1211 	       EOREAD4(sc, EHCI_FRINDEX),
   1212 	       EOREAD4(sc, EHCI_CTRLDSSEGMENT),
   1213 	       EOREAD4(sc, EHCI_PERIODICLISTBASE),
   1214 	       EOREAD4(sc, EHCI_ASYNCLISTADDR));
   1215 	for (i = 1; i <= sc->sc_noport; i++)
   1216 		printf("port %d status=0x%08x\n", i,
   1217 		       EOREAD4(sc, EHCI_PORTSC(i)));
   1218 }
   1219 
   1220 /*
   1221  * Unused function - this is meant to be called from a kernel
   1222  * debugger.
   1223  */
   1224 void
   1225 ehci_dump()
   1226 {
   1227 	ehci_dump_regs(theehci);
   1228 }
   1229 
   1230 void
   1231 ehci_dump_link(ehci_link_t link, int type)
   1232 {
   1233 	link = le32toh(link);
   1234 	printf("0x%08x", link);
   1235 	if (link & EHCI_LINK_TERMINATE)
   1236 		printf("<T>");
   1237 	else {
   1238 		printf("<");
   1239 		if (type) {
   1240 			switch (EHCI_LINK_TYPE(link)) {
   1241 			case EHCI_LINK_ITD: printf("ITD"); break;
   1242 			case EHCI_LINK_QH: printf("QH"); break;
   1243 			case EHCI_LINK_SITD: printf("SITD"); break;
   1244 			case EHCI_LINK_FSTN: printf("FSTN"); break;
   1245 			}
   1246 		}
   1247 		printf(">");
   1248 	}
   1249 }
   1250 
   1251 void
   1252 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
   1253 {
   1254 	int i;
   1255 	u_int32_t stop;
   1256 
   1257 	stop = 0;
   1258 	for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
   1259 		ehci_dump_sqtd(sqtd);
   1260 		stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE);
   1261 	}
   1262 	if (sqtd)
   1263 		printf("dump aborted, too many TDs\n");
   1264 }
   1265 
   1266 void
   1267 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
   1268 {
   1269 	printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr);
   1270 	ehci_dump_qtd(&sqtd->qtd);
   1271 }
   1272 
   1273 void
   1274 ehci_dump_qtd(ehci_qtd_t *qtd)
   1275 {
   1276 	u_int32_t s;
   1277 	char sbuf[128];
   1278 
   1279 	printf("  next="); ehci_dump_link(qtd->qtd_next, 0);
   1280 	printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0);
   1281 	printf("\n");
   1282 	s = le32toh(qtd->qtd_status);
   1283 	bitmask_snprintf(EHCI_QTD_GET_STATUS(s),
   1284 			 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
   1285 			 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
   1286 	printf("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
   1287 	       s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
   1288 	       EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
   1289 	printf("    cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s),
   1290 	       EHCI_QTD_GET_PID(s), sbuf);
   1291 	for (s = 0; s < 5; s++)
   1292 		printf("  buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s]));
   1293 }
   1294 
   1295 void
   1296 ehci_dump_sqh(ehci_soft_qh_t *sqh)
   1297 {
   1298 	ehci_qh_t *qh = &sqh->qh;
   1299 	u_int32_t endp, endphub;
   1300 
   1301 	printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr);
   1302 	printf("  link="); ehci_dump_link(qh->qh_link, 1); printf("\n");
   1303 	endp = le32toh(qh->qh_endp);
   1304 	printf("  endp=0x%08x\n", endp);
   1305 	printf("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
   1306 	       EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
   1307 	       EHCI_QH_GET_ENDPT(endp),  EHCI_QH_GET_EPS(endp),
   1308 	       EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
   1309 	printf("    mpl=0x%x ctl=%d nrl=%d\n",
   1310 	       EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
   1311 	       EHCI_QH_GET_NRL(endp));
   1312 	endphub = le32toh(qh->qh_endphub);
   1313 	printf("  endphub=0x%08x\n", endphub);
   1314 	printf("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
   1315 	       EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
   1316 	       EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
   1317 	       EHCI_QH_GET_MULT(endphub));
   1318 	printf("  curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n");
   1319 	printf("Overlay qTD:\n");
   1320 	ehci_dump_qtd(&qh->qh_qtd);
   1321 }
   1322 
   1323 #ifdef DIAGNOSTIC
   1324 Static void
   1325 ehci_dump_exfer(struct ehci_xfer *ex)
   1326 {
   1327 	printf("ehci_dump_exfer: ex=%p\n", ex);
   1328 }
   1329 #endif
   1330 #endif
   1331 
   1332 usbd_status
   1333 ehci_open(usbd_pipe_handle pipe)
   1334 {
   1335 	usbd_device_handle dev = pipe->device;
   1336 	ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
   1337 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
   1338 	u_int8_t addr = dev->address;
   1339 	u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
   1340 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   1341 	ehci_soft_qh_t *sqh;
   1342 	usbd_status err;
   1343 	int s;
   1344 	int ival, speed, naks;
   1345 	int hshubaddr, hshubport;
   1346 
   1347 	DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
   1348 		     pipe, addr, ed->bEndpointAddress, sc->sc_addr));
   1349 
   1350 	if (dev->myhsport) {
   1351 		hshubaddr = dev->myhsport->parent->address;
   1352 		hshubport = dev->myhsport->portno;
   1353 	} else {
   1354 		hshubaddr = 0;
   1355 		hshubport = 0;
   1356 	}
   1357 
   1358 	if (sc->sc_dying)
   1359 		return (USBD_IOERROR);
   1360 
   1361 	epipe->nexttoggle = 0;
   1362 
   1363 	if (addr == sc->sc_addr) {
   1364 		switch (ed->bEndpointAddress) {
   1365 		case USB_CONTROL_ENDPOINT:
   1366 			pipe->methods = &ehci_root_ctrl_methods;
   1367 			break;
   1368 		case UE_DIR_IN | EHCI_INTR_ENDPT:
   1369 			pipe->methods = &ehci_root_intr_methods;
   1370 			break;
   1371 		default:
   1372 			return (USBD_INVAL);
   1373 		}
   1374 		return (USBD_NORMAL_COMPLETION);
   1375 	}
   1376 
   1377 	/* XXX All this stuff is only valid for async. */
   1378 	switch (dev->speed) {
   1379 	case USB_SPEED_LOW:  speed = EHCI_QH_SPEED_LOW;  break;
   1380 	case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
   1381 	case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
   1382 	default: panic("ehci_open: bad device speed %d", dev->speed);
   1383 	}
   1384 	if (speed != EHCI_QH_SPEED_HIGH) {
   1385 		printf("%s: *** WARNING: opening low/full speed device, this "
   1386 		       "may not work yet.\n",
   1387 		       USBDEVNAME(sc->sc_bus.bdev));
   1388 		DPRINTFN(1,("ehci_open: hshubaddr=%d hshubport=%d\n",
   1389 			    hshubaddr, hshubport));
   1390 #if 0
   1391 		if (xfertype != UE_CONTROL)
   1392 			return USBD_INVAL;
   1393 #endif
   1394 	}
   1395 
   1396 	naks = 8;		/* XXX */
   1397 	sqh = ehci_alloc_sqh(sc);
   1398 	if (sqh == NULL)
   1399 		goto bad0;
   1400 	/* qh_link filled when the QH is added */
   1401 	sqh->qh.qh_endp = htole32(
   1402 		EHCI_QH_SET_ADDR(addr) |
   1403 		EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
   1404 		EHCI_QH_SET_EPS(speed) |
   1405 		EHCI_QH_DTC |
   1406 		EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
   1407 		(speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
   1408 		 EHCI_QH_CTL : 0) |
   1409 		EHCI_QH_SET_NRL(naks)
   1410 		);
   1411 	sqh->qh.qh_endphub = htole32(
   1412 		EHCI_QH_SET_MULT(1) |
   1413 		EHCI_QH_SET_HUBA(hshubaddr) |
   1414 		EHCI_QH_SET_PORT(hshubport) |
   1415 		EHCI_QH_SET_CMASK(0x08) | /* XXX */
   1416 		EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x02 : 0)
   1417 		);
   1418 	sqh->qh.qh_curqtd = EHCI_NULL;
   1419 	/* Fill the overlay qTD */
   1420 	sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
   1421 	sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
   1422 	sqh->qh.qh_qtd.qtd_status = htole32(0);
   1423 
   1424 	epipe->sqh = sqh;
   1425 
   1426 	switch (xfertype) {
   1427 	case UE_CONTROL:
   1428 		err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
   1429 				   0, &epipe->u.ctl.reqdma);
   1430 #ifdef EHCI_DEBUG
   1431 		if (err)
   1432 			printf("ehci_open: usb_allocmem()=%d\n", err);
   1433 #endif
   1434 		if (err)
   1435 			goto bad1;
   1436 		pipe->methods = &ehci_device_ctrl_methods;
   1437 		s = splusb();
   1438 		ehci_add_qh(sqh, sc->sc_async_head);
   1439 		splx(s);
   1440 		break;
   1441 	case UE_BULK:
   1442 		pipe->methods = &ehci_device_bulk_methods;
   1443 		s = splusb();
   1444 		ehci_add_qh(sqh, sc->sc_async_head);
   1445 		splx(s);
   1446 		break;
   1447 	case UE_INTERRUPT:
   1448 		pipe->methods = &ehci_device_intr_methods;
   1449 		ival = pipe->interval;
   1450 		if (ival == USBD_DEFAULT_INTERVAL)
   1451 			ival = ed->bInterval;
   1452 		return (ehci_device_setintr(sc, sqh, ival));
   1453 	case UE_ISOCHRONOUS:
   1454 		pipe->methods = &ehci_device_isoc_methods;
   1455 		return (USBD_INVAL);
   1456 	default:
   1457 		return (USBD_INVAL);
   1458 	}
   1459 	return (USBD_NORMAL_COMPLETION);
   1460 
   1461  bad1:
   1462 	ehci_free_sqh(sc, sqh);
   1463  bad0:
   1464 	return (USBD_NOMEM);
   1465 }
   1466 
   1467 /*
   1468  * Add an ED to the schedule.  Called at splusb().
   1469  */
   1470 void
   1471 ehci_add_qh(ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
   1472 {
   1473 	SPLUSBCHECK;
   1474 
   1475 	sqh->next = head->next;
   1476 	sqh->qh.qh_link = head->qh.qh_link;
   1477 	head->next = sqh;
   1478 	head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
   1479 
   1480 #ifdef EHCI_DEBUG
   1481 	if (ehcidebug > 5) {
   1482 		printf("ehci_add_qh:\n");
   1483 		ehci_dump_sqh(sqh);
   1484 	}
   1485 #endif
   1486 }
   1487 
   1488 /*
   1489  * Remove an ED from the schedule.  Called at splusb().
   1490  */
   1491 void
   1492 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
   1493 {
   1494 	ehci_soft_qh_t *p;
   1495 
   1496 	SPLUSBCHECK;
   1497 	/* XXX */
   1498 	for (p = head; p != NULL && p->next != sqh; p = p->next)
   1499 		;
   1500 	if (p == NULL)
   1501 		panic("ehci_rem_qh: ED not found");
   1502 	p->next = sqh->next;
   1503 	p->qh.qh_link = sqh->qh.qh_link;
   1504 
   1505 	ehci_sync_hc(sc);
   1506 }
   1507 
   1508 void
   1509 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
   1510 {
   1511 	int i;
   1512 	u_int32_t status;
   1513 
   1514 	/* Save toggle bit and ping status. */
   1515 	status = sqh->qh.qh_qtd.qtd_status &
   1516 	    htole32(EHCI_QTD_TOGGLE_MASK |
   1517 		    EHCI_QTD_SET_STATUS(EHCI_QTD_PINGSTATE));
   1518 	/* Set HALTED to make hw leave it alone. */
   1519 	sqh->qh.qh_qtd.qtd_status =
   1520 	    htole32(EHCI_QTD_SET_STATUS(EHCI_QTD_HALTED));
   1521 	sqh->qh.qh_curqtd = 0;
   1522 	sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
   1523 	sqh->qh.qh_qtd.qtd_altnext = 0;
   1524 	for (i = 0; i < EHCI_QTD_NBUFFERS; i++)
   1525 		sqh->qh.qh_qtd.qtd_buffer[i] = 0;
   1526 	sqh->sqtd = sqtd;
   1527 	/* Set !HALTED && !ACTIVE to start execution, preserve some fields */
   1528 	sqh->qh.qh_qtd.qtd_status = status;
   1529 }
   1530 
   1531 /*
   1532  * Ensure that the HC has released all references to the QH.  We do this
   1533  * by asking for a Async Advance Doorbell interrupt and then we wait for
   1534  * the interrupt.
   1535  * To make this easier we first obtain exclusive use of the doorbell.
   1536  */
   1537 void
   1538 ehci_sync_hc(ehci_softc_t *sc)
   1539 {
   1540 	int s, error;
   1541 
   1542 	if (sc->sc_dying) {
   1543 		DPRINTFN(2,("ehci_sync_hc: dying\n"));
   1544 		return;
   1545 	}
   1546 	DPRINTFN(2,("ehci_sync_hc: enter\n"));
   1547 	usb_lockmgr(&sc->sc_doorbell_lock, LK_EXCLUSIVE, NULL); /* get doorbell */
   1548 	s = splhardusb();
   1549 	/* ask for doorbell */
   1550 	EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
   1551 	DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
   1552 		    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
   1553 	error = tsleep(&sc->sc_async_head, PZERO, "ehcidi", hz); /* bell wait */
   1554 	DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
   1555 		    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
   1556 	splx(s);
   1557 	usb_lockmgr(&sc->sc_doorbell_lock, LK_RELEASE, NULL); /* release doorbell */
   1558 #ifdef DIAGNOSTIC
   1559 	if (error)
   1560 		printf("ehci_sync_hc: tsleep() = %d\n", error);
   1561 #endif
   1562 	DPRINTFN(2,("ehci_sync_hc: exit\n"));
   1563 }
   1564 
   1565 /***********/
   1566 
   1567 /*
   1568  * Data structures and routines to emulate the root hub.
   1569  */
   1570 Static usb_device_descriptor_t ehci_devd = {
   1571 	USB_DEVICE_DESCRIPTOR_SIZE,
   1572 	UDESC_DEVICE,		/* type */
   1573 	{0x00, 0x02},		/* USB version */
   1574 	UDCLASS_HUB,		/* class */
   1575 	UDSUBCLASS_HUB,		/* subclass */
   1576 	UDPROTO_HSHUBSTT,	/* protocol */
   1577 	64,			/* max packet */
   1578 	{0},{0},{0x00,0x01},	/* device id */
   1579 	1,2,0,			/* string indicies */
   1580 	1			/* # of configurations */
   1581 };
   1582 
   1583 Static usb_device_qualifier_t ehci_odevd = {
   1584 	USB_DEVICE_DESCRIPTOR_SIZE,
   1585 	UDESC_DEVICE_QUALIFIER,	/* type */
   1586 	{0x00, 0x02},		/* USB version */
   1587 	UDCLASS_HUB,		/* class */
   1588 	UDSUBCLASS_HUB,		/* subclass */
   1589 	UDPROTO_FSHUB,		/* protocol */
   1590 	64,			/* max packet */
   1591 	1,			/* # of configurations */
   1592 	0
   1593 };
   1594 
   1595 Static usb_config_descriptor_t ehci_confd = {
   1596 	USB_CONFIG_DESCRIPTOR_SIZE,
   1597 	UDESC_CONFIG,
   1598 	{USB_CONFIG_DESCRIPTOR_SIZE +
   1599 	 USB_INTERFACE_DESCRIPTOR_SIZE +
   1600 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
   1601 	1,
   1602 	1,
   1603 	0,
   1604 	UC_SELF_POWERED,
   1605 	0			/* max power */
   1606 };
   1607 
   1608 Static usb_interface_descriptor_t ehci_ifcd = {
   1609 	USB_INTERFACE_DESCRIPTOR_SIZE,
   1610 	UDESC_INTERFACE,
   1611 	0,
   1612 	0,
   1613 	1,
   1614 	UICLASS_HUB,
   1615 	UISUBCLASS_HUB,
   1616 	UIPROTO_HSHUBSTT,
   1617 	0
   1618 };
   1619 
   1620 Static usb_endpoint_descriptor_t ehci_endpd = {
   1621 	USB_ENDPOINT_DESCRIPTOR_SIZE,
   1622 	UDESC_ENDPOINT,
   1623 	UE_DIR_IN | EHCI_INTR_ENDPT,
   1624 	UE_INTERRUPT,
   1625 	{8, 0},			/* max packet */
   1626 	255
   1627 };
   1628 
   1629 Static usb_hub_descriptor_t ehci_hubd = {
   1630 	USB_HUB_DESCRIPTOR_SIZE,
   1631 	UDESC_HUB,
   1632 	0,
   1633 	{0,0},
   1634 	0,
   1635 	0,
   1636 	{0},
   1637 };
   1638 
   1639 Static int
   1640 ehci_str(usb_string_descriptor_t *p, int l, char *s)
   1641 {
   1642 	int i;
   1643 
   1644 	if (l == 0)
   1645 		return (0);
   1646 	p->bLength = 2 * strlen(s) + 2;
   1647 	if (l == 1)
   1648 		return (1);
   1649 	p->bDescriptorType = UDESC_STRING;
   1650 	l -= 2;
   1651 	for (i = 0; s[i] && l > 1; i++, l -= 2)
   1652 		USETW2(p->bString[i], 0, s[i]);
   1653 	return (2*i+2);
   1654 }
   1655 
   1656 /*
   1657  * Simulate a hardware hub by handling all the necessary requests.
   1658  */
   1659 Static usbd_status
   1660 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
   1661 {
   1662 	usbd_status err;
   1663 
   1664 	/* Insert last in queue. */
   1665 	err = usb_insert_transfer(xfer);
   1666 	if (err)
   1667 		return (err);
   1668 
   1669 	/* Pipe isn't running, start first */
   1670 	return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1671 }
   1672 
   1673 Static usbd_status
   1674 ehci_root_ctrl_start(usbd_xfer_handle xfer)
   1675 {
   1676 	ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
   1677 	usb_device_request_t *req;
   1678 	void *buf = NULL;
   1679 	int port, i;
   1680 	int s, len, value, index, l, totlen = 0;
   1681 	usb_port_status_t ps;
   1682 	usb_hub_descriptor_t hubd;
   1683 	usbd_status err;
   1684 	u_int32_t v;
   1685 
   1686 	if (sc->sc_dying)
   1687 		return (USBD_IOERROR);
   1688 
   1689 #ifdef DIAGNOSTIC
   1690 	if (!(xfer->rqflags & URQ_REQUEST))
   1691 		/* XXX panic */
   1692 		return (USBD_INVAL);
   1693 #endif
   1694 	req = &xfer->request;
   1695 
   1696 	DPRINTFN(4,("ehci_root_ctrl_start: type=0x%02x request=%02x\n",
   1697 		    req->bmRequestType, req->bRequest));
   1698 
   1699 	len = UGETW(req->wLength);
   1700 	value = UGETW(req->wValue);
   1701 	index = UGETW(req->wIndex);
   1702 
   1703 	if (len != 0)
   1704 		buf = KERNADDR(&xfer->dmabuf, 0);
   1705 
   1706 #define C(x,y) ((x) | ((y) << 8))
   1707 	switch(C(req->bRequest, req->bmRequestType)) {
   1708 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
   1709 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
   1710 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
   1711 		/*
   1712 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
   1713 		 * for the integrated root hub.
   1714 		 */
   1715 		break;
   1716 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
   1717 		if (len > 0) {
   1718 			*(u_int8_t *)buf = sc->sc_conf;
   1719 			totlen = 1;
   1720 		}
   1721 		break;
   1722 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   1723 		DPRINTFN(8,("ehci_root_ctrl_start: wValue=0x%04x\n", value));
   1724 		switch(value >> 8) {
   1725 		case UDESC_DEVICE:
   1726 			if ((value & 0xff) != 0) {
   1727 				err = USBD_IOERROR;
   1728 				goto ret;
   1729 			}
   1730 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   1731 			USETW(ehci_devd.idVendor, sc->sc_id_vendor);
   1732 			memcpy(buf, &ehci_devd, l);
   1733 			break;
   1734 		/*
   1735 		 * We can't really operate at another speed, but the spec says
   1736 		 * we need this descriptor.
   1737 		 */
   1738 		case UDESC_DEVICE_QUALIFIER:
   1739 			if ((value & 0xff) != 0) {
   1740 				err = USBD_IOERROR;
   1741 				goto ret;
   1742 			}
   1743 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   1744 			memcpy(buf, &ehci_odevd, l);
   1745 			break;
   1746 		/*
   1747 		 * We can't really operate at another speed, but the spec says
   1748 		 * we need this descriptor.
   1749 		 */
   1750 		case UDESC_OTHER_SPEED_CONFIGURATION:
   1751 		case UDESC_CONFIG:
   1752 			if ((value & 0xff) != 0) {
   1753 				err = USBD_IOERROR;
   1754 				goto ret;
   1755 			}
   1756 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
   1757 			memcpy(buf, &ehci_confd, l);
   1758 			((usb_config_descriptor_t *)buf)->bDescriptorType =
   1759 				value >> 8;
   1760 			buf = (char *)buf + l;
   1761 			len -= l;
   1762 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
   1763 			totlen += l;
   1764 			memcpy(buf, &ehci_ifcd, l);
   1765 			buf = (char *)buf + l;
   1766 			len -= l;
   1767 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
   1768 			totlen += l;
   1769 			memcpy(buf, &ehci_endpd, l);
   1770 			break;
   1771 		case UDESC_STRING:
   1772 			if (len == 0)
   1773 				break;
   1774 			*(u_int8_t *)buf = 0;
   1775 			totlen = 1;
   1776 			switch (value & 0xff) {
   1777 			case 0: /* Language table */
   1778 				totlen = ehci_str(buf, len, "\001");
   1779 				break;
   1780 			case 1: /* Vendor */
   1781 				totlen = ehci_str(buf, len, sc->sc_vendor);
   1782 				break;
   1783 			case 2: /* Product */
   1784 				totlen = ehci_str(buf, len, "EHCI root hub");
   1785 				break;
   1786 			}
   1787 			break;
   1788 		default:
   1789 			err = USBD_IOERROR;
   1790 			goto ret;
   1791 		}
   1792 		break;
   1793 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   1794 		if (len > 0) {
   1795 			*(u_int8_t *)buf = 0;
   1796 			totlen = 1;
   1797 		}
   1798 		break;
   1799 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   1800 		if (len > 1) {
   1801 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   1802 			totlen = 2;
   1803 		}
   1804 		break;
   1805 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   1806 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   1807 		if (len > 1) {
   1808 			USETW(((usb_status_t *)buf)->wStatus, 0);
   1809 			totlen = 2;
   1810 		}
   1811 		break;
   1812 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   1813 		if (value >= USB_MAX_DEVICES) {
   1814 			err = USBD_IOERROR;
   1815 			goto ret;
   1816 		}
   1817 		sc->sc_addr = value;
   1818 		break;
   1819 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   1820 		if (value != 0 && value != 1) {
   1821 			err = USBD_IOERROR;
   1822 			goto ret;
   1823 		}
   1824 		sc->sc_conf = value;
   1825 		break;
   1826 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   1827 		break;
   1828 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   1829 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   1830 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   1831 		err = USBD_IOERROR;
   1832 		goto ret;
   1833 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   1834 		break;
   1835 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   1836 		break;
   1837 	/* Hub requests */
   1838 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   1839 		break;
   1840 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   1841 		DPRINTFN(8, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
   1842 			     "port=%d feature=%d\n",
   1843 			     index, value));
   1844 		if (index < 1 || index > sc->sc_noport) {
   1845 			err = USBD_IOERROR;
   1846 			goto ret;
   1847 		}
   1848 		port = EHCI_PORTSC(index);
   1849 		v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
   1850 		switch(value) {
   1851 		case UHF_PORT_ENABLE:
   1852 			EOWRITE4(sc, port, v &~ EHCI_PS_PE);
   1853 			break;
   1854 		case UHF_PORT_SUSPEND:
   1855 			EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
   1856 			break;
   1857 		case UHF_PORT_POWER:
   1858 			EOWRITE4(sc, port, v &~ EHCI_PS_PP);
   1859 			break;
   1860 		case UHF_PORT_TEST:
   1861 			DPRINTFN(2,("ehci_root_ctrl_start: clear port test "
   1862 				    "%d\n", index));
   1863 			break;
   1864 		case UHF_PORT_INDICATOR:
   1865 			DPRINTFN(2,("ehci_root_ctrl_start: clear port ind "
   1866 				    "%d\n", index));
   1867 			EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
   1868 			break;
   1869 		case UHF_C_PORT_CONNECTION:
   1870 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
   1871 			break;
   1872 		case UHF_C_PORT_ENABLE:
   1873 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
   1874 			break;
   1875 		case UHF_C_PORT_SUSPEND:
   1876 			/* how? */
   1877 			break;
   1878 		case UHF_C_PORT_OVER_CURRENT:
   1879 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
   1880 			break;
   1881 		case UHF_C_PORT_RESET:
   1882 			sc->sc_isreset = 0;
   1883 			break;
   1884 		default:
   1885 			err = USBD_IOERROR;
   1886 			goto ret;
   1887 		}
   1888 #if 0
   1889 		switch(value) {
   1890 		case UHF_C_PORT_CONNECTION:
   1891 		case UHF_C_PORT_ENABLE:
   1892 		case UHF_C_PORT_SUSPEND:
   1893 		case UHF_C_PORT_OVER_CURRENT:
   1894 		case UHF_C_PORT_RESET:
   1895 			/* Enable RHSC interrupt if condition is cleared. */
   1896 			if ((OREAD4(sc, port) >> 16) == 0)
   1897 				ehci_pcd_able(sc, 1);
   1898 			break;
   1899 		default:
   1900 			break;
   1901 		}
   1902 #endif
   1903 		break;
   1904 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   1905 		if ((value & 0xff) != 0) {
   1906 			err = USBD_IOERROR;
   1907 			goto ret;
   1908 		}
   1909 		hubd = ehci_hubd;
   1910 		hubd.bNbrPorts = sc->sc_noport;
   1911 		v = EOREAD4(sc, EHCI_HCSPARAMS);
   1912 		USETW(hubd.wHubCharacteristics,
   1913 		    EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
   1914 		    EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
   1915 		        ? UHD_PORT_IND : 0);
   1916 		hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
   1917 		for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
   1918 			hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
   1919 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
   1920 		l = min(len, hubd.bDescLength);
   1921 		totlen = l;
   1922 		memcpy(buf, &hubd, l);
   1923 		break;
   1924 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   1925 		if (len != 4) {
   1926 			err = USBD_IOERROR;
   1927 			goto ret;
   1928 		}
   1929 		memset(buf, 0, len); /* ? XXX */
   1930 		totlen = len;
   1931 		break;
   1932 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   1933 		DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n",
   1934 			    index));
   1935 		if (index < 1 || index > sc->sc_noport) {
   1936 			err = USBD_IOERROR;
   1937 			goto ret;
   1938 		}
   1939 		if (len != 4) {
   1940 			err = USBD_IOERROR;
   1941 			goto ret;
   1942 		}
   1943 		v = EOREAD4(sc, EHCI_PORTSC(index));
   1944 		DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n",
   1945 			    v));
   1946 		i = UPS_HIGH_SPEED;
   1947 		if (v & EHCI_PS_CS)	i |= UPS_CURRENT_CONNECT_STATUS;
   1948 		if (v & EHCI_PS_PE)	i |= UPS_PORT_ENABLED;
   1949 		if (v & EHCI_PS_SUSP)	i |= UPS_SUSPEND;
   1950 		if (v & EHCI_PS_OCA)	i |= UPS_OVERCURRENT_INDICATOR;
   1951 		if (v & EHCI_PS_PR)	i |= UPS_RESET;
   1952 		if (v & EHCI_PS_PP)	i |= UPS_PORT_POWER;
   1953 		USETW(ps.wPortStatus, i);
   1954 		i = 0;
   1955 		if (v & EHCI_PS_CSC)	i |= UPS_C_CONNECT_STATUS;
   1956 		if (v & EHCI_PS_PEC)	i |= UPS_C_PORT_ENABLED;
   1957 		if (v & EHCI_PS_OCC)	i |= UPS_C_OVERCURRENT_INDICATOR;
   1958 		if (sc->sc_isreset)	i |= UPS_C_PORT_RESET;
   1959 		USETW(ps.wPortChange, i);
   1960 		l = min(len, sizeof ps);
   1961 		memcpy(buf, &ps, l);
   1962 		totlen = l;
   1963 		break;
   1964 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   1965 		err = USBD_IOERROR;
   1966 		goto ret;
   1967 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   1968 		break;
   1969 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   1970 		if (index < 1 || index > sc->sc_noport) {
   1971 			err = USBD_IOERROR;
   1972 			goto ret;
   1973 		}
   1974 		port = EHCI_PORTSC(index);
   1975 		v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
   1976 		switch(value) {
   1977 		case UHF_PORT_ENABLE:
   1978 			EOWRITE4(sc, port, v | EHCI_PS_PE);
   1979 			break;
   1980 		case UHF_PORT_SUSPEND:
   1981 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
   1982 			break;
   1983 		case UHF_PORT_RESET:
   1984 			DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n",
   1985 				    index));
   1986 			if (EHCI_PS_IS_LOWSPEED(v)) {
   1987 				/* Low speed device, give up ownership. */
   1988 				ehci_disown(sc, index, 1);
   1989 				break;
   1990 			}
   1991 			/* Start reset sequence. */
   1992 			v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
   1993 			EOWRITE4(sc, port, v | EHCI_PS_PR);
   1994 			/* Wait for reset to complete. */
   1995 			usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
   1996 			if (sc->sc_dying) {
   1997 				err = USBD_IOERROR;
   1998 				goto ret;
   1999 			}
   2000 			/* Terminate reset sequence. */
   2001 			EOWRITE4(sc, port, v);
   2002 			/* Wait for HC to complete reset. */
   2003 			usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE);
   2004 			if (sc->sc_dying) {
   2005 				err = USBD_IOERROR;
   2006 				goto ret;
   2007 			}
   2008 			v = EOREAD4(sc, port);
   2009 			DPRINTF(("ehci after reset, status=0x%08x\n", v));
   2010 			if (v & EHCI_PS_PR) {
   2011 				printf("%s: port reset timeout\n",
   2012 				       USBDEVNAME(sc->sc_bus.bdev));
   2013 				return (USBD_TIMEOUT);
   2014 			}
   2015 			if (!(v & EHCI_PS_PE)) {
   2016 				/* Not a high speed device, give up ownership.*/
   2017 				ehci_disown(sc, index, 0);
   2018 				break;
   2019 			}
   2020 			sc->sc_isreset = 1;
   2021 			DPRINTF(("ehci port %d reset, status = 0x%08x\n",
   2022 				 index, v));
   2023 			break;
   2024 		case UHF_PORT_POWER:
   2025 			DPRINTFN(2,("ehci_root_ctrl_start: set port power "
   2026 				    "%d\n", index));
   2027 			EOWRITE4(sc, port, v | EHCI_PS_PP);
   2028 			break;
   2029 		case UHF_PORT_TEST:
   2030 			DPRINTFN(2,("ehci_root_ctrl_start: set port test "
   2031 				    "%d\n", index));
   2032 			break;
   2033 		case UHF_PORT_INDICATOR:
   2034 			DPRINTFN(2,("ehci_root_ctrl_start: set port ind "
   2035 				    "%d\n", index));
   2036 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
   2037 			break;
   2038 		default:
   2039 			err = USBD_IOERROR;
   2040 			goto ret;
   2041 		}
   2042 		break;
   2043 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
   2044 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
   2045 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
   2046 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
   2047 		break;
   2048 	default:
   2049 		err = USBD_IOERROR;
   2050 		goto ret;
   2051 	}
   2052 	xfer->actlen = totlen;
   2053 	err = USBD_NORMAL_COMPLETION;
   2054  ret:
   2055 	xfer->status = err;
   2056 	s = splusb();
   2057 	usb_transfer_complete(xfer);
   2058 	splx(s);
   2059 	return (USBD_IN_PROGRESS);
   2060 }
   2061 
   2062 void
   2063 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
   2064 {
   2065 	int port;
   2066 	u_int32_t v;
   2067 
   2068 	DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
   2069 #ifdef DIAGNOSTIC
   2070 	if (sc->sc_npcomp != 0) {
   2071 		int i = (index-1) / sc->sc_npcomp;
   2072 		if (i >= sc->sc_ncomp)
   2073 			printf("%s: strange port\n",
   2074 			       USBDEVNAME(sc->sc_bus.bdev));
   2075 		else
   2076 			printf("%s: handing over %s speed device on "
   2077 			       "port %d to %s\n",
   2078 			       USBDEVNAME(sc->sc_bus.bdev),
   2079 			       lowspeed ? "low" : "full",
   2080 			       index, USBDEVNAME(sc->sc_comps[i]->bdev));
   2081 	} else {
   2082 		printf("%s: npcomp == 0\n", USBDEVNAME(sc->sc_bus.bdev));
   2083 	}
   2084 #endif
   2085 	port = EHCI_PORTSC(index);
   2086 	v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
   2087 	EOWRITE4(sc, port, v | EHCI_PS_PO);
   2088 }
   2089 
   2090 /* Abort a root control request. */
   2091 Static void
   2092 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
   2093 {
   2094 	/* Nothing to do, all transfers are synchronous. */
   2095 }
   2096 
   2097 /* Close the root pipe. */
   2098 Static void
   2099 ehci_root_ctrl_close(usbd_pipe_handle pipe)
   2100 {
   2101 	DPRINTF(("ehci_root_ctrl_close\n"));
   2102 	/* Nothing to do. */
   2103 }
   2104 
   2105 void
   2106 ehci_root_intr_done(usbd_xfer_handle xfer)
   2107 {
   2108 	xfer->hcpriv = NULL;
   2109 }
   2110 
   2111 Static usbd_status
   2112 ehci_root_intr_transfer(usbd_xfer_handle xfer)
   2113 {
   2114 	usbd_status err;
   2115 
   2116 	/* Insert last in queue. */
   2117 	err = usb_insert_transfer(xfer);
   2118 	if (err)
   2119 		return (err);
   2120 
   2121 	/* Pipe isn't running, start first */
   2122 	return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2123 }
   2124 
   2125 Static usbd_status
   2126 ehci_root_intr_start(usbd_xfer_handle xfer)
   2127 {
   2128 	usbd_pipe_handle pipe = xfer->pipe;
   2129 	ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
   2130 
   2131 	if (sc->sc_dying)
   2132 		return (USBD_IOERROR);
   2133 
   2134 	sc->sc_intrxfer = xfer;
   2135 
   2136 	return (USBD_IN_PROGRESS);
   2137 }
   2138 
   2139 /* Abort a root interrupt request. */
   2140 Static void
   2141 ehci_root_intr_abort(usbd_xfer_handle xfer)
   2142 {
   2143 	int s;
   2144 
   2145 	if (xfer->pipe->intrxfer == xfer) {
   2146 		DPRINTF(("ehci_root_intr_abort: remove\n"));
   2147 		xfer->pipe->intrxfer = NULL;
   2148 	}
   2149 	xfer->status = USBD_CANCELLED;
   2150 	s = splusb();
   2151 	usb_transfer_complete(xfer);
   2152 	splx(s);
   2153 }
   2154 
   2155 /* Close the root pipe. */
   2156 Static void
   2157 ehci_root_intr_close(usbd_pipe_handle pipe)
   2158 {
   2159 	ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
   2160 
   2161 	DPRINTF(("ehci_root_intr_close\n"));
   2162 
   2163 	sc->sc_intrxfer = NULL;
   2164 }
   2165 
   2166 void
   2167 ehci_root_ctrl_done(usbd_xfer_handle xfer)
   2168 {
   2169 	xfer->hcpriv = NULL;
   2170 }
   2171 
   2172 /************************/
   2173 
   2174 ehci_soft_qh_t *
   2175 ehci_alloc_sqh(ehci_softc_t *sc)
   2176 {
   2177 	ehci_soft_qh_t *sqh;
   2178 	usbd_status err;
   2179 	int i, offs;
   2180 	usb_dma_t dma;
   2181 
   2182 	if (sc->sc_freeqhs == NULL) {
   2183 		DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
   2184 		err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
   2185 			  EHCI_PAGE_SIZE, &dma);
   2186 #ifdef EHCI_DEBUG
   2187 		if (err)
   2188 			printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
   2189 #endif
   2190 		if (err)
   2191 			return (NULL);
   2192 		for(i = 0; i < EHCI_SQH_CHUNK; i++) {
   2193 			offs = i * EHCI_SQH_SIZE;
   2194 			sqh = KERNADDR(&dma, offs);
   2195 			sqh->physaddr = DMAADDR(&dma, offs);
   2196 			sqh->next = sc->sc_freeqhs;
   2197 			sc->sc_freeqhs = sqh;
   2198 		}
   2199 	}
   2200 	sqh = sc->sc_freeqhs;
   2201 	sc->sc_freeqhs = sqh->next;
   2202 	memset(&sqh->qh, 0, sizeof(ehci_qh_t));
   2203 	sqh->next = NULL;
   2204 	return (sqh);
   2205 }
   2206 
   2207 void
   2208 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
   2209 {
   2210 	sqh->next = sc->sc_freeqhs;
   2211 	sc->sc_freeqhs = sqh;
   2212 }
   2213 
   2214 ehci_soft_qtd_t *
   2215 ehci_alloc_sqtd(ehci_softc_t *sc)
   2216 {
   2217 	ehci_soft_qtd_t *sqtd;
   2218 	usbd_status err;
   2219 	int i, offs;
   2220 	usb_dma_t dma;
   2221 	int s;
   2222 
   2223 	if (sc->sc_freeqtds == NULL) {
   2224 		DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n"));
   2225 		err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
   2226 			  EHCI_PAGE_SIZE, &dma);
   2227 #ifdef EHCI_DEBUG
   2228 		if (err)
   2229 			printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
   2230 #endif
   2231 		if (err)
   2232 			return (NULL);
   2233 		s = splusb();
   2234 		for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
   2235 			offs = i * EHCI_SQTD_SIZE;
   2236 			sqtd = KERNADDR(&dma, offs);
   2237 			sqtd->physaddr = DMAADDR(&dma, offs);
   2238 			sqtd->nextqtd = sc->sc_freeqtds;
   2239 			sc->sc_freeqtds = sqtd;
   2240 		}
   2241 		splx(s);
   2242 	}
   2243 
   2244 	s = splusb();
   2245 	sqtd = sc->sc_freeqtds;
   2246 	sc->sc_freeqtds = sqtd->nextqtd;
   2247 	memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t));
   2248 	sqtd->nextqtd = NULL;
   2249 	sqtd->xfer = NULL;
   2250 	splx(s);
   2251 
   2252 	return (sqtd);
   2253 }
   2254 
   2255 void
   2256 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
   2257 {
   2258 	int s;
   2259 
   2260 	s = splusb();
   2261 	sqtd->nextqtd = sc->sc_freeqtds;
   2262 	sc->sc_freeqtds = sqtd;
   2263 	splx(s);
   2264 }
   2265 
   2266 usbd_status
   2267 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
   2268 		     int alen, int rd, usbd_xfer_handle xfer,
   2269 		     ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
   2270 {
   2271 	ehci_soft_qtd_t *next, *cur;
   2272 	ehci_physaddr_t dataphys, dataphyspage, dataphyslastpage, nextphys;
   2273 	u_int32_t qtdstatus;
   2274 	int len, curlen, mps;
   2275 	int i, tog;
   2276 	usb_dma_t *dma = &xfer->dmabuf;
   2277 	u_int16_t flags = xfer->flags;
   2278 
   2279 	DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen));
   2280 
   2281 	len = alen;
   2282 	dataphys = DMAADDR(dma, 0);
   2283 	dataphyslastpage = EHCI_PAGE(dataphys + len - 1);
   2284 	qtdstatus = EHCI_QTD_ACTIVE |
   2285 	    EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
   2286 	    EHCI_QTD_SET_CERR(3)
   2287 	    /* IOC set below */
   2288 	    /* BYTES set below */
   2289 	    ;
   2290 	mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize);
   2291 	tog = epipe->nexttoggle;
   2292 	qtdstatus |= EHCI_QTD_SET_TOGGLE(tog);
   2293 
   2294 	cur = ehci_alloc_sqtd(sc);
   2295 	*sp = cur;
   2296 	if (cur == NULL)
   2297 		goto nomem;
   2298 	for (;;) {
   2299 		dataphyspage = EHCI_PAGE(dataphys);
   2300 		/* The EHCI hardware can handle at most 5 pages. */
   2301 		if (dataphyslastpage - dataphyspage <
   2302 		    EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE) {
   2303 			/* we can handle it in this QTD */
   2304 			curlen = len;
   2305 		} else {
   2306 			/* must use multiple TDs, fill as much as possible. */
   2307 			curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE -
   2308 				 EHCI_PAGE_OFFSET(dataphys);
   2309 #ifdef DIAGNOSTIC
   2310 			if (curlen > len) {
   2311 				printf("ehci_alloc_sqtd_chain: curlen=0x%x "
   2312 				       "len=0x%x offs=0x%x\n", curlen, len,
   2313 				       EHCI_PAGE_OFFSET(dataphys));
   2314 				printf("lastpage=0x%x page=0x%x phys=0x%x\n",
   2315 				       dataphyslastpage, dataphyspage,
   2316 				       dataphys);
   2317 				curlen = len;
   2318 			}
   2319 #endif
   2320 			/* the length must be a multiple of the max size */
   2321 			curlen -= curlen % mps;
   2322 			DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, "
   2323 				    "curlen=%d\n", curlen));
   2324 #ifdef DIAGNOSTIC
   2325 			if (curlen == 0)
   2326 				panic("ehci_alloc_sqtd_chain: curlen == 0");
   2327 #endif
   2328 		}
   2329 		DPRINTFN(4,("ehci_alloc_sqtd_chain: dataphys=0x%08x "
   2330 			    "dataphyslastpage=0x%08x len=%d curlen=%d\n",
   2331 			    dataphys, dataphyslastpage,
   2332 			    len, curlen));
   2333 		len -= curlen;
   2334 
   2335 		/*
   2336 		 * Allocate another transfer if there's more data left,
   2337 		 * or if force last short transfer flag is set and we're
   2338 		 * allocating a multiple of the max packet size.
   2339 		 */
   2340 		if (len != 0 ||
   2341 		    ((curlen % mps) == 0 && !rd && curlen != 0 &&
   2342 		     (flags & USBD_FORCE_SHORT_XFER))) {
   2343 			next = ehci_alloc_sqtd(sc);
   2344 			if (next == NULL)
   2345 				goto nomem;
   2346 			nextphys = htole32(next->physaddr);
   2347 		} else {
   2348 			next = NULL;
   2349 			nextphys = EHCI_NULL;
   2350 		}
   2351 
   2352 		for (i = 0; i * EHCI_PAGE_SIZE <
   2353 		            curlen + EHCI_PAGE_OFFSET(dataphys); i++) {
   2354 			ehci_physaddr_t a = dataphys + i * EHCI_PAGE_SIZE;
   2355 			if (i != 0) /* use offset only in first buffer */
   2356 				a = EHCI_PAGE(a);
   2357 			cur->qtd.qtd_buffer[i] = htole32(a);
   2358 			cur->qtd.qtd_buffer_hi[i] = 0;
   2359 #ifdef DIAGNOSTIC
   2360 			if (i >= EHCI_QTD_NBUFFERS) {
   2361 				printf("ehci_alloc_sqtd_chain: i=%d\n", i);
   2362 				goto nomem;
   2363 			}
   2364 #endif
   2365 		}
   2366 		cur->nextqtd = next;
   2367 		cur->qtd.qtd_next = cur->qtd.qtd_altnext = nextphys;
   2368 		cur->qtd.qtd_status =
   2369 		    htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
   2370 		cur->xfer = xfer;
   2371 		cur->len = curlen;
   2372 		DPRINTFN(10,("ehci_alloc_sqtd_chain: cbp=0x%08x end=0x%08x\n",
   2373 			    dataphys, dataphys + curlen));
   2374 		/* adjust the toggle based on the number of packets in this
   2375 		   qtd */
   2376 		if (((curlen + mps - 1) / mps) & 1) {
   2377 			tog ^= 1;
   2378 			qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
   2379 		}
   2380 		if (next == NULL)
   2381 			break;
   2382 		DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
   2383 		dataphys += curlen;
   2384 		cur = next;
   2385 	}
   2386 	cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
   2387 	*ep = cur;
   2388 	epipe->nexttoggle = tog;
   2389 
   2390 	DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
   2391 		     *sp, *ep));
   2392 
   2393 	return (USBD_NORMAL_COMPLETION);
   2394 
   2395  nomem:
   2396 	/* XXX free chain */
   2397 	DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
   2398 	return (USBD_NOMEM);
   2399 }
   2400 
   2401 Static void
   2402 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd,
   2403 		    ehci_soft_qtd_t *sqtdend)
   2404 {
   2405 	ehci_soft_qtd_t *p;
   2406 	int i;
   2407 
   2408 	DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
   2409 		     sqtd, sqtdend));
   2410 
   2411 	for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
   2412 		p = sqtd->nextqtd;
   2413 		ehci_free_sqtd(sc, sqtd);
   2414 	}
   2415 }
   2416 
   2417 /****************/
   2418 
   2419 /*
   2420  * Close a reqular pipe.
   2421  * Assumes that there are no pending transactions.
   2422  */
   2423 void
   2424 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
   2425 {
   2426 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   2427 	ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
   2428 	ehci_soft_qh_t *sqh = epipe->sqh;
   2429 	int s;
   2430 
   2431 	s = splusb();
   2432 	ehci_rem_qh(sc, sqh, head);
   2433 	splx(s);
   2434 	ehci_free_sqh(sc, epipe->sqh);
   2435 }
   2436 
   2437 /*
   2438  * Abort a device request.
   2439  * If this routine is called at splusb() it guarantees that the request
   2440  * will be removed from the hardware scheduling and that the callback
   2441  * for it will be called with USBD_CANCELLED status.
   2442  * It's impossible to guarantee that the requested transfer will not
   2443  * have happened since the hardware runs concurrently.
   2444  * If the transaction has already happened we rely on the ordinary
   2445  * interrupt processing to process it.
   2446  * XXX This is most probably wrong.
   2447  */
   2448 void
   2449 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
   2450 {
   2451 #define exfer EXFER(xfer)
   2452 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   2453 	ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
   2454 	ehci_soft_qh_t *sqh = epipe->sqh;
   2455 	ehci_soft_qtd_t *sqtd;
   2456 	ehci_physaddr_t cur;
   2457 	u_int32_t qhstatus;
   2458 	int s;
   2459 	int hit;
   2460 
   2461 	DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe));
   2462 
   2463 	if (sc->sc_dying) {
   2464 		/* If we're dying, just do the software part. */
   2465 		s = splusb();
   2466 		xfer->status = status;	/* make software ignore it */
   2467 		usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
   2468 		usb_transfer_complete(xfer);
   2469 		splx(s);
   2470 		return;
   2471 	}
   2472 
   2473 	if (xfer->device->bus->intr_context || !curproc)
   2474 		panic("ehci_abort_xfer: not in process context");
   2475 
   2476 	/*
   2477 	 * Step 1: Make interrupt routine and hardware ignore xfer.
   2478 	 */
   2479 	s = splusb();
   2480 	xfer->status = status;	/* make software ignore it */
   2481 	usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
   2482 	qhstatus = sqh->qh.qh_qtd.qtd_status;
   2483 	sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED);
   2484 	for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
   2485 		sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
   2486 		if (sqtd == exfer->sqtdend)
   2487 			break;
   2488 	}
   2489 	splx(s);
   2490 
   2491 	/*
   2492 	 * Step 2: Wait until we know hardware has finished any possible
   2493 	 * use of the xfer.  Also make sure the soft interrupt routine
   2494 	 * has run.
   2495 	 */
   2496 	ehci_sync_hc(sc);
   2497 	s = splusb();
   2498 #ifdef USB_USE_SOFTINTR
   2499 	sc->sc_softwake = 1;
   2500 #endif /* USB_USE_SOFTINTR */
   2501 	usb_schedsoftintr(&sc->sc_bus);
   2502 #ifdef USB_USE_SOFTINTR
   2503 	tsleep(&sc->sc_softwake, PZERO, "ehciab", 0);
   2504 #endif /* USB_USE_SOFTINTR */
   2505 	splx(s);
   2506 
   2507 	/*
   2508 	 * Step 3: Remove any vestiges of the xfer from the hardware.
   2509 	 * The complication here is that the hardware may have executed
   2510 	 * beyond the xfer we're trying to abort.  So as we're scanning
   2511 	 * the TDs of this xfer we check if the hardware points to
   2512 	 * any of them.
   2513 	 */
   2514 	s = splusb();		/* XXX why? */
   2515 	cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
   2516 	hit = 0;
   2517 	for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
   2518 		hit |= cur == sqtd->physaddr;
   2519 		if (sqtd == exfer->sqtdend)
   2520 			break;
   2521 	}
   2522 	sqtd = sqtd->nextqtd;
   2523 	/* Zap curqtd register if hardware pointed inside the xfer. */
   2524 	if (hit && sqtd != NULL) {
   2525 		DPRINTFN(1,("ehci_abort_xfer: cur=0x%08x\n", sqtd->physaddr));
   2526 		sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */
   2527 		sqh->qh.qh_qtd.qtd_status = qhstatus;
   2528 	} else {
   2529 		DPRINTFN(1,("ehci_abort_xfer: no hit\n"));
   2530 	}
   2531 
   2532 	/*
   2533 	 * Step 4: Execute callback.
   2534 	 */
   2535 #ifdef DIAGNOSTIC
   2536 	exfer->isdone = 1;
   2537 #endif
   2538 	usb_transfer_complete(xfer);
   2539 
   2540 	splx(s);
   2541 #undef exfer
   2542 }
   2543 
   2544 void
   2545 ehci_timeout(void *addr)
   2546 {
   2547 	struct ehci_xfer *exfer = addr;
   2548 	struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe;
   2549 	ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
   2550 
   2551 	DPRINTF(("ehci_timeout: exfer=%p\n", exfer));
   2552 #ifdef USB_DEBUG
   2553 	if (ehcidebug > 1)
   2554 		usbd_dump_pipe(exfer->xfer.pipe);
   2555 #endif
   2556 
   2557 	if (sc->sc_dying) {
   2558 		ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT);
   2559 		return;
   2560 	}
   2561 
   2562 	/* Execute the abort in a process context. */
   2563 	usb_init_task(&exfer->abort_task, ehci_timeout_task, addr);
   2564 	usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task);
   2565 }
   2566 
   2567 void
   2568 ehci_timeout_task(void *addr)
   2569 {
   2570 	usbd_xfer_handle xfer = addr;
   2571 	int s;
   2572 
   2573 	DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer));
   2574 
   2575 	s = splusb();
   2576 	ehci_abort_xfer(xfer, USBD_TIMEOUT);
   2577 	splx(s);
   2578 }
   2579 
   2580 /************************/
   2581 
   2582 Static usbd_status
   2583 ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
   2584 {
   2585 	usbd_status err;
   2586 
   2587 	/* Insert last in queue. */
   2588 	err = usb_insert_transfer(xfer);
   2589 	if (err)
   2590 		return (err);
   2591 
   2592 	/* Pipe isn't running, start first */
   2593 	return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2594 }
   2595 
   2596 Static usbd_status
   2597 ehci_device_ctrl_start(usbd_xfer_handle xfer)
   2598 {
   2599 	ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
   2600 	usbd_status err;
   2601 
   2602 	if (sc->sc_dying)
   2603 		return (USBD_IOERROR);
   2604 
   2605 #ifdef DIAGNOSTIC
   2606 	if (!(xfer->rqflags & URQ_REQUEST)) {
   2607 		/* XXX panic */
   2608 		printf("ehci_device_ctrl_transfer: not a request\n");
   2609 		return (USBD_INVAL);
   2610 	}
   2611 #endif
   2612 
   2613 	err = ehci_device_request(xfer);
   2614 	if (err)
   2615 		return (err);
   2616 
   2617 	if (sc->sc_bus.use_polling)
   2618 		ehci_waitintr(sc, xfer);
   2619 	return (USBD_IN_PROGRESS);
   2620 }
   2621 
   2622 void
   2623 ehci_device_ctrl_done(usbd_xfer_handle xfer)
   2624 {
   2625 	struct ehci_xfer *ex = EXFER(xfer);
   2626 	ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
   2627 	/*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
   2628 
   2629 	DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer));
   2630 
   2631 #ifdef DIAGNOSTIC
   2632 	if (!(xfer->rqflags & URQ_REQUEST)) {
   2633 		panic("ehci_ctrl_done: not a request");
   2634 	}
   2635 #endif
   2636 
   2637 	if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
   2638 		ehci_del_intr_list(ex);	/* remove from active list */
   2639 		ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
   2640 	}
   2641 
   2642 	DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen));
   2643 }
   2644 
   2645 /* Abort a device control request. */
   2646 Static void
   2647 ehci_device_ctrl_abort(usbd_xfer_handle xfer)
   2648 {
   2649 	DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer));
   2650 	ehci_abort_xfer(xfer, USBD_CANCELLED);
   2651 }
   2652 
   2653 /* Close a device control pipe. */
   2654 Static void
   2655 ehci_device_ctrl_close(usbd_pipe_handle pipe)
   2656 {
   2657 	ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
   2658 	/*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
   2659 
   2660 	DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe));
   2661 	ehci_close_pipe(pipe, sc->sc_async_head);
   2662 }
   2663 
   2664 usbd_status
   2665 ehci_device_request(usbd_xfer_handle xfer)
   2666 {
   2667 #define exfer EXFER(xfer)
   2668 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   2669 	usb_device_request_t *req = &xfer->request;
   2670 	usbd_device_handle dev = epipe->pipe.device;
   2671 	ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
   2672 	int addr = dev->address;
   2673 	ehci_soft_qtd_t *setup, *stat, *next;
   2674 	ehci_soft_qh_t *sqh;
   2675 	int isread;
   2676 	int len;
   2677 	usbd_status err;
   2678 	int s;
   2679 
   2680 	isread = req->bmRequestType & UT_READ;
   2681 	len = UGETW(req->wLength);
   2682 
   2683 	DPRINTFN(3,("ehci_device_request: type=0x%02x, request=0x%02x, "
   2684 		    "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
   2685 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
   2686 		    UGETW(req->wIndex), len, addr,
   2687 		    epipe->pipe.endpoint->edesc->bEndpointAddress));
   2688 
   2689 	setup = ehci_alloc_sqtd(sc);
   2690 	if (setup == NULL) {
   2691 		err = USBD_NOMEM;
   2692 		goto bad1;
   2693 	}
   2694 	stat = ehci_alloc_sqtd(sc);
   2695 	if (stat == NULL) {
   2696 		err = USBD_NOMEM;
   2697 		goto bad2;
   2698 	}
   2699 
   2700 	sqh = epipe->sqh;
   2701 	epipe->u.ctl.length = len;
   2702 
   2703 	/* Update device address and length since they may have changed
   2704 	   during the setup of the control pipe in usbd_new_device(). */
   2705 	/* XXX This only needs to be done once, but it's too early in open. */
   2706 	/* XXXX Should not touch ED here! */
   2707 	sqh->qh.qh_endp =
   2708 	    (sqh->qh.qh_endp & htole32(~(EHCI_QH_ADDRMASK | EHCI_QH_MPLMASK))) |
   2709 	    htole32(
   2710 	     EHCI_QH_SET_ADDR(addr) |
   2711 	     EHCI_QH_SET_MPL(UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize))
   2712 	    );
   2713 
   2714 	/* Set up data transaction */
   2715 	if (len != 0) {
   2716 		ehci_soft_qtd_t *end;
   2717 
   2718 		/* Start toggle at 1. */
   2719 		epipe->nexttoggle = 1;
   2720 		err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
   2721 			  &next, &end);
   2722 		if (err)
   2723 			goto bad3;
   2724 		end->qtd.qtd_status &= htole32(~EHCI_QTD_IOC);
   2725 		end->nextqtd = stat;
   2726 		end->qtd.qtd_next =
   2727 		end->qtd.qtd_altnext = htole32(stat->physaddr);
   2728 	} else {
   2729 		next = stat;
   2730 	}
   2731 
   2732 	memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
   2733 
   2734 	/* Clear toggle */
   2735 	setup->qtd.qtd_status = htole32(
   2736 	    EHCI_QTD_ACTIVE |
   2737 	    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
   2738 	    EHCI_QTD_SET_CERR(3) |
   2739 	    EHCI_QTD_SET_TOGGLE(0) |
   2740 	    EHCI_QTD_SET_BYTES(sizeof *req)
   2741 	    );
   2742 	setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
   2743 	setup->qtd.qtd_buffer_hi[0] = 0;
   2744 	setup->nextqtd = next;
   2745 	setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
   2746 	setup->xfer = xfer;
   2747 	setup->len = sizeof *req;
   2748 
   2749 	stat->qtd.qtd_status = htole32(
   2750 	    EHCI_QTD_ACTIVE |
   2751 	    EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
   2752 	    EHCI_QTD_SET_CERR(3) |
   2753 	    EHCI_QTD_SET_TOGGLE(1) |
   2754 	    EHCI_QTD_IOC
   2755 	    );
   2756 	stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
   2757 	stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */
   2758 	stat->nextqtd = NULL;
   2759 	stat->qtd.qtd_next = stat->qtd.qtd_altnext = EHCI_NULL;
   2760 	stat->xfer = xfer;
   2761 	stat->len = 0;
   2762 
   2763 #ifdef EHCI_DEBUG
   2764 	if (ehcidebug > 5) {
   2765 		DPRINTF(("ehci_device_request:\n"));
   2766 		ehci_dump_sqh(sqh);
   2767 		ehci_dump_sqtds(setup);
   2768 	}
   2769 #endif
   2770 
   2771 	exfer->sqtdstart = setup;
   2772 	exfer->sqtdend = stat;
   2773 #ifdef DIAGNOSTIC
   2774 	if (!exfer->isdone) {
   2775 		printf("ehci_device_request: not done, exfer=%p\n", exfer);
   2776 	}
   2777 	exfer->isdone = 0;
   2778 #endif
   2779 
   2780 	/* Insert qTD in QH list. */
   2781 	s = splusb();
   2782 	ehci_set_qh_qtd(sqh, setup);
   2783 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   2784                 usb_callout(xfer->timeout_handle, mstohz(xfer->timeout),
   2785 			    ehci_timeout, xfer);
   2786 	}
   2787 	ehci_add_intr_list(sc, exfer);
   2788 	xfer->status = USBD_IN_PROGRESS;
   2789 	splx(s);
   2790 
   2791 #ifdef EHCI_DEBUG
   2792 	if (ehcidebug > 10) {
   2793 		DPRINTF(("ehci_device_request: status=%x\n",
   2794 			 EOREAD4(sc, EHCI_USBSTS)));
   2795 		delay(10000);
   2796 		ehci_dump_regs(sc);
   2797 		ehci_dump_sqh(sc->sc_async_head);
   2798 		ehci_dump_sqh(sqh);
   2799 		ehci_dump_sqtds(setup);
   2800 	}
   2801 #endif
   2802 
   2803 	return (USBD_NORMAL_COMPLETION);
   2804 
   2805  bad3:
   2806 	ehci_free_sqtd(sc, stat);
   2807  bad2:
   2808 	ehci_free_sqtd(sc, setup);
   2809  bad1:
   2810 	DPRINTFN(-1,("ehci_device_request: no memory\n"));
   2811 	xfer->status = err;
   2812 	usb_transfer_complete(xfer);
   2813 	return (err);
   2814 #undef exfer
   2815 }
   2816 
   2817 /************************/
   2818 
   2819 Static usbd_status
   2820 ehci_device_bulk_transfer(usbd_xfer_handle xfer)
   2821 {
   2822 	usbd_status err;
   2823 
   2824 	/* Insert last in queue. */
   2825 	err = usb_insert_transfer(xfer);
   2826 	if (err)
   2827 		return (err);
   2828 
   2829 	/* Pipe isn't running, start first */
   2830 	return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2831 }
   2832 
   2833 usbd_status
   2834 ehci_device_bulk_start(usbd_xfer_handle xfer)
   2835 {
   2836 #define exfer EXFER(xfer)
   2837 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   2838 	usbd_device_handle dev = epipe->pipe.device;
   2839 	ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
   2840 	ehci_soft_qtd_t *data, *dataend;
   2841 	ehci_soft_qh_t *sqh;
   2842 	usbd_status err;
   2843 	int len, isread, endpt;
   2844 	int s;
   2845 
   2846 	DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%d flags=%d\n",
   2847 		     xfer, xfer->length, xfer->flags));
   2848 
   2849 	if (sc->sc_dying)
   2850 		return (USBD_IOERROR);
   2851 
   2852 #ifdef DIAGNOSTIC
   2853 	if (xfer->rqflags & URQ_REQUEST)
   2854 		panic("ehci_device_bulk_start: a request");
   2855 #endif
   2856 
   2857 	len = xfer->length;
   2858 	endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
   2859 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   2860 	sqh = epipe->sqh;
   2861 
   2862 	epipe->u.bulk.length = len;
   2863 
   2864 	err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
   2865 				   &dataend);
   2866 	if (err) {
   2867 		DPRINTFN(-1,("ehci_device_bulk_transfer: no memory\n"));
   2868 		xfer->status = err;
   2869 		usb_transfer_complete(xfer);
   2870 		return (err);
   2871 	}
   2872 
   2873 #ifdef EHCI_DEBUG
   2874 	if (ehcidebug > 5) {
   2875 		DPRINTF(("ehci_device_bulk_start: data(1)\n"));
   2876 		ehci_dump_sqh(sqh);
   2877 		ehci_dump_sqtds(data);
   2878 	}
   2879 #endif
   2880 
   2881 	/* Set up interrupt info. */
   2882 	exfer->sqtdstart = data;
   2883 	exfer->sqtdend = dataend;
   2884 #ifdef DIAGNOSTIC
   2885 	if (!exfer->isdone) {
   2886 		printf("ehci_device_bulk_start: not done, ex=%p\n", exfer);
   2887 	}
   2888 	exfer->isdone = 0;
   2889 #endif
   2890 
   2891 	s = splusb();
   2892 	ehci_set_qh_qtd(sqh, data);
   2893 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   2894 		usb_callout(xfer->timeout_handle, mstohz(xfer->timeout),
   2895 			    ehci_timeout, xfer);
   2896 	}
   2897 	ehci_add_intr_list(sc, exfer);
   2898 	xfer->status = USBD_IN_PROGRESS;
   2899 	splx(s);
   2900 
   2901 #ifdef EHCI_DEBUG
   2902 	if (ehcidebug > 10) {
   2903 		DPRINTF(("ehci_device_bulk_start: data(2)\n"));
   2904 		delay(10000);
   2905 		DPRINTF(("ehci_device_bulk_start: data(3)\n"));
   2906 		ehci_dump_regs(sc);
   2907 #if 0
   2908 		printf("async_head:\n");
   2909 		ehci_dump_sqh(sc->sc_async_head);
   2910 #endif
   2911 		printf("sqh:\n");
   2912 		ehci_dump_sqh(sqh);
   2913 		ehci_dump_sqtds(data);
   2914 	}
   2915 #endif
   2916 
   2917 	if (sc->sc_bus.use_polling)
   2918 		ehci_waitintr(sc, xfer);
   2919 
   2920 	return (USBD_IN_PROGRESS);
   2921 #undef exfer
   2922 }
   2923 
   2924 Static void
   2925 ehci_device_bulk_abort(usbd_xfer_handle xfer)
   2926 {
   2927 	DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer));
   2928 	ehci_abort_xfer(xfer, USBD_CANCELLED);
   2929 }
   2930 
   2931 /*
   2932  * Close a device bulk pipe.
   2933  */
   2934 Static void
   2935 ehci_device_bulk_close(usbd_pipe_handle pipe)
   2936 {
   2937 	ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
   2938 
   2939 	DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe));
   2940 	ehci_close_pipe(pipe, sc->sc_async_head);
   2941 }
   2942 
   2943 void
   2944 ehci_device_bulk_done(usbd_xfer_handle xfer)
   2945 {
   2946 	struct ehci_xfer *ex = EXFER(xfer);
   2947 	ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
   2948 	/*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/
   2949 
   2950 	DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
   2951 		     xfer, xfer->actlen));
   2952 
   2953 	if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
   2954 		ehci_del_intr_list(ex);	/* remove from active list */
   2955 		ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
   2956 	}
   2957 
   2958 	DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen));
   2959 }
   2960 
   2961 /************************/
   2962 
   2963 Static usbd_status
   2964 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival)
   2965 {
   2966 	struct ehci_soft_islot *isp;
   2967 	int islot, lev;
   2968 
   2969 	/* Find a poll rate that is large enough. */
   2970 	for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--)
   2971 		if (EHCI_ILEV_IVAL(lev) <= ival)
   2972 			break;
   2973 
   2974 	/* Pick an interrupt slot at the right level. */
   2975 	/* XXX could do better than picking at random */
   2976 	sc->sc_rand = (sc->sc_rand + 191) % sc->sc_flsize;
   2977 	islot = EHCI_IQHIDX(lev, sc->sc_rand);
   2978 
   2979 	sqh->islot = islot;
   2980 	isp = &sc->sc_islots[islot];
   2981 	ehci_add_qh(sqh, isp->sqh);
   2982 
   2983 	return (USBD_NORMAL_COMPLETION);
   2984 }
   2985 
   2986 Static usbd_status
   2987 ehci_device_intr_transfer(usbd_xfer_handle xfer)
   2988 {
   2989 	usbd_status err;
   2990 
   2991 	/* Insert last in queue. */
   2992 	err = usb_insert_transfer(xfer);
   2993 	if (err)
   2994 		return (err);
   2995 
   2996 	/*
   2997 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   2998 	 * so start it first.
   2999 	 */
   3000 	return (ehci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   3001 }
   3002 
   3003 Static usbd_status
   3004 ehci_device_intr_start(usbd_xfer_handle xfer)
   3005 {
   3006 #define exfer EXFER(xfer)
   3007 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   3008 	usbd_device_handle dev = xfer->pipe->device;
   3009 	ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
   3010 	ehci_soft_qtd_t *data, *dataend;
   3011 	ehci_soft_qh_t *sqh;
   3012 	usbd_status err;
   3013 	int len, isread, endpt;
   3014 	int s;
   3015 
   3016 	DPRINTFN(2, ("ehci_device_intr_start: xfer=%p len=%d flags=%d\n",
   3017 	    xfer, xfer->length, xfer->flags));
   3018 
   3019 	if (sc->sc_dying)
   3020 		return (USBD_IOERROR);
   3021 
   3022 #ifdef DIAGNOSTIC
   3023 	if (xfer->rqflags & URQ_REQUEST)
   3024 		panic("ehci_device_intr_start: a request");
   3025 #endif
   3026 
   3027 	len = xfer->length;
   3028 	endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
   3029 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3030 	sqh = epipe->sqh;
   3031 
   3032 	epipe->u.intr.length = len;
   3033 
   3034 	err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
   3035 	    &dataend);
   3036 	if (err) {
   3037 		DPRINTFN(-1, ("ehci_device_intr_start: no memory\n"));
   3038 		xfer->status = err;
   3039 		usb_transfer_complete(xfer);
   3040 		return (err);
   3041 	}
   3042 
   3043 #ifdef EHCI_DEBUG
   3044 	if (ehcidebug > 5) {
   3045 		DPRINTF(("ehci_device_intr_start: data(1)\n"));
   3046 		ehci_dump_sqh(sqh);
   3047 		ehci_dump_sqtds(data);
   3048 	}
   3049 #endif
   3050 
   3051 	/* Set up interrupt info. */
   3052 	exfer->sqtdstart = data;
   3053 	exfer->sqtdend = dataend;
   3054 #ifdef DIAGNOSTIC
   3055 	if (!exfer->isdone) {
   3056 		printf("ehci_device_intr_start: not done, ex=%p\n", exfer);
   3057 	}
   3058 	exfer->isdone = 0;
   3059 #endif
   3060 
   3061 	s = splusb();
   3062 	ehci_set_qh_qtd(sqh, data);
   3063 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   3064 		usb_callout(xfer->timeout_handle, mstohz(xfer->timeout),
   3065 		    ehci_timeout, xfer);
   3066 	}
   3067 	ehci_add_intr_list(sc, exfer);
   3068 	xfer->status = USBD_IN_PROGRESS;
   3069 	splx(s);
   3070 
   3071 #ifdef EHCI_DEBUG
   3072 	if (ehcidebug > 10) {
   3073 		DPRINTF(("ehci_device_intr_start: data(2)\n"));
   3074 		delay(10000);
   3075 		DPRINTF(("ehci_device_intr_start: data(3)\n"));
   3076 		ehci_dump_regs(sc);
   3077 		printf("sqh:\n");
   3078 		ehci_dump_sqh(sqh);
   3079 		ehci_dump_sqtds(data);
   3080 	}
   3081 #endif
   3082 
   3083 	if (sc->sc_bus.use_polling)
   3084 		ehci_waitintr(sc, xfer);
   3085 
   3086 	return (USBD_IN_PROGRESS);
   3087 #undef exfer
   3088 }
   3089 
   3090 Static void
   3091 ehci_device_intr_abort(usbd_xfer_handle xfer)
   3092 {
   3093 	DPRINTFN(1, ("ehci_device_intr_abort: xfer=%p\n", xfer));
   3094 	if (xfer->pipe->intrxfer == xfer) {
   3095 		DPRINTFN(1, ("echi_device_intr_abort: remove\n"));
   3096 		xfer->pipe->intrxfer = NULL;
   3097 	}
   3098 	ehci_abort_xfer(xfer, USBD_CANCELLED);
   3099 }
   3100 
   3101 Static void
   3102 ehci_device_intr_close(usbd_pipe_handle pipe)
   3103 {
   3104 	ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
   3105 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   3106 	struct ehci_soft_islot *isp;
   3107 
   3108 	isp = &sc->sc_islots[epipe->sqh->islot];
   3109 	ehci_close_pipe(pipe, isp->sqh);
   3110 }
   3111 
   3112 Static void
   3113 ehci_device_intr_done(usbd_xfer_handle xfer)
   3114 {
   3115 #define exfer EXFER(xfer)
   3116 	struct ehci_xfer *ex = EXFER(xfer);
   3117 	ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
   3118 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
   3119 	ehci_soft_qtd_t *data, *dataend;
   3120 	ehci_soft_qh_t *sqh;
   3121 	usbd_status err;
   3122 	int len, isread, endpt, s;
   3123 
   3124 	DPRINTFN(10, ("ehci_device_intr_done: xfer=%p, actlen=%d\n",
   3125 	    xfer, xfer->actlen));
   3126 
   3127 	if (xfer->pipe->repeat) {
   3128 		ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
   3129 
   3130 		len = epipe->u.intr.length;
   3131 		xfer->length = len;
   3132 		endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
   3133 		isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3134 		sqh = epipe->sqh;
   3135 
   3136 		err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
   3137 		    &data, &dataend);
   3138 		if (err) {
   3139 			DPRINTFN(-1, ("ehci_device_intr_done: no memory\n"));
   3140 			xfer->status = err;
   3141 			return;
   3142 		}
   3143 
   3144 		/* Set up interrupt info. */
   3145 		exfer->sqtdstart = data;
   3146 		exfer->sqtdend = dataend;
   3147 #ifdef DIAGNOSTIC
   3148 		if (!exfer->isdone) {
   3149 			printf("ehci_device_intr_done: not done, ex=%p\n",
   3150 			    exfer);
   3151 		}
   3152 		exfer->isdone = 0;
   3153 #endif
   3154 
   3155 		s = splusb();
   3156 		ehci_set_qh_qtd(sqh, data);
   3157 		if (xfer->timeout && !sc->sc_bus.use_polling) {
   3158 			usb_callout(xfer->timeout_handle,
   3159 			    mstohz(xfer->timeout), ehci_timeout, xfer);
   3160 		}
   3161 		splx(s);
   3162 
   3163 		xfer->status = USBD_IN_PROGRESS;
   3164 	} else if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
   3165 		ehci_del_intr_list(ex); /* remove from active list */
   3166 		ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL);
   3167 	}
   3168 #undef exfer
   3169 }
   3170 
   3171 /************************/
   3172 
   3173 Static usbd_status	ehci_device_isoc_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   3174 Static usbd_status	ehci_device_isoc_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   3175 Static void		ehci_device_isoc_abort(usbd_xfer_handle xfer) { }
   3176 Static void		ehci_device_isoc_close(usbd_pipe_handle pipe) { }
   3177 Static void		ehci_device_isoc_done(usbd_xfer_handle xfer) { }
   3178