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