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