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