Home | History | Annotate | Line # | Download | only in usb
uhci.c revision 1.130
      1 /*	$NetBSD: uhci.c,v 1.130 2000/12/18 15:55:30 tsutsui 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://www.intel.com/design/usb/uhci11d.pdf
     46  * USB spec: http://www.usb.org/developers/data/usb11.pdf
     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_busreset(uhci_softc_t *);
    162 Static void		uhci_shutdown(void *v);
    163 Static void		uhci_power(int, void *);
    164 Static usbd_status	uhci_run(uhci_softc_t *, int run);
    165 Static uhci_soft_td_t  *uhci_alloc_std(uhci_softc_t *);
    166 Static void		uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
    167 Static uhci_soft_qh_t  *uhci_alloc_sqh(uhci_softc_t *);
    168 Static void		uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
    169 #if 0
    170 Static void		uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
    171 					 uhci_intr_info_t *);
    172 Static void		uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
    173 #endif
    174 
    175 Static void		uhci_free_std_chain(uhci_softc_t *,
    176 					    uhci_soft_td_t *, uhci_soft_td_t *);
    177 Static usbd_status	uhci_alloc_std_chain(struct uhci_pipe *,
    178 			    uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
    179 			    uhci_soft_td_t **, uhci_soft_td_t **);
    180 Static void		uhci_poll_hub(void *);
    181 Static void		uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
    182 Static void		uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
    183 Static void		uhci_idone(uhci_intr_info_t *);
    184 
    185 Static void		uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
    186 
    187 Static void		uhci_timeout(void *);
    188 Static void		uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
    189 Static void		uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
    190 Static void		uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
    191 Static void		uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
    192 Static void		uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
    193 Static void		uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
    194 Static int		uhci_str(usb_string_descriptor_t *, int, char *);
    195 Static void		uhci_add_loop(uhci_softc_t *sc);
    196 Static void		uhci_rem_loop(uhci_softc_t *sc);
    197 
    198 Static usbd_status	uhci_setup_isoc(usbd_pipe_handle pipe);
    199 Static void		uhci_device_isoc_enter(usbd_xfer_handle);
    200 
    201 Static usbd_status	uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
    202 Static void		uhci_freem(struct usbd_bus *, usb_dma_t *);
    203 
    204 Static usbd_xfer_handle	uhci_allocx(struct usbd_bus *);
    205 Static void		uhci_freex(struct usbd_bus *, usbd_xfer_handle);
    206 
    207 Static usbd_status	uhci_device_ctrl_transfer(usbd_xfer_handle);
    208 Static usbd_status	uhci_device_ctrl_start(usbd_xfer_handle);
    209 Static void		uhci_device_ctrl_abort(usbd_xfer_handle);
    210 Static void		uhci_device_ctrl_close(usbd_pipe_handle);
    211 Static void		uhci_device_ctrl_done(usbd_xfer_handle);
    212 
    213 Static usbd_status	uhci_device_intr_transfer(usbd_xfer_handle);
    214 Static usbd_status	uhci_device_intr_start(usbd_xfer_handle);
    215 Static void		uhci_device_intr_abort(usbd_xfer_handle);
    216 Static void		uhci_device_intr_close(usbd_pipe_handle);
    217 Static void		uhci_device_intr_done(usbd_xfer_handle);
    218 
    219 Static usbd_status	uhci_device_bulk_transfer(usbd_xfer_handle);
    220 Static usbd_status	uhci_device_bulk_start(usbd_xfer_handle);
    221 Static void		uhci_device_bulk_abort(usbd_xfer_handle);
    222 Static void		uhci_device_bulk_close(usbd_pipe_handle);
    223 Static void		uhci_device_bulk_done(usbd_xfer_handle);
    224 
    225 Static usbd_status	uhci_device_isoc_transfer(usbd_xfer_handle);
    226 Static usbd_status	uhci_device_isoc_start(usbd_xfer_handle);
    227 Static void		uhci_device_isoc_abort(usbd_xfer_handle);
    228 Static void		uhci_device_isoc_close(usbd_pipe_handle);
    229 Static void		uhci_device_isoc_done(usbd_xfer_handle);
    230 
    231 Static usbd_status	uhci_root_ctrl_transfer(usbd_xfer_handle);
    232 Static usbd_status	uhci_root_ctrl_start(usbd_xfer_handle);
    233 Static void		uhci_root_ctrl_abort(usbd_xfer_handle);
    234 Static void		uhci_root_ctrl_close(usbd_pipe_handle);
    235 Static void		uhci_root_ctrl_done(usbd_xfer_handle);
    236 
    237 Static usbd_status	uhci_root_intr_transfer(usbd_xfer_handle);
    238 Static usbd_status	uhci_root_intr_start(usbd_xfer_handle);
    239 Static void		uhci_root_intr_abort(usbd_xfer_handle);
    240 Static void		uhci_root_intr_close(usbd_pipe_handle);
    241 Static void		uhci_root_intr_done(usbd_xfer_handle);
    242 
    243 Static usbd_status	uhci_open(usbd_pipe_handle);
    244 Static void		uhci_poll(struct usbd_bus *);
    245 Static void		uhci_softintr(struct usbd_bus *);
    246 
    247 Static usbd_status	uhci_device_request(usbd_xfer_handle xfer);
    248 
    249 Static void		uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
    250 Static void		uhci_remove_intr(uhci_softc_t*, uhci_soft_qh_t*);
    251 Static usbd_status	uhci_device_setintr(uhci_softc_t *sc,
    252 			    struct uhci_pipe *pipe, int ival);
    253 
    254 Static void		uhci_device_clear_toggle(usbd_pipe_handle pipe);
    255 Static void		uhci_noop(usbd_pipe_handle pipe);
    256 
    257 Static __inline__ uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
    258 						    uhci_soft_qh_t *);
    259 
    260 #ifdef UHCI_DEBUG
    261 Static void		uhci_dump_all(uhci_softc_t *);
    262 Static void		uhci_dumpregs(uhci_softc_t *);
    263 Static void		uhci_dump_qhs(uhci_soft_qh_t *);
    264 Static void		uhci_dump_qh(uhci_soft_qh_t *);
    265 Static void		uhci_dump_tds(uhci_soft_td_t *);
    266 Static void		uhci_dump_td(uhci_soft_td_t *);
    267 Static void		uhci_dump_ii(uhci_intr_info_t *ii);
    268 void			uhci_dump(void);
    269 #endif
    270 
    271 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
    272 			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
    273 #define UWRITE1(sc, r, x) \
    274  do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
    275 #define UWRITE2(sc, r, x) \
    276  do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
    277 #define UWRITE4(sc, r, x) \
    278  do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
    279 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
    280 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
    281 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
    282 
    283 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
    284 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
    285 
    286 #define UHCI_RESET_TIMEOUT 100	/* reset timeout */
    287 
    288 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
    289 
    290 #define UHCI_INTR_ENDPT 1
    291 
    292 struct usbd_bus_methods uhci_bus_methods = {
    293 	uhci_open,
    294 	uhci_softintr,
    295 	uhci_poll,
    296 	uhci_allocm,
    297 	uhci_freem,
    298 	uhci_allocx,
    299 	uhci_freex,
    300 };
    301 
    302 struct usbd_pipe_methods uhci_root_ctrl_methods = {
    303 	uhci_root_ctrl_transfer,
    304 	uhci_root_ctrl_start,
    305 	uhci_root_ctrl_abort,
    306 	uhci_root_ctrl_close,
    307 	uhci_noop,
    308 	uhci_root_ctrl_done,
    309 };
    310 
    311 struct usbd_pipe_methods uhci_root_intr_methods = {
    312 	uhci_root_intr_transfer,
    313 	uhci_root_intr_start,
    314 	uhci_root_intr_abort,
    315 	uhci_root_intr_close,
    316 	uhci_noop,
    317 	uhci_root_intr_done,
    318 };
    319 
    320 struct usbd_pipe_methods uhci_device_ctrl_methods = {
    321 	uhci_device_ctrl_transfer,
    322 	uhci_device_ctrl_start,
    323 	uhci_device_ctrl_abort,
    324 	uhci_device_ctrl_close,
    325 	uhci_noop,
    326 	uhci_device_ctrl_done,
    327 };
    328 
    329 struct usbd_pipe_methods uhci_device_intr_methods = {
    330 	uhci_device_intr_transfer,
    331 	uhci_device_intr_start,
    332 	uhci_device_intr_abort,
    333 	uhci_device_intr_close,
    334 	uhci_device_clear_toggle,
    335 	uhci_device_intr_done,
    336 };
    337 
    338 struct usbd_pipe_methods uhci_device_bulk_methods = {
    339 	uhci_device_bulk_transfer,
    340 	uhci_device_bulk_start,
    341 	uhci_device_bulk_abort,
    342 	uhci_device_bulk_close,
    343 	uhci_device_clear_toggle,
    344 	uhci_device_bulk_done,
    345 };
    346 
    347 struct usbd_pipe_methods uhci_device_isoc_methods = {
    348 	uhci_device_isoc_transfer,
    349 	uhci_device_isoc_start,
    350 	uhci_device_isoc_abort,
    351 	uhci_device_isoc_close,
    352 	uhci_noop,
    353 	uhci_device_isoc_done,
    354 };
    355 
    356 #define uhci_add_intr_info(sc, ii) \
    357 	LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list);
    358 #define uhci_del_intr_info(ii) \
    359 	LIST_REMOVE((ii), list)
    360 
    361 Static __inline__ uhci_soft_qh_t *
    362 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
    363 {
    364 	DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
    365 
    366 	for (; pqh->hlink != sqh; pqh = pqh->hlink) {
    367 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
    368 		if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
    369 			printf("uhci_find_prev_qh: QH not found\n");
    370 			return (NULL);
    371 		}
    372 #endif
    373 	}
    374 	return (pqh);
    375 }
    376 
    377 void
    378 uhci_busreset(uhci_softc_t *sc)
    379 {
    380 	UHCICMD(sc, UHCI_CMD_GRESET);	/* global reset */
    381 	usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
    382 	UHCICMD(sc, 0);			/* do nothing */
    383 }
    384 
    385 usbd_status
    386 uhci_init(uhci_softc_t *sc)
    387 {
    388 	usbd_status err;
    389 	int i, j;
    390 	uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
    391 	uhci_soft_td_t *std;
    392 
    393 	DPRINTFN(1,("uhci_init: start\n"));
    394 
    395 #ifdef UHCI_DEBUG
    396 	thesc = sc;
    397 
    398 	if (uhcidebug > 2)
    399 		uhci_dumpregs(sc);
    400 #endif
    401 
    402 	uhci_run(sc, 0);			/* stop the controller */
    403 	UWRITE2(sc, UHCI_INTR, 0);		/* disable interrupts */
    404 
    405 	uhci_busreset(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 #endif
    638 	}
    639 #ifdef DIAGNOSTIC
    640 	xfer->busy_free = XFER_BUSY;
    641 #endif
    642 	return (xfer);
    643 }
    644 
    645 void
    646 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
    647 {
    648 	struct uhci_softc *sc = (struct uhci_softc *)bus;
    649 
    650 #ifdef DIAGNOSTIC
    651 	if (xfer->busy_free != XFER_BUSY) {
    652 		printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
    653 		       xfer->busy_free);
    654 		return;
    655 	}
    656 	xfer->busy_free = XFER_FREE;
    657 	if (!UXFER(xfer)->iinfo.isdone) {
    658 		printf("uhci_freex: !isdone\n");
    659 		return;
    660 	}
    661 #endif
    662 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
    663 }
    664 
    665 /*
    666  * Shut down the controller when the system is going down.
    667  */
    668 void
    669 uhci_shutdown(void *v)
    670 {
    671 	uhci_softc_t *sc = v;
    672 
    673 	DPRINTF(("uhci_shutdown: stopping the HC\n"));
    674 	uhci_run(sc, 0); /* stop the controller */
    675 }
    676 
    677 /*
    678  * Handle suspend/resume.
    679  *
    680  * We need to switch to polling mode here, because this routine is
    681  * called from an interrupt context.  This is all right since we
    682  * are almost suspended anyway.
    683  */
    684 void
    685 uhci_power(int why, void *v)
    686 {
    687 	uhci_softc_t *sc = v;
    688 	int cmd;
    689 	int s;
    690 
    691 	s = splusb();
    692 	cmd = UREAD2(sc, UHCI_CMD);
    693 
    694 	DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
    695 		 sc, why, sc->sc_suspend, cmd));
    696 
    697 	switch (why) {
    698 	case PWR_SUSPEND:
    699 	case PWR_STANDBY:
    700 #ifdef UHCI_DEBUG
    701 		if (uhcidebug > 2)
    702 			uhci_dumpregs(sc);
    703 #endif
    704 		if (sc->sc_intr_xfer != NULL)
    705 			usb_uncallout(sc->sc_poll_handle, uhci_poll_hub,
    706 			    sc->sc_intr_xfer);
    707 		sc->sc_bus.use_polling++;
    708 		uhci_run(sc, 0); /* stop the controller */
    709 
    710 		/* save some state if BIOS doesn't */
    711 		sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
    712 		sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
    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         ed = pipe->endpoint->edesc;
    912         dev = pipe->device;
    913 	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",
    914 	       ii, DONE, ii->xfer, dev,
    915 	       UGETW(dev->ddesc.idVendor),
    916 	       UGETW(dev->ddesc.idProduct),
    917 	       dev->address, pipe,
    918 	       ed->bEndpointAddress, ed->bmAttributes);
    919 #undef DONE
    920 }
    921 
    922 void uhci_dump_iis(struct uhci_softc *sc);
    923 void
    924 uhci_dump_iis(struct uhci_softc *sc)
    925 {
    926 	uhci_intr_info_t *ii;
    927 
    928 	printf("intr_info list:\n");
    929 	for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
    930 		uhci_dump_ii(ii);
    931 }
    932 
    933 void iidump(void);
    934 void iidump(void) { uhci_dump_iis(thesc); }
    935 
    936 #endif
    937 
    938 /*
    939  * This routine is executed periodically and simulates interrupts
    940  * from the root controller interrupt pipe for port status change.
    941  */
    942 void
    943 uhci_poll_hub(void *addr)
    944 {
    945 	usbd_xfer_handle xfer = addr;
    946 	usbd_pipe_handle pipe = xfer->pipe;
    947 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
    948 	int s;
    949 	u_char *p;
    950 
    951 	DPRINTFN(20, ("uhci_poll_hub\n"));
    952 
    953 	usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
    954 
    955 	p = KERNADDR(&xfer->dmabuf);
    956 	p[0] = 0;
    957 	if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
    958 		p[0] |= 1<<1;
    959 	if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
    960 		p[0] |= 1<<2;
    961 	if (p[0] == 0)
    962 		/* No change, try again in a while */
    963 		return;
    964 
    965 	xfer->actlen = 1;
    966 	xfer->status = USBD_NORMAL_COMPLETION;
    967 	s = splusb();
    968 	xfer->device->bus->intr_context++;
    969 	usb_transfer_complete(xfer);
    970 	xfer->device->bus->intr_context--;
    971 	splx(s);
    972 }
    973 
    974 void
    975 uhci_root_intr_done(usbd_xfer_handle xfer)
    976 {
    977 }
    978 
    979 void
    980 uhci_root_ctrl_done(usbd_xfer_handle xfer)
    981 {
    982 }
    983 
    984 /*
    985  * Let the last QH loop back to the high speed control transfer QH.
    986  * This is what intel calls "bandwidth reclamation" and improves
    987  * USB performance a lot for some devices.
    988  * If we are already looping, just count it.
    989  */
    990 void
    991 uhci_add_loop(uhci_softc_t *sc) {
    992 #ifdef UHCI_DEBUG
    993 	if (uhcinoloop)
    994 		return;
    995 #endif
    996 	if (++sc->sc_loops == 1) {
    997 		DPRINTFN(5,("uhci_start_loop: add\n"));
    998 		/* Note, we don't loop back the soft pointer. */
    999 		sc->sc_last_qh->qh.qh_hlink =
   1000 		    htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
   1001 	}
   1002 }
   1003 
   1004 void
   1005 uhci_rem_loop(uhci_softc_t *sc) {
   1006 #ifdef UHCI_DEBUG
   1007 	if (uhcinoloop)
   1008 		return;
   1009 #endif
   1010 	if (--sc->sc_loops == 0) {
   1011 		DPRINTFN(5,("uhci_end_loop: remove\n"));
   1012 		sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
   1013 	}
   1014 }
   1015 
   1016 /* Add high speed control QH, called at splusb(). */
   1017 void
   1018 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   1019 {
   1020 	uhci_soft_qh_t *eqh;
   1021 
   1022 	SPLUSBCHECK;
   1023 
   1024 	DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
   1025 	eqh = sc->sc_hctl_end;
   1026 	sqh->hlink       = eqh->hlink;
   1027 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
   1028 	eqh->hlink       = sqh;
   1029 	eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
   1030 	sc->sc_hctl_end = sqh;
   1031 #ifdef UHCI_CTL_LOOP
   1032 	uhci_add_loop(sc);
   1033 #endif
   1034 }
   1035 
   1036 /* Remove high speed control QH, called at splusb(). */
   1037 void
   1038 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   1039 {
   1040 	uhci_soft_qh_t *pqh;
   1041 
   1042 	SPLUSBCHECK;
   1043 
   1044 	DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
   1045 #ifdef UHCI_CTL_LOOP
   1046 	uhci_rem_loop(sc);
   1047 #endif
   1048 	/*
   1049 	 * The T bit should be set in the elink of the QH so that the HC
   1050 	 * doesn't follow the pointer.  This condition may fail if the
   1051 	 * the transferred packet was short so that the QH still points
   1052 	 * at the last used TD.
   1053 	 * In this case we set the T bit and wait a little for the HC
   1054 	 * to stop looking at the TD.
   1055 	 */
   1056 	if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
   1057 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
   1058 		delay(UHCI_QH_REMOVE_DELAY);
   1059 	}
   1060 
   1061 	pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
   1062 	pqh->hlink       = sqh->hlink;
   1063 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
   1064 	delay(UHCI_QH_REMOVE_DELAY);
   1065 	if (sc->sc_hctl_end == sqh)
   1066 		sc->sc_hctl_end = pqh;
   1067 }
   1068 
   1069 /* Add low speed control QH, called at splusb(). */
   1070 void
   1071 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   1072 {
   1073 	uhci_soft_qh_t *eqh;
   1074 
   1075 	SPLUSBCHECK;
   1076 
   1077 	DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
   1078 	eqh = sc->sc_lctl_end;
   1079 	sqh->hlink       = eqh->hlink;
   1080 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
   1081 	eqh->hlink       = sqh;
   1082 	eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
   1083 	sc->sc_lctl_end = sqh;
   1084 }
   1085 
   1086 /* Remove low speed control QH, called at splusb(). */
   1087 void
   1088 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   1089 {
   1090 	uhci_soft_qh_t *pqh;
   1091 
   1092 	SPLUSBCHECK;
   1093 
   1094 	DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
   1095 	/* See comment in uhci_remove_hs_ctrl() */
   1096 	if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
   1097 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
   1098 		delay(UHCI_QH_REMOVE_DELAY);
   1099 	}
   1100 	pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
   1101 	pqh->hlink       = sqh->hlink;
   1102 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
   1103 	delay(UHCI_QH_REMOVE_DELAY);
   1104 	if (sc->sc_lctl_end == sqh)
   1105 		sc->sc_lctl_end = pqh;
   1106 }
   1107 
   1108 /* Add bulk QH, called at splusb(). */
   1109 void
   1110 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   1111 {
   1112 	uhci_soft_qh_t *eqh;
   1113 
   1114 	SPLUSBCHECK;
   1115 
   1116 	DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
   1117 	eqh = sc->sc_bulk_end;
   1118 	sqh->hlink       = eqh->hlink;
   1119 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
   1120 	eqh->hlink       = sqh;
   1121 	eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
   1122 	sc->sc_bulk_end = sqh;
   1123 	uhci_add_loop(sc);
   1124 }
   1125 
   1126 /* Remove bulk QH, called at splusb(). */
   1127 void
   1128 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   1129 {
   1130 	uhci_soft_qh_t *pqh;
   1131 
   1132 	SPLUSBCHECK;
   1133 
   1134 	DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
   1135 	uhci_rem_loop(sc);
   1136 	/* See comment in uhci_remove_hs_ctrl() */
   1137 	if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
   1138 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
   1139 		delay(UHCI_QH_REMOVE_DELAY);
   1140 	}
   1141 	pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
   1142 	pqh->hlink       = sqh->hlink;
   1143 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
   1144 	delay(UHCI_QH_REMOVE_DELAY);
   1145 	if (sc->sc_bulk_end == sqh)
   1146 		sc->sc_bulk_end = pqh;
   1147 }
   1148 
   1149 int
   1150 uhci_intr(void *arg)
   1151 {
   1152 	uhci_softc_t *sc = arg;
   1153 	int status;
   1154 	int ack;
   1155 
   1156 #ifdef UHCI_DEBUG
   1157 	if (uhcidebug > 15) {
   1158 		DPRINTF(("%s: uhci_intr\n", USBDEVNAME(sc->sc_bus.bdev)));
   1159 		uhci_dumpregs(sc);
   1160 	}
   1161 #endif
   1162 
   1163 	status = UREAD2(sc, UHCI_STS);
   1164 	if (status == 0)	/* The interrupt was not for us. */
   1165 		return (0);
   1166 
   1167 	if (sc->sc_suspend != PWR_RESUME) {
   1168 		printf("%s: interrupt while not operating ignored\n",
   1169 		       USBDEVNAME(sc->sc_bus.bdev));
   1170 		return (0);
   1171 	}
   1172 
   1173 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
   1174 	if (sc->sc_suspend != PWR_RESUME)
   1175 		printf("uhci_intr: suspended sts=0x%x\n", status);
   1176 #endif
   1177 
   1178 	ack = 0;
   1179 	if (status & UHCI_STS_USBINT)
   1180 		ack |= UHCI_STS_USBINT;
   1181 	if (status & UHCI_STS_USBEI)
   1182 		ack |= UHCI_STS_USBEI;
   1183 	if (status & UHCI_STS_RD) {
   1184 		ack |= UHCI_STS_RD;
   1185 #ifdef UHCI_DEBUG
   1186 		printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
   1187 #endif
   1188 	}
   1189 	if (status & UHCI_STS_HSE) {
   1190 		ack |= UHCI_STS_HSE;
   1191 		printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
   1192 	}
   1193 	if (status & UHCI_STS_HCPE) {
   1194 		ack |= UHCI_STS_HCPE;
   1195 		printf("%s: host controller process error\n",
   1196 		       USBDEVNAME(sc->sc_bus.bdev));
   1197 	}
   1198 	if (status & UHCI_STS_HCH) {
   1199 		/* no acknowledge needed */
   1200 		if (!sc->sc_dying)
   1201 			printf("%s: host controller halted\n",
   1202 			    USBDEVNAME(sc->sc_bus.bdev));
   1203 		sc->sc_dying = 1;
   1204 #ifdef UHCI_DEBUG
   1205 		uhci_dump_all(sc);
   1206 #endif
   1207 
   1208 	}
   1209 
   1210 	if (ack)	/* acknowledge the ints */
   1211 		UWRITE2(sc, UHCI_STS, ack);
   1212 	else	/* nothing to acknowledge */
   1213 		return (0);
   1214 
   1215 	sc->sc_bus.no_intrs++;
   1216 	usb_schedsoftintr(&sc->sc_bus);
   1217 
   1218 	DPRINTFN(10, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
   1219 
   1220 	return (1);
   1221 }
   1222 
   1223 void
   1224 uhci_softintr(struct usbd_bus *bus)
   1225 {
   1226 	uhci_softc_t *sc = (uhci_softc_t *)bus;
   1227 	uhci_intr_info_t *ii;
   1228 
   1229 	DPRINTFN(10,("%s: uhci_softintr\n", USBDEVNAME(sc->sc_bus.bdev)));
   1230 
   1231 	sc->sc_bus.intr_context++;
   1232 
   1233 	/*
   1234 	 * Interrupts on UHCI really suck.  When the host controller
   1235 	 * interrupts because a transfer is completed there is no
   1236 	 * way of knowing which transfer it was.  You can scan down
   1237 	 * the TDs and QHs of the previous frame to limit the search,
   1238 	 * but that assumes that the interrupt was not delayed by more
   1239 	 * than 1 ms, which may not always be true (e.g. after debug
   1240 	 * output on a slow console).
   1241 	 * We scan all interrupt descriptors to see if any have
   1242 	 * completed.
   1243 	 */
   1244 	for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
   1245 		uhci_check_intr(sc, ii);
   1246 
   1247 	sc->sc_bus.intr_context--;
   1248 }
   1249 
   1250 /* Check for an interrupt. */
   1251 void
   1252 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
   1253 {
   1254 	uhci_soft_td_t *std, *lstd;
   1255 	u_int32_t status;
   1256 
   1257 	DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
   1258 #ifdef DIAGNOSTIC
   1259 	if (ii == NULL) {
   1260 		printf("uhci_check_intr: no ii? %p\n", ii);
   1261 		return;
   1262 	}
   1263 #endif
   1264 	if (ii->stdstart == NULL)
   1265 		return;
   1266 	lstd = ii->stdend;
   1267 #ifdef DIAGNOSTIC
   1268 	if (lstd == NULL) {
   1269 		printf("uhci_check_intr: std==0\n");
   1270 		return;
   1271 	}
   1272 #endif
   1273 	/*
   1274 	 * If the last TD is still active we need to check whether there
   1275 	 * is a an error somewhere in the middle, or whether there was a
   1276 	 * short packet (SPD and not ACTIVE).
   1277 	 */
   1278 	if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
   1279 		DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
   1280 		for (std = ii->stdstart; std != lstd; std = std->link.std) {
   1281 			status = le32toh(std->td.td_status);
   1282 			/* If there's an active TD the xfer isn't done. */
   1283 			if (status & UHCI_TD_ACTIVE)
   1284 				break;
   1285 			/* Any kind of error makes the xfer done. */
   1286 			if (status & UHCI_TD_STALLED)
   1287 				goto done;
   1288 			/* We want short packets, and it is short: it's done */
   1289 			if ((status & UHCI_TD_SPD) &&
   1290 			      UHCI_TD_GET_ACTLEN(status) <
   1291 			      UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token)))
   1292 				goto done;
   1293 		}
   1294 		DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
   1295 			      ii, ii->stdstart));
   1296 		return;
   1297 	}
   1298  done:
   1299 	DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
   1300 	usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
   1301 	uhci_idone(ii);
   1302 }
   1303 
   1304 /* Called at splusb() */
   1305 void
   1306 uhci_idone(uhci_intr_info_t *ii)
   1307 {
   1308 	usbd_xfer_handle xfer = ii->xfer;
   1309 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1310 	uhci_soft_td_t *std;
   1311 	u_int32_t status = 0, nstatus;
   1312 	int actlen;
   1313 
   1314 #ifdef DIAGNOSTIC
   1315 	{
   1316 		int s = splhigh();
   1317 		if (ii->isdone) {
   1318 			splx(s);
   1319 #ifdef UHCI_DEBUG
   1320 			printf("uhci_idone: ii is done!\n   ");
   1321 			uhci_dump_ii(ii);
   1322 #else
   1323 			printf("uhci_idone: ii=%p is done!\n", ii);
   1324 #endif
   1325 			return;
   1326 		}
   1327 		ii->isdone = 1;
   1328 		splx(s);
   1329 	}
   1330 #endif
   1331 
   1332 	if (xfer->status == USBD_CANCELLED ||
   1333 	    xfer->status == USBD_TIMEOUT) {
   1334 		DPRINTF(("uhci_idone: aborted xfer=%p\n", xfer));
   1335 		return;
   1336 	}
   1337 
   1338 	if (xfer->nframes != 0) {
   1339 		/* Isoc transfer, do things differently. */
   1340 		uhci_soft_td_t **stds = upipe->u.iso.stds;
   1341 		int i, n, nframes, len;
   1342 
   1343 		DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
   1344 
   1345 		nframes = xfer->nframes;
   1346 		actlen = 0;
   1347 		n = UXFER(xfer)->curframe;
   1348 		for (i = 0; i < nframes; i++) {
   1349 			std = stds[n];
   1350 #ifdef UHCI_DEBUG
   1351 			if (uhcidebug > 5) {
   1352 				DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
   1353 				uhci_dump_td(std);
   1354 			}
   1355 #endif
   1356 			if (++n >= UHCI_VFRAMELIST_COUNT)
   1357 				n = 0;
   1358 			status = le32toh(std->td.td_status);
   1359 			len = UHCI_TD_GET_ACTLEN(status);
   1360 			xfer->frlengths[i] = len;
   1361 			actlen += len;
   1362 		}
   1363 		upipe->u.iso.inuse -= nframes;
   1364 		xfer->actlen = actlen;
   1365 		xfer->status = USBD_NORMAL_COMPLETION;
   1366 		usb_transfer_complete(xfer);
   1367 		return;
   1368 	}
   1369 
   1370 #ifdef UHCI_DEBUG
   1371 	DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
   1372 		      ii, xfer, upipe));
   1373 	if (uhcidebug > 10)
   1374 		uhci_dump_tds(ii->stdstart);
   1375 #endif
   1376 
   1377 	/* The transfer is done, compute actual length and status. */
   1378 	actlen = 0;
   1379 	for (std = ii->stdstart; std != NULL; std = std->link.std) {
   1380 		nstatus = le32toh(std->td.td_status);
   1381 		if (nstatus & UHCI_TD_ACTIVE)
   1382 			break;
   1383 
   1384 		status = nstatus;
   1385 		if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
   1386 			UHCI_TD_PID_SETUP)
   1387 			actlen += UHCI_TD_GET_ACTLEN(status);
   1388 	}
   1389 	/* If there are left over TDs we need to update the toggle. */
   1390 	if (std != NULL)
   1391 		upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
   1392 
   1393 	status &= UHCI_TD_ERROR;
   1394 	DPRINTFN(10, ("uhci_check_intr: actlen=%d, status=0x%x\n",
   1395 		      actlen, status));
   1396 	xfer->actlen = actlen;
   1397 	if (status != 0) {
   1398 #ifdef UHCI_DEBUG
   1399 		char sbuf[128];
   1400 
   1401 		bitmask_snprintf((int)status, "\20\22BITSTUFF\23CRCTO\24NAK\25"
   1402 				 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
   1403 				 sbuf, sizeof(sbuf));
   1404 
   1405 		DPRINTFN((status == UHCI_TD_STALLED)*10,
   1406 			 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
   1407 			  "status 0x%s\n",
   1408 			  xfer->pipe->device->address,
   1409 			  xfer->pipe->endpoint->edesc->bEndpointAddress,
   1410 			  sbuf));
   1411 #endif
   1412 
   1413 		if (status == UHCI_TD_STALLED)
   1414 			xfer->status = USBD_STALLED;
   1415 		else
   1416 			xfer->status = USBD_IOERROR; /* more info XXX */
   1417 	} else {
   1418 		xfer->status = USBD_NORMAL_COMPLETION;
   1419 	}
   1420 	usb_transfer_complete(xfer);
   1421 }
   1422 
   1423 /*
   1424  * Called when a request does not complete.
   1425  */
   1426 void
   1427 uhci_timeout(void *addr)
   1428 {
   1429 	uhci_intr_info_t *ii = addr;
   1430 
   1431 	DPRINTF(("uhci_timeout: ii=%p\n", ii));
   1432 
   1433 #ifdef UHCI_DEBUG
   1434 	if (uhcidebug > 10)
   1435 		uhci_dump_tds(ii->stdstart);
   1436 #endif
   1437 
   1438 	ii->xfer->device->bus->intr_context++;
   1439 	uhci_abort_xfer(ii->xfer, USBD_TIMEOUT);
   1440 	ii->xfer->device->bus->intr_context--;
   1441 }
   1442 
   1443 /*
   1444  * Wait here until controller claims to have an interrupt.
   1445  * Then call uhci_intr and return.  Use timeout to avoid waiting
   1446  * too long.
   1447  * Only used during boot when interrupts are not enabled yet.
   1448  */
   1449 void
   1450 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
   1451 {
   1452 	int timo = xfer->timeout;
   1453 	uhci_intr_info_t *ii;
   1454 
   1455 	DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
   1456 
   1457 	xfer->status = USBD_IN_PROGRESS;
   1458 	for (; timo >= 0; timo--) {
   1459 		usb_delay_ms(&sc->sc_bus, 1);
   1460 		DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
   1461 		if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
   1462 			uhci_intr(sc);
   1463 			if (xfer->status != USBD_IN_PROGRESS)
   1464 				return;
   1465 		}
   1466 	}
   1467 
   1468 	/* Timeout */
   1469 	DPRINTF(("uhci_waitintr: timeout\n"));
   1470 	for (ii = LIST_FIRST(&sc->sc_intrhead);
   1471 	     ii != NULL && ii->xfer != xfer;
   1472 	     ii = LIST_NEXT(ii, list))
   1473 		;
   1474 #ifdef DIAGNOSTIC
   1475 	if (ii == NULL)
   1476 		panic("uhci_waitintr: lost intr_info\n");
   1477 #endif
   1478 	uhci_idone(ii);
   1479 }
   1480 
   1481 void
   1482 uhci_poll(struct usbd_bus *bus)
   1483 {
   1484 	uhci_softc_t *sc = (uhci_softc_t *)bus;
   1485 
   1486 	if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
   1487 		uhci_intr(sc);
   1488 }
   1489 
   1490 #if 0
   1491 void
   1492 uhci_reset(uhci_softc_t *sc)
   1493 {
   1494 	int n;
   1495 
   1496 	UHCICMD(sc, UHCI_CMD_HCRESET);
   1497 	/* The reset bit goes low when the controller is done. */
   1498 	for (n = 0; n < UHCI_RESET_TIMEOUT &&
   1499 		    (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
   1500 		usb_delay_ms(&sc->sc_bus, 1);
   1501 	if (n >= UHCI_RESET_TIMEOUT)
   1502 		printf("%s: controller did not reset\n",
   1503 		       USBDEVNAME(sc->sc_bus.bdev));
   1504 }
   1505 #endif
   1506 
   1507 usbd_status
   1508 uhci_run(uhci_softc_t *sc, int run)
   1509 {
   1510 	int s, n, running;
   1511 	u_int16_t cmd;
   1512 
   1513 	run = run != 0;
   1514 	s = splusb();
   1515 	DPRINTF(("uhci_run: setting run=%d\n", run));
   1516 	cmd = UREAD2(sc, UHCI_CMD);
   1517 	if (run)
   1518 		cmd |= UHCI_CMD_RS;
   1519 	else
   1520 		cmd &= ~UHCI_CMD_RS;
   1521 	UHCICMD(sc, cmd);
   1522 	for(n = 0; n < 10; n++) {
   1523 		running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
   1524 		/* return when we've entered the state we want */
   1525 		if (run == running) {
   1526 			splx(s);
   1527 			DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
   1528 				 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
   1529 			return (USBD_NORMAL_COMPLETION);
   1530 		}
   1531 		usb_delay_ms(&sc->sc_bus, 1);
   1532 	}
   1533 	splx(s);
   1534 	printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
   1535 	       run ? "start" : "stop");
   1536 	return (USBD_IOERROR);
   1537 }
   1538 
   1539 /*
   1540  * Memory management routines.
   1541  *  uhci_alloc_std allocates TDs
   1542  *  uhci_alloc_sqh allocates QHs
   1543  * These two routines do their own free list management,
   1544  * partly for speed, partly because allocating DMAable memory
   1545  * has page size granularaity so much memory would be wasted if
   1546  * only one TD/QH (32 bytes) was placed in each allocated chunk.
   1547  */
   1548 
   1549 uhci_soft_td_t *
   1550 uhci_alloc_std(uhci_softc_t *sc)
   1551 {
   1552 	uhci_soft_td_t *std;
   1553 	usbd_status err;
   1554 	int i, offs;
   1555 	usb_dma_t dma;
   1556 
   1557 	if (sc->sc_freetds == NULL) {
   1558 		DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
   1559 		err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
   1560 			  UHCI_TD_ALIGN, &dma);
   1561 		if (err)
   1562 			return (0);
   1563 		for(i = 0; i < UHCI_STD_CHUNK; i++) {
   1564 			offs = i * UHCI_STD_SIZE;
   1565 			std = (uhci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
   1566 			std->physaddr = DMAADDR(&dma) + offs;
   1567 			std->link.std = sc->sc_freetds;
   1568 			sc->sc_freetds = std;
   1569 		}
   1570 	}
   1571 	std = sc->sc_freetds;
   1572 	sc->sc_freetds = std->link.std;
   1573 	memset(&std->td, 0, sizeof(uhci_td_t));
   1574 	return std;
   1575 }
   1576 
   1577 void
   1578 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
   1579 {
   1580 #ifdef DIAGNOSTIC
   1581 #define TD_IS_FREE 0x12345678
   1582 	if (le32toh(std->td.td_token) == TD_IS_FREE) {
   1583 		printf("uhci_free_std: freeing free TD %p\n", std);
   1584 		return;
   1585 	}
   1586 	std->td.td_token = htole32(TD_IS_FREE);
   1587 #endif
   1588 	std->link.std = sc->sc_freetds;
   1589 	sc->sc_freetds = std;
   1590 }
   1591 
   1592 uhci_soft_qh_t *
   1593 uhci_alloc_sqh(uhci_softc_t *sc)
   1594 {
   1595 	uhci_soft_qh_t *sqh;
   1596 	usbd_status err;
   1597 	int i, offs;
   1598 	usb_dma_t dma;
   1599 
   1600 	if (sc->sc_freeqhs == NULL) {
   1601 		DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
   1602 		err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
   1603 			  UHCI_QH_ALIGN, &dma);
   1604 		if (err)
   1605 			return (0);
   1606 		for(i = 0; i < UHCI_SQH_CHUNK; i++) {
   1607 			offs = i * UHCI_SQH_SIZE;
   1608 			sqh = (uhci_soft_qh_t *)((char *)KERNADDR(&dma) +offs);
   1609 			sqh->physaddr = DMAADDR(&dma) + offs;
   1610 			sqh->hlink = sc->sc_freeqhs;
   1611 			sc->sc_freeqhs = sqh;
   1612 		}
   1613 	}
   1614 	sqh = sc->sc_freeqhs;
   1615 	sc->sc_freeqhs = sqh->hlink;
   1616 	memset(&sqh->qh, 0, sizeof(uhci_qh_t));
   1617 	return (sqh);
   1618 }
   1619 
   1620 void
   1621 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   1622 {
   1623 	sqh->hlink = sc->sc_freeqhs;
   1624 	sc->sc_freeqhs = sqh;
   1625 }
   1626 
   1627 void
   1628 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
   1629 		    uhci_soft_td_t *stdend)
   1630 {
   1631 	uhci_soft_td_t *p;
   1632 
   1633 	for (; std != stdend; std = p) {
   1634 		p = std->link.std;
   1635 		uhci_free_std(sc, std);
   1636 	}
   1637 }
   1638 
   1639 usbd_status
   1640 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
   1641 		     int rd, u_int16_t flags, usb_dma_t *dma,
   1642 		     uhci_soft_td_t **sp, uhci_soft_td_t **ep)
   1643 {
   1644 	uhci_soft_td_t *p, *lastp;
   1645 	uhci_physaddr_t lastlink;
   1646 	int i, ntd, l, tog, maxp;
   1647 	u_int32_t status;
   1648 	int addr = upipe->pipe.device->address;
   1649 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1650 
   1651 	DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d ls=%d "
   1652 		      "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
   1653 		      upipe->pipe.device->lowspeed, flags));
   1654 	maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
   1655 	if (maxp == 0) {
   1656 		printf("uhci_alloc_std_chain: maxp=0\n");
   1657 		return (USBD_INVAL);
   1658 	}
   1659 	ntd = (len + maxp - 1) / maxp;
   1660 	if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
   1661 		ntd++;
   1662 	DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
   1663 	if (ntd == 0) {
   1664 		*sp = *ep = 0;
   1665 		DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
   1666 		return (USBD_NORMAL_COMPLETION);
   1667 	}
   1668 	tog = upipe->nexttoggle;
   1669 	if (ntd % 2 == 0)
   1670 		tog ^= 1;
   1671 	upipe->nexttoggle = tog ^ 1;
   1672 	lastp = NULL;
   1673 	lastlink = UHCI_PTR_T;
   1674 	ntd--;
   1675 	status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
   1676 	if (upipe->pipe.device->lowspeed)
   1677 		status |= UHCI_TD_LS;
   1678 	if (flags & USBD_SHORT_XFER_OK)
   1679 		status |= UHCI_TD_SPD;
   1680 	for (i = ntd; i >= 0; i--) {
   1681 		p = uhci_alloc_std(sc);
   1682 		if (p == NULL) {
   1683 			uhci_free_std_chain(sc, lastp, 0);
   1684 			return (USBD_NOMEM);
   1685 		}
   1686 		p->link.std = lastp;
   1687 		p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
   1688 		lastp = p;
   1689 		lastlink = p->physaddr;
   1690 		p->td.td_status = htole32(status);
   1691 		if (i == ntd) {
   1692 			/* last TD */
   1693 			l = len % maxp;
   1694 			if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
   1695 				l = maxp;
   1696 			*ep = p;
   1697 		} else
   1698 			l = maxp;
   1699 		p->td.td_token =
   1700 		    htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
   1701 				 UHCI_TD_OUT(l, endpt, addr, tog));
   1702 		p->td.td_buffer = htole32(DMAADDR(dma) + i * maxp);
   1703 		tog ^= 1;
   1704 	}
   1705 	*sp = lastp;
   1706 	DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
   1707 		      upipe->nexttoggle));
   1708 	return (USBD_NORMAL_COMPLETION);
   1709 }
   1710 
   1711 void
   1712 uhci_device_clear_toggle(usbd_pipe_handle pipe)
   1713 {
   1714 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1715 	upipe->nexttoggle = 0;
   1716 }
   1717 
   1718 void
   1719 uhci_noop(usbd_pipe_handle pipe)
   1720 {
   1721 }
   1722 
   1723 usbd_status
   1724 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
   1725 {
   1726 	usbd_status err;
   1727 
   1728 	/* Insert last in queue. */
   1729 	err = usb_insert_transfer(xfer);
   1730 	if (err)
   1731 		return (err);
   1732 
   1733 	/*
   1734 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   1735 	 * so start it first.
   1736 	 */
   1737 	return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1738 }
   1739 
   1740 usbd_status
   1741 uhci_device_bulk_start(usbd_xfer_handle xfer)
   1742 {
   1743 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1744 	usbd_device_handle dev = upipe->pipe.device;
   1745 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1746 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   1747 	uhci_soft_td_t *data, *dataend;
   1748 	uhci_soft_qh_t *sqh;
   1749 	usbd_status err;
   1750 	int len, isread, endpt;
   1751 	int s;
   1752 
   1753 	DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
   1754 		     xfer, xfer->length, xfer->flags));
   1755 
   1756 	if (sc->sc_dying)
   1757 		return (USBD_IOERROR);
   1758 
   1759 #ifdef DIAGNOSTIC
   1760 	if (xfer->rqflags & URQ_REQUEST)
   1761 		panic("uhci_device_bulk_transfer: a request\n");
   1762 #endif
   1763 
   1764 	len = xfer->length;
   1765 	endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1766 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   1767 	sqh = upipe->u.bulk.sqh;
   1768 
   1769 	upipe->u.bulk.isread = isread;
   1770 	upipe->u.bulk.length = len;
   1771 
   1772 	err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
   1773 				   &xfer->dmabuf, &data, &dataend);
   1774 	if (err)
   1775 		return (err);
   1776 	dataend->td.td_status |= htole32(UHCI_TD_IOC);
   1777 
   1778 #ifdef UHCI_DEBUG
   1779 	if (uhcidebug > 8) {
   1780 		DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
   1781 		uhci_dump_tds(data);
   1782 	}
   1783 #endif
   1784 
   1785 	/* Set up interrupt info. */
   1786 	ii->xfer = xfer;
   1787 	ii->stdstart = data;
   1788 	ii->stdend = dataend;
   1789 #ifdef DIAGNOSTIC
   1790 	if (!ii->isdone) {
   1791 		printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
   1792 	}
   1793 	ii->isdone = 0;
   1794 #endif
   1795 
   1796 	sqh->elink = data;
   1797 	sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
   1798 
   1799 	s = splusb();
   1800 	uhci_add_bulk(sc, sqh);
   1801 	uhci_add_intr_info(sc, ii);
   1802 
   1803 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   1804 		usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
   1805 			    uhci_timeout, ii);
   1806 	}
   1807 	xfer->status = USBD_IN_PROGRESS;
   1808 	splx(s);
   1809 
   1810 #ifdef UHCI_DEBUG
   1811 	if (uhcidebug > 10) {
   1812 		DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
   1813 		uhci_dump_tds(data);
   1814 	}
   1815 #endif
   1816 
   1817 	if (sc->sc_bus.use_polling)
   1818 		uhci_waitintr(sc, xfer);
   1819 
   1820 	return (USBD_IN_PROGRESS);
   1821 }
   1822 
   1823 /* Abort a device bulk request. */
   1824 void
   1825 uhci_device_bulk_abort(usbd_xfer_handle xfer)
   1826 {
   1827 	DPRINTF(("uhci_device_bulk_abort:\n"));
   1828 	uhci_abort_xfer(xfer, USBD_CANCELLED);
   1829 }
   1830 
   1831 /*
   1832  * XXX This way of aborting is neither safe, nor good.
   1833  * But it will have to do until I figure out what to do.
   1834  * I apologize for the delay().
   1835  */
   1836 void
   1837 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
   1838 {
   1839 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   1840 	uhci_soft_td_t *std;
   1841 	int s;
   1842 
   1843 	DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
   1844 
   1845 	s = splusb();
   1846 
   1847 	/* Transfer is already done. */
   1848 	if (xfer->status != USBD_NOT_STARTED &&
   1849 	    xfer->status != USBD_IN_PROGRESS) {
   1850 		splx(s);
   1851 		return;
   1852 	}
   1853 
   1854 	/* Make interrupt routine ignore it, */
   1855 	xfer->status = status;
   1856 
   1857 	/* don't timeout, */
   1858 	usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
   1859 
   1860 	/* make hardware ignore it, */
   1861 	for (std = ii->stdstart; std != NULL; std = std->link.std)
   1862 		std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
   1863 
   1864 	xfer->hcpriv = ii;
   1865 
   1866 	splx(s);
   1867 
   1868 	delay(1000);
   1869 
   1870 	s = splusb();
   1871 #ifdef DIAGNOSTIC
   1872 	ii->isdone = 1;
   1873 #endif
   1874 	usb_transfer_complete(xfer);
   1875 	splx(s);
   1876 }
   1877 
   1878 /* Close a device bulk pipe. */
   1879 void
   1880 uhci_device_bulk_close(usbd_pipe_handle pipe)
   1881 {
   1882 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1883 	usbd_device_handle dev = upipe->pipe.device;
   1884 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1885 
   1886 	uhci_free_sqh(sc, upipe->u.bulk.sqh);
   1887 }
   1888 
   1889 usbd_status
   1890 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
   1891 {
   1892 	usbd_status err;
   1893 
   1894 	/* Insert last in queue. */
   1895 	err = usb_insert_transfer(xfer);
   1896 	if (err)
   1897 		return (err);
   1898 
   1899 	/*
   1900 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   1901 	 * so start it first.
   1902 	 */
   1903 	return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1904 }
   1905 
   1906 usbd_status
   1907 uhci_device_ctrl_start(usbd_xfer_handle xfer)
   1908 {
   1909 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   1910 	usbd_status err;
   1911 
   1912 	if (sc->sc_dying)
   1913 		return (USBD_IOERROR);
   1914 
   1915 #ifdef DIAGNOSTIC
   1916 	if (!(xfer->rqflags & URQ_REQUEST))
   1917 		panic("uhci_device_ctrl_transfer: not a request\n");
   1918 #endif
   1919 
   1920 	err = uhci_device_request(xfer);
   1921 	if (err)
   1922 		return (err);
   1923 
   1924 	if (sc->sc_bus.use_polling)
   1925 		uhci_waitintr(sc, xfer);
   1926 	return (USBD_IN_PROGRESS);
   1927 }
   1928 
   1929 usbd_status
   1930 uhci_device_intr_transfer(usbd_xfer_handle xfer)
   1931 {
   1932 	usbd_status err;
   1933 
   1934 	/* Insert last in queue. */
   1935 	err = usb_insert_transfer(xfer);
   1936 	if (err)
   1937 		return (err);
   1938 
   1939 	/*
   1940 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   1941 	 * so start it first.
   1942 	 */
   1943 	return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   1944 }
   1945 
   1946 usbd_status
   1947 uhci_device_intr_start(usbd_xfer_handle xfer)
   1948 {
   1949 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   1950 	usbd_device_handle dev = upipe->pipe.device;
   1951 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1952 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   1953 	uhci_soft_td_t *data, *dataend;
   1954 	uhci_soft_qh_t *sqh;
   1955 	usbd_status err;
   1956 	int i, s;
   1957 
   1958 	if (sc->sc_dying)
   1959 		return (USBD_IOERROR);
   1960 
   1961 	DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
   1962 		    xfer, xfer->length, xfer->flags));
   1963 
   1964 #ifdef DIAGNOSTIC
   1965 	if (xfer->rqflags & URQ_REQUEST)
   1966 		panic("uhci_device_intr_transfer: a request\n");
   1967 #endif
   1968 
   1969 	err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
   1970 				   &xfer->dmabuf, &data, &dataend);
   1971 	if (err)
   1972 		return (err);
   1973 	dataend->td.td_status |= htole32(UHCI_TD_IOC);
   1974 
   1975 #ifdef UHCI_DEBUG
   1976 	if (uhcidebug > 10) {
   1977 		DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
   1978 		uhci_dump_tds(data);
   1979 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   1980 	}
   1981 #endif
   1982 
   1983 	s = splusb();
   1984 	/* Set up interrupt info. */
   1985 	ii->xfer = xfer;
   1986 	ii->stdstart = data;
   1987 	ii->stdend = dataend;
   1988 #ifdef DIAGNOSTIC
   1989 	if (!ii->isdone) {
   1990 		printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
   1991 	}
   1992 	ii->isdone = 0;
   1993 #endif
   1994 
   1995 	DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
   1996 		     upipe->u.intr.qhs[0]));
   1997 	for (i = 0; i < upipe->u.intr.npoll; i++) {
   1998 		sqh = upipe->u.intr.qhs[i];
   1999 		sqh->elink = data;
   2000 		sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
   2001 	}
   2002 	uhci_add_intr_info(sc, ii);
   2003 	xfer->status = USBD_IN_PROGRESS;
   2004 	splx(s);
   2005 
   2006 #ifdef UHCI_DEBUG
   2007 	if (uhcidebug > 10) {
   2008 		DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
   2009 		uhci_dump_tds(data);
   2010 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   2011 	}
   2012 #endif
   2013 
   2014 	return (USBD_IN_PROGRESS);
   2015 }
   2016 
   2017 /* Abort a device control request. */
   2018 void
   2019 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
   2020 {
   2021 	DPRINTF(("uhci_device_ctrl_abort:\n"));
   2022 	uhci_abort_xfer(xfer, USBD_CANCELLED);
   2023 }
   2024 
   2025 /* Close a device control pipe. */
   2026 void
   2027 uhci_device_ctrl_close(usbd_pipe_handle pipe)
   2028 {
   2029 }
   2030 
   2031 /* Abort a device interrupt request. */
   2032 void
   2033 uhci_device_intr_abort(usbd_xfer_handle xfer)
   2034 {
   2035 	DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
   2036 	if (xfer->pipe->intrxfer == xfer) {
   2037 		DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
   2038 		xfer->pipe->intrxfer = 0;
   2039 	}
   2040 	uhci_abort_xfer(xfer, USBD_CANCELLED);
   2041 }
   2042 
   2043 /* Close a device interrupt pipe. */
   2044 void
   2045 uhci_device_intr_close(usbd_pipe_handle pipe)
   2046 {
   2047 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2048 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2049 	int i, npoll;
   2050 	int s;
   2051 
   2052 	/* Unlink descriptors from controller data structures. */
   2053 	npoll = upipe->u.intr.npoll;
   2054 	s = splusb();
   2055 	for (i = 0; i < npoll; i++)
   2056 		uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
   2057 	splx(s);
   2058 
   2059 	/*
   2060 	 * We now have to wait for any activity on the physical
   2061 	 * descriptors to stop.
   2062 	 */
   2063 	usb_delay_ms(&sc->sc_bus, 2);
   2064 
   2065 	for(i = 0; i < npoll; i++)
   2066 		uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
   2067 	free(upipe->u.intr.qhs, M_USBHC);
   2068 
   2069 	/* XXX free other resources */
   2070 }
   2071 
   2072 usbd_status
   2073 uhci_device_request(usbd_xfer_handle xfer)
   2074 {
   2075 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2076 	usb_device_request_t *req = &xfer->request;
   2077 	usbd_device_handle dev = upipe->pipe.device;
   2078 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2079 	int addr = dev->address;
   2080 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   2081 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2082 	uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
   2083 	uhci_soft_qh_t *sqh;
   2084 	int len;
   2085 	u_int32_t ls;
   2086 	usbd_status err;
   2087 	int isread;
   2088 	int s;
   2089 
   2090 	DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
   2091 		    "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
   2092 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
   2093 		    UGETW(req->wIndex), UGETW(req->wLength),
   2094 		    addr, endpt));
   2095 
   2096 	ls = dev->lowspeed ? UHCI_TD_LS : 0;
   2097 	isread = req->bmRequestType & UT_READ;
   2098 	len = UGETW(req->wLength);
   2099 
   2100 	setup = upipe->u.ctl.setup;
   2101 	stat = upipe->u.ctl.stat;
   2102 	sqh = upipe->u.ctl.sqh;
   2103 
   2104 	/* Set up data transaction */
   2105 	if (len != 0) {
   2106 		upipe->nexttoggle = 1;
   2107 		err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
   2108 					   &xfer->dmabuf, &data, &dataend);
   2109 		if (err)
   2110 			return (err);
   2111 		next = data;
   2112 		dataend->link.std = stat;
   2113 		dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
   2114 	} else {
   2115 		next = stat;
   2116 	}
   2117 	upipe->u.ctl.length = len;
   2118 
   2119 	memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
   2120 
   2121 	setup->link.std = next;
   2122 	setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
   2123 	setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
   2124 		UHCI_TD_ACTIVE);
   2125 	setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
   2126 	setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma));
   2127 
   2128 	stat->link.std = NULL;
   2129 	stat->td.td_link = htole32(UHCI_PTR_T);
   2130 	stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
   2131 		UHCI_TD_ACTIVE | UHCI_TD_IOC);
   2132 	stat->td.td_token =
   2133 		htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
   2134 		                 UHCI_TD_IN (0, endpt, addr, 1));
   2135 	stat->td.td_buffer = htole32(0);
   2136 
   2137 #ifdef UHCI_DEBUG
   2138 	if (uhcidebug > 10) {
   2139 		DPRINTF(("uhci_device_request: before transfer\n"));
   2140 		uhci_dump_tds(setup);
   2141 	}
   2142 #endif
   2143 
   2144 	/* Set up interrupt info. */
   2145 	ii->xfer = xfer;
   2146 	ii->stdstart = setup;
   2147 	ii->stdend = stat;
   2148 #ifdef DIAGNOSTIC
   2149 	if (!ii->isdone) {
   2150 		printf("uhci_device_request: not done, ii=%p\n", ii);
   2151 	}
   2152 	ii->isdone = 0;
   2153 #endif
   2154 
   2155 	sqh->elink = setup;
   2156 	sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
   2157 
   2158 	s = splusb();
   2159 	if (dev->lowspeed)
   2160 		uhci_add_ls_ctrl(sc, sqh);
   2161 	else
   2162 		uhci_add_hs_ctrl(sc, sqh);
   2163 	uhci_add_intr_info(sc, ii);
   2164 #ifdef UHCI_DEBUG
   2165 	if (uhcidebug > 12) {
   2166 		uhci_soft_td_t *std;
   2167 		uhci_soft_qh_t *xqh;
   2168 		uhci_soft_qh_t *sxqh;
   2169 		int maxqh = 0;
   2170 		uhci_physaddr_t link;
   2171 		DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
   2172 		for (std = sc->sc_vframes[0].htd, link = 0;
   2173 		     (link & UHCI_PTR_QH) == 0;
   2174 		     std = std->link.std) {
   2175 			link = le32toh(std->td.td_link);
   2176 			uhci_dump_td(std);
   2177 		}
   2178 		sxqh = (uhci_soft_qh_t *)std;
   2179 		uhci_dump_qh(sxqh);
   2180 		for (xqh = sxqh;
   2181 		     xqh != NULL;
   2182 		     xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
   2183                             xqh->hlink == xqh ? NULL : xqh->hlink)) {
   2184 			uhci_dump_qh(xqh);
   2185 		}
   2186 		DPRINTF(("Enqueued QH:\n"));
   2187 		uhci_dump_qh(sqh);
   2188 		uhci_dump_tds(sqh->elink);
   2189 	}
   2190 #endif
   2191 	if (xfer->timeout && !sc->sc_bus.use_polling) {
   2192 		usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
   2193 			    uhci_timeout, ii);
   2194 	}
   2195 	xfer->status = USBD_IN_PROGRESS;
   2196 	splx(s);
   2197 
   2198 	return (USBD_NORMAL_COMPLETION);
   2199 }
   2200 
   2201 usbd_status
   2202 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
   2203 {
   2204 	usbd_status err;
   2205 
   2206 	DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
   2207 
   2208 	/* Put it on our queue, */
   2209 	err = usb_insert_transfer(xfer);
   2210 
   2211 	/* bail out on error, */
   2212 	if (err && err != USBD_IN_PROGRESS)
   2213 		return (err);
   2214 
   2215 	/* XXX should check inuse here */
   2216 
   2217 	/* insert into schedule, */
   2218 	uhci_device_isoc_enter(xfer);
   2219 
   2220 	/* and start if the pipe wasn't running */
   2221 	if (!err)
   2222 		uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
   2223 
   2224 	return (err);
   2225 }
   2226 
   2227 void
   2228 uhci_device_isoc_enter(usbd_xfer_handle xfer)
   2229 {
   2230 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2231 	usbd_device_handle dev = upipe->pipe.device;
   2232 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2233 	struct iso *iso = &upipe->u.iso;
   2234 	uhci_soft_td_t *std;
   2235 	u_int32_t buf, len, status;
   2236 	int s, i, next, nframes;
   2237 
   2238 	DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
   2239 		    "nframes=%d\n",
   2240 		    iso->inuse, iso->next, xfer, xfer->nframes));
   2241 
   2242 	if (sc->sc_dying)
   2243 		return;
   2244 
   2245 	if (xfer->status == USBD_IN_PROGRESS) {
   2246 		/* This request has already been entered into the frame list */
   2247 		printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
   2248 		/* XXX */
   2249 	}
   2250 
   2251 #ifdef DIAGNOSTIC
   2252 	if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
   2253 		printf("uhci_device_isoc_enter: overflow!\n");
   2254 #endif
   2255 
   2256 	next = iso->next;
   2257 	if (next == -1) {
   2258 		/* Not in use yet, schedule it a few frames ahead. */
   2259 		next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
   2260 		DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
   2261 	}
   2262 
   2263 	xfer->status = USBD_IN_PROGRESS;
   2264 	UXFER(xfer)->curframe = next;
   2265 
   2266 	buf = DMAADDR(&xfer->dmabuf);
   2267 	status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
   2268 				     UHCI_TD_ACTIVE |
   2269 				     UHCI_TD_IOS);
   2270 	nframes = xfer->nframes;
   2271 	s = splusb();
   2272 	for (i = 0; i < nframes; i++) {
   2273 		std = iso->stds[next];
   2274 		if (++next >= UHCI_VFRAMELIST_COUNT)
   2275 			next = 0;
   2276 		len = xfer->frlengths[i];
   2277 		std->td.td_buffer = htole32(buf);
   2278 		if (i == nframes - 1)
   2279 			status |= UHCI_TD_IOC;
   2280 		std->td.td_status = htole32(status);
   2281 		std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
   2282 		std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
   2283 #ifdef UHCI_DEBUG
   2284 		if (uhcidebug > 5) {
   2285 			DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
   2286 			uhci_dump_td(std);
   2287 		}
   2288 #endif
   2289 		buf += len;
   2290 	}
   2291 	iso->next = next;
   2292 	iso->inuse += xfer->nframes;
   2293 
   2294 	splx(s);
   2295 }
   2296 
   2297 usbd_status
   2298 uhci_device_isoc_start(usbd_xfer_handle xfer)
   2299 {
   2300 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2301 	uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
   2302 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2303 	uhci_soft_td_t *end;
   2304 	int s, i;
   2305 
   2306 	DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
   2307 
   2308 	if (sc->sc_dying)
   2309 		return (USBD_IOERROR);
   2310 
   2311 #ifdef DIAGNOSTIC
   2312 	if (xfer->status != USBD_IN_PROGRESS)
   2313 		printf("uhci_device_isoc_start: not in progress %p\n", xfer);
   2314 #endif
   2315 
   2316 	/* Find the last TD */
   2317 	i = UXFER(xfer)->curframe + xfer->nframes;
   2318 	if (i >= UHCI_VFRAMELIST_COUNT)
   2319 		i -= UHCI_VFRAMELIST_COUNT;
   2320 	end = upipe->u.iso.stds[i];
   2321 
   2322 #ifdef DIAGNOSTIC
   2323 	if (end == NULL) {
   2324 		printf("uhci_device_isoc_start: end == NULL\n");
   2325 		return (USBD_INVAL);
   2326 	}
   2327 #endif
   2328 
   2329 	s = splusb();
   2330 
   2331 	/* Set up interrupt info. */
   2332 	ii->xfer = xfer;
   2333 	ii->stdstart = end;
   2334 	ii->stdend = end;
   2335 #ifdef DIAGNOSTIC
   2336 	if (!ii->isdone)
   2337 		printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
   2338 	ii->isdone = 0;
   2339 #endif
   2340 	uhci_add_intr_info(sc, ii);
   2341 
   2342 	splx(s);
   2343 
   2344 	return (USBD_IN_PROGRESS);
   2345 }
   2346 
   2347 void
   2348 uhci_device_isoc_abort(usbd_xfer_handle xfer)
   2349 {
   2350 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2351 	uhci_soft_td_t **stds = upipe->u.iso.stds;
   2352 	uhci_soft_td_t *std;
   2353 	int i, n, s, nframes, maxlen, len;
   2354 
   2355 	s = splusb();
   2356 
   2357 	/* Transfer is already done. */
   2358 	if (xfer->status != USBD_NOT_STARTED &&
   2359 	    xfer->status != USBD_IN_PROGRESS) {
   2360 		splx(s);
   2361 		return;
   2362 	}
   2363 
   2364 	/* Give xfer the requested abort code. */
   2365 	xfer->status = USBD_CANCELLED;
   2366 
   2367 	/* make hardware ignore it, */
   2368 	nframes = xfer->nframes;
   2369 	n = UXFER(xfer)->curframe;
   2370 	maxlen = 0;
   2371 	for (i = 0; i < nframes; i++) {
   2372 		std = stds[n];
   2373 		std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
   2374 		len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
   2375 		if (len > maxlen)
   2376 			maxlen = len;
   2377 		if (++n >= UHCI_VFRAMELIST_COUNT)
   2378 			n = 0;
   2379 	}
   2380 
   2381 	/* and wait until we are sure the hardware has finished. */
   2382 	delay(maxlen);
   2383 
   2384 #ifdef DIAGNOSTIC
   2385 	UXFER(xfer)->iinfo.isdone = 1;
   2386 #endif
   2387 	/* Run callback and remove from interrupt list. */
   2388 	usb_transfer_complete(xfer);
   2389 
   2390 	splx(s);
   2391 }
   2392 
   2393 void
   2394 uhci_device_isoc_close(usbd_pipe_handle pipe)
   2395 {
   2396 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2397 	usbd_device_handle dev = upipe->pipe.device;
   2398 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2399 	uhci_soft_td_t *std, *vstd;
   2400 	struct iso *iso;
   2401 	int i, s;
   2402 
   2403 	/*
   2404 	 * Make sure all TDs are marked as inactive.
   2405 	 * Wait for completion.
   2406 	 * Unschedule.
   2407 	 * Deallocate.
   2408 	 */
   2409 	iso = &upipe->u.iso;
   2410 
   2411 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
   2412 		iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
   2413 	usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
   2414 
   2415 	s = splusb();
   2416 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2417 		std = iso->stds[i];
   2418 		for (vstd = sc->sc_vframes[i].htd;
   2419 		     vstd != NULL && vstd->link.std != std;
   2420 		     vstd = vstd->link.std)
   2421 			;
   2422 		if (vstd == NULL) {
   2423 			/*panic*/
   2424 			printf("uhci_device_isoc_close: %p not found\n", std);
   2425 			splx(s);
   2426 			return;
   2427 		}
   2428 		vstd->link = std->link;
   2429 		vstd->td.td_link = std->td.td_link;
   2430 		uhci_free_std(sc, std);
   2431 	}
   2432 	splx(s);
   2433 
   2434 	free(iso->stds, M_USBHC);
   2435 }
   2436 
   2437 usbd_status
   2438 uhci_setup_isoc(usbd_pipe_handle pipe)
   2439 {
   2440 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2441 	usbd_device_handle dev = upipe->pipe.device;
   2442 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   2443 	int addr = upipe->pipe.device->address;
   2444 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   2445 	int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
   2446 	uhci_soft_td_t *std, *vstd;
   2447 	u_int32_t token;
   2448 	struct iso *iso;
   2449 	int i, s;
   2450 
   2451 	iso = &upipe->u.iso;
   2452 	iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
   2453 			   M_USBHC, M_WAITOK);
   2454 
   2455 	token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
   2456 		     UHCI_TD_OUT(0, endpt, addr, 0);
   2457 
   2458 	/* Allocate the TDs and mark as inactive; */
   2459 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2460 		std = uhci_alloc_std(sc);
   2461 		if (std == 0)
   2462 			goto bad;
   2463 		std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
   2464 		std->td.td_token = htole32(token);
   2465 		iso->stds[i] = std;
   2466 	}
   2467 
   2468 	/* Insert TDs into schedule. */
   2469 	s = splusb();
   2470 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   2471 		std = iso->stds[i];
   2472 		vstd = sc->sc_vframes[i].htd;
   2473 		std->link = vstd->link;
   2474 		std->td.td_link = vstd->td.td_link;
   2475 		vstd->link.std = std;
   2476 		vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
   2477 	}
   2478 	splx(s);
   2479 
   2480 	iso->next = -1;
   2481 	iso->inuse = 0;
   2482 
   2483 	return (USBD_NORMAL_COMPLETION);
   2484 
   2485  bad:
   2486 	while (--i >= 0)
   2487 		uhci_free_std(sc, iso->stds[i]);
   2488 	free(iso->stds, M_USBHC);
   2489 	return (USBD_NOMEM);
   2490 }
   2491 
   2492 void
   2493 uhci_device_isoc_done(usbd_xfer_handle xfer)
   2494 {
   2495 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2496 
   2497 	DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
   2498 
   2499 	if (ii->xfer != xfer)
   2500 		/* Not on interrupt list, ignore it. */
   2501 		return;
   2502 
   2503 #ifdef DIAGNOSTIC
   2504 	if (xfer->busy_free != XFER_BUSY) {
   2505 		printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
   2506 		       xfer, xfer->busy_free);
   2507 		return;
   2508 	}
   2509 
   2510         if (ii->stdend == NULL) {
   2511                 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
   2512 #ifdef UHCI_DEBUG
   2513 		uhci_dump_ii(ii);
   2514 #endif
   2515 		return;
   2516 	}
   2517 #endif
   2518 
   2519 	/* Turn off the interrupt since it is active even if the TD is not. */
   2520 	ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
   2521 
   2522 	uhci_del_intr_info(ii);	/* remove from active list */
   2523 }
   2524 
   2525 void
   2526 uhci_device_intr_done(usbd_xfer_handle xfer)
   2527 {
   2528 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2529 	uhci_softc_t *sc = ii->sc;
   2530 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2531 	uhci_soft_qh_t *sqh;
   2532 	int i, npoll;
   2533 
   2534 	DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
   2535 
   2536 	npoll = upipe->u.intr.npoll;
   2537 	for(i = 0; i < npoll; i++) {
   2538 		sqh = upipe->u.intr.qhs[i];
   2539 		sqh->elink = NULL;
   2540 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
   2541 	}
   2542 	uhci_free_std_chain(sc, ii->stdstart, 0);
   2543 
   2544 	/* XXX Wasteful. */
   2545 	if (xfer->pipe->repeat) {
   2546 		uhci_soft_td_t *data, *dataend;
   2547 
   2548 		DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
   2549 
   2550 		/* This alloc cannot fail since we freed the chain above. */
   2551 		uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
   2552 				     &xfer->dmabuf, &data, &dataend);
   2553 		dataend->td.td_status |= htole32(UHCI_TD_IOC);
   2554 
   2555 #ifdef UHCI_DEBUG
   2556 		if (uhcidebug > 10) {
   2557 			DPRINTF(("uhci_device_intr_done: data(1)\n"));
   2558 			uhci_dump_tds(data);
   2559 			uhci_dump_qh(upipe->u.intr.qhs[0]);
   2560 		}
   2561 #endif
   2562 
   2563 		ii->stdstart = data;
   2564 		ii->stdend = dataend;
   2565 #ifdef DIAGNOSTIC
   2566 		if (!ii->isdone) {
   2567 			printf("uhci_device_intr_done: not done, ii=%p\n", ii);
   2568 		}
   2569 		ii->isdone = 0;
   2570 #endif
   2571 		for (i = 0; i < npoll; i++) {
   2572 			sqh = upipe->u.intr.qhs[i];
   2573 			sqh->elink = data;
   2574 			sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
   2575 		}
   2576 		xfer->status = USBD_IN_PROGRESS;
   2577 		/* The ii is already on the examined list, just leave it. */
   2578 	} else {
   2579 		DPRINTFN(5,("uhci_device_intr_done: removing\n"));
   2580 		uhci_del_intr_info(ii);
   2581 	}
   2582 }
   2583 
   2584 /* Deallocate request data structures */
   2585 void
   2586 uhci_device_ctrl_done(usbd_xfer_handle xfer)
   2587 {
   2588 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2589 	uhci_softc_t *sc = ii->sc;
   2590 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2591 
   2592 #ifdef DIAGNOSTIC
   2593 	if (!(xfer->rqflags & URQ_REQUEST))
   2594 		panic("uhci_ctrl_done: not a request\n");
   2595 #endif
   2596 
   2597 	uhci_del_intr_info(ii);	/* remove from active list */
   2598 
   2599 	if (upipe->pipe.device->lowspeed)
   2600 		uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
   2601 	else
   2602 		uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
   2603 
   2604 	if (upipe->u.ctl.length != 0)
   2605 		uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
   2606 
   2607 	DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
   2608 }
   2609 
   2610 /* Deallocate request data structures */
   2611 void
   2612 uhci_device_bulk_done(usbd_xfer_handle xfer)
   2613 {
   2614 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
   2615 	uhci_softc_t *sc = ii->sc;
   2616 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
   2617 
   2618 	uhci_del_intr_info(ii);	/* remove from active list */
   2619 
   2620 	uhci_remove_bulk(sc, upipe->u.bulk.sqh);
   2621 
   2622 	uhci_free_std_chain(sc, ii->stdstart, 0);
   2623 
   2624 	DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
   2625 }
   2626 
   2627 /* Add interrupt QH, called with vflock. */
   2628 void
   2629 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   2630 {
   2631 	struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
   2632 	uhci_soft_qh_t *eqh;
   2633 
   2634 	DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
   2635 
   2636 	eqh = vf->eqh;
   2637 	sqh->hlink       = eqh->hlink;
   2638 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
   2639 	eqh->hlink       = sqh;
   2640 	eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
   2641 	vf->eqh = sqh;
   2642 	vf->bandwidth++;
   2643 }
   2644 
   2645 /* Remove interrupt QH. */
   2646 void
   2647 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
   2648 {
   2649 	struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
   2650 	uhci_soft_qh_t *pqh;
   2651 
   2652 	DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
   2653 
   2654 	/* See comment in uhci_remove_ctrl() */
   2655 	if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
   2656 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
   2657 		delay(UHCI_QH_REMOVE_DELAY);
   2658 	}
   2659 
   2660 	pqh = uhci_find_prev_qh(vf->hqh, sqh);
   2661 	pqh->hlink       = sqh->hlink;
   2662 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
   2663 	delay(UHCI_QH_REMOVE_DELAY);
   2664 	if (vf->eqh == sqh)
   2665 		vf->eqh = pqh;
   2666 	vf->bandwidth--;
   2667 }
   2668 
   2669 usbd_status
   2670 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
   2671 {
   2672 	uhci_soft_qh_t *sqh;
   2673 	int i, npoll, s;
   2674 	u_int bestbw, bw, bestoffs, offs;
   2675 
   2676 	DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
   2677 	if (ival == 0) {
   2678 		printf("uhci_setintr: 0 interval\n");
   2679 		return (USBD_INVAL);
   2680 	}
   2681 
   2682 	if (ival > UHCI_VFRAMELIST_COUNT)
   2683 		ival = UHCI_VFRAMELIST_COUNT;
   2684 	npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
   2685 	DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
   2686 
   2687 	upipe->u.intr.npoll = npoll;
   2688 	upipe->u.intr.qhs =
   2689 		malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
   2690 
   2691 	/*
   2692 	 * Figure out which offset in the schedule that has most
   2693 	 * bandwidth left over.
   2694 	 */
   2695 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
   2696 	for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
   2697 		for (bw = i = 0; i < npoll; i++)
   2698 			bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
   2699 		if (bw < bestbw) {
   2700 			bestbw = bw;
   2701 			bestoffs = offs;
   2702 		}
   2703 	}
   2704 	DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
   2705 
   2706 	for(i = 0; i < npoll; i++) {
   2707 		upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
   2708 		sqh->elink = NULL;
   2709 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
   2710 		sqh->pos = MOD(i * ival + bestoffs);
   2711 	}
   2712 #undef MOD
   2713 
   2714 	s = splusb();
   2715 	/* Enter QHs into the controller data structures. */
   2716 	for(i = 0; i < npoll; i++)
   2717 		uhci_add_intr(sc, upipe->u.intr.qhs[i]);
   2718 	splx(s);
   2719 
   2720 	DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
   2721 	return (USBD_NORMAL_COMPLETION);
   2722 }
   2723 
   2724 /* Open a new pipe. */
   2725 usbd_status
   2726 uhci_open(usbd_pipe_handle pipe)
   2727 {
   2728 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2729 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2730 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
   2731 	usbd_status err;
   2732 	int ival;
   2733 
   2734 	DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
   2735 		     pipe, pipe->device->address,
   2736 		     ed->bEndpointAddress, sc->sc_addr));
   2737 
   2738 	upipe->aborting = 0;
   2739 	upipe->nexttoggle = 0;
   2740 
   2741 	if (pipe->device->address == sc->sc_addr) {
   2742 		switch (ed->bEndpointAddress) {
   2743 		case USB_CONTROL_ENDPOINT:
   2744 			pipe->methods = &uhci_root_ctrl_methods;
   2745 			break;
   2746 		case UE_DIR_IN | UHCI_INTR_ENDPT:
   2747 			pipe->methods = &uhci_root_intr_methods;
   2748 			break;
   2749 		default:
   2750 			return (USBD_INVAL);
   2751 		}
   2752 	} else {
   2753 		switch (ed->bmAttributes & UE_XFERTYPE) {
   2754 		case UE_CONTROL:
   2755 			pipe->methods = &uhci_device_ctrl_methods;
   2756 			upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
   2757 			if (upipe->u.ctl.sqh == NULL)
   2758 				goto bad;
   2759 			upipe->u.ctl.setup = uhci_alloc_std(sc);
   2760 			if (upipe->u.ctl.setup == NULL) {
   2761 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2762 				goto bad;
   2763 			}
   2764 			upipe->u.ctl.stat = uhci_alloc_std(sc);
   2765 			if (upipe->u.ctl.stat == NULL) {
   2766 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2767 				uhci_free_std(sc, upipe->u.ctl.setup);
   2768 				goto bad;
   2769 			}
   2770 			err = usb_allocmem(&sc->sc_bus,
   2771 				  sizeof(usb_device_request_t),
   2772 				  0, &upipe->u.ctl.reqdma);
   2773 			if (err) {
   2774 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2775 				uhci_free_std(sc, upipe->u.ctl.setup);
   2776 				uhci_free_std(sc, upipe->u.ctl.stat);
   2777 				goto bad;
   2778 			}
   2779 			break;
   2780 		case UE_INTERRUPT:
   2781 			pipe->methods = &uhci_device_intr_methods;
   2782 			ival = pipe->interval;
   2783 			if (ival == USBD_DEFAULT_INTERVAL)
   2784 				ival = ed->bInterval;
   2785 			return (uhci_device_setintr(sc, upipe, ival));
   2786 		case UE_ISOCHRONOUS:
   2787 			pipe->methods = &uhci_device_isoc_methods;
   2788 			return (uhci_setup_isoc(pipe));
   2789 		case UE_BULK:
   2790 			pipe->methods = &uhci_device_bulk_methods;
   2791 			upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
   2792 			if (upipe->u.bulk.sqh == NULL)
   2793 				goto bad;
   2794 			break;
   2795 		}
   2796 	}
   2797 	return (USBD_NORMAL_COMPLETION);
   2798 
   2799  bad:
   2800 	return (USBD_NOMEM);
   2801 }
   2802 
   2803 /*
   2804  * Data structures and routines to emulate the root hub.
   2805  */
   2806 usb_device_descriptor_t uhci_devd = {
   2807 	USB_DEVICE_DESCRIPTOR_SIZE,
   2808 	UDESC_DEVICE,		/* type */
   2809 	{0x00, 0x01},		/* USB version */
   2810 	UDCLASS_HUB,		/* class */
   2811 	UDSUBCLASS_HUB,		/* subclass */
   2812 	0,			/* protocol */
   2813 	64,			/* max packet */
   2814 	{0},{0},{0x00,0x01},	/* device id */
   2815 	1,2,0,			/* string indicies */
   2816 	1			/* # of configurations */
   2817 };
   2818 
   2819 usb_config_descriptor_t uhci_confd = {
   2820 	USB_CONFIG_DESCRIPTOR_SIZE,
   2821 	UDESC_CONFIG,
   2822 	{USB_CONFIG_DESCRIPTOR_SIZE +
   2823 	 USB_INTERFACE_DESCRIPTOR_SIZE +
   2824 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
   2825 	1,
   2826 	1,
   2827 	0,
   2828 	UC_SELF_POWERED,
   2829 	0			/* max power */
   2830 };
   2831 
   2832 usb_interface_descriptor_t uhci_ifcd = {
   2833 	USB_INTERFACE_DESCRIPTOR_SIZE,
   2834 	UDESC_INTERFACE,
   2835 	0,
   2836 	0,
   2837 	1,
   2838 	UICLASS_HUB,
   2839 	UISUBCLASS_HUB,
   2840 	0,
   2841 	0
   2842 };
   2843 
   2844 usb_endpoint_descriptor_t uhci_endpd = {
   2845 	USB_ENDPOINT_DESCRIPTOR_SIZE,
   2846 	UDESC_ENDPOINT,
   2847 	UE_DIR_IN | UHCI_INTR_ENDPT,
   2848 	UE_INTERRUPT,
   2849 	{8},
   2850 	255
   2851 };
   2852 
   2853 usb_hub_descriptor_t uhci_hubd_piix = {
   2854 	USB_HUB_DESCRIPTOR_SIZE,
   2855 	UDESC_HUB,
   2856 	2,
   2857 	{ UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
   2858 	50,			/* power on to power good */
   2859 	0,
   2860 	{ 0x00 },		/* both ports are removable */
   2861 };
   2862 
   2863 int
   2864 uhci_str(usb_string_descriptor_t *p, int l, char *s)
   2865 {
   2866 	int i;
   2867 
   2868 	if (l == 0)
   2869 		return (0);
   2870 	p->bLength = 2 * strlen(s) + 2;
   2871 	if (l == 1)
   2872 		return (1);
   2873 	p->bDescriptorType = UDESC_STRING;
   2874 	l -= 2;
   2875 	for (i = 0; s[i] && l > 1; i++, l -= 2)
   2876 		USETW2(p->bString[i], 0, s[i]);
   2877 	return (2*i+2);
   2878 }
   2879 
   2880 /*
   2881  * Simulate a hardware hub by handling all the necessary requests.
   2882  */
   2883 usbd_status
   2884 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
   2885 {
   2886 	usbd_status err;
   2887 
   2888 	/* Insert last in queue. */
   2889 	err = usb_insert_transfer(xfer);
   2890 	if (err)
   2891 		return (err);
   2892 
   2893 	/*
   2894 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   2895 	 * so start it first.
   2896 	 */
   2897 	return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   2898 }
   2899 
   2900 usbd_status
   2901 uhci_root_ctrl_start(usbd_xfer_handle xfer)
   2902 {
   2903 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   2904 	usb_device_request_t *req;
   2905 	void *buf = NULL;
   2906 	int port, x;
   2907 	int s, len, value, index, status, change, l, totlen = 0;
   2908 	usb_port_status_t ps;
   2909 	usbd_status err;
   2910 
   2911 	if (sc->sc_dying)
   2912 		return (USBD_IOERROR);
   2913 
   2914 #ifdef DIAGNOSTIC
   2915 	if (!(xfer->rqflags & URQ_REQUEST))
   2916 		panic("uhci_root_ctrl_transfer: not a request\n");
   2917 #endif
   2918 	req = &xfer->request;
   2919 
   2920 	DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
   2921 		    req->bmRequestType, req->bRequest));
   2922 
   2923 	len = UGETW(req->wLength);
   2924 	value = UGETW(req->wValue);
   2925 	index = UGETW(req->wIndex);
   2926 
   2927 	if (len != 0)
   2928 		buf = KERNADDR(&xfer->dmabuf);
   2929 
   2930 #define C(x,y) ((x) | ((y) << 8))
   2931 	switch(C(req->bRequest, req->bmRequestType)) {
   2932 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
   2933 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
   2934 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
   2935 		/*
   2936 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
   2937 		 * for the integrated root hub.
   2938 		 */
   2939 		break;
   2940 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
   2941 		if (len > 0) {
   2942 			*(u_int8_t *)buf = sc->sc_conf;
   2943 			totlen = 1;
   2944 		}
   2945 		break;
   2946 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   2947 		DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
   2948 		switch(value >> 8) {
   2949 		case UDESC_DEVICE:
   2950 			if ((value & 0xff) != 0) {
   2951 				err = USBD_IOERROR;
   2952 				goto ret;
   2953 			}
   2954 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   2955 			USETW(uhci_devd.idVendor, sc->sc_id_vendor);
   2956 			memcpy(buf, &uhci_devd, l);
   2957 			break;
   2958 		case UDESC_CONFIG:
   2959 			if ((value & 0xff) != 0) {
   2960 				err = USBD_IOERROR;
   2961 				goto ret;
   2962 			}
   2963 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
   2964 			memcpy(buf, &uhci_confd, l);
   2965 			buf = (char *)buf + l;
   2966 			len -= l;
   2967 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
   2968 			totlen += l;
   2969 			memcpy(buf, &uhci_ifcd, l);
   2970 			buf = (char *)buf + l;
   2971 			len -= l;
   2972 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
   2973 			totlen += l;
   2974 			memcpy(buf, &uhci_endpd, l);
   2975 			break;
   2976 		case UDESC_STRING:
   2977 			if (len == 0)
   2978 				break;
   2979 			*(u_int8_t *)buf = 0;
   2980 			totlen = 1;
   2981 			switch (value & 0xff) {
   2982 			case 1: /* Vendor */
   2983 				totlen = uhci_str(buf, len, sc->sc_vendor);
   2984 				break;
   2985 			case 2: /* Product */
   2986 				totlen = uhci_str(buf, len, "UHCI root hub");
   2987 				break;
   2988 			}
   2989 			break;
   2990 		default:
   2991 			err = USBD_IOERROR;
   2992 			goto ret;
   2993 		}
   2994 		break;
   2995 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   2996 		if (len > 0) {
   2997 			*(u_int8_t *)buf = 0;
   2998 			totlen = 1;
   2999 		}
   3000 		break;
   3001 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   3002 		if (len > 1) {
   3003 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   3004 			totlen = 2;
   3005 		}
   3006 		break;
   3007 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   3008 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   3009 		if (len > 1) {
   3010 			USETW(((usb_status_t *)buf)->wStatus, 0);
   3011 			totlen = 2;
   3012 		}
   3013 		break;
   3014 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   3015 		if (value >= USB_MAX_DEVICES) {
   3016 			err = USBD_IOERROR;
   3017 			goto ret;
   3018 		}
   3019 		sc->sc_addr = value;
   3020 		break;
   3021 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   3022 		if (value != 0 && value != 1) {
   3023 			err = USBD_IOERROR;
   3024 			goto ret;
   3025 		}
   3026 		sc->sc_conf = value;
   3027 		break;
   3028 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   3029 		break;
   3030 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   3031 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   3032 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   3033 		err = USBD_IOERROR;
   3034 		goto ret;
   3035 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   3036 		break;
   3037 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   3038 		break;
   3039 	/* Hub requests */
   3040 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   3041 		break;
   3042 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   3043 		DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
   3044 			     "port=%d feature=%d\n",
   3045 			     index, value));
   3046 		if (index == 1)
   3047 			port = UHCI_PORTSC1;
   3048 		else if (index == 2)
   3049 			port = UHCI_PORTSC2;
   3050 		else {
   3051 			err = USBD_IOERROR;
   3052 			goto ret;
   3053 		}
   3054 		switch(value) {
   3055 		case UHF_PORT_ENABLE:
   3056 			x = UREAD2(sc, port);
   3057 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
   3058 			break;
   3059 		case UHF_PORT_SUSPEND:
   3060 			x = UREAD2(sc, port);
   3061 			UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
   3062 			break;
   3063 		case UHF_PORT_RESET:
   3064 			x = UREAD2(sc, port);
   3065 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   3066 			break;
   3067 		case UHF_C_PORT_CONNECTION:
   3068 			x = UREAD2(sc, port);
   3069 			UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
   3070 			break;
   3071 		case UHF_C_PORT_ENABLE:
   3072 			x = UREAD2(sc, port);
   3073 			UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
   3074 			break;
   3075 		case UHF_C_PORT_OVER_CURRENT:
   3076 			x = UREAD2(sc, port);
   3077 			UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
   3078 			break;
   3079 		case UHF_C_PORT_RESET:
   3080 			sc->sc_isreset = 0;
   3081 			err = USBD_NORMAL_COMPLETION;
   3082 			goto ret;
   3083 		case UHF_PORT_CONNECTION:
   3084 		case UHF_PORT_OVER_CURRENT:
   3085 		case UHF_PORT_POWER:
   3086 		case UHF_PORT_LOW_SPEED:
   3087 		case UHF_C_PORT_SUSPEND:
   3088 		default:
   3089 			err = USBD_IOERROR;
   3090 			goto ret;
   3091 		}
   3092 		break;
   3093 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
   3094 		if (index == 1)
   3095 			port = UHCI_PORTSC1;
   3096 		else if (index == 2)
   3097 			port = UHCI_PORTSC2;
   3098 		else {
   3099 			err = USBD_IOERROR;
   3100 			goto ret;
   3101 		}
   3102 		if (len > 0) {
   3103 			*(u_int8_t *)buf =
   3104 				(UREAD2(sc, port) & UHCI_PORTSC_LS) >>
   3105 				UHCI_PORTSC_LS_SHIFT;
   3106 			totlen = 1;
   3107 		}
   3108 		break;
   3109 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   3110 		if (value != 0) {
   3111 			err = USBD_IOERROR;
   3112 			goto ret;
   3113 		}
   3114 		l = min(len, USB_HUB_DESCRIPTOR_SIZE);
   3115 		totlen = l;
   3116 		memcpy(buf, &uhci_hubd_piix, l);
   3117 		break;
   3118 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   3119 		if (len != 4) {
   3120 			err = USBD_IOERROR;
   3121 			goto ret;
   3122 		}
   3123 		memset(buf, 0, len);
   3124 		totlen = len;
   3125 		break;
   3126 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   3127 		if (index == 1)
   3128 			port = UHCI_PORTSC1;
   3129 		else if (index == 2)
   3130 			port = UHCI_PORTSC2;
   3131 		else {
   3132 			err = USBD_IOERROR;
   3133 			goto ret;
   3134 		}
   3135 		if (len != 4) {
   3136 			err = USBD_IOERROR;
   3137 			goto ret;
   3138 		}
   3139 		x = UREAD2(sc, port);
   3140 		status = change = 0;
   3141 		if (x & UHCI_PORTSC_CCS  )
   3142 			status |= UPS_CURRENT_CONNECT_STATUS;
   3143 		if (x & UHCI_PORTSC_CSC  )
   3144 			change |= UPS_C_CONNECT_STATUS;
   3145 		if (x & UHCI_PORTSC_PE   )
   3146 			status |= UPS_PORT_ENABLED;
   3147 		if (x & UHCI_PORTSC_POEDC)
   3148 			change |= UPS_C_PORT_ENABLED;
   3149 		if (x & UHCI_PORTSC_OCI  )
   3150 			status |= UPS_OVERCURRENT_INDICATOR;
   3151 		if (x & UHCI_PORTSC_OCIC )
   3152 			change |= UPS_C_OVERCURRENT_INDICATOR;
   3153 		if (x & UHCI_PORTSC_SUSP )
   3154 			status |= UPS_SUSPEND;
   3155 		if (x & UHCI_PORTSC_LSDA )
   3156 			status |= UPS_LOW_SPEED;
   3157 		status |= UPS_PORT_POWER;
   3158 		if (sc->sc_isreset)
   3159 			change |= UPS_C_PORT_RESET;
   3160 		USETW(ps.wPortStatus, status);
   3161 		USETW(ps.wPortChange, change);
   3162 		l = min(len, sizeof ps);
   3163 		memcpy(buf, &ps, l);
   3164 		totlen = l;
   3165 		break;
   3166 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   3167 		err = USBD_IOERROR;
   3168 		goto ret;
   3169 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   3170 		break;
   3171 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   3172 		if (index == 1)
   3173 			port = UHCI_PORTSC1;
   3174 		else if (index == 2)
   3175 			port = UHCI_PORTSC2;
   3176 		else {
   3177 			err = USBD_IOERROR;
   3178 			goto ret;
   3179 		}
   3180 		switch(value) {
   3181 		case UHF_PORT_ENABLE:
   3182 			x = UREAD2(sc, port);
   3183 			UWRITE2(sc, port, x | UHCI_PORTSC_PE);
   3184 			break;
   3185 		case UHF_PORT_SUSPEND:
   3186 			x = UREAD2(sc, port);
   3187 			UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
   3188 			break;
   3189 		case UHF_PORT_RESET:
   3190 			x = UREAD2(sc, port);
   3191 			UWRITE2(sc, port, x | UHCI_PORTSC_PR);
   3192 			usb_delay_ms(&sc->sc_bus, 50); /*XXX USB v1.1 7.1.7.3 */
   3193 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   3194 			delay(100);
   3195 			x = UREAD2(sc, port);
   3196 			UWRITE2(sc, port, x  | UHCI_PORTSC_PE);
   3197 			usb_delay_ms(&sc->sc_bus, 10); /* XXX */
   3198 			DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
   3199 				    index, UREAD2(sc, port)));
   3200 			sc->sc_isreset = 1;
   3201 			break;
   3202 		case UHF_PORT_POWER:
   3203 			/* Pretend we turned on power */
   3204 			err = USBD_NORMAL_COMPLETION;
   3205 			goto ret;
   3206 		case UHF_C_PORT_CONNECTION:
   3207 		case UHF_C_PORT_ENABLE:
   3208 		case UHF_C_PORT_OVER_CURRENT:
   3209 		case UHF_PORT_CONNECTION:
   3210 		case UHF_PORT_OVER_CURRENT:
   3211 		case UHF_PORT_LOW_SPEED:
   3212 		case UHF_C_PORT_SUSPEND:
   3213 		case UHF_C_PORT_RESET:
   3214 		default:
   3215 			err = USBD_IOERROR;
   3216 			goto ret;
   3217 		}
   3218 		break;
   3219 	default:
   3220 		err = USBD_IOERROR;
   3221 		goto ret;
   3222 	}
   3223 	xfer->actlen = totlen;
   3224 	err = USBD_NORMAL_COMPLETION;
   3225  ret:
   3226 	xfer->status = err;
   3227 	s = splusb();
   3228 	usb_transfer_complete(xfer);
   3229 	splx(s);
   3230 	return (USBD_IN_PROGRESS);
   3231 }
   3232 
   3233 /* Abort a root control request. */
   3234 void
   3235 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
   3236 {
   3237 	/* Nothing to do, all transfers are synchronous. */
   3238 }
   3239 
   3240 /* Close the root pipe. */
   3241 void
   3242 uhci_root_ctrl_close(usbd_pipe_handle pipe)
   3243 {
   3244 	DPRINTF(("uhci_root_ctrl_close\n"));
   3245 }
   3246 
   3247 /* Abort a root interrupt request. */
   3248 void
   3249 uhci_root_intr_abort(usbd_xfer_handle xfer)
   3250 {
   3251 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
   3252 
   3253 	usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
   3254 	sc->sc_intr_xfer = NULL;
   3255 
   3256 	if (xfer->pipe->intrxfer == xfer) {
   3257 		DPRINTF(("uhci_root_intr_abort: remove\n"));
   3258 		xfer->pipe->intrxfer = 0;
   3259 	}
   3260 	xfer->status = USBD_CANCELLED;
   3261 #ifdef DIAGNOSTIC
   3262 	UXFER(xfer)->iinfo.isdone = 1;
   3263 #endif
   3264 	usb_transfer_complete(xfer);
   3265 }
   3266 
   3267 usbd_status
   3268 uhci_root_intr_transfer(usbd_xfer_handle xfer)
   3269 {
   3270 	usbd_status err;
   3271 
   3272 	/* Insert last in queue. */
   3273 	err = usb_insert_transfer(xfer);
   3274 	if (err)
   3275 		return (err);
   3276 
   3277 	/* Pipe isn't running (otherwise err would be USBD_INPROG),
   3278 	 * start first
   3279 	 */
   3280 	return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
   3281 }
   3282 
   3283 /* Start a transfer on the root interrupt pipe */
   3284 usbd_status
   3285 uhci_root_intr_start(usbd_xfer_handle xfer)
   3286 {
   3287 	usbd_pipe_handle pipe = xfer->pipe;
   3288 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   3289 
   3290 	DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
   3291 		     xfer, xfer->length, xfer->flags));
   3292 
   3293 	if (sc->sc_dying)
   3294 		return (USBD_IOERROR);
   3295 
   3296 	sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
   3297 	usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
   3298 	sc->sc_intr_xfer = xfer;
   3299 	return (USBD_IN_PROGRESS);
   3300 }
   3301 
   3302 /* Close the root interrupt pipe. */
   3303 void
   3304 uhci_root_intr_close(usbd_pipe_handle pipe)
   3305 {
   3306 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   3307 
   3308 	usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
   3309 	sc->sc_intr_xfer = NULL;
   3310 	DPRINTF(("uhci_root_intr_close\n"));
   3311 }
   3312