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