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