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