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