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