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