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