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