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