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