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