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