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