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