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