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