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