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