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