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