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