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