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