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