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