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