Home | History | Annotate | Line # | Download | only in usb
uhci.c revision 1.65
      1 /*	$NetBSD: uhci.c,v 1.65 1999/11/16 22:19:03 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, nstatus;
    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, xfer=%p, pipe=%p ready\n",
   1018 		      ii, xfer, upipe));
   1019 	if (uhcidebug > 10)
   1020 		uhci_dump_tds(ii->stdstart);
   1021 #endif
   1022 
   1023 	/* The transfer is done, compute actual length and status. */
   1024 	actlen = 0;
   1025 	for (std = ii->stdstart; std != NULL; std = std->link.std) {
   1026 		nstatus = LE(std->td.td_status);
   1027 		if (nstatus & UHCI_TD_ACTIVE)
   1028 			break;
   1029 		status = nstatus;
   1030 		if (UHCI_TD_GET_PID(LE(std->td.td_token)) !=
   1031 		    UHCI_TD_PID_SETUP)
   1032 			actlen += UHCI_TD_GET_ACTLEN(status);
   1033 	}
   1034 	/* If there are left over TDs we need to update the toggle. */
   1035 	if (std != NULL)
   1036 		upipe->nexttoggle = UHCI_TD_GET_DT(LE(std->td.td_token));
   1037 
   1038 	status &= UHCI_TD_ERROR;
   1039 	DPRINTFN(10, ("uhci_check_intr: actlen=%d, status=0x%x\n",
   1040 		      actlen, status));
   1041 	xfer->actlen = actlen;
   1042 	if (status != 0) {
   1043 		DPRINTFN(-1+((status&UHCI_TD_STALLED)!=0),
   1044 			 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
   1045 			  "status 0x%b\n",
   1046 			  xfer->pipe->device->address,
   1047 			  xfer->pipe->endpoint->edesc->bEndpointAddress,
   1048 			  (int)status,
   1049 			  "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
   1050 			  "STALLED\30ACTIVE"));
   1051 		if (status == UHCI_TD_STALLED)
   1052 			xfer->status = USBD_STALLED;
   1053 		else
   1054 			xfer->status = USBD_IOERROR; /* more info XXX */
   1055 	} else {
   1056 		xfer->status = USBD_NORMAL_COMPLETION;
   1057 	}
   1058 	xfer->hcpriv = ii;
   1059 	usb_transfer_complete(xfer);
   1060 }
   1061 
   1062 /*
   1063  * Called when a request does not complete.
   1064  */
   1065 void
   1066 uhci_timeout(addr)
   1067 	void *addr;
   1068 {
   1069 	uhci_intr_info_t *ii = addr;
   1070 
   1071 	DPRINTF(("uhci_timeout: ii=%p\n", ii));
   1072 
   1073 	ii->xfer->device->bus->intr_context++;
   1074 	uhci_abort_req(ii->xfer, USBD_TIMEOUT);
   1075 	ii->xfer->device->bus->intr_context--;
   1076 }
   1077 
   1078 /*
   1079  * Wait here until controller claims to have an interrupt.
   1080  * Then call uhci_intr and return.  Use timeout to avoid waiting
   1081  * too long.
   1082  * Only used during boot when interrupts are not enabled yet.
   1083  */
   1084 void
   1085 uhci_waitintr(sc, xfer)
   1086 	uhci_softc_t *sc;
   1087 	usbd_xfer_handle xfer;
   1088 {
   1089 	int timo = xfer->timeout;
   1090 	uhci_intr_info_t *ii;
   1091 
   1092 	DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
   1093 
   1094 	xfer->status = USBD_IN_PROGRESS;
   1095 	for (; timo >= 0; timo--) {
   1096 		usb_delay_ms(&sc->sc_bus, 1);
   1097 		DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
   1098 		if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
   1099 			uhci_intr(sc);
   1100 			if (xfer->status != USBD_IN_PROGRESS)
   1101 				return;
   1102 		}
   1103 	}
   1104 
   1105 	/* Timeout */
   1106 	DPRINTF(("uhci_waitintr: timeout\n"));
   1107 	for (ii = LIST_FIRST(&sc->sc_intrhead);
   1108 	     ii != NULL && ii->xfer != xfer;
   1109 	     ii = LIST_NEXT(ii, list))
   1110 		;
   1111 #ifdef DIAGNOSTIC
   1112 	if (ii == NULL)
   1113 		panic("uhci_waitintr: lost intr_info\n");
   1114 #endif
   1115 	uhci_idone(ii);
   1116 }
   1117 
   1118 void
   1119 uhci_poll(bus)
   1120 	struct usbd_bus *bus;
   1121 {
   1122 	uhci_softc_t *sc = (uhci_softc_t *)bus;
   1123 
   1124 	if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
   1125 		uhci_intr(sc);
   1126 }
   1127 
   1128 #if 0
   1129 void
   1130 uhci_reset(p)
   1131 	void *p;
   1132 {
   1133 	uhci_softc_t *sc = p;
   1134 	int n;
   1135 
   1136 	UHCICMD(sc, UHCI_CMD_HCRESET);
   1137 	/* The reset bit goes low when the controller is done. */
   1138 	for (n = 0; n < UHCI_RESET_TIMEOUT &&
   1139 		    (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
   1140 		delay(100);
   1141 	if (n >= UHCI_RESET_TIMEOUT)
   1142 		printf("%s: controller did not reset\n",
   1143 		       USBDEVNAME(sc->sc_bus.bdev));
   1144 }
   1145 #endif
   1146 
   1147 usbd_status
   1148 uhci_run(sc, run)
   1149 	uhci_softc_t *sc;
   1150 	int run;
   1151 {
   1152 	int s, n, running;
   1153 
   1154 	run = run != 0;
   1155 	s = splusb();
   1156 	DPRINTF(("uhci_run: setting run=%d\n", run));
   1157 	UHCICMD(sc, run ? UHCI_CMD_RS : 0);
   1158 	for(n = 0; n < 10; n++) {
   1159 		running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
   1160 		/* return when we've entered the state we want */
   1161 		if (run == running) {
   1162 			splx(s);
   1163 			DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
   1164 				 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
   1165 			return (USBD_NORMAL_COMPLETION);
   1166 		}
   1167 		usb_delay_ms(&sc->sc_bus, 1);
   1168 	}
   1169 	splx(s);
   1170 	printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
   1171 	       run ? "start" : "stop");
   1172 	return (USBD_IOERROR);
   1173 }
   1174 
   1175 /*
   1176  * Memory management routines.
   1177  *  uhci_alloc_std allocates TDs
   1178  *  uhci_alloc_sqh allocates QHs
   1179  * These two routines do their own free list management,
   1180  * partly for speed, partly because allocating DMAable memory
   1181  * has page size granularaity so much memory would be wasted if
   1182  * only one TD/QH (32 bytes) was placed in each allocated chunk.
   1183  */
   1184 
   1185 uhci_soft_td_t *
   1186 uhci_alloc_std(sc)
   1187 	uhci_softc_t *sc;
   1188 {
   1189 	uhci_soft_td_t *std;
   1190 	usbd_status err;
   1191 	int i, offs;
   1192 	usb_dma_t dma;
   1193 
   1194 	if (sc->sc_freetds == NULL) {
   1195 		DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
   1196 		err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
   1197 			  UHCI_TD_ALIGN, &dma);
   1198 		if (err)
   1199 			return (0);
   1200 		for(i = 0; i < UHCI_STD_CHUNK; i++) {
   1201 			offs = i * UHCI_STD_SIZE;
   1202 			std = (uhci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
   1203 			std->physaddr = DMAADDR(&dma) + offs;
   1204 			std->link.std = sc->sc_freetds;
   1205 			sc->sc_freetds = std;
   1206 		}
   1207 	}
   1208 	std = sc->sc_freetds;
   1209 	sc->sc_freetds = std->link.std;
   1210 	memset(&std->td, 0, sizeof(uhci_td_t));
   1211 	return std;
   1212 }
   1213 
   1214 void
   1215 uhci_free_std(sc, std)
   1216 	uhci_softc_t *sc;
   1217 	uhci_soft_td_t *std;
   1218 {
   1219 #ifdef DIAGNOSTIC
   1220 #define TD_IS_FREE 0x12345678
   1221 	if (LE(std->td.td_token) == TD_IS_FREE) {
   1222 		printf("uhci_free_std: freeing free TD %p\n", std);
   1223 		return;
   1224 	}
   1225 	std->td.td_token = LE(TD_IS_FREE);
   1226 #endif
   1227 	std->link.std = sc->sc_freetds;
   1228 	sc->sc_freetds = std;
   1229 }
   1230 
   1231 uhci_soft_qh_t *
   1232 uhci_alloc_sqh(sc)
   1233 	uhci_softc_t *sc;
   1234 {
   1235 	uhci_soft_qh_t *sqh;
   1236 	usbd_status err;
   1237 	int i, offs;
   1238 	usb_dma_t dma;
   1239 
   1240 	if (sc->sc_freeqhs == NULL) {
   1241 		DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
   1242 		err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
   1243 			  UHCI_QH_ALIGN, &dma);
   1244 		if (err)
   1245 			return (0);
   1246 		for(i = 0; i < UHCI_SQH_CHUNK; i++) {
   1247 			offs = i * UHCI_SQH_SIZE;
   1248 			sqh = (uhci_soft_qh_t *)((char *)KERNADDR(&dma) +offs);
   1249 			sqh->physaddr = DMAADDR(&dma) + offs;
   1250 			sqh->hlink = sc->sc_freeqhs;
   1251 			sc->sc_freeqhs = sqh;
   1252 		}
   1253 	}
   1254 	sqh = sc->sc_freeqhs;
   1255 	sc->sc_freeqhs = sqh->hlink;
   1256 	memset(&sqh->qh, 0, sizeof(uhci_qh_t));
   1257 	return (sqh);
   1258 }
   1259 
   1260 void
   1261 uhci_free_sqh(sc, sqh)
   1262 	uhci_softc_t *sc;
   1263 	uhci_soft_qh_t *sqh;
   1264 {
   1265 	sqh->hlink = sc->sc_freeqhs;
   1266 	sc->sc_freeqhs = sqh;
   1267 }
   1268 
   1269 #if 0
   1270 /*
   1271  * Enter a list of transfers onto a control queue.
   1272  * Called at splusb()
   1273  */
   1274 void
   1275 uhci_enter_ctl_q(sc, sqh, ii)
   1276 	uhci_softc_t *sc;
   1277 	uhci_soft_qh_t *sqh;
   1278 	uhci_intr_info_t *ii;
   1279 {
   1280 	DPRINTFN(5, ("uhci_enter_ctl_q: sqh=%p\n", sqh));
   1281 
   1282 }
   1283 #endif
   1284 
   1285 void
   1286 uhci_free_std_chain(sc, std, stdend)
   1287 	uhci_softc_t *sc;
   1288 	uhci_soft_td_t *std;
   1289 	uhci_soft_td_t *stdend;
   1290 {
   1291 	uhci_soft_td_t *p;
   1292 
   1293 	for (; std != stdend; std = p) {
   1294 		p = std->link.std;
   1295 		uhci_free_std(sc, std);
   1296 	}
   1297 }
   1298 
   1299 usbd_status
   1300 uhci_alloc_std_chain(upipe, sc, len, rd, shortok, dma, sp, ep)
   1301 	struct uhci_pipe *upipe;
   1302 	uhci_softc_t *sc;
   1303 	int len, rd, shortok;
   1304 	usb_dma_t *dma;
   1305 	uhci_soft_td_t **sp, **ep;
   1306 {
   1307 	uhci_soft_td_t *p, *lastp;
   1308 	uhci_physaddr_t lastlink;
   1309 	int i, ntd, l, tog, maxp;
   1310 	u_int32_t status;
   1311 	int addr = upipe->pipe.device->address;
   1312 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1313 
   1314 	DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d ls=%d "
   1315 		      "shortok=%d\n", addr, UE_GET_ADDR(endpt), len,
   1316 		      upipe->pipe.device->lowspeed, shortok));
   1317 	if (len == 0) {
   1318 		*sp = *ep = 0;
   1319 		DPRINTFN(-1,("uhci_alloc_std_chain: len=0\n"));
   1320 		return (USBD_NORMAL_COMPLETION);
   1321 	}
   1322 	maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
   1323 	if (maxp == 0) {
   1324 		printf("uhci_alloc_std_chain: maxp=0\n");
   1325 		return (USBD_INVAL);
   1326 	}
   1327 	ntd = (len + maxp - 1) / maxp;
   1328 	DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
   1329 	tog = upipe->nexttoggle;
   1330 	if (ntd % 2 == 0)
   1331 		tog ^= 1;
   1332 	upipe->nexttoggle = tog ^ 1;
   1333 	lastp = 0;
   1334 	lastlink = UHCI_PTR_T;
   1335 	ntd--;
   1336 	status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
   1337 	if (upipe->pipe.device->lowspeed)
   1338 		status |= UHCI_TD_LS;
   1339 	if (shortok)
   1340 		status |= UHCI_TD_SPD;
   1341 	for (i = ntd; i >= 0; i--) {
   1342 		p = uhci_alloc_std(sc);
   1343 		if (p == NULL) {
   1344 			uhci_free_std_chain(sc, lastp, 0);
   1345 			return (USBD_NOMEM);
   1346 		}
   1347 		p->link.std = lastp;
   1348 		p->td.td_link = LE(lastlink);
   1349 		lastp = p;
   1350 		lastlink = p->physaddr;
   1351 		p->td.td_status = LE(status);
   1352 		if (i == ntd) {
   1353 			/* last TD */
   1354 			l = len % maxp;
   1355 			if (l == 0) l = maxp;
   1356 			*ep = p;
   1357 		} else
   1358 			l = maxp;
   1359 		p->td.td_token =
   1360 		    LE(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
   1361 			    UHCI_TD_OUT(l, endpt, addr, tog));
   1362 		p->td.td_buffer = LE(DMAADDR(dma) + i * maxp);
   1363 		tog ^= 1;
   1364 	}
   1365 	*sp = lastp;
   1366 	DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
   1367 		      upipe->nexttoggle));
   1368 	return (USBD_NORMAL_COMPLETION);
   1369 }
   1370 
   1371 void
   1372 uhci_device_clear_toggle(pipe)
   1373 	usbd_pipe_handle pipe;
   1374 {
   1375 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1376 	upipe->nexttoggle = 0;
   1377 }
   1378 
   1379 void
   1380 uhci_noop(pipe)
   1381 	usbd_pipe_handle pipe;
   1382 {
   1383 }
   1384 
   1385 usbd_status
   1386 uhci_device_bulk_transfer(xfer)
   1387 	usbd_xfer_handle xfer;
   1388 {
   1389 	usbd_status err;
   1390 
   1391 	/* Insert last in queue. */
   1392 	err = usb_insert_transfer(xfer);
   1393 	if (err)
   1394 		return (err);
   1395 
   1396 	/* Pipe isn't running, start first */
   1397 	return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1398 }
   1399 
   1400 usbd_status
   1401 uhci_device_bulk_start(xfer)
   1402 	usbd_xfer_handle xfer;
   1403 {
   1404 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1405 	usbd_device_handle dev = upipe->pipe.device;
   1406 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1407 	uhci_intr_info_t *ii = upipe->iinfo;
   1408 	uhci_soft_td_t *data, *dataend;
   1409 	uhci_soft_qh_t *sqh;
   1410 	usbd_status err;
   1411 	int len, isread, endpt;
   1412 	int s;
   1413 
   1414 	DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
   1415 		     xfer, xfer->length, xfer->flags));
   1416 
   1417 #ifdef DIAGNOSTIC
   1418 	if (xfer->rqflags & URQ_REQUEST)
   1419 		panic("uhci_device_bulk_transfer: a request\n");
   1420 #endif
   1421 
   1422 	len = xfer->length;
   1423 	endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
   1424 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   1425 	sqh = upipe->u.bulk.sqh;
   1426 
   1427 	upipe->u.bulk.isread = isread;
   1428 	upipe->u.bulk.length = len;
   1429 
   1430 	err = uhci_alloc_std_chain(upipe, sc, len, isread,
   1431 				 xfer->flags & USBD_SHORT_XFER_OK,
   1432 				 &xfer->dmabuf, &data, &dataend);
   1433 	if (err)
   1434 		return (err);
   1435 	dataend->td.td_status |= LE(UHCI_TD_IOC);
   1436 
   1437 #ifdef UHCI_DEBUG
   1438 	if (uhcidebug > 8) {
   1439 		DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
   1440 		uhci_dump_tds(data);
   1441 	}
   1442 #endif
   1443 
   1444 	/* Set up interrupt info. */
   1445 	ii->xfer = xfer;
   1446 	ii->stdstart = data;
   1447 	ii->stdend = dataend;
   1448 #if defined(__FreeBSD__)
   1449 	callout_handle_init(&ii->timeout_handle);
   1450 #endif
   1451 #ifdef DIAGNOSTIC
   1452 	ii->isdone = 0;
   1453 #endif
   1454 
   1455 	sqh->elink = data;
   1456 	sqh->qh.qh_elink = LE(data->physaddr);
   1457 	sqh->intr_info = ii;
   1458 
   1459 	s = splusb();
   1460 	uhci_add_bulk(sc, sqh);
   1461 	LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
   1462 
   1463 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   1464 		usb_timeout(uhci_timeout, ii, MS_TO_TICKS(xfer->timeout),
   1465 			    ii->timeout_handle);
   1466 	}
   1467 	splx(s);
   1468 
   1469 #ifdef UHCI_DEBUG
   1470 	if (uhcidebug > 10) {
   1471 		DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
   1472 		uhci_dump_tds(data);
   1473 	}
   1474 #endif
   1475 
   1476 	if (sc->sc_bus.use_polling)
   1477 		uhci_waitintr(sc, xfer);
   1478 
   1479 	return (USBD_IN_PROGRESS);
   1480 }
   1481 
   1482 /* Abort a device bulk request. */
   1483 void
   1484 uhci_device_bulk_abort(xfer)
   1485 	usbd_xfer_handle xfer;
   1486 {
   1487 	DPRINTF(("uhci_device_bulk_abort:\n"));
   1488 	uhci_abort_req(xfer, USBD_CANCELLED);
   1489 }
   1490 
   1491 void
   1492 uhci_abort_req(xfer, status)
   1493 	usbd_xfer_handle xfer;
   1494 	usbd_status status;
   1495 {
   1496 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1497 	uhci_intr_info_t *ii = upipe->iinfo;
   1498 	uhci_soft_td_t *std;
   1499 
   1500 	DPRINTFN(1,("uhci_abort_req: xfer=%p, status=%d\n", xfer, status));
   1501 
   1502 	/* Make interrupt routine ignore it, */
   1503 	xfer->status = status;
   1504 
   1505 	/* don't timeout, */
   1506 	usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
   1507 
   1508 	/* make hardware ignore it, */
   1509 	for (std = ii->stdstart; std != 0; std = std->link.std)
   1510 		std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
   1511 
   1512 	xfer->hcpriv = ii;
   1513 
   1514 	/* make sure hardware has completed, */
   1515 	if (xfer->device->bus->intr_context) {
   1516 		/* We have no process context, so we can't use tsleep(). */
   1517 		timeout(uhci_abort_req_end, xfer, hz / USB_FRAMES_PER_SECOND);
   1518 	} else {
   1519 		usb_delay_ms(xfer->pipe->device->bus, 1);
   1520 		/* and call final part of interrupt handler. */
   1521 		uhci_abort_req_end(xfer);
   1522 	}
   1523 }
   1524 
   1525 void
   1526 uhci_abort_req_end(v)
   1527 	void *v;
   1528 {
   1529 	usbd_xfer_handle xfer = v;
   1530 	int s;
   1531 
   1532 	s = splusb();
   1533 	usb_transfer_complete(xfer);
   1534 	splx(s);
   1535 }
   1536 
   1537 /* Close a device bulk pipe. */
   1538 void
   1539 uhci_device_bulk_close(pipe)
   1540 	usbd_pipe_handle pipe;
   1541 {
   1542 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1543 	usbd_device_handle dev = upipe->pipe.device;
   1544 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1545 
   1546 	uhci_free_sqh(sc, upipe->u.bulk.sqh);
   1547 	uhci_free_intr_info(upipe->iinfo);
   1548 	/* XXX free other resources */
   1549 }
   1550 
   1551 usbd_status
   1552 uhci_device_ctrl_transfer(xfer)
   1553 	usbd_xfer_handle xfer;
   1554 {
   1555 	usbd_status err;
   1556 
   1557 	/* Insert last in queue. */
   1558 	err = usb_insert_transfer(xfer);
   1559 	if (err)
   1560 		return (err);
   1561 
   1562 	/* Pipe isn't running, start first */
   1563 	return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1564 }
   1565 
   1566 usbd_status
   1567 uhci_device_ctrl_start(xfer)
   1568 	usbd_xfer_handle xfer;
   1569 {
   1570 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   1571 	usbd_status err;
   1572 
   1573 #ifdef DIAGNOSTIC
   1574 	if (!(xfer->rqflags & URQ_REQUEST))
   1575 		panic("uhci_device_ctrl_transfer: not a request\n");
   1576 #endif
   1577 
   1578 	err = uhci_device_request(xfer);
   1579 	if (err)
   1580 		return (err);
   1581 
   1582 	if (sc->sc_bus.use_polling)
   1583 		uhci_waitintr(sc, xfer);
   1584 	return (USBD_IN_PROGRESS);
   1585 }
   1586 
   1587 usbd_status
   1588 uhci_device_intr_transfer(xfer)
   1589 	usbd_xfer_handle xfer;
   1590 {
   1591 	usbd_status err;
   1592 
   1593 	/* Insert last in queue. */
   1594 	err = usb_insert_transfer(xfer);
   1595 	if (err)
   1596 		return (err);
   1597 
   1598 	/* Pipe isn't running, start first */
   1599 	return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1600 }
   1601 
   1602 usbd_status
   1603 uhci_device_intr_start(xfer)
   1604 	usbd_xfer_handle xfer;
   1605 {
   1606 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1607 	usbd_device_handle dev = upipe->pipe.device;
   1608 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1609 	uhci_intr_info_t *ii = upipe->iinfo;
   1610 	uhci_soft_td_t *data, *dataend;
   1611 	uhci_soft_qh_t *sqh;
   1612 	usbd_status err;
   1613 	int i, s;
   1614 
   1615 	DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
   1616 		    xfer, xfer->length, xfer->flags));
   1617 
   1618 #ifdef DIAGNOSTIC
   1619 	if (xfer->rqflags & URQ_REQUEST)
   1620 		panic("uhci_device_intr_transfer: a request\n");
   1621 #endif
   1622 
   1623 	err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
   1624  		  xfer->flags & USBD_SHORT_XFER_OK,
   1625 		  &xfer->dmabuf, &data, &dataend);
   1626 	if (err)
   1627 		return (err);
   1628 	dataend->td.td_status |= LE(UHCI_TD_IOC);
   1629 
   1630 #ifdef UHCI_DEBUG
   1631 	if (uhcidebug > 10) {
   1632 		DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
   1633 		uhci_dump_tds(data);
   1634 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   1635 	}
   1636 #endif
   1637 
   1638 	s = splusb();
   1639 	/* Set up interrupt info. */
   1640 	ii->xfer = xfer;
   1641 	ii->stdstart = data;
   1642 	ii->stdend = dataend;
   1643 #if defined(__FreeBSD__)
   1644 	callout_handle_init(&ii->timeout_handle);
   1645 #endif
   1646 #ifdef DIAGNOSTIC
   1647 	ii->isdone = 0;
   1648 #endif
   1649 
   1650 	DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
   1651 		     upipe->u.intr.qhs[0]));
   1652 	for (i = 0; i < upipe->u.intr.npoll; i++) {
   1653 		sqh = upipe->u.intr.qhs[i];
   1654 		sqh->elink = data;
   1655 		sqh->qh.qh_elink = LE(data->physaddr);
   1656 	}
   1657 	splx(s);
   1658 
   1659 #ifdef UHCI_DEBUG
   1660 	if (uhcidebug > 10) {
   1661 		DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
   1662 		uhci_dump_tds(data);
   1663 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   1664 	}
   1665 #endif
   1666 
   1667 	return (USBD_IN_PROGRESS);
   1668 }
   1669 
   1670 /* Abort a device control request. */
   1671 void
   1672 uhci_device_ctrl_abort(xfer)
   1673 	usbd_xfer_handle xfer;
   1674 {
   1675 	DPRINTF(("uhci_device_ctrl_abort:\n"));
   1676 	uhci_abort_req(xfer, USBD_CANCELLED);
   1677 }
   1678 
   1679 /* Close a device control pipe. */
   1680 void
   1681 uhci_device_ctrl_close(pipe)
   1682 	usbd_pipe_handle pipe;
   1683 {
   1684 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1685 
   1686 	uhci_free_intr_info(upipe->iinfo);
   1687 	/* XXX free other resources? */
   1688 }
   1689 
   1690 /* Abort a device interrupt request. */
   1691 void
   1692 uhci_device_intr_abort(xfer)
   1693 	usbd_xfer_handle xfer;
   1694 {
   1695 	DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
   1696 	if (xfer->pipe->intrxfer == xfer) {
   1697 		DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
   1698 		xfer->pipe->intrxfer = 0;
   1699 	}
   1700 	uhci_abort_req(xfer, USBD_CANCELLED);
   1701 }
   1702 
   1703 /* Close a device interrupt pipe. */
   1704 void
   1705 uhci_device_intr_close(pipe)
   1706 	usbd_pipe_handle pipe;
   1707 {
   1708 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1709 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   1710 	int i, s, npoll;
   1711 
   1712 	upipe->iinfo->stdstart = 0;		/* inactive */
   1713 
   1714 	/* Unlink descriptors from controller data structures. */
   1715 	npoll = upipe->u.intr.npoll;
   1716 	uhci_lock_frames(sc);
   1717 	for (i = 0; i < npoll; i++)
   1718 		uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos,
   1719 				 upipe->u.intr.qhs[i]);
   1720 	uhci_unlock_frames(sc);
   1721 
   1722 	/*
   1723 	 * We now have to wait for any activity on the physical
   1724 	 * descriptors to stop.
   1725 	 */
   1726 	usb_delay_ms(&sc->sc_bus, 2);
   1727 
   1728 	for(i = 0; i < npoll; i++)
   1729 		uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
   1730 	free(upipe->u.intr.qhs, M_USBHC);
   1731 
   1732 	s = splusb();
   1733 	LIST_REMOVE(upipe->iinfo, list);	/* remove from active list */
   1734 	splx(s);
   1735 	uhci_free_intr_info(upipe->iinfo);
   1736 
   1737 	/* XXX free other resources */
   1738 }
   1739 
   1740 usbd_status
   1741 uhci_device_request(xfer)
   1742 	usbd_xfer_handle xfer;
   1743 {
   1744 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1745 	usb_device_request_t *req = &xfer->request;
   1746 	usbd_device_handle dev = upipe->pipe.device;
   1747 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1748 	int addr = dev->address;
   1749 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1750 	uhci_intr_info_t *ii = upipe->iinfo;
   1751 	uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
   1752 	uhci_soft_qh_t *sqh;
   1753 	int len;
   1754 	u_int32_t ls;
   1755 	usbd_status err;
   1756 	int isread;
   1757 	int s;
   1758 
   1759 	DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
   1760 		    "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
   1761 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
   1762 		    UGETW(req->wIndex), UGETW(req->wLength),
   1763 		    addr, endpt));
   1764 
   1765 	ls = dev->lowspeed ? UHCI_TD_LS : 0;
   1766 	isread = req->bmRequestType & UT_READ;
   1767 	len = UGETW(req->wLength);
   1768 
   1769 	setup = upipe->u.ctl.setup;
   1770 	stat = upipe->u.ctl.stat;
   1771 	sqh = upipe->u.ctl.sqh;
   1772 
   1773 	/* Set up data transaction */
   1774 	if (len != 0) {
   1775 		upipe->nexttoggle = 1;
   1776 		err = uhci_alloc_std_chain(upipe, sc, len, isread,
   1777 			  xfer->flags & USBD_SHORT_XFER_OK,
   1778 			  &xfer->dmabuf, &data, &dataend);
   1779 		if (err)
   1780 			return (err);
   1781 		next = data;
   1782 		dataend->link.std = stat;
   1783 		dataend->td.td_link = LE(stat->physaddr);
   1784 	} else {
   1785 		next = stat;
   1786 	}
   1787 	upipe->u.ctl.length = len;
   1788 
   1789 	memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
   1790 
   1791 	setup->link.std = next;
   1792 	setup->td.td_link = LE(next->physaddr);
   1793 	setup->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls | UHCI_TD_ACTIVE);
   1794 	setup->td.td_token = LE(UHCI_TD_SETUP(sizeof *req, endpt, addr));
   1795 	setup->td.td_buffer = LE(DMAADDR(&upipe->u.ctl.reqdma));
   1796 
   1797 	stat->link.std = 0;
   1798 	stat->td.td_link = LE(UHCI_PTR_T);
   1799 	stat->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls |
   1800 		UHCI_TD_ACTIVE | UHCI_TD_IOC);
   1801 	stat->td.td_token =
   1802 		LE(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
   1803 		            UHCI_TD_IN (0, endpt, addr, 1));
   1804 	stat->td.td_buffer = LE(0);
   1805 
   1806 #ifdef UHCI_DEBUG
   1807 	if (uhcidebug > 20) {
   1808 		DPRINTF(("uhci_device_request: before transfer\n"));
   1809 		uhci_dump_tds(setup);
   1810 	}
   1811 #endif
   1812 
   1813 	/* Set up interrupt info. */
   1814 	ii->xfer = xfer;
   1815 	ii->stdstart = setup;
   1816 	ii->stdend = stat;
   1817 #if defined(__FreeBSD__)
   1818 	callout_handle_init(&ii->timeout_handle);
   1819 #endif
   1820 #ifdef DIAGNOSTIC
   1821 	ii->isdone = 0;
   1822 #endif
   1823 
   1824 	sqh->elink = setup;
   1825 	sqh->qh.qh_elink = LE(setup->physaddr);
   1826 	sqh->intr_info = ii;
   1827 
   1828 	s = splusb();
   1829 	uhci_add_ctrl(sc, sqh);
   1830 	LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
   1831 #ifdef UHCI_DEBUG
   1832 	if (uhcidebug > 12) {
   1833 		uhci_soft_td_t *std;
   1834 		uhci_soft_qh_t *xqh;
   1835 		uhci_soft_qh_t *sxqh;
   1836 		int maxqh = 0;
   1837 		uhci_physaddr_t link;
   1838 		DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
   1839 		for (std = sc->sc_vframes[0].htd, link = 0;
   1840 		     (link & UHCI_PTR_Q) == 0;
   1841 		     std = std->link.std) {
   1842 			link = LE(std->td.td_link);
   1843 			uhci_dump_td(std);
   1844 		}
   1845 		for (sxqh = xqh = (uhci_soft_qh_t *)std;
   1846 		     xqh != NULL;
   1847 		     xqh = (maxqh++ == 5 || xqh->hlink==sxqh ||
   1848                             xqh->hlink==xqh ? NULL : xqh->hlink)) {
   1849 			uhci_dump_qh(xqh);
   1850 			uhci_dump_qh(sxqh);
   1851 		}
   1852 		DPRINTF(("Enqueued QH:\n"));
   1853 		uhci_dump_qh(sqh);
   1854 		uhci_dump_tds(sqh->elink);
   1855 	}
   1856 #endif
   1857 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   1858 		usb_timeout(uhci_timeout, ii,
   1859                             MS_TO_TICKS(xfer->timeout), ii->timeout_handle);
   1860 	}
   1861 	splx(s);
   1862 
   1863 	return (USBD_NORMAL_COMPLETION);
   1864 }
   1865 
   1866 usbd_status
   1867 uhci_device_isoc_transfer(xfer)
   1868 	usbd_xfer_handle xfer;
   1869 {
   1870 	usbd_status err;
   1871 
   1872 	DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
   1873 
   1874 	/* Put it on our queue, */
   1875 	err = usb_insert_transfer(xfer);
   1876 
   1877 	/* bail out on error, */
   1878 	if (err && err != USBD_IN_PROGRESS)
   1879 		return (err);
   1880 
   1881 	/* XXX should check inuse here */
   1882 
   1883 	/* insert into schedule, */
   1884 	uhci_device_isoc_enter(xfer);
   1885 
   1886 	/* and put on interrupt list if the pipe wasn't running */
   1887 	if (err == USBD_NORMAL_COMPLETION)
   1888 		uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
   1889 
   1890 	return (err);
   1891 }
   1892 
   1893 void
   1894 uhci_device_isoc_enter(xfer)
   1895 	usbd_xfer_handle xfer;
   1896 {
   1897 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1898 	usbd_device_handle dev = upipe->pipe.device;
   1899 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1900 	struct iso *iso = &upipe->u.iso;
   1901 	uhci_soft_td_t *std;
   1902 	u_int32_t buf, len, status;
   1903 	int s, i, next, nframes;
   1904 
   1905 	DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
   1906 		    "nframes=%d\n",
   1907 		    iso->inuse, iso->next, xfer, xfer->nframes));
   1908 
   1909 	if (xfer->status == USBD_IN_PROGRESS) {
   1910 		/* This request has already been entered into the frame list */
   1911 	}
   1912 
   1913 #ifdef DIAGNOSTIC
   1914 	if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
   1915 		printf("uhci_device_isoc_enter: overflow!\n");
   1916 #endif
   1917 
   1918 	next = iso->next;
   1919 	if (next == -1) {
   1920 		/* Not in use yet, schedule it a few frames ahead. */
   1921 		next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
   1922 		DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
   1923 	}
   1924 
   1925 	xfer->status = USBD_IN_PROGRESS;
   1926 	xfer->hcprivint = next;
   1927 
   1928 	buf = DMAADDR(&xfer->dmabuf);
   1929 	status = LE(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
   1930 					UHCI_TD_ACTIVE |
   1931 					UHCI_TD_IOS));
   1932 	nframes = xfer->nframes;
   1933 	s = splusb();
   1934 	for (i = 0; i < nframes; i++) {
   1935 		std = iso->stds[next];
   1936 		if (++next >= UHCI_VFRAMELIST_COUNT)
   1937 			next = 0;
   1938 		len = xfer->frlengths[i];
   1939 		std->td.td_buffer = LE(buf);
   1940 		if (i == nframes - 1)
   1941 			status |= LE(UHCI_TD_IOC);
   1942 		std->td.td_status = status;
   1943 		std->td.td_token &= LE(~UHCI_TD_MAXLEN_MASK);
   1944 		std->td.td_token |= LE(UHCI_TD_SET_MAXLEN(len));
   1945 #ifdef UHCI_DEBUG
   1946 		if (uhcidebug > 5) {
   1947 			DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
   1948 			uhci_dump_td(std);
   1949 		}
   1950 #endif
   1951 		buf += len;
   1952 	}
   1953 	iso->next = next;
   1954 	iso->inuse += xfer->nframes;
   1955 
   1956 	splx(s);
   1957 }
   1958 
   1959 usbd_status
   1960 uhci_device_isoc_start(xfer)
   1961 	usbd_xfer_handle xfer;
   1962 {
   1963 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1964 	uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
   1965 	uhci_intr_info_t *ii = upipe->iinfo;
   1966 	uhci_soft_td_t *end;
   1967 	int s, i;
   1968 
   1969 #ifdef DIAGNOSTIC
   1970 	if (xfer->status != USBD_IN_PROGRESS)
   1971 		printf("uhci_device_isoc_start: not in progress %p\n", xfer);
   1972 #endif
   1973 
   1974 	/* Find the last TD */
   1975 	i = xfer->hcprivint + xfer->nframes;
   1976 	if (i >= UHCI_VFRAMELIST_COUNT)
   1977 		i -= UHCI_VFRAMELIST_COUNT;
   1978 	end = upipe->u.iso.stds[i];
   1979 
   1980 	s = splusb();
   1981 
   1982 	/* Set up interrupt info. */
   1983 	ii->xfer = xfer;
   1984 	ii->stdstart = end;
   1985 	ii->stdend = end;
   1986 #if defined(__FreeBSD__)
   1987 	callout_handle_init(&ii->timeout_handle);
   1988 #endif
   1989 #ifdef DIAGNOSTIC
   1990 	ii->isdone = 0;
   1991 #endif
   1992 	LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
   1993 
   1994 	splx(s);
   1995 
   1996 	return (USBD_IN_PROGRESS);
   1997 }
   1998 
   1999 void
   2000 uhci_device_isoc_abort(xfer)
   2001 	usbd_xfer_handle xfer;
   2002 {
   2003 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2004 	uhci_intr_info_t *ii = upipe->iinfo;
   2005 	uhci_soft_td_t **stds = upipe->u.iso.stds;
   2006 	uhci_soft_td_t *std;
   2007 	int i, n, nframes;
   2008 
   2009 	/* Make interrupt routine ignore it, */
   2010 	xfer->status = USBD_CANCELLED;
   2011 
   2012 	/* make hardware ignore it, */
   2013 	nframes = xfer->nframes;
   2014 	n = xfer->hcprivint;
   2015 	for (i = 0; i < nframes; i++) {
   2016 		std = stds[n];
   2017 		std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
   2018 		if (++n >= UHCI_VFRAMELIST_COUNT)
   2019 			n = 0;
   2020 	}
   2021 
   2022 	xfer->hcpriv = ii;
   2023 
   2024 	/* make sure hardware has completed, */
   2025 	if (xfer->device->bus->intr_context) {
   2026 		/* We have no process context, so we can't use tsleep(). */
   2027 		timeout(uhci_abort_req_end, xfer, hz / USB_FRAMES_PER_SECOND);
   2028 	} else {
   2029 		usb_delay_ms(xfer->pipe->device->bus, 1);
   2030 		/* and call final part of interrupt handler. */
   2031 		uhci_abort_req_end(xfer);
   2032 	}
   2033 }
   2034 
   2035 void
   2036 uhci_device_isoc_close(pipe)
   2037 	usbd_pipe_handle pipe;
   2038 {
   2039 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2040 	usbd_device_handle dev = upipe->pipe.device;
   2041 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2042 	uhci_soft_td_t *std, *vstd;
   2043 	struct iso *iso;
   2044 	int i;
   2045 
   2046 	/*
   2047 	 * Make sure all TDs are marked as inactive.
   2048 	 * Wait for completion.
   2049 	 * Unschedule.
   2050 	 * Deallocate.
   2051 	 */
   2052 	iso = &upipe->u.iso;
   2053 
   2054 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
   2055 		iso->stds[i]->td.td_status &= LE(~UHCI_TD_ACTIVE);
   2056 	usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
   2057 
   2058 	uhci_lock_frames(sc);
   2059 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2060 		std = iso->stds[i];
   2061 		for (vstd = sc->sc_vframes[i].htd;
   2062 		     vstd && vstd->link.std != std;
   2063 		     vstd = vstd->link.std)
   2064 			;
   2065 		if (!vstd) {
   2066 			/*panic*/
   2067 			printf("uhci_device_isoc_close: %p not found\n", std);
   2068 			uhci_unlock_frames(sc);
   2069 			return;
   2070 		}
   2071 		vstd->link = std->link;
   2072 		vstd->td.td_link = std->td.td_link;
   2073 		uhci_free_std(sc, std);
   2074 	}
   2075 	uhci_unlock_frames(sc);
   2076 
   2077 	free(iso->stds, M_USBHC);
   2078 }
   2079 
   2080 usbd_status
   2081 uhci_setup_isoc(pipe)
   2082 	usbd_pipe_handle pipe;
   2083 {
   2084 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2085 	usbd_device_handle dev = upipe->pipe.device;
   2086 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2087 	int addr = upipe->pipe.device->address;
   2088 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   2089 	int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
   2090 	uhci_soft_td_t *std, *vstd;
   2091 	u_int32_t token;
   2092 	struct iso *iso;
   2093 	int i;
   2094 
   2095 	iso = &upipe->u.iso;
   2096 	iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
   2097 			   M_USBHC, M_WAITOK);
   2098 
   2099 	token = LE(rd ? UHCI_TD_IN (0, endpt, addr, 0) :
   2100 			UHCI_TD_OUT(0, endpt, addr, 0));
   2101 
   2102 	/* Allocate the TDs and mark as inactive; */
   2103 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2104 		std = uhci_alloc_std(sc);
   2105 		if (std == 0)
   2106 			goto bad;
   2107 		std->td.td_status = LE(UHCI_TD_IOS);	/* iso, inactive */
   2108 		std->td.td_token = token;
   2109 		iso->stds[i] = std;
   2110 	}
   2111 
   2112 	/* Insert TDs into schedule. */
   2113 	uhci_lock_frames(sc);
   2114 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2115 		std = iso->stds[i];
   2116 		vstd = sc->sc_vframes[i].htd;
   2117 		std->link = vstd->link;
   2118 		std->td.td_link = vstd->td.td_link;
   2119 		vstd->link.std = std;
   2120 		vstd->td.td_link = LE(std->physaddr);
   2121 	}
   2122 	uhci_unlock_frames(sc);
   2123 
   2124 	iso->next = -1;
   2125 	iso->inuse = 0;
   2126 
   2127 	return (USBD_NORMAL_COMPLETION);
   2128 
   2129  bad:
   2130 	while (--i >= 0)
   2131 		uhci_free_std(sc, iso->stds[i]);
   2132 	free(iso->stds, M_USBHC);
   2133 	return (USBD_NOMEM);
   2134 }
   2135 
   2136 void
   2137 uhci_device_isoc_done(xfer)
   2138 	usbd_xfer_handle xfer;
   2139 {
   2140 	uhci_intr_info_t *ii = xfer->hcpriv;
   2141 
   2142 	DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
   2143 
   2144 	/* Turn off the interrupt since it is active even if the TD is not. */
   2145 	ii->stdend->td.td_status &= LE(~UHCI_TD_IOC);
   2146 
   2147 	LIST_REMOVE(ii, list);	/* remove from active list */
   2148 }
   2149 
   2150 void
   2151 uhci_device_intr_done(xfer)
   2152 	usbd_xfer_handle xfer;
   2153 {
   2154 	uhci_intr_info_t *ii = xfer->hcpriv;
   2155 	uhci_softc_t *sc = ii->sc;
   2156 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2157 	uhci_soft_qh_t *sqh;
   2158 	int i, npoll;
   2159 
   2160 	DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
   2161 
   2162 	npoll = upipe->u.intr.npoll;
   2163 	for(i = 0; i < npoll; i++) {
   2164 		sqh = upipe->u.intr.qhs[i];
   2165 		sqh->elink = 0;
   2166 		sqh->qh.qh_elink = LE(UHCI_PTR_T);
   2167 	}
   2168 	uhci_free_std_chain(sc, ii->stdstart, 0);
   2169 
   2170 	/* XXX Wasteful. */
   2171 	if (xfer->pipe->repeat) {
   2172 		uhci_soft_td_t *data, *dataend;
   2173 
   2174 		/* This alloc cannot fail since we freed the chain above. */
   2175 		uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
   2176 				     xfer->flags & USBD_SHORT_XFER_OK,
   2177 				     &xfer->dmabuf, &data, &dataend);
   2178 		dataend->td.td_status |= LE(UHCI_TD_IOC);
   2179 
   2180 #ifdef UHCI_DEBUG
   2181 		if (uhcidebug > 10) {
   2182 			DPRINTF(("uhci_device_intr_done: data(1)\n"));
   2183 			uhci_dump_tds(data);
   2184 			uhci_dump_qh(upipe->u.intr.qhs[0]);
   2185 		}
   2186 #endif
   2187 
   2188 		ii->stdstart = data;
   2189 		ii->stdend = dataend;
   2190 #if defined(__FreeBSD__)
   2191 		callout_handle_init(&ii->timeout_handle);
   2192 #endif
   2193 #ifdef DIAGNOSTIC
   2194 		ii->isdone = 0;
   2195 #endif
   2196 		for (i = 0; i < npoll; i++) {
   2197 			sqh = upipe->u.intr.qhs[i];
   2198 			sqh->elink = data;
   2199 			sqh->qh.qh_elink = LE(data->physaddr);
   2200 		}
   2201 	} else {
   2202 		ii->stdstart = 0;	/* mark as inactive */
   2203 	}
   2204 }
   2205 
   2206 /* Deallocate request data structures */
   2207 void
   2208 uhci_device_ctrl_done(xfer)
   2209 	usbd_xfer_handle xfer;
   2210 {
   2211 	uhci_intr_info_t *ii = xfer->hcpriv;
   2212 	uhci_softc_t *sc = ii->sc;
   2213 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2214 
   2215 #ifdef DIAGNOSTIC
   2216 	if (!(xfer->rqflags & URQ_REQUEST))
   2217 		panic("uhci_ctrl_done: not a request\n");
   2218 #endif
   2219 
   2220 	LIST_REMOVE(ii, list);	/* remove from active list */
   2221 
   2222 	uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
   2223 
   2224 	if (upipe->u.ctl.length != 0)
   2225 		uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
   2226 
   2227 	DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
   2228 }
   2229 
   2230 /* Deallocate request data structures */
   2231 void
   2232 uhci_device_bulk_done(xfer)
   2233 	usbd_xfer_handle xfer;
   2234 {
   2235 	uhci_intr_info_t *ii = xfer->hcpriv;
   2236 	uhci_softc_t *sc = ii->sc;
   2237 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2238 
   2239 	LIST_REMOVE(ii, list);	/* remove from active list */
   2240 
   2241 	uhci_remove_bulk(sc, upipe->u.bulk.sqh);
   2242 
   2243 	uhci_free_std_chain(sc, ii->stdstart, 0);
   2244 
   2245 	DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
   2246 }
   2247 
   2248 /* Add interrupt QH, called with vflock. */
   2249 void
   2250 uhci_add_intr(sc, n, sqh)
   2251 	uhci_softc_t *sc;
   2252 	int n;
   2253 	uhci_soft_qh_t *sqh;
   2254 {
   2255 	struct uhci_vframe *vf = &sc->sc_vframes[n];
   2256 	uhci_soft_qh_t *eqh;
   2257 
   2258 	DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
   2259 	eqh = vf->eqh;
   2260 	sqh->hlink       = eqh->hlink;
   2261 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
   2262 	eqh->hlink       = sqh;
   2263 	eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
   2264 	vf->eqh = sqh;
   2265 	vf->bandwidth++;
   2266 }
   2267 
   2268 /* Remove interrupt QH, called with vflock. */
   2269 void
   2270 uhci_remove_intr(sc, n, sqh)
   2271 	uhci_softc_t *sc;
   2272 	int n;
   2273 	uhci_soft_qh_t *sqh;
   2274 {
   2275 	struct uhci_vframe *vf = &sc->sc_vframes[n];
   2276 	uhci_soft_qh_t *pqh;
   2277 
   2278 	DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
   2279 
   2280 	for (pqh = vf->hqh; pqh->hlink != sqh; pqh = pqh->hlink)
   2281 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
   2282 		if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
   2283 			DPRINTF(("uhci_remove_intr: QH not found\n"));
   2284 			return;
   2285 		}
   2286 #else
   2287 		;
   2288 #endif
   2289 	pqh->hlink       = sqh->hlink;
   2290 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
   2291 	if (vf->eqh == sqh)
   2292 		vf->eqh = pqh;
   2293 	vf->bandwidth--;
   2294 }
   2295 
   2296 usbd_status
   2297 uhci_device_setintr(sc, upipe, ival)
   2298 	uhci_softc_t *sc;
   2299 	struct uhci_pipe *upipe;
   2300 	int ival;
   2301 {
   2302 	uhci_soft_qh_t *sqh;
   2303 	int i, npoll, s;
   2304 	u_int bestbw, bw, bestoffs, offs;
   2305 
   2306 	DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
   2307 	if (ival == 0) {
   2308 		printf("uhci_setintr: 0 interval\n");
   2309 		return (USBD_INVAL);
   2310 	}
   2311 
   2312 	if (ival > UHCI_VFRAMELIST_COUNT)
   2313 		ival = UHCI_VFRAMELIST_COUNT;
   2314 	npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
   2315 	DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
   2316 
   2317 	upipe->u.intr.npoll = npoll;
   2318 	upipe->u.intr.qhs =
   2319 		malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
   2320 
   2321 	/*
   2322 	 * Figure out which offset in the schedule that has most
   2323 	 * bandwidth left over.
   2324 	 */
   2325 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
   2326 	for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
   2327 		for (bw = i = 0; i < npoll; i++)
   2328 			bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
   2329 		if (bw < bestbw) {
   2330 			bestbw = bw;
   2331 			bestoffs = offs;
   2332 		}
   2333 	}
   2334 	DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
   2335 
   2336 	upipe->iinfo->stdstart = 0;
   2337 	for(i = 0; i < npoll; i++) {
   2338 		upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
   2339 		sqh->elink = 0;
   2340 		sqh->qh.qh_elink = LE(UHCI_PTR_T);
   2341 		sqh->pos = MOD(i * ival + bestoffs);
   2342 		sqh->intr_info = upipe->iinfo;
   2343 	}
   2344 #undef MOD
   2345 
   2346 	s = splusb();
   2347 	LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
   2348 	splx(s);
   2349 
   2350 	uhci_lock_frames(sc);
   2351 	/* Enter QHs into the controller data structures. */
   2352 	for(i = 0; i < npoll; i++)
   2353 		uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos,
   2354 			      upipe->u.intr.qhs[i]);
   2355 	uhci_unlock_frames(sc);
   2356 
   2357 	DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
   2358 	return (USBD_NORMAL_COMPLETION);
   2359 }
   2360 
   2361 /* Open a new pipe. */
   2362 usbd_status
   2363 uhci_open(pipe)
   2364 	usbd_pipe_handle pipe;
   2365 {
   2366 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2367 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2368 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
   2369 	usbd_status err;
   2370 
   2371 	DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
   2372 		     pipe, pipe->device->address,
   2373 		     ed->bEndpointAddress, sc->sc_addr));
   2374 	if (pipe->device->address == sc->sc_addr) {
   2375 		switch (ed->bEndpointAddress) {
   2376 		case USB_CONTROL_ENDPOINT:
   2377 			pipe->methods = &uhci_root_ctrl_methods;
   2378 			break;
   2379 		case UE_DIR_IN | UHCI_INTR_ENDPT:
   2380 			pipe->methods = &uhci_root_intr_methods;
   2381 			break;
   2382 		default:
   2383 			return (USBD_INVAL);
   2384 		}
   2385 	} else {
   2386 		upipe->iinfo = uhci_alloc_intr_info(sc);
   2387 		if (upipe->iinfo == 0)
   2388 			return (USBD_NOMEM);
   2389 		switch (ed->bmAttributes & UE_XFERTYPE) {
   2390 		case UE_CONTROL:
   2391 			pipe->methods = &uhci_device_ctrl_methods;
   2392 			upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
   2393 			if (upipe->u.ctl.sqh == NULL)
   2394 				goto bad;
   2395 			upipe->u.ctl.setup = uhci_alloc_std(sc);
   2396 			if (upipe->u.ctl.setup == NULL) {
   2397 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2398 				goto bad;
   2399 			}
   2400 			upipe->u.ctl.stat = uhci_alloc_std(sc);
   2401 			if (upipe->u.ctl.stat == NULL) {
   2402 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2403 				uhci_free_std(sc, upipe->u.ctl.setup);
   2404 				goto bad;
   2405 			}
   2406 			err = usb_allocmem(&sc->sc_bus,
   2407 				  sizeof(usb_device_request_t),
   2408 				  0, &upipe->u.ctl.reqdma);
   2409 			if (err) {
   2410 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2411 				uhci_free_std(sc, upipe->u.ctl.setup);
   2412 				uhci_free_std(sc, upipe->u.ctl.stat);
   2413 				goto bad;
   2414 			}
   2415 			break;
   2416 		case UE_INTERRUPT:
   2417 			pipe->methods = &uhci_device_intr_methods;
   2418 			return (uhci_device_setintr(sc, upipe, ed->bInterval));
   2419 		case UE_ISOCHRONOUS:
   2420 			pipe->methods = &uhci_device_isoc_methods;
   2421 			return (uhci_setup_isoc(pipe));
   2422 		case UE_BULK:
   2423 			pipe->methods = &uhci_device_bulk_methods;
   2424 			upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
   2425 			if (upipe->u.bulk.sqh == NULL)
   2426 				goto bad;
   2427 			break;
   2428 		}
   2429 	}
   2430 	return (USBD_NORMAL_COMPLETION);
   2431 
   2432  bad:
   2433 	uhci_free_intr_info(upipe->iinfo);
   2434 	return (USBD_NOMEM);
   2435 }
   2436 
   2437 /*
   2438  * Data structures and routines to emulate the root hub.
   2439  */
   2440 usb_device_descriptor_t uhci_devd = {
   2441 	USB_DEVICE_DESCRIPTOR_SIZE,
   2442 	UDESC_DEVICE,		/* type */
   2443 	{0x00, 0x01},		/* USB version */
   2444 	UCLASS_HUB,		/* class */
   2445 	USUBCLASS_HUB,		/* subclass */
   2446 	0,			/* protocol */
   2447 	64,			/* max packet */
   2448 	{0},{0},{0x00,0x01},	/* device id */
   2449 	1,2,0,			/* string indicies */
   2450 	1			/* # of configurations */
   2451 };
   2452 
   2453 usb_config_descriptor_t uhci_confd = {
   2454 	USB_CONFIG_DESCRIPTOR_SIZE,
   2455 	UDESC_CONFIG,
   2456 	{USB_CONFIG_DESCRIPTOR_SIZE +
   2457 	 USB_INTERFACE_DESCRIPTOR_SIZE +
   2458 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
   2459 	1,
   2460 	1,
   2461 	0,
   2462 	UC_SELF_POWERED,
   2463 	0			/* max power */
   2464 };
   2465 
   2466 usb_interface_descriptor_t uhci_ifcd = {
   2467 	USB_INTERFACE_DESCRIPTOR_SIZE,
   2468 	UDESC_INTERFACE,
   2469 	0,
   2470 	0,
   2471 	1,
   2472 	UCLASS_HUB,
   2473 	USUBCLASS_HUB,
   2474 	0,
   2475 	0
   2476 };
   2477 
   2478 usb_endpoint_descriptor_t uhci_endpd = {
   2479 	USB_ENDPOINT_DESCRIPTOR_SIZE,
   2480 	UDESC_ENDPOINT,
   2481 	UE_DIR_IN | UHCI_INTR_ENDPT,
   2482 	UE_INTERRUPT,
   2483 	{8},
   2484 	255
   2485 };
   2486 
   2487 usb_hub_descriptor_t uhci_hubd_piix = {
   2488 	USB_HUB_DESCRIPTOR_SIZE,
   2489 	UDESC_HUB,
   2490 	2,
   2491 	{ UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
   2492 	50,			/* power on to power good */
   2493 	0,
   2494 	{ 0x00 },		/* both ports are removable */
   2495 };
   2496 
   2497 int
   2498 uhci_str(p, l, s)
   2499 	usb_string_descriptor_t *p;
   2500 	int l;
   2501 	char *s;
   2502 {
   2503 	int i;
   2504 
   2505 	if (l == 0)
   2506 		return (0);
   2507 	p->bLength = 2 * strlen(s) + 2;
   2508 	if (l == 1)
   2509 		return (1);
   2510 	p->bDescriptorType = UDESC_STRING;
   2511 	l -= 2;
   2512 	for (i = 0; s[i] && l > 1; i++, l -= 2)
   2513 		USETW2(p->bString[i], 0, s[i]);
   2514 	return (2*i+2);
   2515 }
   2516 
   2517 /*
   2518  * Simulate a hardware hub by handling all the necessary requests.
   2519  */
   2520 usbd_status
   2521 uhci_root_ctrl_transfer(xfer)
   2522 	usbd_xfer_handle xfer;
   2523 {
   2524 	usbd_status err;
   2525 
   2526 	/* Insert last in queue. */
   2527 	err = usb_insert_transfer(xfer);
   2528 	if (err)
   2529 		return (err);
   2530 
   2531 	/* Pipe isn't running, start first */
   2532 	return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2533 }
   2534 
   2535 usbd_status
   2536 uhci_root_ctrl_start(xfer)
   2537 	usbd_xfer_handle xfer;
   2538 {
   2539 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   2540 	usb_device_request_t *req;
   2541 	void *buf = NULL;
   2542 	int port, x;
   2543 	int s, len, value, index, status, change, l, totlen = 0;
   2544 	usb_port_status_t ps;
   2545 	usbd_status err;
   2546 
   2547 #ifdef DIAGNOSTIC
   2548 	if (!(xfer->rqflags & URQ_REQUEST))
   2549 		panic("uhci_root_ctrl_transfer: not a request\n");
   2550 #endif
   2551 	req = &xfer->request;
   2552 
   2553 	DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
   2554 		    req->bmRequestType, req->bRequest));
   2555 
   2556 	len = UGETW(req->wLength);
   2557 	value = UGETW(req->wValue);
   2558 	index = UGETW(req->wIndex);
   2559 
   2560 	if (len != 0)
   2561 		buf = KERNADDR(&xfer->dmabuf);
   2562 
   2563 #define C(x,y) ((x) | ((y) << 8))
   2564 	switch(C(req->bRequest, req->bmRequestType)) {
   2565 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
   2566 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
   2567 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
   2568 		/*
   2569 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
   2570 		 * for the integrated root hub.
   2571 		 */
   2572 		break;
   2573 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
   2574 		if (len > 0) {
   2575 			*(u_int8_t *)buf = sc->sc_conf;
   2576 			totlen = 1;
   2577 		}
   2578 		break;
   2579 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   2580 		DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
   2581 		switch(value >> 8) {
   2582 		case UDESC_DEVICE:
   2583 			if ((value & 0xff) != 0) {
   2584 				err = USBD_IOERROR;
   2585 				goto ret;
   2586 			}
   2587 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   2588 			USETW(uhci_devd.idVendor, sc->sc_id_vendor);
   2589 			memcpy(buf, &uhci_devd, l);
   2590 			break;
   2591 		case UDESC_CONFIG:
   2592 			if ((value & 0xff) != 0) {
   2593 				err = USBD_IOERROR;
   2594 				goto ret;
   2595 			}
   2596 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
   2597 			memcpy(buf, &uhci_confd, l);
   2598 			buf = (char *)buf + l;
   2599 			len -= l;
   2600 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
   2601 			totlen += l;
   2602 			memcpy(buf, &uhci_ifcd, l);
   2603 			buf = (char *)buf + l;
   2604 			len -= l;
   2605 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
   2606 			totlen += l;
   2607 			memcpy(buf, &uhci_endpd, l);
   2608 			break;
   2609 		case UDESC_STRING:
   2610 			if (len == 0)
   2611 				break;
   2612 			*(u_int8_t *)buf = 0;
   2613 			totlen = 1;
   2614 			switch (value & 0xff) {
   2615 			case 1: /* Vendor */
   2616 				totlen = uhci_str(buf, len, sc->sc_vendor);
   2617 				break;
   2618 			case 2: /* Product */
   2619 				totlen = uhci_str(buf, len, "UHCI root hub");
   2620 				break;
   2621 			}
   2622 			break;
   2623 		default:
   2624 			err = USBD_IOERROR;
   2625 			goto ret;
   2626 		}
   2627 		break;
   2628 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   2629 		if (len > 0) {
   2630 			*(u_int8_t *)buf = 0;
   2631 			totlen = 1;
   2632 		}
   2633 		break;
   2634 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   2635 		if (len > 1) {
   2636 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   2637 			totlen = 2;
   2638 		}
   2639 		break;
   2640 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   2641 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   2642 		if (len > 1) {
   2643 			USETW(((usb_status_t *)buf)->wStatus, 0);
   2644 			totlen = 2;
   2645 		}
   2646 		break;
   2647 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   2648 		if (value >= USB_MAX_DEVICES) {
   2649 			err = USBD_IOERROR;
   2650 			goto ret;
   2651 		}
   2652 		sc->sc_addr = value;
   2653 		break;
   2654 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   2655 		if (value != 0 && value != 1) {
   2656 			err = USBD_IOERROR;
   2657 			goto ret;
   2658 		}
   2659 		sc->sc_conf = value;
   2660 		break;
   2661 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   2662 		break;
   2663 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   2664 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   2665 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   2666 		err = USBD_IOERROR;
   2667 		goto ret;
   2668 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   2669 		break;
   2670 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   2671 		break;
   2672 	/* Hub requests */
   2673 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   2674 		break;
   2675 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   2676 		DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
   2677 			     "port=%d feature=%d\n",
   2678 			     index, value));
   2679 		if (index == 1)
   2680 			port = UHCI_PORTSC1;
   2681 		else if (index == 2)
   2682 			port = UHCI_PORTSC2;
   2683 		else {
   2684 			err = USBD_IOERROR;
   2685 			goto ret;
   2686 		}
   2687 		switch(value) {
   2688 		case UHF_PORT_ENABLE:
   2689 			x = UREAD2(sc, port);
   2690 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
   2691 			break;
   2692 		case UHF_PORT_SUSPEND:
   2693 			x = UREAD2(sc, port);
   2694 			UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
   2695 			break;
   2696 		case UHF_PORT_RESET:
   2697 			x = UREAD2(sc, port);
   2698 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   2699 			break;
   2700 		case UHF_C_PORT_CONNECTION:
   2701 			x = UREAD2(sc, port);
   2702 			UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
   2703 			break;
   2704 		case UHF_C_PORT_ENABLE:
   2705 			x = UREAD2(sc, port);
   2706 			UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
   2707 			break;
   2708 		case UHF_C_PORT_OVER_CURRENT:
   2709 			x = UREAD2(sc, port);
   2710 			UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
   2711 			break;
   2712 		case UHF_C_PORT_RESET:
   2713 			sc->sc_isreset = 0;
   2714 			err = USBD_NORMAL_COMPLETION;
   2715 			goto ret;
   2716 		case UHF_PORT_CONNECTION:
   2717 		case UHF_PORT_OVER_CURRENT:
   2718 		case UHF_PORT_POWER:
   2719 		case UHF_PORT_LOW_SPEED:
   2720 		case UHF_C_PORT_SUSPEND:
   2721 		default:
   2722 			err = USBD_IOERROR;
   2723 			goto ret;
   2724 		}
   2725 		break;
   2726 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
   2727 		if (index == 1)
   2728 			port = UHCI_PORTSC1;
   2729 		else if (index == 2)
   2730 			port = UHCI_PORTSC2;
   2731 		else {
   2732 			err = USBD_IOERROR;
   2733 			goto ret;
   2734 		}
   2735 		if (len > 0) {
   2736 			*(u_int8_t *)buf =
   2737 				(UREAD2(sc, port) & UHCI_PORTSC_LS) >>
   2738 				UHCI_PORTSC_LS_SHIFT;
   2739 			totlen = 1;
   2740 		}
   2741 		break;
   2742 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   2743 		if (value != 0) {
   2744 			err = USBD_IOERROR;
   2745 			goto ret;
   2746 		}
   2747 		l = min(len, USB_HUB_DESCRIPTOR_SIZE);
   2748 		totlen = l;
   2749 		memcpy(buf, &uhci_hubd_piix, l);
   2750 		break;
   2751 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   2752 		if (len != 4) {
   2753 			err = USBD_IOERROR;
   2754 			goto ret;
   2755 		}
   2756 		memset(buf, 0, len);
   2757 		totlen = len;
   2758 		break;
   2759 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   2760 		if (index == 1)
   2761 			port = UHCI_PORTSC1;
   2762 		else if (index == 2)
   2763 			port = UHCI_PORTSC2;
   2764 		else {
   2765 			err = USBD_IOERROR;
   2766 			goto ret;
   2767 		}
   2768 		if (len != 4) {
   2769 			err = USBD_IOERROR;
   2770 			goto ret;
   2771 		}
   2772 		x = UREAD2(sc, port);
   2773 		status = change = 0;
   2774 		if (x & UHCI_PORTSC_CCS  )
   2775 			status |= UPS_CURRENT_CONNECT_STATUS;
   2776 		if (x & UHCI_PORTSC_CSC  )
   2777 			change |= UPS_C_CONNECT_STATUS;
   2778 		if (x & UHCI_PORTSC_PE   )
   2779 			status |= UPS_PORT_ENABLED;
   2780 		if (x & UHCI_PORTSC_POEDC)
   2781 			change |= UPS_C_PORT_ENABLED;
   2782 		if (x & UHCI_PORTSC_OCI  )
   2783 			status |= UPS_OVERCURRENT_INDICATOR;
   2784 		if (x & UHCI_PORTSC_OCIC )
   2785 			change |= UPS_C_OVERCURRENT_INDICATOR;
   2786 		if (x & UHCI_PORTSC_SUSP )
   2787 			status |= UPS_SUSPEND;
   2788 		if (x & UHCI_PORTSC_LSDA )
   2789 			status |= UPS_LOW_SPEED;
   2790 		status |= UPS_PORT_POWER;
   2791 		if (sc->sc_isreset)
   2792 			change |= UPS_C_PORT_RESET;
   2793 		USETW(ps.wPortStatus, status);
   2794 		USETW(ps.wPortChange, change);
   2795 		l = min(len, sizeof ps);
   2796 		memcpy(buf, &ps, l);
   2797 		totlen = l;
   2798 		break;
   2799 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   2800 		err = USBD_IOERROR;
   2801 		goto ret;
   2802 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   2803 		break;
   2804 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   2805 		if (index == 1)
   2806 			port = UHCI_PORTSC1;
   2807 		else if (index == 2)
   2808 			port = UHCI_PORTSC2;
   2809 		else {
   2810 			err = USBD_IOERROR;
   2811 			goto ret;
   2812 		}
   2813 		switch(value) {
   2814 		case UHF_PORT_ENABLE:
   2815 			x = UREAD2(sc, port);
   2816 			UWRITE2(sc, port, x | UHCI_PORTSC_PE);
   2817 			break;
   2818 		case UHF_PORT_SUSPEND:
   2819 			x = UREAD2(sc, port);
   2820 			UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
   2821 			break;
   2822 		case UHF_PORT_RESET:
   2823 			x = UREAD2(sc, port);
   2824 			UWRITE2(sc, port, x | UHCI_PORTSC_PR);
   2825 			usb_delay_ms(&sc->sc_bus, 10);
   2826 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   2827 			delay(100);
   2828 			x = UREAD2(sc, port);
   2829 			UWRITE2(sc, port, x  | UHCI_PORTSC_PE);
   2830 			delay(100);
   2831 			DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
   2832 				    index, UREAD2(sc, port)));
   2833 			sc->sc_isreset = 1;
   2834 			break;
   2835 		case UHF_C_PORT_CONNECTION:
   2836 		case UHF_C_PORT_ENABLE:
   2837 		case UHF_C_PORT_OVER_CURRENT:
   2838 		case UHF_PORT_CONNECTION:
   2839 		case UHF_PORT_OVER_CURRENT:
   2840 		case UHF_PORT_POWER:
   2841 		case UHF_PORT_LOW_SPEED:
   2842 		case UHF_C_PORT_SUSPEND:
   2843 		case UHF_C_PORT_RESET:
   2844 		default:
   2845 			err = USBD_IOERROR;
   2846 			goto ret;
   2847 		}
   2848 		break;
   2849 	default:
   2850 		err = USBD_IOERROR;
   2851 		goto ret;
   2852 	}
   2853 	xfer->actlen = totlen;
   2854 	err = USBD_NORMAL_COMPLETION;
   2855  ret:
   2856 	xfer->status = err;
   2857 	xfer->hcpriv = 0;
   2858 	s = splusb();
   2859 	usb_transfer_complete(xfer);
   2860 	splx(s);
   2861 	return (USBD_IN_PROGRESS);
   2862 }
   2863 
   2864 /* Abort a root control request. */
   2865 void
   2866 uhci_root_ctrl_abort(xfer)
   2867 	usbd_xfer_handle xfer;
   2868 {
   2869 	/* Nothing to do, all transfers are syncronous. */
   2870 }
   2871 
   2872 /* Close the root pipe. */
   2873 void
   2874 uhci_root_ctrl_close(pipe)
   2875 	usbd_pipe_handle pipe;
   2876 {
   2877 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2878 
   2879 	sc->sc_has_timo = 0;
   2880 	DPRINTF(("uhci_root_ctrl_close\n"));
   2881 }
   2882 
   2883 /* Abort a root interrupt request. */
   2884 void
   2885 uhci_root_intr_abort(xfer)
   2886 	usbd_xfer_handle xfer;
   2887 {
   2888 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   2889 
   2890 	usb_untimeout(uhci_timo, xfer, xfer->timo_handle);
   2891 	sc->sc_has_timo = 0;
   2892 
   2893 	if (xfer->pipe->intrxfer == xfer) {
   2894 		DPRINTF(("uhci_root_intr_abort: remove\n"));
   2895 		xfer->pipe->intrxfer = 0;
   2896 	}
   2897 	xfer->status = USBD_CANCELLED;
   2898 	usb_transfer_complete(xfer);
   2899 }
   2900 
   2901 usbd_status
   2902 uhci_root_intr_transfer(xfer)
   2903 	usbd_xfer_handle xfer;
   2904 {
   2905 	usbd_status err;
   2906 
   2907 	/* Insert last in queue. */
   2908 	err = usb_insert_transfer(xfer);
   2909 	if (err)
   2910 		return (err);
   2911 
   2912 	/* Pipe isn't running, start first */
   2913 	return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2914 }
   2915 
   2916 /* Start a transfer on the root interrupt pipe */
   2917 usbd_status
   2918 uhci_root_intr_start(xfer)
   2919 	usbd_xfer_handle xfer;
   2920 {
   2921 	usbd_pipe_handle pipe = xfer->pipe;
   2922 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2923 
   2924 	DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
   2925 		     xfer, xfer->length, xfer->flags));
   2926 
   2927 	sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
   2928 	usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
   2929 	sc->sc_has_timo = xfer;
   2930 	return (USBD_IN_PROGRESS);
   2931 }
   2932 
   2933 /* Close the root interrupt pipe. */
   2934 void
   2935 uhci_root_intr_close(pipe)
   2936 	usbd_pipe_handle pipe;
   2937 {
   2938 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2939 
   2940 	usb_untimeout(uhci_timo, pipe->intrxfer, pipe->intrxfer->timo_handle);
   2941 	sc->sc_has_timo = 0;
   2942 	DPRINTF(("uhci_root_intr_close\n"));
   2943 }
   2944