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