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