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