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