Home | History | Annotate | Line # | Download | only in usb
uhci.c revision 1.46
      1 /*	$NetBSD: uhci.c,v 1.46 1999/09/05 19:32:18 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(arg)
    752 	void *arg;
    753 {
    754 	uhci_softc_t *sc = arg;
    755 	int status;
    756 	int ack;
    757 	uhci_intr_info_t *ii;
    758 
    759 	sc->sc_intrs++;
    760 
    761 #if defined(USB_DEBUG)
    762 	if (uhcidebug > 15) {
    763 		DPRINTF(("%s: uhci_intr\n", USBDEVNAME(sc->sc_bus.bdev)));
    764 		uhci_dumpregs(sc);
    765 	}
    766 #endif
    767 
    768 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
    769 	if (sc->sc_suspend != PWR_RESUME)
    770 		printf("uhci_intr: suspended sts=0x%x\n", status);
    771 #endif
    772 
    773 	status = UREAD2(sc, UHCI_STS);
    774 	ack = 0;
    775 	if (status & UHCI_STS_USBINT)
    776 		ack |= UHCI_STS_USBINT;
    777 	if (status & UHCI_STS_USBEI)
    778 		ack |= UHCI_STS_USBEI;
    779 	if (status & UHCI_STS_RD) {
    780 		ack |= UHCI_STS_RD;
    781 		printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
    782 	}
    783 	if (status & UHCI_STS_HSE) {
    784 		ack |= UHCI_STS_HSE;
    785 		printf("%s: host controller process error\n",
    786 		       USBDEVNAME(sc->sc_bus.bdev));
    787 	}
    788 	if (status & UHCI_STS_HCPE) {
    789 		ack |= UHCI_STS_HCPE;
    790 		printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
    791 	}
    792 	if (status & UHCI_STS_HCH) {
    793 		/* no acknowledge needed */
    794 		printf("%s: host controller halted\n",
    795 		       USBDEVNAME(sc->sc_bus.bdev));
    796 	}
    797 
    798 	if (ack)	/* acknowledge the ints */
    799 		UWRITE2(sc, UHCI_STS, ack);
    800 	else	/* nothing to acknowledge */
    801 		return (0);
    802 
    803 	/*
    804 	 * Interrupts on UHCI really suck.  When the host controller
    805 	 * interrupts because a transfer is completed there is no
    806 	 * way of knowing which transfer it was.  You can scan down
    807 	 * the TDs and QHs of the previous frame to limit the search,
    808 	 * but that assumes that the interrupt was not delayed by more
    809 	 * than 1 ms, which may not always be true (e.g. after debug
    810 	 * output on a slow console).
    811 	 * We scan all interrupt descriptors to see if any have
    812 	 * completed.
    813 	 */
    814 	for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
    815 		uhci_check_intr(sc, ii);
    816 
    817 	DPRINTFN(10, ("uhci_intr: exit\n"));
    818 
    819 	return (1);
    820 }
    821 
    822 /* Check for an interrupt. */
    823 void
    824 uhci_check_intr(sc, ii)
    825 	uhci_softc_t *sc;
    826 	uhci_intr_info_t *ii;
    827 {
    828 	uhci_soft_td_t *std, *lstd;
    829 	u_int32_t status;
    830 
    831 	DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
    832 #ifdef DIAGNOSTIC
    833 	if (!ii) {
    834 		printf("uhci_check_intr: no ii? %p\n", ii);
    835 		return;
    836 	}
    837 #endif
    838 	if (!ii->stdstart)
    839 		return;
    840 	lstd = ii->stdend;
    841 #ifdef DIAGNOSTIC
    842 	if (!lstd) {
    843 		printf("uhci_check_intr: std==0\n");
    844 		return;
    845 	}
    846 #endif
    847 	/*
    848 	 * If the last TD is still active we need to check whether there
    849 	 * is a an error somewhere in the middle, or whether there was a
    850 	 * short packet (SPD and not ACTIVE).
    851 	 */
    852 	if (LE(lstd->td.td_status) & UHCI_TD_ACTIVE) {
    853 		DPRINTFN(15, ("uhci_check_intr: active ii=%p\n", ii));
    854 		for (std = ii->stdstart; std != lstd; std = std->link.std){
    855 			status = LE(std->td.td_status);
    856 			if ((status & UHCI_TD_STALLED) ||
    857 			     (status & (UHCI_TD_SPD | UHCI_TD_ACTIVE)) ==
    858 			     UHCI_TD_SPD)
    859 				goto done;
    860 		}
    861 		DPRINTFN(15, ("uhci_check_intr: ii=%p std=%p still active\n",
    862 			      ii, ii->stdstart));
    863 		return;
    864 	}
    865  done:
    866 	usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
    867 	uhci_idone(ii);
    868 }
    869 
    870 void
    871 uhci_idone(ii)
    872 	uhci_intr_info_t *ii;
    873 {
    874 	usbd_request_handle reqh = ii->reqh;
    875 	struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
    876 	uhci_soft_td_t *std;
    877 	u_int32_t status;
    878 	int actlen;
    879 
    880 #ifdef USB_DEBUG
    881 	DPRINTFN(10, ("uhci_idone: ii=%p ready\n", ii));
    882 	if (uhcidebug > 10)
    883 		uhci_dump_tds(ii->stdstart);
    884 #endif
    885 
    886 	if (reqh->status == USBD_CANCELLED ||
    887 	    reqh->status == USBD_TIMEOUT) {
    888 		DPRINTF(("uhci_idone: aborted reqh=%p\n", reqh));
    889 		return;
    890 	}
    891 
    892 #ifdef DIAGNOSTIC
    893 	{
    894 		int s = splhigh();
    895 		if (ii->isdone) {
    896 			splx(s);
    897 			printf("uhci_idone: ii=%p is done!\n", ii);
    898 			return;
    899 		}
    900 		ii->isdone = 1;
    901 		splx(s);
    902 	}
    903 #endif
    904 
    905 	/* The transfer is done, compute actual length and status. */
    906 	/* XXX Is this correct for control xfers? */
    907 	actlen = 0;
    908 	for (std = ii->stdstart; std; std = std->link.std) {
    909 		status = LE(std->td.td_status);
    910 		if (status & UHCI_TD_ACTIVE)
    911 			break;
    912 		if (UHCI_TD_GET_PID(LE(std->td.td_token)) !=
    913 		    UHCI_TD_PID_SETUP)
    914 			actlen += UHCI_TD_GET_ACTLEN(status);
    915 	}
    916 	/* If there are left over TDs we need to update the toggle. */
    917 	if (std)
    918 		upipe->nexttoggle = UHCI_TD_GET_DT(LE(std->td.td_token));
    919 
    920 	status &= UHCI_TD_ERROR;
    921 	DPRINTFN(10, ("uhci_check_intr: actlen=%d, status=0x%x\n",
    922 		      actlen, status));
    923 	reqh->actlen = actlen;
    924 	if (status != 0) {
    925 		DPRINTFN(-1+((status&UHCI_TD_STALLED)!=0),
    926 			 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
    927 			  "status 0x%b\n",
    928 			  reqh->pipe->device->address,
    929 			  reqh->pipe->endpoint->edesc->bEndpointAddress,
    930 			  (int)status,
    931 			  "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
    932 			  "STALLED\30ACTIVE"));
    933 		if (status == UHCI_TD_STALLED)
    934 			reqh->status = USBD_STALLED;
    935 		else
    936 			reqh->status = USBD_IOERROR; /* more info XXX */
    937 	} else {
    938 		reqh->status = USBD_NORMAL_COMPLETION;
    939 	}
    940 	reqh->hcpriv = ii;
    941 	usb_transfer_complete(reqh);
    942 }
    943 
    944 /*
    945  * Called when a request does not complete.
    946  */
    947 void
    948 uhci_timeout(addr)
    949 	void *addr;
    950 {
    951 	uhci_intr_info_t *ii = addr;
    952 
    953 	DPRINTF(("uhci_timeout: ii=%p\n", ii));
    954 	uhci_abort_req(ii->reqh, USBD_TIMEOUT);
    955 }
    956 
    957 /*
    958  * Wait here until controller claims to have an interrupt.
    959  * Then call uhci_intr and return.  Use timeout to avoid waiting
    960  * too long.
    961  * Only used during boot when interrupts are not enabled yet.
    962  */
    963 void
    964 uhci_waitintr(sc, reqh)
    965 	uhci_softc_t *sc;
    966 	usbd_request_handle reqh;
    967 {
    968 	int timo = reqh->timeout;
    969 	uhci_intr_info_t *ii;
    970 
    971 	DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
    972 
    973 	reqh->status = USBD_IN_PROGRESS;
    974 	for (; timo >= 0; timo--) {
    975 		usb_delay_ms(&sc->sc_bus, 1);
    976 		DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
    977 		if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
    978 			uhci_intr(sc);
    979 			if (reqh->status != USBD_IN_PROGRESS)
    980 				return;
    981 		}
    982 	}
    983 
    984 	/* Timeout */
    985 	DPRINTF(("uhci_waitintr: timeout\n"));
    986 	for (ii = LIST_FIRST(&sc->sc_intrhead);
    987 	     ii && ii->reqh != reqh;
    988 	     ii = LIST_NEXT(ii, list))
    989 		;
    990 #ifdef DIAGNOSTIC
    991 	if (!ii)
    992 		panic("uhci_waitintr: lost intr_info\n");
    993 #endif
    994 	uhci_idone(ii);
    995 }
    996 
    997 void
    998 uhci_poll(bus)
    999 	struct usbd_bus *bus;
   1000 {
   1001 	uhci_softc_t *sc = (uhci_softc_t *)bus;
   1002 
   1003 	if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
   1004 		uhci_intr(sc);
   1005 }
   1006 
   1007 #if 0
   1008 void
   1009 uhci_reset(p)
   1010 	void *p;
   1011 {
   1012 	uhci_softc_t *sc = p;
   1013 	int n;
   1014 
   1015 	UHCICMD(sc, UHCI_CMD_HCRESET);
   1016 	/* The reset bit goes low when the controller is done. */
   1017 	for (n = 0; n < UHCI_RESET_TIMEOUT &&
   1018 		    (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
   1019 		delay(100);
   1020 	if (n >= UHCI_RESET_TIMEOUT)
   1021 		printf("%s: controller did not reset\n",
   1022 		       USBDEVNAME(sc->sc_bus.bdev));
   1023 }
   1024 #endif
   1025 
   1026 usbd_status
   1027 uhci_run(sc, run)
   1028 	uhci_softc_t *sc;
   1029 	int run;
   1030 {
   1031 	int s, n, running;
   1032 
   1033 	run = run != 0;
   1034 	s = splusb();
   1035 	DPRINTF(("uhci_run: setting run=%d\n", run));
   1036 	UHCICMD(sc, run ? UHCI_CMD_RS : 0);
   1037 	for(n = 0; n < 10; n++) {
   1038 		running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
   1039 		/* return when we've entered the state we want */
   1040 		if (run == running) {
   1041 			splx(s);
   1042 			DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
   1043 				 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
   1044 			return (USBD_NORMAL_COMPLETION);
   1045 		}
   1046 		usb_delay_ms(&sc->sc_bus, 1);
   1047 	}
   1048 	splx(s);
   1049 	printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
   1050 	       run ? "start" : "stop");
   1051 	return (USBD_IOERROR);
   1052 }
   1053 
   1054 /*
   1055  * Memory management routines.
   1056  *  uhci_alloc_std allocates TDs
   1057  *  uhci_alloc_sqh allocates QHs
   1058  * These two routines do their own free list management,
   1059  * partly for speed, partly because allocating DMAable memory
   1060  * has page size granularaity so much memory would be wasted if
   1061  * only one TD/QH (32 bytes) was placed in each allocated chunk.
   1062  */
   1063 
   1064 uhci_soft_td_t *
   1065 uhci_alloc_std(sc)
   1066 	uhci_softc_t *sc;
   1067 {
   1068 	uhci_soft_td_t *std;
   1069 	usbd_status r;
   1070 	int i, offs;
   1071 	usb_dma_t dma;
   1072 
   1073 	if (!sc->sc_freetds) {
   1074 		DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
   1075 		r = usb_allocmem(sc->sc_dmatag, UHCI_STD_SIZE * UHCI_STD_CHUNK,
   1076 				 UHCI_TD_ALIGN, &dma);
   1077 		if (r != USBD_NORMAL_COMPLETION)
   1078 			return (0);
   1079 		for(i = 0; i < UHCI_STD_CHUNK; i++) {
   1080 			offs = i * UHCI_STD_SIZE;
   1081 			std = (uhci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
   1082 			std->physaddr = DMAADDR(&dma) + offs;
   1083 			std->link.std = sc->sc_freetds;
   1084 			sc->sc_freetds = std;
   1085 		}
   1086 	}
   1087 	std = sc->sc_freetds;
   1088 	sc->sc_freetds = std->link.std;
   1089 	memset(&std->td, 0, sizeof(uhci_td_t));
   1090 	return std;
   1091 }
   1092 
   1093 void
   1094 uhci_free_std(sc, std)
   1095 	uhci_softc_t *sc;
   1096 	uhci_soft_td_t *std;
   1097 {
   1098 #ifdef DIAGNOSTIC
   1099 #define TD_IS_FREE 0x12345678
   1100 	if (LE(std->td.td_token) == TD_IS_FREE) {
   1101 		printf("uhci_free_std: freeing free TD %p\n", std);
   1102 		return;
   1103 	}
   1104 	std->td.td_token = LE(TD_IS_FREE);
   1105 #endif
   1106 	std->link.std = sc->sc_freetds;
   1107 	sc->sc_freetds = std;
   1108 }
   1109 
   1110 uhci_soft_qh_t *
   1111 uhci_alloc_sqh(sc)
   1112 	uhci_softc_t *sc;
   1113 {
   1114 	uhci_soft_qh_t *sqh;
   1115 	usbd_status r;
   1116 	int i, offs;
   1117 	usb_dma_t dma;
   1118 
   1119 	if (!sc->sc_freeqhs) {
   1120 		DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
   1121 		r = usb_allocmem(sc->sc_dmatag, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
   1122 				 UHCI_QH_ALIGN, &dma);
   1123 		if (r != USBD_NORMAL_COMPLETION)
   1124 			return 0;
   1125 		for(i = 0; i < UHCI_SQH_CHUNK; i++) {
   1126 			offs = i * UHCI_SQH_SIZE;
   1127 			sqh = (uhci_soft_qh_t *)((char *)KERNADDR(&dma) +offs);
   1128 			sqh->physaddr = DMAADDR(&dma) + offs;
   1129 			sqh->hlink = sc->sc_freeqhs;
   1130 			sc->sc_freeqhs = sqh;
   1131 		}
   1132 	}
   1133 	sqh = sc->sc_freeqhs;
   1134 	sc->sc_freeqhs = sqh->hlink;
   1135 	memset(&sqh->qh, 0, sizeof(uhci_qh_t));
   1136 	return (sqh);
   1137 }
   1138 
   1139 void
   1140 uhci_free_sqh(sc, sqh)
   1141 	uhci_softc_t *sc;
   1142 	uhci_soft_qh_t *sqh;
   1143 {
   1144 	sqh->hlink = sc->sc_freeqhs;
   1145 	sc->sc_freeqhs = sqh;
   1146 }
   1147 
   1148 #if 0
   1149 /*
   1150  * Enter a list of transfers onto a control queue.
   1151  * Called at splusb()
   1152  */
   1153 void
   1154 uhci_enter_ctl_q(sc, sqh, ii)
   1155 	uhci_softc_t *sc;
   1156 	uhci_soft_qh_t *sqh;
   1157 	uhci_intr_info_t *ii;
   1158 {
   1159 	DPRINTFN(5, ("uhci_enter_ctl_q: sqh=%p\n", sqh));
   1160 
   1161 }
   1162 #endif
   1163 
   1164 void
   1165 uhci_free_std_chain(sc, std, stdend)
   1166 	uhci_softc_t *sc;
   1167 	uhci_soft_td_t *std;
   1168 	uhci_soft_td_t *stdend;
   1169 {
   1170 	uhci_soft_td_t *p;
   1171 
   1172 	for (; std != stdend; std = p) {
   1173 		p = std->link.std;
   1174 		uhci_free_std(sc, std);
   1175 	}
   1176 }
   1177 
   1178 usbd_status
   1179 uhci_alloc_std_chain(upipe, sc, len, rd, shortok, dma, sp, ep)
   1180 	struct uhci_pipe *upipe;
   1181 	uhci_softc_t *sc;
   1182 	int len, rd, shortok;
   1183 	usb_dma_t *dma;
   1184 	uhci_soft_td_t **sp, **ep;
   1185 {
   1186 	uhci_soft_td_t *p, *lastp;
   1187 	uhci_physaddr_t lastlink;
   1188 	int i, ntd, l, tog, maxp;
   1189 	u_int32_t status;
   1190 	int addr = upipe->pipe.device->address;
   1191 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1192 
   1193 	DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d ls=%d "
   1194 		      "shortok=%d\n", addr, UE_GET_ADDR(endpt), len,
   1195 		      upipe->pipe.device->lowspeed, shortok));
   1196 	if (len == 0) {
   1197 		*sp = *ep = 0;
   1198 		DPRINTFN(-1,("uhci_alloc_std_chain: len=0\n"));
   1199 		return (USBD_NORMAL_COMPLETION);
   1200 	}
   1201 	maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
   1202 	if (maxp == 0) {
   1203 		printf("uhci_alloc_std_chain: maxp=0\n");
   1204 		return (USBD_INVAL);
   1205 	}
   1206 	ntd = (len + maxp - 1) / maxp;
   1207 	DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
   1208 	tog = upipe->nexttoggle;
   1209 	if (ntd % 2 == 0)
   1210 		tog ^= 1;
   1211 	upipe->nexttoggle = tog ^ 1;
   1212 	lastp = 0;
   1213 	lastlink = UHCI_PTR_T;
   1214 	ntd--;
   1215 	status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
   1216 	if (upipe->pipe.device->lowspeed)
   1217 		status |= UHCI_TD_LS;
   1218 	if (shortok)
   1219 		status |= UHCI_TD_SPD;
   1220 	for (i = ntd; i >= 0; i--) {
   1221 		p = uhci_alloc_std(sc);
   1222 		if (!p) {
   1223 			uhci_free_std_chain(sc, lastp, 0);
   1224 			return (USBD_NOMEM);
   1225 		}
   1226 		p->link.std = lastp;
   1227 		p->td.td_link = LE(lastlink);
   1228 		lastp = p;
   1229 		lastlink = p->physaddr;
   1230 		p->td.td_status = LE(status);
   1231 		if (i == ntd) {
   1232 			/* last TD */
   1233 			l = len % maxp;
   1234 			if (l == 0) l = maxp;
   1235 			*ep = p;
   1236 		} else
   1237 			l = maxp;
   1238 		p->td.td_token =
   1239 		    LE(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
   1240 			    UHCI_TD_OUT(l, endpt, addr, tog));
   1241 		p->td.td_buffer = LE(DMAADDR(dma) + i * maxp);
   1242 		tog ^= 1;
   1243 	}
   1244 	*sp = lastp;
   1245 	DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
   1246 		      upipe->nexttoggle));
   1247 	return (USBD_NORMAL_COMPLETION);
   1248 }
   1249 
   1250 void
   1251 uhci_device_clear_toggle(pipe)
   1252 	usbd_pipe_handle pipe;
   1253 {
   1254 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1255 	upipe->nexttoggle = 0;
   1256 }
   1257 
   1258 void
   1259 uhci_noop(pipe)
   1260 	usbd_pipe_handle pipe;
   1261 {
   1262 }
   1263 
   1264 usbd_status
   1265 uhci_device_bulk_transfer(reqh)
   1266 	usbd_request_handle reqh;
   1267 {
   1268 	int s;
   1269 	usbd_status r;
   1270 
   1271 	s = splusb();
   1272 	r = usb_insert_transfer(reqh);
   1273 	splx(s);
   1274 	if (r != USBD_NORMAL_COMPLETION)
   1275 		return (r);
   1276 	else
   1277 		return (uhci_device_bulk_start(reqh));
   1278 }
   1279 
   1280 usbd_status
   1281 uhci_device_bulk_start(reqh)
   1282 	usbd_request_handle reqh;
   1283 {
   1284 	struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
   1285 	usbd_device_handle dev = upipe->pipe.device;
   1286 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1287 	uhci_intr_info_t *ii = upipe->iinfo;
   1288 	uhci_soft_td_t *xfer, *xferend;
   1289 	uhci_soft_qh_t *sqh;
   1290 	usb_dma_t *dmap;
   1291 	usbd_status r;
   1292 	int len, isread, endpt;
   1293 	int s;
   1294 
   1295 	DPRINTFN(3, ("uhci_device_bulk_transfer: reqh=%p buf=%p len=%d "
   1296 		     "flags=%d\n",
   1297 		     reqh, reqh->buffer, reqh->length, reqh->flags));
   1298 
   1299 	if (reqh->isreq)
   1300 		panic("uhci_device_bulk_transfer: a request\n");
   1301 
   1302 	len = reqh->length;
   1303 	dmap = &upipe->u.bulk.datadma;
   1304 	endpt = reqh->pipe->endpoint->edesc->bEndpointAddress;
   1305 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   1306 	sqh = upipe->u.bulk.sqh;
   1307 
   1308 	upipe->u.bulk.isread = isread;
   1309 	upipe->u.bulk.length = len;
   1310 
   1311 	r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
   1312 	if (r != USBD_NORMAL_COMPLETION)
   1313 		goto ret1;
   1314 	r = uhci_alloc_std_chain(upipe, sc, len, isread,
   1315 				 reqh->flags & USBD_SHORT_XFER_OK,
   1316 				 dmap, &xfer, &xferend);
   1317 	if (r != USBD_NORMAL_COMPLETION)
   1318 		goto ret2;
   1319 	xferend->td.td_status |= LE(UHCI_TD_IOC);
   1320 
   1321 	if (!isread && len != 0)
   1322 		memcpy(KERNADDR(dmap), reqh->buffer, len);
   1323 
   1324 #ifdef USB_DEBUG
   1325 	if (uhcidebug > 8) {
   1326 		printf("uhci_device_bulk_transfer: xfer(1)\n");
   1327 		uhci_dump_tds(xfer);
   1328 	}
   1329 #endif
   1330 
   1331 	/* Set up interrupt info. */
   1332 	ii->reqh = reqh;
   1333 	ii->stdstart = xfer;
   1334 	ii->stdend = xferend;
   1335 #ifdef DIAGNOSTIC
   1336 	ii->isdone = 0;
   1337 #endif
   1338 
   1339 	sqh->elink = xfer;
   1340 	sqh->qh.qh_elink = LE(xfer->physaddr);
   1341 	sqh->intr_info = ii;
   1342 
   1343 	s = splusb();
   1344 	uhci_add_bulk(sc, sqh);
   1345 	LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
   1346 
   1347 	if (reqh->timeout && !sc->sc_bus.use_polling) {
   1348 		usb_timeout(uhci_timeout, ii,
   1349                             MS_TO_TICKS(reqh->timeout), ii->timeout_handle);
   1350 	}
   1351 	splx(s);
   1352 
   1353 #ifdef USB_DEBUG
   1354 	if (uhcidebug > 10) {
   1355 		printf("uhci_device_bulk_transfer: xfer(2)\n");
   1356 		uhci_dump_tds(xfer);
   1357 	}
   1358 #endif
   1359 
   1360 	if (sc->sc_bus.use_polling)
   1361 		uhci_waitintr(sc, reqh);
   1362 
   1363 	return (USBD_IN_PROGRESS);
   1364 
   1365  ret2:
   1366 	if (len != 0)
   1367 		usb_freemem(sc->sc_dmatag, dmap);
   1368  ret1:
   1369 	return (r);
   1370 }
   1371 
   1372 /* Abort a device bulk request. */
   1373 void
   1374 uhci_device_bulk_abort(reqh)
   1375 	usbd_request_handle reqh;
   1376 {
   1377 	DPRINTF(("uhci_device_bulk_abort:\n"));
   1378 	uhci_abort_req(reqh, USBD_CANCELLED);
   1379 }
   1380 
   1381 void
   1382 uhci_abort_req(reqh, status)
   1383 	usbd_request_handle reqh;
   1384 	usbd_status status;
   1385 {
   1386 	struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
   1387 	uhci_intr_info_t *ii = upipe->iinfo;
   1388 	uhci_soft_td_t *std;
   1389 
   1390 	/* Make interrupt routine ignore it, */
   1391 	reqh->status = status;
   1392 
   1393 	/* don't timeout, */
   1394 	usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
   1395 
   1396 	/* make hardware ignore it, */
   1397 	for (std = ii->stdstart; std != 0; std = std->link.std)
   1398 		std->td.td_status &= LE(~UHCI_TD_ACTIVE);
   1399 
   1400 	reqh->hcpriv = ii;
   1401 
   1402 	/* make sure hardware has completed, */
   1403 	if (curproc) {
   1404 		usb_delay_ms(reqh->pipe->device->bus, 1);
   1405 		/* and call final part of interrupt handler. */
   1406 		uhci_abort_req_end(reqh);
   1407 	} else {
   1408 		/* We have no process context, so we can't use tsleep(). */
   1409 		timeout(uhci_abort_req_end, reqh, hz / USB_FRAMES_PER_SECOND);
   1410 	}
   1411 }
   1412 
   1413 void
   1414 uhci_abort_req_end(v)
   1415 	void *v;
   1416 {
   1417 	usbd_request_handle reqh = v;
   1418 	int s;
   1419 
   1420 	s = splusb();
   1421 	usb_transfer_complete(reqh);
   1422 	splx(s);
   1423 }
   1424 
   1425 /* Close a device bulk pipe. */
   1426 void
   1427 uhci_device_bulk_close(pipe)
   1428 	usbd_pipe_handle pipe;
   1429 {
   1430 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1431 	usbd_device_handle dev = upipe->pipe.device;
   1432 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1433 
   1434 	uhci_free_sqh(sc, upipe->u.bulk.sqh);
   1435 	uhci_free_intr_info(upipe->iinfo);
   1436 	/* XXX free other resources */
   1437 }
   1438 
   1439 usbd_status
   1440 uhci_device_ctrl_transfer(reqh)
   1441 	usbd_request_handle reqh;
   1442 {
   1443 	int s;
   1444 	usbd_status r;
   1445 
   1446 	s = splusb();
   1447 	r = usb_insert_transfer(reqh);
   1448 	splx(s);
   1449 	if (r != USBD_NORMAL_COMPLETION)
   1450 		return (r);
   1451 	else
   1452 		return (uhci_device_ctrl_start(reqh));
   1453 }
   1454 
   1455 usbd_status
   1456 uhci_device_ctrl_start(reqh)
   1457 	usbd_request_handle reqh;
   1458 {
   1459 	uhci_softc_t *sc = (uhci_softc_t *)reqh->pipe->device->bus;
   1460 	usbd_status r;
   1461 
   1462 	if (!reqh->isreq)
   1463 		panic("uhci_device_ctrl_transfer: not a request\n");
   1464 
   1465 	r = uhci_device_request(reqh);
   1466 	if (r != USBD_NORMAL_COMPLETION)
   1467 		return (r);
   1468 
   1469 	if (sc->sc_bus.use_polling)
   1470 		uhci_waitintr(sc, reqh);
   1471 	return (USBD_IN_PROGRESS);
   1472 }
   1473 
   1474 usbd_status
   1475 uhci_device_intr_transfer(reqh)
   1476 	usbd_request_handle reqh;
   1477 {
   1478 	int s;
   1479 	usbd_status r;
   1480 
   1481 	s = splusb();
   1482 	r = usb_insert_transfer(reqh);
   1483 	splx(s);
   1484 	if (r != USBD_NORMAL_COMPLETION)
   1485 		return (r);
   1486 	else
   1487 		return (uhci_device_intr_start(reqh));
   1488 }
   1489 
   1490 usbd_status
   1491 uhci_device_intr_start(reqh)
   1492 	usbd_request_handle reqh;
   1493 {
   1494 	struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
   1495 	usbd_device_handle dev = upipe->pipe.device;
   1496 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1497 	uhci_intr_info_t *ii = upipe->iinfo;
   1498 	uhci_soft_td_t *xfer, *xferend;
   1499 	uhci_soft_qh_t *sqh;
   1500 	usb_dma_t *dmap;
   1501 	usbd_status r;
   1502 	int len, i;
   1503 	int s;
   1504 
   1505 	DPRINTFN(3, ("uhci_device_intr_transfer: reqh=%p buf=%p len=%d "
   1506 		     "flags=%d\n",
   1507 		     reqh, reqh->buffer, reqh->length, reqh->flags));
   1508 
   1509 	if (reqh->isreq)
   1510 		panic("uhci_device_intr_transfer: a request\n");
   1511 
   1512 	len = reqh->length;
   1513 	dmap = &upipe->u.intr.datadma;
   1514 	if (len == 0)
   1515 		return (USBD_INVAL); /* XXX should it be? */
   1516 
   1517 	r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
   1518 	if (r != USBD_NORMAL_COMPLETION)
   1519 		goto ret1;
   1520 	r = uhci_alloc_std_chain(upipe, sc, len, 1,
   1521  				 reqh->flags & USBD_SHORT_XFER_OK,
   1522 				 dmap, &xfer, &xferend);
   1523 	if (r != USBD_NORMAL_COMPLETION)
   1524 		goto ret2;
   1525 	xferend->td.td_status |= LE(UHCI_TD_IOC);
   1526 
   1527 #ifdef USB_DEBUG
   1528 	if (uhcidebug > 10) {
   1529 		printf("uhci_device_intr_transfer: xfer(1)\n");
   1530 		uhci_dump_tds(xfer);
   1531 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   1532 	}
   1533 #endif
   1534 
   1535 	s = splusb();
   1536 	/* Set up interrupt info. */
   1537 	ii->reqh = reqh;
   1538 	ii->stdstart = xfer;
   1539 	ii->stdend = xferend;
   1540 #ifdef DIAGNOSTIC
   1541 	ii->isdone = 0;
   1542 #endif
   1543 
   1544 	DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
   1545 		     upipe->u.intr.qhs[0]));
   1546 	for (i = 0; i < upipe->u.intr.npoll; i++) {
   1547 		sqh = upipe->u.intr.qhs[i];
   1548 		sqh->elink = xfer;
   1549 		sqh->qh.qh_elink = LE(xfer->physaddr);
   1550 	}
   1551 	splx(s);
   1552 
   1553 #ifdef USB_DEBUG
   1554 	if (uhcidebug > 10) {
   1555 		printf("uhci_device_intr_transfer: xfer(2)\n");
   1556 		uhci_dump_tds(xfer);
   1557 		uhci_dump_qh(upipe->u.intr.qhs[0]);
   1558 	}
   1559 #endif
   1560 
   1561 	return (USBD_IN_PROGRESS);
   1562 
   1563  ret2:
   1564 	if (len != 0)
   1565 		usb_freemem(sc->sc_dmatag, dmap);
   1566  ret1:
   1567 	return (r);
   1568 }
   1569 
   1570 /* Abort a device control request. */
   1571 void
   1572 uhci_device_ctrl_abort(reqh)
   1573 	usbd_request_handle reqh;
   1574 {
   1575 	DPRINTF(("uhci_device_ctrl_abort:\n"));
   1576 	uhci_abort_req(reqh, USBD_CANCELLED);
   1577 }
   1578 
   1579 /* Close a device control pipe. */
   1580 void
   1581 uhci_device_ctrl_close(pipe)
   1582 	usbd_pipe_handle pipe;
   1583 {
   1584 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1585 
   1586 	uhci_free_intr_info(upipe->iinfo);
   1587 	/* XXX free other resources */
   1588 }
   1589 
   1590 /* Abort a device interrupt request. */
   1591 void
   1592 uhci_device_intr_abort(reqh)
   1593 	usbd_request_handle reqh;
   1594 {
   1595 	DPRINTFN(1,("uhci_device_intr_abort: reqh=%p\n", reqh));
   1596 	if (reqh->pipe->intrreqh == reqh) {
   1597 		DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
   1598 		reqh->pipe->intrreqh = 0;
   1599 	}
   1600 	uhci_abort_req(reqh, USBD_CANCELLED);
   1601 }
   1602 
   1603 /* Close a device interrupt pipe. */
   1604 void
   1605 uhci_device_intr_close(pipe)
   1606 	usbd_pipe_handle pipe;
   1607 {
   1608 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1609 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   1610 	int i, s, npoll;
   1611 
   1612 	upipe->iinfo->stdstart = 0;		/* inactive */
   1613 
   1614 	/* Unlink descriptors from controller data structures. */
   1615 	npoll = upipe->u.intr.npoll;
   1616 	uhci_lock_frames(sc);
   1617 	for (i = 0; i < npoll; i++)
   1618 		uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos,
   1619 				 upipe->u.intr.qhs[i]);
   1620 	uhci_unlock_frames(sc);
   1621 
   1622 	/*
   1623 	 * We now have to wait for any activity on the physical
   1624 	 * descriptors to stop.
   1625 	 */
   1626 	usb_delay_ms(&sc->sc_bus, 2);
   1627 
   1628 	for(i = 0; i < npoll; i++)
   1629 		uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
   1630 	free(upipe->u.intr.qhs, M_USBHC);
   1631 
   1632 	s = splusb();
   1633 	LIST_REMOVE(upipe->iinfo, list);	/* remove from active list */
   1634 	splx(s);
   1635 	uhci_free_intr_info(upipe->iinfo);
   1636 
   1637 	/* XXX free other resources */
   1638 }
   1639 
   1640 usbd_status
   1641 uhci_device_request(reqh)
   1642 	usbd_request_handle reqh;
   1643 {
   1644 	struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
   1645 	usb_device_request_t *req = &reqh->request;
   1646 	usbd_device_handle dev = upipe->pipe.device;
   1647 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1648 	int addr = dev->address;
   1649 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1650 	uhci_intr_info_t *ii = upipe->iinfo;
   1651 	uhci_soft_td_t *setup, *xfer, *stat, *next, *xferend;
   1652 	uhci_soft_qh_t *sqh;
   1653 	usb_dma_t *dmap;
   1654 	int len;
   1655 	u_int32_t ls;
   1656 	usbd_status r;
   1657 	int isread;
   1658 	int s;
   1659 
   1660 	DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
   1661 		    "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
   1662 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
   1663 		    UGETW(req->wIndex), UGETW(req->wLength),
   1664 		    addr, endpt));
   1665 
   1666 	ls = dev->lowspeed ? UHCI_TD_LS : 0;
   1667 	isread = req->bmRequestType & UT_READ;
   1668 	len = UGETW(req->wLength);
   1669 
   1670 	setup = upipe->u.ctl.setup;
   1671 	stat = upipe->u.ctl.stat;
   1672 	sqh = upipe->u.ctl.sqh;
   1673 	dmap = &upipe->u.ctl.datadma;
   1674 
   1675 	/* Set up data transaction */
   1676 	if (len != 0) {
   1677 		r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
   1678 		if (r != USBD_NORMAL_COMPLETION)
   1679 			goto ret1;
   1680 		upipe->nexttoggle = 1;
   1681 		r = uhci_alloc_std_chain(upipe, sc, len, isread,
   1682 					 reqh->flags & USBD_SHORT_XFER_OK,
   1683 					 dmap, &xfer, &xferend);
   1684 		if (r != USBD_NORMAL_COMPLETION)
   1685 			goto ret2;
   1686 		next = xfer;
   1687 		xferend->link.std = stat;
   1688 		xferend->td.td_link = LE(stat->physaddr);
   1689 	} else {
   1690 		next = stat;
   1691 	}
   1692 	upipe->u.ctl.length = len;
   1693 
   1694 	memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
   1695 	if (!isread && len != 0)
   1696 		memcpy(KERNADDR(dmap), reqh->buffer, len);
   1697 
   1698 	setup->link.std = next;
   1699 	setup->td.td_link = LE(next->physaddr);
   1700 	setup->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls | UHCI_TD_ACTIVE);
   1701 	setup->td.td_token = LE(UHCI_TD_SETUP(sizeof *req, endpt, addr));
   1702 	setup->td.td_buffer = LE(DMAADDR(&upipe->u.ctl.reqdma));
   1703 
   1704 	stat->link.std = 0;
   1705 	stat->td.td_link = LE(UHCI_PTR_T);
   1706 	stat->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls |
   1707 		UHCI_TD_ACTIVE | UHCI_TD_IOC);
   1708 	stat->td.td_token =
   1709 		LE(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
   1710 		            UHCI_TD_IN (0, endpt, addr, 1));
   1711 	stat->td.td_buffer = LE(0);
   1712 
   1713 #ifdef USB_DEBUG
   1714 	if (uhcidebug > 20) {
   1715 		printf("uhci_device_request: before transfer\n");
   1716 		uhci_dump_tds(setup);
   1717 	}
   1718 #endif
   1719 
   1720 	/* Set up interrupt info. */
   1721 	ii->reqh = reqh;
   1722 	ii->stdstart = setup;
   1723 	ii->stdend = stat;
   1724 #ifdef DIAGNOSTIC
   1725 	ii->isdone = 0;
   1726 #endif
   1727 
   1728 	sqh->elink = setup;
   1729 	sqh->qh.qh_elink = LE(setup->physaddr);
   1730 	sqh->intr_info = ii;
   1731 
   1732 	s = splusb();
   1733 	uhci_add_ctrl(sc, sqh);
   1734 	LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
   1735 #ifdef USB_DEBUG
   1736 	if (uhcidebug > 12) {
   1737 		uhci_soft_td_t *std;
   1738 		uhci_soft_qh_t *xqh;
   1739 		uhci_soft_qh_t *sxqh;
   1740 		int maxqh = 0;
   1741 		uhci_physaddr_t link;
   1742 		printf("uhci_enter_ctl_q: follow from [0]\n");
   1743 		for (std = sc->sc_vframes[0].htd, link = 0;
   1744 		     (link & UHCI_PTR_Q) == 0;
   1745 		     std = std->link.std) {
   1746 			link = LE(std->td.td_link);
   1747 			uhci_dump_td(std);
   1748 		}
   1749 		for (sxqh = xqh = (uhci_soft_qh_t *)std;
   1750 		     xqh;
   1751 		     xqh = (maxqh++ == 5 || xqh->hlink==sxqh ||
   1752                             xqh->hlink==xqh ? NULL : xqh->hlink)) {
   1753 			uhci_dump_qh(xqh);
   1754 			uhci_dump_qh(sxqh);
   1755 		}
   1756 		printf("Enqueued QH:\n");
   1757 		uhci_dump_qh(sqh);
   1758 		uhci_dump_tds(sqh->elink);
   1759 	}
   1760 #endif
   1761 	if (reqh->timeout && !sc->sc_bus.use_polling) {
   1762 		usb_timeout(uhci_timeout, ii,
   1763                             MS_TO_TICKS(reqh->timeout), ii->timeout_handle);
   1764 	}
   1765 	splx(s);
   1766 
   1767 	return (USBD_NORMAL_COMPLETION);
   1768 
   1769  ret2:
   1770 	if (len != 0)
   1771 		usb_freemem(sc->sc_dmatag, dmap);
   1772  ret1:
   1773 	return (r);
   1774 }
   1775 
   1776 usbd_status
   1777 uhci_device_isoc_transfer(reqh)
   1778 	usbd_request_handle reqh;
   1779 {
   1780 	struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
   1781 #ifdef USB_DEBUG
   1782 	usbd_device_handle dev = upipe->pipe.device;
   1783 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1784 #endif
   1785 
   1786 	DPRINTFN(1,("uhci_device_isoc_transfer: sc=%p\n", sc));
   1787 	if (upipe->u.iso.bufsize == 0)
   1788 		return (USBD_INVAL);
   1789 
   1790 	/* XXX copy data */
   1791 	return (USBD_XXX);
   1792 }
   1793 
   1794 usbd_status
   1795 uhci_device_isoc_start(reqh)
   1796 	usbd_request_handle reqh;
   1797 {
   1798 	return (USBD_XXX);
   1799 }
   1800 
   1801 void
   1802 uhci_device_isoc_abort(reqh)
   1803 	usbd_request_handle reqh;
   1804 {
   1805 	/* XXX Can't abort this. */
   1806 }
   1807 
   1808 void
   1809 uhci_device_isoc_close(pipe)
   1810 	usbd_pipe_handle pipe;
   1811 {
   1812 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1813 	usbd_device_handle dev = upipe->pipe.device;
   1814 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1815 	struct iso *iso;
   1816 	int i;
   1817 
   1818 	/*
   1819 	 * Make sure all TDs are marked as inactive.
   1820 	 * Wait for completion.
   1821 	 * Unschedule.
   1822 	 * Deallocate.
   1823 	 */
   1824 	iso = &upipe->u.iso;
   1825 
   1826 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
   1827 		iso->stds[i]->td.td_status &= LE(~UHCI_TD_ACTIVE);
   1828 	usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
   1829 
   1830 	uhci_lock_frames(sc);
   1831 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   1832 		uhci_soft_td_t *std, *vstd;
   1833 
   1834 		std = iso->stds[i];
   1835 		for (vstd = sc->sc_vframes[i % UHCI_VFRAMELIST_COUNT].htd;
   1836 		     vstd && vstd->link.std != std;
   1837 		     vstd = vstd->link.std)
   1838 			;
   1839 		if (!vstd) {
   1840 			/*panic*/
   1841 			printf("uhci_device_isoc_close: %p not found\n", std);
   1842 			uhci_unlock_frames(sc);
   1843 			return;
   1844 		}
   1845 		vstd->link = std->link;
   1846 		vstd->td.td_link = std->td.td_link;
   1847 		uhci_free_std(sc, std);
   1848 	}
   1849 	uhci_unlock_frames(sc);
   1850 
   1851 	for (i = 0; i < iso->nbuf; i++)
   1852 		usb_freemem(sc->sc_dmatag, &iso->bufs[i]);
   1853 	free(iso->stds, M_USBHC);
   1854 	free(iso->bufs, M_USBHC);
   1855 
   1856 	/* XXX what else? */
   1857 }
   1858 
   1859 usbd_status
   1860 uhci_device_isoc_setbuf(pipe, bufsize, nbuf)
   1861 	usbd_pipe_handle pipe;
   1862 	u_int bufsize;
   1863 	u_int nbuf;
   1864 {
   1865 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   1866 	usbd_device_handle dev = upipe->pipe.device;
   1867 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
   1868 	int addr = upipe->pipe.device->address;
   1869 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
   1870 	int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
   1871 	struct iso *iso;
   1872 	int i;
   1873 	usbd_status r;
   1874 
   1875 	/*
   1876 	 * For simplicity the number of buffers must fit nicely in the frame
   1877 	 * list.
   1878 	 */
   1879 	if (UHCI_VFRAMELIST_COUNT % nbuf != 0)
   1880 		return (USBD_INVAL);
   1881 
   1882 	iso = &upipe->u.iso;
   1883 	iso->bufsize = bufsize;
   1884 	iso->nbuf = nbuf;
   1885 
   1886 	/* Allocate memory for buffers. */
   1887 	iso->bufs = malloc(nbuf * sizeof(usb_dma_t), M_USBHC, M_WAITOK);
   1888 	iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
   1889 			   M_USBHC, M_WAITOK);
   1890 
   1891 	for (i = 0; i < nbuf; i++) {
   1892 		r = usb_allocmem(sc->sc_dmatag, bufsize, 0, &iso->bufs[i]);
   1893 		if (r != USBD_NORMAL_COMPLETION) {
   1894 			nbuf = i;
   1895 			goto bad1;
   1896 		}
   1897 	}
   1898 
   1899 	/* Allocate the TDs. */
   1900 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   1901 		iso->stds[i] = uhci_alloc_std(sc);
   1902 		if (iso->stds[i] == 0)
   1903 			goto bad2;
   1904 	}
   1905 
   1906 	/* XXX check schedule */
   1907 
   1908 	/* XXX interrupts */
   1909 
   1910 	/* Insert TDs into schedule, all marked inactive. */
   1911 	uhci_lock_frames(sc);
   1912 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
   1913 		uhci_soft_td_t *std, *vstd;
   1914 
   1915 		std = iso->stds[i];
   1916 		std->td.td_status = LE(UHCI_TD_IOS);	/* iso, inactive */
   1917 		std->td.td_token =
   1918 		    LE(rd ? UHCI_TD_IN (0, endpt, addr, 0) :
   1919 			    UHCI_TD_OUT(0, endpt, addr, 0));
   1920 		std->td.td_buffer = LE(DMAADDR(&iso->bufs[i % nbuf]));
   1921 
   1922 		vstd = sc->sc_vframes[i % UHCI_VFRAMELIST_COUNT].htd;
   1923 		std->link = vstd->link;
   1924 		std->td.td_link = vstd->td.td_link;
   1925 		vstd->link.std = std;
   1926 		vstd->td.td_link = LE(std->physaddr);
   1927 	}
   1928 	uhci_unlock_frames(sc);
   1929 
   1930 	return (USBD_NORMAL_COMPLETION);
   1931 
   1932  bad2:
   1933 	while (--i >= 0)
   1934 		uhci_free_std(sc, iso->stds[i]);
   1935  bad1:
   1936 	for (i = 0; i < nbuf; i++)
   1937 		usb_freemem(sc->sc_dmatag, &iso->bufs[i]);
   1938 	free(iso->stds, M_USBHC);
   1939 	free(iso->bufs, M_USBHC);
   1940 	return (USBD_NOMEM);
   1941 }
   1942 
   1943 void
   1944 uhci_device_isoc_done(reqh)
   1945 	usbd_request_handle reqh;
   1946 {
   1947 	/*uhci_intr_info_t *ii = v;*/
   1948 }
   1949 
   1950 void
   1951 uhci_device_intr_done(reqh)
   1952 	usbd_request_handle reqh;
   1953 {
   1954 	uhci_intr_info_t *ii = reqh->hcpriv;
   1955 	uhci_softc_t *sc = ii->sc;
   1956 	struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
   1957 	usb_dma_t *dma;
   1958 	uhci_soft_qh_t *sqh;
   1959 	int i, npoll;
   1960 
   1961 	DPRINTFN(5, ("uhci_intr_done: length=%d\n", reqh->actlen));
   1962 
   1963 	dma = &upipe->u.intr.datadma;
   1964 	memcpy(reqh->buffer, KERNADDR(dma), reqh->actlen);
   1965 	npoll = upipe->u.intr.npoll;
   1966 	for(i = 0; i < npoll; i++) {
   1967 		sqh = upipe->u.intr.qhs[i];
   1968 		sqh->elink = 0;
   1969 		sqh->qh.qh_elink = LE(UHCI_PTR_T);
   1970 	}
   1971 	uhci_free_std_chain(sc, ii->stdstart, 0);
   1972 
   1973 	/* XXX Wasteful. */
   1974 	if (reqh->pipe->repeat) {
   1975 		uhci_soft_td_t *xfer, *xferend;
   1976 
   1977 		/* This alloc cannot fail since we freed the chain above. */
   1978 		uhci_alloc_std_chain(upipe, sc, reqh->length, 1,
   1979 				     reqh->flags & USBD_SHORT_XFER_OK,
   1980 				     dma, &xfer, &xferend);
   1981 		xferend->td.td_status |= LE(UHCI_TD_IOC);
   1982 
   1983 #ifdef USB_DEBUG
   1984 		if (uhcidebug > 10) {
   1985 			printf("uhci_device_intr_done: xfer(1)\n");
   1986 			uhci_dump_tds(xfer);
   1987 			uhci_dump_qh(upipe->u.intr.qhs[0]);
   1988 		}
   1989 #endif
   1990 
   1991 		ii->stdstart = xfer;
   1992 		ii->stdend = xferend;
   1993 #ifdef DIAGNOSTIC
   1994 		ii->isdone = 0;
   1995 #endif
   1996 		for (i = 0; i < npoll; i++) {
   1997 			sqh = upipe->u.intr.qhs[i];
   1998 			sqh->elink = xfer;
   1999 			sqh->qh.qh_elink = LE(xfer->physaddr);
   2000 		}
   2001 	} else {
   2002 		usb_freemem(sc->sc_dmatag, dma);
   2003 		ii->stdstart = 0;	/* mark as inactive */
   2004 	}
   2005 }
   2006 
   2007 /* Deallocate request data structures */
   2008 void
   2009 uhci_device_ctrl_done(reqh)
   2010 	usbd_request_handle reqh;
   2011 {
   2012 	uhci_intr_info_t *ii = reqh->hcpriv;
   2013 	uhci_softc_t *sc = ii->sc;
   2014 	struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
   2015 	u_int len = upipe->u.ctl.length;
   2016 	usb_dma_t *dma;
   2017 
   2018 #ifdef DIAGNOSTIC
   2019 	if (!reqh->isreq)
   2020 		panic("uhci_ctrl_done: not a request\n");
   2021 #endif
   2022 
   2023 	LIST_REMOVE(ii, list);	/* remove from active list */
   2024 
   2025 	uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
   2026 
   2027 	if (len != 0) {
   2028 		dma = &upipe->u.ctl.datadma;
   2029 		if (reqh->request.bmRequestType & UT_READ)
   2030 			memcpy(reqh->buffer, KERNADDR(dma), len);
   2031 		uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
   2032 		usb_freemem(sc->sc_dmatag, dma);
   2033 	}
   2034 	DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", reqh->actlen));
   2035 }
   2036 
   2037 /* Deallocate request data structures */
   2038 void
   2039 uhci_device_bulk_done(reqh)
   2040 	usbd_request_handle reqh;
   2041 {
   2042 	uhci_intr_info_t *ii = reqh->hcpriv;
   2043 	uhci_softc_t *sc = ii->sc;
   2044 	struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
   2045 	u_int datalen = upipe->u.bulk.length;
   2046 	usb_dma_t *dma;
   2047 
   2048 	LIST_REMOVE(ii, list);	/* remove from active list */
   2049 
   2050 	uhci_remove_bulk(sc, upipe->u.bulk.sqh);
   2051 
   2052 	/* copy the data from dma memory to userland storage */
   2053 	dma = &upipe->u.bulk.datadma;
   2054 	if (upipe->u.bulk.isread)
   2055 		memcpy(reqh->buffer, KERNADDR(dma), datalen);
   2056 	uhci_free_std_chain(sc, ii->stdstart, 0);
   2057 	usb_freemem(sc->sc_dmatag, dma);
   2058 
   2059 	DPRINTFN(4, ("uhci_bulk_done: length=%d\n", reqh->actlen));
   2060 }
   2061 
   2062 /* Add interrupt QH, called with vflock. */
   2063 void
   2064 uhci_add_intr(sc, n, sqh)
   2065 	uhci_softc_t *sc;
   2066 	int n;
   2067 	uhci_soft_qh_t *sqh;
   2068 {
   2069 	struct uhci_vframe *vf = &sc->sc_vframes[n];
   2070 	uhci_soft_qh_t *eqh;
   2071 
   2072 	DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
   2073 	eqh = vf->eqh;
   2074 	sqh->hlink       = eqh->hlink;
   2075 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
   2076 	eqh->hlink       = sqh;
   2077 	eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
   2078 	vf->eqh = sqh;
   2079 	vf->bandwidth++;
   2080 }
   2081 
   2082 /* Remove interrupt QH, called with vflock. */
   2083 void
   2084 uhci_remove_intr(sc, n, sqh)
   2085 	uhci_softc_t *sc;
   2086 	int n;
   2087 	uhci_soft_qh_t *sqh;
   2088 {
   2089 	struct uhci_vframe *vf = &sc->sc_vframes[n];
   2090 	uhci_soft_qh_t *pqh;
   2091 
   2092 	DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
   2093 
   2094 	for (pqh = vf->hqh; pqh->hlink != sqh; pqh = pqh->hlink)
   2095 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
   2096 		if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
   2097 			printf("uhci_remove_intr: QH not found\n");
   2098 			return;
   2099 		}
   2100 #else
   2101 		;
   2102 #endif
   2103 	pqh->hlink       = sqh->hlink;
   2104 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
   2105 	if (vf->eqh == sqh)
   2106 		vf->eqh = pqh;
   2107 	vf->bandwidth--;
   2108 }
   2109 
   2110 usbd_status
   2111 uhci_device_setintr(sc, upipe, ival)
   2112 	uhci_softc_t *sc;
   2113 	struct uhci_pipe *upipe;
   2114 	int ival;
   2115 {
   2116 	uhci_soft_qh_t *sqh;
   2117 	int i, npoll, s;
   2118 	u_int bestbw, bw, bestoffs, offs;
   2119 
   2120 	DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
   2121 	if (ival == 0) {
   2122 		printf("uhci_setintr: 0 interval\n");
   2123 		return (USBD_INVAL);
   2124 	}
   2125 
   2126 	if (ival > UHCI_VFRAMELIST_COUNT)
   2127 		ival = UHCI_VFRAMELIST_COUNT;
   2128 	npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
   2129 	DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
   2130 
   2131 	upipe->u.intr.npoll = npoll;
   2132 	upipe->u.intr.qhs =
   2133 		malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
   2134 
   2135 	/*
   2136 	 * Figure out which offset in the schedule that has most
   2137 	 * bandwidth left over.
   2138 	 */
   2139 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
   2140 	for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
   2141 		for (bw = i = 0; i < npoll; i++)
   2142 			bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
   2143 		if (bw < bestbw) {
   2144 			bestbw = bw;
   2145 			bestoffs = offs;
   2146 		}
   2147 	}
   2148 	DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
   2149 
   2150 	upipe->iinfo->stdstart = 0;
   2151 	for(i = 0; i < npoll; i++) {
   2152 		upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
   2153 		sqh->elink = 0;
   2154 		sqh->qh.qh_elink = LE(UHCI_PTR_T);
   2155 		sqh->pos = MOD(i * ival + bestoffs);
   2156 		sqh->intr_info = upipe->iinfo;
   2157 	}
   2158 #undef MOD
   2159 
   2160 	s = splusb();
   2161 	LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
   2162 	splx(s);
   2163 
   2164 	uhci_lock_frames(sc);
   2165 	/* Enter QHs into the controller data structures. */
   2166 	for(i = 0; i < npoll; i++)
   2167 		uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos,
   2168 			      upipe->u.intr.qhs[i]);
   2169 	uhci_unlock_frames(sc);
   2170 
   2171 	DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
   2172 	return (USBD_NORMAL_COMPLETION);
   2173 }
   2174 
   2175 /* Open a new pipe. */
   2176 usbd_status
   2177 uhci_open(pipe)
   2178 	usbd_pipe_handle pipe;
   2179 {
   2180 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2181 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2182 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
   2183 	usbd_status r;
   2184 
   2185 	DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
   2186 		     pipe, pipe->device->address,
   2187 		     ed->bEndpointAddress, sc->sc_addr));
   2188 	if (pipe->device->address == sc->sc_addr) {
   2189 		switch (ed->bEndpointAddress) {
   2190 		case USB_CONTROL_ENDPOINT:
   2191 			pipe->methods = &uhci_root_ctrl_methods;
   2192 			break;
   2193 		case UE_DIR_IN | UHCI_INTR_ENDPT:
   2194 			pipe->methods = &uhci_root_intr_methods;
   2195 			break;
   2196 		default:
   2197 			return (USBD_INVAL);
   2198 		}
   2199 	} else {
   2200 		upipe->iinfo = uhci_alloc_intr_info(sc);
   2201 		if (upipe->iinfo == 0)
   2202 			return (USBD_NOMEM);
   2203 		switch (ed->bmAttributes & UE_XFERTYPE) {
   2204 		case UE_CONTROL:
   2205 			pipe->methods = &uhci_device_ctrl_methods;
   2206 			upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
   2207 			if (upipe->u.ctl.sqh == 0)
   2208 				goto bad;
   2209 			upipe->u.ctl.setup = uhci_alloc_std(sc);
   2210 			if (upipe->u.ctl.setup == 0) {
   2211 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2212 				goto bad;
   2213 			}
   2214 			upipe->u.ctl.stat = uhci_alloc_std(sc);
   2215 			if (upipe->u.ctl.stat == 0) {
   2216 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2217 				uhci_free_std(sc, upipe->u.ctl.setup);
   2218 				goto bad;
   2219 			}
   2220 			r = usb_allocmem(sc->sc_dmatag,
   2221 					 sizeof(usb_device_request_t),
   2222 					 0, &upipe->u.ctl.reqdma);
   2223 			if (r != USBD_NORMAL_COMPLETION) {
   2224 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
   2225 				uhci_free_std(sc, upipe->u.ctl.setup);
   2226 				uhci_free_std(sc, upipe->u.ctl.stat);
   2227 				goto bad;
   2228 			}
   2229 			break;
   2230 		case UE_INTERRUPT:
   2231 			pipe->methods = &uhci_device_intr_methods;
   2232 			return (uhci_device_setintr(sc, upipe, ed->bInterval));
   2233 		case UE_ISOCHRONOUS:
   2234 			pipe->methods = &uhci_device_isoc_methods;
   2235 			upipe->u.iso.nbuf = 0;
   2236 			return (USBD_NORMAL_COMPLETION);
   2237 		case UE_BULK:
   2238 			pipe->methods = &uhci_device_bulk_methods;
   2239 			upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
   2240 			if (upipe->u.bulk.sqh == 0)
   2241 				goto bad;
   2242 			break;
   2243 		}
   2244 	}
   2245 	return (USBD_NORMAL_COMPLETION);
   2246 
   2247  bad:
   2248 	uhci_free_intr_info(upipe->iinfo);
   2249 	return (USBD_NOMEM);
   2250 }
   2251 
   2252 /*
   2253  * Data structures and routines to emulate the root hub.
   2254  */
   2255 usb_device_descriptor_t uhci_devd = {
   2256 	USB_DEVICE_DESCRIPTOR_SIZE,
   2257 	UDESC_DEVICE,		/* type */
   2258 	{0x00, 0x01},		/* USB version */
   2259 	UCLASS_HUB,		/* class */
   2260 	USUBCLASS_HUB,		/* subclass */
   2261 	0,			/* protocol */
   2262 	64,			/* max packet */
   2263 	{0},{0},{0x00,0x01},	/* device id */
   2264 	1,2,0,			/* string indicies */
   2265 	1			/* # of configurations */
   2266 };
   2267 
   2268 usb_config_descriptor_t uhci_confd = {
   2269 	USB_CONFIG_DESCRIPTOR_SIZE,
   2270 	UDESC_CONFIG,
   2271 	{USB_CONFIG_DESCRIPTOR_SIZE +
   2272 	 USB_INTERFACE_DESCRIPTOR_SIZE +
   2273 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
   2274 	1,
   2275 	1,
   2276 	0,
   2277 	UC_SELF_POWERED,
   2278 	0			/* max power */
   2279 };
   2280 
   2281 usb_interface_descriptor_t uhci_ifcd = {
   2282 	USB_INTERFACE_DESCRIPTOR_SIZE,
   2283 	UDESC_INTERFACE,
   2284 	0,
   2285 	0,
   2286 	1,
   2287 	UCLASS_HUB,
   2288 	USUBCLASS_HUB,
   2289 	0,
   2290 	0
   2291 };
   2292 
   2293 usb_endpoint_descriptor_t uhci_endpd = {
   2294 	USB_ENDPOINT_DESCRIPTOR_SIZE,
   2295 	UDESC_ENDPOINT,
   2296 	UE_DIR_IN | UHCI_INTR_ENDPT,
   2297 	UE_INTERRUPT,
   2298 	{8},
   2299 	255
   2300 };
   2301 
   2302 usb_hub_descriptor_t uhci_hubd_piix = {
   2303 	USB_HUB_DESCRIPTOR_SIZE,
   2304 	UDESC_HUB,
   2305 	2,
   2306 	{ UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
   2307 	50,			/* power on to power good */
   2308 	0,
   2309 	{ 0x00 },		/* both ports are removable */
   2310 };
   2311 
   2312 int
   2313 uhci_str(p, l, s)
   2314 	usb_string_descriptor_t *p;
   2315 	int l;
   2316 	char *s;
   2317 {
   2318 	int i;
   2319 
   2320 	if (l == 0)
   2321 		return (0);
   2322 	p->bLength = 2 * strlen(s) + 2;
   2323 	if (l == 1)
   2324 		return (1);
   2325 	p->bDescriptorType = UDESC_STRING;
   2326 	l -= 2;
   2327 	for (i = 0; s[i] && l > 1; i++, l -= 2)
   2328 		USETW2(p->bString[i], 0, s[i]);
   2329 	return (2*i+2);
   2330 }
   2331 
   2332 /*
   2333  * Simulate a hardware hub by handling all the necessary requests.
   2334  */
   2335 usbd_status
   2336 uhci_root_ctrl_transfer(reqh)
   2337 	usbd_request_handle reqh;
   2338 {
   2339 	int s;
   2340 	usbd_status r;
   2341 
   2342 	s = splusb();
   2343 	r = usb_insert_transfer(reqh);
   2344 	splx(s);
   2345 	if (r != USBD_NORMAL_COMPLETION)
   2346 		return (r);
   2347 	else
   2348 		return (uhci_root_ctrl_start(reqh));
   2349 }
   2350 
   2351 usbd_status
   2352 uhci_root_ctrl_start(reqh)
   2353 	usbd_request_handle reqh;
   2354 {
   2355 	uhci_softc_t *sc = (uhci_softc_t *)reqh->pipe->device->bus;
   2356 	usb_device_request_t *req;
   2357 	void *buf;
   2358 	int port, x;
   2359 	int len, value, index, status, change, l, totlen = 0;
   2360 	usb_port_status_t ps;
   2361 	usbd_status r;
   2362 
   2363 	if (!reqh->isreq)
   2364 		panic("uhci_root_ctrl_transfer: not a request\n");
   2365 	req = &reqh->request;
   2366 	buf = reqh->buffer;
   2367 
   2368 	DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
   2369 		    req->bmRequestType, req->bRequest));
   2370 
   2371 	len = UGETW(req->wLength);
   2372 	value = UGETW(req->wValue);
   2373 	index = UGETW(req->wIndex);
   2374 #define C(x,y) ((x) | ((y) << 8))
   2375 	switch(C(req->bRequest, req->bmRequestType)) {
   2376 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
   2377 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
   2378 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
   2379 		/*
   2380 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
   2381 		 * for the integrated root hub.
   2382 		 */
   2383 		break;
   2384 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
   2385 		if (len > 0) {
   2386 			*(u_int8_t *)buf = sc->sc_conf;
   2387 			totlen = 1;
   2388 		}
   2389 		break;
   2390 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   2391 		DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
   2392 		switch(value >> 8) {
   2393 		case UDESC_DEVICE:
   2394 			if ((value & 0xff) != 0) {
   2395 				r = USBD_IOERROR;
   2396 				goto ret;
   2397 			}
   2398 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   2399 			USETW(uhci_devd.idVendor, sc->sc_id_vendor);
   2400 			memcpy(buf, &uhci_devd, l);
   2401 			break;
   2402 		case UDESC_CONFIG:
   2403 			if ((value & 0xff) != 0) {
   2404 				r = USBD_IOERROR;
   2405 				goto ret;
   2406 			}
   2407 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
   2408 			memcpy(buf, &uhci_confd, l);
   2409 			buf = (char *)buf + l;
   2410 			len -= l;
   2411 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
   2412 			totlen += l;
   2413 			memcpy(buf, &uhci_ifcd, l);
   2414 			buf = (char *)buf + l;
   2415 			len -= l;
   2416 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
   2417 			totlen += l;
   2418 			memcpy(buf, &uhci_endpd, l);
   2419 			break;
   2420 		case UDESC_STRING:
   2421 			if (len == 0)
   2422 				break;
   2423 			*(u_int8_t *)buf = 0;
   2424 			totlen = 1;
   2425 			switch (value & 0xff) {
   2426 			case 1: /* Vendor */
   2427 				totlen = uhci_str(buf, len, sc->sc_vendor);
   2428 				break;
   2429 			case 2: /* Product */
   2430 				totlen = uhci_str(buf, len, "UHCI root hub");
   2431 				break;
   2432 			}
   2433 			break;
   2434 		default:
   2435 			r = USBD_IOERROR;
   2436 			goto ret;
   2437 		}
   2438 		break;
   2439 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   2440 		if (len > 0) {
   2441 			*(u_int8_t *)buf = 0;
   2442 			totlen = 1;
   2443 		}
   2444 		break;
   2445 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   2446 		if (len > 1) {
   2447 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   2448 			totlen = 2;
   2449 		}
   2450 		break;
   2451 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   2452 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   2453 		if (len > 1) {
   2454 			USETW(((usb_status_t *)buf)->wStatus, 0);
   2455 			totlen = 2;
   2456 		}
   2457 		break;
   2458 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   2459 		if (value >= USB_MAX_DEVICES) {
   2460 			r = USBD_IOERROR;
   2461 			goto ret;
   2462 		}
   2463 		sc->sc_addr = value;
   2464 		break;
   2465 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   2466 		if (value != 0 && value != 1) {
   2467 			r = USBD_IOERROR;
   2468 			goto ret;
   2469 		}
   2470 		sc->sc_conf = value;
   2471 		break;
   2472 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   2473 		break;
   2474 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   2475 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   2476 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   2477 		r = USBD_IOERROR;
   2478 		goto ret;
   2479 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   2480 		break;
   2481 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   2482 		break;
   2483 	/* Hub requests */
   2484 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   2485 		break;
   2486 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   2487 		DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
   2488 			     "port=%d feature=%d\n",
   2489 			     index, value));
   2490 		if (index == 1)
   2491 			port = UHCI_PORTSC1;
   2492 		else if (index == 2)
   2493 			port = UHCI_PORTSC2;
   2494 		else {
   2495 			r = USBD_IOERROR;
   2496 			goto ret;
   2497 		}
   2498 		switch(value) {
   2499 		case UHF_PORT_ENABLE:
   2500 			x = UREAD2(sc, port);
   2501 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
   2502 			break;
   2503 		case UHF_PORT_SUSPEND:
   2504 			x = UREAD2(sc, port);
   2505 			UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
   2506 			break;
   2507 		case UHF_PORT_RESET:
   2508 			x = UREAD2(sc, port);
   2509 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   2510 			break;
   2511 		case UHF_C_PORT_CONNECTION:
   2512 			x = UREAD2(sc, port);
   2513 			UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
   2514 			break;
   2515 		case UHF_C_PORT_ENABLE:
   2516 			x = UREAD2(sc, port);
   2517 			UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
   2518 			break;
   2519 		case UHF_C_PORT_OVER_CURRENT:
   2520 			x = UREAD2(sc, port);
   2521 			UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
   2522 			break;
   2523 		case UHF_C_PORT_RESET:
   2524 			sc->sc_isreset = 0;
   2525 			r = USBD_NORMAL_COMPLETION;
   2526 			goto ret;
   2527 		case UHF_PORT_CONNECTION:
   2528 		case UHF_PORT_OVER_CURRENT:
   2529 		case UHF_PORT_POWER:
   2530 		case UHF_PORT_LOW_SPEED:
   2531 		case UHF_C_PORT_SUSPEND:
   2532 		default:
   2533 			r = USBD_IOERROR;
   2534 			goto ret;
   2535 		}
   2536 		break;
   2537 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
   2538 		if (index == 1)
   2539 			port = UHCI_PORTSC1;
   2540 		else if (index == 2)
   2541 			port = UHCI_PORTSC2;
   2542 		else {
   2543 			r = USBD_IOERROR;
   2544 			goto ret;
   2545 		}
   2546 		if (len > 0) {
   2547 			*(u_int8_t *)buf =
   2548 				(UREAD2(sc, port) & UHCI_PORTSC_LS) >>
   2549 				UHCI_PORTSC_LS_SHIFT;
   2550 			totlen = 1;
   2551 		}
   2552 		break;
   2553 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   2554 		if (value != 0) {
   2555 			r = USBD_IOERROR;
   2556 			goto ret;
   2557 		}
   2558 		l = min(len, USB_HUB_DESCRIPTOR_SIZE);
   2559 		totlen = l;
   2560 		memcpy(buf, &uhci_hubd_piix, l);
   2561 		break;
   2562 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   2563 		if (len != 4) {
   2564 			r = USBD_IOERROR;
   2565 			goto ret;
   2566 		}
   2567 		memset(buf, 0, len);
   2568 		totlen = len;
   2569 		break;
   2570 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   2571 		if (index == 1)
   2572 			port = UHCI_PORTSC1;
   2573 		else if (index == 2)
   2574 			port = UHCI_PORTSC2;
   2575 		else {
   2576 			r = USBD_IOERROR;
   2577 			goto ret;
   2578 		}
   2579 		if (len != 4) {
   2580 			r = USBD_IOERROR;
   2581 			goto ret;
   2582 		}
   2583 		x = UREAD2(sc, port);
   2584 		status = change = 0;
   2585 		if (x & UHCI_PORTSC_CCS  )
   2586 			status |= UPS_CURRENT_CONNECT_STATUS;
   2587 		if (x & UHCI_PORTSC_CSC  )
   2588 			change |= UPS_C_CONNECT_STATUS;
   2589 		if (x & UHCI_PORTSC_PE   )
   2590 			status |= UPS_PORT_ENABLED;
   2591 		if (x & UHCI_PORTSC_POEDC)
   2592 			change |= UPS_C_PORT_ENABLED;
   2593 		if (x & UHCI_PORTSC_OCI  )
   2594 			status |= UPS_OVERCURRENT_INDICATOR;
   2595 		if (x & UHCI_PORTSC_OCIC )
   2596 			change |= UPS_C_OVERCURRENT_INDICATOR;
   2597 		if (x & UHCI_PORTSC_SUSP )
   2598 			status |= UPS_SUSPEND;
   2599 		if (x & UHCI_PORTSC_LSDA )
   2600 			status |= UPS_LOW_SPEED;
   2601 		status |= UPS_PORT_POWER;
   2602 		if (sc->sc_isreset)
   2603 			change |= UPS_C_PORT_RESET;
   2604 		USETW(ps.wPortStatus, status);
   2605 		USETW(ps.wPortChange, change);
   2606 		l = min(len, sizeof ps);
   2607 		memcpy(buf, &ps, l);
   2608 		totlen = l;
   2609 		break;
   2610 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   2611 		r = USBD_IOERROR;
   2612 		goto ret;
   2613 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   2614 		break;
   2615 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   2616 		if (index == 1)
   2617 			port = UHCI_PORTSC1;
   2618 		else if (index == 2)
   2619 			port = UHCI_PORTSC2;
   2620 		else {
   2621 			r = USBD_IOERROR;
   2622 			goto ret;
   2623 		}
   2624 		switch(value) {
   2625 		case UHF_PORT_ENABLE:
   2626 			x = UREAD2(sc, port);
   2627 			UWRITE2(sc, port, x | UHCI_PORTSC_PE);
   2628 			break;
   2629 		case UHF_PORT_SUSPEND:
   2630 			x = UREAD2(sc, port);
   2631 			UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
   2632 			break;
   2633 		case UHF_PORT_RESET:
   2634 			x = UREAD2(sc, port);
   2635 			UWRITE2(sc, port, x | UHCI_PORTSC_PR);
   2636 			usb_delay_ms(&sc->sc_bus, 10);
   2637 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
   2638 			delay(100);
   2639 			x = UREAD2(sc, port);
   2640 			UWRITE2(sc, port, x  | UHCI_PORTSC_PE);
   2641 			delay(100);
   2642 			DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
   2643 				    index, UREAD2(sc, port)));
   2644 			sc->sc_isreset = 1;
   2645 			break;
   2646 		case UHF_C_PORT_CONNECTION:
   2647 		case UHF_C_PORT_ENABLE:
   2648 		case UHF_C_PORT_OVER_CURRENT:
   2649 		case UHF_PORT_CONNECTION:
   2650 		case UHF_PORT_OVER_CURRENT:
   2651 		case UHF_PORT_POWER:
   2652 		case UHF_PORT_LOW_SPEED:
   2653 		case UHF_C_PORT_SUSPEND:
   2654 		case UHF_C_PORT_RESET:
   2655 		default:
   2656 			r = USBD_IOERROR;
   2657 			goto ret;
   2658 		}
   2659 		break;
   2660 	default:
   2661 		r = USBD_IOERROR;
   2662 		goto ret;
   2663 	}
   2664 	reqh->actlen = totlen;
   2665 	r = USBD_NORMAL_COMPLETION;
   2666  ret:
   2667 	reqh->status = r;
   2668 	reqh->hcpriv = 0;
   2669 	usb_transfer_complete(reqh);
   2670 	return (USBD_IN_PROGRESS);
   2671 }
   2672 
   2673 /* Abort a root control request. */
   2674 void
   2675 uhci_root_ctrl_abort(reqh)
   2676 	usbd_request_handle reqh;
   2677 {
   2678 	/* Nothing to do, all transfers are syncronous. */
   2679 }
   2680 
   2681 /* Close the root pipe. */
   2682 void
   2683 uhci_root_ctrl_close(pipe)
   2684 	usbd_pipe_handle pipe;
   2685 {
   2686 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2687 
   2688 	sc->sc_has_timo = 0;
   2689 	DPRINTF(("uhci_root_ctrl_close\n"));
   2690 }
   2691 
   2692 /* Abort a root interrupt request. */
   2693 void
   2694 uhci_root_intr_abort(reqh)
   2695 	usbd_request_handle reqh;
   2696 {
   2697 	uhci_softc_t *sc = (uhci_softc_t *)reqh->pipe->device->bus;
   2698 
   2699 	usb_untimeout(uhci_timo, reqh, reqh->timo_handle);
   2700 	sc->sc_has_timo = 0;
   2701 }
   2702 
   2703 usbd_status
   2704 uhci_root_intr_transfer(reqh)
   2705 	usbd_request_handle reqh;
   2706 {
   2707 	int s;
   2708 	usbd_status r;
   2709 
   2710 	s = splusb();
   2711 	r = usb_insert_transfer(reqh);
   2712 	splx(s);
   2713 	if (r != USBD_NORMAL_COMPLETION)
   2714 		return (r);
   2715 	else
   2716 		return (uhci_root_intr_start(reqh));
   2717 }
   2718 
   2719 /* Start a transfer on the root interrupt pipe */
   2720 usbd_status
   2721 uhci_root_intr_start(reqh)
   2722 	usbd_request_handle reqh;
   2723 {
   2724 	usbd_pipe_handle pipe = reqh->pipe;
   2725 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2726 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
   2727 	usb_dma_t *dmap;
   2728 	usbd_status r;
   2729 	int len;
   2730 
   2731 	DPRINTFN(3, ("uhci_root_intr_transfer: reqh=%p buf=%p len=%d "
   2732 		     "flags=%d\n",
   2733 		     reqh, reqh->buffer, reqh->length, reqh->flags));
   2734 
   2735 	len = reqh->length;
   2736 	dmap = &upipe->u.intr.datadma;
   2737 	if (len == 0)
   2738 		return (USBD_INVAL); /* XXX should it be? */
   2739 
   2740 	r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
   2741 	if (r != USBD_NORMAL_COMPLETION)
   2742 		return (r);
   2743 
   2744 	sc->sc_ival = MS_TO_TICKS(reqh->pipe->endpoint->edesc->bInterval);
   2745 	usb_timeout(uhci_timo, reqh, sc->sc_ival, reqh->timo_handle);
   2746 	sc->sc_has_timo = reqh;
   2747 	return (USBD_IN_PROGRESS);
   2748 }
   2749 
   2750 /* Close the root interrupt pipe. */
   2751 void
   2752 uhci_root_intr_close(pipe)
   2753 	usbd_pipe_handle pipe;
   2754 {
   2755 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
   2756 
   2757 	usb_untimeout(uhci_timo, pipe->intrreqh, pipe->intrreqh->timo_handle);
   2758 	sc->sc_has_timo = 0;
   2759 	DPRINTF(("uhci_root_intr_close\n"));
   2760 }
   2761 
   2762