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