Home | History | Annotate | Line # | Download | only in usb
uhci.c revision 1.82
      1 /*	$NetBSD: uhci.c,v 1.82 2000/01/26 10:04:38 augustss Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/uhci.c,v 1.33 1999/11/17 22:33:41 n_hibma Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (augustss (at) carlstedt.se) at
     10  * Carlstedt Research & Technology.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * USB Universal Host Controller driver.
     43  * Handles e.g. PIIX3 and PIIX4.
     44  *
     45  * UHCI spec: http://www.intel.com/design/usb/uhci11d.pdf
     46  * USB spec: http://www.usb.org/developers/data/usb11.pdf
     47  * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
     48  *             ftp://download.intel.com/design/intarch/datashts/29056201.pdf
     49  */
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/malloc.h>
     55 #if defined(__NetBSD__) || defined(__OpenBSD__)
     56 #include <sys/device.h>
     57 #include <sys/select.h>
     58 #elif defined(__FreeBSD__)
     59 #include <sys/module.h>
     60 #include <sys/bus.h>
     61 #include <machine/bus_pio.h>
     62 #if defined(DIAGNOSTIC) && defined(__i386__)
     63 #include <machine/cpu.h>
     64 #endif
     65 #endif
     66 #include <sys/proc.h>
     67 #include <sys/queue.h>
     68 
     69 #include <machine/bus.h>
     70 #include <machine/endian.h>
     71 
     72 #include <dev/usb/usb.h>
     73 #include <dev/usb/usbdi.h>
     74 #include <dev/usb/usbdivar.h>
     75 #include <dev/usb/usb_mem.h>
     76 #include <dev/usb/usb_quirks.h>
     77 
     78 #include <dev/usb/uhcireg.h>
     79 #include <dev/usb/uhcivar.h>
     80 
     81 #if defined(__FreeBSD__)
     82 #include <machine/clock.h>
     83 
     84 #define delay(d)		DELAY(d)
     85 #endif
     86 
     87 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
     88 
     89 #if defined(__OpenBSD__)
     90 struct cfdriver uhci_cd = {
     91 	NULL, "uhci", DV_DULL
     92 };
     93 #endif
     94 
     95 #ifdef UHCI_DEBUG
     96 #define DPRINTF(x)	if (uhcidebug) printf x
     97 #define DPRINTFN(n,x)	if (uhcidebug>(n)) printf x
     98 int uhcidebug = 0;
     99 #else
    100 #define DPRINTF(x)
    101 #define DPRINTFN(n,x)
    102 #endif
    103 
    104 /*
    105  * The UHCI controller is little endian, so on big endian machines
    106  * the data strored in memory needs to be swapped.
    107  */
    108 #if BYTE_ORDER == BIG_ENDIAN
    109 #define LE(x) (bswap32(x))
    110 #else
    111 #define LE(x) (x)
    112 #endif
    113 
    114 struct uhci_pipe {
    115 	struct usbd_pipe pipe;
    116 	uhci_intr_info_t *iinfo;
    117 	int nexttoggle;
    118 	/* Info needed for different pipe kinds. */
    119 	union {
    120 		/* Control pipe */
    121 		struct {
    122 			uhci_soft_qh_t *sqh;
    123 			usb_dma_t reqdma;
    124 			uhci_soft_td_t *setup, *stat;
    125 			u_int length;
    126 		} ctl;
    127 		/* Interrupt pipe */
    128 		struct {
    129 			int npoll;
    130 			uhci_soft_qh_t **qhs;
    131 		} intr;
    132 		/* Bulk pipe */
    133 		struct {
    134 			uhci_soft_qh_t *sqh;
    135 			u_int length;
    136 			int isread;
    137 		} bulk;
    138 		/* Iso pipe */
    139 		struct iso {
    140 			uhci_soft_td_t **stds;
    141 			int next, inuse;
    142 		} iso;
    143 	} u;
    144 };
    145 
    146 /*
    147  * The uhci_intr_info free list can be global since they contain
    148  * no dma specific data.  The other free lists do.
    149  */
    150 LIST_HEAD(, uhci_intr_info) uhci_ii_free;
    151 
    152 static void		uhci_busreset __P((uhci_softc_t *));
    153 static void		uhci_shutdown __P((void *v));
    154 static void		uhci_power __P((int, void *));
    155 static usbd_status	uhci_run __P((uhci_softc_t *, int run));
    156 static uhci_soft_td_t *uhci_alloc_std __P((uhci_softc_t *));
    157 static void		uhci_free_std __P((uhci_softc_t *, uhci_soft_td_t *));
    158 static uhci_soft_qh_t *uhci_alloc_sqh __P((uhci_softc_t *));
    159 static void		uhci_free_sqh __P((uhci_softc_t *, uhci_soft_qh_t *));
    160 static uhci_intr_info_t *uhci_alloc_intr_info __P((uhci_softc_t *));
    161 static void		uhci_free_intr_info __P((uhci_intr_info_t *ii));
    162 #if 0
    163 static void		uhci_enter_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *,
    164 				      uhci_intr_info_t *));
    165 static void		uhci_exit_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *));
    166 #endif
    167 
    168 static void		uhci_free_std_chain __P((uhci_softc_t *,
    169 					 uhci_soft_td_t *, uhci_soft_td_t *));
    170 static usbd_status	uhci_alloc_std_chain __P((struct uhci_pipe *,
    171 			    uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
    172 			    uhci_soft_td_t **, uhci_soft_td_t **));
    173 static void		uhci_timo __P((void *));
    174 static void		uhci_waitintr __P((uhci_softc_t *,
    175 			    usbd_xfer_handle));
    176 static void		uhci_check_intr __P((uhci_softc_t *,
    177 			    uhci_intr_info_t *));
    178 static void		uhci_idone __P((uhci_intr_info_t *));
    179 static void		uhci_abort_xfer __P((usbd_xfer_handle,
    180 			    usbd_status status));
    181 static void		uhci_abort_xfer_end __P((void *v));
    182 static void		uhci_timeout __P((void *));
    183 static void		uhci_lock_frames __P((uhci_softc_t *));
    184 static void		uhci_unlock_frames __P((uhci_softc_t *));
    185 static void		uhci_add_ctrl __P((uhci_softc_t *, uhci_soft_qh_t *));
    186 static void		uhci_add_bulk __P((uhci_softc_t *, uhci_soft_qh_t *));
    187 static void		uhci_remove_ctrl __P((uhci_softc_t *,uhci_soft_qh_t *));
    188 static void		uhci_remove_bulk __P((uhci_softc_t *,uhci_soft_qh_t *));
    189 static int		uhci_str __P((usb_string_descriptor_t *, int, char *));
    190 
    191 static usbd_status	uhci_setup_isoc __P((usbd_pipe_handle pipe));
    192 static void		uhci_device_isoc_enter __P((usbd_xfer_handle));
    193 
    194 static usbd_status	uhci_allocm __P((struct usbd_bus *, usb_dma_t *,
    195 			    u_int32_t));
    196 static void		uhci_freem __P((struct usbd_bus *, usb_dma_t *));
    197 
    198 static usbd_xfer_handle	uhci_allocx __P((struct usbd_bus *));
    199 static void		uhci_freex __P((struct usbd_bus *, usbd_xfer_handle));
    200 
    201 static usbd_status	uhci_device_ctrl_transfer __P((usbd_xfer_handle));
    202 static usbd_status	uhci_device_ctrl_start __P((usbd_xfer_handle));
    203 static void		uhci_device_ctrl_abort __P((usbd_xfer_handle));
    204 static void		uhci_device_ctrl_close __P((usbd_pipe_handle));
    205 static void		uhci_device_ctrl_done  __P((usbd_xfer_handle));
    206 
    207 static usbd_status	uhci_device_intr_transfer __P((usbd_xfer_handle));
    208 static usbd_status	uhci_device_intr_start __P((usbd_xfer_handle));
    209 static void		uhci_device_intr_abort __P((usbd_xfer_handle));
    210 static void		uhci_device_intr_close __P((usbd_pipe_handle));
    211 static void		uhci_device_intr_done  __P((usbd_xfer_handle));
    212 
    213 static usbd_status	uhci_device_bulk_transfer __P((usbd_xfer_handle));
    214 static usbd_status	uhci_device_bulk_start __P((usbd_xfer_handle));
    215 static void		uhci_device_bulk_abort __P((usbd_xfer_handle));
    216 static void		uhci_device_bulk_close __P((usbd_pipe_handle));
    217 static void		uhci_device_bulk_done  __P((usbd_xfer_handle));
    218 
    219 static usbd_status	uhci_device_isoc_transfer __P((usbd_xfer_handle));
    220 static usbd_status	uhci_device_isoc_start __P((usbd_xfer_handle));
    221 static void		uhci_device_isoc_abort __P((usbd_xfer_handle));
    222 static void		uhci_device_isoc_close __P((usbd_pipe_handle));
    223 static void		uhci_device_isoc_done  __P((usbd_xfer_handle));
    224 
    225 static usbd_status	uhci_root_ctrl_transfer __P((usbd_xfer_handle));
    226 static usbd_status	uhci_root_ctrl_start __P((usbd_xfer_handle));
    227 static void		uhci_root_ctrl_abort __P((usbd_xfer_handle));
    228 static void		uhci_root_ctrl_close __P((usbd_pipe_handle));
    229 
    230 static usbd_status	uhci_root_intr_transfer __P((usbd_xfer_handle));
    231 static usbd_status	uhci_root_intr_start __P((usbd_xfer_handle));
    232 static void		uhci_root_intr_abort __P((usbd_xfer_handle));
    233 static void		uhci_root_intr_close __P((usbd_pipe_handle));
    234 static void		uhci_root_intr_done  __P((usbd_xfer_handle));
    235 
    236 static usbd_status	uhci_open __P((usbd_pipe_handle));
    237 static void		uhci_poll __P((struct usbd_bus *));
    238 
    239 static usbd_status	uhci_device_request __P((usbd_xfer_handle xfer));
    240 
    241 static void		uhci_add_intr __P((uhci_softc_t *, int,
    242 			    uhci_soft_qh_t *));
    243 static void		uhci_remove_intr __P((uhci_softc_t *, int,
    244 			    uhci_soft_qh_t *));
    245 static usbd_status	uhci_device_setintr __P((uhci_softc_t *sc,
    246 			    struct uhci_pipe *pipe, int ival));
    247 
    248 static void		uhci_device_clear_toggle __P((usbd_pipe_handle pipe));
    249 static void		uhci_noop __P((usbd_pipe_handle pipe));
    250 
    251 #ifdef UHCI_DEBUG
    252 static void		uhci_dumpregs __P((uhci_softc_t *));
    253 static void		uhci_dump_qhs __P((uhci_soft_qh_t *));
    254 static void		uhci_dump_qh __P((uhci_soft_qh_t *));
    255 static void		uhci_dump_tds __P((uhci_soft_td_t *));
    256 static void		uhci_dump_td __P((uhci_soft_td_t *));
    257 #endif
    258 
    259 #define UWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
    260 #define UWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
    261 #define UREAD1(sc, r) bus_space_read_1((sc)->iot, (sc)->ioh, (r))
    262 #define UREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
    263 #define UREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
    264 
    265 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
    266 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
    267 
    268 #define UHCI_RESET_TIMEOUT 100	/* reset timeout */
    269 
    270 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
    271 
    272 #define UHCI_INTR_ENDPT 1
    273 
    274 struct usbd_bus_methods uhci_bus_methods = {
    275 	uhci_open,
    276 	uhci_poll,
    277 	uhci_allocm,
    278 	uhci_freem,
    279 	uhci_allocx,
    280 	uhci_freex,
    281 };
    282 
    283 struct usbd_pipe_methods uhci_root_ctrl_methods = {
    284 	uhci_root_ctrl_transfer,
    285 	uhci_root_ctrl_start,
    286 	uhci_root_ctrl_abort,
    287 	uhci_root_ctrl_close,
    288 	uhci_noop,
    289 	0,
    290 };
    291 
    292 struct usbd_pipe_methods uhci_root_intr_methods = {
    293 	uhci_root_intr_transfer,
    294 	uhci_root_intr_start,
    295 	uhci_root_intr_abort,
    296 	uhci_root_intr_close,
    297 	uhci_noop,
    298 	uhci_root_intr_done,
    299 };
    300 
    301 struct usbd_pipe_methods uhci_device_ctrl_methods = {
    302 	uhci_device_ctrl_transfer,
    303 	uhci_device_ctrl_start,
    304 	uhci_device_ctrl_abort,
    305 	uhci_device_ctrl_close,
    306 	uhci_noop,
    307 	uhci_device_ctrl_done,
    308 };
    309 
    310 struct usbd_pipe_methods uhci_device_intr_methods = {
    311 	uhci_device_intr_transfer,
    312 	uhci_device_intr_start,
    313 	uhci_device_intr_abort,
    314 	uhci_device_intr_close,
    315 	uhci_device_clear_toggle,
    316 	uhci_device_intr_done,
    317 };
    318 
    319 struct usbd_pipe_methods uhci_device_bulk_methods = {
    320 	uhci_device_bulk_transfer,
    321 	uhci_device_bulk_start,
    322 	uhci_device_bulk_abort,
    323 	uhci_device_bulk_close,
    324 	uhci_device_clear_toggle,
    325 	uhci_device_bulk_done,
    326 };
    327 
    328 struct usbd_pipe_methods uhci_device_isoc_methods = {
    329 	uhci_device_isoc_transfer,
    330 	uhci_device_isoc_start,
    331 	uhci_device_isoc_abort,
    332 	uhci_device_isoc_close,
    333 	uhci_noop,
    334 	uhci_device_isoc_done,
    335 };
    336 
    337 void
    338 uhci_busreset(sc)
    339 	uhci_softc_t *sc;
    340 {
    341 	UHCICMD(sc, UHCI_CMD_GRESET);	/* global reset */
    342 	usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
    343 	UHCICMD(sc, 0);			/* do nothing */
    344 }
    345 
    346 usbd_status
    347 uhci_init(sc)
    348 	uhci_softc_t *sc;
    349 {
    350 	usbd_status err;
    351 	int i, j;
    352 	uhci_soft_qh_t *csqh, *bsqh, *sqh;
    353 	uhci_soft_td_t *std;
    354 
    355 	DPRINTFN(1,("uhci_init: start\n"));
    356 
    357 #ifdef UHCI_DEBUG
    358 	if (uhcidebug > 2)
    359 		uhci_dumpregs(sc);
    360 #endif
    361 
    362 	uhci_run(sc, 0);			/* stop the controller */
    363 	UWRITE2(sc, UHCI_INTR, 0);		/* disable interrupts */
    364 
    365 	uhci_busreset(sc);
    366 
    367 	/* Allocate and initialize real frame array. */
    368 	err = usb_allocmem(&sc->sc_bus,
    369 		  UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
    370 		  UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
    371 	if (err)
    372 		return (err);
    373 	sc->sc_pframes = KERNADDR(&sc->sc_dma);
    374 	UWRITE2(sc, UHCI_FRNUM, 0);		/* set frame number to 0 */
    375 	UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma)); /* set frame list*/
    376 
    377 	/* Allocate the dummy QH where bulk traffic will be queued. */
    378 	bsqh = uhci_alloc_sqh(sc);
    379 	if (bsqh == NULL)
    380 		return (USBD_NOMEM);
    381 	bsqh->qh.qh_hlink = LE(UHCI_PTR_T);	/* end of QH chain */
    382 	bsqh->qh.qh_elink = LE(UHCI_PTR_T);
    383 	sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
    384 
    385 	/* Allocate the dummy QH where control traffic will be queued. */
    386 	csqh = uhci_alloc_sqh(sc);
    387 	if (csqh == NULL)
    388 		return (USBD_NOMEM);
    389 	csqh->hlink = bsqh;
    390 	csqh->qh.qh_hlink = LE(bsqh->physaddr | UHCI_PTR_Q);
    391 	csqh->qh.qh_elink = LE(UHCI_PTR_T);
    392 	sc->sc_ctl_start = sc->sc_ctl_end = csqh;
    393 
    394 	/*
    395 	 * Make all (virtual) frame list pointers point to the interrupt
    396 	 * queue heads and the interrupt queue heads at the control
    397 	 * queue head and point the physical frame list to the virtual.
    398 	 */
    399 	for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
    400 		std = uhci_alloc_std(sc);
    401 		sqh = uhci_alloc_sqh(sc);
    402 		if (std == NULL || sqh == NULL)
    403 			return (USBD_NOMEM);
    404 		std->link.sqh = sqh;
    405 		std->td.td_link = LE(sqh->physaddr | UHCI_PTR_Q);
    406 		std->td.td_status = LE(UHCI_TD_IOS);	/* iso, inactive */
    407 		std->td.td_token = LE(0);
    408 		std->td.td_buffer = LE(0);
    409 		sqh->hlink = csqh;
    410 		sqh->qh.qh_hlink = LE(csqh->physaddr | UHCI_PTR_Q);
    411 		sqh->elink = 0;
    412 		sqh->qh.qh_elink = LE(UHCI_PTR_T);
    413 		sc->sc_vframes[i].htd = std;
    414 		sc->sc_vframes[i].etd = std;
    415 		sc->sc_vframes[i].hqh = sqh;
    416 		sc->sc_vframes[i].eqh = sqh;
    417 		for (j = i;
    418 		     j < UHCI_FRAMELIST_COUNT;
    419 		     j += UHCI_VFRAMELIST_COUNT)
    420 			sc->sc_pframes[j] = LE(std->physaddr);
    421 	}
    422 
    423 	LIST_INIT(&sc->sc_intrhead);
    424 
    425 	SIMPLEQ_INIT(&sc->sc_free_xfers);
    426 
    427 	/* Set up the bus struct. */
    428 	sc->sc_bus.methods = &uhci_bus_methods;
    429 	sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
    430 
    431 	sc->sc_suspend = PWR_RESUME;
    432 	sc->sc_powerhook = powerhook_establish(uhci_power, sc);
    433 
    434 	sc->sc_shutdownhook = shutdownhook_establish(uhci_shutdown, sc);
    435 
    436 	DPRINTFN(1,("uhci_init: enabling\n"));
    437 	UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
    438 		UHCI_INTR_IOCE | UHCI_INTR_SPIE);	/* enable interrupts */
    439 
    440 	UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
    441 
    442 	return (uhci_run(sc, 1));		/* and here we go... */
    443 }
    444 
    445 #if defined(__NetBSD__) || defined(__OpenBSD__)
    446 int
    447 uhci_activate(self, act)
    448 	device_ptr_t self;
    449 	enum devact act;
    450 {
    451 	struct uhci_softc *sc = (struct uhci_softc *)self;
    452 	int rv = 0;
    453 
    454 	switch (act) {
    455 	case DVACT_ACTIVATE:
    456 		return (EOPNOTSUPP);
    457 		break;
    458 
    459 	case DVACT_DEACTIVATE:
    460 		if (sc->sc_child != NULL)
    461 			rv = config_deactivate(sc->sc_child);
    462 		break;
    463 	}
    464 	return (rv);
    465 }
    466 
    467 int
    468 uhci_detach(sc, flags)
    469 	struct uhci_softc *sc;
    470 	int flags;
    471 {
    472 	usbd_xfer_handle xfer;
    473 	int rv = 0;
    474 
    475 	if (sc->sc_child != NULL)
    476 		rv = config_detach(sc->sc_child, flags);
    477 
    478 	if (rv != 0)
    479 		return (rv);
    480 
    481 	powerhook_disestablish(sc->sc_powerhook);
    482 	shutdownhook_disestablish(sc->sc_shutdownhook);
    483 
    484 	/* Free all xfers associated with this HC. */
    485 	for (;;) {
    486 		xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
    487 		if (xfer == NULL)
    488 			break;
    489 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
    490 		free(xfer, M_USB);
    491 	}
    492 
    493 	/* XXX free other data structures XXX */
    494 
    495 	return (rv);
    496 }
    497 #endif
    498 
    499 usbd_status
    500 uhci_allocm(bus, dma, size)
    501 	struct usbd_bus *bus;
    502 	usb_dma_t *dma;
    503 	u_int32_t size;
    504 {
    505 	return (usb_allocmem(&((struct uhci_softc *)bus)->sc_bus, size, 0,
    506 			     dma));
    507 }
    508 
    509 void
    510 uhci_freem(bus, dma)
    511 	struct usbd_bus *bus;
    512 	usb_dma_t *dma;
    513 {
    514 	usb_freemem(&((struct uhci_softc *)bus)->sc_bus, dma);
    515 }
    516 
    517 usbd_xfer_handle
    518 uhci_allocx(bus)
    519 	struct usbd_bus *bus;
    520 {
    521 	struct uhci_softc *sc = (struct uhci_softc *)bus;
    522 	usbd_xfer_handle xfer;
    523 
    524 	xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
    525 	if (xfer != NULL)
    526 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
    527 	else
    528 		xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
    529 	if (xfer != NULL)
    530 		memset(xfer, 0, sizeof *xfer);
    531 	return (xfer);
    532 }
    533 
    534 void
    535 uhci_freex(bus, xfer)
    536 	struct usbd_bus *bus;
    537 	usbd_xfer_handle xfer;
    538 {
    539 	struct uhci_softc *sc = (struct uhci_softc *)bus;
    540 
    541 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
    542 }
    543 
    544 /*
    545  * Shut down the controller when the system is going down.
    546  */
    547 void
    548 uhci_shutdown(v)
    549 	void *v;
    550 {
    551 	uhci_softc_t *sc = v;
    552 
    553 	DPRINTF(("uhci_shutdown: stopping the HC\n"));
    554 	uhci_run(sc, 0); /* stop the controller */
    555 }
    556 
    557 /*
    558  * Handle suspend/resume.
    559  *
    560  * We need to switch to polling mode here, because this routine is
    561  * called from an intterupt context.  This is all right since we
    562  * are almost suspended anyway.
    563  */
    564 void
    565 uhci_power(why, v)
    566 	int why;
    567 	void *v;
    568 {
    569 	uhci_softc_t *sc = v;
    570 	int cmd;
    571 	int s;
    572 
    573 	s = splusb();
    574 	cmd = UREAD2(sc, UHCI_CMD);
    575 
    576 	DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
    577 		 sc, why, sc->sc_suspend, cmd));
    578 
    579 	if (why != PWR_RESUME) {
    580 #ifdef UHCI_DEBUG
    581 		if (uhcidebug > 2)
    582 			uhci_dumpregs(sc);
    583 #endif
    584 		if (sc->sc_has_timo != NULL)
    585 			usb_untimeout(uhci_timo, sc->sc_has_timo,
    586 				      sc->sc_has_timo->timo_handle);
    587 		sc->sc_bus.use_polling++;
    588 		uhci_run(sc, 0); /* stop the controller */
    589 		UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
    590 		usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
    591 		sc->sc_suspend = why;
    592 		sc->sc_bus.use_polling--;
    593 		DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
    594 	} else {
    595 		/*
    596 		 * XXX We should really do much more here in case the
    597 		 * controller registers have been lost and BIOS has
    598 		 * not restored them.
    599 		 */
    600 #ifdef DIAGNOSTIC
    601 		if (sc->sc_suspend == PWR_RESUME)
    602 			printf("uhci_power: weird, resume without suspend.\n");
    603 #endif
    604 		sc->sc_bus.use_polling++;
    605 		sc->sc_suspend = why;
    606 		if (cmd & UHCI_CMD_RS)
    607 			uhci_run(sc, 0); /* in case BIOS has started it */
    608 		UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
    609 		usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
    610 		UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
    611 		UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
    612 			UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
    613 		uhci_run(sc, 1); /* and start traffic again */
    614 		usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
    615 		sc->sc_bus.use_polling--;
    616 		if (sc->sc_has_timo != NULL)
    617 			usb_timeout(uhci_timo, sc->sc_has_timo,
    618 				    sc->sc_ival, sc->sc_has_timo->timo_handle);
    619 #ifdef UHCI_DEBUG
    620 		if (uhcidebug > 2)
    621 			uhci_dumpregs(sc);
    622 #endif
    623 	}
    624 	splx(s);
    625 }
    626 
    627 #ifdef UHCI_DEBUG
    628 static void
    629 uhci_dumpregs(sc)
    630 	uhci_softc_t *sc;
    631 {
    632 	DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
    633 		     "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
    634 		     USBDEVNAME(sc->sc_bus.bdev),
    635 		     UREAD2(sc, UHCI_CMD),
    636 		     UREAD2(sc, UHCI_STS),
    637 		     UREAD2(sc, UHCI_INTR),
    638 		     UREAD2(sc, UHCI_FRNUM),
    639 		     UREAD4(sc, UHCI_FLBASEADDR),
    640 		     UREAD1(sc, UHCI_SOF),
    641 		     UREAD2(sc, UHCI_PORTSC1),
    642 		     UREAD2(sc, UHCI_PORTSC2)));
    643 }
    644 
    645 void
    646 uhci_dump_td(p)
    647 	uhci_soft_td_t *p;
    648 {
    649 	DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
    650 		     "token=0x%08lx buffer=0x%08lx\n",
    651 		     p, (long)p->physaddr,
    652 		     (long)LE(p->td.td_link),
    653 		     (long)LE(p->td.td_status),
    654 		     (long)LE(p->td.td_token),
    655 		     (long)LE(p->td.td_buffer)));
    656 	DPRINTFN(-1,("  %b %b,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
    657 		     "D=%d,maxlen=%d\n",
    658 		     (int)LE(p->td.td_link),
    659 		     "\20\1T\2Q\3VF",
    660 		     (int)LE(p->td.td_status),
    661 		     "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
    662 		     "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
    663 		     UHCI_TD_GET_ERRCNT(LE(p->td.td_status)),
    664 		     UHCI_TD_GET_ACTLEN(LE(p->td.td_status)),
    665 		     UHCI_TD_GET_PID(LE(p->td.td_token)),
    666 		     UHCI_TD_GET_DEVADDR(LE(p->td.td_token)),
    667 		     UHCI_TD_GET_ENDPT(LE(p->td.td_token)),
    668 		     UHCI_TD_GET_DT(LE(p->td.td_token)),
    669 		     UHCI_TD_GET_MAXLEN(LE(p->td.td_token))));
    670 }
    671 
    672 void
    673 uhci_dump_qh(sqh)
    674 	uhci_soft_qh_t *sqh;
    675 {
    676 	DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
    677 	    (int)sqh->physaddr, LE(sqh->qh.qh_hlink), LE(sqh->qh.qh_elink)));
    678 }
    679 
    680 
    681 #if 0
    682 void
    683 uhci_dump()
    684 {
    685 	uhci_softc_t *sc = uhci;
    686 
    687 	uhci_dumpregs(sc);
    688 	printf("intrs=%d\n", sc->sc_bus.no_intrs);
    689 	printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);
    690 	uhci_dump_qh(sc->sc_ctl_start->qh.hlink);
    691 }
    692 #endif
    693 
    694 
    695 void
    696 uhci_dump_qhs(sqh)
    697 	uhci_soft_qh_t *sqh;
    698 {
    699 	uhci_dump_qh(sqh);
    700 
    701 	/* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
    702 	 * Traverses sideways first, then down.
    703 	 *
    704 	 * QH1
    705 	 * QH2
    706 	 * No QH
    707 	 * TD2.1
    708 	 * TD2.2
    709 	 * TD1.1
    710 	 * etc.
    711 	 *
    712 	 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
    713 	 */
    714 
    715 
    716 	if (sqh->hlink != NULL && !(sqh->qh.qh_hlink & UHCI_PTR_T))
    717 		uhci_dump_qhs(sqh->hlink);
    718 	else
    719 		DPRINTF(("No QH\n"));
    720 
    721 	if (sqh->elink != NULL && !(sqh->qh.qh_elink & UHCI_PTR_T))
    722 		uhci_dump_tds(sqh->elink);
    723 	else
    724 		DPRINTF(("No TD\n"));
    725 }
    726 
    727 void
    728 uhci_dump_tds(std)
    729 	uhci_soft_td_t *std;
    730 {
    731 	uhci_soft_td_t *td;
    732 
    733 	for(td = std; td != NULL; td = td->link.std) {
    734 		uhci_dump_td(td);
    735 
    736 		/* Check whether the link pointer in this TD marks
    737 		 * the link pointer as end of queue. This avoids
    738 		 * printing the free list in case the queue/TD has
    739 		 * already been moved there (seatbelt).
    740 		 */
    741 		if (td->td.td_link & UHCI_PTR_T ||
    742 		    td->td.td_link == 0)
    743 			break;
    744 	}
    745 }
    746 #endif
    747 
    748 /*
    749  * This routine is executed periodically and simulates interrupts
    750  * from the root controller interrupt pipe for port status change.
    751  */
    752 void
    753 uhci_timo(addr)
    754 	void *addr;
    755 {
    756 	usbd_xfer_handle xfer = addr;
    757 	usbd_pipe_handle pipe = xfer->pipe;
    758 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
    759 	int s;
    760 	u_char *p;
    761 
    762 	DPRINTFN(20, ("uhci_timo\n"));
    763 
    764 	usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
    765 
    766 	p = KERNADDR(&xfer->dmabuf);
    767 	p[0] = 0;
    768 	if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
    769 		p[0] |= 1<<1;
    770 	if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
    771 		p[0] |= 1<<2;
    772 	if (p[0] == 0)
    773 		/* No change, try again in a while */
    774 		return;
    775 
    776 	xfer->actlen = 1;
    777 	xfer->status = USBD_NORMAL_COMPLETION;
    778 	s = splusb();
    779 	xfer->hcpriv = 0;
    780 	xfer->device->bus->intr_context++;
    781 	usb_transfer_complete(xfer);
    782 	xfer->device->bus->intr_context--;
    783 	splx(s);
    784 }
    785 
    786 void
    787 uhci_root_intr_done(xfer)
    788 	usbd_xfer_handle xfer;
    789 {
    790 }
    791 
    792 void
    793 uhci_lock_frames(sc)
    794 	uhci_softc_t *sc;
    795 {
    796 	int s = splusb();
    797 
    798 	while (sc->sc_vflock & UHCI_HAS_LOCK) {
    799 		sc->sc_vflock |= UHCI_WANT_LOCK;
    800 		tsleep(&sc->sc_vflock, PRIBIO, "uhcqhl", 0);
    801 	}
    802 	sc->sc_vflock = UHCI_HAS_LOCK;
    803 	splx(s);
    804 }
    805 
    806 void
    807 uhci_unlock_frames(sc)
    808 	uhci_softc_t *sc;
    809 {
    810 	int s = splusb();
    811 
    812 	sc->sc_vflock &= ~UHCI_HAS_LOCK;
    813 	if (sc->sc_vflock & UHCI_WANT_LOCK)
    814 		wakeup(&sc->sc_vflock);
    815 	splx(s);
    816 }
    817 
    818 /*
    819  * Allocate an interrupt information struct.  A free list is kept
    820  * for fast allocation.
    821  */
    822 uhci_intr_info_t *
    823 uhci_alloc_intr_info(sc)
    824 	uhci_softc_t *sc;
    825 {
    826 	uhci_intr_info_t *ii;
    827 
    828 	ii = LIST_FIRST(&uhci_ii_free);
    829 	if (ii)
    830 		LIST_REMOVE(ii, list);
    831 	else {
    832 		ii = malloc(sizeof(uhci_intr_info_t), M_USBHC, M_NOWAIT);
    833 	}
    834 	ii->sc = sc;
    835 #if defined(__FreeBSD__)
    836 	callout_handle_init(&ii->timeout_handle);
    837 #endif
    838 
    839 	return ii;
    840 }
    841 
    842 void
    843 uhci_free_intr_info(ii)
    844 	uhci_intr_info_t *ii;
    845 {
    846 	LIST_INSERT_HEAD(&uhci_ii_free, ii, list); /* and put on free list */
    847 }
    848 
    849 /* Add control QH, called at splusb(). */
    850 void
    851 uhci_add_ctrl(sc, sqh)
    852 	uhci_softc_t *sc;
    853 	uhci_soft_qh_t *sqh;
    854 {
    855 	uhci_soft_qh_t *eqh;
    856 
    857 	SPLUSBCHECK;
    858 
    859 	DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
    860 	eqh = sc->sc_ctl_end;
    861 	sqh->hlink       = eqh->hlink;
    862 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
    863 	eqh->hlink       = sqh;
    864 	eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
    865 	sc->sc_ctl_end = sqh;
    866 }
    867 
    868 /* Remove control QH, called at splusb(). */
    869 void
    870 uhci_remove_ctrl(sc, sqh)
    871 	uhci_softc_t *sc;
    872 	uhci_soft_qh_t *sqh;
    873 {
    874 	uhci_soft_qh_t *pqh;
    875 
    876 	SPLUSBCHECK;
    877 
    878 	DPRINTFN(10, ("uhci_remove_ctrl: sqh=%p\n", sqh));
    879 	for (pqh = sc->sc_ctl_start; pqh->hlink != sqh; pqh=pqh->hlink)
    880 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
    881 		if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
    882 			printf("uhci_remove_ctrl: QH not found\n");
    883 			return;
    884 		}
    885 #else
    886 		;
    887 #endif
    888 	pqh->hlink       = sqh->hlink;
    889 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
    890 	if (sc->sc_ctl_end == sqh)
    891 		sc->sc_ctl_end = pqh;
    892 }
    893 
    894 /* Add bulk QH, called at splusb(). */
    895 void
    896 uhci_add_bulk(sc, sqh)
    897 	uhci_softc_t *sc;
    898 	uhci_soft_qh_t *sqh;
    899 {
    900 	uhci_soft_qh_t *eqh;
    901 
    902 	SPLUSBCHECK;
    903 
    904 	DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
    905 	eqh = sc->sc_bulk_end;
    906 	sqh->hlink       = eqh->hlink;
    907 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
    908 	eqh->hlink       = sqh;
    909 	eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
    910 	sc->sc_bulk_end = sqh;
    911 }
    912 
    913 /* Remove bulk QH, called at splusb(). */
    914 void
    915 uhci_remove_bulk(sc, sqh)
    916 	uhci_softc_t *sc;
    917 	uhci_soft_qh_t *sqh;
    918 {
    919 	uhci_soft_qh_t *pqh;
    920 
    921 	SPLUSBCHECK;
    922 
    923 	DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
    924 	for (pqh = sc->sc_bulk_start; pqh->hlink != sqh; pqh = pqh->hlink)
    925 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
    926 		if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
    927 			printf("uhci_remove_bulk: QH not found\n");
    928 			return;
    929 		}
    930 #else
    931 		;
    932 #endif
    933 	pqh->hlink       = sqh->hlink;
    934 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
    935 	if (sc->sc_bulk_end == sqh)
    936 		sc->sc_bulk_end = pqh;
    937 }
    938 
    939 int
    940 uhci_intr(arg)
    941 	void *arg;
    942 {
    943 	uhci_softc_t *sc = arg;
    944 	int status;
    945 	int ack;
    946 	uhci_intr_info_t *ii;
    947 
    948 #ifdef UHCI_DEBUG
    949 	if (uhcidebug > 15) {
    950 		DPRINTF(("%s: uhci_intr\n", USBDEVNAME(sc->sc_bus.bdev)));
    951 		uhci_dumpregs(sc);
    952 	}
    953 #endif
    954 
    955 	status = UREAD2(sc, UHCI_STS);
    956 	if (status == 0)	/* The interrupt was not for us. */
    957 		return (0);
    958 
    959 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
    960 	if (sc->sc_suspend != PWR_RESUME)
    961 		printf("uhci_intr: suspended sts=0x%x\n", status);
    962 #endif
    963 
    964 	ack = 0;
    965 	if (status & UHCI_STS_USBINT)
    966 		ack |= UHCI_STS_USBINT;
    967 	if (status & UHCI_STS_USBEI)
    968 		ack |= UHCI_STS_USBEI;
    969 	if (status & UHCI_STS_RD) {
    970 		ack |= UHCI_STS_RD;
    971 		printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
    972 	}
    973 	if (status & UHCI_STS_HSE) {
    974 		ack |= UHCI_STS_HSE;
    975 		printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
    976 	}
    977 	if (status & UHCI_STS_HCPE) {
    978 		ack |= UHCI_STS_HCPE;
    979 		printf("%s: host controller process error\n",
    980 		       USBDEVNAME(sc->sc_bus.bdev));
    981 	}
    982 	if (status & UHCI_STS_HCH) {
    983 		/* no acknowledge needed */
    984 		printf("%s: host controller halted\n",
    985 		       USBDEVNAME(sc->sc_bus.bdev));
    986 		sc->sc_dying = 1;
    987 	}
    988 
    989 	if (ack)	/* acknowledge the ints */
    990 		UWRITE2(sc, UHCI_STS, ack);
    991 	else	/* nothing to acknowledge */
    992 		return (0);
    993 
    994 	sc->sc_bus.intr_context++;
    995 	sc->sc_bus.no_intrs++;
    996 
    997 	/*
    998 	 * Interrupts on UHCI really suck.  When the host controller
    999 	 * interrupts because a transfer is completed there is no
   1000 	 * way of knowing which transfer it was.  You can scan down
   1001 	 * the TDs and QHs of the previous frame to limit the search,
   1002 	 * but that assumes that the interrupt was not delayed by more
   1003 	 * than 1 ms, which may not always be true (e.g. after debug
   1004 	 * output on a slow console).
   1005 	 * We scan all interrupt descriptors to see if any have
   1006 	 * completed.
   1007 	 */
   1008 	for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
   1009 		uhci_check_intr(sc, ii);
   1010 
   1011 	DPRINTFN(10, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
   1012 
   1013 	sc->sc_bus.intr_context--;
   1014 
   1015 	return (1);
   1016 }
   1017 
   1018 /* Check for an interrupt. */
   1019 void
   1020 uhci_check_intr(sc, ii)
   1021 	uhci_softc_t *sc;
   1022 	uhci_intr_info_t *ii;
   1023 {
   1024 	uhci_soft_td_t *std, *lstd;
   1025 	u_int32_t status;
   1026 
   1027 	DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
   1028 #ifdef DIAGNOSTIC
   1029 	if (ii == NULL) {
   1030 		printf("uhci_check_intr: no ii? %p\n", ii);
   1031 		return;
   1032 	}
   1033 #endif
   1034 	if (ii->stdstart == NULL)
   1035 		return;
   1036 	lstd = ii->stdend;
   1037 #ifdef DIAGNOSTIC
   1038 	if (lstd == NULL) {
   1039 		printf("uhci_check_intr: std==0\n");
   1040 		return;
   1041 	}
   1042 #endif
   1043 	/*
   1044 	 * If the last TD is still active we need to check whether there
   1045 	 * is a an error somewhere in the middle, or whether there was a
   1046 	 * short packet (SPD and not ACTIVE).
   1047 	 */
   1048 	if (LE(lstd->td.td_status) & UHCI_TD_ACTIVE) {
   1049 		DPRINTFN(15, ("uhci_check_intr: active ii=%p\n", ii));
   1050 		for (std = ii->stdstart; std != lstd; std = std->link.std) {
   1051 			status = LE(std->td.td_status);
   1052 			if ((status & UHCI_TD_STALLED) ||
   1053 			     (status & (UHCI_TD_SPD | UHCI_TD_ACTIVE)) ==
   1054 			     UHCI_TD_SPD)
   1055 				goto done;
   1056 		}
   1057 		DPRINTFN(15, ("uhci_check_intr: ii=%p std=%p still active\n",
   1058 			      ii, ii->stdstart));
   1059 		return;
   1060 	}
   1061  done:
   1062 	usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
   1063 	uhci_idone(ii);
   1064 }
   1065 
   1066 /* Called at splusb() */
   1067 void
   1068 uhci_idone(ii)
   1069 	uhci_intr_info_t *ii;
   1070 {
   1071 	usbd_xfer_handle xfer = ii->xfer;
   1072 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1073 	uhci_soft_td_t *std;
   1074 	u_int32_t status = 0, nstatus;
   1075 	int actlen;
   1076 
   1077 #ifdef DIAGNOSTIC
   1078 	{
   1079 		int s = splhigh();
   1080 		if (ii->isdone) {
   1081 			splx(s);
   1082 			printf("uhci_idone: ii=%p is done!\n", ii);
   1083 			return;
   1084 		}
   1085 		ii->isdone = 1;
   1086 		splx(s);
   1087 	}
   1088 #endif
   1089 
   1090 	if (xfer->status == USBD_CANCELLED ||
   1091 	    xfer->status == USBD_TIMEOUT) {
   1092 		DPRINTF(("uhci_idone: aborted xfer=%p\n", xfer));
   1093 		return;
   1094 	}
   1095 
   1096 	if (xfer->nframes != 0) {
   1097 		/* Isoc transfer, do things differently. */
   1098 		uhci_soft_td_t **stds = upipe->u.iso.stds;
   1099 		int i, n, nframes;
   1100 
   1101 		DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
   1102 
   1103 		nframes = xfer->nframes;
   1104 		actlen = 0;
   1105 		n = xfer->hcprivint;
   1106 		for (i = 0; i < nframes; i++) {
   1107 			std = stds[n];
   1108 #ifdef UHCI_DEBUG
   1109 			if (uhcidebug > 5) {
   1110 				DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
   1111 				uhci_dump_td(std);
   1112 			}
   1113 #endif
   1114 			if (++n >= UHCI_VFRAMELIST_COUNT)
   1115 				n = 0;
   1116 			status = LE(std->td.td_status);
   1117 			actlen += UHCI_TD_GET_ACTLEN(status);
   1118 		}
   1119 		upipe->u.iso.inuse -= nframes;
   1120 		xfer->actlen = actlen;
   1121 		xfer->status = USBD_NORMAL_COMPLETION;
   1122 		xfer->hcpriv = ii;
   1123 		usb_transfer_complete(xfer);
   1124 		return;
   1125 	}
   1126 
   1127 #ifdef UHCI_DEBUG
   1128 	DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
   1129 		      ii, xfer, upipe));
   1130 	if (uhcidebug > 10)
   1131 		uhci_dump_tds(ii->stdstart);
   1132 #endif
   1133 
   1134 	/* The transfer is done, compute actual length and status. */
   1135 	actlen = 0;
   1136 	for (std = ii->stdstart; std != NULL; std = std->link.std) {
   1137 		nstatus = LE(std->td.td_status);
   1138 		if (nstatus & UHCI_TD_ACTIVE)
   1139 			break;
   1140 
   1141 		status = nstatus;
   1142 		if (UHCI_TD_GET_PID(LE(std->td.td_token)) != UHCI_TD_PID_SETUP)
   1143 			actlen += UHCI_TD_GET_ACTLEN(status);
   1144 	}
   1145 	/* If there are left over TDs we need to update the toggle. */
   1146 	if (std != NULL)
   1147 		upipe->nexttoggle = UHCI_TD_GET_DT(LE(std->td.td_token));
   1148 
   1149 	status &= UHCI_TD_ERROR;
   1150 	DPRINTFN(10, ("uhci_check_intr: actlen=%d, status=0x%x\n",
   1151 		      actlen, status));
   1152 	xfer->actlen = actlen;
   1153 	if (status != 0) {
   1154 		DPRINTFN((status == UHCI_TD_STALLED)*10,
   1155 			 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
   1156 			  "status 0x%b\n",
   1157 			  xfer->pipe->device->address,
   1158 			  xfer->pipe->endpoint->edesc->bEndpointAddress,
   1159 			  (int)status,
   1160 			  "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
   1161 			  "STALLED\30ACTIVE"));
   1162 		if (status == UHCI_TD_STALLED)
   1163 			xfer->status = USBD_STALLED;
   1164 		else
   1165 			xfer->status = USBD_IOERROR; /* more info XXX */
   1166 	} else {
   1167 		xfer->status = USBD_NORMAL_COMPLETION;
   1168 	}
   1169 	xfer->hcpriv = ii;
   1170 	usb_transfer_complete(xfer);
   1171 }
   1172 
   1173 /*
   1174  * Called when a request does not complete.
   1175  */
   1176 void
   1177 uhci_timeout(addr)
   1178 	void *addr;
   1179 {
   1180 	uhci_intr_info_t *ii = addr;
   1181 
   1182 	DPRINTF(("uhci_timeout: ii=%p\n", ii));
   1183 
   1184 #ifdef UHCI_DEBUG
   1185 	if (uhcidebug > 10)
   1186 		uhci_dump_tds(ii->stdstart);
   1187 #endif
   1188 
   1189 	ii->xfer->device->bus->intr_context++;
   1190 	uhci_abort_xfer(ii->xfer, USBD_TIMEOUT);
   1191 	ii->xfer->device->bus->intr_context--;
   1192 }
   1193 
   1194 /*
   1195  * Wait here until controller claims to have an interrupt.
   1196  * Then call uhci_intr and return.  Use timeout to avoid waiting
   1197  * too long.
   1198  * Only used during boot when interrupts are not enabled yet.
   1199  */
   1200 void
   1201 uhci_waitintr(sc, xfer)
   1202 	uhci_softc_t *sc;
   1203 	usbd_xfer_handle xfer;
   1204 {
   1205 	int timo = xfer->timeout;
   1206 	uhci_intr_info_t *ii;
   1207 
   1208 	DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
   1209 
   1210 	xfer->status = USBD_IN_PROGRESS;
   1211 	for (; timo >= 0; timo--) {
   1212 		usb_delay_ms(&sc->sc_bus, 1);
   1213 		DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
   1214 		if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
   1215 			uhci_intr(sc);
   1216 			if (xfer->status != USBD_IN_PROGRESS)
   1217 				return;
   1218 		}
   1219 	}
   1220 
   1221 	/* Timeout */
   1222 	DPRINTF(("uhci_waitintr: timeout\n"));
   1223 	for (ii = LIST_FIRST(&sc->sc_intrhead);
   1224 	     ii != NULL && ii->xfer != xfer;
   1225 	     ii = LIST_NEXT(ii, list))
   1226 		;
   1227 #ifdef DIAGNOSTIC
   1228 	if (ii == NULL)
   1229 		panic("uhci_waitintr: lost intr_info\n");
   1230 #endif
   1231 	uhci_idone(ii);
   1232 }
   1233 
   1234 void
   1235 uhci_poll(bus)
   1236 	struct usbd_bus *bus;
   1237 {
   1238 	uhci_softc_t *sc = (uhci_softc_t *)bus;
   1239 
   1240 	if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
   1241 		uhci_intr(sc);
   1242 }
   1243 
   1244 #if 0
   1245 void
   1246 uhci_reset(p)
   1247 	void *p;
   1248 {
   1249 	uhci_softc_t *sc = p;
   1250 	int n;
   1251 
   1252 	UHCICMD(sc, UHCI_CMD_HCRESET);
   1253 	/* The reset bit goes low when the controller is done. */
   1254 	for (n = 0; n < UHCI_RESET_TIMEOUT &&
   1255 		    (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
   1256 		delay(100);
   1257 	if (n >= UHCI_RESET_TIMEOUT)
   1258 		printf("%s: controller did not reset\n",
   1259 		       USBDEVNAME(sc->sc_bus.bdev));
   1260 }
   1261 #endif
   1262 
   1263 usbd_status
   1264 uhci_run(sc, run)
   1265 	uhci_softc_t *sc;
   1266 	int run;
   1267 {
   1268 	int s, n, running;
   1269 	u_int16_t cmd;
   1270 
   1271 	run = run != 0;
   1272 	s = splusb();
   1273 	DPRINTF(("uhci_run: setting run=%d\n", run));
   1274 	cmd = UREAD2(sc, UHCI_CMD);
   1275 	if (run)
   1276 		cmd |= UHCI_CMD_RS;
   1277 	else
   1278 		cmd &= ~UHCI_CMD_RS;
   1279 	UHCICMD(sc, cmd);
   1280 	for(n = 0; n < 10; n++) {
   1281 		running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
   1282 		/* return when we've entered the state we want */
   1283 		if (run == running) {
   1284 			splx(s);
   1285 			DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
   1286 				 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
   1287 			return (USBD_NORMAL_COMPLETION);
   1288 		}
   1289 		usb_delay_ms(&sc->sc_bus, 1);
   1290 	}
   1291 	splx(s);
   1292 	printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
   1293 	       run ? "start" : "stop");
   1294 	return (USBD_IOERROR);
   1295 }
   1296 
   1297 /*
   1298  * Memory management routines.
   1299  *  uhci_alloc_std allocates TDs
   1300  *  uhci_alloc_sqh allocates QHs
   1301  * These two routines do their own free list management,
   1302  * partly for speed, partly because allocating DMAable memory
   1303  * has page size granularaity so much memory would be wasted if
   1304  * only one TD/QH (32 bytes) was placed in each allocated chunk.
   1305  */
   1306 
   1307 uhci_soft_td_t *
   1308 uhci_alloc_std(sc)
   1309 	uhci_softc_t *sc;
   1310 {
   1311 	uhci_soft_td_t *std;
   1312 	usbd_status err;
   1313 	int i, offs;
   1314 	usb_dma_t dma;
   1315 
   1316 	if (sc->sc_freetds == NULL) {
   1317 		DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
   1318 		err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
   1319 			  UHCI_TD_ALIGN, &dma);
   1320 		if (err)
   1321 			return (0);
   1322 		for(i = 0; i < UHCI_STD_CHUNK; i++) {
   1323 			offs = i * UHCI_STD_SIZE;
   1324 			std = (uhci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
   1325 			std->physaddr = DMAADDR(&dma) + offs;
   1326 			std->link.std = sc->sc_freetds;
   1327 			sc->sc_freetds = std;
   1328 		}
   1329 	}
   1330 	std = sc->sc_freetds;
   1331 	sc->sc_freetds = std->link.std;
   1332 	memset(&std->td, 0, sizeof(uhci_td_t));
   1333 	return std;
   1334 }
   1335 
   1336 void
   1337 uhci_free_std(sc, std)
   1338 	uhci_softc_t *sc;
   1339 	uhci_soft_td_t *std;
   1340 {
   1341 #ifdef DIAGNOSTIC
   1342 #define TD_IS_FREE 0x12345678
   1343 	if (std->td.td_token == LE(TD_IS_FREE)) {
   1344 		printf("uhci_free_std: freeing free TD %p\n", std);
   1345 		return;
   1346 	}
   1347 	std->td.td_token = LE(TD_IS_FREE);
   1348 #endif
   1349 	std->link.std = sc->sc_freetds;
   1350 	sc->sc_freetds = std;
   1351 }
   1352 
   1353 uhci_soft_qh_t *
   1354 uhci_alloc_sqh(sc)
   1355 	uhci_softc_t *sc;
   1356 {
   1357 	uhci_soft_qh_t *sqh;
   1358 	usbd_status err;
   1359 	int i, offs;
   1360 	usb_dma_t dma;
   1361 
   1362 	if (sc->sc_freeqhs == NULL) {
   1363 		DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
   1364 		err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
   1365 			  UHCI_QH_ALIGN, &dma);
   1366 		if (err)
   1367 			return (0);
   1368 		for(i = 0; i < UHCI_SQH_CHUNK; i++) {
   1369 			offs = i * UHCI_SQH_SIZE;
   1370 			sqh = (uhci_soft_qh_t *)((char *)KERNADDR(&dma) +offs);
   1371 			sqh->physaddr = DMAADDR(&dma) + offs;
   1372 			sqh->hlink = sc->sc_freeqhs;
   1373 			sc->sc_freeqhs = sqh;
   1374 		}
   1375 	}
   1376 	sqh = sc->sc_freeqhs;
   1377 	sc->sc_freeqhs = sqh->hlink;
   1378 	memset(&sqh->qh, 0, sizeof(uhci_qh_t));
   1379 	return (sqh);
   1380 }
   1381 
   1382 void
   1383 uhci_free_sqh(sc, sqh)
   1384 	uhci_softc_t *sc;
   1385 	uhci_soft_qh_t *sqh;
   1386 {
   1387 	sqh->hlink = sc->sc_freeqhs;
   1388 	sc->sc_freeqhs = sqh;
   1389 }
   1390 
   1391 #if 0
   1392 /*
   1393  * Enter a list of transfers onto a control queue.
   1394  * Called at splusb()
   1395  */
   1396 void
   1397 uhci_enter_ctl_q(sc, sqh, ii)
   1398 	uhci_softc_t *sc;
   1399 	uhci_soft_qh_t *sqh;
   1400 	uhci_intr_info_t *ii;
   1401 {
   1402 	DPRINTFN(5, ("uhci_enter_ctl_q: sqh=%p\n", sqh));
   1403 
   1404 }
   1405 #endif
   1406 
   1407 void
   1408 uhci_free_std_chain(sc, std, stdend)
   1409 	uhci_softc_t *sc;
   1410 	uhci_soft_td_t *std;
   1411 	uhci_soft_td_t *stdend;
   1412 {
   1413 	uhci_soft_td_t *p;
   1414 
   1415 	for (; std != stdend; std = p) {
   1416 		p = std->link.std;
   1417 		uhci_free_std(sc, std);
   1418 	}
   1419 }
   1420 
   1421 usbd_status
   1422 uhci_alloc_std_chain(upipe, sc, len, rd, flags, dma, sp, ep)
   1423 	struct uhci_pipe *upipe;
   1424 	uhci_softc_t *sc;
   1425 	int len, rd;
   1426 	u_int16_t flags;
   1427 	usb_dma_t *dma;
   1428 	uhci_soft_td_t **sp, **ep;
   1429 {
   1430 	uhci_soft_td_t *p, *lastp;
   1431 	uhci_physaddr_t lastlink;
   1432 	int i, ntd, l, tog, maxp;
   1433 	u_int32_t status;
   1434 	int addr = upipe->pipe.device->address;
   1435 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1436 
   1437 	DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d ls=%d "
   1438 		      "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
   1439 		      upipe->pipe.device->lowspeed, flags));
   1440 	maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
   1441 	if (maxp == 0) {
   1442 		printf("uhci_alloc_std_chain: maxp=0\n");
   1443 		return (USBD_INVAL);
   1444 	}
   1445 	ntd = (len + maxp - 1) / maxp;
   1446 	if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
   1447 		ntd++;
   1448 	DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
   1449 	if (ntd == 0) {
   1450 		*sp = *ep = 0;
   1451 		DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
   1452 		return (USBD_NORMAL_COMPLETION);
   1453 	}
   1454 	tog = upipe->nexttoggle;
   1455 	if (ntd % 2 == 0)
   1456 		tog ^= 1;
   1457 	upipe->nexttoggle = tog ^ 1;
   1458 	lastp = 0;
   1459 	lastlink = UHCI_PTR_T;
   1460 	ntd--;
   1461 	status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
   1462 	if (upipe->pipe.device->lowspeed)
   1463 		status |= UHCI_TD_LS;
   1464 	if (flags & USBD_SHORT_XFER_OK)
   1465 		status |= UHCI_TD_SPD;
   1466 	for (i = ntd; i >= 0; i--) {
   1467 		p = uhci_alloc_std(sc);
   1468 		if (p == NULL) {
   1469 			uhci_free_std_chain(sc, lastp, 0);
   1470 			return (USBD_NOMEM);
   1471 		}
   1472 		p->link.std = lastp;
   1473 		if (lastlink == UHCI_PTR_T)
   1474 			p->td.td_link = LE(lastlink);
   1475 		else
   1476 			p->td.td_link = LE(lastlink|UHCI_PTR_VF);
   1477 		lastp = p;
   1478 		lastlink = p->physaddr;
   1479 		p->td.td_status = LE(status);
   1480 		if (i == ntd) {
   1481 			/* last TD */
   1482 			l = len % maxp;
   1483 			if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
   1484 				l = maxp;
   1485 			*ep = p;
   1486 		} else
   1487 			l = maxp;
   1488 		p->td.td_token =
   1489 		    LE(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
   1490 			    UHCI_TD_OUT(l, endpt, addr, tog));
   1491 		p->td.td_buffer = LE(DMAADDR(dma) + i * maxp);
   1492 		tog ^= 1;
   1493 	}
   1494 	*sp = lastp;
   1495 	DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
   1496 		      upipe->nexttoggle));
   1497 	return (USBD_NORMAL_COMPLETION);
   1498 }
   1499 
   1500 void
   1501 uhci_device_clear_toggle(pipe)
   1502 	usbd_pipe_handle pipe;
   1503 {
   1504 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1505 	upipe->nexttoggle = 0;
   1506 }
   1507 
   1508 void
   1509 uhci_noop(pipe)
   1510 	usbd_pipe_handle pipe;
   1511 {
   1512 }
   1513 
   1514 usbd_status
   1515 uhci_device_bulk_transfer(xfer)
   1516 	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 (otherwise err would be USBD_INPROG),
   1526 	 * start first
   1527 	 */
   1528 	return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1529 }
   1530 
   1531 usbd_status
   1532 uhci_device_bulk_start(xfer)
   1533 	usbd_xfer_handle xfer;
   1534 {
   1535 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1536 	usbd_device_handle dev = upipe->pipe.device;
   1537 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1538 	uhci_intr_info_t *ii = upipe->iinfo;
   1539 	uhci_soft_td_t *data, *dataend;
   1540 	uhci_soft_qh_t *sqh;
   1541 	usbd_status err;
   1542 	int len, isread, endpt;
   1543 	int s;
   1544 
   1545 	DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
   1546 		     xfer, xfer->length, xfer->flags));
   1547 
   1548 	if (sc->sc_dying)
   1549 		return (USBD_IOERROR);
   1550 
   1551 #ifdef DIAGNOSTIC
   1552 	if (xfer->rqflags & URQ_REQUEST)
   1553 		panic("uhci_device_bulk_transfer: a request\n");
   1554 #endif
   1555 
   1556 	len = xfer->length;
   1557 	endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
   1558 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   1559 	sqh = upipe->u.bulk.sqh;
   1560 
   1561 	upipe->u.bulk.isread = isread;
   1562 	upipe->u.bulk.length = len;
   1563 
   1564 	err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
   1565 				   &xfer->dmabuf, &data, &dataend);
   1566 	if (err)
   1567 		return (err);
   1568 	dataend->td.td_status |= LE(UHCI_TD_IOC);
   1569 
   1570 #ifdef UHCI_DEBUG
   1571 	if (uhcidebug > 8) {
   1572 		DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
   1573 		uhci_dump_tds(data);
   1574 	}
   1575 #endif
   1576 
   1577 	/* Set up interrupt info. */
   1578 	ii->xfer = xfer;
   1579 	ii->stdstart = data;
   1580 	ii->stdend = dataend;
   1581 #if defined(__FreeBSD__)
   1582 	callout_handle_init(&ii->timeout_handle);
   1583 #endif
   1584 #ifdef DIAGNOSTIC
   1585 	if (!ii->isdone) {
   1586 		printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
   1587 	}
   1588 	ii->isdone = 0;
   1589 #endif
   1590 
   1591 	sqh->elink = data;
   1592 	sqh->qh.qh_elink = LE(data->physaddr);
   1593 	sqh->intr_info = ii;
   1594 
   1595 	s = splusb();
   1596 	uhci_add_bulk(sc, sqh);
   1597 	LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
   1598 
   1599 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   1600 		usb_timeout(uhci_timeout, ii, MS_TO_TICKS(xfer->timeout),
   1601 			    ii->timeout_handle);
   1602 	}
   1603 	splx(s);
   1604 
   1605 #ifdef UHCI_DEBUG
   1606 	if (uhcidebug > 10) {
   1607 		DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
   1608 		uhci_dump_tds(data);
   1609 	}
   1610 #endif
   1611 
   1612 	if (sc->sc_bus.use_polling)
   1613 		uhci_waitintr(sc, xfer);
   1614 
   1615 	return (USBD_IN_PROGRESS);
   1616 }
   1617 
   1618 /* Abort a device bulk request. */
   1619 void
   1620 uhci_device_bulk_abort(xfer)
   1621 	usbd_xfer_handle xfer;
   1622 {
   1623 	DPRINTF(("uhci_device_bulk_abort:\n"));
   1624 	uhci_abort_xfer(xfer, USBD_CANCELLED);
   1625 }
   1626 
   1627 void
   1628 uhci_abort_xfer(xfer, status)
   1629 	usbd_xfer_handle xfer;
   1630 	usbd_status status;
   1631 {
   1632 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1633 	uhci_intr_info_t *ii = upipe->iinfo;
   1634 	uhci_soft_td_t *std;
   1635 
   1636 	DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
   1637 
   1638 	/* Make interrupt routine ignore it, */
   1639 	xfer->status = status;
   1640 
   1641 	/* don't timeout, */
   1642 	usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
   1643 
   1644 	/* make hardware ignore it, */
   1645 	for (std = ii->stdstart; std != 0; std = std->link.std)
   1646 		std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
   1647 
   1648 	xfer->hcpriv = ii;
   1649 
   1650 #if 1
   1651 	/* Make sure hardware has completed. */
   1652 	if (xfer->device->bus->intr_context) {
   1653 		/* We have no process context, so we can't use tsleep(). */
   1654 		timeout(uhci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
   1655 	} else {
   1656 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
   1657 		KASSERT(intr_nesting_level == 0,
   1658 	        	("ohci_abort_req in interrupt context"));
   1659 #endif
   1660 		usb_delay_ms(xfer->pipe->device->bus, 1);
   1661 		/* and call final part of interrupt handler. */
   1662 		uhci_abort_xfer_end(xfer);
   1663 	}
   1664 #else
   1665 	delay(1000);
   1666 	uhci_abort_xfer_end(xfer);
   1667 #endif
   1668 }
   1669 
   1670 void
   1671 uhci_abort_xfer_end(v)
   1672 	void *v;
   1673 {
   1674 	usbd_xfer_handle xfer = v;
   1675 	int s;
   1676 
   1677 	s = splusb();
   1678 	usb_transfer_complete(xfer);
   1679 	splx(s);
   1680 }
   1681 
   1682 /* Close a device bulk pipe. */
   1683 void
   1684 uhci_device_bulk_close(pipe)
   1685 	usbd_pipe_handle pipe;
   1686 {
   1687 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1688 	usbd_device_handle dev = upipe->pipe.device;
   1689 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1690 
   1691 	uhci_free_sqh(sc, upipe->u.bulk.sqh);
   1692 	uhci_free_intr_info(upipe->iinfo);
   1693 	/* XXX free other resources */
   1694 }
   1695 
   1696 usbd_status
   1697 uhci_device_ctrl_transfer(xfer)
   1698 	usbd_xfer_handle xfer;
   1699 {
   1700 	usbd_status err;
   1701 
   1702 	/* Insert last in queue. */
   1703 	err = usb_insert_transfer(xfer);
   1704 	if (err)
   1705 		return (err);
   1706 
   1707 	/* Pipe isn't running (otherwise err would be USBD_INPROG),
   1708 	 * start first
   1709 	 */
   1710 	return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1711 }
   1712 
   1713 usbd_status
   1714 uhci_device_ctrl_start(xfer)
   1715 	usbd_xfer_handle xfer;
   1716 {
   1717 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   1718 	usbd_status err;
   1719 
   1720 	if (sc->sc_dying)
   1721 		return (USBD_IOERROR);
   1722 
   1723 #ifdef DIAGNOSTIC
   1724 	if (!(xfer->rqflags & URQ_REQUEST))
   1725 		panic("uhci_device_ctrl_transfer: not a request\n");
   1726 #endif
   1727 
   1728 	err = uhci_device_request(xfer);
   1729 	if (err)
   1730 		return (err);
   1731 
   1732 	if (sc->sc_bus.use_polling)
   1733 		uhci_waitintr(sc, xfer);
   1734 	return (USBD_IN_PROGRESS);
   1735 }
   1736 
   1737 usbd_status
   1738 uhci_device_intr_transfer(xfer)
   1739 	usbd_xfer_handle xfer;
   1740 {
   1741 	usbd_status err;
   1742 
   1743 	/* Insert last in queue. */
   1744 	err = usb_insert_transfer(xfer);
   1745 	if (err)
   1746 		return (err);
   1747 
   1748 	/* Pipe isn't running (otherwise err would be USBD_INPROG),
   1749 	 * start first
   1750 	 */
   1751 	return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1752 }
   1753 
   1754 usbd_status
   1755 uhci_device_intr_start(xfer)
   1756 	usbd_xfer_handle xfer;
   1757 {
   1758 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1759 	usbd_device_handle dev = upipe->pipe.device;
   1760 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1761 	uhci_intr_info_t *ii = upipe->iinfo;
   1762 	uhci_soft_td_t *data, *dataend;
   1763 	uhci_soft_qh_t *sqh;
   1764 	usbd_status err;
   1765 	int i, s;
   1766 
   1767 	if (sc->sc_dying)
   1768 		return (USBD_IOERROR);
   1769 
   1770 	DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
   1771 		    xfer, xfer->length, xfer->flags));
   1772 
   1773 #ifdef DIAGNOSTIC
   1774 	if (xfer->rqflags & URQ_REQUEST)
   1775 		panic("uhci_device_intr_transfer: a request\n");
   1776 #endif
   1777 
   1778 	err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
   1779 				   &xfer->dmabuf, &data, &dataend);
   1780 	if (err)
   1781 		return (err);
   1782 	dataend->td.td_status |= LE(UHCI_TD_IOC);
   1783 
   1784 #ifdef UHCI_DEBUG
   1785 	if (uhcidebug > 10) {
   1786 		DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
   1787 		uhci_dump_tds(data);
   1788 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   1789 	}
   1790 #endif
   1791 
   1792 	s = splusb();
   1793 	/* Set up interrupt info. */
   1794 	ii->xfer = xfer;
   1795 	ii->stdstart = data;
   1796 	ii->stdend = dataend;
   1797 #if defined(__FreeBSD__)
   1798 	callout_handle_init(&ii->timeout_handle);
   1799 #endif
   1800 #ifdef DIAGNOSTIC
   1801 	if (!ii->isdone) {
   1802 		printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
   1803 	}
   1804 	ii->isdone = 0;
   1805 #endif
   1806 
   1807 	DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
   1808 		     upipe->u.intr.qhs[0]));
   1809 	for (i = 0; i < upipe->u.intr.npoll; i++) {
   1810 		sqh = upipe->u.intr.qhs[i];
   1811 		sqh->elink = data;
   1812 		sqh->qh.qh_elink = LE(data->physaddr);
   1813 	}
   1814 	splx(s);
   1815 
   1816 #ifdef UHCI_DEBUG
   1817 	if (uhcidebug > 10) {
   1818 		DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
   1819 		uhci_dump_tds(data);
   1820 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   1821 	}
   1822 #endif
   1823 
   1824 	return (USBD_IN_PROGRESS);
   1825 }
   1826 
   1827 /* Abort a device control request. */
   1828 void
   1829 uhci_device_ctrl_abort(xfer)
   1830 	usbd_xfer_handle xfer;
   1831 {
   1832 	DPRINTF(("uhci_device_ctrl_abort:\n"));
   1833 	uhci_abort_xfer(xfer, USBD_CANCELLED);
   1834 }
   1835 
   1836 /* Close a device control pipe. */
   1837 void
   1838 uhci_device_ctrl_close(pipe)
   1839 	usbd_pipe_handle pipe;
   1840 {
   1841 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1842 
   1843 	uhci_free_intr_info(upipe->iinfo);
   1844 	/* XXX free other resources? */
   1845 }
   1846 
   1847 /* Abort a device interrupt request. */
   1848 void
   1849 uhci_device_intr_abort(xfer)
   1850 	usbd_xfer_handle xfer;
   1851 {
   1852 	DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
   1853 	if (xfer->pipe->intrxfer == xfer) {
   1854 		DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
   1855 		xfer->pipe->intrxfer = 0;
   1856 	}
   1857 	uhci_abort_xfer(xfer, USBD_CANCELLED);
   1858 }
   1859 
   1860 /* Close a device interrupt pipe. */
   1861 void
   1862 uhci_device_intr_close(pipe)
   1863 	usbd_pipe_handle pipe;
   1864 {
   1865 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1866 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   1867 	int i, s, npoll;
   1868 
   1869 	upipe->iinfo->stdstart = 0;		/* inactive */
   1870 
   1871 	/* Unlink descriptors from controller data structures. */
   1872 	npoll = upipe->u.intr.npoll;
   1873 	uhci_lock_frames(sc);
   1874 	for (i = 0; i < npoll; i++)
   1875 		uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos,
   1876 				 upipe->u.intr.qhs[i]);
   1877 	uhci_unlock_frames(sc);
   1878 
   1879 	/*
   1880 	 * We now have to wait for any activity on the physical
   1881 	 * descriptors to stop.
   1882 	 */
   1883 	usb_delay_ms(&sc->sc_bus, 2);
   1884 
   1885 	for(i = 0; i < npoll; i++)
   1886 		uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
   1887 	free(upipe->u.intr.qhs, M_USBHC);
   1888 
   1889 	s = splusb();
   1890 	LIST_REMOVE(upipe->iinfo, list);	/* remove from active list */
   1891 	splx(s);
   1892 	uhci_free_intr_info(upipe->iinfo);
   1893 
   1894 	/* XXX free other resources */
   1895 }
   1896 
   1897 usbd_status
   1898 uhci_device_request(xfer)
   1899 	usbd_xfer_handle xfer;
   1900 {
   1901 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1902 	usb_device_request_t *req = &xfer->request;
   1903 	usbd_device_handle dev = upipe->pipe.device;
   1904 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1905 	int addr = dev->address;
   1906 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1907 	uhci_intr_info_t *ii = upipe->iinfo;
   1908 	uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
   1909 	uhci_soft_qh_t *sqh;
   1910 	int len;
   1911 	u_int32_t ls;
   1912 	usbd_status err;
   1913 	int isread;
   1914 	int s;
   1915 
   1916 	DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
   1917 		    "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
   1918 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
   1919 		    UGETW(req->wIndex), UGETW(req->wLength),
   1920 		    addr, endpt));
   1921 
   1922 	ls = dev->lowspeed ? UHCI_TD_LS : 0;
   1923 	isread = req->bmRequestType & UT_READ;
   1924 	len = UGETW(req->wLength);
   1925 
   1926 	setup = upipe->u.ctl.setup;
   1927 	stat = upipe->u.ctl.stat;
   1928 	sqh = upipe->u.ctl.sqh;
   1929 
   1930 	/* Set up data transaction */
   1931 	if (len != 0) {
   1932 		upipe->nexttoggle = 1;
   1933 		err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
   1934 					   &xfer->dmabuf, &data, &dataend);
   1935 		if (err)
   1936 			return (err);
   1937 		next = data;
   1938 		dataend->link.std = stat;
   1939 		dataend->td.td_link = LE(stat->physaddr | UHCI_PTR_VF);
   1940 	} else {
   1941 		next = stat;
   1942 	}
   1943 	upipe->u.ctl.length = len;
   1944 
   1945 	memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
   1946 
   1947 	setup->link.std = next;
   1948 	setup->td.td_link = LE(next->physaddr | UHCI_PTR_VF);
   1949 	setup->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls | UHCI_TD_ACTIVE);
   1950 	setup->td.td_token = LE(UHCI_TD_SETUP(sizeof *req, endpt, addr));
   1951 	setup->td.td_buffer = LE(DMAADDR(&upipe->u.ctl.reqdma));
   1952 
   1953 	stat->link.std = 0;
   1954 	stat->td.td_link = LE(UHCI_PTR_T);
   1955 	stat->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls |
   1956 		UHCI_TD_ACTIVE | UHCI_TD_IOC);
   1957 	stat->td.td_token =
   1958 		LE(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
   1959 		            UHCI_TD_IN (0, endpt, addr, 1));
   1960 	stat->td.td_buffer = LE(0);
   1961 
   1962 #ifdef UHCI_DEBUG
   1963 	if (uhcidebug > 10) {
   1964 		DPRINTF(("uhci_device_request: before transfer\n"));
   1965 		uhci_dump_tds(setup);
   1966 	}
   1967 #endif
   1968 
   1969 	/* Set up interrupt info. */
   1970 	ii->xfer = xfer;
   1971 	ii->stdstart = setup;
   1972 	ii->stdend = stat;
   1973 #if defined(__FreeBSD__)
   1974 	callout_handle_init(&ii->timeout_handle);
   1975 #endif
   1976 #ifdef DIAGNOSTIC
   1977 	if (!ii->isdone) {
   1978 		printf("uhci_device_request: not done, ii=%p\n", ii);
   1979 	}
   1980 	ii->isdone = 0;
   1981 #endif
   1982 
   1983 	sqh->elink = setup;
   1984 	sqh->qh.qh_elink = LE(setup->physaddr);
   1985 	sqh->intr_info = ii;
   1986 
   1987 	s = splusb();
   1988 	uhci_add_ctrl(sc, sqh);
   1989 	LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
   1990 #ifdef UHCI_DEBUG
   1991 	if (uhcidebug > 12) {
   1992 		uhci_soft_td_t *std;
   1993 		uhci_soft_qh_t *xqh;
   1994 		uhci_soft_qh_t *sxqh;
   1995 		int maxqh = 0;
   1996 		uhci_physaddr_t link;
   1997 		DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
   1998 		for (std = sc->sc_vframes[0].htd, link = 0;
   1999 		     (link & UHCI_PTR_Q) == 0;
   2000 		     std = std->link.std) {
   2001 			link = LE(std->td.td_link);
   2002 			uhci_dump_td(std);
   2003 		}
   2004 		sxqh = (uhci_soft_qh_t *)std;
   2005 		uhci_dump_qh(sxqh);
   2006 		for (xqh = sxqh;
   2007 		     xqh != NULL;
   2008 		     xqh = (maxqh++ == 5 || xqh->hlink==sxqh ||
   2009                             xqh->hlink==xqh ? NULL : xqh->hlink)) {
   2010 			uhci_dump_qh(xqh);
   2011 		}
   2012 		DPRINTF(("Enqueued QH:\n"));
   2013 		uhci_dump_qh(sqh);
   2014 		uhci_dump_tds(sqh->elink);
   2015 	}
   2016 #endif
   2017 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   2018 		usb_timeout(uhci_timeout, ii,
   2019                             MS_TO_TICKS(xfer->timeout), ii->timeout_handle);
   2020 	}
   2021 	splx(s);
   2022 
   2023 	return (USBD_NORMAL_COMPLETION);
   2024 }
   2025 
   2026 usbd_status
   2027 uhci_device_isoc_transfer(xfer)
   2028 	usbd_xfer_handle xfer;
   2029 {
   2030 	usbd_status err;
   2031 
   2032 	DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
   2033 
   2034 	/* Put it on our queue, */
   2035 	err = usb_insert_transfer(xfer);
   2036 
   2037 	/* bail out on error, */
   2038 	if (err && err != USBD_IN_PROGRESS)
   2039 		return (err);
   2040 
   2041 	/* XXX should check inuse here */
   2042 
   2043 	/* insert into schedule, */
   2044 	uhci_device_isoc_enter(xfer);
   2045 
   2046 	/* and put on interrupt list if the pipe wasn't running */
   2047 	if (!err)
   2048 		uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
   2049 
   2050 	return (err);
   2051 }
   2052 
   2053 void
   2054 uhci_device_isoc_enter(xfer)
   2055 	usbd_xfer_handle xfer;
   2056 {
   2057 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2058 	usbd_device_handle dev = upipe->pipe.device;
   2059 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2060 	struct iso *iso = &upipe->u.iso;
   2061 	uhci_soft_td_t *std;
   2062 	u_int32_t buf, len, status;
   2063 	int s, i, next, nframes;
   2064 
   2065 	DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
   2066 		    "nframes=%d\n",
   2067 		    iso->inuse, iso->next, xfer, xfer->nframes));
   2068 
   2069 	if (sc->sc_dying)
   2070 		return;
   2071 
   2072 	if (xfer->status == USBD_IN_PROGRESS) {
   2073 		/* This request has already been entered into the frame list */
   2074 		/* XXX */
   2075 	}
   2076 
   2077 #ifdef DIAGNOSTIC
   2078 	if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
   2079 		printf("uhci_device_isoc_enter: overflow!\n");
   2080 #endif
   2081 
   2082 	next = iso->next;
   2083 	if (next == -1) {
   2084 		/* Not in use yet, schedule it a few frames ahead. */
   2085 		next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
   2086 		DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
   2087 	}
   2088 
   2089 	xfer->status = USBD_IN_PROGRESS;
   2090 	xfer->hcprivint = next;
   2091 
   2092 	buf = DMAADDR(&xfer->dmabuf);
   2093 	status = LE(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
   2094 					UHCI_TD_ACTIVE |
   2095 					UHCI_TD_IOS));
   2096 	nframes = xfer->nframes;
   2097 	s = splusb();
   2098 	for (i = 0; i < nframes; i++) {
   2099 		std = iso->stds[next];
   2100 		if (++next >= UHCI_VFRAMELIST_COUNT)
   2101 			next = 0;
   2102 		len = xfer->frlengths[i];
   2103 		std->td.td_buffer = LE(buf);
   2104 		if (i == nframes - 1)
   2105 			status |= LE(UHCI_TD_IOC);
   2106 		std->td.td_status = status;
   2107 		std->td.td_token &= LE(~UHCI_TD_MAXLEN_MASK);
   2108 		std->td.td_token |= LE(UHCI_TD_SET_MAXLEN(len));
   2109 #ifdef UHCI_DEBUG
   2110 		if (uhcidebug > 5) {
   2111 			DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
   2112 			uhci_dump_td(std);
   2113 		}
   2114 #endif
   2115 		buf += len;
   2116 	}
   2117 	iso->next = next;
   2118 	iso->inuse += xfer->nframes;
   2119 
   2120 	splx(s);
   2121 }
   2122 
   2123 usbd_status
   2124 uhci_device_isoc_start(xfer)
   2125 	usbd_xfer_handle xfer;
   2126 {
   2127 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2128 	uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
   2129 	uhci_intr_info_t *ii = upipe->iinfo;
   2130 	uhci_soft_td_t *end;
   2131 	int s, i;
   2132 
   2133 	if (sc->sc_dying)
   2134 		return (USBD_IOERROR);
   2135 
   2136 #ifdef DIAGNOSTIC
   2137 	if (xfer->status != USBD_IN_PROGRESS)
   2138 		printf("uhci_device_isoc_start: not in progress %p\n", xfer);
   2139 #endif
   2140 
   2141 	/* Find the last TD */
   2142 	i = xfer->hcprivint + xfer->nframes;
   2143 	if (i >= UHCI_VFRAMELIST_COUNT)
   2144 		i -= UHCI_VFRAMELIST_COUNT;
   2145 	end = upipe->u.iso.stds[i];
   2146 
   2147 	s = splusb();
   2148 
   2149 	/* Set up interrupt info. */
   2150 	ii->xfer = xfer;
   2151 	ii->stdstart = end;
   2152 	ii->stdend = end;
   2153 #if defined(__FreeBSD__)
   2154 	callout_handle_init(&ii->timeout_handle);
   2155 #endif
   2156 #ifdef DIAGNOSTIC
   2157 	if (!ii->isdone) {
   2158 		printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
   2159 	}
   2160 	ii->isdone = 0;
   2161 #endif
   2162 	LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
   2163 
   2164 	splx(s);
   2165 
   2166 	return (USBD_IN_PROGRESS);
   2167 }
   2168 
   2169 void
   2170 uhci_device_isoc_abort(xfer)
   2171 	usbd_xfer_handle xfer;
   2172 {
   2173 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2174 	uhci_intr_info_t *ii = upipe->iinfo;
   2175 	uhci_soft_td_t **stds = upipe->u.iso.stds;
   2176 	uhci_soft_td_t *std;
   2177 	int i, n, nframes;
   2178 
   2179 	/* Make interrupt routine ignore it, */
   2180 	xfer->status = USBD_CANCELLED;
   2181 
   2182 	/* make hardware ignore it, */
   2183 	nframes = xfer->nframes;
   2184 	n = xfer->hcprivint;
   2185 	for (i = 0; i < nframes; i++) {
   2186 		std = stds[n];
   2187 		std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
   2188 		if (++n >= UHCI_VFRAMELIST_COUNT)
   2189 			n = 0;
   2190 	}
   2191 
   2192 	xfer->hcpriv = ii;
   2193 
   2194 	/* make sure hardware has completed, */
   2195 	if (xfer->device->bus->intr_context) {
   2196 		/* We have no process context, so we can't use tsleep(). */
   2197 		timeout(uhci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
   2198 	} else {
   2199 		usb_delay_ms(xfer->pipe->device->bus, 1);
   2200 		/* and call final part of interrupt handler. */
   2201 		uhci_abort_xfer_end(xfer);
   2202 	}
   2203 }
   2204 
   2205 void
   2206 uhci_device_isoc_close(pipe)
   2207 	usbd_pipe_handle pipe;
   2208 {
   2209 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2210 	usbd_device_handle dev = upipe->pipe.device;
   2211 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2212 	uhci_soft_td_t *std, *vstd;
   2213 	struct iso *iso;
   2214 	int i;
   2215 
   2216 	/*
   2217 	 * Make sure all TDs are marked as inactive.
   2218 	 * Wait for completion.
   2219 	 * Unschedule.
   2220 	 * Deallocate.
   2221 	 */
   2222 	iso = &upipe->u.iso;
   2223 
   2224 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
   2225 		iso->stds[i]->td.td_status &= LE(~UHCI_TD_ACTIVE);
   2226 	usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
   2227 
   2228 	uhci_lock_frames(sc);
   2229 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2230 		std = iso->stds[i];
   2231 		for (vstd = sc->sc_vframes[i].htd;
   2232 		     vstd != NULL && vstd->link.std != std;
   2233 		     vstd = vstd->link.std)
   2234 			;
   2235 		if (vstd == NULL) {
   2236 			/*panic*/
   2237 			printf("uhci_device_isoc_close: %p not found\n", std);
   2238 			uhci_unlock_frames(sc);
   2239 			return;
   2240 		}
   2241 		vstd->link = std->link;
   2242 		vstd->td.td_link = std->td.td_link;
   2243 		uhci_free_std(sc, std);
   2244 	}
   2245 	uhci_unlock_frames(sc);
   2246 
   2247 	free(iso->stds, M_USBHC);
   2248 }
   2249 
   2250 usbd_status
   2251 uhci_setup_isoc(pipe)
   2252 	usbd_pipe_handle pipe;
   2253 {
   2254 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2255 	usbd_device_handle dev = upipe->pipe.device;
   2256 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2257 	int addr = upipe->pipe.device->address;
   2258 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   2259 	int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
   2260 	uhci_soft_td_t *std, *vstd;
   2261 	u_int32_t token;
   2262 	struct iso *iso;
   2263 	int i;
   2264 
   2265 	iso = &upipe->u.iso;
   2266 	iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
   2267 			   M_USBHC, M_WAITOK);
   2268 
   2269 	token = LE(rd ? UHCI_TD_IN (0, endpt, addr, 0) :
   2270 			UHCI_TD_OUT(0, endpt, addr, 0));
   2271 
   2272 	/* Allocate the TDs and mark as inactive; */
   2273 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2274 		std = uhci_alloc_std(sc);
   2275 		if (std == 0)
   2276 			goto bad;
   2277 		std->td.td_status = LE(UHCI_TD_IOS);	/* iso, inactive */
   2278 		std->td.td_token = token;
   2279 		iso->stds[i] = std;
   2280 	}
   2281 
   2282 	/* Insert TDs into schedule. */
   2283 	uhci_lock_frames(sc);
   2284 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2285 		std = iso->stds[i];
   2286 		vstd = sc->sc_vframes[i].htd;
   2287 		std->link = vstd->link;
   2288 		std->td.td_link = vstd->td.td_link;
   2289 		vstd->link.std = std;
   2290 		vstd->td.td_link = LE(std->physaddr);
   2291 	}
   2292 	uhci_unlock_frames(sc);
   2293 
   2294 	iso->next = -1;
   2295 	iso->inuse = 0;
   2296 
   2297 	return (USBD_NORMAL_COMPLETION);
   2298 
   2299  bad:
   2300 	while (--i >= 0)
   2301 		uhci_free_std(sc, iso->stds[i]);
   2302 	free(iso->stds, M_USBHC);
   2303 	return (USBD_NOMEM);
   2304 }
   2305 
   2306 void
   2307 uhci_device_isoc_done(xfer)
   2308 	usbd_xfer_handle xfer;
   2309 {
   2310 	uhci_intr_info_t *ii = xfer->hcpriv;
   2311 
   2312 	DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
   2313 
   2314 	/* Turn off the interrupt since it is active even if the TD is not. */
   2315 	ii->stdend->td.td_status &= LE(~UHCI_TD_IOC);
   2316 
   2317 	LIST_REMOVE(ii, list);	/* remove from active list */
   2318 }
   2319 
   2320 void
   2321 uhci_device_intr_done(xfer)
   2322 	usbd_xfer_handle xfer;
   2323 {
   2324 	uhci_intr_info_t *ii = xfer->hcpriv;
   2325 	uhci_softc_t *sc = ii->sc;
   2326 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2327 	uhci_soft_qh_t *sqh;
   2328 	int i, npoll;
   2329 
   2330 	DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
   2331 
   2332 	npoll = upipe->u.intr.npoll;
   2333 	for(i = 0; i < npoll; i++) {
   2334 		sqh = upipe->u.intr.qhs[i];
   2335 		sqh->elink = 0;
   2336 		sqh->qh.qh_elink = LE(UHCI_PTR_T);
   2337 	}
   2338 	uhci_free_std_chain(sc, ii->stdstart, 0);
   2339 
   2340 	/* XXX Wasteful. */
   2341 	if (xfer->pipe->repeat) {
   2342 		uhci_soft_td_t *data, *dataend;
   2343 
   2344 		/* This alloc cannot fail since we freed the chain above. */
   2345 		uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
   2346 				     &xfer->dmabuf, &data, &dataend);
   2347 		dataend->td.td_status |= LE(UHCI_TD_IOC);
   2348 
   2349 #ifdef UHCI_DEBUG
   2350 		if (uhcidebug > 10) {
   2351 			DPRINTF(("uhci_device_intr_done: data(1)\n"));
   2352 			uhci_dump_tds(data);
   2353 			uhci_dump_qh(upipe->u.intr.qhs[0]);
   2354 		}
   2355 #endif
   2356 
   2357 		ii->stdstart = data;
   2358 		ii->stdend = dataend;
   2359 #if defined(__FreeBSD__)
   2360 		callout_handle_init(&ii->timeout_handle);
   2361 #endif
   2362 #ifdef DIAGNOSTIC
   2363 		if (!ii->isdone) {
   2364 			printf("uhci_device_intr_done: not done, ii=%p\n", ii);
   2365 		}
   2366 		ii->isdone = 0;
   2367 #endif
   2368 		for (i = 0; i < npoll; i++) {
   2369 			sqh = upipe->u.intr.qhs[i];
   2370 			sqh->elink = data;
   2371 			sqh->qh.qh_elink = LE(data->physaddr);
   2372 		}
   2373 	} else {
   2374 		ii->stdstart = 0;	/* mark as inactive */
   2375 	}
   2376 }
   2377 
   2378 /* Deallocate request data structures */
   2379 void
   2380 uhci_device_ctrl_done(xfer)
   2381 	usbd_xfer_handle xfer;
   2382 {
   2383 	uhci_intr_info_t *ii = xfer->hcpriv;
   2384 	uhci_softc_t *sc = ii->sc;
   2385 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2386 
   2387 #ifdef DIAGNOSTIC
   2388 	if (!(xfer->rqflags & URQ_REQUEST))
   2389 		panic("uhci_ctrl_done: not a request\n");
   2390 #endif
   2391 
   2392 	LIST_REMOVE(ii, list);	/* remove from active list */
   2393 
   2394 	uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
   2395 
   2396 	if (upipe->u.ctl.length != 0)
   2397 		uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
   2398 
   2399 	DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
   2400 }
   2401 
   2402 /* Deallocate request data structures */
   2403 void
   2404 uhci_device_bulk_done(xfer)
   2405 	usbd_xfer_handle xfer;
   2406 {
   2407 	uhci_intr_info_t *ii = xfer->hcpriv;
   2408 	uhci_softc_t *sc = ii->sc;
   2409 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2410 
   2411 	LIST_REMOVE(ii, list);	/* remove from active list */
   2412 
   2413 	uhci_remove_bulk(sc, upipe->u.bulk.sqh);
   2414 
   2415 	uhci_free_std_chain(sc, ii->stdstart, 0);
   2416 
   2417 	DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
   2418 }
   2419 
   2420 /* Add interrupt QH, called with vflock. */
   2421 void
   2422 uhci_add_intr(sc, n, sqh)
   2423 	uhci_softc_t *sc;
   2424 	int n;
   2425 	uhci_soft_qh_t *sqh;
   2426 {
   2427 	struct uhci_vframe *vf = &sc->sc_vframes[n];
   2428 	uhci_soft_qh_t *eqh;
   2429 
   2430 	DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
   2431 	eqh = vf->eqh;
   2432 	sqh->hlink       = eqh->hlink;
   2433 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
   2434 	eqh->hlink       = sqh;
   2435 	eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
   2436 	vf->eqh = sqh;
   2437 	vf->bandwidth++;
   2438 }
   2439 
   2440 /* Remove interrupt QH, called with vflock. */
   2441 void
   2442 uhci_remove_intr(sc, n, sqh)
   2443 	uhci_softc_t *sc;
   2444 	int n;
   2445 	uhci_soft_qh_t *sqh;
   2446 {
   2447 	struct uhci_vframe *vf = &sc->sc_vframes[n];
   2448 	uhci_soft_qh_t *pqh;
   2449 
   2450 	DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
   2451 
   2452 	for (pqh = vf->hqh; pqh->hlink != sqh; pqh = pqh->hlink)
   2453 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
   2454 		if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
   2455 			DPRINTF(("uhci_remove_intr: QH not found\n"));
   2456 			return;
   2457 		}
   2458 #else
   2459 		;
   2460 #endif
   2461 	pqh->hlink       = sqh->hlink;
   2462 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
   2463 	if (vf->eqh == sqh)
   2464 		vf->eqh = pqh;
   2465 	vf->bandwidth--;
   2466 }
   2467 
   2468 usbd_status
   2469 uhci_device_setintr(sc, upipe, ival)
   2470 	uhci_softc_t *sc;
   2471 	struct uhci_pipe *upipe;
   2472 	int ival;
   2473 {
   2474 	uhci_soft_qh_t *sqh;
   2475 	int i, npoll, s;
   2476 	u_int bestbw, bw, bestoffs, offs;
   2477 
   2478 	DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
   2479 	if (ival == 0) {
   2480 		printf("uhci_setintr: 0 interval\n");
   2481 		return (USBD_INVAL);
   2482 	}
   2483 
   2484 	if (ival > UHCI_VFRAMELIST_COUNT)
   2485 		ival = UHCI_VFRAMELIST_COUNT;
   2486 	npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
   2487 	DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
   2488 
   2489 	upipe->u.intr.npoll = npoll;
   2490 	upipe->u.intr.qhs =
   2491 		malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
   2492 
   2493 	/*
   2494 	 * Figure out which offset in the schedule that has most
   2495 	 * bandwidth left over.
   2496 	 */
   2497 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
   2498 	for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
   2499 		for (bw = i = 0; i < npoll; i++)
   2500 			bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
   2501 		if (bw < bestbw) {
   2502 			bestbw = bw;
   2503 			bestoffs = offs;
   2504 		}
   2505 	}
   2506 	DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
   2507 
   2508 	upipe->iinfo->stdstart = 0;
   2509 	for(i = 0; i < npoll; i++) {
   2510 		upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
   2511 		sqh->elink = 0;
   2512 		sqh->qh.qh_elink = LE(UHCI_PTR_T);
   2513 		sqh->pos = MOD(i * ival + bestoffs);
   2514 		sqh->intr_info = upipe->iinfo;
   2515 	}
   2516 #undef MOD
   2517 
   2518 	s = splusb();
   2519 	LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
   2520 	splx(s);
   2521 
   2522 	uhci_lock_frames(sc);
   2523 	/* Enter QHs into the controller data structures. */
   2524 	for(i = 0; i < npoll; i++)
   2525 		uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos,
   2526 			      upipe->u.intr.qhs[i]);
   2527 	uhci_unlock_frames(sc);
   2528 
   2529 	DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
   2530 	return (USBD_NORMAL_COMPLETION);
   2531 }
   2532 
   2533 /* Open a new pipe. */
   2534 usbd_status
   2535 uhci_open(pipe)
   2536 	usbd_pipe_handle pipe;
   2537 {
   2538 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2539 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2540 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
   2541 	usbd_status err;
   2542 	int ival;
   2543 
   2544 	DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
   2545 		     pipe, pipe->device->address,
   2546 		     ed->bEndpointAddress, sc->sc_addr));
   2547 	if (pipe->device->address == sc->sc_addr) {
   2548 		switch (ed->bEndpointAddress) {
   2549 		case USB_CONTROL_ENDPOINT:
   2550 			pipe->methods = &uhci_root_ctrl_methods;
   2551 			break;
   2552 		case UE_DIR_IN | UHCI_INTR_ENDPT:
   2553 			pipe->methods = &uhci_root_intr_methods;
   2554 			break;
   2555 		default:
   2556 			return (USBD_INVAL);
   2557 		}
   2558 	} else {
   2559 		upipe->iinfo = uhci_alloc_intr_info(sc);
   2560 		if (upipe->iinfo == 0)
   2561 			return (USBD_NOMEM);
   2562 		switch (ed->bmAttributes & UE_XFERTYPE) {
   2563 		case UE_CONTROL:
   2564 			pipe->methods = &uhci_device_ctrl_methods;
   2565 			upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
   2566 			if (upipe->u.ctl.sqh == NULL)
   2567 				goto bad;
   2568 			upipe->u.ctl.setup = uhci_alloc_std(sc);
   2569 			if (upipe->u.ctl.setup == NULL) {
   2570 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2571 				goto bad;
   2572 			}
   2573 			upipe->u.ctl.stat = uhci_alloc_std(sc);
   2574 			if (upipe->u.ctl.stat == NULL) {
   2575 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2576 				uhci_free_std(sc, upipe->u.ctl.setup);
   2577 				goto bad;
   2578 			}
   2579 			err = usb_allocmem(&sc->sc_bus,
   2580 				  sizeof(usb_device_request_t),
   2581 				  0, &upipe->u.ctl.reqdma);
   2582 			if (err) {
   2583 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2584 				uhci_free_std(sc, upipe->u.ctl.setup);
   2585 				uhci_free_std(sc, upipe->u.ctl.stat);
   2586 				goto bad;
   2587 			}
   2588 			break;
   2589 		case UE_INTERRUPT:
   2590 			pipe->methods = &uhci_device_intr_methods;
   2591 			ival = pipe->interval;
   2592 			if (ival == USBD_DEFAULT_INTERVAL)
   2593 				ival = ed->bInterval;
   2594 			return (uhci_device_setintr(sc, upipe, ival));
   2595 		case UE_ISOCHRONOUS:
   2596 			pipe->methods = &uhci_device_isoc_methods;
   2597 			return (uhci_setup_isoc(pipe));
   2598 		case UE_BULK:
   2599 			pipe->methods = &uhci_device_bulk_methods;
   2600 			upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
   2601 			if (upipe->u.bulk.sqh == NULL)
   2602 				goto bad;
   2603 			break;
   2604 		}
   2605 	}
   2606 	return (USBD_NORMAL_COMPLETION);
   2607 
   2608  bad:
   2609 	uhci_free_intr_info(upipe->iinfo);
   2610 	return (USBD_NOMEM);
   2611 }
   2612 
   2613 /*
   2614  * Data structures and routines to emulate the root hub.
   2615  */
   2616 usb_device_descriptor_t uhci_devd = {
   2617 	USB_DEVICE_DESCRIPTOR_SIZE,
   2618 	UDESC_DEVICE,		/* type */
   2619 	{0x00, 0x01},		/* USB version */
   2620 	UCLASS_HUB,		/* class */
   2621 	USUBCLASS_HUB,		/* subclass */
   2622 	0,			/* protocol */
   2623 	64,			/* max packet */
   2624 	{0},{0},{0x00,0x01},	/* device id */
   2625 	1,2,0,			/* string indicies */
   2626 	1			/* # of configurations */
   2627 };
   2628 
   2629 usb_config_descriptor_t uhci_confd = {
   2630 	USB_CONFIG_DESCRIPTOR_SIZE,
   2631 	UDESC_CONFIG,
   2632 	{USB_CONFIG_DESCRIPTOR_SIZE +
   2633 	 USB_INTERFACE_DESCRIPTOR_SIZE +
   2634 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
   2635 	1,
   2636 	1,
   2637 	0,
   2638 	UC_SELF_POWERED,
   2639 	0			/* max power */
   2640 };
   2641 
   2642 usb_interface_descriptor_t uhci_ifcd = {
   2643 	USB_INTERFACE_DESCRIPTOR_SIZE,
   2644 	UDESC_INTERFACE,
   2645 	0,
   2646 	0,
   2647 	1,
   2648 	UCLASS_HUB,
   2649 	USUBCLASS_HUB,
   2650 	0,
   2651 	0
   2652 };
   2653 
   2654 usb_endpoint_descriptor_t uhci_endpd = {
   2655 	USB_ENDPOINT_DESCRIPTOR_SIZE,
   2656 	UDESC_ENDPOINT,
   2657 	UE_DIR_IN | UHCI_INTR_ENDPT,
   2658 	UE_INTERRUPT,
   2659 	{8},
   2660 	255
   2661 };
   2662 
   2663 usb_hub_descriptor_t uhci_hubd_piix = {
   2664 	USB_HUB_DESCRIPTOR_SIZE,
   2665 	UDESC_HUB,
   2666 	2,
   2667 	{ UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
   2668 	50,			/* power on to power good */
   2669 	0,
   2670 	{ 0x00 },		/* both ports are removable */
   2671 };
   2672 
   2673 int
   2674 uhci_str(p, l, s)
   2675 	usb_string_descriptor_t *p;
   2676 	int l;
   2677 	char *s;
   2678 {
   2679 	int i;
   2680 
   2681 	if (l == 0)
   2682 		return (0);
   2683 	p->bLength = 2 * strlen(s) + 2;
   2684 	if (l == 1)
   2685 		return (1);
   2686 	p->bDescriptorType = UDESC_STRING;
   2687 	l -= 2;
   2688 	for (i = 0; s[i] && l > 1; i++, l -= 2)
   2689 		USETW2(p->bString[i], 0, s[i]);
   2690 	return (2*i+2);
   2691 }
   2692 
   2693 /*
   2694  * Simulate a hardware hub by handling all the necessary requests.
   2695  */
   2696 usbd_status
   2697 uhci_root_ctrl_transfer(xfer)
   2698 	usbd_xfer_handle xfer;
   2699 {
   2700 	usbd_status err;
   2701 
   2702 	/* Insert last in queue. */
   2703 	err = usb_insert_transfer(xfer);
   2704 	if (err)
   2705 		return (err);
   2706 
   2707 	/* Pipe isn't running (otherwise err would be USBD_INPROG),
   2708 	 * start first
   2709 	 */
   2710 	return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2711 }
   2712 
   2713 usbd_status
   2714 uhci_root_ctrl_start(xfer)
   2715 	usbd_xfer_handle xfer;
   2716 {
   2717 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   2718 	usb_device_request_t *req;
   2719 	void *buf = NULL;
   2720 	int port, x;
   2721 	int s, len, value, index, status, change, l, totlen = 0;
   2722 	usb_port_status_t ps;
   2723 	usbd_status err;
   2724 
   2725 	if (sc->sc_dying)
   2726 		return (USBD_IOERROR);
   2727 
   2728 #ifdef DIAGNOSTIC
   2729 	if (!(xfer->rqflags & URQ_REQUEST))
   2730 		panic("uhci_root_ctrl_transfer: not a request\n");
   2731 #endif
   2732 	req = &xfer->request;
   2733 
   2734 	DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
   2735 		    req->bmRequestType, req->bRequest));
   2736 
   2737 	len = UGETW(req->wLength);
   2738 	value = UGETW(req->wValue);
   2739 	index = UGETW(req->wIndex);
   2740 
   2741 	if (len != 0)
   2742 		buf = KERNADDR(&xfer->dmabuf);
   2743 
   2744 #define C(x,y) ((x) | ((y) << 8))
   2745 	switch(C(req->bRequest, req->bmRequestType)) {
   2746 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
   2747 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
   2748 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
   2749 		/*
   2750 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
   2751 		 * for the integrated root hub.
   2752 		 */
   2753 		break;
   2754 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
   2755 		if (len > 0) {
   2756 			*(u_int8_t *)buf = sc->sc_conf;
   2757 			totlen = 1;
   2758 		}
   2759 		break;
   2760 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   2761 		DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
   2762 		switch(value >> 8) {
   2763 		case UDESC_DEVICE:
   2764 			if ((value & 0xff) != 0) {
   2765 				err = USBD_IOERROR;
   2766 				goto ret;
   2767 			}
   2768 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   2769 			USETW(uhci_devd.idVendor, sc->sc_id_vendor);
   2770 			memcpy(buf, &uhci_devd, l);
   2771 			break;
   2772 		case UDESC_CONFIG:
   2773 			if ((value & 0xff) != 0) {
   2774 				err = USBD_IOERROR;
   2775 				goto ret;
   2776 			}
   2777 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
   2778 			memcpy(buf, &uhci_confd, l);
   2779 			buf = (char *)buf + l;
   2780 			len -= l;
   2781 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
   2782 			totlen += l;
   2783 			memcpy(buf, &uhci_ifcd, l);
   2784 			buf = (char *)buf + l;
   2785 			len -= l;
   2786 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
   2787 			totlen += l;
   2788 			memcpy(buf, &uhci_endpd, l);
   2789 			break;
   2790 		case UDESC_STRING:
   2791 			if (len == 0)
   2792 				break;
   2793 			*(u_int8_t *)buf = 0;
   2794 			totlen = 1;
   2795 			switch (value & 0xff) {
   2796 			case 1: /* Vendor */
   2797 				totlen = uhci_str(buf, len, sc->sc_vendor);
   2798 				break;
   2799 			case 2: /* Product */
   2800 				totlen = uhci_str(buf, len, "UHCI root hub");
   2801 				break;
   2802 			}
   2803 			break;
   2804 		default:
   2805 			err = USBD_IOERROR;
   2806 			goto ret;
   2807 		}
   2808 		break;
   2809 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   2810 		if (len > 0) {
   2811 			*(u_int8_t *)buf = 0;
   2812 			totlen = 1;
   2813 		}
   2814 		break;
   2815 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   2816 		if (len > 1) {
   2817 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   2818 			totlen = 2;
   2819 		}
   2820 		break;
   2821 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   2822 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   2823 		if (len > 1) {
   2824 			USETW(((usb_status_t *)buf)->wStatus, 0);
   2825 			totlen = 2;
   2826 		}
   2827 		break;
   2828 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   2829 		if (value >= USB_MAX_DEVICES) {
   2830 			err = USBD_IOERROR;
   2831 			goto ret;
   2832 		}
   2833 		sc->sc_addr = value;
   2834 		break;
   2835 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   2836 		if (value != 0 && value != 1) {
   2837 			err = USBD_IOERROR;
   2838 			goto ret;
   2839 		}
   2840 		sc->sc_conf = value;
   2841 		break;
   2842 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   2843 		break;
   2844 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   2845 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   2846 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   2847 		err = USBD_IOERROR;
   2848 		goto ret;
   2849 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   2850 		break;
   2851 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   2852 		break;
   2853 	/* Hub requests */
   2854 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   2855 		break;
   2856 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   2857 		DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
   2858 			     "port=%d feature=%d\n",
   2859 			     index, value));
   2860 		if (index == 1)
   2861 			port = UHCI_PORTSC1;
   2862 		else if (index == 2)
   2863 			port = UHCI_PORTSC2;
   2864 		else {
   2865 			err = USBD_IOERROR;
   2866 			goto ret;
   2867 		}
   2868 		switch(value) {
   2869 		case UHF_PORT_ENABLE:
   2870 			x = UREAD2(sc, port);
   2871 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
   2872 			break;
   2873 		case UHF_PORT_SUSPEND:
   2874 			x = UREAD2(sc, port);
   2875 			UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
   2876 			break;
   2877 		case UHF_PORT_RESET:
   2878 			x = UREAD2(sc, port);
   2879 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   2880 			break;
   2881 		case UHF_C_PORT_CONNECTION:
   2882 			x = UREAD2(sc, port);
   2883 			UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
   2884 			break;
   2885 		case UHF_C_PORT_ENABLE:
   2886 			x = UREAD2(sc, port);
   2887 			UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
   2888 			break;
   2889 		case UHF_C_PORT_OVER_CURRENT:
   2890 			x = UREAD2(sc, port);
   2891 			UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
   2892 			break;
   2893 		case UHF_C_PORT_RESET:
   2894 			sc->sc_isreset = 0;
   2895 			err = USBD_NORMAL_COMPLETION;
   2896 			goto ret;
   2897 		case UHF_PORT_CONNECTION:
   2898 		case UHF_PORT_OVER_CURRENT:
   2899 		case UHF_PORT_POWER:
   2900 		case UHF_PORT_LOW_SPEED:
   2901 		case UHF_C_PORT_SUSPEND:
   2902 		default:
   2903 			err = USBD_IOERROR;
   2904 			goto ret;
   2905 		}
   2906 		break;
   2907 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
   2908 		if (index == 1)
   2909 			port = UHCI_PORTSC1;
   2910 		else if (index == 2)
   2911 			port = UHCI_PORTSC2;
   2912 		else {
   2913 			err = USBD_IOERROR;
   2914 			goto ret;
   2915 		}
   2916 		if (len > 0) {
   2917 			*(u_int8_t *)buf =
   2918 				(UREAD2(sc, port) & UHCI_PORTSC_LS) >>
   2919 				UHCI_PORTSC_LS_SHIFT;
   2920 			totlen = 1;
   2921 		}
   2922 		break;
   2923 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   2924 		if (value != 0) {
   2925 			err = USBD_IOERROR;
   2926 			goto ret;
   2927 		}
   2928 		l = min(len, USB_HUB_DESCRIPTOR_SIZE);
   2929 		totlen = l;
   2930 		memcpy(buf, &uhci_hubd_piix, l);
   2931 		break;
   2932 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   2933 		if (len != 4) {
   2934 			err = USBD_IOERROR;
   2935 			goto ret;
   2936 		}
   2937 		memset(buf, 0, len);
   2938 		totlen = len;
   2939 		break;
   2940 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   2941 		if (index == 1)
   2942 			port = UHCI_PORTSC1;
   2943 		else if (index == 2)
   2944 			port = UHCI_PORTSC2;
   2945 		else {
   2946 			err = USBD_IOERROR;
   2947 			goto ret;
   2948 		}
   2949 		if (len != 4) {
   2950 			err = USBD_IOERROR;
   2951 			goto ret;
   2952 		}
   2953 		x = UREAD2(sc, port);
   2954 		status = change = 0;
   2955 		if (x & UHCI_PORTSC_CCS  )
   2956 			status |= UPS_CURRENT_CONNECT_STATUS;
   2957 		if (x & UHCI_PORTSC_CSC  )
   2958 			change |= UPS_C_CONNECT_STATUS;
   2959 		if (x & UHCI_PORTSC_PE   )
   2960 			status |= UPS_PORT_ENABLED;
   2961 		if (x & UHCI_PORTSC_POEDC)
   2962 			change |= UPS_C_PORT_ENABLED;
   2963 		if (x & UHCI_PORTSC_OCI  )
   2964 			status |= UPS_OVERCURRENT_INDICATOR;
   2965 		if (x & UHCI_PORTSC_OCIC )
   2966 			change |= UPS_C_OVERCURRENT_INDICATOR;
   2967 		if (x & UHCI_PORTSC_SUSP )
   2968 			status |= UPS_SUSPEND;
   2969 		if (x & UHCI_PORTSC_LSDA )
   2970 			status |= UPS_LOW_SPEED;
   2971 		status |= UPS_PORT_POWER;
   2972 		if (sc->sc_isreset)
   2973 			change |= UPS_C_PORT_RESET;
   2974 		USETW(ps.wPortStatus, status);
   2975 		USETW(ps.wPortChange, change);
   2976 		l = min(len, sizeof ps);
   2977 		memcpy(buf, &ps, l);
   2978 		totlen = l;
   2979 		break;
   2980 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   2981 		err = USBD_IOERROR;
   2982 		goto ret;
   2983 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   2984 		break;
   2985 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   2986 		if (index == 1)
   2987 			port = UHCI_PORTSC1;
   2988 		else if (index == 2)
   2989 			port = UHCI_PORTSC2;
   2990 		else {
   2991 			err = USBD_IOERROR;
   2992 			goto ret;
   2993 		}
   2994 		switch(value) {
   2995 		case UHF_PORT_ENABLE:
   2996 			x = UREAD2(sc, port);
   2997 			UWRITE2(sc, port, x | UHCI_PORTSC_PE);
   2998 			break;
   2999 		case UHF_PORT_SUSPEND:
   3000 			x = UREAD2(sc, port);
   3001 			UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
   3002 			break;
   3003 		case UHF_PORT_RESET:
   3004 			x = UREAD2(sc, port);
   3005 			UWRITE2(sc, port, x | UHCI_PORTSC_PR);
   3006 			usb_delay_ms(&sc->sc_bus, 10);
   3007 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   3008 			delay(100);
   3009 			x = UREAD2(sc, port);
   3010 			UWRITE2(sc, port, x  | UHCI_PORTSC_PE);
   3011 			delay(100);
   3012 			DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
   3013 				    index, UREAD2(sc, port)));
   3014 			sc->sc_isreset = 1;
   3015 			break;
   3016 		case UHF_C_PORT_CONNECTION:
   3017 		case UHF_C_PORT_ENABLE:
   3018 		case UHF_C_PORT_OVER_CURRENT:
   3019 		case UHF_PORT_CONNECTION:
   3020 		case UHF_PORT_OVER_CURRENT:
   3021 		case UHF_PORT_POWER:
   3022 		case UHF_PORT_LOW_SPEED:
   3023 		case UHF_C_PORT_SUSPEND:
   3024 		case UHF_C_PORT_RESET:
   3025 		default:
   3026 			err = USBD_IOERROR;
   3027 			goto ret;
   3028 		}
   3029 		break;
   3030 	default:
   3031 		err = USBD_IOERROR;
   3032 		goto ret;
   3033 	}
   3034 	xfer->actlen = totlen;
   3035 	err = USBD_NORMAL_COMPLETION;
   3036  ret:
   3037 	xfer->status = err;
   3038 	xfer->hcpriv = 0;
   3039 	s = splusb();
   3040 	usb_transfer_complete(xfer);
   3041 	splx(s);
   3042 	return (USBD_IN_PROGRESS);
   3043 }
   3044 
   3045 /* Abort a root control request. */
   3046 void
   3047 uhci_root_ctrl_abort(xfer)
   3048 	usbd_xfer_handle xfer;
   3049 {
   3050 	/* Nothing to do, all transfers are synchronous. */
   3051 }
   3052 
   3053 /* Close the root pipe. */
   3054 void
   3055 uhci_root_ctrl_close(pipe)
   3056 	usbd_pipe_handle pipe;
   3057 {
   3058 	DPRINTF(("uhci_root_ctrl_close\n"));
   3059 }
   3060 
   3061 /* Abort a root interrupt request. */
   3062 void
   3063 uhci_root_intr_abort(xfer)
   3064 	usbd_xfer_handle xfer;
   3065 {
   3066 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   3067 
   3068 	usb_untimeout(uhci_timo, xfer, xfer->timo_handle);
   3069 	sc->sc_has_timo = NULL;
   3070 
   3071 	if (xfer->pipe->intrxfer == xfer) {
   3072 		DPRINTF(("uhci_root_intr_abort: remove\n"));
   3073 		xfer->pipe->intrxfer = 0;
   3074 	}
   3075 	xfer->status = USBD_CANCELLED;
   3076 	usb_transfer_complete(xfer);
   3077 }
   3078 
   3079 usbd_status
   3080 uhci_root_intr_transfer(xfer)
   3081 	usbd_xfer_handle xfer;
   3082 {
   3083 	usbd_status err;
   3084 
   3085 	/* Insert last in queue. */
   3086 	err = usb_insert_transfer(xfer);
   3087 	if (err)
   3088 		return (err);
   3089 
   3090 	/* Pipe isn't running (otherwise err would be USBD_INPROG),
   3091 	 * start first
   3092 	 */
   3093 	return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   3094 }
   3095 
   3096 /* Start a transfer on the root interrupt pipe */
   3097 usbd_status
   3098 uhci_root_intr_start(xfer)
   3099 	usbd_xfer_handle xfer;
   3100 {
   3101 	usbd_pipe_handle pipe = xfer->pipe;
   3102 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   3103 
   3104 	DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
   3105 		     xfer, xfer->length, xfer->flags));
   3106 
   3107 	if (sc->sc_dying)
   3108 		return (USBD_IOERROR);
   3109 
   3110 	sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
   3111 	usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
   3112 	sc->sc_has_timo = xfer;
   3113 	return (USBD_IN_PROGRESS);
   3114 }
   3115 
   3116 /* Close the root interrupt pipe. */
   3117 void
   3118 uhci_root_intr_close(pipe)
   3119 	usbd_pipe_handle pipe;
   3120 {
   3121 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   3122 
   3123 	usb_untimeout(uhci_timo, pipe->intrxfer, pipe->intrxfer->timo_handle);
   3124 	sc->sc_has_timo = NULL;
   3125 	DPRINTF(("uhci_root_intr_close\n"));
   3126 }
   3127