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