Home | History | Annotate | Line # | Download | only in usb
ehci.c revision 1.8
      1 /*	$NetBSD: ehci.c,v 1.8 2001/11/16 23:52:10 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 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).
      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 0.96 spec can be found at
     43  * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
     44  * and the USB 2.0 spec at
     45  * http://www.usb.org/developers/data/usb_20.zip
     46  *
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.8 2001/11/16 23:52:10 augustss Exp $");
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/kernel.h>
     55 #include <sys/malloc.h>
     56 #include <sys/device.h>
     57 #include <sys/select.h>
     58 #include <sys/proc.h>
     59 #include <sys/queue.h>
     60 
     61 #include <machine/bus.h>
     62 #include <machine/endian.h>
     63 
     64 #include <dev/usb/usb.h>
     65 #include <dev/usb/usbdi.h>
     66 #include <dev/usb/usbdivar.h>
     67 #include <dev/usb/usb_mem.h>
     68 #include <dev/usb/usb_quirks.h>
     69 
     70 #include <dev/usb/ehcireg.h>
     71 #include <dev/usb/ehcivar.h>
     72 
     73 #ifdef EHCI_DEBUG
     74 #define DPRINTF(x)	if (ehcidebug) printf x
     75 #define DPRINTFN(n,x)	if (ehcidebug>(n)) printf x
     76 int ehcidebug = 0;
     77 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
     78 #else
     79 #define DPRINTF(x)
     80 #define DPRINTFN(n,x)
     81 #endif
     82 
     83 struct ehci_pipe {
     84 	struct usbd_pipe pipe;
     85 };
     86 
     87 Static void		ehci_shutdown(void *);
     88 Static void		ehci_power(int, void *);
     89 
     90 Static usbd_status	ehci_open(usbd_pipe_handle);
     91 Static void		ehci_poll(struct usbd_bus *);
     92 Static void		ehci_softintr(void *);
     93 
     94 Static usbd_status	ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
     95 Static void		ehci_freem(struct usbd_bus *, usb_dma_t *);
     96 
     97 Static usbd_xfer_handle	ehci_allocx(struct usbd_bus *);
     98 Static void		ehci_freex(struct usbd_bus *, usbd_xfer_handle);
     99 
    100 Static usbd_status	ehci_root_ctrl_transfer(usbd_xfer_handle);
    101 Static usbd_status	ehci_root_ctrl_start(usbd_xfer_handle);
    102 Static void		ehci_root_ctrl_abort(usbd_xfer_handle);
    103 Static void		ehci_root_ctrl_close(usbd_pipe_handle);
    104 Static void		ehci_root_ctrl_done(usbd_xfer_handle);
    105 
    106 Static usbd_status	ehci_root_intr_transfer(usbd_xfer_handle);
    107 Static usbd_status	ehci_root_intr_start(usbd_xfer_handle);
    108 Static void		ehci_root_intr_abort(usbd_xfer_handle);
    109 Static void		ehci_root_intr_close(usbd_pipe_handle);
    110 Static void		ehci_root_intr_done(usbd_xfer_handle);
    111 
    112 Static usbd_status	ehci_device_ctrl_transfer(usbd_xfer_handle);
    113 Static usbd_status	ehci_device_ctrl_start(usbd_xfer_handle);
    114 Static void		ehci_device_ctrl_abort(usbd_xfer_handle);
    115 Static void		ehci_device_ctrl_close(usbd_pipe_handle);
    116 Static void		ehci_device_ctrl_done(usbd_xfer_handle);
    117 
    118 Static usbd_status	ehci_device_bulk_transfer(usbd_xfer_handle);
    119 Static usbd_status	ehci_device_bulk_start(usbd_xfer_handle);
    120 Static void		ehci_device_bulk_abort(usbd_xfer_handle);
    121 Static void		ehci_device_bulk_close(usbd_pipe_handle);
    122 Static void		ehci_device_bulk_done(usbd_xfer_handle);
    123 
    124 Static usbd_status	ehci_device_intr_transfer(usbd_xfer_handle);
    125 Static usbd_status	ehci_device_intr_start(usbd_xfer_handle);
    126 Static void		ehci_device_intr_abort(usbd_xfer_handle);
    127 Static void		ehci_device_intr_close(usbd_pipe_handle);
    128 Static void		ehci_device_intr_done(usbd_xfer_handle);
    129 
    130 Static usbd_status	ehci_device_isoc_transfer(usbd_xfer_handle);
    131 Static usbd_status	ehci_device_isoc_start(usbd_xfer_handle);
    132 Static void		ehci_device_isoc_abort(usbd_xfer_handle);
    133 Static void		ehci_device_isoc_close(usbd_pipe_handle);
    134 Static void		ehci_device_isoc_done(usbd_xfer_handle);
    135 
    136 Static void		ehci_device_clear_toggle(usbd_pipe_handle pipe);
    137 Static void		ehci_noop(usbd_pipe_handle pipe);
    138 
    139 Static int		ehci_str(usb_string_descriptor_t *, int, char *);
    140 Static void		ehci_pcd(ehci_softc_t *, usbd_xfer_handle);
    141 Static void		ehci_pcd_able(ehci_softc_t *, int);
    142 Static void		ehci_pcd_enable(void *);
    143 Static void		ehci_disown(ehci_softc_t *, int, int);
    144 
    145 #ifdef EHCI_DEBUG
    146 Static void		ehci_dumpregs(ehci_softc_t *);
    147 Static void		ehci_dump(void);
    148 Static ehci_softc_t 	*theehci;
    149 #endif
    150 
    151 #define EHCI_INTR_ENDPT 1
    152 
    153 Static struct usbd_bus_methods ehci_bus_methods = {
    154 	ehci_open,
    155 	ehci_softintr,
    156 	ehci_poll,
    157 	ehci_allocm,
    158 	ehci_freem,
    159 	ehci_allocx,
    160 	ehci_freex,
    161 };
    162 
    163 Static struct usbd_pipe_methods ehci_root_ctrl_methods = {
    164 	ehci_root_ctrl_transfer,
    165 	ehci_root_ctrl_start,
    166 	ehci_root_ctrl_abort,
    167 	ehci_root_ctrl_close,
    168 	ehci_noop,
    169 	ehci_root_ctrl_done,
    170 };
    171 
    172 Static struct usbd_pipe_methods ehci_root_intr_methods = {
    173 	ehci_root_intr_transfer,
    174 	ehci_root_intr_start,
    175 	ehci_root_intr_abort,
    176 	ehci_root_intr_close,
    177 	ehci_noop,
    178 	ehci_root_intr_done,
    179 };
    180 
    181 Static struct usbd_pipe_methods ehci_device_ctrl_methods = {
    182 	ehci_device_ctrl_transfer,
    183 	ehci_device_ctrl_start,
    184 	ehci_device_ctrl_abort,
    185 	ehci_device_ctrl_close,
    186 	ehci_noop,
    187 	ehci_device_ctrl_done,
    188 };
    189 
    190 Static struct usbd_pipe_methods ehci_device_intr_methods = {
    191 	ehci_device_intr_transfer,
    192 	ehci_device_intr_start,
    193 	ehci_device_intr_abort,
    194 	ehci_device_intr_close,
    195 	ehci_device_clear_toggle,
    196 	ehci_device_intr_done,
    197 };
    198 
    199 Static struct usbd_pipe_methods ehci_device_bulk_methods = {
    200 	ehci_device_bulk_transfer,
    201 	ehci_device_bulk_start,
    202 	ehci_device_bulk_abort,
    203 	ehci_device_bulk_close,
    204 	ehci_device_clear_toggle,
    205 	ehci_device_bulk_done,
    206 };
    207 
    208 Static struct usbd_pipe_methods ehci_device_isoc_methods = {
    209 	ehci_device_isoc_transfer,
    210 	ehci_device_isoc_start,
    211 	ehci_device_isoc_abort,
    212 	ehci_device_isoc_close,
    213 	ehci_noop,
    214 	ehci_device_isoc_done,
    215 };
    216 
    217 usbd_status
    218 ehci_init(ehci_softc_t *sc)
    219 {
    220 	u_int32_t version, sparams, cparams, hcr;
    221 	u_int i;
    222 	usbd_status err;
    223 
    224 	DPRINTF(("ehci_init: start\n"));
    225 #ifdef EHCI_DEBUG
    226 	theehci = sc;
    227 #endif
    228 
    229 	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
    230 
    231 	version = EREAD2(sc, EHCI_HCIVERSION);
    232 	printf("%s: EHCI version %x.%x\n", USBDEVNAME(sc->sc_bus.bdev),
    233 	       version >> 8, version & 0xff);
    234 
    235 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
    236 	DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
    237 	sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
    238 	if (EHCI_HCS_N_CC(sparams) != sc->sc_ncomp) {
    239 		printf("%s: wrong number of companions (%d != %d)\n",
    240 		       USBDEVNAME(sc->sc_bus.bdev),
    241 		       EHCI_HCS_N_CC(sparams), sc->sc_ncomp);
    242 		return (USBD_IOERROR);
    243 	}
    244 	if (sc->sc_ncomp > 0) {
    245 		printf("%s: companion controller%s, %d port%s each:",
    246 		    USBDEVNAME(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "",
    247 		    EHCI_HCS_N_PCC(sparams),
    248 		    EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
    249 		for (i = 0; i < sc->sc_ncomp; i++)
    250 			printf(" %s", USBDEVNAME(sc->sc_comps[i]->bdev));
    251 		printf("\n");
    252 	}
    253 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
    254 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
    255 	DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
    256 
    257 	sc->sc_bus.usbrev = USBREV_2_0;
    258 
    259 	/* Reset the controller */
    260 	DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
    261 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
    262 	usb_delay_ms(&sc->sc_bus, 1);
    263 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
    264 	for (i = 0; i < 100; i++) {
    265 		delay(10);
    266 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
    267 		if (!hcr)
    268 			break;
    269 	}
    270 	if (hcr) {
    271 		printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
    272 		return (USBD_IOERROR);
    273 	}
    274 
    275 	/* frame list size at default, read back what we got and use that */
    276 	switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
    277 	case 0: sc->sc_flsize = 1024*4; break;
    278 	case 1: sc->sc_flsize = 512*4; break;
    279 	case 2: sc->sc_flsize = 256*4; break;
    280 	case 3: return (USBD_IOERROR);
    281 	}
    282 	err = usb_allocmem(&sc->sc_bus, sc->sc_flsize,
    283 			   EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
    284 	if (err)
    285 		return (err);
    286 	DPRINTF(("%s: flsize=%d\n", USBDEVNAME(sc->sc_bus.bdev),sc->sc_flsize));
    287 
    288 	usb_callout_init(sc->sc_tmo_pcd);
    289 
    290 	/* Set up the bus struct. */
    291 	sc->sc_bus.methods = &ehci_bus_methods;
    292 	sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
    293 
    294 	sc->sc_powerhook = powerhook_establish(ehci_power, sc);
    295 	sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
    296 
    297 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
    298 
    299 	/* Enable interrupts */
    300 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    301 
    302 	/* Turn on controller */
    303 	EOWRITE4(sc, EHCI_USBCMD,
    304 		 EHCI_CMD_ITC_8 | /* 8 microframes */
    305 		 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
    306 		 /* EHCI_CMD_ASE | */
    307 		 /* EHCI_CMD_PSE | */
    308 		 EHCI_CMD_RS);
    309 
    310 	/* Take over port ownership */
    311 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
    312 
    313 	for (i = 0; i < 100; i++) {
    314 		delay(10);
    315 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
    316 		if (!hcr)
    317 			break;
    318 	}
    319 	if (hcr) {
    320 		printf("%s: run timeout\n", USBDEVNAME(sc->sc_bus.bdev));
    321 		return (USBD_IOERROR);
    322 	}
    323 
    324 	return (USBD_NORMAL_COMPLETION);
    325 }
    326 
    327 Static int ehci_intr1(ehci_softc_t *);
    328 
    329 int
    330 ehci_intr(void *v)
    331 {
    332 	ehci_softc_t *sc = v;
    333 
    334 	/* If we get an interrupt while polling, then just ignore it. */
    335 	if (sc->sc_bus.use_polling) {
    336 #ifdef DIAGNOSTIC
    337 		printf("ehci_intr: ignored interrupt while polling\n");
    338 #endif
    339 		return (0);
    340 	}
    341 
    342 	return (ehci_intr1(sc));
    343 }
    344 
    345 Static int
    346 ehci_intr1(ehci_softc_t *sc)
    347 {
    348 	u_int32_t intrs, eintrs;
    349 
    350 	DPRINTFN(20,("ehci_intr1: enter\n"));
    351 
    352 	/* In case the interrupt occurs before initialization has completed. */
    353 	if (sc == NULL) {
    354 #ifdef DIAGNOSTIC
    355 		printf("ehci_intr: sc == NULL\n");
    356 #endif
    357 		return (0);
    358 	}
    359 
    360 	intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
    361 
    362 	if (!intrs)
    363 		return (0);
    364 
    365 	EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
    366 	eintrs = intrs & sc->sc_eintrs;
    367 	DPRINTFN(7, ("ehci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
    368 		     sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
    369 		     (u_int)eintrs));
    370 	if (!eintrs)
    371 		return (0);
    372 
    373 	sc->sc_bus.intr_context++;
    374 	sc->sc_bus.no_intrs++;
    375 	if (eintrs & EHCI_STS_INT) {
    376 		DPRINTF(("ehci_intr1: something is done\n"));
    377 		eintrs &= ~EHCI_STS_INT;
    378 	}
    379 	if (eintrs & EHCI_STS_ERRINT) {
    380 		DPRINTF(("ehci_intr1: some error\n"));
    381 		eintrs &= ~EHCI_STS_HSE;
    382 	}
    383 	if (eintrs & EHCI_STS_HSE) {
    384 		printf("%s: unrecoverable error, controller halted\n",
    385 		       USBDEVNAME(sc->sc_bus.bdev));
    386 		/* XXX what else */
    387 	}
    388 	if (eintrs & EHCI_STS_PCD) {
    389 		ehci_pcd(sc, sc->sc_intrxfer);
    390 		/*
    391 		 * Disable PCD interrupt for now, because it will be
    392 		 * on until the port has been reset.
    393 		 */
    394 		ehci_pcd_able(sc, 0);
    395 		/* Do not allow RHSC interrupts > 1 per second */
    396                 usb_callout(sc->sc_tmo_pcd, hz, ehci_pcd_enable, sc);
    397 		eintrs &= ~EHCI_STS_PCD;
    398 	}
    399 
    400 	sc->sc_bus.intr_context--;
    401 
    402 	if (eintrs != 0) {
    403 		/* Block unprocessed interrupts. */
    404 		sc->sc_eintrs &= ~eintrs;
    405 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    406 		printf("%s: blocking intrs 0x%x\n",
    407 		       USBDEVNAME(sc->sc_bus.bdev), eintrs);
    408 	}
    409 
    410 	return (1);
    411 }
    412 
    413 void
    414 ehci_pcd_able(ehci_softc_t *sc, int on)
    415 {
    416 	DPRINTFN(4, ("ehci_pcd_able: on=%d\n", on));
    417 	if (on)
    418 		sc->sc_eintrs |= EHCI_STS_PCD;
    419 	else
    420 		sc->sc_eintrs &= ~EHCI_STS_PCD;
    421 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    422 }
    423 
    424 void
    425 ehci_pcd_enable(void *v_sc)
    426 {
    427 	ehci_softc_t *sc = v_sc;
    428 
    429 	ehci_pcd_able(sc, 1);
    430 }
    431 
    432 void
    433 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer)
    434 {
    435 	usbd_pipe_handle pipe;
    436 	struct ehci_pipe *opipe;
    437 	u_char *p;
    438 	int i, m;
    439 
    440 	if (xfer == NULL) {
    441 		/* Just ignore the change. */
    442 		return;
    443 	}
    444 
    445 	pipe = xfer->pipe;
    446 	opipe = (struct ehci_pipe *)pipe;
    447 
    448 	p = KERNADDR(&xfer->dmabuf);
    449 	m = min(sc->sc_noport, xfer->length * 8 - 1);
    450 	memset(p, 0, xfer->length);
    451 	for (i = 1; i <= m; i++) {
    452 		/* Pick out CHANGE bits from the status reg. */
    453 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
    454 			p[i/8] |= 1 << (i%8);
    455 	}
    456 	DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
    457 	xfer->actlen = xfer->length;
    458 	xfer->status = USBD_NORMAL_COMPLETION;
    459 
    460 	usb_transfer_complete(xfer);
    461 }
    462 
    463 void
    464 ehci_softintr(void *v)
    465 {
    466 	//ehci_softc_t *sc = v;
    467 }
    468 
    469 void
    470 ehci_poll(struct usbd_bus *bus)
    471 {
    472 	ehci_softc_t *sc = (ehci_softc_t *)bus;
    473 #ifdef EHCI_DEBUG
    474 	static int last;
    475 	int new;
    476 	new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
    477 	if (new != last) {
    478 		DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
    479 		last = new;
    480 	}
    481 #endif
    482 
    483 	if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs)
    484 		ehci_intr1(sc);
    485 }
    486 
    487 int
    488 ehci_detach(struct ehci_softc *sc, int flags)
    489 {
    490 	int rv = 0;
    491 
    492 	if (sc->sc_child != NULL)
    493 		rv = config_detach(sc->sc_child, flags);
    494 
    495 	if (rv != 0)
    496 		return (rv);
    497 
    498 	usb_uncallout(sc->sc_tmo_pcd, ehci_pcd_enable, sc);
    499 
    500 	if (sc->sc_powerhook != NULL)
    501 		powerhook_disestablish(sc->sc_powerhook);
    502 	if (sc->sc_shutdownhook != NULL)
    503 		shutdownhook_disestablish(sc->sc_shutdownhook);
    504 
    505 	/* XXX free other data structures XXX */
    506 
    507 	return (rv);
    508 }
    509 
    510 
    511 int
    512 ehci_activate(device_ptr_t self, enum devact act)
    513 {
    514 	struct ehci_softc *sc = (struct ehci_softc *)self;
    515 	int rv = 0;
    516 
    517 	switch (act) {
    518 	case DVACT_ACTIVATE:
    519 		return (EOPNOTSUPP);
    520 		break;
    521 
    522 	case DVACT_DEACTIVATE:
    523 		if (sc->sc_child != NULL)
    524 			rv = config_deactivate(sc->sc_child);
    525 		sc->sc_dying = 1;
    526 		break;
    527 	}
    528 	return (rv);
    529 }
    530 
    531 /*
    532  * Handle suspend/resume.
    533  *
    534  * We need to switch to polling mode here, because this routine is
    535  * called from an intterupt context.  This is all right since we
    536  * are almost suspended anyway.
    537  */
    538 void
    539 ehci_power(int why, void *v)
    540 {
    541 	ehci_softc_t *sc = v;
    542 	//u_int32_t ctl;
    543 	int s;
    544 
    545 #ifdef EHCI_DEBUG
    546 	DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
    547 	ehci_dumpregs(sc);
    548 #endif
    549 
    550 	s = splhardusb();
    551 	switch (why) {
    552 	case PWR_SUSPEND:
    553 	case PWR_STANDBY:
    554 		sc->sc_bus.use_polling++;
    555 #if 0
    556 OOO
    557 		ctl = OREAD4(sc, EHCI_CONTROL) & ~EHCI_HCFS_MASK;
    558 		if (sc->sc_control == 0) {
    559 			/*
    560 			 * Preserve register values, in case that APM BIOS
    561 			 * does not recover them.
    562 			 */
    563 			sc->sc_control = ctl;
    564 			sc->sc_intre = OREAD4(sc, EHCI_INTERRUPT_ENABLE);
    565 		}
    566 		ctl |= EHCI_HCFS_SUSPEND;
    567 		OWRITE4(sc, EHCI_CONTROL, ctl);
    568 #endif
    569 		usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
    570 		sc->sc_bus.use_polling--;
    571 		break;
    572 	case PWR_RESUME:
    573 		sc->sc_bus.use_polling++;
    574 #if 0
    575 OOO
    576 		/* Some broken BIOSes do not recover these values */
    577 		OWRITE4(sc, EHCI_HCCA, DMAADDR(&sc->sc_hccadma));
    578 		OWRITE4(sc, EHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
    579 		OWRITE4(sc, EHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
    580 		if (sc->sc_intre)
    581 			OWRITE4(sc, EHCI_INTERRUPT_ENABLE,
    582 				sc->sc_intre & (EHCI_ALL_INTRS | EHCI_MIE));
    583 		if (sc->sc_control)
    584 			ctl = sc->sc_control;
    585 		else
    586 			ctl = OREAD4(sc, EHCI_CONTROL);
    587 		ctl |= EHCI_HCFS_RESUME;
    588 		OWRITE4(sc, EHCI_CONTROL, ctl);
    589 		usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
    590 		ctl = (ctl & ~EHCI_HCFS_MASK) | EHCI_HCFS_OPERATIONAL;
    591 		OWRITE4(sc, EHCI_CONTROL, ctl);
    592 		usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
    593 		sc->sc_control = sc->sc_intre = 0;
    594 #endif
    595 		sc->sc_bus.use_polling--;
    596 		break;
    597 	case PWR_SOFTSUSPEND:
    598 	case PWR_SOFTSTANDBY:
    599 	case PWR_SOFTRESUME:
    600 		break;
    601 	}
    602 	splx(s);
    603 }
    604 
    605 /*
    606  * Shut down the controller when the system is going down.
    607  */
    608 void
    609 ehci_shutdown(void *v)
    610 {
    611 	ehci_softc_t *sc = v;
    612 
    613 	DPRINTF(("ehci_shutdown: stopping the HC\n"));
    614 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
    615 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
    616 }
    617 
    618 usbd_status
    619 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
    620 {
    621 	struct ehci_softc *sc = (struct ehci_softc *)bus;
    622 
    623 	return (usb_allocmem(&sc->sc_bus, size, 0, dma));
    624 }
    625 
    626 void
    627 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
    628 {
    629 	struct ehci_softc *sc = (struct ehci_softc *)bus;
    630 
    631 	usb_freemem(&sc->sc_bus, dma);
    632 }
    633 
    634 usbd_xfer_handle
    635 ehci_allocx(struct usbd_bus *bus)
    636 {
    637 	struct ehci_softc *sc = (struct ehci_softc *)bus;
    638 	usbd_xfer_handle xfer;
    639 
    640 	xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
    641 	if (xfer != NULL)
    642 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
    643 	else
    644 		xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
    645 	if (xfer != NULL)
    646 		memset(xfer, 0, sizeof *xfer);
    647 	return (xfer);
    648 }
    649 
    650 void
    651 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
    652 {
    653 	struct ehci_softc *sc = (struct ehci_softc *)bus;
    654 
    655 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
    656 }
    657 
    658 Static void
    659 ehci_device_clear_toggle(usbd_pipe_handle pipe)
    660 {
    661 #if 0
    662 OOO
    663 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
    664 
    665 	epipe->sed->ed.ed_headp &= htole32(~EHCI_TOGGLECARRY);
    666 #endif
    667 }
    668 
    669 Static void
    670 ehci_noop(usbd_pipe_handle pipe)
    671 {
    672 }
    673 
    674 #ifdef EHCI_DEBUG
    675 void
    676 ehci_dumpregs(ehci_softc_t *sc)
    677 {
    678 	int i;
    679 	printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
    680 	       EOREAD4(sc, EHCI_USBCMD),
    681 	       EOREAD4(sc, EHCI_USBSTS),
    682 	       EOREAD4(sc, EHCI_USBINTR));
    683 	for (i = 1; i <= sc->sc_noport; i++)
    684 		printf("port %d status=0x%08x\n", i,
    685 		       EOREAD4(sc, EHCI_PORTSC(i)));
    686 }
    687 
    688 void
    689 ehci_dump()
    690 {
    691 	ehci_dumpregs(theehci);
    692 }
    693 #endif
    694 
    695 usbd_status
    696 ehci_open(usbd_pipe_handle pipe)
    697 {
    698 	usbd_device_handle dev = pipe->device;
    699 	ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
    700 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
    701 	u_int8_t addr = dev->address;
    702 #if 0
    703 	u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
    704 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
    705 	ehci_soft_ed_t *sed;
    706 	ehci_soft_td_t *std;
    707 	ehci_soft_itd_t *sitd;
    708 	ehci_physaddr_t tdphys;
    709 	u_int32_t fmt;
    710 	usbd_status err;
    711 	int s;
    712 	int ival;
    713 #endif
    714 
    715 	DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
    716 		     pipe, addr, ed->bEndpointAddress, sc->sc_addr));
    717 
    718 	if (addr == sc->sc_addr) {
    719 		switch (ed->bEndpointAddress) {
    720 		case USB_CONTROL_ENDPOINT:
    721 			pipe->methods = &ehci_root_ctrl_methods;
    722 			break;
    723 		case UE_DIR_IN | EHCI_INTR_ENDPT:
    724 			pipe->methods = &ehci_root_intr_methods;
    725 			break;
    726 		default:
    727 			return (USBD_INVAL);
    728 		}
    729 	} else {
    730 #if 0
    731 		std = NULL;
    732 		sed = NULL;
    733 
    734 		sed = ehci_alloc_sed(sc);
    735 		if (sed == NULL)
    736 			goto bad0;
    737 		epipe->sed = sed;
    738 		if (xfertype == UE_ISOCHRONOUS) {
    739 			sitd = ehci_alloc_sitd(sc);
    740 			if (sitd == NULL) {
    741 				ehci_free_sitd(sc, sitd);
    742 				goto bad1;
    743 			}
    744 			epipe->tail.itd = sitd;
    745 			tdphys = sitd->physaddr;
    746 			fmt = EHCI_ED_FORMAT_ISO;
    747 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    748 				fmt |= EHCI_ED_DIR_IN;
    749 			else
    750 				fmt |= EHCI_ED_DIR_OUT;
    751 		} else {
    752 			std = ehci_alloc_std(sc);
    753 			if (std == NULL) {
    754 				ehci_free_std(sc, std);
    755 				goto bad1;
    756 			}
    757 			epipe->tail.td = std;
    758 			tdphys = std->physaddr;
    759 			fmt = EHCI_ED_FORMAT_GEN | EHCI_ED_DIR_TD;
    760 		}
    761 		sed->ed.ed_flags = htole32(
    762 			EHCI_ED_SET_FA(addr) |
    763 			EHCI_ED_SET_EN(ed->bEndpointAddress) |
    764 			(dev->lowspeed ? EHCI_ED_SPEED : 0) | fmt |
    765 			EHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
    766 		sed->ed.ed_headp = sed->ed.ed_tailp = htole32(tdphys);
    767 
    768 		switch (xfertype) {
    769 		case UE_CONTROL:
    770 			pipe->methods = &ehci_device_ctrl_methods;
    771 			err = usb_allocmem(&sc->sc_bus,
    772 				  sizeof(usb_device_request_t),
    773 				  0, &epipe->u.ctl.reqdma);
    774 			if (err)
    775 				goto bad;
    776 			s = splusb();
    777 			ehci_add_ed(sed, sc->sc_ctrl_head);
    778 			splx(s);
    779 			break;
    780 		case UE_INTERRUPT:
    781 			pipe->methods = &ehci_device_intr_methods;
    782 			ival = pipe->interval;
    783 			if (ival == USBD_DEFAULT_INTERVAL)
    784 				ival = ed->bInterval;
    785 			return (ehci_device_setintr(sc, epipe, ival));
    786 		case UE_ISOCHRONOUS:
    787 			pipe->methods = &ehci_device_isoc_methods;
    788 			return (ehci_setup_isoc(pipe));
    789 		case UE_BULK:
    790 			pipe->methods = &ehci_device_bulk_methods;
    791 			s = splusb();
    792 			ehci_add_ed(sed, sc->sc_bulk_head);
    793 			splx(s);
    794 			break;
    795 		}
    796 #else
    797 		return (USBD_IOERROR);
    798 #endif
    799 	}
    800 	return (USBD_NORMAL_COMPLETION);
    801 
    802 #if 0
    803  bad:
    804 	if (std != NULL)
    805 		ehci_free_std(sc, std);
    806  bad1:
    807 	if (sed != NULL)
    808 		ehci_free_sed(sc, sed);
    809  bad0:
    810 	return (USBD_NOMEM);
    811 #endif
    812 }
    813 
    814 /***********/
    815 
    816 /*
    817  * Data structures and routines to emulate the root hub.
    818  */
    819 Static usb_device_descriptor_t ehci_devd = {
    820 	USB_DEVICE_DESCRIPTOR_SIZE,
    821 	UDESC_DEVICE,		/* type */
    822 	{0x00, 0x02},		/* USB version */
    823 	UDCLASS_HUB,		/* class */
    824 	UDSUBCLASS_HUB,		/* subclass */
    825 	0,			/* protocol */
    826 	64,			/* max packet */
    827 	{0},{0},{0x00,0x01},	/* device id */
    828 	1,2,0,			/* string indicies */
    829 	1			/* # of configurations */
    830 };
    831 
    832 Static usb_config_descriptor_t ehci_confd = {
    833 	USB_CONFIG_DESCRIPTOR_SIZE,
    834 	UDESC_CONFIG,
    835 	{USB_CONFIG_DESCRIPTOR_SIZE +
    836 	 USB_INTERFACE_DESCRIPTOR_SIZE +
    837 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
    838 	1,
    839 	1,
    840 	0,
    841 	UC_SELF_POWERED,
    842 	0			/* max power */
    843 };
    844 
    845 Static usb_interface_descriptor_t ehci_ifcd = {
    846 	USB_INTERFACE_DESCRIPTOR_SIZE,
    847 	UDESC_INTERFACE,
    848 	0,
    849 	0,
    850 	1,
    851 	UICLASS_HUB,
    852 	UISUBCLASS_HUB,
    853 	0,
    854 	0
    855 };
    856 
    857 Static usb_endpoint_descriptor_t ehci_endpd = {
    858 	USB_ENDPOINT_DESCRIPTOR_SIZE,
    859 	UDESC_ENDPOINT,
    860 	UE_DIR_IN | EHCI_INTR_ENDPT,
    861 	UE_INTERRUPT,
    862 	{8, 0},			/* max packet */
    863 	255
    864 };
    865 
    866 Static usb_hub_descriptor_t ehci_hubd = {
    867 	USB_HUB_DESCRIPTOR_SIZE,
    868 	UDESC_HUB,
    869 	0,
    870 	{0,0},
    871 	0,
    872 	0,
    873 	{0},
    874 };
    875 
    876 Static int
    877 ehci_str(p, l, s)
    878 	usb_string_descriptor_t *p;
    879 	int l;
    880 	char *s;
    881 {
    882 	int i;
    883 
    884 	if (l == 0)
    885 		return (0);
    886 	p->bLength = 2 * strlen(s) + 2;
    887 	if (l == 1)
    888 		return (1);
    889 	p->bDescriptorType = UDESC_STRING;
    890 	l -= 2;
    891 	for (i = 0; s[i] && l > 1; i++, l -= 2)
    892 		USETW2(p->bString[i], 0, s[i]);
    893 	return (2*i+2);
    894 }
    895 
    896 /*
    897  * Simulate a hardware hub by handling all the necessary requests.
    898  */
    899 Static usbd_status
    900 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
    901 {
    902 	usbd_status err;
    903 
    904 	/* Insert last in queue. */
    905 	err = usb_insert_transfer(xfer);
    906 	if (err)
    907 		return (err);
    908 
    909 	/* Pipe isn't running, start first */
    910 	return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
    911 }
    912 
    913 Static usbd_status
    914 ehci_root_ctrl_start(usbd_xfer_handle xfer)
    915 {
    916 	ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
    917 	usb_device_request_t *req;
    918 	void *buf = NULL;
    919 	int port, i;
    920 	int s, len, value, index, l, totlen = 0;
    921 	usb_port_status_t ps;
    922 	usb_hub_descriptor_t hubd;
    923 	usbd_status err;
    924 	u_int32_t v;
    925 
    926 	if (sc->sc_dying)
    927 		return (USBD_IOERROR);
    928 
    929 #ifdef DIAGNOSTIC
    930 	if (!(xfer->rqflags & URQ_REQUEST))
    931 		/* XXX panic */
    932 		return (USBD_INVAL);
    933 #endif
    934 	req = &xfer->request;
    935 
    936 	DPRINTFN(4,("ehci_root_ctrl_control type=0x%02x request=%02x\n",
    937 		    req->bmRequestType, req->bRequest));
    938 
    939 	len = UGETW(req->wLength);
    940 	value = UGETW(req->wValue);
    941 	index = UGETW(req->wIndex);
    942 
    943 	if (len != 0)
    944 		buf = KERNADDR(&xfer->dmabuf);
    945 
    946 #define C(x,y) ((x) | ((y) << 8))
    947 	switch(C(req->bRequest, req->bmRequestType)) {
    948 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
    949 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
    950 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
    951 		/*
    952 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
    953 		 * for the integrated root hub.
    954 		 */
    955 		break;
    956 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
    957 		if (len > 0) {
    958 			*(u_int8_t *)buf = sc->sc_conf;
    959 			totlen = 1;
    960 		}
    961 		break;
    962 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
    963 		DPRINTFN(8,("ehci_root_ctrl_control wValue=0x%04x\n", value));
    964 		switch(value >> 8) {
    965 		case UDESC_DEVICE:
    966 			if ((value & 0xff) != 0) {
    967 				err = USBD_IOERROR;
    968 				goto ret;
    969 			}
    970 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
    971 			USETW(ehci_devd.idVendor, sc->sc_id_vendor);
    972 			memcpy(buf, &ehci_devd, l);
    973 			break;
    974 		case UDESC_CONFIG:
    975 			if ((value & 0xff) != 0) {
    976 				err = USBD_IOERROR;
    977 				goto ret;
    978 			}
    979 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
    980 			memcpy(buf, &ehci_confd, l);
    981 			buf = (char *)buf + l;
    982 			len -= l;
    983 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
    984 			totlen += l;
    985 			memcpy(buf, &ehci_ifcd, l);
    986 			buf = (char *)buf + l;
    987 			len -= l;
    988 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
    989 			totlen += l;
    990 			memcpy(buf, &ehci_endpd, l);
    991 			break;
    992 		case UDESC_STRING:
    993 			if (len == 0)
    994 				break;
    995 			*(u_int8_t *)buf = 0;
    996 			totlen = 1;
    997 			switch (value & 0xff) {
    998 			case 1: /* Vendor */
    999 				totlen = ehci_str(buf, len, sc->sc_vendor);
   1000 				break;
   1001 			case 2: /* Product */
   1002 				totlen = ehci_str(buf, len, "EHCI root hub");
   1003 				break;
   1004 			}
   1005 			break;
   1006 		default:
   1007 			err = USBD_IOERROR;
   1008 			goto ret;
   1009 		}
   1010 		break;
   1011 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   1012 		if (len > 0) {
   1013 			*(u_int8_t *)buf = 0;
   1014 			totlen = 1;
   1015 		}
   1016 		break;
   1017 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   1018 		if (len > 1) {
   1019 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   1020 			totlen = 2;
   1021 		}
   1022 		break;
   1023 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   1024 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   1025 		if (len > 1) {
   1026 			USETW(((usb_status_t *)buf)->wStatus, 0);
   1027 			totlen = 2;
   1028 		}
   1029 		break;
   1030 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   1031 		if (value >= USB_MAX_DEVICES) {
   1032 			err = USBD_IOERROR;
   1033 			goto ret;
   1034 		}
   1035 		sc->sc_addr = value;
   1036 		break;
   1037 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   1038 		if (value != 0 && value != 1) {
   1039 			err = USBD_IOERROR;
   1040 			goto ret;
   1041 		}
   1042 		sc->sc_conf = value;
   1043 		break;
   1044 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   1045 		break;
   1046 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   1047 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   1048 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   1049 		err = USBD_IOERROR;
   1050 		goto ret;
   1051 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   1052 		break;
   1053 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   1054 		break;
   1055 	/* Hub requests */
   1056 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   1057 		break;
   1058 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   1059 		DPRINTFN(8, ("ehci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
   1060 			     "port=%d feature=%d\n",
   1061 			     index, value));
   1062 		if (index < 1 || index > sc->sc_noport) {
   1063 			err = USBD_IOERROR;
   1064 			goto ret;
   1065 		}
   1066 		port = EHCI_PORTSC(index);
   1067 		v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
   1068 		switch(value) {
   1069 		case UHF_PORT_ENABLE:
   1070 			EOWRITE4(sc, port, v &~ EHCI_PS_PE);
   1071 			break;
   1072 		case UHF_PORT_SUSPEND:
   1073 			EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
   1074 			break;
   1075 		case UHF_PORT_POWER:
   1076 			EOWRITE4(sc, port, v &~ EHCI_PS_PP);
   1077 			break;
   1078 		case UHF_C_PORT_CONNECTION:
   1079 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
   1080 			break;
   1081 		case UHF_C_PORT_ENABLE:
   1082 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
   1083 			break;
   1084 		case UHF_C_PORT_SUSPEND:
   1085 			/* how? */
   1086 			break;
   1087 		case UHF_C_PORT_OVER_CURRENT:
   1088 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
   1089 			break;
   1090 		case UHF_C_PORT_RESET:
   1091 			sc->sc_isreset = 0;
   1092 			break;
   1093 		default:
   1094 			err = USBD_IOERROR;
   1095 			goto ret;
   1096 		}
   1097 #if 0
   1098 		switch(value) {
   1099 		case UHF_C_PORT_CONNECTION:
   1100 		case UHF_C_PORT_ENABLE:
   1101 		case UHF_C_PORT_SUSPEND:
   1102 		case UHF_C_PORT_OVER_CURRENT:
   1103 		case UHF_C_PORT_RESET:
   1104 			/* Enable RHSC interrupt if condition is cleared. */
   1105 			if ((OREAD4(sc, port) >> 16) == 0)
   1106 				ehci_pcd_able(sc, 1);
   1107 			break;
   1108 		default:
   1109 			break;
   1110 		}
   1111 #endif
   1112 		break;
   1113 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   1114 		if (value != 0) {
   1115 			err = USBD_IOERROR;
   1116 			goto ret;
   1117 		}
   1118 		hubd = ehci_hubd;
   1119 		hubd.bNbrPorts = sc->sc_noport;
   1120 		v = EOREAD4(sc, EHCI_HCSPARAMS);
   1121 		USETW(hubd.wHubCharacteristics,
   1122 		      EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH);
   1123 		hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
   1124 		for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
   1125 			hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
   1126 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
   1127 		l = min(len, hubd.bDescLength);
   1128 		totlen = l;
   1129 		memcpy(buf, &hubd, l);
   1130 		break;
   1131 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   1132 		if (len != 4) {
   1133 			err = USBD_IOERROR;
   1134 			goto ret;
   1135 		}
   1136 		memset(buf, 0, len); /* ? XXX */
   1137 		totlen = len;
   1138 		break;
   1139 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   1140 		DPRINTFN(8,("ehci_root_ctrl_transfer: get port status i=%d\n",
   1141 			    index));
   1142 		if (index < 1 || index > sc->sc_noport) {
   1143 			err = USBD_IOERROR;
   1144 			goto ret;
   1145 		}
   1146 		if (len != 4) {
   1147 			err = USBD_IOERROR;
   1148 			goto ret;
   1149 		}
   1150 		v = EOREAD4(sc, EHCI_PORTSC(index));
   1151 		DPRINTFN(8,("ehci_root_ctrl_transfer: port status=0x%04x\n",
   1152 			    v));
   1153 		i = 0;
   1154 		if (v & EHCI_PS_CS)	i |= UPS_CURRENT_CONNECT_STATUS;
   1155 		if (v & EHCI_PS_PE)	i |= UPS_PORT_ENABLED;
   1156 		if (v & EHCI_PS_SUSP)	i |= UPS_SUSPEND;
   1157 		if (v & EHCI_PS_OCA)	i |= UPS_OVERCURRENT_INDICATOR;
   1158 		if (v & EHCI_PS_PR)	i |= UPS_RESET;
   1159 		if (v & EHCI_PS_PP)	i |= UPS_PORT_POWER;
   1160 		USETW(ps.wPortStatus, i);
   1161 		i = 0;
   1162 		if (v & EHCI_PS_CSC)	i |= UPS_C_CONNECT_STATUS;
   1163 		if (v & EHCI_PS_PEC)	i |= UPS_C_PORT_ENABLED;
   1164 		if (v & EHCI_PS_OCC)	i |= UPS_C_OVERCURRENT_INDICATOR;
   1165 		if (sc->sc_isreset)	i |= UPS_C_PORT_RESET;
   1166 		USETW(ps.wPortChange, i);
   1167 		l = min(len, sizeof ps);
   1168 		memcpy(buf, &ps, l);
   1169 		totlen = l;
   1170 		break;
   1171 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   1172 		err = USBD_IOERROR;
   1173 		goto ret;
   1174 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   1175 		break;
   1176 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   1177 		if (index < 1 || index > sc->sc_noport) {
   1178 			err = USBD_IOERROR;
   1179 			goto ret;
   1180 		}
   1181 		port = EHCI_PORTSC(index);
   1182 		v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
   1183 		switch(value) {
   1184 		case UHF_PORT_ENABLE:
   1185 			EOWRITE4(sc, port, v | EHCI_PS_PE);
   1186 			break;
   1187 		case UHF_PORT_SUSPEND:
   1188 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
   1189 			break;
   1190 		case UHF_PORT_RESET:
   1191 			DPRINTFN(5,("ehci_root_ctrl_transfer: reset port %d\n",
   1192 				    index));
   1193 			if (EHCI_PS_IS_LOWSPEED(v)) {
   1194 				/* Low speed device, give up ownership. */
   1195 				ehci_disown(sc, index, 1);
   1196 				break;
   1197 			}
   1198 			/* Start reset sequence. */
   1199 			v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
   1200 			EOWRITE4(sc, port, v | EHCI_PS_PR);
   1201 			/* Wait for reset to complete. */
   1202 			usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY * 2);
   1203 			/* Terminate reset sequence. */
   1204 			EOWRITE4(sc, port, v);
   1205 			/* Wait for HC to complete reset. */
   1206 			usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE * 2);
   1207 			v = EOREAD4(sc, port);
   1208 			DPRINTF(("ehci after reset, status=0x%08x\n", v));
   1209 			if (v & EHCI_PS_PR) {
   1210 				printf("%s: port reset timeout\n",
   1211 				       USBDEVNAME(sc->sc_bus.bdev));
   1212 				return (USBD_TIMEOUT);
   1213 			}
   1214 			if (!(v & EHCI_PS_PE)) {
   1215 				/* Not a high speed device, give up ownership.*/
   1216 				ehci_disown(sc, index, 0);
   1217 				break;
   1218 			}
   1219 			sc->sc_isreset = 1;
   1220 			DPRINTF(("ehci port %d reset, status = 0x%08x\n",
   1221 				 index, v));
   1222 			break;
   1223 		case UHF_PORT_POWER:
   1224 			DPRINTFN(2,("ehci_root_ctrl_transfer: set port power "
   1225 				    "%d\n", index));
   1226 			EOWRITE4(sc, port, v | EHCI_PS_PP);
   1227 			break;
   1228 		default:
   1229 			err = USBD_IOERROR;
   1230 			goto ret;
   1231 		}
   1232 		break;
   1233 	default:
   1234 		err = USBD_IOERROR;
   1235 		goto ret;
   1236 	}
   1237 	xfer->actlen = totlen;
   1238 	err = USBD_NORMAL_COMPLETION;
   1239  ret:
   1240 	xfer->status = err;
   1241 	s = splusb();
   1242 	usb_transfer_complete(xfer);
   1243 	splx(s);
   1244 	return (USBD_IN_PROGRESS);
   1245 }
   1246 
   1247 void
   1248 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
   1249 {
   1250 	int i, port;
   1251 	u_int32_t v;
   1252 
   1253 	DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
   1254 #ifdef DIAGNOSTIC
   1255 	if (sc->sc_npcomp != 0) {
   1256 		i = (index-1) / sc->sc_npcomp;
   1257 		if (i >= sc->sc_ncomp)
   1258 			printf("%s: strange port\n",
   1259 			       USBDEVNAME(sc->sc_bus.bdev));
   1260 		else
   1261 			printf("%s: handing over %s speed device on "
   1262 			       "port %d to %s\n",
   1263 			       USBDEVNAME(sc->sc_bus.bdev),
   1264 			       lowspeed ? "low" : "full",
   1265 			       index, USBDEVNAME(sc->sc_comps[i]->bdev));
   1266 	} else {
   1267 		printf("%s: npcomp == 0\n", USBDEVNAME(sc->sc_bus.bdev));
   1268 	}
   1269 #endif
   1270 	port = EHCI_PORTSC(index);
   1271 	v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
   1272 	EOWRITE4(sc, port, v | EHCI_PS_PO);
   1273 }
   1274 
   1275 /* Abort a root control request. */
   1276 Static void
   1277 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
   1278 {
   1279 	/* Nothing to do, all transfers are synchronous. */
   1280 }
   1281 
   1282 /* Close the root pipe. */
   1283 Static void
   1284 ehci_root_ctrl_close(usbd_pipe_handle pipe)
   1285 {
   1286 	DPRINTF(("ehci_root_ctrl_close\n"));
   1287 	/* Nothing to do. */
   1288 }
   1289 
   1290 void
   1291 ehci_root_intr_done(usbd_xfer_handle xfer)
   1292 {
   1293 	xfer->hcpriv = NULL;
   1294 }
   1295 
   1296 Static usbd_status
   1297 ehci_root_intr_transfer(usbd_xfer_handle xfer)
   1298 {
   1299 	usbd_status err;
   1300 
   1301 	/* Insert last in queue. */
   1302 	err = usb_insert_transfer(xfer);
   1303 	if (err)
   1304 		return (err);
   1305 
   1306 	/* Pipe isn't running, start first */
   1307 	return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1308 }
   1309 
   1310 Static usbd_status
   1311 ehci_root_intr_start(usbd_xfer_handle xfer)
   1312 {
   1313 	usbd_pipe_handle pipe = xfer->pipe;
   1314 	ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
   1315 
   1316 	if (sc->sc_dying)
   1317 		return (USBD_IOERROR);
   1318 
   1319 	sc->sc_intrxfer = xfer;
   1320 
   1321 	return (USBD_IN_PROGRESS);
   1322 }
   1323 
   1324 /* Abort a root interrupt request. */
   1325 Static void
   1326 ehci_root_intr_abort(usbd_xfer_handle xfer)
   1327 {
   1328 	int s;
   1329 
   1330 	if (xfer->pipe->intrxfer == xfer) {
   1331 		DPRINTF(("ehci_root_intr_abort: remove\n"));
   1332 		xfer->pipe->intrxfer = NULL;
   1333 	}
   1334 	xfer->status = USBD_CANCELLED;
   1335 	s = splusb();
   1336 	usb_transfer_complete(xfer);
   1337 	splx(s);
   1338 }
   1339 
   1340 /* Close the root pipe. */
   1341 Static void
   1342 ehci_root_intr_close(usbd_pipe_handle pipe)
   1343 {
   1344 	ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
   1345 
   1346 	DPRINTF(("ehci_root_intr_close\n"));
   1347 
   1348 	sc->sc_intrxfer = NULL;
   1349 }
   1350 
   1351 void
   1352 ehci_root_ctrl_done(usbd_xfer_handle xfer)
   1353 {
   1354 	xfer->hcpriv = NULL;
   1355 }
   1356 
   1357 /************************/
   1358 
   1359 Static usbd_status	ehci_device_ctrl_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   1360 Static usbd_status	ehci_device_ctrl_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   1361 Static void		ehci_device_ctrl_abort(usbd_xfer_handle xfer) { }
   1362 Static void		ehci_device_ctrl_close(usbd_pipe_handle pipe) { }
   1363 Static void		ehci_device_ctrl_done(usbd_xfer_handle xfer) { }
   1364 
   1365 Static usbd_status	ehci_device_bulk_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   1366 Static usbd_status	ehci_device_bulk_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   1367 Static void		ehci_device_bulk_abort(usbd_xfer_handle xfer) { }
   1368 Static void		ehci_device_bulk_close(usbd_pipe_handle pipe) { }
   1369 Static void		ehci_device_bulk_done(usbd_xfer_handle xfer) { }
   1370 
   1371 Static usbd_status	ehci_device_intr_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   1372 Static usbd_status	ehci_device_intr_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   1373 Static void		ehci_device_intr_abort(usbd_xfer_handle xfer) { }
   1374 Static void		ehci_device_intr_close(usbd_pipe_handle pipe) { }
   1375 Static void		ehci_device_intr_done(usbd_xfer_handle xfer) { }
   1376 
   1377 Static usbd_status	ehci_device_isoc_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   1378 Static usbd_status	ehci_device_isoc_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
   1379 Static void		ehci_device_isoc_abort(usbd_xfer_handle xfer) { }
   1380 Static void		ehci_device_isoc_close(usbd_pipe_handle pipe) { }
   1381 Static void		ehci_device_isoc_done(usbd_xfer_handle xfer) { }
   1382