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